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/blkdev.h>
32 #include <linux/spinlock.h>
33 #include <linux/kthread.h>
35 #include <linux/cdrom.h>
36 #include <linux/module.h>
37 #include <linux/ratelimit.h>
38 #include <asm/unaligned.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_tcq.h>
45 #include <target/target_core_base.h>
46 #include <target/target_core_backend.h>
47 #include <target/target_core_fabric.h>
48 #include <target/target_core_configfs.h>
50 #include "target_core_internal.h"
51 #include "target_core_alua.h"
52 #include "target_core_pr.h"
53 #include "target_core_ua.h"
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/target.h>
58 static struct workqueue_struct
*target_completion_wq
;
59 static struct kmem_cache
*se_sess_cache
;
60 struct kmem_cache
*se_ua_cache
;
61 struct kmem_cache
*t10_pr_reg_cache
;
62 struct kmem_cache
*t10_alua_lu_gp_cache
;
63 struct kmem_cache
*t10_alua_lu_gp_mem_cache
;
64 struct kmem_cache
*t10_alua_tg_pt_gp_cache
;
65 struct kmem_cache
*t10_alua_tg_pt_gp_mem_cache
;
67 static void transport_complete_task_attr(struct se_cmd
*cmd
);
68 static void transport_handle_queue_full(struct se_cmd
*cmd
,
69 struct se_device
*dev
);
70 static int transport_put_cmd(struct se_cmd
*cmd
);
71 static void target_complete_ok_work(struct work_struct
*work
);
73 int init_se_kmem_caches(void)
75 se_sess_cache
= kmem_cache_create("se_sess_cache",
76 sizeof(struct se_session
), __alignof__(struct se_session
),
79 pr_err("kmem_cache_create() for struct se_session"
83 se_ua_cache
= kmem_cache_create("se_ua_cache",
84 sizeof(struct se_ua
), __alignof__(struct se_ua
),
87 pr_err("kmem_cache_create() for struct se_ua failed\n");
88 goto out_free_sess_cache
;
90 t10_pr_reg_cache
= kmem_cache_create("t10_pr_reg_cache",
91 sizeof(struct t10_pr_registration
),
92 __alignof__(struct t10_pr_registration
), 0, NULL
);
93 if (!t10_pr_reg_cache
) {
94 pr_err("kmem_cache_create() for struct t10_pr_registration"
96 goto out_free_ua_cache
;
98 t10_alua_lu_gp_cache
= kmem_cache_create("t10_alua_lu_gp_cache",
99 sizeof(struct t10_alua_lu_gp
), __alignof__(struct t10_alua_lu_gp
),
101 if (!t10_alua_lu_gp_cache
) {
102 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
104 goto out_free_pr_reg_cache
;
106 t10_alua_lu_gp_mem_cache
= kmem_cache_create("t10_alua_lu_gp_mem_cache",
107 sizeof(struct t10_alua_lu_gp_member
),
108 __alignof__(struct t10_alua_lu_gp_member
), 0, NULL
);
109 if (!t10_alua_lu_gp_mem_cache
) {
110 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
112 goto out_free_lu_gp_cache
;
114 t10_alua_tg_pt_gp_cache
= kmem_cache_create("t10_alua_tg_pt_gp_cache",
115 sizeof(struct t10_alua_tg_pt_gp
),
116 __alignof__(struct t10_alua_tg_pt_gp
), 0, NULL
);
117 if (!t10_alua_tg_pt_gp_cache
) {
118 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
120 goto out_free_lu_gp_mem_cache
;
122 t10_alua_tg_pt_gp_mem_cache
= kmem_cache_create(
123 "t10_alua_tg_pt_gp_mem_cache",
124 sizeof(struct t10_alua_tg_pt_gp_member
),
125 __alignof__(struct t10_alua_tg_pt_gp_member
),
127 if (!t10_alua_tg_pt_gp_mem_cache
) {
128 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
130 goto out_free_tg_pt_gp_cache
;
133 target_completion_wq
= alloc_workqueue("target_completion",
135 if (!target_completion_wq
)
136 goto out_free_tg_pt_gp_mem_cache
;
140 out_free_tg_pt_gp_mem_cache
:
141 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
142 out_free_tg_pt_gp_cache
:
143 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
144 out_free_lu_gp_mem_cache
:
145 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
146 out_free_lu_gp_cache
:
147 kmem_cache_destroy(t10_alua_lu_gp_cache
);
148 out_free_pr_reg_cache
:
149 kmem_cache_destroy(t10_pr_reg_cache
);
151 kmem_cache_destroy(se_ua_cache
);
153 kmem_cache_destroy(se_sess_cache
);
158 void release_se_kmem_caches(void)
160 destroy_workqueue(target_completion_wq
);
161 kmem_cache_destroy(se_sess_cache
);
162 kmem_cache_destroy(se_ua_cache
);
163 kmem_cache_destroy(t10_pr_reg_cache
);
164 kmem_cache_destroy(t10_alua_lu_gp_cache
);
165 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
166 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
167 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
170 /* This code ensures unique mib indexes are handed out. */
171 static DEFINE_SPINLOCK(scsi_mib_index_lock
);
172 static u32 scsi_mib_index
[SCSI_INDEX_TYPE_MAX
];
175 * Allocate a new row index for the entry type specified
177 u32
scsi_get_new_index(scsi_index_t type
)
181 BUG_ON((type
< 0) || (type
>= SCSI_INDEX_TYPE_MAX
));
183 spin_lock(&scsi_mib_index_lock
);
184 new_index
= ++scsi_mib_index
[type
];
185 spin_unlock(&scsi_mib_index_lock
);
190 void transport_subsystem_check_init(void)
193 static int sub_api_initialized
;
195 if (sub_api_initialized
)
198 ret
= request_module("target_core_iblock");
200 pr_err("Unable to load target_core_iblock\n");
202 ret
= request_module("target_core_file");
204 pr_err("Unable to load target_core_file\n");
206 ret
= request_module("target_core_pscsi");
208 pr_err("Unable to load target_core_pscsi\n");
210 sub_api_initialized
= 1;
213 struct se_session
*transport_init_session(void)
215 struct se_session
*se_sess
;
217 se_sess
= kmem_cache_zalloc(se_sess_cache
, GFP_KERNEL
);
219 pr_err("Unable to allocate struct se_session from"
221 return ERR_PTR(-ENOMEM
);
223 INIT_LIST_HEAD(&se_sess
->sess_list
);
224 INIT_LIST_HEAD(&se_sess
->sess_acl_list
);
225 INIT_LIST_HEAD(&se_sess
->sess_cmd_list
);
226 INIT_LIST_HEAD(&se_sess
->sess_wait_list
);
227 spin_lock_init(&se_sess
->sess_cmd_lock
);
228 kref_init(&se_sess
->sess_kref
);
232 EXPORT_SYMBOL(transport_init_session
);
234 int transport_alloc_session_tags(struct se_session
*se_sess
,
235 unsigned int tag_num
, unsigned int tag_size
)
239 se_sess
->sess_cmd_map
= kzalloc(tag_num
* tag_size
,
240 GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
241 if (!se_sess
->sess_cmd_map
) {
242 se_sess
->sess_cmd_map
= vzalloc(tag_num
* tag_size
);
243 if (!se_sess
->sess_cmd_map
) {
244 pr_err("Unable to allocate se_sess->sess_cmd_map\n");
249 rc
= percpu_ida_init(&se_sess
->sess_tag_pool
, tag_num
);
251 pr_err("Unable to init se_sess->sess_tag_pool,"
252 " tag_num: %u\n", tag_num
);
253 if (is_vmalloc_addr(se_sess
->sess_cmd_map
))
254 vfree(se_sess
->sess_cmd_map
);
256 kfree(se_sess
->sess_cmd_map
);
257 se_sess
->sess_cmd_map
= NULL
;
263 EXPORT_SYMBOL(transport_alloc_session_tags
);
265 struct se_session
*transport_init_session_tags(unsigned int tag_num
,
266 unsigned int tag_size
)
268 struct se_session
*se_sess
;
271 se_sess
= transport_init_session();
275 rc
= transport_alloc_session_tags(se_sess
, tag_num
, tag_size
);
277 transport_free_session(se_sess
);
278 return ERR_PTR(-ENOMEM
);
283 EXPORT_SYMBOL(transport_init_session_tags
);
286 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
288 void __transport_register_session(
289 struct se_portal_group
*se_tpg
,
290 struct se_node_acl
*se_nacl
,
291 struct se_session
*se_sess
,
292 void *fabric_sess_ptr
)
294 unsigned char buf
[PR_REG_ISID_LEN
];
296 se_sess
->se_tpg
= se_tpg
;
297 se_sess
->fabric_sess_ptr
= fabric_sess_ptr
;
299 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
301 * Only set for struct se_session's that will actually be moving I/O.
302 * eg: *NOT* discovery sessions.
306 * If the fabric module supports an ISID based TransportID,
307 * save this value in binary from the fabric I_T Nexus now.
309 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
310 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
311 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
,
312 &buf
[0], PR_REG_ISID_LEN
);
313 se_sess
->sess_bin_isid
= get_unaligned_be64(&buf
[0]);
315 kref_get(&se_nacl
->acl_kref
);
317 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
319 * The se_nacl->nacl_sess pointer will be set to the
320 * last active I_T Nexus for each struct se_node_acl.
322 se_nacl
->nacl_sess
= se_sess
;
324 list_add_tail(&se_sess
->sess_acl_list
,
325 &se_nacl
->acl_sess_list
);
326 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
328 list_add_tail(&se_sess
->sess_list
, &se_tpg
->tpg_sess_list
);
330 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
331 se_tpg
->se_tpg_tfo
->get_fabric_name(), se_sess
->fabric_sess_ptr
);
333 EXPORT_SYMBOL(__transport_register_session
);
335 void transport_register_session(
336 struct se_portal_group
*se_tpg
,
337 struct se_node_acl
*se_nacl
,
338 struct se_session
*se_sess
,
339 void *fabric_sess_ptr
)
343 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
344 __transport_register_session(se_tpg
, se_nacl
, se_sess
, fabric_sess_ptr
);
345 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
347 EXPORT_SYMBOL(transport_register_session
);
349 static void target_release_session(struct kref
*kref
)
351 struct se_session
*se_sess
= container_of(kref
,
352 struct se_session
, sess_kref
);
353 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
355 se_tpg
->se_tpg_tfo
->close_session(se_sess
);
358 void target_get_session(struct se_session
*se_sess
)
360 kref_get(&se_sess
->sess_kref
);
362 EXPORT_SYMBOL(target_get_session
);
364 void target_put_session(struct se_session
*se_sess
)
366 struct se_portal_group
*tpg
= se_sess
->se_tpg
;
368 if (tpg
->se_tpg_tfo
->put_session
!= NULL
) {
369 tpg
->se_tpg_tfo
->put_session(se_sess
);
372 kref_put(&se_sess
->sess_kref
, target_release_session
);
374 EXPORT_SYMBOL(target_put_session
);
376 static void target_complete_nacl(struct kref
*kref
)
378 struct se_node_acl
*nacl
= container_of(kref
,
379 struct se_node_acl
, acl_kref
);
381 complete(&nacl
->acl_free_comp
);
384 void target_put_nacl(struct se_node_acl
*nacl
)
386 kref_put(&nacl
->acl_kref
, target_complete_nacl
);
389 void transport_deregister_session_configfs(struct se_session
*se_sess
)
391 struct se_node_acl
*se_nacl
;
394 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
396 se_nacl
= se_sess
->se_node_acl
;
398 spin_lock_irqsave(&se_nacl
->nacl_sess_lock
, flags
);
399 if (se_nacl
->acl_stop
== 0)
400 list_del(&se_sess
->sess_acl_list
);
402 * If the session list is empty, then clear the pointer.
403 * Otherwise, set the struct se_session pointer from the tail
404 * element of the per struct se_node_acl active session list.
406 if (list_empty(&se_nacl
->acl_sess_list
))
407 se_nacl
->nacl_sess
= NULL
;
409 se_nacl
->nacl_sess
= container_of(
410 se_nacl
->acl_sess_list
.prev
,
411 struct se_session
, sess_acl_list
);
413 spin_unlock_irqrestore(&se_nacl
->nacl_sess_lock
, flags
);
416 EXPORT_SYMBOL(transport_deregister_session_configfs
);
418 void transport_free_session(struct se_session
*se_sess
)
420 if (se_sess
->sess_cmd_map
) {
421 percpu_ida_destroy(&se_sess
->sess_tag_pool
);
422 if (is_vmalloc_addr(se_sess
->sess_cmd_map
))
423 vfree(se_sess
->sess_cmd_map
);
425 kfree(se_sess
->sess_cmd_map
);
427 kmem_cache_free(se_sess_cache
, se_sess
);
429 EXPORT_SYMBOL(transport_free_session
);
431 void transport_deregister_session(struct se_session
*se_sess
)
433 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
434 struct target_core_fabric_ops
*se_tfo
;
435 struct se_node_acl
*se_nacl
;
437 bool comp_nacl
= true;
440 transport_free_session(se_sess
);
443 se_tfo
= se_tpg
->se_tpg_tfo
;
445 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
446 list_del(&se_sess
->sess_list
);
447 se_sess
->se_tpg
= NULL
;
448 se_sess
->fabric_sess_ptr
= NULL
;
449 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
452 * Determine if we need to do extra work for this initiator node's
453 * struct se_node_acl if it had been previously dynamically generated.
455 se_nacl
= se_sess
->se_node_acl
;
457 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
458 if (se_nacl
&& se_nacl
->dynamic_node_acl
) {
459 if (!se_tfo
->tpg_check_demo_mode_cache(se_tpg
)) {
460 list_del(&se_nacl
->acl_list
);
461 se_tpg
->num_node_acls
--;
462 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
463 core_tpg_wait_for_nacl_pr_ref(se_nacl
);
464 core_free_device_list_for_node(se_nacl
, se_tpg
);
465 se_tfo
->tpg_release_fabric_acl(se_tpg
, se_nacl
);
468 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
471 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
473 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
474 se_tpg
->se_tpg_tfo
->get_fabric_name());
476 * If last kref is dropping now for an explict NodeACL, awake sleeping
477 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
480 if (se_nacl
&& comp_nacl
== true)
481 target_put_nacl(se_nacl
);
483 transport_free_session(se_sess
);
485 EXPORT_SYMBOL(transport_deregister_session
);
488 * Called with cmd->t_state_lock held.
490 static void target_remove_from_state_list(struct se_cmd
*cmd
)
492 struct se_device
*dev
= cmd
->se_dev
;
498 if (cmd
->transport_state
& CMD_T_BUSY
)
501 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
502 if (cmd
->state_active
) {
503 list_del(&cmd
->state_list
);
504 cmd
->state_active
= false;
506 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
509 static int transport_cmd_check_stop(struct se_cmd
*cmd
, bool remove_from_lists
,
514 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
516 cmd
->t_state
= TRANSPORT_WRITE_PENDING
;
519 * Determine if IOCTL context caller in requesting the stopping of this
520 * command for LUN shutdown purposes.
522 if (cmd
->transport_state
& CMD_T_LUN_STOP
) {
523 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
524 __func__
, __LINE__
, cmd
->se_tfo
->get_task_tag(cmd
));
526 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
527 if (remove_from_lists
)
528 target_remove_from_state_list(cmd
);
529 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
531 complete(&cmd
->transport_lun_stop_comp
);
535 if (remove_from_lists
) {
536 target_remove_from_state_list(cmd
);
539 * Clear struct se_cmd->se_lun before the handoff to FE.
545 * Determine if frontend context caller is requesting the stopping of
546 * this command for frontend exceptions.
548 if (cmd
->transport_state
& CMD_T_STOP
) {
549 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
551 cmd
->se_tfo
->get_task_tag(cmd
));
553 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
555 complete_all(&cmd
->t_transport_stop_comp
);
559 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
560 if (remove_from_lists
) {
562 * Some fabric modules like tcm_loop can release
563 * their internally allocated I/O reference now and
566 * Fabric modules are expected to return '1' here if the
567 * se_cmd being passed is released at this point,
568 * or zero if not being released.
570 if (cmd
->se_tfo
->check_stop_free
!= NULL
) {
571 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
572 return cmd
->se_tfo
->check_stop_free(cmd
);
576 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
580 static int transport_cmd_check_stop_to_fabric(struct se_cmd
*cmd
)
582 return transport_cmd_check_stop(cmd
, true, false);
585 static void transport_lun_remove_cmd(struct se_cmd
*cmd
)
587 struct se_lun
*lun
= cmd
->se_lun
;
593 spin_lock_irqsave(&lun
->lun_cmd_lock
, flags
);
594 if (!list_empty(&cmd
->se_lun_node
))
595 list_del_init(&cmd
->se_lun_node
);
596 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, flags
);
599 void transport_cmd_finish_abort(struct se_cmd
*cmd
, int remove
)
601 if (transport_cmd_check_stop_to_fabric(cmd
))
604 transport_put_cmd(cmd
);
607 static void target_complete_failure_work(struct work_struct
*work
)
609 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
611 transport_generic_request_failure(cmd
,
612 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
);
616 * Used when asking transport to copy Sense Data from the underlying
617 * Linux/SCSI struct scsi_cmnd
619 static unsigned char *transport_get_sense_buffer(struct se_cmd
*cmd
)
621 struct se_device
*dev
= cmd
->se_dev
;
623 WARN_ON(!cmd
->se_lun
);
628 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
)
631 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
633 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
634 dev
->se_hba
->hba_id
, dev
->transport
->name
, cmd
->scsi_status
);
635 return cmd
->sense_buffer
;
638 void target_complete_cmd(struct se_cmd
*cmd
, u8 scsi_status
)
640 struct se_device
*dev
= cmd
->se_dev
;
641 int success
= scsi_status
== GOOD
;
644 cmd
->scsi_status
= scsi_status
;
647 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
648 cmd
->transport_state
&= ~CMD_T_BUSY
;
650 if (dev
&& dev
->transport
->transport_complete
) {
651 dev
->transport
->transport_complete(cmd
,
653 transport_get_sense_buffer(cmd
));
654 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
)
659 * See if we are waiting to complete for an exception condition.
661 if (cmd
->transport_state
& CMD_T_REQUEST_STOP
) {
662 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
663 complete(&cmd
->task_stop_comp
);
668 cmd
->transport_state
|= CMD_T_FAILED
;
671 * Check for case where an explict ABORT_TASK has been received
672 * and transport_wait_for_tasks() will be waiting for completion..
674 if (cmd
->transport_state
& CMD_T_ABORTED
&&
675 cmd
->transport_state
& CMD_T_STOP
) {
676 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
677 complete_all(&cmd
->t_transport_stop_comp
);
679 } else if (cmd
->transport_state
& CMD_T_FAILED
) {
680 INIT_WORK(&cmd
->work
, target_complete_failure_work
);
682 INIT_WORK(&cmd
->work
, target_complete_ok_work
);
685 cmd
->t_state
= TRANSPORT_COMPLETE
;
686 cmd
->transport_state
|= (CMD_T_COMPLETE
| CMD_T_ACTIVE
);
687 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
689 queue_work(target_completion_wq
, &cmd
->work
);
691 EXPORT_SYMBOL(target_complete_cmd
);
693 void target_complete_cmd_with_length(struct se_cmd
*cmd
, u8 scsi_status
, int length
)
695 if (scsi_status
== SAM_STAT_GOOD
&& length
< cmd
->data_length
) {
696 if (cmd
->se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
697 cmd
->residual_count
+= cmd
->data_length
- length
;
699 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
700 cmd
->residual_count
= cmd
->data_length
- length
;
703 cmd
->data_length
= length
;
706 target_complete_cmd(cmd
, scsi_status
);
708 EXPORT_SYMBOL(target_complete_cmd_with_length
);
710 static void target_add_to_state_list(struct se_cmd
*cmd
)
712 struct se_device
*dev
= cmd
->se_dev
;
715 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
716 if (!cmd
->state_active
) {
717 list_add_tail(&cmd
->state_list
, &dev
->state_list
);
718 cmd
->state_active
= true;
720 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
724 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
726 static void transport_write_pending_qf(struct se_cmd
*cmd
);
727 static void transport_complete_qf(struct se_cmd
*cmd
);
729 void target_qf_do_work(struct work_struct
*work
)
731 struct se_device
*dev
= container_of(work
, struct se_device
,
733 LIST_HEAD(qf_cmd_list
);
734 struct se_cmd
*cmd
, *cmd_tmp
;
736 spin_lock_irq(&dev
->qf_cmd_lock
);
737 list_splice_init(&dev
->qf_cmd_list
, &qf_cmd_list
);
738 spin_unlock_irq(&dev
->qf_cmd_lock
);
740 list_for_each_entry_safe(cmd
, cmd_tmp
, &qf_cmd_list
, se_qf_node
) {
741 list_del(&cmd
->se_qf_node
);
742 atomic_dec(&dev
->dev_qf_count
);
743 smp_mb__after_atomic_dec();
745 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
746 " context: %s\n", cmd
->se_tfo
->get_fabric_name(), cmd
,
747 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
) ? "COMPLETE_OK" :
748 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
) ? "WRITE_PENDING"
751 if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
)
752 transport_write_pending_qf(cmd
);
753 else if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
)
754 transport_complete_qf(cmd
);
758 unsigned char *transport_dump_cmd_direction(struct se_cmd
*cmd
)
760 switch (cmd
->data_direction
) {
763 case DMA_FROM_DEVICE
:
767 case DMA_BIDIRECTIONAL
:
776 void transport_dump_dev_state(
777 struct se_device
*dev
,
781 *bl
+= sprintf(b
+ *bl
, "Status: ");
782 if (dev
->export_count
)
783 *bl
+= sprintf(b
+ *bl
, "ACTIVATED");
785 *bl
+= sprintf(b
+ *bl
, "DEACTIVATED");
787 *bl
+= sprintf(b
+ *bl
, " Max Queue Depth: %d", dev
->queue_depth
);
788 *bl
+= sprintf(b
+ *bl
, " SectorSize: %u HwMaxSectors: %u\n",
789 dev
->dev_attrib
.block_size
,
790 dev
->dev_attrib
.hw_max_sectors
);
791 *bl
+= sprintf(b
+ *bl
, " ");
794 void transport_dump_vpd_proto_id(
796 unsigned char *p_buf
,
799 unsigned char buf
[VPD_TMP_BUF_SIZE
];
802 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
803 len
= sprintf(buf
, "T10 VPD Protocol Identifier: ");
805 switch (vpd
->protocol_identifier
) {
807 sprintf(buf
+len
, "Fibre Channel\n");
810 sprintf(buf
+len
, "Parallel SCSI\n");
813 sprintf(buf
+len
, "SSA\n");
816 sprintf(buf
+len
, "IEEE 1394\n");
819 sprintf(buf
+len
, "SCSI Remote Direct Memory Access"
823 sprintf(buf
+len
, "Internet SCSI (iSCSI)\n");
826 sprintf(buf
+len
, "SAS Serial SCSI Protocol\n");
829 sprintf(buf
+len
, "Automation/Drive Interface Transport"
833 sprintf(buf
+len
, "AT Attachment Interface ATA/ATAPI\n");
836 sprintf(buf
+len
, "Unknown 0x%02x\n",
837 vpd
->protocol_identifier
);
842 strncpy(p_buf
, buf
, p_buf_len
);
848 transport_set_vpd_proto_id(struct t10_vpd
*vpd
, unsigned char *page_83
)
851 * Check if the Protocol Identifier Valid (PIV) bit is set..
853 * from spc3r23.pdf section 7.5.1
855 if (page_83
[1] & 0x80) {
856 vpd
->protocol_identifier
= (page_83
[0] & 0xf0);
857 vpd
->protocol_identifier_set
= 1;
858 transport_dump_vpd_proto_id(vpd
, NULL
, 0);
861 EXPORT_SYMBOL(transport_set_vpd_proto_id
);
863 int transport_dump_vpd_assoc(
865 unsigned char *p_buf
,
868 unsigned char buf
[VPD_TMP_BUF_SIZE
];
872 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
873 len
= sprintf(buf
, "T10 VPD Identifier Association: ");
875 switch (vpd
->association
) {
877 sprintf(buf
+len
, "addressed logical unit\n");
880 sprintf(buf
+len
, "target port\n");
883 sprintf(buf
+len
, "SCSI target device\n");
886 sprintf(buf
+len
, "Unknown 0x%02x\n", vpd
->association
);
892 strncpy(p_buf
, buf
, p_buf_len
);
899 int transport_set_vpd_assoc(struct t10_vpd
*vpd
, unsigned char *page_83
)
902 * The VPD identification association..
904 * from spc3r23.pdf Section 7.6.3.1 Table 297
906 vpd
->association
= (page_83
[1] & 0x30);
907 return transport_dump_vpd_assoc(vpd
, NULL
, 0);
909 EXPORT_SYMBOL(transport_set_vpd_assoc
);
911 int transport_dump_vpd_ident_type(
913 unsigned char *p_buf
,
916 unsigned char buf
[VPD_TMP_BUF_SIZE
];
920 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
921 len
= sprintf(buf
, "T10 VPD Identifier Type: ");
923 switch (vpd
->device_identifier_type
) {
925 sprintf(buf
+len
, "Vendor specific\n");
928 sprintf(buf
+len
, "T10 Vendor ID based\n");
931 sprintf(buf
+len
, "EUI-64 based\n");
934 sprintf(buf
+len
, "NAA\n");
937 sprintf(buf
+len
, "Relative target port identifier\n");
940 sprintf(buf
+len
, "SCSI name string\n");
943 sprintf(buf
+len
, "Unsupported: 0x%02x\n",
944 vpd
->device_identifier_type
);
950 if (p_buf_len
< strlen(buf
)+1)
952 strncpy(p_buf
, buf
, p_buf_len
);
960 int transport_set_vpd_ident_type(struct t10_vpd
*vpd
, unsigned char *page_83
)
963 * The VPD identifier type..
965 * from spc3r23.pdf Section 7.6.3.1 Table 298
967 vpd
->device_identifier_type
= (page_83
[1] & 0x0f);
968 return transport_dump_vpd_ident_type(vpd
, NULL
, 0);
970 EXPORT_SYMBOL(transport_set_vpd_ident_type
);
972 int transport_dump_vpd_ident(
974 unsigned char *p_buf
,
977 unsigned char buf
[VPD_TMP_BUF_SIZE
];
980 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
982 switch (vpd
->device_identifier_code_set
) {
983 case 0x01: /* Binary */
984 snprintf(buf
, sizeof(buf
),
985 "T10 VPD Binary Device Identifier: %s\n",
986 &vpd
->device_identifier
[0]);
988 case 0x02: /* ASCII */
989 snprintf(buf
, sizeof(buf
),
990 "T10 VPD ASCII Device Identifier: %s\n",
991 &vpd
->device_identifier
[0]);
993 case 0x03: /* UTF-8 */
994 snprintf(buf
, sizeof(buf
),
995 "T10 VPD UTF-8 Device Identifier: %s\n",
996 &vpd
->device_identifier
[0]);
999 sprintf(buf
, "T10 VPD Device Identifier encoding unsupported:"
1000 " 0x%02x", vpd
->device_identifier_code_set
);
1006 strncpy(p_buf
, buf
, p_buf_len
);
1008 pr_debug("%s", buf
);
1014 transport_set_vpd_ident(struct t10_vpd
*vpd
, unsigned char *page_83
)
1016 static const char hex_str
[] = "0123456789abcdef";
1017 int j
= 0, i
= 4; /* offset to start of the identifier */
1020 * The VPD Code Set (encoding)
1022 * from spc3r23.pdf Section 7.6.3.1 Table 296
1024 vpd
->device_identifier_code_set
= (page_83
[0] & 0x0f);
1025 switch (vpd
->device_identifier_code_set
) {
1026 case 0x01: /* Binary */
1027 vpd
->device_identifier
[j
++] =
1028 hex_str
[vpd
->device_identifier_type
];
1029 while (i
< (4 + page_83
[3])) {
1030 vpd
->device_identifier
[j
++] =
1031 hex_str
[(page_83
[i
] & 0xf0) >> 4];
1032 vpd
->device_identifier
[j
++] =
1033 hex_str
[page_83
[i
] & 0x0f];
1037 case 0x02: /* ASCII */
1038 case 0x03: /* UTF-8 */
1039 while (i
< (4 + page_83
[3]))
1040 vpd
->device_identifier
[j
++] = page_83
[i
++];
1046 return transport_dump_vpd_ident(vpd
, NULL
, 0);
1048 EXPORT_SYMBOL(transport_set_vpd_ident
);
1051 target_cmd_size_check(struct se_cmd
*cmd
, unsigned int size
)
1053 struct se_device
*dev
= cmd
->se_dev
;
1055 if (cmd
->unknown_data_length
) {
1056 cmd
->data_length
= size
;
1057 } else if (size
!= cmd
->data_length
) {
1058 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1059 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1060 " 0x%02x\n", cmd
->se_tfo
->get_fabric_name(),
1061 cmd
->data_length
, size
, cmd
->t_task_cdb
[0]);
1063 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
1064 pr_err("Rejecting underflow/overflow"
1066 return TCM_INVALID_CDB_FIELD
;
1069 * Reject READ_* or WRITE_* with overflow/underflow for
1070 * type SCF_SCSI_DATA_CDB.
1072 if (dev
->dev_attrib
.block_size
!= 512) {
1073 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1074 " CDB on non 512-byte sector setup subsystem"
1075 " plugin: %s\n", dev
->transport
->name
);
1076 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1077 return TCM_INVALID_CDB_FIELD
;
1080 * For the overflow case keep the existing fabric provided
1081 * ->data_length. Otherwise for the underflow case, reset
1082 * ->data_length to the smaller SCSI expected data transfer
1085 if (size
> cmd
->data_length
) {
1086 cmd
->se_cmd_flags
|= SCF_OVERFLOW_BIT
;
1087 cmd
->residual_count
= (size
- cmd
->data_length
);
1089 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
1090 cmd
->residual_count
= (cmd
->data_length
- size
);
1091 cmd
->data_length
= size
;
1100 * Used by fabric modules containing a local struct se_cmd within their
1101 * fabric dependent per I/O descriptor.
1103 void transport_init_se_cmd(
1105 struct target_core_fabric_ops
*tfo
,
1106 struct se_session
*se_sess
,
1110 unsigned char *sense_buffer
)
1112 INIT_LIST_HEAD(&cmd
->se_lun_node
);
1113 INIT_LIST_HEAD(&cmd
->se_delayed_node
);
1114 INIT_LIST_HEAD(&cmd
->se_qf_node
);
1115 INIT_LIST_HEAD(&cmd
->se_cmd_list
);
1116 INIT_LIST_HEAD(&cmd
->state_list
);
1117 init_completion(&cmd
->transport_lun_fe_stop_comp
);
1118 init_completion(&cmd
->transport_lun_stop_comp
);
1119 init_completion(&cmd
->t_transport_stop_comp
);
1120 init_completion(&cmd
->cmd_wait_comp
);
1121 init_completion(&cmd
->task_stop_comp
);
1122 spin_lock_init(&cmd
->t_state_lock
);
1123 kref_init(&cmd
->cmd_kref
);
1124 cmd
->transport_state
= CMD_T_DEV_ACTIVE
;
1127 cmd
->se_sess
= se_sess
;
1128 cmd
->data_length
= data_length
;
1129 cmd
->data_direction
= data_direction
;
1130 cmd
->sam_task_attr
= task_attr
;
1131 cmd
->sense_buffer
= sense_buffer
;
1133 cmd
->state_active
= false;
1135 EXPORT_SYMBOL(transport_init_se_cmd
);
1137 static sense_reason_t
1138 transport_check_alloc_task_attr(struct se_cmd
*cmd
)
1140 struct se_device
*dev
= cmd
->se_dev
;
1143 * Check if SAM Task Attribute emulation is enabled for this
1144 * struct se_device storage object
1146 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1149 if (cmd
->sam_task_attr
== MSG_ACA_TAG
) {
1150 pr_debug("SAM Task Attribute ACA"
1151 " emulation is not supported\n");
1152 return TCM_INVALID_CDB_FIELD
;
1155 * Used to determine when ORDERED commands should go from
1156 * Dormant to Active status.
1158 cmd
->se_ordered_id
= atomic_inc_return(&dev
->dev_ordered_id
);
1159 smp_mb__after_atomic_inc();
1160 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1161 cmd
->se_ordered_id
, cmd
->sam_task_attr
,
1162 dev
->transport
->name
);
1167 target_setup_cmd_from_cdb(struct se_cmd
*cmd
, unsigned char *cdb
)
1169 struct se_device
*dev
= cmd
->se_dev
;
1173 * Ensure that the received CDB is less than the max (252 + 8) bytes
1174 * for VARIABLE_LENGTH_CMD
1176 if (scsi_command_size(cdb
) > SCSI_MAX_VARLEN_CDB_SIZE
) {
1177 pr_err("Received SCSI CDB with command_size: %d that"
1178 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1179 scsi_command_size(cdb
), SCSI_MAX_VARLEN_CDB_SIZE
);
1180 return TCM_INVALID_CDB_FIELD
;
1183 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1184 * allocate the additional extended CDB buffer now.. Otherwise
1185 * setup the pointer from __t_task_cdb to t_task_cdb.
1187 if (scsi_command_size(cdb
) > sizeof(cmd
->__t_task_cdb
)) {
1188 cmd
->t_task_cdb
= kzalloc(scsi_command_size(cdb
),
1190 if (!cmd
->t_task_cdb
) {
1191 pr_err("Unable to allocate cmd->t_task_cdb"
1192 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1193 scsi_command_size(cdb
),
1194 (unsigned long)sizeof(cmd
->__t_task_cdb
));
1195 return TCM_OUT_OF_RESOURCES
;
1198 cmd
->t_task_cdb
= &cmd
->__t_task_cdb
[0];
1200 * Copy the original CDB into cmd->
1202 memcpy(cmd
->t_task_cdb
, cdb
, scsi_command_size(cdb
));
1204 trace_target_sequencer_start(cmd
);
1207 * Check for an existing UNIT ATTENTION condition
1209 ret
= target_scsi3_ua_check(cmd
);
1213 ret
= target_alua_state_check(cmd
);
1217 ret
= target_check_reservation(cmd
);
1219 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
1223 ret
= dev
->transport
->parse_cdb(cmd
);
1227 ret
= transport_check_alloc_task_attr(cmd
);
1231 cmd
->se_cmd_flags
|= SCF_SUPPORTED_SAM_OPCODE
;
1233 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1234 if (cmd
->se_lun
->lun_sep
)
1235 cmd
->se_lun
->lun_sep
->sep_stats
.cmd_pdus
++;
1236 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1239 EXPORT_SYMBOL(target_setup_cmd_from_cdb
);
1242 * Used by fabric module frontends to queue tasks directly.
1243 * Many only be used from process context only
1245 int transport_handle_cdb_direct(
1252 pr_err("cmd->se_lun is NULL\n");
1255 if (in_interrupt()) {
1257 pr_err("transport_generic_handle_cdb cannot be called"
1258 " from interrupt context\n");
1262 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1263 * outstanding descriptors are handled correctly during shutdown via
1264 * transport_wait_for_tasks()
1266 * Also, we don't take cmd->t_state_lock here as we only expect
1267 * this to be called for initial descriptor submission.
1269 cmd
->t_state
= TRANSPORT_NEW_CMD
;
1270 cmd
->transport_state
|= CMD_T_ACTIVE
;
1273 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1274 * so follow TRANSPORT_NEW_CMD processing thread context usage
1275 * and call transport_generic_request_failure() if necessary..
1277 ret
= transport_generic_new_cmd(cmd
);
1279 transport_generic_request_failure(cmd
, ret
);
1282 EXPORT_SYMBOL(transport_handle_cdb_direct
);
1285 transport_generic_map_mem_to_cmd(struct se_cmd
*cmd
, struct scatterlist
*sgl
,
1286 u32 sgl_count
, struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
)
1288 if (!sgl
|| !sgl_count
)
1292 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
1293 * scatterlists already have been set to follow what the fabric
1294 * passes for the original expected data transfer length.
1296 if (cmd
->se_cmd_flags
& SCF_OVERFLOW_BIT
) {
1297 pr_warn("Rejecting SCSI DATA overflow for fabric using"
1298 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
1299 return TCM_INVALID_CDB_FIELD
;
1302 cmd
->t_data_sg
= sgl
;
1303 cmd
->t_data_nents
= sgl_count
;
1305 if (sgl_bidi
&& sgl_bidi_count
) {
1306 cmd
->t_bidi_data_sg
= sgl_bidi
;
1307 cmd
->t_bidi_data_nents
= sgl_bidi_count
;
1309 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
;
1314 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1315 * se_cmd + use pre-allocated SGL memory.
1317 * @se_cmd: command descriptor to submit
1318 * @se_sess: associated se_sess for endpoint
1319 * @cdb: pointer to SCSI CDB
1320 * @sense: pointer to SCSI sense buffer
1321 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1322 * @data_length: fabric expected data transfer length
1323 * @task_addr: SAM task attribute
1324 * @data_dir: DMA data direction
1325 * @flags: flags for command submission from target_sc_flags_tables
1326 * @sgl: struct scatterlist memory for unidirectional mapping
1327 * @sgl_count: scatterlist count for unidirectional mapping
1328 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1329 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1331 * Returns non zero to signal active I/O shutdown failure. All other
1332 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1333 * but still return zero here.
1335 * This may only be called from process context, and also currently
1336 * assumes internal allocation of fabric payload buffer by target-core.
1338 int target_submit_cmd_map_sgls(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1339 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1340 u32 data_length
, int task_attr
, int data_dir
, int flags
,
1341 struct scatterlist
*sgl
, u32 sgl_count
,
1342 struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
)
1344 struct se_portal_group
*se_tpg
;
1348 se_tpg
= se_sess
->se_tpg
;
1350 BUG_ON(se_cmd
->se_tfo
|| se_cmd
->se_sess
);
1351 BUG_ON(in_interrupt());
1353 * Initialize se_cmd for target operation. From this point
1354 * exceptions are handled by sending exception status via
1355 * target_core_fabric_ops->queue_status() callback
1357 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1358 data_length
, data_dir
, task_attr
, sense
);
1359 if (flags
& TARGET_SCF_UNKNOWN_SIZE
)
1360 se_cmd
->unknown_data_length
= 1;
1362 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1363 * se_sess->sess_cmd_list. A second kref_get here is necessary
1364 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1365 * kref_put() to happen during fabric packet acknowledgement.
1367 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1371 * Signal bidirectional data payloads to target-core
1373 if (flags
& TARGET_SCF_BIDI_OP
)
1374 se_cmd
->se_cmd_flags
|= SCF_BIDI
;
1376 * Locate se_lun pointer and attach it to struct se_cmd
1378 rc
= transport_lookup_cmd_lun(se_cmd
, unpacked_lun
);
1380 transport_send_check_condition_and_sense(se_cmd
, rc
, 0);
1381 target_put_sess_cmd(se_sess
, se_cmd
);
1385 rc
= target_setup_cmd_from_cdb(se_cmd
, cdb
);
1387 transport_generic_request_failure(se_cmd
, rc
);
1391 * When a non zero sgl_count has been passed perform SGL passthrough
1392 * mapping for pre-allocated fabric memory instead of having target
1393 * core perform an internal SGL allocation..
1395 if (sgl_count
!= 0) {
1399 * A work-around for tcm_loop as some userspace code via
1400 * scsi-generic do not memset their associated read buffers,
1401 * so go ahead and do that here for type non-data CDBs. Also
1402 * note that this is currently guaranteed to be a single SGL
1403 * for this case by target core in target_setup_cmd_from_cdb()
1404 * -> transport_generic_cmd_sequencer().
1406 if (!(se_cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
) &&
1407 se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
1408 unsigned char *buf
= NULL
;
1411 buf
= kmap(sg_page(sgl
)) + sgl
->offset
;
1414 memset(buf
, 0, sgl
->length
);
1415 kunmap(sg_page(sgl
));
1419 rc
= transport_generic_map_mem_to_cmd(se_cmd
, sgl
, sgl_count
,
1420 sgl_bidi
, sgl_bidi_count
);
1422 transport_generic_request_failure(se_cmd
, rc
);
1427 * Check if we need to delay processing because of ALUA
1428 * Active/NonOptimized primary access state..
1430 core_alua_check_nonop_delay(se_cmd
);
1432 transport_handle_cdb_direct(se_cmd
);
1435 EXPORT_SYMBOL(target_submit_cmd_map_sgls
);
1438 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1440 * @se_cmd: command descriptor to submit
1441 * @se_sess: associated se_sess for endpoint
1442 * @cdb: pointer to SCSI CDB
1443 * @sense: pointer to SCSI sense buffer
1444 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1445 * @data_length: fabric expected data transfer length
1446 * @task_addr: SAM task attribute
1447 * @data_dir: DMA data direction
1448 * @flags: flags for command submission from target_sc_flags_tables
1450 * Returns non zero to signal active I/O shutdown failure. All other
1451 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1452 * but still return zero here.
1454 * This may only be called from process context, and also currently
1455 * assumes internal allocation of fabric payload buffer by target-core.
1457 * It also assumes interal target core SGL memory allocation.
1459 int target_submit_cmd(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1460 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1461 u32 data_length
, int task_attr
, int data_dir
, int flags
)
1463 return target_submit_cmd_map_sgls(se_cmd
, se_sess
, cdb
, sense
,
1464 unpacked_lun
, data_length
, task_attr
, data_dir
,
1465 flags
, NULL
, 0, NULL
, 0);
1467 EXPORT_SYMBOL(target_submit_cmd
);
1469 static void target_complete_tmr_failure(struct work_struct
*work
)
1471 struct se_cmd
*se_cmd
= container_of(work
, struct se_cmd
, work
);
1473 se_cmd
->se_tmr_req
->response
= TMR_LUN_DOES_NOT_EXIST
;
1474 se_cmd
->se_tfo
->queue_tm_rsp(se_cmd
);
1476 transport_cmd_check_stop_to_fabric(se_cmd
);
1480 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1483 * @se_cmd: command descriptor to submit
1484 * @se_sess: associated se_sess for endpoint
1485 * @sense: pointer to SCSI sense buffer
1486 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1487 * @fabric_context: fabric context for TMR req
1488 * @tm_type: Type of TM request
1489 * @gfp: gfp type for caller
1490 * @tag: referenced task tag for TMR_ABORT_TASK
1491 * @flags: submit cmd flags
1493 * Callable from all contexts.
1496 int target_submit_tmr(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1497 unsigned char *sense
, u32 unpacked_lun
,
1498 void *fabric_tmr_ptr
, unsigned char tm_type
,
1499 gfp_t gfp
, unsigned int tag
, int flags
)
1501 struct se_portal_group
*se_tpg
;
1504 se_tpg
= se_sess
->se_tpg
;
1507 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1508 0, DMA_NONE
, MSG_SIMPLE_TAG
, sense
);
1510 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1511 * allocation failure.
1513 ret
= core_tmr_alloc_req(se_cmd
, fabric_tmr_ptr
, tm_type
, gfp
);
1517 if (tm_type
== TMR_ABORT_TASK
)
1518 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
1520 /* See target_submit_cmd for commentary */
1521 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1523 core_tmr_release_req(se_cmd
->se_tmr_req
);
1527 ret
= transport_lookup_tmr_lun(se_cmd
, unpacked_lun
);
1530 * For callback during failure handling, push this work off
1531 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1533 INIT_WORK(&se_cmd
->work
, target_complete_tmr_failure
);
1534 schedule_work(&se_cmd
->work
);
1537 transport_generic_handle_tmr(se_cmd
);
1540 EXPORT_SYMBOL(target_submit_tmr
);
1543 * If the cmd is active, request it to be stopped and sleep until it
1546 bool target_stop_cmd(struct se_cmd
*cmd
, unsigned long *flags
)
1548 bool was_active
= false;
1550 if (cmd
->transport_state
& CMD_T_BUSY
) {
1551 cmd
->transport_state
|= CMD_T_REQUEST_STOP
;
1552 spin_unlock_irqrestore(&cmd
->t_state_lock
, *flags
);
1554 pr_debug("cmd %p waiting to complete\n", cmd
);
1555 wait_for_completion(&cmd
->task_stop_comp
);
1556 pr_debug("cmd %p stopped successfully\n", cmd
);
1558 spin_lock_irqsave(&cmd
->t_state_lock
, *flags
);
1559 cmd
->transport_state
&= ~CMD_T_REQUEST_STOP
;
1560 cmd
->transport_state
&= ~CMD_T_BUSY
;
1568 * Handle SAM-esque emulation for generic transport request failures.
1570 void transport_generic_request_failure(struct se_cmd
*cmd
,
1571 sense_reason_t sense_reason
)
1573 int ret
= 0, post_ret
= 0;
1575 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1576 " CDB: 0x%02x\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
1577 cmd
->t_task_cdb
[0]);
1578 pr_debug("-----[ i_state: %d t_state: %d sense_reason: %d\n",
1579 cmd
->se_tfo
->get_cmd_state(cmd
),
1580 cmd
->t_state
, sense_reason
);
1581 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1582 (cmd
->transport_state
& CMD_T_ACTIVE
) != 0,
1583 (cmd
->transport_state
& CMD_T_STOP
) != 0,
1584 (cmd
->transport_state
& CMD_T_SENT
) != 0);
1587 * For SAM Task Attribute emulation for failed struct se_cmd
1589 transport_complete_task_attr(cmd
);
1591 * Handle special case for COMPARE_AND_WRITE failure, where the
1592 * callback is expected to drop the per device ->caw_sem.
1594 if ((cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) &&
1595 cmd
->transport_complete_callback
)
1596 cmd
->transport_complete_callback(cmd
, false, &post_ret
);
1598 switch (sense_reason
) {
1599 case TCM_NON_EXISTENT_LUN
:
1600 case TCM_UNSUPPORTED_SCSI_OPCODE
:
1601 case TCM_INVALID_CDB_FIELD
:
1602 case TCM_INVALID_PARAMETER_LIST
:
1603 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
1604 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
1605 case TCM_UNKNOWN_MODE_PAGE
:
1606 case TCM_WRITE_PROTECTED
:
1607 case TCM_ADDRESS_OUT_OF_RANGE
:
1608 case TCM_CHECK_CONDITION_ABORT_CMD
:
1609 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
1610 case TCM_CHECK_CONDITION_NOT_READY
:
1612 case TCM_OUT_OF_RESOURCES
:
1613 sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1615 case TCM_RESERVATION_CONFLICT
:
1617 * No SENSE Data payload for this case, set SCSI Status
1618 * and queue the response to $FABRIC_MOD.
1620 * Uses linux/include/scsi/scsi.h SAM status codes defs
1622 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
1624 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1625 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1628 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1631 cmd
->se_dev
->dev_attrib
.emulate_ua_intlck_ctrl
== 2)
1632 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
1633 cmd
->orig_fe_lun
, 0x2C,
1634 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
1636 trace_target_cmd_complete(cmd
);
1637 ret
= cmd
->se_tfo
-> queue_status(cmd
);
1638 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1642 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1643 cmd
->t_task_cdb
[0], sense_reason
);
1644 sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
1648 ret
= transport_send_check_condition_and_sense(cmd
, sense_reason
, 0);
1649 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1653 transport_lun_remove_cmd(cmd
);
1654 if (!transport_cmd_check_stop_to_fabric(cmd
))
1659 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
1660 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1662 EXPORT_SYMBOL(transport_generic_request_failure
);
1664 void __target_execute_cmd(struct se_cmd
*cmd
)
1668 if (cmd
->execute_cmd
) {
1669 ret
= cmd
->execute_cmd(cmd
);
1671 spin_lock_irq(&cmd
->t_state_lock
);
1672 cmd
->transport_state
&= ~(CMD_T_BUSY
|CMD_T_SENT
);
1673 spin_unlock_irq(&cmd
->t_state_lock
);
1675 transport_generic_request_failure(cmd
, ret
);
1680 static bool target_handle_task_attr(struct se_cmd
*cmd
)
1682 struct se_device
*dev
= cmd
->se_dev
;
1684 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1688 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1689 * to allow the passed struct se_cmd list of tasks to the front of the list.
1691 switch (cmd
->sam_task_attr
) {
1693 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1694 "se_ordered_id: %u\n",
1695 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1697 case MSG_ORDERED_TAG
:
1698 atomic_inc(&dev
->dev_ordered_sync
);
1699 smp_mb__after_atomic_inc();
1701 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1702 " se_ordered_id: %u\n",
1703 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1706 * Execute an ORDERED command if no other older commands
1707 * exist that need to be completed first.
1709 if (!atomic_read(&dev
->simple_cmds
))
1714 * For SIMPLE and UNTAGGED Task Attribute commands
1716 atomic_inc(&dev
->simple_cmds
);
1717 smp_mb__after_atomic_inc();
1721 if (atomic_read(&dev
->dev_ordered_sync
) == 0)
1724 spin_lock(&dev
->delayed_cmd_lock
);
1725 list_add_tail(&cmd
->se_delayed_node
, &dev
->delayed_cmd_list
);
1726 spin_unlock(&dev
->delayed_cmd_lock
);
1728 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1729 " delayed CMD list, se_ordered_id: %u\n",
1730 cmd
->t_task_cdb
[0], cmd
->sam_task_attr
,
1731 cmd
->se_ordered_id
);
1735 void target_execute_cmd(struct se_cmd
*cmd
)
1738 * If the received CDB has aleady been aborted stop processing it here.
1740 if (transport_check_aborted_status(cmd
, 1)) {
1741 complete(&cmd
->transport_lun_stop_comp
);
1746 * Determine if IOCTL context caller in requesting the stopping of this
1747 * command for LUN shutdown purposes.
1749 spin_lock_irq(&cmd
->t_state_lock
);
1750 if (cmd
->transport_state
& CMD_T_LUN_STOP
) {
1751 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
1752 __func__
, __LINE__
, cmd
->se_tfo
->get_task_tag(cmd
));
1754 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
1755 spin_unlock_irq(&cmd
->t_state_lock
);
1756 complete(&cmd
->transport_lun_stop_comp
);
1760 * Determine if frontend context caller is requesting the stopping of
1761 * this command for frontend exceptions.
1763 if (cmd
->transport_state
& CMD_T_STOP
) {
1764 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1766 cmd
->se_tfo
->get_task_tag(cmd
));
1768 spin_unlock_irq(&cmd
->t_state_lock
);
1769 complete_all(&cmd
->t_transport_stop_comp
);
1773 cmd
->t_state
= TRANSPORT_PROCESSING
;
1774 cmd
->transport_state
|= CMD_T_ACTIVE
|CMD_T_BUSY
|CMD_T_SENT
;
1775 spin_unlock_irq(&cmd
->t_state_lock
);
1777 if (target_handle_task_attr(cmd
)) {
1778 spin_lock_irq(&cmd
->t_state_lock
);
1779 cmd
->transport_state
&= ~CMD_T_BUSY
|CMD_T_SENT
;
1780 spin_unlock_irq(&cmd
->t_state_lock
);
1784 __target_execute_cmd(cmd
);
1786 EXPORT_SYMBOL(target_execute_cmd
);
1789 * Process all commands up to the last received ORDERED task attribute which
1790 * requires another blocking boundary
1792 static void target_restart_delayed_cmds(struct se_device
*dev
)
1797 spin_lock(&dev
->delayed_cmd_lock
);
1798 if (list_empty(&dev
->delayed_cmd_list
)) {
1799 spin_unlock(&dev
->delayed_cmd_lock
);
1803 cmd
= list_entry(dev
->delayed_cmd_list
.next
,
1804 struct se_cmd
, se_delayed_node
);
1805 list_del(&cmd
->se_delayed_node
);
1806 spin_unlock(&dev
->delayed_cmd_lock
);
1808 __target_execute_cmd(cmd
);
1810 if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
)
1816 * Called from I/O completion to determine which dormant/delayed
1817 * and ordered cmds need to have their tasks added to the execution queue.
1819 static void transport_complete_task_attr(struct se_cmd
*cmd
)
1821 struct se_device
*dev
= cmd
->se_dev
;
1823 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1826 if (cmd
->sam_task_attr
== MSG_SIMPLE_TAG
) {
1827 atomic_dec(&dev
->simple_cmds
);
1828 smp_mb__after_atomic_dec();
1829 dev
->dev_cur_ordered_id
++;
1830 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
1831 " SIMPLE: %u\n", dev
->dev_cur_ordered_id
,
1832 cmd
->se_ordered_id
);
1833 } else if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
1834 dev
->dev_cur_ordered_id
++;
1835 pr_debug("Incremented dev_cur_ordered_id: %u for"
1836 " HEAD_OF_QUEUE: %u\n", dev
->dev_cur_ordered_id
,
1837 cmd
->se_ordered_id
);
1838 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
1839 atomic_dec(&dev
->dev_ordered_sync
);
1840 smp_mb__after_atomic_dec();
1842 dev
->dev_cur_ordered_id
++;
1843 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
1844 " %u\n", dev
->dev_cur_ordered_id
, cmd
->se_ordered_id
);
1847 target_restart_delayed_cmds(dev
);
1850 static void transport_complete_qf(struct se_cmd
*cmd
)
1854 transport_complete_task_attr(cmd
);
1856 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
1857 trace_target_cmd_complete(cmd
);
1858 ret
= cmd
->se_tfo
->queue_status(cmd
);
1862 switch (cmd
->data_direction
) {
1863 case DMA_FROM_DEVICE
:
1864 trace_target_cmd_complete(cmd
);
1865 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1868 if (cmd
->se_cmd_flags
& SCF_BIDI
) {
1869 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1873 /* Fall through for DMA_TO_DEVICE */
1875 trace_target_cmd_complete(cmd
);
1876 ret
= cmd
->se_tfo
->queue_status(cmd
);
1884 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1887 transport_lun_remove_cmd(cmd
);
1888 transport_cmd_check_stop_to_fabric(cmd
);
1891 static void transport_handle_queue_full(
1893 struct se_device
*dev
)
1895 spin_lock_irq(&dev
->qf_cmd_lock
);
1896 list_add_tail(&cmd
->se_qf_node
, &cmd
->se_dev
->qf_cmd_list
);
1897 atomic_inc(&dev
->dev_qf_count
);
1898 smp_mb__after_atomic_inc();
1899 spin_unlock_irq(&cmd
->se_dev
->qf_cmd_lock
);
1901 schedule_work(&cmd
->se_dev
->qf_work_queue
);
1904 static void target_complete_ok_work(struct work_struct
*work
)
1906 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
1910 * Check if we need to move delayed/dormant tasks from cmds on the
1911 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1914 transport_complete_task_attr(cmd
);
1917 * Check to schedule QUEUE_FULL work, or execute an existing
1918 * cmd->transport_qf_callback()
1920 if (atomic_read(&cmd
->se_dev
->dev_qf_count
) != 0)
1921 schedule_work(&cmd
->se_dev
->qf_work_queue
);
1924 * Check if we need to send a sense buffer from
1925 * the struct se_cmd in question.
1927 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
1928 WARN_ON(!cmd
->scsi_status
);
1929 ret
= transport_send_check_condition_and_sense(
1931 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1934 transport_lun_remove_cmd(cmd
);
1935 transport_cmd_check_stop_to_fabric(cmd
);
1939 * Check for a callback, used by amongst other things
1940 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
1942 if (cmd
->transport_complete_callback
) {
1944 bool caw
= (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
);
1945 bool zero_dl
= !(cmd
->data_length
);
1948 rc
= cmd
->transport_complete_callback(cmd
, true, &post_ret
);
1949 if (!rc
&& !post_ret
) {
1955 ret
= transport_send_check_condition_and_sense(cmd
,
1957 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1960 transport_lun_remove_cmd(cmd
);
1961 transport_cmd_check_stop_to_fabric(cmd
);
1967 switch (cmd
->data_direction
) {
1968 case DMA_FROM_DEVICE
:
1969 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1970 if (cmd
->se_lun
->lun_sep
) {
1971 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
1974 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1976 trace_target_cmd_complete(cmd
);
1977 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1978 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1982 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1983 if (cmd
->se_lun
->lun_sep
) {
1984 cmd
->se_lun
->lun_sep
->sep_stats
.rx_data_octets
+=
1987 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1989 * Check if we need to send READ payload for BIDI-COMMAND
1991 if (cmd
->se_cmd_flags
& SCF_BIDI
) {
1992 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1993 if (cmd
->se_lun
->lun_sep
) {
1994 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
1997 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1998 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1999 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2003 /* Fall through for DMA_TO_DEVICE */
2005 trace_target_cmd_complete(cmd
);
2006 ret
= cmd
->se_tfo
->queue_status(cmd
);
2007 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2014 transport_lun_remove_cmd(cmd
);
2015 transport_cmd_check_stop_to_fabric(cmd
);
2019 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
2020 " data_direction: %d\n", cmd
, cmd
->data_direction
);
2021 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
2022 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2025 static inline void transport_free_sgl(struct scatterlist
*sgl
, int nents
)
2027 struct scatterlist
*sg
;
2030 for_each_sg(sgl
, sg
, nents
, count
)
2031 __free_page(sg_page(sg
));
2036 static inline void transport_reset_sgl_orig(struct se_cmd
*cmd
)
2039 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
2040 * emulation, and free + reset pointers if necessary..
2042 if (!cmd
->t_data_sg_orig
)
2045 kfree(cmd
->t_data_sg
);
2046 cmd
->t_data_sg
= cmd
->t_data_sg_orig
;
2047 cmd
->t_data_sg_orig
= NULL
;
2048 cmd
->t_data_nents
= cmd
->t_data_nents_orig
;
2049 cmd
->t_data_nents_orig
= 0;
2052 static inline void transport_free_pages(struct se_cmd
*cmd
)
2054 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) {
2056 * Release special case READ buffer payload required for
2057 * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE
2059 if (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) {
2060 transport_free_sgl(cmd
->t_bidi_data_sg
,
2061 cmd
->t_bidi_data_nents
);
2062 cmd
->t_bidi_data_sg
= NULL
;
2063 cmd
->t_bidi_data_nents
= 0;
2065 transport_reset_sgl_orig(cmd
);
2068 transport_reset_sgl_orig(cmd
);
2070 transport_free_sgl(cmd
->t_data_sg
, cmd
->t_data_nents
);
2071 cmd
->t_data_sg
= NULL
;
2072 cmd
->t_data_nents
= 0;
2074 transport_free_sgl(cmd
->t_bidi_data_sg
, cmd
->t_bidi_data_nents
);
2075 cmd
->t_bidi_data_sg
= NULL
;
2076 cmd
->t_bidi_data_nents
= 0;
2080 * transport_release_cmd - free a command
2081 * @cmd: command to free
2083 * This routine unconditionally frees a command, and reference counting
2084 * or list removal must be done in the caller.
2086 static int transport_release_cmd(struct se_cmd
*cmd
)
2088 BUG_ON(!cmd
->se_tfo
);
2090 if (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
2091 core_tmr_release_req(cmd
->se_tmr_req
);
2092 if (cmd
->t_task_cdb
!= cmd
->__t_task_cdb
)
2093 kfree(cmd
->t_task_cdb
);
2095 * If this cmd has been setup with target_get_sess_cmd(), drop
2096 * the kref and call ->release_cmd() in kref callback.
2098 return target_put_sess_cmd(cmd
->se_sess
, cmd
);
2102 * transport_put_cmd - release a reference to a command
2103 * @cmd: command to release
2105 * This routine releases our reference to the command and frees it if possible.
2107 static int transport_put_cmd(struct se_cmd
*cmd
)
2109 transport_free_pages(cmd
);
2110 return transport_release_cmd(cmd
);
2113 void *transport_kmap_data_sg(struct se_cmd
*cmd
)
2115 struct scatterlist
*sg
= cmd
->t_data_sg
;
2116 struct page
**pages
;
2120 * We need to take into account a possible offset here for fabrics like
2121 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2122 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
2124 if (!cmd
->t_data_nents
)
2128 if (cmd
->t_data_nents
== 1)
2129 return kmap(sg_page(sg
)) + sg
->offset
;
2131 /* >1 page. use vmap */
2132 pages
= kmalloc(sizeof(*pages
) * cmd
->t_data_nents
, GFP_KERNEL
);
2136 /* convert sg[] to pages[] */
2137 for_each_sg(cmd
->t_data_sg
, sg
, cmd
->t_data_nents
, i
) {
2138 pages
[i
] = sg_page(sg
);
2141 cmd
->t_data_vmap
= vmap(pages
, cmd
->t_data_nents
, VM_MAP
, PAGE_KERNEL
);
2143 if (!cmd
->t_data_vmap
)
2146 return cmd
->t_data_vmap
+ cmd
->t_data_sg
[0].offset
;
2148 EXPORT_SYMBOL(transport_kmap_data_sg
);
2150 void transport_kunmap_data_sg(struct se_cmd
*cmd
)
2152 if (!cmd
->t_data_nents
) {
2154 } else if (cmd
->t_data_nents
== 1) {
2155 kunmap(sg_page(cmd
->t_data_sg
));
2159 vunmap(cmd
->t_data_vmap
);
2160 cmd
->t_data_vmap
= NULL
;
2162 EXPORT_SYMBOL(transport_kunmap_data_sg
);
2165 target_alloc_sgl(struct scatterlist
**sgl
, unsigned int *nents
, u32 length
,
2168 struct scatterlist
*sg
;
2170 gfp_t zero_flag
= (zero_page
) ? __GFP_ZERO
: 0;
2174 nent
= DIV_ROUND_UP(length
, PAGE_SIZE
);
2175 sg
= kmalloc(sizeof(struct scatterlist
) * nent
, GFP_KERNEL
);
2179 sg_init_table(sg
, nent
);
2182 u32 page_len
= min_t(u32
, length
, PAGE_SIZE
);
2183 page
= alloc_page(GFP_KERNEL
| zero_flag
);
2187 sg_set_page(&sg
[i
], page
, page_len
, 0);
2198 __free_page(sg_page(&sg
[i
]));
2205 * Allocate any required resources to execute the command. For writes we
2206 * might not have the payload yet, so notify the fabric via a call to
2207 * ->write_pending instead. Otherwise place it on the execution queue.
2210 transport_generic_new_cmd(struct se_cmd
*cmd
)
2213 bool zero_flag
= !(cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
);
2216 * Determine is the TCM fabric module has already allocated physical
2217 * memory, and is directly calling transport_generic_map_mem_to_cmd()
2220 if (!(cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) &&
2223 if ((cmd
->se_cmd_flags
& SCF_BIDI
) ||
2224 (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
)) {
2227 if (cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
)
2228 bidi_length
= cmd
->t_task_nolb
*
2229 cmd
->se_dev
->dev_attrib
.block_size
;
2231 bidi_length
= cmd
->data_length
;
2233 ret
= target_alloc_sgl(&cmd
->t_bidi_data_sg
,
2234 &cmd
->t_bidi_data_nents
,
2235 bidi_length
, zero_flag
);
2237 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2240 ret
= target_alloc_sgl(&cmd
->t_data_sg
, &cmd
->t_data_nents
,
2241 cmd
->data_length
, zero_flag
);
2243 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2244 } else if ((cmd
->se_cmd_flags
& SCF_COMPARE_AND_WRITE
) &&
2247 * Special case for COMPARE_AND_WRITE with fabrics
2248 * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC.
2250 u32 caw_length
= cmd
->t_task_nolb
*
2251 cmd
->se_dev
->dev_attrib
.block_size
;
2253 ret
= target_alloc_sgl(&cmd
->t_bidi_data_sg
,
2254 &cmd
->t_bidi_data_nents
,
2255 caw_length
, zero_flag
);
2257 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2260 * If this command is not a write we can execute it right here,
2261 * for write buffers we need to notify the fabric driver first
2262 * and let it call back once the write buffers are ready.
2264 target_add_to_state_list(cmd
);
2265 if (cmd
->data_direction
!= DMA_TO_DEVICE
|| cmd
->data_length
== 0) {
2266 target_execute_cmd(cmd
);
2269 transport_cmd_check_stop(cmd
, false, true);
2271 ret
= cmd
->se_tfo
->write_pending(cmd
);
2272 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2275 /* fabric drivers should only return -EAGAIN or -ENOMEM as error */
2278 return (!ret
) ? 0 : TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2281 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd
);
2282 cmd
->t_state
= TRANSPORT_COMPLETE_QF_WP
;
2283 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2286 EXPORT_SYMBOL(transport_generic_new_cmd
);
2288 static void transport_write_pending_qf(struct se_cmd
*cmd
)
2292 ret
= cmd
->se_tfo
->write_pending(cmd
);
2293 if (ret
== -EAGAIN
|| ret
== -ENOMEM
) {
2294 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2296 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2300 int transport_generic_free_cmd(struct se_cmd
*cmd
, int wait_for_tasks
)
2302 unsigned long flags
;
2305 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
)) {
2306 if (wait_for_tasks
&& (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
))
2307 transport_wait_for_tasks(cmd
);
2309 ret
= transport_release_cmd(cmd
);
2312 transport_wait_for_tasks(cmd
);
2314 * Handle WRITE failure case where transport_generic_new_cmd()
2315 * has already added se_cmd to state_list, but fabric has
2316 * failed command before I/O submission.
2318 if (cmd
->state_active
) {
2319 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2320 target_remove_from_state_list(cmd
);
2321 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2325 transport_lun_remove_cmd(cmd
);
2327 ret
= transport_put_cmd(cmd
);
2331 EXPORT_SYMBOL(transport_generic_free_cmd
);
2333 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
2334 * @se_sess: session to reference
2335 * @se_cmd: command descriptor to add
2336 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2338 int target_get_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
,
2341 unsigned long flags
;
2345 * Add a second kref if the fabric caller is expecting to handle
2346 * fabric acknowledgement that requires two target_put_sess_cmd()
2347 * invocations before se_cmd descriptor release.
2349 if (ack_kref
== true) {
2350 kref_get(&se_cmd
->cmd_kref
);
2351 se_cmd
->se_cmd_flags
|= SCF_ACK_KREF
;
2354 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2355 if (se_sess
->sess_tearing_down
) {
2359 list_add_tail(&se_cmd
->se_cmd_list
, &se_sess
->sess_cmd_list
);
2361 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2363 if (ret
&& ack_kref
)
2364 target_put_sess_cmd(se_sess
, se_cmd
);
2368 EXPORT_SYMBOL(target_get_sess_cmd
);
2370 static void target_release_cmd_kref(struct kref
*kref
)
2372 struct se_cmd
*se_cmd
= container_of(kref
, struct se_cmd
, cmd_kref
);
2373 struct se_session
*se_sess
= se_cmd
->se_sess
;
2375 if (list_empty(&se_cmd
->se_cmd_list
)) {
2376 spin_unlock(&se_sess
->sess_cmd_lock
);
2377 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2380 if (se_sess
->sess_tearing_down
&& se_cmd
->cmd_wait_set
) {
2381 spin_unlock(&se_sess
->sess_cmd_lock
);
2382 complete(&se_cmd
->cmd_wait_comp
);
2385 list_del(&se_cmd
->se_cmd_list
);
2386 spin_unlock(&se_sess
->sess_cmd_lock
);
2388 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2391 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2392 * @se_sess: session to reference
2393 * @se_cmd: command descriptor to drop
2395 int target_put_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
)
2398 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2401 return kref_put_spinlock_irqsave(&se_cmd
->cmd_kref
, target_release_cmd_kref
,
2402 &se_sess
->sess_cmd_lock
);
2404 EXPORT_SYMBOL(target_put_sess_cmd
);
2406 /* target_sess_cmd_list_set_waiting - Flag all commands in
2407 * sess_cmd_list to complete cmd_wait_comp. Set
2408 * sess_tearing_down so no more commands are queued.
2409 * @se_sess: session to flag
2411 void target_sess_cmd_list_set_waiting(struct se_session
*se_sess
)
2413 struct se_cmd
*se_cmd
;
2414 unsigned long flags
;
2416 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2417 if (se_sess
->sess_tearing_down
) {
2418 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2421 se_sess
->sess_tearing_down
= 1;
2422 list_splice_init(&se_sess
->sess_cmd_list
, &se_sess
->sess_wait_list
);
2424 list_for_each_entry(se_cmd
, &se_sess
->sess_wait_list
, se_cmd_list
)
2425 se_cmd
->cmd_wait_set
= 1;
2427 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2429 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting
);
2431 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
2432 * @se_sess: session to wait for active I/O
2434 void target_wait_for_sess_cmds(struct se_session
*se_sess
)
2436 struct se_cmd
*se_cmd
, *tmp_cmd
;
2437 unsigned long flags
;
2439 list_for_each_entry_safe(se_cmd
, tmp_cmd
,
2440 &se_sess
->sess_wait_list
, se_cmd_list
) {
2441 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2442 " %d\n", se_cmd
, se_cmd
->t_state
,
2443 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2445 wait_for_completion(&se_cmd
->cmd_wait_comp
);
2446 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2447 " fabric state: %d\n", se_cmd
, se_cmd
->t_state
,
2448 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2450 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2453 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2454 WARN_ON(!list_empty(&se_sess
->sess_cmd_list
));
2455 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2458 EXPORT_SYMBOL(target_wait_for_sess_cmds
);
2460 /* transport_lun_wait_for_tasks():
2462 * Called from ConfigFS context to stop the passed struct se_cmd to allow
2463 * an struct se_lun to be successfully shutdown.
2465 static int transport_lun_wait_for_tasks(struct se_cmd
*cmd
, struct se_lun
*lun
)
2467 unsigned long flags
;
2471 * If the frontend has already requested this struct se_cmd to
2472 * be stopped, we can safely ignore this struct se_cmd.
2474 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2475 if (cmd
->transport_state
& CMD_T_STOP
) {
2476 cmd
->transport_state
&= ~CMD_T_LUN_STOP
;
2478 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
2479 cmd
->se_tfo
->get_task_tag(cmd
));
2480 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2481 transport_cmd_check_stop(cmd
, false, false);
2484 cmd
->transport_state
|= CMD_T_LUN_FE_STOP
;
2485 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2487 // XXX: audit task_flags checks.
2488 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2489 if ((cmd
->transport_state
& CMD_T_BUSY
) &&
2490 (cmd
->transport_state
& CMD_T_SENT
)) {
2491 if (!target_stop_cmd(cmd
, &flags
))
2494 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2496 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
2499 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
2500 cmd
->se_tfo
->get_task_tag(cmd
));
2501 wait_for_completion(&cmd
->transport_lun_stop_comp
);
2502 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
2503 cmd
->se_tfo
->get_task_tag(cmd
));
2509 static void __transport_clear_lun_from_sessions(struct se_lun
*lun
)
2511 struct se_cmd
*cmd
= NULL
;
2512 unsigned long lun_flags
, cmd_flags
;
2514 * Do exception processing and return CHECK_CONDITION status to the
2517 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2518 while (!list_empty(&lun
->lun_cmd_list
)) {
2519 cmd
= list_first_entry(&lun
->lun_cmd_list
,
2520 struct se_cmd
, se_lun_node
);
2521 list_del_init(&cmd
->se_lun_node
);
2523 spin_lock(&cmd
->t_state_lock
);
2524 pr_debug("SE_LUN[%d] - Setting cmd->transport"
2525 "_lun_stop for ITT: 0x%08x\n",
2526 cmd
->se_lun
->unpacked_lun
,
2527 cmd
->se_tfo
->get_task_tag(cmd
));
2528 cmd
->transport_state
|= CMD_T_LUN_STOP
;
2529 spin_unlock(&cmd
->t_state_lock
);
2531 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
2534 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
2535 cmd
->se_tfo
->get_task_tag(cmd
),
2536 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
2540 * If the Storage engine still owns the iscsi_cmd_t, determine
2541 * and/or stop its context.
2543 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
2544 "_lun_wait_for_tasks()\n", cmd
->se_lun
->unpacked_lun
,
2545 cmd
->se_tfo
->get_task_tag(cmd
));
2547 if (transport_lun_wait_for_tasks(cmd
, cmd
->se_lun
) < 0) {
2548 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2552 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
2553 "_wait_for_tasks(): SUCCESS\n",
2554 cmd
->se_lun
->unpacked_lun
,
2555 cmd
->se_tfo
->get_task_tag(cmd
));
2557 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
2558 if (!(cmd
->transport_state
& CMD_T_DEV_ACTIVE
)) {
2559 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
2562 cmd
->transport_state
&= ~CMD_T_DEV_ACTIVE
;
2563 target_remove_from_state_list(cmd
);
2564 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
2567 * The Storage engine stopped this struct se_cmd before it was
2568 * send to the fabric frontend for delivery back to the
2569 * Initiator Node. Return this SCSI CDB back with an
2570 * CHECK_CONDITION status.
2573 transport_send_check_condition_and_sense(cmd
,
2574 TCM_NON_EXISTENT_LUN
, 0);
2576 * If the fabric frontend is waiting for this iscsi_cmd_t to
2577 * be released, notify the waiting thread now that LU has
2578 * finished accessing it.
2580 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
2581 if (cmd
->transport_state
& CMD_T_LUN_FE_STOP
) {
2582 pr_debug("SE_LUN[%d] - Detected FE stop for"
2583 " struct se_cmd: %p ITT: 0x%08x\n",
2585 cmd
, cmd
->se_tfo
->get_task_tag(cmd
));
2587 spin_unlock_irqrestore(&cmd
->t_state_lock
,
2589 transport_cmd_check_stop(cmd
, false, false);
2590 complete(&cmd
->transport_lun_fe_stop_comp
);
2591 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2594 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
2595 lun
->unpacked_lun
, cmd
->se_tfo
->get_task_tag(cmd
));
2597 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
2598 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2600 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
2603 static int transport_clear_lun_thread(void *p
)
2605 struct se_lun
*lun
= p
;
2607 __transport_clear_lun_from_sessions(lun
);
2608 complete(&lun
->lun_shutdown_comp
);
2613 int transport_clear_lun_from_sessions(struct se_lun
*lun
)
2615 struct task_struct
*kt
;
2617 kt
= kthread_run(transport_clear_lun_thread
, lun
,
2618 "tcm_cl_%u", lun
->unpacked_lun
);
2620 pr_err("Unable to start clear_lun thread\n");
2623 wait_for_completion(&lun
->lun_shutdown_comp
);
2629 * transport_wait_for_tasks - wait for completion to occur
2630 * @cmd: command to wait
2632 * Called from frontend fabric context to wait for storage engine
2633 * to pause and/or release frontend generated struct se_cmd.
2635 bool transport_wait_for_tasks(struct se_cmd
*cmd
)
2637 unsigned long flags
;
2639 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2640 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
) &&
2641 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2642 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2646 if (!(cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
) &&
2647 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2648 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2652 * If we are already stopped due to an external event (ie: LUN shutdown)
2653 * sleep until the connection can have the passed struct se_cmd back.
2654 * The cmd->transport_lun_stopped_sem will be upped by
2655 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2656 * has completed its operation on the struct se_cmd.
2658 if (cmd
->transport_state
& CMD_T_LUN_STOP
) {
2659 pr_debug("wait_for_tasks: Stopping"
2660 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
2661 "_stop_comp); for ITT: 0x%08x\n",
2662 cmd
->se_tfo
->get_task_tag(cmd
));
2664 * There is a special case for WRITES where a FE exception +
2665 * LUN shutdown means ConfigFS context is still sleeping on
2666 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2667 * We go ahead and up transport_lun_stop_comp just to be sure
2670 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2671 complete(&cmd
->transport_lun_stop_comp
);
2672 wait_for_completion(&cmd
->transport_lun_fe_stop_comp
);
2673 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2675 target_remove_from_state_list(cmd
);
2677 * At this point, the frontend who was the originator of this
2678 * struct se_cmd, now owns the structure and can be released through
2679 * normal means below.
2681 pr_debug("wait_for_tasks: Stopped"
2682 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
2683 "stop_comp); for ITT: 0x%08x\n",
2684 cmd
->se_tfo
->get_task_tag(cmd
));
2686 cmd
->transport_state
&= ~CMD_T_LUN_STOP
;
2689 if (!(cmd
->transport_state
& CMD_T_ACTIVE
)) {
2690 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2694 cmd
->transport_state
|= CMD_T_STOP
;
2696 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
2697 " i_state: %d, t_state: %d, CMD_T_STOP\n",
2698 cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
2699 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
2701 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2703 wait_for_completion(&cmd
->t_transport_stop_comp
);
2705 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2706 cmd
->transport_state
&= ~(CMD_T_ACTIVE
| CMD_T_STOP
);
2708 pr_debug("wait_for_tasks: Stopped wait_for_completion("
2709 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
2710 cmd
->se_tfo
->get_task_tag(cmd
));
2712 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2716 EXPORT_SYMBOL(transport_wait_for_tasks
);
2718 static int transport_get_sense_codes(
2723 *asc
= cmd
->scsi_asc
;
2724 *ascq
= cmd
->scsi_ascq
;
2730 transport_send_check_condition_and_sense(struct se_cmd
*cmd
,
2731 sense_reason_t reason
, int from_transport
)
2733 unsigned char *buffer
= cmd
->sense_buffer
;
2734 unsigned long flags
;
2735 u8 asc
= 0, ascq
= 0;
2737 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2738 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
2739 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2742 cmd
->se_cmd_flags
|= SCF_SENT_CHECK_CONDITION
;
2743 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2745 if (!reason
&& from_transport
)
2748 if (!from_transport
)
2749 cmd
->se_cmd_flags
|= SCF_EMULATED_TASK_SENSE
;
2752 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2753 * SENSE KEY values from include/scsi/scsi.h
2759 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2761 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2762 /* NO ADDITIONAL SENSE INFORMATION */
2763 buffer
[SPC_ASC_KEY_OFFSET
] = 0;
2764 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0;
2766 case TCM_NON_EXISTENT_LUN
:
2769 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2770 /* ILLEGAL REQUEST */
2771 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2772 /* LOGICAL UNIT NOT SUPPORTED */
2773 buffer
[SPC_ASC_KEY_OFFSET
] = 0x25;
2775 case TCM_UNSUPPORTED_SCSI_OPCODE
:
2776 case TCM_SECTOR_COUNT_TOO_MANY
:
2779 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2780 /* ILLEGAL REQUEST */
2781 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2782 /* INVALID COMMAND OPERATION CODE */
2783 buffer
[SPC_ASC_KEY_OFFSET
] = 0x20;
2785 case TCM_UNKNOWN_MODE_PAGE
:
2788 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2789 /* ILLEGAL REQUEST */
2790 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2791 /* INVALID FIELD IN CDB */
2792 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2794 case TCM_CHECK_CONDITION_ABORT_CMD
:
2797 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2798 /* ABORTED COMMAND */
2799 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2800 /* BUS DEVICE RESET FUNCTION OCCURRED */
2801 buffer
[SPC_ASC_KEY_OFFSET
] = 0x29;
2802 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x03;
2804 case TCM_INCORRECT_AMOUNT_OF_DATA
:
2807 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2808 /* ABORTED COMMAND */
2809 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2811 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2812 /* NOT ENOUGH UNSOLICITED DATA */
2813 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0d;
2815 case TCM_INVALID_CDB_FIELD
:
2818 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2819 /* ILLEGAL REQUEST */
2820 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2821 /* INVALID FIELD IN CDB */
2822 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2824 case TCM_INVALID_PARAMETER_LIST
:
2827 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2828 /* ILLEGAL REQUEST */
2829 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2830 /* INVALID FIELD IN PARAMETER LIST */
2831 buffer
[SPC_ASC_KEY_OFFSET
] = 0x26;
2833 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
2836 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2837 /* ILLEGAL REQUEST */
2838 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2839 /* PARAMETER LIST LENGTH ERROR */
2840 buffer
[SPC_ASC_KEY_OFFSET
] = 0x1a;
2842 case TCM_UNEXPECTED_UNSOLICITED_DATA
:
2845 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2846 /* ABORTED COMMAND */
2847 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2849 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2850 /* UNEXPECTED_UNSOLICITED_DATA */
2851 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0c;
2853 case TCM_SERVICE_CRC_ERROR
:
2856 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2857 /* ABORTED COMMAND */
2858 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2859 /* PROTOCOL SERVICE CRC ERROR */
2860 buffer
[SPC_ASC_KEY_OFFSET
] = 0x47;
2862 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x05;
2864 case TCM_SNACK_REJECTED
:
2867 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2868 /* ABORTED COMMAND */
2869 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2871 buffer
[SPC_ASC_KEY_OFFSET
] = 0x11;
2872 /* FAILED RETRANSMISSION REQUEST */
2873 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x13;
2875 case TCM_WRITE_PROTECTED
:
2878 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2880 buffer
[SPC_SENSE_KEY_OFFSET
] = DATA_PROTECT
;
2881 /* WRITE PROTECTED */
2882 buffer
[SPC_ASC_KEY_OFFSET
] = 0x27;
2884 case TCM_ADDRESS_OUT_OF_RANGE
:
2887 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2888 /* ILLEGAL REQUEST */
2889 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2890 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2891 buffer
[SPC_ASC_KEY_OFFSET
] = 0x21;
2893 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
2896 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2897 /* UNIT ATTENTION */
2898 buffer
[SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
2899 core_scsi3_ua_for_check_condition(cmd
, &asc
, &ascq
);
2900 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2901 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2903 case TCM_CHECK_CONDITION_NOT_READY
:
2906 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2908 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2909 transport_get_sense_codes(cmd
, &asc
, &ascq
);
2910 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2911 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2913 case TCM_MISCOMPARE_VERIFY
:
2916 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2917 buffer
[SPC_SENSE_KEY_OFFSET
] = MISCOMPARE
;
2918 /* MISCOMPARE DURING VERIFY OPERATION */
2919 buffer
[SPC_ASC_KEY_OFFSET
] = 0x1d;
2920 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x00;
2922 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
2926 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2928 * Returning ILLEGAL REQUEST would cause immediate IO errors on
2929 * Solaris initiators. Returning NOT READY instead means the
2930 * operations will be retried a finite number of times and we
2931 * can survive intermittent errors.
2933 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2934 /* LOGICAL UNIT COMMUNICATION FAILURE */
2935 buffer
[SPC_ASC_KEY_OFFSET
] = 0x08;
2939 * This code uses linux/include/scsi/scsi.h SAM status codes!
2941 cmd
->scsi_status
= SAM_STAT_CHECK_CONDITION
;
2943 * Automatically padded, this value is encoded in the fabric's
2944 * data_length response PDU containing the SCSI defined sense data.
2946 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
2949 trace_target_cmd_complete(cmd
);
2950 return cmd
->se_tfo
->queue_status(cmd
);
2952 EXPORT_SYMBOL(transport_send_check_condition_and_sense
);
2954 int transport_check_aborted_status(struct se_cmd
*cmd
, int send_status
)
2956 if (!(cmd
->transport_state
& CMD_T_ABORTED
))
2959 if (!send_status
|| (cmd
->se_cmd_flags
& SCF_SENT_DELAYED_TAS
))
2962 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB: 0x%02x ITT: 0x%08x\n",
2963 cmd
->t_task_cdb
[0], cmd
->se_tfo
->get_task_tag(cmd
));
2965 cmd
->se_cmd_flags
|= SCF_SENT_DELAYED_TAS
;
2966 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
2967 trace_target_cmd_complete(cmd
);
2968 cmd
->se_tfo
->queue_status(cmd
);
2972 EXPORT_SYMBOL(transport_check_aborted_status
);
2974 void transport_send_task_abort(struct se_cmd
*cmd
)
2976 unsigned long flags
;
2978 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2979 if (cmd
->se_cmd_flags
& (SCF_SENT_CHECK_CONDITION
| SCF_SENT_DELAYED_TAS
)) {
2980 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2983 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2986 * If there are still expected incoming fabric WRITEs, we wait
2987 * until until they have completed before sending a TASK_ABORTED
2988 * response. This response with TASK_ABORTED status will be
2989 * queued back to fabric module by transport_check_aborted_status().
2991 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
2992 if (cmd
->se_tfo
->write_pending_status(cmd
) != 0) {
2993 cmd
->transport_state
|= CMD_T_ABORTED
;
2994 smp_mb__after_atomic_inc();
2998 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
3000 transport_lun_remove_cmd(cmd
);
3002 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
3003 " ITT: 0x%08x\n", cmd
->t_task_cdb
[0],
3004 cmd
->se_tfo
->get_task_tag(cmd
));
3006 trace_target_cmd_complete(cmd
);
3007 cmd
->se_tfo
->queue_status(cmd
);
3010 static void target_tmr_work(struct work_struct
*work
)
3012 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
3013 struct se_device
*dev
= cmd
->se_dev
;
3014 struct se_tmr_req
*tmr
= cmd
->se_tmr_req
;
3017 switch (tmr
->function
) {
3018 case TMR_ABORT_TASK
:
3019 core_tmr_abort_task(dev
, tmr
, cmd
->se_sess
);
3021 case TMR_ABORT_TASK_SET
:
3023 case TMR_CLEAR_TASK_SET
:
3024 tmr
->response
= TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
3027 ret
= core_tmr_lun_reset(dev
, tmr
, NULL
, NULL
);
3028 tmr
->response
= (!ret
) ? TMR_FUNCTION_COMPLETE
:
3029 TMR_FUNCTION_REJECTED
;
3031 case TMR_TARGET_WARM_RESET
:
3032 tmr
->response
= TMR_FUNCTION_REJECTED
;
3034 case TMR_TARGET_COLD_RESET
:
3035 tmr
->response
= TMR_FUNCTION_REJECTED
;
3038 pr_err("Uknown TMR function: 0x%02x.\n",
3040 tmr
->response
= TMR_FUNCTION_REJECTED
;
3044 cmd
->t_state
= TRANSPORT_ISTATE_PROCESSING
;
3045 cmd
->se_tfo
->queue_tm_rsp(cmd
);
3047 transport_cmd_check_stop_to_fabric(cmd
);
3050 int transport_generic_handle_tmr(
3053 unsigned long flags
;
3055 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3056 cmd
->transport_state
|= CMD_T_ACTIVE
;
3057 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3059 INIT_WORK(&cmd
->work
, target_tmr_work
);
3060 queue_work(cmd
->se_dev
->tmr_wq
, &cmd
->work
);
3063 EXPORT_SYMBOL(transport_generic_handle_tmr
);