1 /******************************************************************************* 2 * Filename: target_core_device.c (based on iscsi_target_device.c) 3 * 4 * This file contains the iSCSI Virtual Device and Disk Transport 5 * agnostic related functions. 6 * 7 * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc. 8 * Copyright (c) 2005-2006 SBE, Inc. All Rights Reserved. 9 * Copyright (c) 2007-2010 Rising Tide Systems 10 * Copyright (c) 2008-2010 Linux-iSCSI.org 11 * 12 * Nicholas A. Bellinger <nab@kernel.org> 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or 17 * (at your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 * 24 * You should have received a copy of the GNU General Public License 25 * along with this program; if not, write to the Free Software 26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 27 * 28 ******************************************************************************/ 29 30 #include <linux/net.h> 31 #include <linux/string.h> 32 #include <linux/delay.h> 33 #include <linux/timer.h> 34 #include <linux/slab.h> 35 #include <linux/spinlock.h> 36 #include <linux/kthread.h> 37 #include <linux/in.h> 38 #include <net/sock.h> 39 #include <net/tcp.h> 40 #include <scsi/scsi.h> 41 42 #include <target/target_core_base.h> 43 #include <target/target_core_device.h> 44 #include <target/target_core_tpg.h> 45 #include <target/target_core_transport.h> 46 #include <target/target_core_fabric_ops.h> 47 48 #include "target_core_alua.h" 49 #include "target_core_hba.h" 50 #include "target_core_pr.h" 51 #include "target_core_ua.h" 52 53 static void se_dev_start(struct se_device *dev); 54 static void se_dev_stop(struct se_device *dev); 55 56 int transport_get_lun_for_cmd( 57 struct se_cmd *se_cmd, 58 unsigned char *cdb, 59 u32 unpacked_lun) 60 { 61 struct se_dev_entry *deve; 62 struct se_lun *se_lun = NULL; 63 struct se_session *se_sess = SE_SESS(se_cmd); 64 unsigned long flags; 65 int read_only = 0; 66 67 spin_lock_irq(&SE_NODE_ACL(se_sess)->device_list_lock); 68 deve = se_cmd->se_deve = 69 &SE_NODE_ACL(se_sess)->device_list[unpacked_lun]; 70 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) { 71 if (se_cmd) { 72 deve->total_cmds++; 73 deve->total_bytes += se_cmd->data_length; 74 75 if (se_cmd->data_direction == DMA_TO_DEVICE) { 76 if (deve->lun_flags & 77 TRANSPORT_LUNFLAGS_READ_ONLY) { 78 read_only = 1; 79 goto out; 80 } 81 deve->write_bytes += se_cmd->data_length; 82 } else if (se_cmd->data_direction == 83 DMA_FROM_DEVICE) { 84 deve->read_bytes += se_cmd->data_length; 85 } 86 } 87 deve->deve_cmds++; 88 89 se_lun = se_cmd->se_lun = deve->se_lun; 90 se_cmd->pr_res_key = deve->pr_res_key; 91 se_cmd->orig_fe_lun = unpacked_lun; 92 se_cmd->se_orig_obj_ptr = SE_LUN(se_cmd)->lun_se_dev; 93 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD; 94 } 95 out: 96 spin_unlock_irq(&SE_NODE_ACL(se_sess)->device_list_lock); 97 98 if (!se_lun) { 99 if (read_only) { 100 se_cmd->scsi_sense_reason = TCM_WRITE_PROTECTED; 101 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; 102 printk("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN" 103 " Access for 0x%08x\n", 104 CMD_TFO(se_cmd)->get_fabric_name(), 105 unpacked_lun); 106 return -1; 107 } else { 108 /* 109 * Use the se_portal_group->tpg_virt_lun0 to allow for 110 * REPORT_LUNS, et al to be returned when no active 111 * MappedLUN=0 exists for this Initiator Port. 112 */ 113 if (unpacked_lun != 0) { 114 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN; 115 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; 116 printk("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN" 117 " Access for 0x%08x\n", 118 CMD_TFO(se_cmd)->get_fabric_name(), 119 unpacked_lun); 120 return -1; 121 } 122 /* 123 * Force WRITE PROTECT for virtual LUN 0 124 */ 125 if ((se_cmd->data_direction != DMA_FROM_DEVICE) && 126 (se_cmd->data_direction != DMA_NONE)) { 127 se_cmd->scsi_sense_reason = TCM_WRITE_PROTECTED; 128 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; 129 return -1; 130 } 131 #if 0 132 printk("TARGET_CORE[%s]: Using virtual LUN0! :-)\n", 133 CMD_TFO(se_cmd)->get_fabric_name()); 134 #endif 135 se_lun = se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0; 136 se_cmd->orig_fe_lun = 0; 137 se_cmd->se_orig_obj_ptr = SE_LUN(se_cmd)->lun_se_dev; 138 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD; 139 } 140 } 141 /* 142 * Determine if the struct se_lun is online. 143 */ 144 /* #warning FIXME: Check for LUN_RESET + UNIT Attention */ 145 if (se_dev_check_online(se_lun->lun_se_dev) != 0) { 146 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN; 147 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; 148 return -1; 149 } 150 151 { 152 struct se_device *dev = se_lun->lun_se_dev; 153 spin_lock(&dev->stats_lock); 154 dev->num_cmds++; 155 if (se_cmd->data_direction == DMA_TO_DEVICE) 156 dev->write_bytes += se_cmd->data_length; 157 else if (se_cmd->data_direction == DMA_FROM_DEVICE) 158 dev->read_bytes += se_cmd->data_length; 159 spin_unlock(&dev->stats_lock); 160 } 161 162 /* 163 * Add the iscsi_cmd_t to the struct se_lun's cmd list. This list is used 164 * for tracking state of struct se_cmds during LUN shutdown events. 165 */ 166 spin_lock_irqsave(&se_lun->lun_cmd_lock, flags); 167 list_add_tail(&se_cmd->se_lun_list, &se_lun->lun_cmd_list); 168 atomic_set(&T_TASK(se_cmd)->transport_lun_active, 1); 169 #if 0 170 printk(KERN_INFO "Adding ITT: 0x%08x to LUN LIST[%d]\n", 171 CMD_TFO(se_cmd)->get_task_tag(se_cmd), se_lun->unpacked_lun); 172 #endif 173 spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags); 174 175 return 0; 176 } 177 EXPORT_SYMBOL(transport_get_lun_for_cmd); 178 179 int transport_get_lun_for_tmr( 180 struct se_cmd *se_cmd, 181 u32 unpacked_lun) 182 { 183 struct se_device *dev = NULL; 184 struct se_dev_entry *deve; 185 struct se_lun *se_lun = NULL; 186 struct se_session *se_sess = SE_SESS(se_cmd); 187 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req; 188 189 spin_lock_irq(&SE_NODE_ACL(se_sess)->device_list_lock); 190 deve = se_cmd->se_deve = 191 &SE_NODE_ACL(se_sess)->device_list[unpacked_lun]; 192 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) { 193 se_lun = se_cmd->se_lun = se_tmr->tmr_lun = deve->se_lun; 194 dev = se_tmr->tmr_dev = se_lun->lun_se_dev; 195 se_cmd->pr_res_key = deve->pr_res_key; 196 se_cmd->orig_fe_lun = unpacked_lun; 197 se_cmd->se_orig_obj_ptr = SE_LUN(se_cmd)->lun_se_dev; 198 /* se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD; */ 199 } 200 spin_unlock_irq(&SE_NODE_ACL(se_sess)->device_list_lock); 201 202 if (!se_lun) { 203 printk(KERN_INFO "TARGET_CORE[%s]: Detected NON_EXISTENT_LUN" 204 " Access for 0x%08x\n", 205 CMD_TFO(se_cmd)->get_fabric_name(), 206 unpacked_lun); 207 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; 208 return -1; 209 } 210 /* 211 * Determine if the struct se_lun is online. 212 */ 213 /* #warning FIXME: Check for LUN_RESET + UNIT Attention */ 214 if (se_dev_check_online(se_lun->lun_se_dev) != 0) { 215 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; 216 return -1; 217 } 218 219 spin_lock(&dev->se_tmr_lock); 220 list_add_tail(&se_tmr->tmr_list, &dev->dev_tmr_list); 221 spin_unlock(&dev->se_tmr_lock); 222 223 return 0; 224 } 225 EXPORT_SYMBOL(transport_get_lun_for_tmr); 226 227 /* 228 * This function is called from core_scsi3_emulate_pro_register_and_move() 229 * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count 230 * when a matching rtpi is found. 231 */ 232 struct se_dev_entry *core_get_se_deve_from_rtpi( 233 struct se_node_acl *nacl, 234 u16 rtpi) 235 { 236 struct se_dev_entry *deve; 237 struct se_lun *lun; 238 struct se_port *port; 239 struct se_portal_group *tpg = nacl->se_tpg; 240 u32 i; 241 242 spin_lock_irq(&nacl->device_list_lock); 243 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { 244 deve = &nacl->device_list[i]; 245 246 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)) 247 continue; 248 249 lun = deve->se_lun; 250 if (!(lun)) { 251 printk(KERN_ERR "%s device entries device pointer is" 252 " NULL, but Initiator has access.\n", 253 TPG_TFO(tpg)->get_fabric_name()); 254 continue; 255 } 256 port = lun->lun_sep; 257 if (!(port)) { 258 printk(KERN_ERR "%s device entries device pointer is" 259 " NULL, but Initiator has access.\n", 260 TPG_TFO(tpg)->get_fabric_name()); 261 continue; 262 } 263 if (port->sep_rtpi != rtpi) 264 continue; 265 266 atomic_inc(&deve->pr_ref_count); 267 smp_mb__after_atomic_inc(); 268 spin_unlock_irq(&nacl->device_list_lock); 269 270 return deve; 271 } 272 spin_unlock_irq(&nacl->device_list_lock); 273 274 return NULL; 275 } 276 277 int core_free_device_list_for_node( 278 struct se_node_acl *nacl, 279 struct se_portal_group *tpg) 280 { 281 struct se_dev_entry *deve; 282 struct se_lun *lun; 283 u32 i; 284 285 if (!nacl->device_list) 286 return 0; 287 288 spin_lock_irq(&nacl->device_list_lock); 289 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { 290 deve = &nacl->device_list[i]; 291 292 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)) 293 continue; 294 295 if (!deve->se_lun) { 296 printk(KERN_ERR "%s device entries device pointer is" 297 " NULL, but Initiator has access.\n", 298 TPG_TFO(tpg)->get_fabric_name()); 299 continue; 300 } 301 lun = deve->se_lun; 302 303 spin_unlock_irq(&nacl->device_list_lock); 304 core_update_device_list_for_node(lun, NULL, deve->mapped_lun, 305 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0); 306 spin_lock_irq(&nacl->device_list_lock); 307 } 308 spin_unlock_irq(&nacl->device_list_lock); 309 310 kfree(nacl->device_list); 311 nacl->device_list = NULL; 312 313 return 0; 314 } 315 316 void core_dec_lacl_count(struct se_node_acl *se_nacl, struct se_cmd *se_cmd) 317 { 318 struct se_dev_entry *deve; 319 320 spin_lock_irq(&se_nacl->device_list_lock); 321 deve = &se_nacl->device_list[se_cmd->orig_fe_lun]; 322 deve->deve_cmds--; 323 spin_unlock_irq(&se_nacl->device_list_lock); 324 325 return; 326 } 327 328 void core_update_device_list_access( 329 u32 mapped_lun, 330 u32 lun_access, 331 struct se_node_acl *nacl) 332 { 333 struct se_dev_entry *deve; 334 335 spin_lock_irq(&nacl->device_list_lock); 336 deve = &nacl->device_list[mapped_lun]; 337 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) { 338 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY; 339 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE; 340 } else { 341 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE; 342 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY; 343 } 344 spin_unlock_irq(&nacl->device_list_lock); 345 346 return; 347 } 348 349 /* core_update_device_list_for_node(): 350 * 351 * 352 */ 353 int core_update_device_list_for_node( 354 struct se_lun *lun, 355 struct se_lun_acl *lun_acl, 356 u32 mapped_lun, 357 u32 lun_access, 358 struct se_node_acl *nacl, 359 struct se_portal_group *tpg, 360 int enable) 361 { 362 struct se_port *port = lun->lun_sep; 363 struct se_dev_entry *deve = &nacl->device_list[mapped_lun]; 364 int trans = 0; 365 /* 366 * If the MappedLUN entry is being disabled, the entry in 367 * port->sep_alua_list must be removed now before clearing the 368 * struct se_dev_entry pointers below as logic in 369 * core_alua_do_transition_tg_pt() depends on these being present. 370 */ 371 if (!(enable)) { 372 /* 373 * deve->se_lun_acl will be NULL for demo-mode created LUNs 374 * that have not been explicitly concerted to MappedLUNs -> 375 * struct se_lun_acl, but we remove deve->alua_port_list from 376 * port->sep_alua_list. This also means that active UAs and 377 * NodeACL context specific PR metadata for demo-mode 378 * MappedLUN *deve will be released below.. 379 */ 380 spin_lock_bh(&port->sep_alua_lock); 381 list_del(&deve->alua_port_list); 382 spin_unlock_bh(&port->sep_alua_lock); 383 } 384 385 spin_lock_irq(&nacl->device_list_lock); 386 if (enable) { 387 /* 388 * Check if the call is handling demo mode -> explict LUN ACL 389 * transition. This transition must be for the same struct se_lun 390 * + mapped_lun that was setup in demo mode.. 391 */ 392 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) { 393 if (deve->se_lun_acl != NULL) { 394 printk(KERN_ERR "struct se_dev_entry->se_lun_acl" 395 " already set for demo mode -> explict" 396 " LUN ACL transition\n"); 397 spin_unlock_irq(&nacl->device_list_lock); 398 return -1; 399 } 400 if (deve->se_lun != lun) { 401 printk(KERN_ERR "struct se_dev_entry->se_lun does" 402 " match passed struct se_lun for demo mode" 403 " -> explict LUN ACL transition\n"); 404 spin_unlock_irq(&nacl->device_list_lock); 405 return -1; 406 } 407 deve->se_lun_acl = lun_acl; 408 trans = 1; 409 } else { 410 deve->se_lun = lun; 411 deve->se_lun_acl = lun_acl; 412 deve->mapped_lun = mapped_lun; 413 deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS; 414 } 415 416 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) { 417 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY; 418 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE; 419 } else { 420 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE; 421 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY; 422 } 423 424 if (trans) { 425 spin_unlock_irq(&nacl->device_list_lock); 426 return 0; 427 } 428 deve->creation_time = get_jiffies_64(); 429 deve->attach_count++; 430 spin_unlock_irq(&nacl->device_list_lock); 431 432 spin_lock_bh(&port->sep_alua_lock); 433 list_add_tail(&deve->alua_port_list, &port->sep_alua_list); 434 spin_unlock_bh(&port->sep_alua_lock); 435 436 return 0; 437 } 438 /* 439 * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE 440 * PR operation to complete. 441 */ 442 spin_unlock_irq(&nacl->device_list_lock); 443 while (atomic_read(&deve->pr_ref_count) != 0) 444 cpu_relax(); 445 spin_lock_irq(&nacl->device_list_lock); 446 /* 447 * Disable struct se_dev_entry LUN ACL mapping 448 */ 449 core_scsi3_ua_release_all(deve); 450 deve->se_lun = NULL; 451 deve->se_lun_acl = NULL; 452 deve->lun_flags = 0; 453 deve->creation_time = 0; 454 deve->attach_count--; 455 spin_unlock_irq(&nacl->device_list_lock); 456 457 core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl); 458 return 0; 459 } 460 461 /* core_clear_lun_from_tpg(): 462 * 463 * 464 */ 465 void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg) 466 { 467 struct se_node_acl *nacl; 468 struct se_dev_entry *deve; 469 u32 i; 470 471 spin_lock_bh(&tpg->acl_node_lock); 472 list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) { 473 spin_unlock_bh(&tpg->acl_node_lock); 474 475 spin_lock_irq(&nacl->device_list_lock); 476 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { 477 deve = &nacl->device_list[i]; 478 if (lun != deve->se_lun) 479 continue; 480 spin_unlock_irq(&nacl->device_list_lock); 481 482 core_update_device_list_for_node(lun, NULL, 483 deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS, 484 nacl, tpg, 0); 485 486 spin_lock_irq(&nacl->device_list_lock); 487 } 488 spin_unlock_irq(&nacl->device_list_lock); 489 490 spin_lock_bh(&tpg->acl_node_lock); 491 } 492 spin_unlock_bh(&tpg->acl_node_lock); 493 494 return; 495 } 496 497 static struct se_port *core_alloc_port(struct se_device *dev) 498 { 499 struct se_port *port, *port_tmp; 500 501 port = kzalloc(sizeof(struct se_port), GFP_KERNEL); 502 if (!(port)) { 503 printk(KERN_ERR "Unable to allocate struct se_port\n"); 504 return NULL; 505 } 506 INIT_LIST_HEAD(&port->sep_alua_list); 507 INIT_LIST_HEAD(&port->sep_list); 508 atomic_set(&port->sep_tg_pt_secondary_offline, 0); 509 spin_lock_init(&port->sep_alua_lock); 510 mutex_init(&port->sep_tg_pt_md_mutex); 511 512 spin_lock(&dev->se_port_lock); 513 if (dev->dev_port_count == 0x0000ffff) { 514 printk(KERN_WARNING "Reached dev->dev_port_count ==" 515 " 0x0000ffff\n"); 516 spin_unlock(&dev->se_port_lock); 517 return NULL; 518 } 519 again: 520 /* 521 * Allocate the next RELATIVE TARGET PORT IDENTIFER for this struct se_device 522 * Here is the table from spc4r17 section 7.7.3.8. 523 * 524 * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field 525 * 526 * Code Description 527 * 0h Reserved 528 * 1h Relative port 1, historically known as port A 529 * 2h Relative port 2, historically known as port B 530 * 3h to FFFFh Relative port 3 through 65 535 531 */ 532 port->sep_rtpi = dev->dev_rpti_counter++; 533 if (!(port->sep_rtpi)) 534 goto again; 535 536 list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) { 537 /* 538 * Make sure RELATIVE TARGET PORT IDENTIFER is unique 539 * for 16-bit wrap.. 540 */ 541 if (port->sep_rtpi == port_tmp->sep_rtpi) 542 goto again; 543 } 544 spin_unlock(&dev->se_port_lock); 545 546 return port; 547 } 548 549 static void core_export_port( 550 struct se_device *dev, 551 struct se_portal_group *tpg, 552 struct se_port *port, 553 struct se_lun *lun) 554 { 555 struct se_subsystem_dev *su_dev = SU_DEV(dev); 556 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL; 557 558 spin_lock(&dev->se_port_lock); 559 spin_lock(&lun->lun_sep_lock); 560 port->sep_tpg = tpg; 561 port->sep_lun = lun; 562 lun->lun_sep = port; 563 spin_unlock(&lun->lun_sep_lock); 564 565 list_add_tail(&port->sep_list, &dev->dev_sep_list); 566 spin_unlock(&dev->se_port_lock); 567 568 if (T10_ALUA(su_dev)->alua_type == SPC3_ALUA_EMULATED) { 569 tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port); 570 if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) { 571 printk(KERN_ERR "Unable to allocate t10_alua_tg_pt" 572 "_gp_member_t\n"); 573 return; 574 } 575 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 576 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, 577 T10_ALUA(su_dev)->default_tg_pt_gp); 578 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); 579 printk(KERN_INFO "%s/%s: Adding to default ALUA Target Port" 580 " Group: alua/default_tg_pt_gp\n", 581 TRANSPORT(dev)->name, TPG_TFO(tpg)->get_fabric_name()); 582 } 583 584 dev->dev_port_count++; 585 port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFER */ 586 } 587 588 /* 589 * Called with struct se_device->se_port_lock spinlock held. 590 */ 591 static void core_release_port(struct se_device *dev, struct se_port *port) 592 __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock) 593 { 594 /* 595 * Wait for any port reference for PR ALL_TG_PT=1 operation 596 * to complete in __core_scsi3_alloc_registration() 597 */ 598 spin_unlock(&dev->se_port_lock); 599 if (atomic_read(&port->sep_tg_pt_ref_cnt)) 600 cpu_relax(); 601 spin_lock(&dev->se_port_lock); 602 603 core_alua_free_tg_pt_gp_mem(port); 604 605 list_del(&port->sep_list); 606 dev->dev_port_count--; 607 kfree(port); 608 609 return; 610 } 611 612 int core_dev_export( 613 struct se_device *dev, 614 struct se_portal_group *tpg, 615 struct se_lun *lun) 616 { 617 struct se_port *port; 618 619 port = core_alloc_port(dev); 620 if (!(port)) 621 return -1; 622 623 lun->lun_se_dev = dev; 624 se_dev_start(dev); 625 626 atomic_inc(&dev->dev_export_obj.obj_access_count); 627 core_export_port(dev, tpg, port, lun); 628 return 0; 629 } 630 631 void core_dev_unexport( 632 struct se_device *dev, 633 struct se_portal_group *tpg, 634 struct se_lun *lun) 635 { 636 struct se_port *port = lun->lun_sep; 637 638 spin_lock(&lun->lun_sep_lock); 639 if (lun->lun_se_dev == NULL) { 640 spin_unlock(&lun->lun_sep_lock); 641 return; 642 } 643 spin_unlock(&lun->lun_sep_lock); 644 645 spin_lock(&dev->se_port_lock); 646 atomic_dec(&dev->dev_export_obj.obj_access_count); 647 core_release_port(dev, port); 648 spin_unlock(&dev->se_port_lock); 649 650 se_dev_stop(dev); 651 lun->lun_se_dev = NULL; 652 } 653 654 int transport_core_report_lun_response(struct se_cmd *se_cmd) 655 { 656 struct se_dev_entry *deve; 657 struct se_lun *se_lun; 658 struct se_session *se_sess = SE_SESS(se_cmd); 659 struct se_task *se_task; 660 unsigned char *buf = (unsigned char *)T_TASK(se_cmd)->t_task_buf; 661 u32 cdb_offset = 0, lun_count = 0, offset = 8; 662 u64 i, lun; 663 664 list_for_each_entry(se_task, &T_TASK(se_cmd)->t_task_list, t_list) 665 break; 666 667 if (!(se_task)) { 668 printk(KERN_ERR "Unable to locate struct se_task for struct se_cmd\n"); 669 return PYX_TRANSPORT_LU_COMM_FAILURE; 670 } 671 672 /* 673 * If no struct se_session pointer is present, this struct se_cmd is 674 * coming via a target_core_mod PASSTHROUGH op, and not through 675 * a $FABRIC_MOD. In that case, report LUN=0 only. 676 */ 677 if (!(se_sess)) { 678 lun = 0; 679 buf[offset++] = ((lun >> 56) & 0xff); 680 buf[offset++] = ((lun >> 48) & 0xff); 681 buf[offset++] = ((lun >> 40) & 0xff); 682 buf[offset++] = ((lun >> 32) & 0xff); 683 buf[offset++] = ((lun >> 24) & 0xff); 684 buf[offset++] = ((lun >> 16) & 0xff); 685 buf[offset++] = ((lun >> 8) & 0xff); 686 buf[offset++] = (lun & 0xff); 687 lun_count = 1; 688 goto done; 689 } 690 691 spin_lock_irq(&SE_NODE_ACL(se_sess)->device_list_lock); 692 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) { 693 deve = &SE_NODE_ACL(se_sess)->device_list[i]; 694 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS)) 695 continue; 696 se_lun = deve->se_lun; 697 /* 698 * We determine the correct LUN LIST LENGTH even once we 699 * have reached the initial allocation length. 700 * See SPC2-R20 7.19. 701 */ 702 lun_count++; 703 if ((cdb_offset + 8) >= se_cmd->data_length) 704 continue; 705 706 lun = cpu_to_be64(CMD_TFO(se_cmd)->pack_lun(deve->mapped_lun)); 707 buf[offset++] = ((lun >> 56) & 0xff); 708 buf[offset++] = ((lun >> 48) & 0xff); 709 buf[offset++] = ((lun >> 40) & 0xff); 710 buf[offset++] = ((lun >> 32) & 0xff); 711 buf[offset++] = ((lun >> 24) & 0xff); 712 buf[offset++] = ((lun >> 16) & 0xff); 713 buf[offset++] = ((lun >> 8) & 0xff); 714 buf[offset++] = (lun & 0xff); 715 cdb_offset += 8; 716 } 717 spin_unlock_irq(&SE_NODE_ACL(se_sess)->device_list_lock); 718 719 /* 720 * See SPC3 r07, page 159. 721 */ 722 done: 723 lun_count *= 8; 724 buf[0] = ((lun_count >> 24) & 0xff); 725 buf[1] = ((lun_count >> 16) & 0xff); 726 buf[2] = ((lun_count >> 8) & 0xff); 727 buf[3] = (lun_count & 0xff); 728 729 return PYX_TRANSPORT_SENT_TO_TRANSPORT; 730 } 731 732 /* se_release_device_for_hba(): 733 * 734 * 735 */ 736 void se_release_device_for_hba(struct se_device *dev) 737 { 738 struct se_hba *hba = dev->se_hba; 739 740 if ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) || 741 (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) || 742 (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) || 743 (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_ACTIVATED) || 744 (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_DEACTIVATED)) 745 se_dev_stop(dev); 746 747 if (dev->dev_ptr) { 748 kthread_stop(dev->process_thread); 749 if (dev->transport->free_device) 750 dev->transport->free_device(dev->dev_ptr); 751 } 752 753 spin_lock(&hba->device_lock); 754 list_del(&dev->dev_list); 755 hba->dev_count--; 756 spin_unlock(&hba->device_lock); 757 758 core_scsi3_free_all_registrations(dev); 759 se_release_vpd_for_dev(dev); 760 761 kfree(dev->dev_status_queue_obj); 762 kfree(dev->dev_queue_obj); 763 kfree(dev); 764 765 return; 766 } 767 768 void se_release_vpd_for_dev(struct se_device *dev) 769 { 770 struct t10_vpd *vpd, *vpd_tmp; 771 772 spin_lock(&DEV_T10_WWN(dev)->t10_vpd_lock); 773 list_for_each_entry_safe(vpd, vpd_tmp, 774 &DEV_T10_WWN(dev)->t10_vpd_list, vpd_list) { 775 list_del(&vpd->vpd_list); 776 kfree(vpd); 777 } 778 spin_unlock(&DEV_T10_WWN(dev)->t10_vpd_lock); 779 780 return; 781 } 782 783 /* se_free_virtual_device(): 784 * 785 * Used for IBLOCK, RAMDISK, and FILEIO Transport Drivers. 786 */ 787 int se_free_virtual_device(struct se_device *dev, struct se_hba *hba) 788 { 789 if (!list_empty(&dev->dev_sep_list)) 790 dump_stack(); 791 792 core_alua_free_lu_gp_mem(dev); 793 se_release_device_for_hba(dev); 794 795 return 0; 796 } 797 798 static void se_dev_start(struct se_device *dev) 799 { 800 struct se_hba *hba = dev->se_hba; 801 802 spin_lock(&hba->device_lock); 803 atomic_inc(&dev->dev_obj.obj_access_count); 804 if (atomic_read(&dev->dev_obj.obj_access_count) == 1) { 805 if (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) { 806 dev->dev_status &= ~TRANSPORT_DEVICE_DEACTIVATED; 807 dev->dev_status |= TRANSPORT_DEVICE_ACTIVATED; 808 } else if (dev->dev_status & 809 TRANSPORT_DEVICE_OFFLINE_DEACTIVATED) { 810 dev->dev_status &= 811 ~TRANSPORT_DEVICE_OFFLINE_DEACTIVATED; 812 dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_ACTIVATED; 813 } 814 } 815 spin_unlock(&hba->device_lock); 816 } 817 818 static void se_dev_stop(struct se_device *dev) 819 { 820 struct se_hba *hba = dev->se_hba; 821 822 spin_lock(&hba->device_lock); 823 atomic_dec(&dev->dev_obj.obj_access_count); 824 if (atomic_read(&dev->dev_obj.obj_access_count) == 0) { 825 if (dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) { 826 dev->dev_status &= ~TRANSPORT_DEVICE_ACTIVATED; 827 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED; 828 } else if (dev->dev_status & 829 TRANSPORT_DEVICE_OFFLINE_ACTIVATED) { 830 dev->dev_status &= ~TRANSPORT_DEVICE_OFFLINE_ACTIVATED; 831 dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_DEACTIVATED; 832 } 833 } 834 spin_unlock(&hba->device_lock); 835 } 836 837 int se_dev_check_online(struct se_device *dev) 838 { 839 int ret; 840 841 spin_lock_irq(&dev->dev_status_lock); 842 ret = ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) || 843 (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED)) ? 0 : 1; 844 spin_unlock_irq(&dev->dev_status_lock); 845 846 return ret; 847 } 848 849 int se_dev_check_shutdown(struct se_device *dev) 850 { 851 int ret; 852 853 spin_lock_irq(&dev->dev_status_lock); 854 ret = (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN); 855 spin_unlock_irq(&dev->dev_status_lock); 856 857 return ret; 858 } 859 860 void se_dev_set_default_attribs( 861 struct se_device *dev, 862 struct se_dev_limits *dev_limits) 863 { 864 struct queue_limits *limits = &dev_limits->limits; 865 866 DEV_ATTRIB(dev)->emulate_dpo = DA_EMULATE_DPO; 867 DEV_ATTRIB(dev)->emulate_fua_write = DA_EMULATE_FUA_WRITE; 868 DEV_ATTRIB(dev)->emulate_fua_read = DA_EMULATE_FUA_READ; 869 DEV_ATTRIB(dev)->emulate_write_cache = DA_EMULATE_WRITE_CACHE; 870 DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL; 871 DEV_ATTRIB(dev)->emulate_tas = DA_EMULATE_TAS; 872 DEV_ATTRIB(dev)->emulate_tpu = DA_EMULATE_TPU; 873 DEV_ATTRIB(dev)->emulate_tpws = DA_EMULATE_TPWS; 874 DEV_ATTRIB(dev)->emulate_reservations = DA_EMULATE_RESERVATIONS; 875 DEV_ATTRIB(dev)->emulate_alua = DA_EMULATE_ALUA; 876 DEV_ATTRIB(dev)->enforce_pr_isids = DA_ENFORCE_PR_ISIDS; 877 /* 878 * The TPU=1 and TPWS=1 settings will be set in TCM/IBLOCK 879 * iblock_create_virtdevice() from struct queue_limits values 880 * if blk_queue_discard()==1 881 */ 882 DEV_ATTRIB(dev)->max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT; 883 DEV_ATTRIB(dev)->max_unmap_block_desc_count = 884 DA_MAX_UNMAP_BLOCK_DESC_COUNT; 885 DEV_ATTRIB(dev)->unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT; 886 DEV_ATTRIB(dev)->unmap_granularity_alignment = 887 DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT; 888 /* 889 * block_size is based on subsystem plugin dependent requirements. 890 */ 891 DEV_ATTRIB(dev)->hw_block_size = limits->logical_block_size; 892 DEV_ATTRIB(dev)->block_size = limits->logical_block_size; 893 /* 894 * max_sectors is based on subsystem plugin dependent requirements. 895 */ 896 DEV_ATTRIB(dev)->hw_max_sectors = limits->max_hw_sectors; 897 DEV_ATTRIB(dev)->max_sectors = limits->max_sectors; 898 /* 899 * Set optimal_sectors from max_sectors, which can be lowered via 900 * configfs. 901 */ 902 DEV_ATTRIB(dev)->optimal_sectors = limits->max_sectors; 903 /* 904 * queue_depth is based on subsystem plugin dependent requirements. 905 */ 906 DEV_ATTRIB(dev)->hw_queue_depth = dev_limits->hw_queue_depth; 907 DEV_ATTRIB(dev)->queue_depth = dev_limits->queue_depth; 908 } 909 910 int se_dev_set_task_timeout(struct se_device *dev, u32 task_timeout) 911 { 912 if (task_timeout > DA_TASK_TIMEOUT_MAX) { 913 printk(KERN_ERR "dev[%p]: Passed task_timeout: %u larger then" 914 " DA_TASK_TIMEOUT_MAX\n", dev, task_timeout); 915 return -1; 916 } else { 917 DEV_ATTRIB(dev)->task_timeout = task_timeout; 918 printk(KERN_INFO "dev[%p]: Set SE Device task_timeout: %u\n", 919 dev, task_timeout); 920 } 921 922 return 0; 923 } 924 925 int se_dev_set_max_unmap_lba_count( 926 struct se_device *dev, 927 u32 max_unmap_lba_count) 928 { 929 DEV_ATTRIB(dev)->max_unmap_lba_count = max_unmap_lba_count; 930 printk(KERN_INFO "dev[%p]: Set max_unmap_lba_count: %u\n", 931 dev, DEV_ATTRIB(dev)->max_unmap_lba_count); 932 return 0; 933 } 934 935 int se_dev_set_max_unmap_block_desc_count( 936 struct se_device *dev, 937 u32 max_unmap_block_desc_count) 938 { 939 DEV_ATTRIB(dev)->max_unmap_block_desc_count = max_unmap_block_desc_count; 940 printk(KERN_INFO "dev[%p]: Set max_unmap_block_desc_count: %u\n", 941 dev, DEV_ATTRIB(dev)->max_unmap_block_desc_count); 942 return 0; 943 } 944 945 int se_dev_set_unmap_granularity( 946 struct se_device *dev, 947 u32 unmap_granularity) 948 { 949 DEV_ATTRIB(dev)->unmap_granularity = unmap_granularity; 950 printk(KERN_INFO "dev[%p]: Set unmap_granularity: %u\n", 951 dev, DEV_ATTRIB(dev)->unmap_granularity); 952 return 0; 953 } 954 955 int se_dev_set_unmap_granularity_alignment( 956 struct se_device *dev, 957 u32 unmap_granularity_alignment) 958 { 959 DEV_ATTRIB(dev)->unmap_granularity_alignment = unmap_granularity_alignment; 960 printk(KERN_INFO "dev[%p]: Set unmap_granularity_alignment: %u\n", 961 dev, DEV_ATTRIB(dev)->unmap_granularity_alignment); 962 return 0; 963 } 964 965 int se_dev_set_emulate_dpo(struct se_device *dev, int flag) 966 { 967 if ((flag != 0) && (flag != 1)) { 968 printk(KERN_ERR "Illegal value %d\n", flag); 969 return -1; 970 } 971 if (TRANSPORT(dev)->dpo_emulated == NULL) { 972 printk(KERN_ERR "TRANSPORT(dev)->dpo_emulated is NULL\n"); 973 return -1; 974 } 975 if (TRANSPORT(dev)->dpo_emulated(dev) == 0) { 976 printk(KERN_ERR "TRANSPORT(dev)->dpo_emulated not supported\n"); 977 return -1; 978 } 979 DEV_ATTRIB(dev)->emulate_dpo = flag; 980 printk(KERN_INFO "dev[%p]: SE Device Page Out (DPO) Emulation" 981 " bit: %d\n", dev, DEV_ATTRIB(dev)->emulate_dpo); 982 return 0; 983 } 984 985 int se_dev_set_emulate_fua_write(struct se_device *dev, int flag) 986 { 987 if ((flag != 0) && (flag != 1)) { 988 printk(KERN_ERR "Illegal value %d\n", flag); 989 return -1; 990 } 991 if (TRANSPORT(dev)->fua_write_emulated == NULL) { 992 printk(KERN_ERR "TRANSPORT(dev)->fua_write_emulated is NULL\n"); 993 return -1; 994 } 995 if (TRANSPORT(dev)->fua_write_emulated(dev) == 0) { 996 printk(KERN_ERR "TRANSPORT(dev)->fua_write_emulated not supported\n"); 997 return -1; 998 } 999 DEV_ATTRIB(dev)->emulate_fua_write = flag; 1000 printk(KERN_INFO "dev[%p]: SE Device Forced Unit Access WRITEs: %d\n", 1001 dev, DEV_ATTRIB(dev)->emulate_fua_write); 1002 return 0; 1003 } 1004 1005 int se_dev_set_emulate_fua_read(struct se_device *dev, int flag) 1006 { 1007 if ((flag != 0) && (flag != 1)) { 1008 printk(KERN_ERR "Illegal value %d\n", flag); 1009 return -1; 1010 } 1011 if (TRANSPORT(dev)->fua_read_emulated == NULL) { 1012 printk(KERN_ERR "TRANSPORT(dev)->fua_read_emulated is NULL\n"); 1013 return -1; 1014 } 1015 if (TRANSPORT(dev)->fua_read_emulated(dev) == 0) { 1016 printk(KERN_ERR "TRANSPORT(dev)->fua_read_emulated not supported\n"); 1017 return -1; 1018 } 1019 DEV_ATTRIB(dev)->emulate_fua_read = flag; 1020 printk(KERN_INFO "dev[%p]: SE Device Forced Unit Access READs: %d\n", 1021 dev, DEV_ATTRIB(dev)->emulate_fua_read); 1022 return 0; 1023 } 1024 1025 int se_dev_set_emulate_write_cache(struct se_device *dev, int flag) 1026 { 1027 if ((flag != 0) && (flag != 1)) { 1028 printk(KERN_ERR "Illegal value %d\n", flag); 1029 return -1; 1030 } 1031 if (TRANSPORT(dev)->write_cache_emulated == NULL) { 1032 printk(KERN_ERR "TRANSPORT(dev)->write_cache_emulated is NULL\n"); 1033 return -1; 1034 } 1035 if (TRANSPORT(dev)->write_cache_emulated(dev) == 0) { 1036 printk(KERN_ERR "TRANSPORT(dev)->write_cache_emulated not supported\n"); 1037 return -1; 1038 } 1039 DEV_ATTRIB(dev)->emulate_write_cache = flag; 1040 printk(KERN_INFO "dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n", 1041 dev, DEV_ATTRIB(dev)->emulate_write_cache); 1042 return 0; 1043 } 1044 1045 int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag) 1046 { 1047 if ((flag != 0) && (flag != 1) && (flag != 2)) { 1048 printk(KERN_ERR "Illegal value %d\n", flag); 1049 return -1; 1050 } 1051 1052 if (atomic_read(&dev->dev_export_obj.obj_access_count)) { 1053 printk(KERN_ERR "dev[%p]: Unable to change SE Device" 1054 " UA_INTRLCK_CTRL while dev_export_obj: %d count" 1055 " exists\n", dev, 1056 atomic_read(&dev->dev_export_obj.obj_access_count)); 1057 return -1; 1058 } 1059 DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl = flag; 1060 printk(KERN_INFO "dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n", 1061 dev, DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl); 1062 1063 return 0; 1064 } 1065 1066 int se_dev_set_emulate_tas(struct se_device *dev, int flag) 1067 { 1068 if ((flag != 0) && (flag != 1)) { 1069 printk(KERN_ERR "Illegal value %d\n", flag); 1070 return -1; 1071 } 1072 1073 if (atomic_read(&dev->dev_export_obj.obj_access_count)) { 1074 printk(KERN_ERR "dev[%p]: Unable to change SE Device TAS while" 1075 " dev_export_obj: %d count exists\n", dev, 1076 atomic_read(&dev->dev_export_obj.obj_access_count)); 1077 return -1; 1078 } 1079 DEV_ATTRIB(dev)->emulate_tas = flag; 1080 printk(KERN_INFO "dev[%p]: SE Device TASK_ABORTED status bit: %s\n", 1081 dev, (DEV_ATTRIB(dev)->emulate_tas) ? "Enabled" : "Disabled"); 1082 1083 return 0; 1084 } 1085 1086 int se_dev_set_emulate_tpu(struct se_device *dev, int flag) 1087 { 1088 if ((flag != 0) && (flag != 1)) { 1089 printk(KERN_ERR "Illegal value %d\n", flag); 1090 return -1; 1091 } 1092 /* 1093 * We expect this value to be non-zero when generic Block Layer 1094 * Discard supported is detected iblock_create_virtdevice(). 1095 */ 1096 if (!(DEV_ATTRIB(dev)->max_unmap_block_desc_count)) { 1097 printk(KERN_ERR "Generic Block Discard not supported\n"); 1098 return -ENOSYS; 1099 } 1100 1101 DEV_ATTRIB(dev)->emulate_tpu = flag; 1102 printk(KERN_INFO "dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n", 1103 dev, flag); 1104 return 0; 1105 } 1106 1107 int se_dev_set_emulate_tpws(struct se_device *dev, int flag) 1108 { 1109 if ((flag != 0) && (flag != 1)) { 1110 printk(KERN_ERR "Illegal value %d\n", flag); 1111 return -1; 1112 } 1113 /* 1114 * We expect this value to be non-zero when generic Block Layer 1115 * Discard supported is detected iblock_create_virtdevice(). 1116 */ 1117 if (!(DEV_ATTRIB(dev)->max_unmap_block_desc_count)) { 1118 printk(KERN_ERR "Generic Block Discard not supported\n"); 1119 return -ENOSYS; 1120 } 1121 1122 DEV_ATTRIB(dev)->emulate_tpws = flag; 1123 printk(KERN_INFO "dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n", 1124 dev, flag); 1125 return 0; 1126 } 1127 1128 int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag) 1129 { 1130 if ((flag != 0) && (flag != 1)) { 1131 printk(KERN_ERR "Illegal value %d\n", flag); 1132 return -1; 1133 } 1134 DEV_ATTRIB(dev)->enforce_pr_isids = flag; 1135 printk(KERN_INFO "dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev, 1136 (DEV_ATTRIB(dev)->enforce_pr_isids) ? "Enabled" : "Disabled"); 1137 return 0; 1138 } 1139 1140 /* 1141 * Note, this can only be called on unexported SE Device Object. 1142 */ 1143 int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth) 1144 { 1145 u32 orig_queue_depth = dev->queue_depth; 1146 1147 if (atomic_read(&dev->dev_export_obj.obj_access_count)) { 1148 printk(KERN_ERR "dev[%p]: Unable to change SE Device TCQ while" 1149 " dev_export_obj: %d count exists\n", dev, 1150 atomic_read(&dev->dev_export_obj.obj_access_count)); 1151 return -1; 1152 } 1153 if (!(queue_depth)) { 1154 printk(KERN_ERR "dev[%p]: Illegal ZERO value for queue" 1155 "_depth\n", dev); 1156 return -1; 1157 } 1158 1159 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) { 1160 if (queue_depth > DEV_ATTRIB(dev)->hw_queue_depth) { 1161 printk(KERN_ERR "dev[%p]: Passed queue_depth: %u" 1162 " exceeds TCM/SE_Device TCQ: %u\n", 1163 dev, queue_depth, 1164 DEV_ATTRIB(dev)->hw_queue_depth); 1165 return -1; 1166 } 1167 } else { 1168 if (queue_depth > DEV_ATTRIB(dev)->queue_depth) { 1169 if (queue_depth > DEV_ATTRIB(dev)->hw_queue_depth) { 1170 printk(KERN_ERR "dev[%p]: Passed queue_depth:" 1171 " %u exceeds TCM/SE_Device MAX" 1172 " TCQ: %u\n", dev, queue_depth, 1173 DEV_ATTRIB(dev)->hw_queue_depth); 1174 return -1; 1175 } 1176 } 1177 } 1178 1179 DEV_ATTRIB(dev)->queue_depth = dev->queue_depth = queue_depth; 1180 if (queue_depth > orig_queue_depth) 1181 atomic_add(queue_depth - orig_queue_depth, &dev->depth_left); 1182 else if (queue_depth < orig_queue_depth) 1183 atomic_sub(orig_queue_depth - queue_depth, &dev->depth_left); 1184 1185 printk(KERN_INFO "dev[%p]: SE Device TCQ Depth changed to: %u\n", 1186 dev, queue_depth); 1187 return 0; 1188 } 1189 1190 int se_dev_set_max_sectors(struct se_device *dev, u32 max_sectors) 1191 { 1192 int force = 0; /* Force setting for VDEVS */ 1193 1194 if (atomic_read(&dev->dev_export_obj.obj_access_count)) { 1195 printk(KERN_ERR "dev[%p]: Unable to change SE Device" 1196 " max_sectors while dev_export_obj: %d count exists\n", 1197 dev, atomic_read(&dev->dev_export_obj.obj_access_count)); 1198 return -1; 1199 } 1200 if (!(max_sectors)) { 1201 printk(KERN_ERR "dev[%p]: Illegal ZERO value for" 1202 " max_sectors\n", dev); 1203 return -1; 1204 } 1205 if (max_sectors < DA_STATUS_MAX_SECTORS_MIN) { 1206 printk(KERN_ERR "dev[%p]: Passed max_sectors: %u less than" 1207 " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, max_sectors, 1208 DA_STATUS_MAX_SECTORS_MIN); 1209 return -1; 1210 } 1211 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) { 1212 if (max_sectors > DEV_ATTRIB(dev)->hw_max_sectors) { 1213 printk(KERN_ERR "dev[%p]: Passed max_sectors: %u" 1214 " greater than TCM/SE_Device max_sectors:" 1215 " %u\n", dev, max_sectors, 1216 DEV_ATTRIB(dev)->hw_max_sectors); 1217 return -1; 1218 } 1219 } else { 1220 if (!(force) && (max_sectors > 1221 DEV_ATTRIB(dev)->hw_max_sectors)) { 1222 printk(KERN_ERR "dev[%p]: Passed max_sectors: %u" 1223 " greater than TCM/SE_Device max_sectors" 1224 ": %u, use force=1 to override.\n", dev, 1225 max_sectors, DEV_ATTRIB(dev)->hw_max_sectors); 1226 return -1; 1227 } 1228 if (max_sectors > DA_STATUS_MAX_SECTORS_MAX) { 1229 printk(KERN_ERR "dev[%p]: Passed max_sectors: %u" 1230 " greater than DA_STATUS_MAX_SECTORS_MAX:" 1231 " %u\n", dev, max_sectors, 1232 DA_STATUS_MAX_SECTORS_MAX); 1233 return -1; 1234 } 1235 } 1236 1237 DEV_ATTRIB(dev)->max_sectors = max_sectors; 1238 printk("dev[%p]: SE Device max_sectors changed to %u\n", 1239 dev, max_sectors); 1240 return 0; 1241 } 1242 1243 int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors) 1244 { 1245 if (atomic_read(&dev->dev_export_obj.obj_access_count)) { 1246 printk(KERN_ERR "dev[%p]: Unable to change SE Device" 1247 " optimal_sectors while dev_export_obj: %d count exists\n", 1248 dev, atomic_read(&dev->dev_export_obj.obj_access_count)); 1249 return -EINVAL; 1250 } 1251 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) { 1252 printk(KERN_ERR "dev[%p]: Passed optimal_sectors cannot be" 1253 " changed for TCM/pSCSI\n", dev); 1254 return -EINVAL; 1255 } 1256 if (optimal_sectors > DEV_ATTRIB(dev)->max_sectors) { 1257 printk(KERN_ERR "dev[%p]: Passed optimal_sectors %u cannot be" 1258 " greater than max_sectors: %u\n", dev, 1259 optimal_sectors, DEV_ATTRIB(dev)->max_sectors); 1260 return -EINVAL; 1261 } 1262 1263 DEV_ATTRIB(dev)->optimal_sectors = optimal_sectors; 1264 printk(KERN_INFO "dev[%p]: SE Device optimal_sectors changed to %u\n", 1265 dev, optimal_sectors); 1266 return 0; 1267 } 1268 1269 int se_dev_set_block_size(struct se_device *dev, u32 block_size) 1270 { 1271 if (atomic_read(&dev->dev_export_obj.obj_access_count)) { 1272 printk(KERN_ERR "dev[%p]: Unable to change SE Device block_size" 1273 " while dev_export_obj: %d count exists\n", dev, 1274 atomic_read(&dev->dev_export_obj.obj_access_count)); 1275 return -1; 1276 } 1277 1278 if ((block_size != 512) && 1279 (block_size != 1024) && 1280 (block_size != 2048) && 1281 (block_size != 4096)) { 1282 printk(KERN_ERR "dev[%p]: Illegal value for block_device: %u" 1283 " for SE device, must be 512, 1024, 2048 or 4096\n", 1284 dev, block_size); 1285 return -1; 1286 } 1287 1288 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) { 1289 printk(KERN_ERR "dev[%p]: Not allowed to change block_size for" 1290 " Physical Device, use for Linux/SCSI to change" 1291 " block_size for underlying hardware\n", dev); 1292 return -1; 1293 } 1294 1295 DEV_ATTRIB(dev)->block_size = block_size; 1296 printk(KERN_INFO "dev[%p]: SE Device block_size changed to %u\n", 1297 dev, block_size); 1298 return 0; 1299 } 1300 1301 struct se_lun *core_dev_add_lun( 1302 struct se_portal_group *tpg, 1303 struct se_hba *hba, 1304 struct se_device *dev, 1305 u32 lun) 1306 { 1307 struct se_lun *lun_p; 1308 u32 lun_access = 0; 1309 1310 if (atomic_read(&dev->dev_access_obj.obj_access_count) != 0) { 1311 printk(KERN_ERR "Unable to export struct se_device while dev_access_obj: %d\n", 1312 atomic_read(&dev->dev_access_obj.obj_access_count)); 1313 return NULL; 1314 } 1315 1316 lun_p = core_tpg_pre_addlun(tpg, lun); 1317 if ((IS_ERR(lun_p)) || !(lun_p)) 1318 return NULL; 1319 1320 if (dev->dev_flags & DF_READ_ONLY) 1321 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY; 1322 else 1323 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE; 1324 1325 if (core_tpg_post_addlun(tpg, lun_p, lun_access, dev) < 0) 1326 return NULL; 1327 1328 printk(KERN_INFO "%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from" 1329 " CORE HBA: %u\n", TPG_TFO(tpg)->get_fabric_name(), 1330 TPG_TFO(tpg)->tpg_get_tag(tpg), lun_p->unpacked_lun, 1331 TPG_TFO(tpg)->get_fabric_name(), hba->hba_id); 1332 /* 1333 * Update LUN maps for dynamically added initiators when 1334 * generate_node_acl is enabled. 1335 */ 1336 if (TPG_TFO(tpg)->tpg_check_demo_mode(tpg)) { 1337 struct se_node_acl *acl; 1338 spin_lock_bh(&tpg->acl_node_lock); 1339 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) { 1340 if (acl->dynamic_node_acl) { 1341 spin_unlock_bh(&tpg->acl_node_lock); 1342 core_tpg_add_node_to_devs(acl, tpg); 1343 spin_lock_bh(&tpg->acl_node_lock); 1344 } 1345 } 1346 spin_unlock_bh(&tpg->acl_node_lock); 1347 } 1348 1349 return lun_p; 1350 } 1351 1352 /* core_dev_del_lun(): 1353 * 1354 * 1355 */ 1356 int core_dev_del_lun( 1357 struct se_portal_group *tpg, 1358 u32 unpacked_lun) 1359 { 1360 struct se_lun *lun; 1361 int ret = 0; 1362 1363 lun = core_tpg_pre_dellun(tpg, unpacked_lun, &ret); 1364 if (!(lun)) 1365 return ret; 1366 1367 core_tpg_post_dellun(tpg, lun); 1368 1369 printk(KERN_INFO "%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from" 1370 " device object\n", TPG_TFO(tpg)->get_fabric_name(), 1371 TPG_TFO(tpg)->tpg_get_tag(tpg), unpacked_lun, 1372 TPG_TFO(tpg)->get_fabric_name()); 1373 1374 return 0; 1375 } 1376 1377 struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun) 1378 { 1379 struct se_lun *lun; 1380 1381 spin_lock(&tpg->tpg_lun_lock); 1382 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) { 1383 printk(KERN_ERR "%s LUN: %u exceeds TRANSPORT_MAX_LUNS" 1384 "_PER_TPG-1: %u for Target Portal Group: %hu\n", 1385 TPG_TFO(tpg)->get_fabric_name(), unpacked_lun, 1386 TRANSPORT_MAX_LUNS_PER_TPG-1, 1387 TPG_TFO(tpg)->tpg_get_tag(tpg)); 1388 spin_unlock(&tpg->tpg_lun_lock); 1389 return NULL; 1390 } 1391 lun = &tpg->tpg_lun_list[unpacked_lun]; 1392 1393 if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) { 1394 printk(KERN_ERR "%s Logical Unit Number: %u is not free on" 1395 " Target Portal Group: %hu, ignoring request.\n", 1396 TPG_TFO(tpg)->get_fabric_name(), unpacked_lun, 1397 TPG_TFO(tpg)->tpg_get_tag(tpg)); 1398 spin_unlock(&tpg->tpg_lun_lock); 1399 return NULL; 1400 } 1401 spin_unlock(&tpg->tpg_lun_lock); 1402 1403 return lun; 1404 } 1405 1406 /* core_dev_get_lun(): 1407 * 1408 * 1409 */ 1410 static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun) 1411 { 1412 struct se_lun *lun; 1413 1414 spin_lock(&tpg->tpg_lun_lock); 1415 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) { 1416 printk(KERN_ERR "%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER" 1417 "_TPG-1: %u for Target Portal Group: %hu\n", 1418 TPG_TFO(tpg)->get_fabric_name(), unpacked_lun, 1419 TRANSPORT_MAX_LUNS_PER_TPG-1, 1420 TPG_TFO(tpg)->tpg_get_tag(tpg)); 1421 spin_unlock(&tpg->tpg_lun_lock); 1422 return NULL; 1423 } 1424 lun = &tpg->tpg_lun_list[unpacked_lun]; 1425 1426 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) { 1427 printk(KERN_ERR "%s Logical Unit Number: %u is not active on" 1428 " Target Portal Group: %hu, ignoring request.\n", 1429 TPG_TFO(tpg)->get_fabric_name(), unpacked_lun, 1430 TPG_TFO(tpg)->tpg_get_tag(tpg)); 1431 spin_unlock(&tpg->tpg_lun_lock); 1432 return NULL; 1433 } 1434 spin_unlock(&tpg->tpg_lun_lock); 1435 1436 return lun; 1437 } 1438 1439 struct se_lun_acl *core_dev_init_initiator_node_lun_acl( 1440 struct se_portal_group *tpg, 1441 u32 mapped_lun, 1442 char *initiatorname, 1443 int *ret) 1444 { 1445 struct se_lun_acl *lacl; 1446 struct se_node_acl *nacl; 1447 1448 if (strlen(initiatorname) > TRANSPORT_IQN_LEN) { 1449 printk(KERN_ERR "%s InitiatorName exceeds maximum size.\n", 1450 TPG_TFO(tpg)->get_fabric_name()); 1451 *ret = -EOVERFLOW; 1452 return NULL; 1453 } 1454 nacl = core_tpg_get_initiator_node_acl(tpg, initiatorname); 1455 if (!(nacl)) { 1456 *ret = -EINVAL; 1457 return NULL; 1458 } 1459 lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL); 1460 if (!(lacl)) { 1461 printk(KERN_ERR "Unable to allocate memory for struct se_lun_acl.\n"); 1462 *ret = -ENOMEM; 1463 return NULL; 1464 } 1465 1466 INIT_LIST_HEAD(&lacl->lacl_list); 1467 lacl->mapped_lun = mapped_lun; 1468 lacl->se_lun_nacl = nacl; 1469 snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname); 1470 1471 return lacl; 1472 } 1473 1474 int core_dev_add_initiator_node_lun_acl( 1475 struct se_portal_group *tpg, 1476 struct se_lun_acl *lacl, 1477 u32 unpacked_lun, 1478 u32 lun_access) 1479 { 1480 struct se_lun *lun; 1481 struct se_node_acl *nacl; 1482 1483 lun = core_dev_get_lun(tpg, unpacked_lun); 1484 if (!(lun)) { 1485 printk(KERN_ERR "%s Logical Unit Number: %u is not active on" 1486 " Target Portal Group: %hu, ignoring request.\n", 1487 TPG_TFO(tpg)->get_fabric_name(), unpacked_lun, 1488 TPG_TFO(tpg)->tpg_get_tag(tpg)); 1489 return -EINVAL; 1490 } 1491 1492 nacl = lacl->se_lun_nacl; 1493 if (!(nacl)) 1494 return -EINVAL; 1495 1496 if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) && 1497 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE)) 1498 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY; 1499 1500 lacl->se_lun = lun; 1501 1502 if (core_update_device_list_for_node(lun, lacl, lacl->mapped_lun, 1503 lun_access, nacl, tpg, 1) < 0) 1504 return -EINVAL; 1505 1506 spin_lock(&lun->lun_acl_lock); 1507 list_add_tail(&lacl->lacl_list, &lun->lun_acl_list); 1508 atomic_inc(&lun->lun_acl_count); 1509 smp_mb__after_atomic_inc(); 1510 spin_unlock(&lun->lun_acl_lock); 1511 1512 printk(KERN_INFO "%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for " 1513 " InitiatorNode: %s\n", TPG_TFO(tpg)->get_fabric_name(), 1514 TPG_TFO(tpg)->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun, 1515 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO", 1516 lacl->initiatorname); 1517 /* 1518 * Check to see if there are any existing persistent reservation APTPL 1519 * pre-registrations that need to be enabled for this LUN ACL.. 1520 */ 1521 core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl); 1522 return 0; 1523 } 1524 1525 /* core_dev_del_initiator_node_lun_acl(): 1526 * 1527 * 1528 */ 1529 int core_dev_del_initiator_node_lun_acl( 1530 struct se_portal_group *tpg, 1531 struct se_lun *lun, 1532 struct se_lun_acl *lacl) 1533 { 1534 struct se_node_acl *nacl; 1535 1536 nacl = lacl->se_lun_nacl; 1537 if (!(nacl)) 1538 return -EINVAL; 1539 1540 spin_lock(&lun->lun_acl_lock); 1541 list_del(&lacl->lacl_list); 1542 atomic_dec(&lun->lun_acl_count); 1543 smp_mb__after_atomic_dec(); 1544 spin_unlock(&lun->lun_acl_lock); 1545 1546 core_update_device_list_for_node(lun, NULL, lacl->mapped_lun, 1547 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0); 1548 1549 lacl->se_lun = NULL; 1550 1551 printk(KERN_INFO "%s_TPG[%hu]_LUN[%u] - Removed ACL for" 1552 " InitiatorNode: %s Mapped LUN: %u\n", 1553 TPG_TFO(tpg)->get_fabric_name(), 1554 TPG_TFO(tpg)->tpg_get_tag(tpg), lun->unpacked_lun, 1555 lacl->initiatorname, lacl->mapped_lun); 1556 1557 return 0; 1558 } 1559 1560 void core_dev_free_initiator_node_lun_acl( 1561 struct se_portal_group *tpg, 1562 struct se_lun_acl *lacl) 1563 { 1564 printk("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s" 1565 " Mapped LUN: %u\n", TPG_TFO(tpg)->get_fabric_name(), 1566 TPG_TFO(tpg)->tpg_get_tag(tpg), 1567 TPG_TFO(tpg)->get_fabric_name(), 1568 lacl->initiatorname, lacl->mapped_lun); 1569 1570 kfree(lacl); 1571 } 1572 1573 int core_dev_setup_virtual_lun0(void) 1574 { 1575 struct se_hba *hba; 1576 struct se_device *dev; 1577 struct se_subsystem_dev *se_dev = NULL; 1578 struct se_subsystem_api *t; 1579 char buf[16]; 1580 int ret; 1581 1582 hba = core_alloc_hba("rd_dr", 0, HBA_FLAGS_INTERNAL_USE); 1583 if (IS_ERR(hba)) 1584 return PTR_ERR(hba); 1585 1586 se_global->g_lun0_hba = hba; 1587 t = hba->transport; 1588 1589 se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL); 1590 if (!(se_dev)) { 1591 printk(KERN_ERR "Unable to allocate memory for" 1592 " struct se_subsystem_dev\n"); 1593 ret = -ENOMEM; 1594 goto out; 1595 } 1596 INIT_LIST_HEAD(&se_dev->g_se_dev_list); 1597 INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list); 1598 spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock); 1599 INIT_LIST_HEAD(&se_dev->t10_reservation.registration_list); 1600 INIT_LIST_HEAD(&se_dev->t10_reservation.aptpl_reg_list); 1601 spin_lock_init(&se_dev->t10_reservation.registration_lock); 1602 spin_lock_init(&se_dev->t10_reservation.aptpl_reg_lock); 1603 INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list); 1604 spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock); 1605 spin_lock_init(&se_dev->se_dev_lock); 1606 se_dev->t10_reservation.pr_aptpl_buf_len = PR_APTPL_BUF_LEN; 1607 se_dev->t10_wwn.t10_sub_dev = se_dev; 1608 se_dev->t10_alua.t10_sub_dev = se_dev; 1609 se_dev->se_dev_attrib.da_sub_dev = se_dev; 1610 se_dev->se_dev_hba = hba; 1611 1612 se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, "virt_lun0"); 1613 if (!(se_dev->se_dev_su_ptr)) { 1614 printk(KERN_ERR "Unable to locate subsystem dependent pointer" 1615 " from allocate_virtdevice()\n"); 1616 ret = -ENOMEM; 1617 goto out; 1618 } 1619 se_global->g_lun0_su_dev = se_dev; 1620 1621 memset(buf, 0, 16); 1622 sprintf(buf, "rd_pages=8"); 1623 t->set_configfs_dev_params(hba, se_dev, buf, sizeof(buf)); 1624 1625 dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr); 1626 if (!(dev) || IS_ERR(dev)) { 1627 ret = -ENOMEM; 1628 goto out; 1629 } 1630 se_dev->se_dev_ptr = dev; 1631 se_global->g_lun0_dev = dev; 1632 1633 return 0; 1634 out: 1635 se_global->g_lun0_su_dev = NULL; 1636 kfree(se_dev); 1637 if (se_global->g_lun0_hba) { 1638 core_delete_hba(se_global->g_lun0_hba); 1639 se_global->g_lun0_hba = NULL; 1640 } 1641 return ret; 1642 } 1643 1644 1645 void core_dev_release_virtual_lun0(void) 1646 { 1647 struct se_hba *hba = se_global->g_lun0_hba; 1648 struct se_subsystem_dev *su_dev = se_global->g_lun0_su_dev; 1649 1650 if (!(hba)) 1651 return; 1652 1653 if (se_global->g_lun0_dev) 1654 se_free_virtual_device(se_global->g_lun0_dev, hba); 1655 1656 kfree(su_dev); 1657 core_delete_hba(hba); 1658 } 1659