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_debug_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 atomic_inc_mb(&dev->non_ordered); 2177 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x\n", 2178 cmd->t_task_cdb[0]); 2179 return false; 2180 case TCM_ORDERED_TAG: 2181 atomic_inc_mb(&dev->delayed_cmd_count); 2182 2183 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list\n", 2184 cmd->t_task_cdb[0]); 2185 break; 2186 default: 2187 /* 2188 * For SIMPLE and UNTAGGED Task Attribute commands 2189 */ 2190 atomic_inc_mb(&dev->non_ordered); 2191 2192 if (atomic_read(&dev->delayed_cmd_count) == 0) 2193 return false; 2194 break; 2195 } 2196 2197 if (cmd->sam_task_attr != TCM_ORDERED_TAG) { 2198 atomic_inc_mb(&dev->delayed_cmd_count); 2199 /* 2200 * We will account for this when we dequeue from the delayed 2201 * list. 2202 */ 2203 atomic_dec_mb(&dev->non_ordered); 2204 } 2205 2206 spin_lock_irq(&cmd->t_state_lock); 2207 cmd->transport_state &= ~CMD_T_SENT; 2208 spin_unlock_irq(&cmd->t_state_lock); 2209 2210 spin_lock(&dev->delayed_cmd_lock); 2211 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list); 2212 spin_unlock(&dev->delayed_cmd_lock); 2213 2214 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to delayed CMD listn", 2215 cmd->t_task_cdb[0], cmd->sam_task_attr); 2216 /* 2217 * We may have no non ordered cmds when this function started or we 2218 * could have raced with the last simple/head cmd completing, so kick 2219 * the delayed handler here. 2220 */ 2221 schedule_work(&dev->delayed_cmd_work); 2222 return true; 2223 } 2224 2225 void target_execute_cmd(struct se_cmd *cmd) 2226 { 2227 /* 2228 * Determine if frontend context caller is requesting the stopping of 2229 * this command for frontend exceptions. 2230 * 2231 * If the received CDB has already been aborted stop processing it here. 2232 */ 2233 if (target_cmd_interrupted(cmd)) 2234 return; 2235 2236 spin_lock_irq(&cmd->t_state_lock); 2237 cmd->t_state = TRANSPORT_PROCESSING; 2238 cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT; 2239 spin_unlock_irq(&cmd->t_state_lock); 2240 2241 if (target_write_prot_action(cmd)) 2242 return; 2243 2244 if (target_handle_task_attr(cmd)) 2245 return; 2246 2247 __target_execute_cmd(cmd, true); 2248 } 2249 EXPORT_SYMBOL(target_execute_cmd); 2250 2251 /* 2252 * Process all commands up to the last received ORDERED task attribute which 2253 * requires another blocking boundary 2254 */ 2255 void target_do_delayed_work(struct work_struct *work) 2256 { 2257 struct se_device *dev = container_of(work, struct se_device, 2258 delayed_cmd_work); 2259 2260 spin_lock(&dev->delayed_cmd_lock); 2261 while (!dev->ordered_sync_in_progress) { 2262 struct se_cmd *cmd; 2263 2264 if (list_empty(&dev->delayed_cmd_list)) 2265 break; 2266 2267 cmd = list_entry(dev->delayed_cmd_list.next, 2268 struct se_cmd, se_delayed_node); 2269 2270 if (cmd->sam_task_attr == TCM_ORDERED_TAG) { 2271 /* 2272 * Check if we started with: 2273 * [ordered] [simple] [ordered] 2274 * and we are now at the last ordered so we have to wait 2275 * for the simple cmd. 2276 */ 2277 if (atomic_read(&dev->non_ordered) > 0) 2278 break; 2279 2280 dev->ordered_sync_in_progress = true; 2281 } 2282 2283 list_del(&cmd->se_delayed_node); 2284 atomic_dec_mb(&dev->delayed_cmd_count); 2285 spin_unlock(&dev->delayed_cmd_lock); 2286 2287 if (cmd->sam_task_attr != TCM_ORDERED_TAG) 2288 atomic_inc_mb(&dev->non_ordered); 2289 2290 cmd->transport_state |= CMD_T_SENT; 2291 2292 __target_execute_cmd(cmd, true); 2293 2294 spin_lock(&dev->delayed_cmd_lock); 2295 } 2296 spin_unlock(&dev->delayed_cmd_lock); 2297 } 2298 2299 /* 2300 * Called from I/O completion to determine which dormant/delayed 2301 * and ordered cmds need to have their tasks added to the execution queue. 2302 */ 2303 static void transport_complete_task_attr(struct se_cmd *cmd) 2304 { 2305 struct se_device *dev = cmd->se_dev; 2306 2307 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) 2308 return; 2309 2310 if (!(cmd->se_cmd_flags & SCF_TASK_ATTR_SET)) 2311 goto restart; 2312 2313 if (cmd->sam_task_attr == TCM_SIMPLE_TAG) { 2314 atomic_dec_mb(&dev->non_ordered); 2315 dev->dev_cur_ordered_id++; 2316 } else if (cmd->sam_task_attr == TCM_HEAD_TAG) { 2317 atomic_dec_mb(&dev->non_ordered); 2318 dev->dev_cur_ordered_id++; 2319 pr_debug("Incremented dev_cur_ordered_id: %u for HEAD_OF_QUEUE\n", 2320 dev->dev_cur_ordered_id); 2321 } else if (cmd->sam_task_attr == TCM_ORDERED_TAG) { 2322 spin_lock(&dev->delayed_cmd_lock); 2323 dev->ordered_sync_in_progress = false; 2324 spin_unlock(&dev->delayed_cmd_lock); 2325 2326 dev->dev_cur_ordered_id++; 2327 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED\n", 2328 dev->dev_cur_ordered_id); 2329 } 2330 cmd->se_cmd_flags &= ~SCF_TASK_ATTR_SET; 2331 2332 restart: 2333 if (atomic_read(&dev->delayed_cmd_count) > 0) 2334 schedule_work(&dev->delayed_cmd_work); 2335 } 2336 2337 static void transport_complete_qf(struct se_cmd *cmd) 2338 { 2339 int ret = 0; 2340 2341 transport_complete_task_attr(cmd); 2342 /* 2343 * If a fabric driver ->write_pending() or ->queue_data_in() callback 2344 * has returned neither -ENOMEM or -EAGAIN, assume it's fatal and 2345 * the same callbacks should not be retried. Return CHECK_CONDITION 2346 * if a scsi_status is not already set. 2347 * 2348 * If a fabric driver ->queue_status() has returned non zero, always 2349 * keep retrying no matter what.. 2350 */ 2351 if (cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) { 2352 if (cmd->scsi_status) 2353 goto queue_status; 2354 2355 translate_sense_reason(cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE); 2356 goto queue_status; 2357 } 2358 2359 /* 2360 * Check if we need to send a sense buffer from 2361 * the struct se_cmd in question. We do NOT want 2362 * to take this path of the IO has been marked as 2363 * needing to be treated like a "normal read". This 2364 * is the case if it's a tape read, and either the 2365 * FM, EOM, or ILI bits are set, but there is no 2366 * sense data. 2367 */ 2368 if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && 2369 cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) 2370 goto queue_status; 2371 2372 switch (cmd->data_direction) { 2373 case DMA_FROM_DEVICE: 2374 /* queue status if not treating this as a normal read */ 2375 if (cmd->scsi_status && 2376 !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL)) 2377 goto queue_status; 2378 2379 trace_target_cmd_complete(cmd); 2380 ret = cmd->se_tfo->queue_data_in(cmd); 2381 break; 2382 case DMA_TO_DEVICE: 2383 if (cmd->se_cmd_flags & SCF_BIDI) { 2384 ret = cmd->se_tfo->queue_data_in(cmd); 2385 break; 2386 } 2387 fallthrough; 2388 case DMA_NONE: 2389 queue_status: 2390 trace_target_cmd_complete(cmd); 2391 ret = cmd->se_tfo->queue_status(cmd); 2392 break; 2393 default: 2394 break; 2395 } 2396 2397 if (ret < 0) { 2398 transport_handle_queue_full(cmd, cmd->se_dev, ret, false); 2399 return; 2400 } 2401 transport_lun_remove_cmd(cmd); 2402 transport_cmd_check_stop_to_fabric(cmd); 2403 } 2404 2405 static void transport_handle_queue_full(struct se_cmd *cmd, struct se_device *dev, 2406 int err, bool write_pending) 2407 { 2408 /* 2409 * -EAGAIN or -ENOMEM signals retry of ->write_pending() and/or 2410 * ->queue_data_in() callbacks from new process context. 2411 * 2412 * Otherwise for other errors, transport_complete_qf() will send 2413 * CHECK_CONDITION via ->queue_status() instead of attempting to 2414 * retry associated fabric driver data-transfer callbacks. 2415 */ 2416 if (err == -EAGAIN || err == -ENOMEM) { 2417 cmd->t_state = (write_pending) ? TRANSPORT_COMPLETE_QF_WP : 2418 TRANSPORT_COMPLETE_QF_OK; 2419 } else { 2420 pr_warn_ratelimited("Got unknown fabric queue status: %d\n", err); 2421 cmd->t_state = TRANSPORT_COMPLETE_QF_ERR; 2422 } 2423 2424 spin_lock_irq(&dev->qf_cmd_lock); 2425 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list); 2426 atomic_inc_mb(&dev->dev_qf_count); 2427 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock); 2428 2429 schedule_work(&cmd->se_dev->qf_work_queue); 2430 } 2431 2432 static bool target_read_prot_action(struct se_cmd *cmd) 2433 { 2434 switch (cmd->prot_op) { 2435 case TARGET_PROT_DIN_STRIP: 2436 if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_STRIP)) { 2437 u32 sectors = cmd->data_length >> 2438 ilog2(cmd->se_dev->dev_attrib.block_size); 2439 2440 cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba, 2441 sectors, 0, cmd->t_prot_sg, 2442 0); 2443 if (cmd->pi_err) 2444 return true; 2445 } 2446 break; 2447 case TARGET_PROT_DIN_INSERT: 2448 if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_INSERT) 2449 break; 2450 2451 sbc_dif_generate(cmd); 2452 break; 2453 default: 2454 break; 2455 } 2456 2457 return false; 2458 } 2459 2460 static void target_complete_ok_work(struct work_struct *work) 2461 { 2462 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 2463 int ret; 2464 2465 /* 2466 * Check if we need to move delayed/dormant tasks from cmds on the 2467 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task 2468 * Attribute. 2469 */ 2470 transport_complete_task_attr(cmd); 2471 2472 /* 2473 * Check to schedule QUEUE_FULL work, or execute an existing 2474 * cmd->transport_qf_callback() 2475 */ 2476 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0) 2477 schedule_work(&cmd->se_dev->qf_work_queue); 2478 2479 /* 2480 * Check if we need to send a sense buffer from 2481 * the struct se_cmd in question. We do NOT want 2482 * to take this path of the IO has been marked as 2483 * needing to be treated like a "normal read". This 2484 * is the case if it's a tape read, and either the 2485 * FM, EOM, or ILI bits are set, but there is no 2486 * sense data. 2487 */ 2488 if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) && 2489 cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { 2490 WARN_ON(!cmd->scsi_status); 2491 ret = transport_send_check_condition_and_sense( 2492 cmd, 0, 1); 2493 if (ret) 2494 goto queue_full; 2495 2496 transport_lun_remove_cmd(cmd); 2497 transport_cmd_check_stop_to_fabric(cmd); 2498 return; 2499 } 2500 /* 2501 * Check for a callback, used by amongst other things 2502 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation. 2503 */ 2504 if (cmd->transport_complete_callback) { 2505 sense_reason_t rc; 2506 bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE); 2507 bool zero_dl = !(cmd->data_length); 2508 int post_ret = 0; 2509 2510 rc = cmd->transport_complete_callback(cmd, true, &post_ret); 2511 if (!rc && !post_ret) { 2512 if (caw && zero_dl) 2513 goto queue_rsp; 2514 2515 return; 2516 } else if (rc) { 2517 ret = transport_send_check_condition_and_sense(cmd, 2518 rc, 0); 2519 if (ret) 2520 goto queue_full; 2521 2522 transport_lun_remove_cmd(cmd); 2523 transport_cmd_check_stop_to_fabric(cmd); 2524 return; 2525 } 2526 } 2527 2528 queue_rsp: 2529 switch (cmd->data_direction) { 2530 case DMA_FROM_DEVICE: 2531 /* 2532 * if this is a READ-type IO, but SCSI status 2533 * is set, then skip returning data and just 2534 * return the status -- unless this IO is marked 2535 * as needing to be treated as a normal read, 2536 * in which case we want to go ahead and return 2537 * the data. This happens, for example, for tape 2538 * reads with the FM, EOM, or ILI bits set, with 2539 * no sense data. 2540 */ 2541 if (cmd->scsi_status && 2542 !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL)) 2543 goto queue_status; 2544 2545 atomic_long_add(cmd->data_length, 2546 &cmd->se_lun->lun_stats.tx_data_octets); 2547 /* 2548 * Perform READ_STRIP of PI using software emulation when 2549 * backend had PI enabled, if the transport will not be 2550 * performing hardware READ_STRIP offload. 2551 */ 2552 if (target_read_prot_action(cmd)) { 2553 ret = transport_send_check_condition_and_sense(cmd, 2554 cmd->pi_err, 0); 2555 if (ret) 2556 goto queue_full; 2557 2558 transport_lun_remove_cmd(cmd); 2559 transport_cmd_check_stop_to_fabric(cmd); 2560 return; 2561 } 2562 2563 trace_target_cmd_complete(cmd); 2564 ret = cmd->se_tfo->queue_data_in(cmd); 2565 if (ret) 2566 goto queue_full; 2567 break; 2568 case DMA_TO_DEVICE: 2569 atomic_long_add(cmd->data_length, 2570 &cmd->se_lun->lun_stats.rx_data_octets); 2571 /* 2572 * Check if we need to send READ payload for BIDI-COMMAND 2573 */ 2574 if (cmd->se_cmd_flags & SCF_BIDI) { 2575 atomic_long_add(cmd->data_length, 2576 &cmd->se_lun->lun_stats.tx_data_octets); 2577 ret = cmd->se_tfo->queue_data_in(cmd); 2578 if (ret) 2579 goto queue_full; 2580 break; 2581 } 2582 fallthrough; 2583 case DMA_NONE: 2584 queue_status: 2585 trace_target_cmd_complete(cmd); 2586 ret = cmd->se_tfo->queue_status(cmd); 2587 if (ret) 2588 goto queue_full; 2589 break; 2590 default: 2591 break; 2592 } 2593 2594 transport_lun_remove_cmd(cmd); 2595 transport_cmd_check_stop_to_fabric(cmd); 2596 return; 2597 2598 queue_full: 2599 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p," 2600 " data_direction: %d\n", cmd, cmd->data_direction); 2601 2602 transport_handle_queue_full(cmd, cmd->se_dev, ret, false); 2603 } 2604 2605 void target_free_sgl(struct scatterlist *sgl, int nents) 2606 { 2607 sgl_free_n_order(sgl, nents, 0); 2608 } 2609 EXPORT_SYMBOL(target_free_sgl); 2610 2611 static inline void transport_reset_sgl_orig(struct se_cmd *cmd) 2612 { 2613 /* 2614 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE 2615 * emulation, and free + reset pointers if necessary.. 2616 */ 2617 if (!cmd->t_data_sg_orig) 2618 return; 2619 2620 kfree(cmd->t_data_sg); 2621 cmd->t_data_sg = cmd->t_data_sg_orig; 2622 cmd->t_data_sg_orig = NULL; 2623 cmd->t_data_nents = cmd->t_data_nents_orig; 2624 cmd->t_data_nents_orig = 0; 2625 } 2626 2627 static inline void transport_free_pages(struct se_cmd *cmd) 2628 { 2629 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) { 2630 target_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents); 2631 cmd->t_prot_sg = NULL; 2632 cmd->t_prot_nents = 0; 2633 } 2634 2635 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) { 2636 /* 2637 * Release special case READ buffer payload required for 2638 * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE 2639 */ 2640 if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) { 2641 target_free_sgl(cmd->t_bidi_data_sg, 2642 cmd->t_bidi_data_nents); 2643 cmd->t_bidi_data_sg = NULL; 2644 cmd->t_bidi_data_nents = 0; 2645 } 2646 transport_reset_sgl_orig(cmd); 2647 return; 2648 } 2649 transport_reset_sgl_orig(cmd); 2650 2651 target_free_sgl(cmd->t_data_sg, cmd->t_data_nents); 2652 cmd->t_data_sg = NULL; 2653 cmd->t_data_nents = 0; 2654 2655 target_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents); 2656 cmd->t_bidi_data_sg = NULL; 2657 cmd->t_bidi_data_nents = 0; 2658 } 2659 2660 void *transport_kmap_data_sg(struct se_cmd *cmd) 2661 { 2662 struct scatterlist *sg = cmd->t_data_sg; 2663 struct page **pages; 2664 int i; 2665 2666 /* 2667 * We need to take into account a possible offset here for fabrics like 2668 * tcm_loop who may be using a contig buffer from the SCSI midlayer for 2669 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd() 2670 */ 2671 if (!cmd->t_data_nents) 2672 return NULL; 2673 2674 BUG_ON(!sg); 2675 if (cmd->t_data_nents == 1) 2676 return kmap(sg_page(sg)) + sg->offset; 2677 2678 /* >1 page. use vmap */ 2679 pages = kmalloc_array(cmd->t_data_nents, sizeof(*pages), GFP_KERNEL); 2680 if (!pages) 2681 return NULL; 2682 2683 /* convert sg[] to pages[] */ 2684 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) { 2685 pages[i] = sg_page(sg); 2686 } 2687 2688 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL); 2689 kfree(pages); 2690 if (!cmd->t_data_vmap) 2691 return NULL; 2692 2693 return cmd->t_data_vmap + cmd->t_data_sg[0].offset; 2694 } 2695 EXPORT_SYMBOL(transport_kmap_data_sg); 2696 2697 void transport_kunmap_data_sg(struct se_cmd *cmd) 2698 { 2699 if (!cmd->t_data_nents) { 2700 return; 2701 } else if (cmd->t_data_nents == 1) { 2702 kunmap(sg_page(cmd->t_data_sg)); 2703 return; 2704 } 2705 2706 vunmap(cmd->t_data_vmap); 2707 cmd->t_data_vmap = NULL; 2708 } 2709 EXPORT_SYMBOL(transport_kunmap_data_sg); 2710 2711 int 2712 target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length, 2713 bool zero_page, bool chainable) 2714 { 2715 gfp_t gfp = GFP_KERNEL | (zero_page ? __GFP_ZERO : 0); 2716 2717 *sgl = sgl_alloc_order(length, 0, chainable, gfp, nents); 2718 return *sgl ? 0 : -ENOMEM; 2719 } 2720 EXPORT_SYMBOL(target_alloc_sgl); 2721 2722 /* 2723 * Allocate any required resources to execute the command. For writes we 2724 * might not have the payload yet, so notify the fabric via a call to 2725 * ->write_pending instead. Otherwise place it on the execution queue. 2726 */ 2727 sense_reason_t 2728 transport_generic_new_cmd(struct se_cmd *cmd) 2729 { 2730 unsigned long flags; 2731 int ret = 0; 2732 bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB); 2733 2734 if (cmd->prot_op != TARGET_PROT_NORMAL && 2735 !(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) { 2736 ret = target_alloc_sgl(&cmd->t_prot_sg, &cmd->t_prot_nents, 2737 cmd->prot_length, true, false); 2738 if (ret < 0) 2739 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2740 } 2741 2742 /* 2743 * Determine if the TCM fabric module has already allocated physical 2744 * memory, and is directly calling transport_generic_map_mem_to_cmd() 2745 * beforehand. 2746 */ 2747 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) && 2748 cmd->data_length) { 2749 2750 if ((cmd->se_cmd_flags & SCF_BIDI) || 2751 (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) { 2752 u32 bidi_length; 2753 2754 if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) 2755 bidi_length = cmd->t_task_nolb * 2756 cmd->se_dev->dev_attrib.block_size; 2757 else 2758 bidi_length = cmd->data_length; 2759 2760 ret = target_alloc_sgl(&cmd->t_bidi_data_sg, 2761 &cmd->t_bidi_data_nents, 2762 bidi_length, zero_flag, false); 2763 if (ret < 0) 2764 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2765 } 2766 2767 ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents, 2768 cmd->data_length, zero_flag, false); 2769 if (ret < 0) 2770 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2771 } else if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) && 2772 cmd->data_length) { 2773 /* 2774 * Special case for COMPARE_AND_WRITE with fabrics 2775 * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC. 2776 */ 2777 u32 caw_length = cmd->t_task_nolb * 2778 cmd->se_dev->dev_attrib.block_size; 2779 2780 ret = target_alloc_sgl(&cmd->t_bidi_data_sg, 2781 &cmd->t_bidi_data_nents, 2782 caw_length, zero_flag, false); 2783 if (ret < 0) 2784 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; 2785 } 2786 /* 2787 * If this command is not a write we can execute it right here, 2788 * for write buffers we need to notify the fabric driver first 2789 * and let it call back once the write buffers are ready. 2790 */ 2791 target_add_to_state_list(cmd); 2792 if (cmd->data_direction != DMA_TO_DEVICE || cmd->data_length == 0) { 2793 target_execute_cmd(cmd); 2794 return 0; 2795 } 2796 2797 spin_lock_irqsave(&cmd->t_state_lock, flags); 2798 cmd->t_state = TRANSPORT_WRITE_PENDING; 2799 /* 2800 * Determine if frontend context caller is requesting the stopping of 2801 * this command for frontend exceptions. 2802 */ 2803 if (cmd->transport_state & CMD_T_STOP && 2804 !cmd->se_tfo->write_pending_must_be_called) { 2805 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n", 2806 __func__, __LINE__, cmd->tag); 2807 2808 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2809 2810 complete_all(&cmd->t_transport_stop_comp); 2811 return 0; 2812 } 2813 cmd->transport_state &= ~CMD_T_ACTIVE; 2814 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2815 2816 ret = cmd->se_tfo->write_pending(cmd); 2817 if (ret) 2818 goto queue_full; 2819 2820 return 0; 2821 2822 queue_full: 2823 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd); 2824 transport_handle_queue_full(cmd, cmd->se_dev, ret, true); 2825 return 0; 2826 } 2827 EXPORT_SYMBOL(transport_generic_new_cmd); 2828 2829 static void transport_write_pending_qf(struct se_cmd *cmd) 2830 { 2831 unsigned long flags; 2832 int ret; 2833 bool stop; 2834 2835 spin_lock_irqsave(&cmd->t_state_lock, flags); 2836 stop = (cmd->transport_state & (CMD_T_STOP | CMD_T_ABORTED)); 2837 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2838 2839 if (stop) { 2840 pr_debug("%s:%d CMD_T_STOP|CMD_T_ABORTED for ITT: 0x%08llx\n", 2841 __func__, __LINE__, cmd->tag); 2842 complete_all(&cmd->t_transport_stop_comp); 2843 return; 2844 } 2845 2846 ret = cmd->se_tfo->write_pending(cmd); 2847 if (ret) { 2848 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", 2849 cmd); 2850 transport_handle_queue_full(cmd, cmd->se_dev, ret, true); 2851 } 2852 } 2853 2854 static bool 2855 __transport_wait_for_tasks(struct se_cmd *, bool, bool *, bool *, 2856 unsigned long *flags); 2857 2858 static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas) 2859 { 2860 unsigned long flags; 2861 2862 spin_lock_irqsave(&cmd->t_state_lock, flags); 2863 __transport_wait_for_tasks(cmd, true, aborted, tas, &flags); 2864 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 2865 } 2866 2867 /* 2868 * Call target_put_sess_cmd() and wait until target_release_cmd_kref(@cmd) has 2869 * finished. 2870 */ 2871 void target_put_cmd_and_wait(struct se_cmd *cmd) 2872 { 2873 DECLARE_COMPLETION_ONSTACK(compl); 2874 2875 WARN_ON_ONCE(cmd->abrt_compl); 2876 cmd->abrt_compl = &compl; 2877 target_put_sess_cmd(cmd); 2878 wait_for_completion(&compl); 2879 } 2880 2881 /* 2882 * This function is called by frontend drivers after processing of a command 2883 * has finished. 2884 * 2885 * The protocol for ensuring that either the regular frontend command 2886 * processing flow or target_handle_abort() code drops one reference is as 2887 * follows: 2888 * - Calling .queue_data_in(), .queue_status() or queue_tm_rsp() will cause 2889 * the frontend driver to call this function synchronously or asynchronously. 2890 * That will cause one reference to be dropped. 2891 * - During regular command processing the target core sets CMD_T_COMPLETE 2892 * before invoking one of the .queue_*() functions. 2893 * - The code that aborts commands skips commands and TMFs for which 2894 * CMD_T_COMPLETE has been set. 2895 * - CMD_T_ABORTED is set atomically after the CMD_T_COMPLETE check for 2896 * commands that will be aborted. 2897 * - If the CMD_T_ABORTED flag is set but CMD_T_TAS has not been set 2898 * transport_generic_free_cmd() skips its call to target_put_sess_cmd(). 2899 * - For aborted commands for which CMD_T_TAS has been set .queue_status() will 2900 * be called and will drop a reference. 2901 * - For aborted commands for which CMD_T_TAS has not been set .aborted_task() 2902 * will be called. target_handle_abort() will drop the final reference. 2903 */ 2904 int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) 2905 { 2906 DECLARE_COMPLETION_ONSTACK(compl); 2907 int ret = 0; 2908 bool aborted = false, tas = false; 2909 2910 if (wait_for_tasks) 2911 target_wait_free_cmd(cmd, &aborted, &tas); 2912 2913 if (cmd->se_cmd_flags & SCF_SE_LUN_CMD) { 2914 /* 2915 * Handle WRITE failure case where transport_generic_new_cmd() 2916 * has already added se_cmd to state_list, but fabric has 2917 * failed command before I/O submission. 2918 */ 2919 if (cmd->state_active) 2920 target_remove_from_state_list(cmd); 2921 2922 if (cmd->se_lun) 2923 transport_lun_remove_cmd(cmd); 2924 } 2925 if (aborted) 2926 cmd->free_compl = &compl; 2927 ret = target_put_sess_cmd(cmd); 2928 if (aborted) { 2929 pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag); 2930 wait_for_completion(&compl); 2931 ret = 1; 2932 } 2933 return ret; 2934 } 2935 EXPORT_SYMBOL(transport_generic_free_cmd); 2936 2937 /** 2938 * target_get_sess_cmd - Verify the session is accepting cmds and take ref 2939 * @se_cmd: command descriptor to add 2940 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd() 2941 */ 2942 int target_get_sess_cmd(struct se_cmd *se_cmd, bool ack_kref) 2943 { 2944 struct se_session *se_sess = se_cmd->se_sess; 2945 int ret = 0; 2946 2947 /* 2948 * Add a second kref if the fabric caller is expecting to handle 2949 * fabric acknowledgement that requires two target_put_sess_cmd() 2950 * invocations before se_cmd descriptor release. 2951 */ 2952 if (ack_kref) { 2953 kref_get(&se_cmd->cmd_kref); 2954 se_cmd->se_cmd_flags |= SCF_ACK_KREF; 2955 } 2956 2957 if (!percpu_ref_tryget_live(&se_sess->cmd_count)) 2958 ret = -ESHUTDOWN; 2959 2960 if (ret && ack_kref) 2961 target_put_sess_cmd(se_cmd); 2962 2963 return ret; 2964 } 2965 EXPORT_SYMBOL(target_get_sess_cmd); 2966 2967 static void target_free_cmd_mem(struct se_cmd *cmd) 2968 { 2969 transport_free_pages(cmd); 2970 2971 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) 2972 core_tmr_release_req(cmd->se_tmr_req); 2973 if (cmd->t_task_cdb != cmd->__t_task_cdb) 2974 kfree(cmd->t_task_cdb); 2975 } 2976 2977 static void target_release_cmd_kref(struct kref *kref) 2978 { 2979 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref); 2980 struct se_session *se_sess = se_cmd->se_sess; 2981 struct completion *free_compl = se_cmd->free_compl; 2982 struct completion *abrt_compl = se_cmd->abrt_compl; 2983 2984 target_free_cmd_mem(se_cmd); 2985 se_cmd->se_tfo->release_cmd(se_cmd); 2986 if (free_compl) 2987 complete(free_compl); 2988 if (abrt_compl) 2989 complete(abrt_compl); 2990 2991 percpu_ref_put(&se_sess->cmd_count); 2992 } 2993 2994 /** 2995 * target_put_sess_cmd - decrease the command reference count 2996 * @se_cmd: command to drop a reference from 2997 * 2998 * Returns 1 if and only if this target_put_sess_cmd() call caused the 2999 * refcount to drop to zero. Returns zero otherwise. 3000 */ 3001 int target_put_sess_cmd(struct se_cmd *se_cmd) 3002 { 3003 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref); 3004 } 3005 EXPORT_SYMBOL(target_put_sess_cmd); 3006 3007 static const char *data_dir_name(enum dma_data_direction d) 3008 { 3009 switch (d) { 3010 case DMA_BIDIRECTIONAL: return "BIDI"; 3011 case DMA_TO_DEVICE: return "WRITE"; 3012 case DMA_FROM_DEVICE: return "READ"; 3013 case DMA_NONE: return "NONE"; 3014 } 3015 3016 return "(?)"; 3017 } 3018 3019 static const char *cmd_state_name(enum transport_state_table t) 3020 { 3021 switch (t) { 3022 case TRANSPORT_NO_STATE: return "NO_STATE"; 3023 case TRANSPORT_NEW_CMD: return "NEW_CMD"; 3024 case TRANSPORT_WRITE_PENDING: return "WRITE_PENDING"; 3025 case TRANSPORT_PROCESSING: return "PROCESSING"; 3026 case TRANSPORT_COMPLETE: return "COMPLETE"; 3027 case TRANSPORT_ISTATE_PROCESSING: 3028 return "ISTATE_PROCESSING"; 3029 case TRANSPORT_COMPLETE_QF_WP: return "COMPLETE_QF_WP"; 3030 case TRANSPORT_COMPLETE_QF_OK: return "COMPLETE_QF_OK"; 3031 case TRANSPORT_COMPLETE_QF_ERR: return "COMPLETE_QF_ERR"; 3032 } 3033 3034 return "(?)"; 3035 } 3036 3037 static void target_append_str(char **str, const char *txt) 3038 { 3039 char *prev = *str; 3040 3041 *str = *str ? kasprintf(GFP_ATOMIC, "%s,%s", *str, txt) : 3042 kstrdup(txt, GFP_ATOMIC); 3043 kfree(prev); 3044 } 3045 3046 /* 3047 * Convert a transport state bitmask into a string. The caller is 3048 * responsible for freeing the returned pointer. 3049 */ 3050 static char *target_ts_to_str(u32 ts) 3051 { 3052 char *str = NULL; 3053 3054 if (ts & CMD_T_ABORTED) 3055 target_append_str(&str, "aborted"); 3056 if (ts & CMD_T_ACTIVE) 3057 target_append_str(&str, "active"); 3058 if (ts & CMD_T_COMPLETE) 3059 target_append_str(&str, "complete"); 3060 if (ts & CMD_T_SENT) 3061 target_append_str(&str, "sent"); 3062 if (ts & CMD_T_STOP) 3063 target_append_str(&str, "stop"); 3064 if (ts & CMD_T_FABRIC_STOP) 3065 target_append_str(&str, "fabric_stop"); 3066 3067 return str; 3068 } 3069 3070 static const char *target_tmf_name(enum tcm_tmreq_table tmf) 3071 { 3072 switch (tmf) { 3073 case TMR_ABORT_TASK: return "ABORT_TASK"; 3074 case TMR_ABORT_TASK_SET: return "ABORT_TASK_SET"; 3075 case TMR_CLEAR_ACA: return "CLEAR_ACA"; 3076 case TMR_CLEAR_TASK_SET: return "CLEAR_TASK_SET"; 3077 case TMR_LUN_RESET: return "LUN_RESET"; 3078 case TMR_TARGET_WARM_RESET: return "TARGET_WARM_RESET"; 3079 case TMR_TARGET_COLD_RESET: return "TARGET_COLD_RESET"; 3080 case TMR_LUN_RESET_PRO: return "LUN_RESET_PRO"; 3081 case TMR_UNKNOWN: break; 3082 } 3083 return "(?)"; 3084 } 3085 3086 void target_show_cmd(const char *pfx, struct se_cmd *cmd) 3087 { 3088 char *ts_str = target_ts_to_str(cmd->transport_state); 3089 const u8 *cdb = cmd->t_task_cdb; 3090 struct se_tmr_req *tmf = cmd->se_tmr_req; 3091 3092 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { 3093 pr_debug("%scmd %#02x:%#02x with tag %#llx dir %s i_state %d t_state %s len %d refcnt %d transport_state %s\n", 3094 pfx, cdb[0], cdb[1], cmd->tag, 3095 data_dir_name(cmd->data_direction), 3096 cmd->se_tfo->get_cmd_state(cmd), 3097 cmd_state_name(cmd->t_state), cmd->data_length, 3098 kref_read(&cmd->cmd_kref), ts_str); 3099 } else { 3100 pr_debug("%stmf %s with tag %#llx ref_task_tag %#llx i_state %d t_state %s refcnt %d transport_state %s\n", 3101 pfx, target_tmf_name(tmf->function), cmd->tag, 3102 tmf->ref_task_tag, cmd->se_tfo->get_cmd_state(cmd), 3103 cmd_state_name(cmd->t_state), 3104 kref_read(&cmd->cmd_kref), ts_str); 3105 } 3106 kfree(ts_str); 3107 } 3108 EXPORT_SYMBOL(target_show_cmd); 3109 3110 static void target_stop_session_confirm(struct percpu_ref *ref) 3111 { 3112 struct se_session *se_sess = container_of(ref, struct se_session, 3113 cmd_count); 3114 complete_all(&se_sess->stop_done); 3115 } 3116 3117 /** 3118 * target_stop_session - Stop new IO from being queued on the session. 3119 * @se_sess: session to stop 3120 */ 3121 void target_stop_session(struct se_session *se_sess) 3122 { 3123 pr_debug("Stopping session queue.\n"); 3124 if (atomic_cmpxchg(&se_sess->stopped, 0, 1) == 0) 3125 percpu_ref_kill_and_confirm(&se_sess->cmd_count, 3126 target_stop_session_confirm); 3127 } 3128 EXPORT_SYMBOL(target_stop_session); 3129 3130 /** 3131 * target_wait_for_sess_cmds - Wait for outstanding commands 3132 * @se_sess: session to wait for active I/O 3133 */ 3134 void target_wait_for_sess_cmds(struct se_session *se_sess) 3135 { 3136 int ret; 3137 3138 WARN_ON_ONCE(!atomic_read(&se_sess->stopped)); 3139 3140 do { 3141 pr_debug("Waiting for running cmds to complete.\n"); 3142 ret = wait_event_timeout(se_sess->cmd_count_wq, 3143 percpu_ref_is_zero(&se_sess->cmd_count), 3144 180 * HZ); 3145 } while (ret <= 0); 3146 3147 wait_for_completion(&se_sess->stop_done); 3148 pr_debug("Waiting for cmds done.\n"); 3149 } 3150 EXPORT_SYMBOL(target_wait_for_sess_cmds); 3151 3152 /* 3153 * Prevent that new percpu_ref_tryget_live() calls succeed and wait until 3154 * all references to the LUN have been released. Called during LUN shutdown. 3155 */ 3156 void transport_clear_lun_ref(struct se_lun *lun) 3157 { 3158 percpu_ref_kill(&lun->lun_ref); 3159 wait_for_completion(&lun->lun_shutdown_comp); 3160 } 3161 3162 static bool 3163 __transport_wait_for_tasks(struct se_cmd *cmd, bool fabric_stop, 3164 bool *aborted, bool *tas, unsigned long *flags) 3165 __releases(&cmd->t_state_lock) 3166 __acquires(&cmd->t_state_lock) 3167 { 3168 lockdep_assert_held(&cmd->t_state_lock); 3169 3170 if (fabric_stop) 3171 cmd->transport_state |= CMD_T_FABRIC_STOP; 3172 3173 if (cmd->transport_state & CMD_T_ABORTED) 3174 *aborted = true; 3175 3176 if (cmd->transport_state & CMD_T_TAS) 3177 *tas = true; 3178 3179 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && 3180 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) 3181 return false; 3182 3183 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && 3184 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) 3185 return false; 3186 3187 if (!(cmd->transport_state & CMD_T_ACTIVE)) 3188 return false; 3189 3190 if (fabric_stop && *aborted) 3191 return false; 3192 3193 cmd->transport_state |= CMD_T_STOP; 3194 3195 target_show_cmd("wait_for_tasks: Stopping ", cmd); 3196 3197 spin_unlock_irqrestore(&cmd->t_state_lock, *flags); 3198 3199 while (!wait_for_completion_timeout(&cmd->t_transport_stop_comp, 3200 180 * HZ)) 3201 target_show_cmd("wait for tasks: ", cmd); 3202 3203 spin_lock_irqsave(&cmd->t_state_lock, *flags); 3204 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP); 3205 3206 pr_debug("wait_for_tasks: Stopped wait_for_completion(&cmd->" 3207 "t_transport_stop_comp) for ITT: 0x%08llx\n", cmd->tag); 3208 3209 return true; 3210 } 3211 3212 /** 3213 * transport_wait_for_tasks - set CMD_T_STOP and wait for t_transport_stop_comp 3214 * @cmd: command to wait on 3215 */ 3216 bool transport_wait_for_tasks(struct se_cmd *cmd) 3217 { 3218 unsigned long flags; 3219 bool ret, aborted = false, tas = false; 3220 3221 spin_lock_irqsave(&cmd->t_state_lock, flags); 3222 ret = __transport_wait_for_tasks(cmd, false, &aborted, &tas, &flags); 3223 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3224 3225 return ret; 3226 } 3227 EXPORT_SYMBOL(transport_wait_for_tasks); 3228 3229 struct sense_detail { 3230 u8 key; 3231 u8 asc; 3232 u8 ascq; 3233 bool add_sense_info; 3234 }; 3235 3236 static const struct sense_detail sense_detail_table[] = { 3237 [TCM_NO_SENSE] = { 3238 .key = NOT_READY 3239 }, 3240 [TCM_NON_EXISTENT_LUN] = { 3241 .key = ILLEGAL_REQUEST, 3242 .asc = 0x25 /* LOGICAL UNIT NOT SUPPORTED */ 3243 }, 3244 [TCM_UNSUPPORTED_SCSI_OPCODE] = { 3245 .key = ILLEGAL_REQUEST, 3246 .asc = 0x20, /* INVALID COMMAND OPERATION CODE */ 3247 }, 3248 [TCM_SECTOR_COUNT_TOO_MANY] = { 3249 .key = ILLEGAL_REQUEST, 3250 .asc = 0x20, /* INVALID COMMAND OPERATION CODE */ 3251 }, 3252 [TCM_UNKNOWN_MODE_PAGE] = { 3253 .key = ILLEGAL_REQUEST, 3254 .asc = 0x24, /* INVALID FIELD IN CDB */ 3255 }, 3256 [TCM_CHECK_CONDITION_ABORT_CMD] = { 3257 .key = ABORTED_COMMAND, 3258 .asc = 0x29, /* BUS DEVICE RESET FUNCTION OCCURRED */ 3259 .ascq = 0x03, 3260 }, 3261 [TCM_INCORRECT_AMOUNT_OF_DATA] = { 3262 .key = ABORTED_COMMAND, 3263 .asc = 0x0c, /* WRITE ERROR */ 3264 .ascq = 0x0d, /* NOT ENOUGH UNSOLICITED DATA */ 3265 }, 3266 [TCM_INVALID_CDB_FIELD] = { 3267 .key = ILLEGAL_REQUEST, 3268 .asc = 0x24, /* INVALID FIELD IN CDB */ 3269 }, 3270 [TCM_INVALID_PARAMETER_LIST] = { 3271 .key = ILLEGAL_REQUEST, 3272 .asc = 0x26, /* INVALID FIELD IN PARAMETER LIST */ 3273 }, 3274 [TCM_TOO_MANY_TARGET_DESCS] = { 3275 .key = ILLEGAL_REQUEST, 3276 .asc = 0x26, 3277 .ascq = 0x06, /* TOO MANY TARGET DESCRIPTORS */ 3278 }, 3279 [TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE] = { 3280 .key = ILLEGAL_REQUEST, 3281 .asc = 0x26, 3282 .ascq = 0x07, /* UNSUPPORTED TARGET DESCRIPTOR TYPE CODE */ 3283 }, 3284 [TCM_TOO_MANY_SEGMENT_DESCS] = { 3285 .key = ILLEGAL_REQUEST, 3286 .asc = 0x26, 3287 .ascq = 0x08, /* TOO MANY SEGMENT DESCRIPTORS */ 3288 }, 3289 [TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE] = { 3290 .key = ILLEGAL_REQUEST, 3291 .asc = 0x26, 3292 .ascq = 0x09, /* UNSUPPORTED SEGMENT DESCRIPTOR TYPE CODE */ 3293 }, 3294 [TCM_PARAMETER_LIST_LENGTH_ERROR] = { 3295 .key = ILLEGAL_REQUEST, 3296 .asc = 0x1a, /* PARAMETER LIST LENGTH ERROR */ 3297 }, 3298 [TCM_UNEXPECTED_UNSOLICITED_DATA] = { 3299 .key = ILLEGAL_REQUEST, 3300 .asc = 0x0c, /* WRITE ERROR */ 3301 .ascq = 0x0c, /* UNEXPECTED_UNSOLICITED_DATA */ 3302 }, 3303 [TCM_SERVICE_CRC_ERROR] = { 3304 .key = ABORTED_COMMAND, 3305 .asc = 0x47, /* PROTOCOL SERVICE CRC ERROR */ 3306 .ascq = 0x05, /* N/A */ 3307 }, 3308 [TCM_SNACK_REJECTED] = { 3309 .key = ABORTED_COMMAND, 3310 .asc = 0x11, /* READ ERROR */ 3311 .ascq = 0x13, /* FAILED RETRANSMISSION REQUEST */ 3312 }, 3313 [TCM_WRITE_PROTECTED] = { 3314 .key = DATA_PROTECT, 3315 .asc = 0x27, /* WRITE PROTECTED */ 3316 }, 3317 [TCM_ADDRESS_OUT_OF_RANGE] = { 3318 .key = ILLEGAL_REQUEST, 3319 .asc = 0x21, /* LOGICAL BLOCK ADDRESS OUT OF RANGE */ 3320 }, 3321 [TCM_CHECK_CONDITION_UNIT_ATTENTION] = { 3322 .key = UNIT_ATTENTION, 3323 }, 3324 [TCM_MISCOMPARE_VERIFY] = { 3325 .key = MISCOMPARE, 3326 .asc = 0x1d, /* MISCOMPARE DURING VERIFY OPERATION */ 3327 .ascq = 0x00, 3328 .add_sense_info = true, 3329 }, 3330 [TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED] = { 3331 .key = ABORTED_COMMAND, 3332 .asc = 0x10, 3333 .ascq = 0x01, /* LOGICAL BLOCK GUARD CHECK FAILED */ 3334 .add_sense_info = true, 3335 }, 3336 [TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED] = { 3337 .key = ABORTED_COMMAND, 3338 .asc = 0x10, 3339 .ascq = 0x02, /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */ 3340 .add_sense_info = true, 3341 }, 3342 [TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED] = { 3343 .key = ABORTED_COMMAND, 3344 .asc = 0x10, 3345 .ascq = 0x03, /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */ 3346 .add_sense_info = true, 3347 }, 3348 [TCM_COPY_TARGET_DEVICE_NOT_REACHABLE] = { 3349 .key = COPY_ABORTED, 3350 .asc = 0x0d, 3351 .ascq = 0x02, /* COPY TARGET DEVICE NOT REACHABLE */ 3352 3353 }, 3354 [TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE] = { 3355 /* 3356 * Returning ILLEGAL REQUEST would cause immediate IO errors on 3357 * Solaris initiators. Returning NOT READY instead means the 3358 * operations will be retried a finite number of times and we 3359 * can survive intermittent errors. 3360 */ 3361 .key = NOT_READY, 3362 .asc = 0x08, /* LOGICAL UNIT COMMUNICATION FAILURE */ 3363 }, 3364 [TCM_INSUFFICIENT_REGISTRATION_RESOURCES] = { 3365 /* 3366 * From spc4r22 section5.7.7,5.7.8 3367 * If a PERSISTENT RESERVE OUT command with a REGISTER service action 3368 * or a REGISTER AND IGNORE EXISTING KEY service action or 3369 * REGISTER AND MOVE service actionis attempted, 3370 * but there are insufficient device server resources to complete the 3371 * operation, then the command shall be terminated with CHECK CONDITION 3372 * status, with the sense key set to ILLEGAL REQUEST,and the additonal 3373 * sense code set to INSUFFICIENT REGISTRATION RESOURCES. 3374 */ 3375 .key = ILLEGAL_REQUEST, 3376 .asc = 0x55, 3377 .ascq = 0x04, /* INSUFFICIENT REGISTRATION RESOURCES */ 3378 }, 3379 [TCM_INVALID_FIELD_IN_COMMAND_IU] = { 3380 .key = ILLEGAL_REQUEST, 3381 .asc = 0x0e, 3382 .ascq = 0x03, /* INVALID FIELD IN COMMAND INFORMATION UNIT */ 3383 }, 3384 [TCM_ALUA_TG_PT_STANDBY] = { 3385 .key = NOT_READY, 3386 .asc = 0x04, 3387 .ascq = ASCQ_04H_ALUA_TG_PT_STANDBY, 3388 }, 3389 [TCM_ALUA_TG_PT_UNAVAILABLE] = { 3390 .key = NOT_READY, 3391 .asc = 0x04, 3392 .ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE, 3393 }, 3394 [TCM_ALUA_STATE_TRANSITION] = { 3395 .key = NOT_READY, 3396 .asc = 0x04, 3397 .ascq = ASCQ_04H_ALUA_STATE_TRANSITION, 3398 }, 3399 [TCM_ALUA_OFFLINE] = { 3400 .key = NOT_READY, 3401 .asc = 0x04, 3402 .ascq = ASCQ_04H_ALUA_OFFLINE, 3403 }, 3404 }; 3405 3406 /** 3407 * translate_sense_reason - translate a sense reason into T10 key, asc and ascq 3408 * @cmd: SCSI command in which the resulting sense buffer or SCSI status will 3409 * be stored. 3410 * @reason: LIO sense reason code. If this argument has the value 3411 * TCM_CHECK_CONDITION_UNIT_ATTENTION, try to dequeue a unit attention. If 3412 * dequeuing a unit attention fails due to multiple commands being processed 3413 * concurrently, set the command status to BUSY. 3414 * 3415 * Return: 0 upon success or -EINVAL if the sense buffer is too small. 3416 */ 3417 static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason) 3418 { 3419 const struct sense_detail *sd; 3420 u8 *buffer = cmd->sense_buffer; 3421 int r = (__force int)reason; 3422 u8 key, asc, ascq; 3423 bool desc_format = target_sense_desc_format(cmd->se_dev); 3424 3425 if (r < ARRAY_SIZE(sense_detail_table) && sense_detail_table[r].key) 3426 sd = &sense_detail_table[r]; 3427 else 3428 sd = &sense_detail_table[(__force int) 3429 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE]; 3430 3431 key = sd->key; 3432 if (reason == TCM_CHECK_CONDITION_UNIT_ATTENTION) { 3433 if (!core_scsi3_ua_for_check_condition(cmd, &key, &asc, 3434 &ascq)) { 3435 cmd->scsi_status = SAM_STAT_BUSY; 3436 return; 3437 } 3438 } else { 3439 WARN_ON_ONCE(sd->asc == 0); 3440 asc = sd->asc; 3441 ascq = sd->ascq; 3442 } 3443 3444 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE; 3445 cmd->scsi_status = SAM_STAT_CHECK_CONDITION; 3446 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; 3447 scsi_build_sense_buffer(desc_format, buffer, key, asc, ascq); 3448 if (sd->add_sense_info) 3449 WARN_ON_ONCE(scsi_set_sense_information(buffer, 3450 cmd->scsi_sense_length, 3451 cmd->sense_info) < 0); 3452 } 3453 3454 int 3455 transport_send_check_condition_and_sense(struct se_cmd *cmd, 3456 sense_reason_t reason, int from_transport) 3457 { 3458 unsigned long flags; 3459 3460 WARN_ON_ONCE(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB); 3461 3462 spin_lock_irqsave(&cmd->t_state_lock, flags); 3463 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { 3464 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3465 return 0; 3466 } 3467 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION; 3468 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3469 3470 if (!from_transport) 3471 translate_sense_reason(cmd, reason); 3472 3473 trace_target_cmd_complete(cmd); 3474 return cmd->se_tfo->queue_status(cmd); 3475 } 3476 EXPORT_SYMBOL(transport_send_check_condition_and_sense); 3477 3478 /** 3479 * target_send_busy - Send SCSI BUSY status back to the initiator 3480 * @cmd: SCSI command for which to send a BUSY reply. 3481 * 3482 * Note: Only call this function if target_submit_cmd*() failed. 3483 */ 3484 int target_send_busy(struct se_cmd *cmd) 3485 { 3486 WARN_ON_ONCE(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB); 3487 3488 cmd->scsi_status = SAM_STAT_BUSY; 3489 trace_target_cmd_complete(cmd); 3490 return cmd->se_tfo->queue_status(cmd); 3491 } 3492 EXPORT_SYMBOL(target_send_busy); 3493 3494 static void target_tmr_work(struct work_struct *work) 3495 { 3496 struct se_cmd *cmd = container_of(work, struct se_cmd, work); 3497 struct se_device *dev = cmd->se_dev; 3498 struct se_tmr_req *tmr = cmd->se_tmr_req; 3499 int ret; 3500 3501 if (cmd->transport_state & CMD_T_ABORTED) 3502 goto aborted; 3503 3504 switch (tmr->function) { 3505 case TMR_ABORT_TASK: 3506 core_tmr_abort_task(dev, tmr, cmd->se_sess); 3507 break; 3508 case TMR_ABORT_TASK_SET: 3509 case TMR_CLEAR_ACA: 3510 case TMR_CLEAR_TASK_SET: 3511 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED; 3512 break; 3513 case TMR_LUN_RESET: 3514 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL); 3515 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE : 3516 TMR_FUNCTION_REJECTED; 3517 if (tmr->response == TMR_FUNCTION_COMPLETE) { 3518 target_ua_allocate_lun(cmd->se_sess->se_node_acl, 3519 cmd->orig_fe_lun, 0x29, 3520 ASCQ_29H_BUS_DEVICE_RESET_FUNCTION_OCCURRED); 3521 } 3522 break; 3523 case TMR_TARGET_WARM_RESET: 3524 tmr->response = TMR_FUNCTION_REJECTED; 3525 break; 3526 case TMR_TARGET_COLD_RESET: 3527 tmr->response = TMR_FUNCTION_REJECTED; 3528 break; 3529 default: 3530 pr_err("Unknown TMR function: 0x%02x.\n", 3531 tmr->function); 3532 tmr->response = TMR_FUNCTION_REJECTED; 3533 break; 3534 } 3535 3536 if (cmd->transport_state & CMD_T_ABORTED) 3537 goto aborted; 3538 3539 cmd->se_tfo->queue_tm_rsp(cmd); 3540 3541 transport_lun_remove_cmd(cmd); 3542 transport_cmd_check_stop_to_fabric(cmd); 3543 return; 3544 3545 aborted: 3546 target_handle_abort(cmd); 3547 } 3548 3549 int transport_generic_handle_tmr( 3550 struct se_cmd *cmd) 3551 { 3552 unsigned long flags; 3553 bool aborted = false; 3554 3555 spin_lock_irqsave(&cmd->t_state_lock, flags); 3556 if (cmd->transport_state & CMD_T_ABORTED) { 3557 aborted = true; 3558 } else { 3559 cmd->t_state = TRANSPORT_ISTATE_PROCESSING; 3560 cmd->transport_state |= CMD_T_ACTIVE; 3561 } 3562 spin_unlock_irqrestore(&cmd->t_state_lock, flags); 3563 3564 if (aborted) { 3565 pr_warn_ratelimited("handle_tmr caught CMD_T_ABORTED TMR %d ref_tag: %llu tag: %llu\n", 3566 cmd->se_tmr_req->function, 3567 cmd->se_tmr_req->ref_task_tag, cmd->tag); 3568 target_handle_abort(cmd); 3569 return 0; 3570 } 3571 3572 INIT_WORK(&cmd->work, target_tmr_work); 3573 schedule_work(&cmd->work); 3574 return 0; 3575 } 3576 EXPORT_SYMBOL(transport_generic_handle_tmr); 3577 3578 bool 3579 target_check_wce(struct se_device *dev) 3580 { 3581 bool wce = false; 3582 3583 if (dev->transport->get_write_cache) 3584 wce = dev->transport->get_write_cache(dev); 3585 else if (dev->dev_attrib.emulate_write_cache > 0) 3586 wce = true; 3587 3588 return wce; 3589 } 3590 3591 bool 3592 target_check_fua(struct se_device *dev) 3593 { 3594 return target_check_wce(dev) && dev->dev_attrib.emulate_fua_write > 0; 3595 } 3596