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