1 /******************************************************************************* 2 * Filename: target_core_transport.c 3 * 4 * This file contains the Generic Target Engine Core. 5 * 6 * (c) Copyright 2002-2013 Datera, Inc. 7 * 8 * Nicholas A. Bellinger <nab@kernel.org> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 23 * 24 ******************************************************************************/ 25 26 #include <linux/net.h> 27 #include <linux/delay.h> 28 #include <linux/string.h> 29 #include <linux/timer.h> 30 #include <linux/slab.h> 31 #include <linux/spinlock.h> 32 #include <linux/kthread.h> 33 #include <linux/in.h> 34 #include <linux/cdrom.h> 35 #include <linux/module.h> 36 #include <linux/ratelimit.h> 37 #include <asm/unaligned.h> 38 #include <net/sock.h> 39 #include <net/tcp.h> 40 #include <scsi/scsi.h> 41 #include <scsi/scsi_cmnd.h> 42 #include <scsi/scsi_tcq.h> 43 44 #include <target/target_core_base.h> 45 #include <target/target_core_backend.h> 46 #include <target/target_core_fabric.h> 47 #include <target/target_core_configfs.h> 48 49 #include "target_core_internal.h" 50 #include "target_core_alua.h" 51 #include "target_core_pr.h" 52 #include "target_core_ua.h" 53 54 #define CREATE_TRACE_POINTS 55 #include <trace/events/target.h> 56 57 static struct workqueue_struct *target_completion_wq; 58 static struct kmem_cache *se_sess_cache; 59 struct kmem_cache *se_ua_cache; 60 struct kmem_cache *t10_pr_reg_cache; 61 struct kmem_cache *t10_alua_lu_gp_cache; 62 struct kmem_cache *t10_alua_lu_gp_mem_cache; 63 struct kmem_cache *t10_alua_tg_pt_gp_cache; 64 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache; 65 struct kmem_cache *t10_alua_lba_map_cache; 66 struct kmem_cache *t10_alua_lba_map_mem_cache; 67 68 static void transport_complete_task_attr(struct se_cmd *cmd); 69 static void transport_handle_queue_full(struct se_cmd *cmd, 70 struct se_device *dev); 71 static int transport_put_cmd(struct se_cmd *cmd); 72 static void target_complete_ok_work(struct work_struct *work); 73 74 int init_se_kmem_caches(void) 75 { 76 se_sess_cache = kmem_cache_create("se_sess_cache", 77 sizeof(struct se_session), __alignof__(struct se_session), 78 0, NULL); 79 if (!se_sess_cache) { 80 pr_err("kmem_cache_create() for struct se_session" 81 " failed\n"); 82 goto out; 83 } 84 se_ua_cache = kmem_cache_create("se_ua_cache", 85 sizeof(struct se_ua), __alignof__(struct se_ua), 86 0, NULL); 87 if (!se_ua_cache) { 88 pr_err("kmem_cache_create() for struct se_ua failed\n"); 89 goto out_free_sess_cache; 90 } 91 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache", 92 sizeof(struct t10_pr_registration), 93 __alignof__(struct t10_pr_registration), 0, NULL); 94 if (!t10_pr_reg_cache) { 95 pr_err("kmem_cache_create() for struct t10_pr_registration" 96 " failed\n"); 97 goto out_free_ua_cache; 98 } 99 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache", 100 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp), 101 0, NULL); 102 if (!t10_alua_lu_gp_cache) { 103 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache" 104 " failed\n"); 105 goto out_free_pr_reg_cache; 106 } 107 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache", 108 sizeof(struct t10_alua_lu_gp_member), 109 __alignof__(struct t10_alua_lu_gp_member), 0, NULL); 110 if (!t10_alua_lu_gp_mem_cache) { 111 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_" 112 "cache failed\n"); 113 goto out_free_lu_gp_cache; 114 } 115 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache", 116 sizeof(struct t10_alua_tg_pt_gp), 117 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL); 118 if (!t10_alua_tg_pt_gp_cache) { 119 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" 120 "cache failed\n"); 121 goto out_free_lu_gp_mem_cache; 122 } 123 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create( 124 "t10_alua_tg_pt_gp_mem_cache", 125 sizeof(struct t10_alua_tg_pt_gp_member), 126 __alignof__(struct t10_alua_tg_pt_gp_member), 127 0, NULL); 128 if (!t10_alua_tg_pt_gp_mem_cache) { 129 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" 130 "mem_t failed\n"); 131 goto out_free_tg_pt_gp_cache; 132 } 133 t10_alua_lba_map_cache = kmem_cache_create( 134 "t10_alua_lba_map_cache", 135 sizeof(struct t10_alua_lba_map), 136 __alignof__(struct t10_alua_lba_map), 0, NULL); 137 if (!t10_alua_lba_map_cache) { 138 pr_err("kmem_cache_create() for t10_alua_lba_map_" 139 "cache failed\n"); 140 goto out_free_tg_pt_gp_mem_cache; 141 } 142 t10_alua_lba_map_mem_cache = kmem_cache_create( 143 "t10_alua_lba_map_mem_cache", 144 sizeof(struct t10_alua_lba_map_member), 145 __alignof__(struct t10_alua_lba_map_member), 0, NULL); 146 if (!t10_alua_lba_map_mem_cache) { 147 pr_err("kmem_cache_create() for t10_alua_lba_map_mem_" 148 "cache failed\n"); 149 goto out_free_lba_map_cache; 150 } 151 152 target_completion_wq = alloc_workqueue("target_completion", 153 WQ_MEM_RECLAIM, 0); 154 if (!target_completion_wq) 155 goto out_free_lba_map_mem_cache; 156 157 return 0; 158 159 out_free_lba_map_mem_cache: 160 kmem_cache_destroy(t10_alua_lba_map_mem_cache); 161 out_free_lba_map_cache: 162 kmem_cache_destroy(t10_alua_lba_map_cache); 163 out_free_tg_pt_gp_mem_cache: 164 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); 165 out_free_tg_pt_gp_cache: 166 kmem_cache_destroy(t10_alua_tg_pt_gp_cache); 167 out_free_lu_gp_mem_cache: 168 kmem_cache_destroy(t10_alua_lu_gp_mem_cache); 169 out_free_lu_gp_cache: 170 kmem_cache_destroy(t10_alua_lu_gp_cache); 171 out_free_pr_reg_cache: 172 kmem_cache_destroy(t10_pr_reg_cache); 173 out_free_ua_cache: 174 kmem_cache_destroy(se_ua_cache); 175 out_free_sess_cache: 176 kmem_cache_destroy(se_sess_cache); 177 out: 178 return -ENOMEM; 179 } 180 181 void release_se_kmem_caches(void) 182 { 183 destroy_workqueue(target_completion_wq); 184 kmem_cache_destroy(se_sess_cache); 185 kmem_cache_destroy(se_ua_cache); 186 kmem_cache_destroy(t10_pr_reg_cache); 187 kmem_cache_destroy(t10_alua_lu_gp_cache); 188 kmem_cache_destroy(t10_alua_lu_gp_mem_cache); 189 kmem_cache_destroy(t10_alua_tg_pt_gp_cache); 190 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); 191 kmem_cache_destroy(t10_alua_lba_map_cache); 192 kmem_cache_destroy(t10_alua_lba_map_mem_cache); 193 } 194 195 /* This code ensures unique mib indexes are handed out. */ 196 static DEFINE_SPINLOCK(scsi_mib_index_lock); 197 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX]; 198 199 /* 200 * Allocate a new row index for the entry type specified 201 */ 202 u32 scsi_get_new_index(scsi_index_t type) 203 { 204 u32 new_index; 205 206 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)); 207 208 spin_lock(&scsi_mib_index_lock); 209 new_index = ++scsi_mib_index[type]; 210 spin_unlock(&scsi_mib_index_lock); 211 212 return new_index; 213 } 214 215 void transport_subsystem_check_init(void) 216 { 217 int ret; 218 static int sub_api_initialized; 219 220 if (sub_api_initialized) 221 return; 222 223 ret = request_module("target_core_iblock"); 224 if (ret != 0) 225 pr_err("Unable to load target_core_iblock\n"); 226 227 ret = request_module("target_core_file"); 228 if (ret != 0) 229 pr_err("Unable to load target_core_file\n"); 230 231 ret = request_module("target_core_pscsi"); 232 if (ret != 0) 233 pr_err("Unable to load target_core_pscsi\n"); 234 235 sub_api_initialized = 1; 236 } 237 238 struct se_session *transport_init_session(enum target_prot_op sup_prot_ops) 239 { 240 struct se_session *se_sess; 241 242 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL); 243 if (!se_sess) { 244 pr_err("Unable to allocate struct se_session from" 245 " se_sess_cache\n"); 246 return ERR_PTR(-ENOMEM); 247 } 248 INIT_LIST_HEAD(&se_sess->sess_list); 249 INIT_LIST_HEAD(&se_sess->sess_acl_list); 250 INIT_LIST_HEAD(&se_sess->sess_cmd_list); 251 INIT_LIST_HEAD(&se_sess->sess_wait_list); 252 spin_lock_init(&se_sess->sess_cmd_lock); 253 kref_init(&se_sess->sess_kref); 254 se_sess->sup_prot_ops = sup_prot_ops; 255 256 return se_sess; 257 } 258 EXPORT_SYMBOL(transport_init_session); 259 260 int transport_alloc_session_tags(struct se_session *se_sess, 261 unsigned int tag_num, unsigned int tag_size) 262 { 263 int rc; 264 265 se_sess->sess_cmd_map = kzalloc(tag_num * tag_size, 266 GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); 267 if (!se_sess->sess_cmd_map) { 268 se_sess->sess_cmd_map = vzalloc(tag_num * tag_size); 269 if (!se_sess->sess_cmd_map) { 270 pr_err("Unable to allocate se_sess->sess_cmd_map\n"); 271 return -ENOMEM; 272 } 273 } 274 275 rc = percpu_ida_init(&se_sess->sess_tag_pool, tag_num); 276 if (rc < 0) { 277 pr_err("Unable to init se_sess->sess_tag_pool," 278 " tag_num: %u\n", tag_num); 279 if (is_vmalloc_addr(se_sess->sess_cmd_map)) 280 vfree(se_sess->sess_cmd_map); 281 else 282 kfree(se_sess->sess_cmd_map); 283 se_sess->sess_cmd_map = NULL; 284 return -ENOMEM; 285 } 286 287 return 0; 288 } 289 EXPORT_SYMBOL(transport_alloc_session_tags); 290 291 struct se_session *transport_init_session_tags(unsigned int tag_num, 292 unsigned int tag_size, 293 enum target_prot_op sup_prot_ops) 294 { 295 struct se_session *se_sess; 296 int rc; 297 298 se_sess = transport_init_session(sup_prot_ops); 299 if (IS_ERR(se_sess)) 300 return se_sess; 301 302 rc = transport_alloc_session_tags(se_sess, tag_num, tag_size); 303 if (rc < 0) { 304 transport_free_session(se_sess); 305 return ERR_PTR(-ENOMEM); 306 } 307 308 return se_sess; 309 } 310 EXPORT_SYMBOL(transport_init_session_tags); 311 312 /* 313 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called. 314 */ 315 void __transport_register_session( 316 struct se_portal_group *se_tpg, 317 struct se_node_acl *se_nacl, 318 struct se_session *se_sess, 319 void *fabric_sess_ptr) 320 { 321 unsigned char buf[PR_REG_ISID_LEN]; 322 323 se_sess->se_tpg = se_tpg; 324 se_sess->fabric_sess_ptr = fabric_sess_ptr; 325 /* 326 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t 327 * 328 * Only set for struct se_session's that will actually be moving I/O. 329 * eg: *NOT* discovery sessions. 330 */ 331 if (se_nacl) { 332 /* 333 * If the fabric module supports an ISID based TransportID, 334 * save this value in binary from the fabric I_T Nexus now. 335 */ 336 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 337 memset(&buf[0], 0, PR_REG_ISID_LEN); 338 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, 339 &buf[0], PR_REG_ISID_LEN); 340 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]); 341 } 342 kref_get(&se_nacl->acl_kref); 343 344 spin_lock_irq(&se_nacl->nacl_sess_lock); 345 /* 346 * The se_nacl->nacl_sess pointer will be set to the 347 * last active I_T Nexus for each struct se_node_acl. 348 */ 349 se_nacl->nacl_sess = se_sess; 350 351 list_add_tail(&se_sess->sess_acl_list, 352 &se_nacl->acl_sess_list); 353 spin_unlock_irq(&se_nacl->nacl_sess_lock); 354 } 355 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list); 356 357 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n", 358 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr); 359 } 360 EXPORT_SYMBOL(__transport_register_session); 361 362 void transport_register_session( 363 struct se_portal_group *se_tpg, 364 struct se_node_acl *se_nacl, 365 struct se_session *se_sess, 366 void *fabric_sess_ptr) 367 { 368 unsigned long flags; 369 370 spin_lock_irqsave(&se_tpg->session_lock, flags); 371 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr); 372 spin_unlock_irqrestore(&se_tpg->session_lock, flags); 373 } 374 EXPORT_SYMBOL(transport_register_session); 375 376 static void target_release_session(struct kref *kref) 377 { 378 struct se_session *se_sess = container_of(kref, 379 struct se_session, sess_kref); 380 struct se_portal_group *se_tpg = se_sess->se_tpg; 381 382 se_tpg->se_tpg_tfo->close_session(se_sess); 383 } 384 385 void target_get_session(struct se_session *se_sess) 386 { 387 kref_get(&se_sess->sess_kref); 388 } 389 EXPORT_SYMBOL(target_get_session); 390 391 void target_put_session(struct se_session *se_sess) 392 { 393 struct se_portal_group *tpg = se_sess->se_tpg; 394 395 if (tpg->se_tpg_tfo->put_session != NULL) { 396 tpg->se_tpg_tfo->put_session(se_sess); 397 return; 398 } 399 kref_put(&se_sess->sess_kref, target_release_session); 400 } 401 EXPORT_SYMBOL(target_put_session); 402 403 static void target_complete_nacl(struct kref *kref) 404 { 405 struct se_node_acl *nacl = container_of(kref, 406 struct se_node_acl, acl_kref); 407 408 complete(&nacl->acl_free_comp); 409 } 410 411 void target_put_nacl(struct se_node_acl *nacl) 412 { 413 kref_put(&nacl->acl_kref, target_complete_nacl); 414 } 415 416 void transport_deregister_session_configfs(struct se_session *se_sess) 417 { 418 struct se_node_acl *se_nacl; 419 unsigned long flags; 420 /* 421 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session 422 */ 423 se_nacl = se_sess->se_node_acl; 424 if (se_nacl) { 425 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); 426 if (se_nacl->acl_stop == 0) 427 list_del(&se_sess->sess_acl_list); 428 /* 429 * If the session list is empty, then clear the pointer. 430 * Otherwise, set the struct se_session pointer from the tail 431 * element of the per struct se_node_acl active session list. 432 */ 433 if (list_empty(&se_nacl->acl_sess_list)) 434 se_nacl->nacl_sess = NULL; 435 else { 436 se_nacl->nacl_sess = container_of( 437 se_nacl->acl_sess_list.prev, 438 struct se_session, sess_acl_list); 439 } 440 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); 441 } 442 } 443 EXPORT_SYMBOL(transport_deregister_session_configfs); 444 445 void transport_free_session(struct se_session *se_sess) 446 { 447 if (se_sess->sess_cmd_map) { 448 percpu_ida_destroy(&se_sess->sess_tag_pool); 449 if (is_vmalloc_addr(se_sess->sess_cmd_map)) 450 vfree(se_sess->sess_cmd_map); 451 else 452 kfree(se_sess->sess_cmd_map); 453 } 454 kmem_cache_free(se_sess_cache, se_sess); 455 } 456 EXPORT_SYMBOL(transport_free_session); 457 458 void transport_deregister_session(struct se_session *se_sess) 459 { 460 struct se_portal_group *se_tpg = se_sess->se_tpg; 461 struct target_core_fabric_ops *se_tfo; 462 struct se_node_acl *se_nacl; 463 unsigned long flags; 464 bool comp_nacl = true; 465 466 if (!se_tpg) { 467 transport_free_session(se_sess); 468 return; 469 } 470 se_tfo = se_tpg->se_tpg_tfo; 471 472 spin_lock_irqsave(&se_tpg->session_lock, flags); 473 list_del(&se_sess->sess_list); 474 se_sess->se_tpg = NULL; 475 se_sess->fabric_sess_ptr = NULL; 476 spin_unlock_irqrestore(&se_tpg->session_lock, flags); 477 478 /* 479 * Determine if we need to do extra work for this initiator node's 480 * struct se_node_acl if it had been previously dynamically generated. 481 */ 482 se_nacl = se_sess->se_node_acl; 483 484 spin_lock_irqsave(&se_tpg->acl_node_lock, flags); 485 if (se_nacl && se_nacl->dynamic_node_acl) { 486 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) { 487 list_del(&se_nacl->acl_list); 488 se_tpg->num_node_acls--; 489 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags); 490 core_tpg_wait_for_nacl_pr_ref(se_nacl); 491 core_free_device_list_for_node(se_nacl, se_tpg); 492 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl); 493 494 comp_nacl = false; 495 spin_lock_irqsave(&se_tpg->acl_node_lock, flags); 496 } 497 } 498 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags); 499 500 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", 501 se_tpg->se_tpg_tfo->get_fabric_name()); 502 /* 503 * If last kref is dropping now for an explicit NodeACL, awake sleeping 504 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group 505 * removal context. 506 */ 507 if (se_nacl && comp_nacl == true) 508 target_put_nacl(se_nacl); 509 510 transport_free_session(se_sess); 511 } 512 EXPORT_SYMBOL(transport_deregister_session); 513 514 /* 515 * Called with cmd->t_state_lock held. 516 */ 517 static void target_remove_from_state_list(struct se_cmd *cmd) 518 { 519 struct se_device *dev = cmd->se_dev; 520 unsigned long flags; 521 522 if (!dev) 523 return; 524 525 if (cmd->transport_state & CMD_T_BUSY) 526 return; 527 528 spin_lock_irqsave(&dev->execute_task_lock, flags); 529 if (cmd->state_active) { 530 list_del(&cmd->state_list); 531 cmd->state_active = false; 532 } 533 spin_unlock_irqrestore(&dev->execute_task_lock, flags); 534 } 535 536 static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists, 537 bool write_pending) 538 { 539 unsigned long flags; 540 541 spin_lock_irqsave(&cmd->t_state_lock, flags); 542 if (write_pending) 543 cmd->t_state = TRANSPORT_WRITE_PENDING; 544 545 if (remove_from_lists) { 546 target_remove_from_state_list(cmd); 547 548 /* 549 * Clear struct se_cmd->se_lun before the handoff to FE. 550 */ 551 cmd->se_lun = NULL; 552 } 553 554 /* 555 * Determine if frontend context caller is requesting the stopping of 556 * this command for frontend exceptions. 557 */ 558 if (cmd->transport_state & CMD_T_STOP) { 559 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n", 560 __func__, __LINE__, 561 cmd->se_tfo->get_task_tag(cmd)); 562 563 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 564 565 complete(&cmd->t_transport_stop_comp); 566 return 1; 567 } 568 569 cmd->transport_state &= ~CMD_T_ACTIVE; 570 if (remove_from_lists) { 571 /* 572 * Some fabric modules like tcm_loop can release 573 * their internally allocated I/O reference now and 574 * struct se_cmd now. 575 * 576 * Fabric modules are expected to return '1' here if the 577 * se_cmd being passed is released at this point, 578 * or zero if not being released. 579 */ 580 if (cmd->se_tfo->check_stop_free != NULL) { 581 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 582 return cmd->se_tfo->check_stop_free(cmd); 583 } 584 } 585 586 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 587 return 0; 588 } 589 590 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd) 591 { 592 return transport_cmd_check_stop(cmd, true, false); 593 } 594 595 static void transport_lun_remove_cmd(struct se_cmd *cmd) 596 { 597 struct se_lun *lun = cmd->se_lun; 598 599 if (!lun) 600 return; 601 602 if (cmpxchg(&cmd->lun_ref_active, true, false)) 603 percpu_ref_put(&lun->lun_ref); 604 } 605 606 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove) 607 { 608 if (cmd->se_cmd_flags & SCF_SE_LUN_CMD) 609 transport_lun_remove_cmd(cmd); 610 /* 611 * Allow the fabric driver to unmap any resources before 612 * releasing the descriptor via TFO->release_cmd() 613 */ 614 if (remove) 615 cmd->se_tfo->aborted_task(cmd); 616 617 if (transport_cmd_check_stop_to_fabric(cmd)) 618 return; 619 if (remove) 620 transport_put_cmd(cmd); 621 } 622 623 static void target_complete_failure_work(struct work_struct *work) 624 { 625 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 626 627 transport_generic_request_failure(cmd, 628 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE); 629 } 630 631 /* 632 * Used when asking transport to copy Sense Data from the underlying 633 * Linux/SCSI struct scsi_cmnd 634 */ 635 static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd) 636 { 637 struct se_device *dev = cmd->se_dev; 638 639 WARN_ON(!cmd->se_lun); 640 641 if (!dev) 642 return NULL; 643 644 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) 645 return NULL; 646 647 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; 648 649 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n", 650 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status); 651 return cmd->sense_buffer; 652 } 653 654 void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status) 655 { 656 struct se_device *dev = cmd->se_dev; 657 int success = scsi_status == GOOD; 658 unsigned long flags; 659 660 cmd->scsi_status = scsi_status; 661 662 663 spin_lock_irqsave(&cmd->t_state_lock, flags); 664 cmd->transport_state &= ~CMD_T_BUSY; 665 666 if (dev && dev->transport->transport_complete) { 667 dev->transport->transport_complete(cmd, 668 cmd->t_data_sg, 669 transport_get_sense_buffer(cmd)); 670 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) 671 success = 1; 672 } 673 674 /* 675 * See if we are waiting to complete for an exception condition. 676 */ 677 if (cmd->transport_state & CMD_T_REQUEST_STOP) { 678 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 679 complete(&cmd->task_stop_comp); 680 return; 681 } 682 683 /* 684 * Check for case where an explicit ABORT_TASK has been received 685 * and transport_wait_for_tasks() will be waiting for completion.. 686 */ 687 if (cmd->transport_state & CMD_T_ABORTED && 688 cmd->transport_state & CMD_T_STOP) { 689 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 690 complete(&cmd->t_transport_stop_comp); 691 return; 692 } else if (!success) { 693 INIT_WORK(&cmd->work, target_complete_failure_work); 694 } else { 695 INIT_WORK(&cmd->work, target_complete_ok_work); 696 } 697 698 cmd->t_state = TRANSPORT_COMPLETE; 699 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE); 700 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 701 702 queue_work(target_completion_wq, &cmd->work); 703 } 704 EXPORT_SYMBOL(target_complete_cmd); 705 706 static void target_add_to_state_list(struct se_cmd *cmd) 707 { 708 struct se_device *dev = cmd->se_dev; 709 unsigned long flags; 710 711 spin_lock_irqsave(&dev->execute_task_lock, flags); 712 if (!cmd->state_active) { 713 list_add_tail(&cmd->state_list, &dev->state_list); 714 cmd->state_active = true; 715 } 716 spin_unlock_irqrestore(&dev->execute_task_lock, flags); 717 } 718 719 /* 720 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status 721 */ 722 static void transport_write_pending_qf(struct se_cmd *cmd); 723 static void transport_complete_qf(struct se_cmd *cmd); 724 725 void target_qf_do_work(struct work_struct *work) 726 { 727 struct se_device *dev = container_of(work, struct se_device, 728 qf_work_queue); 729 LIST_HEAD(qf_cmd_list); 730 struct se_cmd *cmd, *cmd_tmp; 731 732 spin_lock_irq(&dev->qf_cmd_lock); 733 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list); 734 spin_unlock_irq(&dev->qf_cmd_lock); 735 736 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) { 737 list_del(&cmd->se_qf_node); 738 atomic_dec(&dev->dev_qf_count); 739 smp_mb__after_atomic_dec(); 740 741 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue" 742 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd, 743 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" : 744 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING" 745 : "UNKNOWN"); 746 747 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) 748 transport_write_pending_qf(cmd); 749 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) 750 transport_complete_qf(cmd); 751 } 752 } 753 754 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd) 755 { 756 switch (cmd->data_direction) { 757 case DMA_NONE: 758 return "NONE"; 759 case DMA_FROM_DEVICE: 760 return "READ"; 761 case DMA_TO_DEVICE: 762 return "WRITE"; 763 case DMA_BIDIRECTIONAL: 764 return "BIDI"; 765 default: 766 break; 767 } 768 769 return "UNKNOWN"; 770 } 771 772 void transport_dump_dev_state( 773 struct se_device *dev, 774 char *b, 775 int *bl) 776 { 777 *bl += sprintf(b + *bl, "Status: "); 778 if (dev->export_count) 779 *bl += sprintf(b + *bl, "ACTIVATED"); 780 else 781 *bl += sprintf(b + *bl, "DEACTIVATED"); 782 783 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth); 784 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n", 785 dev->dev_attrib.block_size, 786 dev->dev_attrib.hw_max_sectors); 787 *bl += sprintf(b + *bl, " "); 788 } 789 790 void transport_dump_vpd_proto_id( 791 struct t10_vpd *vpd, 792 unsigned char *p_buf, 793 int p_buf_len) 794 { 795 unsigned char buf[VPD_TMP_BUF_SIZE]; 796 int len; 797 798 memset(buf, 0, VPD_TMP_BUF_SIZE); 799 len = sprintf(buf, "T10 VPD Protocol Identifier: "); 800 801 switch (vpd->protocol_identifier) { 802 case 0x00: 803 sprintf(buf+len, "Fibre Channel\n"); 804 break; 805 case 0x10: 806 sprintf(buf+len, "Parallel SCSI\n"); 807 break; 808 case 0x20: 809 sprintf(buf+len, "SSA\n"); 810 break; 811 case 0x30: 812 sprintf(buf+len, "IEEE 1394\n"); 813 break; 814 case 0x40: 815 sprintf(buf+len, "SCSI Remote Direct Memory Access" 816 " Protocol\n"); 817 break; 818 case 0x50: 819 sprintf(buf+len, "Internet SCSI (iSCSI)\n"); 820 break; 821 case 0x60: 822 sprintf(buf+len, "SAS Serial SCSI Protocol\n"); 823 break; 824 case 0x70: 825 sprintf(buf+len, "Automation/Drive Interface Transport" 826 " Protocol\n"); 827 break; 828 case 0x80: 829 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n"); 830 break; 831 default: 832 sprintf(buf+len, "Unknown 0x%02x\n", 833 vpd->protocol_identifier); 834 break; 835 } 836 837 if (p_buf) 838 strncpy(p_buf, buf, p_buf_len); 839 else 840 pr_debug("%s", buf); 841 } 842 843 void 844 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83) 845 { 846 /* 847 * Check if the Protocol Identifier Valid (PIV) bit is set.. 848 * 849 * from spc3r23.pdf section 7.5.1 850 */ 851 if (page_83[1] & 0x80) { 852 vpd->protocol_identifier = (page_83[0] & 0xf0); 853 vpd->protocol_identifier_set = 1; 854 transport_dump_vpd_proto_id(vpd, NULL, 0); 855 } 856 } 857 EXPORT_SYMBOL(transport_set_vpd_proto_id); 858 859 int transport_dump_vpd_assoc( 860 struct t10_vpd *vpd, 861 unsigned char *p_buf, 862 int p_buf_len) 863 { 864 unsigned char buf[VPD_TMP_BUF_SIZE]; 865 int ret = 0; 866 int len; 867 868 memset(buf, 0, VPD_TMP_BUF_SIZE); 869 len = sprintf(buf, "T10 VPD Identifier Association: "); 870 871 switch (vpd->association) { 872 case 0x00: 873 sprintf(buf+len, "addressed logical unit\n"); 874 break; 875 case 0x10: 876 sprintf(buf+len, "target port\n"); 877 break; 878 case 0x20: 879 sprintf(buf+len, "SCSI target device\n"); 880 break; 881 default: 882 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association); 883 ret = -EINVAL; 884 break; 885 } 886 887 if (p_buf) 888 strncpy(p_buf, buf, p_buf_len); 889 else 890 pr_debug("%s", buf); 891 892 return ret; 893 } 894 895 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83) 896 { 897 /* 898 * The VPD identification association.. 899 * 900 * from spc3r23.pdf Section 7.6.3.1 Table 297 901 */ 902 vpd->association = (page_83[1] & 0x30); 903 return transport_dump_vpd_assoc(vpd, NULL, 0); 904 } 905 EXPORT_SYMBOL(transport_set_vpd_assoc); 906 907 int transport_dump_vpd_ident_type( 908 struct t10_vpd *vpd, 909 unsigned char *p_buf, 910 int p_buf_len) 911 { 912 unsigned char buf[VPD_TMP_BUF_SIZE]; 913 int ret = 0; 914 int len; 915 916 memset(buf, 0, VPD_TMP_BUF_SIZE); 917 len = sprintf(buf, "T10 VPD Identifier Type: "); 918 919 switch (vpd->device_identifier_type) { 920 case 0x00: 921 sprintf(buf+len, "Vendor specific\n"); 922 break; 923 case 0x01: 924 sprintf(buf+len, "T10 Vendor ID based\n"); 925 break; 926 case 0x02: 927 sprintf(buf+len, "EUI-64 based\n"); 928 break; 929 case 0x03: 930 sprintf(buf+len, "NAA\n"); 931 break; 932 case 0x04: 933 sprintf(buf+len, "Relative target port identifier\n"); 934 break; 935 case 0x08: 936 sprintf(buf+len, "SCSI name string\n"); 937 break; 938 default: 939 sprintf(buf+len, "Unsupported: 0x%02x\n", 940 vpd->device_identifier_type); 941 ret = -EINVAL; 942 break; 943 } 944 945 if (p_buf) { 946 if (p_buf_len < strlen(buf)+1) 947 return -EINVAL; 948 strncpy(p_buf, buf, p_buf_len); 949 } else { 950 pr_debug("%s", buf); 951 } 952 953 return ret; 954 } 955 956 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83) 957 { 958 /* 959 * The VPD identifier type.. 960 * 961 * from spc3r23.pdf Section 7.6.3.1 Table 298 962 */ 963 vpd->device_identifier_type = (page_83[1] & 0x0f); 964 return transport_dump_vpd_ident_type(vpd, NULL, 0); 965 } 966 EXPORT_SYMBOL(transport_set_vpd_ident_type); 967 968 int transport_dump_vpd_ident( 969 struct t10_vpd *vpd, 970 unsigned char *p_buf, 971 int p_buf_len) 972 { 973 unsigned char buf[VPD_TMP_BUF_SIZE]; 974 int ret = 0; 975 976 memset(buf, 0, VPD_TMP_BUF_SIZE); 977 978 switch (vpd->device_identifier_code_set) { 979 case 0x01: /* Binary */ 980 snprintf(buf, sizeof(buf), 981 "T10 VPD Binary Device Identifier: %s\n", 982 &vpd->device_identifier[0]); 983 break; 984 case 0x02: /* ASCII */ 985 snprintf(buf, sizeof(buf), 986 "T10 VPD ASCII Device Identifier: %s\n", 987 &vpd->device_identifier[0]); 988 break; 989 case 0x03: /* UTF-8 */ 990 snprintf(buf, sizeof(buf), 991 "T10 VPD UTF-8 Device Identifier: %s\n", 992 &vpd->device_identifier[0]); 993 break; 994 default: 995 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:" 996 " 0x%02x", vpd->device_identifier_code_set); 997 ret = -EINVAL; 998 break; 999 } 1000 1001 if (p_buf) 1002 strncpy(p_buf, buf, p_buf_len); 1003 else 1004 pr_debug("%s", buf); 1005 1006 return ret; 1007 } 1008 1009 int 1010 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83) 1011 { 1012 static const char hex_str[] = "0123456789abcdef"; 1013 int j = 0, i = 4; /* offset to start of the identifier */ 1014 1015 /* 1016 * The VPD Code Set (encoding) 1017 * 1018 * from spc3r23.pdf Section 7.6.3.1 Table 296 1019 */ 1020 vpd->device_identifier_code_set = (page_83[0] & 0x0f); 1021 switch (vpd->device_identifier_code_set) { 1022 case 0x01: /* Binary */ 1023 vpd->device_identifier[j++] = 1024 hex_str[vpd->device_identifier_type]; 1025 while (i < (4 + page_83[3])) { 1026 vpd->device_identifier[j++] = 1027 hex_str[(page_83[i] & 0xf0) >> 4]; 1028 vpd->device_identifier[j++] = 1029 hex_str[page_83[i] & 0x0f]; 1030 i++; 1031 } 1032 break; 1033 case 0x02: /* ASCII */ 1034 case 0x03: /* UTF-8 */ 1035 while (i < (4 + page_83[3])) 1036 vpd->device_identifier[j++] = page_83[i++]; 1037 break; 1038 default: 1039 break; 1040 } 1041 1042 return transport_dump_vpd_ident(vpd, NULL, 0); 1043 } 1044 EXPORT_SYMBOL(transport_set_vpd_ident); 1045 1046 sense_reason_t 1047 target_cmd_size_check(struct se_cmd *cmd, unsigned int size) 1048 { 1049 struct se_device *dev = cmd->se_dev; 1050 1051 if (cmd->unknown_data_length) { 1052 cmd->data_length = size; 1053 } else if (size != cmd->data_length) { 1054 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:" 1055 " %u does not match SCSI CDB Length: %u for SAM Opcode:" 1056 " 0x%02x\n", cmd->se_tfo->get_fabric_name(), 1057 cmd->data_length, size, cmd->t_task_cdb[0]); 1058 1059 if (cmd->data_direction == DMA_TO_DEVICE) { 1060 pr_err("Rejecting underflow/overflow" 1061 " WRITE data\n"); 1062 return TCM_INVALID_CDB_FIELD; 1063 } 1064 /* 1065 * Reject READ_* or WRITE_* with overflow/underflow for 1066 * type SCF_SCSI_DATA_CDB. 1067 */ 1068 if (dev->dev_attrib.block_size != 512) { 1069 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op" 1070 " CDB on non 512-byte sector setup subsystem" 1071 " plugin: %s\n", dev->transport->name); 1072 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */ 1073 return TCM_INVALID_CDB_FIELD; 1074 } 1075 /* 1076 * For the overflow case keep the existing fabric provided 1077 * ->data_length. Otherwise for the underflow case, reset 1078 * ->data_length to the smaller SCSI expected data transfer 1079 * length. 1080 */ 1081 if (size > cmd->data_length) { 1082 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT; 1083 cmd->residual_count = (size - cmd->data_length); 1084 } else { 1085 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; 1086 cmd->residual_count = (cmd->data_length - size); 1087 cmd->data_length = size; 1088 } 1089 } 1090 1091 return 0; 1092 1093 } 1094 1095 /* 1096 * Used by fabric modules containing a local struct se_cmd within their 1097 * fabric dependent per I/O descriptor. 1098 */ 1099 void transport_init_se_cmd( 1100 struct se_cmd *cmd, 1101 struct target_core_fabric_ops *tfo, 1102 struct se_session *se_sess, 1103 u32 data_length, 1104 int data_direction, 1105 int task_attr, 1106 unsigned char *sense_buffer) 1107 { 1108 INIT_LIST_HEAD(&cmd->se_delayed_node); 1109 INIT_LIST_HEAD(&cmd->se_qf_node); 1110 INIT_LIST_HEAD(&cmd->se_cmd_list); 1111 INIT_LIST_HEAD(&cmd->state_list); 1112 init_completion(&cmd->t_transport_stop_comp); 1113 init_completion(&cmd->cmd_wait_comp); 1114 init_completion(&cmd->task_stop_comp); 1115 spin_lock_init(&cmd->t_state_lock); 1116 cmd->transport_state = CMD_T_DEV_ACTIVE; 1117 1118 cmd->se_tfo = tfo; 1119 cmd->se_sess = se_sess; 1120 cmd->data_length = data_length; 1121 cmd->data_direction = data_direction; 1122 cmd->sam_task_attr = task_attr; 1123 cmd->sense_buffer = sense_buffer; 1124 1125 cmd->state_active = false; 1126 } 1127 EXPORT_SYMBOL(transport_init_se_cmd); 1128 1129 static sense_reason_t 1130 transport_check_alloc_task_attr(struct se_cmd *cmd) 1131 { 1132 struct se_device *dev = cmd->se_dev; 1133 1134 /* 1135 * Check if SAM Task Attribute emulation is enabled for this 1136 * struct se_device storage object 1137 */ 1138 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) 1139 return 0; 1140 1141 if (cmd->sam_task_attr == MSG_ACA_TAG) { 1142 pr_debug("SAM Task Attribute ACA" 1143 " emulation is not supported\n"); 1144 return TCM_INVALID_CDB_FIELD; 1145 } 1146 /* 1147 * Used to determine when ORDERED commands should go from 1148 * Dormant to Active status. 1149 */ 1150 cmd->se_ordered_id = atomic_inc_return(&dev->dev_ordered_id); 1151 smp_mb__after_atomic_inc(); 1152 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n", 1153 cmd->se_ordered_id, cmd->sam_task_attr, 1154 dev->transport->name); 1155 return 0; 1156 } 1157 1158 sense_reason_t 1159 target_setup_cmd_from_cdb(struct se_cmd *cmd, unsigned char *cdb) 1160 { 1161 struct se_device *dev = cmd->se_dev; 1162 sense_reason_t ret; 1163 1164 /* 1165 * Ensure that the received CDB is less than the max (252 + 8) bytes 1166 * for VARIABLE_LENGTH_CMD 1167 */ 1168 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) { 1169 pr_err("Received SCSI CDB with command_size: %d that" 1170 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", 1171 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE); 1172 return TCM_INVALID_CDB_FIELD; 1173 } 1174 /* 1175 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE, 1176 * allocate the additional extended CDB buffer now.. Otherwise 1177 * setup the pointer from __t_task_cdb to t_task_cdb. 1178 */ 1179 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) { 1180 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb), 1181 GFP_KERNEL); 1182 if (!cmd->t_task_cdb) { 1183 pr_err("Unable to allocate cmd->t_task_cdb" 1184 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n", 1185 scsi_command_size(cdb), 1186 (unsigned long)sizeof(cmd->__t_task_cdb)); 1187 return TCM_OUT_OF_RESOURCES; 1188 } 1189 } else 1190 cmd->t_task_cdb = &cmd->__t_task_cdb[0]; 1191 /* 1192 * Copy the original CDB into cmd-> 1193 */ 1194 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb)); 1195 1196 trace_target_sequencer_start(cmd); 1197 1198 /* 1199 * Check for an existing UNIT ATTENTION condition 1200 */ 1201 ret = target_scsi3_ua_check(cmd); 1202 if (ret) 1203 return ret; 1204 1205 ret = target_alua_state_check(cmd); 1206 if (ret) 1207 return ret; 1208 1209 ret = target_check_reservation(cmd); 1210 if (ret) { 1211 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; 1212 return ret; 1213 } 1214 1215 ret = dev->transport->parse_cdb(cmd); 1216 if (ret) 1217 return ret; 1218 1219 ret = transport_check_alloc_task_attr(cmd); 1220 if (ret) 1221 return ret; 1222 1223 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE; 1224 1225 spin_lock(&cmd->se_lun->lun_sep_lock); 1226 if (cmd->se_lun->lun_sep) 1227 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++; 1228 spin_unlock(&cmd->se_lun->lun_sep_lock); 1229 return 0; 1230 } 1231 EXPORT_SYMBOL(target_setup_cmd_from_cdb); 1232 1233 /* 1234 * Used by fabric module frontends to queue tasks directly. 1235 * Many only be used from process context only 1236 */ 1237 int transport_handle_cdb_direct( 1238 struct se_cmd *cmd) 1239 { 1240 sense_reason_t ret; 1241 1242 if (!cmd->se_lun) { 1243 dump_stack(); 1244 pr_err("cmd->se_lun is NULL\n"); 1245 return -EINVAL; 1246 } 1247 if (in_interrupt()) { 1248 dump_stack(); 1249 pr_err("transport_generic_handle_cdb cannot be called" 1250 " from interrupt context\n"); 1251 return -EINVAL; 1252 } 1253 /* 1254 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that 1255 * outstanding descriptors are handled correctly during shutdown via 1256 * transport_wait_for_tasks() 1257 * 1258 * Also, we don't take cmd->t_state_lock here as we only expect 1259 * this to be called for initial descriptor submission. 1260 */ 1261 cmd->t_state = TRANSPORT_NEW_CMD; 1262 cmd->transport_state |= CMD_T_ACTIVE; 1263 1264 /* 1265 * transport_generic_new_cmd() is already handling QUEUE_FULL, 1266 * so follow TRANSPORT_NEW_CMD processing thread context usage 1267 * and call transport_generic_request_failure() if necessary.. 1268 */ 1269 ret = transport_generic_new_cmd(cmd); 1270 if (ret) 1271 transport_generic_request_failure(cmd, ret); 1272 return 0; 1273 } 1274 EXPORT_SYMBOL(transport_handle_cdb_direct); 1275 1276 sense_reason_t 1277 transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl, 1278 u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count) 1279 { 1280 if (!sgl || !sgl_count) 1281 return 0; 1282 1283 /* 1284 * Reject SCSI data overflow with map_mem_to_cmd() as incoming 1285 * scatterlists already have been set to follow what the fabric 1286 * passes for the original expected data transfer length. 1287 */ 1288 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { 1289 pr_warn("Rejecting SCSI DATA overflow for fabric using" 1290 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n"); 1291 return TCM_INVALID_CDB_FIELD; 1292 } 1293 1294 cmd->t_data_sg = sgl; 1295 cmd->t_data_nents = sgl_count; 1296 1297 if (sgl_bidi && sgl_bidi_count) { 1298 cmd->t_bidi_data_sg = sgl_bidi; 1299 cmd->t_bidi_data_nents = sgl_bidi_count; 1300 } 1301 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC; 1302 return 0; 1303 } 1304 1305 /* 1306 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized 1307 * se_cmd + use pre-allocated SGL memory. 1308 * 1309 * @se_cmd: command descriptor to submit 1310 * @se_sess: associated se_sess for endpoint 1311 * @cdb: pointer to SCSI CDB 1312 * @sense: pointer to SCSI sense buffer 1313 * @unpacked_lun: unpacked LUN to reference for struct se_lun 1314 * @data_length: fabric expected data transfer length 1315 * @task_addr: SAM task attribute 1316 * @data_dir: DMA data direction 1317 * @flags: flags for command submission from target_sc_flags_tables 1318 * @sgl: struct scatterlist memory for unidirectional mapping 1319 * @sgl_count: scatterlist count for unidirectional mapping 1320 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping 1321 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping 1322 * @sgl_prot: struct scatterlist memory protection information 1323 * @sgl_prot_count: scatterlist count for protection information 1324 * 1325 * Returns non zero to signal active I/O shutdown failure. All other 1326 * setup exceptions will be returned as a SCSI CHECK_CONDITION response, 1327 * but still return zero here. 1328 * 1329 * This may only be called from process context, and also currently 1330 * assumes internal allocation of fabric payload buffer by target-core. 1331 */ 1332 int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess, 1333 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun, 1334 u32 data_length, int task_attr, int data_dir, int flags, 1335 struct scatterlist *sgl, u32 sgl_count, 1336 struct scatterlist *sgl_bidi, u32 sgl_bidi_count, 1337 struct scatterlist *sgl_prot, u32 sgl_prot_count) 1338 { 1339 struct se_portal_group *se_tpg; 1340 sense_reason_t rc; 1341 int ret; 1342 1343 se_tpg = se_sess->se_tpg; 1344 BUG_ON(!se_tpg); 1345 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess); 1346 BUG_ON(in_interrupt()); 1347 /* 1348 * Initialize se_cmd for target operation. From this point 1349 * exceptions are handled by sending exception status via 1350 * target_core_fabric_ops->queue_status() callback 1351 */ 1352 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 1353 data_length, data_dir, task_attr, sense); 1354 if (flags & TARGET_SCF_UNKNOWN_SIZE) 1355 se_cmd->unknown_data_length = 1; 1356 /* 1357 * Obtain struct se_cmd->cmd_kref reference and add new cmd to 1358 * se_sess->sess_cmd_list. A second kref_get here is necessary 1359 * for fabrics using TARGET_SCF_ACK_KREF that expect a second 1360 * kref_put() to happen during fabric packet acknowledgement. 1361 */ 1362 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF)); 1363 if (ret) 1364 return ret; 1365 /* 1366 * Signal bidirectional data payloads to target-core 1367 */ 1368 if (flags & TARGET_SCF_BIDI_OP) 1369 se_cmd->se_cmd_flags |= SCF_BIDI; 1370 /* 1371 * Locate se_lun pointer and attach it to struct se_cmd 1372 */ 1373 rc = transport_lookup_cmd_lun(se_cmd, unpacked_lun); 1374 if (rc) { 1375 transport_send_check_condition_and_sense(se_cmd, rc, 0); 1376 target_put_sess_cmd(se_sess, se_cmd); 1377 return 0; 1378 } 1379 1380 rc = target_setup_cmd_from_cdb(se_cmd, cdb); 1381 if (rc != 0) { 1382 transport_generic_request_failure(se_cmd, rc); 1383 return 0; 1384 } 1385 1386 /* 1387 * Save pointers for SGLs containing protection information, 1388 * if present. 1389 */ 1390 if (sgl_prot_count) { 1391 se_cmd->t_prot_sg = sgl_prot; 1392 se_cmd->t_prot_nents = sgl_prot_count; 1393 } 1394 1395 /* 1396 * When a non zero sgl_count has been passed perform SGL passthrough 1397 * mapping for pre-allocated fabric memory instead of having target 1398 * core perform an internal SGL allocation.. 1399 */ 1400 if (sgl_count != 0) { 1401 BUG_ON(!sgl); 1402 1403 /* 1404 * A work-around for tcm_loop as some userspace code via 1405 * scsi-generic do not memset their associated read buffers, 1406 * so go ahead and do that here for type non-data CDBs. Also 1407 * note that this is currently guaranteed to be a single SGL 1408 * for this case by target core in target_setup_cmd_from_cdb() 1409 * -> transport_generic_cmd_sequencer(). 1410 */ 1411 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && 1412 se_cmd->data_direction == DMA_FROM_DEVICE) { 1413 unsigned char *buf = NULL; 1414 1415 if (sgl) 1416 buf = kmap(sg_page(sgl)) + sgl->offset; 1417 1418 if (buf) { 1419 memset(buf, 0, sgl->length); 1420 kunmap(sg_page(sgl)); 1421 } 1422 } 1423 1424 rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count, 1425 sgl_bidi, sgl_bidi_count); 1426 if (rc != 0) { 1427 transport_generic_request_failure(se_cmd, rc); 1428 return 0; 1429 } 1430 } 1431 1432 /* 1433 * Check if we need to delay processing because of ALUA 1434 * Active/NonOptimized primary access state.. 1435 */ 1436 core_alua_check_nonop_delay(se_cmd); 1437 1438 transport_handle_cdb_direct(se_cmd); 1439 return 0; 1440 } 1441 EXPORT_SYMBOL(target_submit_cmd_map_sgls); 1442 1443 /* 1444 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd 1445 * 1446 * @se_cmd: command descriptor to submit 1447 * @se_sess: associated se_sess for endpoint 1448 * @cdb: pointer to SCSI CDB 1449 * @sense: pointer to SCSI sense buffer 1450 * @unpacked_lun: unpacked LUN to reference for struct se_lun 1451 * @data_length: fabric expected data transfer length 1452 * @task_addr: SAM task attribute 1453 * @data_dir: DMA data direction 1454 * @flags: flags for command submission from target_sc_flags_tables 1455 * 1456 * Returns non zero to signal active I/O shutdown failure. All other 1457 * setup exceptions will be returned as a SCSI CHECK_CONDITION response, 1458 * but still return zero here. 1459 * 1460 * This may only be called from process context, and also currently 1461 * assumes internal allocation of fabric payload buffer by target-core. 1462 * 1463 * It also assumes interal target core SGL memory allocation. 1464 */ 1465 int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess, 1466 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun, 1467 u32 data_length, int task_attr, int data_dir, int flags) 1468 { 1469 return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense, 1470 unpacked_lun, data_length, task_attr, data_dir, 1471 flags, NULL, 0, NULL, 0, NULL, 0); 1472 } 1473 EXPORT_SYMBOL(target_submit_cmd); 1474 1475 static void target_complete_tmr_failure(struct work_struct *work) 1476 { 1477 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work); 1478 1479 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST; 1480 se_cmd->se_tfo->queue_tm_rsp(se_cmd); 1481 1482 transport_cmd_check_stop_to_fabric(se_cmd); 1483 } 1484 1485 /** 1486 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd 1487 * for TMR CDBs 1488 * 1489 * @se_cmd: command descriptor to submit 1490 * @se_sess: associated se_sess for endpoint 1491 * @sense: pointer to SCSI sense buffer 1492 * @unpacked_lun: unpacked LUN to reference for struct se_lun 1493 * @fabric_context: fabric context for TMR req 1494 * @tm_type: Type of TM request 1495 * @gfp: gfp type for caller 1496 * @tag: referenced task tag for TMR_ABORT_TASK 1497 * @flags: submit cmd flags 1498 * 1499 * Callable from all contexts. 1500 **/ 1501 1502 int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess, 1503 unsigned char *sense, u32 unpacked_lun, 1504 void *fabric_tmr_ptr, unsigned char tm_type, 1505 gfp_t gfp, unsigned int tag, int flags) 1506 { 1507 struct se_portal_group *se_tpg; 1508 int ret; 1509 1510 se_tpg = se_sess->se_tpg; 1511 BUG_ON(!se_tpg); 1512 1513 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 1514 0, DMA_NONE, MSG_SIMPLE_TAG, sense); 1515 /* 1516 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req 1517 * allocation failure. 1518 */ 1519 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp); 1520 if (ret < 0) 1521 return -ENOMEM; 1522 1523 if (tm_type == TMR_ABORT_TASK) 1524 se_cmd->se_tmr_req->ref_task_tag = tag; 1525 1526 /* See target_submit_cmd for commentary */ 1527 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF)); 1528 if (ret) { 1529 core_tmr_release_req(se_cmd->se_tmr_req); 1530 return ret; 1531 } 1532 1533 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun); 1534 if (ret) { 1535 /* 1536 * For callback during failure handling, push this work off 1537 * to process context with TMR_LUN_DOES_NOT_EXIST status. 1538 */ 1539 INIT_WORK(&se_cmd->work, target_complete_tmr_failure); 1540 schedule_work(&se_cmd->work); 1541 return 0; 1542 } 1543 transport_generic_handle_tmr(se_cmd); 1544 return 0; 1545 } 1546 EXPORT_SYMBOL(target_submit_tmr); 1547 1548 /* 1549 * If the cmd is active, request it to be stopped and sleep until it 1550 * has completed. 1551 */ 1552 bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags) 1553 { 1554 bool was_active = false; 1555 1556 if (cmd->transport_state & CMD_T_BUSY) { 1557 cmd->transport_state |= CMD_T_REQUEST_STOP; 1558 spin_unlock_irqrestore(&cmd->t_state_lock, *flags); 1559 1560 pr_debug("cmd %p waiting to complete\n", cmd); 1561 wait_for_completion(&cmd->task_stop_comp); 1562 pr_debug("cmd %p stopped successfully\n", cmd); 1563 1564 spin_lock_irqsave(&cmd->t_state_lock, *flags); 1565 cmd->transport_state &= ~CMD_T_REQUEST_STOP; 1566 cmd->transport_state &= ~CMD_T_BUSY; 1567 was_active = true; 1568 } 1569 1570 return was_active; 1571 } 1572 1573 /* 1574 * Handle SAM-esque emulation for generic transport request failures. 1575 */ 1576 void transport_generic_request_failure(struct se_cmd *cmd, 1577 sense_reason_t sense_reason) 1578 { 1579 int ret = 0; 1580 1581 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x" 1582 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd), 1583 cmd->t_task_cdb[0]); 1584 pr_debug("-----[ i_state: %d t_state: %d sense_reason: %d\n", 1585 cmd->se_tfo->get_cmd_state(cmd), 1586 cmd->t_state, sense_reason); 1587 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n", 1588 (cmd->transport_state & CMD_T_ACTIVE) != 0, 1589 (cmd->transport_state & CMD_T_STOP) != 0, 1590 (cmd->transport_state & CMD_T_SENT) != 0); 1591 1592 /* 1593 * For SAM Task Attribute emulation for failed struct se_cmd 1594 */ 1595 transport_complete_task_attr(cmd); 1596 /* 1597 * Handle special case for COMPARE_AND_WRITE failure, where the 1598 * callback is expected to drop the per device ->caw_mutex. 1599 */ 1600 if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) && 1601 cmd->transport_complete_callback) 1602 cmd->transport_complete_callback(cmd); 1603 1604 switch (sense_reason) { 1605 case TCM_NON_EXISTENT_LUN: 1606 case TCM_UNSUPPORTED_SCSI_OPCODE: 1607 case TCM_INVALID_CDB_FIELD: 1608 case TCM_INVALID_PARAMETER_LIST: 1609 case TCM_PARAMETER_LIST_LENGTH_ERROR: 1610 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: 1611 case TCM_UNKNOWN_MODE_PAGE: 1612 case TCM_WRITE_PROTECTED: 1613 case TCM_ADDRESS_OUT_OF_RANGE: 1614 case TCM_CHECK_CONDITION_ABORT_CMD: 1615 case TCM_CHECK_CONDITION_UNIT_ATTENTION: 1616 case TCM_CHECK_CONDITION_NOT_READY: 1617 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED: 1618 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED: 1619 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED: 1620 break; 1621 case TCM_OUT_OF_RESOURCES: 1622 sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 1623 break; 1624 case TCM_RESERVATION_CONFLICT: 1625 /* 1626 * No SENSE Data payload for this case, set SCSI Status 1627 * and queue the response to $FABRIC_MOD. 1628 * 1629 * Uses linux/include/scsi/scsi.h SAM status codes defs 1630 */ 1631 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; 1632 /* 1633 * For UA Interlock Code 11b, a RESERVATION CONFLICT will 1634 * establish a UNIT ATTENTION with PREVIOUS RESERVATION 1635 * CONFLICT STATUS. 1636 * 1637 * See spc4r17, section 7.4.6 Control Mode Page, Table 349 1638 */ 1639 if (cmd->se_sess && 1640 cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2) 1641 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl, 1642 cmd->orig_fe_lun, 0x2C, 1643 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS); 1644 1645 trace_target_cmd_complete(cmd); 1646 ret = cmd->se_tfo-> queue_status(cmd); 1647 if (ret == -EAGAIN || ret == -ENOMEM) 1648 goto queue_full; 1649 goto check_stop; 1650 default: 1651 pr_err("Unknown transport error for CDB 0x%02x: %d\n", 1652 cmd->t_task_cdb[0], sense_reason); 1653 sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; 1654 break; 1655 } 1656 1657 ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0); 1658 if (ret == -EAGAIN || ret == -ENOMEM) 1659 goto queue_full; 1660 1661 check_stop: 1662 transport_lun_remove_cmd(cmd); 1663 if (!transport_cmd_check_stop_to_fabric(cmd)) 1664 ; 1665 return; 1666 1667 queue_full: 1668 cmd->t_state = TRANSPORT_COMPLETE_QF_OK; 1669 transport_handle_queue_full(cmd, cmd->se_dev); 1670 } 1671 EXPORT_SYMBOL(transport_generic_request_failure); 1672 1673 void __target_execute_cmd(struct se_cmd *cmd) 1674 { 1675 sense_reason_t ret; 1676 1677 if (cmd->execute_cmd) { 1678 ret = cmd->execute_cmd(cmd); 1679 if (ret) { 1680 spin_lock_irq(&cmd->t_state_lock); 1681 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT); 1682 spin_unlock_irq(&cmd->t_state_lock); 1683 1684 transport_generic_request_failure(cmd, ret); 1685 } 1686 } 1687 } 1688 1689 static bool target_handle_task_attr(struct se_cmd *cmd) 1690 { 1691 struct se_device *dev = cmd->se_dev; 1692 1693 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) 1694 return false; 1695 1696 /* 1697 * Check for the existence of HEAD_OF_QUEUE, and if true return 1 1698 * to allow the passed struct se_cmd list of tasks to the front of the list. 1699 */ 1700 switch (cmd->sam_task_attr) { 1701 case MSG_HEAD_TAG: 1702 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, " 1703 "se_ordered_id: %u\n", 1704 cmd->t_task_cdb[0], cmd->se_ordered_id); 1705 return false; 1706 case MSG_ORDERED_TAG: 1707 atomic_inc(&dev->dev_ordered_sync); 1708 smp_mb__after_atomic_inc(); 1709 1710 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, " 1711 " se_ordered_id: %u\n", 1712 cmd->t_task_cdb[0], cmd->se_ordered_id); 1713 1714 /* 1715 * Execute an ORDERED command if no other older commands 1716 * exist that need to be completed first. 1717 */ 1718 if (!atomic_read(&dev->simple_cmds)) 1719 return false; 1720 break; 1721 default: 1722 /* 1723 * For SIMPLE and UNTAGGED Task Attribute commands 1724 */ 1725 atomic_inc(&dev->simple_cmds); 1726 smp_mb__after_atomic_inc(); 1727 break; 1728 } 1729 1730 if (atomic_read(&dev->dev_ordered_sync) == 0) 1731 return false; 1732 1733 spin_lock(&dev->delayed_cmd_lock); 1734 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list); 1735 spin_unlock(&dev->delayed_cmd_lock); 1736 1737 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to" 1738 " delayed CMD list, se_ordered_id: %u\n", 1739 cmd->t_task_cdb[0], cmd->sam_task_attr, 1740 cmd->se_ordered_id); 1741 return true; 1742 } 1743 1744 void target_execute_cmd(struct se_cmd *cmd) 1745 { 1746 /* 1747 * If the received CDB has aleady been aborted stop processing it here. 1748 */ 1749 if (transport_check_aborted_status(cmd, 1)) 1750 return; 1751 1752 /* 1753 * Determine if frontend context caller is requesting the stopping of 1754 * this command for frontend exceptions. 1755 */ 1756 spin_lock_irq(&cmd->t_state_lock); 1757 if (cmd->transport_state & CMD_T_STOP) { 1758 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n", 1759 __func__, __LINE__, 1760 cmd->se_tfo->get_task_tag(cmd)); 1761 1762 spin_unlock_irq(&cmd->t_state_lock); 1763 complete(&cmd->t_transport_stop_comp); 1764 return; 1765 } 1766 1767 cmd->t_state = TRANSPORT_PROCESSING; 1768 cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT; 1769 spin_unlock_irq(&cmd->t_state_lock); 1770 /* 1771 * Perform WRITE_INSERT of PI using software emulation when backend 1772 * device has PI enabled, if the transport has not already generated 1773 * PI using hardware WRITE_INSERT offload. 1774 */ 1775 if (cmd->prot_op == TARGET_PROT_DOUT_INSERT) { 1776 if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_INSERT)) 1777 sbc_dif_generate(cmd); 1778 } 1779 1780 if (target_handle_task_attr(cmd)) { 1781 spin_lock_irq(&cmd->t_state_lock); 1782 cmd->transport_state &= ~CMD_T_BUSY|CMD_T_SENT; 1783 spin_unlock_irq(&cmd->t_state_lock); 1784 return; 1785 } 1786 1787 __target_execute_cmd(cmd); 1788 } 1789 EXPORT_SYMBOL(target_execute_cmd); 1790 1791 /* 1792 * Process all commands up to the last received ORDERED task attribute which 1793 * requires another blocking boundary 1794 */ 1795 static void target_restart_delayed_cmds(struct se_device *dev) 1796 { 1797 for (;;) { 1798 struct se_cmd *cmd; 1799 1800 spin_lock(&dev->delayed_cmd_lock); 1801 if (list_empty(&dev->delayed_cmd_list)) { 1802 spin_unlock(&dev->delayed_cmd_lock); 1803 break; 1804 } 1805 1806 cmd = list_entry(dev->delayed_cmd_list.next, 1807 struct se_cmd, se_delayed_node); 1808 list_del(&cmd->se_delayed_node); 1809 spin_unlock(&dev->delayed_cmd_lock); 1810 1811 __target_execute_cmd(cmd); 1812 1813 if (cmd->sam_task_attr == MSG_ORDERED_TAG) 1814 break; 1815 } 1816 } 1817 1818 /* 1819 * Called from I/O completion to determine which dormant/delayed 1820 * and ordered cmds need to have their tasks added to the execution queue. 1821 */ 1822 static void transport_complete_task_attr(struct se_cmd *cmd) 1823 { 1824 struct se_device *dev = cmd->se_dev; 1825 1826 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) 1827 return; 1828 1829 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) { 1830 atomic_dec(&dev->simple_cmds); 1831 smp_mb__after_atomic_dec(); 1832 dev->dev_cur_ordered_id++; 1833 pr_debug("Incremented dev->dev_cur_ordered_id: %u for" 1834 " SIMPLE: %u\n", dev->dev_cur_ordered_id, 1835 cmd->se_ordered_id); 1836 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) { 1837 dev->dev_cur_ordered_id++; 1838 pr_debug("Incremented dev_cur_ordered_id: %u for" 1839 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id, 1840 cmd->se_ordered_id); 1841 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) { 1842 atomic_dec(&dev->dev_ordered_sync); 1843 smp_mb__after_atomic_dec(); 1844 1845 dev->dev_cur_ordered_id++; 1846 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:" 1847 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id); 1848 } 1849 1850 target_restart_delayed_cmds(dev); 1851 } 1852 1853 static void transport_complete_qf(struct se_cmd *cmd) 1854 { 1855 int ret = 0; 1856 1857 transport_complete_task_attr(cmd); 1858 1859 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { 1860 trace_target_cmd_complete(cmd); 1861 ret = cmd->se_tfo->queue_status(cmd); 1862 if (ret) 1863 goto out; 1864 } 1865 1866 switch (cmd->data_direction) { 1867 case DMA_FROM_DEVICE: 1868 trace_target_cmd_complete(cmd); 1869 ret = cmd->se_tfo->queue_data_in(cmd); 1870 break; 1871 case DMA_TO_DEVICE: 1872 if (cmd->se_cmd_flags & SCF_BIDI) { 1873 ret = cmd->se_tfo->queue_data_in(cmd); 1874 if (ret < 0) 1875 break; 1876 } 1877 /* Fall through for DMA_TO_DEVICE */ 1878 case DMA_NONE: 1879 trace_target_cmd_complete(cmd); 1880 ret = cmd->se_tfo->queue_status(cmd); 1881 break; 1882 default: 1883 break; 1884 } 1885 1886 out: 1887 if (ret < 0) { 1888 transport_handle_queue_full(cmd, cmd->se_dev); 1889 return; 1890 } 1891 transport_lun_remove_cmd(cmd); 1892 transport_cmd_check_stop_to_fabric(cmd); 1893 } 1894 1895 static void transport_handle_queue_full( 1896 struct se_cmd *cmd, 1897 struct se_device *dev) 1898 { 1899 spin_lock_irq(&dev->qf_cmd_lock); 1900 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list); 1901 atomic_inc(&dev->dev_qf_count); 1902 smp_mb__after_atomic_inc(); 1903 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock); 1904 1905 schedule_work(&cmd->se_dev->qf_work_queue); 1906 } 1907 1908 static bool target_check_read_strip(struct se_cmd *cmd) 1909 { 1910 sense_reason_t rc; 1911 1912 if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_STRIP)) { 1913 rc = sbc_dif_read_strip(cmd); 1914 if (rc) { 1915 cmd->pi_err = rc; 1916 return true; 1917 } 1918 } 1919 1920 return false; 1921 } 1922 1923 static void target_complete_ok_work(struct work_struct *work) 1924 { 1925 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 1926 int ret; 1927 1928 /* 1929 * Check if we need to move delayed/dormant tasks from cmds on the 1930 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task 1931 * Attribute. 1932 */ 1933 transport_complete_task_attr(cmd); 1934 1935 /* 1936 * Check to schedule QUEUE_FULL work, or execute an existing 1937 * cmd->transport_qf_callback() 1938 */ 1939 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0) 1940 schedule_work(&cmd->se_dev->qf_work_queue); 1941 1942 /* 1943 * Check if we need to send a sense buffer from 1944 * the struct se_cmd in question. 1945 */ 1946 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { 1947 WARN_ON(!cmd->scsi_status); 1948 ret = transport_send_check_condition_and_sense( 1949 cmd, 0, 1); 1950 if (ret == -EAGAIN || ret == -ENOMEM) 1951 goto queue_full; 1952 1953 transport_lun_remove_cmd(cmd); 1954 transport_cmd_check_stop_to_fabric(cmd); 1955 return; 1956 } 1957 /* 1958 * Check for a callback, used by amongst other things 1959 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation. 1960 */ 1961 if (cmd->transport_complete_callback) { 1962 sense_reason_t rc; 1963 1964 rc = cmd->transport_complete_callback(cmd); 1965 if (!rc && !(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE_POST)) { 1966 return; 1967 } else if (rc) { 1968 ret = transport_send_check_condition_and_sense(cmd, 1969 rc, 0); 1970 if (ret == -EAGAIN || ret == -ENOMEM) 1971 goto queue_full; 1972 1973 transport_lun_remove_cmd(cmd); 1974 transport_cmd_check_stop_to_fabric(cmd); 1975 return; 1976 } 1977 } 1978 1979 switch (cmd->data_direction) { 1980 case DMA_FROM_DEVICE: 1981 spin_lock(&cmd->se_lun->lun_sep_lock); 1982 if (cmd->se_lun->lun_sep) { 1983 cmd->se_lun->lun_sep->sep_stats.tx_data_octets += 1984 cmd->data_length; 1985 } 1986 spin_unlock(&cmd->se_lun->lun_sep_lock); 1987 /* 1988 * Perform READ_STRIP of PI using software emulation when 1989 * backend had PI enabled, if the transport will not be 1990 * performing hardware READ_STRIP offload. 1991 */ 1992 if (cmd->prot_op == TARGET_PROT_DIN_STRIP && 1993 target_check_read_strip(cmd)) { 1994 ret = transport_send_check_condition_and_sense(cmd, 1995 cmd->pi_err, 0); 1996 if (ret == -EAGAIN || ret == -ENOMEM) 1997 goto queue_full; 1998 1999 transport_lun_remove_cmd(cmd); 2000 transport_cmd_check_stop_to_fabric(cmd); 2001 return; 2002 } 2003 2004 trace_target_cmd_complete(cmd); 2005 ret = cmd->se_tfo->queue_data_in(cmd); 2006 if (ret == -EAGAIN || ret == -ENOMEM) 2007 goto queue_full; 2008 break; 2009 case DMA_TO_DEVICE: 2010 spin_lock(&cmd->se_lun->lun_sep_lock); 2011 if (cmd->se_lun->lun_sep) { 2012 cmd->se_lun->lun_sep->sep_stats.rx_data_octets += 2013 cmd->data_length; 2014 } 2015 spin_unlock(&cmd->se_lun->lun_sep_lock); 2016 /* 2017 * Check if we need to send READ payload for BIDI-COMMAND 2018 */ 2019 if (cmd->se_cmd_flags & SCF_BIDI) { 2020 spin_lock(&cmd->se_lun->lun_sep_lock); 2021 if (cmd->se_lun->lun_sep) { 2022 cmd->se_lun->lun_sep->sep_stats.tx_data_octets += 2023 cmd->data_length; 2024 } 2025 spin_unlock(&cmd->se_lun->lun_sep_lock); 2026 ret = cmd->se_tfo->queue_data_in(cmd); 2027 if (ret == -EAGAIN || ret == -ENOMEM) 2028 goto queue_full; 2029 break; 2030 } 2031 /* Fall through for DMA_TO_DEVICE */ 2032 case DMA_NONE: 2033 trace_target_cmd_complete(cmd); 2034 ret = cmd->se_tfo->queue_status(cmd); 2035 if (ret == -EAGAIN || ret == -ENOMEM) 2036 goto queue_full; 2037 break; 2038 default: 2039 break; 2040 } 2041 2042 transport_lun_remove_cmd(cmd); 2043 transport_cmd_check_stop_to_fabric(cmd); 2044 return; 2045 2046 queue_full: 2047 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p," 2048 " data_direction: %d\n", cmd, cmd->data_direction); 2049 cmd->t_state = TRANSPORT_COMPLETE_QF_OK; 2050 transport_handle_queue_full(cmd, cmd->se_dev); 2051 } 2052 2053 static inline void transport_free_sgl(struct scatterlist *sgl, int nents) 2054 { 2055 struct scatterlist *sg; 2056 int count; 2057 2058 for_each_sg(sgl, sg, nents, count) 2059 __free_page(sg_page(sg)); 2060 2061 kfree(sgl); 2062 } 2063 2064 static inline void transport_reset_sgl_orig(struct se_cmd *cmd) 2065 { 2066 /* 2067 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE 2068 * emulation, and free + reset pointers if necessary.. 2069 */ 2070 if (!cmd->t_data_sg_orig) 2071 return; 2072 2073 kfree(cmd->t_data_sg); 2074 cmd->t_data_sg = cmd->t_data_sg_orig; 2075 cmd->t_data_sg_orig = NULL; 2076 cmd->t_data_nents = cmd->t_data_nents_orig; 2077 cmd->t_data_nents_orig = 0; 2078 } 2079 2080 static inline void transport_free_pages(struct se_cmd *cmd) 2081 { 2082 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) { 2083 transport_reset_sgl_orig(cmd); 2084 return; 2085 } 2086 transport_reset_sgl_orig(cmd); 2087 2088 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents); 2089 cmd->t_data_sg = NULL; 2090 cmd->t_data_nents = 0; 2091 2092 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents); 2093 cmd->t_bidi_data_sg = NULL; 2094 cmd->t_bidi_data_nents = 0; 2095 2096 transport_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents); 2097 cmd->t_prot_sg = NULL; 2098 cmd->t_prot_nents = 0; 2099 } 2100 2101 /** 2102 * transport_release_cmd - free a command 2103 * @cmd: command to free 2104 * 2105 * This routine unconditionally frees a command, and reference counting 2106 * or list removal must be done in the caller. 2107 */ 2108 static int transport_release_cmd(struct se_cmd *cmd) 2109 { 2110 BUG_ON(!cmd->se_tfo); 2111 2112 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) 2113 core_tmr_release_req(cmd->se_tmr_req); 2114 if (cmd->t_task_cdb != cmd->__t_task_cdb) 2115 kfree(cmd->t_task_cdb); 2116 /* 2117 * If this cmd has been setup with target_get_sess_cmd(), drop 2118 * the kref and call ->release_cmd() in kref callback. 2119 */ 2120 return target_put_sess_cmd(cmd->se_sess, cmd); 2121 } 2122 2123 /** 2124 * transport_put_cmd - release a reference to a command 2125 * @cmd: command to release 2126 * 2127 * This routine releases our reference to the command and frees it if possible. 2128 */ 2129 static int transport_put_cmd(struct se_cmd *cmd) 2130 { 2131 transport_free_pages(cmd); 2132 return transport_release_cmd(cmd); 2133 } 2134 2135 void *transport_kmap_data_sg(struct se_cmd *cmd) 2136 { 2137 struct scatterlist *sg = cmd->t_data_sg; 2138 struct page **pages; 2139 int i; 2140 2141 /* 2142 * We need to take into account a possible offset here for fabrics like 2143 * tcm_loop who may be using a contig buffer from the SCSI midlayer for 2144 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd() 2145 */ 2146 if (!cmd->t_data_nents) 2147 return NULL; 2148 2149 BUG_ON(!sg); 2150 if (cmd->t_data_nents == 1) 2151 return kmap(sg_page(sg)) + sg->offset; 2152 2153 /* >1 page. use vmap */ 2154 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL); 2155 if (!pages) 2156 return NULL; 2157 2158 /* convert sg[] to pages[] */ 2159 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) { 2160 pages[i] = sg_page(sg); 2161 } 2162 2163 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL); 2164 kfree(pages); 2165 if (!cmd->t_data_vmap) 2166 return NULL; 2167 2168 return cmd->t_data_vmap + cmd->t_data_sg[0].offset; 2169 } 2170 EXPORT_SYMBOL(transport_kmap_data_sg); 2171 2172 void transport_kunmap_data_sg(struct se_cmd *cmd) 2173 { 2174 if (!cmd->t_data_nents) { 2175 return; 2176 } else if (cmd->t_data_nents == 1) { 2177 kunmap(sg_page(cmd->t_data_sg)); 2178 return; 2179 } 2180 2181 vunmap(cmd->t_data_vmap); 2182 cmd->t_data_vmap = NULL; 2183 } 2184 EXPORT_SYMBOL(transport_kunmap_data_sg); 2185 2186 int 2187 target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length, 2188 bool zero_page) 2189 { 2190 struct scatterlist *sg; 2191 struct page *page; 2192 gfp_t zero_flag = (zero_page) ? __GFP_ZERO : 0; 2193 unsigned int nent; 2194 int i = 0; 2195 2196 nent = DIV_ROUND_UP(length, PAGE_SIZE); 2197 sg = kmalloc(sizeof(struct scatterlist) * nent, GFP_KERNEL); 2198 if (!sg) 2199 return -ENOMEM; 2200 2201 sg_init_table(sg, nent); 2202 2203 while (length) { 2204 u32 page_len = min_t(u32, length, PAGE_SIZE); 2205 page = alloc_page(GFP_KERNEL | zero_flag); 2206 if (!page) 2207 goto out; 2208 2209 sg_set_page(&sg[i], page, page_len, 0); 2210 length -= page_len; 2211 i++; 2212 } 2213 *sgl = sg; 2214 *nents = nent; 2215 return 0; 2216 2217 out: 2218 while (i > 0) { 2219 i--; 2220 __free_page(sg_page(&sg[i])); 2221 } 2222 kfree(sg); 2223 return -ENOMEM; 2224 } 2225 2226 /* 2227 * Allocate any required resources to execute the command. For writes we 2228 * might not have the payload yet, so notify the fabric via a call to 2229 * ->write_pending instead. Otherwise place it on the execution queue. 2230 */ 2231 sense_reason_t 2232 transport_generic_new_cmd(struct se_cmd *cmd) 2233 { 2234 int ret = 0; 2235 2236 /* 2237 * Determine is the TCM fabric module has already allocated physical 2238 * memory, and is directly calling transport_generic_map_mem_to_cmd() 2239 * beforehand. 2240 */ 2241 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) && 2242 cmd->data_length) { 2243 bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB); 2244 2245 if ((cmd->se_cmd_flags & SCF_BIDI) || 2246 (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) { 2247 u32 bidi_length; 2248 2249 if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) 2250 bidi_length = cmd->t_task_nolb * 2251 cmd->se_dev->dev_attrib.block_size; 2252 else 2253 bidi_length = cmd->data_length; 2254 2255 ret = target_alloc_sgl(&cmd->t_bidi_data_sg, 2256 &cmd->t_bidi_data_nents, 2257 bidi_length, zero_flag); 2258 if (ret < 0) 2259 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2260 } 2261 2262 if (cmd->prot_op != TARGET_PROT_NORMAL) { 2263 ret = target_alloc_sgl(&cmd->t_prot_sg, 2264 &cmd->t_prot_nents, 2265 cmd->prot_length, true); 2266 if (ret < 0) 2267 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2268 } 2269 2270 ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents, 2271 cmd->data_length, zero_flag); 2272 if (ret < 0) 2273 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2274 } 2275 /* 2276 * If this command is not a write we can execute it right here, 2277 * for write buffers we need to notify the fabric driver first 2278 * and let it call back once the write buffers are ready. 2279 */ 2280 target_add_to_state_list(cmd); 2281 if (cmd->data_direction != DMA_TO_DEVICE) { 2282 target_execute_cmd(cmd); 2283 return 0; 2284 } 2285 transport_cmd_check_stop(cmd, false, true); 2286 2287 ret = cmd->se_tfo->write_pending(cmd); 2288 if (ret == -EAGAIN || ret == -ENOMEM) 2289 goto queue_full; 2290 2291 /* fabric drivers should only return -EAGAIN or -ENOMEM as error */ 2292 WARN_ON(ret); 2293 2294 return (!ret) ? 0 : TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2295 2296 queue_full: 2297 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd); 2298 cmd->t_state = TRANSPORT_COMPLETE_QF_WP; 2299 transport_handle_queue_full(cmd, cmd->se_dev); 2300 return 0; 2301 } 2302 EXPORT_SYMBOL(transport_generic_new_cmd); 2303 2304 static void transport_write_pending_qf(struct se_cmd *cmd) 2305 { 2306 int ret; 2307 2308 ret = cmd->se_tfo->write_pending(cmd); 2309 if (ret == -EAGAIN || ret == -ENOMEM) { 2310 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", 2311 cmd); 2312 transport_handle_queue_full(cmd, cmd->se_dev); 2313 } 2314 } 2315 2316 int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) 2317 { 2318 unsigned long flags; 2319 int ret = 0; 2320 2321 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) { 2322 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) 2323 transport_wait_for_tasks(cmd); 2324 2325 ret = transport_release_cmd(cmd); 2326 } else { 2327 if (wait_for_tasks) 2328 transport_wait_for_tasks(cmd); 2329 /* 2330 * Handle WRITE failure case where transport_generic_new_cmd() 2331 * has already added se_cmd to state_list, but fabric has 2332 * failed command before I/O submission. 2333 */ 2334 if (cmd->state_active) { 2335 spin_lock_irqsave(&cmd->t_state_lock, flags); 2336 target_remove_from_state_list(cmd); 2337 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2338 } 2339 2340 if (cmd->se_lun) 2341 transport_lun_remove_cmd(cmd); 2342 2343 ret = transport_put_cmd(cmd); 2344 } 2345 return ret; 2346 } 2347 EXPORT_SYMBOL(transport_generic_free_cmd); 2348 2349 /* target_get_sess_cmd - Add command to active ->sess_cmd_list 2350 * @se_sess: session to reference 2351 * @se_cmd: command descriptor to add 2352 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd() 2353 */ 2354 int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd, 2355 bool ack_kref) 2356 { 2357 unsigned long flags; 2358 int ret = 0; 2359 2360 kref_init(&se_cmd->cmd_kref); 2361 /* 2362 * Add a second kref if the fabric caller is expecting to handle 2363 * fabric acknowledgement that requires two target_put_sess_cmd() 2364 * invocations before se_cmd descriptor release. 2365 */ 2366 if (ack_kref == true) { 2367 kref_get(&se_cmd->cmd_kref); 2368 se_cmd->se_cmd_flags |= SCF_ACK_KREF; 2369 } 2370 2371 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); 2372 if (se_sess->sess_tearing_down) { 2373 ret = -ESHUTDOWN; 2374 goto out; 2375 } 2376 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list); 2377 out: 2378 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); 2379 return ret; 2380 } 2381 EXPORT_SYMBOL(target_get_sess_cmd); 2382 2383 static void target_release_cmd_kref(struct kref *kref) 2384 { 2385 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref); 2386 struct se_session *se_sess = se_cmd->se_sess; 2387 2388 if (list_empty(&se_cmd->se_cmd_list)) { 2389 spin_unlock(&se_sess->sess_cmd_lock); 2390 se_cmd->se_tfo->release_cmd(se_cmd); 2391 return; 2392 } 2393 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) { 2394 spin_unlock(&se_sess->sess_cmd_lock); 2395 complete(&se_cmd->cmd_wait_comp); 2396 return; 2397 } 2398 list_del(&se_cmd->se_cmd_list); 2399 spin_unlock(&se_sess->sess_cmd_lock); 2400 2401 se_cmd->se_tfo->release_cmd(se_cmd); 2402 } 2403 2404 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put 2405 * @se_sess: session to reference 2406 * @se_cmd: command descriptor to drop 2407 */ 2408 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd) 2409 { 2410 return kref_put_spinlock_irqsave(&se_cmd->cmd_kref, target_release_cmd_kref, 2411 &se_sess->sess_cmd_lock); 2412 } 2413 EXPORT_SYMBOL(target_put_sess_cmd); 2414 2415 /* target_sess_cmd_list_set_waiting - Flag all commands in 2416 * sess_cmd_list to complete cmd_wait_comp. Set 2417 * sess_tearing_down so no more commands are queued. 2418 * @se_sess: session to flag 2419 */ 2420 void target_sess_cmd_list_set_waiting(struct se_session *se_sess) 2421 { 2422 struct se_cmd *se_cmd; 2423 unsigned long flags; 2424 2425 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); 2426 if (se_sess->sess_tearing_down) { 2427 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); 2428 return; 2429 } 2430 se_sess->sess_tearing_down = 1; 2431 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list); 2432 2433 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list) 2434 se_cmd->cmd_wait_set = 1; 2435 2436 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); 2437 } 2438 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting); 2439 2440 /* target_wait_for_sess_cmds - Wait for outstanding descriptors 2441 * @se_sess: session to wait for active I/O 2442 */ 2443 void target_wait_for_sess_cmds(struct se_session *se_sess) 2444 { 2445 struct se_cmd *se_cmd, *tmp_cmd; 2446 unsigned long flags; 2447 2448 list_for_each_entry_safe(se_cmd, tmp_cmd, 2449 &se_sess->sess_wait_list, se_cmd_list) { 2450 list_del(&se_cmd->se_cmd_list); 2451 2452 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:" 2453 " %d\n", se_cmd, se_cmd->t_state, 2454 se_cmd->se_tfo->get_cmd_state(se_cmd)); 2455 2456 wait_for_completion(&se_cmd->cmd_wait_comp); 2457 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d" 2458 " fabric state: %d\n", se_cmd, se_cmd->t_state, 2459 se_cmd->se_tfo->get_cmd_state(se_cmd)); 2460 2461 se_cmd->se_tfo->release_cmd(se_cmd); 2462 } 2463 2464 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); 2465 WARN_ON(!list_empty(&se_sess->sess_cmd_list)); 2466 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); 2467 2468 } 2469 EXPORT_SYMBOL(target_wait_for_sess_cmds); 2470 2471 static int transport_clear_lun_ref_thread(void *p) 2472 { 2473 struct se_lun *lun = p; 2474 2475 percpu_ref_kill(&lun->lun_ref); 2476 2477 wait_for_completion(&lun->lun_ref_comp); 2478 complete(&lun->lun_shutdown_comp); 2479 2480 return 0; 2481 } 2482 2483 int transport_clear_lun_ref(struct se_lun *lun) 2484 { 2485 struct task_struct *kt; 2486 2487 kt = kthread_run(transport_clear_lun_ref_thread, lun, 2488 "tcm_cl_%u", lun->unpacked_lun); 2489 if (IS_ERR(kt)) { 2490 pr_err("Unable to start clear_lun thread\n"); 2491 return PTR_ERR(kt); 2492 } 2493 wait_for_completion(&lun->lun_shutdown_comp); 2494 2495 return 0; 2496 } 2497 2498 /** 2499 * transport_wait_for_tasks - wait for completion to occur 2500 * @cmd: command to wait 2501 * 2502 * Called from frontend fabric context to wait for storage engine 2503 * to pause and/or release frontend generated struct se_cmd. 2504 */ 2505 bool transport_wait_for_tasks(struct se_cmd *cmd) 2506 { 2507 unsigned long flags; 2508 2509 spin_lock_irqsave(&cmd->t_state_lock, flags); 2510 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && 2511 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { 2512 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2513 return false; 2514 } 2515 2516 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && 2517 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { 2518 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2519 return false; 2520 } 2521 2522 if (!(cmd->transport_state & CMD_T_ACTIVE)) { 2523 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2524 return false; 2525 } 2526 2527 cmd->transport_state |= CMD_T_STOP; 2528 2529 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x" 2530 " i_state: %d, t_state: %d, CMD_T_STOP\n", 2531 cmd, cmd->se_tfo->get_task_tag(cmd), 2532 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state); 2533 2534 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2535 2536 wait_for_completion(&cmd->t_transport_stop_comp); 2537 2538 spin_lock_irqsave(&cmd->t_state_lock, flags); 2539 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP); 2540 2541 pr_debug("wait_for_tasks: Stopped wait_for_completion(" 2542 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n", 2543 cmd->se_tfo->get_task_tag(cmd)); 2544 2545 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2546 2547 return true; 2548 } 2549 EXPORT_SYMBOL(transport_wait_for_tasks); 2550 2551 static int transport_get_sense_codes( 2552 struct se_cmd *cmd, 2553 u8 *asc, 2554 u8 *ascq) 2555 { 2556 *asc = cmd->scsi_asc; 2557 *ascq = cmd->scsi_ascq; 2558 2559 return 0; 2560 } 2561 2562 static 2563 void transport_err_sector_info(unsigned char *buffer, sector_t bad_sector) 2564 { 2565 /* Place failed LBA in sense data information descriptor 0. */ 2566 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 0xc; 2567 buffer[SPC_DESC_TYPE_OFFSET] = 0; /* Information */ 2568 buffer[SPC_ADDITIONAL_DESC_LEN_OFFSET] = 0xa; 2569 buffer[SPC_VALIDITY_OFFSET] = 0x80; 2570 2571 /* Descriptor Information: failing sector */ 2572 put_unaligned_be64(bad_sector, &buffer[12]); 2573 } 2574 2575 int 2576 transport_send_check_condition_and_sense(struct se_cmd *cmd, 2577 sense_reason_t reason, int from_transport) 2578 { 2579 unsigned char *buffer = cmd->sense_buffer; 2580 unsigned long flags; 2581 u8 asc = 0, ascq = 0; 2582 2583 spin_lock_irqsave(&cmd->t_state_lock, flags); 2584 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { 2585 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2586 return 0; 2587 } 2588 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION; 2589 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2590 2591 if (!reason && from_transport) 2592 goto after_reason; 2593 2594 if (!from_transport) 2595 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE; 2596 2597 /* 2598 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses 2599 * SENSE KEY values from include/scsi/scsi.h 2600 */ 2601 switch (reason) { 2602 case TCM_NO_SENSE: 2603 /* CURRENT ERROR */ 2604 buffer[0] = 0x70; 2605 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2606 /* Not Ready */ 2607 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY; 2608 /* NO ADDITIONAL SENSE INFORMATION */ 2609 buffer[SPC_ASC_KEY_OFFSET] = 0; 2610 buffer[SPC_ASCQ_KEY_OFFSET] = 0; 2611 break; 2612 case TCM_NON_EXISTENT_LUN: 2613 /* CURRENT ERROR */ 2614 buffer[0] = 0x70; 2615 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2616 /* ILLEGAL REQUEST */ 2617 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2618 /* LOGICAL UNIT NOT SUPPORTED */ 2619 buffer[SPC_ASC_KEY_OFFSET] = 0x25; 2620 break; 2621 case TCM_UNSUPPORTED_SCSI_OPCODE: 2622 case TCM_SECTOR_COUNT_TOO_MANY: 2623 /* CURRENT ERROR */ 2624 buffer[0] = 0x70; 2625 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2626 /* ILLEGAL REQUEST */ 2627 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2628 /* INVALID COMMAND OPERATION CODE */ 2629 buffer[SPC_ASC_KEY_OFFSET] = 0x20; 2630 break; 2631 case TCM_UNKNOWN_MODE_PAGE: 2632 /* CURRENT ERROR */ 2633 buffer[0] = 0x70; 2634 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2635 /* ILLEGAL REQUEST */ 2636 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2637 /* INVALID FIELD IN CDB */ 2638 buffer[SPC_ASC_KEY_OFFSET] = 0x24; 2639 break; 2640 case TCM_CHECK_CONDITION_ABORT_CMD: 2641 /* CURRENT ERROR */ 2642 buffer[0] = 0x70; 2643 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2644 /* ABORTED COMMAND */ 2645 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; 2646 /* BUS DEVICE RESET FUNCTION OCCURRED */ 2647 buffer[SPC_ASC_KEY_OFFSET] = 0x29; 2648 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03; 2649 break; 2650 case TCM_INCORRECT_AMOUNT_OF_DATA: 2651 /* CURRENT ERROR */ 2652 buffer[0] = 0x70; 2653 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2654 /* ABORTED COMMAND */ 2655 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; 2656 /* WRITE ERROR */ 2657 buffer[SPC_ASC_KEY_OFFSET] = 0x0c; 2658 /* NOT ENOUGH UNSOLICITED DATA */ 2659 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0d; 2660 break; 2661 case TCM_INVALID_CDB_FIELD: 2662 /* CURRENT ERROR */ 2663 buffer[0] = 0x70; 2664 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2665 /* ILLEGAL REQUEST */ 2666 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2667 /* INVALID FIELD IN CDB */ 2668 buffer[SPC_ASC_KEY_OFFSET] = 0x24; 2669 break; 2670 case TCM_INVALID_PARAMETER_LIST: 2671 /* CURRENT ERROR */ 2672 buffer[0] = 0x70; 2673 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2674 /* ILLEGAL REQUEST */ 2675 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2676 /* INVALID FIELD IN PARAMETER LIST */ 2677 buffer[SPC_ASC_KEY_OFFSET] = 0x26; 2678 break; 2679 case TCM_PARAMETER_LIST_LENGTH_ERROR: 2680 /* CURRENT ERROR */ 2681 buffer[0] = 0x70; 2682 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2683 /* ILLEGAL REQUEST */ 2684 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2685 /* PARAMETER LIST LENGTH ERROR */ 2686 buffer[SPC_ASC_KEY_OFFSET] = 0x1a; 2687 break; 2688 case TCM_UNEXPECTED_UNSOLICITED_DATA: 2689 /* CURRENT ERROR */ 2690 buffer[0] = 0x70; 2691 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2692 /* ABORTED COMMAND */ 2693 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; 2694 /* WRITE ERROR */ 2695 buffer[SPC_ASC_KEY_OFFSET] = 0x0c; 2696 /* UNEXPECTED_UNSOLICITED_DATA */ 2697 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0c; 2698 break; 2699 case TCM_SERVICE_CRC_ERROR: 2700 /* CURRENT ERROR */ 2701 buffer[0] = 0x70; 2702 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2703 /* ABORTED COMMAND */ 2704 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; 2705 /* PROTOCOL SERVICE CRC ERROR */ 2706 buffer[SPC_ASC_KEY_OFFSET] = 0x47; 2707 /* N/A */ 2708 buffer[SPC_ASCQ_KEY_OFFSET] = 0x05; 2709 break; 2710 case TCM_SNACK_REJECTED: 2711 /* CURRENT ERROR */ 2712 buffer[0] = 0x70; 2713 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2714 /* ABORTED COMMAND */ 2715 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; 2716 /* READ ERROR */ 2717 buffer[SPC_ASC_KEY_OFFSET] = 0x11; 2718 /* FAILED RETRANSMISSION REQUEST */ 2719 buffer[SPC_ASCQ_KEY_OFFSET] = 0x13; 2720 break; 2721 case TCM_WRITE_PROTECTED: 2722 /* CURRENT ERROR */ 2723 buffer[0] = 0x70; 2724 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2725 /* DATA PROTECT */ 2726 buffer[SPC_SENSE_KEY_OFFSET] = DATA_PROTECT; 2727 /* WRITE PROTECTED */ 2728 buffer[SPC_ASC_KEY_OFFSET] = 0x27; 2729 break; 2730 case TCM_ADDRESS_OUT_OF_RANGE: 2731 /* CURRENT ERROR */ 2732 buffer[0] = 0x70; 2733 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2734 /* ILLEGAL REQUEST */ 2735 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2736 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */ 2737 buffer[SPC_ASC_KEY_OFFSET] = 0x21; 2738 break; 2739 case TCM_CHECK_CONDITION_UNIT_ATTENTION: 2740 /* CURRENT ERROR */ 2741 buffer[0] = 0x70; 2742 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2743 /* UNIT ATTENTION */ 2744 buffer[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION; 2745 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq); 2746 buffer[SPC_ASC_KEY_OFFSET] = asc; 2747 buffer[SPC_ASCQ_KEY_OFFSET] = ascq; 2748 break; 2749 case TCM_CHECK_CONDITION_NOT_READY: 2750 /* CURRENT ERROR */ 2751 buffer[0] = 0x70; 2752 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2753 /* Not Ready */ 2754 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY; 2755 transport_get_sense_codes(cmd, &asc, &ascq); 2756 buffer[SPC_ASC_KEY_OFFSET] = asc; 2757 buffer[SPC_ASCQ_KEY_OFFSET] = ascq; 2758 break; 2759 case TCM_MISCOMPARE_VERIFY: 2760 /* CURRENT ERROR */ 2761 buffer[0] = 0x70; 2762 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2763 buffer[SPC_SENSE_KEY_OFFSET] = MISCOMPARE; 2764 /* MISCOMPARE DURING VERIFY OPERATION */ 2765 buffer[SPC_ASC_KEY_OFFSET] = 0x1d; 2766 buffer[SPC_ASCQ_KEY_OFFSET] = 0x00; 2767 break; 2768 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED: 2769 /* CURRENT ERROR */ 2770 buffer[0] = 0x70; 2771 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2772 /* ILLEGAL REQUEST */ 2773 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2774 /* LOGICAL BLOCK GUARD CHECK FAILED */ 2775 buffer[SPC_ASC_KEY_OFFSET] = 0x10; 2776 buffer[SPC_ASCQ_KEY_OFFSET] = 0x01; 2777 transport_err_sector_info(buffer, cmd->bad_sector); 2778 break; 2779 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED: 2780 /* CURRENT ERROR */ 2781 buffer[0] = 0x70; 2782 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2783 /* ILLEGAL REQUEST */ 2784 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2785 /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */ 2786 buffer[SPC_ASC_KEY_OFFSET] = 0x10; 2787 buffer[SPC_ASCQ_KEY_OFFSET] = 0x02; 2788 transport_err_sector_info(buffer, cmd->bad_sector); 2789 break; 2790 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED: 2791 /* CURRENT ERROR */ 2792 buffer[0] = 0x70; 2793 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2794 /* ILLEGAL REQUEST */ 2795 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; 2796 /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */ 2797 buffer[SPC_ASC_KEY_OFFSET] = 0x10; 2798 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03; 2799 transport_err_sector_info(buffer, cmd->bad_sector); 2800 break; 2801 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: 2802 default: 2803 /* CURRENT ERROR */ 2804 buffer[0] = 0x70; 2805 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; 2806 /* 2807 * Returning ILLEGAL REQUEST would cause immediate IO errors on 2808 * Solaris initiators. Returning NOT READY instead means the 2809 * operations will be retried a finite number of times and we 2810 * can survive intermittent errors. 2811 */ 2812 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY; 2813 /* LOGICAL UNIT COMMUNICATION FAILURE */ 2814 buffer[SPC_ASC_KEY_OFFSET] = 0x08; 2815 break; 2816 } 2817 /* 2818 * This code uses linux/include/scsi/scsi.h SAM status codes! 2819 */ 2820 cmd->scsi_status = SAM_STAT_CHECK_CONDITION; 2821 /* 2822 * Automatically padded, this value is encoded in the fabric's 2823 * data_length response PDU containing the SCSI defined sense data. 2824 */ 2825 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; 2826 2827 after_reason: 2828 trace_target_cmd_complete(cmd); 2829 return cmd->se_tfo->queue_status(cmd); 2830 } 2831 EXPORT_SYMBOL(transport_send_check_condition_and_sense); 2832 2833 int transport_check_aborted_status(struct se_cmd *cmd, int send_status) 2834 { 2835 if (!(cmd->transport_state & CMD_T_ABORTED)) 2836 return 0; 2837 2838 /* 2839 * If cmd has been aborted but either no status is to be sent or it has 2840 * already been sent, just return 2841 */ 2842 if (!send_status || !(cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS)) 2843 return 1; 2844 2845 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB: 0x%02x ITT: 0x%08x\n", 2846 cmd->t_task_cdb[0], cmd->se_tfo->get_task_tag(cmd)); 2847 2848 cmd->se_cmd_flags &= ~SCF_SEND_DELAYED_TAS; 2849 cmd->scsi_status = SAM_STAT_TASK_ABORTED; 2850 trace_target_cmd_complete(cmd); 2851 cmd->se_tfo->queue_status(cmd); 2852 2853 return 1; 2854 } 2855 EXPORT_SYMBOL(transport_check_aborted_status); 2856 2857 void transport_send_task_abort(struct se_cmd *cmd) 2858 { 2859 unsigned long flags; 2860 2861 spin_lock_irqsave(&cmd->t_state_lock, flags); 2862 if (cmd->se_cmd_flags & (SCF_SENT_CHECK_CONDITION)) { 2863 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2864 return; 2865 } 2866 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2867 2868 /* 2869 * If there are still expected incoming fabric WRITEs, we wait 2870 * until until they have completed before sending a TASK_ABORTED 2871 * response. This response with TASK_ABORTED status will be 2872 * queued back to fabric module by transport_check_aborted_status(). 2873 */ 2874 if (cmd->data_direction == DMA_TO_DEVICE) { 2875 if (cmd->se_tfo->write_pending_status(cmd) != 0) { 2876 cmd->transport_state |= CMD_T_ABORTED; 2877 cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS; 2878 smp_mb__after_atomic_inc(); 2879 return; 2880 } 2881 } 2882 cmd->scsi_status = SAM_STAT_TASK_ABORTED; 2883 2884 transport_lun_remove_cmd(cmd); 2885 2886 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x," 2887 " ITT: 0x%08x\n", cmd->t_task_cdb[0], 2888 cmd->se_tfo->get_task_tag(cmd)); 2889 2890 trace_target_cmd_complete(cmd); 2891 cmd->se_tfo->queue_status(cmd); 2892 } 2893 2894 static void target_tmr_work(struct work_struct *work) 2895 { 2896 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 2897 struct se_device *dev = cmd->se_dev; 2898 struct se_tmr_req *tmr = cmd->se_tmr_req; 2899 int ret; 2900 2901 switch (tmr->function) { 2902 case TMR_ABORT_TASK: 2903 core_tmr_abort_task(dev, tmr, cmd->se_sess); 2904 break; 2905 case TMR_ABORT_TASK_SET: 2906 case TMR_CLEAR_ACA: 2907 case TMR_CLEAR_TASK_SET: 2908 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED; 2909 break; 2910 case TMR_LUN_RESET: 2911 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL); 2912 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE : 2913 TMR_FUNCTION_REJECTED; 2914 break; 2915 case TMR_TARGET_WARM_RESET: 2916 tmr->response = TMR_FUNCTION_REJECTED; 2917 break; 2918 case TMR_TARGET_COLD_RESET: 2919 tmr->response = TMR_FUNCTION_REJECTED; 2920 break; 2921 default: 2922 pr_err("Uknown TMR function: 0x%02x.\n", 2923 tmr->function); 2924 tmr->response = TMR_FUNCTION_REJECTED; 2925 break; 2926 } 2927 2928 cmd->t_state = TRANSPORT_ISTATE_PROCESSING; 2929 cmd->se_tfo->queue_tm_rsp(cmd); 2930 2931 transport_cmd_check_stop_to_fabric(cmd); 2932 } 2933 2934 int transport_generic_handle_tmr( 2935 struct se_cmd *cmd) 2936 { 2937 INIT_WORK(&cmd->work, target_tmr_work); 2938 queue_work(cmd->se_dev->tmr_wq, &cmd->work); 2939 return 0; 2940 } 2941 EXPORT_SYMBOL(transport_generic_handle_tmr); 2942