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