1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /******************************************************************************* 3 * Filename: target_core_transport.c 4 * 5 * This file contains the Generic Target Engine Core. 6 * 7 * (c) Copyright 2002-2013 Datera, Inc. 8 * 9 * Nicholas A. Bellinger <nab@kernel.org> 10 * 11 ******************************************************************************/ 12 13 #include <linux/net.h> 14 #include <linux/delay.h> 15 #include <linux/string.h> 16 #include <linux/timer.h> 17 #include <linux/slab.h> 18 #include <linux/spinlock.h> 19 #include <linux/kthread.h> 20 #include <linux/in.h> 21 #include <linux/cdrom.h> 22 #include <linux/module.h> 23 #include <linux/ratelimit.h> 24 #include <linux/vmalloc.h> 25 #include <asm/unaligned.h> 26 #include <net/sock.h> 27 #include <net/tcp.h> 28 #include <scsi/scsi_proto.h> 29 #include <scsi/scsi_common.h> 30 31 #include <target/target_core_base.h> 32 #include <target/target_core_backend.h> 33 #include <target/target_core_fabric.h> 34 35 #include "target_core_internal.h" 36 #include "target_core_alua.h" 37 #include "target_core_pr.h" 38 #include "target_core_ua.h" 39 40 #define CREATE_TRACE_POINTS 41 #include <trace/events/target.h> 42 43 static struct workqueue_struct *target_completion_wq; 44 static struct workqueue_struct *target_submission_wq; 45 static struct kmem_cache *se_sess_cache; 46 struct kmem_cache *se_ua_cache; 47 struct kmem_cache *t10_pr_reg_cache; 48 struct kmem_cache *t10_alua_lu_gp_cache; 49 struct kmem_cache *t10_alua_lu_gp_mem_cache; 50 struct kmem_cache *t10_alua_tg_pt_gp_cache; 51 struct kmem_cache *t10_alua_lba_map_cache; 52 struct kmem_cache *t10_alua_lba_map_mem_cache; 53 54 static void transport_complete_task_attr(struct se_cmd *cmd); 55 static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason); 56 static void transport_handle_queue_full(struct se_cmd *cmd, 57 struct se_device *dev, int err, bool write_pending); 58 static void target_complete_ok_work(struct work_struct *work); 59 60 int init_se_kmem_caches(void) 61 { 62 se_sess_cache = kmem_cache_create("se_sess_cache", 63 sizeof(struct se_session), __alignof__(struct se_session), 64 0, NULL); 65 if (!se_sess_cache) { 66 pr_err("kmem_cache_create() for struct se_session" 67 " failed\n"); 68 goto out; 69 } 70 se_ua_cache = kmem_cache_create("se_ua_cache", 71 sizeof(struct se_ua), __alignof__(struct se_ua), 72 0, NULL); 73 if (!se_ua_cache) { 74 pr_err("kmem_cache_create() for struct se_ua failed\n"); 75 goto out_free_sess_cache; 76 } 77 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache", 78 sizeof(struct t10_pr_registration), 79 __alignof__(struct t10_pr_registration), 0, NULL); 80 if (!t10_pr_reg_cache) { 81 pr_err("kmem_cache_create() for struct t10_pr_registration" 82 " failed\n"); 83 goto out_free_ua_cache; 84 } 85 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache", 86 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp), 87 0, NULL); 88 if (!t10_alua_lu_gp_cache) { 89 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache" 90 " failed\n"); 91 goto out_free_pr_reg_cache; 92 } 93 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache", 94 sizeof(struct t10_alua_lu_gp_member), 95 __alignof__(struct t10_alua_lu_gp_member), 0, NULL); 96 if (!t10_alua_lu_gp_mem_cache) { 97 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_" 98 "cache failed\n"); 99 goto out_free_lu_gp_cache; 100 } 101 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache", 102 sizeof(struct t10_alua_tg_pt_gp), 103 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL); 104 if (!t10_alua_tg_pt_gp_cache) { 105 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" 106 "cache failed\n"); 107 goto out_free_lu_gp_mem_cache; 108 } 109 t10_alua_lba_map_cache = kmem_cache_create( 110 "t10_alua_lba_map_cache", 111 sizeof(struct t10_alua_lba_map), 112 __alignof__(struct t10_alua_lba_map), 0, NULL); 113 if (!t10_alua_lba_map_cache) { 114 pr_err("kmem_cache_create() for t10_alua_lba_map_" 115 "cache failed\n"); 116 goto out_free_tg_pt_gp_cache; 117 } 118 t10_alua_lba_map_mem_cache = kmem_cache_create( 119 "t10_alua_lba_map_mem_cache", 120 sizeof(struct t10_alua_lba_map_member), 121 __alignof__(struct t10_alua_lba_map_member), 0, NULL); 122 if (!t10_alua_lba_map_mem_cache) { 123 pr_err("kmem_cache_create() for t10_alua_lba_map_mem_" 124 "cache failed\n"); 125 goto out_free_lba_map_cache; 126 } 127 128 target_completion_wq = alloc_workqueue("target_completion", 129 WQ_MEM_RECLAIM, 0); 130 if (!target_completion_wq) 131 goto out_free_lba_map_mem_cache; 132 133 target_submission_wq = alloc_workqueue("target_submission", 134 WQ_MEM_RECLAIM, 0); 135 if (!target_submission_wq) 136 goto out_free_completion_wq; 137 138 return 0; 139 140 out_free_completion_wq: 141 destroy_workqueue(target_completion_wq); 142 out_free_lba_map_mem_cache: 143 kmem_cache_destroy(t10_alua_lba_map_mem_cache); 144 out_free_lba_map_cache: 145 kmem_cache_destroy(t10_alua_lba_map_cache); 146 out_free_tg_pt_gp_cache: 147 kmem_cache_destroy(t10_alua_tg_pt_gp_cache); 148 out_free_lu_gp_mem_cache: 149 kmem_cache_destroy(t10_alua_lu_gp_mem_cache); 150 out_free_lu_gp_cache: 151 kmem_cache_destroy(t10_alua_lu_gp_cache); 152 out_free_pr_reg_cache: 153 kmem_cache_destroy(t10_pr_reg_cache); 154 out_free_ua_cache: 155 kmem_cache_destroy(se_ua_cache); 156 out_free_sess_cache: 157 kmem_cache_destroy(se_sess_cache); 158 out: 159 return -ENOMEM; 160 } 161 162 void release_se_kmem_caches(void) 163 { 164 destroy_workqueue(target_submission_wq); 165 destroy_workqueue(target_completion_wq); 166 kmem_cache_destroy(se_sess_cache); 167 kmem_cache_destroy(se_ua_cache); 168 kmem_cache_destroy(t10_pr_reg_cache); 169 kmem_cache_destroy(t10_alua_lu_gp_cache); 170 kmem_cache_destroy(t10_alua_lu_gp_mem_cache); 171 kmem_cache_destroy(t10_alua_tg_pt_gp_cache); 172 kmem_cache_destroy(t10_alua_lba_map_cache); 173 kmem_cache_destroy(t10_alua_lba_map_mem_cache); 174 } 175 176 /* This code ensures unique mib indexes are handed out. */ 177 static DEFINE_SPINLOCK(scsi_mib_index_lock); 178 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX]; 179 180 /* 181 * Allocate a new row index for the entry type specified 182 */ 183 u32 scsi_get_new_index(scsi_index_t type) 184 { 185 u32 new_index; 186 187 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)); 188 189 spin_lock(&scsi_mib_index_lock); 190 new_index = ++scsi_mib_index[type]; 191 spin_unlock(&scsi_mib_index_lock); 192 193 return new_index; 194 } 195 196 void transport_subsystem_check_init(void) 197 { 198 int ret; 199 static int sub_api_initialized; 200 201 if (sub_api_initialized) 202 return; 203 204 ret = IS_ENABLED(CONFIG_TCM_IBLOCK) && request_module("target_core_iblock"); 205 if (ret != 0) 206 pr_err("Unable to load target_core_iblock\n"); 207 208 ret = IS_ENABLED(CONFIG_TCM_FILEIO) && request_module("target_core_file"); 209 if (ret != 0) 210 pr_err("Unable to load target_core_file\n"); 211 212 ret = IS_ENABLED(CONFIG_TCM_PSCSI) && request_module("target_core_pscsi"); 213 if (ret != 0) 214 pr_err("Unable to load target_core_pscsi\n"); 215 216 ret = IS_ENABLED(CONFIG_TCM_USER2) && request_module("target_core_user"); 217 if (ret != 0) 218 pr_err("Unable to load target_core_user\n"); 219 220 sub_api_initialized = 1; 221 } 222 223 static void target_release_sess_cmd_refcnt(struct percpu_ref *ref) 224 { 225 struct se_session *sess = container_of(ref, typeof(*sess), cmd_count); 226 227 wake_up(&sess->cmd_count_wq); 228 } 229 230 /** 231 * transport_init_session - initialize a session object 232 * @se_sess: Session object pointer. 233 * 234 * The caller must have zero-initialized @se_sess before calling this function. 235 */ 236 int transport_init_session(struct se_session *se_sess) 237 { 238 INIT_LIST_HEAD(&se_sess->sess_list); 239 INIT_LIST_HEAD(&se_sess->sess_acl_list); 240 spin_lock_init(&se_sess->sess_cmd_lock); 241 init_waitqueue_head(&se_sess->cmd_count_wq); 242 init_completion(&se_sess->stop_done); 243 atomic_set(&se_sess->stopped, 0); 244 return percpu_ref_init(&se_sess->cmd_count, 245 target_release_sess_cmd_refcnt, 0, GFP_KERNEL); 246 } 247 EXPORT_SYMBOL(transport_init_session); 248 249 void transport_uninit_session(struct se_session *se_sess) 250 { 251 /* 252 * Drivers like iscsi and loop do not call target_stop_session 253 * during session shutdown so we have to drop the ref taken at init 254 * time here. 255 */ 256 if (!atomic_read(&se_sess->stopped)) 257 percpu_ref_put(&se_sess->cmd_count); 258 259 percpu_ref_exit(&se_sess->cmd_count); 260 } 261 262 /** 263 * transport_alloc_session - allocate a session object and initialize it 264 * @sup_prot_ops: bitmask that defines which T10-PI modes are supported. 265 */ 266 struct se_session *transport_alloc_session(enum target_prot_op sup_prot_ops) 267 { 268 struct se_session *se_sess; 269 int ret; 270 271 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL); 272 if (!se_sess) { 273 pr_err("Unable to allocate struct se_session from" 274 " se_sess_cache\n"); 275 return ERR_PTR(-ENOMEM); 276 } 277 ret = transport_init_session(se_sess); 278 if (ret < 0) { 279 kmem_cache_free(se_sess_cache, se_sess); 280 return ERR_PTR(ret); 281 } 282 se_sess->sup_prot_ops = sup_prot_ops; 283 284 return se_sess; 285 } 286 EXPORT_SYMBOL(transport_alloc_session); 287 288 /** 289 * transport_alloc_session_tags - allocate target driver private data 290 * @se_sess: Session pointer. 291 * @tag_num: Maximum number of in-flight commands between initiator and target. 292 * @tag_size: Size in bytes of the private data a target driver associates with 293 * each command. 294 */ 295 int transport_alloc_session_tags(struct se_session *se_sess, 296 unsigned int tag_num, unsigned int tag_size) 297 { 298 int rc; 299 300 se_sess->sess_cmd_map = kvcalloc(tag_size, tag_num, 301 GFP_KERNEL | __GFP_RETRY_MAYFAIL); 302 if (!se_sess->sess_cmd_map) { 303 pr_err("Unable to allocate se_sess->sess_cmd_map\n"); 304 return -ENOMEM; 305 } 306 307 rc = sbitmap_queue_init_node(&se_sess->sess_tag_pool, tag_num, -1, 308 false, GFP_KERNEL, NUMA_NO_NODE); 309 if (rc < 0) { 310 pr_err("Unable to init se_sess->sess_tag_pool," 311 " tag_num: %u\n", tag_num); 312 kvfree(se_sess->sess_cmd_map); 313 se_sess->sess_cmd_map = NULL; 314 return -ENOMEM; 315 } 316 317 return 0; 318 } 319 EXPORT_SYMBOL(transport_alloc_session_tags); 320 321 /** 322 * transport_init_session_tags - allocate a session and target driver private data 323 * @tag_num: Maximum number of in-flight commands between initiator and target. 324 * @tag_size: Size in bytes of the private data a target driver associates with 325 * each command. 326 * @sup_prot_ops: bitmask that defines which T10-PI modes are supported. 327 */ 328 static struct se_session * 329 transport_init_session_tags(unsigned int tag_num, unsigned int tag_size, 330 enum target_prot_op sup_prot_ops) 331 { 332 struct se_session *se_sess; 333 int rc; 334 335 if (tag_num != 0 && !tag_size) { 336 pr_err("init_session_tags called with percpu-ida tag_num:" 337 " %u, but zero tag_size\n", tag_num); 338 return ERR_PTR(-EINVAL); 339 } 340 if (!tag_num && tag_size) { 341 pr_err("init_session_tags called with percpu-ida tag_size:" 342 " %u, but zero tag_num\n", tag_size); 343 return ERR_PTR(-EINVAL); 344 } 345 346 se_sess = transport_alloc_session(sup_prot_ops); 347 if (IS_ERR(se_sess)) 348 return se_sess; 349 350 rc = transport_alloc_session_tags(se_sess, tag_num, tag_size); 351 if (rc < 0) { 352 transport_free_session(se_sess); 353 return ERR_PTR(-ENOMEM); 354 } 355 356 return se_sess; 357 } 358 359 /* 360 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called. 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 const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo; 369 unsigned char buf[PR_REG_ISID_LEN]; 370 unsigned long flags; 371 372 se_sess->se_tpg = se_tpg; 373 se_sess->fabric_sess_ptr = fabric_sess_ptr; 374 /* 375 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t 376 * 377 * Only set for struct se_session's that will actually be moving I/O. 378 * eg: *NOT* discovery sessions. 379 */ 380 if (se_nacl) { 381 /* 382 * 383 * Determine if fabric allows for T10-PI feature bits exposed to 384 * initiators for device backends with !dev->dev_attrib.pi_prot_type. 385 * 386 * If so, then always save prot_type on a per se_node_acl node 387 * basis and re-instate the previous sess_prot_type to avoid 388 * disabling PI from below any previously initiator side 389 * registered LUNs. 390 */ 391 if (se_nacl->saved_prot_type) 392 se_sess->sess_prot_type = se_nacl->saved_prot_type; 393 else if (tfo->tpg_check_prot_fabric_only) 394 se_sess->sess_prot_type = se_nacl->saved_prot_type = 395 tfo->tpg_check_prot_fabric_only(se_tpg); 396 /* 397 * If the fabric module supports an ISID based TransportID, 398 * save this value in binary from the fabric I_T Nexus now. 399 */ 400 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { 401 memset(&buf[0], 0, PR_REG_ISID_LEN); 402 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, 403 &buf[0], PR_REG_ISID_LEN); 404 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]); 405 } 406 407 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); 408 /* 409 * The se_nacl->nacl_sess pointer will be set to the 410 * last active I_T Nexus for each struct se_node_acl. 411 */ 412 se_nacl->nacl_sess = se_sess; 413 414 list_add_tail(&se_sess->sess_acl_list, 415 &se_nacl->acl_sess_list); 416 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); 417 } 418 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list); 419 420 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n", 421 se_tpg->se_tpg_tfo->fabric_name, se_sess->fabric_sess_ptr); 422 } 423 EXPORT_SYMBOL(__transport_register_session); 424 425 void transport_register_session( 426 struct se_portal_group *se_tpg, 427 struct se_node_acl *se_nacl, 428 struct se_session *se_sess, 429 void *fabric_sess_ptr) 430 { 431 unsigned long flags; 432 433 spin_lock_irqsave(&se_tpg->session_lock, flags); 434 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr); 435 spin_unlock_irqrestore(&se_tpg->session_lock, flags); 436 } 437 EXPORT_SYMBOL(transport_register_session); 438 439 struct se_session * 440 target_setup_session(struct se_portal_group *tpg, 441 unsigned int tag_num, unsigned int tag_size, 442 enum target_prot_op prot_op, 443 const char *initiatorname, void *private, 444 int (*callback)(struct se_portal_group *, 445 struct se_session *, void *)) 446 { 447 struct se_session *sess; 448 449 /* 450 * If the fabric driver is using percpu-ida based pre allocation 451 * of I/O descriptor tags, go ahead and perform that setup now.. 452 */ 453 if (tag_num != 0) 454 sess = transport_init_session_tags(tag_num, tag_size, prot_op); 455 else 456 sess = transport_alloc_session(prot_op); 457 458 if (IS_ERR(sess)) 459 return sess; 460 461 sess->se_node_acl = core_tpg_check_initiator_node_acl(tpg, 462 (unsigned char *)initiatorname); 463 if (!sess->se_node_acl) { 464 transport_free_session(sess); 465 return ERR_PTR(-EACCES); 466 } 467 /* 468 * Go ahead and perform any remaining fabric setup that is 469 * required before transport_register_session(). 470 */ 471 if (callback != NULL) { 472 int rc = callback(tpg, sess, private); 473 if (rc) { 474 transport_free_session(sess); 475 return ERR_PTR(rc); 476 } 477 } 478 479 transport_register_session(tpg, sess->se_node_acl, sess, private); 480 return sess; 481 } 482 EXPORT_SYMBOL(target_setup_session); 483 484 ssize_t target_show_dynamic_sessions(struct se_portal_group *se_tpg, char *page) 485 { 486 struct se_session *se_sess; 487 ssize_t len = 0; 488 489 spin_lock_bh(&se_tpg->session_lock); 490 list_for_each_entry(se_sess, &se_tpg->tpg_sess_list, sess_list) { 491 if (!se_sess->se_node_acl) 492 continue; 493 if (!se_sess->se_node_acl->dynamic_node_acl) 494 continue; 495 if (strlen(se_sess->se_node_acl->initiatorname) + 1 + len > PAGE_SIZE) 496 break; 497 498 len += snprintf(page + len, PAGE_SIZE - len, "%s\n", 499 se_sess->se_node_acl->initiatorname); 500 len += 1; /* Include NULL terminator */ 501 } 502 spin_unlock_bh(&se_tpg->session_lock); 503 504 return len; 505 } 506 EXPORT_SYMBOL(target_show_dynamic_sessions); 507 508 static void target_complete_nacl(struct kref *kref) 509 { 510 struct se_node_acl *nacl = container_of(kref, 511 struct se_node_acl, acl_kref); 512 struct se_portal_group *se_tpg = nacl->se_tpg; 513 514 if (!nacl->dynamic_stop) { 515 complete(&nacl->acl_free_comp); 516 return; 517 } 518 519 mutex_lock(&se_tpg->acl_node_mutex); 520 list_del_init(&nacl->acl_list); 521 mutex_unlock(&se_tpg->acl_node_mutex); 522 523 core_tpg_wait_for_nacl_pr_ref(nacl); 524 core_free_device_list_for_node(nacl, se_tpg); 525 kfree(nacl); 526 } 527 528 void target_put_nacl(struct se_node_acl *nacl) 529 { 530 kref_put(&nacl->acl_kref, target_complete_nacl); 531 } 532 EXPORT_SYMBOL(target_put_nacl); 533 534 void transport_deregister_session_configfs(struct se_session *se_sess) 535 { 536 struct se_node_acl *se_nacl; 537 unsigned long flags; 538 /* 539 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session 540 */ 541 se_nacl = se_sess->se_node_acl; 542 if (se_nacl) { 543 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); 544 if (!list_empty(&se_sess->sess_acl_list)) 545 list_del_init(&se_sess->sess_acl_list); 546 /* 547 * If the session list is empty, then clear the pointer. 548 * Otherwise, set the struct se_session pointer from the tail 549 * element of the per struct se_node_acl active session list. 550 */ 551 if (list_empty(&se_nacl->acl_sess_list)) 552 se_nacl->nacl_sess = NULL; 553 else { 554 se_nacl->nacl_sess = container_of( 555 se_nacl->acl_sess_list.prev, 556 struct se_session, sess_acl_list); 557 } 558 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); 559 } 560 } 561 EXPORT_SYMBOL(transport_deregister_session_configfs); 562 563 void transport_free_session(struct se_session *se_sess) 564 { 565 struct se_node_acl *se_nacl = se_sess->se_node_acl; 566 567 /* 568 * Drop the se_node_acl->nacl_kref obtained from within 569 * core_tpg_get_initiator_node_acl(). 570 */ 571 if (se_nacl) { 572 struct se_portal_group *se_tpg = se_nacl->se_tpg; 573 const struct target_core_fabric_ops *se_tfo = se_tpg->se_tpg_tfo; 574 unsigned long flags; 575 576 se_sess->se_node_acl = NULL; 577 578 /* 579 * Also determine if we need to drop the extra ->cmd_kref if 580 * it had been previously dynamically generated, and 581 * the endpoint is not caching dynamic ACLs. 582 */ 583 mutex_lock(&se_tpg->acl_node_mutex); 584 if (se_nacl->dynamic_node_acl && 585 !se_tfo->tpg_check_demo_mode_cache(se_tpg)) { 586 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); 587 if (list_empty(&se_nacl->acl_sess_list)) 588 se_nacl->dynamic_stop = true; 589 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); 590 591 if (se_nacl->dynamic_stop) 592 list_del_init(&se_nacl->acl_list); 593 } 594 mutex_unlock(&se_tpg->acl_node_mutex); 595 596 if (se_nacl->dynamic_stop) 597 target_put_nacl(se_nacl); 598 599 target_put_nacl(se_nacl); 600 } 601 if (se_sess->sess_cmd_map) { 602 sbitmap_queue_free(&se_sess->sess_tag_pool); 603 kvfree(se_sess->sess_cmd_map); 604 } 605 transport_uninit_session(se_sess); 606 kmem_cache_free(se_sess_cache, se_sess); 607 } 608 EXPORT_SYMBOL(transport_free_session); 609 610 static int target_release_res(struct se_device *dev, void *data) 611 { 612 struct se_session *sess = data; 613 614 if (dev->reservation_holder == sess) 615 target_release_reservation(dev); 616 return 0; 617 } 618 619 void transport_deregister_session(struct se_session *se_sess) 620 { 621 struct se_portal_group *se_tpg = se_sess->se_tpg; 622 unsigned long flags; 623 624 if (!se_tpg) { 625 transport_free_session(se_sess); 626 return; 627 } 628 629 spin_lock_irqsave(&se_tpg->session_lock, flags); 630 list_del(&se_sess->sess_list); 631 se_sess->se_tpg = NULL; 632 se_sess->fabric_sess_ptr = NULL; 633 spin_unlock_irqrestore(&se_tpg->session_lock, flags); 634 635 /* 636 * Since the session is being removed, release SPC-2 637 * reservations held by the session that is disappearing. 638 */ 639 target_for_each_device(target_release_res, se_sess); 640 641 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", 642 se_tpg->se_tpg_tfo->fabric_name); 643 /* 644 * If last kref is dropping now for an explicit NodeACL, awake sleeping 645 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group 646 * removal context from within transport_free_session() code. 647 * 648 * For dynamic ACL, target_put_nacl() uses target_complete_nacl() 649 * to release all remaining generate_node_acl=1 created ACL resources. 650 */ 651 652 transport_free_session(se_sess); 653 } 654 EXPORT_SYMBOL(transport_deregister_session); 655 656 void target_remove_session(struct se_session *se_sess) 657 { 658 transport_deregister_session_configfs(se_sess); 659 transport_deregister_session(se_sess); 660 } 661 EXPORT_SYMBOL(target_remove_session); 662 663 static void target_remove_from_state_list(struct se_cmd *cmd) 664 { 665 struct se_device *dev = cmd->se_dev; 666 unsigned long flags; 667 668 if (!dev) 669 return; 670 671 spin_lock_irqsave(&dev->queues[cmd->cpuid].lock, flags); 672 if (cmd->state_active) { 673 list_del(&cmd->state_list); 674 cmd->state_active = false; 675 } 676 spin_unlock_irqrestore(&dev->queues[cmd->cpuid].lock, flags); 677 } 678 679 /* 680 * This function is called by the target core after the target core has 681 * finished processing a SCSI command or SCSI TMF. Both the regular command 682 * processing code and the code for aborting commands can call this 683 * function. CMD_T_STOP is set if and only if another thread is waiting 684 * inside transport_wait_for_tasks() for t_transport_stop_comp. 685 */ 686 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd) 687 { 688 unsigned long flags; 689 690 target_remove_from_state_list(cmd); 691 692 /* 693 * Clear struct se_cmd->se_lun before the handoff to FE. 694 */ 695 cmd->se_lun = NULL; 696 697 spin_lock_irqsave(&cmd->t_state_lock, flags); 698 /* 699 * Determine if frontend context caller is requesting the stopping of 700 * this command for frontend exceptions. 701 */ 702 if (cmd->transport_state & CMD_T_STOP) { 703 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n", 704 __func__, __LINE__, cmd->tag); 705 706 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 707 708 complete_all(&cmd->t_transport_stop_comp); 709 return 1; 710 } 711 cmd->transport_state &= ~CMD_T_ACTIVE; 712 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 713 714 /* 715 * Some fabric modules like tcm_loop can release their internally 716 * allocated I/O reference and struct se_cmd now. 717 * 718 * Fabric modules are expected to return '1' here if the se_cmd being 719 * passed is released at this point, or zero if not being released. 720 */ 721 return cmd->se_tfo->check_stop_free(cmd); 722 } 723 724 static void transport_lun_remove_cmd(struct se_cmd *cmd) 725 { 726 struct se_lun *lun = cmd->se_lun; 727 728 if (!lun) 729 return; 730 731 if (cmpxchg(&cmd->lun_ref_active, true, false)) 732 percpu_ref_put(&lun->lun_ref); 733 } 734 735 static void target_complete_failure_work(struct work_struct *work) 736 { 737 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 738 739 transport_generic_request_failure(cmd, cmd->sense_reason); 740 } 741 742 /* 743 * Used when asking transport to copy Sense Data from the underlying 744 * Linux/SCSI struct scsi_cmnd 745 */ 746 static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd) 747 { 748 struct se_device *dev = cmd->se_dev; 749 750 WARN_ON(!cmd->se_lun); 751 752 if (!dev) 753 return NULL; 754 755 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) 756 return NULL; 757 758 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; 759 760 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n", 761 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status); 762 return cmd->sense_buffer; 763 } 764 765 void transport_copy_sense_to_cmd(struct se_cmd *cmd, unsigned char *sense) 766 { 767 unsigned char *cmd_sense_buf; 768 unsigned long flags; 769 770 spin_lock_irqsave(&cmd->t_state_lock, flags); 771 cmd_sense_buf = transport_get_sense_buffer(cmd); 772 if (!cmd_sense_buf) { 773 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 774 return; 775 } 776 777 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE; 778 memcpy(cmd_sense_buf, sense, cmd->scsi_sense_length); 779 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 780 } 781 EXPORT_SYMBOL(transport_copy_sense_to_cmd); 782 783 static void target_handle_abort(struct se_cmd *cmd) 784 { 785 bool tas = cmd->transport_state & CMD_T_TAS; 786 bool ack_kref = cmd->se_cmd_flags & SCF_ACK_KREF; 787 int ret; 788 789 pr_debug("tag %#llx: send_abort_response = %d\n", cmd->tag, tas); 790 791 if (tas) { 792 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { 793 cmd->scsi_status = SAM_STAT_TASK_ABORTED; 794 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x, ITT: 0x%08llx\n", 795 cmd->t_task_cdb[0], cmd->tag); 796 trace_target_cmd_complete(cmd); 797 ret = cmd->se_tfo->queue_status(cmd); 798 if (ret) { 799 transport_handle_queue_full(cmd, cmd->se_dev, 800 ret, false); 801 return; 802 } 803 } else { 804 cmd->se_tmr_req->response = TMR_FUNCTION_REJECTED; 805 cmd->se_tfo->queue_tm_rsp(cmd); 806 } 807 } else { 808 /* 809 * Allow the fabric driver to unmap any resources before 810 * releasing the descriptor via TFO->release_cmd(). 811 */ 812 cmd->se_tfo->aborted_task(cmd); 813 if (ack_kref) 814 WARN_ON_ONCE(target_put_sess_cmd(cmd) != 0); 815 /* 816 * To do: establish a unit attention condition on the I_T 817 * nexus associated with cmd. See also the paragraph "Aborting 818 * commands" in SAM. 819 */ 820 } 821 822 WARN_ON_ONCE(kref_read(&cmd->cmd_kref) == 0); 823 824 transport_lun_remove_cmd(cmd); 825 826 transport_cmd_check_stop_to_fabric(cmd); 827 } 828 829 static void target_abort_work(struct work_struct *work) 830 { 831 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 832 833 target_handle_abort(cmd); 834 } 835 836 static bool target_cmd_interrupted(struct se_cmd *cmd) 837 { 838 int post_ret; 839 840 if (cmd->transport_state & CMD_T_ABORTED) { 841 if (cmd->transport_complete_callback) 842 cmd->transport_complete_callback(cmd, false, &post_ret); 843 INIT_WORK(&cmd->work, target_abort_work); 844 queue_work(target_completion_wq, &cmd->work); 845 return true; 846 } else if (cmd->transport_state & CMD_T_STOP) { 847 if (cmd->transport_complete_callback) 848 cmd->transport_complete_callback(cmd, false, &post_ret); 849 complete_all(&cmd->t_transport_stop_comp); 850 return true; 851 } 852 853 return false; 854 } 855 856 /* May be called from interrupt context so must not sleep. */ 857 void target_complete_cmd_with_sense(struct se_cmd *cmd, u8 scsi_status, 858 sense_reason_t sense_reason) 859 { 860 struct se_wwn *wwn = cmd->se_sess->se_tpg->se_tpg_wwn; 861 int success, cpu; 862 unsigned long flags; 863 864 if (target_cmd_interrupted(cmd)) 865 return; 866 867 cmd->scsi_status = scsi_status; 868 cmd->sense_reason = sense_reason; 869 870 spin_lock_irqsave(&cmd->t_state_lock, flags); 871 switch (cmd->scsi_status) { 872 case SAM_STAT_CHECK_CONDITION: 873 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) 874 success = 1; 875 else 876 success = 0; 877 break; 878 default: 879 success = 1; 880 break; 881 } 882 883 cmd->t_state = TRANSPORT_COMPLETE; 884 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE); 885 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 886 887 INIT_WORK(&cmd->work, success ? target_complete_ok_work : 888 target_complete_failure_work); 889 890 if (!wwn || wwn->cmd_compl_affinity == SE_COMPL_AFFINITY_CPUID) 891 cpu = cmd->cpuid; 892 else 893 cpu = wwn->cmd_compl_affinity; 894 895 queue_work_on(cpu, target_completion_wq, &cmd->work); 896 } 897 EXPORT_SYMBOL(target_complete_cmd_with_sense); 898 899 void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status) 900 { 901 target_complete_cmd_with_sense(cmd, scsi_status, scsi_status ? 902 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE : 903 TCM_NO_SENSE); 904 } 905 EXPORT_SYMBOL(target_complete_cmd); 906 907 void target_set_cmd_data_length(struct se_cmd *cmd, int length) 908 { 909 if (length < cmd->data_length) { 910 if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { 911 cmd->residual_count += cmd->data_length - length; 912 } else { 913 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; 914 cmd->residual_count = cmd->data_length - length; 915 } 916 917 cmd->data_length = length; 918 } 919 } 920 EXPORT_SYMBOL(target_set_cmd_data_length); 921 922 void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length) 923 { 924 if (scsi_status == SAM_STAT_GOOD || 925 cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) { 926 target_set_cmd_data_length(cmd, length); 927 } 928 929 target_complete_cmd(cmd, scsi_status); 930 } 931 EXPORT_SYMBOL(target_complete_cmd_with_length); 932 933 static void target_add_to_state_list(struct se_cmd *cmd) 934 { 935 struct se_device *dev = cmd->se_dev; 936 unsigned long flags; 937 938 spin_lock_irqsave(&dev->queues[cmd->cpuid].lock, flags); 939 if (!cmd->state_active) { 940 list_add_tail(&cmd->state_list, 941 &dev->queues[cmd->cpuid].state_list); 942 cmd->state_active = true; 943 } 944 spin_unlock_irqrestore(&dev->queues[cmd->cpuid].lock, flags); 945 } 946 947 /* 948 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status 949 */ 950 static void transport_write_pending_qf(struct se_cmd *cmd); 951 static void transport_complete_qf(struct se_cmd *cmd); 952 953 void target_qf_do_work(struct work_struct *work) 954 { 955 struct se_device *dev = container_of(work, struct se_device, 956 qf_work_queue); 957 LIST_HEAD(qf_cmd_list); 958 struct se_cmd *cmd, *cmd_tmp; 959 960 spin_lock_irq(&dev->qf_cmd_lock); 961 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list); 962 spin_unlock_irq(&dev->qf_cmd_lock); 963 964 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) { 965 list_del(&cmd->se_qf_node); 966 atomic_dec_mb(&dev->dev_qf_count); 967 968 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue" 969 " context: %s\n", cmd->se_tfo->fabric_name, cmd, 970 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" : 971 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING" 972 : "UNKNOWN"); 973 974 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) 975 transport_write_pending_qf(cmd); 976 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK || 977 cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) 978 transport_complete_qf(cmd); 979 } 980 } 981 982 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd) 983 { 984 switch (cmd->data_direction) { 985 case DMA_NONE: 986 return "NONE"; 987 case DMA_FROM_DEVICE: 988 return "READ"; 989 case DMA_TO_DEVICE: 990 return "WRITE"; 991 case DMA_BIDIRECTIONAL: 992 return "BIDI"; 993 default: 994 break; 995 } 996 997 return "UNKNOWN"; 998 } 999 1000 void transport_dump_dev_state( 1001 struct se_device *dev, 1002 char *b, 1003 int *bl) 1004 { 1005 *bl += sprintf(b + *bl, "Status: "); 1006 if (dev->export_count) 1007 *bl += sprintf(b + *bl, "ACTIVATED"); 1008 else 1009 *bl += sprintf(b + *bl, "DEACTIVATED"); 1010 1011 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth); 1012 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n", 1013 dev->dev_attrib.block_size, 1014 dev->dev_attrib.hw_max_sectors); 1015 *bl += sprintf(b + *bl, " "); 1016 } 1017 1018 void transport_dump_vpd_proto_id( 1019 struct t10_vpd *vpd, 1020 unsigned char *p_buf, 1021 int p_buf_len) 1022 { 1023 unsigned char buf[VPD_TMP_BUF_SIZE]; 1024 int len; 1025 1026 memset(buf, 0, VPD_TMP_BUF_SIZE); 1027 len = sprintf(buf, "T10 VPD Protocol Identifier: "); 1028 1029 switch (vpd->protocol_identifier) { 1030 case 0x00: 1031 sprintf(buf+len, "Fibre Channel\n"); 1032 break; 1033 case 0x10: 1034 sprintf(buf+len, "Parallel SCSI\n"); 1035 break; 1036 case 0x20: 1037 sprintf(buf+len, "SSA\n"); 1038 break; 1039 case 0x30: 1040 sprintf(buf+len, "IEEE 1394\n"); 1041 break; 1042 case 0x40: 1043 sprintf(buf+len, "SCSI Remote Direct Memory Access" 1044 " Protocol\n"); 1045 break; 1046 case 0x50: 1047 sprintf(buf+len, "Internet SCSI (iSCSI)\n"); 1048 break; 1049 case 0x60: 1050 sprintf(buf+len, "SAS Serial SCSI Protocol\n"); 1051 break; 1052 case 0x70: 1053 sprintf(buf+len, "Automation/Drive Interface Transport" 1054 " Protocol\n"); 1055 break; 1056 case 0x80: 1057 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n"); 1058 break; 1059 default: 1060 sprintf(buf+len, "Unknown 0x%02x\n", 1061 vpd->protocol_identifier); 1062 break; 1063 } 1064 1065 if (p_buf) 1066 strncpy(p_buf, buf, p_buf_len); 1067 else 1068 pr_debug("%s", buf); 1069 } 1070 1071 void 1072 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83) 1073 { 1074 /* 1075 * Check if the Protocol Identifier Valid (PIV) bit is set.. 1076 * 1077 * from spc3r23.pdf section 7.5.1 1078 */ 1079 if (page_83[1] & 0x80) { 1080 vpd->protocol_identifier = (page_83[0] & 0xf0); 1081 vpd->protocol_identifier_set = 1; 1082 transport_dump_vpd_proto_id(vpd, NULL, 0); 1083 } 1084 } 1085 EXPORT_SYMBOL(transport_set_vpd_proto_id); 1086 1087 int transport_dump_vpd_assoc( 1088 struct t10_vpd *vpd, 1089 unsigned char *p_buf, 1090 int p_buf_len) 1091 { 1092 unsigned char buf[VPD_TMP_BUF_SIZE]; 1093 int ret = 0; 1094 int len; 1095 1096 memset(buf, 0, VPD_TMP_BUF_SIZE); 1097 len = sprintf(buf, "T10 VPD Identifier Association: "); 1098 1099 switch (vpd->association) { 1100 case 0x00: 1101 sprintf(buf+len, "addressed logical unit\n"); 1102 break; 1103 case 0x10: 1104 sprintf(buf+len, "target port\n"); 1105 break; 1106 case 0x20: 1107 sprintf(buf+len, "SCSI target device\n"); 1108 break; 1109 default: 1110 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association); 1111 ret = -EINVAL; 1112 break; 1113 } 1114 1115 if (p_buf) 1116 strncpy(p_buf, buf, p_buf_len); 1117 else 1118 pr_debug("%s", buf); 1119 1120 return ret; 1121 } 1122 1123 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83) 1124 { 1125 /* 1126 * The VPD identification association.. 1127 * 1128 * from spc3r23.pdf Section 7.6.3.1 Table 297 1129 */ 1130 vpd->association = (page_83[1] & 0x30); 1131 return transport_dump_vpd_assoc(vpd, NULL, 0); 1132 } 1133 EXPORT_SYMBOL(transport_set_vpd_assoc); 1134 1135 int transport_dump_vpd_ident_type( 1136 struct t10_vpd *vpd, 1137 unsigned char *p_buf, 1138 int p_buf_len) 1139 { 1140 unsigned char buf[VPD_TMP_BUF_SIZE]; 1141 int ret = 0; 1142 int len; 1143 1144 memset(buf, 0, VPD_TMP_BUF_SIZE); 1145 len = sprintf(buf, "T10 VPD Identifier Type: "); 1146 1147 switch (vpd->device_identifier_type) { 1148 case 0x00: 1149 sprintf(buf+len, "Vendor specific\n"); 1150 break; 1151 case 0x01: 1152 sprintf(buf+len, "T10 Vendor ID based\n"); 1153 break; 1154 case 0x02: 1155 sprintf(buf+len, "EUI-64 based\n"); 1156 break; 1157 case 0x03: 1158 sprintf(buf+len, "NAA\n"); 1159 break; 1160 case 0x04: 1161 sprintf(buf+len, "Relative target port identifier\n"); 1162 break; 1163 case 0x08: 1164 sprintf(buf+len, "SCSI name string\n"); 1165 break; 1166 default: 1167 sprintf(buf+len, "Unsupported: 0x%02x\n", 1168 vpd->device_identifier_type); 1169 ret = -EINVAL; 1170 break; 1171 } 1172 1173 if (p_buf) { 1174 if (p_buf_len < strlen(buf)+1) 1175 return -EINVAL; 1176 strncpy(p_buf, buf, p_buf_len); 1177 } else { 1178 pr_debug("%s", buf); 1179 } 1180 1181 return ret; 1182 } 1183 1184 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83) 1185 { 1186 /* 1187 * The VPD identifier type.. 1188 * 1189 * from spc3r23.pdf Section 7.6.3.1 Table 298 1190 */ 1191 vpd->device_identifier_type = (page_83[1] & 0x0f); 1192 return transport_dump_vpd_ident_type(vpd, NULL, 0); 1193 } 1194 EXPORT_SYMBOL(transport_set_vpd_ident_type); 1195 1196 int transport_dump_vpd_ident( 1197 struct t10_vpd *vpd, 1198 unsigned char *p_buf, 1199 int p_buf_len) 1200 { 1201 unsigned char buf[VPD_TMP_BUF_SIZE]; 1202 int ret = 0; 1203 1204 memset(buf, 0, VPD_TMP_BUF_SIZE); 1205 1206 switch (vpd->device_identifier_code_set) { 1207 case 0x01: /* Binary */ 1208 snprintf(buf, sizeof(buf), 1209 "T10 VPD Binary Device Identifier: %s\n", 1210 &vpd->device_identifier[0]); 1211 break; 1212 case 0x02: /* ASCII */ 1213 snprintf(buf, sizeof(buf), 1214 "T10 VPD ASCII Device Identifier: %s\n", 1215 &vpd->device_identifier[0]); 1216 break; 1217 case 0x03: /* UTF-8 */ 1218 snprintf(buf, sizeof(buf), 1219 "T10 VPD UTF-8 Device Identifier: %s\n", 1220 &vpd->device_identifier[0]); 1221 break; 1222 default: 1223 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:" 1224 " 0x%02x", vpd->device_identifier_code_set); 1225 ret = -EINVAL; 1226 break; 1227 } 1228 1229 if (p_buf) 1230 strncpy(p_buf, buf, p_buf_len); 1231 else 1232 pr_debug("%s", buf); 1233 1234 return ret; 1235 } 1236 1237 int 1238 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83) 1239 { 1240 static const char hex_str[] = "0123456789abcdef"; 1241 int j = 0, i = 4; /* offset to start of the identifier */ 1242 1243 /* 1244 * The VPD Code Set (encoding) 1245 * 1246 * from spc3r23.pdf Section 7.6.3.1 Table 296 1247 */ 1248 vpd->device_identifier_code_set = (page_83[0] & 0x0f); 1249 switch (vpd->device_identifier_code_set) { 1250 case 0x01: /* Binary */ 1251 vpd->device_identifier[j++] = 1252 hex_str[vpd->device_identifier_type]; 1253 while (i < (4 + page_83[3])) { 1254 vpd->device_identifier[j++] = 1255 hex_str[(page_83[i] & 0xf0) >> 4]; 1256 vpd->device_identifier[j++] = 1257 hex_str[page_83[i] & 0x0f]; 1258 i++; 1259 } 1260 break; 1261 case 0x02: /* ASCII */ 1262 case 0x03: /* UTF-8 */ 1263 while (i < (4 + page_83[3])) 1264 vpd->device_identifier[j++] = page_83[i++]; 1265 break; 1266 default: 1267 break; 1268 } 1269 1270 return transport_dump_vpd_ident(vpd, NULL, 0); 1271 } 1272 EXPORT_SYMBOL(transport_set_vpd_ident); 1273 1274 static sense_reason_t 1275 target_check_max_data_sg_nents(struct se_cmd *cmd, struct se_device *dev, 1276 unsigned int size) 1277 { 1278 u32 mtl; 1279 1280 if (!cmd->se_tfo->max_data_sg_nents) 1281 return TCM_NO_SENSE; 1282 /* 1283 * Check if fabric enforced maximum SGL entries per I/O descriptor 1284 * exceeds se_cmd->data_length. If true, set SCF_UNDERFLOW_BIT + 1285 * residual_count and reduce original cmd->data_length to maximum 1286 * length based on single PAGE_SIZE entry scatter-lists. 1287 */ 1288 mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE); 1289 if (cmd->data_length > mtl) { 1290 /* 1291 * If an existing CDB overflow is present, calculate new residual 1292 * based on CDB size minus fabric maximum transfer length. 1293 * 1294 * If an existing CDB underflow is present, calculate new residual 1295 * based on original cmd->data_length minus fabric maximum transfer 1296 * length. 1297 * 1298 * Otherwise, set the underflow residual based on cmd->data_length 1299 * minus fabric maximum transfer length. 1300 */ 1301 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { 1302 cmd->residual_count = (size - mtl); 1303 } else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) { 1304 u32 orig_dl = size + cmd->residual_count; 1305 cmd->residual_count = (orig_dl - mtl); 1306 } else { 1307 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; 1308 cmd->residual_count = (cmd->data_length - mtl); 1309 } 1310 cmd->data_length = mtl; 1311 /* 1312 * Reset sbc_check_prot() calculated protection payload 1313 * length based upon the new smaller MTL. 1314 */ 1315 if (cmd->prot_length) { 1316 u32 sectors = (mtl / dev->dev_attrib.block_size); 1317 cmd->prot_length = dev->prot_length * sectors; 1318 } 1319 } 1320 return TCM_NO_SENSE; 1321 } 1322 1323 /** 1324 * target_cmd_size_check - Check whether there will be a residual. 1325 * @cmd: SCSI command. 1326 * @size: Data buffer size derived from CDB. The data buffer size provided by 1327 * the SCSI transport driver is available in @cmd->data_length. 1328 * 1329 * Compare the data buffer size from the CDB with the data buffer limit from the transport 1330 * header. Set @cmd->residual_count and SCF_OVERFLOW_BIT or SCF_UNDERFLOW_BIT if necessary. 1331 * 1332 * Note: target drivers set @cmd->data_length by calling __target_init_cmd(). 1333 * 1334 * Return: TCM_NO_SENSE 1335 */ 1336 sense_reason_t 1337 target_cmd_size_check(struct se_cmd *cmd, unsigned int size) 1338 { 1339 struct se_device *dev = cmd->se_dev; 1340 1341 if (cmd->unknown_data_length) { 1342 cmd->data_length = size; 1343 } else if (size != cmd->data_length) { 1344 pr_warn_ratelimited("TARGET_CORE[%s]: Expected Transfer Length:" 1345 " %u does not match SCSI CDB Length: %u for SAM Opcode:" 1346 " 0x%02x\n", cmd->se_tfo->fabric_name, 1347 cmd->data_length, size, cmd->t_task_cdb[0]); 1348 /* 1349 * For READ command for the overflow case keep the existing 1350 * fabric provided ->data_length. Otherwise for the underflow 1351 * case, reset ->data_length to the smaller SCSI expected data 1352 * transfer length. 1353 */ 1354 if (size > cmd->data_length) { 1355 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT; 1356 cmd->residual_count = (size - cmd->data_length); 1357 } else { 1358 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; 1359 cmd->residual_count = (cmd->data_length - size); 1360 /* 1361 * Do not truncate ->data_length for WRITE command to 1362 * dump all payload 1363 */ 1364 if (cmd->data_direction == DMA_FROM_DEVICE) { 1365 cmd->data_length = size; 1366 } 1367 } 1368 1369 if (cmd->data_direction == DMA_TO_DEVICE) { 1370 if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) { 1371 pr_err_ratelimited("Rejecting underflow/overflow" 1372 " for WRITE data CDB\n"); 1373 return TCM_INVALID_FIELD_IN_COMMAND_IU; 1374 } 1375 /* 1376 * Some fabric drivers like iscsi-target still expect to 1377 * always reject overflow writes. Reject this case until 1378 * full fabric driver level support for overflow writes 1379 * is introduced tree-wide. 1380 */ 1381 if (size > cmd->data_length) { 1382 pr_err_ratelimited("Rejecting overflow for" 1383 " WRITE control CDB\n"); 1384 return TCM_INVALID_CDB_FIELD; 1385 } 1386 } 1387 } 1388 1389 return target_check_max_data_sg_nents(cmd, dev, size); 1390 1391 } 1392 1393 /* 1394 * Used by fabric modules containing a local struct se_cmd within their 1395 * fabric dependent per I/O descriptor. 1396 * 1397 * Preserves the value of @cmd->tag. 1398 */ 1399 void __target_init_cmd( 1400 struct se_cmd *cmd, 1401 const struct target_core_fabric_ops *tfo, 1402 struct se_session *se_sess, 1403 u32 data_length, 1404 int data_direction, 1405 int task_attr, 1406 unsigned char *sense_buffer, u64 unpacked_lun) 1407 { 1408 INIT_LIST_HEAD(&cmd->se_delayed_node); 1409 INIT_LIST_HEAD(&cmd->se_qf_node); 1410 INIT_LIST_HEAD(&cmd->state_list); 1411 init_completion(&cmd->t_transport_stop_comp); 1412 cmd->free_compl = NULL; 1413 cmd->abrt_compl = NULL; 1414 spin_lock_init(&cmd->t_state_lock); 1415 INIT_WORK(&cmd->work, NULL); 1416 kref_init(&cmd->cmd_kref); 1417 1418 cmd->t_task_cdb = &cmd->__t_task_cdb[0]; 1419 cmd->se_tfo = tfo; 1420 cmd->se_sess = se_sess; 1421 cmd->data_length = data_length; 1422 cmd->data_direction = data_direction; 1423 cmd->sam_task_attr = task_attr; 1424 cmd->sense_buffer = sense_buffer; 1425 cmd->orig_fe_lun = unpacked_lun; 1426 1427 if (!(cmd->se_cmd_flags & SCF_USE_CPUID)) 1428 cmd->cpuid = raw_smp_processor_id(); 1429 1430 cmd->state_active = false; 1431 } 1432 EXPORT_SYMBOL(__target_init_cmd); 1433 1434 static sense_reason_t 1435 transport_check_alloc_task_attr(struct se_cmd *cmd) 1436 { 1437 struct se_device *dev = cmd->se_dev; 1438 1439 /* 1440 * Check if SAM Task Attribute emulation is enabled for this 1441 * struct se_device storage object 1442 */ 1443 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) 1444 return 0; 1445 1446 if (cmd->sam_task_attr == TCM_ACA_TAG) { 1447 pr_debug("SAM Task Attribute ACA" 1448 " emulation is not supported\n"); 1449 return TCM_INVALID_CDB_FIELD; 1450 } 1451 1452 return 0; 1453 } 1454 1455 sense_reason_t 1456 target_cmd_init_cdb(struct se_cmd *cmd, unsigned char *cdb, gfp_t gfp) 1457 { 1458 sense_reason_t ret; 1459 1460 /* 1461 * Ensure that the received CDB is less than the max (252 + 8) bytes 1462 * for VARIABLE_LENGTH_CMD 1463 */ 1464 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) { 1465 pr_err("Received SCSI CDB with command_size: %d that" 1466 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", 1467 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE); 1468 ret = TCM_INVALID_CDB_FIELD; 1469 goto err; 1470 } 1471 /* 1472 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE, 1473 * allocate the additional extended CDB buffer now.. Otherwise 1474 * setup the pointer from __t_task_cdb to t_task_cdb. 1475 */ 1476 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) { 1477 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb), gfp); 1478 if (!cmd->t_task_cdb) { 1479 pr_err("Unable to allocate cmd->t_task_cdb" 1480 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n", 1481 scsi_command_size(cdb), 1482 (unsigned long)sizeof(cmd->__t_task_cdb)); 1483 ret = TCM_OUT_OF_RESOURCES; 1484 goto err; 1485 } 1486 } 1487 /* 1488 * Copy the original CDB into cmd-> 1489 */ 1490 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb)); 1491 1492 trace_target_sequencer_start(cmd); 1493 return 0; 1494 1495 err: 1496 /* 1497 * Copy the CDB here to allow trace_target_cmd_complete() to 1498 * print the cdb to the trace buffers. 1499 */ 1500 memcpy(cmd->t_task_cdb, cdb, min(scsi_command_size(cdb), 1501 (unsigned int)TCM_MAX_COMMAND_SIZE)); 1502 return ret; 1503 } 1504 EXPORT_SYMBOL(target_cmd_init_cdb); 1505 1506 sense_reason_t 1507 target_cmd_parse_cdb(struct se_cmd *cmd) 1508 { 1509 struct se_device *dev = cmd->se_dev; 1510 sense_reason_t ret; 1511 1512 ret = dev->transport->parse_cdb(cmd); 1513 if (ret == TCM_UNSUPPORTED_SCSI_OPCODE) 1514 pr_warn_ratelimited("%s/%s: Unsupported SCSI Opcode 0x%02x, sending CHECK_CONDITION.\n", 1515 cmd->se_tfo->fabric_name, 1516 cmd->se_sess->se_node_acl->initiatorname, 1517 cmd->t_task_cdb[0]); 1518 if (ret) 1519 return ret; 1520 1521 ret = transport_check_alloc_task_attr(cmd); 1522 if (ret) 1523 return ret; 1524 1525 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE; 1526 atomic_long_inc(&cmd->se_lun->lun_stats.cmd_pdus); 1527 return 0; 1528 } 1529 EXPORT_SYMBOL(target_cmd_parse_cdb); 1530 1531 /* 1532 * Used by fabric module frontends to queue tasks directly. 1533 * May only be used from process context. 1534 */ 1535 int transport_handle_cdb_direct( 1536 struct se_cmd *cmd) 1537 { 1538 sense_reason_t ret; 1539 1540 might_sleep(); 1541 1542 if (!cmd->se_lun) { 1543 dump_stack(); 1544 pr_err("cmd->se_lun is NULL\n"); 1545 return -EINVAL; 1546 } 1547 1548 /* 1549 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that 1550 * outstanding descriptors are handled correctly during shutdown via 1551 * transport_wait_for_tasks() 1552 * 1553 * Also, we don't take cmd->t_state_lock here as we only expect 1554 * this to be called for initial descriptor submission. 1555 */ 1556 cmd->t_state = TRANSPORT_NEW_CMD; 1557 cmd->transport_state |= CMD_T_ACTIVE; 1558 1559 /* 1560 * transport_generic_new_cmd() is already handling QUEUE_FULL, 1561 * so follow TRANSPORT_NEW_CMD processing thread context usage 1562 * and call transport_generic_request_failure() if necessary.. 1563 */ 1564 ret = transport_generic_new_cmd(cmd); 1565 if (ret) 1566 transport_generic_request_failure(cmd, ret); 1567 return 0; 1568 } 1569 EXPORT_SYMBOL(transport_handle_cdb_direct); 1570 1571 sense_reason_t 1572 transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl, 1573 u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count) 1574 { 1575 if (!sgl || !sgl_count) 1576 return 0; 1577 1578 /* 1579 * Reject SCSI data overflow with map_mem_to_cmd() as incoming 1580 * scatterlists already have been set to follow what the fabric 1581 * passes for the original expected data transfer length. 1582 */ 1583 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { 1584 pr_warn("Rejecting SCSI DATA overflow for fabric using" 1585 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n"); 1586 return TCM_INVALID_CDB_FIELD; 1587 } 1588 1589 cmd->t_data_sg = sgl; 1590 cmd->t_data_nents = sgl_count; 1591 cmd->t_bidi_data_sg = sgl_bidi; 1592 cmd->t_bidi_data_nents = sgl_bidi_count; 1593 1594 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC; 1595 return 0; 1596 } 1597 1598 /** 1599 * target_init_cmd - initialize se_cmd 1600 * @se_cmd: command descriptor to init 1601 * @se_sess: associated se_sess for endpoint 1602 * @sense: pointer to SCSI sense buffer 1603 * @unpacked_lun: unpacked LUN to reference for struct se_lun 1604 * @data_length: fabric expected data transfer length 1605 * @task_attr: SAM task attribute 1606 * @data_dir: DMA data direction 1607 * @flags: flags for command submission from target_sc_flags_tables 1608 * 1609 * Task tags are supported if the caller has set @se_cmd->tag. 1610 * 1611 * Returns: 1612 * - less than zero to signal active I/O shutdown failure. 1613 * - zero on success. 1614 * 1615 * If the fabric driver calls target_stop_session, then it must check the 1616 * return code and handle failures. This will never fail for other drivers, 1617 * and the return code can be ignored. 1618 */ 1619 int target_init_cmd(struct se_cmd *se_cmd, struct se_session *se_sess, 1620 unsigned char *sense, u64 unpacked_lun, 1621 u32 data_length, int task_attr, int data_dir, int flags) 1622 { 1623 struct se_portal_group *se_tpg; 1624 1625 se_tpg = se_sess->se_tpg; 1626 BUG_ON(!se_tpg); 1627 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess); 1628 1629 if (flags & TARGET_SCF_USE_CPUID) 1630 se_cmd->se_cmd_flags |= SCF_USE_CPUID; 1631 /* 1632 * Signal bidirectional data payloads to target-core 1633 */ 1634 if (flags & TARGET_SCF_BIDI_OP) 1635 se_cmd->se_cmd_flags |= SCF_BIDI; 1636 1637 if (flags & TARGET_SCF_UNKNOWN_SIZE) 1638 se_cmd->unknown_data_length = 1; 1639 /* 1640 * Initialize se_cmd for target operation. From this point 1641 * exceptions are handled by sending exception status via 1642 * target_core_fabric_ops->queue_status() callback 1643 */ 1644 __target_init_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, data_length, 1645 data_dir, task_attr, sense, unpacked_lun); 1646 1647 /* 1648 * Obtain struct se_cmd->cmd_kref reference. A second kref_get here is 1649 * necessary for fabrics using TARGET_SCF_ACK_KREF that expect a second 1650 * kref_put() to happen during fabric packet acknowledgement. 1651 */ 1652 return target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF); 1653 } 1654 EXPORT_SYMBOL_GPL(target_init_cmd); 1655 1656 /** 1657 * target_submit_prep - prepare cmd for submission 1658 * @se_cmd: command descriptor to prep 1659 * @cdb: pointer to SCSI CDB 1660 * @sgl: struct scatterlist memory for unidirectional mapping 1661 * @sgl_count: scatterlist count for unidirectional mapping 1662 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping 1663 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping 1664 * @sgl_prot: struct scatterlist memory protection information 1665 * @sgl_prot_count: scatterlist count for protection information 1666 * @gfp: gfp allocation type 1667 * 1668 * Returns: 1669 * - less than zero to signal failure. 1670 * - zero on success. 1671 * 1672 * If failure is returned, lio will the callers queue_status to complete 1673 * the cmd. 1674 */ 1675 int target_submit_prep(struct se_cmd *se_cmd, unsigned char *cdb, 1676 struct scatterlist *sgl, u32 sgl_count, 1677 struct scatterlist *sgl_bidi, u32 sgl_bidi_count, 1678 struct scatterlist *sgl_prot, u32 sgl_prot_count, 1679 gfp_t gfp) 1680 { 1681 sense_reason_t rc; 1682 1683 rc = target_cmd_init_cdb(se_cmd, cdb, gfp); 1684 if (rc) 1685 goto send_cc_direct; 1686 1687 /* 1688 * Locate se_lun pointer and attach it to struct se_cmd 1689 */ 1690 rc = transport_lookup_cmd_lun(se_cmd); 1691 if (rc) 1692 goto send_cc_direct; 1693 1694 rc = target_cmd_parse_cdb(se_cmd); 1695 if (rc != 0) 1696 goto generic_fail; 1697 1698 /* 1699 * Save pointers for SGLs containing protection information, 1700 * if present. 1701 */ 1702 if (sgl_prot_count) { 1703 se_cmd->t_prot_sg = sgl_prot; 1704 se_cmd->t_prot_nents = sgl_prot_count; 1705 se_cmd->se_cmd_flags |= SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC; 1706 } 1707 1708 /* 1709 * When a non zero sgl_count has been passed perform SGL passthrough 1710 * mapping for pre-allocated fabric memory instead of having target 1711 * core perform an internal SGL allocation.. 1712 */ 1713 if (sgl_count != 0) { 1714 BUG_ON(!sgl); 1715 1716 rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count, 1717 sgl_bidi, sgl_bidi_count); 1718 if (rc != 0) 1719 goto generic_fail; 1720 } 1721 1722 return 0; 1723 1724 send_cc_direct: 1725 transport_send_check_condition_and_sense(se_cmd, rc, 0); 1726 target_put_sess_cmd(se_cmd); 1727 return -EIO; 1728 1729 generic_fail: 1730 transport_generic_request_failure(se_cmd, rc); 1731 return -EIO; 1732 } 1733 EXPORT_SYMBOL_GPL(target_submit_prep); 1734 1735 /** 1736 * target_submit - perform final initialization and submit cmd to LIO core 1737 * @se_cmd: command descriptor to submit 1738 * 1739 * target_submit_prep must have been called on the cmd, and this must be 1740 * called from process context. 1741 */ 1742 void target_submit(struct se_cmd *se_cmd) 1743 { 1744 struct scatterlist *sgl = se_cmd->t_data_sg; 1745 unsigned char *buf = NULL; 1746 1747 might_sleep(); 1748 1749 if (se_cmd->t_data_nents != 0) { 1750 BUG_ON(!sgl); 1751 /* 1752 * A work-around for tcm_loop as some userspace code via 1753 * scsi-generic do not memset their associated read buffers, 1754 * so go ahead and do that here for type non-data CDBs. Also 1755 * note that this is currently guaranteed to be a single SGL 1756 * for this case by target core in target_setup_cmd_from_cdb() 1757 * -> transport_generic_cmd_sequencer(). 1758 */ 1759 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && 1760 se_cmd->data_direction == DMA_FROM_DEVICE) { 1761 if (sgl) 1762 buf = kmap(sg_page(sgl)) + sgl->offset; 1763 1764 if (buf) { 1765 memset(buf, 0, sgl->length); 1766 kunmap(sg_page(sgl)); 1767 } 1768 } 1769 1770 } 1771 1772 /* 1773 * Check if we need to delay processing because of ALUA 1774 * Active/NonOptimized primary access state.. 1775 */ 1776 core_alua_check_nonop_delay(se_cmd); 1777 1778 transport_handle_cdb_direct(se_cmd); 1779 } 1780 EXPORT_SYMBOL_GPL(target_submit); 1781 1782 /** 1783 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd 1784 * 1785 * @se_cmd: command descriptor to submit 1786 * @se_sess: associated se_sess for endpoint 1787 * @cdb: pointer to SCSI CDB 1788 * @sense: pointer to SCSI sense buffer 1789 * @unpacked_lun: unpacked LUN to reference for struct se_lun 1790 * @data_length: fabric expected data transfer length 1791 * @task_attr: SAM task attribute 1792 * @data_dir: DMA data direction 1793 * @flags: flags for command submission from target_sc_flags_tables 1794 * 1795 * Task tags are supported if the caller has set @se_cmd->tag. 1796 * 1797 * This may only be called from process context, and also currently 1798 * assumes internal allocation of fabric payload buffer by target-core. 1799 * 1800 * It also assumes interal target core SGL memory allocation. 1801 * 1802 * This function must only be used by drivers that do their own 1803 * sync during shutdown and does not use target_stop_session. If there 1804 * is a failure this function will call into the fabric driver's 1805 * queue_status with a CHECK_CONDITION. 1806 */ 1807 void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess, 1808 unsigned char *cdb, unsigned char *sense, u64 unpacked_lun, 1809 u32 data_length, int task_attr, int data_dir, int flags) 1810 { 1811 int rc; 1812 1813 rc = target_init_cmd(se_cmd, se_sess, sense, unpacked_lun, data_length, 1814 task_attr, data_dir, flags); 1815 WARN(rc, "Invalid target_submit_cmd use. Driver must not use target_stop_session or call target_init_cmd directly.\n"); 1816 if (rc) 1817 return; 1818 1819 if (target_submit_prep(se_cmd, cdb, NULL, 0, NULL, 0, NULL, 0, 1820 GFP_KERNEL)) 1821 return; 1822 1823 target_submit(se_cmd); 1824 } 1825 EXPORT_SYMBOL(target_submit_cmd); 1826 1827 1828 static struct se_dev_plug *target_plug_device(struct se_device *se_dev) 1829 { 1830 struct se_dev_plug *se_plug; 1831 1832 if (!se_dev->transport->plug_device) 1833 return NULL; 1834 1835 se_plug = se_dev->transport->plug_device(se_dev); 1836 if (!se_plug) 1837 return NULL; 1838 1839 se_plug->se_dev = se_dev; 1840 /* 1841 * We have a ref to the lun at this point, but the cmds could 1842 * complete before we unplug, so grab a ref to the se_device so we 1843 * can call back into the backend. 1844 */ 1845 config_group_get(&se_dev->dev_group); 1846 return se_plug; 1847 } 1848 1849 static void target_unplug_device(struct se_dev_plug *se_plug) 1850 { 1851 struct se_device *se_dev = se_plug->se_dev; 1852 1853 se_dev->transport->unplug_device(se_plug); 1854 config_group_put(&se_dev->dev_group); 1855 } 1856 1857 void target_queued_submit_work(struct work_struct *work) 1858 { 1859 struct se_cmd_queue *sq = container_of(work, struct se_cmd_queue, work); 1860 struct se_cmd *se_cmd, *next_cmd; 1861 struct se_dev_plug *se_plug = NULL; 1862 struct se_device *se_dev = NULL; 1863 struct llist_node *cmd_list; 1864 1865 cmd_list = llist_del_all(&sq->cmd_list); 1866 if (!cmd_list) 1867 /* Previous call took what we were queued to submit */ 1868 return; 1869 1870 cmd_list = llist_reverse_order(cmd_list); 1871 llist_for_each_entry_safe(se_cmd, next_cmd, cmd_list, se_cmd_list) { 1872 if (!se_dev) { 1873 se_dev = se_cmd->se_dev; 1874 se_plug = target_plug_device(se_dev); 1875 } 1876 1877 target_submit(se_cmd); 1878 } 1879 1880 if (se_plug) 1881 target_unplug_device(se_plug); 1882 } 1883 1884 /** 1885 * target_queue_submission - queue the cmd to run on the LIO workqueue 1886 * @se_cmd: command descriptor to submit 1887 */ 1888 void target_queue_submission(struct se_cmd *se_cmd) 1889 { 1890 struct se_device *se_dev = se_cmd->se_dev; 1891 int cpu = se_cmd->cpuid; 1892 struct se_cmd_queue *sq; 1893 1894 sq = &se_dev->queues[cpu].sq; 1895 llist_add(&se_cmd->se_cmd_list, &sq->cmd_list); 1896 queue_work_on(cpu, target_submission_wq, &sq->work); 1897 } 1898 EXPORT_SYMBOL_GPL(target_queue_submission); 1899 1900 static void target_complete_tmr_failure(struct work_struct *work) 1901 { 1902 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work); 1903 1904 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST; 1905 se_cmd->se_tfo->queue_tm_rsp(se_cmd); 1906 1907 transport_lun_remove_cmd(se_cmd); 1908 transport_cmd_check_stop_to_fabric(se_cmd); 1909 } 1910 1911 /** 1912 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd 1913 * for TMR CDBs 1914 * 1915 * @se_cmd: command descriptor to submit 1916 * @se_sess: associated se_sess for endpoint 1917 * @sense: pointer to SCSI sense buffer 1918 * @unpacked_lun: unpacked LUN to reference for struct se_lun 1919 * @fabric_tmr_ptr: fabric context for TMR req 1920 * @tm_type: Type of TM request 1921 * @gfp: gfp type for caller 1922 * @tag: referenced task tag for TMR_ABORT_TASK 1923 * @flags: submit cmd flags 1924 * 1925 * Callable from all contexts. 1926 **/ 1927 1928 int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess, 1929 unsigned char *sense, u64 unpacked_lun, 1930 void *fabric_tmr_ptr, unsigned char tm_type, 1931 gfp_t gfp, u64 tag, int flags) 1932 { 1933 struct se_portal_group *se_tpg; 1934 int ret; 1935 1936 se_tpg = se_sess->se_tpg; 1937 BUG_ON(!se_tpg); 1938 1939 __target_init_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, 1940 0, DMA_NONE, TCM_SIMPLE_TAG, sense, unpacked_lun); 1941 /* 1942 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req 1943 * allocation failure. 1944 */ 1945 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp); 1946 if (ret < 0) 1947 return -ENOMEM; 1948 1949 if (tm_type == TMR_ABORT_TASK) 1950 se_cmd->se_tmr_req->ref_task_tag = tag; 1951 1952 /* See target_submit_cmd for commentary */ 1953 ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF); 1954 if (ret) { 1955 core_tmr_release_req(se_cmd->se_tmr_req); 1956 return ret; 1957 } 1958 1959 ret = transport_lookup_tmr_lun(se_cmd); 1960 if (ret) 1961 goto failure; 1962 1963 transport_generic_handle_tmr(se_cmd); 1964 return 0; 1965 1966 /* 1967 * For callback during failure handling, push this work off 1968 * to process context with TMR_LUN_DOES_NOT_EXIST status. 1969 */ 1970 failure: 1971 INIT_WORK(&se_cmd->work, target_complete_tmr_failure); 1972 schedule_work(&se_cmd->work); 1973 return 0; 1974 } 1975 EXPORT_SYMBOL(target_submit_tmr); 1976 1977 /* 1978 * Handle SAM-esque emulation for generic transport request failures. 1979 */ 1980 void transport_generic_request_failure(struct se_cmd *cmd, 1981 sense_reason_t sense_reason) 1982 { 1983 int ret = 0, post_ret; 1984 1985 pr_debug("-----[ Storage Engine Exception; sense_reason %d\n", 1986 sense_reason); 1987 target_show_cmd("-----[ ", cmd); 1988 1989 /* 1990 * For SAM Task Attribute emulation for failed struct se_cmd 1991 */ 1992 transport_complete_task_attr(cmd); 1993 1994 if (cmd->transport_complete_callback) 1995 cmd->transport_complete_callback(cmd, false, &post_ret); 1996 1997 if (cmd->transport_state & CMD_T_ABORTED) { 1998 INIT_WORK(&cmd->work, target_abort_work); 1999 queue_work(target_completion_wq, &cmd->work); 2000 return; 2001 } 2002 2003 switch (sense_reason) { 2004 case TCM_NON_EXISTENT_LUN: 2005 case TCM_UNSUPPORTED_SCSI_OPCODE: 2006 case TCM_INVALID_CDB_FIELD: 2007 case TCM_INVALID_PARAMETER_LIST: 2008 case TCM_PARAMETER_LIST_LENGTH_ERROR: 2009 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: 2010 case TCM_UNKNOWN_MODE_PAGE: 2011 case TCM_WRITE_PROTECTED: 2012 case TCM_ADDRESS_OUT_OF_RANGE: 2013 case TCM_CHECK_CONDITION_ABORT_CMD: 2014 case TCM_CHECK_CONDITION_UNIT_ATTENTION: 2015 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED: 2016 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED: 2017 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED: 2018 case TCM_COPY_TARGET_DEVICE_NOT_REACHABLE: 2019 case TCM_TOO_MANY_TARGET_DESCS: 2020 case TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE: 2021 case TCM_TOO_MANY_SEGMENT_DESCS: 2022 case TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE: 2023 case TCM_INVALID_FIELD_IN_COMMAND_IU: 2024 case TCM_ALUA_TG_PT_STANDBY: 2025 case TCM_ALUA_TG_PT_UNAVAILABLE: 2026 case TCM_ALUA_STATE_TRANSITION: 2027 case TCM_ALUA_OFFLINE: 2028 break; 2029 case TCM_OUT_OF_RESOURCES: 2030 cmd->scsi_status = SAM_STAT_TASK_SET_FULL; 2031 goto queue_status; 2032 case TCM_LUN_BUSY: 2033 cmd->scsi_status = SAM_STAT_BUSY; 2034 goto queue_status; 2035 case TCM_RESERVATION_CONFLICT: 2036 /* 2037 * No SENSE Data payload for this case, set SCSI Status 2038 * and queue the response to $FABRIC_MOD. 2039 * 2040 * Uses linux/include/scsi/scsi.h SAM status codes defs 2041 */ 2042 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; 2043 /* 2044 * For UA Interlock Code 11b, a RESERVATION CONFLICT will 2045 * establish a UNIT ATTENTION with PREVIOUS RESERVATION 2046 * CONFLICT STATUS. 2047 * 2048 * See spc4r17, section 7.4.6 Control Mode Page, Table 349 2049 */ 2050 if (cmd->se_sess && 2051 cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl 2052 == TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) { 2053 target_ua_allocate_lun(cmd->se_sess->se_node_acl, 2054 cmd->orig_fe_lun, 0x2C, 2055 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS); 2056 } 2057 2058 goto queue_status; 2059 default: 2060 pr_err("Unknown transport error for CDB 0x%02x: %d\n", 2061 cmd->t_task_cdb[0], sense_reason); 2062 sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; 2063 break; 2064 } 2065 2066 ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0); 2067 if (ret) 2068 goto queue_full; 2069 2070 check_stop: 2071 transport_lun_remove_cmd(cmd); 2072 transport_cmd_check_stop_to_fabric(cmd); 2073 return; 2074 2075 queue_status: 2076 trace_target_cmd_complete(cmd); 2077 ret = cmd->se_tfo->queue_status(cmd); 2078 if (!ret) 2079 goto check_stop; 2080 queue_full: 2081 transport_handle_queue_full(cmd, cmd->se_dev, ret, false); 2082 } 2083 EXPORT_SYMBOL(transport_generic_request_failure); 2084 2085 void __target_execute_cmd(struct se_cmd *cmd, bool do_checks) 2086 { 2087 sense_reason_t ret; 2088 2089 if (!cmd->execute_cmd) { 2090 ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2091 goto err; 2092 } 2093 if (do_checks) { 2094 /* 2095 * Check for an existing UNIT ATTENTION condition after 2096 * target_handle_task_attr() has done SAM task attr 2097 * checking, and possibly have already defered execution 2098 * out to target_restart_delayed_cmds() context. 2099 */ 2100 ret = target_scsi3_ua_check(cmd); 2101 if (ret) 2102 goto err; 2103 2104 ret = target_alua_state_check(cmd); 2105 if (ret) 2106 goto err; 2107 2108 ret = target_check_reservation(cmd); 2109 if (ret) { 2110 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; 2111 goto err; 2112 } 2113 } 2114 2115 ret = cmd->execute_cmd(cmd); 2116 if (!ret) 2117 return; 2118 err: 2119 spin_lock_irq(&cmd->t_state_lock); 2120 cmd->transport_state &= ~CMD_T_SENT; 2121 spin_unlock_irq(&cmd->t_state_lock); 2122 2123 transport_generic_request_failure(cmd, ret); 2124 } 2125 2126 static int target_write_prot_action(struct se_cmd *cmd) 2127 { 2128 u32 sectors; 2129 /* 2130 * Perform WRITE_INSERT of PI using software emulation when backend 2131 * device has PI enabled, if the transport has not already generated 2132 * PI using hardware WRITE_INSERT offload. 2133 */ 2134 switch (cmd->prot_op) { 2135 case TARGET_PROT_DOUT_INSERT: 2136 if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_INSERT)) 2137 sbc_dif_generate(cmd); 2138 break; 2139 case TARGET_PROT_DOUT_STRIP: 2140 if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_STRIP) 2141 break; 2142 2143 sectors = cmd->data_length >> ilog2(cmd->se_dev->dev_attrib.block_size); 2144 cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba, 2145 sectors, 0, cmd->t_prot_sg, 0); 2146 if (unlikely(cmd->pi_err)) { 2147 spin_lock_irq(&cmd->t_state_lock); 2148 cmd->transport_state &= ~CMD_T_SENT; 2149 spin_unlock_irq(&cmd->t_state_lock); 2150 transport_generic_request_failure(cmd, cmd->pi_err); 2151 return -1; 2152 } 2153 break; 2154 default: 2155 break; 2156 } 2157 2158 return 0; 2159 } 2160 2161 static bool target_handle_task_attr(struct se_cmd *cmd) 2162 { 2163 struct se_device *dev = cmd->se_dev; 2164 2165 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) 2166 return false; 2167 2168 cmd->se_cmd_flags |= SCF_TASK_ATTR_SET; 2169 2170 /* 2171 * Check for the existence of HEAD_OF_QUEUE, and if true return 1 2172 * to allow the passed struct se_cmd list of tasks to the front of the list. 2173 */ 2174 switch (cmd->sam_task_attr) { 2175 case TCM_HEAD_TAG: 2176 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x\n", 2177 cmd->t_task_cdb[0]); 2178 return false; 2179 case TCM_ORDERED_TAG: 2180 atomic_inc_mb(&dev->dev_ordered_sync); 2181 2182 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list\n", 2183 cmd->t_task_cdb[0]); 2184 2185 /* 2186 * Execute an ORDERED command if no other older commands 2187 * exist that need to be completed first. 2188 */ 2189 if (!atomic_read(&dev->simple_cmds)) 2190 return false; 2191 break; 2192 default: 2193 /* 2194 * For SIMPLE and UNTAGGED Task Attribute commands 2195 */ 2196 atomic_inc_mb(&dev->simple_cmds); 2197 break; 2198 } 2199 2200 if (atomic_read(&dev->dev_ordered_sync) == 0) 2201 return false; 2202 2203 spin_lock(&dev->delayed_cmd_lock); 2204 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list); 2205 spin_unlock(&dev->delayed_cmd_lock); 2206 2207 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to delayed CMD listn", 2208 cmd->t_task_cdb[0], cmd->sam_task_attr); 2209 return true; 2210 } 2211 2212 void target_execute_cmd(struct se_cmd *cmd) 2213 { 2214 /* 2215 * Determine if frontend context caller is requesting the stopping of 2216 * this command for frontend exceptions. 2217 * 2218 * If the received CDB has already been aborted stop processing it here. 2219 */ 2220 if (target_cmd_interrupted(cmd)) 2221 return; 2222 2223 spin_lock_irq(&cmd->t_state_lock); 2224 cmd->t_state = TRANSPORT_PROCESSING; 2225 cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT; 2226 spin_unlock_irq(&cmd->t_state_lock); 2227 2228 if (target_write_prot_action(cmd)) 2229 return; 2230 2231 if (target_handle_task_attr(cmd)) { 2232 spin_lock_irq(&cmd->t_state_lock); 2233 cmd->transport_state &= ~CMD_T_SENT; 2234 spin_unlock_irq(&cmd->t_state_lock); 2235 return; 2236 } 2237 2238 __target_execute_cmd(cmd, true); 2239 } 2240 EXPORT_SYMBOL(target_execute_cmd); 2241 2242 /* 2243 * Process all commands up to the last received ORDERED task attribute which 2244 * requires another blocking boundary 2245 */ 2246 static void target_restart_delayed_cmds(struct se_device *dev) 2247 { 2248 for (;;) { 2249 struct se_cmd *cmd; 2250 2251 spin_lock(&dev->delayed_cmd_lock); 2252 if (list_empty(&dev->delayed_cmd_list)) { 2253 spin_unlock(&dev->delayed_cmd_lock); 2254 break; 2255 } 2256 2257 cmd = list_entry(dev->delayed_cmd_list.next, 2258 struct se_cmd, se_delayed_node); 2259 list_del(&cmd->se_delayed_node); 2260 spin_unlock(&dev->delayed_cmd_lock); 2261 2262 cmd->transport_state |= CMD_T_SENT; 2263 2264 __target_execute_cmd(cmd, true); 2265 2266 if (cmd->sam_task_attr == TCM_ORDERED_TAG) 2267 break; 2268 } 2269 } 2270 2271 /* 2272 * Called from I/O completion to determine which dormant/delayed 2273 * and ordered cmds need to have their tasks added to the execution queue. 2274 */ 2275 static void transport_complete_task_attr(struct se_cmd *cmd) 2276 { 2277 struct se_device *dev = cmd->se_dev; 2278 2279 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) 2280 return; 2281 2282 if (!(cmd->se_cmd_flags & SCF_TASK_ATTR_SET)) 2283 goto restart; 2284 2285 if (cmd->sam_task_attr == TCM_SIMPLE_TAG) { 2286 atomic_dec_mb(&dev->simple_cmds); 2287 dev->dev_cur_ordered_id++; 2288 } else if (cmd->sam_task_attr == TCM_HEAD_TAG) { 2289 dev->dev_cur_ordered_id++; 2290 pr_debug("Incremented dev_cur_ordered_id: %u for HEAD_OF_QUEUE\n", 2291 dev->dev_cur_ordered_id); 2292 } else if (cmd->sam_task_attr == TCM_ORDERED_TAG) { 2293 atomic_dec_mb(&dev->dev_ordered_sync); 2294 2295 dev->dev_cur_ordered_id++; 2296 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED\n", 2297 dev->dev_cur_ordered_id); 2298 } 2299 cmd->se_cmd_flags &= ~SCF_TASK_ATTR_SET; 2300 2301 restart: 2302 target_restart_delayed_cmds(dev); 2303 } 2304 2305 static void transport_complete_qf(struct se_cmd *cmd) 2306 { 2307 int ret = 0; 2308 2309 transport_complete_task_attr(cmd); 2310 /* 2311 * If a fabric driver ->write_pending() or ->queue_data_in() callback 2312 * has returned neither -ENOMEM or -EAGAIN, assume it's fatal and 2313 * the same callbacks should not be retried. Return CHECK_CONDITION 2314 * if a scsi_status is not already set. 2315 * 2316 * If a fabric driver ->queue_status() has returned non zero, always 2317 * keep retrying no matter what.. 2318 */ 2319 if (cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) { 2320 if (cmd->scsi_status) 2321 goto queue_status; 2322 2323 translate_sense_reason(cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE); 2324 goto queue_status; 2325 } 2326 2327 /* 2328 * Check if we need to send a sense buffer from 2329 * the struct se_cmd in question. We do NOT want 2330 * to take this path of the IO has been marked as 2331 * needing to be treated like a "normal read". This 2332 * is the case if it's a tape read, and either the 2333 * FM, EOM, or ILI bits are set, but there is no 2334 * sense data. 2335 */ 2336 if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && 2337 cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) 2338 goto queue_status; 2339 2340 switch (cmd->data_direction) { 2341 case DMA_FROM_DEVICE: 2342 /* queue status if not treating this as a normal read */ 2343 if (cmd->scsi_status && 2344 !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL)) 2345 goto queue_status; 2346 2347 trace_target_cmd_complete(cmd); 2348 ret = cmd->se_tfo->queue_data_in(cmd); 2349 break; 2350 case DMA_TO_DEVICE: 2351 if (cmd->se_cmd_flags & SCF_BIDI) { 2352 ret = cmd->se_tfo->queue_data_in(cmd); 2353 break; 2354 } 2355 fallthrough; 2356 case DMA_NONE: 2357 queue_status: 2358 trace_target_cmd_complete(cmd); 2359 ret = cmd->se_tfo->queue_status(cmd); 2360 break; 2361 default: 2362 break; 2363 } 2364 2365 if (ret < 0) { 2366 transport_handle_queue_full(cmd, cmd->se_dev, ret, false); 2367 return; 2368 } 2369 transport_lun_remove_cmd(cmd); 2370 transport_cmd_check_stop_to_fabric(cmd); 2371 } 2372 2373 static void transport_handle_queue_full(struct se_cmd *cmd, struct se_device *dev, 2374 int err, bool write_pending) 2375 { 2376 /* 2377 * -EAGAIN or -ENOMEM signals retry of ->write_pending() and/or 2378 * ->queue_data_in() callbacks from new process context. 2379 * 2380 * Otherwise for other errors, transport_complete_qf() will send 2381 * CHECK_CONDITION via ->queue_status() instead of attempting to 2382 * retry associated fabric driver data-transfer callbacks. 2383 */ 2384 if (err == -EAGAIN || err == -ENOMEM) { 2385 cmd->t_state = (write_pending) ? TRANSPORT_COMPLETE_QF_WP : 2386 TRANSPORT_COMPLETE_QF_OK; 2387 } else { 2388 pr_warn_ratelimited("Got unknown fabric queue status: %d\n", err); 2389 cmd->t_state = TRANSPORT_COMPLETE_QF_ERR; 2390 } 2391 2392 spin_lock_irq(&dev->qf_cmd_lock); 2393 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list); 2394 atomic_inc_mb(&dev->dev_qf_count); 2395 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock); 2396 2397 schedule_work(&cmd->se_dev->qf_work_queue); 2398 } 2399 2400 static bool target_read_prot_action(struct se_cmd *cmd) 2401 { 2402 switch (cmd->prot_op) { 2403 case TARGET_PROT_DIN_STRIP: 2404 if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_STRIP)) { 2405 u32 sectors = cmd->data_length >> 2406 ilog2(cmd->se_dev->dev_attrib.block_size); 2407 2408 cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba, 2409 sectors, 0, cmd->t_prot_sg, 2410 0); 2411 if (cmd->pi_err) 2412 return true; 2413 } 2414 break; 2415 case TARGET_PROT_DIN_INSERT: 2416 if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_INSERT) 2417 break; 2418 2419 sbc_dif_generate(cmd); 2420 break; 2421 default: 2422 break; 2423 } 2424 2425 return false; 2426 } 2427 2428 static void target_complete_ok_work(struct work_struct *work) 2429 { 2430 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 2431 int ret; 2432 2433 /* 2434 * Check if we need to move delayed/dormant tasks from cmds on the 2435 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task 2436 * Attribute. 2437 */ 2438 transport_complete_task_attr(cmd); 2439 2440 /* 2441 * Check to schedule QUEUE_FULL work, or execute an existing 2442 * cmd->transport_qf_callback() 2443 */ 2444 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0) 2445 schedule_work(&cmd->se_dev->qf_work_queue); 2446 2447 /* 2448 * Check if we need to send a sense buffer from 2449 * the struct se_cmd in question. We do NOT want 2450 * to take this path of the IO has been marked as 2451 * needing to be treated like a "normal read". This 2452 * is the case if it's a tape read, and either the 2453 * FM, EOM, or ILI bits are set, but there is no 2454 * sense data. 2455 */ 2456 if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && 2457 cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { 2458 WARN_ON(!cmd->scsi_status); 2459 ret = transport_send_check_condition_and_sense( 2460 cmd, 0, 1); 2461 if (ret) 2462 goto queue_full; 2463 2464 transport_lun_remove_cmd(cmd); 2465 transport_cmd_check_stop_to_fabric(cmd); 2466 return; 2467 } 2468 /* 2469 * Check for a callback, used by amongst other things 2470 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation. 2471 */ 2472 if (cmd->transport_complete_callback) { 2473 sense_reason_t rc; 2474 bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE); 2475 bool zero_dl = !(cmd->data_length); 2476 int post_ret = 0; 2477 2478 rc = cmd->transport_complete_callback(cmd, true, &post_ret); 2479 if (!rc && !post_ret) { 2480 if (caw && zero_dl) 2481 goto queue_rsp; 2482 2483 return; 2484 } else if (rc) { 2485 ret = transport_send_check_condition_and_sense(cmd, 2486 rc, 0); 2487 if (ret) 2488 goto queue_full; 2489 2490 transport_lun_remove_cmd(cmd); 2491 transport_cmd_check_stop_to_fabric(cmd); 2492 return; 2493 } 2494 } 2495 2496 queue_rsp: 2497 switch (cmd->data_direction) { 2498 case DMA_FROM_DEVICE: 2499 /* 2500 * if this is a READ-type IO, but SCSI status 2501 * is set, then skip returning data and just 2502 * return the status -- unless this IO is marked 2503 * as needing to be treated as a normal read, 2504 * in which case we want to go ahead and return 2505 * the data. This happens, for example, for tape 2506 * reads with the FM, EOM, or ILI bits set, with 2507 * no sense data. 2508 */ 2509 if (cmd->scsi_status && 2510 !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL)) 2511 goto queue_status; 2512 2513 atomic_long_add(cmd->data_length, 2514 &cmd->se_lun->lun_stats.tx_data_octets); 2515 /* 2516 * Perform READ_STRIP of PI using software emulation when 2517 * backend had PI enabled, if the transport will not be 2518 * performing hardware READ_STRIP offload. 2519 */ 2520 if (target_read_prot_action(cmd)) { 2521 ret = transport_send_check_condition_and_sense(cmd, 2522 cmd->pi_err, 0); 2523 if (ret) 2524 goto queue_full; 2525 2526 transport_lun_remove_cmd(cmd); 2527 transport_cmd_check_stop_to_fabric(cmd); 2528 return; 2529 } 2530 2531 trace_target_cmd_complete(cmd); 2532 ret = cmd->se_tfo->queue_data_in(cmd); 2533 if (ret) 2534 goto queue_full; 2535 break; 2536 case DMA_TO_DEVICE: 2537 atomic_long_add(cmd->data_length, 2538 &cmd->se_lun->lun_stats.rx_data_octets); 2539 /* 2540 * Check if we need to send READ payload for BIDI-COMMAND 2541 */ 2542 if (cmd->se_cmd_flags & SCF_BIDI) { 2543 atomic_long_add(cmd->data_length, 2544 &cmd->se_lun->lun_stats.tx_data_octets); 2545 ret = cmd->se_tfo->queue_data_in(cmd); 2546 if (ret) 2547 goto queue_full; 2548 break; 2549 } 2550 fallthrough; 2551 case DMA_NONE: 2552 queue_status: 2553 trace_target_cmd_complete(cmd); 2554 ret = cmd->se_tfo->queue_status(cmd); 2555 if (ret) 2556 goto queue_full; 2557 break; 2558 default: 2559 break; 2560 } 2561 2562 transport_lun_remove_cmd(cmd); 2563 transport_cmd_check_stop_to_fabric(cmd); 2564 return; 2565 2566 queue_full: 2567 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p," 2568 " data_direction: %d\n", cmd, cmd->data_direction); 2569 2570 transport_handle_queue_full(cmd, cmd->se_dev, ret, false); 2571 } 2572 2573 void target_free_sgl(struct scatterlist *sgl, int nents) 2574 { 2575 sgl_free_n_order(sgl, nents, 0); 2576 } 2577 EXPORT_SYMBOL(target_free_sgl); 2578 2579 static inline void transport_reset_sgl_orig(struct se_cmd *cmd) 2580 { 2581 /* 2582 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE 2583 * emulation, and free + reset pointers if necessary.. 2584 */ 2585 if (!cmd->t_data_sg_orig) 2586 return; 2587 2588 kfree(cmd->t_data_sg); 2589 cmd->t_data_sg = cmd->t_data_sg_orig; 2590 cmd->t_data_sg_orig = NULL; 2591 cmd->t_data_nents = cmd->t_data_nents_orig; 2592 cmd->t_data_nents_orig = 0; 2593 } 2594 2595 static inline void transport_free_pages(struct se_cmd *cmd) 2596 { 2597 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) { 2598 target_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents); 2599 cmd->t_prot_sg = NULL; 2600 cmd->t_prot_nents = 0; 2601 } 2602 2603 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) { 2604 /* 2605 * Release special case READ buffer payload required for 2606 * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE 2607 */ 2608 if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) { 2609 target_free_sgl(cmd->t_bidi_data_sg, 2610 cmd->t_bidi_data_nents); 2611 cmd->t_bidi_data_sg = NULL; 2612 cmd->t_bidi_data_nents = 0; 2613 } 2614 transport_reset_sgl_orig(cmd); 2615 return; 2616 } 2617 transport_reset_sgl_orig(cmd); 2618 2619 target_free_sgl(cmd->t_data_sg, cmd->t_data_nents); 2620 cmd->t_data_sg = NULL; 2621 cmd->t_data_nents = 0; 2622 2623 target_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents); 2624 cmd->t_bidi_data_sg = NULL; 2625 cmd->t_bidi_data_nents = 0; 2626 } 2627 2628 void *transport_kmap_data_sg(struct se_cmd *cmd) 2629 { 2630 struct scatterlist *sg = cmd->t_data_sg; 2631 struct page **pages; 2632 int i; 2633 2634 /* 2635 * We need to take into account a possible offset here for fabrics like 2636 * tcm_loop who may be using a contig buffer from the SCSI midlayer for 2637 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd() 2638 */ 2639 if (!cmd->t_data_nents) 2640 return NULL; 2641 2642 BUG_ON(!sg); 2643 if (cmd->t_data_nents == 1) 2644 return kmap(sg_page(sg)) + sg->offset; 2645 2646 /* >1 page. use vmap */ 2647 pages = kmalloc_array(cmd->t_data_nents, sizeof(*pages), GFP_KERNEL); 2648 if (!pages) 2649 return NULL; 2650 2651 /* convert sg[] to pages[] */ 2652 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) { 2653 pages[i] = sg_page(sg); 2654 } 2655 2656 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL); 2657 kfree(pages); 2658 if (!cmd->t_data_vmap) 2659 return NULL; 2660 2661 return cmd->t_data_vmap + cmd->t_data_sg[0].offset; 2662 } 2663 EXPORT_SYMBOL(transport_kmap_data_sg); 2664 2665 void transport_kunmap_data_sg(struct se_cmd *cmd) 2666 { 2667 if (!cmd->t_data_nents) { 2668 return; 2669 } else if (cmd->t_data_nents == 1) { 2670 kunmap(sg_page(cmd->t_data_sg)); 2671 return; 2672 } 2673 2674 vunmap(cmd->t_data_vmap); 2675 cmd->t_data_vmap = NULL; 2676 } 2677 EXPORT_SYMBOL(transport_kunmap_data_sg); 2678 2679 int 2680 target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length, 2681 bool zero_page, bool chainable) 2682 { 2683 gfp_t gfp = GFP_KERNEL | (zero_page ? __GFP_ZERO : 0); 2684 2685 *sgl = sgl_alloc_order(length, 0, chainable, gfp, nents); 2686 return *sgl ? 0 : -ENOMEM; 2687 } 2688 EXPORT_SYMBOL(target_alloc_sgl); 2689 2690 /* 2691 * Allocate any required resources to execute the command. For writes we 2692 * might not have the payload yet, so notify the fabric via a call to 2693 * ->write_pending instead. Otherwise place it on the execution queue. 2694 */ 2695 sense_reason_t 2696 transport_generic_new_cmd(struct se_cmd *cmd) 2697 { 2698 unsigned long flags; 2699 int ret = 0; 2700 bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB); 2701 2702 if (cmd->prot_op != TARGET_PROT_NORMAL && 2703 !(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) { 2704 ret = target_alloc_sgl(&cmd->t_prot_sg, &cmd->t_prot_nents, 2705 cmd->prot_length, true, false); 2706 if (ret < 0) 2707 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2708 } 2709 2710 /* 2711 * Determine if the TCM fabric module has already allocated physical 2712 * memory, and is directly calling transport_generic_map_mem_to_cmd() 2713 * beforehand. 2714 */ 2715 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) && 2716 cmd->data_length) { 2717 2718 if ((cmd->se_cmd_flags & SCF_BIDI) || 2719 (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) { 2720 u32 bidi_length; 2721 2722 if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) 2723 bidi_length = cmd->t_task_nolb * 2724 cmd->se_dev->dev_attrib.block_size; 2725 else 2726 bidi_length = cmd->data_length; 2727 2728 ret = target_alloc_sgl(&cmd->t_bidi_data_sg, 2729 &cmd->t_bidi_data_nents, 2730 bidi_length, zero_flag, false); 2731 if (ret < 0) 2732 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2733 } 2734 2735 ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents, 2736 cmd->data_length, zero_flag, false); 2737 if (ret < 0) 2738 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2739 } else if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) && 2740 cmd->data_length) { 2741 /* 2742 * Special case for COMPARE_AND_WRITE with fabrics 2743 * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC. 2744 */ 2745 u32 caw_length = cmd->t_task_nolb * 2746 cmd->se_dev->dev_attrib.block_size; 2747 2748 ret = target_alloc_sgl(&cmd->t_bidi_data_sg, 2749 &cmd->t_bidi_data_nents, 2750 caw_length, zero_flag, false); 2751 if (ret < 0) 2752 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2753 } 2754 /* 2755 * If this command is not a write we can execute it right here, 2756 * for write buffers we need to notify the fabric driver first 2757 * and let it call back once the write buffers are ready. 2758 */ 2759 target_add_to_state_list(cmd); 2760 if (cmd->data_direction != DMA_TO_DEVICE || cmd->data_length == 0) { 2761 target_execute_cmd(cmd); 2762 return 0; 2763 } 2764 2765 spin_lock_irqsave(&cmd->t_state_lock, flags); 2766 cmd->t_state = TRANSPORT_WRITE_PENDING; 2767 /* 2768 * Determine if frontend context caller is requesting the stopping of 2769 * this command for frontend exceptions. 2770 */ 2771 if (cmd->transport_state & CMD_T_STOP && 2772 !cmd->se_tfo->write_pending_must_be_called) { 2773 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n", 2774 __func__, __LINE__, cmd->tag); 2775 2776 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2777 2778 complete_all(&cmd->t_transport_stop_comp); 2779 return 0; 2780 } 2781 cmd->transport_state &= ~CMD_T_ACTIVE; 2782 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2783 2784 ret = cmd->se_tfo->write_pending(cmd); 2785 if (ret) 2786 goto queue_full; 2787 2788 return 0; 2789 2790 queue_full: 2791 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd); 2792 transport_handle_queue_full(cmd, cmd->se_dev, ret, true); 2793 return 0; 2794 } 2795 EXPORT_SYMBOL(transport_generic_new_cmd); 2796 2797 static void transport_write_pending_qf(struct se_cmd *cmd) 2798 { 2799 unsigned long flags; 2800 int ret; 2801 bool stop; 2802 2803 spin_lock_irqsave(&cmd->t_state_lock, flags); 2804 stop = (cmd->transport_state & (CMD_T_STOP | CMD_T_ABORTED)); 2805 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2806 2807 if (stop) { 2808 pr_debug("%s:%d CMD_T_STOP|CMD_T_ABORTED for ITT: 0x%08llx\n", 2809 __func__, __LINE__, cmd->tag); 2810 complete_all(&cmd->t_transport_stop_comp); 2811 return; 2812 } 2813 2814 ret = cmd->se_tfo->write_pending(cmd); 2815 if (ret) { 2816 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", 2817 cmd); 2818 transport_handle_queue_full(cmd, cmd->se_dev, ret, true); 2819 } 2820 } 2821 2822 static bool 2823 __transport_wait_for_tasks(struct se_cmd *, bool, bool *, bool *, 2824 unsigned long *flags); 2825 2826 static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas) 2827 { 2828 unsigned long flags; 2829 2830 spin_lock_irqsave(&cmd->t_state_lock, flags); 2831 __transport_wait_for_tasks(cmd, true, aborted, tas, &flags); 2832 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2833 } 2834 2835 /* 2836 * Call target_put_sess_cmd() and wait until target_release_cmd_kref(@cmd) has 2837 * finished. 2838 */ 2839 void target_put_cmd_and_wait(struct se_cmd *cmd) 2840 { 2841 DECLARE_COMPLETION_ONSTACK(compl); 2842 2843 WARN_ON_ONCE(cmd->abrt_compl); 2844 cmd->abrt_compl = &compl; 2845 target_put_sess_cmd(cmd); 2846 wait_for_completion(&compl); 2847 } 2848 2849 /* 2850 * This function is called by frontend drivers after processing of a command 2851 * has finished. 2852 * 2853 * The protocol for ensuring that either the regular frontend command 2854 * processing flow or target_handle_abort() code drops one reference is as 2855 * follows: 2856 * - Calling .queue_data_in(), .queue_status() or queue_tm_rsp() will cause 2857 * the frontend driver to call this function synchronously or asynchronously. 2858 * That will cause one reference to be dropped. 2859 * - During regular command processing the target core sets CMD_T_COMPLETE 2860 * before invoking one of the .queue_*() functions. 2861 * - The code that aborts commands skips commands and TMFs for which 2862 * CMD_T_COMPLETE has been set. 2863 * - CMD_T_ABORTED is set atomically after the CMD_T_COMPLETE check for 2864 * commands that will be aborted. 2865 * - If the CMD_T_ABORTED flag is set but CMD_T_TAS has not been set 2866 * transport_generic_free_cmd() skips its call to target_put_sess_cmd(). 2867 * - For aborted commands for which CMD_T_TAS has been set .queue_status() will 2868 * be called and will drop a reference. 2869 * - For aborted commands for which CMD_T_TAS has not been set .aborted_task() 2870 * will be called. target_handle_abort() will drop the final reference. 2871 */ 2872 int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) 2873 { 2874 DECLARE_COMPLETION_ONSTACK(compl); 2875 int ret = 0; 2876 bool aborted = false, tas = false; 2877 2878 if (wait_for_tasks) 2879 target_wait_free_cmd(cmd, &aborted, &tas); 2880 2881 if (cmd->se_cmd_flags & SCF_SE_LUN_CMD) { 2882 /* 2883 * Handle WRITE failure case where transport_generic_new_cmd() 2884 * has already added se_cmd to state_list, but fabric has 2885 * failed command before I/O submission. 2886 */ 2887 if (cmd->state_active) 2888 target_remove_from_state_list(cmd); 2889 2890 if (cmd->se_lun) 2891 transport_lun_remove_cmd(cmd); 2892 } 2893 if (aborted) 2894 cmd->free_compl = &compl; 2895 ret = target_put_sess_cmd(cmd); 2896 if (aborted) { 2897 pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag); 2898 wait_for_completion(&compl); 2899 ret = 1; 2900 } 2901 return ret; 2902 } 2903 EXPORT_SYMBOL(transport_generic_free_cmd); 2904 2905 /** 2906 * target_get_sess_cmd - Verify the session is accepting cmds and take ref 2907 * @se_cmd: command descriptor to add 2908 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd() 2909 */ 2910 int target_get_sess_cmd(struct se_cmd *se_cmd, bool ack_kref) 2911 { 2912 struct se_session *se_sess = se_cmd->se_sess; 2913 int ret = 0; 2914 2915 /* 2916 * Add a second kref if the fabric caller is expecting to handle 2917 * fabric acknowledgement that requires two target_put_sess_cmd() 2918 * invocations before se_cmd descriptor release. 2919 */ 2920 if (ack_kref) { 2921 kref_get(&se_cmd->cmd_kref); 2922 se_cmd->se_cmd_flags |= SCF_ACK_KREF; 2923 } 2924 2925 if (!percpu_ref_tryget_live(&se_sess->cmd_count)) 2926 ret = -ESHUTDOWN; 2927 2928 if (ret && ack_kref) 2929 target_put_sess_cmd(se_cmd); 2930 2931 return ret; 2932 } 2933 EXPORT_SYMBOL(target_get_sess_cmd); 2934 2935 static void target_free_cmd_mem(struct se_cmd *cmd) 2936 { 2937 transport_free_pages(cmd); 2938 2939 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) 2940 core_tmr_release_req(cmd->se_tmr_req); 2941 if (cmd->t_task_cdb != cmd->__t_task_cdb) 2942 kfree(cmd->t_task_cdb); 2943 } 2944 2945 static void target_release_cmd_kref(struct kref *kref) 2946 { 2947 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref); 2948 struct se_session *se_sess = se_cmd->se_sess; 2949 struct completion *free_compl = se_cmd->free_compl; 2950 struct completion *abrt_compl = se_cmd->abrt_compl; 2951 2952 target_free_cmd_mem(se_cmd); 2953 se_cmd->se_tfo->release_cmd(se_cmd); 2954 if (free_compl) 2955 complete(free_compl); 2956 if (abrt_compl) 2957 complete(abrt_compl); 2958 2959 percpu_ref_put(&se_sess->cmd_count); 2960 } 2961 2962 /** 2963 * target_put_sess_cmd - decrease the command reference count 2964 * @se_cmd: command to drop a reference from 2965 * 2966 * Returns 1 if and only if this target_put_sess_cmd() call caused the 2967 * refcount to drop to zero. Returns zero otherwise. 2968 */ 2969 int target_put_sess_cmd(struct se_cmd *se_cmd) 2970 { 2971 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref); 2972 } 2973 EXPORT_SYMBOL(target_put_sess_cmd); 2974 2975 static const char *data_dir_name(enum dma_data_direction d) 2976 { 2977 switch (d) { 2978 case DMA_BIDIRECTIONAL: return "BIDI"; 2979 case DMA_TO_DEVICE: return "WRITE"; 2980 case DMA_FROM_DEVICE: return "READ"; 2981 case DMA_NONE: return "NONE"; 2982 } 2983 2984 return "(?)"; 2985 } 2986 2987 static const char *cmd_state_name(enum transport_state_table t) 2988 { 2989 switch (t) { 2990 case TRANSPORT_NO_STATE: return "NO_STATE"; 2991 case TRANSPORT_NEW_CMD: return "NEW_CMD"; 2992 case TRANSPORT_WRITE_PENDING: return "WRITE_PENDING"; 2993 case TRANSPORT_PROCESSING: return "PROCESSING"; 2994 case TRANSPORT_COMPLETE: return "COMPLETE"; 2995 case TRANSPORT_ISTATE_PROCESSING: 2996 return "ISTATE_PROCESSING"; 2997 case TRANSPORT_COMPLETE_QF_WP: return "COMPLETE_QF_WP"; 2998 case TRANSPORT_COMPLETE_QF_OK: return "COMPLETE_QF_OK"; 2999 case TRANSPORT_COMPLETE_QF_ERR: return "COMPLETE_QF_ERR"; 3000 } 3001 3002 return "(?)"; 3003 } 3004 3005 static void target_append_str(char **str, const char *txt) 3006 { 3007 char *prev = *str; 3008 3009 *str = *str ? kasprintf(GFP_ATOMIC, "%s,%s", *str, txt) : 3010 kstrdup(txt, GFP_ATOMIC); 3011 kfree(prev); 3012 } 3013 3014 /* 3015 * Convert a transport state bitmask into a string. The caller is 3016 * responsible for freeing the returned pointer. 3017 */ 3018 static char *target_ts_to_str(u32 ts) 3019 { 3020 char *str = NULL; 3021 3022 if (ts & CMD_T_ABORTED) 3023 target_append_str(&str, "aborted"); 3024 if (ts & CMD_T_ACTIVE) 3025 target_append_str(&str, "active"); 3026 if (ts & CMD_T_COMPLETE) 3027 target_append_str(&str, "complete"); 3028 if (ts & CMD_T_SENT) 3029 target_append_str(&str, "sent"); 3030 if (ts & CMD_T_STOP) 3031 target_append_str(&str, "stop"); 3032 if (ts & CMD_T_FABRIC_STOP) 3033 target_append_str(&str, "fabric_stop"); 3034 3035 return str; 3036 } 3037 3038 static const char *target_tmf_name(enum tcm_tmreq_table tmf) 3039 { 3040 switch (tmf) { 3041 case TMR_ABORT_TASK: return "ABORT_TASK"; 3042 case TMR_ABORT_TASK_SET: return "ABORT_TASK_SET"; 3043 case TMR_CLEAR_ACA: return "CLEAR_ACA"; 3044 case TMR_CLEAR_TASK_SET: return "CLEAR_TASK_SET"; 3045 case TMR_LUN_RESET: return "LUN_RESET"; 3046 case TMR_TARGET_WARM_RESET: return "TARGET_WARM_RESET"; 3047 case TMR_TARGET_COLD_RESET: return "TARGET_COLD_RESET"; 3048 case TMR_LUN_RESET_PRO: return "LUN_RESET_PRO"; 3049 case TMR_UNKNOWN: break; 3050 } 3051 return "(?)"; 3052 } 3053 3054 void target_show_cmd(const char *pfx, struct se_cmd *cmd) 3055 { 3056 char *ts_str = target_ts_to_str(cmd->transport_state); 3057 const u8 *cdb = cmd->t_task_cdb; 3058 struct se_tmr_req *tmf = cmd->se_tmr_req; 3059 3060 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { 3061 pr_debug("%scmd %#02x:%#02x with tag %#llx dir %s i_state %d t_state %s len %d refcnt %d transport_state %s\n", 3062 pfx, cdb[0], cdb[1], cmd->tag, 3063 data_dir_name(cmd->data_direction), 3064 cmd->se_tfo->get_cmd_state(cmd), 3065 cmd_state_name(cmd->t_state), cmd->data_length, 3066 kref_read(&cmd->cmd_kref), ts_str); 3067 } else { 3068 pr_debug("%stmf %s with tag %#llx ref_task_tag %#llx i_state %d t_state %s refcnt %d transport_state %s\n", 3069 pfx, target_tmf_name(tmf->function), cmd->tag, 3070 tmf->ref_task_tag, cmd->se_tfo->get_cmd_state(cmd), 3071 cmd_state_name(cmd->t_state), 3072 kref_read(&cmd->cmd_kref), ts_str); 3073 } 3074 kfree(ts_str); 3075 } 3076 EXPORT_SYMBOL(target_show_cmd); 3077 3078 static void target_stop_session_confirm(struct percpu_ref *ref) 3079 { 3080 struct se_session *se_sess = container_of(ref, struct se_session, 3081 cmd_count); 3082 complete_all(&se_sess->stop_done); 3083 } 3084 3085 /** 3086 * target_stop_session - Stop new IO from being queued on the session. 3087 * @se_sess: session to stop 3088 */ 3089 void target_stop_session(struct se_session *se_sess) 3090 { 3091 pr_debug("Stopping session queue.\n"); 3092 if (atomic_cmpxchg(&se_sess->stopped, 0, 1) == 0) 3093 percpu_ref_kill_and_confirm(&se_sess->cmd_count, 3094 target_stop_session_confirm); 3095 } 3096 EXPORT_SYMBOL(target_stop_session); 3097 3098 /** 3099 * target_wait_for_sess_cmds - Wait for outstanding commands 3100 * @se_sess: session to wait for active I/O 3101 */ 3102 void target_wait_for_sess_cmds(struct se_session *se_sess) 3103 { 3104 int ret; 3105 3106 WARN_ON_ONCE(!atomic_read(&se_sess->stopped)); 3107 3108 do { 3109 pr_debug("Waiting for running cmds to complete.\n"); 3110 ret = wait_event_timeout(se_sess->cmd_count_wq, 3111 percpu_ref_is_zero(&se_sess->cmd_count), 3112 180 * HZ); 3113 } while (ret <= 0); 3114 3115 wait_for_completion(&se_sess->stop_done); 3116 pr_debug("Waiting for cmds done.\n"); 3117 } 3118 EXPORT_SYMBOL(target_wait_for_sess_cmds); 3119 3120 /* 3121 * Prevent that new percpu_ref_tryget_live() calls succeed and wait until 3122 * all references to the LUN have been released. Called during LUN shutdown. 3123 */ 3124 void transport_clear_lun_ref(struct se_lun *lun) 3125 { 3126 percpu_ref_kill(&lun->lun_ref); 3127 wait_for_completion(&lun->lun_shutdown_comp); 3128 } 3129 3130 static bool 3131 __transport_wait_for_tasks(struct se_cmd *cmd, bool fabric_stop, 3132 bool *aborted, bool *tas, unsigned long *flags) 3133 __releases(&cmd->t_state_lock) 3134 __acquires(&cmd->t_state_lock) 3135 { 3136 lockdep_assert_held(&cmd->t_state_lock); 3137 3138 if (fabric_stop) 3139 cmd->transport_state |= CMD_T_FABRIC_STOP; 3140 3141 if (cmd->transport_state & CMD_T_ABORTED) 3142 *aborted = true; 3143 3144 if (cmd->transport_state & CMD_T_TAS) 3145 *tas = true; 3146 3147 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && 3148 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) 3149 return false; 3150 3151 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && 3152 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) 3153 return false; 3154 3155 if (!(cmd->transport_state & CMD_T_ACTIVE)) 3156 return false; 3157 3158 if (fabric_stop && *aborted) 3159 return false; 3160 3161 cmd->transport_state |= CMD_T_STOP; 3162 3163 target_show_cmd("wait_for_tasks: Stopping ", cmd); 3164 3165 spin_unlock_irqrestore(&cmd->t_state_lock, *flags); 3166 3167 while (!wait_for_completion_timeout(&cmd->t_transport_stop_comp, 3168 180 * HZ)) 3169 target_show_cmd("wait for tasks: ", cmd); 3170 3171 spin_lock_irqsave(&cmd->t_state_lock, *flags); 3172 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP); 3173 3174 pr_debug("wait_for_tasks: Stopped wait_for_completion(&cmd->" 3175 "t_transport_stop_comp) for ITT: 0x%08llx\n", cmd->tag); 3176 3177 return true; 3178 } 3179 3180 /** 3181 * transport_wait_for_tasks - set CMD_T_STOP and wait for t_transport_stop_comp 3182 * @cmd: command to wait on 3183 */ 3184 bool transport_wait_for_tasks(struct se_cmd *cmd) 3185 { 3186 unsigned long flags; 3187 bool ret, aborted = false, tas = false; 3188 3189 spin_lock_irqsave(&cmd->t_state_lock, flags); 3190 ret = __transport_wait_for_tasks(cmd, false, &aborted, &tas, &flags); 3191 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3192 3193 return ret; 3194 } 3195 EXPORT_SYMBOL(transport_wait_for_tasks); 3196 3197 struct sense_detail { 3198 u8 key; 3199 u8 asc; 3200 u8 ascq; 3201 bool add_sense_info; 3202 }; 3203 3204 static const struct sense_detail sense_detail_table[] = { 3205 [TCM_NO_SENSE] = { 3206 .key = NOT_READY 3207 }, 3208 [TCM_NON_EXISTENT_LUN] = { 3209 .key = ILLEGAL_REQUEST, 3210 .asc = 0x25 /* LOGICAL UNIT NOT SUPPORTED */ 3211 }, 3212 [TCM_UNSUPPORTED_SCSI_OPCODE] = { 3213 .key = ILLEGAL_REQUEST, 3214 .asc = 0x20, /* INVALID COMMAND OPERATION CODE */ 3215 }, 3216 [TCM_SECTOR_COUNT_TOO_MANY] = { 3217 .key = ILLEGAL_REQUEST, 3218 .asc = 0x20, /* INVALID COMMAND OPERATION CODE */ 3219 }, 3220 [TCM_UNKNOWN_MODE_PAGE] = { 3221 .key = ILLEGAL_REQUEST, 3222 .asc = 0x24, /* INVALID FIELD IN CDB */ 3223 }, 3224 [TCM_CHECK_CONDITION_ABORT_CMD] = { 3225 .key = ABORTED_COMMAND, 3226 .asc = 0x29, /* BUS DEVICE RESET FUNCTION OCCURRED */ 3227 .ascq = 0x03, 3228 }, 3229 [TCM_INCORRECT_AMOUNT_OF_DATA] = { 3230 .key = ABORTED_COMMAND, 3231 .asc = 0x0c, /* WRITE ERROR */ 3232 .ascq = 0x0d, /* NOT ENOUGH UNSOLICITED DATA */ 3233 }, 3234 [TCM_INVALID_CDB_FIELD] = { 3235 .key = ILLEGAL_REQUEST, 3236 .asc = 0x24, /* INVALID FIELD IN CDB */ 3237 }, 3238 [TCM_INVALID_PARAMETER_LIST] = { 3239 .key = ILLEGAL_REQUEST, 3240 .asc = 0x26, /* INVALID FIELD IN PARAMETER LIST */ 3241 }, 3242 [TCM_TOO_MANY_TARGET_DESCS] = { 3243 .key = ILLEGAL_REQUEST, 3244 .asc = 0x26, 3245 .ascq = 0x06, /* TOO MANY TARGET DESCRIPTORS */ 3246 }, 3247 [TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE] = { 3248 .key = ILLEGAL_REQUEST, 3249 .asc = 0x26, 3250 .ascq = 0x07, /* UNSUPPORTED TARGET DESCRIPTOR TYPE CODE */ 3251 }, 3252 [TCM_TOO_MANY_SEGMENT_DESCS] = { 3253 .key = ILLEGAL_REQUEST, 3254 .asc = 0x26, 3255 .ascq = 0x08, /* TOO MANY SEGMENT DESCRIPTORS */ 3256 }, 3257 [TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE] = { 3258 .key = ILLEGAL_REQUEST, 3259 .asc = 0x26, 3260 .ascq = 0x09, /* UNSUPPORTED SEGMENT DESCRIPTOR TYPE CODE */ 3261 }, 3262 [TCM_PARAMETER_LIST_LENGTH_ERROR] = { 3263 .key = ILLEGAL_REQUEST, 3264 .asc = 0x1a, /* PARAMETER LIST LENGTH ERROR */ 3265 }, 3266 [TCM_UNEXPECTED_UNSOLICITED_DATA] = { 3267 .key = ILLEGAL_REQUEST, 3268 .asc = 0x0c, /* WRITE ERROR */ 3269 .ascq = 0x0c, /* UNEXPECTED_UNSOLICITED_DATA */ 3270 }, 3271 [TCM_SERVICE_CRC_ERROR] = { 3272 .key = ABORTED_COMMAND, 3273 .asc = 0x47, /* PROTOCOL SERVICE CRC ERROR */ 3274 .ascq = 0x05, /* N/A */ 3275 }, 3276 [TCM_SNACK_REJECTED] = { 3277 .key = ABORTED_COMMAND, 3278 .asc = 0x11, /* READ ERROR */ 3279 .ascq = 0x13, /* FAILED RETRANSMISSION REQUEST */ 3280 }, 3281 [TCM_WRITE_PROTECTED] = { 3282 .key = DATA_PROTECT, 3283 .asc = 0x27, /* WRITE PROTECTED */ 3284 }, 3285 [TCM_ADDRESS_OUT_OF_RANGE] = { 3286 .key = ILLEGAL_REQUEST, 3287 .asc = 0x21, /* LOGICAL BLOCK ADDRESS OUT OF RANGE */ 3288 }, 3289 [TCM_CHECK_CONDITION_UNIT_ATTENTION] = { 3290 .key = UNIT_ATTENTION, 3291 }, 3292 [TCM_MISCOMPARE_VERIFY] = { 3293 .key = MISCOMPARE, 3294 .asc = 0x1d, /* MISCOMPARE DURING VERIFY OPERATION */ 3295 .ascq = 0x00, 3296 .add_sense_info = true, 3297 }, 3298 [TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED] = { 3299 .key = ABORTED_COMMAND, 3300 .asc = 0x10, 3301 .ascq = 0x01, /* LOGICAL BLOCK GUARD CHECK FAILED */ 3302 .add_sense_info = true, 3303 }, 3304 [TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED] = { 3305 .key = ABORTED_COMMAND, 3306 .asc = 0x10, 3307 .ascq = 0x02, /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */ 3308 .add_sense_info = true, 3309 }, 3310 [TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED] = { 3311 .key = ABORTED_COMMAND, 3312 .asc = 0x10, 3313 .ascq = 0x03, /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */ 3314 .add_sense_info = true, 3315 }, 3316 [TCM_COPY_TARGET_DEVICE_NOT_REACHABLE] = { 3317 .key = COPY_ABORTED, 3318 .asc = 0x0d, 3319 .ascq = 0x02, /* COPY TARGET DEVICE NOT REACHABLE */ 3320 3321 }, 3322 [TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE] = { 3323 /* 3324 * Returning ILLEGAL REQUEST would cause immediate IO errors on 3325 * Solaris initiators. Returning NOT READY instead means the 3326 * operations will be retried a finite number of times and we 3327 * can survive intermittent errors. 3328 */ 3329 .key = NOT_READY, 3330 .asc = 0x08, /* LOGICAL UNIT COMMUNICATION FAILURE */ 3331 }, 3332 [TCM_INSUFFICIENT_REGISTRATION_RESOURCES] = { 3333 /* 3334 * From spc4r22 section5.7.7,5.7.8 3335 * If a PERSISTENT RESERVE OUT command with a REGISTER service action 3336 * or a REGISTER AND IGNORE EXISTING KEY service action or 3337 * REGISTER AND MOVE service actionis attempted, 3338 * but there are insufficient device server resources to complete the 3339 * operation, then the command shall be terminated with CHECK CONDITION 3340 * status, with the sense key set to ILLEGAL REQUEST,and the additonal 3341 * sense code set to INSUFFICIENT REGISTRATION RESOURCES. 3342 */ 3343 .key = ILLEGAL_REQUEST, 3344 .asc = 0x55, 3345 .ascq = 0x04, /* INSUFFICIENT REGISTRATION RESOURCES */ 3346 }, 3347 [TCM_INVALID_FIELD_IN_COMMAND_IU] = { 3348 .key = ILLEGAL_REQUEST, 3349 .asc = 0x0e, 3350 .ascq = 0x03, /* INVALID FIELD IN COMMAND INFORMATION UNIT */ 3351 }, 3352 [TCM_ALUA_TG_PT_STANDBY] = { 3353 .key = NOT_READY, 3354 .asc = 0x04, 3355 .ascq = ASCQ_04H_ALUA_TG_PT_STANDBY, 3356 }, 3357 [TCM_ALUA_TG_PT_UNAVAILABLE] = { 3358 .key = NOT_READY, 3359 .asc = 0x04, 3360 .ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE, 3361 }, 3362 [TCM_ALUA_STATE_TRANSITION] = { 3363 .key = NOT_READY, 3364 .asc = 0x04, 3365 .ascq = ASCQ_04H_ALUA_STATE_TRANSITION, 3366 }, 3367 [TCM_ALUA_OFFLINE] = { 3368 .key = NOT_READY, 3369 .asc = 0x04, 3370 .ascq = ASCQ_04H_ALUA_OFFLINE, 3371 }, 3372 }; 3373 3374 /** 3375 * translate_sense_reason - translate a sense reason into T10 key, asc and ascq 3376 * @cmd: SCSI command in which the resulting sense buffer or SCSI status will 3377 * be stored. 3378 * @reason: LIO sense reason code. If this argument has the value 3379 * TCM_CHECK_CONDITION_UNIT_ATTENTION, try to dequeue a unit attention. If 3380 * dequeuing a unit attention fails due to multiple commands being processed 3381 * concurrently, set the command status to BUSY. 3382 * 3383 * Return: 0 upon success or -EINVAL if the sense buffer is too small. 3384 */ 3385 static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason) 3386 { 3387 const struct sense_detail *sd; 3388 u8 *buffer = cmd->sense_buffer; 3389 int r = (__force int)reason; 3390 u8 key, asc, ascq; 3391 bool desc_format = target_sense_desc_format(cmd->se_dev); 3392 3393 if (r < ARRAY_SIZE(sense_detail_table) && sense_detail_table[r].key) 3394 sd = &sense_detail_table[r]; 3395 else 3396 sd = &sense_detail_table[(__force int) 3397 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE]; 3398 3399 key = sd->key; 3400 if (reason == TCM_CHECK_CONDITION_UNIT_ATTENTION) { 3401 if (!core_scsi3_ua_for_check_condition(cmd, &key, &asc, 3402 &ascq)) { 3403 cmd->scsi_status = SAM_STAT_BUSY; 3404 return; 3405 } 3406 } else { 3407 WARN_ON_ONCE(sd->asc == 0); 3408 asc = sd->asc; 3409 ascq = sd->ascq; 3410 } 3411 3412 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE; 3413 cmd->scsi_status = SAM_STAT_CHECK_CONDITION; 3414 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; 3415 scsi_build_sense_buffer(desc_format, buffer, key, asc, ascq); 3416 if (sd->add_sense_info) 3417 WARN_ON_ONCE(scsi_set_sense_information(buffer, 3418 cmd->scsi_sense_length, 3419 cmd->sense_info) < 0); 3420 } 3421 3422 int 3423 transport_send_check_condition_and_sense(struct se_cmd *cmd, 3424 sense_reason_t reason, int from_transport) 3425 { 3426 unsigned long flags; 3427 3428 WARN_ON_ONCE(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB); 3429 3430 spin_lock_irqsave(&cmd->t_state_lock, flags); 3431 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { 3432 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3433 return 0; 3434 } 3435 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION; 3436 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3437 3438 if (!from_transport) 3439 translate_sense_reason(cmd, reason); 3440 3441 trace_target_cmd_complete(cmd); 3442 return cmd->se_tfo->queue_status(cmd); 3443 } 3444 EXPORT_SYMBOL(transport_send_check_condition_and_sense); 3445 3446 /** 3447 * target_send_busy - Send SCSI BUSY status back to the initiator 3448 * @cmd: SCSI command for which to send a BUSY reply. 3449 * 3450 * Note: Only call this function if target_submit_cmd*() failed. 3451 */ 3452 int target_send_busy(struct se_cmd *cmd) 3453 { 3454 WARN_ON_ONCE(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB); 3455 3456 cmd->scsi_status = SAM_STAT_BUSY; 3457 trace_target_cmd_complete(cmd); 3458 return cmd->se_tfo->queue_status(cmd); 3459 } 3460 EXPORT_SYMBOL(target_send_busy); 3461 3462 static void target_tmr_work(struct work_struct *work) 3463 { 3464 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 3465 struct se_device *dev = cmd->se_dev; 3466 struct se_tmr_req *tmr = cmd->se_tmr_req; 3467 int ret; 3468 3469 if (cmd->transport_state & CMD_T_ABORTED) 3470 goto aborted; 3471 3472 switch (tmr->function) { 3473 case TMR_ABORT_TASK: 3474 core_tmr_abort_task(dev, tmr, cmd->se_sess); 3475 break; 3476 case TMR_ABORT_TASK_SET: 3477 case TMR_CLEAR_ACA: 3478 case TMR_CLEAR_TASK_SET: 3479 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED; 3480 break; 3481 case TMR_LUN_RESET: 3482 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL); 3483 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE : 3484 TMR_FUNCTION_REJECTED; 3485 if (tmr->response == TMR_FUNCTION_COMPLETE) { 3486 target_ua_allocate_lun(cmd->se_sess->se_node_acl, 3487 cmd->orig_fe_lun, 0x29, 3488 ASCQ_29H_BUS_DEVICE_RESET_FUNCTION_OCCURRED); 3489 } 3490 break; 3491 case TMR_TARGET_WARM_RESET: 3492 tmr->response = TMR_FUNCTION_REJECTED; 3493 break; 3494 case TMR_TARGET_COLD_RESET: 3495 tmr->response = TMR_FUNCTION_REJECTED; 3496 break; 3497 default: 3498 pr_err("Unknown TMR function: 0x%02x.\n", 3499 tmr->function); 3500 tmr->response = TMR_FUNCTION_REJECTED; 3501 break; 3502 } 3503 3504 if (cmd->transport_state & CMD_T_ABORTED) 3505 goto aborted; 3506 3507 cmd->se_tfo->queue_tm_rsp(cmd); 3508 3509 transport_lun_remove_cmd(cmd); 3510 transport_cmd_check_stop_to_fabric(cmd); 3511 return; 3512 3513 aborted: 3514 target_handle_abort(cmd); 3515 } 3516 3517 int transport_generic_handle_tmr( 3518 struct se_cmd *cmd) 3519 { 3520 unsigned long flags; 3521 bool aborted = false; 3522 3523 spin_lock_irqsave(&cmd->t_state_lock, flags); 3524 if (cmd->transport_state & CMD_T_ABORTED) { 3525 aborted = true; 3526 } else { 3527 cmd->t_state = TRANSPORT_ISTATE_PROCESSING; 3528 cmd->transport_state |= CMD_T_ACTIVE; 3529 } 3530 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3531 3532 if (aborted) { 3533 pr_warn_ratelimited("handle_tmr caught CMD_T_ABORTED TMR %d ref_tag: %llu tag: %llu\n", 3534 cmd->se_tmr_req->function, 3535 cmd->se_tmr_req->ref_task_tag, cmd->tag); 3536 target_handle_abort(cmd); 3537 return 0; 3538 } 3539 3540 INIT_WORK(&cmd->work, target_tmr_work); 3541 schedule_work(&cmd->work); 3542 return 0; 3543 } 3544 EXPORT_SYMBOL(transport_generic_handle_tmr); 3545 3546 bool 3547 target_check_wce(struct se_device *dev) 3548 { 3549 bool wce = false; 3550 3551 if (dev->transport->get_write_cache) 3552 wce = dev->transport->get_write_cache(dev); 3553 else if (dev->dev_attrib.emulate_write_cache > 0) 3554 wce = true; 3555 3556 return wce; 3557 } 3558 3559 bool 3560 target_check_fua(struct se_device *dev) 3561 { 3562 return target_check_wce(dev) && dev->dev_attrib.emulate_fua_write > 0; 3563 } 3564