1 /*******************************************************************************
2 * Filename: target_core_transport.c
4 * This file contains the Generic Target Engine Core.
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
11 * Nicholas A. Bellinger <nab@kernel.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ******************************************************************************/
29 #include <linux/version.h>
30 #include <linux/net.h>
31 #include <linux/delay.h>
32 #include <linux/string.h>
33 #include <linux/timer.h>
34 #include <linux/slab.h>
35 #include <linux/blkdev.h>
36 #include <linux/spinlock.h>
37 #include <linux/kthread.h>
39 #include <linux/cdrom.h>
40 #include <asm/unaligned.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_tcq.h>
47 #include <target/target_core_base.h>
48 #include <target/target_core_device.h>
49 #include <target/target_core_tmr.h>
50 #include <target/target_core_tpg.h>
51 #include <target/target_core_transport.h>
52 #include <target/target_core_fabric_ops.h>
53 #include <target/target_core_configfs.h>
55 #include "target_core_alua.h"
56 #include "target_core_hba.h"
57 #include "target_core_pr.h"
58 #include "target_core_scdb.h"
59 #include "target_core_ua.h"
61 static int sub_api_initialized
;
63 static struct kmem_cache
*se_cmd_cache
;
64 static struct kmem_cache
*se_sess_cache
;
65 struct kmem_cache
*se_tmr_req_cache
;
66 struct kmem_cache
*se_ua_cache
;
67 struct kmem_cache
*t10_pr_reg_cache
;
68 struct kmem_cache
*t10_alua_lu_gp_cache
;
69 struct kmem_cache
*t10_alua_lu_gp_mem_cache
;
70 struct kmem_cache
*t10_alua_tg_pt_gp_cache
;
71 struct kmem_cache
*t10_alua_tg_pt_gp_mem_cache
;
73 /* Used for transport_dev_get_map_*() */
74 typedef int (*map_func_t
)(struct se_task
*, u32
);
76 static int transport_generic_write_pending(struct se_cmd
*);
77 static int transport_processing_thread(void *param
);
78 static int __transport_execute_tasks(struct se_device
*dev
);
79 static void transport_complete_task_attr(struct se_cmd
*cmd
);
80 static int transport_complete_qf(struct se_cmd
*cmd
);
81 static void transport_handle_queue_full(struct se_cmd
*cmd
,
82 struct se_device
*dev
, int (*qf_callback
)(struct se_cmd
*));
83 static void transport_direct_request_timeout(struct se_cmd
*cmd
);
84 static void transport_free_dev_tasks(struct se_cmd
*cmd
);
85 static u32
transport_allocate_tasks(struct se_cmd
*cmd
,
86 unsigned long long starting_lba
,
87 enum dma_data_direction data_direction
,
88 struct scatterlist
*sgl
, unsigned int nents
);
89 static int transport_generic_get_mem(struct se_cmd
*cmd
);
90 static int transport_generic_remove(struct se_cmd
*cmd
,
91 int session_reinstatement
);
92 static void transport_release_fe_cmd(struct se_cmd
*cmd
);
93 static void transport_remove_cmd_from_queue(struct se_cmd
*cmd
,
94 struct se_queue_obj
*qobj
);
95 static int transport_set_sense_codes(struct se_cmd
*cmd
, u8 asc
, u8 ascq
);
96 static void transport_stop_all_task_timers(struct se_cmd
*cmd
);
98 int init_se_kmem_caches(void)
100 se_cmd_cache
= kmem_cache_create("se_cmd_cache",
101 sizeof(struct se_cmd
), __alignof__(struct se_cmd
), 0, NULL
);
103 pr_err("kmem_cache_create for struct se_cmd failed\n");
106 se_tmr_req_cache
= kmem_cache_create("se_tmr_cache",
107 sizeof(struct se_tmr_req
), __alignof__(struct se_tmr_req
),
109 if (!se_tmr_req_cache
) {
110 pr_err("kmem_cache_create() for struct se_tmr_req"
114 se_sess_cache
= kmem_cache_create("se_sess_cache",
115 sizeof(struct se_session
), __alignof__(struct se_session
),
117 if (!se_sess_cache
) {
118 pr_err("kmem_cache_create() for struct se_session"
122 se_ua_cache
= kmem_cache_create("se_ua_cache",
123 sizeof(struct se_ua
), __alignof__(struct se_ua
),
126 pr_err("kmem_cache_create() for struct se_ua failed\n");
129 t10_pr_reg_cache
= kmem_cache_create("t10_pr_reg_cache",
130 sizeof(struct t10_pr_registration
),
131 __alignof__(struct t10_pr_registration
), 0, NULL
);
132 if (!t10_pr_reg_cache
) {
133 pr_err("kmem_cache_create() for struct t10_pr_registration"
137 t10_alua_lu_gp_cache
= kmem_cache_create("t10_alua_lu_gp_cache",
138 sizeof(struct t10_alua_lu_gp
), __alignof__(struct t10_alua_lu_gp
),
140 if (!t10_alua_lu_gp_cache
) {
141 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
145 t10_alua_lu_gp_mem_cache
= kmem_cache_create("t10_alua_lu_gp_mem_cache",
146 sizeof(struct t10_alua_lu_gp_member
),
147 __alignof__(struct t10_alua_lu_gp_member
), 0, NULL
);
148 if (!t10_alua_lu_gp_mem_cache
) {
149 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
153 t10_alua_tg_pt_gp_cache
= kmem_cache_create("t10_alua_tg_pt_gp_cache",
154 sizeof(struct t10_alua_tg_pt_gp
),
155 __alignof__(struct t10_alua_tg_pt_gp
), 0, NULL
);
156 if (!t10_alua_tg_pt_gp_cache
) {
157 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
161 t10_alua_tg_pt_gp_mem_cache
= kmem_cache_create(
162 "t10_alua_tg_pt_gp_mem_cache",
163 sizeof(struct t10_alua_tg_pt_gp_member
),
164 __alignof__(struct t10_alua_tg_pt_gp_member
),
166 if (!t10_alua_tg_pt_gp_mem_cache
) {
167 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
175 kmem_cache_destroy(se_cmd_cache
);
176 if (se_tmr_req_cache
)
177 kmem_cache_destroy(se_tmr_req_cache
);
179 kmem_cache_destroy(se_sess_cache
);
181 kmem_cache_destroy(se_ua_cache
);
182 if (t10_pr_reg_cache
)
183 kmem_cache_destroy(t10_pr_reg_cache
);
184 if (t10_alua_lu_gp_cache
)
185 kmem_cache_destroy(t10_alua_lu_gp_cache
);
186 if (t10_alua_lu_gp_mem_cache
)
187 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
188 if (t10_alua_tg_pt_gp_cache
)
189 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
190 if (t10_alua_tg_pt_gp_mem_cache
)
191 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
195 void release_se_kmem_caches(void)
197 kmem_cache_destroy(se_cmd_cache
);
198 kmem_cache_destroy(se_tmr_req_cache
);
199 kmem_cache_destroy(se_sess_cache
);
200 kmem_cache_destroy(se_ua_cache
);
201 kmem_cache_destroy(t10_pr_reg_cache
);
202 kmem_cache_destroy(t10_alua_lu_gp_cache
);
203 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
204 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
205 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
208 /* This code ensures unique mib indexes are handed out. */
209 static DEFINE_SPINLOCK(scsi_mib_index_lock
);
210 static u32 scsi_mib_index
[SCSI_INDEX_TYPE_MAX
];
213 * Allocate a new row index for the entry type specified
215 u32
scsi_get_new_index(scsi_index_t type
)
219 BUG_ON((type
< 0) || (type
>= SCSI_INDEX_TYPE_MAX
));
221 spin_lock(&scsi_mib_index_lock
);
222 new_index
= ++scsi_mib_index
[type
];
223 spin_unlock(&scsi_mib_index_lock
);
228 void transport_init_queue_obj(struct se_queue_obj
*qobj
)
230 atomic_set(&qobj
->queue_cnt
, 0);
231 INIT_LIST_HEAD(&qobj
->qobj_list
);
232 init_waitqueue_head(&qobj
->thread_wq
);
233 spin_lock_init(&qobj
->cmd_queue_lock
);
235 EXPORT_SYMBOL(transport_init_queue_obj
);
237 static int transport_subsystem_reqmods(void)
241 ret
= request_module("target_core_iblock");
243 pr_err("Unable to load target_core_iblock\n");
245 ret
= request_module("target_core_file");
247 pr_err("Unable to load target_core_file\n");
249 ret
= request_module("target_core_pscsi");
251 pr_err("Unable to load target_core_pscsi\n");
253 ret
= request_module("target_core_stgt");
255 pr_err("Unable to load target_core_stgt\n");
260 int transport_subsystem_check_init(void)
264 if (sub_api_initialized
)
267 * Request the loading of known TCM subsystem plugins..
269 ret
= transport_subsystem_reqmods();
273 sub_api_initialized
= 1;
277 struct se_session
*transport_init_session(void)
279 struct se_session
*se_sess
;
281 se_sess
= kmem_cache_zalloc(se_sess_cache
, GFP_KERNEL
);
283 pr_err("Unable to allocate struct se_session from"
285 return ERR_PTR(-ENOMEM
);
287 INIT_LIST_HEAD(&se_sess
->sess_list
);
288 INIT_LIST_HEAD(&se_sess
->sess_acl_list
);
292 EXPORT_SYMBOL(transport_init_session
);
295 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
297 void __transport_register_session(
298 struct se_portal_group
*se_tpg
,
299 struct se_node_acl
*se_nacl
,
300 struct se_session
*se_sess
,
301 void *fabric_sess_ptr
)
303 unsigned char buf
[PR_REG_ISID_LEN
];
305 se_sess
->se_tpg
= se_tpg
;
306 se_sess
->fabric_sess_ptr
= fabric_sess_ptr
;
308 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
310 * Only set for struct se_session's that will actually be moving I/O.
311 * eg: *NOT* discovery sessions.
315 * If the fabric module supports an ISID based TransportID,
316 * save this value in binary from the fabric I_T Nexus now.
318 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
319 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
320 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
,
321 &buf
[0], PR_REG_ISID_LEN
);
322 se_sess
->sess_bin_isid
= get_unaligned_be64(&buf
[0]);
324 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
326 * The se_nacl->nacl_sess pointer will be set to the
327 * last active I_T Nexus for each struct se_node_acl.
329 se_nacl
->nacl_sess
= se_sess
;
331 list_add_tail(&se_sess
->sess_acl_list
,
332 &se_nacl
->acl_sess_list
);
333 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
335 list_add_tail(&se_sess
->sess_list
, &se_tpg
->tpg_sess_list
);
337 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
338 se_tpg
->se_tpg_tfo
->get_fabric_name(), se_sess
->fabric_sess_ptr
);
340 EXPORT_SYMBOL(__transport_register_session
);
342 void transport_register_session(
343 struct se_portal_group
*se_tpg
,
344 struct se_node_acl
*se_nacl
,
345 struct se_session
*se_sess
,
346 void *fabric_sess_ptr
)
348 spin_lock_bh(&se_tpg
->session_lock
);
349 __transport_register_session(se_tpg
, se_nacl
, se_sess
, fabric_sess_ptr
);
350 spin_unlock_bh(&se_tpg
->session_lock
);
352 EXPORT_SYMBOL(transport_register_session
);
354 void transport_deregister_session_configfs(struct se_session
*se_sess
)
356 struct se_node_acl
*se_nacl
;
359 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
361 se_nacl
= se_sess
->se_node_acl
;
363 spin_lock_irqsave(&se_nacl
->nacl_sess_lock
, flags
);
364 list_del(&se_sess
->sess_acl_list
);
366 * If the session list is empty, then clear the pointer.
367 * Otherwise, set the struct se_session pointer from the tail
368 * element of the per struct se_node_acl active session list.
370 if (list_empty(&se_nacl
->acl_sess_list
))
371 se_nacl
->nacl_sess
= NULL
;
373 se_nacl
->nacl_sess
= container_of(
374 se_nacl
->acl_sess_list
.prev
,
375 struct se_session
, sess_acl_list
);
377 spin_unlock_irqrestore(&se_nacl
->nacl_sess_lock
, flags
);
380 EXPORT_SYMBOL(transport_deregister_session_configfs
);
382 void transport_free_session(struct se_session
*se_sess
)
384 kmem_cache_free(se_sess_cache
, se_sess
);
386 EXPORT_SYMBOL(transport_free_session
);
388 void transport_deregister_session(struct se_session
*se_sess
)
390 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
391 struct se_node_acl
*se_nacl
;
394 transport_free_session(se_sess
);
398 spin_lock_bh(&se_tpg
->session_lock
);
399 list_del(&se_sess
->sess_list
);
400 se_sess
->se_tpg
= NULL
;
401 se_sess
->fabric_sess_ptr
= NULL
;
402 spin_unlock_bh(&se_tpg
->session_lock
);
405 * Determine if we need to do extra work for this initiator node's
406 * struct se_node_acl if it had been previously dynamically generated.
408 se_nacl
= se_sess
->se_node_acl
;
410 spin_lock_bh(&se_tpg
->acl_node_lock
);
411 if (se_nacl
->dynamic_node_acl
) {
412 if (!se_tpg
->se_tpg_tfo
->tpg_check_demo_mode_cache(
414 list_del(&se_nacl
->acl_list
);
415 se_tpg
->num_node_acls
--;
416 spin_unlock_bh(&se_tpg
->acl_node_lock
);
418 core_tpg_wait_for_nacl_pr_ref(se_nacl
);
419 core_free_device_list_for_node(se_nacl
, se_tpg
);
420 se_tpg
->se_tpg_tfo
->tpg_release_fabric_acl(se_tpg
,
422 spin_lock_bh(&se_tpg
->acl_node_lock
);
425 spin_unlock_bh(&se_tpg
->acl_node_lock
);
428 transport_free_session(se_sess
);
430 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
431 se_tpg
->se_tpg_tfo
->get_fabric_name());
433 EXPORT_SYMBOL(transport_deregister_session
);
436 * Called with cmd->t_state_lock held.
438 static void transport_all_task_dev_remove_state(struct se_cmd
*cmd
)
440 struct se_device
*dev
;
441 struct se_task
*task
;
444 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
449 if (atomic_read(&task
->task_active
))
452 if (!atomic_read(&task
->task_state_active
))
455 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
456 list_del(&task
->t_state_list
);
457 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
458 cmd
->se_tfo
->get_task_tag(cmd
), dev
, task
);
459 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
461 atomic_set(&task
->task_state_active
, 0);
462 atomic_dec(&cmd
->t_task_cdbs_ex_left
);
466 /* transport_cmd_check_stop():
468 * 'transport_off = 1' determines if t_transport_active should be cleared.
469 * 'transport_off = 2' determines if task_dev_state should be removed.
471 * A non-zero u8 t_state sets cmd->t_state.
472 * Returns 1 when command is stopped, else 0.
474 static int transport_cmd_check_stop(
481 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
483 * Determine if IOCTL context caller in requesting the stopping of this
484 * command for LUN shutdown purposes.
486 if (atomic_read(&cmd
->transport_lun_stop
)) {
487 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
488 " == TRUE for ITT: 0x%08x\n", __func__
, __LINE__
,
489 cmd
->se_tfo
->get_task_tag(cmd
));
491 cmd
->deferred_t_state
= cmd
->t_state
;
492 cmd
->t_state
= TRANSPORT_DEFERRED_CMD
;
493 atomic_set(&cmd
->t_transport_active
, 0);
494 if (transport_off
== 2)
495 transport_all_task_dev_remove_state(cmd
);
496 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
498 complete(&cmd
->transport_lun_stop_comp
);
502 * Determine if frontend context caller is requesting the stopping of
503 * this command for frontend exceptions.
505 if (atomic_read(&cmd
->t_transport_stop
)) {
506 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
507 " TRUE for ITT: 0x%08x\n", __func__
, __LINE__
,
508 cmd
->se_tfo
->get_task_tag(cmd
));
510 cmd
->deferred_t_state
= cmd
->t_state
;
511 cmd
->t_state
= TRANSPORT_DEFERRED_CMD
;
512 if (transport_off
== 2)
513 transport_all_task_dev_remove_state(cmd
);
516 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
519 if (transport_off
== 2)
521 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
523 complete(&cmd
->t_transport_stop_comp
);
527 atomic_set(&cmd
->t_transport_active
, 0);
528 if (transport_off
== 2) {
529 transport_all_task_dev_remove_state(cmd
);
531 * Clear struct se_cmd->se_lun before the transport_off == 2
532 * handoff to fabric module.
536 * Some fabric modules like tcm_loop can release
537 * their internally allocated I/O reference now and
540 if (cmd
->se_tfo
->check_stop_free
!= NULL
) {
541 spin_unlock_irqrestore(
542 &cmd
->t_state_lock
, flags
);
544 cmd
->se_tfo
->check_stop_free(cmd
);
548 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
552 cmd
->t_state
= t_state
;
553 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
558 static int transport_cmd_check_stop_to_fabric(struct se_cmd
*cmd
)
560 return transport_cmd_check_stop(cmd
, 2, 0);
563 static void transport_lun_remove_cmd(struct se_cmd
*cmd
)
565 struct se_lun
*lun
= cmd
->se_lun
;
571 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
572 if (!atomic_read(&cmd
->transport_dev_active
)) {
573 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
576 atomic_set(&cmd
->transport_dev_active
, 0);
577 transport_all_task_dev_remove_state(cmd
);
578 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
582 spin_lock_irqsave(&lun
->lun_cmd_lock
, flags
);
583 if (atomic_read(&cmd
->transport_lun_active
)) {
584 list_del(&cmd
->se_lun_node
);
585 atomic_set(&cmd
->transport_lun_active
, 0);
587 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
588 cmd
->se_tfo
->get_task_tag(cmd
), lun
->unpacked_lun
);
591 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, flags
);
594 void transport_cmd_finish_abort(struct se_cmd
*cmd
, int remove
)
596 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
597 transport_lun_remove_cmd(cmd
);
599 if (transport_cmd_check_stop_to_fabric(cmd
))
602 transport_generic_remove(cmd
, 0);
605 void transport_cmd_finish_abort_tmr(struct se_cmd
*cmd
)
607 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
609 if (transport_cmd_check_stop_to_fabric(cmd
))
612 transport_generic_remove(cmd
, 0);
615 static void transport_add_cmd_to_queue(
619 struct se_device
*dev
= cmd
->se_dev
;
620 struct se_queue_obj
*qobj
= &dev
->dev_queue_obj
;
623 INIT_LIST_HEAD(&cmd
->se_queue_node
);
626 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
627 cmd
->t_state
= t_state
;
628 atomic_set(&cmd
->t_transport_active
, 1);
629 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
632 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
633 if (cmd
->se_cmd_flags
& SCF_EMULATE_QUEUE_FULL
) {
634 cmd
->se_cmd_flags
&= ~SCF_EMULATE_QUEUE_FULL
;
635 list_add(&cmd
->se_queue_node
, &qobj
->qobj_list
);
637 list_add_tail(&cmd
->se_queue_node
, &qobj
->qobj_list
);
638 atomic_inc(&cmd
->t_transport_queue_active
);
639 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
641 atomic_inc(&qobj
->queue_cnt
);
642 wake_up_interruptible(&qobj
->thread_wq
);
645 static struct se_cmd
*
646 transport_get_cmd_from_queue(struct se_queue_obj
*qobj
)
651 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
652 if (list_empty(&qobj
->qobj_list
)) {
653 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
656 cmd
= list_first_entry(&qobj
->qobj_list
, struct se_cmd
, se_queue_node
);
658 atomic_dec(&cmd
->t_transport_queue_active
);
660 list_del(&cmd
->se_queue_node
);
661 atomic_dec(&qobj
->queue_cnt
);
662 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
667 static void transport_remove_cmd_from_queue(struct se_cmd
*cmd
,
668 struct se_queue_obj
*qobj
)
673 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
674 if (!atomic_read(&cmd
->t_transport_queue_active
)) {
675 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
679 list_for_each_entry(t
, &qobj
->qobj_list
, se_queue_node
)
681 atomic_dec(&cmd
->t_transport_queue_active
);
682 atomic_dec(&qobj
->queue_cnt
);
683 list_del(&cmd
->se_queue_node
);
686 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
688 if (atomic_read(&cmd
->t_transport_queue_active
)) {
689 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
690 cmd
->se_tfo
->get_task_tag(cmd
),
691 atomic_read(&cmd
->t_transport_queue_active
));
696 * Completion function used by TCM subsystem plugins (such as FILEIO)
697 * for queueing up response from struct se_subsystem_api->do_task()
699 void transport_complete_sync_cache(struct se_cmd
*cmd
, int good
)
701 struct se_task
*task
= list_entry(cmd
->t_task_list
.next
,
702 struct se_task
, t_list
);
705 cmd
->scsi_status
= SAM_STAT_GOOD
;
706 task
->task_scsi_status
= GOOD
;
708 task
->task_scsi_status
= SAM_STAT_CHECK_CONDITION
;
709 task
->task_error_status
= PYX_TRANSPORT_ILLEGAL_REQUEST
;
710 task
->task_se_cmd
->transport_error_status
=
711 PYX_TRANSPORT_ILLEGAL_REQUEST
;
714 transport_complete_task(task
, good
);
716 EXPORT_SYMBOL(transport_complete_sync_cache
);
718 /* transport_complete_task():
720 * Called from interrupt and non interrupt context depending
721 * on the transport plugin.
723 void transport_complete_task(struct se_task
*task
, int success
)
725 struct se_cmd
*cmd
= task
->task_se_cmd
;
726 struct se_device
*dev
= task
->se_dev
;
730 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task
,
731 cmd
->t_task_cdb
[0], dev
);
734 atomic_inc(&dev
->depth_left
);
736 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
737 atomic_set(&task
->task_active
, 0);
740 * See if any sense data exists, if so set the TASK_SENSE flag.
741 * Also check for any other post completion work that needs to be
742 * done by the plugins.
744 if (dev
&& dev
->transport
->transport_complete
) {
745 if (dev
->transport
->transport_complete(task
) != 0) {
746 cmd
->se_cmd_flags
|= SCF_TRANSPORT_TASK_SENSE
;
747 task
->task_sense
= 1;
753 * See if we are waiting for outstanding struct se_task
754 * to complete for an exception condition
756 if (atomic_read(&task
->task_stop
)) {
758 * Decrement cmd->t_se_count if this task had
759 * previously thrown its timeout exception handler.
761 if (atomic_read(&task
->task_timeout
)) {
762 atomic_dec(&cmd
->t_se_count
);
763 atomic_set(&task
->task_timeout
, 0);
765 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
767 complete(&task
->task_stop_comp
);
771 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
772 * left counter to determine when the struct se_cmd is ready to be queued to
773 * the processing thread.
775 if (atomic_read(&task
->task_timeout
)) {
776 if (!atomic_dec_and_test(
777 &cmd
->t_task_cdbs_timeout_left
)) {
778 spin_unlock_irqrestore(&cmd
->t_state_lock
,
782 t_state
= TRANSPORT_COMPLETE_TIMEOUT
;
783 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
785 transport_add_cmd_to_queue(cmd
, t_state
);
788 atomic_dec(&cmd
->t_task_cdbs_timeout_left
);
791 * Decrement the outstanding t_task_cdbs_left count. The last
792 * struct se_task from struct se_cmd will complete itself into the
793 * device queue depending upon int success.
795 if (!atomic_dec_and_test(&cmd
->t_task_cdbs_left
)) {
797 cmd
->t_tasks_failed
= 1;
799 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
803 if (!success
|| cmd
->t_tasks_failed
) {
804 t_state
= TRANSPORT_COMPLETE_FAILURE
;
805 if (!task
->task_error_status
) {
806 task
->task_error_status
=
807 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
808 cmd
->transport_error_status
=
809 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
812 atomic_set(&cmd
->t_transport_complete
, 1);
813 t_state
= TRANSPORT_COMPLETE_OK
;
815 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
817 transport_add_cmd_to_queue(cmd
, t_state
);
819 EXPORT_SYMBOL(transport_complete_task
);
822 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
823 * struct se_task list are ready to be added to the active execution list
826 * Called with se_dev_t->execute_task_lock called.
828 static inline int transport_add_task_check_sam_attr(
829 struct se_task
*task
,
830 struct se_task
*task_prev
,
831 struct se_device
*dev
)
834 * No SAM Task attribute emulation enabled, add to tail of
837 if (dev
->dev_task_attr_type
!= SAM_TASK_ATTR_EMULATED
) {
838 list_add_tail(&task
->t_execute_list
, &dev
->execute_task_list
);
842 * HEAD_OF_QUEUE attribute for received CDB, which means
843 * the first task that is associated with a struct se_cmd goes to
844 * head of the struct se_device->execute_task_list, and task_prev
845 * after that for each subsequent task
847 if (task
->task_se_cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
848 list_add(&task
->t_execute_list
,
849 (task_prev
!= NULL
) ?
850 &task_prev
->t_execute_list
:
851 &dev
->execute_task_list
);
853 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
854 " in execution queue\n",
855 task
->task_se_cmd
->t_task_cdb
[0]);
859 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
860 * transitioned from Dermant -> Active state, and are added to the end
861 * of the struct se_device->execute_task_list
863 list_add_tail(&task
->t_execute_list
, &dev
->execute_task_list
);
867 /* __transport_add_task_to_execute_queue():
869 * Called with se_dev_t->execute_task_lock called.
871 static void __transport_add_task_to_execute_queue(
872 struct se_task
*task
,
873 struct se_task
*task_prev
,
874 struct se_device
*dev
)
878 head_of_queue
= transport_add_task_check_sam_attr(task
, task_prev
, dev
);
879 atomic_inc(&dev
->execute_tasks
);
881 if (atomic_read(&task
->task_state_active
))
884 * Determine if this task needs to go to HEAD_OF_QUEUE for the
885 * state list as well. Running with SAM Task Attribute emulation
886 * will always return head_of_queue == 0 here
889 list_add(&task
->t_state_list
, (task_prev
) ?
890 &task_prev
->t_state_list
:
891 &dev
->state_task_list
);
893 list_add_tail(&task
->t_state_list
, &dev
->state_task_list
);
895 atomic_set(&task
->task_state_active
, 1);
897 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
898 task
->task_se_cmd
->se_tfo
->get_task_tag(task
->task_se_cmd
),
902 static void transport_add_tasks_to_state_queue(struct se_cmd
*cmd
)
904 struct se_device
*dev
;
905 struct se_task
*task
;
908 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
909 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
912 if (atomic_read(&task
->task_state_active
))
915 spin_lock(&dev
->execute_task_lock
);
916 list_add_tail(&task
->t_state_list
, &dev
->state_task_list
);
917 atomic_set(&task
->task_state_active
, 1);
919 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
920 task
->task_se_cmd
->se_tfo
->get_task_tag(
921 task
->task_se_cmd
), task
, dev
);
923 spin_unlock(&dev
->execute_task_lock
);
925 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
928 static void transport_add_tasks_from_cmd(struct se_cmd
*cmd
)
930 struct se_device
*dev
= cmd
->se_dev
;
931 struct se_task
*task
, *task_prev
= NULL
;
934 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
935 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
936 if (atomic_read(&task
->task_execute_queue
))
939 * __transport_add_task_to_execute_queue() handles the
940 * SAM Task Attribute emulation if enabled
942 __transport_add_task_to_execute_queue(task
, task_prev
, dev
);
943 atomic_set(&task
->task_execute_queue
, 1);
946 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
949 /* transport_remove_task_from_execute_queue():
953 void transport_remove_task_from_execute_queue(
954 struct se_task
*task
,
955 struct se_device
*dev
)
959 if (atomic_read(&task
->task_execute_queue
) == 0) {
964 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
965 list_del(&task
->t_execute_list
);
966 atomic_set(&task
->task_execute_queue
, 0);
967 atomic_dec(&dev
->execute_tasks
);
968 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
972 * Handle QUEUE_FULL / -EAGAIN status
975 static void target_qf_do_work(struct work_struct
*work
)
977 struct se_device
*dev
= container_of(work
, struct se_device
,
979 struct se_cmd
*cmd
, *cmd_tmp
;
981 spin_lock_irq(&dev
->qf_cmd_lock
);
982 list_for_each_entry_safe(cmd
, cmd_tmp
, &dev
->qf_cmd_list
, se_qf_node
) {
984 list_del(&cmd
->se_qf_node
);
985 atomic_dec(&dev
->dev_qf_count
);
986 smp_mb__after_atomic_dec();
987 spin_unlock_irq(&dev
->qf_cmd_lock
);
989 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
990 " context: %s\n", cmd
->se_tfo
->get_fabric_name(), cmd
,
991 (cmd
->t_state
== TRANSPORT_COMPLETE_OK
) ? "COMPLETE_OK" :
992 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
) ? "WRITE_PENDING"
995 * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd
996 * has been added to head of queue
998 transport_add_cmd_to_queue(cmd
, cmd
->t_state
);
1000 spin_lock_irq(&dev
->qf_cmd_lock
);
1002 spin_unlock_irq(&dev
->qf_cmd_lock
);
1005 unsigned char *transport_dump_cmd_direction(struct se_cmd
*cmd
)
1007 switch (cmd
->data_direction
) {
1010 case DMA_FROM_DEVICE
:
1014 case DMA_BIDIRECTIONAL
:
1023 void transport_dump_dev_state(
1024 struct se_device
*dev
,
1028 *bl
+= sprintf(b
+ *bl
, "Status: ");
1029 switch (dev
->dev_status
) {
1030 case TRANSPORT_DEVICE_ACTIVATED
:
1031 *bl
+= sprintf(b
+ *bl
, "ACTIVATED");
1033 case TRANSPORT_DEVICE_DEACTIVATED
:
1034 *bl
+= sprintf(b
+ *bl
, "DEACTIVATED");
1036 case TRANSPORT_DEVICE_SHUTDOWN
:
1037 *bl
+= sprintf(b
+ *bl
, "SHUTDOWN");
1039 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED
:
1040 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED
:
1041 *bl
+= sprintf(b
+ *bl
, "OFFLINE");
1044 *bl
+= sprintf(b
+ *bl
, "UNKNOWN=%d", dev
->dev_status
);
1048 *bl
+= sprintf(b
+ *bl
, " Execute/Left/Max Queue Depth: %d/%d/%d",
1049 atomic_read(&dev
->execute_tasks
), atomic_read(&dev
->depth_left
),
1051 *bl
+= sprintf(b
+ *bl
, " SectorSize: %u MaxSectors: %u\n",
1052 dev
->se_sub_dev
->se_dev_attrib
.block_size
, dev
->se_sub_dev
->se_dev_attrib
.max_sectors
);
1053 *bl
+= sprintf(b
+ *bl
, " ");
1056 /* transport_release_all_cmds():
1060 static void transport_release_all_cmds(struct se_device
*dev
)
1062 struct se_cmd
*cmd
, *tcmd
;
1063 int bug_out
= 0, t_state
;
1064 unsigned long flags
;
1066 spin_lock_irqsave(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1067 list_for_each_entry_safe(cmd
, tcmd
, &dev
->dev_queue_obj
.qobj_list
,
1069 t_state
= cmd
->t_state
;
1070 list_del(&cmd
->se_queue_node
);
1071 spin_unlock_irqrestore(&dev
->dev_queue_obj
.cmd_queue_lock
,
1074 pr_err("Releasing ITT: 0x%08x, i_state: %u,"
1075 " t_state: %u directly\n",
1076 cmd
->se_tfo
->get_task_tag(cmd
),
1077 cmd
->se_tfo
->get_cmd_state(cmd
), t_state
);
1079 transport_release_fe_cmd(cmd
);
1082 spin_lock_irqsave(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1084 spin_unlock_irqrestore(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1091 void transport_dump_vpd_proto_id(
1092 struct t10_vpd
*vpd
,
1093 unsigned char *p_buf
,
1096 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1099 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1100 len
= sprintf(buf
, "T10 VPD Protocol Identifier: ");
1102 switch (vpd
->protocol_identifier
) {
1104 sprintf(buf
+len
, "Fibre Channel\n");
1107 sprintf(buf
+len
, "Parallel SCSI\n");
1110 sprintf(buf
+len
, "SSA\n");
1113 sprintf(buf
+len
, "IEEE 1394\n");
1116 sprintf(buf
+len
, "SCSI Remote Direct Memory Access"
1120 sprintf(buf
+len
, "Internet SCSI (iSCSI)\n");
1123 sprintf(buf
+len
, "SAS Serial SCSI Protocol\n");
1126 sprintf(buf
+len
, "Automation/Drive Interface Transport"
1130 sprintf(buf
+len
, "AT Attachment Interface ATA/ATAPI\n");
1133 sprintf(buf
+len
, "Unknown 0x%02x\n",
1134 vpd
->protocol_identifier
);
1139 strncpy(p_buf
, buf
, p_buf_len
);
1141 pr_debug("%s", buf
);
1145 transport_set_vpd_proto_id(struct t10_vpd
*vpd
, unsigned char *page_83
)
1148 * Check if the Protocol Identifier Valid (PIV) bit is set..
1150 * from spc3r23.pdf section 7.5.1
1152 if (page_83
[1] & 0x80) {
1153 vpd
->protocol_identifier
= (page_83
[0] & 0xf0);
1154 vpd
->protocol_identifier_set
= 1;
1155 transport_dump_vpd_proto_id(vpd
, NULL
, 0);
1158 EXPORT_SYMBOL(transport_set_vpd_proto_id
);
1160 int transport_dump_vpd_assoc(
1161 struct t10_vpd
*vpd
,
1162 unsigned char *p_buf
,
1165 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1169 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1170 len
= sprintf(buf
, "T10 VPD Identifier Association: ");
1172 switch (vpd
->association
) {
1174 sprintf(buf
+len
, "addressed logical unit\n");
1177 sprintf(buf
+len
, "target port\n");
1180 sprintf(buf
+len
, "SCSI target device\n");
1183 sprintf(buf
+len
, "Unknown 0x%02x\n", vpd
->association
);
1189 strncpy(p_buf
, buf
, p_buf_len
);
1191 pr_debug("%s", buf
);
1196 int transport_set_vpd_assoc(struct t10_vpd
*vpd
, unsigned char *page_83
)
1199 * The VPD identification association..
1201 * from spc3r23.pdf Section 7.6.3.1 Table 297
1203 vpd
->association
= (page_83
[1] & 0x30);
1204 return transport_dump_vpd_assoc(vpd
, NULL
, 0);
1206 EXPORT_SYMBOL(transport_set_vpd_assoc
);
1208 int transport_dump_vpd_ident_type(
1209 struct t10_vpd
*vpd
,
1210 unsigned char *p_buf
,
1213 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1217 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1218 len
= sprintf(buf
, "T10 VPD Identifier Type: ");
1220 switch (vpd
->device_identifier_type
) {
1222 sprintf(buf
+len
, "Vendor specific\n");
1225 sprintf(buf
+len
, "T10 Vendor ID based\n");
1228 sprintf(buf
+len
, "EUI-64 based\n");
1231 sprintf(buf
+len
, "NAA\n");
1234 sprintf(buf
+len
, "Relative target port identifier\n");
1237 sprintf(buf
+len
, "SCSI name string\n");
1240 sprintf(buf
+len
, "Unsupported: 0x%02x\n",
1241 vpd
->device_identifier_type
);
1247 if (p_buf_len
< strlen(buf
)+1)
1249 strncpy(p_buf
, buf
, p_buf_len
);
1251 pr_debug("%s", buf
);
1257 int transport_set_vpd_ident_type(struct t10_vpd
*vpd
, unsigned char *page_83
)
1260 * The VPD identifier type..
1262 * from spc3r23.pdf Section 7.6.3.1 Table 298
1264 vpd
->device_identifier_type
= (page_83
[1] & 0x0f);
1265 return transport_dump_vpd_ident_type(vpd
, NULL
, 0);
1267 EXPORT_SYMBOL(transport_set_vpd_ident_type
);
1269 int transport_dump_vpd_ident(
1270 struct t10_vpd
*vpd
,
1271 unsigned char *p_buf
,
1274 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1277 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1279 switch (vpd
->device_identifier_code_set
) {
1280 case 0x01: /* Binary */
1281 sprintf(buf
, "T10 VPD Binary Device Identifier: %s\n",
1282 &vpd
->device_identifier
[0]);
1284 case 0x02: /* ASCII */
1285 sprintf(buf
, "T10 VPD ASCII Device Identifier: %s\n",
1286 &vpd
->device_identifier
[0]);
1288 case 0x03: /* UTF-8 */
1289 sprintf(buf
, "T10 VPD UTF-8 Device Identifier: %s\n",
1290 &vpd
->device_identifier
[0]);
1293 sprintf(buf
, "T10 VPD Device Identifier encoding unsupported:"
1294 " 0x%02x", vpd
->device_identifier_code_set
);
1300 strncpy(p_buf
, buf
, p_buf_len
);
1302 pr_debug("%s", buf
);
1308 transport_set_vpd_ident(struct t10_vpd
*vpd
, unsigned char *page_83
)
1310 static const char hex_str
[] = "0123456789abcdef";
1311 int j
= 0, i
= 4; /* offset to start of the identifer */
1314 * The VPD Code Set (encoding)
1316 * from spc3r23.pdf Section 7.6.3.1 Table 296
1318 vpd
->device_identifier_code_set
= (page_83
[0] & 0x0f);
1319 switch (vpd
->device_identifier_code_set
) {
1320 case 0x01: /* Binary */
1321 vpd
->device_identifier
[j
++] =
1322 hex_str
[vpd
->device_identifier_type
];
1323 while (i
< (4 + page_83
[3])) {
1324 vpd
->device_identifier
[j
++] =
1325 hex_str
[(page_83
[i
] & 0xf0) >> 4];
1326 vpd
->device_identifier
[j
++] =
1327 hex_str
[page_83
[i
] & 0x0f];
1331 case 0x02: /* ASCII */
1332 case 0x03: /* UTF-8 */
1333 while (i
< (4 + page_83
[3]))
1334 vpd
->device_identifier
[j
++] = page_83
[i
++];
1340 return transport_dump_vpd_ident(vpd
, NULL
, 0);
1342 EXPORT_SYMBOL(transport_set_vpd_ident
);
1344 static void core_setup_task_attr_emulation(struct se_device
*dev
)
1347 * If this device is from Target_Core_Mod/pSCSI, disable the
1348 * SAM Task Attribute emulation.
1350 * This is currently not available in upsream Linux/SCSI Target
1351 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1353 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) {
1354 dev
->dev_task_attr_type
= SAM_TASK_ATTR_PASSTHROUGH
;
1358 dev
->dev_task_attr_type
= SAM_TASK_ATTR_EMULATED
;
1359 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1360 " device\n", dev
->transport
->name
,
1361 dev
->transport
->get_device_rev(dev
));
1364 static void scsi_dump_inquiry(struct se_device
*dev
)
1366 struct t10_wwn
*wwn
= &dev
->se_sub_dev
->t10_wwn
;
1369 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1371 pr_debug(" Vendor: ");
1372 for (i
= 0; i
< 8; i
++)
1373 if (wwn
->vendor
[i
] >= 0x20)
1374 pr_debug("%c", wwn
->vendor
[i
]);
1378 pr_debug(" Model: ");
1379 for (i
= 0; i
< 16; i
++)
1380 if (wwn
->model
[i
] >= 0x20)
1381 pr_debug("%c", wwn
->model
[i
]);
1385 pr_debug(" Revision: ");
1386 for (i
= 0; i
< 4; i
++)
1387 if (wwn
->revision
[i
] >= 0x20)
1388 pr_debug("%c", wwn
->revision
[i
]);
1394 device_type
= dev
->transport
->get_device_type(dev
);
1395 pr_debug(" Type: %s ", scsi_device_type(device_type
));
1396 pr_debug(" ANSI SCSI revision: %02x\n",
1397 dev
->transport
->get_device_rev(dev
));
1400 struct se_device
*transport_add_device_to_core_hba(
1402 struct se_subsystem_api
*transport
,
1403 struct se_subsystem_dev
*se_dev
,
1405 void *transport_dev
,
1406 struct se_dev_limits
*dev_limits
,
1407 const char *inquiry_prod
,
1408 const char *inquiry_rev
)
1411 struct se_device
*dev
;
1413 dev
= kzalloc(sizeof(struct se_device
), GFP_KERNEL
);
1415 pr_err("Unable to allocate memory for se_dev_t\n");
1419 transport_init_queue_obj(&dev
->dev_queue_obj
);
1420 dev
->dev_flags
= device_flags
;
1421 dev
->dev_status
|= TRANSPORT_DEVICE_DEACTIVATED
;
1422 dev
->dev_ptr
= transport_dev
;
1424 dev
->se_sub_dev
= se_dev
;
1425 dev
->transport
= transport
;
1426 atomic_set(&dev
->active_cmds
, 0);
1427 INIT_LIST_HEAD(&dev
->dev_list
);
1428 INIT_LIST_HEAD(&dev
->dev_sep_list
);
1429 INIT_LIST_HEAD(&dev
->dev_tmr_list
);
1430 INIT_LIST_HEAD(&dev
->execute_task_list
);
1431 INIT_LIST_HEAD(&dev
->delayed_cmd_list
);
1432 INIT_LIST_HEAD(&dev
->ordered_cmd_list
);
1433 INIT_LIST_HEAD(&dev
->state_task_list
);
1434 INIT_LIST_HEAD(&dev
->qf_cmd_list
);
1435 spin_lock_init(&dev
->execute_task_lock
);
1436 spin_lock_init(&dev
->delayed_cmd_lock
);
1437 spin_lock_init(&dev
->ordered_cmd_lock
);
1438 spin_lock_init(&dev
->state_task_lock
);
1439 spin_lock_init(&dev
->dev_alua_lock
);
1440 spin_lock_init(&dev
->dev_reservation_lock
);
1441 spin_lock_init(&dev
->dev_status_lock
);
1442 spin_lock_init(&dev
->dev_status_thr_lock
);
1443 spin_lock_init(&dev
->se_port_lock
);
1444 spin_lock_init(&dev
->se_tmr_lock
);
1445 spin_lock_init(&dev
->qf_cmd_lock
);
1447 dev
->queue_depth
= dev_limits
->queue_depth
;
1448 atomic_set(&dev
->depth_left
, dev
->queue_depth
);
1449 atomic_set(&dev
->dev_ordered_id
, 0);
1451 se_dev_set_default_attribs(dev
, dev_limits
);
1453 dev
->dev_index
= scsi_get_new_index(SCSI_DEVICE_INDEX
);
1454 dev
->creation_time
= get_jiffies_64();
1455 spin_lock_init(&dev
->stats_lock
);
1457 spin_lock(&hba
->device_lock
);
1458 list_add_tail(&dev
->dev_list
, &hba
->hba_dev_list
);
1460 spin_unlock(&hba
->device_lock
);
1462 * Setup the SAM Task Attribute emulation for struct se_device
1464 core_setup_task_attr_emulation(dev
);
1466 * Force PR and ALUA passthrough emulation with internal object use.
1468 force_pt
= (hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
);
1470 * Setup the Reservations infrastructure for struct se_device
1472 core_setup_reservations(dev
, force_pt
);
1474 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1476 if (core_setup_alua(dev
, force_pt
) < 0)
1480 * Startup the struct se_device processing thread
1482 dev
->process_thread
= kthread_run(transport_processing_thread
, dev
,
1483 "LIO_%s", dev
->transport
->name
);
1484 if (IS_ERR(dev
->process_thread
)) {
1485 pr_err("Unable to create kthread: LIO_%s\n",
1486 dev
->transport
->name
);
1490 * Setup work_queue for QUEUE_FULL
1492 INIT_WORK(&dev
->qf_work_queue
, target_qf_do_work
);
1494 * Preload the initial INQUIRY const values if we are doing
1495 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1496 * passthrough because this is being provided by the backend LLD.
1497 * This is required so that transport_get_inquiry() copies these
1498 * originals once back into DEV_T10_WWN(dev) for the virtual device
1501 if (dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
) {
1502 if (!inquiry_prod
|| !inquiry_rev
) {
1503 pr_err("All non TCM/pSCSI plugins require"
1504 " INQUIRY consts\n");
1508 strncpy(&dev
->se_sub_dev
->t10_wwn
.vendor
[0], "LIO-ORG", 8);
1509 strncpy(&dev
->se_sub_dev
->t10_wwn
.model
[0], inquiry_prod
, 16);
1510 strncpy(&dev
->se_sub_dev
->t10_wwn
.revision
[0], inquiry_rev
, 4);
1512 scsi_dump_inquiry(dev
);
1516 kthread_stop(dev
->process_thread
);
1518 spin_lock(&hba
->device_lock
);
1519 list_del(&dev
->dev_list
);
1521 spin_unlock(&hba
->device_lock
);
1523 se_release_vpd_for_dev(dev
);
1529 EXPORT_SYMBOL(transport_add_device_to_core_hba
);
1531 /* transport_generic_prepare_cdb():
1533 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1534 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1535 * The point of this is since we are mapping iSCSI LUNs to
1536 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1537 * devices and HBAs for a loop.
1539 static inline void transport_generic_prepare_cdb(
1543 case READ_10
: /* SBC - RDProtect */
1544 case READ_12
: /* SBC - RDProtect */
1545 case READ_16
: /* SBC - RDProtect */
1546 case SEND_DIAGNOSTIC
: /* SPC - SELF-TEST Code */
1547 case VERIFY
: /* SBC - VRProtect */
1548 case VERIFY_16
: /* SBC - VRProtect */
1549 case WRITE_VERIFY
: /* SBC - VRProtect */
1550 case WRITE_VERIFY_12
: /* SBC - VRProtect */
1553 cdb
[1] &= 0x1f; /* clear logical unit number */
1558 static struct se_task
*
1559 transport_generic_get_task(struct se_cmd
*cmd
,
1560 enum dma_data_direction data_direction
)
1562 struct se_task
*task
;
1563 struct se_device
*dev
= cmd
->se_dev
;
1565 task
= dev
->transport
->alloc_task(cmd
->t_task_cdb
);
1567 pr_err("Unable to allocate struct se_task\n");
1571 INIT_LIST_HEAD(&task
->t_list
);
1572 INIT_LIST_HEAD(&task
->t_execute_list
);
1573 INIT_LIST_HEAD(&task
->t_state_list
);
1574 init_completion(&task
->task_stop_comp
);
1575 task
->task_se_cmd
= cmd
;
1577 task
->task_data_direction
= data_direction
;
1582 static int transport_generic_cmd_sequencer(struct se_cmd
*, unsigned char *);
1585 * Used by fabric modules containing a local struct se_cmd within their
1586 * fabric dependent per I/O descriptor.
1588 void transport_init_se_cmd(
1590 struct target_core_fabric_ops
*tfo
,
1591 struct se_session
*se_sess
,
1595 unsigned char *sense_buffer
)
1597 INIT_LIST_HEAD(&cmd
->se_lun_node
);
1598 INIT_LIST_HEAD(&cmd
->se_delayed_node
);
1599 INIT_LIST_HEAD(&cmd
->se_ordered_node
);
1600 INIT_LIST_HEAD(&cmd
->se_qf_node
);
1602 INIT_LIST_HEAD(&cmd
->t_task_list
);
1603 init_completion(&cmd
->transport_lun_fe_stop_comp
);
1604 init_completion(&cmd
->transport_lun_stop_comp
);
1605 init_completion(&cmd
->t_transport_stop_comp
);
1606 spin_lock_init(&cmd
->t_state_lock
);
1607 atomic_set(&cmd
->transport_dev_active
, 1);
1610 cmd
->se_sess
= se_sess
;
1611 cmd
->data_length
= data_length
;
1612 cmd
->data_direction
= data_direction
;
1613 cmd
->sam_task_attr
= task_attr
;
1614 cmd
->sense_buffer
= sense_buffer
;
1616 EXPORT_SYMBOL(transport_init_se_cmd
);
1618 static int transport_check_alloc_task_attr(struct se_cmd
*cmd
)
1621 * Check if SAM Task Attribute emulation is enabled for this
1622 * struct se_device storage object
1624 if (cmd
->se_dev
->dev_task_attr_type
!= SAM_TASK_ATTR_EMULATED
)
1627 if (cmd
->sam_task_attr
== MSG_ACA_TAG
) {
1628 pr_debug("SAM Task Attribute ACA"
1629 " emulation is not supported\n");
1633 * Used to determine when ORDERED commands should go from
1634 * Dormant to Active status.
1636 cmd
->se_ordered_id
= atomic_inc_return(&cmd
->se_dev
->dev_ordered_id
);
1637 smp_mb__after_atomic_inc();
1638 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1639 cmd
->se_ordered_id
, cmd
->sam_task_attr
,
1640 cmd
->se_dev
->transport
->name
);
1644 void transport_free_se_cmd(
1645 struct se_cmd
*se_cmd
)
1647 if (se_cmd
->se_tmr_req
)
1648 core_tmr_release_req(se_cmd
->se_tmr_req
);
1650 * Check and free any extended CDB buffer that was allocated
1652 if (se_cmd
->t_task_cdb
!= se_cmd
->__t_task_cdb
)
1653 kfree(se_cmd
->t_task_cdb
);
1655 EXPORT_SYMBOL(transport_free_se_cmd
);
1657 static void transport_generic_wait_for_tasks(struct se_cmd
*, int, int);
1659 /* transport_generic_allocate_tasks():
1661 * Called from fabric RX Thread.
1663 int transport_generic_allocate_tasks(
1669 transport_generic_prepare_cdb(cdb
);
1672 * This is needed for early exceptions.
1674 cmd
->transport_wait_for_tasks
= &transport_generic_wait_for_tasks
;
1677 * Ensure that the received CDB is less than the max (252 + 8) bytes
1678 * for VARIABLE_LENGTH_CMD
1680 if (scsi_command_size(cdb
) > SCSI_MAX_VARLEN_CDB_SIZE
) {
1681 pr_err("Received SCSI CDB with command_size: %d that"
1682 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1683 scsi_command_size(cdb
), SCSI_MAX_VARLEN_CDB_SIZE
);
1687 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1688 * allocate the additional extended CDB buffer now.. Otherwise
1689 * setup the pointer from __t_task_cdb to t_task_cdb.
1691 if (scsi_command_size(cdb
) > sizeof(cmd
->__t_task_cdb
)) {
1692 cmd
->t_task_cdb
= kzalloc(scsi_command_size(cdb
),
1694 if (!cmd
->t_task_cdb
) {
1695 pr_err("Unable to allocate cmd->t_task_cdb"
1696 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1697 scsi_command_size(cdb
),
1698 (unsigned long)sizeof(cmd
->__t_task_cdb
));
1702 cmd
->t_task_cdb
= &cmd
->__t_task_cdb
[0];
1704 * Copy the original CDB into cmd->
1706 memcpy(cmd
->t_task_cdb
, cdb
, scsi_command_size(cdb
));
1708 * Setup the received CDB based on SCSI defined opcodes and
1709 * perform unit attention, persistent reservations and ALUA
1710 * checks for virtual device backends. The cmd->t_task_cdb
1711 * pointer is expected to be setup before we reach this point.
1713 ret
= transport_generic_cmd_sequencer(cmd
, cdb
);
1717 * Check for SAM Task Attribute Emulation
1719 if (transport_check_alloc_task_attr(cmd
) < 0) {
1720 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1721 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
1724 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1725 if (cmd
->se_lun
->lun_sep
)
1726 cmd
->se_lun
->lun_sep
->sep_stats
.cmd_pdus
++;
1727 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1730 EXPORT_SYMBOL(transport_generic_allocate_tasks
);
1733 * Used by fabric module frontends not defining a TFO->new_cmd_map()
1734 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
1736 int transport_generic_handle_cdb(
1741 pr_err("cmd->se_lun is NULL\n");
1745 transport_add_cmd_to_queue(cmd
, TRANSPORT_NEW_CMD
);
1748 EXPORT_SYMBOL(transport_generic_handle_cdb
);
1750 static void transport_generic_request_failure(struct se_cmd
*,
1751 struct se_device
*, int, int);
1753 * Used by fabric module frontends to queue tasks directly.
1754 * Many only be used from process context only
1756 int transport_handle_cdb_direct(
1763 pr_err("cmd->se_lun is NULL\n");
1766 if (in_interrupt()) {
1768 pr_err("transport_generic_handle_cdb cannot be called"
1769 " from interrupt context\n");
1773 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1774 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1775 * in existing usage to ensure that outstanding descriptors are handled
1776 * correctly during shutdown via transport_generic_wait_for_tasks()
1778 * Also, we don't take cmd->t_state_lock here as we only expect
1779 * this to be called for initial descriptor submission.
1781 cmd
->t_state
= TRANSPORT_NEW_CMD
;
1782 atomic_set(&cmd
->t_transport_active
, 1);
1784 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1785 * so follow TRANSPORT_NEW_CMD processing thread context usage
1786 * and call transport_generic_request_failure() if necessary..
1788 ret
= transport_generic_new_cmd(cmd
);
1792 cmd
->transport_error_status
= ret
;
1793 transport_generic_request_failure(cmd
, NULL
, 0,
1794 (cmd
->data_direction
!= DMA_TO_DEVICE
));
1798 EXPORT_SYMBOL(transport_handle_cdb_direct
);
1801 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1802 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1803 * complete setup in TCM process context w/ TFO->new_cmd_map().
1805 int transport_generic_handle_cdb_map(
1810 pr_err("cmd->se_lun is NULL\n");
1814 transport_add_cmd_to_queue(cmd
, TRANSPORT_NEW_CMD_MAP
);
1817 EXPORT_SYMBOL(transport_generic_handle_cdb_map
);
1819 /* transport_generic_handle_data():
1823 int transport_generic_handle_data(
1827 * For the software fabric case, then we assume the nexus is being
1828 * failed/shutdown when signals are pending from the kthread context
1829 * caller, so we return a failure. For the HW target mode case running
1830 * in interrupt code, the signal_pending() check is skipped.
1832 if (!in_interrupt() && signal_pending(current
))
1835 * If the received CDB has aleady been ABORTED by the generic
1836 * target engine, we now call transport_check_aborted_status()
1837 * to queue any delated TASK_ABORTED status for the received CDB to the
1838 * fabric module as we are expecting no further incoming DATA OUT
1839 * sequences at this point.
1841 if (transport_check_aborted_status(cmd
, 1) != 0)
1844 transport_add_cmd_to_queue(cmd
, TRANSPORT_PROCESS_WRITE
);
1847 EXPORT_SYMBOL(transport_generic_handle_data
);
1849 /* transport_generic_handle_tmr():
1853 int transport_generic_handle_tmr(
1857 * This is needed for early exceptions.
1859 cmd
->transport_wait_for_tasks
= &transport_generic_wait_for_tasks
;
1861 transport_add_cmd_to_queue(cmd
, TRANSPORT_PROCESS_TMR
);
1864 EXPORT_SYMBOL(transport_generic_handle_tmr
);
1866 void transport_generic_free_cmd_intr(
1869 transport_add_cmd_to_queue(cmd
, TRANSPORT_FREE_CMD_INTR
);
1871 EXPORT_SYMBOL(transport_generic_free_cmd_intr
);
1873 static int transport_stop_tasks_for_cmd(struct se_cmd
*cmd
)
1875 struct se_task
*task
, *task_tmp
;
1876 unsigned long flags
;
1879 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1880 cmd
->se_tfo
->get_task_tag(cmd
));
1883 * No tasks remain in the execution queue
1885 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
1886 list_for_each_entry_safe(task
, task_tmp
,
1887 &cmd
->t_task_list
, t_list
) {
1888 pr_debug("task_no[%d] - Processing task %p\n",
1889 task
->task_no
, task
);
1891 * If the struct se_task has not been sent and is not active,
1892 * remove the struct se_task from the execution queue.
1894 if (!atomic_read(&task
->task_sent
) &&
1895 !atomic_read(&task
->task_active
)) {
1896 spin_unlock_irqrestore(&cmd
->t_state_lock
,
1898 transport_remove_task_from_execute_queue(task
,
1901 pr_debug("task_no[%d] - Removed from execute queue\n",
1903 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
1908 * If the struct se_task is active, sleep until it is returned
1911 if (atomic_read(&task
->task_active
)) {
1912 atomic_set(&task
->task_stop
, 1);
1913 spin_unlock_irqrestore(&cmd
->t_state_lock
,
1916 pr_debug("task_no[%d] - Waiting to complete\n",
1918 wait_for_completion(&task
->task_stop_comp
);
1919 pr_debug("task_no[%d] - Stopped successfully\n",
1922 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
1923 atomic_dec(&cmd
->t_task_cdbs_left
);
1925 atomic_set(&task
->task_active
, 0);
1926 atomic_set(&task
->task_stop
, 0);
1928 pr_debug("task_no[%d] - Did nothing\n", task
->task_no
);
1932 __transport_stop_task_timer(task
, &flags
);
1934 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
1940 * Handle SAM-esque emulation for generic transport request failures.
1942 static void transport_generic_request_failure(
1944 struct se_device
*dev
,
1950 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1951 " CDB: 0x%02x\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
1952 cmd
->t_task_cdb
[0]);
1953 pr_debug("-----[ i_state: %d t_state/def_t_state:"
1954 " %d/%d transport_error_status: %d\n",
1955 cmd
->se_tfo
->get_cmd_state(cmd
),
1956 cmd
->t_state
, cmd
->deferred_t_state
,
1957 cmd
->transport_error_status
);
1958 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1959 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1960 " t_transport_active: %d t_transport_stop: %d"
1961 " t_transport_sent: %d\n", cmd
->t_task_list_num
,
1962 atomic_read(&cmd
->t_task_cdbs_left
),
1963 atomic_read(&cmd
->t_task_cdbs_sent
),
1964 atomic_read(&cmd
->t_task_cdbs_ex_left
),
1965 atomic_read(&cmd
->t_transport_active
),
1966 atomic_read(&cmd
->t_transport_stop
),
1967 atomic_read(&cmd
->t_transport_sent
));
1969 transport_stop_all_task_timers(cmd
);
1972 atomic_inc(&dev
->depth_left
);
1974 * For SAM Task Attribute emulation for failed struct se_cmd
1976 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
1977 transport_complete_task_attr(cmd
);
1980 transport_direct_request_timeout(cmd
);
1981 cmd
->transport_error_status
= PYX_TRANSPORT_LU_COMM_FAILURE
;
1984 switch (cmd
->transport_error_status
) {
1985 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
:
1986 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
1988 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS
:
1989 cmd
->scsi_sense_reason
= TCM_SECTOR_COUNT_TOO_MANY
;
1991 case PYX_TRANSPORT_INVALID_CDB_FIELD
:
1992 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
1994 case PYX_TRANSPORT_INVALID_PARAMETER_LIST
:
1995 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
1997 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES
:
1999 transport_new_cmd_failure(cmd
);
2001 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
2002 * we force this session to fall back to session
2005 cmd
->se_tfo
->fall_back_to_erl0(cmd
->se_sess
);
2006 cmd
->se_tfo
->stop_session(cmd
->se_sess
, 0, 0);
2009 case PYX_TRANSPORT_LU_COMM_FAILURE
:
2010 case PYX_TRANSPORT_ILLEGAL_REQUEST
:
2011 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2013 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE
:
2014 cmd
->scsi_sense_reason
= TCM_UNKNOWN_MODE_PAGE
;
2016 case PYX_TRANSPORT_WRITE_PROTECTED
:
2017 cmd
->scsi_sense_reason
= TCM_WRITE_PROTECTED
;
2019 case PYX_TRANSPORT_RESERVATION_CONFLICT
:
2021 * No SENSE Data payload for this case, set SCSI Status
2022 * and queue the response to $FABRIC_MOD.
2024 * Uses linux/include/scsi/scsi.h SAM status codes defs
2026 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
2028 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2029 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2032 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2035 cmd
->se_dev
->se_sub_dev
->se_dev_attrib
.emulate_ua_intlck_ctrl
== 2)
2036 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
2037 cmd
->orig_fe_lun
, 0x2C,
2038 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
2040 ret
= cmd
->se_tfo
->queue_status(cmd
);
2044 case PYX_TRANSPORT_USE_SENSE_REASON
:
2046 * struct se_cmd->scsi_sense_reason already set
2050 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
2052 cmd
->transport_error_status
);
2053 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
2058 transport_new_cmd_failure(cmd
);
2060 ret
= transport_send_check_condition_and_sense(cmd
,
2061 cmd
->scsi_sense_reason
, 0);
2067 transport_lun_remove_cmd(cmd
);
2068 if (!transport_cmd_check_stop_to_fabric(cmd
))
2073 cmd
->t_state
= TRANSPORT_COMPLETE_OK
;
2074 transport_handle_queue_full(cmd
, cmd
->se_dev
, transport_complete_qf
);
2077 static void transport_direct_request_timeout(struct se_cmd
*cmd
)
2079 unsigned long flags
;
2081 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2082 if (!atomic_read(&cmd
->t_transport_timeout
)) {
2083 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2086 if (atomic_read(&cmd
->t_task_cdbs_timeout_left
)) {
2087 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2091 atomic_sub(atomic_read(&cmd
->t_transport_timeout
),
2093 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2096 static void transport_generic_request_timeout(struct se_cmd
*cmd
)
2098 unsigned long flags
;
2101 * Reset cmd->t_se_count to allow transport_generic_remove()
2102 * to allow last call to free memory resources.
2104 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2105 if (atomic_read(&cmd
->t_transport_timeout
) > 1) {
2106 int tmp
= (atomic_read(&cmd
->t_transport_timeout
) - 1);
2108 atomic_sub(tmp
, &cmd
->t_se_count
);
2110 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2112 transport_generic_remove(cmd
, 0);
2115 static inline u32
transport_lba_21(unsigned char *cdb
)
2117 return ((cdb
[1] & 0x1f) << 16) | (cdb
[2] << 8) | cdb
[3];
2120 static inline u32
transport_lba_32(unsigned char *cdb
)
2122 return (cdb
[2] << 24) | (cdb
[3] << 16) | (cdb
[4] << 8) | cdb
[5];
2125 static inline unsigned long long transport_lba_64(unsigned char *cdb
)
2127 unsigned int __v1
, __v2
;
2129 __v1
= (cdb
[2] << 24) | (cdb
[3] << 16) | (cdb
[4] << 8) | cdb
[5];
2130 __v2
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
2132 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
2136 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2138 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb
)
2140 unsigned int __v1
, __v2
;
2142 __v1
= (cdb
[12] << 24) | (cdb
[13] << 16) | (cdb
[14] << 8) | cdb
[15];
2143 __v2
= (cdb
[16] << 24) | (cdb
[17] << 16) | (cdb
[18] << 8) | cdb
[19];
2145 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
2148 static void transport_set_supported_SAM_opcode(struct se_cmd
*se_cmd
)
2150 unsigned long flags
;
2152 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
2153 se_cmd
->se_cmd_flags
|= SCF_SUPPORTED_SAM_OPCODE
;
2154 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
2158 * Called from interrupt context.
2160 static void transport_task_timeout_handler(unsigned long data
)
2162 struct se_task
*task
= (struct se_task
*)data
;
2163 struct se_cmd
*cmd
= task
->task_se_cmd
;
2164 unsigned long flags
;
2166 pr_debug("transport task timeout fired! task: %p cmd: %p\n", task
, cmd
);
2168 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2169 if (task
->task_flags
& TF_STOP
) {
2170 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2173 task
->task_flags
&= ~TF_RUNNING
;
2176 * Determine if transport_complete_task() has already been called.
2178 if (!atomic_read(&task
->task_active
)) {
2179 pr_debug("transport task: %p cmd: %p timeout task_active"
2180 " == 0\n", task
, cmd
);
2181 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2185 atomic_inc(&cmd
->t_se_count
);
2186 atomic_inc(&cmd
->t_transport_timeout
);
2187 cmd
->t_tasks_failed
= 1;
2189 atomic_set(&task
->task_timeout
, 1);
2190 task
->task_error_status
= PYX_TRANSPORT_TASK_TIMEOUT
;
2191 task
->task_scsi_status
= 1;
2193 if (atomic_read(&task
->task_stop
)) {
2194 pr_debug("transport task: %p cmd: %p timeout task_stop"
2195 " == 1\n", task
, cmd
);
2196 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2197 complete(&task
->task_stop_comp
);
2201 if (!atomic_dec_and_test(&cmd
->t_task_cdbs_left
)) {
2202 pr_debug("transport task: %p cmd: %p timeout non zero"
2203 " t_task_cdbs_left\n", task
, cmd
);
2204 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2207 pr_debug("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2210 cmd
->t_state
= TRANSPORT_COMPLETE_FAILURE
;
2211 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2213 transport_add_cmd_to_queue(cmd
, TRANSPORT_COMPLETE_FAILURE
);
2217 * Called with cmd->t_state_lock held.
2219 static void transport_start_task_timer(struct se_task
*task
)
2221 struct se_device
*dev
= task
->se_dev
;
2224 if (task
->task_flags
& TF_RUNNING
)
2227 * If the task_timeout is disabled, exit now.
2229 timeout
= dev
->se_sub_dev
->se_dev_attrib
.task_timeout
;
2233 init_timer(&task
->task_timer
);
2234 task
->task_timer
.expires
= (get_jiffies_64() + timeout
* HZ
);
2235 task
->task_timer
.data
= (unsigned long) task
;
2236 task
->task_timer
.function
= transport_task_timeout_handler
;
2238 task
->task_flags
|= TF_RUNNING
;
2239 add_timer(&task
->task_timer
);
2241 pr_debug("Starting task timer for cmd: %p task: %p seconds:"
2242 " %d\n", task
->task_se_cmd
, task
, timeout
);
2247 * Called with spin_lock_irq(&cmd->t_state_lock) held.
2249 void __transport_stop_task_timer(struct se_task
*task
, unsigned long *flags
)
2251 struct se_cmd
*cmd
= task
->task_se_cmd
;
2253 if (!task
->task_flags
& TF_RUNNING
)
2256 task
->task_flags
|= TF_STOP
;
2257 spin_unlock_irqrestore(&cmd
->t_state_lock
, *flags
);
2259 del_timer_sync(&task
->task_timer
);
2261 spin_lock_irqsave(&cmd
->t_state_lock
, *flags
);
2262 task
->task_flags
&= ~TF_RUNNING
;
2263 task
->task_flags
&= ~TF_STOP
;
2266 static void transport_stop_all_task_timers(struct se_cmd
*cmd
)
2268 struct se_task
*task
= NULL
, *task_tmp
;
2269 unsigned long flags
;
2271 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2272 list_for_each_entry_safe(task
, task_tmp
,
2273 &cmd
->t_task_list
, t_list
)
2274 __transport_stop_task_timer(task
, &flags
);
2275 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2278 static inline int transport_tcq_window_closed(struct se_device
*dev
)
2280 if (dev
->dev_tcq_window_closed
++ <
2281 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD
) {
2282 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT
);
2284 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG
);
2286 wake_up_interruptible(&dev
->dev_queue_obj
.thread_wq
);
2291 * Called from Fabric Module context from transport_execute_tasks()
2293 * The return of this function determins if the tasks from struct se_cmd
2294 * get added to the execution queue in transport_execute_tasks(),
2295 * or are added to the delayed or ordered lists here.
2297 static inline int transport_execute_task_attr(struct se_cmd
*cmd
)
2299 if (cmd
->se_dev
->dev_task_attr_type
!= SAM_TASK_ATTR_EMULATED
)
2302 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2303 * to allow the passed struct se_cmd list of tasks to the front of the list.
2305 if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
2306 atomic_inc(&cmd
->se_dev
->dev_hoq_count
);
2307 smp_mb__after_atomic_inc();
2308 pr_debug("Added HEAD_OF_QUEUE for CDB:"
2309 " 0x%02x, se_ordered_id: %u\n",
2311 cmd
->se_ordered_id
);
2313 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
2314 spin_lock(&cmd
->se_dev
->ordered_cmd_lock
);
2315 list_add_tail(&cmd
->se_ordered_node
,
2316 &cmd
->se_dev
->ordered_cmd_list
);
2317 spin_unlock(&cmd
->se_dev
->ordered_cmd_lock
);
2319 atomic_inc(&cmd
->se_dev
->dev_ordered_sync
);
2320 smp_mb__after_atomic_inc();
2322 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
2323 " list, se_ordered_id: %u\n",
2325 cmd
->se_ordered_id
);
2327 * Add ORDERED command to tail of execution queue if
2328 * no other older commands exist that need to be
2331 if (!atomic_read(&cmd
->se_dev
->simple_cmds
))
2335 * For SIMPLE and UNTAGGED Task Attribute commands
2337 atomic_inc(&cmd
->se_dev
->simple_cmds
);
2338 smp_mb__after_atomic_inc();
2341 * Otherwise if one or more outstanding ORDERED task attribute exist,
2342 * add the dormant task(s) built for the passed struct se_cmd to the
2343 * execution queue and become in Active state for this struct se_device.
2345 if (atomic_read(&cmd
->se_dev
->dev_ordered_sync
) != 0) {
2347 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2348 * will be drained upon completion of HEAD_OF_QUEUE task.
2350 spin_lock(&cmd
->se_dev
->delayed_cmd_lock
);
2351 cmd
->se_cmd_flags
|= SCF_DELAYED_CMD_FROM_SAM_ATTR
;
2352 list_add_tail(&cmd
->se_delayed_node
,
2353 &cmd
->se_dev
->delayed_cmd_list
);
2354 spin_unlock(&cmd
->se_dev
->delayed_cmd_lock
);
2356 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2357 " delayed CMD list, se_ordered_id: %u\n",
2358 cmd
->t_task_cdb
[0], cmd
->sam_task_attr
,
2359 cmd
->se_ordered_id
);
2361 * Return zero to let transport_execute_tasks() know
2362 * not to add the delayed tasks to the execution list.
2367 * Otherwise, no ORDERED task attributes exist..
2373 * Called from fabric module context in transport_generic_new_cmd() and
2374 * transport_generic_process_write()
2376 static int transport_execute_tasks(struct se_cmd
*cmd
)
2380 if (se_dev_check_online(cmd
->se_orig_obj_ptr
) != 0) {
2381 cmd
->transport_error_status
= PYX_TRANSPORT_LU_COMM_FAILURE
;
2382 transport_generic_request_failure(cmd
, NULL
, 0, 1);
2387 * Call transport_cmd_check_stop() to see if a fabric exception
2388 * has occurred that prevents execution.
2390 if (!transport_cmd_check_stop(cmd
, 0, TRANSPORT_PROCESSING
)) {
2392 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2393 * attribute for the tasks of the received struct se_cmd CDB
2395 add_tasks
= transport_execute_task_attr(cmd
);
2399 * This calls transport_add_tasks_from_cmd() to handle
2400 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2401 * (if enabled) in __transport_add_task_to_execute_queue() and
2402 * transport_add_task_check_sam_attr().
2404 transport_add_tasks_from_cmd(cmd
);
2407 * Kick the execution queue for the cmd associated struct se_device
2411 __transport_execute_tasks(cmd
->se_dev
);
2416 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2417 * from struct se_device->execute_task_list and
2419 * Called from transport_processing_thread()
2421 static int __transport_execute_tasks(struct se_device
*dev
)
2424 struct se_cmd
*cmd
= NULL
;
2425 struct se_task
*task
= NULL
;
2426 unsigned long flags
;
2429 * Check if there is enough room in the device and HBA queue to send
2430 * struct se_tasks to the selected transport.
2433 if (!atomic_read(&dev
->depth_left
))
2434 return transport_tcq_window_closed(dev
);
2436 dev
->dev_tcq_window_closed
= 0;
2438 spin_lock_irq(&dev
->execute_task_lock
);
2439 if (list_empty(&dev
->execute_task_list
)) {
2440 spin_unlock_irq(&dev
->execute_task_lock
);
2443 task
= list_first_entry(&dev
->execute_task_list
,
2444 struct se_task
, t_execute_list
);
2445 list_del(&task
->t_execute_list
);
2446 atomic_set(&task
->task_execute_queue
, 0);
2447 atomic_dec(&dev
->execute_tasks
);
2448 spin_unlock_irq(&dev
->execute_task_lock
);
2450 atomic_dec(&dev
->depth_left
);
2452 cmd
= task
->task_se_cmd
;
2454 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2455 atomic_set(&task
->task_active
, 1);
2456 atomic_set(&task
->task_sent
, 1);
2457 atomic_inc(&cmd
->t_task_cdbs_sent
);
2459 if (atomic_read(&cmd
->t_task_cdbs_sent
) ==
2460 cmd
->t_task_list_num
)
2461 atomic_set(&cmd
->transport_sent
, 1);
2463 transport_start_task_timer(task
);
2464 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2466 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2467 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2468 * struct se_subsystem_api->do_task() caller below.
2470 if (cmd
->transport_emulate_cdb
) {
2471 error
= cmd
->transport_emulate_cdb(cmd
);
2473 cmd
->transport_error_status
= error
;
2474 atomic_set(&task
->task_active
, 0);
2475 atomic_set(&cmd
->transport_sent
, 0);
2476 transport_stop_tasks_for_cmd(cmd
);
2477 transport_generic_request_failure(cmd
, dev
, 0, 1);
2481 * Handle the successful completion for transport_emulate_cdb()
2482 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2483 * Otherwise the caller is expected to complete the task with
2486 if (!(cmd
->se_cmd_flags
& SCF_EMULATE_CDB_ASYNC
)) {
2487 cmd
->scsi_status
= SAM_STAT_GOOD
;
2488 task
->task_scsi_status
= GOOD
;
2489 transport_complete_task(task
, 1);
2493 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2494 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2495 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2496 * LUN emulation code.
2498 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2499 * call ->do_task() directly and let the underlying TCM subsystem plugin
2500 * code handle the CDB emulation.
2502 if ((dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
) &&
2503 (!(task
->task_se_cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
)))
2504 error
= transport_emulate_control_cdb(task
);
2506 error
= dev
->transport
->do_task(task
);
2509 cmd
->transport_error_status
= error
;
2510 atomic_set(&task
->task_active
, 0);
2511 atomic_set(&cmd
->transport_sent
, 0);
2512 transport_stop_tasks_for_cmd(cmd
);
2513 transport_generic_request_failure(cmd
, dev
, 0, 1);
2522 void transport_new_cmd_failure(struct se_cmd
*se_cmd
)
2524 unsigned long flags
;
2526 * Any unsolicited data will get dumped for failed command inside of
2529 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
2530 se_cmd
->se_cmd_flags
|= SCF_SE_CMD_FAILED
;
2531 se_cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2532 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
2535 static void transport_nop_wait_for_tasks(struct se_cmd
*, int, int);
2537 static inline u32
transport_get_sectors_6(
2542 struct se_device
*dev
= cmd
->se_dev
;
2545 * Assume TYPE_DISK for non struct se_device objects.
2546 * Use 8-bit sector value.
2552 * Use 24-bit allocation length for TYPE_TAPE.
2554 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
2555 return (u32
)(cdb
[2] << 16) + (cdb
[3] << 8) + cdb
[4];
2558 * Everything else assume TYPE_DISK Sector CDB location.
2559 * Use 8-bit sector value.
2565 static inline u32
transport_get_sectors_10(
2570 struct se_device
*dev
= cmd
->se_dev
;
2573 * Assume TYPE_DISK for non struct se_device objects.
2574 * Use 16-bit sector value.
2580 * XXX_10 is not defined in SSC, throw an exception
2582 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2588 * Everything else assume TYPE_DISK Sector CDB location.
2589 * Use 16-bit sector value.
2592 return (u32
)(cdb
[7] << 8) + cdb
[8];
2595 static inline u32
transport_get_sectors_12(
2600 struct se_device
*dev
= cmd
->se_dev
;
2603 * Assume TYPE_DISK for non struct se_device objects.
2604 * Use 32-bit sector value.
2610 * XXX_12 is not defined in SSC, throw an exception
2612 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2618 * Everything else assume TYPE_DISK Sector CDB location.
2619 * Use 32-bit sector value.
2622 return (u32
)(cdb
[6] << 24) + (cdb
[7] << 16) + (cdb
[8] << 8) + cdb
[9];
2625 static inline u32
transport_get_sectors_16(
2630 struct se_device
*dev
= cmd
->se_dev
;
2633 * Assume TYPE_DISK for non struct se_device objects.
2634 * Use 32-bit sector value.
2640 * Use 24-bit allocation length for TYPE_TAPE.
2642 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
2643 return (u32
)(cdb
[12] << 16) + (cdb
[13] << 8) + cdb
[14];
2646 return (u32
)(cdb
[10] << 24) + (cdb
[11] << 16) +
2647 (cdb
[12] << 8) + cdb
[13];
2651 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2653 static inline u32
transport_get_sectors_32(
2659 * Assume TYPE_DISK for non struct se_device objects.
2660 * Use 32-bit sector value.
2662 return (u32
)(cdb
[28] << 24) + (cdb
[29] << 16) +
2663 (cdb
[30] << 8) + cdb
[31];
2667 static inline u32
transport_get_size(
2672 struct se_device
*dev
= cmd
->se_dev
;
2674 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2675 if (cdb
[1] & 1) { /* sectors */
2676 return dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
;
2681 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2682 " %s object\n", dev
->se_sub_dev
->se_dev_attrib
.block_size
, sectors
,
2683 dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
,
2684 dev
->transport
->name
);
2686 return dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
;
2689 static void transport_xor_callback(struct se_cmd
*cmd
)
2691 unsigned char *buf
, *addr
;
2692 struct scatterlist
*sg
;
2693 unsigned int offset
;
2697 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2699 * 1) read the specified logical block(s);
2700 * 2) transfer logical blocks from the data-out buffer;
2701 * 3) XOR the logical blocks transferred from the data-out buffer with
2702 * the logical blocks read, storing the resulting XOR data in a buffer;
2703 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2704 * blocks transferred from the data-out buffer; and
2705 * 5) transfer the resulting XOR data to the data-in buffer.
2707 buf
= kmalloc(cmd
->data_length
, GFP_KERNEL
);
2709 pr_err("Unable to allocate xor_callback buf\n");
2713 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2714 * into the locally allocated *buf
2716 sg_copy_to_buffer(cmd
->t_data_sg
,
2722 * Now perform the XOR against the BIDI read memory located at
2723 * cmd->t_mem_bidi_list
2727 for_each_sg(cmd
->t_bidi_data_sg
, sg
, cmd
->t_bidi_data_nents
, count
) {
2728 addr
= kmap_atomic(sg_page(sg
), KM_USER0
);
2732 for (i
= 0; i
< sg
->length
; i
++)
2733 *(addr
+ sg
->offset
+ i
) ^= *(buf
+ offset
+ i
);
2735 offset
+= sg
->length
;
2736 kunmap_atomic(addr
, KM_USER0
);
2744 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2746 static int transport_get_sense_data(struct se_cmd
*cmd
)
2748 unsigned char *buffer
= cmd
->sense_buffer
, *sense_buffer
= NULL
;
2749 struct se_device
*dev
;
2750 struct se_task
*task
= NULL
, *task_tmp
;
2751 unsigned long flags
;
2754 WARN_ON(!cmd
->se_lun
);
2756 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2757 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
2758 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2762 list_for_each_entry_safe(task
, task_tmp
,
2763 &cmd
->t_task_list
, t_list
) {
2765 if (!task
->task_sense
)
2772 if (!dev
->transport
->get_sense_buffer
) {
2773 pr_err("dev->transport->get_sense_buffer"
2778 sense_buffer
= dev
->transport
->get_sense_buffer(task
);
2779 if (!sense_buffer
) {
2780 pr_err("ITT[0x%08x]_TASK[%d]: Unable to locate"
2781 " sense buffer for task with sense\n",
2782 cmd
->se_tfo
->get_task_tag(cmd
), task
->task_no
);
2785 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2787 offset
= cmd
->se_tfo
->set_fabric_sense_len(cmd
,
2788 TRANSPORT_SENSE_BUFFER
);
2790 memcpy(&buffer
[offset
], sense_buffer
,
2791 TRANSPORT_SENSE_BUFFER
);
2792 cmd
->scsi_status
= task
->task_scsi_status
;
2793 /* Automatically padded */
2794 cmd
->scsi_sense_length
=
2795 (TRANSPORT_SENSE_BUFFER
+ offset
);
2797 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2799 dev
->se_hba
->hba_id
, dev
->transport
->name
,
2803 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2809 transport_handle_reservation_conflict(struct se_cmd
*cmd
)
2811 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
2812 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2813 cmd
->se_cmd_flags
|= SCF_SCSI_RESERVATION_CONFLICT
;
2814 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
2816 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2817 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2820 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2823 cmd
->se_dev
->se_sub_dev
->se_dev_attrib
.emulate_ua_intlck_ctrl
== 2)
2824 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
2825 cmd
->orig_fe_lun
, 0x2C,
2826 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
2830 static inline long long transport_dev_end_lba(struct se_device
*dev
)
2832 return dev
->transport
->get_blocks(dev
) + 1;
2835 static int transport_cmd_get_valid_sectors(struct se_cmd
*cmd
)
2837 struct se_device
*dev
= cmd
->se_dev
;
2840 if (dev
->transport
->get_device_type(dev
) != TYPE_DISK
)
2843 sectors
= (cmd
->data_length
/ dev
->se_sub_dev
->se_dev_attrib
.block_size
);
2845 if ((cmd
->t_task_lba
+ sectors
) > transport_dev_end_lba(dev
)) {
2846 pr_err("LBA: %llu Sectors: %u exceeds"
2847 " transport_dev_end_lba(): %llu\n",
2848 cmd
->t_task_lba
, sectors
,
2849 transport_dev_end_lba(dev
));
2850 pr_err(" We should return CHECK_CONDITION"
2851 " but we don't yet\n");
2858 /* transport_generic_cmd_sequencer():
2860 * Generic Command Sequencer that should work for most DAS transport
2863 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2866 * FIXME: Need to support other SCSI OPCODES where as well.
2868 static int transport_generic_cmd_sequencer(
2872 struct se_device
*dev
= cmd
->se_dev
;
2873 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
2874 int ret
= 0, sector_ret
= 0, passthrough
;
2875 u32 sectors
= 0, size
= 0, pr_reg_type
= 0;
2879 * Check for an existing UNIT ATTENTION condition
2881 if (core_scsi3_ua_check(cmd
, cdb
) < 0) {
2882 cmd
->transport_wait_for_tasks
=
2883 &transport_nop_wait_for_tasks
;
2884 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2885 cmd
->scsi_sense_reason
= TCM_CHECK_CONDITION_UNIT_ATTENTION
;
2889 * Check status of Asymmetric Logical Unit Assignment port
2891 ret
= su_dev
->t10_alua
.alua_state_check(cmd
, cdb
, &alua_ascq
);
2893 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
2895 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2896 * The ALUA additional sense code qualifier (ASCQ) is determined
2897 * by the ALUA primary or secondary access state..
2901 pr_debug("[%s]: ALUA TG Port not available,"
2902 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2903 cmd
->se_tfo
->get_fabric_name(), alua_ascq
);
2905 transport_set_sense_codes(cmd
, 0x04, alua_ascq
);
2906 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2907 cmd
->scsi_sense_reason
= TCM_CHECK_CONDITION_NOT_READY
;
2910 goto out_invalid_cdb_field
;
2913 * Check status for SPC-3 Persistent Reservations
2915 if (su_dev
->t10_pr
.pr_ops
.t10_reservation_check(cmd
, &pr_reg_type
) != 0) {
2916 if (su_dev
->t10_pr
.pr_ops
.t10_seq_non_holder(
2917 cmd
, cdb
, pr_reg_type
) != 0)
2918 return transport_handle_reservation_conflict(cmd
);
2920 * This means the CDB is allowed for the SCSI Initiator port
2921 * when said port is *NOT* holding the legacy SPC-2 or
2922 * SPC-3 Persistent Reservation.
2928 sectors
= transport_get_sectors_6(cdb
, cmd
, §or_ret
);
2930 goto out_unsupported_cdb
;
2931 size
= transport_get_size(sectors
, cdb
, cmd
);
2932 cmd
->transport_split_cdb
= &split_cdb_XX_6
;
2933 cmd
->t_task_lba
= transport_lba_21(cdb
);
2934 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2937 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
2939 goto out_unsupported_cdb
;
2940 size
= transport_get_size(sectors
, cdb
, cmd
);
2941 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
2942 cmd
->t_task_lba
= transport_lba_32(cdb
);
2943 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2946 sectors
= transport_get_sectors_12(cdb
, cmd
, §or_ret
);
2948 goto out_unsupported_cdb
;
2949 size
= transport_get_size(sectors
, cdb
, cmd
);
2950 cmd
->transport_split_cdb
= &split_cdb_XX_12
;
2951 cmd
->t_task_lba
= transport_lba_32(cdb
);
2952 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2955 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
2957 goto out_unsupported_cdb
;
2958 size
= transport_get_size(sectors
, cdb
, cmd
);
2959 cmd
->transport_split_cdb
= &split_cdb_XX_16
;
2960 cmd
->t_task_lba
= transport_lba_64(cdb
);
2961 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2964 sectors
= transport_get_sectors_6(cdb
, cmd
, §or_ret
);
2966 goto out_unsupported_cdb
;
2967 size
= transport_get_size(sectors
, cdb
, cmd
);
2968 cmd
->transport_split_cdb
= &split_cdb_XX_6
;
2969 cmd
->t_task_lba
= transport_lba_21(cdb
);
2970 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2973 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
2975 goto out_unsupported_cdb
;
2976 size
= transport_get_size(sectors
, cdb
, cmd
);
2977 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
2978 cmd
->t_task_lba
= transport_lba_32(cdb
);
2979 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
2980 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2983 sectors
= transport_get_sectors_12(cdb
, cmd
, §or_ret
);
2985 goto out_unsupported_cdb
;
2986 size
= transport_get_size(sectors
, cdb
, cmd
);
2987 cmd
->transport_split_cdb
= &split_cdb_XX_12
;
2988 cmd
->t_task_lba
= transport_lba_32(cdb
);
2989 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
2990 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2993 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
2995 goto out_unsupported_cdb
;
2996 size
= transport_get_size(sectors
, cdb
, cmd
);
2997 cmd
->transport_split_cdb
= &split_cdb_XX_16
;
2998 cmd
->t_task_lba
= transport_lba_64(cdb
);
2999 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
3000 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3002 case XDWRITEREAD_10
:
3003 if ((cmd
->data_direction
!= DMA_TO_DEVICE
) ||
3004 !(cmd
->t_tasks_bidi
))
3005 goto out_invalid_cdb_field
;
3006 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3008 goto out_unsupported_cdb
;
3009 size
= transport_get_size(sectors
, cdb
, cmd
);
3010 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
3011 cmd
->t_task_lba
= transport_lba_32(cdb
);
3012 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3013 passthrough
= (dev
->transport
->transport_type
==
3014 TRANSPORT_PLUGIN_PHBA_PDEV
);
3016 * Skip the remaining assignments for TCM/PSCSI passthrough
3021 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3023 cmd
->transport_complete_callback
= &transport_xor_callback
;
3024 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
3026 case VARIABLE_LENGTH_CMD
:
3027 service_action
= get_unaligned_be16(&cdb
[8]);
3029 * Determine if this is TCM/PSCSI device and we should disable
3030 * internal emulation for this CDB.
3032 passthrough
= (dev
->transport
->transport_type
==
3033 TRANSPORT_PLUGIN_PHBA_PDEV
);
3035 switch (service_action
) {
3036 case XDWRITEREAD_32
:
3037 sectors
= transport_get_sectors_32(cdb
, cmd
, §or_ret
);
3039 goto out_unsupported_cdb
;
3040 size
= transport_get_size(sectors
, cdb
, cmd
);
3042 * Use WRITE_32 and READ_32 opcodes for the emulated
3043 * XDWRITE_READ_32 logic.
3045 cmd
->transport_split_cdb
= &split_cdb_XX_32
;
3046 cmd
->t_task_lba
= transport_lba_64_ext(cdb
);
3047 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3050 * Skip the remaining assignments for TCM/PSCSI passthrough
3056 * Setup BIDI XOR callback to be run during
3057 * transport_generic_complete_ok()
3059 cmd
->transport_complete_callback
= &transport_xor_callback
;
3060 cmd
->t_tasks_fua
= (cdb
[10] & 0x8);
3063 sectors
= transport_get_sectors_32(cdb
, cmd
, §or_ret
);
3065 goto out_unsupported_cdb
;
3068 size
= transport_get_size(sectors
, cdb
, cmd
);
3070 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
3072 goto out_invalid_cdb_field
;
3075 cmd
->t_task_lba
= get_unaligned_be64(&cdb
[12]);
3076 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3079 * Skip the remaining assignments for TCM/PSCSI passthrough
3084 if ((cdb
[10] & 0x04) || (cdb
[10] & 0x02)) {
3085 pr_err("WRITE_SAME PBDATA and LBDATA"
3086 " bits not supported for Block Discard"
3088 goto out_invalid_cdb_field
;
3091 * Currently for the emulated case we only accept
3092 * tpws with the UNMAP=1 bit set.
3094 if (!(cdb
[10] & 0x08)) {
3095 pr_err("WRITE_SAME w/o UNMAP bit not"
3096 " supported for Block Discard Emulation\n");
3097 goto out_invalid_cdb_field
;
3101 pr_err("VARIABLE_LENGTH_CMD service action"
3102 " 0x%04x not supported\n", service_action
);
3103 goto out_unsupported_cdb
;
3106 case MAINTENANCE_IN
:
3107 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
3108 /* MAINTENANCE_IN from SCC-2 */
3110 * Check for emulated MI_REPORT_TARGET_PGS.
3112 if (cdb
[1] == MI_REPORT_TARGET_PGS
) {
3113 cmd
->transport_emulate_cdb
=
3114 (su_dev
->t10_alua
.alua_type
==
3115 SPC3_ALUA_EMULATED
) ?
3116 core_emulate_report_target_port_groups
:
3119 size
= (cdb
[6] << 24) | (cdb
[7] << 16) |
3120 (cdb
[8] << 8) | cdb
[9];
3122 /* GPCMD_SEND_KEY from multi media commands */
3123 size
= (cdb
[8] << 8) + cdb
[9];
3125 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3129 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3131 case MODE_SELECT_10
:
3132 size
= (cdb
[7] << 8) + cdb
[8];
3133 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3137 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3140 case GPCMD_READ_BUFFER_CAPACITY
:
3141 case GPCMD_SEND_OPC
:
3144 size
= (cdb
[7] << 8) + cdb
[8];
3145 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3147 case READ_BLOCK_LIMITS
:
3148 size
= READ_BLOCK_LEN
;
3149 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3151 case GPCMD_GET_CONFIGURATION
:
3152 case GPCMD_READ_FORMAT_CAPACITIES
:
3153 case GPCMD_READ_DISC_INFO
:
3154 case GPCMD_READ_TRACK_RZONE_INFO
:
3155 size
= (cdb
[7] << 8) + cdb
[8];
3156 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3158 case PERSISTENT_RESERVE_IN
:
3159 case PERSISTENT_RESERVE_OUT
:
3160 cmd
->transport_emulate_cdb
=
3161 (su_dev
->t10_pr
.res_type
==
3162 SPC3_PERSISTENT_RESERVATIONS
) ?
3163 core_scsi3_emulate_pr
: NULL
;
3164 size
= (cdb
[7] << 8) + cdb
[8];
3165 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3167 case GPCMD_MECHANISM_STATUS
:
3168 case GPCMD_READ_DVD_STRUCTURE
:
3169 size
= (cdb
[8] << 8) + cdb
[9];
3170 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3173 size
= READ_POSITION_LEN
;
3174 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3176 case MAINTENANCE_OUT
:
3177 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
3178 /* MAINTENANCE_OUT from SCC-2
3180 * Check for emulated MO_SET_TARGET_PGS.
3182 if (cdb
[1] == MO_SET_TARGET_PGS
) {
3183 cmd
->transport_emulate_cdb
=
3184 (su_dev
->t10_alua
.alua_type
==
3185 SPC3_ALUA_EMULATED
) ?
3186 core_emulate_set_target_port_groups
:
3190 size
= (cdb
[6] << 24) | (cdb
[7] << 16) |
3191 (cdb
[8] << 8) | cdb
[9];
3193 /* GPCMD_REPORT_KEY from multi media commands */
3194 size
= (cdb
[8] << 8) + cdb
[9];
3196 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3199 size
= (cdb
[3] << 8) + cdb
[4];
3201 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3202 * See spc4r17 section 5.3
3204 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3205 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
3206 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3209 size
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3210 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3213 size
= READ_CAP_LEN
;
3214 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3216 case READ_MEDIA_SERIAL_NUMBER
:
3217 case SECURITY_PROTOCOL_IN
:
3218 case SECURITY_PROTOCOL_OUT
:
3219 size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
3220 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3222 case SERVICE_ACTION_IN
:
3223 case ACCESS_CONTROL_IN
:
3224 case ACCESS_CONTROL_OUT
:
3226 case READ_ATTRIBUTE
:
3227 case RECEIVE_COPY_RESULTS
:
3228 case WRITE_ATTRIBUTE
:
3229 size
= (cdb
[10] << 24) | (cdb
[11] << 16) |
3230 (cdb
[12] << 8) | cdb
[13];
3231 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3233 case RECEIVE_DIAGNOSTIC
:
3234 case SEND_DIAGNOSTIC
:
3235 size
= (cdb
[3] << 8) | cdb
[4];
3236 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3238 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3241 sectors
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3242 size
= (2336 * sectors
);
3243 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3248 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3252 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3254 case READ_ELEMENT_STATUS
:
3255 size
= 65536 * cdb
[7] + 256 * cdb
[8] + cdb
[9];
3256 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3259 size
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3260 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3265 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3266 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3268 if (cdb
[0] == RESERVE_10
)
3269 size
= (cdb
[7] << 8) | cdb
[8];
3271 size
= cmd
->data_length
;
3274 * Setup the legacy emulated handler for SPC-2 and
3275 * >= SPC-3 compatible reservation handling (CRH=1)
3276 * Otherwise, we assume the underlying SCSI logic is
3277 * is running in SPC_PASSTHROUGH, and wants reservations
3278 * emulation disabled.
3280 cmd
->transport_emulate_cdb
=
3281 (su_dev
->t10_pr
.res_type
!=
3283 core_scsi2_emulate_crh
: NULL
;
3284 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3289 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3290 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3292 if (cdb
[0] == RELEASE_10
)
3293 size
= (cdb
[7] << 8) | cdb
[8];
3295 size
= cmd
->data_length
;
3297 cmd
->transport_emulate_cdb
=
3298 (su_dev
->t10_pr
.res_type
!=
3300 core_scsi2_emulate_crh
: NULL
;
3301 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3303 case SYNCHRONIZE_CACHE
:
3304 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3306 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3308 if (cdb
[0] == SYNCHRONIZE_CACHE
) {
3309 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3310 cmd
->t_task_lba
= transport_lba_32(cdb
);
3312 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3313 cmd
->t_task_lba
= transport_lba_64(cdb
);
3316 goto out_unsupported_cdb
;
3318 size
= transport_get_size(sectors
, cdb
, cmd
);
3319 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3322 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3324 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
3327 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3328 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3330 cmd
->se_cmd_flags
|= SCF_EMULATE_CDB_ASYNC
;
3332 * Check to ensure that LBA + Range does not exceed past end of
3335 if (!transport_cmd_get_valid_sectors(cmd
))
3336 goto out_invalid_cdb_field
;
3339 size
= get_unaligned_be16(&cdb
[7]);
3340 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3343 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3345 goto out_unsupported_cdb
;
3348 size
= transport_get_size(sectors
, cdb
, cmd
);
3350 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3351 goto out_invalid_cdb_field
;
3354 cmd
->t_task_lba
= get_unaligned_be64(&cdb
[2]);
3355 passthrough
= (dev
->transport
->transport_type
==
3356 TRANSPORT_PLUGIN_PHBA_PDEV
);
3358 * Determine if the received WRITE_SAME_16 is used to for direct
3359 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
3360 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
3361 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
3362 * TCM/FILEIO subsystem plugin backstores.
3365 if ((cdb
[1] & 0x04) || (cdb
[1] & 0x02)) {
3366 pr_err("WRITE_SAME PBDATA and LBDATA"
3367 " bits not supported for Block Discard"
3369 goto out_invalid_cdb_field
;
3372 * Currently for the emulated case we only accept
3373 * tpws with the UNMAP=1 bit set.
3375 if (!(cdb
[1] & 0x08)) {
3376 pr_err("WRITE_SAME w/o UNMAP bit not "
3377 " supported for Block Discard Emulation\n");
3378 goto out_invalid_cdb_field
;
3381 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3383 case ALLOW_MEDIUM_REMOVAL
:
3384 case GPCMD_CLOSE_TRACK
:
3386 case INITIALIZE_ELEMENT_STATUS
:
3387 case GPCMD_LOAD_UNLOAD
:
3390 case GPCMD_SET_SPEED
:
3393 case TEST_UNIT_READY
:
3395 case WRITE_FILEMARKS
:
3397 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3400 cmd
->transport_emulate_cdb
=
3401 transport_core_report_lun_response
;
3402 size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
3404 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3405 * See spc4r17 section 5.3
3407 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3408 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
3409 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3412 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3413 " 0x%02x, sending CHECK_CONDITION.\n",
3414 cmd
->se_tfo
->get_fabric_name(), cdb
[0]);
3415 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
3416 goto out_unsupported_cdb
;
3419 if (size
!= cmd
->data_length
) {
3420 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3421 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3422 " 0x%02x\n", cmd
->se_tfo
->get_fabric_name(),
3423 cmd
->data_length
, size
, cdb
[0]);
3425 cmd
->cmd_spdtl
= size
;
3427 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
3428 pr_err("Rejecting underflow/overflow"
3430 goto out_invalid_cdb_field
;
3433 * Reject READ_* or WRITE_* with overflow/underflow for
3434 * type SCF_SCSI_DATA_SG_IO_CDB.
3436 if (!ret
&& (dev
->se_sub_dev
->se_dev_attrib
.block_size
!= 512)) {
3437 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3438 " CDB on non 512-byte sector setup subsystem"
3439 " plugin: %s\n", dev
->transport
->name
);
3440 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3441 goto out_invalid_cdb_field
;
3444 if (size
> cmd
->data_length
) {
3445 cmd
->se_cmd_flags
|= SCF_OVERFLOW_BIT
;
3446 cmd
->residual_count
= (size
- cmd
->data_length
);
3448 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
3449 cmd
->residual_count
= (cmd
->data_length
- size
);
3451 cmd
->data_length
= size
;
3454 /* Let's limit control cdbs to a page, for simplicity's sake. */
3455 if ((cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
) &&
3457 goto out_invalid_cdb_field
;
3459 transport_set_supported_SAM_opcode(cmd
);
3462 out_unsupported_cdb
:
3463 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3464 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
3466 out_invalid_cdb_field
:
3467 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3468 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3473 * Called from transport_generic_complete_ok() and
3474 * transport_generic_request_failure() to determine which dormant/delayed
3475 * and ordered cmds need to have their tasks added to the execution queue.
3477 static void transport_complete_task_attr(struct se_cmd
*cmd
)
3479 struct se_device
*dev
= cmd
->se_dev
;
3480 struct se_cmd
*cmd_p
, *cmd_tmp
;
3481 int new_active_tasks
= 0;
3483 if (cmd
->sam_task_attr
== MSG_SIMPLE_TAG
) {
3484 atomic_dec(&dev
->simple_cmds
);
3485 smp_mb__after_atomic_dec();
3486 dev
->dev_cur_ordered_id
++;
3487 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3488 " SIMPLE: %u\n", dev
->dev_cur_ordered_id
,
3489 cmd
->se_ordered_id
);
3490 } else if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
3491 atomic_dec(&dev
->dev_hoq_count
);
3492 smp_mb__after_atomic_dec();
3493 dev
->dev_cur_ordered_id
++;
3494 pr_debug("Incremented dev_cur_ordered_id: %u for"
3495 " HEAD_OF_QUEUE: %u\n", dev
->dev_cur_ordered_id
,
3496 cmd
->se_ordered_id
);
3497 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
3498 spin_lock(&dev
->ordered_cmd_lock
);
3499 list_del(&cmd
->se_ordered_node
);
3500 atomic_dec(&dev
->dev_ordered_sync
);
3501 smp_mb__after_atomic_dec();
3502 spin_unlock(&dev
->ordered_cmd_lock
);
3504 dev
->dev_cur_ordered_id
++;
3505 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3506 " %u\n", dev
->dev_cur_ordered_id
, cmd
->se_ordered_id
);
3509 * Process all commands up to the last received
3510 * ORDERED task attribute which requires another blocking
3513 spin_lock(&dev
->delayed_cmd_lock
);
3514 list_for_each_entry_safe(cmd_p
, cmd_tmp
,
3515 &dev
->delayed_cmd_list
, se_delayed_node
) {
3517 list_del(&cmd_p
->se_delayed_node
);
3518 spin_unlock(&dev
->delayed_cmd_lock
);
3520 pr_debug("Calling add_tasks() for"
3521 " cmd_p: 0x%02x Task Attr: 0x%02x"
3522 " Dormant -> Active, se_ordered_id: %u\n",
3523 cmd_p
->t_task_cdb
[0],
3524 cmd_p
->sam_task_attr
, cmd_p
->se_ordered_id
);
3526 transport_add_tasks_from_cmd(cmd_p
);
3529 spin_lock(&dev
->delayed_cmd_lock
);
3530 if (cmd_p
->sam_task_attr
== MSG_ORDERED_TAG
)
3533 spin_unlock(&dev
->delayed_cmd_lock
);
3535 * If new tasks have become active, wake up the transport thread
3536 * to do the processing of the Active tasks.
3538 if (new_active_tasks
!= 0)
3539 wake_up_interruptible(&dev
->dev_queue_obj
.thread_wq
);
3542 static int transport_complete_qf(struct se_cmd
*cmd
)
3546 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
)
3547 return cmd
->se_tfo
->queue_status(cmd
);
3549 switch (cmd
->data_direction
) {
3550 case DMA_FROM_DEVICE
:
3551 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3554 if (cmd
->t_bidi_data_sg
) {
3555 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3559 /* Fall through for DMA_TO_DEVICE */
3561 ret
= cmd
->se_tfo
->queue_status(cmd
);
3570 static void transport_handle_queue_full(
3572 struct se_device
*dev
,
3573 int (*qf_callback
)(struct se_cmd
*))
3575 spin_lock_irq(&dev
->qf_cmd_lock
);
3576 cmd
->se_cmd_flags
|= SCF_EMULATE_QUEUE_FULL
;
3577 cmd
->transport_qf_callback
= qf_callback
;
3578 list_add_tail(&cmd
->se_qf_node
, &cmd
->se_dev
->qf_cmd_list
);
3579 atomic_inc(&dev
->dev_qf_count
);
3580 smp_mb__after_atomic_inc();
3581 spin_unlock_irq(&cmd
->se_dev
->qf_cmd_lock
);
3583 schedule_work(&cmd
->se_dev
->qf_work_queue
);
3586 static void transport_generic_complete_ok(struct se_cmd
*cmd
)
3588 int reason
= 0, ret
;
3590 * Check if we need to move delayed/dormant tasks from cmds on the
3591 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3594 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3595 transport_complete_task_attr(cmd
);
3597 * Check to schedule QUEUE_FULL work, or execute an existing
3598 * cmd->transport_qf_callback()
3600 if (atomic_read(&cmd
->se_dev
->dev_qf_count
) != 0)
3601 schedule_work(&cmd
->se_dev
->qf_work_queue
);
3603 if (cmd
->transport_qf_callback
) {
3604 ret
= cmd
->transport_qf_callback(cmd
);
3608 cmd
->transport_qf_callback
= NULL
;
3612 * Check if we need to retrieve a sense buffer from
3613 * the struct se_cmd in question.
3615 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
3616 if (transport_get_sense_data(cmd
) < 0)
3617 reason
= TCM_NON_EXISTENT_LUN
;
3620 * Only set when an struct se_task->task_scsi_status returned
3621 * a non GOOD status.
3623 if (cmd
->scsi_status
) {
3624 ret
= transport_send_check_condition_and_sense(
3629 transport_lun_remove_cmd(cmd
);
3630 transport_cmd_check_stop_to_fabric(cmd
);
3635 * Check for a callback, used by amongst other things
3636 * XDWRITE_READ_10 emulation.
3638 if (cmd
->transport_complete_callback
)
3639 cmd
->transport_complete_callback(cmd
);
3641 switch (cmd
->data_direction
) {
3642 case DMA_FROM_DEVICE
:
3643 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3644 if (cmd
->se_lun
->lun_sep
) {
3645 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
3648 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3650 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3655 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3656 if (cmd
->se_lun
->lun_sep
) {
3657 cmd
->se_lun
->lun_sep
->sep_stats
.rx_data_octets
+=
3660 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3662 * Check if we need to send READ payload for BIDI-COMMAND
3664 if (cmd
->t_bidi_data_sg
) {
3665 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3666 if (cmd
->se_lun
->lun_sep
) {
3667 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
3670 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3671 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3676 /* Fall through for DMA_TO_DEVICE */
3678 ret
= cmd
->se_tfo
->queue_status(cmd
);
3687 transport_lun_remove_cmd(cmd
);
3688 transport_cmd_check_stop_to_fabric(cmd
);
3692 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3693 " data_direction: %d\n", cmd
, cmd
->data_direction
);
3694 transport_handle_queue_full(cmd
, cmd
->se_dev
, transport_complete_qf
);
3697 static void transport_free_dev_tasks(struct se_cmd
*cmd
)
3699 struct se_task
*task
, *task_tmp
;
3700 unsigned long flags
;
3702 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3703 list_for_each_entry_safe(task
, task_tmp
,
3704 &cmd
->t_task_list
, t_list
) {
3705 if (atomic_read(&task
->task_active
))
3708 kfree(task
->task_sg_bidi
);
3709 kfree(task
->task_sg
);
3711 list_del(&task
->t_list
);
3713 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3715 task
->se_dev
->transport
->free_task(task
);
3717 pr_err("task[%u] - task->se_dev is NULL\n",
3719 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3721 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3724 static inline void transport_free_sgl(struct scatterlist
*sgl
, int nents
)
3726 struct scatterlist
*sg
;
3729 for_each_sg(sgl
, sg
, nents
, count
)
3730 __free_page(sg_page(sg
));
3735 static inline void transport_free_pages(struct se_cmd
*cmd
)
3737 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
)
3740 transport_free_sgl(cmd
->t_data_sg
, cmd
->t_data_nents
);
3741 cmd
->t_data_sg
= NULL
;
3742 cmd
->t_data_nents
= 0;
3744 transport_free_sgl(cmd
->t_bidi_data_sg
, cmd
->t_bidi_data_nents
);
3745 cmd
->t_bidi_data_sg
= NULL
;
3746 cmd
->t_bidi_data_nents
= 0;
3749 static inline void transport_release_tasks(struct se_cmd
*cmd
)
3751 transport_free_dev_tasks(cmd
);
3754 static inline int transport_dec_and_check(struct se_cmd
*cmd
)
3756 unsigned long flags
;
3758 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3759 if (atomic_read(&cmd
->t_fe_count
)) {
3760 if (!atomic_dec_and_test(&cmd
->t_fe_count
)) {
3761 spin_unlock_irqrestore(&cmd
->t_state_lock
,
3767 if (atomic_read(&cmd
->t_se_count
)) {
3768 if (!atomic_dec_and_test(&cmd
->t_se_count
)) {
3769 spin_unlock_irqrestore(&cmd
->t_state_lock
,
3774 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3779 static void transport_release_fe_cmd(struct se_cmd
*cmd
)
3781 unsigned long flags
;
3783 if (transport_dec_and_check(cmd
))
3786 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3787 if (!atomic_read(&cmd
->transport_dev_active
)) {
3788 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3791 atomic_set(&cmd
->transport_dev_active
, 0);
3792 transport_all_task_dev_remove_state(cmd
);
3793 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3795 transport_release_tasks(cmd
);
3797 transport_free_pages(cmd
);
3798 transport_free_se_cmd(cmd
);
3799 cmd
->se_tfo
->release_cmd(cmd
);
3803 transport_generic_remove(struct se_cmd
*cmd
, int session_reinstatement
)
3805 unsigned long flags
;
3807 if (transport_dec_and_check(cmd
)) {
3808 if (session_reinstatement
) {
3809 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3810 transport_all_task_dev_remove_state(cmd
);
3811 spin_unlock_irqrestore(&cmd
->t_state_lock
,
3817 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3818 if (!atomic_read(&cmd
->transport_dev_active
)) {
3819 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3822 atomic_set(&cmd
->transport_dev_active
, 0);
3823 transport_all_task_dev_remove_state(cmd
);
3824 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3826 transport_release_tasks(cmd
);
3829 transport_free_pages(cmd
);
3830 transport_release_cmd(cmd
);
3835 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3836 * allocating in the core.
3837 * @cmd: Associated se_cmd descriptor
3838 * @mem: SGL style memory for TCM WRITE / READ
3839 * @sg_mem_num: Number of SGL elements
3840 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3841 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3843 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3846 int transport_generic_map_mem_to_cmd(
3848 struct scatterlist
*sgl
,
3850 struct scatterlist
*sgl_bidi
,
3853 if (!sgl
|| !sgl_count
)
3856 if ((cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
) ||
3857 (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
)) {
3859 cmd
->t_data_sg
= sgl
;
3860 cmd
->t_data_nents
= sgl_count
;
3862 if (sgl_bidi
&& sgl_bidi_count
) {
3863 cmd
->t_bidi_data_sg
= sgl_bidi
;
3864 cmd
->t_bidi_data_nents
= sgl_bidi_count
;
3866 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
;
3871 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd
);
3873 static int transport_new_cmd_obj(struct se_cmd
*cmd
)
3875 struct se_device
*dev
= cmd
->se_dev
;
3881 * Setup any BIDI READ tasks and memory from
3882 * cmd->t_mem_bidi_list so the READ struct se_tasks
3883 * are queued first for the non pSCSI passthrough case.
3885 if (cmd
->t_bidi_data_sg
&&
3886 (dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
)) {
3887 rc
= transport_allocate_tasks(cmd
,
3890 cmd
->t_bidi_data_sg
,
3891 cmd
->t_bidi_data_nents
);
3893 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3894 cmd
->scsi_sense_reason
=
3895 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3896 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3898 atomic_inc(&cmd
->t_fe_count
);
3899 atomic_inc(&cmd
->t_se_count
);
3903 * Setup the tasks and memory from cmd->t_mem_list
3904 * Note for BIDI transfers this will contain the WRITE payload
3906 task_cdbs
= transport_allocate_tasks(cmd
,
3908 cmd
->data_direction
,
3911 if (task_cdbs
<= 0) {
3912 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3913 cmd
->scsi_sense_reason
=
3914 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3915 return PYX_TRANSPORT_LU_COMM_FAILURE
;
3919 atomic_inc(&cmd
->t_fe_count
);
3920 atomic_inc(&cmd
->t_se_count
);
3923 cmd
->t_task_list_num
= task_cdbs
;
3925 atomic_set(&cmd
->t_task_cdbs_left
, task_cdbs
);
3926 atomic_set(&cmd
->t_task_cdbs_ex_left
, task_cdbs
);
3927 atomic_set(&cmd
->t_task_cdbs_timeout_left
, task_cdbs
);
3931 void *transport_kmap_first_data_page(struct se_cmd
*cmd
)
3933 struct scatterlist
*sg
= cmd
->t_data_sg
;
3937 * We need to take into account a possible offset here for fabrics like
3938 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3939 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3941 return kmap(sg_page(sg
)) + sg
->offset
;
3943 EXPORT_SYMBOL(transport_kmap_first_data_page
);
3945 void transport_kunmap_first_data_page(struct se_cmd
*cmd
)
3947 kunmap(sg_page(cmd
->t_data_sg
));
3949 EXPORT_SYMBOL(transport_kunmap_first_data_page
);
3952 transport_generic_get_mem(struct se_cmd
*cmd
)
3954 u32 length
= cmd
->data_length
;
3959 nents
= DIV_ROUND_UP(length
, PAGE_SIZE
);
3960 cmd
->t_data_sg
= kmalloc(sizeof(struct scatterlist
) * nents
, GFP_KERNEL
);
3961 if (!cmd
->t_data_sg
)
3964 cmd
->t_data_nents
= nents
;
3965 sg_init_table(cmd
->t_data_sg
, nents
);
3968 u32 page_len
= min_t(u32
, length
, PAGE_SIZE
);
3969 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
3973 sg_set_page(&cmd
->t_data_sg
[i
], page
, page_len
, 0);
3981 __free_page(sg_page(&cmd
->t_data_sg
[i
]));
3984 kfree(cmd
->t_data_sg
);
3985 cmd
->t_data_sg
= NULL
;
3989 /* Reduce sectors if they are too long for the device */
3990 static inline sector_t
transport_limit_task_sectors(
3991 struct se_device
*dev
,
3992 unsigned long long lba
,
3995 sectors
= min_t(sector_t
, sectors
, dev
->se_sub_dev
->se_dev_attrib
.max_sectors
);
3997 if (dev
->transport
->get_device_type(dev
) == TYPE_DISK
)
3998 if ((lba
+ sectors
) > transport_dev_end_lba(dev
))
3999 sectors
= ((transport_dev_end_lba(dev
) - lba
) + 1);
4006 * This function can be used by HW target mode drivers to create a linked
4007 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4008 * This is intended to be called during the completion path by TCM Core
4009 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4011 void transport_do_task_sg_chain(struct se_cmd
*cmd
)
4013 struct scatterlist
*sg_first
= NULL
;
4014 struct scatterlist
*sg_prev
= NULL
;
4015 int sg_prev_nents
= 0;
4016 struct scatterlist
*sg
;
4017 struct se_task
*task
;
4018 u32 chained_nents
= 0;
4021 BUG_ON(!cmd
->se_tfo
->task_sg_chaining
);
4024 * Walk the struct se_task list and setup scatterlist chains
4025 * for each contiguously allocated struct se_task->task_sg[].
4027 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
4031 BUG_ON(!task
->task_padded_sg
);
4034 sg_first
= task
->task_sg
;
4035 chained_nents
= task
->task_sg_nents
;
4037 sg_chain(sg_prev
, sg_prev_nents
, task
->task_sg
);
4038 chained_nents
+= task
->task_sg_nents
;
4041 sg_prev
= task
->task_sg
;
4042 sg_prev_nents
= task
->task_sg_nents
;
4045 * Setup the starting pointer and total t_tasks_sg_linked_no including
4046 * padding SGs for linking and to mark the end.
4048 cmd
->t_tasks_sg_chained
= sg_first
;
4049 cmd
->t_tasks_sg_chained_no
= chained_nents
;
4051 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
4052 " t_tasks_sg_chained_no: %u\n", cmd
, cmd
->t_tasks_sg_chained
,
4053 cmd
->t_tasks_sg_chained_no
);
4055 for_each_sg(cmd
->t_tasks_sg_chained
, sg
,
4056 cmd
->t_tasks_sg_chained_no
, i
) {
4058 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
4059 i
, sg
, sg_page(sg
), sg
->length
, sg
->offset
);
4060 if (sg_is_chain(sg
))
4061 pr_debug("SG: %p sg_is_chain=1\n", sg
);
4063 pr_debug("SG: %p sg_is_last=1\n", sg
);
4066 EXPORT_SYMBOL(transport_do_task_sg_chain
);
4069 * Break up cmd into chunks transport can handle
4071 static int transport_allocate_data_tasks(
4073 unsigned long long lba
,
4074 enum dma_data_direction data_direction
,
4075 struct scatterlist
*sgl
,
4076 unsigned int sgl_nents
)
4078 unsigned char *cdb
= NULL
;
4079 struct se_task
*task
;
4080 struct se_device
*dev
= cmd
->se_dev
;
4081 unsigned long flags
;
4082 int task_count
, i
, ret
;
4083 sector_t sectors
, dev_max_sectors
= dev
->se_sub_dev
->se_dev_attrib
.max_sectors
;
4084 u32 sector_size
= dev
->se_sub_dev
->se_dev_attrib
.block_size
;
4085 struct scatterlist
*sg
;
4086 struct scatterlist
*cmd_sg
;
4088 WARN_ON(cmd
->data_length
% sector_size
);
4089 sectors
= DIV_ROUND_UP(cmd
->data_length
, sector_size
);
4090 task_count
= DIV_ROUND_UP_SECTOR_T(sectors
, dev_max_sectors
);
4093 for (i
= 0; i
< task_count
; i
++) {
4094 unsigned int task_size
;
4097 task
= transport_generic_get_task(cmd
, data_direction
);
4101 task
->task_lba
= lba
;
4102 task
->task_sectors
= min(sectors
, dev_max_sectors
);
4103 task
->task_size
= task
->task_sectors
* sector_size
;
4105 cdb
= dev
->transport
->get_cdb(task
);
4108 memcpy(cdb
, cmd
->t_task_cdb
,
4109 scsi_command_size(cmd
->t_task_cdb
));
4111 /* Update new cdb with updated lba/sectors */
4112 cmd
->transport_split_cdb(task
->task_lba
, task
->task_sectors
, cdb
);
4115 * Check if the fabric module driver is requesting that all
4116 * struct se_task->task_sg[] be chained together.. If so,
4117 * then allocate an extra padding SG entry for linking and
4118 * marking the end of the chained SGL.
4119 * Possibly over-allocate task sgl size by using cmd sgl size.
4120 * It's so much easier and only a waste when task_count > 1.
4121 * That is extremely rare.
4123 task
->task_sg_nents
= sgl_nents
;
4124 if (cmd
->se_tfo
->task_sg_chaining
) {
4125 task
->task_sg_nents
++;
4126 task
->task_padded_sg
= 1;
4129 task
->task_sg
= kmalloc(sizeof(struct scatterlist
) *
4130 task
->task_sg_nents
, GFP_KERNEL
);
4131 if (!task
->task_sg
) {
4132 cmd
->se_dev
->transport
->free_task(task
);
4136 sg_init_table(task
->task_sg
, task
->task_sg_nents
);
4138 task_size
= task
->task_size
;
4140 /* Build new sgl, only up to task_size */
4141 for_each_sg(task
->task_sg
, sg
, task
->task_sg_nents
, count
) {
4142 if (cmd_sg
->length
> task_size
)
4146 task_size
-= cmd_sg
->length
;
4147 cmd_sg
= sg_next(cmd_sg
);
4150 lba
+= task
->task_sectors
;
4151 sectors
-= task
->task_sectors
;
4153 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4154 list_add_tail(&task
->t_list
, &cmd
->t_task_list
);
4155 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4158 * Now perform the memory map of task->task_sg[] into backend
4159 * subsystem memory..
4161 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
4162 if (atomic_read(&task
->task_sent
))
4164 if (!dev
->transport
->map_data_SG
)
4167 ret
= dev
->transport
->map_data_SG(task
);
4176 transport_allocate_control_task(struct se_cmd
*cmd
)
4178 struct se_device
*dev
= cmd
->se_dev
;
4180 struct se_task
*task
;
4181 unsigned long flags
;
4184 task
= transport_generic_get_task(cmd
, cmd
->data_direction
);
4188 cdb
= dev
->transport
->get_cdb(task
);
4190 memcpy(cdb
, cmd
->t_task_cdb
,
4191 scsi_command_size(cmd
->t_task_cdb
));
4193 task
->task_sg
= kmalloc(sizeof(struct scatterlist
) * cmd
->t_data_nents
,
4195 if (!task
->task_sg
) {
4196 cmd
->se_dev
->transport
->free_task(task
);
4200 memcpy(task
->task_sg
, cmd
->t_data_sg
,
4201 sizeof(struct scatterlist
) * cmd
->t_data_nents
);
4202 task
->task_size
= cmd
->data_length
;
4203 task
->task_sg_nents
= cmd
->t_data_nents
;
4205 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4206 list_add_tail(&task
->t_list
, &cmd
->t_task_list
);
4207 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4209 if (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
) {
4210 if (dev
->transport
->map_control_SG
)
4211 ret
= dev
->transport
->map_control_SG(task
);
4212 } else if (cmd
->se_cmd_flags
& SCF_SCSI_NON_DATA_CDB
) {
4213 if (dev
->transport
->cdb_none
)
4214 ret
= dev
->transport
->cdb_none(task
);
4216 pr_err("target: Unknown control cmd type!\n");
4220 /* Success! Return number of tasks allocated */
4226 static u32
transport_allocate_tasks(
4228 unsigned long long lba
,
4229 enum dma_data_direction data_direction
,
4230 struct scatterlist
*sgl
,
4231 unsigned int sgl_nents
)
4233 if (cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
)
4234 return transport_allocate_data_tasks(cmd
, lba
, data_direction
,
4237 return transport_allocate_control_task(cmd
);
4242 /* transport_generic_new_cmd(): Called from transport_processing_thread()
4244 * Allocate storage transport resources from a set of values predefined
4245 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4246 * Any non zero return here is treated as an "out of resource' op here.
4249 * Generate struct se_task(s) and/or their payloads for this CDB.
4251 int transport_generic_new_cmd(struct se_cmd
*cmd
)
4256 * Determine is the TCM fabric module has already allocated physical
4257 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4260 if (!(cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) &&
4262 ret
= transport_generic_get_mem(cmd
);
4267 * Call transport_new_cmd_obj() to invoke transport_allocate_tasks() for
4268 * control or data CDB types, and perform the map to backend subsystem
4269 * code from SGL memory allocated here by transport_generic_get_mem(), or
4270 * via pre-existing SGL memory setup explictly by fabric module code with
4271 * transport_generic_map_mem_to_cmd().
4273 ret
= transport_new_cmd_obj(cmd
);
4277 * For WRITEs, let the fabric know its buffer is ready..
4278 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4279 * will be added to the struct se_device execution queue after its WRITE
4280 * data has arrived. (ie: It gets handled by the transport processing
4281 * thread a second time)
4283 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
4284 transport_add_tasks_to_state_queue(cmd
);
4285 return transport_generic_write_pending(cmd
);
4288 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4289 * to the execution queue.
4291 transport_execute_tasks(cmd
);
4294 EXPORT_SYMBOL(transport_generic_new_cmd
);
4296 /* transport_generic_process_write():
4300 void transport_generic_process_write(struct se_cmd
*cmd
)
4302 transport_execute_tasks(cmd
);
4304 EXPORT_SYMBOL(transport_generic_process_write
);
4306 static int transport_write_pending_qf(struct se_cmd
*cmd
)
4308 return cmd
->se_tfo
->write_pending(cmd
);
4311 /* transport_generic_write_pending():
4315 static int transport_generic_write_pending(struct se_cmd
*cmd
)
4317 unsigned long flags
;
4320 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4321 cmd
->t_state
= TRANSPORT_WRITE_PENDING
;
4322 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4324 if (cmd
->transport_qf_callback
) {
4325 ret
= cmd
->transport_qf_callback(cmd
);
4331 cmd
->transport_qf_callback
= NULL
;
4336 * Clear the se_cmd for WRITE_PENDING status in order to set
4337 * cmd->t_transport_active=0 so that transport_generic_handle_data
4338 * can be called from HW target mode interrupt code. This is safe
4339 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
4340 * because the se_cmd->se_lun pointer is not being cleared.
4342 transport_cmd_check_stop(cmd
, 1, 0);
4345 * Call the fabric write_pending function here to let the
4346 * frontend know that WRITE buffers are ready.
4348 ret
= cmd
->se_tfo
->write_pending(cmd
);
4354 return PYX_TRANSPORT_WRITE_PENDING
;
4357 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd
);
4358 cmd
->t_state
= TRANSPORT_COMPLETE_QF_WP
;
4359 transport_handle_queue_full(cmd
, cmd
->se_dev
,
4360 transport_write_pending_qf
);
4364 void transport_release_cmd(struct se_cmd
*cmd
)
4366 BUG_ON(!cmd
->se_tfo
);
4368 transport_free_se_cmd(cmd
);
4369 cmd
->se_tfo
->release_cmd(cmd
);
4371 EXPORT_SYMBOL(transport_release_cmd
);
4373 /* transport_generic_free_cmd():
4375 * Called from processing frontend to release storage engine resources
4377 void transport_generic_free_cmd(
4380 int session_reinstatement
)
4382 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
))
4383 transport_release_cmd(cmd
);
4385 core_dec_lacl_count(cmd
->se_sess
->se_node_acl
, cmd
);
4389 pr_debug("cmd: %p ITT: 0x%08x contains"
4390 " cmd->se_lun\n", cmd
,
4391 cmd
->se_tfo
->get_task_tag(cmd
));
4393 transport_lun_remove_cmd(cmd
);
4396 if (wait_for_tasks
&& cmd
->transport_wait_for_tasks
)
4397 cmd
->transport_wait_for_tasks(cmd
, 0, 0);
4399 transport_free_dev_tasks(cmd
);
4401 transport_generic_remove(cmd
, session_reinstatement
);
4404 EXPORT_SYMBOL(transport_generic_free_cmd
);
4406 static void transport_nop_wait_for_tasks(
4409 int session_reinstatement
)
4414 /* transport_lun_wait_for_tasks():
4416 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4417 * an struct se_lun to be successfully shutdown.
4419 static int transport_lun_wait_for_tasks(struct se_cmd
*cmd
, struct se_lun
*lun
)
4421 unsigned long flags
;
4424 * If the frontend has already requested this struct se_cmd to
4425 * be stopped, we can safely ignore this struct se_cmd.
4427 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4428 if (atomic_read(&cmd
->t_transport_stop
)) {
4429 atomic_set(&cmd
->transport_lun_stop
, 0);
4430 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4431 " TRUE, skipping\n", cmd
->se_tfo
->get_task_tag(cmd
));
4432 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4433 transport_cmd_check_stop(cmd
, 1, 0);
4436 atomic_set(&cmd
->transport_lun_fe_stop
, 1);
4437 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4439 wake_up_interruptible(&cmd
->se_dev
->dev_queue_obj
.thread_wq
);
4441 ret
= transport_stop_tasks_for_cmd(cmd
);
4443 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4444 " %d\n", cmd
, cmd
->t_task_list_num
, ret
);
4446 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4447 cmd
->se_tfo
->get_task_tag(cmd
));
4448 wait_for_completion(&cmd
->transport_lun_stop_comp
);
4449 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4450 cmd
->se_tfo
->get_task_tag(cmd
));
4452 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
4457 static void __transport_clear_lun_from_sessions(struct se_lun
*lun
)
4459 struct se_cmd
*cmd
= NULL
;
4460 unsigned long lun_flags
, cmd_flags
;
4462 * Do exception processing and return CHECK_CONDITION status to the
4465 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4466 while (!list_empty(&lun
->lun_cmd_list
)) {
4467 cmd
= list_first_entry(&lun
->lun_cmd_list
,
4468 struct se_cmd
, se_lun_node
);
4469 list_del(&cmd
->se_lun_node
);
4471 atomic_set(&cmd
->transport_lun_active
, 0);
4473 * This will notify iscsi_target_transport.c:
4474 * transport_cmd_check_stop() that a LUN shutdown is in
4475 * progress for the iscsi_cmd_t.
4477 spin_lock(&cmd
->t_state_lock
);
4478 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4479 "_lun_stop for ITT: 0x%08x\n",
4480 cmd
->se_lun
->unpacked_lun
,
4481 cmd
->se_tfo
->get_task_tag(cmd
));
4482 atomic_set(&cmd
->transport_lun_stop
, 1);
4483 spin_unlock(&cmd
->t_state_lock
);
4485 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
4488 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4489 cmd
->se_tfo
->get_task_tag(cmd
),
4490 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
4494 * If the Storage engine still owns the iscsi_cmd_t, determine
4495 * and/or stop its context.
4497 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4498 "_lun_wait_for_tasks()\n", cmd
->se_lun
->unpacked_lun
,
4499 cmd
->se_tfo
->get_task_tag(cmd
));
4501 if (transport_lun_wait_for_tasks(cmd
, cmd
->se_lun
) < 0) {
4502 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4506 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4507 "_wait_for_tasks(): SUCCESS\n",
4508 cmd
->se_lun
->unpacked_lun
,
4509 cmd
->se_tfo
->get_task_tag(cmd
));
4511 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
4512 if (!atomic_read(&cmd
->transport_dev_active
)) {
4513 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
4516 atomic_set(&cmd
->transport_dev_active
, 0);
4517 transport_all_task_dev_remove_state(cmd
);
4518 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
4520 transport_free_dev_tasks(cmd
);
4522 * The Storage engine stopped this struct se_cmd before it was
4523 * send to the fabric frontend for delivery back to the
4524 * Initiator Node. Return this SCSI CDB back with an
4525 * CHECK_CONDITION status.
4528 transport_send_check_condition_and_sense(cmd
,
4529 TCM_NON_EXISTENT_LUN
, 0);
4531 * If the fabric frontend is waiting for this iscsi_cmd_t to
4532 * be released, notify the waiting thread now that LU has
4533 * finished accessing it.
4535 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
4536 if (atomic_read(&cmd
->transport_lun_fe_stop
)) {
4537 pr_debug("SE_LUN[%d] - Detected FE stop for"
4538 " struct se_cmd: %p ITT: 0x%08x\n",
4540 cmd
, cmd
->se_tfo
->get_task_tag(cmd
));
4542 spin_unlock_irqrestore(&cmd
->t_state_lock
,
4544 transport_cmd_check_stop(cmd
, 1, 0);
4545 complete(&cmd
->transport_lun_fe_stop_comp
);
4546 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4549 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4550 lun
->unpacked_lun
, cmd
->se_tfo
->get_task_tag(cmd
));
4552 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
4553 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4555 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
4558 static int transport_clear_lun_thread(void *p
)
4560 struct se_lun
*lun
= (struct se_lun
*)p
;
4562 __transport_clear_lun_from_sessions(lun
);
4563 complete(&lun
->lun_shutdown_comp
);
4568 int transport_clear_lun_from_sessions(struct se_lun
*lun
)
4570 struct task_struct
*kt
;
4572 kt
= kthread_run(transport_clear_lun_thread
, lun
,
4573 "tcm_cl_%u", lun
->unpacked_lun
);
4575 pr_err("Unable to start clear_lun thread\n");
4578 wait_for_completion(&lun
->lun_shutdown_comp
);
4583 /* transport_generic_wait_for_tasks():
4585 * Called from frontend or passthrough context to wait for storage engine
4586 * to pause and/or release frontend generated struct se_cmd.
4588 static void transport_generic_wait_for_tasks(
4591 int session_reinstatement
)
4593 unsigned long flags
;
4595 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
) && !(cmd
->se_tmr_req
))
4598 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4600 * If we are already stopped due to an external event (ie: LUN shutdown)
4601 * sleep until the connection can have the passed struct se_cmd back.
4602 * The cmd->transport_lun_stopped_sem will be upped by
4603 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4604 * has completed its operation on the struct se_cmd.
4606 if (atomic_read(&cmd
->transport_lun_stop
)) {
4608 pr_debug("wait_for_tasks: Stopping"
4609 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4610 "_stop_comp); for ITT: 0x%08x\n",
4611 cmd
->se_tfo
->get_task_tag(cmd
));
4613 * There is a special case for WRITES where a FE exception +
4614 * LUN shutdown means ConfigFS context is still sleeping on
4615 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4616 * We go ahead and up transport_lun_stop_comp just to be sure
4619 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4620 complete(&cmd
->transport_lun_stop_comp
);
4621 wait_for_completion(&cmd
->transport_lun_fe_stop_comp
);
4622 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4624 transport_all_task_dev_remove_state(cmd
);
4626 * At this point, the frontend who was the originator of this
4627 * struct se_cmd, now owns the structure and can be released through
4628 * normal means below.
4630 pr_debug("wait_for_tasks: Stopped"
4631 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4632 "stop_comp); for ITT: 0x%08x\n",
4633 cmd
->se_tfo
->get_task_tag(cmd
));
4635 atomic_set(&cmd
->transport_lun_stop
, 0);
4637 if (!atomic_read(&cmd
->t_transport_active
) ||
4638 atomic_read(&cmd
->t_transport_aborted
))
4641 atomic_set(&cmd
->t_transport_stop
, 1);
4643 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4644 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
4645 " = TRUE\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
4646 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
,
4647 cmd
->deferred_t_state
);
4649 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4651 wake_up_interruptible(&cmd
->se_dev
->dev_queue_obj
.thread_wq
);
4653 wait_for_completion(&cmd
->t_transport_stop_comp
);
4655 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4656 atomic_set(&cmd
->t_transport_active
, 0);
4657 atomic_set(&cmd
->t_transport_stop
, 0);
4659 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4660 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4661 cmd
->se_tfo
->get_task_tag(cmd
));
4663 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4667 transport_generic_free_cmd(cmd
, 0, session_reinstatement
);
4670 static int transport_get_sense_codes(
4675 *asc
= cmd
->scsi_asc
;
4676 *ascq
= cmd
->scsi_ascq
;
4681 static int transport_set_sense_codes(
4686 cmd
->scsi_asc
= asc
;
4687 cmd
->scsi_ascq
= ascq
;
4692 int transport_send_check_condition_and_sense(
4697 unsigned char *buffer
= cmd
->sense_buffer
;
4698 unsigned long flags
;
4700 u8 asc
= 0, ascq
= 0;
4702 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4703 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
4704 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4707 cmd
->se_cmd_flags
|= SCF_SENT_CHECK_CONDITION
;
4708 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4710 if (!reason
&& from_transport
)
4713 if (!from_transport
)
4714 cmd
->se_cmd_flags
|= SCF_EMULATED_TASK_SENSE
;
4716 * Data Segment and SenseLength of the fabric response PDU.
4718 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4719 * from include/scsi/scsi_cmnd.h
4721 offset
= cmd
->se_tfo
->set_fabric_sense_len(cmd
,
4722 TRANSPORT_SENSE_BUFFER
);
4724 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4725 * SENSE KEY values from include/scsi/scsi.h
4728 case TCM_NON_EXISTENT_LUN
:
4729 case TCM_UNSUPPORTED_SCSI_OPCODE
:
4730 case TCM_SECTOR_COUNT_TOO_MANY
:
4732 buffer
[offset
] = 0x70;
4733 /* ILLEGAL REQUEST */
4734 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
4735 /* INVALID COMMAND OPERATION CODE */
4736 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x20;
4738 case TCM_UNKNOWN_MODE_PAGE
:
4740 buffer
[offset
] = 0x70;
4741 /* ILLEGAL REQUEST */
4742 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
4743 /* INVALID FIELD IN CDB */
4744 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x24;
4746 case TCM_CHECK_CONDITION_ABORT_CMD
:
4748 buffer
[offset
] = 0x70;
4749 /* ABORTED COMMAND */
4750 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4751 /* BUS DEVICE RESET FUNCTION OCCURRED */
4752 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x29;
4753 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x03;
4755 case TCM_INCORRECT_AMOUNT_OF_DATA
:
4757 buffer
[offset
] = 0x70;
4758 /* ABORTED COMMAND */
4759 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4761 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x0c;
4762 /* NOT ENOUGH UNSOLICITED DATA */
4763 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x0d;
4765 case TCM_INVALID_CDB_FIELD
:
4767 buffer
[offset
] = 0x70;
4768 /* ABORTED COMMAND */
4769 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4770 /* INVALID FIELD IN CDB */
4771 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x24;
4773 case TCM_INVALID_PARAMETER_LIST
:
4775 buffer
[offset
] = 0x70;
4776 /* ABORTED COMMAND */
4777 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4778 /* INVALID FIELD IN PARAMETER LIST */
4779 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x26;
4781 case TCM_UNEXPECTED_UNSOLICITED_DATA
:
4783 buffer
[offset
] = 0x70;
4784 /* ABORTED COMMAND */
4785 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4787 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x0c;
4788 /* UNEXPECTED_UNSOLICITED_DATA */
4789 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x0c;
4791 case TCM_SERVICE_CRC_ERROR
:
4793 buffer
[offset
] = 0x70;
4794 /* ABORTED COMMAND */
4795 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4796 /* PROTOCOL SERVICE CRC ERROR */
4797 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x47;
4799 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x05;
4801 case TCM_SNACK_REJECTED
:
4803 buffer
[offset
] = 0x70;
4804 /* ABORTED COMMAND */
4805 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4807 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x11;
4808 /* FAILED RETRANSMISSION REQUEST */
4809 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x13;
4811 case TCM_WRITE_PROTECTED
:
4813 buffer
[offset
] = 0x70;
4815 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = DATA_PROTECT
;
4816 /* WRITE PROTECTED */
4817 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x27;
4819 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
4821 buffer
[offset
] = 0x70;
4822 /* UNIT ATTENTION */
4823 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
4824 core_scsi3_ua_for_check_condition(cmd
, &asc
, &ascq
);
4825 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = asc
;
4826 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = ascq
;
4828 case TCM_CHECK_CONDITION_NOT_READY
:
4830 buffer
[offset
] = 0x70;
4832 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
4833 transport_get_sense_codes(cmd
, &asc
, &ascq
);
4834 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = asc
;
4835 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = ascq
;
4837 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
4840 buffer
[offset
] = 0x70;
4841 /* ILLEGAL REQUEST */
4842 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
4843 /* LOGICAL UNIT COMMUNICATION FAILURE */
4844 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x80;
4848 * This code uses linux/include/scsi/scsi.h SAM status codes!
4850 cmd
->scsi_status
= SAM_STAT_CHECK_CONDITION
;
4852 * Automatically padded, this value is encoded in the fabric's
4853 * data_length response PDU containing the SCSI defined sense data.
4855 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
+ offset
;
4858 return cmd
->se_tfo
->queue_status(cmd
);
4860 EXPORT_SYMBOL(transport_send_check_condition_and_sense
);
4862 int transport_check_aborted_status(struct se_cmd
*cmd
, int send_status
)
4866 if (atomic_read(&cmd
->t_transport_aborted
) != 0) {
4868 (cmd
->se_cmd_flags
& SCF_SENT_DELAYED_TAS
))
4871 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4872 " status for CDB: 0x%02x ITT: 0x%08x\n",
4874 cmd
->se_tfo
->get_task_tag(cmd
));
4876 cmd
->se_cmd_flags
|= SCF_SENT_DELAYED_TAS
;
4877 cmd
->se_tfo
->queue_status(cmd
);
4882 EXPORT_SYMBOL(transport_check_aborted_status
);
4884 void transport_send_task_abort(struct se_cmd
*cmd
)
4887 * If there are still expected incoming fabric WRITEs, we wait
4888 * until until they have completed before sending a TASK_ABORTED
4889 * response. This response with TASK_ABORTED status will be
4890 * queued back to fabric module by transport_check_aborted_status().
4892 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
4893 if (cmd
->se_tfo
->write_pending_status(cmd
) != 0) {
4894 atomic_inc(&cmd
->t_transport_aborted
);
4895 smp_mb__after_atomic_inc();
4896 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
4897 transport_new_cmd_failure(cmd
);
4901 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
4903 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4904 " ITT: 0x%08x\n", cmd
->t_task_cdb
[0],
4905 cmd
->se_tfo
->get_task_tag(cmd
));
4907 cmd
->se_tfo
->queue_status(cmd
);
4910 /* transport_generic_do_tmr():
4914 int transport_generic_do_tmr(struct se_cmd
*cmd
)
4916 struct se_device
*dev
= cmd
->se_dev
;
4917 struct se_tmr_req
*tmr
= cmd
->se_tmr_req
;
4920 switch (tmr
->function
) {
4921 case TMR_ABORT_TASK
:
4922 tmr
->response
= TMR_FUNCTION_REJECTED
;
4924 case TMR_ABORT_TASK_SET
:
4926 case TMR_CLEAR_TASK_SET
:
4927 tmr
->response
= TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
4930 ret
= core_tmr_lun_reset(dev
, tmr
, NULL
, NULL
);
4931 tmr
->response
= (!ret
) ? TMR_FUNCTION_COMPLETE
:
4932 TMR_FUNCTION_REJECTED
;
4934 case TMR_TARGET_WARM_RESET
:
4935 tmr
->response
= TMR_FUNCTION_REJECTED
;
4937 case TMR_TARGET_COLD_RESET
:
4938 tmr
->response
= TMR_FUNCTION_REJECTED
;
4941 pr_err("Uknown TMR function: 0x%02x.\n",
4943 tmr
->response
= TMR_FUNCTION_REJECTED
;
4947 cmd
->t_state
= TRANSPORT_ISTATE_PROCESSING
;
4948 cmd
->se_tfo
->queue_tm_rsp(cmd
);
4950 transport_cmd_check_stop(cmd
, 2, 0);
4955 * Called with spin_lock_irq(&dev->execute_task_lock); held
4958 static struct se_task
*
4959 transport_get_task_from_state_list(struct se_device
*dev
)
4961 struct se_task
*task
;
4963 if (list_empty(&dev
->state_task_list
))
4966 list_for_each_entry(task
, &dev
->state_task_list
, t_state_list
)
4969 list_del(&task
->t_state_list
);
4970 atomic_set(&task
->task_state_active
, 0);
4975 static void transport_processing_shutdown(struct se_device
*dev
)
4978 struct se_task
*task
;
4979 unsigned long flags
;
4981 * Empty the struct se_device's struct se_task state list.
4983 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
4984 while ((task
= transport_get_task_from_state_list(dev
))) {
4985 if (!task
->task_se_cmd
) {
4986 pr_err("task->task_se_cmd is NULL!\n");
4989 cmd
= task
->task_se_cmd
;
4991 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
4993 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4995 pr_debug("PT: cmd: %p task: %p ITT: 0x%08x,"
4996 " i_state: %d, t_state/def_t_state:"
4997 " %d/%d cdb: 0x%02x\n", cmd
, task
,
4998 cmd
->se_tfo
->get_task_tag(cmd
),
4999 cmd
->se_tfo
->get_cmd_state(cmd
),
5000 cmd
->t_state
, cmd
->deferred_t_state
,
5001 cmd
->t_task_cdb
[0]);
5002 pr_debug("PT: ITT[0x%08x] - t_tasks: %d t_task_cdbs_left:"
5003 " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5004 " t_transport_stop: %d t_transport_sent: %d\n",
5005 cmd
->se_tfo
->get_task_tag(cmd
),
5006 cmd
->t_task_list_num
,
5007 atomic_read(&cmd
->t_task_cdbs_left
),
5008 atomic_read(&cmd
->t_task_cdbs_sent
),
5009 atomic_read(&cmd
->t_transport_active
),
5010 atomic_read(&cmd
->t_transport_stop
),
5011 atomic_read(&cmd
->t_transport_sent
));
5013 if (atomic_read(&task
->task_active
)) {
5014 atomic_set(&task
->task_stop
, 1);
5015 spin_unlock_irqrestore(
5016 &cmd
->t_state_lock
, flags
);
5018 pr_debug("Waiting for task: %p to shutdown for dev:"
5019 " %p\n", task
, dev
);
5020 wait_for_completion(&task
->task_stop_comp
);
5021 pr_debug("Completed task: %p shutdown for dev: %p\n",
5024 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
5025 atomic_dec(&cmd
->t_task_cdbs_left
);
5027 atomic_set(&task
->task_active
, 0);
5028 atomic_set(&task
->task_stop
, 0);
5030 if (atomic_read(&task
->task_execute_queue
) != 0)
5031 transport_remove_task_from_execute_queue(task
, dev
);
5033 __transport_stop_task_timer(task
, &flags
);
5035 if (!atomic_dec_and_test(&cmd
->t_task_cdbs_ex_left
)) {
5036 spin_unlock_irqrestore(
5037 &cmd
->t_state_lock
, flags
);
5039 pr_debug("Skipping task: %p, dev: %p for"
5040 " t_task_cdbs_ex_left: %d\n", task
, dev
,
5041 atomic_read(&cmd
->t_task_cdbs_ex_left
));
5043 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5047 if (atomic_read(&cmd
->t_transport_active
)) {
5048 pr_debug("got t_transport_active = 1 for task: %p, dev:"
5049 " %p\n", task
, dev
);
5051 if (atomic_read(&cmd
->t_fe_count
)) {
5052 spin_unlock_irqrestore(
5053 &cmd
->t_state_lock
, flags
);
5054 transport_send_check_condition_and_sense(
5055 cmd
, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
,
5057 transport_remove_cmd_from_queue(cmd
,
5058 &cmd
->se_dev
->dev_queue_obj
);
5060 transport_lun_remove_cmd(cmd
);
5061 transport_cmd_check_stop(cmd
, 1, 0);
5063 spin_unlock_irqrestore(
5064 &cmd
->t_state_lock
, flags
);
5066 transport_remove_cmd_from_queue(cmd
,
5067 &cmd
->se_dev
->dev_queue_obj
);
5069 transport_lun_remove_cmd(cmd
);
5071 if (transport_cmd_check_stop(cmd
, 1, 0))
5072 transport_generic_remove(cmd
, 0);
5075 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5078 pr_debug("Got t_transport_active = 0 for task: %p, dev: %p\n",
5081 if (atomic_read(&cmd
->t_fe_count
)) {
5082 spin_unlock_irqrestore(
5083 &cmd
->t_state_lock
, flags
);
5084 transport_send_check_condition_and_sense(cmd
,
5085 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
5086 transport_remove_cmd_from_queue(cmd
,
5087 &cmd
->se_dev
->dev_queue_obj
);
5089 transport_lun_remove_cmd(cmd
);
5090 transport_cmd_check_stop(cmd
, 1, 0);
5092 spin_unlock_irqrestore(
5093 &cmd
->t_state_lock
, flags
);
5095 transport_remove_cmd_from_queue(cmd
,
5096 &cmd
->se_dev
->dev_queue_obj
);
5097 transport_lun_remove_cmd(cmd
);
5099 if (transport_cmd_check_stop(cmd
, 1, 0))
5100 transport_generic_remove(cmd
, 0);
5103 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5105 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
5107 * Empty the struct se_device's struct se_cmd list.
5109 while ((cmd
= transport_get_cmd_from_queue(&dev
->dev_queue_obj
))) {
5111 pr_debug("From Device Queue: cmd: %p t_state: %d\n",
5114 if (atomic_read(&cmd
->t_fe_count
)) {
5115 transport_send_check_condition_and_sense(cmd
,
5116 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
5118 transport_lun_remove_cmd(cmd
);
5119 transport_cmd_check_stop(cmd
, 1, 0);
5121 transport_lun_remove_cmd(cmd
);
5122 if (transport_cmd_check_stop(cmd
, 1, 0))
5123 transport_generic_remove(cmd
, 0);
5128 /* transport_processing_thread():
5132 static int transport_processing_thread(void *param
)
5136 struct se_device
*dev
= (struct se_device
*) param
;
5138 set_user_nice(current
, -20);
5140 while (!kthread_should_stop()) {
5141 ret
= wait_event_interruptible(dev
->dev_queue_obj
.thread_wq
,
5142 atomic_read(&dev
->dev_queue_obj
.queue_cnt
) ||
5143 kthread_should_stop());
5147 spin_lock_irq(&dev
->dev_status_lock
);
5148 if (dev
->dev_status
& TRANSPORT_DEVICE_SHUTDOWN
) {
5149 spin_unlock_irq(&dev
->dev_status_lock
);
5150 transport_processing_shutdown(dev
);
5153 spin_unlock_irq(&dev
->dev_status_lock
);
5156 __transport_execute_tasks(dev
);
5158 cmd
= transport_get_cmd_from_queue(&dev
->dev_queue_obj
);
5162 switch (cmd
->t_state
) {
5163 case TRANSPORT_NEW_CMD_MAP
:
5164 if (!cmd
->se_tfo
->new_cmd_map
) {
5165 pr_err("cmd->se_tfo->new_cmd_map is"
5166 " NULL for TRANSPORT_NEW_CMD_MAP\n");
5169 ret
= cmd
->se_tfo
->new_cmd_map(cmd
);
5171 cmd
->transport_error_status
= ret
;
5172 transport_generic_request_failure(cmd
, NULL
,
5173 0, (cmd
->data_direction
!=
5178 case TRANSPORT_NEW_CMD
:
5179 ret
= transport_generic_new_cmd(cmd
);
5183 cmd
->transport_error_status
= ret
;
5184 transport_generic_request_failure(cmd
, NULL
,
5185 0, (cmd
->data_direction
!=
5189 case TRANSPORT_PROCESS_WRITE
:
5190 transport_generic_process_write(cmd
);
5192 case TRANSPORT_COMPLETE_OK
:
5193 transport_stop_all_task_timers(cmd
);
5194 transport_generic_complete_ok(cmd
);
5196 case TRANSPORT_REMOVE
:
5197 transport_generic_remove(cmd
, 0);
5199 case TRANSPORT_FREE_CMD_INTR
:
5200 transport_generic_free_cmd(cmd
, 0, 0);
5202 case TRANSPORT_PROCESS_TMR
:
5203 transport_generic_do_tmr(cmd
);
5205 case TRANSPORT_COMPLETE_FAILURE
:
5206 transport_generic_request_failure(cmd
, NULL
, 1, 1);
5208 case TRANSPORT_COMPLETE_TIMEOUT
:
5209 transport_stop_all_task_timers(cmd
);
5210 transport_generic_request_timeout(cmd
);
5212 case TRANSPORT_COMPLETE_QF_WP
:
5213 transport_generic_write_pending(cmd
);
5216 pr_err("Unknown t_state: %d deferred_t_state:"
5217 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
5218 " %u\n", cmd
->t_state
, cmd
->deferred_t_state
,
5219 cmd
->se_tfo
->get_task_tag(cmd
),
5220 cmd
->se_tfo
->get_cmd_state(cmd
),
5221 cmd
->se_lun
->unpacked_lun
);
5229 transport_release_all_cmds(dev
);
5230 dev
->process_thread
= NULL
;