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
;
395 transport_free_session(se_sess
);
399 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
400 list_del(&se_sess
->sess_list
);
401 se_sess
->se_tpg
= NULL
;
402 se_sess
->fabric_sess_ptr
= NULL
;
403 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
406 * Determine if we need to do extra work for this initiator node's
407 * struct se_node_acl if it had been previously dynamically generated.
409 se_nacl
= se_sess
->se_node_acl
;
411 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
412 if (se_nacl
->dynamic_node_acl
) {
413 if (!se_tpg
->se_tpg_tfo
->tpg_check_demo_mode_cache(
415 list_del(&se_nacl
->acl_list
);
416 se_tpg
->num_node_acls
--;
417 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
419 core_tpg_wait_for_nacl_pr_ref(se_nacl
);
420 core_free_device_list_for_node(se_nacl
, se_tpg
);
421 se_tpg
->se_tpg_tfo
->tpg_release_fabric_acl(se_tpg
,
423 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
426 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
429 transport_free_session(se_sess
);
431 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
432 se_tpg
->se_tpg_tfo
->get_fabric_name());
434 EXPORT_SYMBOL(transport_deregister_session
);
437 * Called with cmd->t_state_lock held.
439 static void transport_all_task_dev_remove_state(struct se_cmd
*cmd
)
441 struct se_device
*dev
;
442 struct se_task
*task
;
445 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
450 if (atomic_read(&task
->task_active
))
453 if (!atomic_read(&task
->task_state_active
))
456 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
457 list_del(&task
->t_state_list
);
458 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
459 cmd
->se_tfo
->get_task_tag(cmd
), dev
, task
);
460 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
462 atomic_set(&task
->task_state_active
, 0);
463 atomic_dec(&cmd
->t_task_cdbs_ex_left
);
467 /* transport_cmd_check_stop():
469 * 'transport_off = 1' determines if t_transport_active should be cleared.
470 * 'transport_off = 2' determines if task_dev_state should be removed.
472 * A non-zero u8 t_state sets cmd->t_state.
473 * Returns 1 when command is stopped, else 0.
475 static int transport_cmd_check_stop(
482 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
484 * Determine if IOCTL context caller in requesting the stopping of this
485 * command for LUN shutdown purposes.
487 if (atomic_read(&cmd
->transport_lun_stop
)) {
488 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
489 " == TRUE for ITT: 0x%08x\n", __func__
, __LINE__
,
490 cmd
->se_tfo
->get_task_tag(cmd
));
492 cmd
->deferred_t_state
= cmd
->t_state
;
493 cmd
->t_state
= TRANSPORT_DEFERRED_CMD
;
494 atomic_set(&cmd
->t_transport_active
, 0);
495 if (transport_off
== 2)
496 transport_all_task_dev_remove_state(cmd
);
497 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
499 complete(&cmd
->transport_lun_stop_comp
);
503 * Determine if frontend context caller is requesting the stopping of
504 * this command for frontend exceptions.
506 if (atomic_read(&cmd
->t_transport_stop
)) {
507 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
508 " TRUE for ITT: 0x%08x\n", __func__
, __LINE__
,
509 cmd
->se_tfo
->get_task_tag(cmd
));
511 cmd
->deferred_t_state
= cmd
->t_state
;
512 cmd
->t_state
= TRANSPORT_DEFERRED_CMD
;
513 if (transport_off
== 2)
514 transport_all_task_dev_remove_state(cmd
);
517 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
520 if (transport_off
== 2)
522 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
524 complete(&cmd
->t_transport_stop_comp
);
528 atomic_set(&cmd
->t_transport_active
, 0);
529 if (transport_off
== 2) {
530 transport_all_task_dev_remove_state(cmd
);
532 * Clear struct se_cmd->se_lun before the transport_off == 2
533 * handoff to fabric module.
537 * Some fabric modules like tcm_loop can release
538 * their internally allocated I/O reference now and
541 if (cmd
->se_tfo
->check_stop_free
!= NULL
) {
542 spin_unlock_irqrestore(
543 &cmd
->t_state_lock
, flags
);
545 cmd
->se_tfo
->check_stop_free(cmd
);
549 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
553 cmd
->t_state
= t_state
;
554 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
559 static int transport_cmd_check_stop_to_fabric(struct se_cmd
*cmd
)
561 return transport_cmd_check_stop(cmd
, 2, 0);
564 static void transport_lun_remove_cmd(struct se_cmd
*cmd
)
566 struct se_lun
*lun
= cmd
->se_lun
;
572 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
573 if (!atomic_read(&cmd
->transport_dev_active
)) {
574 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
577 atomic_set(&cmd
->transport_dev_active
, 0);
578 transport_all_task_dev_remove_state(cmd
);
579 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
583 spin_lock_irqsave(&lun
->lun_cmd_lock
, flags
);
584 if (atomic_read(&cmd
->transport_lun_active
)) {
585 list_del(&cmd
->se_lun_node
);
586 atomic_set(&cmd
->transport_lun_active
, 0);
588 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
589 cmd
->se_tfo
->get_task_tag(cmd
), lun
->unpacked_lun
);
592 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, flags
);
595 void transport_cmd_finish_abort(struct se_cmd
*cmd
, int remove
)
597 transport_lun_remove_cmd(cmd
);
599 if (transport_cmd_check_stop_to_fabric(cmd
))
602 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
603 transport_generic_remove(cmd
, 0);
607 void transport_cmd_finish_abort_tmr(struct se_cmd
*cmd
)
609 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
611 if (transport_cmd_check_stop_to_fabric(cmd
))
614 transport_generic_remove(cmd
, 0);
617 static void transport_add_cmd_to_queue(
621 struct se_device
*dev
= cmd
->se_dev
;
622 struct se_queue_obj
*qobj
= &dev
->dev_queue_obj
;
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
);
634 /* If the cmd is already on the list, remove it before we add it */
635 if (!list_empty(&cmd
->se_queue_node
))
636 list_del(&cmd
->se_queue_node
);
638 atomic_inc(&qobj
->queue_cnt
);
640 if (cmd
->se_cmd_flags
& SCF_EMULATE_QUEUE_FULL
) {
641 cmd
->se_cmd_flags
&= ~SCF_EMULATE_QUEUE_FULL
;
642 list_add(&cmd
->se_queue_node
, &qobj
->qobj_list
);
644 list_add_tail(&cmd
->se_queue_node
, &qobj
->qobj_list
);
645 atomic_set(&cmd
->t_transport_queue_active
, 1);
646 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
648 wake_up_interruptible(&qobj
->thread_wq
);
651 static struct se_cmd
*
652 transport_get_cmd_from_queue(struct se_queue_obj
*qobj
)
657 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
658 if (list_empty(&qobj
->qobj_list
)) {
659 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
662 cmd
= list_first_entry(&qobj
->qobj_list
, struct se_cmd
, se_queue_node
);
664 atomic_set(&cmd
->t_transport_queue_active
, 0);
666 list_del_init(&cmd
->se_queue_node
);
667 atomic_dec(&qobj
->queue_cnt
);
668 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
673 static void transport_remove_cmd_from_queue(struct se_cmd
*cmd
,
674 struct se_queue_obj
*qobj
)
678 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
679 if (!atomic_read(&cmd
->t_transport_queue_active
)) {
680 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
683 atomic_set(&cmd
->t_transport_queue_active
, 0);
684 atomic_dec(&qobj
->queue_cnt
);
685 list_del_init(&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 LIST_HEAD(qf_cmd_list
);
980 struct se_cmd
*cmd
, *cmd_tmp
;
982 spin_lock_irq(&dev
->qf_cmd_lock
);
983 list_splice_init(&dev
->qf_cmd_list
, &qf_cmd_list
);
984 spin_unlock_irq(&dev
->qf_cmd_lock
);
986 list_for_each_entry_safe(cmd
, cmd_tmp
, &qf_cmd_list
, se_qf_node
) {
987 list_del(&cmd
->se_qf_node
);
988 atomic_dec(&dev
->dev_qf_count
);
989 smp_mb__after_atomic_dec();
991 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
992 " context: %s\n", cmd
->se_tfo
->get_fabric_name(), cmd
,
993 (cmd
->t_state
== TRANSPORT_COMPLETE_OK
) ? "COMPLETE_OK" :
994 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
) ? "WRITE_PENDING"
997 * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd
998 * has been added to head of queue
1000 transport_add_cmd_to_queue(cmd
, cmd
->t_state
);
1004 unsigned char *transport_dump_cmd_direction(struct se_cmd
*cmd
)
1006 switch (cmd
->data_direction
) {
1009 case DMA_FROM_DEVICE
:
1013 case DMA_BIDIRECTIONAL
:
1022 void transport_dump_dev_state(
1023 struct se_device
*dev
,
1027 *bl
+= sprintf(b
+ *bl
, "Status: ");
1028 switch (dev
->dev_status
) {
1029 case TRANSPORT_DEVICE_ACTIVATED
:
1030 *bl
+= sprintf(b
+ *bl
, "ACTIVATED");
1032 case TRANSPORT_DEVICE_DEACTIVATED
:
1033 *bl
+= sprintf(b
+ *bl
, "DEACTIVATED");
1035 case TRANSPORT_DEVICE_SHUTDOWN
:
1036 *bl
+= sprintf(b
+ *bl
, "SHUTDOWN");
1038 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED
:
1039 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED
:
1040 *bl
+= sprintf(b
+ *bl
, "OFFLINE");
1043 *bl
+= sprintf(b
+ *bl
, "UNKNOWN=%d", dev
->dev_status
);
1047 *bl
+= sprintf(b
+ *bl
, " Execute/Left/Max Queue Depth: %d/%d/%d",
1048 atomic_read(&dev
->execute_tasks
), atomic_read(&dev
->depth_left
),
1050 *bl
+= sprintf(b
+ *bl
, " SectorSize: %u MaxSectors: %u\n",
1051 dev
->se_sub_dev
->se_dev_attrib
.block_size
, dev
->se_sub_dev
->se_dev_attrib
.max_sectors
);
1052 *bl
+= sprintf(b
+ *bl
, " ");
1055 /* transport_release_all_cmds():
1059 static void transport_release_all_cmds(struct se_device
*dev
)
1061 struct se_cmd
*cmd
, *tcmd
;
1062 int bug_out
= 0, t_state
;
1063 unsigned long flags
;
1065 spin_lock_irqsave(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1066 list_for_each_entry_safe(cmd
, tcmd
, &dev
->dev_queue_obj
.qobj_list
,
1068 t_state
= cmd
->t_state
;
1069 list_del_init(&cmd
->se_queue_node
);
1070 spin_unlock_irqrestore(&dev
->dev_queue_obj
.cmd_queue_lock
,
1073 pr_err("Releasing ITT: 0x%08x, i_state: %u,"
1074 " t_state: %u directly\n",
1075 cmd
->se_tfo
->get_task_tag(cmd
),
1076 cmd
->se_tfo
->get_cmd_state(cmd
), t_state
);
1078 transport_release_fe_cmd(cmd
);
1081 spin_lock_irqsave(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1083 spin_unlock_irqrestore(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1090 void transport_dump_vpd_proto_id(
1091 struct t10_vpd
*vpd
,
1092 unsigned char *p_buf
,
1095 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1098 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1099 len
= sprintf(buf
, "T10 VPD Protocol Identifier: ");
1101 switch (vpd
->protocol_identifier
) {
1103 sprintf(buf
+len
, "Fibre Channel\n");
1106 sprintf(buf
+len
, "Parallel SCSI\n");
1109 sprintf(buf
+len
, "SSA\n");
1112 sprintf(buf
+len
, "IEEE 1394\n");
1115 sprintf(buf
+len
, "SCSI Remote Direct Memory Access"
1119 sprintf(buf
+len
, "Internet SCSI (iSCSI)\n");
1122 sprintf(buf
+len
, "SAS Serial SCSI Protocol\n");
1125 sprintf(buf
+len
, "Automation/Drive Interface Transport"
1129 sprintf(buf
+len
, "AT Attachment Interface ATA/ATAPI\n");
1132 sprintf(buf
+len
, "Unknown 0x%02x\n",
1133 vpd
->protocol_identifier
);
1138 strncpy(p_buf
, buf
, p_buf_len
);
1140 pr_debug("%s", buf
);
1144 transport_set_vpd_proto_id(struct t10_vpd
*vpd
, unsigned char *page_83
)
1147 * Check if the Protocol Identifier Valid (PIV) bit is set..
1149 * from spc3r23.pdf section 7.5.1
1151 if (page_83
[1] & 0x80) {
1152 vpd
->protocol_identifier
= (page_83
[0] & 0xf0);
1153 vpd
->protocol_identifier_set
= 1;
1154 transport_dump_vpd_proto_id(vpd
, NULL
, 0);
1157 EXPORT_SYMBOL(transport_set_vpd_proto_id
);
1159 int transport_dump_vpd_assoc(
1160 struct t10_vpd
*vpd
,
1161 unsigned char *p_buf
,
1164 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1168 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1169 len
= sprintf(buf
, "T10 VPD Identifier Association: ");
1171 switch (vpd
->association
) {
1173 sprintf(buf
+len
, "addressed logical unit\n");
1176 sprintf(buf
+len
, "target port\n");
1179 sprintf(buf
+len
, "SCSI target device\n");
1182 sprintf(buf
+len
, "Unknown 0x%02x\n", vpd
->association
);
1188 strncpy(p_buf
, buf
, p_buf_len
);
1190 pr_debug("%s", buf
);
1195 int transport_set_vpd_assoc(struct t10_vpd
*vpd
, unsigned char *page_83
)
1198 * The VPD identification association..
1200 * from spc3r23.pdf Section 7.6.3.1 Table 297
1202 vpd
->association
= (page_83
[1] & 0x30);
1203 return transport_dump_vpd_assoc(vpd
, NULL
, 0);
1205 EXPORT_SYMBOL(transport_set_vpd_assoc
);
1207 int transport_dump_vpd_ident_type(
1208 struct t10_vpd
*vpd
,
1209 unsigned char *p_buf
,
1212 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1216 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1217 len
= sprintf(buf
, "T10 VPD Identifier Type: ");
1219 switch (vpd
->device_identifier_type
) {
1221 sprintf(buf
+len
, "Vendor specific\n");
1224 sprintf(buf
+len
, "T10 Vendor ID based\n");
1227 sprintf(buf
+len
, "EUI-64 based\n");
1230 sprintf(buf
+len
, "NAA\n");
1233 sprintf(buf
+len
, "Relative target port identifier\n");
1236 sprintf(buf
+len
, "SCSI name string\n");
1239 sprintf(buf
+len
, "Unsupported: 0x%02x\n",
1240 vpd
->device_identifier_type
);
1246 if (p_buf_len
< strlen(buf
)+1)
1248 strncpy(p_buf
, buf
, p_buf_len
);
1250 pr_debug("%s", buf
);
1256 int transport_set_vpd_ident_type(struct t10_vpd
*vpd
, unsigned char *page_83
)
1259 * The VPD identifier type..
1261 * from spc3r23.pdf Section 7.6.3.1 Table 298
1263 vpd
->device_identifier_type
= (page_83
[1] & 0x0f);
1264 return transport_dump_vpd_ident_type(vpd
, NULL
, 0);
1266 EXPORT_SYMBOL(transport_set_vpd_ident_type
);
1268 int transport_dump_vpd_ident(
1269 struct t10_vpd
*vpd
,
1270 unsigned char *p_buf
,
1273 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1276 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1278 switch (vpd
->device_identifier_code_set
) {
1279 case 0x01: /* Binary */
1280 sprintf(buf
, "T10 VPD Binary Device Identifier: %s\n",
1281 &vpd
->device_identifier
[0]);
1283 case 0x02: /* ASCII */
1284 sprintf(buf
, "T10 VPD ASCII Device Identifier: %s\n",
1285 &vpd
->device_identifier
[0]);
1287 case 0x03: /* UTF-8 */
1288 sprintf(buf
, "T10 VPD UTF-8 Device Identifier: %s\n",
1289 &vpd
->device_identifier
[0]);
1292 sprintf(buf
, "T10 VPD Device Identifier encoding unsupported:"
1293 " 0x%02x", vpd
->device_identifier_code_set
);
1299 strncpy(p_buf
, buf
, p_buf_len
);
1301 pr_debug("%s", buf
);
1307 transport_set_vpd_ident(struct t10_vpd
*vpd
, unsigned char *page_83
)
1309 static const char hex_str
[] = "0123456789abcdef";
1310 int j
= 0, i
= 4; /* offset to start of the identifer */
1313 * The VPD Code Set (encoding)
1315 * from spc3r23.pdf Section 7.6.3.1 Table 296
1317 vpd
->device_identifier_code_set
= (page_83
[0] & 0x0f);
1318 switch (vpd
->device_identifier_code_set
) {
1319 case 0x01: /* Binary */
1320 vpd
->device_identifier
[j
++] =
1321 hex_str
[vpd
->device_identifier_type
];
1322 while (i
< (4 + page_83
[3])) {
1323 vpd
->device_identifier
[j
++] =
1324 hex_str
[(page_83
[i
] & 0xf0) >> 4];
1325 vpd
->device_identifier
[j
++] =
1326 hex_str
[page_83
[i
] & 0x0f];
1330 case 0x02: /* ASCII */
1331 case 0x03: /* UTF-8 */
1332 while (i
< (4 + page_83
[3]))
1333 vpd
->device_identifier
[j
++] = page_83
[i
++];
1339 return transport_dump_vpd_ident(vpd
, NULL
, 0);
1341 EXPORT_SYMBOL(transport_set_vpd_ident
);
1343 static void core_setup_task_attr_emulation(struct se_device
*dev
)
1346 * If this device is from Target_Core_Mod/pSCSI, disable the
1347 * SAM Task Attribute emulation.
1349 * This is currently not available in upsream Linux/SCSI Target
1350 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1352 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) {
1353 dev
->dev_task_attr_type
= SAM_TASK_ATTR_PASSTHROUGH
;
1357 dev
->dev_task_attr_type
= SAM_TASK_ATTR_EMULATED
;
1358 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1359 " device\n", dev
->transport
->name
,
1360 dev
->transport
->get_device_rev(dev
));
1363 static void scsi_dump_inquiry(struct se_device
*dev
)
1365 struct t10_wwn
*wwn
= &dev
->se_sub_dev
->t10_wwn
;
1368 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1370 pr_debug(" Vendor: ");
1371 for (i
= 0; i
< 8; i
++)
1372 if (wwn
->vendor
[i
] >= 0x20)
1373 pr_debug("%c", wwn
->vendor
[i
]);
1377 pr_debug(" Model: ");
1378 for (i
= 0; i
< 16; i
++)
1379 if (wwn
->model
[i
] >= 0x20)
1380 pr_debug("%c", wwn
->model
[i
]);
1384 pr_debug(" Revision: ");
1385 for (i
= 0; i
< 4; i
++)
1386 if (wwn
->revision
[i
] >= 0x20)
1387 pr_debug("%c", wwn
->revision
[i
]);
1393 device_type
= dev
->transport
->get_device_type(dev
);
1394 pr_debug(" Type: %s ", scsi_device_type(device_type
));
1395 pr_debug(" ANSI SCSI revision: %02x\n",
1396 dev
->transport
->get_device_rev(dev
));
1399 struct se_device
*transport_add_device_to_core_hba(
1401 struct se_subsystem_api
*transport
,
1402 struct se_subsystem_dev
*se_dev
,
1404 void *transport_dev
,
1405 struct se_dev_limits
*dev_limits
,
1406 const char *inquiry_prod
,
1407 const char *inquiry_rev
)
1410 struct se_device
*dev
;
1412 dev
= kzalloc(sizeof(struct se_device
), GFP_KERNEL
);
1414 pr_err("Unable to allocate memory for se_dev_t\n");
1418 transport_init_queue_obj(&dev
->dev_queue_obj
);
1419 dev
->dev_flags
= device_flags
;
1420 dev
->dev_status
|= TRANSPORT_DEVICE_DEACTIVATED
;
1421 dev
->dev_ptr
= transport_dev
;
1423 dev
->se_sub_dev
= se_dev
;
1424 dev
->transport
= transport
;
1425 atomic_set(&dev
->active_cmds
, 0);
1426 INIT_LIST_HEAD(&dev
->dev_list
);
1427 INIT_LIST_HEAD(&dev
->dev_sep_list
);
1428 INIT_LIST_HEAD(&dev
->dev_tmr_list
);
1429 INIT_LIST_HEAD(&dev
->execute_task_list
);
1430 INIT_LIST_HEAD(&dev
->delayed_cmd_list
);
1431 INIT_LIST_HEAD(&dev
->ordered_cmd_list
);
1432 INIT_LIST_HEAD(&dev
->state_task_list
);
1433 INIT_LIST_HEAD(&dev
->qf_cmd_list
);
1434 spin_lock_init(&dev
->execute_task_lock
);
1435 spin_lock_init(&dev
->delayed_cmd_lock
);
1436 spin_lock_init(&dev
->ordered_cmd_lock
);
1437 spin_lock_init(&dev
->state_task_lock
);
1438 spin_lock_init(&dev
->dev_alua_lock
);
1439 spin_lock_init(&dev
->dev_reservation_lock
);
1440 spin_lock_init(&dev
->dev_status_lock
);
1441 spin_lock_init(&dev
->dev_status_thr_lock
);
1442 spin_lock_init(&dev
->se_port_lock
);
1443 spin_lock_init(&dev
->se_tmr_lock
);
1444 spin_lock_init(&dev
->qf_cmd_lock
);
1446 dev
->queue_depth
= dev_limits
->queue_depth
;
1447 atomic_set(&dev
->depth_left
, dev
->queue_depth
);
1448 atomic_set(&dev
->dev_ordered_id
, 0);
1450 se_dev_set_default_attribs(dev
, dev_limits
);
1452 dev
->dev_index
= scsi_get_new_index(SCSI_DEVICE_INDEX
);
1453 dev
->creation_time
= get_jiffies_64();
1454 spin_lock_init(&dev
->stats_lock
);
1456 spin_lock(&hba
->device_lock
);
1457 list_add_tail(&dev
->dev_list
, &hba
->hba_dev_list
);
1459 spin_unlock(&hba
->device_lock
);
1461 * Setup the SAM Task Attribute emulation for struct se_device
1463 core_setup_task_attr_emulation(dev
);
1465 * Force PR and ALUA passthrough emulation with internal object use.
1467 force_pt
= (hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
);
1469 * Setup the Reservations infrastructure for struct se_device
1471 core_setup_reservations(dev
, force_pt
);
1473 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1475 if (core_setup_alua(dev
, force_pt
) < 0)
1479 * Startup the struct se_device processing thread
1481 dev
->process_thread
= kthread_run(transport_processing_thread
, dev
,
1482 "LIO_%s", dev
->transport
->name
);
1483 if (IS_ERR(dev
->process_thread
)) {
1484 pr_err("Unable to create kthread: LIO_%s\n",
1485 dev
->transport
->name
);
1489 * Setup work_queue for QUEUE_FULL
1491 INIT_WORK(&dev
->qf_work_queue
, target_qf_do_work
);
1493 * Preload the initial INQUIRY const values if we are doing
1494 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1495 * passthrough because this is being provided by the backend LLD.
1496 * This is required so that transport_get_inquiry() copies these
1497 * originals once back into DEV_T10_WWN(dev) for the virtual device
1500 if (dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
) {
1501 if (!inquiry_prod
|| !inquiry_rev
) {
1502 pr_err("All non TCM/pSCSI plugins require"
1503 " INQUIRY consts\n");
1507 strncpy(&dev
->se_sub_dev
->t10_wwn
.vendor
[0], "LIO-ORG", 8);
1508 strncpy(&dev
->se_sub_dev
->t10_wwn
.model
[0], inquiry_prod
, 16);
1509 strncpy(&dev
->se_sub_dev
->t10_wwn
.revision
[0], inquiry_rev
, 4);
1511 scsi_dump_inquiry(dev
);
1515 kthread_stop(dev
->process_thread
);
1517 spin_lock(&hba
->device_lock
);
1518 list_del(&dev
->dev_list
);
1520 spin_unlock(&hba
->device_lock
);
1522 se_release_vpd_for_dev(dev
);
1528 EXPORT_SYMBOL(transport_add_device_to_core_hba
);
1530 /* transport_generic_prepare_cdb():
1532 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1533 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1534 * The point of this is since we are mapping iSCSI LUNs to
1535 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1536 * devices and HBAs for a loop.
1538 static inline void transport_generic_prepare_cdb(
1542 case READ_10
: /* SBC - RDProtect */
1543 case READ_12
: /* SBC - RDProtect */
1544 case READ_16
: /* SBC - RDProtect */
1545 case SEND_DIAGNOSTIC
: /* SPC - SELF-TEST Code */
1546 case VERIFY
: /* SBC - VRProtect */
1547 case VERIFY_16
: /* SBC - VRProtect */
1548 case WRITE_VERIFY
: /* SBC - VRProtect */
1549 case WRITE_VERIFY_12
: /* SBC - VRProtect */
1552 cdb
[1] &= 0x1f; /* clear logical unit number */
1557 static struct se_task
*
1558 transport_generic_get_task(struct se_cmd
*cmd
,
1559 enum dma_data_direction data_direction
)
1561 struct se_task
*task
;
1562 struct se_device
*dev
= cmd
->se_dev
;
1564 task
= dev
->transport
->alloc_task(cmd
->t_task_cdb
);
1566 pr_err("Unable to allocate struct se_task\n");
1570 INIT_LIST_HEAD(&task
->t_list
);
1571 INIT_LIST_HEAD(&task
->t_execute_list
);
1572 INIT_LIST_HEAD(&task
->t_state_list
);
1573 init_completion(&task
->task_stop_comp
);
1574 task
->task_se_cmd
= cmd
;
1576 task
->task_data_direction
= data_direction
;
1581 static int transport_generic_cmd_sequencer(struct se_cmd
*, unsigned char *);
1584 * Used by fabric modules containing a local struct se_cmd within their
1585 * fabric dependent per I/O descriptor.
1587 void transport_init_se_cmd(
1589 struct target_core_fabric_ops
*tfo
,
1590 struct se_session
*se_sess
,
1594 unsigned char *sense_buffer
)
1596 INIT_LIST_HEAD(&cmd
->se_lun_node
);
1597 INIT_LIST_HEAD(&cmd
->se_delayed_node
);
1598 INIT_LIST_HEAD(&cmd
->se_ordered_node
);
1599 INIT_LIST_HEAD(&cmd
->se_qf_node
);
1600 INIT_LIST_HEAD(&cmd
->se_queue_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
;
2057 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
2058 * make the call to transport_send_check_condition_and_sense()
2059 * directly. Otherwise expect the fabric to make the call to
2060 * transport_send_check_condition_and_sense() after handling
2061 * possible unsoliticied write data payloads.
2063 if (!sc
&& !cmd
->se_tfo
->new_cmd_map
)
2064 transport_new_cmd_failure(cmd
);
2066 ret
= transport_send_check_condition_and_sense(cmd
,
2067 cmd
->scsi_sense_reason
, 0);
2073 transport_lun_remove_cmd(cmd
);
2074 if (!transport_cmd_check_stop_to_fabric(cmd
))
2079 cmd
->t_state
= TRANSPORT_COMPLETE_OK
;
2080 transport_handle_queue_full(cmd
, cmd
->se_dev
, transport_complete_qf
);
2083 static void transport_direct_request_timeout(struct se_cmd
*cmd
)
2085 unsigned long flags
;
2087 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2088 if (!atomic_read(&cmd
->t_transport_timeout
)) {
2089 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2092 if (atomic_read(&cmd
->t_task_cdbs_timeout_left
)) {
2093 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2097 atomic_sub(atomic_read(&cmd
->t_transport_timeout
),
2099 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2102 static void transport_generic_request_timeout(struct se_cmd
*cmd
)
2104 unsigned long flags
;
2107 * Reset cmd->t_se_count to allow transport_generic_remove()
2108 * to allow last call to free memory resources.
2110 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2111 if (atomic_read(&cmd
->t_transport_timeout
) > 1) {
2112 int tmp
= (atomic_read(&cmd
->t_transport_timeout
) - 1);
2114 atomic_sub(tmp
, &cmd
->t_se_count
);
2116 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2118 transport_generic_remove(cmd
, 0);
2121 static inline u32
transport_lba_21(unsigned char *cdb
)
2123 return ((cdb
[1] & 0x1f) << 16) | (cdb
[2] << 8) | cdb
[3];
2126 static inline u32
transport_lba_32(unsigned char *cdb
)
2128 return (cdb
[2] << 24) | (cdb
[3] << 16) | (cdb
[4] << 8) | cdb
[5];
2131 static inline unsigned long long transport_lba_64(unsigned char *cdb
)
2133 unsigned int __v1
, __v2
;
2135 __v1
= (cdb
[2] << 24) | (cdb
[3] << 16) | (cdb
[4] << 8) | cdb
[5];
2136 __v2
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
2138 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
2142 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2144 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb
)
2146 unsigned int __v1
, __v2
;
2148 __v1
= (cdb
[12] << 24) | (cdb
[13] << 16) | (cdb
[14] << 8) | cdb
[15];
2149 __v2
= (cdb
[16] << 24) | (cdb
[17] << 16) | (cdb
[18] << 8) | cdb
[19];
2151 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
2154 static void transport_set_supported_SAM_opcode(struct se_cmd
*se_cmd
)
2156 unsigned long flags
;
2158 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
2159 se_cmd
->se_cmd_flags
|= SCF_SUPPORTED_SAM_OPCODE
;
2160 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
2164 * Called from interrupt context.
2166 static void transport_task_timeout_handler(unsigned long data
)
2168 struct se_task
*task
= (struct se_task
*)data
;
2169 struct se_cmd
*cmd
= task
->task_se_cmd
;
2170 unsigned long flags
;
2172 pr_debug("transport task timeout fired! task: %p cmd: %p\n", task
, cmd
);
2174 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2175 if (task
->task_flags
& TF_STOP
) {
2176 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2179 task
->task_flags
&= ~TF_RUNNING
;
2182 * Determine if transport_complete_task() has already been called.
2184 if (!atomic_read(&task
->task_active
)) {
2185 pr_debug("transport task: %p cmd: %p timeout task_active"
2186 " == 0\n", task
, cmd
);
2187 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2191 atomic_inc(&cmd
->t_se_count
);
2192 atomic_inc(&cmd
->t_transport_timeout
);
2193 cmd
->t_tasks_failed
= 1;
2195 atomic_set(&task
->task_timeout
, 1);
2196 task
->task_error_status
= PYX_TRANSPORT_TASK_TIMEOUT
;
2197 task
->task_scsi_status
= 1;
2199 if (atomic_read(&task
->task_stop
)) {
2200 pr_debug("transport task: %p cmd: %p timeout task_stop"
2201 " == 1\n", task
, cmd
);
2202 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2203 complete(&task
->task_stop_comp
);
2207 if (!atomic_dec_and_test(&cmd
->t_task_cdbs_left
)) {
2208 pr_debug("transport task: %p cmd: %p timeout non zero"
2209 " t_task_cdbs_left\n", task
, cmd
);
2210 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2213 pr_debug("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2216 cmd
->t_state
= TRANSPORT_COMPLETE_FAILURE
;
2217 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2219 transport_add_cmd_to_queue(cmd
, TRANSPORT_COMPLETE_FAILURE
);
2223 * Called with cmd->t_state_lock held.
2225 static void transport_start_task_timer(struct se_task
*task
)
2227 struct se_device
*dev
= task
->se_dev
;
2230 if (task
->task_flags
& TF_RUNNING
)
2233 * If the task_timeout is disabled, exit now.
2235 timeout
= dev
->se_sub_dev
->se_dev_attrib
.task_timeout
;
2239 init_timer(&task
->task_timer
);
2240 task
->task_timer
.expires
= (get_jiffies_64() + timeout
* HZ
);
2241 task
->task_timer
.data
= (unsigned long) task
;
2242 task
->task_timer
.function
= transport_task_timeout_handler
;
2244 task
->task_flags
|= TF_RUNNING
;
2245 add_timer(&task
->task_timer
);
2247 pr_debug("Starting task timer for cmd: %p task: %p seconds:"
2248 " %d\n", task
->task_se_cmd
, task
, timeout
);
2253 * Called with spin_lock_irq(&cmd->t_state_lock) held.
2255 void __transport_stop_task_timer(struct se_task
*task
, unsigned long *flags
)
2257 struct se_cmd
*cmd
= task
->task_se_cmd
;
2259 if (!task
->task_flags
& TF_RUNNING
)
2262 task
->task_flags
|= TF_STOP
;
2263 spin_unlock_irqrestore(&cmd
->t_state_lock
, *flags
);
2265 del_timer_sync(&task
->task_timer
);
2267 spin_lock_irqsave(&cmd
->t_state_lock
, *flags
);
2268 task
->task_flags
&= ~TF_RUNNING
;
2269 task
->task_flags
&= ~TF_STOP
;
2272 static void transport_stop_all_task_timers(struct se_cmd
*cmd
)
2274 struct se_task
*task
= NULL
, *task_tmp
;
2275 unsigned long flags
;
2277 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2278 list_for_each_entry_safe(task
, task_tmp
,
2279 &cmd
->t_task_list
, t_list
)
2280 __transport_stop_task_timer(task
, &flags
);
2281 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2284 static inline int transport_tcq_window_closed(struct se_device
*dev
)
2286 if (dev
->dev_tcq_window_closed
++ <
2287 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD
) {
2288 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT
);
2290 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG
);
2292 wake_up_interruptible(&dev
->dev_queue_obj
.thread_wq
);
2297 * Called from Fabric Module context from transport_execute_tasks()
2299 * The return of this function determins if the tasks from struct se_cmd
2300 * get added to the execution queue in transport_execute_tasks(),
2301 * or are added to the delayed or ordered lists here.
2303 static inline int transport_execute_task_attr(struct se_cmd
*cmd
)
2305 if (cmd
->se_dev
->dev_task_attr_type
!= SAM_TASK_ATTR_EMULATED
)
2308 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2309 * to allow the passed struct se_cmd list of tasks to the front of the list.
2311 if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
2312 atomic_inc(&cmd
->se_dev
->dev_hoq_count
);
2313 smp_mb__after_atomic_inc();
2314 pr_debug("Added HEAD_OF_QUEUE for CDB:"
2315 " 0x%02x, se_ordered_id: %u\n",
2317 cmd
->se_ordered_id
);
2319 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
2320 spin_lock(&cmd
->se_dev
->ordered_cmd_lock
);
2321 list_add_tail(&cmd
->se_ordered_node
,
2322 &cmd
->se_dev
->ordered_cmd_list
);
2323 spin_unlock(&cmd
->se_dev
->ordered_cmd_lock
);
2325 atomic_inc(&cmd
->se_dev
->dev_ordered_sync
);
2326 smp_mb__after_atomic_inc();
2328 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
2329 " list, se_ordered_id: %u\n",
2331 cmd
->se_ordered_id
);
2333 * Add ORDERED command to tail of execution queue if
2334 * no other older commands exist that need to be
2337 if (!atomic_read(&cmd
->se_dev
->simple_cmds
))
2341 * For SIMPLE and UNTAGGED Task Attribute commands
2343 atomic_inc(&cmd
->se_dev
->simple_cmds
);
2344 smp_mb__after_atomic_inc();
2347 * Otherwise if one or more outstanding ORDERED task attribute exist,
2348 * add the dormant task(s) built for the passed struct se_cmd to the
2349 * execution queue and become in Active state for this struct se_device.
2351 if (atomic_read(&cmd
->se_dev
->dev_ordered_sync
) != 0) {
2353 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2354 * will be drained upon completion of HEAD_OF_QUEUE task.
2356 spin_lock(&cmd
->se_dev
->delayed_cmd_lock
);
2357 cmd
->se_cmd_flags
|= SCF_DELAYED_CMD_FROM_SAM_ATTR
;
2358 list_add_tail(&cmd
->se_delayed_node
,
2359 &cmd
->se_dev
->delayed_cmd_list
);
2360 spin_unlock(&cmd
->se_dev
->delayed_cmd_lock
);
2362 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2363 " delayed CMD list, se_ordered_id: %u\n",
2364 cmd
->t_task_cdb
[0], cmd
->sam_task_attr
,
2365 cmd
->se_ordered_id
);
2367 * Return zero to let transport_execute_tasks() know
2368 * not to add the delayed tasks to the execution list.
2373 * Otherwise, no ORDERED task attributes exist..
2379 * Called from fabric module context in transport_generic_new_cmd() and
2380 * transport_generic_process_write()
2382 static int transport_execute_tasks(struct se_cmd
*cmd
)
2386 if (se_dev_check_online(cmd
->se_orig_obj_ptr
) != 0) {
2387 cmd
->transport_error_status
= PYX_TRANSPORT_LU_COMM_FAILURE
;
2388 transport_generic_request_failure(cmd
, NULL
, 0, 1);
2393 * Call transport_cmd_check_stop() to see if a fabric exception
2394 * has occurred that prevents execution.
2396 if (!transport_cmd_check_stop(cmd
, 0, TRANSPORT_PROCESSING
)) {
2398 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2399 * attribute for the tasks of the received struct se_cmd CDB
2401 add_tasks
= transport_execute_task_attr(cmd
);
2405 * This calls transport_add_tasks_from_cmd() to handle
2406 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2407 * (if enabled) in __transport_add_task_to_execute_queue() and
2408 * transport_add_task_check_sam_attr().
2410 transport_add_tasks_from_cmd(cmd
);
2413 * Kick the execution queue for the cmd associated struct se_device
2417 __transport_execute_tasks(cmd
->se_dev
);
2422 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2423 * from struct se_device->execute_task_list and
2425 * Called from transport_processing_thread()
2427 static int __transport_execute_tasks(struct se_device
*dev
)
2430 struct se_cmd
*cmd
= NULL
;
2431 struct se_task
*task
= NULL
;
2432 unsigned long flags
;
2435 * Check if there is enough room in the device and HBA queue to send
2436 * struct se_tasks to the selected transport.
2439 if (!atomic_read(&dev
->depth_left
))
2440 return transport_tcq_window_closed(dev
);
2442 dev
->dev_tcq_window_closed
= 0;
2444 spin_lock_irq(&dev
->execute_task_lock
);
2445 if (list_empty(&dev
->execute_task_list
)) {
2446 spin_unlock_irq(&dev
->execute_task_lock
);
2449 task
= list_first_entry(&dev
->execute_task_list
,
2450 struct se_task
, t_execute_list
);
2451 list_del(&task
->t_execute_list
);
2452 atomic_set(&task
->task_execute_queue
, 0);
2453 atomic_dec(&dev
->execute_tasks
);
2454 spin_unlock_irq(&dev
->execute_task_lock
);
2456 atomic_dec(&dev
->depth_left
);
2458 cmd
= task
->task_se_cmd
;
2460 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2461 atomic_set(&task
->task_active
, 1);
2462 atomic_set(&task
->task_sent
, 1);
2463 atomic_inc(&cmd
->t_task_cdbs_sent
);
2465 if (atomic_read(&cmd
->t_task_cdbs_sent
) ==
2466 cmd
->t_task_list_num
)
2467 atomic_set(&cmd
->transport_sent
, 1);
2469 transport_start_task_timer(task
);
2470 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2472 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2473 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2474 * struct se_subsystem_api->do_task() caller below.
2476 if (cmd
->transport_emulate_cdb
) {
2477 error
= cmd
->transport_emulate_cdb(cmd
);
2479 cmd
->transport_error_status
= error
;
2480 atomic_set(&task
->task_active
, 0);
2481 atomic_set(&cmd
->transport_sent
, 0);
2482 transport_stop_tasks_for_cmd(cmd
);
2483 transport_generic_request_failure(cmd
, dev
, 0, 1);
2487 * Handle the successful completion for transport_emulate_cdb()
2488 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2489 * Otherwise the caller is expected to complete the task with
2492 if (!(cmd
->se_cmd_flags
& SCF_EMULATE_CDB_ASYNC
)) {
2493 cmd
->scsi_status
= SAM_STAT_GOOD
;
2494 task
->task_scsi_status
= GOOD
;
2495 transport_complete_task(task
, 1);
2499 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2500 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2501 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2502 * LUN emulation code.
2504 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2505 * call ->do_task() directly and let the underlying TCM subsystem plugin
2506 * code handle the CDB emulation.
2508 if ((dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
) &&
2509 (!(task
->task_se_cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
)))
2510 error
= transport_emulate_control_cdb(task
);
2512 error
= dev
->transport
->do_task(task
);
2515 cmd
->transport_error_status
= error
;
2516 atomic_set(&task
->task_active
, 0);
2517 atomic_set(&cmd
->transport_sent
, 0);
2518 transport_stop_tasks_for_cmd(cmd
);
2519 transport_generic_request_failure(cmd
, dev
, 0, 1);
2528 void transport_new_cmd_failure(struct se_cmd
*se_cmd
)
2530 unsigned long flags
;
2532 * Any unsolicited data will get dumped for failed command inside of
2535 spin_lock_irqsave(&se_cmd
->t_state_lock
, flags
);
2536 se_cmd
->se_cmd_flags
|= SCF_SE_CMD_FAILED
;
2537 se_cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2538 spin_unlock_irqrestore(&se_cmd
->t_state_lock
, flags
);
2541 static void transport_nop_wait_for_tasks(struct se_cmd
*, int, int);
2543 static inline u32
transport_get_sectors_6(
2548 struct se_device
*dev
= cmd
->se_dev
;
2551 * Assume TYPE_DISK for non struct se_device objects.
2552 * Use 8-bit sector value.
2558 * Use 24-bit allocation length for TYPE_TAPE.
2560 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
2561 return (u32
)(cdb
[2] << 16) + (cdb
[3] << 8) + cdb
[4];
2564 * Everything else assume TYPE_DISK Sector CDB location.
2565 * Use 8-bit sector value. SBC-3 says:
2567 * A TRANSFER LENGTH field set to zero specifies that 256
2568 * logical blocks shall be written. Any other value
2569 * specifies the number of logical blocks that shall be
2573 return cdb
[4] ? : 256;
2576 static inline u32
transport_get_sectors_10(
2581 struct se_device
*dev
= cmd
->se_dev
;
2584 * Assume TYPE_DISK for non struct se_device objects.
2585 * Use 16-bit sector value.
2591 * XXX_10 is not defined in SSC, throw an exception
2593 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2599 * Everything else assume TYPE_DISK Sector CDB location.
2600 * Use 16-bit sector value.
2603 return (u32
)(cdb
[7] << 8) + cdb
[8];
2606 static inline u32
transport_get_sectors_12(
2611 struct se_device
*dev
= cmd
->se_dev
;
2614 * Assume TYPE_DISK for non struct se_device objects.
2615 * Use 32-bit sector value.
2621 * XXX_12 is not defined in SSC, throw an exception
2623 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2629 * Everything else assume TYPE_DISK Sector CDB location.
2630 * Use 32-bit sector value.
2633 return (u32
)(cdb
[6] << 24) + (cdb
[7] << 16) + (cdb
[8] << 8) + cdb
[9];
2636 static inline u32
transport_get_sectors_16(
2641 struct se_device
*dev
= cmd
->se_dev
;
2644 * Assume TYPE_DISK for non struct se_device objects.
2645 * Use 32-bit sector value.
2651 * Use 24-bit allocation length for TYPE_TAPE.
2653 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
2654 return (u32
)(cdb
[12] << 16) + (cdb
[13] << 8) + cdb
[14];
2657 return (u32
)(cdb
[10] << 24) + (cdb
[11] << 16) +
2658 (cdb
[12] << 8) + cdb
[13];
2662 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2664 static inline u32
transport_get_sectors_32(
2670 * Assume TYPE_DISK for non struct se_device objects.
2671 * Use 32-bit sector value.
2673 return (u32
)(cdb
[28] << 24) + (cdb
[29] << 16) +
2674 (cdb
[30] << 8) + cdb
[31];
2678 static inline u32
transport_get_size(
2683 struct se_device
*dev
= cmd
->se_dev
;
2685 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2686 if (cdb
[1] & 1) { /* sectors */
2687 return dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
;
2692 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2693 " %s object\n", dev
->se_sub_dev
->se_dev_attrib
.block_size
, sectors
,
2694 dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
,
2695 dev
->transport
->name
);
2697 return dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
;
2700 static void transport_xor_callback(struct se_cmd
*cmd
)
2702 unsigned char *buf
, *addr
;
2703 struct scatterlist
*sg
;
2704 unsigned int offset
;
2708 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2710 * 1) read the specified logical block(s);
2711 * 2) transfer logical blocks from the data-out buffer;
2712 * 3) XOR the logical blocks transferred from the data-out buffer with
2713 * the logical blocks read, storing the resulting XOR data in a buffer;
2714 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2715 * blocks transferred from the data-out buffer; and
2716 * 5) transfer the resulting XOR data to the data-in buffer.
2718 buf
= kmalloc(cmd
->data_length
, GFP_KERNEL
);
2720 pr_err("Unable to allocate xor_callback buf\n");
2724 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2725 * into the locally allocated *buf
2727 sg_copy_to_buffer(cmd
->t_data_sg
,
2733 * Now perform the XOR against the BIDI read memory located at
2734 * cmd->t_mem_bidi_list
2738 for_each_sg(cmd
->t_bidi_data_sg
, sg
, cmd
->t_bidi_data_nents
, count
) {
2739 addr
= kmap_atomic(sg_page(sg
), KM_USER0
);
2743 for (i
= 0; i
< sg
->length
; i
++)
2744 *(addr
+ sg
->offset
+ i
) ^= *(buf
+ offset
+ i
);
2746 offset
+= sg
->length
;
2747 kunmap_atomic(addr
, KM_USER0
);
2755 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2757 static int transport_get_sense_data(struct se_cmd
*cmd
)
2759 unsigned char *buffer
= cmd
->sense_buffer
, *sense_buffer
= NULL
;
2760 struct se_device
*dev
;
2761 struct se_task
*task
= NULL
, *task_tmp
;
2762 unsigned long flags
;
2765 WARN_ON(!cmd
->se_lun
);
2767 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2768 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
2769 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2773 list_for_each_entry_safe(task
, task_tmp
,
2774 &cmd
->t_task_list
, t_list
) {
2776 if (!task
->task_sense
)
2783 if (!dev
->transport
->get_sense_buffer
) {
2784 pr_err("dev->transport->get_sense_buffer"
2789 sense_buffer
= dev
->transport
->get_sense_buffer(task
);
2790 if (!sense_buffer
) {
2791 pr_err("ITT[0x%08x]_TASK[%d]: Unable to locate"
2792 " sense buffer for task with sense\n",
2793 cmd
->se_tfo
->get_task_tag(cmd
), task
->task_no
);
2796 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2798 offset
= cmd
->se_tfo
->set_fabric_sense_len(cmd
,
2799 TRANSPORT_SENSE_BUFFER
);
2801 memcpy(&buffer
[offset
], sense_buffer
,
2802 TRANSPORT_SENSE_BUFFER
);
2803 cmd
->scsi_status
= task
->task_scsi_status
;
2804 /* Automatically padded */
2805 cmd
->scsi_sense_length
=
2806 (TRANSPORT_SENSE_BUFFER
+ offset
);
2808 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2810 dev
->se_hba
->hba_id
, dev
->transport
->name
,
2814 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2820 transport_handle_reservation_conflict(struct se_cmd
*cmd
)
2822 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
2823 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2824 cmd
->se_cmd_flags
|= SCF_SCSI_RESERVATION_CONFLICT
;
2825 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
2827 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2828 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2831 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2834 cmd
->se_dev
->se_sub_dev
->se_dev_attrib
.emulate_ua_intlck_ctrl
== 2)
2835 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
2836 cmd
->orig_fe_lun
, 0x2C,
2837 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
2841 static inline long long transport_dev_end_lba(struct se_device
*dev
)
2843 return dev
->transport
->get_blocks(dev
) + 1;
2846 static int transport_cmd_get_valid_sectors(struct se_cmd
*cmd
)
2848 struct se_device
*dev
= cmd
->se_dev
;
2851 if (dev
->transport
->get_device_type(dev
) != TYPE_DISK
)
2854 sectors
= (cmd
->data_length
/ dev
->se_sub_dev
->se_dev_attrib
.block_size
);
2856 if ((cmd
->t_task_lba
+ sectors
) > transport_dev_end_lba(dev
)) {
2857 pr_err("LBA: %llu Sectors: %u exceeds"
2858 " transport_dev_end_lba(): %llu\n",
2859 cmd
->t_task_lba
, sectors
,
2860 transport_dev_end_lba(dev
));
2867 static int target_check_write_same_discard(unsigned char *flags
, struct se_device
*dev
)
2870 * Determine if the received WRITE_SAME is used to for direct
2871 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2872 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2873 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2875 int passthrough
= (dev
->transport
->transport_type
==
2876 TRANSPORT_PLUGIN_PHBA_PDEV
);
2879 if ((flags
[0] & 0x04) || (flags
[0] & 0x02)) {
2880 pr_err("WRITE_SAME PBDATA and LBDATA"
2881 " bits not supported for Block Discard"
2886 * Currently for the emulated case we only accept
2887 * tpws with the UNMAP=1 bit set.
2889 if (!(flags
[0] & 0x08)) {
2890 pr_err("WRITE_SAME w/o UNMAP bit not"
2891 " supported for Block Discard Emulation\n");
2899 /* transport_generic_cmd_sequencer():
2901 * Generic Command Sequencer that should work for most DAS transport
2904 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2907 * FIXME: Need to support other SCSI OPCODES where as well.
2909 static int transport_generic_cmd_sequencer(
2913 struct se_device
*dev
= cmd
->se_dev
;
2914 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
2915 int ret
= 0, sector_ret
= 0, passthrough
;
2916 u32 sectors
= 0, size
= 0, pr_reg_type
= 0;
2920 * Check for an existing UNIT ATTENTION condition
2922 if (core_scsi3_ua_check(cmd
, cdb
) < 0) {
2923 cmd
->transport_wait_for_tasks
=
2924 &transport_nop_wait_for_tasks
;
2925 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2926 cmd
->scsi_sense_reason
= TCM_CHECK_CONDITION_UNIT_ATTENTION
;
2930 * Check status of Asymmetric Logical Unit Assignment port
2932 ret
= su_dev
->t10_alua
.alua_state_check(cmd
, cdb
, &alua_ascq
);
2934 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
2936 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2937 * The ALUA additional sense code qualifier (ASCQ) is determined
2938 * by the ALUA primary or secondary access state..
2942 pr_debug("[%s]: ALUA TG Port not available,"
2943 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2944 cmd
->se_tfo
->get_fabric_name(), alua_ascq
);
2946 transport_set_sense_codes(cmd
, 0x04, alua_ascq
);
2947 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2948 cmd
->scsi_sense_reason
= TCM_CHECK_CONDITION_NOT_READY
;
2951 goto out_invalid_cdb_field
;
2954 * Check status for SPC-3 Persistent Reservations
2956 if (su_dev
->t10_pr
.pr_ops
.t10_reservation_check(cmd
, &pr_reg_type
) != 0) {
2957 if (su_dev
->t10_pr
.pr_ops
.t10_seq_non_holder(
2958 cmd
, cdb
, pr_reg_type
) != 0)
2959 return transport_handle_reservation_conflict(cmd
);
2961 * This means the CDB is allowed for the SCSI Initiator port
2962 * when said port is *NOT* holding the legacy SPC-2 or
2963 * SPC-3 Persistent Reservation.
2969 sectors
= transport_get_sectors_6(cdb
, cmd
, §or_ret
);
2971 goto out_unsupported_cdb
;
2972 size
= transport_get_size(sectors
, cdb
, cmd
);
2973 cmd
->transport_split_cdb
= &split_cdb_XX_6
;
2974 cmd
->t_task_lba
= transport_lba_21(cdb
);
2975 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2978 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
2980 goto out_unsupported_cdb
;
2981 size
= transport_get_size(sectors
, cdb
, cmd
);
2982 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
2983 cmd
->t_task_lba
= transport_lba_32(cdb
);
2984 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2987 sectors
= transport_get_sectors_12(cdb
, cmd
, §or_ret
);
2989 goto out_unsupported_cdb
;
2990 size
= transport_get_size(sectors
, cdb
, cmd
);
2991 cmd
->transport_split_cdb
= &split_cdb_XX_12
;
2992 cmd
->t_task_lba
= transport_lba_32(cdb
);
2993 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
2996 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
2998 goto out_unsupported_cdb
;
2999 size
= transport_get_size(sectors
, cdb
, cmd
);
3000 cmd
->transport_split_cdb
= &split_cdb_XX_16
;
3001 cmd
->t_task_lba
= transport_lba_64(cdb
);
3002 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3005 sectors
= transport_get_sectors_6(cdb
, cmd
, §or_ret
);
3007 goto out_unsupported_cdb
;
3008 size
= transport_get_size(sectors
, cdb
, cmd
);
3009 cmd
->transport_split_cdb
= &split_cdb_XX_6
;
3010 cmd
->t_task_lba
= transport_lba_21(cdb
);
3011 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3014 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3016 goto out_unsupported_cdb
;
3017 size
= transport_get_size(sectors
, cdb
, cmd
);
3018 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
3019 cmd
->t_task_lba
= transport_lba_32(cdb
);
3020 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
3021 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3024 sectors
= transport_get_sectors_12(cdb
, cmd
, §or_ret
);
3026 goto out_unsupported_cdb
;
3027 size
= transport_get_size(sectors
, cdb
, cmd
);
3028 cmd
->transport_split_cdb
= &split_cdb_XX_12
;
3029 cmd
->t_task_lba
= transport_lba_32(cdb
);
3030 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
3031 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3034 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3036 goto out_unsupported_cdb
;
3037 size
= transport_get_size(sectors
, cdb
, cmd
);
3038 cmd
->transport_split_cdb
= &split_cdb_XX_16
;
3039 cmd
->t_task_lba
= transport_lba_64(cdb
);
3040 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
3041 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3043 case XDWRITEREAD_10
:
3044 if ((cmd
->data_direction
!= DMA_TO_DEVICE
) ||
3045 !(cmd
->t_tasks_bidi
))
3046 goto out_invalid_cdb_field
;
3047 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3049 goto out_unsupported_cdb
;
3050 size
= transport_get_size(sectors
, cdb
, cmd
);
3051 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
3052 cmd
->t_task_lba
= transport_lba_32(cdb
);
3053 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3054 passthrough
= (dev
->transport
->transport_type
==
3055 TRANSPORT_PLUGIN_PHBA_PDEV
);
3057 * Skip the remaining assignments for TCM/PSCSI passthrough
3062 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3064 cmd
->transport_complete_callback
= &transport_xor_callback
;
3065 cmd
->t_tasks_fua
= (cdb
[1] & 0x8);
3067 case VARIABLE_LENGTH_CMD
:
3068 service_action
= get_unaligned_be16(&cdb
[8]);
3070 * Determine if this is TCM/PSCSI device and we should disable
3071 * internal emulation for this CDB.
3073 passthrough
= (dev
->transport
->transport_type
==
3074 TRANSPORT_PLUGIN_PHBA_PDEV
);
3076 switch (service_action
) {
3077 case XDWRITEREAD_32
:
3078 sectors
= transport_get_sectors_32(cdb
, cmd
, §or_ret
);
3080 goto out_unsupported_cdb
;
3081 size
= transport_get_size(sectors
, cdb
, cmd
);
3083 * Use WRITE_32 and READ_32 opcodes for the emulated
3084 * XDWRITE_READ_32 logic.
3086 cmd
->transport_split_cdb
= &split_cdb_XX_32
;
3087 cmd
->t_task_lba
= transport_lba_64_ext(cdb
);
3088 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3091 * Skip the remaining assignments for TCM/PSCSI passthrough
3097 * Setup BIDI XOR callback to be run during
3098 * transport_generic_complete_ok()
3100 cmd
->transport_complete_callback
= &transport_xor_callback
;
3101 cmd
->t_tasks_fua
= (cdb
[10] & 0x8);
3104 sectors
= transport_get_sectors_32(cdb
, cmd
, §or_ret
);
3106 goto out_unsupported_cdb
;
3109 size
= transport_get_size(1, cdb
, cmd
);
3111 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
3113 goto out_invalid_cdb_field
;
3116 cmd
->t_task_lba
= get_unaligned_be64(&cdb
[12]);
3117 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3119 if (target_check_write_same_discard(&cdb
[10], dev
) < 0)
3120 goto out_invalid_cdb_field
;
3124 pr_err("VARIABLE_LENGTH_CMD service action"
3125 " 0x%04x not supported\n", service_action
);
3126 goto out_unsupported_cdb
;
3129 case MAINTENANCE_IN
:
3130 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
3131 /* MAINTENANCE_IN from SCC-2 */
3133 * Check for emulated MI_REPORT_TARGET_PGS.
3135 if (cdb
[1] == MI_REPORT_TARGET_PGS
) {
3136 cmd
->transport_emulate_cdb
=
3137 (su_dev
->t10_alua
.alua_type
==
3138 SPC3_ALUA_EMULATED
) ?
3139 core_emulate_report_target_port_groups
:
3142 size
= (cdb
[6] << 24) | (cdb
[7] << 16) |
3143 (cdb
[8] << 8) | cdb
[9];
3145 /* GPCMD_SEND_KEY from multi media commands */
3146 size
= (cdb
[8] << 8) + cdb
[9];
3148 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3152 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3154 case MODE_SELECT_10
:
3155 size
= (cdb
[7] << 8) + cdb
[8];
3156 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3160 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3163 case GPCMD_READ_BUFFER_CAPACITY
:
3164 case GPCMD_SEND_OPC
:
3167 size
= (cdb
[7] << 8) + cdb
[8];
3168 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3170 case READ_BLOCK_LIMITS
:
3171 size
= READ_BLOCK_LEN
;
3172 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3174 case GPCMD_GET_CONFIGURATION
:
3175 case GPCMD_READ_FORMAT_CAPACITIES
:
3176 case GPCMD_READ_DISC_INFO
:
3177 case GPCMD_READ_TRACK_RZONE_INFO
:
3178 size
= (cdb
[7] << 8) + cdb
[8];
3179 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3181 case PERSISTENT_RESERVE_IN
:
3182 case PERSISTENT_RESERVE_OUT
:
3183 cmd
->transport_emulate_cdb
=
3184 (su_dev
->t10_pr
.res_type
==
3185 SPC3_PERSISTENT_RESERVATIONS
) ?
3186 core_scsi3_emulate_pr
: NULL
;
3187 size
= (cdb
[7] << 8) + cdb
[8];
3188 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3190 case GPCMD_MECHANISM_STATUS
:
3191 case GPCMD_READ_DVD_STRUCTURE
:
3192 size
= (cdb
[8] << 8) + cdb
[9];
3193 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3196 size
= READ_POSITION_LEN
;
3197 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3199 case MAINTENANCE_OUT
:
3200 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
3201 /* MAINTENANCE_OUT from SCC-2
3203 * Check for emulated MO_SET_TARGET_PGS.
3205 if (cdb
[1] == MO_SET_TARGET_PGS
) {
3206 cmd
->transport_emulate_cdb
=
3207 (su_dev
->t10_alua
.alua_type
==
3208 SPC3_ALUA_EMULATED
) ?
3209 core_emulate_set_target_port_groups
:
3213 size
= (cdb
[6] << 24) | (cdb
[7] << 16) |
3214 (cdb
[8] << 8) | cdb
[9];
3216 /* GPCMD_REPORT_KEY from multi media commands */
3217 size
= (cdb
[8] << 8) + cdb
[9];
3219 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3222 size
= (cdb
[3] << 8) + cdb
[4];
3224 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3225 * See spc4r17 section 5.3
3227 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3228 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
3229 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3232 size
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3233 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3236 size
= READ_CAP_LEN
;
3237 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3239 case READ_MEDIA_SERIAL_NUMBER
:
3240 case SECURITY_PROTOCOL_IN
:
3241 case SECURITY_PROTOCOL_OUT
:
3242 size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
3243 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3245 case SERVICE_ACTION_IN
:
3246 case ACCESS_CONTROL_IN
:
3247 case ACCESS_CONTROL_OUT
:
3249 case READ_ATTRIBUTE
:
3250 case RECEIVE_COPY_RESULTS
:
3251 case WRITE_ATTRIBUTE
:
3252 size
= (cdb
[10] << 24) | (cdb
[11] << 16) |
3253 (cdb
[12] << 8) | cdb
[13];
3254 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3256 case RECEIVE_DIAGNOSTIC
:
3257 case SEND_DIAGNOSTIC
:
3258 size
= (cdb
[3] << 8) | cdb
[4];
3259 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3261 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3264 sectors
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3265 size
= (2336 * sectors
);
3266 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3271 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3275 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3277 case READ_ELEMENT_STATUS
:
3278 size
= 65536 * cdb
[7] + 256 * cdb
[8] + cdb
[9];
3279 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3282 size
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3283 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3288 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3289 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3291 if (cdb
[0] == RESERVE_10
)
3292 size
= (cdb
[7] << 8) | cdb
[8];
3294 size
= cmd
->data_length
;
3297 * Setup the legacy emulated handler for SPC-2 and
3298 * >= SPC-3 compatible reservation handling (CRH=1)
3299 * Otherwise, we assume the underlying SCSI logic is
3300 * is running in SPC_PASSTHROUGH, and wants reservations
3301 * emulation disabled.
3303 cmd
->transport_emulate_cdb
=
3304 (su_dev
->t10_pr
.res_type
!=
3306 core_scsi2_emulate_crh
: NULL
;
3307 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3312 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3313 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3315 if (cdb
[0] == RELEASE_10
)
3316 size
= (cdb
[7] << 8) | cdb
[8];
3318 size
= cmd
->data_length
;
3320 cmd
->transport_emulate_cdb
=
3321 (su_dev
->t10_pr
.res_type
!=
3323 core_scsi2_emulate_crh
: NULL
;
3324 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3326 case SYNCHRONIZE_CACHE
:
3327 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3329 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3331 if (cdb
[0] == SYNCHRONIZE_CACHE
) {
3332 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3333 cmd
->t_task_lba
= transport_lba_32(cdb
);
3335 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3336 cmd
->t_task_lba
= transport_lba_64(cdb
);
3339 goto out_unsupported_cdb
;
3341 size
= transport_get_size(sectors
, cdb
, cmd
);
3342 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3345 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3347 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
3350 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3351 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3353 cmd
->se_cmd_flags
|= SCF_EMULATE_CDB_ASYNC
;
3355 * Check to ensure that LBA + Range does not exceed past end of
3356 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
3358 if ((cmd
->t_task_lba
!= 0) || (sectors
!= 0)) {
3359 if (transport_cmd_get_valid_sectors(cmd
) < 0)
3360 goto out_invalid_cdb_field
;
3364 size
= get_unaligned_be16(&cdb
[7]);
3365 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3368 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3370 goto out_unsupported_cdb
;
3373 size
= transport_get_size(1, cdb
, cmd
);
3375 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3376 goto out_invalid_cdb_field
;
3379 cmd
->t_task_lba
= get_unaligned_be64(&cdb
[2]);
3380 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3382 if (target_check_write_same_discard(&cdb
[1], dev
) < 0)
3383 goto out_invalid_cdb_field
;
3386 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3388 goto out_unsupported_cdb
;
3391 size
= transport_get_size(1, cdb
, cmd
);
3393 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3394 goto out_invalid_cdb_field
;
3397 cmd
->t_task_lba
= get_unaligned_be32(&cdb
[2]);
3398 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3400 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3401 * of byte 1 bit 3 UNMAP instead of original reserved field
3403 if (target_check_write_same_discard(&cdb
[1], dev
) < 0)
3404 goto out_invalid_cdb_field
;
3406 case ALLOW_MEDIUM_REMOVAL
:
3407 case GPCMD_CLOSE_TRACK
:
3409 case INITIALIZE_ELEMENT_STATUS
:
3410 case GPCMD_LOAD_UNLOAD
:
3413 case GPCMD_SET_SPEED
:
3416 case TEST_UNIT_READY
:
3418 case WRITE_FILEMARKS
:
3420 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3423 cmd
->transport_emulate_cdb
=
3424 transport_core_report_lun_response
;
3425 size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
3427 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3428 * See spc4r17 section 5.3
3430 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3431 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
3432 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3435 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3436 " 0x%02x, sending CHECK_CONDITION.\n",
3437 cmd
->se_tfo
->get_fabric_name(), cdb
[0]);
3438 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
3439 goto out_unsupported_cdb
;
3442 if (size
!= cmd
->data_length
) {
3443 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3444 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3445 " 0x%02x\n", cmd
->se_tfo
->get_fabric_name(),
3446 cmd
->data_length
, size
, cdb
[0]);
3448 cmd
->cmd_spdtl
= size
;
3450 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
3451 pr_err("Rejecting underflow/overflow"
3453 goto out_invalid_cdb_field
;
3456 * Reject READ_* or WRITE_* with overflow/underflow for
3457 * type SCF_SCSI_DATA_SG_IO_CDB.
3459 if (!ret
&& (dev
->se_sub_dev
->se_dev_attrib
.block_size
!= 512)) {
3460 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3461 " CDB on non 512-byte sector setup subsystem"
3462 " plugin: %s\n", dev
->transport
->name
);
3463 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3464 goto out_invalid_cdb_field
;
3467 if (size
> cmd
->data_length
) {
3468 cmd
->se_cmd_flags
|= SCF_OVERFLOW_BIT
;
3469 cmd
->residual_count
= (size
- cmd
->data_length
);
3471 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
3472 cmd
->residual_count
= (cmd
->data_length
- size
);
3474 cmd
->data_length
= size
;
3477 /* Let's limit control cdbs to a page, for simplicity's sake. */
3478 if ((cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
) &&
3480 goto out_invalid_cdb_field
;
3482 transport_set_supported_SAM_opcode(cmd
);
3485 out_unsupported_cdb
:
3486 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3487 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
3489 out_invalid_cdb_field
:
3490 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3491 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3496 * Called from transport_generic_complete_ok() and
3497 * transport_generic_request_failure() to determine which dormant/delayed
3498 * and ordered cmds need to have their tasks added to the execution queue.
3500 static void transport_complete_task_attr(struct se_cmd
*cmd
)
3502 struct se_device
*dev
= cmd
->se_dev
;
3503 struct se_cmd
*cmd_p
, *cmd_tmp
;
3504 int new_active_tasks
= 0;
3506 if (cmd
->sam_task_attr
== MSG_SIMPLE_TAG
) {
3507 atomic_dec(&dev
->simple_cmds
);
3508 smp_mb__after_atomic_dec();
3509 dev
->dev_cur_ordered_id
++;
3510 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3511 " SIMPLE: %u\n", dev
->dev_cur_ordered_id
,
3512 cmd
->se_ordered_id
);
3513 } else if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
3514 atomic_dec(&dev
->dev_hoq_count
);
3515 smp_mb__after_atomic_dec();
3516 dev
->dev_cur_ordered_id
++;
3517 pr_debug("Incremented dev_cur_ordered_id: %u for"
3518 " HEAD_OF_QUEUE: %u\n", dev
->dev_cur_ordered_id
,
3519 cmd
->se_ordered_id
);
3520 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
3521 spin_lock(&dev
->ordered_cmd_lock
);
3522 list_del(&cmd
->se_ordered_node
);
3523 atomic_dec(&dev
->dev_ordered_sync
);
3524 smp_mb__after_atomic_dec();
3525 spin_unlock(&dev
->ordered_cmd_lock
);
3527 dev
->dev_cur_ordered_id
++;
3528 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3529 " %u\n", dev
->dev_cur_ordered_id
, cmd
->se_ordered_id
);
3532 * Process all commands up to the last received
3533 * ORDERED task attribute which requires another blocking
3536 spin_lock(&dev
->delayed_cmd_lock
);
3537 list_for_each_entry_safe(cmd_p
, cmd_tmp
,
3538 &dev
->delayed_cmd_list
, se_delayed_node
) {
3540 list_del(&cmd_p
->se_delayed_node
);
3541 spin_unlock(&dev
->delayed_cmd_lock
);
3543 pr_debug("Calling add_tasks() for"
3544 " cmd_p: 0x%02x Task Attr: 0x%02x"
3545 " Dormant -> Active, se_ordered_id: %u\n",
3546 cmd_p
->t_task_cdb
[0],
3547 cmd_p
->sam_task_attr
, cmd_p
->se_ordered_id
);
3549 transport_add_tasks_from_cmd(cmd_p
);
3552 spin_lock(&dev
->delayed_cmd_lock
);
3553 if (cmd_p
->sam_task_attr
== MSG_ORDERED_TAG
)
3556 spin_unlock(&dev
->delayed_cmd_lock
);
3558 * If new tasks have become active, wake up the transport thread
3559 * to do the processing of the Active tasks.
3561 if (new_active_tasks
!= 0)
3562 wake_up_interruptible(&dev
->dev_queue_obj
.thread_wq
);
3565 static int transport_complete_qf(struct se_cmd
*cmd
)
3569 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
)
3570 return cmd
->se_tfo
->queue_status(cmd
);
3572 switch (cmd
->data_direction
) {
3573 case DMA_FROM_DEVICE
:
3574 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3577 if (cmd
->t_bidi_data_sg
) {
3578 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3582 /* Fall through for DMA_TO_DEVICE */
3584 ret
= cmd
->se_tfo
->queue_status(cmd
);
3593 static void transport_handle_queue_full(
3595 struct se_device
*dev
,
3596 int (*qf_callback
)(struct se_cmd
*))
3598 spin_lock_irq(&dev
->qf_cmd_lock
);
3599 cmd
->se_cmd_flags
|= SCF_EMULATE_QUEUE_FULL
;
3600 cmd
->transport_qf_callback
= qf_callback
;
3601 list_add_tail(&cmd
->se_qf_node
, &cmd
->se_dev
->qf_cmd_list
);
3602 atomic_inc(&dev
->dev_qf_count
);
3603 smp_mb__after_atomic_inc();
3604 spin_unlock_irq(&cmd
->se_dev
->qf_cmd_lock
);
3606 schedule_work(&cmd
->se_dev
->qf_work_queue
);
3609 static void transport_generic_complete_ok(struct se_cmd
*cmd
)
3611 int reason
= 0, ret
;
3613 * Check if we need to move delayed/dormant tasks from cmds on the
3614 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3617 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3618 transport_complete_task_attr(cmd
);
3620 * Check to schedule QUEUE_FULL work, or execute an existing
3621 * cmd->transport_qf_callback()
3623 if (atomic_read(&cmd
->se_dev
->dev_qf_count
) != 0)
3624 schedule_work(&cmd
->se_dev
->qf_work_queue
);
3626 if (cmd
->transport_qf_callback
) {
3627 ret
= cmd
->transport_qf_callback(cmd
);
3631 cmd
->transport_qf_callback
= NULL
;
3635 * Check if we need to retrieve a sense buffer from
3636 * the struct se_cmd in question.
3638 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
3639 if (transport_get_sense_data(cmd
) < 0)
3640 reason
= TCM_NON_EXISTENT_LUN
;
3643 * Only set when an struct se_task->task_scsi_status returned
3644 * a non GOOD status.
3646 if (cmd
->scsi_status
) {
3647 ret
= transport_send_check_condition_and_sense(
3652 transport_lun_remove_cmd(cmd
);
3653 transport_cmd_check_stop_to_fabric(cmd
);
3658 * Check for a callback, used by amongst other things
3659 * XDWRITE_READ_10 emulation.
3661 if (cmd
->transport_complete_callback
)
3662 cmd
->transport_complete_callback(cmd
);
3664 switch (cmd
->data_direction
) {
3665 case DMA_FROM_DEVICE
:
3666 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3667 if (cmd
->se_lun
->lun_sep
) {
3668 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
3671 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3673 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3678 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3679 if (cmd
->se_lun
->lun_sep
) {
3680 cmd
->se_lun
->lun_sep
->sep_stats
.rx_data_octets
+=
3683 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3685 * Check if we need to send READ payload for BIDI-COMMAND
3687 if (cmd
->t_bidi_data_sg
) {
3688 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3689 if (cmd
->se_lun
->lun_sep
) {
3690 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
3693 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3694 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
3699 /* Fall through for DMA_TO_DEVICE */
3701 ret
= cmd
->se_tfo
->queue_status(cmd
);
3710 transport_lun_remove_cmd(cmd
);
3711 transport_cmd_check_stop_to_fabric(cmd
);
3715 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3716 " data_direction: %d\n", cmd
, cmd
->data_direction
);
3717 transport_handle_queue_full(cmd
, cmd
->se_dev
, transport_complete_qf
);
3720 static void transport_free_dev_tasks(struct se_cmd
*cmd
)
3722 struct se_task
*task
, *task_tmp
;
3723 unsigned long flags
;
3725 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3726 list_for_each_entry_safe(task
, task_tmp
,
3727 &cmd
->t_task_list
, t_list
) {
3728 if (atomic_read(&task
->task_active
))
3731 kfree(task
->task_sg_bidi
);
3732 kfree(task
->task_sg
);
3734 list_del(&task
->t_list
);
3736 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3738 task
->se_dev
->transport
->free_task(task
);
3740 pr_err("task[%u] - task->se_dev is NULL\n",
3742 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3744 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3747 static inline void transport_free_sgl(struct scatterlist
*sgl
, int nents
)
3749 struct scatterlist
*sg
;
3752 for_each_sg(sgl
, sg
, nents
, count
)
3753 __free_page(sg_page(sg
));
3758 static inline void transport_free_pages(struct se_cmd
*cmd
)
3760 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
)
3763 transport_free_sgl(cmd
->t_data_sg
, cmd
->t_data_nents
);
3764 cmd
->t_data_sg
= NULL
;
3765 cmd
->t_data_nents
= 0;
3767 transport_free_sgl(cmd
->t_bidi_data_sg
, cmd
->t_bidi_data_nents
);
3768 cmd
->t_bidi_data_sg
= NULL
;
3769 cmd
->t_bidi_data_nents
= 0;
3772 static inline void transport_release_tasks(struct se_cmd
*cmd
)
3774 transport_free_dev_tasks(cmd
);
3777 static inline int transport_dec_and_check(struct se_cmd
*cmd
)
3779 unsigned long flags
;
3781 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3782 if (atomic_read(&cmd
->t_fe_count
)) {
3783 if (!atomic_dec_and_test(&cmd
->t_fe_count
)) {
3784 spin_unlock_irqrestore(&cmd
->t_state_lock
,
3790 if (atomic_read(&cmd
->t_se_count
)) {
3791 if (!atomic_dec_and_test(&cmd
->t_se_count
)) {
3792 spin_unlock_irqrestore(&cmd
->t_state_lock
,
3797 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3802 static void transport_release_fe_cmd(struct se_cmd
*cmd
)
3804 unsigned long flags
;
3806 if (transport_dec_and_check(cmd
))
3809 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3810 if (!atomic_read(&cmd
->transport_dev_active
)) {
3811 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3814 atomic_set(&cmd
->transport_dev_active
, 0);
3815 transport_all_task_dev_remove_state(cmd
);
3816 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3818 transport_release_tasks(cmd
);
3820 transport_free_pages(cmd
);
3821 transport_free_se_cmd(cmd
);
3822 cmd
->se_tfo
->release_cmd(cmd
);
3826 transport_generic_remove(struct se_cmd
*cmd
, int session_reinstatement
)
3828 unsigned long flags
;
3830 if (transport_dec_and_check(cmd
)) {
3831 if (session_reinstatement
) {
3832 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3833 transport_all_task_dev_remove_state(cmd
);
3834 spin_unlock_irqrestore(&cmd
->t_state_lock
,
3840 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
3841 if (!atomic_read(&cmd
->transport_dev_active
)) {
3842 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3845 atomic_set(&cmd
->transport_dev_active
, 0);
3846 transport_all_task_dev_remove_state(cmd
);
3847 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
3849 transport_release_tasks(cmd
);
3852 transport_free_pages(cmd
);
3853 transport_release_cmd(cmd
);
3858 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3859 * allocating in the core.
3860 * @cmd: Associated se_cmd descriptor
3861 * @mem: SGL style memory for TCM WRITE / READ
3862 * @sg_mem_num: Number of SGL elements
3863 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3864 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3866 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3869 int transport_generic_map_mem_to_cmd(
3871 struct scatterlist
*sgl
,
3873 struct scatterlist
*sgl_bidi
,
3876 if (!sgl
|| !sgl_count
)
3879 if ((cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
) ||
3880 (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
)) {
3882 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3883 * scatterlists already have been set to follow what the fabric
3884 * passes for the original expected data transfer length.
3886 if (cmd
->se_cmd_flags
& SCF_OVERFLOW_BIT
) {
3887 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3888 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3889 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3890 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3894 cmd
->t_data_sg
= sgl
;
3895 cmd
->t_data_nents
= sgl_count
;
3897 if (sgl_bidi
&& sgl_bidi_count
) {
3898 cmd
->t_bidi_data_sg
= sgl_bidi
;
3899 cmd
->t_bidi_data_nents
= sgl_bidi_count
;
3901 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
;
3906 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd
);
3908 static int transport_new_cmd_obj(struct se_cmd
*cmd
)
3910 struct se_device
*dev
= cmd
->se_dev
;
3911 int set_counts
= 1, rc
, task_cdbs
;
3914 * Setup any BIDI READ tasks and memory from
3915 * cmd->t_mem_bidi_list so the READ struct se_tasks
3916 * are queued first for the non pSCSI passthrough case.
3918 if (cmd
->t_bidi_data_sg
&&
3919 (dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
)) {
3920 rc
= transport_allocate_tasks(cmd
,
3923 cmd
->t_bidi_data_sg
,
3924 cmd
->t_bidi_data_nents
);
3926 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3927 cmd
->scsi_sense_reason
=
3928 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3931 atomic_inc(&cmd
->t_fe_count
);
3932 atomic_inc(&cmd
->t_se_count
);
3936 * Setup the tasks and memory from cmd->t_mem_list
3937 * Note for BIDI transfers this will contain the WRITE payload
3939 task_cdbs
= transport_allocate_tasks(cmd
,
3941 cmd
->data_direction
,
3944 if (task_cdbs
<= 0) {
3945 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3946 cmd
->scsi_sense_reason
=
3947 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
3952 atomic_inc(&cmd
->t_fe_count
);
3953 atomic_inc(&cmd
->t_se_count
);
3956 cmd
->t_task_list_num
= task_cdbs
;
3958 atomic_set(&cmd
->t_task_cdbs_left
, task_cdbs
);
3959 atomic_set(&cmd
->t_task_cdbs_ex_left
, task_cdbs
);
3960 atomic_set(&cmd
->t_task_cdbs_timeout_left
, task_cdbs
);
3964 void *transport_kmap_first_data_page(struct se_cmd
*cmd
)
3966 struct scatterlist
*sg
= cmd
->t_data_sg
;
3970 * We need to take into account a possible offset here for fabrics like
3971 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3972 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3974 return kmap(sg_page(sg
)) + sg
->offset
;
3976 EXPORT_SYMBOL(transport_kmap_first_data_page
);
3978 void transport_kunmap_first_data_page(struct se_cmd
*cmd
)
3980 kunmap(sg_page(cmd
->t_data_sg
));
3982 EXPORT_SYMBOL(transport_kunmap_first_data_page
);
3985 transport_generic_get_mem(struct se_cmd
*cmd
)
3987 u32 length
= cmd
->data_length
;
3992 nents
= DIV_ROUND_UP(length
, PAGE_SIZE
);
3993 cmd
->t_data_sg
= kmalloc(sizeof(struct scatterlist
) * nents
, GFP_KERNEL
);
3994 if (!cmd
->t_data_sg
)
3997 cmd
->t_data_nents
= nents
;
3998 sg_init_table(cmd
->t_data_sg
, nents
);
4001 u32 page_len
= min_t(u32
, length
, PAGE_SIZE
);
4002 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
4006 sg_set_page(&cmd
->t_data_sg
[i
], page
, page_len
, 0);
4014 __free_page(sg_page(&cmd
->t_data_sg
[i
]));
4017 kfree(cmd
->t_data_sg
);
4018 cmd
->t_data_sg
= NULL
;
4022 /* Reduce sectors if they are too long for the device */
4023 static inline sector_t
transport_limit_task_sectors(
4024 struct se_device
*dev
,
4025 unsigned long long lba
,
4028 sectors
= min_t(sector_t
, sectors
, dev
->se_sub_dev
->se_dev_attrib
.max_sectors
);
4030 if (dev
->transport
->get_device_type(dev
) == TYPE_DISK
)
4031 if ((lba
+ sectors
) > transport_dev_end_lba(dev
))
4032 sectors
= ((transport_dev_end_lba(dev
) - lba
) + 1);
4039 * This function can be used by HW target mode drivers to create a linked
4040 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4041 * This is intended to be called during the completion path by TCM Core
4042 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4044 void transport_do_task_sg_chain(struct se_cmd
*cmd
)
4046 struct scatterlist
*sg_first
= NULL
;
4047 struct scatterlist
*sg_prev
= NULL
;
4048 int sg_prev_nents
= 0;
4049 struct scatterlist
*sg
;
4050 struct se_task
*task
;
4051 u32 chained_nents
= 0;
4054 BUG_ON(!cmd
->se_tfo
->task_sg_chaining
);
4057 * Walk the struct se_task list and setup scatterlist chains
4058 * for each contiguously allocated struct se_task->task_sg[].
4060 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
4065 sg_first
= task
->task_sg
;
4066 chained_nents
= task
->task_sg_nents
;
4068 sg_chain(sg_prev
, sg_prev_nents
, task
->task_sg
);
4069 chained_nents
+= task
->task_sg_nents
;
4072 * For the padded tasks, use the extra SGL vector allocated
4073 * in transport_allocate_data_tasks() for the sg_prev_nents
4074 * offset into sg_chain() above.. The last task of a
4075 * multi-task list, or a single task will not have
4076 * task->task_sg_padded set..
4078 if (task
->task_padded_sg
)
4079 sg_prev_nents
= (task
->task_sg_nents
+ 1);
4081 sg_prev_nents
= task
->task_sg_nents
;
4083 sg_prev
= task
->task_sg
;
4086 * Setup the starting pointer and total t_tasks_sg_linked_no including
4087 * padding SGs for linking and to mark the end.
4089 cmd
->t_tasks_sg_chained
= sg_first
;
4090 cmd
->t_tasks_sg_chained_no
= chained_nents
;
4092 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
4093 " t_tasks_sg_chained_no: %u\n", cmd
, cmd
->t_tasks_sg_chained
,
4094 cmd
->t_tasks_sg_chained_no
);
4096 for_each_sg(cmd
->t_tasks_sg_chained
, sg
,
4097 cmd
->t_tasks_sg_chained_no
, i
) {
4099 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
4100 i
, sg
, sg_page(sg
), sg
->length
, sg
->offset
);
4101 if (sg_is_chain(sg
))
4102 pr_debug("SG: %p sg_is_chain=1\n", sg
);
4104 pr_debug("SG: %p sg_is_last=1\n", sg
);
4107 EXPORT_SYMBOL(transport_do_task_sg_chain
);
4110 * Break up cmd into chunks transport can handle
4112 static int transport_allocate_data_tasks(
4114 unsigned long long lba
,
4115 enum dma_data_direction data_direction
,
4116 struct scatterlist
*sgl
,
4117 unsigned int sgl_nents
)
4119 unsigned char *cdb
= NULL
;
4120 struct se_task
*task
;
4121 struct se_device
*dev
= cmd
->se_dev
;
4122 unsigned long flags
;
4123 int task_count
, i
, ret
;
4124 sector_t sectors
, dev_max_sectors
= dev
->se_sub_dev
->se_dev_attrib
.max_sectors
;
4125 u32 sector_size
= dev
->se_sub_dev
->se_dev_attrib
.block_size
;
4126 struct scatterlist
*sg
;
4127 struct scatterlist
*cmd_sg
;
4129 WARN_ON(cmd
->data_length
% sector_size
);
4130 sectors
= DIV_ROUND_UP(cmd
->data_length
, sector_size
);
4131 task_count
= DIV_ROUND_UP_SECTOR_T(sectors
, dev_max_sectors
);
4134 for (i
= 0; i
< task_count
; i
++) {
4135 unsigned int task_size
, task_sg_nents_padded
;
4138 task
= transport_generic_get_task(cmd
, data_direction
);
4142 task
->task_lba
= lba
;
4143 task
->task_sectors
= min(sectors
, dev_max_sectors
);
4144 task
->task_size
= task
->task_sectors
* sector_size
;
4146 cdb
= dev
->transport
->get_cdb(task
);
4149 memcpy(cdb
, cmd
->t_task_cdb
,
4150 scsi_command_size(cmd
->t_task_cdb
));
4152 /* Update new cdb with updated lba/sectors */
4153 cmd
->transport_split_cdb(task
->task_lba
, task
->task_sectors
, cdb
);
4155 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
4156 * in order to calculate the number per task SGL entries
4158 task
->task_sg_nents
= DIV_ROUND_UP(task
->task_size
, PAGE_SIZE
);
4160 * Check if the fabric module driver is requesting that all
4161 * struct se_task->task_sg[] be chained together.. If so,
4162 * then allocate an extra padding SG entry for linking and
4163 * marking the end of the chained SGL for every task except
4164 * the last one for (task_count > 1) operation, or skipping
4165 * the extra padding for the (task_count == 1) case.
4167 if (cmd
->se_tfo
->task_sg_chaining
&& (i
< (task_count
- 1))) {
4168 task_sg_nents_padded
= (task
->task_sg_nents
+ 1);
4169 task
->task_padded_sg
= 1;
4171 task_sg_nents_padded
= task
->task_sg_nents
;
4173 task
->task_sg
= kmalloc(sizeof(struct scatterlist
) *
4174 task_sg_nents_padded
, GFP_KERNEL
);
4175 if (!task
->task_sg
) {
4176 cmd
->se_dev
->transport
->free_task(task
);
4180 sg_init_table(task
->task_sg
, task_sg_nents_padded
);
4182 task_size
= task
->task_size
;
4184 /* Build new sgl, only up to task_size */
4185 for_each_sg(task
->task_sg
, sg
, task
->task_sg_nents
, count
) {
4186 if (cmd_sg
->length
> task_size
)
4190 task_size
-= cmd_sg
->length
;
4191 cmd_sg
= sg_next(cmd_sg
);
4194 lba
+= task
->task_sectors
;
4195 sectors
-= task
->task_sectors
;
4197 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4198 list_add_tail(&task
->t_list
, &cmd
->t_task_list
);
4199 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4202 * Now perform the memory map of task->task_sg[] into backend
4203 * subsystem memory..
4205 list_for_each_entry(task
, &cmd
->t_task_list
, t_list
) {
4206 if (atomic_read(&task
->task_sent
))
4208 if (!dev
->transport
->map_data_SG
)
4211 ret
= dev
->transport
->map_data_SG(task
);
4220 transport_allocate_control_task(struct se_cmd
*cmd
)
4222 struct se_device
*dev
= cmd
->se_dev
;
4224 struct se_task
*task
;
4225 unsigned long flags
;
4228 task
= transport_generic_get_task(cmd
, cmd
->data_direction
);
4232 cdb
= dev
->transport
->get_cdb(task
);
4234 memcpy(cdb
, cmd
->t_task_cdb
,
4235 scsi_command_size(cmd
->t_task_cdb
));
4237 task
->task_sg
= kmalloc(sizeof(struct scatterlist
) * cmd
->t_data_nents
,
4239 if (!task
->task_sg
) {
4240 cmd
->se_dev
->transport
->free_task(task
);
4244 memcpy(task
->task_sg
, cmd
->t_data_sg
,
4245 sizeof(struct scatterlist
) * cmd
->t_data_nents
);
4246 task
->task_size
= cmd
->data_length
;
4247 task
->task_sg_nents
= cmd
->t_data_nents
;
4249 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4250 list_add_tail(&task
->t_list
, &cmd
->t_task_list
);
4251 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4253 if (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
) {
4254 if (dev
->transport
->map_control_SG
)
4255 ret
= dev
->transport
->map_control_SG(task
);
4256 } else if (cmd
->se_cmd_flags
& SCF_SCSI_NON_DATA_CDB
) {
4257 if (dev
->transport
->cdb_none
)
4258 ret
= dev
->transport
->cdb_none(task
);
4260 pr_err("target: Unknown control cmd type!\n");
4264 /* Success! Return number of tasks allocated */
4270 static u32
transport_allocate_tasks(
4272 unsigned long long lba
,
4273 enum dma_data_direction data_direction
,
4274 struct scatterlist
*sgl
,
4275 unsigned int sgl_nents
)
4277 if (cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
) {
4278 if (transport_cmd_get_valid_sectors(cmd
) < 0)
4281 return transport_allocate_data_tasks(cmd
, lba
, data_direction
,
4284 return transport_allocate_control_task(cmd
);
4289 /* transport_generic_new_cmd(): Called from transport_processing_thread()
4291 * Allocate storage transport resources from a set of values predefined
4292 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4293 * Any non zero return here is treated as an "out of resource' op here.
4296 * Generate struct se_task(s) and/or their payloads for this CDB.
4298 int transport_generic_new_cmd(struct se_cmd
*cmd
)
4303 * Determine is the TCM fabric module has already allocated physical
4304 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4307 if (!(cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) &&
4309 ret
= transport_generic_get_mem(cmd
);
4314 * Call transport_new_cmd_obj() to invoke transport_allocate_tasks() for
4315 * control or data CDB types, and perform the map to backend subsystem
4316 * code from SGL memory allocated here by transport_generic_get_mem(), or
4317 * via pre-existing SGL memory setup explictly by fabric module code with
4318 * transport_generic_map_mem_to_cmd().
4320 ret
= transport_new_cmd_obj(cmd
);
4324 * For WRITEs, let the fabric know its buffer is ready..
4325 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4326 * will be added to the struct se_device execution queue after its WRITE
4327 * data has arrived. (ie: It gets handled by the transport processing
4328 * thread a second time)
4330 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
4331 transport_add_tasks_to_state_queue(cmd
);
4332 return transport_generic_write_pending(cmd
);
4335 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4336 * to the execution queue.
4338 transport_execute_tasks(cmd
);
4341 EXPORT_SYMBOL(transport_generic_new_cmd
);
4343 /* transport_generic_process_write():
4347 void transport_generic_process_write(struct se_cmd
*cmd
)
4349 transport_execute_tasks(cmd
);
4351 EXPORT_SYMBOL(transport_generic_process_write
);
4353 static int transport_write_pending_qf(struct se_cmd
*cmd
)
4355 return cmd
->se_tfo
->write_pending(cmd
);
4358 /* transport_generic_write_pending():
4362 static int transport_generic_write_pending(struct se_cmd
*cmd
)
4364 unsigned long flags
;
4367 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4368 cmd
->t_state
= TRANSPORT_WRITE_PENDING
;
4369 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4371 if (cmd
->transport_qf_callback
) {
4372 ret
= cmd
->transport_qf_callback(cmd
);
4378 cmd
->transport_qf_callback
= NULL
;
4383 * Clear the se_cmd for WRITE_PENDING status in order to set
4384 * cmd->t_transport_active=0 so that transport_generic_handle_data
4385 * can be called from HW target mode interrupt code. This is safe
4386 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
4387 * because the se_cmd->se_lun pointer is not being cleared.
4389 transport_cmd_check_stop(cmd
, 1, 0);
4392 * Call the fabric write_pending function here to let the
4393 * frontend know that WRITE buffers are ready.
4395 ret
= cmd
->se_tfo
->write_pending(cmd
);
4401 return PYX_TRANSPORT_WRITE_PENDING
;
4404 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd
);
4405 cmd
->t_state
= TRANSPORT_COMPLETE_QF_WP
;
4406 transport_handle_queue_full(cmd
, cmd
->se_dev
,
4407 transport_write_pending_qf
);
4411 void transport_release_cmd(struct se_cmd
*cmd
)
4413 BUG_ON(!cmd
->se_tfo
);
4415 transport_free_se_cmd(cmd
);
4416 cmd
->se_tfo
->release_cmd(cmd
);
4418 EXPORT_SYMBOL(transport_release_cmd
);
4420 /* transport_generic_free_cmd():
4422 * Called from processing frontend to release storage engine resources
4424 void transport_generic_free_cmd(
4427 int session_reinstatement
)
4429 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
))
4430 transport_release_cmd(cmd
);
4432 core_dec_lacl_count(cmd
->se_sess
->se_node_acl
, cmd
);
4436 pr_debug("cmd: %p ITT: 0x%08x contains"
4437 " cmd->se_lun\n", cmd
,
4438 cmd
->se_tfo
->get_task_tag(cmd
));
4440 transport_lun_remove_cmd(cmd
);
4443 if (wait_for_tasks
&& cmd
->transport_wait_for_tasks
)
4444 cmd
->transport_wait_for_tasks(cmd
, 0, 0);
4446 transport_free_dev_tasks(cmd
);
4448 transport_generic_remove(cmd
, session_reinstatement
);
4451 EXPORT_SYMBOL(transport_generic_free_cmd
);
4453 static void transport_nop_wait_for_tasks(
4456 int session_reinstatement
)
4461 /* transport_lun_wait_for_tasks():
4463 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4464 * an struct se_lun to be successfully shutdown.
4466 static int transport_lun_wait_for_tasks(struct se_cmd
*cmd
, struct se_lun
*lun
)
4468 unsigned long flags
;
4471 * If the frontend has already requested this struct se_cmd to
4472 * be stopped, we can safely ignore this struct se_cmd.
4474 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4475 if (atomic_read(&cmd
->t_transport_stop
)) {
4476 atomic_set(&cmd
->transport_lun_stop
, 0);
4477 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4478 " TRUE, skipping\n", cmd
->se_tfo
->get_task_tag(cmd
));
4479 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4480 transport_cmd_check_stop(cmd
, 1, 0);
4483 atomic_set(&cmd
->transport_lun_fe_stop
, 1);
4484 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4486 wake_up_interruptible(&cmd
->se_dev
->dev_queue_obj
.thread_wq
);
4488 ret
= transport_stop_tasks_for_cmd(cmd
);
4490 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4491 " %d\n", cmd
, cmd
->t_task_list_num
, ret
);
4493 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4494 cmd
->se_tfo
->get_task_tag(cmd
));
4495 wait_for_completion(&cmd
->transport_lun_stop_comp
);
4496 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4497 cmd
->se_tfo
->get_task_tag(cmd
));
4499 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
4504 static void __transport_clear_lun_from_sessions(struct se_lun
*lun
)
4506 struct se_cmd
*cmd
= NULL
;
4507 unsigned long lun_flags
, cmd_flags
;
4509 * Do exception processing and return CHECK_CONDITION status to the
4512 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4513 while (!list_empty(&lun
->lun_cmd_list
)) {
4514 cmd
= list_first_entry(&lun
->lun_cmd_list
,
4515 struct se_cmd
, se_lun_node
);
4516 list_del(&cmd
->se_lun_node
);
4518 atomic_set(&cmd
->transport_lun_active
, 0);
4520 * This will notify iscsi_target_transport.c:
4521 * transport_cmd_check_stop() that a LUN shutdown is in
4522 * progress for the iscsi_cmd_t.
4524 spin_lock(&cmd
->t_state_lock
);
4525 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4526 "_lun_stop for ITT: 0x%08x\n",
4527 cmd
->se_lun
->unpacked_lun
,
4528 cmd
->se_tfo
->get_task_tag(cmd
));
4529 atomic_set(&cmd
->transport_lun_stop
, 1);
4530 spin_unlock(&cmd
->t_state_lock
);
4532 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
4535 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4536 cmd
->se_tfo
->get_task_tag(cmd
),
4537 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
4541 * If the Storage engine still owns the iscsi_cmd_t, determine
4542 * and/or stop its context.
4544 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4545 "_lun_wait_for_tasks()\n", cmd
->se_lun
->unpacked_lun
,
4546 cmd
->se_tfo
->get_task_tag(cmd
));
4548 if (transport_lun_wait_for_tasks(cmd
, cmd
->se_lun
) < 0) {
4549 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4553 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4554 "_wait_for_tasks(): SUCCESS\n",
4555 cmd
->se_lun
->unpacked_lun
,
4556 cmd
->se_tfo
->get_task_tag(cmd
));
4558 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
4559 if (!atomic_read(&cmd
->transport_dev_active
)) {
4560 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
4563 atomic_set(&cmd
->transport_dev_active
, 0);
4564 transport_all_task_dev_remove_state(cmd
);
4565 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
4567 transport_free_dev_tasks(cmd
);
4569 * The Storage engine stopped this struct se_cmd before it was
4570 * send to the fabric frontend for delivery back to the
4571 * Initiator Node. Return this SCSI CDB back with an
4572 * CHECK_CONDITION status.
4575 transport_send_check_condition_and_sense(cmd
,
4576 TCM_NON_EXISTENT_LUN
, 0);
4578 * If the fabric frontend is waiting for this iscsi_cmd_t to
4579 * be released, notify the waiting thread now that LU has
4580 * finished accessing it.
4582 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
4583 if (atomic_read(&cmd
->transport_lun_fe_stop
)) {
4584 pr_debug("SE_LUN[%d] - Detected FE stop for"
4585 " struct se_cmd: %p ITT: 0x%08x\n",
4587 cmd
, cmd
->se_tfo
->get_task_tag(cmd
));
4589 spin_unlock_irqrestore(&cmd
->t_state_lock
,
4591 transport_cmd_check_stop(cmd
, 1, 0);
4592 complete(&cmd
->transport_lun_fe_stop_comp
);
4593 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4596 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4597 lun
->unpacked_lun
, cmd
->se_tfo
->get_task_tag(cmd
));
4599 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
4600 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
4602 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
4605 static int transport_clear_lun_thread(void *p
)
4607 struct se_lun
*lun
= (struct se_lun
*)p
;
4609 __transport_clear_lun_from_sessions(lun
);
4610 complete(&lun
->lun_shutdown_comp
);
4615 int transport_clear_lun_from_sessions(struct se_lun
*lun
)
4617 struct task_struct
*kt
;
4619 kt
= kthread_run(transport_clear_lun_thread
, lun
,
4620 "tcm_cl_%u", lun
->unpacked_lun
);
4622 pr_err("Unable to start clear_lun thread\n");
4625 wait_for_completion(&lun
->lun_shutdown_comp
);
4630 /* transport_generic_wait_for_tasks():
4632 * Called from frontend or passthrough context to wait for storage engine
4633 * to pause and/or release frontend generated struct se_cmd.
4635 static void transport_generic_wait_for_tasks(
4638 int session_reinstatement
)
4640 unsigned long flags
;
4642 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
) && !(cmd
->se_tmr_req
))
4645 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4647 * If we are already stopped due to an external event (ie: LUN shutdown)
4648 * sleep until the connection can have the passed struct se_cmd back.
4649 * The cmd->transport_lun_stopped_sem will be upped by
4650 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4651 * has completed its operation on the struct se_cmd.
4653 if (atomic_read(&cmd
->transport_lun_stop
)) {
4655 pr_debug("wait_for_tasks: Stopping"
4656 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4657 "_stop_comp); for ITT: 0x%08x\n",
4658 cmd
->se_tfo
->get_task_tag(cmd
));
4660 * There is a special case for WRITES where a FE exception +
4661 * LUN shutdown means ConfigFS context is still sleeping on
4662 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4663 * We go ahead and up transport_lun_stop_comp just to be sure
4666 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4667 complete(&cmd
->transport_lun_stop_comp
);
4668 wait_for_completion(&cmd
->transport_lun_fe_stop_comp
);
4669 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4671 transport_all_task_dev_remove_state(cmd
);
4673 * At this point, the frontend who was the originator of this
4674 * struct se_cmd, now owns the structure and can be released through
4675 * normal means below.
4677 pr_debug("wait_for_tasks: Stopped"
4678 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4679 "stop_comp); for ITT: 0x%08x\n",
4680 cmd
->se_tfo
->get_task_tag(cmd
));
4682 atomic_set(&cmd
->transport_lun_stop
, 0);
4684 if (!atomic_read(&cmd
->t_transport_active
) ||
4685 atomic_read(&cmd
->t_transport_aborted
))
4688 atomic_set(&cmd
->t_transport_stop
, 1);
4690 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4691 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
4692 " = TRUE\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
4693 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
,
4694 cmd
->deferred_t_state
);
4696 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4698 wake_up_interruptible(&cmd
->se_dev
->dev_queue_obj
.thread_wq
);
4700 wait_for_completion(&cmd
->t_transport_stop_comp
);
4702 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4703 atomic_set(&cmd
->t_transport_active
, 0);
4704 atomic_set(&cmd
->t_transport_stop
, 0);
4706 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4707 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4708 cmd
->se_tfo
->get_task_tag(cmd
));
4710 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4714 transport_generic_free_cmd(cmd
, 0, session_reinstatement
);
4717 static int transport_get_sense_codes(
4722 *asc
= cmd
->scsi_asc
;
4723 *ascq
= cmd
->scsi_ascq
;
4728 static int transport_set_sense_codes(
4733 cmd
->scsi_asc
= asc
;
4734 cmd
->scsi_ascq
= ascq
;
4739 int transport_send_check_condition_and_sense(
4744 unsigned char *buffer
= cmd
->sense_buffer
;
4745 unsigned long flags
;
4747 u8 asc
= 0, ascq
= 0;
4749 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4750 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
4751 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4754 cmd
->se_cmd_flags
|= SCF_SENT_CHECK_CONDITION
;
4755 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4757 if (!reason
&& from_transport
)
4760 if (!from_transport
)
4761 cmd
->se_cmd_flags
|= SCF_EMULATED_TASK_SENSE
;
4763 * Data Segment and SenseLength of the fabric response PDU.
4765 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4766 * from include/scsi/scsi_cmnd.h
4768 offset
= cmd
->se_tfo
->set_fabric_sense_len(cmd
,
4769 TRANSPORT_SENSE_BUFFER
);
4771 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4772 * SENSE KEY values from include/scsi/scsi.h
4775 case TCM_NON_EXISTENT_LUN
:
4777 buffer
[offset
] = 0x70;
4778 /* ILLEGAL REQUEST */
4779 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
4780 /* LOGICAL UNIT NOT SUPPORTED */
4781 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x25;
4783 case TCM_UNSUPPORTED_SCSI_OPCODE
:
4784 case TCM_SECTOR_COUNT_TOO_MANY
:
4786 buffer
[offset
] = 0x70;
4787 /* ILLEGAL REQUEST */
4788 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
4789 /* INVALID COMMAND OPERATION CODE */
4790 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x20;
4792 case TCM_UNKNOWN_MODE_PAGE
:
4794 buffer
[offset
] = 0x70;
4795 /* ILLEGAL REQUEST */
4796 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
4797 /* INVALID FIELD IN CDB */
4798 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x24;
4800 case TCM_CHECK_CONDITION_ABORT_CMD
:
4802 buffer
[offset
] = 0x70;
4803 /* ABORTED COMMAND */
4804 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4805 /* BUS DEVICE RESET FUNCTION OCCURRED */
4806 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x29;
4807 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x03;
4809 case TCM_INCORRECT_AMOUNT_OF_DATA
:
4811 buffer
[offset
] = 0x70;
4812 /* ABORTED COMMAND */
4813 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4815 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x0c;
4816 /* NOT ENOUGH UNSOLICITED DATA */
4817 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x0d;
4819 case TCM_INVALID_CDB_FIELD
:
4821 buffer
[offset
] = 0x70;
4822 /* ABORTED COMMAND */
4823 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4824 /* INVALID FIELD IN CDB */
4825 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x24;
4827 case TCM_INVALID_PARAMETER_LIST
:
4829 buffer
[offset
] = 0x70;
4830 /* ABORTED COMMAND */
4831 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4832 /* INVALID FIELD IN PARAMETER LIST */
4833 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x26;
4835 case TCM_UNEXPECTED_UNSOLICITED_DATA
:
4837 buffer
[offset
] = 0x70;
4838 /* ABORTED COMMAND */
4839 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4841 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x0c;
4842 /* UNEXPECTED_UNSOLICITED_DATA */
4843 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x0c;
4845 case TCM_SERVICE_CRC_ERROR
:
4847 buffer
[offset
] = 0x70;
4848 /* ABORTED COMMAND */
4849 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4850 /* PROTOCOL SERVICE CRC ERROR */
4851 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x47;
4853 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x05;
4855 case TCM_SNACK_REJECTED
:
4857 buffer
[offset
] = 0x70;
4858 /* ABORTED COMMAND */
4859 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
4861 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x11;
4862 /* FAILED RETRANSMISSION REQUEST */
4863 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x13;
4865 case TCM_WRITE_PROTECTED
:
4867 buffer
[offset
] = 0x70;
4869 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = DATA_PROTECT
;
4870 /* WRITE PROTECTED */
4871 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x27;
4873 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
4875 buffer
[offset
] = 0x70;
4876 /* UNIT ATTENTION */
4877 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
4878 core_scsi3_ua_for_check_condition(cmd
, &asc
, &ascq
);
4879 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = asc
;
4880 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = ascq
;
4882 case TCM_CHECK_CONDITION_NOT_READY
:
4884 buffer
[offset
] = 0x70;
4886 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
4887 transport_get_sense_codes(cmd
, &asc
, &ascq
);
4888 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = asc
;
4889 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = ascq
;
4891 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
4894 buffer
[offset
] = 0x70;
4895 /* ILLEGAL REQUEST */
4896 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
4897 /* LOGICAL UNIT COMMUNICATION FAILURE */
4898 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x80;
4902 * This code uses linux/include/scsi/scsi.h SAM status codes!
4904 cmd
->scsi_status
= SAM_STAT_CHECK_CONDITION
;
4906 * Automatically padded, this value is encoded in the fabric's
4907 * data_length response PDU containing the SCSI defined sense data.
4909 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
+ offset
;
4912 return cmd
->se_tfo
->queue_status(cmd
);
4914 EXPORT_SYMBOL(transport_send_check_condition_and_sense
);
4916 int transport_check_aborted_status(struct se_cmd
*cmd
, int send_status
)
4920 if (atomic_read(&cmd
->t_transport_aborted
) != 0) {
4922 (cmd
->se_cmd_flags
& SCF_SENT_DELAYED_TAS
))
4925 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4926 " status for CDB: 0x%02x ITT: 0x%08x\n",
4928 cmd
->se_tfo
->get_task_tag(cmd
));
4930 cmd
->se_cmd_flags
|= SCF_SENT_DELAYED_TAS
;
4931 cmd
->se_tfo
->queue_status(cmd
);
4936 EXPORT_SYMBOL(transport_check_aborted_status
);
4938 void transport_send_task_abort(struct se_cmd
*cmd
)
4940 unsigned long flags
;
4942 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
4943 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
4944 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4947 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
4950 * If there are still expected incoming fabric WRITEs, we wait
4951 * until until they have completed before sending a TASK_ABORTED
4952 * response. This response with TASK_ABORTED status will be
4953 * queued back to fabric module by transport_check_aborted_status().
4955 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
4956 if (cmd
->se_tfo
->write_pending_status(cmd
) != 0) {
4957 atomic_inc(&cmd
->t_transport_aborted
);
4958 smp_mb__after_atomic_inc();
4959 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
4960 transport_new_cmd_failure(cmd
);
4964 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
4966 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4967 " ITT: 0x%08x\n", cmd
->t_task_cdb
[0],
4968 cmd
->se_tfo
->get_task_tag(cmd
));
4970 cmd
->se_tfo
->queue_status(cmd
);
4973 /* transport_generic_do_tmr():
4977 int transport_generic_do_tmr(struct se_cmd
*cmd
)
4979 struct se_device
*dev
= cmd
->se_dev
;
4980 struct se_tmr_req
*tmr
= cmd
->se_tmr_req
;
4983 switch (tmr
->function
) {
4984 case TMR_ABORT_TASK
:
4985 tmr
->response
= TMR_FUNCTION_REJECTED
;
4987 case TMR_ABORT_TASK_SET
:
4989 case TMR_CLEAR_TASK_SET
:
4990 tmr
->response
= TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
4993 ret
= core_tmr_lun_reset(dev
, tmr
, NULL
, NULL
);
4994 tmr
->response
= (!ret
) ? TMR_FUNCTION_COMPLETE
:
4995 TMR_FUNCTION_REJECTED
;
4997 case TMR_TARGET_WARM_RESET
:
4998 tmr
->response
= TMR_FUNCTION_REJECTED
;
5000 case TMR_TARGET_COLD_RESET
:
5001 tmr
->response
= TMR_FUNCTION_REJECTED
;
5004 pr_err("Uknown TMR function: 0x%02x.\n",
5006 tmr
->response
= TMR_FUNCTION_REJECTED
;
5010 cmd
->t_state
= TRANSPORT_ISTATE_PROCESSING
;
5011 cmd
->se_tfo
->queue_tm_rsp(cmd
);
5013 transport_cmd_check_stop(cmd
, 2, 0);
5018 * Called with spin_lock_irq(&dev->execute_task_lock); held
5021 static struct se_task
*
5022 transport_get_task_from_state_list(struct se_device
*dev
)
5024 struct se_task
*task
;
5026 if (list_empty(&dev
->state_task_list
))
5029 list_for_each_entry(task
, &dev
->state_task_list
, t_state_list
)
5032 list_del(&task
->t_state_list
);
5033 atomic_set(&task
->task_state_active
, 0);
5038 static void transport_processing_shutdown(struct se_device
*dev
)
5041 struct se_task
*task
;
5042 unsigned long flags
;
5044 * Empty the struct se_device's struct se_task state list.
5046 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5047 while ((task
= transport_get_task_from_state_list(dev
))) {
5048 if (!task
->task_se_cmd
) {
5049 pr_err("task->task_se_cmd is NULL!\n");
5052 cmd
= task
->task_se_cmd
;
5054 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
5056 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
5058 pr_debug("PT: cmd: %p task: %p ITT: 0x%08x,"
5059 " i_state: %d, t_state/def_t_state:"
5060 " %d/%d cdb: 0x%02x\n", cmd
, task
,
5061 cmd
->se_tfo
->get_task_tag(cmd
),
5062 cmd
->se_tfo
->get_cmd_state(cmd
),
5063 cmd
->t_state
, cmd
->deferred_t_state
,
5064 cmd
->t_task_cdb
[0]);
5065 pr_debug("PT: ITT[0x%08x] - t_tasks: %d t_task_cdbs_left:"
5066 " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5067 " t_transport_stop: %d t_transport_sent: %d\n",
5068 cmd
->se_tfo
->get_task_tag(cmd
),
5069 cmd
->t_task_list_num
,
5070 atomic_read(&cmd
->t_task_cdbs_left
),
5071 atomic_read(&cmd
->t_task_cdbs_sent
),
5072 atomic_read(&cmd
->t_transport_active
),
5073 atomic_read(&cmd
->t_transport_stop
),
5074 atomic_read(&cmd
->t_transport_sent
));
5076 if (atomic_read(&task
->task_active
)) {
5077 atomic_set(&task
->task_stop
, 1);
5078 spin_unlock_irqrestore(
5079 &cmd
->t_state_lock
, flags
);
5081 pr_debug("Waiting for task: %p to shutdown for dev:"
5082 " %p\n", task
, dev
);
5083 wait_for_completion(&task
->task_stop_comp
);
5084 pr_debug("Completed task: %p shutdown for dev: %p\n",
5087 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
5088 atomic_dec(&cmd
->t_task_cdbs_left
);
5090 atomic_set(&task
->task_active
, 0);
5091 atomic_set(&task
->task_stop
, 0);
5093 if (atomic_read(&task
->task_execute_queue
) != 0)
5094 transport_remove_task_from_execute_queue(task
, dev
);
5096 __transport_stop_task_timer(task
, &flags
);
5098 if (!atomic_dec_and_test(&cmd
->t_task_cdbs_ex_left
)) {
5099 spin_unlock_irqrestore(
5100 &cmd
->t_state_lock
, flags
);
5102 pr_debug("Skipping task: %p, dev: %p for"
5103 " t_task_cdbs_ex_left: %d\n", task
, dev
,
5104 atomic_read(&cmd
->t_task_cdbs_ex_left
));
5106 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5110 if (atomic_read(&cmd
->t_transport_active
)) {
5111 pr_debug("got t_transport_active = 1 for task: %p, dev:"
5112 " %p\n", task
, dev
);
5114 if (atomic_read(&cmd
->t_fe_count
)) {
5115 spin_unlock_irqrestore(
5116 &cmd
->t_state_lock
, flags
);
5117 transport_send_check_condition_and_sense(
5118 cmd
, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
,
5120 transport_remove_cmd_from_queue(cmd
,
5121 &cmd
->se_dev
->dev_queue_obj
);
5123 transport_lun_remove_cmd(cmd
);
5124 transport_cmd_check_stop(cmd
, 1, 0);
5126 spin_unlock_irqrestore(
5127 &cmd
->t_state_lock
, flags
);
5129 transport_remove_cmd_from_queue(cmd
,
5130 &cmd
->se_dev
->dev_queue_obj
);
5132 transport_lun_remove_cmd(cmd
);
5134 if (transport_cmd_check_stop(cmd
, 1, 0))
5135 transport_generic_remove(cmd
, 0);
5138 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5141 pr_debug("Got t_transport_active = 0 for task: %p, dev: %p\n",
5144 if (atomic_read(&cmd
->t_fe_count
)) {
5145 spin_unlock_irqrestore(
5146 &cmd
->t_state_lock
, flags
);
5147 transport_send_check_condition_and_sense(cmd
,
5148 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
5149 transport_remove_cmd_from_queue(cmd
,
5150 &cmd
->se_dev
->dev_queue_obj
);
5152 transport_lun_remove_cmd(cmd
);
5153 transport_cmd_check_stop(cmd
, 1, 0);
5155 spin_unlock_irqrestore(
5156 &cmd
->t_state_lock
, flags
);
5158 transport_remove_cmd_from_queue(cmd
,
5159 &cmd
->se_dev
->dev_queue_obj
);
5160 transport_lun_remove_cmd(cmd
);
5162 if (transport_cmd_check_stop(cmd
, 1, 0))
5163 transport_generic_remove(cmd
, 0);
5166 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5168 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
5170 * Empty the struct se_device's struct se_cmd list.
5172 while ((cmd
= transport_get_cmd_from_queue(&dev
->dev_queue_obj
))) {
5174 pr_debug("From Device Queue: cmd: %p t_state: %d\n",
5177 if (atomic_read(&cmd
->t_fe_count
)) {
5178 transport_send_check_condition_and_sense(cmd
,
5179 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
5181 transport_lun_remove_cmd(cmd
);
5182 transport_cmd_check_stop(cmd
, 1, 0);
5184 transport_lun_remove_cmd(cmd
);
5185 if (transport_cmd_check_stop(cmd
, 1, 0))
5186 transport_generic_remove(cmd
, 0);
5191 /* transport_processing_thread():
5195 static int transport_processing_thread(void *param
)
5199 struct se_device
*dev
= (struct se_device
*) param
;
5201 set_user_nice(current
, -20);
5203 while (!kthread_should_stop()) {
5204 ret
= wait_event_interruptible(dev
->dev_queue_obj
.thread_wq
,
5205 atomic_read(&dev
->dev_queue_obj
.queue_cnt
) ||
5206 kthread_should_stop());
5210 spin_lock_irq(&dev
->dev_status_lock
);
5211 if (dev
->dev_status
& TRANSPORT_DEVICE_SHUTDOWN
) {
5212 spin_unlock_irq(&dev
->dev_status_lock
);
5213 transport_processing_shutdown(dev
);
5216 spin_unlock_irq(&dev
->dev_status_lock
);
5219 __transport_execute_tasks(dev
);
5221 cmd
= transport_get_cmd_from_queue(&dev
->dev_queue_obj
);
5225 switch (cmd
->t_state
) {
5226 case TRANSPORT_NEW_CMD_MAP
:
5227 if (!cmd
->se_tfo
->new_cmd_map
) {
5228 pr_err("cmd->se_tfo->new_cmd_map is"
5229 " NULL for TRANSPORT_NEW_CMD_MAP\n");
5232 ret
= cmd
->se_tfo
->new_cmd_map(cmd
);
5234 cmd
->transport_error_status
= ret
;
5235 transport_generic_request_failure(cmd
, NULL
,
5236 0, (cmd
->data_direction
!=
5241 case TRANSPORT_NEW_CMD
:
5242 ret
= transport_generic_new_cmd(cmd
);
5246 cmd
->transport_error_status
= ret
;
5247 transport_generic_request_failure(cmd
, NULL
,
5248 0, (cmd
->data_direction
!=
5252 case TRANSPORT_PROCESS_WRITE
:
5253 transport_generic_process_write(cmd
);
5255 case TRANSPORT_COMPLETE_OK
:
5256 transport_stop_all_task_timers(cmd
);
5257 transport_generic_complete_ok(cmd
);
5259 case TRANSPORT_REMOVE
:
5260 transport_generic_remove(cmd
, 0);
5262 case TRANSPORT_FREE_CMD_INTR
:
5263 transport_generic_free_cmd(cmd
, 0, 0);
5265 case TRANSPORT_PROCESS_TMR
:
5266 transport_generic_do_tmr(cmd
);
5268 case TRANSPORT_COMPLETE_FAILURE
:
5269 transport_generic_request_failure(cmd
, NULL
, 1, 1);
5271 case TRANSPORT_COMPLETE_TIMEOUT
:
5272 transport_stop_all_task_timers(cmd
);
5273 transport_generic_request_timeout(cmd
);
5275 case TRANSPORT_COMPLETE_QF_WP
:
5276 transport_generic_write_pending(cmd
);
5279 pr_err("Unknown t_state: %d deferred_t_state:"
5280 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
5281 " %u\n", cmd
->t_state
, cmd
->deferred_t_state
,
5282 cmd
->se_tfo
->get_task_tag(cmd
),
5283 cmd
->se_tfo
->get_cmd_state(cmd
),
5284 cmd
->se_lun
->unpacked_lun
);
5292 transport_release_all_cmds(dev
);
5293 dev
->process_thread
= NULL
;