1 /*******************************************************************************
2 * Filename: target_core_transport.c
4 * This file contains the Generic Target Engine Core.
6 * (c) Copyright 2002-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/net.h>
27 #include <linux/delay.h>
28 #include <linux/string.h>
29 #include <linux/timer.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/kthread.h>
34 #include <linux/cdrom.h>
35 #include <linux/module.h>
36 #include <linux/ratelimit.h>
37 #include <asm/unaligned.h>
40 #include <scsi/scsi_proto.h>
42 #include <target/target_core_base.h>
43 #include <target/target_core_backend.h>
44 #include <target/target_core_fabric.h>
45 #include <target/target_core_configfs.h>
47 #include "target_core_internal.h"
48 #include "target_core_alua.h"
49 #include "target_core_pr.h"
50 #include "target_core_ua.h"
52 #define CREATE_TRACE_POINTS
53 #include <trace/events/target.h>
55 static struct workqueue_struct
*target_completion_wq
;
56 static struct kmem_cache
*se_sess_cache
;
57 struct kmem_cache
*se_ua_cache
;
58 struct kmem_cache
*t10_pr_reg_cache
;
59 struct kmem_cache
*t10_alua_lu_gp_cache
;
60 struct kmem_cache
*t10_alua_lu_gp_mem_cache
;
61 struct kmem_cache
*t10_alua_tg_pt_gp_cache
;
62 struct kmem_cache
*t10_alua_tg_pt_gp_mem_cache
;
63 struct kmem_cache
*t10_alua_lba_map_cache
;
64 struct kmem_cache
*t10_alua_lba_map_mem_cache
;
66 static void transport_complete_task_attr(struct se_cmd
*cmd
);
67 static void transport_handle_queue_full(struct se_cmd
*cmd
,
68 struct se_device
*dev
);
69 static int transport_put_cmd(struct se_cmd
*cmd
);
70 static void target_complete_ok_work(struct work_struct
*work
);
72 int init_se_kmem_caches(void)
74 se_sess_cache
= kmem_cache_create("se_sess_cache",
75 sizeof(struct se_session
), __alignof__(struct se_session
),
78 pr_err("kmem_cache_create() for struct se_session"
82 se_ua_cache
= kmem_cache_create("se_ua_cache",
83 sizeof(struct se_ua
), __alignof__(struct se_ua
),
86 pr_err("kmem_cache_create() for struct se_ua failed\n");
87 goto out_free_sess_cache
;
89 t10_pr_reg_cache
= kmem_cache_create("t10_pr_reg_cache",
90 sizeof(struct t10_pr_registration
),
91 __alignof__(struct t10_pr_registration
), 0, NULL
);
92 if (!t10_pr_reg_cache
) {
93 pr_err("kmem_cache_create() for struct t10_pr_registration"
95 goto out_free_ua_cache
;
97 t10_alua_lu_gp_cache
= kmem_cache_create("t10_alua_lu_gp_cache",
98 sizeof(struct t10_alua_lu_gp
), __alignof__(struct t10_alua_lu_gp
),
100 if (!t10_alua_lu_gp_cache
) {
101 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
103 goto out_free_pr_reg_cache
;
105 t10_alua_lu_gp_mem_cache
= kmem_cache_create("t10_alua_lu_gp_mem_cache",
106 sizeof(struct t10_alua_lu_gp_member
),
107 __alignof__(struct t10_alua_lu_gp_member
), 0, NULL
);
108 if (!t10_alua_lu_gp_mem_cache
) {
109 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
111 goto out_free_lu_gp_cache
;
113 t10_alua_tg_pt_gp_cache
= kmem_cache_create("t10_alua_tg_pt_gp_cache",
114 sizeof(struct t10_alua_tg_pt_gp
),
115 __alignof__(struct t10_alua_tg_pt_gp
), 0, NULL
);
116 if (!t10_alua_tg_pt_gp_cache
) {
117 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
119 goto out_free_lu_gp_mem_cache
;
121 t10_alua_tg_pt_gp_mem_cache
= kmem_cache_create(
122 "t10_alua_tg_pt_gp_mem_cache",
123 sizeof(struct t10_alua_tg_pt_gp_member
),
124 __alignof__(struct t10_alua_tg_pt_gp_member
),
126 if (!t10_alua_tg_pt_gp_mem_cache
) {
127 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
129 goto out_free_tg_pt_gp_cache
;
131 t10_alua_lba_map_cache
= kmem_cache_create(
132 "t10_alua_lba_map_cache",
133 sizeof(struct t10_alua_lba_map
),
134 __alignof__(struct t10_alua_lba_map
), 0, NULL
);
135 if (!t10_alua_lba_map_cache
) {
136 pr_err("kmem_cache_create() for t10_alua_lba_map_"
138 goto out_free_tg_pt_gp_mem_cache
;
140 t10_alua_lba_map_mem_cache
= kmem_cache_create(
141 "t10_alua_lba_map_mem_cache",
142 sizeof(struct t10_alua_lba_map_member
),
143 __alignof__(struct t10_alua_lba_map_member
), 0, NULL
);
144 if (!t10_alua_lba_map_mem_cache
) {
145 pr_err("kmem_cache_create() for t10_alua_lba_map_mem_"
147 goto out_free_lba_map_cache
;
150 target_completion_wq
= alloc_workqueue("target_completion",
152 if (!target_completion_wq
)
153 goto out_free_lba_map_mem_cache
;
157 out_free_lba_map_mem_cache
:
158 kmem_cache_destroy(t10_alua_lba_map_mem_cache
);
159 out_free_lba_map_cache
:
160 kmem_cache_destroy(t10_alua_lba_map_cache
);
161 out_free_tg_pt_gp_mem_cache
:
162 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
163 out_free_tg_pt_gp_cache
:
164 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
165 out_free_lu_gp_mem_cache
:
166 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
167 out_free_lu_gp_cache
:
168 kmem_cache_destroy(t10_alua_lu_gp_cache
);
169 out_free_pr_reg_cache
:
170 kmem_cache_destroy(t10_pr_reg_cache
);
172 kmem_cache_destroy(se_ua_cache
);
174 kmem_cache_destroy(se_sess_cache
);
179 void release_se_kmem_caches(void)
181 destroy_workqueue(target_completion_wq
);
182 kmem_cache_destroy(se_sess_cache
);
183 kmem_cache_destroy(se_ua_cache
);
184 kmem_cache_destroy(t10_pr_reg_cache
);
185 kmem_cache_destroy(t10_alua_lu_gp_cache
);
186 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
187 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
188 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
189 kmem_cache_destroy(t10_alua_lba_map_cache
);
190 kmem_cache_destroy(t10_alua_lba_map_mem_cache
);
193 /* This code ensures unique mib indexes are handed out. */
194 static DEFINE_SPINLOCK(scsi_mib_index_lock
);
195 static u32 scsi_mib_index
[SCSI_INDEX_TYPE_MAX
];
198 * Allocate a new row index for the entry type specified
200 u32
scsi_get_new_index(scsi_index_t type
)
204 BUG_ON((type
< 0) || (type
>= SCSI_INDEX_TYPE_MAX
));
206 spin_lock(&scsi_mib_index_lock
);
207 new_index
= ++scsi_mib_index
[type
];
208 spin_unlock(&scsi_mib_index_lock
);
213 void transport_subsystem_check_init(void)
216 static int sub_api_initialized
;
218 if (sub_api_initialized
)
221 ret
= request_module("target_core_iblock");
223 pr_err("Unable to load target_core_iblock\n");
225 ret
= request_module("target_core_file");
227 pr_err("Unable to load target_core_file\n");
229 ret
= request_module("target_core_pscsi");
231 pr_err("Unable to load target_core_pscsi\n");
233 ret
= request_module("target_core_user");
235 pr_err("Unable to load target_core_user\n");
237 sub_api_initialized
= 1;
240 struct se_session
*transport_init_session(enum target_prot_op sup_prot_ops
)
242 struct se_session
*se_sess
;
244 se_sess
= kmem_cache_zalloc(se_sess_cache
, GFP_KERNEL
);
246 pr_err("Unable to allocate struct se_session from"
248 return ERR_PTR(-ENOMEM
);
250 INIT_LIST_HEAD(&se_sess
->sess_list
);
251 INIT_LIST_HEAD(&se_sess
->sess_acl_list
);
252 INIT_LIST_HEAD(&se_sess
->sess_cmd_list
);
253 INIT_LIST_HEAD(&se_sess
->sess_wait_list
);
254 spin_lock_init(&se_sess
->sess_cmd_lock
);
255 kref_init(&se_sess
->sess_kref
);
256 se_sess
->sup_prot_ops
= sup_prot_ops
;
260 EXPORT_SYMBOL(transport_init_session
);
262 int transport_alloc_session_tags(struct se_session
*se_sess
,
263 unsigned int tag_num
, unsigned int tag_size
)
267 se_sess
->sess_cmd_map
= kzalloc(tag_num
* tag_size
,
268 GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
269 if (!se_sess
->sess_cmd_map
) {
270 se_sess
->sess_cmd_map
= vzalloc(tag_num
* tag_size
);
271 if (!se_sess
->sess_cmd_map
) {
272 pr_err("Unable to allocate se_sess->sess_cmd_map\n");
277 rc
= percpu_ida_init(&se_sess
->sess_tag_pool
, tag_num
);
279 pr_err("Unable to init se_sess->sess_tag_pool,"
280 " tag_num: %u\n", tag_num
);
281 if (is_vmalloc_addr(se_sess
->sess_cmd_map
))
282 vfree(se_sess
->sess_cmd_map
);
284 kfree(se_sess
->sess_cmd_map
);
285 se_sess
->sess_cmd_map
= NULL
;
291 EXPORT_SYMBOL(transport_alloc_session_tags
);
293 struct se_session
*transport_init_session_tags(unsigned int tag_num
,
294 unsigned int tag_size
,
295 enum target_prot_op sup_prot_ops
)
297 struct se_session
*se_sess
;
300 se_sess
= transport_init_session(sup_prot_ops
);
304 rc
= transport_alloc_session_tags(se_sess
, tag_num
, tag_size
);
306 transport_free_session(se_sess
);
307 return ERR_PTR(-ENOMEM
);
312 EXPORT_SYMBOL(transport_init_session_tags
);
315 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
317 void __transport_register_session(
318 struct se_portal_group
*se_tpg
,
319 struct se_node_acl
*se_nacl
,
320 struct se_session
*se_sess
,
321 void *fabric_sess_ptr
)
323 const struct target_core_fabric_ops
*tfo
= se_tpg
->se_tpg_tfo
;
324 unsigned char buf
[PR_REG_ISID_LEN
];
326 se_sess
->se_tpg
= se_tpg
;
327 se_sess
->fabric_sess_ptr
= fabric_sess_ptr
;
329 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
331 * Only set for struct se_session's that will actually be moving I/O.
332 * eg: *NOT* discovery sessions.
337 * Determine if fabric allows for T10-PI feature bits exposed to
338 * initiators for device backends with !dev->dev_attrib.pi_prot_type.
340 * If so, then always save prot_type on a per se_node_acl node
341 * basis and re-instate the previous sess_prot_type to avoid
342 * disabling PI from below any previously initiator side
345 if (se_nacl
->saved_prot_type
)
346 se_sess
->sess_prot_type
= se_nacl
->saved_prot_type
;
347 else if (tfo
->tpg_check_prot_fabric_only
)
348 se_sess
->sess_prot_type
= se_nacl
->saved_prot_type
=
349 tfo
->tpg_check_prot_fabric_only(se_tpg
);
351 * If the fabric module supports an ISID based TransportID,
352 * save this value in binary from the fabric I_T Nexus now.
354 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
355 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
356 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
,
357 &buf
[0], PR_REG_ISID_LEN
);
358 se_sess
->sess_bin_isid
= get_unaligned_be64(&buf
[0]);
360 kref_get(&se_nacl
->acl_kref
);
362 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
364 * The se_nacl->nacl_sess pointer will be set to the
365 * last active I_T Nexus for each struct se_node_acl.
367 se_nacl
->nacl_sess
= se_sess
;
369 list_add_tail(&se_sess
->sess_acl_list
,
370 &se_nacl
->acl_sess_list
);
371 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
373 list_add_tail(&se_sess
->sess_list
, &se_tpg
->tpg_sess_list
);
375 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
376 se_tpg
->se_tpg_tfo
->get_fabric_name(), se_sess
->fabric_sess_ptr
);
378 EXPORT_SYMBOL(__transport_register_session
);
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
)
388 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
389 __transport_register_session(se_tpg
, se_nacl
, se_sess
, fabric_sess_ptr
);
390 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
392 EXPORT_SYMBOL(transport_register_session
);
394 static void target_release_session(struct kref
*kref
)
396 struct se_session
*se_sess
= container_of(kref
,
397 struct se_session
, sess_kref
);
398 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
400 se_tpg
->se_tpg_tfo
->close_session(se_sess
);
403 void target_get_session(struct se_session
*se_sess
)
405 kref_get(&se_sess
->sess_kref
);
407 EXPORT_SYMBOL(target_get_session
);
409 void target_put_session(struct se_session
*se_sess
)
411 struct se_portal_group
*tpg
= se_sess
->se_tpg
;
413 if (tpg
->se_tpg_tfo
->put_session
!= NULL
) {
414 tpg
->se_tpg_tfo
->put_session(se_sess
);
417 kref_put(&se_sess
->sess_kref
, target_release_session
);
419 EXPORT_SYMBOL(target_put_session
);
421 ssize_t
target_show_dynamic_sessions(struct se_portal_group
*se_tpg
, char *page
)
423 struct se_session
*se_sess
;
426 spin_lock_bh(&se_tpg
->session_lock
);
427 list_for_each_entry(se_sess
, &se_tpg
->tpg_sess_list
, sess_list
) {
428 if (!se_sess
->se_node_acl
)
430 if (!se_sess
->se_node_acl
->dynamic_node_acl
)
432 if (strlen(se_sess
->se_node_acl
->initiatorname
) + 1 + len
> PAGE_SIZE
)
435 len
+= snprintf(page
+ len
, PAGE_SIZE
- len
, "%s\n",
436 se_sess
->se_node_acl
->initiatorname
);
437 len
+= 1; /* Include NULL terminator */
439 spin_unlock_bh(&se_tpg
->session_lock
);
443 EXPORT_SYMBOL(target_show_dynamic_sessions
);
445 static void target_complete_nacl(struct kref
*kref
)
447 struct se_node_acl
*nacl
= container_of(kref
,
448 struct se_node_acl
, acl_kref
);
450 complete(&nacl
->acl_free_comp
);
453 void target_put_nacl(struct se_node_acl
*nacl
)
455 kref_put(&nacl
->acl_kref
, target_complete_nacl
);
458 void transport_deregister_session_configfs(struct se_session
*se_sess
)
460 struct se_node_acl
*se_nacl
;
463 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
465 se_nacl
= se_sess
->se_node_acl
;
467 spin_lock_irqsave(&se_nacl
->nacl_sess_lock
, flags
);
468 if (se_nacl
->acl_stop
== 0)
469 list_del(&se_sess
->sess_acl_list
);
471 * If the session list is empty, then clear the pointer.
472 * Otherwise, set the struct se_session pointer from the tail
473 * element of the per struct se_node_acl active session list.
475 if (list_empty(&se_nacl
->acl_sess_list
))
476 se_nacl
->nacl_sess
= NULL
;
478 se_nacl
->nacl_sess
= container_of(
479 se_nacl
->acl_sess_list
.prev
,
480 struct se_session
, sess_acl_list
);
482 spin_unlock_irqrestore(&se_nacl
->nacl_sess_lock
, flags
);
485 EXPORT_SYMBOL(transport_deregister_session_configfs
);
487 void transport_free_session(struct se_session
*se_sess
)
489 if (se_sess
->sess_cmd_map
) {
490 percpu_ida_destroy(&se_sess
->sess_tag_pool
);
491 if (is_vmalloc_addr(se_sess
->sess_cmd_map
))
492 vfree(se_sess
->sess_cmd_map
);
494 kfree(se_sess
->sess_cmd_map
);
496 kmem_cache_free(se_sess_cache
, se_sess
);
498 EXPORT_SYMBOL(transport_free_session
);
500 void transport_deregister_session(struct se_session
*se_sess
)
502 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
503 const struct target_core_fabric_ops
*se_tfo
;
504 struct se_node_acl
*se_nacl
;
506 bool comp_nacl
= true;
509 transport_free_session(se_sess
);
512 se_tfo
= se_tpg
->se_tpg_tfo
;
514 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
515 list_del(&se_sess
->sess_list
);
516 se_sess
->se_tpg
= NULL
;
517 se_sess
->fabric_sess_ptr
= NULL
;
518 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
521 * Determine if we need to do extra work for this initiator node's
522 * struct se_node_acl if it had been previously dynamically generated.
524 se_nacl
= se_sess
->se_node_acl
;
526 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
527 if (se_nacl
&& se_nacl
->dynamic_node_acl
) {
528 if (!se_tfo
->tpg_check_demo_mode_cache(se_tpg
)) {
529 list_del(&se_nacl
->acl_list
);
530 se_tpg
->num_node_acls
--;
531 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
532 core_tpg_wait_for_nacl_pr_ref(se_nacl
);
533 core_free_device_list_for_node(se_nacl
, se_tpg
);
534 se_tfo
->tpg_release_fabric_acl(se_tpg
, se_nacl
);
537 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
540 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
542 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
543 se_tpg
->se_tpg_tfo
->get_fabric_name());
545 * If last kref is dropping now for an explicit NodeACL, awake sleeping
546 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
549 if (se_nacl
&& comp_nacl
)
550 target_put_nacl(se_nacl
);
552 transport_free_session(se_sess
);
554 EXPORT_SYMBOL(transport_deregister_session
);
557 * Called with cmd->t_state_lock held.
559 static void target_remove_from_state_list(struct se_cmd
*cmd
)
561 struct se_device
*dev
= cmd
->se_dev
;
567 if (cmd
->transport_state
& CMD_T_BUSY
)
570 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
571 if (cmd
->state_active
) {
572 list_del(&cmd
->state_list
);
573 cmd
->state_active
= false;
575 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
578 static int transport_cmd_check_stop(struct se_cmd
*cmd
, bool remove_from_lists
,
583 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
585 cmd
->t_state
= TRANSPORT_WRITE_PENDING
;
587 if (remove_from_lists
) {
588 target_remove_from_state_list(cmd
);
591 * Clear struct se_cmd->se_lun before the handoff to FE.
597 * Determine if frontend context caller is requesting the stopping of
598 * this command for frontend exceptions.
600 if (cmd
->transport_state
& CMD_T_STOP
) {
601 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
603 cmd
->se_tfo
->get_task_tag(cmd
));
605 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
607 complete_all(&cmd
->t_transport_stop_comp
);
611 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
612 if (remove_from_lists
) {
614 * Some fabric modules like tcm_loop can release
615 * their internally allocated I/O reference now and
618 * Fabric modules are expected to return '1' here if the
619 * se_cmd being passed is released at this point,
620 * or zero if not being released.
622 if (cmd
->se_tfo
->check_stop_free
!= NULL
) {
623 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
624 return cmd
->se_tfo
->check_stop_free(cmd
);
628 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
632 static int transport_cmd_check_stop_to_fabric(struct se_cmd
*cmd
)
634 return transport_cmd_check_stop(cmd
, true, false);
637 static void transport_lun_remove_cmd(struct se_cmd
*cmd
)
639 struct se_lun
*lun
= cmd
->se_lun
;
644 if (cmpxchg(&cmd
->lun_ref_active
, true, false))
645 percpu_ref_put(&lun
->lun_ref
);
648 void transport_cmd_finish_abort(struct se_cmd
*cmd
, int remove
)
650 if (cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
)
651 transport_lun_remove_cmd(cmd
);
653 * Allow the fabric driver to unmap any resources before
654 * releasing the descriptor via TFO->release_cmd()
657 cmd
->se_tfo
->aborted_task(cmd
);
659 if (transport_cmd_check_stop_to_fabric(cmd
))
662 transport_put_cmd(cmd
);
665 static void target_complete_failure_work(struct work_struct
*work
)
667 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
669 transport_generic_request_failure(cmd
,
670 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
);
674 * Used when asking transport to copy Sense Data from the underlying
675 * Linux/SCSI struct scsi_cmnd
677 static unsigned char *transport_get_sense_buffer(struct se_cmd
*cmd
)
679 struct se_device
*dev
= cmd
->se_dev
;
681 WARN_ON(!cmd
->se_lun
);
686 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
)
689 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
691 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
692 dev
->se_hba
->hba_id
, dev
->transport
->name
, cmd
->scsi_status
);
693 return cmd
->sense_buffer
;
696 void target_complete_cmd(struct se_cmd
*cmd
, u8 scsi_status
)
698 struct se_device
*dev
= cmd
->se_dev
;
699 int success
= scsi_status
== GOOD
;
702 cmd
->scsi_status
= scsi_status
;
705 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
706 cmd
->transport_state
&= ~CMD_T_BUSY
;
708 if (dev
&& dev
->transport
->transport_complete
) {
709 dev
->transport
->transport_complete(cmd
,
711 transport_get_sense_buffer(cmd
));
712 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
)
717 * See if we are waiting to complete for an exception condition.
719 if (cmd
->transport_state
& CMD_T_REQUEST_STOP
) {
720 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
721 complete(&cmd
->task_stop_comp
);
726 * Check for case where an explicit ABORT_TASK has been received
727 * and transport_wait_for_tasks() will be waiting for completion..
729 if (cmd
->transport_state
& CMD_T_ABORTED
&&
730 cmd
->transport_state
& CMD_T_STOP
) {
731 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
732 complete_all(&cmd
->t_transport_stop_comp
);
734 } else if (!success
) {
735 INIT_WORK(&cmd
->work
, target_complete_failure_work
);
737 INIT_WORK(&cmd
->work
, target_complete_ok_work
);
740 cmd
->t_state
= TRANSPORT_COMPLETE
;
741 cmd
->transport_state
|= (CMD_T_COMPLETE
| CMD_T_ACTIVE
);
742 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
744 queue_work(target_completion_wq
, &cmd
->work
);
746 EXPORT_SYMBOL(target_complete_cmd
);
748 void target_complete_cmd_with_length(struct se_cmd
*cmd
, u8 scsi_status
, int length
)
750 if (scsi_status
== SAM_STAT_GOOD
&& length
< cmd
->data_length
) {
751 if (cmd
->se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
752 cmd
->residual_count
+= cmd
->data_length
- length
;
754 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
755 cmd
->residual_count
= cmd
->data_length
- length
;
758 cmd
->data_length
= length
;
761 target_complete_cmd(cmd
, scsi_status
);
763 EXPORT_SYMBOL(target_complete_cmd_with_length
);
765 static void target_add_to_state_list(struct se_cmd
*cmd
)
767 struct se_device
*dev
= cmd
->se_dev
;
770 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
771 if (!cmd
->state_active
) {
772 list_add_tail(&cmd
->state_list
, &dev
->state_list
);
773 cmd
->state_active
= true;
775 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
779 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
781 static void transport_write_pending_qf(struct se_cmd
*cmd
);
782 static void transport_complete_qf(struct se_cmd
*cmd
);
784 void target_qf_do_work(struct work_struct
*work
)
786 struct se_device
*dev
= container_of(work
, struct se_device
,
788 LIST_HEAD(qf_cmd_list
);
789 struct se_cmd
*cmd
, *cmd_tmp
;
791 spin_lock_irq(&dev
->qf_cmd_lock
);
792 list_splice_init(&dev
->qf_cmd_list
, &qf_cmd_list
);
793 spin_unlock_irq(&dev
->qf_cmd_lock
);
795 list_for_each_entry_safe(cmd
, cmd_tmp
, &qf_cmd_list
, se_qf_node
) {
796 list_del(&cmd
->se_qf_node
);
797 atomic_dec_mb(&dev
->dev_qf_count
);
799 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
800 " context: %s\n", cmd
->se_tfo
->get_fabric_name(), cmd
,
801 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
) ? "COMPLETE_OK" :
802 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
) ? "WRITE_PENDING"
805 if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
)
806 transport_write_pending_qf(cmd
);
807 else if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
)
808 transport_complete_qf(cmd
);
812 unsigned char *transport_dump_cmd_direction(struct se_cmd
*cmd
)
814 switch (cmd
->data_direction
) {
817 case DMA_FROM_DEVICE
:
821 case DMA_BIDIRECTIONAL
:
830 void transport_dump_dev_state(
831 struct se_device
*dev
,
835 *bl
+= sprintf(b
+ *bl
, "Status: ");
836 if (dev
->export_count
)
837 *bl
+= sprintf(b
+ *bl
, "ACTIVATED");
839 *bl
+= sprintf(b
+ *bl
, "DEACTIVATED");
841 *bl
+= sprintf(b
+ *bl
, " Max Queue Depth: %d", dev
->queue_depth
);
842 *bl
+= sprintf(b
+ *bl
, " SectorSize: %u HwMaxSectors: %u\n",
843 dev
->dev_attrib
.block_size
,
844 dev
->dev_attrib
.hw_max_sectors
);
845 *bl
+= sprintf(b
+ *bl
, " ");
848 void transport_dump_vpd_proto_id(
850 unsigned char *p_buf
,
853 unsigned char buf
[VPD_TMP_BUF_SIZE
];
856 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
857 len
= sprintf(buf
, "T10 VPD Protocol Identifier: ");
859 switch (vpd
->protocol_identifier
) {
861 sprintf(buf
+len
, "Fibre Channel\n");
864 sprintf(buf
+len
, "Parallel SCSI\n");
867 sprintf(buf
+len
, "SSA\n");
870 sprintf(buf
+len
, "IEEE 1394\n");
873 sprintf(buf
+len
, "SCSI Remote Direct Memory Access"
877 sprintf(buf
+len
, "Internet SCSI (iSCSI)\n");
880 sprintf(buf
+len
, "SAS Serial SCSI Protocol\n");
883 sprintf(buf
+len
, "Automation/Drive Interface Transport"
887 sprintf(buf
+len
, "AT Attachment Interface ATA/ATAPI\n");
890 sprintf(buf
+len
, "Unknown 0x%02x\n",
891 vpd
->protocol_identifier
);
896 strncpy(p_buf
, buf
, p_buf_len
);
902 transport_set_vpd_proto_id(struct t10_vpd
*vpd
, unsigned char *page_83
)
905 * Check if the Protocol Identifier Valid (PIV) bit is set..
907 * from spc3r23.pdf section 7.5.1
909 if (page_83
[1] & 0x80) {
910 vpd
->protocol_identifier
= (page_83
[0] & 0xf0);
911 vpd
->protocol_identifier_set
= 1;
912 transport_dump_vpd_proto_id(vpd
, NULL
, 0);
915 EXPORT_SYMBOL(transport_set_vpd_proto_id
);
917 int transport_dump_vpd_assoc(
919 unsigned char *p_buf
,
922 unsigned char buf
[VPD_TMP_BUF_SIZE
];
926 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
927 len
= sprintf(buf
, "T10 VPD Identifier Association: ");
929 switch (vpd
->association
) {
931 sprintf(buf
+len
, "addressed logical unit\n");
934 sprintf(buf
+len
, "target port\n");
937 sprintf(buf
+len
, "SCSI target device\n");
940 sprintf(buf
+len
, "Unknown 0x%02x\n", vpd
->association
);
946 strncpy(p_buf
, buf
, p_buf_len
);
953 int transport_set_vpd_assoc(struct t10_vpd
*vpd
, unsigned char *page_83
)
956 * The VPD identification association..
958 * from spc3r23.pdf Section 7.6.3.1 Table 297
960 vpd
->association
= (page_83
[1] & 0x30);
961 return transport_dump_vpd_assoc(vpd
, NULL
, 0);
963 EXPORT_SYMBOL(transport_set_vpd_assoc
);
965 int transport_dump_vpd_ident_type(
967 unsigned char *p_buf
,
970 unsigned char buf
[VPD_TMP_BUF_SIZE
];
974 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
975 len
= sprintf(buf
, "T10 VPD Identifier Type: ");
977 switch (vpd
->device_identifier_type
) {
979 sprintf(buf
+len
, "Vendor specific\n");
982 sprintf(buf
+len
, "T10 Vendor ID based\n");
985 sprintf(buf
+len
, "EUI-64 based\n");
988 sprintf(buf
+len
, "NAA\n");
991 sprintf(buf
+len
, "Relative target port identifier\n");
994 sprintf(buf
+len
, "SCSI name string\n");
997 sprintf(buf
+len
, "Unsupported: 0x%02x\n",
998 vpd
->device_identifier_type
);
1004 if (p_buf_len
< strlen(buf
)+1)
1006 strncpy(p_buf
, buf
, p_buf_len
);
1008 pr_debug("%s", buf
);
1014 int transport_set_vpd_ident_type(struct t10_vpd
*vpd
, unsigned char *page_83
)
1017 * The VPD identifier type..
1019 * from spc3r23.pdf Section 7.6.3.1 Table 298
1021 vpd
->device_identifier_type
= (page_83
[1] & 0x0f);
1022 return transport_dump_vpd_ident_type(vpd
, NULL
, 0);
1024 EXPORT_SYMBOL(transport_set_vpd_ident_type
);
1026 int transport_dump_vpd_ident(
1027 struct t10_vpd
*vpd
,
1028 unsigned char *p_buf
,
1031 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1034 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1036 switch (vpd
->device_identifier_code_set
) {
1037 case 0x01: /* Binary */
1038 snprintf(buf
, sizeof(buf
),
1039 "T10 VPD Binary Device Identifier: %s\n",
1040 &vpd
->device_identifier
[0]);
1042 case 0x02: /* ASCII */
1043 snprintf(buf
, sizeof(buf
),
1044 "T10 VPD ASCII Device Identifier: %s\n",
1045 &vpd
->device_identifier
[0]);
1047 case 0x03: /* UTF-8 */
1048 snprintf(buf
, sizeof(buf
),
1049 "T10 VPD UTF-8 Device Identifier: %s\n",
1050 &vpd
->device_identifier
[0]);
1053 sprintf(buf
, "T10 VPD Device Identifier encoding unsupported:"
1054 " 0x%02x", vpd
->device_identifier_code_set
);
1060 strncpy(p_buf
, buf
, p_buf_len
);
1062 pr_debug("%s", buf
);
1068 transport_set_vpd_ident(struct t10_vpd
*vpd
, unsigned char *page_83
)
1070 static const char hex_str
[] = "0123456789abcdef";
1071 int j
= 0, i
= 4; /* offset to start of the identifier */
1074 * The VPD Code Set (encoding)
1076 * from spc3r23.pdf Section 7.6.3.1 Table 296
1078 vpd
->device_identifier_code_set
= (page_83
[0] & 0x0f);
1079 switch (vpd
->device_identifier_code_set
) {
1080 case 0x01: /* Binary */
1081 vpd
->device_identifier
[j
++] =
1082 hex_str
[vpd
->device_identifier_type
];
1083 while (i
< (4 + page_83
[3])) {
1084 vpd
->device_identifier
[j
++] =
1085 hex_str
[(page_83
[i
] & 0xf0) >> 4];
1086 vpd
->device_identifier
[j
++] =
1087 hex_str
[page_83
[i
] & 0x0f];
1091 case 0x02: /* ASCII */
1092 case 0x03: /* UTF-8 */
1093 while (i
< (4 + page_83
[3]))
1094 vpd
->device_identifier
[j
++] = page_83
[i
++];
1100 return transport_dump_vpd_ident(vpd
, NULL
, 0);
1102 EXPORT_SYMBOL(transport_set_vpd_ident
);
1105 target_cmd_size_check(struct se_cmd
*cmd
, unsigned int size
)
1107 struct se_device
*dev
= cmd
->se_dev
;
1109 if (cmd
->unknown_data_length
) {
1110 cmd
->data_length
= size
;
1111 } else if (size
!= cmd
->data_length
) {
1112 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1113 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1114 " 0x%02x\n", cmd
->se_tfo
->get_fabric_name(),
1115 cmd
->data_length
, size
, cmd
->t_task_cdb
[0]);
1117 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
1118 pr_err("Rejecting underflow/overflow"
1120 return TCM_INVALID_CDB_FIELD
;
1123 * Reject READ_* or WRITE_* with overflow/underflow for
1124 * type SCF_SCSI_DATA_CDB.
1126 if (dev
->dev_attrib
.block_size
!= 512) {
1127 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1128 " CDB on non 512-byte sector setup subsystem"
1129 " plugin: %s\n", dev
->transport
->name
);
1130 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1131 return TCM_INVALID_CDB_FIELD
;
1134 * For the overflow case keep the existing fabric provided
1135 * ->data_length. Otherwise for the underflow case, reset
1136 * ->data_length to the smaller SCSI expected data transfer
1139 if (size
> cmd
->data_length
) {
1140 cmd
->se_cmd_flags
|= SCF_OVERFLOW_BIT
;
1141 cmd
->residual_count
= (size
- cmd
->data_length
);
1143 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
1144 cmd
->residual_count
= (cmd
->data_length
- size
);
1145 cmd
->data_length
= size
;
1154 * Used by fabric modules containing a local struct se_cmd within their
1155 * fabric dependent per I/O descriptor.
1157 void transport_init_se_cmd(
1159 const struct target_core_fabric_ops
*tfo
,
1160 struct se_session
*se_sess
,
1164 unsigned char *sense_buffer
)
1166 INIT_LIST_HEAD(&cmd
->se_delayed_node
);
1167 INIT_LIST_HEAD(&cmd
->se_qf_node
);
1168 INIT_LIST_HEAD(&cmd
->se_cmd_list
);
1169 INIT_LIST_HEAD(&cmd
->state_list
);
1170 init_completion(&cmd
->t_transport_stop_comp
);
1171 init_completion(&cmd
->cmd_wait_comp
);
1172 init_completion(&cmd
->task_stop_comp
);
1173 spin_lock_init(&cmd
->t_state_lock
);
1174 kref_init(&cmd
->cmd_kref
);
1175 cmd
->transport_state
= CMD_T_DEV_ACTIVE
;
1178 cmd
->se_sess
= se_sess
;
1179 cmd
->data_length
= data_length
;
1180 cmd
->data_direction
= data_direction
;
1181 cmd
->sam_task_attr
= task_attr
;
1182 cmd
->sense_buffer
= sense_buffer
;
1184 cmd
->state_active
= false;
1186 EXPORT_SYMBOL(transport_init_se_cmd
);
1188 static sense_reason_t
1189 transport_check_alloc_task_attr(struct se_cmd
*cmd
)
1191 struct se_device
*dev
= cmd
->se_dev
;
1194 * Check if SAM Task Attribute emulation is enabled for this
1195 * struct se_device storage object
1197 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1200 if (cmd
->sam_task_attr
== TCM_ACA_TAG
) {
1201 pr_debug("SAM Task Attribute ACA"
1202 " emulation is not supported\n");
1203 return TCM_INVALID_CDB_FIELD
;
1206 * Used to determine when ORDERED commands should go from
1207 * Dormant to Active status.
1209 cmd
->se_ordered_id
= atomic_inc_return(&dev
->dev_ordered_id
);
1210 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1211 cmd
->se_ordered_id
, cmd
->sam_task_attr
,
1212 dev
->transport
->name
);
1217 target_setup_cmd_from_cdb(struct se_cmd
*cmd
, unsigned char *cdb
)
1219 struct se_device
*dev
= cmd
->se_dev
;
1223 * Ensure that the received CDB is less than the max (252 + 8) bytes
1224 * for VARIABLE_LENGTH_CMD
1226 if (scsi_command_size(cdb
) > SCSI_MAX_VARLEN_CDB_SIZE
) {
1227 pr_err("Received SCSI CDB with command_size: %d that"
1228 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1229 scsi_command_size(cdb
), SCSI_MAX_VARLEN_CDB_SIZE
);
1230 return TCM_INVALID_CDB_FIELD
;
1233 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1234 * allocate the additional extended CDB buffer now.. Otherwise
1235 * setup the pointer from __t_task_cdb to t_task_cdb.
1237 if (scsi_command_size(cdb
) > sizeof(cmd
->__t_task_cdb
)) {
1238 cmd
->t_task_cdb
= kzalloc(scsi_command_size(cdb
),
1240 if (!cmd
->t_task_cdb
) {
1241 pr_err("Unable to allocate cmd->t_task_cdb"
1242 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1243 scsi_command_size(cdb
),
1244 (unsigned long)sizeof(cmd
->__t_task_cdb
));
1245 return TCM_OUT_OF_RESOURCES
;
1248 cmd
->t_task_cdb
= &cmd
->__t_task_cdb
[0];
1250 * Copy the original CDB into cmd->
1252 memcpy(cmd
->t_task_cdb
, cdb
, scsi_command_size(cdb
));
1254 trace_target_sequencer_start(cmd
);
1257 * Check for an existing UNIT ATTENTION condition
1259 ret
= target_scsi3_ua_check(cmd
);
1263 ret
= target_alua_state_check(cmd
);
1267 ret
= target_check_reservation(cmd
);
1269 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
1273 ret
= dev
->transport
->parse_cdb(cmd
);
1277 ret
= transport_check_alloc_task_attr(cmd
);
1281 cmd
->se_cmd_flags
|= SCF_SUPPORTED_SAM_OPCODE
;
1283 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1284 if (cmd
->se_lun
->lun_sep
)
1285 cmd
->se_lun
->lun_sep
->sep_stats
.cmd_pdus
++;
1286 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1289 EXPORT_SYMBOL(target_setup_cmd_from_cdb
);
1292 * Used by fabric module frontends to queue tasks directly.
1293 * Many only be used from process context only
1295 int transport_handle_cdb_direct(
1302 pr_err("cmd->se_lun is NULL\n");
1305 if (in_interrupt()) {
1307 pr_err("transport_generic_handle_cdb cannot be called"
1308 " from interrupt context\n");
1312 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1313 * outstanding descriptors are handled correctly during shutdown via
1314 * transport_wait_for_tasks()
1316 * Also, we don't take cmd->t_state_lock here as we only expect
1317 * this to be called for initial descriptor submission.
1319 cmd
->t_state
= TRANSPORT_NEW_CMD
;
1320 cmd
->transport_state
|= CMD_T_ACTIVE
;
1323 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1324 * so follow TRANSPORT_NEW_CMD processing thread context usage
1325 * and call transport_generic_request_failure() if necessary..
1327 ret
= transport_generic_new_cmd(cmd
);
1329 transport_generic_request_failure(cmd
, ret
);
1332 EXPORT_SYMBOL(transport_handle_cdb_direct
);
1335 transport_generic_map_mem_to_cmd(struct se_cmd
*cmd
, struct scatterlist
*sgl
,
1336 u32 sgl_count
, struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
)
1338 if (!sgl
|| !sgl_count
)
1342 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
1343 * scatterlists already have been set to follow what the fabric
1344 * passes for the original expected data transfer length.
1346 if (cmd
->se_cmd_flags
& SCF_OVERFLOW_BIT
) {
1347 pr_warn("Rejecting SCSI DATA overflow for fabric using"
1348 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
1349 return TCM_INVALID_CDB_FIELD
;
1352 cmd
->t_data_sg
= sgl
;
1353 cmd
->t_data_nents
= sgl_count
;
1355 if (sgl_bidi
&& sgl_bidi_count
) {
1356 cmd
->t_bidi_data_sg
= sgl_bidi
;
1357 cmd
->t_bidi_data_nents
= sgl_bidi_count
;
1359 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
;
1364 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1365 * se_cmd + use pre-allocated SGL memory.
1367 * @se_cmd: command descriptor to submit
1368 * @se_sess: associated se_sess for endpoint
1369 * @cdb: pointer to SCSI CDB
1370 * @sense: pointer to SCSI sense buffer
1371 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1372 * @data_length: fabric expected data transfer length
1373 * @task_addr: SAM task attribute
1374 * @data_dir: DMA data direction
1375 * @flags: flags for command submission from target_sc_flags_tables
1376 * @sgl: struct scatterlist memory for unidirectional mapping
1377 * @sgl_count: scatterlist count for unidirectional mapping
1378 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1379 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1380 * @sgl_prot: struct scatterlist memory protection information
1381 * @sgl_prot_count: scatterlist count for protection information
1383 * Returns non zero to signal active I/O shutdown failure. All other
1384 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1385 * but still return zero here.
1387 * This may only be called from process context, and also currently
1388 * assumes internal allocation of fabric payload buffer by target-core.
1390 int target_submit_cmd_map_sgls(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1391 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1392 u32 data_length
, int task_attr
, int data_dir
, int flags
,
1393 struct scatterlist
*sgl
, u32 sgl_count
,
1394 struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
,
1395 struct scatterlist
*sgl_prot
, u32 sgl_prot_count
)
1397 struct se_portal_group
*se_tpg
;
1401 se_tpg
= se_sess
->se_tpg
;
1403 BUG_ON(se_cmd
->se_tfo
|| se_cmd
->se_sess
);
1404 BUG_ON(in_interrupt());
1406 * Initialize se_cmd for target operation. From this point
1407 * exceptions are handled by sending exception status via
1408 * target_core_fabric_ops->queue_status() callback
1410 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1411 data_length
, data_dir
, task_attr
, sense
);
1412 if (flags
& TARGET_SCF_UNKNOWN_SIZE
)
1413 se_cmd
->unknown_data_length
= 1;
1415 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1416 * se_sess->sess_cmd_list. A second kref_get here is necessary
1417 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1418 * kref_put() to happen during fabric packet acknowledgement.
1420 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1424 * Signal bidirectional data payloads to target-core
1426 if (flags
& TARGET_SCF_BIDI_OP
)
1427 se_cmd
->se_cmd_flags
|= SCF_BIDI
;
1429 * Locate se_lun pointer and attach it to struct se_cmd
1431 rc
= transport_lookup_cmd_lun(se_cmd
, unpacked_lun
);
1433 transport_send_check_condition_and_sense(se_cmd
, rc
, 0);
1434 target_put_sess_cmd(se_sess
, se_cmd
);
1438 rc
= target_setup_cmd_from_cdb(se_cmd
, cdb
);
1440 transport_generic_request_failure(se_cmd
, rc
);
1445 * Save pointers for SGLs containing protection information,
1448 if (sgl_prot_count
) {
1449 se_cmd
->t_prot_sg
= sgl_prot
;
1450 se_cmd
->t_prot_nents
= sgl_prot_count
;
1454 * When a non zero sgl_count has been passed perform SGL passthrough
1455 * mapping for pre-allocated fabric memory instead of having target
1456 * core perform an internal SGL allocation..
1458 if (sgl_count
!= 0) {
1462 * A work-around for tcm_loop as some userspace code via
1463 * scsi-generic do not memset their associated read buffers,
1464 * so go ahead and do that here for type non-data CDBs. Also
1465 * note that this is currently guaranteed to be a single SGL
1466 * for this case by target core in target_setup_cmd_from_cdb()
1467 * -> transport_generic_cmd_sequencer().
1469 if (!(se_cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
) &&
1470 se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
1471 unsigned char *buf
= NULL
;
1474 buf
= kmap(sg_page(sgl
)) + sgl
->offset
;
1477 memset(buf
, 0, sgl
->length
);
1478 kunmap(sg_page(sgl
));
1482 rc
= transport_generic_map_mem_to_cmd(se_cmd
, sgl
, sgl_count
,
1483 sgl_bidi
, sgl_bidi_count
);
1485 transport_generic_request_failure(se_cmd
, rc
);
1491 * Check if we need to delay processing because of ALUA
1492 * Active/NonOptimized primary access state..
1494 core_alua_check_nonop_delay(se_cmd
);
1496 transport_handle_cdb_direct(se_cmd
);
1499 EXPORT_SYMBOL(target_submit_cmd_map_sgls
);
1502 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1504 * @se_cmd: command descriptor to submit
1505 * @se_sess: associated se_sess for endpoint
1506 * @cdb: pointer to SCSI CDB
1507 * @sense: pointer to SCSI sense buffer
1508 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1509 * @data_length: fabric expected data transfer length
1510 * @task_addr: SAM task attribute
1511 * @data_dir: DMA data direction
1512 * @flags: flags for command submission from target_sc_flags_tables
1514 * Returns non zero to signal active I/O shutdown failure. All other
1515 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1516 * but still return zero here.
1518 * This may only be called from process context, and also currently
1519 * assumes internal allocation of fabric payload buffer by target-core.
1521 * It also assumes interal target core SGL memory allocation.
1523 int target_submit_cmd(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1524 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1525 u32 data_length
, int task_attr
, int data_dir
, int flags
)
1527 return target_submit_cmd_map_sgls(se_cmd
, se_sess
, cdb
, sense
,
1528 unpacked_lun
, data_length
, task_attr
, data_dir
,
1529 flags
, NULL
, 0, NULL
, 0, NULL
, 0);
1531 EXPORT_SYMBOL(target_submit_cmd
);
1533 static void target_complete_tmr_failure(struct work_struct
*work
)
1535 struct se_cmd
*se_cmd
= container_of(work
, struct se_cmd
, work
);
1537 se_cmd
->se_tmr_req
->response
= TMR_LUN_DOES_NOT_EXIST
;
1538 se_cmd
->se_tfo
->queue_tm_rsp(se_cmd
);
1540 transport_cmd_check_stop_to_fabric(se_cmd
);
1544 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1547 * @se_cmd: command descriptor to submit
1548 * @se_sess: associated se_sess for endpoint
1549 * @sense: pointer to SCSI sense buffer
1550 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1551 * @fabric_context: fabric context for TMR req
1552 * @tm_type: Type of TM request
1553 * @gfp: gfp type for caller
1554 * @tag: referenced task tag for TMR_ABORT_TASK
1555 * @flags: submit cmd flags
1557 * Callable from all contexts.
1560 int target_submit_tmr(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1561 unsigned char *sense
, u32 unpacked_lun
,
1562 void *fabric_tmr_ptr
, unsigned char tm_type
,
1563 gfp_t gfp
, unsigned int tag
, int flags
)
1565 struct se_portal_group
*se_tpg
;
1568 se_tpg
= se_sess
->se_tpg
;
1571 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1572 0, DMA_NONE
, TCM_SIMPLE_TAG
, sense
);
1574 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1575 * allocation failure.
1577 ret
= core_tmr_alloc_req(se_cmd
, fabric_tmr_ptr
, tm_type
, gfp
);
1581 if (tm_type
== TMR_ABORT_TASK
)
1582 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
1584 /* See target_submit_cmd for commentary */
1585 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1587 core_tmr_release_req(se_cmd
->se_tmr_req
);
1591 ret
= transport_lookup_tmr_lun(se_cmd
, unpacked_lun
);
1594 * For callback during failure handling, push this work off
1595 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1597 INIT_WORK(&se_cmd
->work
, target_complete_tmr_failure
);
1598 schedule_work(&se_cmd
->work
);
1601 transport_generic_handle_tmr(se_cmd
);
1604 EXPORT_SYMBOL(target_submit_tmr
);
1607 * If the cmd is active, request it to be stopped and sleep until it
1610 bool target_stop_cmd(struct se_cmd
*cmd
, unsigned long *flags
)
1611 __releases(&cmd
->t_state_lock
)
1612 __acquires(&cmd
->t_state_lock
)
1614 bool was_active
= false;
1616 if (cmd
->transport_state
& CMD_T_BUSY
) {
1617 cmd
->transport_state
|= CMD_T_REQUEST_STOP
;
1618 spin_unlock_irqrestore(&cmd
->t_state_lock
, *flags
);
1620 pr_debug("cmd %p waiting to complete\n", cmd
);
1621 wait_for_completion(&cmd
->task_stop_comp
);
1622 pr_debug("cmd %p stopped successfully\n", cmd
);
1624 spin_lock_irqsave(&cmd
->t_state_lock
, *flags
);
1625 cmd
->transport_state
&= ~CMD_T_REQUEST_STOP
;
1626 cmd
->transport_state
&= ~CMD_T_BUSY
;
1634 * Handle SAM-esque emulation for generic transport request failures.
1636 void transport_generic_request_failure(struct se_cmd
*cmd
,
1637 sense_reason_t sense_reason
)
1641 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1642 " CDB: 0x%02x\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
1643 cmd
->t_task_cdb
[0]);
1644 pr_debug("-----[ i_state: %d t_state: %d sense_reason: %d\n",
1645 cmd
->se_tfo
->get_cmd_state(cmd
),
1646 cmd
->t_state
, sense_reason
);
1647 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1648 (cmd
->transport_state
& CMD_T_ACTIVE
) != 0,
1649 (cmd
->transport_state
& CMD_T_STOP
) != 0,
1650 (cmd
->transport_state
& CMD_T_SENT
) != 0);
1653 * For SAM Task Attribute emulation for failed struct se_cmd
1655 transport_complete_task_attr(cmd
);
1657 * Handle special case for COMPARE_AND_WRITE failure, where the
1658 * callback is expected to drop the per device ->caw_sem.
1660 if ((cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) &&
1661 cmd
->transport_complete_callback
)
1662 cmd
->transport_complete_callback(cmd
, false);
1664 switch (sense_reason
) {
1665 case TCM_NON_EXISTENT_LUN
:
1666 case TCM_UNSUPPORTED_SCSI_OPCODE
:
1667 case TCM_INVALID_CDB_FIELD
:
1668 case TCM_INVALID_PARAMETER_LIST
:
1669 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
1670 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
1671 case TCM_UNKNOWN_MODE_PAGE
:
1672 case TCM_WRITE_PROTECTED
:
1673 case TCM_ADDRESS_OUT_OF_RANGE
:
1674 case TCM_CHECK_CONDITION_ABORT_CMD
:
1675 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
1676 case TCM_CHECK_CONDITION_NOT_READY
:
1677 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED
:
1678 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED
:
1679 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED
:
1681 case TCM_OUT_OF_RESOURCES
:
1682 sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1684 case TCM_RESERVATION_CONFLICT
:
1686 * No SENSE Data payload for this case, set SCSI Status
1687 * and queue the response to $FABRIC_MOD.
1689 * Uses linux/include/scsi/scsi.h SAM status codes defs
1691 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
1693 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1694 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1697 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1700 cmd
->se_dev
->dev_attrib
.emulate_ua_intlck_ctrl
== 2)
1701 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
1702 cmd
->orig_fe_lun
, 0x2C,
1703 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
1705 trace_target_cmd_complete(cmd
);
1706 ret
= cmd
->se_tfo
-> queue_status(cmd
);
1707 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1711 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1712 cmd
->t_task_cdb
[0], sense_reason
);
1713 sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
1717 ret
= transport_send_check_condition_and_sense(cmd
, sense_reason
, 0);
1718 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1722 transport_lun_remove_cmd(cmd
);
1723 if (!transport_cmd_check_stop_to_fabric(cmd
))
1728 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
1729 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1731 EXPORT_SYMBOL(transport_generic_request_failure
);
1733 void __target_execute_cmd(struct se_cmd
*cmd
)
1737 if (cmd
->execute_cmd
) {
1738 ret
= cmd
->execute_cmd(cmd
);
1740 spin_lock_irq(&cmd
->t_state_lock
);
1741 cmd
->transport_state
&= ~(CMD_T_BUSY
|CMD_T_SENT
);
1742 spin_unlock_irq(&cmd
->t_state_lock
);
1744 transport_generic_request_failure(cmd
, ret
);
1749 static int target_write_prot_action(struct se_cmd
*cmd
)
1753 * Perform WRITE_INSERT of PI using software emulation when backend
1754 * device has PI enabled, if the transport has not already generated
1755 * PI using hardware WRITE_INSERT offload.
1757 switch (cmd
->prot_op
) {
1758 case TARGET_PROT_DOUT_INSERT
:
1759 if (!(cmd
->se_sess
->sup_prot_ops
& TARGET_PROT_DOUT_INSERT
))
1760 sbc_dif_generate(cmd
);
1762 case TARGET_PROT_DOUT_STRIP
:
1763 if (cmd
->se_sess
->sup_prot_ops
& TARGET_PROT_DOUT_STRIP
)
1766 sectors
= cmd
->data_length
>> ilog2(cmd
->se_dev
->dev_attrib
.block_size
);
1767 cmd
->pi_err
= sbc_dif_verify_write(cmd
, cmd
->t_task_lba
,
1768 sectors
, 0, NULL
, 0);
1769 if (unlikely(cmd
->pi_err
)) {
1770 spin_lock_irq(&cmd
->t_state_lock
);
1771 cmd
->transport_state
&= ~CMD_T_BUSY
|CMD_T_SENT
;
1772 spin_unlock_irq(&cmd
->t_state_lock
);
1773 transport_generic_request_failure(cmd
, cmd
->pi_err
);
1784 static bool target_handle_task_attr(struct se_cmd
*cmd
)
1786 struct se_device
*dev
= cmd
->se_dev
;
1788 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1792 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1793 * to allow the passed struct se_cmd list of tasks to the front of the list.
1795 switch (cmd
->sam_task_attr
) {
1797 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1798 "se_ordered_id: %u\n",
1799 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1801 case TCM_ORDERED_TAG
:
1802 atomic_inc_mb(&dev
->dev_ordered_sync
);
1804 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1805 " se_ordered_id: %u\n",
1806 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1809 * Execute an ORDERED command if no other older commands
1810 * exist that need to be completed first.
1812 if (!atomic_read(&dev
->simple_cmds
))
1817 * For SIMPLE and UNTAGGED Task Attribute commands
1819 atomic_inc_mb(&dev
->simple_cmds
);
1823 if (atomic_read(&dev
->dev_ordered_sync
) == 0)
1826 spin_lock(&dev
->delayed_cmd_lock
);
1827 list_add_tail(&cmd
->se_delayed_node
, &dev
->delayed_cmd_list
);
1828 spin_unlock(&dev
->delayed_cmd_lock
);
1830 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1831 " delayed CMD list, se_ordered_id: %u\n",
1832 cmd
->t_task_cdb
[0], cmd
->sam_task_attr
,
1833 cmd
->se_ordered_id
);
1837 void target_execute_cmd(struct se_cmd
*cmd
)
1840 * If the received CDB has aleady been aborted stop processing it here.
1842 if (transport_check_aborted_status(cmd
, 1))
1846 * Determine if frontend context caller is requesting the stopping of
1847 * this command for frontend exceptions.
1849 spin_lock_irq(&cmd
->t_state_lock
);
1850 if (cmd
->transport_state
& CMD_T_STOP
) {
1851 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1853 cmd
->se_tfo
->get_task_tag(cmd
));
1855 spin_unlock_irq(&cmd
->t_state_lock
);
1856 complete_all(&cmd
->t_transport_stop_comp
);
1860 cmd
->t_state
= TRANSPORT_PROCESSING
;
1861 cmd
->transport_state
|= CMD_T_ACTIVE
|CMD_T_BUSY
|CMD_T_SENT
;
1862 spin_unlock_irq(&cmd
->t_state_lock
);
1864 if (target_write_prot_action(cmd
))
1867 if (target_handle_task_attr(cmd
)) {
1868 spin_lock_irq(&cmd
->t_state_lock
);
1869 cmd
->transport_state
&= ~CMD_T_BUSY
|CMD_T_SENT
;
1870 spin_unlock_irq(&cmd
->t_state_lock
);
1874 __target_execute_cmd(cmd
);
1876 EXPORT_SYMBOL(target_execute_cmd
);
1879 * Process all commands up to the last received ORDERED task attribute which
1880 * requires another blocking boundary
1882 static void target_restart_delayed_cmds(struct se_device
*dev
)
1887 spin_lock(&dev
->delayed_cmd_lock
);
1888 if (list_empty(&dev
->delayed_cmd_list
)) {
1889 spin_unlock(&dev
->delayed_cmd_lock
);
1893 cmd
= list_entry(dev
->delayed_cmd_list
.next
,
1894 struct se_cmd
, se_delayed_node
);
1895 list_del(&cmd
->se_delayed_node
);
1896 spin_unlock(&dev
->delayed_cmd_lock
);
1898 __target_execute_cmd(cmd
);
1900 if (cmd
->sam_task_attr
== TCM_ORDERED_TAG
)
1906 * Called from I/O completion to determine which dormant/delayed
1907 * and ordered cmds need to have their tasks added to the execution queue.
1909 static void transport_complete_task_attr(struct se_cmd
*cmd
)
1911 struct se_device
*dev
= cmd
->se_dev
;
1913 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1916 if (cmd
->sam_task_attr
== TCM_SIMPLE_TAG
) {
1917 atomic_dec_mb(&dev
->simple_cmds
);
1918 dev
->dev_cur_ordered_id
++;
1919 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
1920 " SIMPLE: %u\n", dev
->dev_cur_ordered_id
,
1921 cmd
->se_ordered_id
);
1922 } else if (cmd
->sam_task_attr
== TCM_HEAD_TAG
) {
1923 dev
->dev_cur_ordered_id
++;
1924 pr_debug("Incremented dev_cur_ordered_id: %u for"
1925 " HEAD_OF_QUEUE: %u\n", dev
->dev_cur_ordered_id
,
1926 cmd
->se_ordered_id
);
1927 } else if (cmd
->sam_task_attr
== TCM_ORDERED_TAG
) {
1928 atomic_dec_mb(&dev
->dev_ordered_sync
);
1930 dev
->dev_cur_ordered_id
++;
1931 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
1932 " %u\n", dev
->dev_cur_ordered_id
, cmd
->se_ordered_id
);
1935 target_restart_delayed_cmds(dev
);
1938 static void transport_complete_qf(struct se_cmd
*cmd
)
1942 transport_complete_task_attr(cmd
);
1944 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
1945 trace_target_cmd_complete(cmd
);
1946 ret
= cmd
->se_tfo
->queue_status(cmd
);
1950 switch (cmd
->data_direction
) {
1951 case DMA_FROM_DEVICE
:
1952 trace_target_cmd_complete(cmd
);
1953 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1956 if (cmd
->se_cmd_flags
& SCF_BIDI
) {
1957 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1961 /* Fall through for DMA_TO_DEVICE */
1963 trace_target_cmd_complete(cmd
);
1964 ret
= cmd
->se_tfo
->queue_status(cmd
);
1972 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1975 transport_lun_remove_cmd(cmd
);
1976 transport_cmd_check_stop_to_fabric(cmd
);
1979 static void transport_handle_queue_full(
1981 struct se_device
*dev
)
1983 spin_lock_irq(&dev
->qf_cmd_lock
);
1984 list_add_tail(&cmd
->se_qf_node
, &cmd
->se_dev
->qf_cmd_list
);
1985 atomic_inc_mb(&dev
->dev_qf_count
);
1986 spin_unlock_irq(&cmd
->se_dev
->qf_cmd_lock
);
1988 schedule_work(&cmd
->se_dev
->qf_work_queue
);
1991 static bool target_read_prot_action(struct se_cmd
*cmd
)
1995 switch (cmd
->prot_op
) {
1996 case TARGET_PROT_DIN_STRIP
:
1997 if (!(cmd
->se_sess
->sup_prot_ops
& TARGET_PROT_DIN_STRIP
)) {
1998 rc
= sbc_dif_read_strip(cmd
);
2005 case TARGET_PROT_DIN_INSERT
:
2006 if (cmd
->se_sess
->sup_prot_ops
& TARGET_PROT_DIN_INSERT
)
2009 sbc_dif_generate(cmd
);
2018 static void target_complete_ok_work(struct work_struct
*work
)
2020 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
2024 * Check if we need to move delayed/dormant tasks from cmds on the
2025 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
2028 transport_complete_task_attr(cmd
);
2031 * Check to schedule QUEUE_FULL work, or execute an existing
2032 * cmd->transport_qf_callback()
2034 if (atomic_read(&cmd
->se_dev
->dev_qf_count
) != 0)
2035 schedule_work(&cmd
->se_dev
->qf_work_queue
);
2038 * Check if we need to send a sense buffer from
2039 * the struct se_cmd in question.
2041 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
2042 WARN_ON(!cmd
->scsi_status
);
2043 ret
= transport_send_check_condition_and_sense(
2045 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2048 transport_lun_remove_cmd(cmd
);
2049 transport_cmd_check_stop_to_fabric(cmd
);
2053 * Check for a callback, used by amongst other things
2054 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
2056 if (cmd
->transport_complete_callback
) {
2059 rc
= cmd
->transport_complete_callback(cmd
, true);
2060 if (!rc
&& !(cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE_POST
)) {
2061 if ((cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) &&
2067 ret
= transport_send_check_condition_and_sense(cmd
,
2069 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2072 transport_lun_remove_cmd(cmd
);
2073 transport_cmd_check_stop_to_fabric(cmd
);
2079 switch (cmd
->data_direction
) {
2080 case DMA_FROM_DEVICE
:
2081 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
2082 if (cmd
->se_lun
->lun_sep
) {
2083 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
2086 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
2088 * Perform READ_STRIP of PI using software emulation when
2089 * backend had PI enabled, if the transport will not be
2090 * performing hardware READ_STRIP offload.
2092 if (target_read_prot_action(cmd
)) {
2093 ret
= transport_send_check_condition_and_sense(cmd
,
2095 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2098 transport_lun_remove_cmd(cmd
);
2099 transport_cmd_check_stop_to_fabric(cmd
);
2103 trace_target_cmd_complete(cmd
);
2104 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
2105 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2109 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
2110 if (cmd
->se_lun
->lun_sep
) {
2111 cmd
->se_lun
->lun_sep
->sep_stats
.rx_data_octets
+=
2114 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
2116 * Check if we need to send READ payload for BIDI-COMMAND
2118 if (cmd
->se_cmd_flags
& SCF_BIDI
) {
2119 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
2120 if (cmd
->se_lun
->lun_sep
) {
2121 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
2124 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
2125 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
2126 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2130 /* Fall through for DMA_TO_DEVICE */
2132 trace_target_cmd_complete(cmd
);
2133 ret
= cmd
->se_tfo
->queue_status(cmd
);
2134 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2141 transport_lun_remove_cmd(cmd
);
2142 transport_cmd_check_stop_to_fabric(cmd
);
2146 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
2147 " data_direction: %d\n", cmd
, cmd
->data_direction
);
2148 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
2149 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2152 static inline void transport_free_sgl(struct scatterlist
*sgl
, int nents
)
2154 struct scatterlist
*sg
;
2157 for_each_sg(sgl
, sg
, nents
, count
)
2158 __free_page(sg_page(sg
));
2163 static inline void transport_reset_sgl_orig(struct se_cmd
*cmd
)
2166 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
2167 * emulation, and free + reset pointers if necessary..
2169 if (!cmd
->t_data_sg_orig
)
2172 kfree(cmd
->t_data_sg
);
2173 cmd
->t_data_sg
= cmd
->t_data_sg_orig
;
2174 cmd
->t_data_sg_orig
= NULL
;
2175 cmd
->t_data_nents
= cmd
->t_data_nents_orig
;
2176 cmd
->t_data_nents_orig
= 0;
2179 static inline void transport_free_pages(struct se_cmd
*cmd
)
2181 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) {
2183 * Release special case READ buffer payload required for
2184 * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE
2186 if (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) {
2187 transport_free_sgl(cmd
->t_bidi_data_sg
,
2188 cmd
->t_bidi_data_nents
);
2189 cmd
->t_bidi_data_sg
= NULL
;
2190 cmd
->t_bidi_data_nents
= 0;
2192 transport_reset_sgl_orig(cmd
);
2195 transport_reset_sgl_orig(cmd
);
2197 transport_free_sgl(cmd
->t_data_sg
, cmd
->t_data_nents
);
2198 cmd
->t_data_sg
= NULL
;
2199 cmd
->t_data_nents
= 0;
2201 transport_free_sgl(cmd
->t_bidi_data_sg
, cmd
->t_bidi_data_nents
);
2202 cmd
->t_bidi_data_sg
= NULL
;
2203 cmd
->t_bidi_data_nents
= 0;
2205 transport_free_sgl(cmd
->t_prot_sg
, cmd
->t_prot_nents
);
2206 cmd
->t_prot_sg
= NULL
;
2207 cmd
->t_prot_nents
= 0;
2211 * transport_release_cmd - free a command
2212 * @cmd: command to free
2214 * This routine unconditionally frees a command, and reference counting
2215 * or list removal must be done in the caller.
2217 static int transport_release_cmd(struct se_cmd
*cmd
)
2219 BUG_ON(!cmd
->se_tfo
);
2221 if (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
2222 core_tmr_release_req(cmd
->se_tmr_req
);
2223 if (cmd
->t_task_cdb
!= cmd
->__t_task_cdb
)
2224 kfree(cmd
->t_task_cdb
);
2226 * If this cmd has been setup with target_get_sess_cmd(), drop
2227 * the kref and call ->release_cmd() in kref callback.
2229 return target_put_sess_cmd(cmd
->se_sess
, cmd
);
2233 * transport_put_cmd - release a reference to a command
2234 * @cmd: command to release
2236 * This routine releases our reference to the command and frees it if possible.
2238 static int transport_put_cmd(struct se_cmd
*cmd
)
2240 transport_free_pages(cmd
);
2241 return transport_release_cmd(cmd
);
2244 void *transport_kmap_data_sg(struct se_cmd
*cmd
)
2246 struct scatterlist
*sg
= cmd
->t_data_sg
;
2247 struct page
**pages
;
2251 * We need to take into account a possible offset here for fabrics like
2252 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2253 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
2255 if (!cmd
->t_data_nents
)
2259 if (cmd
->t_data_nents
== 1)
2260 return kmap(sg_page(sg
)) + sg
->offset
;
2262 /* >1 page. use vmap */
2263 pages
= kmalloc(sizeof(*pages
) * cmd
->t_data_nents
, GFP_KERNEL
);
2267 /* convert sg[] to pages[] */
2268 for_each_sg(cmd
->t_data_sg
, sg
, cmd
->t_data_nents
, i
) {
2269 pages
[i
] = sg_page(sg
);
2272 cmd
->t_data_vmap
= vmap(pages
, cmd
->t_data_nents
, VM_MAP
, PAGE_KERNEL
);
2274 if (!cmd
->t_data_vmap
)
2277 return cmd
->t_data_vmap
+ cmd
->t_data_sg
[0].offset
;
2279 EXPORT_SYMBOL(transport_kmap_data_sg
);
2281 void transport_kunmap_data_sg(struct se_cmd
*cmd
)
2283 if (!cmd
->t_data_nents
) {
2285 } else if (cmd
->t_data_nents
== 1) {
2286 kunmap(sg_page(cmd
->t_data_sg
));
2290 vunmap(cmd
->t_data_vmap
);
2291 cmd
->t_data_vmap
= NULL
;
2293 EXPORT_SYMBOL(transport_kunmap_data_sg
);
2296 target_alloc_sgl(struct scatterlist
**sgl
, unsigned int *nents
, u32 length
,
2299 struct scatterlist
*sg
;
2301 gfp_t zero_flag
= (zero_page
) ? __GFP_ZERO
: 0;
2305 nent
= DIV_ROUND_UP(length
, PAGE_SIZE
);
2306 sg
= kmalloc(sizeof(struct scatterlist
) * nent
, GFP_KERNEL
);
2310 sg_init_table(sg
, nent
);
2313 u32 page_len
= min_t(u32
, length
, PAGE_SIZE
);
2314 page
= alloc_page(GFP_KERNEL
| zero_flag
);
2318 sg_set_page(&sg
[i
], page
, page_len
, 0);
2329 __free_page(sg_page(&sg
[i
]));
2336 * Allocate any required resources to execute the command. For writes we
2337 * might not have the payload yet, so notify the fabric via a call to
2338 * ->write_pending instead. Otherwise place it on the execution queue.
2341 transport_generic_new_cmd(struct se_cmd
*cmd
)
2344 bool zero_flag
= !(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
);
2347 * Determine is the TCM fabric module has already allocated physical
2348 * memory, and is directly calling transport_generic_map_mem_to_cmd()
2351 if (!(cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) &&
2354 if ((cmd
->se_cmd_flags
& SCF_BIDI
) ||
2355 (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
)) {
2358 if (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
)
2359 bidi_length
= cmd
->t_task_nolb
*
2360 cmd
->se_dev
->dev_attrib
.block_size
;
2362 bidi_length
= cmd
->data_length
;
2364 ret
= target_alloc_sgl(&cmd
->t_bidi_data_sg
,
2365 &cmd
->t_bidi_data_nents
,
2366 bidi_length
, zero_flag
);
2368 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2371 if (cmd
->prot_op
!= TARGET_PROT_NORMAL
) {
2372 ret
= target_alloc_sgl(&cmd
->t_prot_sg
,
2374 cmd
->prot_length
, true);
2376 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2379 ret
= target_alloc_sgl(&cmd
->t_data_sg
, &cmd
->t_data_nents
,
2380 cmd
->data_length
, zero_flag
);
2382 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2383 } else if ((cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) &&
2386 * Special case for COMPARE_AND_WRITE with fabrics
2387 * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC.
2389 u32 caw_length
= cmd
->t_task_nolb
*
2390 cmd
->se_dev
->dev_attrib
.block_size
;
2392 ret
= target_alloc_sgl(&cmd
->t_bidi_data_sg
,
2393 &cmd
->t_bidi_data_nents
,
2394 caw_length
, zero_flag
);
2396 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2399 * If this command is not a write we can execute it right here,
2400 * for write buffers we need to notify the fabric driver first
2401 * and let it call back once the write buffers are ready.
2403 target_add_to_state_list(cmd
);
2404 if (cmd
->data_direction
!= DMA_TO_DEVICE
|| cmd
->data_length
== 0) {
2405 target_execute_cmd(cmd
);
2408 transport_cmd_check_stop(cmd
, false, true);
2410 ret
= cmd
->se_tfo
->write_pending(cmd
);
2411 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2414 /* fabric drivers should only return -EAGAIN or -ENOMEM as error */
2417 return (!ret
) ? 0 : TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2420 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd
);
2421 cmd
->t_state
= TRANSPORT_COMPLETE_QF_WP
;
2422 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2425 EXPORT_SYMBOL(transport_generic_new_cmd
);
2427 static void transport_write_pending_qf(struct se_cmd
*cmd
)
2431 ret
= cmd
->se_tfo
->write_pending(cmd
);
2432 if (ret
== -EAGAIN
|| ret
== -ENOMEM
) {
2433 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2435 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2439 int transport_generic_free_cmd(struct se_cmd
*cmd
, int wait_for_tasks
)
2441 unsigned long flags
;
2444 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
)) {
2445 if (wait_for_tasks
&& (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
))
2446 transport_wait_for_tasks(cmd
);
2448 ret
= transport_release_cmd(cmd
);
2451 transport_wait_for_tasks(cmd
);
2453 * Handle WRITE failure case where transport_generic_new_cmd()
2454 * has already added se_cmd to state_list, but fabric has
2455 * failed command before I/O submission.
2457 if (cmd
->state_active
) {
2458 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2459 target_remove_from_state_list(cmd
);
2460 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2464 transport_lun_remove_cmd(cmd
);
2466 ret
= transport_put_cmd(cmd
);
2470 EXPORT_SYMBOL(transport_generic_free_cmd
);
2472 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
2473 * @se_sess: session to reference
2474 * @se_cmd: command descriptor to add
2475 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2477 int target_get_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
,
2480 unsigned long flags
;
2484 * Add a second kref if the fabric caller is expecting to handle
2485 * fabric acknowledgement that requires two target_put_sess_cmd()
2486 * invocations before se_cmd descriptor release.
2489 kref_get(&se_cmd
->cmd_kref
);
2491 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2492 if (se_sess
->sess_tearing_down
) {
2496 list_add_tail(&se_cmd
->se_cmd_list
, &se_sess
->sess_cmd_list
);
2498 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2500 if (ret
&& ack_kref
)
2501 target_put_sess_cmd(se_sess
, se_cmd
);
2505 EXPORT_SYMBOL(target_get_sess_cmd
);
2507 static void target_release_cmd_kref(struct kref
*kref
)
2508 __releases(&se_cmd
->se_sess
->sess_cmd_lock
)
2510 struct se_cmd
*se_cmd
= container_of(kref
, struct se_cmd
, cmd_kref
);
2511 struct se_session
*se_sess
= se_cmd
->se_sess
;
2513 if (list_empty(&se_cmd
->se_cmd_list
)) {
2514 spin_unlock(&se_sess
->sess_cmd_lock
);
2515 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2518 if (se_sess
->sess_tearing_down
&& se_cmd
->cmd_wait_set
) {
2519 spin_unlock(&se_sess
->sess_cmd_lock
);
2520 complete(&se_cmd
->cmd_wait_comp
);
2523 list_del(&se_cmd
->se_cmd_list
);
2524 spin_unlock(&se_sess
->sess_cmd_lock
);
2526 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2529 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2530 * @se_sess: session to reference
2531 * @se_cmd: command descriptor to drop
2533 int target_put_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
)
2536 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2539 return kref_put_spinlock_irqsave(&se_cmd
->cmd_kref
, target_release_cmd_kref
,
2540 &se_sess
->sess_cmd_lock
);
2542 EXPORT_SYMBOL(target_put_sess_cmd
);
2544 /* target_sess_cmd_list_set_waiting - Flag all commands in
2545 * sess_cmd_list to complete cmd_wait_comp. Set
2546 * sess_tearing_down so no more commands are queued.
2547 * @se_sess: session to flag
2549 void target_sess_cmd_list_set_waiting(struct se_session
*se_sess
)
2551 struct se_cmd
*se_cmd
;
2552 unsigned long flags
;
2554 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2555 if (se_sess
->sess_tearing_down
) {
2556 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2559 se_sess
->sess_tearing_down
= 1;
2560 list_splice_init(&se_sess
->sess_cmd_list
, &se_sess
->sess_wait_list
);
2562 list_for_each_entry(se_cmd
, &se_sess
->sess_wait_list
, se_cmd_list
)
2563 se_cmd
->cmd_wait_set
= 1;
2565 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2567 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting
);
2569 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
2570 * @se_sess: session to wait for active I/O
2572 void target_wait_for_sess_cmds(struct se_session
*se_sess
)
2574 struct se_cmd
*se_cmd
, *tmp_cmd
;
2575 unsigned long flags
;
2577 list_for_each_entry_safe(se_cmd
, tmp_cmd
,
2578 &se_sess
->sess_wait_list
, se_cmd_list
) {
2579 list_del(&se_cmd
->se_cmd_list
);
2581 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2582 " %d\n", se_cmd
, se_cmd
->t_state
,
2583 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2585 wait_for_completion(&se_cmd
->cmd_wait_comp
);
2586 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2587 " fabric state: %d\n", se_cmd
, se_cmd
->t_state
,
2588 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2590 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2593 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2594 WARN_ON(!list_empty(&se_sess
->sess_cmd_list
));
2595 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2598 EXPORT_SYMBOL(target_wait_for_sess_cmds
);
2600 static int transport_clear_lun_ref_thread(void *p
)
2602 struct se_lun
*lun
= p
;
2604 percpu_ref_kill(&lun
->lun_ref
);
2606 wait_for_completion(&lun
->lun_ref_comp
);
2607 complete(&lun
->lun_shutdown_comp
);
2612 int transport_clear_lun_ref(struct se_lun
*lun
)
2614 struct task_struct
*kt
;
2616 kt
= kthread_run(transport_clear_lun_ref_thread
, lun
,
2617 "tcm_cl_%u", lun
->unpacked_lun
);
2619 pr_err("Unable to start clear_lun thread\n");
2622 wait_for_completion(&lun
->lun_shutdown_comp
);
2628 * transport_wait_for_tasks - wait for completion to occur
2629 * @cmd: command to wait
2631 * Called from frontend fabric context to wait for storage engine
2632 * to pause and/or release frontend generated struct se_cmd.
2634 bool transport_wait_for_tasks(struct se_cmd
*cmd
)
2636 unsigned long flags
;
2638 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2639 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
) &&
2640 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2641 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2645 if (!(cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
) &&
2646 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2647 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2651 if (!(cmd
->transport_state
& CMD_T_ACTIVE
)) {
2652 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2656 cmd
->transport_state
|= CMD_T_STOP
;
2658 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
2659 " i_state: %d, t_state: %d, CMD_T_STOP\n",
2660 cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
2661 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
2663 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2665 wait_for_completion(&cmd
->t_transport_stop_comp
);
2667 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2668 cmd
->transport_state
&= ~(CMD_T_ACTIVE
| CMD_T_STOP
);
2670 pr_debug("wait_for_tasks: Stopped wait_for_completion("
2671 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
2672 cmd
->se_tfo
->get_task_tag(cmd
));
2674 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2678 EXPORT_SYMBOL(transport_wait_for_tasks
);
2680 static int transport_get_sense_codes(
2685 *asc
= cmd
->scsi_asc
;
2686 *ascq
= cmd
->scsi_ascq
;
2692 void transport_err_sector_info(unsigned char *buffer
, sector_t bad_sector
)
2694 /* Place failed LBA in sense data information descriptor 0. */
2695 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 0xc;
2696 buffer
[SPC_DESC_TYPE_OFFSET
] = 0; /* Information */
2697 buffer
[SPC_ADDITIONAL_DESC_LEN_OFFSET
] = 0xa;
2698 buffer
[SPC_VALIDITY_OFFSET
] = 0x80;
2700 /* Descriptor Information: failing sector */
2701 put_unaligned_be64(bad_sector
, &buffer
[12]);
2705 transport_send_check_condition_and_sense(struct se_cmd
*cmd
,
2706 sense_reason_t reason
, int from_transport
)
2708 unsigned char *buffer
= cmd
->sense_buffer
;
2709 unsigned long flags
;
2710 u8 asc
= 0, ascq
= 0;
2712 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2713 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
2714 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2717 cmd
->se_cmd_flags
|= SCF_SENT_CHECK_CONDITION
;
2718 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2720 if (!reason
&& from_transport
)
2723 if (!from_transport
)
2724 cmd
->se_cmd_flags
|= SCF_EMULATED_TASK_SENSE
;
2727 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2728 * SENSE KEY values from include/scsi/scsi.h
2734 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2736 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2737 /* NO ADDITIONAL SENSE INFORMATION */
2738 buffer
[SPC_ASC_KEY_OFFSET
] = 0;
2739 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0;
2741 case TCM_NON_EXISTENT_LUN
:
2744 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2745 /* ILLEGAL REQUEST */
2746 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2747 /* LOGICAL UNIT NOT SUPPORTED */
2748 buffer
[SPC_ASC_KEY_OFFSET
] = 0x25;
2750 case TCM_UNSUPPORTED_SCSI_OPCODE
:
2751 case TCM_SECTOR_COUNT_TOO_MANY
:
2754 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2755 /* ILLEGAL REQUEST */
2756 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2757 /* INVALID COMMAND OPERATION CODE */
2758 buffer
[SPC_ASC_KEY_OFFSET
] = 0x20;
2760 case TCM_UNKNOWN_MODE_PAGE
:
2763 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2764 /* ILLEGAL REQUEST */
2765 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2766 /* INVALID FIELD IN CDB */
2767 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2769 case TCM_CHECK_CONDITION_ABORT_CMD
:
2772 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2773 /* ABORTED COMMAND */
2774 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2775 /* BUS DEVICE RESET FUNCTION OCCURRED */
2776 buffer
[SPC_ASC_KEY_OFFSET
] = 0x29;
2777 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x03;
2779 case TCM_INCORRECT_AMOUNT_OF_DATA
:
2782 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2783 /* ABORTED COMMAND */
2784 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2786 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2787 /* NOT ENOUGH UNSOLICITED DATA */
2788 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0d;
2790 case TCM_INVALID_CDB_FIELD
:
2793 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2794 /* ILLEGAL REQUEST */
2795 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2796 /* INVALID FIELD IN CDB */
2797 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2799 case TCM_INVALID_PARAMETER_LIST
:
2802 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2803 /* ILLEGAL REQUEST */
2804 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2805 /* INVALID FIELD IN PARAMETER LIST */
2806 buffer
[SPC_ASC_KEY_OFFSET
] = 0x26;
2808 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
2811 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2812 /* ILLEGAL REQUEST */
2813 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2814 /* PARAMETER LIST LENGTH ERROR */
2815 buffer
[SPC_ASC_KEY_OFFSET
] = 0x1a;
2817 case TCM_UNEXPECTED_UNSOLICITED_DATA
:
2820 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2821 /* ABORTED COMMAND */
2822 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2824 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2825 /* UNEXPECTED_UNSOLICITED_DATA */
2826 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0c;
2828 case TCM_SERVICE_CRC_ERROR
:
2831 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2832 /* ABORTED COMMAND */
2833 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2834 /* PROTOCOL SERVICE CRC ERROR */
2835 buffer
[SPC_ASC_KEY_OFFSET
] = 0x47;
2837 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x05;
2839 case TCM_SNACK_REJECTED
:
2842 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2843 /* ABORTED COMMAND */
2844 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2846 buffer
[SPC_ASC_KEY_OFFSET
] = 0x11;
2847 /* FAILED RETRANSMISSION REQUEST */
2848 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x13;
2850 case TCM_WRITE_PROTECTED
:
2853 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2855 buffer
[SPC_SENSE_KEY_OFFSET
] = DATA_PROTECT
;
2856 /* WRITE PROTECTED */
2857 buffer
[SPC_ASC_KEY_OFFSET
] = 0x27;
2859 case TCM_ADDRESS_OUT_OF_RANGE
:
2862 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2863 /* ILLEGAL REQUEST */
2864 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2865 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2866 buffer
[SPC_ASC_KEY_OFFSET
] = 0x21;
2868 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
2871 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2872 /* UNIT ATTENTION */
2873 buffer
[SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
2874 core_scsi3_ua_for_check_condition(cmd
, &asc
, &ascq
);
2875 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2876 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2878 case TCM_CHECK_CONDITION_NOT_READY
:
2881 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2883 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2884 transport_get_sense_codes(cmd
, &asc
, &ascq
);
2885 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2886 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2888 case TCM_MISCOMPARE_VERIFY
:
2891 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2892 buffer
[SPC_SENSE_KEY_OFFSET
] = MISCOMPARE
;
2893 /* MISCOMPARE DURING VERIFY OPERATION */
2894 buffer
[SPC_ASC_KEY_OFFSET
] = 0x1d;
2895 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x00;
2897 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED
:
2900 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2901 /* ILLEGAL REQUEST */
2902 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2903 /* LOGICAL BLOCK GUARD CHECK FAILED */
2904 buffer
[SPC_ASC_KEY_OFFSET
] = 0x10;
2905 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x01;
2906 transport_err_sector_info(buffer
, cmd
->bad_sector
);
2908 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED
:
2911 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2912 /* ILLEGAL REQUEST */
2913 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2914 /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */
2915 buffer
[SPC_ASC_KEY_OFFSET
] = 0x10;
2916 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x02;
2917 transport_err_sector_info(buffer
, cmd
->bad_sector
);
2919 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED
:
2922 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2923 /* ILLEGAL REQUEST */
2924 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2925 /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */
2926 buffer
[SPC_ASC_KEY_OFFSET
] = 0x10;
2927 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x03;
2928 transport_err_sector_info(buffer
, cmd
->bad_sector
);
2930 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
2934 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2936 * Returning ILLEGAL REQUEST would cause immediate IO errors on
2937 * Solaris initiators. Returning NOT READY instead means the
2938 * operations will be retried a finite number of times and we
2939 * can survive intermittent errors.
2941 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2942 /* LOGICAL UNIT COMMUNICATION FAILURE */
2943 buffer
[SPC_ASC_KEY_OFFSET
] = 0x08;
2947 * This code uses linux/include/scsi/scsi.h SAM status codes!
2949 cmd
->scsi_status
= SAM_STAT_CHECK_CONDITION
;
2951 * Automatically padded, this value is encoded in the fabric's
2952 * data_length response PDU containing the SCSI defined sense data.
2954 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
2957 trace_target_cmd_complete(cmd
);
2958 return cmd
->se_tfo
->queue_status(cmd
);
2960 EXPORT_SYMBOL(transport_send_check_condition_and_sense
);
2962 int transport_check_aborted_status(struct se_cmd
*cmd
, int send_status
)
2964 if (!(cmd
->transport_state
& CMD_T_ABORTED
))
2968 * If cmd has been aborted but either no status is to be sent or it has
2969 * already been sent, just return
2971 if (!send_status
|| !(cmd
->se_cmd_flags
& SCF_SEND_DELAYED_TAS
))
2974 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB: 0x%02x ITT: 0x%08x\n",
2975 cmd
->t_task_cdb
[0], cmd
->se_tfo
->get_task_tag(cmd
));
2977 cmd
->se_cmd_flags
&= ~SCF_SEND_DELAYED_TAS
;
2978 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
2979 trace_target_cmd_complete(cmd
);
2980 cmd
->se_tfo
->queue_status(cmd
);
2984 EXPORT_SYMBOL(transport_check_aborted_status
);
2986 void transport_send_task_abort(struct se_cmd
*cmd
)
2988 unsigned long flags
;
2990 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2991 if (cmd
->se_cmd_flags
& (SCF_SENT_CHECK_CONDITION
)) {
2992 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2995 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2998 * If there are still expected incoming fabric WRITEs, we wait
2999 * until until they have completed before sending a TASK_ABORTED
3000 * response. This response with TASK_ABORTED status will be
3001 * queued back to fabric module by transport_check_aborted_status().
3003 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
3004 if (cmd
->se_tfo
->write_pending_status(cmd
) != 0) {
3005 cmd
->transport_state
|= CMD_T_ABORTED
;
3006 cmd
->se_cmd_flags
|= SCF_SEND_DELAYED_TAS
;
3010 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
3012 transport_lun_remove_cmd(cmd
);
3014 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
3015 " ITT: 0x%08x\n", cmd
->t_task_cdb
[0],
3016 cmd
->se_tfo
->get_task_tag(cmd
));
3018 trace_target_cmd_complete(cmd
);
3019 cmd
->se_tfo
->queue_status(cmd
);
3022 static void target_tmr_work(struct work_struct
*work
)
3024 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
3025 struct se_device
*dev
= cmd
->se_dev
;
3026 struct se_tmr_req
*tmr
= cmd
->se_tmr_req
;
3029 switch (tmr
->function
) {
3030 case TMR_ABORT_TASK
:
3031 core_tmr_abort_task(dev
, tmr
, cmd
->se_sess
);
3033 case TMR_ABORT_TASK_SET
:
3035 case TMR_CLEAR_TASK_SET
:
3036 tmr
->response
= TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
3039 ret
= core_tmr_lun_reset(dev
, tmr
, NULL
, NULL
);
3040 tmr
->response
= (!ret
) ? TMR_FUNCTION_COMPLETE
:
3041 TMR_FUNCTION_REJECTED
;
3043 case TMR_TARGET_WARM_RESET
:
3044 tmr
->response
= TMR_FUNCTION_REJECTED
;
3046 case TMR_TARGET_COLD_RESET
:
3047 tmr
->response
= TMR_FUNCTION_REJECTED
;
3050 pr_err("Uknown TMR function: 0x%02x.\n",
3052 tmr
->response
= TMR_FUNCTION_REJECTED
;
3056 cmd
->t_state
= TRANSPORT_ISTATE_PROCESSING
;
3057 cmd
->se_tfo
->queue_tm_rsp(cmd
);
3059 transport_cmd_check_stop_to_fabric(cmd
);
3062 int transport_generic_handle_tmr(
3065 unsigned long flags
;
3067 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3068 cmd
->transport_state
|= CMD_T_ACTIVE
;
3069 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3071 INIT_WORK(&cmd
->work
, target_tmr_work
);
3072 queue_work(cmd
->se_dev
->tmr_wq
, &cmd
->work
);
3075 EXPORT_SYMBOL(transport_generic_handle_tmr
);