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.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_tcq.h>
44 #include <target/target_core_base.h>
45 #include <target/target_core_backend.h>
46 #include <target/target_core_fabric.h>
47 #include <target/target_core_configfs.h>
49 #include "target_core_internal.h"
50 #include "target_core_alua.h"
51 #include "target_core_pr.h"
52 #include "target_core_ua.h"
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/target.h>
57 static struct workqueue_struct
*target_completion_wq
;
58 static struct kmem_cache
*se_sess_cache
;
59 struct kmem_cache
*se_ua_cache
;
60 struct kmem_cache
*t10_pr_reg_cache
;
61 struct kmem_cache
*t10_alua_lu_gp_cache
;
62 struct kmem_cache
*t10_alua_lu_gp_mem_cache
;
63 struct kmem_cache
*t10_alua_tg_pt_gp_cache
;
64 struct kmem_cache
*t10_alua_tg_pt_gp_mem_cache
;
65 struct kmem_cache
*t10_alua_lba_map_cache
;
66 struct kmem_cache
*t10_alua_lba_map_mem_cache
;
68 static void transport_complete_task_attr(struct se_cmd
*cmd
);
69 static void transport_handle_queue_full(struct se_cmd
*cmd
,
70 struct se_device
*dev
);
71 static int transport_put_cmd(struct se_cmd
*cmd
);
72 static void target_complete_ok_work(struct work_struct
*work
);
74 int init_se_kmem_caches(void)
76 se_sess_cache
= kmem_cache_create("se_sess_cache",
77 sizeof(struct se_session
), __alignof__(struct se_session
),
80 pr_err("kmem_cache_create() for struct se_session"
84 se_ua_cache
= kmem_cache_create("se_ua_cache",
85 sizeof(struct se_ua
), __alignof__(struct se_ua
),
88 pr_err("kmem_cache_create() for struct se_ua failed\n");
89 goto out_free_sess_cache
;
91 t10_pr_reg_cache
= kmem_cache_create("t10_pr_reg_cache",
92 sizeof(struct t10_pr_registration
),
93 __alignof__(struct t10_pr_registration
), 0, NULL
);
94 if (!t10_pr_reg_cache
) {
95 pr_err("kmem_cache_create() for struct t10_pr_registration"
97 goto out_free_ua_cache
;
99 t10_alua_lu_gp_cache
= kmem_cache_create("t10_alua_lu_gp_cache",
100 sizeof(struct t10_alua_lu_gp
), __alignof__(struct t10_alua_lu_gp
),
102 if (!t10_alua_lu_gp_cache
) {
103 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
105 goto out_free_pr_reg_cache
;
107 t10_alua_lu_gp_mem_cache
= kmem_cache_create("t10_alua_lu_gp_mem_cache",
108 sizeof(struct t10_alua_lu_gp_member
),
109 __alignof__(struct t10_alua_lu_gp_member
), 0, NULL
);
110 if (!t10_alua_lu_gp_mem_cache
) {
111 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
113 goto out_free_lu_gp_cache
;
115 t10_alua_tg_pt_gp_cache
= kmem_cache_create("t10_alua_tg_pt_gp_cache",
116 sizeof(struct t10_alua_tg_pt_gp
),
117 __alignof__(struct t10_alua_tg_pt_gp
), 0, NULL
);
118 if (!t10_alua_tg_pt_gp_cache
) {
119 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
121 goto out_free_lu_gp_mem_cache
;
123 t10_alua_tg_pt_gp_mem_cache
= kmem_cache_create(
124 "t10_alua_tg_pt_gp_mem_cache",
125 sizeof(struct t10_alua_tg_pt_gp_member
),
126 __alignof__(struct t10_alua_tg_pt_gp_member
),
128 if (!t10_alua_tg_pt_gp_mem_cache
) {
129 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
131 goto out_free_tg_pt_gp_cache
;
133 t10_alua_lba_map_cache
= kmem_cache_create(
134 "t10_alua_lba_map_cache",
135 sizeof(struct t10_alua_lba_map
),
136 __alignof__(struct t10_alua_lba_map
), 0, NULL
);
137 if (!t10_alua_lba_map_cache
) {
138 pr_err("kmem_cache_create() for t10_alua_lba_map_"
140 goto out_free_tg_pt_gp_mem_cache
;
142 t10_alua_lba_map_mem_cache
= kmem_cache_create(
143 "t10_alua_lba_map_mem_cache",
144 sizeof(struct t10_alua_lba_map_member
),
145 __alignof__(struct t10_alua_lba_map_member
), 0, NULL
);
146 if (!t10_alua_lba_map_mem_cache
) {
147 pr_err("kmem_cache_create() for t10_alua_lba_map_mem_"
149 goto out_free_lba_map_cache
;
152 target_completion_wq
= alloc_workqueue("target_completion",
154 if (!target_completion_wq
)
155 goto out_free_lba_map_mem_cache
;
159 out_free_lba_map_mem_cache
:
160 kmem_cache_destroy(t10_alua_lba_map_mem_cache
);
161 out_free_lba_map_cache
:
162 kmem_cache_destroy(t10_alua_lba_map_cache
);
163 out_free_tg_pt_gp_mem_cache
:
164 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
165 out_free_tg_pt_gp_cache
:
166 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
167 out_free_lu_gp_mem_cache
:
168 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
169 out_free_lu_gp_cache
:
170 kmem_cache_destroy(t10_alua_lu_gp_cache
);
171 out_free_pr_reg_cache
:
172 kmem_cache_destroy(t10_pr_reg_cache
);
174 kmem_cache_destroy(se_ua_cache
);
176 kmem_cache_destroy(se_sess_cache
);
181 void release_se_kmem_caches(void)
183 destroy_workqueue(target_completion_wq
);
184 kmem_cache_destroy(se_sess_cache
);
185 kmem_cache_destroy(se_ua_cache
);
186 kmem_cache_destroy(t10_pr_reg_cache
);
187 kmem_cache_destroy(t10_alua_lu_gp_cache
);
188 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
189 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
190 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
191 kmem_cache_destroy(t10_alua_lba_map_cache
);
192 kmem_cache_destroy(t10_alua_lba_map_mem_cache
);
195 /* This code ensures unique mib indexes are handed out. */
196 static DEFINE_SPINLOCK(scsi_mib_index_lock
);
197 static u32 scsi_mib_index
[SCSI_INDEX_TYPE_MAX
];
200 * Allocate a new row index for the entry type specified
202 u32
scsi_get_new_index(scsi_index_t type
)
206 BUG_ON((type
< 0) || (type
>= SCSI_INDEX_TYPE_MAX
));
208 spin_lock(&scsi_mib_index_lock
);
209 new_index
= ++scsi_mib_index
[type
];
210 spin_unlock(&scsi_mib_index_lock
);
215 void transport_subsystem_check_init(void)
218 static int sub_api_initialized
;
220 if (sub_api_initialized
)
223 ret
= request_module("target_core_iblock");
225 pr_err("Unable to load target_core_iblock\n");
227 ret
= request_module("target_core_file");
229 pr_err("Unable to load target_core_file\n");
231 ret
= request_module("target_core_pscsi");
233 pr_err("Unable to load target_core_pscsi\n");
235 sub_api_initialized
= 1;
238 struct se_session
*transport_init_session(void)
240 struct se_session
*se_sess
;
242 se_sess
= kmem_cache_zalloc(se_sess_cache
, GFP_KERNEL
);
244 pr_err("Unable to allocate struct se_session from"
246 return ERR_PTR(-ENOMEM
);
248 INIT_LIST_HEAD(&se_sess
->sess_list
);
249 INIT_LIST_HEAD(&se_sess
->sess_acl_list
);
250 INIT_LIST_HEAD(&se_sess
->sess_cmd_list
);
251 INIT_LIST_HEAD(&se_sess
->sess_wait_list
);
252 spin_lock_init(&se_sess
->sess_cmd_lock
);
253 kref_init(&se_sess
->sess_kref
);
257 EXPORT_SYMBOL(transport_init_session
);
259 int transport_alloc_session_tags(struct se_session
*se_sess
,
260 unsigned int tag_num
, unsigned int tag_size
)
264 se_sess
->sess_cmd_map
= kzalloc(tag_num
* tag_size
,
265 GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
266 if (!se_sess
->sess_cmd_map
) {
267 se_sess
->sess_cmd_map
= vzalloc(tag_num
* tag_size
);
268 if (!se_sess
->sess_cmd_map
) {
269 pr_err("Unable to allocate se_sess->sess_cmd_map\n");
274 rc
= percpu_ida_init(&se_sess
->sess_tag_pool
, tag_num
);
276 pr_err("Unable to init se_sess->sess_tag_pool,"
277 " tag_num: %u\n", tag_num
);
278 if (is_vmalloc_addr(se_sess
->sess_cmd_map
))
279 vfree(se_sess
->sess_cmd_map
);
281 kfree(se_sess
->sess_cmd_map
);
282 se_sess
->sess_cmd_map
= NULL
;
288 EXPORT_SYMBOL(transport_alloc_session_tags
);
290 struct se_session
*transport_init_session_tags(unsigned int tag_num
,
291 unsigned int tag_size
)
293 struct se_session
*se_sess
;
296 se_sess
= transport_init_session();
300 rc
= transport_alloc_session_tags(se_sess
, tag_num
, tag_size
);
302 transport_free_session(se_sess
);
303 return ERR_PTR(-ENOMEM
);
308 EXPORT_SYMBOL(transport_init_session_tags
);
311 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
313 void __transport_register_session(
314 struct se_portal_group
*se_tpg
,
315 struct se_node_acl
*se_nacl
,
316 struct se_session
*se_sess
,
317 void *fabric_sess_ptr
)
319 unsigned char buf
[PR_REG_ISID_LEN
];
321 se_sess
->se_tpg
= se_tpg
;
322 se_sess
->fabric_sess_ptr
= fabric_sess_ptr
;
324 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
326 * Only set for struct se_session's that will actually be moving I/O.
327 * eg: *NOT* discovery sessions.
331 * If the fabric module supports an ISID based TransportID,
332 * save this value in binary from the fabric I_T Nexus now.
334 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
335 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
336 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
,
337 &buf
[0], PR_REG_ISID_LEN
);
338 se_sess
->sess_bin_isid
= get_unaligned_be64(&buf
[0]);
340 kref_get(&se_nacl
->acl_kref
);
342 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
344 * The se_nacl->nacl_sess pointer will be set to the
345 * last active I_T Nexus for each struct se_node_acl.
347 se_nacl
->nacl_sess
= se_sess
;
349 list_add_tail(&se_sess
->sess_acl_list
,
350 &se_nacl
->acl_sess_list
);
351 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
353 list_add_tail(&se_sess
->sess_list
, &se_tpg
->tpg_sess_list
);
355 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
356 se_tpg
->se_tpg_tfo
->get_fabric_name(), se_sess
->fabric_sess_ptr
);
358 EXPORT_SYMBOL(__transport_register_session
);
360 void transport_register_session(
361 struct se_portal_group
*se_tpg
,
362 struct se_node_acl
*se_nacl
,
363 struct se_session
*se_sess
,
364 void *fabric_sess_ptr
)
368 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
369 __transport_register_session(se_tpg
, se_nacl
, se_sess
, fabric_sess_ptr
);
370 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
372 EXPORT_SYMBOL(transport_register_session
);
374 static void target_release_session(struct kref
*kref
)
376 struct se_session
*se_sess
= container_of(kref
,
377 struct se_session
, sess_kref
);
378 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
380 se_tpg
->se_tpg_tfo
->close_session(se_sess
);
383 void target_get_session(struct se_session
*se_sess
)
385 kref_get(&se_sess
->sess_kref
);
387 EXPORT_SYMBOL(target_get_session
);
389 void target_put_session(struct se_session
*se_sess
)
391 struct se_portal_group
*tpg
= se_sess
->se_tpg
;
393 if (tpg
->se_tpg_tfo
->put_session
!= NULL
) {
394 tpg
->se_tpg_tfo
->put_session(se_sess
);
397 kref_put(&se_sess
->sess_kref
, target_release_session
);
399 EXPORT_SYMBOL(target_put_session
);
401 static void target_complete_nacl(struct kref
*kref
)
403 struct se_node_acl
*nacl
= container_of(kref
,
404 struct se_node_acl
, acl_kref
);
406 complete(&nacl
->acl_free_comp
);
409 void target_put_nacl(struct se_node_acl
*nacl
)
411 kref_put(&nacl
->acl_kref
, target_complete_nacl
);
414 void transport_deregister_session_configfs(struct se_session
*se_sess
)
416 struct se_node_acl
*se_nacl
;
419 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
421 se_nacl
= se_sess
->se_node_acl
;
423 spin_lock_irqsave(&se_nacl
->nacl_sess_lock
, flags
);
424 if (se_nacl
->acl_stop
== 0)
425 list_del(&se_sess
->sess_acl_list
);
427 * If the session list is empty, then clear the pointer.
428 * Otherwise, set the struct se_session pointer from the tail
429 * element of the per struct se_node_acl active session list.
431 if (list_empty(&se_nacl
->acl_sess_list
))
432 se_nacl
->nacl_sess
= NULL
;
434 se_nacl
->nacl_sess
= container_of(
435 se_nacl
->acl_sess_list
.prev
,
436 struct se_session
, sess_acl_list
);
438 spin_unlock_irqrestore(&se_nacl
->nacl_sess_lock
, flags
);
441 EXPORT_SYMBOL(transport_deregister_session_configfs
);
443 void transport_free_session(struct se_session
*se_sess
)
445 if (se_sess
->sess_cmd_map
) {
446 percpu_ida_destroy(&se_sess
->sess_tag_pool
);
447 if (is_vmalloc_addr(se_sess
->sess_cmd_map
))
448 vfree(se_sess
->sess_cmd_map
);
450 kfree(se_sess
->sess_cmd_map
);
452 kmem_cache_free(se_sess_cache
, se_sess
);
454 EXPORT_SYMBOL(transport_free_session
);
456 void transport_deregister_session(struct se_session
*se_sess
)
458 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
459 struct target_core_fabric_ops
*se_tfo
;
460 struct se_node_acl
*se_nacl
;
462 bool comp_nacl
= true;
465 transport_free_session(se_sess
);
468 se_tfo
= se_tpg
->se_tpg_tfo
;
470 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
471 list_del(&se_sess
->sess_list
);
472 se_sess
->se_tpg
= NULL
;
473 se_sess
->fabric_sess_ptr
= NULL
;
474 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
477 * Determine if we need to do extra work for this initiator node's
478 * struct se_node_acl if it had been previously dynamically generated.
480 se_nacl
= se_sess
->se_node_acl
;
482 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
483 if (se_nacl
&& se_nacl
->dynamic_node_acl
) {
484 if (!se_tfo
->tpg_check_demo_mode_cache(se_tpg
)) {
485 list_del(&se_nacl
->acl_list
);
486 se_tpg
->num_node_acls
--;
487 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
488 core_tpg_wait_for_nacl_pr_ref(se_nacl
);
489 core_free_device_list_for_node(se_nacl
, se_tpg
);
490 se_tfo
->tpg_release_fabric_acl(se_tpg
, se_nacl
);
493 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
496 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
498 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
499 se_tpg
->se_tpg_tfo
->get_fabric_name());
501 * If last kref is dropping now for an explicit NodeACL, awake sleeping
502 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
505 if (se_nacl
&& comp_nacl
== true)
506 target_put_nacl(se_nacl
);
508 transport_free_session(se_sess
);
510 EXPORT_SYMBOL(transport_deregister_session
);
513 * Called with cmd->t_state_lock held.
515 static void target_remove_from_state_list(struct se_cmd
*cmd
)
517 struct se_device
*dev
= cmd
->se_dev
;
523 if (cmd
->transport_state
& CMD_T_BUSY
)
526 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
527 if (cmd
->state_active
) {
528 list_del(&cmd
->state_list
);
529 cmd
->state_active
= false;
531 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
534 static int transport_cmd_check_stop(struct se_cmd
*cmd
, bool remove_from_lists
,
539 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
541 cmd
->t_state
= TRANSPORT_WRITE_PENDING
;
543 if (remove_from_lists
) {
544 target_remove_from_state_list(cmd
);
547 * Clear struct se_cmd->se_lun before the handoff to FE.
553 * Determine if frontend context caller is requesting the stopping of
554 * this command for frontend exceptions.
556 if (cmd
->transport_state
& CMD_T_STOP
) {
557 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
559 cmd
->se_tfo
->get_task_tag(cmd
));
561 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
563 complete(&cmd
->t_transport_stop_comp
);
567 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
568 if (remove_from_lists
) {
570 * Some fabric modules like tcm_loop can release
571 * their internally allocated I/O reference now and
574 * Fabric modules are expected to return '1' here if the
575 * se_cmd being passed is released at this point,
576 * or zero if not being released.
578 if (cmd
->se_tfo
->check_stop_free
!= NULL
) {
579 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
580 return cmd
->se_tfo
->check_stop_free(cmd
);
584 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
588 static int transport_cmd_check_stop_to_fabric(struct se_cmd
*cmd
)
590 return transport_cmd_check_stop(cmd
, true, false);
593 static void transport_lun_remove_cmd(struct se_cmd
*cmd
)
595 struct se_lun
*lun
= cmd
->se_lun
;
600 if (cmpxchg(&cmd
->lun_ref_active
, true, false))
601 percpu_ref_put(&lun
->lun_ref
);
604 void transport_cmd_finish_abort(struct se_cmd
*cmd
, int remove
)
606 if (transport_cmd_check_stop_to_fabric(cmd
))
609 transport_put_cmd(cmd
);
612 static void target_complete_failure_work(struct work_struct
*work
)
614 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
616 transport_generic_request_failure(cmd
,
617 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
);
621 * Used when asking transport to copy Sense Data from the underlying
622 * Linux/SCSI struct scsi_cmnd
624 static unsigned char *transport_get_sense_buffer(struct se_cmd
*cmd
)
626 struct se_device
*dev
= cmd
->se_dev
;
628 WARN_ON(!cmd
->se_lun
);
633 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
)
636 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
638 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
639 dev
->se_hba
->hba_id
, dev
->transport
->name
, cmd
->scsi_status
);
640 return cmd
->sense_buffer
;
643 void target_complete_cmd(struct se_cmd
*cmd
, u8 scsi_status
)
645 struct se_device
*dev
= cmd
->se_dev
;
646 int success
= scsi_status
== GOOD
;
649 cmd
->scsi_status
= scsi_status
;
652 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
653 cmd
->transport_state
&= ~CMD_T_BUSY
;
655 if (dev
&& dev
->transport
->transport_complete
) {
656 dev
->transport
->transport_complete(cmd
,
658 transport_get_sense_buffer(cmd
));
659 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
)
664 * See if we are waiting to complete for an exception condition.
666 if (cmd
->transport_state
& CMD_T_REQUEST_STOP
) {
667 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
668 complete(&cmd
->task_stop_comp
);
673 cmd
->transport_state
|= CMD_T_FAILED
;
676 * Check for case where an explicit ABORT_TASK has been received
677 * and transport_wait_for_tasks() will be waiting for completion..
679 if (cmd
->transport_state
& CMD_T_ABORTED
&&
680 cmd
->transport_state
& CMD_T_STOP
) {
681 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
682 complete(&cmd
->t_transport_stop_comp
);
684 } else if (cmd
->transport_state
& CMD_T_FAILED
) {
685 INIT_WORK(&cmd
->work
, target_complete_failure_work
);
687 INIT_WORK(&cmd
->work
, target_complete_ok_work
);
690 cmd
->t_state
= TRANSPORT_COMPLETE
;
691 cmd
->transport_state
|= (CMD_T_COMPLETE
| CMD_T_ACTIVE
);
692 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
694 queue_work(target_completion_wq
, &cmd
->work
);
696 EXPORT_SYMBOL(target_complete_cmd
);
698 static void target_add_to_state_list(struct se_cmd
*cmd
)
700 struct se_device
*dev
= cmd
->se_dev
;
703 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
704 if (!cmd
->state_active
) {
705 list_add_tail(&cmd
->state_list
, &dev
->state_list
);
706 cmd
->state_active
= true;
708 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
712 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
714 static void transport_write_pending_qf(struct se_cmd
*cmd
);
715 static void transport_complete_qf(struct se_cmd
*cmd
);
717 void target_qf_do_work(struct work_struct
*work
)
719 struct se_device
*dev
= container_of(work
, struct se_device
,
721 LIST_HEAD(qf_cmd_list
);
722 struct se_cmd
*cmd
, *cmd_tmp
;
724 spin_lock_irq(&dev
->qf_cmd_lock
);
725 list_splice_init(&dev
->qf_cmd_list
, &qf_cmd_list
);
726 spin_unlock_irq(&dev
->qf_cmd_lock
);
728 list_for_each_entry_safe(cmd
, cmd_tmp
, &qf_cmd_list
, se_qf_node
) {
729 list_del(&cmd
->se_qf_node
);
730 atomic_dec(&dev
->dev_qf_count
);
731 smp_mb__after_atomic_dec();
733 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
734 " context: %s\n", cmd
->se_tfo
->get_fabric_name(), cmd
,
735 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
) ? "COMPLETE_OK" :
736 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
) ? "WRITE_PENDING"
739 if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
)
740 transport_write_pending_qf(cmd
);
741 else if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
)
742 transport_complete_qf(cmd
);
746 unsigned char *transport_dump_cmd_direction(struct se_cmd
*cmd
)
748 switch (cmd
->data_direction
) {
751 case DMA_FROM_DEVICE
:
755 case DMA_BIDIRECTIONAL
:
764 void transport_dump_dev_state(
765 struct se_device
*dev
,
769 *bl
+= sprintf(b
+ *bl
, "Status: ");
770 if (dev
->export_count
)
771 *bl
+= sprintf(b
+ *bl
, "ACTIVATED");
773 *bl
+= sprintf(b
+ *bl
, "DEACTIVATED");
775 *bl
+= sprintf(b
+ *bl
, " Max Queue Depth: %d", dev
->queue_depth
);
776 *bl
+= sprintf(b
+ *bl
, " SectorSize: %u HwMaxSectors: %u\n",
777 dev
->dev_attrib
.block_size
,
778 dev
->dev_attrib
.hw_max_sectors
);
779 *bl
+= sprintf(b
+ *bl
, " ");
782 void transport_dump_vpd_proto_id(
784 unsigned char *p_buf
,
787 unsigned char buf
[VPD_TMP_BUF_SIZE
];
790 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
791 len
= sprintf(buf
, "T10 VPD Protocol Identifier: ");
793 switch (vpd
->protocol_identifier
) {
795 sprintf(buf
+len
, "Fibre Channel\n");
798 sprintf(buf
+len
, "Parallel SCSI\n");
801 sprintf(buf
+len
, "SSA\n");
804 sprintf(buf
+len
, "IEEE 1394\n");
807 sprintf(buf
+len
, "SCSI Remote Direct Memory Access"
811 sprintf(buf
+len
, "Internet SCSI (iSCSI)\n");
814 sprintf(buf
+len
, "SAS Serial SCSI Protocol\n");
817 sprintf(buf
+len
, "Automation/Drive Interface Transport"
821 sprintf(buf
+len
, "AT Attachment Interface ATA/ATAPI\n");
824 sprintf(buf
+len
, "Unknown 0x%02x\n",
825 vpd
->protocol_identifier
);
830 strncpy(p_buf
, buf
, p_buf_len
);
836 transport_set_vpd_proto_id(struct t10_vpd
*vpd
, unsigned char *page_83
)
839 * Check if the Protocol Identifier Valid (PIV) bit is set..
841 * from spc3r23.pdf section 7.5.1
843 if (page_83
[1] & 0x80) {
844 vpd
->protocol_identifier
= (page_83
[0] & 0xf0);
845 vpd
->protocol_identifier_set
= 1;
846 transport_dump_vpd_proto_id(vpd
, NULL
, 0);
849 EXPORT_SYMBOL(transport_set_vpd_proto_id
);
851 int transport_dump_vpd_assoc(
853 unsigned char *p_buf
,
856 unsigned char buf
[VPD_TMP_BUF_SIZE
];
860 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
861 len
= sprintf(buf
, "T10 VPD Identifier Association: ");
863 switch (vpd
->association
) {
865 sprintf(buf
+len
, "addressed logical unit\n");
868 sprintf(buf
+len
, "target port\n");
871 sprintf(buf
+len
, "SCSI target device\n");
874 sprintf(buf
+len
, "Unknown 0x%02x\n", vpd
->association
);
880 strncpy(p_buf
, buf
, p_buf_len
);
887 int transport_set_vpd_assoc(struct t10_vpd
*vpd
, unsigned char *page_83
)
890 * The VPD identification association..
892 * from spc3r23.pdf Section 7.6.3.1 Table 297
894 vpd
->association
= (page_83
[1] & 0x30);
895 return transport_dump_vpd_assoc(vpd
, NULL
, 0);
897 EXPORT_SYMBOL(transport_set_vpd_assoc
);
899 int transport_dump_vpd_ident_type(
901 unsigned char *p_buf
,
904 unsigned char buf
[VPD_TMP_BUF_SIZE
];
908 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
909 len
= sprintf(buf
, "T10 VPD Identifier Type: ");
911 switch (vpd
->device_identifier_type
) {
913 sprintf(buf
+len
, "Vendor specific\n");
916 sprintf(buf
+len
, "T10 Vendor ID based\n");
919 sprintf(buf
+len
, "EUI-64 based\n");
922 sprintf(buf
+len
, "NAA\n");
925 sprintf(buf
+len
, "Relative target port identifier\n");
928 sprintf(buf
+len
, "SCSI name string\n");
931 sprintf(buf
+len
, "Unsupported: 0x%02x\n",
932 vpd
->device_identifier_type
);
938 if (p_buf_len
< strlen(buf
)+1)
940 strncpy(p_buf
, buf
, p_buf_len
);
948 int transport_set_vpd_ident_type(struct t10_vpd
*vpd
, unsigned char *page_83
)
951 * The VPD identifier type..
953 * from spc3r23.pdf Section 7.6.3.1 Table 298
955 vpd
->device_identifier_type
= (page_83
[1] & 0x0f);
956 return transport_dump_vpd_ident_type(vpd
, NULL
, 0);
958 EXPORT_SYMBOL(transport_set_vpd_ident_type
);
960 int transport_dump_vpd_ident(
962 unsigned char *p_buf
,
965 unsigned char buf
[VPD_TMP_BUF_SIZE
];
968 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
970 switch (vpd
->device_identifier_code_set
) {
971 case 0x01: /* Binary */
972 snprintf(buf
, sizeof(buf
),
973 "T10 VPD Binary Device Identifier: %s\n",
974 &vpd
->device_identifier
[0]);
976 case 0x02: /* ASCII */
977 snprintf(buf
, sizeof(buf
),
978 "T10 VPD ASCII Device Identifier: %s\n",
979 &vpd
->device_identifier
[0]);
981 case 0x03: /* UTF-8 */
982 snprintf(buf
, sizeof(buf
),
983 "T10 VPD UTF-8 Device Identifier: %s\n",
984 &vpd
->device_identifier
[0]);
987 sprintf(buf
, "T10 VPD Device Identifier encoding unsupported:"
988 " 0x%02x", vpd
->device_identifier_code_set
);
994 strncpy(p_buf
, buf
, p_buf_len
);
1002 transport_set_vpd_ident(struct t10_vpd
*vpd
, unsigned char *page_83
)
1004 static const char hex_str
[] = "0123456789abcdef";
1005 int j
= 0, i
= 4; /* offset to start of the identifier */
1008 * The VPD Code Set (encoding)
1010 * from spc3r23.pdf Section 7.6.3.1 Table 296
1012 vpd
->device_identifier_code_set
= (page_83
[0] & 0x0f);
1013 switch (vpd
->device_identifier_code_set
) {
1014 case 0x01: /* Binary */
1015 vpd
->device_identifier
[j
++] =
1016 hex_str
[vpd
->device_identifier_type
];
1017 while (i
< (4 + page_83
[3])) {
1018 vpd
->device_identifier
[j
++] =
1019 hex_str
[(page_83
[i
] & 0xf0) >> 4];
1020 vpd
->device_identifier
[j
++] =
1021 hex_str
[page_83
[i
] & 0x0f];
1025 case 0x02: /* ASCII */
1026 case 0x03: /* UTF-8 */
1027 while (i
< (4 + page_83
[3]))
1028 vpd
->device_identifier
[j
++] = page_83
[i
++];
1034 return transport_dump_vpd_ident(vpd
, NULL
, 0);
1036 EXPORT_SYMBOL(transport_set_vpd_ident
);
1039 target_cmd_size_check(struct se_cmd
*cmd
, unsigned int size
)
1041 struct se_device
*dev
= cmd
->se_dev
;
1043 if (cmd
->unknown_data_length
) {
1044 cmd
->data_length
= size
;
1045 } else if (size
!= cmd
->data_length
) {
1046 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1047 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1048 " 0x%02x\n", cmd
->se_tfo
->get_fabric_name(),
1049 cmd
->data_length
, size
, cmd
->t_task_cdb
[0]);
1051 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
1052 pr_err("Rejecting underflow/overflow"
1054 return TCM_INVALID_CDB_FIELD
;
1057 * Reject READ_* or WRITE_* with overflow/underflow for
1058 * type SCF_SCSI_DATA_CDB.
1060 if (dev
->dev_attrib
.block_size
!= 512) {
1061 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1062 " CDB on non 512-byte sector setup subsystem"
1063 " plugin: %s\n", dev
->transport
->name
);
1064 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1065 return TCM_INVALID_CDB_FIELD
;
1068 * For the overflow case keep the existing fabric provided
1069 * ->data_length. Otherwise for the underflow case, reset
1070 * ->data_length to the smaller SCSI expected data transfer
1073 if (size
> cmd
->data_length
) {
1074 cmd
->se_cmd_flags
|= SCF_OVERFLOW_BIT
;
1075 cmd
->residual_count
= (size
- cmd
->data_length
);
1077 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
1078 cmd
->residual_count
= (cmd
->data_length
- size
);
1079 cmd
->data_length
= size
;
1088 * Used by fabric modules containing a local struct se_cmd within their
1089 * fabric dependent per I/O descriptor.
1091 void transport_init_se_cmd(
1093 struct target_core_fabric_ops
*tfo
,
1094 struct se_session
*se_sess
,
1098 unsigned char *sense_buffer
)
1100 INIT_LIST_HEAD(&cmd
->se_delayed_node
);
1101 INIT_LIST_HEAD(&cmd
->se_qf_node
);
1102 INIT_LIST_HEAD(&cmd
->se_cmd_list
);
1103 INIT_LIST_HEAD(&cmd
->state_list
);
1104 init_completion(&cmd
->t_transport_stop_comp
);
1105 init_completion(&cmd
->cmd_wait_comp
);
1106 init_completion(&cmd
->task_stop_comp
);
1107 spin_lock_init(&cmd
->t_state_lock
);
1108 cmd
->transport_state
= CMD_T_DEV_ACTIVE
;
1111 cmd
->se_sess
= se_sess
;
1112 cmd
->data_length
= data_length
;
1113 cmd
->data_direction
= data_direction
;
1114 cmd
->sam_task_attr
= task_attr
;
1115 cmd
->sense_buffer
= sense_buffer
;
1117 cmd
->state_active
= false;
1119 EXPORT_SYMBOL(transport_init_se_cmd
);
1121 static sense_reason_t
1122 transport_check_alloc_task_attr(struct se_cmd
*cmd
)
1124 struct se_device
*dev
= cmd
->se_dev
;
1127 * Check if SAM Task Attribute emulation is enabled for this
1128 * struct se_device storage object
1130 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1133 if (cmd
->sam_task_attr
== MSG_ACA_TAG
) {
1134 pr_debug("SAM Task Attribute ACA"
1135 " emulation is not supported\n");
1136 return TCM_INVALID_CDB_FIELD
;
1139 * Used to determine when ORDERED commands should go from
1140 * Dormant to Active status.
1142 cmd
->se_ordered_id
= atomic_inc_return(&dev
->dev_ordered_id
);
1143 smp_mb__after_atomic_inc();
1144 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1145 cmd
->se_ordered_id
, cmd
->sam_task_attr
,
1146 dev
->transport
->name
);
1151 target_setup_cmd_from_cdb(struct se_cmd
*cmd
, unsigned char *cdb
)
1153 struct se_device
*dev
= cmd
->se_dev
;
1157 * Ensure that the received CDB is less than the max (252 + 8) bytes
1158 * for VARIABLE_LENGTH_CMD
1160 if (scsi_command_size(cdb
) > SCSI_MAX_VARLEN_CDB_SIZE
) {
1161 pr_err("Received SCSI CDB with command_size: %d that"
1162 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1163 scsi_command_size(cdb
), SCSI_MAX_VARLEN_CDB_SIZE
);
1164 return TCM_INVALID_CDB_FIELD
;
1167 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1168 * allocate the additional extended CDB buffer now.. Otherwise
1169 * setup the pointer from __t_task_cdb to t_task_cdb.
1171 if (scsi_command_size(cdb
) > sizeof(cmd
->__t_task_cdb
)) {
1172 cmd
->t_task_cdb
= kzalloc(scsi_command_size(cdb
),
1174 if (!cmd
->t_task_cdb
) {
1175 pr_err("Unable to allocate cmd->t_task_cdb"
1176 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1177 scsi_command_size(cdb
),
1178 (unsigned long)sizeof(cmd
->__t_task_cdb
));
1179 return TCM_OUT_OF_RESOURCES
;
1182 cmd
->t_task_cdb
= &cmd
->__t_task_cdb
[0];
1184 * Copy the original CDB into cmd->
1186 memcpy(cmd
->t_task_cdb
, cdb
, scsi_command_size(cdb
));
1188 trace_target_sequencer_start(cmd
);
1191 * Check for an existing UNIT ATTENTION condition
1193 ret
= target_scsi3_ua_check(cmd
);
1197 ret
= target_alua_state_check(cmd
);
1201 ret
= target_check_reservation(cmd
);
1203 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
1207 ret
= dev
->transport
->parse_cdb(cmd
);
1211 ret
= transport_check_alloc_task_attr(cmd
);
1215 cmd
->se_cmd_flags
|= SCF_SUPPORTED_SAM_OPCODE
;
1217 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1218 if (cmd
->se_lun
->lun_sep
)
1219 cmd
->se_lun
->lun_sep
->sep_stats
.cmd_pdus
++;
1220 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1223 EXPORT_SYMBOL(target_setup_cmd_from_cdb
);
1226 * Used by fabric module frontends to queue tasks directly.
1227 * Many only be used from process context only
1229 int transport_handle_cdb_direct(
1236 pr_err("cmd->se_lun is NULL\n");
1239 if (in_interrupt()) {
1241 pr_err("transport_generic_handle_cdb cannot be called"
1242 " from interrupt context\n");
1246 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1247 * outstanding descriptors are handled correctly during shutdown via
1248 * transport_wait_for_tasks()
1250 * Also, we don't take cmd->t_state_lock here as we only expect
1251 * this to be called for initial descriptor submission.
1253 cmd
->t_state
= TRANSPORT_NEW_CMD
;
1254 cmd
->transport_state
|= CMD_T_ACTIVE
;
1257 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1258 * so follow TRANSPORT_NEW_CMD processing thread context usage
1259 * and call transport_generic_request_failure() if necessary..
1261 ret
= transport_generic_new_cmd(cmd
);
1263 transport_generic_request_failure(cmd
, ret
);
1266 EXPORT_SYMBOL(transport_handle_cdb_direct
);
1269 transport_generic_map_mem_to_cmd(struct se_cmd
*cmd
, struct scatterlist
*sgl
,
1270 u32 sgl_count
, struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
)
1272 if (!sgl
|| !sgl_count
)
1276 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
1277 * scatterlists already have been set to follow what the fabric
1278 * passes for the original expected data transfer length.
1280 if (cmd
->se_cmd_flags
& SCF_OVERFLOW_BIT
) {
1281 pr_warn("Rejecting SCSI DATA overflow for fabric using"
1282 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
1283 return TCM_INVALID_CDB_FIELD
;
1286 cmd
->t_data_sg
= sgl
;
1287 cmd
->t_data_nents
= sgl_count
;
1289 if (sgl_bidi
&& sgl_bidi_count
) {
1290 cmd
->t_bidi_data_sg
= sgl_bidi
;
1291 cmd
->t_bidi_data_nents
= sgl_bidi_count
;
1293 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
;
1298 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1299 * se_cmd + use pre-allocated SGL memory.
1301 * @se_cmd: command descriptor to submit
1302 * @se_sess: associated se_sess for endpoint
1303 * @cdb: pointer to SCSI CDB
1304 * @sense: pointer to SCSI sense buffer
1305 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1306 * @data_length: fabric expected data transfer length
1307 * @task_addr: SAM task attribute
1308 * @data_dir: DMA data direction
1309 * @flags: flags for command submission from target_sc_flags_tables
1310 * @sgl: struct scatterlist memory for unidirectional mapping
1311 * @sgl_count: scatterlist count for unidirectional mapping
1312 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1313 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1314 * @sgl_prot: struct scatterlist memory protection information
1315 * @sgl_prot_count: scatterlist count for protection information
1317 * Returns non zero to signal active I/O shutdown failure. All other
1318 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1319 * but still return zero here.
1321 * This may only be called from process context, and also currently
1322 * assumes internal allocation of fabric payload buffer by target-core.
1324 int target_submit_cmd_map_sgls(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1325 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1326 u32 data_length
, int task_attr
, int data_dir
, int flags
,
1327 struct scatterlist
*sgl
, u32 sgl_count
,
1328 struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
,
1329 struct scatterlist
*sgl_prot
, u32 sgl_prot_count
)
1331 struct se_portal_group
*se_tpg
;
1335 se_tpg
= se_sess
->se_tpg
;
1337 BUG_ON(se_cmd
->se_tfo
|| se_cmd
->se_sess
);
1338 BUG_ON(in_interrupt());
1340 * Initialize se_cmd for target operation. From this point
1341 * exceptions are handled by sending exception status via
1342 * target_core_fabric_ops->queue_status() callback
1344 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1345 data_length
, data_dir
, task_attr
, sense
);
1346 if (flags
& TARGET_SCF_UNKNOWN_SIZE
)
1347 se_cmd
->unknown_data_length
= 1;
1349 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1350 * se_sess->sess_cmd_list. A second kref_get here is necessary
1351 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1352 * kref_put() to happen during fabric packet acknowledgement.
1354 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1358 * Signal bidirectional data payloads to target-core
1360 if (flags
& TARGET_SCF_BIDI_OP
)
1361 se_cmd
->se_cmd_flags
|= SCF_BIDI
;
1363 * Locate se_lun pointer and attach it to struct se_cmd
1365 rc
= transport_lookup_cmd_lun(se_cmd
, unpacked_lun
);
1367 transport_send_check_condition_and_sense(se_cmd
, rc
, 0);
1368 target_put_sess_cmd(se_sess
, se_cmd
);
1372 * Save pointers for SGLs containing protection information,
1375 if (sgl_prot_count
) {
1376 se_cmd
->t_prot_sg
= sgl_prot
;
1377 se_cmd
->t_prot_nents
= sgl_prot_count
;
1380 rc
= target_setup_cmd_from_cdb(se_cmd
, cdb
);
1382 transport_generic_request_failure(se_cmd
, rc
);
1386 * When a non zero sgl_count has been passed perform SGL passthrough
1387 * mapping for pre-allocated fabric memory instead of having target
1388 * core perform an internal SGL allocation..
1390 if (sgl_count
!= 0) {
1394 * A work-around for tcm_loop as some userspace code via
1395 * scsi-generic do not memset their associated read buffers,
1396 * so go ahead and do that here for type non-data CDBs. Also
1397 * note that this is currently guaranteed to be a single SGL
1398 * for this case by target core in target_setup_cmd_from_cdb()
1399 * -> transport_generic_cmd_sequencer().
1401 if (!(se_cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
) &&
1402 se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
1403 unsigned char *buf
= NULL
;
1406 buf
= kmap(sg_page(sgl
)) + sgl
->offset
;
1409 memset(buf
, 0, sgl
->length
);
1410 kunmap(sg_page(sgl
));
1414 rc
= transport_generic_map_mem_to_cmd(se_cmd
, sgl
, sgl_count
,
1415 sgl_bidi
, sgl_bidi_count
);
1417 transport_generic_request_failure(se_cmd
, rc
);
1423 * Check if we need to delay processing because of ALUA
1424 * Active/NonOptimized primary access state..
1426 core_alua_check_nonop_delay(se_cmd
);
1428 transport_handle_cdb_direct(se_cmd
);
1431 EXPORT_SYMBOL(target_submit_cmd_map_sgls
);
1434 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1436 * @se_cmd: command descriptor to submit
1437 * @se_sess: associated se_sess for endpoint
1438 * @cdb: pointer to SCSI CDB
1439 * @sense: pointer to SCSI sense buffer
1440 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1441 * @data_length: fabric expected data transfer length
1442 * @task_addr: SAM task attribute
1443 * @data_dir: DMA data direction
1444 * @flags: flags for command submission from target_sc_flags_tables
1446 * Returns non zero to signal active I/O shutdown failure. All other
1447 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1448 * but still return zero here.
1450 * This may only be called from process context, and also currently
1451 * assumes internal allocation of fabric payload buffer by target-core.
1453 * It also assumes interal target core SGL memory allocation.
1455 int target_submit_cmd(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1456 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1457 u32 data_length
, int task_attr
, int data_dir
, int flags
)
1459 return target_submit_cmd_map_sgls(se_cmd
, se_sess
, cdb
, sense
,
1460 unpacked_lun
, data_length
, task_attr
, data_dir
,
1461 flags
, NULL
, 0, NULL
, 0, NULL
, 0);
1463 EXPORT_SYMBOL(target_submit_cmd
);
1465 static void target_complete_tmr_failure(struct work_struct
*work
)
1467 struct se_cmd
*se_cmd
= container_of(work
, struct se_cmd
, work
);
1469 se_cmd
->se_tmr_req
->response
= TMR_LUN_DOES_NOT_EXIST
;
1470 se_cmd
->se_tfo
->queue_tm_rsp(se_cmd
);
1472 transport_cmd_check_stop_to_fabric(se_cmd
);
1476 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1479 * @se_cmd: command descriptor to submit
1480 * @se_sess: associated se_sess for endpoint
1481 * @sense: pointer to SCSI sense buffer
1482 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1483 * @fabric_context: fabric context for TMR req
1484 * @tm_type: Type of TM request
1485 * @gfp: gfp type for caller
1486 * @tag: referenced task tag for TMR_ABORT_TASK
1487 * @flags: submit cmd flags
1489 * Callable from all contexts.
1492 int target_submit_tmr(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1493 unsigned char *sense
, u32 unpacked_lun
,
1494 void *fabric_tmr_ptr
, unsigned char tm_type
,
1495 gfp_t gfp
, unsigned int tag
, int flags
)
1497 struct se_portal_group
*se_tpg
;
1500 se_tpg
= se_sess
->se_tpg
;
1503 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1504 0, DMA_NONE
, MSG_SIMPLE_TAG
, sense
);
1506 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1507 * allocation failure.
1509 ret
= core_tmr_alloc_req(se_cmd
, fabric_tmr_ptr
, tm_type
, gfp
);
1513 if (tm_type
== TMR_ABORT_TASK
)
1514 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
1516 /* See target_submit_cmd for commentary */
1517 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1519 core_tmr_release_req(se_cmd
->se_tmr_req
);
1523 ret
= transport_lookup_tmr_lun(se_cmd
, unpacked_lun
);
1526 * For callback during failure handling, push this work off
1527 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1529 INIT_WORK(&se_cmd
->work
, target_complete_tmr_failure
);
1530 schedule_work(&se_cmd
->work
);
1533 transport_generic_handle_tmr(se_cmd
);
1536 EXPORT_SYMBOL(target_submit_tmr
);
1539 * If the cmd is active, request it to be stopped and sleep until it
1542 bool target_stop_cmd(struct se_cmd
*cmd
, unsigned long *flags
)
1544 bool was_active
= false;
1546 if (cmd
->transport_state
& CMD_T_BUSY
) {
1547 cmd
->transport_state
|= CMD_T_REQUEST_STOP
;
1548 spin_unlock_irqrestore(&cmd
->t_state_lock
, *flags
);
1550 pr_debug("cmd %p waiting to complete\n", cmd
);
1551 wait_for_completion(&cmd
->task_stop_comp
);
1552 pr_debug("cmd %p stopped successfully\n", cmd
);
1554 spin_lock_irqsave(&cmd
->t_state_lock
, *flags
);
1555 cmd
->transport_state
&= ~CMD_T_REQUEST_STOP
;
1556 cmd
->transport_state
&= ~CMD_T_BUSY
;
1564 * Handle SAM-esque emulation for generic transport request failures.
1566 void transport_generic_request_failure(struct se_cmd
*cmd
,
1567 sense_reason_t sense_reason
)
1571 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1572 " CDB: 0x%02x\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
1573 cmd
->t_task_cdb
[0]);
1574 pr_debug("-----[ i_state: %d t_state: %d sense_reason: %d\n",
1575 cmd
->se_tfo
->get_cmd_state(cmd
),
1576 cmd
->t_state
, sense_reason
);
1577 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1578 (cmd
->transport_state
& CMD_T_ACTIVE
) != 0,
1579 (cmd
->transport_state
& CMD_T_STOP
) != 0,
1580 (cmd
->transport_state
& CMD_T_SENT
) != 0);
1583 * For SAM Task Attribute emulation for failed struct se_cmd
1585 transport_complete_task_attr(cmd
);
1587 * Handle special case for COMPARE_AND_WRITE failure, where the
1588 * callback is expected to drop the per device ->caw_mutex.
1590 if ((cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) &&
1591 cmd
->transport_complete_callback
)
1592 cmd
->transport_complete_callback(cmd
);
1594 switch (sense_reason
) {
1595 case TCM_NON_EXISTENT_LUN
:
1596 case TCM_UNSUPPORTED_SCSI_OPCODE
:
1597 case TCM_INVALID_CDB_FIELD
:
1598 case TCM_INVALID_PARAMETER_LIST
:
1599 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
1600 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
1601 case TCM_UNKNOWN_MODE_PAGE
:
1602 case TCM_WRITE_PROTECTED
:
1603 case TCM_ADDRESS_OUT_OF_RANGE
:
1604 case TCM_CHECK_CONDITION_ABORT_CMD
:
1605 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
1606 case TCM_CHECK_CONDITION_NOT_READY
:
1608 case TCM_OUT_OF_RESOURCES
:
1609 sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1611 case TCM_RESERVATION_CONFLICT
:
1613 * No SENSE Data payload for this case, set SCSI Status
1614 * and queue the response to $FABRIC_MOD.
1616 * Uses linux/include/scsi/scsi.h SAM status codes defs
1618 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
1620 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1621 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1624 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1627 cmd
->se_dev
->dev_attrib
.emulate_ua_intlck_ctrl
== 2)
1628 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
1629 cmd
->orig_fe_lun
, 0x2C,
1630 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
1632 trace_target_cmd_complete(cmd
);
1633 ret
= cmd
->se_tfo
-> queue_status(cmd
);
1634 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1638 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1639 cmd
->t_task_cdb
[0], sense_reason
);
1640 sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
1644 ret
= transport_send_check_condition_and_sense(cmd
, sense_reason
, 0);
1645 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1649 transport_lun_remove_cmd(cmd
);
1650 if (!transport_cmd_check_stop_to_fabric(cmd
))
1655 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
1656 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1658 EXPORT_SYMBOL(transport_generic_request_failure
);
1660 void __target_execute_cmd(struct se_cmd
*cmd
)
1664 if (cmd
->execute_cmd
) {
1665 ret
= cmd
->execute_cmd(cmd
);
1667 spin_lock_irq(&cmd
->t_state_lock
);
1668 cmd
->transport_state
&= ~(CMD_T_BUSY
|CMD_T_SENT
);
1669 spin_unlock_irq(&cmd
->t_state_lock
);
1671 transport_generic_request_failure(cmd
, ret
);
1676 static bool target_handle_task_attr(struct se_cmd
*cmd
)
1678 struct se_device
*dev
= cmd
->se_dev
;
1680 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1684 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1685 * to allow the passed struct se_cmd list of tasks to the front of the list.
1687 switch (cmd
->sam_task_attr
) {
1689 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1690 "se_ordered_id: %u\n",
1691 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1693 case MSG_ORDERED_TAG
:
1694 atomic_inc(&dev
->dev_ordered_sync
);
1695 smp_mb__after_atomic_inc();
1697 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1698 " se_ordered_id: %u\n",
1699 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1702 * Execute an ORDERED command if no other older commands
1703 * exist that need to be completed first.
1705 if (!atomic_read(&dev
->simple_cmds
))
1710 * For SIMPLE and UNTAGGED Task Attribute commands
1712 atomic_inc(&dev
->simple_cmds
);
1713 smp_mb__after_atomic_inc();
1717 if (atomic_read(&dev
->dev_ordered_sync
) == 0)
1720 spin_lock(&dev
->delayed_cmd_lock
);
1721 list_add_tail(&cmd
->se_delayed_node
, &dev
->delayed_cmd_list
);
1722 spin_unlock(&dev
->delayed_cmd_lock
);
1724 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1725 " delayed CMD list, se_ordered_id: %u\n",
1726 cmd
->t_task_cdb
[0], cmd
->sam_task_attr
,
1727 cmd
->se_ordered_id
);
1731 void target_execute_cmd(struct se_cmd
*cmd
)
1734 * If the received CDB has aleady been aborted stop processing it here.
1736 if (transport_check_aborted_status(cmd
, 1))
1740 * Determine if frontend context caller is requesting the stopping of
1741 * this command for frontend exceptions.
1743 spin_lock_irq(&cmd
->t_state_lock
);
1744 if (cmd
->transport_state
& CMD_T_STOP
) {
1745 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1747 cmd
->se_tfo
->get_task_tag(cmd
));
1749 spin_unlock_irq(&cmd
->t_state_lock
);
1750 complete(&cmd
->t_transport_stop_comp
);
1754 cmd
->t_state
= TRANSPORT_PROCESSING
;
1755 cmd
->transport_state
|= CMD_T_ACTIVE
|CMD_T_BUSY
|CMD_T_SENT
;
1756 spin_unlock_irq(&cmd
->t_state_lock
);
1758 if (target_handle_task_attr(cmd
)) {
1759 spin_lock_irq(&cmd
->t_state_lock
);
1760 cmd
->transport_state
&= ~CMD_T_BUSY
|CMD_T_SENT
;
1761 spin_unlock_irq(&cmd
->t_state_lock
);
1765 __target_execute_cmd(cmd
);
1767 EXPORT_SYMBOL(target_execute_cmd
);
1770 * Process all commands up to the last received ORDERED task attribute which
1771 * requires another blocking boundary
1773 static void target_restart_delayed_cmds(struct se_device
*dev
)
1778 spin_lock(&dev
->delayed_cmd_lock
);
1779 if (list_empty(&dev
->delayed_cmd_list
)) {
1780 spin_unlock(&dev
->delayed_cmd_lock
);
1784 cmd
= list_entry(dev
->delayed_cmd_list
.next
,
1785 struct se_cmd
, se_delayed_node
);
1786 list_del(&cmd
->se_delayed_node
);
1787 spin_unlock(&dev
->delayed_cmd_lock
);
1789 __target_execute_cmd(cmd
);
1791 if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
)
1797 * Called from I/O completion to determine which dormant/delayed
1798 * and ordered cmds need to have their tasks added to the execution queue.
1800 static void transport_complete_task_attr(struct se_cmd
*cmd
)
1802 struct se_device
*dev
= cmd
->se_dev
;
1804 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1807 if (cmd
->sam_task_attr
== MSG_SIMPLE_TAG
) {
1808 atomic_dec(&dev
->simple_cmds
);
1809 smp_mb__after_atomic_dec();
1810 dev
->dev_cur_ordered_id
++;
1811 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
1812 " SIMPLE: %u\n", dev
->dev_cur_ordered_id
,
1813 cmd
->se_ordered_id
);
1814 } else if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
1815 dev
->dev_cur_ordered_id
++;
1816 pr_debug("Incremented dev_cur_ordered_id: %u for"
1817 " HEAD_OF_QUEUE: %u\n", dev
->dev_cur_ordered_id
,
1818 cmd
->se_ordered_id
);
1819 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
1820 atomic_dec(&dev
->dev_ordered_sync
);
1821 smp_mb__after_atomic_dec();
1823 dev
->dev_cur_ordered_id
++;
1824 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
1825 " %u\n", dev
->dev_cur_ordered_id
, cmd
->se_ordered_id
);
1828 target_restart_delayed_cmds(dev
);
1831 static void transport_complete_qf(struct se_cmd
*cmd
)
1835 transport_complete_task_attr(cmd
);
1837 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
1838 trace_target_cmd_complete(cmd
);
1839 ret
= cmd
->se_tfo
->queue_status(cmd
);
1844 switch (cmd
->data_direction
) {
1845 case DMA_FROM_DEVICE
:
1846 trace_target_cmd_complete(cmd
);
1847 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1850 if (cmd
->se_cmd_flags
& SCF_BIDI
) {
1851 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1855 /* Fall through for DMA_TO_DEVICE */
1857 trace_target_cmd_complete(cmd
);
1858 ret
= cmd
->se_tfo
->queue_status(cmd
);
1866 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1869 transport_lun_remove_cmd(cmd
);
1870 transport_cmd_check_stop_to_fabric(cmd
);
1873 static void transport_handle_queue_full(
1875 struct se_device
*dev
)
1877 spin_lock_irq(&dev
->qf_cmd_lock
);
1878 list_add_tail(&cmd
->se_qf_node
, &cmd
->se_dev
->qf_cmd_list
);
1879 atomic_inc(&dev
->dev_qf_count
);
1880 smp_mb__after_atomic_inc();
1881 spin_unlock_irq(&cmd
->se_dev
->qf_cmd_lock
);
1883 schedule_work(&cmd
->se_dev
->qf_work_queue
);
1886 static void target_complete_ok_work(struct work_struct
*work
)
1888 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
1892 * Check if we need to move delayed/dormant tasks from cmds on the
1893 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1896 transport_complete_task_attr(cmd
);
1899 * Check to schedule QUEUE_FULL work, or execute an existing
1900 * cmd->transport_qf_callback()
1902 if (atomic_read(&cmd
->se_dev
->dev_qf_count
) != 0)
1903 schedule_work(&cmd
->se_dev
->qf_work_queue
);
1906 * Check if we need to send a sense buffer from
1907 * the struct se_cmd in question.
1909 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
1910 WARN_ON(!cmd
->scsi_status
);
1911 ret
= transport_send_check_condition_and_sense(
1913 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1916 transport_lun_remove_cmd(cmd
);
1917 transport_cmd_check_stop_to_fabric(cmd
);
1921 * Check for a callback, used by amongst other things
1922 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
1924 if (cmd
->transport_complete_callback
) {
1927 rc
= cmd
->transport_complete_callback(cmd
);
1928 if (!rc
&& !(cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE_POST
)) {
1931 ret
= transport_send_check_condition_and_sense(cmd
,
1933 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1936 transport_lun_remove_cmd(cmd
);
1937 transport_cmd_check_stop_to_fabric(cmd
);
1942 switch (cmd
->data_direction
) {
1943 case DMA_FROM_DEVICE
:
1944 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1945 if (cmd
->se_lun
->lun_sep
) {
1946 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
1949 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1951 trace_target_cmd_complete(cmd
);
1952 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1953 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1957 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1958 if (cmd
->se_lun
->lun_sep
) {
1959 cmd
->se_lun
->lun_sep
->sep_stats
.rx_data_octets
+=
1962 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1964 * Check if we need to send READ payload for BIDI-COMMAND
1966 if (cmd
->se_cmd_flags
& SCF_BIDI
) {
1967 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1968 if (cmd
->se_lun
->lun_sep
) {
1969 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
1972 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1973 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1974 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1978 /* Fall through for DMA_TO_DEVICE */
1980 trace_target_cmd_complete(cmd
);
1981 ret
= cmd
->se_tfo
->queue_status(cmd
);
1982 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1989 transport_lun_remove_cmd(cmd
);
1990 transport_cmd_check_stop_to_fabric(cmd
);
1994 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
1995 " data_direction: %d\n", cmd
, cmd
->data_direction
);
1996 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
1997 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2000 static inline void transport_free_sgl(struct scatterlist
*sgl
, int nents
)
2002 struct scatterlist
*sg
;
2005 for_each_sg(sgl
, sg
, nents
, count
)
2006 __free_page(sg_page(sg
));
2011 static inline void transport_reset_sgl_orig(struct se_cmd
*cmd
)
2014 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
2015 * emulation, and free + reset pointers if necessary..
2017 if (!cmd
->t_data_sg_orig
)
2020 kfree(cmd
->t_data_sg
);
2021 cmd
->t_data_sg
= cmd
->t_data_sg_orig
;
2022 cmd
->t_data_sg_orig
= NULL
;
2023 cmd
->t_data_nents
= cmd
->t_data_nents_orig
;
2024 cmd
->t_data_nents_orig
= 0;
2027 static inline void transport_free_pages(struct se_cmd
*cmd
)
2029 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) {
2030 transport_reset_sgl_orig(cmd
);
2033 transport_reset_sgl_orig(cmd
);
2035 transport_free_sgl(cmd
->t_data_sg
, cmd
->t_data_nents
);
2036 cmd
->t_data_sg
= NULL
;
2037 cmd
->t_data_nents
= 0;
2039 transport_free_sgl(cmd
->t_bidi_data_sg
, cmd
->t_bidi_data_nents
);
2040 cmd
->t_bidi_data_sg
= NULL
;
2041 cmd
->t_bidi_data_nents
= 0;
2045 * transport_release_cmd - free a command
2046 * @cmd: command to free
2048 * This routine unconditionally frees a command, and reference counting
2049 * or list removal must be done in the caller.
2051 static int transport_release_cmd(struct se_cmd
*cmd
)
2053 BUG_ON(!cmd
->se_tfo
);
2055 if (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
2056 core_tmr_release_req(cmd
->se_tmr_req
);
2057 if (cmd
->t_task_cdb
!= cmd
->__t_task_cdb
)
2058 kfree(cmd
->t_task_cdb
);
2060 * If this cmd has been setup with target_get_sess_cmd(), drop
2061 * the kref and call ->release_cmd() in kref callback.
2063 return target_put_sess_cmd(cmd
->se_sess
, cmd
);
2067 * transport_put_cmd - release a reference to a command
2068 * @cmd: command to release
2070 * This routine releases our reference to the command and frees it if possible.
2072 static int transport_put_cmd(struct se_cmd
*cmd
)
2074 transport_free_pages(cmd
);
2075 return transport_release_cmd(cmd
);
2078 void *transport_kmap_data_sg(struct se_cmd
*cmd
)
2080 struct scatterlist
*sg
= cmd
->t_data_sg
;
2081 struct page
**pages
;
2085 * We need to take into account a possible offset here for fabrics like
2086 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2087 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
2089 if (!cmd
->t_data_nents
)
2093 if (cmd
->t_data_nents
== 1)
2094 return kmap(sg_page(sg
)) + sg
->offset
;
2096 /* >1 page. use vmap */
2097 pages
= kmalloc(sizeof(*pages
) * cmd
->t_data_nents
, GFP_KERNEL
);
2101 /* convert sg[] to pages[] */
2102 for_each_sg(cmd
->t_data_sg
, sg
, cmd
->t_data_nents
, i
) {
2103 pages
[i
] = sg_page(sg
);
2106 cmd
->t_data_vmap
= vmap(pages
, cmd
->t_data_nents
, VM_MAP
, PAGE_KERNEL
);
2108 if (!cmd
->t_data_vmap
)
2111 return cmd
->t_data_vmap
+ cmd
->t_data_sg
[0].offset
;
2113 EXPORT_SYMBOL(transport_kmap_data_sg
);
2115 void transport_kunmap_data_sg(struct se_cmd
*cmd
)
2117 if (!cmd
->t_data_nents
) {
2119 } else if (cmd
->t_data_nents
== 1) {
2120 kunmap(sg_page(cmd
->t_data_sg
));
2124 vunmap(cmd
->t_data_vmap
);
2125 cmd
->t_data_vmap
= NULL
;
2127 EXPORT_SYMBOL(transport_kunmap_data_sg
);
2130 target_alloc_sgl(struct scatterlist
**sgl
, unsigned int *nents
, u32 length
,
2133 struct scatterlist
*sg
;
2135 gfp_t zero_flag
= (zero_page
) ? __GFP_ZERO
: 0;
2139 nent
= DIV_ROUND_UP(length
, PAGE_SIZE
);
2140 sg
= kmalloc(sizeof(struct scatterlist
) * nent
, GFP_KERNEL
);
2144 sg_init_table(sg
, nent
);
2147 u32 page_len
= min_t(u32
, length
, PAGE_SIZE
);
2148 page
= alloc_page(GFP_KERNEL
| zero_flag
);
2152 sg_set_page(&sg
[i
], page
, page_len
, 0);
2163 __free_page(sg_page(&sg
[i
]));
2170 * Allocate any required resources to execute the command. For writes we
2171 * might not have the payload yet, so notify the fabric via a call to
2172 * ->write_pending instead. Otherwise place it on the execution queue.
2175 transport_generic_new_cmd(struct se_cmd
*cmd
)
2180 * Determine is the TCM fabric module has already allocated physical
2181 * memory, and is directly calling transport_generic_map_mem_to_cmd()
2184 if (!(cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) &&
2186 bool zero_flag
= !(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
);
2188 if ((cmd
->se_cmd_flags
& SCF_BIDI
) ||
2189 (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
)) {
2192 if (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
)
2193 bidi_length
= cmd
->t_task_nolb
*
2194 cmd
->se_dev
->dev_attrib
.block_size
;
2196 bidi_length
= cmd
->data_length
;
2198 ret
= target_alloc_sgl(&cmd
->t_bidi_data_sg
,
2199 &cmd
->t_bidi_data_nents
,
2200 bidi_length
, zero_flag
);
2202 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2205 ret
= target_alloc_sgl(&cmd
->t_data_sg
, &cmd
->t_data_nents
,
2206 cmd
->data_length
, zero_flag
);
2208 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2211 * If this command is not a write we can execute it right here,
2212 * for write buffers we need to notify the fabric driver first
2213 * and let it call back once the write buffers are ready.
2215 target_add_to_state_list(cmd
);
2216 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
2217 target_execute_cmd(cmd
);
2220 transport_cmd_check_stop(cmd
, false, true);
2222 ret
= cmd
->se_tfo
->write_pending(cmd
);
2223 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2226 /* fabric drivers should only return -EAGAIN or -ENOMEM as error */
2229 return (!ret
) ? 0 : TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2232 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd
);
2233 cmd
->t_state
= TRANSPORT_COMPLETE_QF_WP
;
2234 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2237 EXPORT_SYMBOL(transport_generic_new_cmd
);
2239 static void transport_write_pending_qf(struct se_cmd
*cmd
)
2243 ret
= cmd
->se_tfo
->write_pending(cmd
);
2244 if (ret
== -EAGAIN
|| ret
== -ENOMEM
) {
2245 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2247 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2251 int transport_generic_free_cmd(struct se_cmd
*cmd
, int wait_for_tasks
)
2253 unsigned long flags
;
2256 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
)) {
2257 if (wait_for_tasks
&& (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
))
2258 transport_wait_for_tasks(cmd
);
2260 ret
= transport_release_cmd(cmd
);
2263 transport_wait_for_tasks(cmd
);
2265 * Handle WRITE failure case where transport_generic_new_cmd()
2266 * has already added se_cmd to state_list, but fabric has
2267 * failed command before I/O submission.
2269 if (cmd
->state_active
) {
2270 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2271 target_remove_from_state_list(cmd
);
2272 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2276 transport_lun_remove_cmd(cmd
);
2278 ret
= transport_put_cmd(cmd
);
2282 EXPORT_SYMBOL(transport_generic_free_cmd
);
2284 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
2285 * @se_sess: session to reference
2286 * @se_cmd: command descriptor to add
2287 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2289 int target_get_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
,
2292 unsigned long flags
;
2295 kref_init(&se_cmd
->cmd_kref
);
2297 * Add a second kref if the fabric caller is expecting to handle
2298 * fabric acknowledgement that requires two target_put_sess_cmd()
2299 * invocations before se_cmd descriptor release.
2301 if (ack_kref
== true) {
2302 kref_get(&se_cmd
->cmd_kref
);
2303 se_cmd
->se_cmd_flags
|= SCF_ACK_KREF
;
2306 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2307 if (se_sess
->sess_tearing_down
) {
2311 list_add_tail(&se_cmd
->se_cmd_list
, &se_sess
->sess_cmd_list
);
2313 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2316 EXPORT_SYMBOL(target_get_sess_cmd
);
2318 static void target_release_cmd_kref(struct kref
*kref
)
2320 struct se_cmd
*se_cmd
= container_of(kref
, struct se_cmd
, cmd_kref
);
2321 struct se_session
*se_sess
= se_cmd
->se_sess
;
2323 if (list_empty(&se_cmd
->se_cmd_list
)) {
2324 spin_unlock(&se_sess
->sess_cmd_lock
);
2325 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2328 if (se_sess
->sess_tearing_down
&& se_cmd
->cmd_wait_set
) {
2329 spin_unlock(&se_sess
->sess_cmd_lock
);
2330 complete(&se_cmd
->cmd_wait_comp
);
2333 list_del(&se_cmd
->se_cmd_list
);
2334 spin_unlock(&se_sess
->sess_cmd_lock
);
2336 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2339 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2340 * @se_sess: session to reference
2341 * @se_cmd: command descriptor to drop
2343 int target_put_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
)
2345 return kref_put_spinlock_irqsave(&se_cmd
->cmd_kref
, target_release_cmd_kref
,
2346 &se_sess
->sess_cmd_lock
);
2348 EXPORT_SYMBOL(target_put_sess_cmd
);
2350 /* target_sess_cmd_list_set_waiting - Flag all commands in
2351 * sess_cmd_list to complete cmd_wait_comp. Set
2352 * sess_tearing_down so no more commands are queued.
2353 * @se_sess: session to flag
2355 void target_sess_cmd_list_set_waiting(struct se_session
*se_sess
)
2357 struct se_cmd
*se_cmd
;
2358 unsigned long flags
;
2360 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2361 if (se_sess
->sess_tearing_down
) {
2362 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2365 se_sess
->sess_tearing_down
= 1;
2366 list_splice_init(&se_sess
->sess_cmd_list
, &se_sess
->sess_wait_list
);
2368 list_for_each_entry(se_cmd
, &se_sess
->sess_wait_list
, se_cmd_list
)
2369 se_cmd
->cmd_wait_set
= 1;
2371 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2373 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting
);
2375 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
2376 * @se_sess: session to wait for active I/O
2378 void target_wait_for_sess_cmds(struct se_session
*se_sess
)
2380 struct se_cmd
*se_cmd
, *tmp_cmd
;
2381 unsigned long flags
;
2383 list_for_each_entry_safe(se_cmd
, tmp_cmd
,
2384 &se_sess
->sess_wait_list
, se_cmd_list
) {
2385 list_del(&se_cmd
->se_cmd_list
);
2387 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2388 " %d\n", se_cmd
, se_cmd
->t_state
,
2389 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2391 wait_for_completion(&se_cmd
->cmd_wait_comp
);
2392 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2393 " fabric state: %d\n", se_cmd
, se_cmd
->t_state
,
2394 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2396 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2399 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2400 WARN_ON(!list_empty(&se_sess
->sess_cmd_list
));
2401 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2404 EXPORT_SYMBOL(target_wait_for_sess_cmds
);
2406 static int transport_clear_lun_ref_thread(void *p
)
2408 struct se_lun
*lun
= p
;
2410 percpu_ref_kill(&lun
->lun_ref
);
2412 wait_for_completion(&lun
->lun_ref_comp
);
2413 complete(&lun
->lun_shutdown_comp
);
2418 int transport_clear_lun_ref(struct se_lun
*lun
)
2420 struct task_struct
*kt
;
2422 kt
= kthread_run(transport_clear_lun_ref_thread
, lun
,
2423 "tcm_cl_%u", lun
->unpacked_lun
);
2425 pr_err("Unable to start clear_lun thread\n");
2428 wait_for_completion(&lun
->lun_shutdown_comp
);
2434 * transport_wait_for_tasks - wait for completion to occur
2435 * @cmd: command to wait
2437 * Called from frontend fabric context to wait for storage engine
2438 * to pause and/or release frontend generated struct se_cmd.
2440 bool transport_wait_for_tasks(struct se_cmd
*cmd
)
2442 unsigned long flags
;
2444 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2445 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
) &&
2446 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2447 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2451 if (!(cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
) &&
2452 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2453 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2457 if (!(cmd
->transport_state
& CMD_T_ACTIVE
)) {
2458 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2462 cmd
->transport_state
|= CMD_T_STOP
;
2464 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
2465 " i_state: %d, t_state: %d, CMD_T_STOP\n",
2466 cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
2467 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
2469 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2471 wait_for_completion(&cmd
->t_transport_stop_comp
);
2473 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2474 cmd
->transport_state
&= ~(CMD_T_ACTIVE
| CMD_T_STOP
);
2476 pr_debug("wait_for_tasks: Stopped wait_for_completion("
2477 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
2478 cmd
->se_tfo
->get_task_tag(cmd
));
2480 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2484 EXPORT_SYMBOL(transport_wait_for_tasks
);
2486 static int transport_get_sense_codes(
2491 *asc
= cmd
->scsi_asc
;
2492 *ascq
= cmd
->scsi_ascq
;
2498 void transport_err_sector_info(unsigned char *buffer
, sector_t bad_sector
)
2500 /* Place failed LBA in sense data information descriptor 0. */
2501 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 0xc;
2502 buffer
[SPC_DESC_TYPE_OFFSET
] = 0; /* Information */
2503 buffer
[SPC_ADDITIONAL_DESC_LEN_OFFSET
] = 0xa;
2504 buffer
[SPC_VALIDITY_OFFSET
] = 0x80;
2506 /* Descriptor Information: failing sector */
2507 put_unaligned_be64(bad_sector
, &buffer
[12]);
2511 transport_send_check_condition_and_sense(struct se_cmd
*cmd
,
2512 sense_reason_t reason
, int from_transport
)
2514 unsigned char *buffer
= cmd
->sense_buffer
;
2515 unsigned long flags
;
2516 u8 asc
= 0, ascq
= 0;
2518 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2519 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
2520 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2523 cmd
->se_cmd_flags
|= SCF_SENT_CHECK_CONDITION
;
2524 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2526 if (!reason
&& from_transport
)
2529 if (!from_transport
)
2530 cmd
->se_cmd_flags
|= SCF_EMULATED_TASK_SENSE
;
2533 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2534 * SENSE KEY values from include/scsi/scsi.h
2540 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2542 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2543 /* NO ADDITIONAL SENSE INFORMATION */
2544 buffer
[SPC_ASC_KEY_OFFSET
] = 0;
2545 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0;
2547 case TCM_NON_EXISTENT_LUN
:
2550 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2551 /* ILLEGAL REQUEST */
2552 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2553 /* LOGICAL UNIT NOT SUPPORTED */
2554 buffer
[SPC_ASC_KEY_OFFSET
] = 0x25;
2556 case TCM_UNSUPPORTED_SCSI_OPCODE
:
2557 case TCM_SECTOR_COUNT_TOO_MANY
:
2560 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2561 /* ILLEGAL REQUEST */
2562 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2563 /* INVALID COMMAND OPERATION CODE */
2564 buffer
[SPC_ASC_KEY_OFFSET
] = 0x20;
2566 case TCM_UNKNOWN_MODE_PAGE
:
2569 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2570 /* ILLEGAL REQUEST */
2571 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2572 /* INVALID FIELD IN CDB */
2573 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2575 case TCM_CHECK_CONDITION_ABORT_CMD
:
2578 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2579 /* ABORTED COMMAND */
2580 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2581 /* BUS DEVICE RESET FUNCTION OCCURRED */
2582 buffer
[SPC_ASC_KEY_OFFSET
] = 0x29;
2583 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x03;
2585 case TCM_INCORRECT_AMOUNT_OF_DATA
:
2588 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2589 /* ABORTED COMMAND */
2590 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2592 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2593 /* NOT ENOUGH UNSOLICITED DATA */
2594 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0d;
2596 case TCM_INVALID_CDB_FIELD
:
2599 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2600 /* ILLEGAL REQUEST */
2601 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2602 /* INVALID FIELD IN CDB */
2603 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2605 case TCM_INVALID_PARAMETER_LIST
:
2608 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2609 /* ILLEGAL REQUEST */
2610 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2611 /* INVALID FIELD IN PARAMETER LIST */
2612 buffer
[SPC_ASC_KEY_OFFSET
] = 0x26;
2614 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
2617 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2618 /* ILLEGAL REQUEST */
2619 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2620 /* PARAMETER LIST LENGTH ERROR */
2621 buffer
[SPC_ASC_KEY_OFFSET
] = 0x1a;
2623 case TCM_UNEXPECTED_UNSOLICITED_DATA
:
2626 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2627 /* ABORTED COMMAND */
2628 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2630 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2631 /* UNEXPECTED_UNSOLICITED_DATA */
2632 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0c;
2634 case TCM_SERVICE_CRC_ERROR
:
2637 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2638 /* ABORTED COMMAND */
2639 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2640 /* PROTOCOL SERVICE CRC ERROR */
2641 buffer
[SPC_ASC_KEY_OFFSET
] = 0x47;
2643 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x05;
2645 case TCM_SNACK_REJECTED
:
2648 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2649 /* ABORTED COMMAND */
2650 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2652 buffer
[SPC_ASC_KEY_OFFSET
] = 0x11;
2653 /* FAILED RETRANSMISSION REQUEST */
2654 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x13;
2656 case TCM_WRITE_PROTECTED
:
2659 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2661 buffer
[SPC_SENSE_KEY_OFFSET
] = DATA_PROTECT
;
2662 /* WRITE PROTECTED */
2663 buffer
[SPC_ASC_KEY_OFFSET
] = 0x27;
2665 case TCM_ADDRESS_OUT_OF_RANGE
:
2668 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2669 /* ILLEGAL REQUEST */
2670 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2671 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2672 buffer
[SPC_ASC_KEY_OFFSET
] = 0x21;
2674 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
2677 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2678 /* UNIT ATTENTION */
2679 buffer
[SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
2680 core_scsi3_ua_for_check_condition(cmd
, &asc
, &ascq
);
2681 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2682 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2684 case TCM_CHECK_CONDITION_NOT_READY
:
2687 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2689 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2690 transport_get_sense_codes(cmd
, &asc
, &ascq
);
2691 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2692 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2694 case TCM_MISCOMPARE_VERIFY
:
2697 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2698 buffer
[SPC_SENSE_KEY_OFFSET
] = MISCOMPARE
;
2699 /* MISCOMPARE DURING VERIFY OPERATION */
2700 buffer
[SPC_ASC_KEY_OFFSET
] = 0x1d;
2701 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x00;
2703 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED
:
2706 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2707 /* ILLEGAL REQUEST */
2708 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2709 /* LOGICAL BLOCK GUARD CHECK FAILED */
2710 buffer
[SPC_ASC_KEY_OFFSET
] = 0x10;
2711 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x01;
2712 transport_err_sector_info(buffer
, cmd
->bad_sector
);
2714 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED
:
2717 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2718 /* ILLEGAL REQUEST */
2719 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2720 /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */
2721 buffer
[SPC_ASC_KEY_OFFSET
] = 0x10;
2722 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x02;
2723 transport_err_sector_info(buffer
, cmd
->bad_sector
);
2725 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED
:
2728 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2729 /* ILLEGAL REQUEST */
2730 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2731 /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */
2732 buffer
[SPC_ASC_KEY_OFFSET
] = 0x10;
2733 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x03;
2734 transport_err_sector_info(buffer
, cmd
->bad_sector
);
2736 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
2740 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2742 * Returning ILLEGAL REQUEST would cause immediate IO errors on
2743 * Solaris initiators. Returning NOT READY instead means the
2744 * operations will be retried a finite number of times and we
2745 * can survive intermittent errors.
2747 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2748 /* LOGICAL UNIT COMMUNICATION FAILURE */
2749 buffer
[SPC_ASC_KEY_OFFSET
] = 0x08;
2753 * This code uses linux/include/scsi/scsi.h SAM status codes!
2755 cmd
->scsi_status
= SAM_STAT_CHECK_CONDITION
;
2757 * Automatically padded, this value is encoded in the fabric's
2758 * data_length response PDU containing the SCSI defined sense data.
2760 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
2763 trace_target_cmd_complete(cmd
);
2764 return cmd
->se_tfo
->queue_status(cmd
);
2766 EXPORT_SYMBOL(transport_send_check_condition_and_sense
);
2768 int transport_check_aborted_status(struct se_cmd
*cmd
, int send_status
)
2770 if (!(cmd
->transport_state
& CMD_T_ABORTED
))
2773 if (!send_status
|| (cmd
->se_cmd_flags
& SCF_SENT_DELAYED_TAS
))
2776 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB: 0x%02x ITT: 0x%08x\n",
2777 cmd
->t_task_cdb
[0], cmd
->se_tfo
->get_task_tag(cmd
));
2779 cmd
->se_cmd_flags
|= SCF_SENT_DELAYED_TAS
;
2780 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
2781 trace_target_cmd_complete(cmd
);
2782 cmd
->se_tfo
->queue_status(cmd
);
2786 EXPORT_SYMBOL(transport_check_aborted_status
);
2788 void transport_send_task_abort(struct se_cmd
*cmd
)
2790 unsigned long flags
;
2792 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2793 if (cmd
->se_cmd_flags
& (SCF_SENT_CHECK_CONDITION
| SCF_SENT_DELAYED_TAS
)) {
2794 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2797 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2800 * If there are still expected incoming fabric WRITEs, we wait
2801 * until until they have completed before sending a TASK_ABORTED
2802 * response. This response with TASK_ABORTED status will be
2803 * queued back to fabric module by transport_check_aborted_status().
2805 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
2806 if (cmd
->se_tfo
->write_pending_status(cmd
) != 0) {
2807 cmd
->transport_state
|= CMD_T_ABORTED
;
2808 smp_mb__after_atomic_inc();
2812 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
2814 transport_lun_remove_cmd(cmd
);
2816 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
2817 " ITT: 0x%08x\n", cmd
->t_task_cdb
[0],
2818 cmd
->se_tfo
->get_task_tag(cmd
));
2820 trace_target_cmd_complete(cmd
);
2821 cmd
->se_tfo
->queue_status(cmd
);
2824 static void target_tmr_work(struct work_struct
*work
)
2826 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
2827 struct se_device
*dev
= cmd
->se_dev
;
2828 struct se_tmr_req
*tmr
= cmd
->se_tmr_req
;
2831 switch (tmr
->function
) {
2832 case TMR_ABORT_TASK
:
2833 core_tmr_abort_task(dev
, tmr
, cmd
->se_sess
);
2835 case TMR_ABORT_TASK_SET
:
2837 case TMR_CLEAR_TASK_SET
:
2838 tmr
->response
= TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
2841 ret
= core_tmr_lun_reset(dev
, tmr
, NULL
, NULL
);
2842 tmr
->response
= (!ret
) ? TMR_FUNCTION_COMPLETE
:
2843 TMR_FUNCTION_REJECTED
;
2845 case TMR_TARGET_WARM_RESET
:
2846 tmr
->response
= TMR_FUNCTION_REJECTED
;
2848 case TMR_TARGET_COLD_RESET
:
2849 tmr
->response
= TMR_FUNCTION_REJECTED
;
2852 pr_err("Uknown TMR function: 0x%02x.\n",
2854 tmr
->response
= TMR_FUNCTION_REJECTED
;
2858 cmd
->t_state
= TRANSPORT_ISTATE_PROCESSING
;
2859 cmd
->se_tfo
->queue_tm_rsp(cmd
);
2861 transport_cmd_check_stop_to_fabric(cmd
);
2864 int transport_generic_handle_tmr(
2867 INIT_WORK(&cmd
->work
, target_tmr_work
);
2868 queue_work(cmd
->se_dev
->tmr_wq
, &cmd
->work
);
2871 EXPORT_SYMBOL(transport_generic_handle_tmr
);