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