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