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 /* #define DEBUG_CDB_HANDLER */
62 #ifdef DEBUG_CDB_HANDLER
63 #define DEBUG_CDB_H(x...) printk(KERN_INFO x)
65 #define DEBUG_CDB_H(x...)
68 /* #define DEBUG_CMD_MAP */
70 #define DEBUG_CMD_M(x...) printk(KERN_INFO x)
72 #define DEBUG_CMD_M(x...)
75 /* #define DEBUG_MEM_ALLOC */
76 #ifdef DEBUG_MEM_ALLOC
77 #define DEBUG_MEM(x...) printk(KERN_INFO x)
79 #define DEBUG_MEM(x...)
82 /* #define DEBUG_MEM2_ALLOC */
83 #ifdef DEBUG_MEM2_ALLOC
84 #define DEBUG_MEM2(x...) printk(KERN_INFO x)
86 #define DEBUG_MEM2(x...)
89 /* #define DEBUG_SG_CALC */
91 #define DEBUG_SC(x...) printk(KERN_INFO x)
93 #define DEBUG_SC(x...)
96 /* #define DEBUG_SE_OBJ */
98 #define DEBUG_SO(x...) printk(KERN_INFO x)
100 #define DEBUG_SO(x...)
103 /* #define DEBUG_CMD_VOL */
105 #define DEBUG_VOL(x...) printk(KERN_INFO x)
107 #define DEBUG_VOL(x...)
110 /* #define DEBUG_CMD_STOP */
111 #ifdef DEBUG_CMD_STOP
112 #define DEBUG_CS(x...) printk(KERN_INFO x)
114 #define DEBUG_CS(x...)
117 /* #define DEBUG_PASSTHROUGH */
118 #ifdef DEBUG_PASSTHROUGH
119 #define DEBUG_PT(x...) printk(KERN_INFO x)
121 #define DEBUG_PT(x...)
124 /* #define DEBUG_TASK_STOP */
125 #ifdef DEBUG_TASK_STOP
126 #define DEBUG_TS(x...) printk(KERN_INFO x)
128 #define DEBUG_TS(x...)
131 /* #define DEBUG_TRANSPORT_STOP */
132 #ifdef DEBUG_TRANSPORT_STOP
133 #define DEBUG_TRANSPORT_S(x...) printk(KERN_INFO x)
135 #define DEBUG_TRANSPORT_S(x...)
138 /* #define DEBUG_TASK_FAILURE */
139 #ifdef DEBUG_TASK_FAILURE
140 #define DEBUG_TF(x...) printk(KERN_INFO x)
142 #define DEBUG_TF(x...)
145 /* #define DEBUG_DEV_OFFLINE */
146 #ifdef DEBUG_DEV_OFFLINE
147 #define DEBUG_DO(x...) printk(KERN_INFO x)
149 #define DEBUG_DO(x...)
152 /* #define DEBUG_TASK_STATE */
153 #ifdef DEBUG_TASK_STATE
154 #define DEBUG_TSTATE(x...) printk(KERN_INFO x)
156 #define DEBUG_TSTATE(x...)
159 /* #define DEBUG_STATUS_THR */
160 #ifdef DEBUG_STATUS_THR
161 #define DEBUG_ST(x...) printk(KERN_INFO x)
163 #define DEBUG_ST(x...)
166 /* #define DEBUG_TASK_TIMEOUT */
167 #ifdef DEBUG_TASK_TIMEOUT
168 #define DEBUG_TT(x...) printk(KERN_INFO x)
170 #define DEBUG_TT(x...)
173 /* #define DEBUG_GENERIC_REQUEST_FAILURE */
174 #ifdef DEBUG_GENERIC_REQUEST_FAILURE
175 #define DEBUG_GRF(x...) printk(KERN_INFO x)
177 #define DEBUG_GRF(x...)
180 /* #define DEBUG_SAM_TASK_ATTRS */
181 #ifdef DEBUG_SAM_TASK_ATTRS
182 #define DEBUG_STA(x...) printk(KERN_INFO x)
184 #define DEBUG_STA(x...)
187 static int sub_api_initialized
;
189 static struct kmem_cache
*se_cmd_cache
;
190 static struct kmem_cache
*se_sess_cache
;
191 struct kmem_cache
*se_tmr_req_cache
;
192 struct kmem_cache
*se_ua_cache
;
193 struct kmem_cache
*se_mem_cache
;
194 struct kmem_cache
*t10_pr_reg_cache
;
195 struct kmem_cache
*t10_alua_lu_gp_cache
;
196 struct kmem_cache
*t10_alua_lu_gp_mem_cache
;
197 struct kmem_cache
*t10_alua_tg_pt_gp_cache
;
198 struct kmem_cache
*t10_alua_tg_pt_gp_mem_cache
;
200 /* Used for transport_dev_get_map_*() */
201 typedef int (*map_func_t
)(struct se_task
*, u32
);
203 static int transport_generic_write_pending(struct se_cmd
*);
204 static int transport_processing_thread(void *param
);
205 static int __transport_execute_tasks(struct se_device
*dev
);
206 static void transport_complete_task_attr(struct se_cmd
*cmd
);
207 static void transport_direct_request_timeout(struct se_cmd
*cmd
);
208 static void transport_free_dev_tasks(struct se_cmd
*cmd
);
209 static u32
transport_generic_get_cdb_count(struct se_cmd
*cmd
,
210 unsigned long long starting_lba
, u32 sectors
,
211 enum dma_data_direction data_direction
,
212 struct list_head
*mem_list
, int set_counts
);
213 static int transport_generic_get_mem(struct se_cmd
*cmd
, u32 length
,
215 static int transport_generic_remove(struct se_cmd
*cmd
,
216 int release_to_pool
, int session_reinstatement
);
217 static int transport_get_sectors(struct se_cmd
*cmd
);
218 static int transport_map_sg_to_mem(struct se_cmd
*cmd
,
219 struct list_head
*se_mem_list
, struct scatterlist
*sgl
,
221 static void transport_memcpy_se_mem_read_contig(struct se_cmd
*cmd
,
222 unsigned char *dst
, struct list_head
*se_mem_list
);
223 static void transport_release_fe_cmd(struct se_cmd
*cmd
);
224 static void transport_remove_cmd_from_queue(struct se_cmd
*cmd
,
225 struct se_queue_obj
*qobj
);
226 static int transport_set_sense_codes(struct se_cmd
*cmd
, u8 asc
, u8 ascq
);
227 static void transport_stop_all_task_timers(struct se_cmd
*cmd
);
229 int init_se_kmem_caches(void)
231 se_cmd_cache
= kmem_cache_create("se_cmd_cache",
232 sizeof(struct se_cmd
), __alignof__(struct se_cmd
), 0, NULL
);
233 if (!(se_cmd_cache
)) {
234 printk(KERN_ERR
"kmem_cache_create for struct se_cmd failed\n");
237 se_tmr_req_cache
= kmem_cache_create("se_tmr_cache",
238 sizeof(struct se_tmr_req
), __alignof__(struct se_tmr_req
),
240 if (!(se_tmr_req_cache
)) {
241 printk(KERN_ERR
"kmem_cache_create() for struct se_tmr_req"
245 se_sess_cache
= kmem_cache_create("se_sess_cache",
246 sizeof(struct se_session
), __alignof__(struct se_session
),
248 if (!(se_sess_cache
)) {
249 printk(KERN_ERR
"kmem_cache_create() for struct se_session"
253 se_ua_cache
= kmem_cache_create("se_ua_cache",
254 sizeof(struct se_ua
), __alignof__(struct se_ua
),
256 if (!(se_ua_cache
)) {
257 printk(KERN_ERR
"kmem_cache_create() for struct se_ua failed\n");
260 se_mem_cache
= kmem_cache_create("se_mem_cache",
261 sizeof(struct se_mem
), __alignof__(struct se_mem
), 0, NULL
);
262 if (!(se_mem_cache
)) {
263 printk(KERN_ERR
"kmem_cache_create() for struct se_mem failed\n");
266 t10_pr_reg_cache
= kmem_cache_create("t10_pr_reg_cache",
267 sizeof(struct t10_pr_registration
),
268 __alignof__(struct t10_pr_registration
), 0, NULL
);
269 if (!(t10_pr_reg_cache
)) {
270 printk(KERN_ERR
"kmem_cache_create() for struct t10_pr_registration"
274 t10_alua_lu_gp_cache
= kmem_cache_create("t10_alua_lu_gp_cache",
275 sizeof(struct t10_alua_lu_gp
), __alignof__(struct t10_alua_lu_gp
),
277 if (!(t10_alua_lu_gp_cache
)) {
278 printk(KERN_ERR
"kmem_cache_create() for t10_alua_lu_gp_cache"
282 t10_alua_lu_gp_mem_cache
= kmem_cache_create("t10_alua_lu_gp_mem_cache",
283 sizeof(struct t10_alua_lu_gp_member
),
284 __alignof__(struct t10_alua_lu_gp_member
), 0, NULL
);
285 if (!(t10_alua_lu_gp_mem_cache
)) {
286 printk(KERN_ERR
"kmem_cache_create() for t10_alua_lu_gp_mem_"
290 t10_alua_tg_pt_gp_cache
= kmem_cache_create("t10_alua_tg_pt_gp_cache",
291 sizeof(struct t10_alua_tg_pt_gp
),
292 __alignof__(struct t10_alua_tg_pt_gp
), 0, NULL
);
293 if (!(t10_alua_tg_pt_gp_cache
)) {
294 printk(KERN_ERR
"kmem_cache_create() for t10_alua_tg_pt_gp_"
298 t10_alua_tg_pt_gp_mem_cache
= kmem_cache_create(
299 "t10_alua_tg_pt_gp_mem_cache",
300 sizeof(struct t10_alua_tg_pt_gp_member
),
301 __alignof__(struct t10_alua_tg_pt_gp_member
),
303 if (!(t10_alua_tg_pt_gp_mem_cache
)) {
304 printk(KERN_ERR
"kmem_cache_create() for t10_alua_tg_pt_gp_"
312 kmem_cache_destroy(se_cmd_cache
);
313 if (se_tmr_req_cache
)
314 kmem_cache_destroy(se_tmr_req_cache
);
316 kmem_cache_destroy(se_sess_cache
);
318 kmem_cache_destroy(se_ua_cache
);
320 kmem_cache_destroy(se_mem_cache
);
321 if (t10_pr_reg_cache
)
322 kmem_cache_destroy(t10_pr_reg_cache
);
323 if (t10_alua_lu_gp_cache
)
324 kmem_cache_destroy(t10_alua_lu_gp_cache
);
325 if (t10_alua_lu_gp_mem_cache
)
326 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
327 if (t10_alua_tg_pt_gp_cache
)
328 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
329 if (t10_alua_tg_pt_gp_mem_cache
)
330 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
334 void release_se_kmem_caches(void)
336 kmem_cache_destroy(se_cmd_cache
);
337 kmem_cache_destroy(se_tmr_req_cache
);
338 kmem_cache_destroy(se_sess_cache
);
339 kmem_cache_destroy(se_ua_cache
);
340 kmem_cache_destroy(se_mem_cache
);
341 kmem_cache_destroy(t10_pr_reg_cache
);
342 kmem_cache_destroy(t10_alua_lu_gp_cache
);
343 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
344 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
345 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
348 /* This code ensures unique mib indexes are handed out. */
349 static DEFINE_SPINLOCK(scsi_mib_index_lock
);
350 static u32 scsi_mib_index
[SCSI_INDEX_TYPE_MAX
];
353 * Allocate a new row index for the entry type specified
355 u32
scsi_get_new_index(scsi_index_t type
)
359 BUG_ON((type
< 0) || (type
>= SCSI_INDEX_TYPE_MAX
));
361 spin_lock(&scsi_mib_index_lock
);
362 new_index
= ++scsi_mib_index
[type
];
363 spin_unlock(&scsi_mib_index_lock
);
368 void transport_init_queue_obj(struct se_queue_obj
*qobj
)
370 atomic_set(&qobj
->queue_cnt
, 0);
371 INIT_LIST_HEAD(&qobj
->qobj_list
);
372 init_waitqueue_head(&qobj
->thread_wq
);
373 spin_lock_init(&qobj
->cmd_queue_lock
);
375 EXPORT_SYMBOL(transport_init_queue_obj
);
377 static int transport_subsystem_reqmods(void)
381 ret
= request_module("target_core_iblock");
383 printk(KERN_ERR
"Unable to load target_core_iblock\n");
385 ret
= request_module("target_core_file");
387 printk(KERN_ERR
"Unable to load target_core_file\n");
389 ret
= request_module("target_core_pscsi");
391 printk(KERN_ERR
"Unable to load target_core_pscsi\n");
393 ret
= request_module("target_core_stgt");
395 printk(KERN_ERR
"Unable to load target_core_stgt\n");
400 int transport_subsystem_check_init(void)
404 if (sub_api_initialized
)
407 * Request the loading of known TCM subsystem plugins..
409 ret
= transport_subsystem_reqmods();
413 sub_api_initialized
= 1;
417 struct se_session
*transport_init_session(void)
419 struct se_session
*se_sess
;
421 se_sess
= kmem_cache_zalloc(se_sess_cache
, GFP_KERNEL
);
423 printk(KERN_ERR
"Unable to allocate struct se_session from"
425 return ERR_PTR(-ENOMEM
);
427 INIT_LIST_HEAD(&se_sess
->sess_list
);
428 INIT_LIST_HEAD(&se_sess
->sess_acl_list
);
432 EXPORT_SYMBOL(transport_init_session
);
435 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
437 void __transport_register_session(
438 struct se_portal_group
*se_tpg
,
439 struct se_node_acl
*se_nacl
,
440 struct se_session
*se_sess
,
441 void *fabric_sess_ptr
)
443 unsigned char buf
[PR_REG_ISID_LEN
];
445 se_sess
->se_tpg
= se_tpg
;
446 se_sess
->fabric_sess_ptr
= fabric_sess_ptr
;
448 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
450 * Only set for struct se_session's that will actually be moving I/O.
451 * eg: *NOT* discovery sessions.
455 * If the fabric module supports an ISID based TransportID,
456 * save this value in binary from the fabric I_T Nexus now.
458 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
459 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
460 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
,
461 &buf
[0], PR_REG_ISID_LEN
);
462 se_sess
->sess_bin_isid
= get_unaligned_be64(&buf
[0]);
464 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
466 * The se_nacl->nacl_sess pointer will be set to the
467 * last active I_T Nexus for each struct se_node_acl.
469 se_nacl
->nacl_sess
= se_sess
;
471 list_add_tail(&se_sess
->sess_acl_list
,
472 &se_nacl
->acl_sess_list
);
473 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
475 list_add_tail(&se_sess
->sess_list
, &se_tpg
->tpg_sess_list
);
477 printk(KERN_INFO
"TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
478 se_tpg
->se_tpg_tfo
->get_fabric_name(), se_sess
->fabric_sess_ptr
);
480 EXPORT_SYMBOL(__transport_register_session
);
482 void transport_register_session(
483 struct se_portal_group
*se_tpg
,
484 struct se_node_acl
*se_nacl
,
485 struct se_session
*se_sess
,
486 void *fabric_sess_ptr
)
488 spin_lock_bh(&se_tpg
->session_lock
);
489 __transport_register_session(se_tpg
, se_nacl
, se_sess
, fabric_sess_ptr
);
490 spin_unlock_bh(&se_tpg
->session_lock
);
492 EXPORT_SYMBOL(transport_register_session
);
494 void transport_deregister_session_configfs(struct se_session
*se_sess
)
496 struct se_node_acl
*se_nacl
;
499 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
501 se_nacl
= se_sess
->se_node_acl
;
503 spin_lock_irqsave(&se_nacl
->nacl_sess_lock
, flags
);
504 list_del(&se_sess
->sess_acl_list
);
506 * If the session list is empty, then clear the pointer.
507 * Otherwise, set the struct se_session pointer from the tail
508 * element of the per struct se_node_acl active session list.
510 if (list_empty(&se_nacl
->acl_sess_list
))
511 se_nacl
->nacl_sess
= NULL
;
513 se_nacl
->nacl_sess
= container_of(
514 se_nacl
->acl_sess_list
.prev
,
515 struct se_session
, sess_acl_list
);
517 spin_unlock_irqrestore(&se_nacl
->nacl_sess_lock
, flags
);
520 EXPORT_SYMBOL(transport_deregister_session_configfs
);
522 void transport_free_session(struct se_session
*se_sess
)
524 kmem_cache_free(se_sess_cache
, se_sess
);
526 EXPORT_SYMBOL(transport_free_session
);
528 void transport_deregister_session(struct se_session
*se_sess
)
530 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
531 struct se_node_acl
*se_nacl
;
534 transport_free_session(se_sess
);
538 spin_lock_bh(&se_tpg
->session_lock
);
539 list_del(&se_sess
->sess_list
);
540 se_sess
->se_tpg
= NULL
;
541 se_sess
->fabric_sess_ptr
= NULL
;
542 spin_unlock_bh(&se_tpg
->session_lock
);
545 * Determine if we need to do extra work for this initiator node's
546 * struct se_node_acl if it had been previously dynamically generated.
548 se_nacl
= se_sess
->se_node_acl
;
550 spin_lock_bh(&se_tpg
->acl_node_lock
);
551 if (se_nacl
->dynamic_node_acl
) {
552 if (!(se_tpg
->se_tpg_tfo
->tpg_check_demo_mode_cache(
554 list_del(&se_nacl
->acl_list
);
555 se_tpg
->num_node_acls
--;
556 spin_unlock_bh(&se_tpg
->acl_node_lock
);
558 core_tpg_wait_for_nacl_pr_ref(se_nacl
);
559 core_free_device_list_for_node(se_nacl
, se_tpg
);
560 se_tpg
->se_tpg_tfo
->tpg_release_fabric_acl(se_tpg
,
562 spin_lock_bh(&se_tpg
->acl_node_lock
);
565 spin_unlock_bh(&se_tpg
->acl_node_lock
);
568 transport_free_session(se_sess
);
570 printk(KERN_INFO
"TARGET_CORE[%s]: Deregistered fabric_sess\n",
571 se_tpg
->se_tpg_tfo
->get_fabric_name());
573 EXPORT_SYMBOL(transport_deregister_session
);
576 * Called with cmd->t_task.t_state_lock held.
578 static void transport_all_task_dev_remove_state(struct se_cmd
*cmd
)
580 struct se_device
*dev
;
581 struct se_task
*task
;
584 list_for_each_entry(task
, &cmd
->t_task
.t_task_list
, t_list
) {
589 if (atomic_read(&task
->task_active
))
592 if (!(atomic_read(&task
->task_state_active
)))
595 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
596 list_del(&task
->t_state_list
);
597 DEBUG_TSTATE("Removed ITT: 0x%08x dev: %p task[%p]\n",
598 cmd
->se_tfo
->tfo_get_task_tag(cmd
), dev
, task
);
599 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
601 atomic_set(&task
->task_state_active
, 0);
602 atomic_dec(&cmd
->t_task
.t_task_cdbs_ex_left
);
606 /* transport_cmd_check_stop():
608 * 'transport_off = 1' determines if t_transport_active should be cleared.
609 * 'transport_off = 2' determines if task_dev_state should be removed.
611 * A non-zero u8 t_state sets cmd->t_state.
612 * Returns 1 when command is stopped, else 0.
614 static int transport_cmd_check_stop(
621 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
623 * Determine if IOCTL context caller in requesting the stopping of this
624 * command for LUN shutdown purposes.
626 if (atomic_read(&cmd
->t_task
.transport_lun_stop
)) {
627 DEBUG_CS("%s:%d atomic_read(&cmd->t_task.transport_lun_stop)"
628 " == TRUE for ITT: 0x%08x\n", __func__
, __LINE__
,
629 cmd
->se_tfo
->get_task_tag(cmd
));
631 cmd
->deferred_t_state
= cmd
->t_state
;
632 cmd
->t_state
= TRANSPORT_DEFERRED_CMD
;
633 atomic_set(&cmd
->t_task
.t_transport_active
, 0);
634 if (transport_off
== 2)
635 transport_all_task_dev_remove_state(cmd
);
636 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
638 complete(&cmd
->t_task
.transport_lun_stop_comp
);
642 * Determine if frontend context caller is requesting the stopping of
643 * this command for frontend exceptions.
645 if (atomic_read(&cmd
->t_task
.t_transport_stop
)) {
646 DEBUG_CS("%s:%d atomic_read(&cmd->t_task.t_transport_stop) =="
647 " TRUE for ITT: 0x%08x\n", __func__
, __LINE__
,
648 cmd
->se_tfo
->get_task_tag(cmd
));
650 cmd
->deferred_t_state
= cmd
->t_state
;
651 cmd
->t_state
= TRANSPORT_DEFERRED_CMD
;
652 if (transport_off
== 2)
653 transport_all_task_dev_remove_state(cmd
);
656 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
659 if (transport_off
== 2)
661 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
663 complete(&cmd
->t_task
.t_transport_stop_comp
);
667 atomic_set(&cmd
->t_task
.t_transport_active
, 0);
668 if (transport_off
== 2) {
669 transport_all_task_dev_remove_state(cmd
);
671 * Clear struct se_cmd->se_lun before the transport_off == 2
672 * handoff to fabric module.
676 * Some fabric modules like tcm_loop can release
677 * their internally allocated I/O reference now and
680 if (cmd
->se_tfo
->check_stop_free
!= NULL
) {
681 spin_unlock_irqrestore(
682 &cmd
->t_task
.t_state_lock
, flags
);
684 cmd
->se_tfo
->check_stop_free(cmd
);
688 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
692 cmd
->t_state
= t_state
;
693 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
698 static int transport_cmd_check_stop_to_fabric(struct se_cmd
*cmd
)
700 return transport_cmd_check_stop(cmd
, 2, 0);
703 static void transport_lun_remove_cmd(struct se_cmd
*cmd
)
705 struct se_lun
*lun
= cmd
->se_lun
;
711 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
712 if (!(atomic_read(&cmd
->t_task
.transport_dev_active
))) {
713 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
716 atomic_set(&cmd
->t_task
.transport_dev_active
, 0);
717 transport_all_task_dev_remove_state(cmd
);
718 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
722 spin_lock_irqsave(&lun
->lun_cmd_lock
, flags
);
723 if (atomic_read(&cmd
->t_task
.transport_lun_active
)) {
724 list_del(&cmd
->se_lun_node
);
725 atomic_set(&cmd
->t_task
.transport_lun_active
, 0);
727 printk(KERN_INFO
"Removed ITT: 0x%08x from LUN LIST[%d]\n"
728 cmd
->se_tfo
->get_task_tag(cmd
), lun
->unpacked_lun
);
731 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, flags
);
734 void transport_cmd_finish_abort(struct se_cmd
*cmd
, int remove
)
736 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
737 transport_lun_remove_cmd(cmd
);
739 if (transport_cmd_check_stop_to_fabric(cmd
))
742 transport_generic_remove(cmd
, 0, 0);
745 void transport_cmd_finish_abort_tmr(struct se_cmd
*cmd
)
747 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
749 if (transport_cmd_check_stop_to_fabric(cmd
))
752 transport_generic_remove(cmd
, 0, 0);
755 static void transport_add_cmd_to_queue(
759 struct se_device
*dev
= cmd
->se_dev
;
760 struct se_queue_obj
*qobj
= &dev
->dev_queue_obj
;
763 INIT_LIST_HEAD(&cmd
->se_queue_node
);
766 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
767 cmd
->t_state
= t_state
;
768 atomic_set(&cmd
->t_task
.t_transport_active
, 1);
769 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
772 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
773 list_add_tail(&cmd
->se_queue_node
, &qobj
->qobj_list
);
774 atomic_inc(&cmd
->t_task
.t_transport_queue_active
);
775 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
777 atomic_inc(&qobj
->queue_cnt
);
778 wake_up_interruptible(&qobj
->thread_wq
);
781 static struct se_cmd
*
782 transport_get_cmd_from_queue(struct se_queue_obj
*qobj
)
787 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
788 if (list_empty(&qobj
->qobj_list
)) {
789 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
792 cmd
= list_first_entry(&qobj
->qobj_list
, struct se_cmd
, se_queue_node
);
794 atomic_dec(&cmd
->t_task
.t_transport_queue_active
);
796 list_del(&cmd
->se_queue_node
);
797 atomic_dec(&qobj
->queue_cnt
);
798 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
803 static void transport_remove_cmd_from_queue(struct se_cmd
*cmd
,
804 struct se_queue_obj
*qobj
)
809 spin_lock_irqsave(&qobj
->cmd_queue_lock
, flags
);
810 if (!(atomic_read(&cmd
->t_task
.t_transport_queue_active
))) {
811 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
815 list_for_each_entry(t
, &qobj
->qobj_list
, se_queue_node
)
817 atomic_dec(&cmd
->t_task
.t_transport_queue_active
);
818 atomic_dec(&qobj
->queue_cnt
);
819 list_del(&cmd
->se_queue_node
);
822 spin_unlock_irqrestore(&qobj
->cmd_queue_lock
, flags
);
824 if (atomic_read(&cmd
->t_task
.t_transport_queue_active
)) {
825 printk(KERN_ERR
"ITT: 0x%08x t_transport_queue_active: %d\n",
826 cmd
->se_tfo
->get_task_tag(cmd
),
827 atomic_read(&cmd
->t_task
.t_transport_queue_active
));
832 * Completion function used by TCM subsystem plugins (such as FILEIO)
833 * for queueing up response from struct se_subsystem_api->do_task()
835 void transport_complete_sync_cache(struct se_cmd
*cmd
, int good
)
837 struct se_task
*task
= list_entry(cmd
->t_task
.t_task_list
.next
,
838 struct se_task
, t_list
);
841 cmd
->scsi_status
= SAM_STAT_GOOD
;
842 task
->task_scsi_status
= GOOD
;
844 task
->task_scsi_status
= SAM_STAT_CHECK_CONDITION
;
845 task
->task_error_status
= PYX_TRANSPORT_ILLEGAL_REQUEST
;
846 task
->task_se_cmd
->transport_error_status
=
847 PYX_TRANSPORT_ILLEGAL_REQUEST
;
850 transport_complete_task(task
, good
);
852 EXPORT_SYMBOL(transport_complete_sync_cache
);
854 /* transport_complete_task():
856 * Called from interrupt and non interrupt context depending
857 * on the transport plugin.
859 void transport_complete_task(struct se_task
*task
, int success
)
861 struct se_cmd
*cmd
= task
->task_se_cmd
;
862 struct se_device
*dev
= task
->se_dev
;
866 printk(KERN_INFO
"task: %p CDB: 0x%02x obj_ptr: %p\n", task
,
867 cmd
->t_task
.t_task_cdb
[0], dev
);
870 atomic_inc(&dev
->depth_left
);
872 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
873 atomic_set(&task
->task_active
, 0);
876 * See if any sense data exists, if so set the TASK_SENSE flag.
877 * Also check for any other post completion work that needs to be
878 * done by the plugins.
880 if (dev
&& dev
->transport
->transport_complete
) {
881 if (dev
->transport
->transport_complete(task
) != 0) {
882 cmd
->se_cmd_flags
|= SCF_TRANSPORT_TASK_SENSE
;
883 task
->task_sense
= 1;
889 * See if we are waiting for outstanding struct se_task
890 * to complete for an exception condition
892 if (atomic_read(&task
->task_stop
)) {
894 * Decrement cmd->t_task.t_se_count if this task had
895 * previously thrown its timeout exception handler.
897 if (atomic_read(&task
->task_timeout
)) {
898 atomic_dec(&cmd
->t_task
.t_se_count
);
899 atomic_set(&task
->task_timeout
, 0);
901 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
903 complete(&task
->task_stop_comp
);
907 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
908 * left counter to determine when the struct se_cmd is ready to be queued to
909 * the processing thread.
911 if (atomic_read(&task
->task_timeout
)) {
912 if (!(atomic_dec_and_test(
913 &cmd
->t_task
.t_task_cdbs_timeout_left
))) {
914 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
,
918 t_state
= TRANSPORT_COMPLETE_TIMEOUT
;
919 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
921 transport_add_cmd_to_queue(cmd
, t_state
);
924 atomic_dec(&cmd
->t_task
.t_task_cdbs_timeout_left
);
927 * Decrement the outstanding t_task_cdbs_left count. The last
928 * struct se_task from struct se_cmd will complete itself into the
929 * device queue depending upon int success.
931 if (!(atomic_dec_and_test(&cmd
->t_task
.t_task_cdbs_left
))) {
933 cmd
->t_task
.t_tasks_failed
= 1;
935 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
939 if (!success
|| cmd
->t_task
.t_tasks_failed
) {
940 t_state
= TRANSPORT_COMPLETE_FAILURE
;
941 if (!task
->task_error_status
) {
942 task
->task_error_status
=
943 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
944 cmd
->transport_error_status
=
945 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
;
948 atomic_set(&cmd
->t_task
.t_transport_complete
, 1);
949 t_state
= TRANSPORT_COMPLETE_OK
;
951 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
953 transport_add_cmd_to_queue(cmd
, t_state
);
955 EXPORT_SYMBOL(transport_complete_task
);
958 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
959 * struct se_task list are ready to be added to the active execution list
962 * Called with se_dev_t->execute_task_lock called.
964 static inline int transport_add_task_check_sam_attr(
965 struct se_task
*task
,
966 struct se_task
*task_prev
,
967 struct se_device
*dev
)
970 * No SAM Task attribute emulation enabled, add to tail of
973 if (dev
->dev_task_attr_type
!= SAM_TASK_ATTR_EMULATED
) {
974 list_add_tail(&task
->t_execute_list
, &dev
->execute_task_list
);
978 * HEAD_OF_QUEUE attribute for received CDB, which means
979 * the first task that is associated with a struct se_cmd goes to
980 * head of the struct se_device->execute_task_list, and task_prev
981 * after that for each subsequent task
983 if (task
->task_se_cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
984 list_add(&task
->t_execute_list
,
985 (task_prev
!= NULL
) ?
986 &task_prev
->t_execute_list
:
987 &dev
->execute_task_list
);
989 DEBUG_STA("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
990 " in execution queue\n",
991 T_TASK(task
->task_se_cmd
)->t_task_cdb
[0]);
995 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
996 * transitioned from Dermant -> Active state, and are added to the end
997 * of the struct se_device->execute_task_list
999 list_add_tail(&task
->t_execute_list
, &dev
->execute_task_list
);
1003 /* __transport_add_task_to_execute_queue():
1005 * Called with se_dev_t->execute_task_lock called.
1007 static void __transport_add_task_to_execute_queue(
1008 struct se_task
*task
,
1009 struct se_task
*task_prev
,
1010 struct se_device
*dev
)
1014 head_of_queue
= transport_add_task_check_sam_attr(task
, task_prev
, dev
);
1015 atomic_inc(&dev
->execute_tasks
);
1017 if (atomic_read(&task
->task_state_active
))
1020 * Determine if this task needs to go to HEAD_OF_QUEUE for the
1021 * state list as well. Running with SAM Task Attribute emulation
1022 * will always return head_of_queue == 0 here
1025 list_add(&task
->t_state_list
, (task_prev
) ?
1026 &task_prev
->t_state_list
:
1027 &dev
->state_task_list
);
1029 list_add_tail(&task
->t_state_list
, &dev
->state_task_list
);
1031 atomic_set(&task
->task_state_active
, 1);
1033 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1034 task
->task_se_cmd
->se_tfo
->get_task_tag(task
->task_se_cmd
),
1038 static void transport_add_tasks_to_state_queue(struct se_cmd
*cmd
)
1040 struct se_device
*dev
;
1041 struct se_task
*task
;
1042 unsigned long flags
;
1044 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
1045 list_for_each_entry(task
, &cmd
->t_task
.t_task_list
, t_list
) {
1048 if (atomic_read(&task
->task_state_active
))
1051 spin_lock(&dev
->execute_task_lock
);
1052 list_add_tail(&task
->t_state_list
, &dev
->state_task_list
);
1053 atomic_set(&task
->task_state_active
, 1);
1055 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1056 task
->se_cmd
->se_tfo
->get_task_tag(
1057 task
->task_se_cmd
), task
, dev
);
1059 spin_unlock(&dev
->execute_task_lock
);
1061 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
1064 static void transport_add_tasks_from_cmd(struct se_cmd
*cmd
)
1066 struct se_device
*dev
= cmd
->se_dev
;
1067 struct se_task
*task
, *task_prev
= NULL
;
1068 unsigned long flags
;
1070 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
1071 list_for_each_entry(task
, &cmd
->t_task
.t_task_list
, t_list
) {
1072 if (atomic_read(&task
->task_execute_queue
))
1075 * __transport_add_task_to_execute_queue() handles the
1076 * SAM Task Attribute emulation if enabled
1078 __transport_add_task_to_execute_queue(task
, task_prev
, dev
);
1079 atomic_set(&task
->task_execute_queue
, 1);
1082 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
1085 /* transport_remove_task_from_execute_queue():
1089 void transport_remove_task_from_execute_queue(
1090 struct se_task
*task
,
1091 struct se_device
*dev
)
1093 unsigned long flags
;
1095 if (atomic_read(&task
->task_execute_queue
) == 0) {
1100 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
1101 list_del(&task
->t_execute_list
);
1102 atomic_set(&task
->task_execute_queue
, 0);
1103 atomic_dec(&dev
->execute_tasks
);
1104 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
1107 unsigned char *transport_dump_cmd_direction(struct se_cmd
*cmd
)
1109 switch (cmd
->data_direction
) {
1112 case DMA_FROM_DEVICE
:
1116 case DMA_BIDIRECTIONAL
:
1125 void transport_dump_dev_state(
1126 struct se_device
*dev
,
1130 *bl
+= sprintf(b
+ *bl
, "Status: ");
1131 switch (dev
->dev_status
) {
1132 case TRANSPORT_DEVICE_ACTIVATED
:
1133 *bl
+= sprintf(b
+ *bl
, "ACTIVATED");
1135 case TRANSPORT_DEVICE_DEACTIVATED
:
1136 *bl
+= sprintf(b
+ *bl
, "DEACTIVATED");
1138 case TRANSPORT_DEVICE_SHUTDOWN
:
1139 *bl
+= sprintf(b
+ *bl
, "SHUTDOWN");
1141 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED
:
1142 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED
:
1143 *bl
+= sprintf(b
+ *bl
, "OFFLINE");
1146 *bl
+= sprintf(b
+ *bl
, "UNKNOWN=%d", dev
->dev_status
);
1150 *bl
+= sprintf(b
+ *bl
, " Execute/Left/Max Queue Depth: %d/%d/%d",
1151 atomic_read(&dev
->execute_tasks
), atomic_read(&dev
->depth_left
),
1153 *bl
+= sprintf(b
+ *bl
, " SectorSize: %u MaxSectors: %u\n",
1154 dev
->se_sub_dev
->se_dev_attrib
.block_size
, dev
->se_sub_dev
->se_dev_attrib
.max_sectors
);
1155 *bl
+= sprintf(b
+ *bl
, " ");
1158 /* transport_release_all_cmds():
1162 static void transport_release_all_cmds(struct se_device
*dev
)
1164 struct se_cmd
*cmd
, *tcmd
;
1165 int bug_out
= 0, t_state
;
1166 unsigned long flags
;
1168 spin_lock_irqsave(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1169 list_for_each_entry_safe(cmd
, tcmd
, &dev
->dev_queue_obj
.qobj_list
,
1171 t_state
= cmd
->t_state
;
1172 list_del(&cmd
->se_queue_node
);
1173 spin_unlock_irqrestore(&dev
->dev_queue_obj
.cmd_queue_lock
,
1176 printk(KERN_ERR
"Releasing ITT: 0x%08x, i_state: %u,"
1177 " t_state: %u directly\n",
1178 cmd
->se_tfo
->get_task_tag(cmd
),
1179 cmd
->se_tfo
->get_cmd_state(cmd
), t_state
);
1181 transport_release_fe_cmd(cmd
);
1184 spin_lock_irqsave(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1186 spin_unlock_irqrestore(&dev
->dev_queue_obj
.cmd_queue_lock
, flags
);
1193 void transport_dump_vpd_proto_id(
1194 struct t10_vpd
*vpd
,
1195 unsigned char *p_buf
,
1198 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1201 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1202 len
= sprintf(buf
, "T10 VPD Protocol Identifier: ");
1204 switch (vpd
->protocol_identifier
) {
1206 sprintf(buf
+len
, "Fibre Channel\n");
1209 sprintf(buf
+len
, "Parallel SCSI\n");
1212 sprintf(buf
+len
, "SSA\n");
1215 sprintf(buf
+len
, "IEEE 1394\n");
1218 sprintf(buf
+len
, "SCSI Remote Direct Memory Access"
1222 sprintf(buf
+len
, "Internet SCSI (iSCSI)\n");
1225 sprintf(buf
+len
, "SAS Serial SCSI Protocol\n");
1228 sprintf(buf
+len
, "Automation/Drive Interface Transport"
1232 sprintf(buf
+len
, "AT Attachment Interface ATA/ATAPI\n");
1235 sprintf(buf
+len
, "Unknown 0x%02x\n",
1236 vpd
->protocol_identifier
);
1241 strncpy(p_buf
, buf
, p_buf_len
);
1243 printk(KERN_INFO
"%s", buf
);
1247 transport_set_vpd_proto_id(struct t10_vpd
*vpd
, unsigned char *page_83
)
1250 * Check if the Protocol Identifier Valid (PIV) bit is set..
1252 * from spc3r23.pdf section 7.5.1
1254 if (page_83
[1] & 0x80) {
1255 vpd
->protocol_identifier
= (page_83
[0] & 0xf0);
1256 vpd
->protocol_identifier_set
= 1;
1257 transport_dump_vpd_proto_id(vpd
, NULL
, 0);
1260 EXPORT_SYMBOL(transport_set_vpd_proto_id
);
1262 int transport_dump_vpd_assoc(
1263 struct t10_vpd
*vpd
,
1264 unsigned char *p_buf
,
1267 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1271 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1272 len
= sprintf(buf
, "T10 VPD Identifier Association: ");
1274 switch (vpd
->association
) {
1276 sprintf(buf
+len
, "addressed logical unit\n");
1279 sprintf(buf
+len
, "target port\n");
1282 sprintf(buf
+len
, "SCSI target device\n");
1285 sprintf(buf
+len
, "Unknown 0x%02x\n", vpd
->association
);
1291 strncpy(p_buf
, buf
, p_buf_len
);
1298 int transport_set_vpd_assoc(struct t10_vpd
*vpd
, unsigned char *page_83
)
1301 * The VPD identification association..
1303 * from spc3r23.pdf Section 7.6.3.1 Table 297
1305 vpd
->association
= (page_83
[1] & 0x30);
1306 return transport_dump_vpd_assoc(vpd
, NULL
, 0);
1308 EXPORT_SYMBOL(transport_set_vpd_assoc
);
1310 int transport_dump_vpd_ident_type(
1311 struct t10_vpd
*vpd
,
1312 unsigned char *p_buf
,
1315 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1319 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1320 len
= sprintf(buf
, "T10 VPD Identifier Type: ");
1322 switch (vpd
->device_identifier_type
) {
1324 sprintf(buf
+len
, "Vendor specific\n");
1327 sprintf(buf
+len
, "T10 Vendor ID based\n");
1330 sprintf(buf
+len
, "EUI-64 based\n");
1333 sprintf(buf
+len
, "NAA\n");
1336 sprintf(buf
+len
, "Relative target port identifier\n");
1339 sprintf(buf
+len
, "SCSI name string\n");
1342 sprintf(buf
+len
, "Unsupported: 0x%02x\n",
1343 vpd
->device_identifier_type
);
1349 if (p_buf_len
< strlen(buf
)+1)
1351 strncpy(p_buf
, buf
, p_buf_len
);
1359 int transport_set_vpd_ident_type(struct t10_vpd
*vpd
, unsigned char *page_83
)
1362 * The VPD identifier type..
1364 * from spc3r23.pdf Section 7.6.3.1 Table 298
1366 vpd
->device_identifier_type
= (page_83
[1] & 0x0f);
1367 return transport_dump_vpd_ident_type(vpd
, NULL
, 0);
1369 EXPORT_SYMBOL(transport_set_vpd_ident_type
);
1371 int transport_dump_vpd_ident(
1372 struct t10_vpd
*vpd
,
1373 unsigned char *p_buf
,
1376 unsigned char buf
[VPD_TMP_BUF_SIZE
];
1379 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
1381 switch (vpd
->device_identifier_code_set
) {
1382 case 0x01: /* Binary */
1383 sprintf(buf
, "T10 VPD Binary Device Identifier: %s\n",
1384 &vpd
->device_identifier
[0]);
1386 case 0x02: /* ASCII */
1387 sprintf(buf
, "T10 VPD ASCII Device Identifier: %s\n",
1388 &vpd
->device_identifier
[0]);
1390 case 0x03: /* UTF-8 */
1391 sprintf(buf
, "T10 VPD UTF-8 Device Identifier: %s\n",
1392 &vpd
->device_identifier
[0]);
1395 sprintf(buf
, "T10 VPD Device Identifier encoding unsupported:"
1396 " 0x%02x", vpd
->device_identifier_code_set
);
1402 strncpy(p_buf
, buf
, p_buf_len
);
1410 transport_set_vpd_ident(struct t10_vpd
*vpd
, unsigned char *page_83
)
1412 static const char hex_str
[] = "0123456789abcdef";
1413 int j
= 0, i
= 4; /* offset to start of the identifer */
1416 * The VPD Code Set (encoding)
1418 * from spc3r23.pdf Section 7.6.3.1 Table 296
1420 vpd
->device_identifier_code_set
= (page_83
[0] & 0x0f);
1421 switch (vpd
->device_identifier_code_set
) {
1422 case 0x01: /* Binary */
1423 vpd
->device_identifier
[j
++] =
1424 hex_str
[vpd
->device_identifier_type
];
1425 while (i
< (4 + page_83
[3])) {
1426 vpd
->device_identifier
[j
++] =
1427 hex_str
[(page_83
[i
] & 0xf0) >> 4];
1428 vpd
->device_identifier
[j
++] =
1429 hex_str
[page_83
[i
] & 0x0f];
1433 case 0x02: /* ASCII */
1434 case 0x03: /* UTF-8 */
1435 while (i
< (4 + page_83
[3]))
1436 vpd
->device_identifier
[j
++] = page_83
[i
++];
1442 return transport_dump_vpd_ident(vpd
, NULL
, 0);
1444 EXPORT_SYMBOL(transport_set_vpd_ident
);
1446 static void core_setup_task_attr_emulation(struct se_device
*dev
)
1449 * If this device is from Target_Core_Mod/pSCSI, disable the
1450 * SAM Task Attribute emulation.
1452 * This is currently not available in upsream Linux/SCSI Target
1453 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1455 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
) {
1456 dev
->dev_task_attr_type
= SAM_TASK_ATTR_PASSTHROUGH
;
1460 dev
->dev_task_attr_type
= SAM_TASK_ATTR_EMULATED
;
1461 DEBUG_STA("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1462 " device\n", dev
->transport
->name
,
1463 dev
->transport
->get_device_rev(dev
));
1466 static void scsi_dump_inquiry(struct se_device
*dev
)
1468 struct t10_wwn
*wwn
= &dev
->se_sub_dev
->t10_wwn
;
1471 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1473 printk(" Vendor: ");
1474 for (i
= 0; i
< 8; i
++)
1475 if (wwn
->vendor
[i
] >= 0x20)
1476 printk("%c", wwn
->vendor
[i
]);
1481 for (i
= 0; i
< 16; i
++)
1482 if (wwn
->model
[i
] >= 0x20)
1483 printk("%c", wwn
->model
[i
]);
1487 printk(" Revision: ");
1488 for (i
= 0; i
< 4; i
++)
1489 if (wwn
->revision
[i
] >= 0x20)
1490 printk("%c", wwn
->revision
[i
]);
1496 device_type
= dev
->transport
->get_device_type(dev
);
1497 printk(" Type: %s ", scsi_device_type(device_type
));
1498 printk(" ANSI SCSI revision: %02x\n",
1499 dev
->transport
->get_device_rev(dev
));
1502 struct se_device
*transport_add_device_to_core_hba(
1504 struct se_subsystem_api
*transport
,
1505 struct se_subsystem_dev
*se_dev
,
1507 void *transport_dev
,
1508 struct se_dev_limits
*dev_limits
,
1509 const char *inquiry_prod
,
1510 const char *inquiry_rev
)
1513 struct se_device
*dev
;
1515 dev
= kzalloc(sizeof(struct se_device
), GFP_KERNEL
);
1517 printk(KERN_ERR
"Unable to allocate memory for se_dev_t\n");
1521 transport_init_queue_obj(&dev
->dev_queue_obj
);
1522 dev
->dev_flags
= device_flags
;
1523 dev
->dev_status
|= TRANSPORT_DEVICE_DEACTIVATED
;
1524 dev
->dev_ptr
= transport_dev
;
1526 dev
->se_sub_dev
= se_dev
;
1527 dev
->transport
= transport
;
1528 atomic_set(&dev
->active_cmds
, 0);
1529 INIT_LIST_HEAD(&dev
->dev_list
);
1530 INIT_LIST_HEAD(&dev
->dev_sep_list
);
1531 INIT_LIST_HEAD(&dev
->dev_tmr_list
);
1532 INIT_LIST_HEAD(&dev
->execute_task_list
);
1533 INIT_LIST_HEAD(&dev
->delayed_cmd_list
);
1534 INIT_LIST_HEAD(&dev
->ordered_cmd_list
);
1535 INIT_LIST_HEAD(&dev
->state_task_list
);
1536 spin_lock_init(&dev
->execute_task_lock
);
1537 spin_lock_init(&dev
->delayed_cmd_lock
);
1538 spin_lock_init(&dev
->ordered_cmd_lock
);
1539 spin_lock_init(&dev
->state_task_lock
);
1540 spin_lock_init(&dev
->dev_alua_lock
);
1541 spin_lock_init(&dev
->dev_reservation_lock
);
1542 spin_lock_init(&dev
->dev_status_lock
);
1543 spin_lock_init(&dev
->dev_status_thr_lock
);
1544 spin_lock_init(&dev
->se_port_lock
);
1545 spin_lock_init(&dev
->se_tmr_lock
);
1547 dev
->queue_depth
= dev_limits
->queue_depth
;
1548 atomic_set(&dev
->depth_left
, dev
->queue_depth
);
1549 atomic_set(&dev
->dev_ordered_id
, 0);
1551 se_dev_set_default_attribs(dev
, dev_limits
);
1553 dev
->dev_index
= scsi_get_new_index(SCSI_DEVICE_INDEX
);
1554 dev
->creation_time
= get_jiffies_64();
1555 spin_lock_init(&dev
->stats_lock
);
1557 spin_lock(&hba
->device_lock
);
1558 list_add_tail(&dev
->dev_list
, &hba
->hba_dev_list
);
1560 spin_unlock(&hba
->device_lock
);
1562 * Setup the SAM Task Attribute emulation for struct se_device
1564 core_setup_task_attr_emulation(dev
);
1566 * Force PR and ALUA passthrough emulation with internal object use.
1568 force_pt
= (hba
->hba_flags
& HBA_FLAGS_INTERNAL_USE
);
1570 * Setup the Reservations infrastructure for struct se_device
1572 core_setup_reservations(dev
, force_pt
);
1574 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1576 if (core_setup_alua(dev
, force_pt
) < 0)
1580 * Startup the struct se_device processing thread
1582 dev
->process_thread
= kthread_run(transport_processing_thread
, dev
,
1583 "LIO_%s", dev
->transport
->name
);
1584 if (IS_ERR(dev
->process_thread
)) {
1585 printk(KERN_ERR
"Unable to create kthread: LIO_%s\n",
1586 dev
->transport
->name
);
1591 * Preload the initial INQUIRY const values if we are doing
1592 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1593 * passthrough because this is being provided by the backend LLD.
1594 * This is required so that transport_get_inquiry() copies these
1595 * originals once back into DEV_T10_WWN(dev) for the virtual device
1598 if (dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
) {
1599 if (!inquiry_prod
|| !inquiry_rev
) {
1600 printk(KERN_ERR
"All non TCM/pSCSI plugins require"
1601 " INQUIRY consts\n");
1605 strncpy(&dev
->se_sub_dev
->t10_wwn
.vendor
[0], "LIO-ORG", 8);
1606 strncpy(&dev
->se_sub_dev
->t10_wwn
.model
[0], inquiry_prod
, 16);
1607 strncpy(&dev
->se_sub_dev
->t10_wwn
.revision
[0], inquiry_rev
, 4);
1609 scsi_dump_inquiry(dev
);
1613 kthread_stop(dev
->process_thread
);
1615 spin_lock(&hba
->device_lock
);
1616 list_del(&dev
->dev_list
);
1618 spin_unlock(&hba
->device_lock
);
1620 se_release_vpd_for_dev(dev
);
1626 EXPORT_SYMBOL(transport_add_device_to_core_hba
);
1628 /* transport_generic_prepare_cdb():
1630 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1631 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1632 * The point of this is since we are mapping iSCSI LUNs to
1633 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1634 * devices and HBAs for a loop.
1636 static inline void transport_generic_prepare_cdb(
1640 case READ_10
: /* SBC - RDProtect */
1641 case READ_12
: /* SBC - RDProtect */
1642 case READ_16
: /* SBC - RDProtect */
1643 case SEND_DIAGNOSTIC
: /* SPC - SELF-TEST Code */
1644 case VERIFY
: /* SBC - VRProtect */
1645 case VERIFY_16
: /* SBC - VRProtect */
1646 case WRITE_VERIFY
: /* SBC - VRProtect */
1647 case WRITE_VERIFY_12
: /* SBC - VRProtect */
1650 cdb
[1] &= 0x1f; /* clear logical unit number */
1655 static struct se_task
*
1656 transport_generic_get_task(struct se_cmd
*cmd
,
1657 enum dma_data_direction data_direction
)
1659 struct se_task
*task
;
1660 struct se_device
*dev
= cmd
->se_dev
;
1661 unsigned long flags
;
1663 task
= dev
->transport
->alloc_task(cmd
);
1665 printk(KERN_ERR
"Unable to allocate struct se_task\n");
1669 INIT_LIST_HEAD(&task
->t_list
);
1670 INIT_LIST_HEAD(&task
->t_execute_list
);
1671 INIT_LIST_HEAD(&task
->t_state_list
);
1672 init_completion(&task
->task_stop_comp
);
1673 task
->task_no
= cmd
->t_task
.t_tasks_no
++;
1674 task
->task_se_cmd
= cmd
;
1676 task
->task_data_direction
= data_direction
;
1678 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
1679 list_add_tail(&task
->t_list
, &cmd
->t_task
.t_task_list
);
1680 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
1685 static int transport_generic_cmd_sequencer(struct se_cmd
*, unsigned char *);
1688 * Used by fabric modules containing a local struct se_cmd within their
1689 * fabric dependent per I/O descriptor.
1691 void transport_init_se_cmd(
1693 struct target_core_fabric_ops
*tfo
,
1694 struct se_session
*se_sess
,
1698 unsigned char *sense_buffer
)
1700 INIT_LIST_HEAD(&cmd
->se_lun_node
);
1701 INIT_LIST_HEAD(&cmd
->se_delayed_node
);
1702 INIT_LIST_HEAD(&cmd
->se_ordered_node
);
1704 INIT_LIST_HEAD(&cmd
->t_task
.t_mem_list
);
1705 INIT_LIST_HEAD(&cmd
->t_task
.t_mem_bidi_list
);
1706 INIT_LIST_HEAD(&cmd
->t_task
.t_task_list
);
1707 init_completion(&cmd
->t_task
.transport_lun_fe_stop_comp
);
1708 init_completion(&cmd
->t_task
.transport_lun_stop_comp
);
1709 init_completion(&cmd
->t_task
.t_transport_stop_comp
);
1710 spin_lock_init(&cmd
->t_task
.t_state_lock
);
1711 atomic_set(&cmd
->t_task
.transport_dev_active
, 1);
1714 cmd
->se_sess
= se_sess
;
1715 cmd
->data_length
= data_length
;
1716 cmd
->data_direction
= data_direction
;
1717 cmd
->sam_task_attr
= task_attr
;
1718 cmd
->sense_buffer
= sense_buffer
;
1720 EXPORT_SYMBOL(transport_init_se_cmd
);
1722 static int transport_check_alloc_task_attr(struct se_cmd
*cmd
)
1725 * Check if SAM Task Attribute emulation is enabled for this
1726 * struct se_device storage object
1728 if (cmd
->se_dev
->dev_task_attr_type
!= SAM_TASK_ATTR_EMULATED
)
1731 if (cmd
->sam_task_attr
== MSG_ACA_TAG
) {
1732 DEBUG_STA("SAM Task Attribute ACA"
1733 " emulation is not supported\n");
1737 * Used to determine when ORDERED commands should go from
1738 * Dormant to Active status.
1740 cmd
->se_ordered_id
= atomic_inc_return(&cmd
->se_dev
->dev_ordered_id
);
1741 smp_mb__after_atomic_inc();
1742 DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1743 cmd
->se_ordered_id
, cmd
->sam_task_attr
,
1744 TRANSPORT(cmd
->se_dev
)->name
);
1748 void transport_free_se_cmd(
1749 struct se_cmd
*se_cmd
)
1751 if (se_cmd
->se_tmr_req
)
1752 core_tmr_release_req(se_cmd
->se_tmr_req
);
1754 * Check and free any extended CDB buffer that was allocated
1756 if (se_cmd
->t_task
.t_task_cdb
!= se_cmd
->t_task
.__t_task_cdb
)
1757 kfree(se_cmd
->t_task
.t_task_cdb
);
1759 EXPORT_SYMBOL(transport_free_se_cmd
);
1761 static void transport_generic_wait_for_tasks(struct se_cmd
*, int, int);
1763 /* transport_generic_allocate_tasks():
1765 * Called from fabric RX Thread.
1767 int transport_generic_allocate_tasks(
1773 transport_generic_prepare_cdb(cdb
);
1776 * This is needed for early exceptions.
1778 cmd
->transport_wait_for_tasks
= &transport_generic_wait_for_tasks
;
1781 * Ensure that the received CDB is less than the max (252 + 8) bytes
1782 * for VARIABLE_LENGTH_CMD
1784 if (scsi_command_size(cdb
) > SCSI_MAX_VARLEN_CDB_SIZE
) {
1785 printk(KERN_ERR
"Received SCSI CDB with command_size: %d that"
1786 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1787 scsi_command_size(cdb
), SCSI_MAX_VARLEN_CDB_SIZE
);
1791 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1792 * allocate the additional extended CDB buffer now.. Otherwise
1793 * setup the pointer from __t_task_cdb to t_task_cdb.
1795 if (scsi_command_size(cdb
) > sizeof(cmd
->t_task
.__t_task_cdb
)) {
1796 cmd
->t_task
.t_task_cdb
= kzalloc(scsi_command_size(cdb
),
1798 if (!(cmd
->t_task
.t_task_cdb
)) {
1799 printk(KERN_ERR
"Unable to allocate cmd->t_task.t_task_cdb"
1800 " %u > sizeof(cmd->t_task.__t_task_cdb): %lu ops\n",
1801 scsi_command_size(cdb
),
1802 (unsigned long)sizeof(cmd
->t_task
.__t_task_cdb
));
1806 cmd
->t_task
.t_task_cdb
= &cmd
->t_task
.__t_task_cdb
[0];
1808 * Copy the original CDB into cmd->t_task.
1810 memcpy(cmd
->t_task
.t_task_cdb
, cdb
, scsi_command_size(cdb
));
1812 * Setup the received CDB based on SCSI defined opcodes and
1813 * perform unit attention, persistent reservations and ALUA
1814 * checks for virtual device backends. The cmd->t_task.t_task_cdb
1815 * pointer is expected to be setup before we reach this point.
1817 ret
= transport_generic_cmd_sequencer(cmd
, cdb
);
1821 * Check for SAM Task Attribute Emulation
1823 if (transport_check_alloc_task_attr(cmd
) < 0) {
1824 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
1825 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
1828 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1829 if (cmd
->se_lun
->lun_sep
)
1830 cmd
->se_lun
->lun_sep
->sep_stats
.cmd_pdus
++;
1831 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1834 EXPORT_SYMBOL(transport_generic_allocate_tasks
);
1837 * Used by fabric module frontends not defining a TFO->new_cmd_map()
1838 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
1840 int transport_generic_handle_cdb(
1845 printk(KERN_ERR
"cmd->se_lun is NULL\n");
1849 transport_add_cmd_to_queue(cmd
, TRANSPORT_NEW_CMD
);
1852 EXPORT_SYMBOL(transport_generic_handle_cdb
);
1855 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1856 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1857 * complete setup in TCM process context w/ TFO->new_cmd_map().
1859 int transport_generic_handle_cdb_map(
1864 printk(KERN_ERR
"cmd->se_lun is NULL\n");
1868 transport_add_cmd_to_queue(cmd
, TRANSPORT_NEW_CMD_MAP
);
1871 EXPORT_SYMBOL(transport_generic_handle_cdb_map
);
1873 /* transport_generic_handle_data():
1877 int transport_generic_handle_data(
1881 * For the software fabric case, then we assume the nexus is being
1882 * failed/shutdown when signals are pending from the kthread context
1883 * caller, so we return a failure. For the HW target mode case running
1884 * in interrupt code, the signal_pending() check is skipped.
1886 if (!in_interrupt() && signal_pending(current
))
1889 * If the received CDB has aleady been ABORTED by the generic
1890 * target engine, we now call transport_check_aborted_status()
1891 * to queue any delated TASK_ABORTED status for the received CDB to the
1892 * fabric module as we are expecting no further incoming DATA OUT
1893 * sequences at this point.
1895 if (transport_check_aborted_status(cmd
, 1) != 0)
1898 transport_add_cmd_to_queue(cmd
, TRANSPORT_PROCESS_WRITE
);
1901 EXPORT_SYMBOL(transport_generic_handle_data
);
1903 /* transport_generic_handle_tmr():
1907 int transport_generic_handle_tmr(
1911 * This is needed for early exceptions.
1913 cmd
->transport_wait_for_tasks
= &transport_generic_wait_for_tasks
;
1915 transport_add_cmd_to_queue(cmd
, TRANSPORT_PROCESS_TMR
);
1918 EXPORT_SYMBOL(transport_generic_handle_tmr
);
1920 void transport_generic_free_cmd_intr(
1923 transport_add_cmd_to_queue(cmd
, TRANSPORT_FREE_CMD_INTR
);
1925 EXPORT_SYMBOL(transport_generic_free_cmd_intr
);
1927 static int transport_stop_tasks_for_cmd(struct se_cmd
*cmd
)
1929 struct se_task
*task
, *task_tmp
;
1930 unsigned long flags
;
1933 DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
1934 cmd
->se_tfo
->get_task_tag(cmd
));
1937 * No tasks remain in the execution queue
1939 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
1940 list_for_each_entry_safe(task
, task_tmp
,
1941 &cmd
->t_task
.t_task_list
, t_list
) {
1942 DEBUG_TS("task_no[%d] - Processing task %p\n",
1943 task
->task_no
, task
);
1945 * If the struct se_task has not been sent and is not active,
1946 * remove the struct se_task from the execution queue.
1948 if (!atomic_read(&task
->task_sent
) &&
1949 !atomic_read(&task
->task_active
)) {
1950 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
,
1952 transport_remove_task_from_execute_queue(task
,
1955 DEBUG_TS("task_no[%d] - Removed from execute queue\n",
1957 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
1962 * If the struct se_task is active, sleep until it is returned
1965 if (atomic_read(&task
->task_active
)) {
1966 atomic_set(&task
->task_stop
, 1);
1967 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
,
1970 DEBUG_TS("task_no[%d] - Waiting to complete\n",
1972 wait_for_completion(&task
->task_stop_comp
);
1973 DEBUG_TS("task_no[%d] - Stopped successfully\n",
1976 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
1977 atomic_dec(&cmd
->t_task
.t_task_cdbs_left
);
1979 atomic_set(&task
->task_active
, 0);
1980 atomic_set(&task
->task_stop
, 0);
1982 DEBUG_TS("task_no[%d] - Did nothing\n", task
->task_no
);
1986 __transport_stop_task_timer(task
, &flags
);
1988 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
1994 * Handle SAM-esque emulation for generic transport request failures.
1996 static void transport_generic_request_failure(
1998 struct se_device
*dev
,
2002 DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
2003 " CDB: 0x%02x\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
2004 cmd
->t_task
.t_task_cdb
[0]);
2005 DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
2006 " %d/%d transport_error_status: %d\n",
2007 cmd
->se_tfo
->get_cmd_state(cmd
),
2008 cmd
->t_state
, cmd
->deferred_t_state
,
2009 cmd
->transport_error_status
);
2010 DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
2011 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
2012 " t_transport_active: %d t_transport_stop: %d"
2013 " t_transport_sent: %d\n", cmd
->t_task
.t_task_cdbs
,
2014 atomic_read(&cmd
->t_task
.t_task_cdbs_left
),
2015 atomic_read(&cmd
->t_task
.t_task_cdbs_sent
),
2016 atomic_read(&cmd
->t_task
.t_task_cdbs_ex_left
),
2017 atomic_read(&cmd
->t_task
.t_transport_active
),
2018 atomic_read(&cmd
->t_task
.t_transport_stop
),
2019 atomic_read(&cmd
->t_task
.t_transport_sent
));
2021 transport_stop_all_task_timers(cmd
);
2024 atomic_inc(&dev
->depth_left
);
2026 * For SAM Task Attribute emulation for failed struct se_cmd
2028 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
2029 transport_complete_task_attr(cmd
);
2032 transport_direct_request_timeout(cmd
);
2033 cmd
->transport_error_status
= PYX_TRANSPORT_LU_COMM_FAILURE
;
2036 switch (cmd
->transport_error_status
) {
2037 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE
:
2038 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
2040 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS
:
2041 cmd
->scsi_sense_reason
= TCM_SECTOR_COUNT_TOO_MANY
;
2043 case PYX_TRANSPORT_INVALID_CDB_FIELD
:
2044 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
2046 case PYX_TRANSPORT_INVALID_PARAMETER_LIST
:
2047 cmd
->scsi_sense_reason
= TCM_INVALID_PARAMETER_LIST
;
2049 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES
:
2051 transport_new_cmd_failure(cmd
);
2053 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
2054 * we force this session to fall back to session
2057 cmd
->se_tfo
->fall_back_to_erl0(cmd
->se_sess
);
2058 cmd
->se_tfo
->stop_session(cmd
->se_sess
, 0, 0);
2061 case PYX_TRANSPORT_LU_COMM_FAILURE
:
2062 case PYX_TRANSPORT_ILLEGAL_REQUEST
:
2063 cmd
->scsi_sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2065 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE
:
2066 cmd
->scsi_sense_reason
= TCM_UNKNOWN_MODE_PAGE
;
2068 case PYX_TRANSPORT_WRITE_PROTECTED
:
2069 cmd
->scsi_sense_reason
= TCM_WRITE_PROTECTED
;
2071 case PYX_TRANSPORT_RESERVATION_CONFLICT
:
2073 * No SENSE Data payload for this case, set SCSI Status
2074 * and queue the response to $FABRIC_MOD.
2076 * Uses linux/include/scsi/scsi.h SAM status codes defs
2078 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
2080 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2081 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2084 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2087 cmd
->se_dev
->se_sub_dev
->se_dev_attrib
.emulate_ua_intlck_ctrl
== 2)
2088 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
2089 cmd
->orig_fe_lun
, 0x2C,
2090 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
2092 cmd
->se_tfo
->queue_status(cmd
);
2094 case PYX_TRANSPORT_USE_SENSE_REASON
:
2096 * struct se_cmd->scsi_sense_reason already set
2100 printk(KERN_ERR
"Unknown transport error for CDB 0x%02x: %d\n",
2101 cmd
->t_task
.t_task_cdb
[0],
2102 cmd
->transport_error_status
);
2103 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
2108 transport_new_cmd_failure(cmd
);
2110 transport_send_check_condition_and_sense(cmd
,
2111 cmd
->scsi_sense_reason
, 0);
2113 transport_lun_remove_cmd(cmd
);
2114 if (!(transport_cmd_check_stop_to_fabric(cmd
)))
2118 static void transport_direct_request_timeout(struct se_cmd
*cmd
)
2120 unsigned long flags
;
2122 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
2123 if (!(atomic_read(&cmd
->t_task
.t_transport_timeout
))) {
2124 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2127 if (atomic_read(&cmd
->t_task
.t_task_cdbs_timeout_left
)) {
2128 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2132 atomic_sub(atomic_read(&cmd
->t_task
.t_transport_timeout
),
2133 &cmd
->t_task
.t_se_count
);
2134 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2137 static void transport_generic_request_timeout(struct se_cmd
*cmd
)
2139 unsigned long flags
;
2142 * Reset cmd->t_task.t_se_count to allow transport_generic_remove()
2143 * to allow last call to free memory resources.
2145 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
2146 if (atomic_read(&cmd
->t_task
.t_transport_timeout
) > 1) {
2147 int tmp
= (atomic_read(&cmd
->t_task
.t_transport_timeout
) - 1);
2149 atomic_sub(tmp
, &cmd
->t_task
.t_se_count
);
2151 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2153 transport_generic_remove(cmd
, 0, 0);
2157 transport_generic_allocate_buf(struct se_cmd
*cmd
, u32 data_length
)
2161 buf
= kzalloc(data_length
, GFP_KERNEL
);
2163 printk(KERN_ERR
"Unable to allocate memory for buffer\n");
2167 cmd
->t_task
.t_tasks_se_num
= 0;
2168 cmd
->t_task
.t_task_buf
= buf
;
2173 static inline u32
transport_lba_21(unsigned char *cdb
)
2175 return ((cdb
[1] & 0x1f) << 16) | (cdb
[2] << 8) | cdb
[3];
2178 static inline u32
transport_lba_32(unsigned char *cdb
)
2180 return (cdb
[2] << 24) | (cdb
[3] << 16) | (cdb
[4] << 8) | cdb
[5];
2183 static inline unsigned long long transport_lba_64(unsigned char *cdb
)
2185 unsigned int __v1
, __v2
;
2187 __v1
= (cdb
[2] << 24) | (cdb
[3] << 16) | (cdb
[4] << 8) | cdb
[5];
2188 __v2
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
2190 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
2194 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2196 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb
)
2198 unsigned int __v1
, __v2
;
2200 __v1
= (cdb
[12] << 24) | (cdb
[13] << 16) | (cdb
[14] << 8) | cdb
[15];
2201 __v2
= (cdb
[16] << 24) | (cdb
[17] << 16) | (cdb
[18] << 8) | cdb
[19];
2203 return ((unsigned long long)__v2
) | (unsigned long long)__v1
<< 32;
2206 static void transport_set_supported_SAM_opcode(struct se_cmd
*se_cmd
)
2208 unsigned long flags
;
2210 spin_lock_irqsave(&se_cmd
->t_task
.t_state_lock
, flags
);
2211 se_cmd
->se_cmd_flags
|= SCF_SUPPORTED_SAM_OPCODE
;
2212 spin_unlock_irqrestore(&se_cmd
->t_task
.t_state_lock
, flags
);
2216 * Called from interrupt context.
2218 static void transport_task_timeout_handler(unsigned long data
)
2220 struct se_task
*task
= (struct se_task
*)data
;
2221 struct se_cmd
*cmd
= task
->task_se_cmd
;
2222 unsigned long flags
;
2224 DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task
, cmd
);
2226 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
2227 if (task
->task_flags
& TF_STOP
) {
2228 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2231 task
->task_flags
&= ~TF_RUNNING
;
2234 * Determine if transport_complete_task() has already been called.
2236 if (!(atomic_read(&task
->task_active
))) {
2237 DEBUG_TT("transport task: %p cmd: %p timeout task_active"
2238 " == 0\n", task
, cmd
);
2239 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2243 atomic_inc(&cmd
->t_task
.t_se_count
);
2244 atomic_inc(&cmd
->t_task
.t_transport_timeout
);
2245 cmd
->t_task
.t_tasks_failed
= 1;
2247 atomic_set(&task
->task_timeout
, 1);
2248 task
->task_error_status
= PYX_TRANSPORT_TASK_TIMEOUT
;
2249 task
->task_scsi_status
= 1;
2251 if (atomic_read(&task
->task_stop
)) {
2252 DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
2253 " == 1\n", task
, cmd
);
2254 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2255 complete(&task
->task_stop_comp
);
2259 if (!(atomic_dec_and_test(&cmd
->t_task
.t_task_cdbs_left
))) {
2260 DEBUG_TT("transport task: %p cmd: %p timeout non zero"
2261 " t_task_cdbs_left\n", task
, cmd
);
2262 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2265 DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2268 cmd
->t_state
= TRANSPORT_COMPLETE_FAILURE
;
2269 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2271 transport_add_cmd_to_queue(cmd
, TRANSPORT_COMPLETE_FAILURE
);
2275 * Called with cmd->t_task.t_state_lock held.
2277 static void transport_start_task_timer(struct se_task
*task
)
2279 struct se_device
*dev
= task
->se_dev
;
2282 if (task
->task_flags
& TF_RUNNING
)
2285 * If the task_timeout is disabled, exit now.
2287 timeout
= dev
->se_sub_dev
->se_dev_attrib
.task_timeout
;
2291 init_timer(&task
->task_timer
);
2292 task
->task_timer
.expires
= (get_jiffies_64() + timeout
* HZ
);
2293 task
->task_timer
.data
= (unsigned long) task
;
2294 task
->task_timer
.function
= transport_task_timeout_handler
;
2296 task
->task_flags
|= TF_RUNNING
;
2297 add_timer(&task
->task_timer
);
2299 printk(KERN_INFO
"Starting task timer for cmd: %p task: %p seconds:"
2300 " %d\n", task
->task_se_cmd
, task
, timeout
);
2305 * Called with spin_lock_irq(&cmd->t_task.t_state_lock) held.
2307 void __transport_stop_task_timer(struct se_task
*task
, unsigned long *flags
)
2309 struct se_cmd
*cmd
= task
->task_se_cmd
;
2311 if (!(task
->task_flags
& TF_RUNNING
))
2314 task
->task_flags
|= TF_STOP
;
2315 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, *flags
);
2317 del_timer_sync(&task
->task_timer
);
2319 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, *flags
);
2320 task
->task_flags
&= ~TF_RUNNING
;
2321 task
->task_flags
&= ~TF_STOP
;
2324 static void transport_stop_all_task_timers(struct se_cmd
*cmd
)
2326 struct se_task
*task
= NULL
, *task_tmp
;
2327 unsigned long flags
;
2329 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
2330 list_for_each_entry_safe(task
, task_tmp
,
2331 &cmd
->t_task
.t_task_list
, t_list
)
2332 __transport_stop_task_timer(task
, &flags
);
2333 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2336 static inline int transport_tcq_window_closed(struct se_device
*dev
)
2338 if (dev
->dev_tcq_window_closed
++ <
2339 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD
) {
2340 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT
);
2342 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG
);
2344 wake_up_interruptible(&dev
->dev_queue_obj
.thread_wq
);
2349 * Called from Fabric Module context from transport_execute_tasks()
2351 * The return of this function determins if the tasks from struct se_cmd
2352 * get added to the execution queue in transport_execute_tasks(),
2353 * or are added to the delayed or ordered lists here.
2355 static inline int transport_execute_task_attr(struct se_cmd
*cmd
)
2357 if (cmd
->se_dev
->dev_task_attr_type
!= SAM_TASK_ATTR_EMULATED
)
2360 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2361 * to allow the passed struct se_cmd list of tasks to the front of the list.
2363 if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
2364 atomic_inc(&cmd
->se_dev
->dev_hoq_count
);
2365 smp_mb__after_atomic_inc();
2366 DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
2367 " 0x%02x, se_ordered_id: %u\n",
2368 cmd
->t_task
->t_task_cdb
[0],
2369 cmd
->se_ordered_id
);
2371 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
2372 spin_lock(&cmd
->se_dev
->ordered_cmd_lock
);
2373 list_add_tail(&cmd
->se_ordered_node
,
2374 &cmd
->se_dev
->ordered_cmd_list
);
2375 spin_unlock(&cmd
->se_dev
->ordered_cmd_lock
);
2377 atomic_inc(&cmd
->se_dev
->dev_ordered_sync
);
2378 smp_mb__after_atomic_inc();
2380 DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
2381 " list, se_ordered_id: %u\n",
2382 cmd
->t_task
.t_task_cdb
[0],
2383 cmd
->se_ordered_id
);
2385 * Add ORDERED command to tail of execution queue if
2386 * no other older commands exist that need to be
2389 if (!(atomic_read(&cmd
->se_dev
->simple_cmds
)))
2393 * For SIMPLE and UNTAGGED Task Attribute commands
2395 atomic_inc(&cmd
->se_dev
->simple_cmds
);
2396 smp_mb__after_atomic_inc();
2399 * Otherwise if one or more outstanding ORDERED task attribute exist,
2400 * add the dormant task(s) built for the passed struct se_cmd to the
2401 * execution queue and become in Active state for this struct se_device.
2403 if (atomic_read(&cmd
->se_dev
->dev_ordered_sync
) != 0) {
2405 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2406 * will be drained upon completion of HEAD_OF_QUEUE task.
2408 spin_lock(&cmd
->se_dev
->delayed_cmd_lock
);
2409 cmd
->se_cmd_flags
|= SCF_DELAYED_CMD_FROM_SAM_ATTR
;
2410 list_add_tail(&cmd
->se_delayed_node
,
2411 &cmd
->se_dev
->delayed_cmd_list
);
2412 spin_unlock(&cmd
->se_dev
->delayed_cmd_lock
);
2414 DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
2415 " delayed CMD list, se_ordered_id: %u\n",
2416 cmd
->t_task
.t_task_cdb
[0], cmd
->sam_task_attr
,
2417 cmd
->se_ordered_id
);
2419 * Return zero to let transport_execute_tasks() know
2420 * not to add the delayed tasks to the execution list.
2425 * Otherwise, no ORDERED task attributes exist..
2431 * Called from fabric module context in transport_generic_new_cmd() and
2432 * transport_generic_process_write()
2434 static int transport_execute_tasks(struct se_cmd
*cmd
)
2438 if (!(cmd
->se_cmd_flags
& SCF_SE_DISABLE_ONLINE_CHECK
)) {
2439 if (se_dev_check_online(cmd
->se_orig_obj_ptr
) != 0) {
2440 cmd
->transport_error_status
=
2441 PYX_TRANSPORT_LU_COMM_FAILURE
;
2442 transport_generic_request_failure(cmd
, NULL
, 0, 1);
2447 * Call transport_cmd_check_stop() to see if a fabric exception
2448 * has occurred that prevents execution.
2450 if (!(transport_cmd_check_stop(cmd
, 0, TRANSPORT_PROCESSING
))) {
2452 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2453 * attribute for the tasks of the received struct se_cmd CDB
2455 add_tasks
= transport_execute_task_attr(cmd
);
2459 * This calls transport_add_tasks_from_cmd() to handle
2460 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2461 * (if enabled) in __transport_add_task_to_execute_queue() and
2462 * transport_add_task_check_sam_attr().
2464 transport_add_tasks_from_cmd(cmd
);
2467 * Kick the execution queue for the cmd associated struct se_device
2471 __transport_execute_tasks(cmd
->se_dev
);
2476 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2477 * from struct se_device->execute_task_list and
2479 * Called from transport_processing_thread()
2481 static int __transport_execute_tasks(struct se_device
*dev
)
2484 struct se_cmd
*cmd
= NULL
;
2485 struct se_task
*task
= NULL
;
2486 unsigned long flags
;
2489 * Check if there is enough room in the device and HBA queue to send
2490 * struct se_transport_task's to the selected transport.
2493 if (!atomic_read(&dev
->depth_left
))
2494 return transport_tcq_window_closed(dev
);
2496 dev
->dev_tcq_window_closed
= 0;
2498 spin_lock_irq(&dev
->execute_task_lock
);
2499 if (list_empty(&dev
->execute_task_list
)) {
2500 spin_unlock_irq(&dev
->execute_task_lock
);
2503 task
= list_first_entry(&dev
->execute_task_list
,
2504 struct se_task
, t_execute_list
);
2505 list_del(&task
->t_execute_list
);
2506 atomic_set(&task
->task_execute_queue
, 0);
2507 atomic_dec(&dev
->execute_tasks
);
2508 spin_unlock_irq(&dev
->execute_task_lock
);
2510 atomic_dec(&dev
->depth_left
);
2512 cmd
= task
->task_se_cmd
;
2514 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
2515 atomic_set(&task
->task_active
, 1);
2516 atomic_set(&task
->task_sent
, 1);
2517 atomic_inc(&cmd
->t_task
.t_task_cdbs_sent
);
2519 if (atomic_read(&cmd
->t_task
.t_task_cdbs_sent
) ==
2520 cmd
->t_task
.t_task_cdbs
)
2521 atomic_set(&cmd
->transport_sent
, 1);
2523 transport_start_task_timer(task
);
2524 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2526 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2527 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2528 * struct se_subsystem_api->do_task() caller below.
2530 if (cmd
->transport_emulate_cdb
) {
2531 error
= cmd
->transport_emulate_cdb(cmd
);
2533 cmd
->transport_error_status
= error
;
2534 atomic_set(&task
->task_active
, 0);
2535 atomic_set(&cmd
->transport_sent
, 0);
2536 transport_stop_tasks_for_cmd(cmd
);
2537 transport_generic_request_failure(cmd
, dev
, 0, 1);
2541 * Handle the successful completion for transport_emulate_cdb()
2542 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2543 * Otherwise the caller is expected to complete the task with
2546 if (!(cmd
->se_cmd_flags
& SCF_EMULATE_CDB_ASYNC
)) {
2547 cmd
->scsi_status
= SAM_STAT_GOOD
;
2548 task
->task_scsi_status
= GOOD
;
2549 transport_complete_task(task
, 1);
2553 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2554 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2555 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2556 * LUN emulation code.
2558 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2559 * call ->do_task() directly and let the underlying TCM subsystem plugin
2560 * code handle the CDB emulation.
2562 if ((dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
) &&
2563 (!(task
->task_se_cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
)))
2564 error
= transport_emulate_control_cdb(task
);
2566 error
= dev
->transport
->do_task(task
);
2569 cmd
->transport_error_status
= error
;
2570 atomic_set(&task
->task_active
, 0);
2571 atomic_set(&cmd
->transport_sent
, 0);
2572 transport_stop_tasks_for_cmd(cmd
);
2573 transport_generic_request_failure(cmd
, dev
, 0, 1);
2582 void transport_new_cmd_failure(struct se_cmd
*se_cmd
)
2584 unsigned long flags
;
2586 * Any unsolicited data will get dumped for failed command inside of
2589 spin_lock_irqsave(&se_cmd
->t_task
.t_state_lock
, flags
);
2590 se_cmd
->se_cmd_flags
|= SCF_SE_CMD_FAILED
;
2591 se_cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2592 spin_unlock_irqrestore(&se_cmd
->t_task
.t_state_lock
, flags
);
2594 se_cmd
->se_tfo
->new_cmd_failure(se_cmd
);
2597 static void transport_nop_wait_for_tasks(struct se_cmd
*, int, int);
2599 static inline u32
transport_get_sectors_6(
2604 struct se_device
*dev
= cmd
->se_dev
;
2607 * Assume TYPE_DISK for non struct se_device objects.
2608 * Use 8-bit sector value.
2614 * Use 24-bit allocation length for TYPE_TAPE.
2616 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
2617 return (u32
)(cdb
[2] << 16) + (cdb
[3] << 8) + cdb
[4];
2620 * Everything else assume TYPE_DISK Sector CDB location.
2621 * Use 8-bit sector value.
2627 static inline u32
transport_get_sectors_10(
2632 struct se_device
*dev
= cmd
->se_dev
;
2635 * Assume TYPE_DISK for non struct se_device objects.
2636 * Use 16-bit sector value.
2642 * XXX_10 is not defined in SSC, throw an exception
2644 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2650 * Everything else assume TYPE_DISK Sector CDB location.
2651 * Use 16-bit sector value.
2654 return (u32
)(cdb
[7] << 8) + cdb
[8];
2657 static inline u32
transport_get_sectors_12(
2662 struct se_device
*dev
= cmd
->se_dev
;
2665 * Assume TYPE_DISK for non struct se_device objects.
2666 * Use 32-bit sector value.
2672 * XXX_12 is not defined in SSC, throw an exception
2674 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2680 * Everything else assume TYPE_DISK Sector CDB location.
2681 * Use 32-bit sector value.
2684 return (u32
)(cdb
[6] << 24) + (cdb
[7] << 16) + (cdb
[8] << 8) + cdb
[9];
2687 static inline u32
transport_get_sectors_16(
2692 struct se_device
*dev
= cmd
->se_dev
;
2695 * Assume TYPE_DISK for non struct se_device objects.
2696 * Use 32-bit sector value.
2702 * Use 24-bit allocation length for TYPE_TAPE.
2704 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
)
2705 return (u32
)(cdb
[12] << 16) + (cdb
[13] << 8) + cdb
[14];
2708 return (u32
)(cdb
[10] << 24) + (cdb
[11] << 16) +
2709 (cdb
[12] << 8) + cdb
[13];
2713 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2715 static inline u32
transport_get_sectors_32(
2721 * Assume TYPE_DISK for non struct se_device objects.
2722 * Use 32-bit sector value.
2724 return (u32
)(cdb
[28] << 24) + (cdb
[29] << 16) +
2725 (cdb
[30] << 8) + cdb
[31];
2729 static inline u32
transport_get_size(
2734 struct se_device
*dev
= cmd
->se_dev
;
2736 if (dev
->transport
->get_device_type(dev
) == TYPE_TAPE
) {
2737 if (cdb
[1] & 1) { /* sectors */
2738 return dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
;
2743 printk(KERN_INFO
"Returning block_size: %u, sectors: %u == %u for"
2744 " %s object\n", dev
->se_sub_dev
->se_dev_attrib
.block_size
, sectors
,
2745 dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
,
2746 dev
->transport
->name
);
2748 return dev
->se_sub_dev
->se_dev_attrib
.block_size
* sectors
;
2751 unsigned char transport_asciihex_to_binaryhex(unsigned char val
[2])
2753 unsigned char result
= 0;
2757 if ((val
[0] >= 'a') && (val
[0] <= 'f'))
2758 result
= ((val
[0] - 'a' + 10) & 0xf) << 4;
2760 if ((val
[0] >= 'A') && (val
[0] <= 'F'))
2761 result
= ((val
[0] - 'A' + 10) & 0xf) << 4;
2763 result
= ((val
[0] - '0') & 0xf) << 4;
2767 if ((val
[1] >= 'a') && (val
[1] <= 'f'))
2768 result
|= ((val
[1] - 'a' + 10) & 0xf);
2770 if ((val
[1] >= 'A') && (val
[1] <= 'F'))
2771 result
|= ((val
[1] - 'A' + 10) & 0xf);
2773 result
|= ((val
[1] - '0') & 0xf);
2777 EXPORT_SYMBOL(transport_asciihex_to_binaryhex
);
2779 static void transport_xor_callback(struct se_cmd
*cmd
)
2781 unsigned char *buf
, *addr
;
2782 struct se_mem
*se_mem
;
2783 unsigned int offset
;
2786 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2788 * 1) read the specified logical block(s);
2789 * 2) transfer logical blocks from the data-out buffer;
2790 * 3) XOR the logical blocks transferred from the data-out buffer with
2791 * the logical blocks read, storing the resulting XOR data in a buffer;
2792 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2793 * blocks transferred from the data-out buffer; and
2794 * 5) transfer the resulting XOR data to the data-in buffer.
2796 buf
= kmalloc(cmd
->data_length
, GFP_KERNEL
);
2798 printk(KERN_ERR
"Unable to allocate xor_callback buf\n");
2802 * Copy the scatterlist WRITE buffer located at cmd->t_task.t_mem_list
2803 * into the locally allocated *buf
2805 transport_memcpy_se_mem_read_contig(cmd
, buf
, &cmd
->t_task
.t_mem_list
);
2807 * Now perform the XOR against the BIDI read memory located at
2808 * cmd->t_task.t_mem_bidi_list
2812 list_for_each_entry(se_mem
, &cmd
->t_task
.t_mem_bidi_list
, se_list
) {
2813 addr
= (unsigned char *)kmap_atomic(se_mem
->se_page
, KM_USER0
);
2817 for (i
= 0; i
< se_mem
->se_len
; i
++)
2818 *(addr
+ se_mem
->se_off
+ i
) ^= *(buf
+ offset
+ i
);
2820 offset
+= se_mem
->se_len
;
2821 kunmap_atomic(addr
, KM_USER0
);
2828 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2830 static int transport_get_sense_data(struct se_cmd
*cmd
)
2832 unsigned char *buffer
= cmd
->sense_buffer
, *sense_buffer
= NULL
;
2833 struct se_device
*dev
;
2834 struct se_task
*task
= NULL
, *task_tmp
;
2835 unsigned long flags
;
2838 WARN_ON(!cmd
->se_lun
);
2840 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
2841 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
2842 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2846 list_for_each_entry_safe(task
, task_tmp
,
2847 &cmd
->t_task
.t_task_list
, t_list
) {
2849 if (!task
->task_sense
)
2856 if (!dev
->transport
->get_sense_buffer
) {
2857 printk(KERN_ERR
"dev->transport->get_sense_buffer"
2862 sense_buffer
= dev
->transport
->get_sense_buffer(task
);
2863 if (!(sense_buffer
)) {
2864 printk(KERN_ERR
"ITT[0x%08x]_TASK[%d]: Unable to locate"
2865 " sense buffer for task with sense\n",
2866 cmd
->se_tfo
->get_task_tag(cmd
), task
->task_no
);
2869 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2871 offset
= cmd
->se_tfo
->set_fabric_sense_len(cmd
,
2872 TRANSPORT_SENSE_BUFFER
);
2874 memcpy(&buffer
[offset
], sense_buffer
,
2875 TRANSPORT_SENSE_BUFFER
);
2876 cmd
->scsi_status
= task
->task_scsi_status
;
2877 /* Automatically padded */
2878 cmd
->scsi_sense_length
=
2879 (TRANSPORT_SENSE_BUFFER
+ offset
);
2881 printk(KERN_INFO
"HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2883 dev
->se_hba
->hba_id
, dev
->transport
->name
,
2887 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
2892 static int transport_allocate_resources(struct se_cmd
*cmd
)
2894 u32 length
= cmd
->data_length
;
2896 if ((cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
) ||
2897 (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
))
2898 return transport_generic_get_mem(cmd
, length
, PAGE_SIZE
);
2899 else if (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_NONSG_IO_CDB
)
2900 return transport_generic_allocate_buf(cmd
, length
);
2906 transport_handle_reservation_conflict(struct se_cmd
*cmd
)
2908 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
2909 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2910 cmd
->se_cmd_flags
|= SCF_SCSI_RESERVATION_CONFLICT
;
2911 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
2913 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2914 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2917 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2920 cmd
->se_dev
->se_sub_dev
->se_dev_attrib
.emulate_ua_intlck_ctrl
== 2)
2921 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
2922 cmd
->orig_fe_lun
, 0x2C,
2923 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
2927 /* transport_generic_cmd_sequencer():
2929 * Generic Command Sequencer that should work for most DAS transport
2932 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2935 * FIXME: Need to support other SCSI OPCODES where as well.
2937 static int transport_generic_cmd_sequencer(
2941 struct se_device
*dev
= cmd
->se_dev
;
2942 struct se_subsystem_dev
*su_dev
= dev
->se_sub_dev
;
2943 int ret
= 0, sector_ret
= 0, passthrough
;
2944 u32 sectors
= 0, size
= 0, pr_reg_type
= 0;
2948 * Check for an existing UNIT ATTENTION condition
2950 if (core_scsi3_ua_check(cmd
, cdb
) < 0) {
2951 cmd
->transport_wait_for_tasks
=
2952 &transport_nop_wait_for_tasks
;
2953 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2954 cmd
->scsi_sense_reason
= TCM_CHECK_CONDITION_UNIT_ATTENTION
;
2958 * Check status of Asymmetric Logical Unit Assignment port
2960 ret
= su_dev
->t10_alua
.alua_state_check(cmd
, cdb
, &alua_ascq
);
2962 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
2964 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2965 * The ALUA additional sense code qualifier (ASCQ) is determined
2966 * by the ALUA primary or secondary access state..
2970 printk(KERN_INFO
"[%s]: ALUA TG Port not available,"
2971 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2972 cmd
->se_tfo
->get_fabric_name(), alua_ascq
);
2974 transport_set_sense_codes(cmd
, 0x04, alua_ascq
);
2975 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
2976 cmd
->scsi_sense_reason
= TCM_CHECK_CONDITION_NOT_READY
;
2979 goto out_invalid_cdb_field
;
2982 * Check status for SPC-3 Persistent Reservations
2984 if (su_dev
->t10_pr
.pr_ops
.t10_reservation_check(cmd
, &pr_reg_type
) != 0) {
2985 if (su_dev
->t10_pr
.pr_ops
.t10_seq_non_holder(
2986 cmd
, cdb
, pr_reg_type
) != 0)
2987 return transport_handle_reservation_conflict(cmd
);
2989 * This means the CDB is allowed for the SCSI Initiator port
2990 * when said port is *NOT* holding the legacy SPC-2 or
2991 * SPC-3 Persistent Reservation.
2997 sectors
= transport_get_sectors_6(cdb
, cmd
, §or_ret
);
2999 goto out_unsupported_cdb
;
3000 size
= transport_get_size(sectors
, cdb
, cmd
);
3001 cmd
->transport_split_cdb
= &split_cdb_XX_6
;
3002 cmd
->t_task
.t_task_lba
= transport_lba_21(cdb
);
3003 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3006 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3008 goto out_unsupported_cdb
;
3009 size
= transport_get_size(sectors
, cdb
, cmd
);
3010 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
3011 cmd
->t_task
.t_task_lba
= transport_lba_32(cdb
);
3012 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3015 sectors
= transport_get_sectors_12(cdb
, cmd
, §or_ret
);
3017 goto out_unsupported_cdb
;
3018 size
= transport_get_size(sectors
, cdb
, cmd
);
3019 cmd
->transport_split_cdb
= &split_cdb_XX_12
;
3020 cmd
->t_task
.t_task_lba
= transport_lba_32(cdb
);
3021 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3024 sectors
= transport_get_sectors_16(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_16
;
3029 cmd
->t_task
.t_task_lba
= transport_lba_64(cdb
);
3030 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3033 sectors
= transport_get_sectors_6(cdb
, cmd
, §or_ret
);
3035 goto out_unsupported_cdb
;
3036 size
= transport_get_size(sectors
, cdb
, cmd
);
3037 cmd
->transport_split_cdb
= &split_cdb_XX_6
;
3038 cmd
->t_task
.t_task_lba
= transport_lba_21(cdb
);
3039 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3042 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3044 goto out_unsupported_cdb
;
3045 size
= transport_get_size(sectors
, cdb
, cmd
);
3046 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
3047 cmd
->t_task
.t_task_lba
= transport_lba_32(cdb
);
3048 cmd
->t_task
.t_tasks_fua
= (cdb
[1] & 0x8);
3049 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3052 sectors
= transport_get_sectors_12(cdb
, cmd
, §or_ret
);
3054 goto out_unsupported_cdb
;
3055 size
= transport_get_size(sectors
, cdb
, cmd
);
3056 cmd
->transport_split_cdb
= &split_cdb_XX_12
;
3057 cmd
->t_task
.t_task_lba
= transport_lba_32(cdb
);
3058 cmd
->t_task
.t_tasks_fua
= (cdb
[1] & 0x8);
3059 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3062 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3064 goto out_unsupported_cdb
;
3065 size
= transport_get_size(sectors
, cdb
, cmd
);
3066 cmd
->transport_split_cdb
= &split_cdb_XX_16
;
3067 cmd
->t_task
.t_task_lba
= transport_lba_64(cdb
);
3068 cmd
->t_task
.t_tasks_fua
= (cdb
[1] & 0x8);
3069 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3071 case XDWRITEREAD_10
:
3072 if ((cmd
->data_direction
!= DMA_TO_DEVICE
) ||
3073 !(cmd
->t_task
.t_tasks_bidi
))
3074 goto out_invalid_cdb_field
;
3075 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3077 goto out_unsupported_cdb
;
3078 size
= transport_get_size(sectors
, cdb
, cmd
);
3079 cmd
->transport_split_cdb
= &split_cdb_XX_10
;
3080 cmd
->t_task
.t_task_lba
= transport_lba_32(cdb
);
3081 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3082 passthrough
= (dev
->transport
->transport_type
==
3083 TRANSPORT_PLUGIN_PHBA_PDEV
);
3085 * Skip the remaining assignments for TCM/PSCSI passthrough
3090 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3092 cmd
->transport_complete_callback
= &transport_xor_callback
;
3093 cmd
->t_task
.t_tasks_fua
= (cdb
[1] & 0x8);
3095 case VARIABLE_LENGTH_CMD
:
3096 service_action
= get_unaligned_be16(&cdb
[8]);
3098 * Determine if this is TCM/PSCSI device and we should disable
3099 * internal emulation for this CDB.
3101 passthrough
= (dev
->transport
->transport_type
==
3102 TRANSPORT_PLUGIN_PHBA_PDEV
);
3104 switch (service_action
) {
3105 case XDWRITEREAD_32
:
3106 sectors
= transport_get_sectors_32(cdb
, cmd
, §or_ret
);
3108 goto out_unsupported_cdb
;
3109 size
= transport_get_size(sectors
, cdb
, cmd
);
3111 * Use WRITE_32 and READ_32 opcodes for the emulated
3112 * XDWRITE_READ_32 logic.
3114 cmd
->transport_split_cdb
= &split_cdb_XX_32
;
3115 cmd
->t_task
.t_task_lba
= transport_lba_64_ext(cdb
);
3116 cmd
->se_cmd_flags
|= SCF_SCSI_DATA_SG_IO_CDB
;
3119 * Skip the remaining assignments for TCM/PSCSI passthrough
3125 * Setup BIDI XOR callback to be run during
3126 * transport_generic_complete_ok()
3128 cmd
->transport_complete_callback
= &transport_xor_callback
;
3129 cmd
->t_task
.t_tasks_fua
= (cdb
[10] & 0x8);
3132 sectors
= transport_get_sectors_32(cdb
, cmd
, §or_ret
);
3134 goto out_unsupported_cdb
;
3135 size
= transport_get_size(sectors
, cdb
, cmd
);
3136 cmd
->t_task
.t_task_lba
= get_unaligned_be64(&cdb
[12]);
3137 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3140 * Skip the remaining assignments for TCM/PSCSI passthrough
3145 if ((cdb
[10] & 0x04) || (cdb
[10] & 0x02)) {
3146 printk(KERN_ERR
"WRITE_SAME PBDATA and LBDATA"
3147 " bits not supported for Block Discard"
3149 goto out_invalid_cdb_field
;
3152 * Currently for the emulated case we only accept
3153 * tpws with the UNMAP=1 bit set.
3155 if (!(cdb
[10] & 0x08)) {
3156 printk(KERN_ERR
"WRITE_SAME w/o UNMAP bit not"
3157 " supported for Block Discard Emulation\n");
3158 goto out_invalid_cdb_field
;
3162 printk(KERN_ERR
"VARIABLE_LENGTH_CMD service action"
3163 " 0x%04x not supported\n", service_action
);
3164 goto out_unsupported_cdb
;
3167 case MAINTENANCE_IN
:
3168 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
3169 /* MAINTENANCE_IN from SCC-2 */
3171 * Check for emulated MI_REPORT_TARGET_PGS.
3173 if (cdb
[1] == MI_REPORT_TARGET_PGS
) {
3174 cmd
->transport_emulate_cdb
=
3175 (su_dev
->t10_alua
.alua_type
==
3176 SPC3_ALUA_EMULATED
) ?
3177 core_emulate_report_target_port_groups
:
3180 size
= (cdb
[6] << 24) | (cdb
[7] << 16) |
3181 (cdb
[8] << 8) | cdb
[9];
3183 /* GPCMD_SEND_KEY from multi media commands */
3184 size
= (cdb
[8] << 8) + cdb
[9];
3186 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3190 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3192 case MODE_SELECT_10
:
3193 size
= (cdb
[7] << 8) + cdb
[8];
3194 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3198 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3201 case GPCMD_READ_BUFFER_CAPACITY
:
3202 case GPCMD_SEND_OPC
:
3205 size
= (cdb
[7] << 8) + cdb
[8];
3206 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3208 case READ_BLOCK_LIMITS
:
3209 size
= READ_BLOCK_LEN
;
3210 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3212 case GPCMD_GET_CONFIGURATION
:
3213 case GPCMD_READ_FORMAT_CAPACITIES
:
3214 case GPCMD_READ_DISC_INFO
:
3215 case GPCMD_READ_TRACK_RZONE_INFO
:
3216 size
= (cdb
[7] << 8) + cdb
[8];
3217 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3219 case PERSISTENT_RESERVE_IN
:
3220 case PERSISTENT_RESERVE_OUT
:
3221 cmd
->transport_emulate_cdb
=
3222 (su_dev
->t10_pr
.res_type
==
3223 SPC3_PERSISTENT_RESERVATIONS
) ?
3224 core_scsi3_emulate_pr
: NULL
;
3225 size
= (cdb
[7] << 8) + cdb
[8];
3226 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3228 case GPCMD_MECHANISM_STATUS
:
3229 case GPCMD_READ_DVD_STRUCTURE
:
3230 size
= (cdb
[8] << 8) + cdb
[9];
3231 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3234 size
= READ_POSITION_LEN
;
3235 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3237 case MAINTENANCE_OUT
:
3238 if (dev
->transport
->get_device_type(dev
) != TYPE_ROM
) {
3239 /* MAINTENANCE_OUT from SCC-2
3241 * Check for emulated MO_SET_TARGET_PGS.
3243 if (cdb
[1] == MO_SET_TARGET_PGS
) {
3244 cmd
->transport_emulate_cdb
=
3245 (su_dev
->t10_alua
.alua_type
==
3246 SPC3_ALUA_EMULATED
) ?
3247 core_emulate_set_target_port_groups
:
3251 size
= (cdb
[6] << 24) | (cdb
[7] << 16) |
3252 (cdb
[8] << 8) | cdb
[9];
3254 /* GPCMD_REPORT_KEY from multi media commands */
3255 size
= (cdb
[8] << 8) + cdb
[9];
3257 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3260 size
= (cdb
[3] << 8) + cdb
[4];
3262 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3263 * See spc4r17 section 5.3
3265 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3266 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
3267 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3270 size
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3271 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3274 size
= READ_CAP_LEN
;
3275 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3277 case READ_MEDIA_SERIAL_NUMBER
:
3278 case SECURITY_PROTOCOL_IN
:
3279 case SECURITY_PROTOCOL_OUT
:
3280 size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
3281 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3283 case SERVICE_ACTION_IN
:
3284 case ACCESS_CONTROL_IN
:
3285 case ACCESS_CONTROL_OUT
:
3287 case READ_ATTRIBUTE
:
3288 case RECEIVE_COPY_RESULTS
:
3289 case WRITE_ATTRIBUTE
:
3290 size
= (cdb
[10] << 24) | (cdb
[11] << 16) |
3291 (cdb
[12] << 8) | cdb
[13];
3292 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3294 case RECEIVE_DIAGNOSTIC
:
3295 case SEND_DIAGNOSTIC
:
3296 size
= (cdb
[3] << 8) | cdb
[4];
3297 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3299 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3302 sectors
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3303 size
= (2336 * sectors
);
3304 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3309 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3313 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3315 case READ_ELEMENT_STATUS
:
3316 size
= 65536 * cdb
[7] + 256 * cdb
[8] + cdb
[9];
3317 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3320 size
= (cdb
[6] << 16) + (cdb
[7] << 8) + cdb
[8];
3321 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3326 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3327 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3329 if (cdb
[0] == RESERVE_10
)
3330 size
= (cdb
[7] << 8) | cdb
[8];
3332 size
= cmd
->data_length
;
3335 * Setup the legacy emulated handler for SPC-2 and
3336 * >= SPC-3 compatible reservation handling (CRH=1)
3337 * Otherwise, we assume the underlying SCSI logic is
3338 * is running in SPC_PASSTHROUGH, and wants reservations
3339 * emulation disabled.
3341 cmd
->transport_emulate_cdb
=
3342 (su_dev
->t10_pr
.res_type
!=
3344 core_scsi2_emulate_crh
: NULL
;
3345 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3350 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3351 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3353 if (cdb
[0] == RELEASE_10
)
3354 size
= (cdb
[7] << 8) | cdb
[8];
3356 size
= cmd
->data_length
;
3358 cmd
->transport_emulate_cdb
=
3359 (su_dev
->t10_pr
.res_type
!=
3361 core_scsi2_emulate_crh
: NULL
;
3362 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3364 case SYNCHRONIZE_CACHE
:
3365 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3367 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3369 if (cdb
[0] == SYNCHRONIZE_CACHE
) {
3370 sectors
= transport_get_sectors_10(cdb
, cmd
, §or_ret
);
3371 cmd
->t_task
.t_task_lba
= transport_lba_32(cdb
);
3373 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3374 cmd
->t_task
.t_task_lba
= transport_lba_64(cdb
);
3377 goto out_unsupported_cdb
;
3379 size
= transport_get_size(sectors
, cdb
, cmd
);
3380 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3383 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3385 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
3388 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3389 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3391 cmd
->se_cmd_flags
|= SCF_EMULATE_CDB_ASYNC
;
3393 * Check to ensure that LBA + Range does not exceed past end of
3396 if (transport_get_sectors(cmd
) < 0)
3397 goto out_invalid_cdb_field
;
3400 size
= get_unaligned_be16(&cdb
[7]);
3401 passthrough
= (dev
->transport
->transport_type
==
3402 TRANSPORT_PLUGIN_PHBA_PDEV
);
3404 * Determine if the received UNMAP used to for direct passthrough
3405 * into Linux/SCSI with struct request via TCM/pSCSI or we are
3406 * signaling the use of internal transport_generic_unmap() emulation
3407 * for UNMAP -> Linux/BLOCK disbard with TCM/IBLOCK and TCM/FILEIO
3408 * subsystem plugin backstores.
3411 cmd
->se_cmd_flags
|= SCF_EMULATE_SYNC_UNMAP
;
3413 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3416 sectors
= transport_get_sectors_16(cdb
, cmd
, §or_ret
);
3418 goto out_unsupported_cdb
;
3419 size
= transport_get_size(sectors
, cdb
, cmd
);
3420 cmd
->t_task
.t_task_lba
= get_unaligned_be64(&cdb
[2]);
3421 passthrough
= (dev
->transport
->transport_type
==
3422 TRANSPORT_PLUGIN_PHBA_PDEV
);
3424 * Determine if the received WRITE_SAME_16 is used to for direct
3425 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
3426 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
3427 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
3428 * TCM/FILEIO subsystem plugin backstores.
3430 if (!(passthrough
)) {
3431 if ((cdb
[1] & 0x04) || (cdb
[1] & 0x02)) {
3432 printk(KERN_ERR
"WRITE_SAME PBDATA and LBDATA"
3433 " bits not supported for Block Discard"
3435 goto out_invalid_cdb_field
;
3438 * Currently for the emulated case we only accept
3439 * tpws with the UNMAP=1 bit set.
3441 if (!(cdb
[1] & 0x08)) {
3442 printk(KERN_ERR
"WRITE_SAME w/o UNMAP bit not "
3443 " supported for Block Discard Emulation\n");
3444 goto out_invalid_cdb_field
;
3447 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_SG_IO_CDB
;
3449 case ALLOW_MEDIUM_REMOVAL
:
3450 case GPCMD_CLOSE_TRACK
:
3452 case INITIALIZE_ELEMENT_STATUS
:
3453 case GPCMD_LOAD_UNLOAD
:
3456 case GPCMD_SET_SPEED
:
3459 case TEST_UNIT_READY
:
3461 case WRITE_FILEMARKS
:
3463 cmd
->se_cmd_flags
|= SCF_SCSI_NON_DATA_CDB
;
3466 cmd
->transport_emulate_cdb
=
3467 transport_core_report_lun_response
;
3468 size
= (cdb
[6] << 24) | (cdb
[7] << 16) | (cdb
[8] << 8) | cdb
[9];
3470 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3471 * See spc4r17 section 5.3
3473 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3474 cmd
->sam_task_attr
= MSG_HEAD_TAG
;
3475 cmd
->se_cmd_flags
|= SCF_SCSI_CONTROL_NONSG_IO_CDB
;
3478 printk(KERN_WARNING
"TARGET_CORE[%s]: Unsupported SCSI Opcode"
3479 " 0x%02x, sending CHECK_CONDITION.\n",
3480 cmd
->se_tfo
->get_fabric_name(), cdb
[0]);
3481 cmd
->transport_wait_for_tasks
= &transport_nop_wait_for_tasks
;
3482 goto out_unsupported_cdb
;
3485 if (size
!= cmd
->data_length
) {
3486 printk(KERN_WARNING
"TARGET_CORE[%s]: Expected Transfer Length:"
3487 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3488 " 0x%02x\n", cmd
->se_tfo
->get_fabric_name(),
3489 cmd
->data_length
, size
, cdb
[0]);
3491 cmd
->cmd_spdtl
= size
;
3493 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
3494 printk(KERN_ERR
"Rejecting underflow/overflow"
3496 goto out_invalid_cdb_field
;
3499 * Reject READ_* or WRITE_* with overflow/underflow for
3500 * type SCF_SCSI_DATA_SG_IO_CDB.
3502 if (!(ret
) && (dev
->se_sub_dev
->se_dev_attrib
.block_size
!= 512)) {
3503 printk(KERN_ERR
"Failing OVERFLOW/UNDERFLOW for LBA op"
3504 " CDB on non 512-byte sector setup subsystem"
3505 " plugin: %s\n", dev
->transport
->name
);
3506 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3507 goto out_invalid_cdb_field
;
3510 if (size
> cmd
->data_length
) {
3511 cmd
->se_cmd_flags
|= SCF_OVERFLOW_BIT
;
3512 cmd
->residual_count
= (size
- cmd
->data_length
);
3514 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
3515 cmd
->residual_count
= (cmd
->data_length
- size
);
3517 cmd
->data_length
= size
;
3520 transport_set_supported_SAM_opcode(cmd
);
3523 out_unsupported_cdb
:
3524 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3525 cmd
->scsi_sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
3527 out_invalid_cdb_field
:
3528 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
3529 cmd
->scsi_sense_reason
= TCM_INVALID_CDB_FIELD
;
3533 static inline void transport_release_tasks(struct se_cmd
*);
3536 * This function will copy a contiguous *src buffer into a destination
3537 * struct scatterlist array.
3539 static void transport_memcpy_write_contig(
3541 struct scatterlist
*sg_d
,
3544 u32 i
= 0, length
= 0, total_length
= cmd
->data_length
;
3547 while (total_length
) {
3548 length
= sg_d
[i
].length
;
3550 if (length
> total_length
)
3551 length
= total_length
;
3553 dst
= sg_virt(&sg_d
[i
]);
3555 memcpy(dst
, src
, length
);
3557 if (!(total_length
-= length
))
3566 * This function will copy a struct scatterlist array *sg_s into a destination
3567 * contiguous *dst buffer.
3569 static void transport_memcpy_read_contig(
3572 struct scatterlist
*sg_s
)
3574 u32 i
= 0, length
= 0, total_length
= cmd
->data_length
;
3577 while (total_length
) {
3578 length
= sg_s
[i
].length
;
3580 if (length
> total_length
)
3581 length
= total_length
;
3583 src
= sg_virt(&sg_s
[i
]);
3585 memcpy(dst
, src
, length
);
3587 if (!(total_length
-= length
))
3595 static void transport_memcpy_se_mem_read_contig(
3598 struct list_head
*se_mem_list
)
3600 struct se_mem
*se_mem
;
3602 u32 length
= 0, total_length
= cmd
->data_length
;
3604 list_for_each_entry(se_mem
, se_mem_list
, se_list
) {
3605 length
= se_mem
->se_len
;
3607 if (length
> total_length
)
3608 length
= total_length
;
3610 src
= page_address(se_mem
->se_page
) + se_mem
->se_off
;
3612 memcpy(dst
, src
, length
);
3614 if (!(total_length
-= length
))
3622 * Called from transport_generic_complete_ok() and
3623 * transport_generic_request_failure() to determine which dormant/delayed
3624 * and ordered cmds need to have their tasks added to the execution queue.
3626 static void transport_complete_task_attr(struct se_cmd
*cmd
)
3628 struct se_device
*dev
= cmd
->se_dev
;
3629 struct se_cmd
*cmd_p
, *cmd_tmp
;
3630 int new_active_tasks
= 0;
3632 if (cmd
->sam_task_attr
== MSG_SIMPLE_TAG
) {
3633 atomic_dec(&dev
->simple_cmds
);
3634 smp_mb__after_atomic_dec();
3635 dev
->dev_cur_ordered_id
++;
3636 DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
3637 " SIMPLE: %u\n", dev
->dev_cur_ordered_id
,
3638 cmd
->se_ordered_id
);
3639 } else if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
3640 atomic_dec(&dev
->dev_hoq_count
);
3641 smp_mb__after_atomic_dec();
3642 dev
->dev_cur_ordered_id
++;
3643 DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
3644 " HEAD_OF_QUEUE: %u\n", dev
->dev_cur_ordered_id
,
3645 cmd
->se_ordered_id
);
3646 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
3647 spin_lock(&dev
->ordered_cmd_lock
);
3648 list_del(&cmd
->se_ordered_node
);
3649 atomic_dec(&dev
->dev_ordered_sync
);
3650 smp_mb__after_atomic_dec();
3651 spin_unlock(&dev
->ordered_cmd_lock
);
3653 dev
->dev_cur_ordered_id
++;
3654 DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
3655 " %u\n", dev
->dev_cur_ordered_id
, cmd
->se_ordered_id
);
3658 * Process all commands up to the last received
3659 * ORDERED task attribute which requires another blocking
3662 spin_lock(&dev
->delayed_cmd_lock
);
3663 list_for_each_entry_safe(cmd_p
, cmd_tmp
,
3664 &dev
->delayed_cmd_list
, se_delayed_node
) {
3666 list_del(&cmd_p
->se_delayed_node
);
3667 spin_unlock(&dev
->delayed_cmd_lock
);
3669 DEBUG_STA("Calling add_tasks() for"
3670 " cmd_p: 0x%02x Task Attr: 0x%02x"
3671 " Dormant -> Active, se_ordered_id: %u\n",
3672 T_TASK(cmd_p
)->t_task_cdb
[0],
3673 cmd_p
->sam_task_attr
, cmd_p
->se_ordered_id
);
3675 transport_add_tasks_from_cmd(cmd_p
);
3678 spin_lock(&dev
->delayed_cmd_lock
);
3679 if (cmd_p
->sam_task_attr
== MSG_ORDERED_TAG
)
3682 spin_unlock(&dev
->delayed_cmd_lock
);
3684 * If new tasks have become active, wake up the transport thread
3685 * to do the processing of the Active tasks.
3687 if (new_active_tasks
!= 0)
3688 wake_up_interruptible(&dev
->dev_queue_obj
.thread_wq
);
3691 static void transport_generic_complete_ok(struct se_cmd
*cmd
)
3695 * Check if we need to move delayed/dormant tasks from cmds on the
3696 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3699 if (cmd
->se_dev
->dev_task_attr_type
== SAM_TASK_ATTR_EMULATED
)
3700 transport_complete_task_attr(cmd
);
3702 * Check if we need to retrieve a sense buffer from
3703 * the struct se_cmd in question.
3705 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
3706 if (transport_get_sense_data(cmd
) < 0)
3707 reason
= TCM_NON_EXISTENT_LUN
;
3710 * Only set when an struct se_task->task_scsi_status returned
3711 * a non GOOD status.
3713 if (cmd
->scsi_status
) {
3714 transport_send_check_condition_and_sense(
3716 transport_lun_remove_cmd(cmd
);
3717 transport_cmd_check_stop_to_fabric(cmd
);
3722 * Check for a callback, used by amongst other things
3723 * XDWRITE_READ_10 emulation.
3725 if (cmd
->transport_complete_callback
)
3726 cmd
->transport_complete_callback(cmd
);
3728 switch (cmd
->data_direction
) {
3729 case DMA_FROM_DEVICE
:
3730 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3731 if (cmd
->se_lun
->lun_sep
) {
3732 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
3735 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3737 * If enabled by TCM fabirc module pre-registered SGL
3738 * memory, perform the memcpy() from the TCM internal
3739 * contigious buffer back to the original SGL.
3741 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_CONTIG_TO_SG
)
3742 transport_memcpy_write_contig(cmd
,
3743 cmd
->t_task
.t_task_pt_sgl
,
3744 cmd
->t_task
.t_task_buf
);
3746 cmd
->se_tfo
->queue_data_in(cmd
);
3749 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3750 if (cmd
->se_lun
->lun_sep
) {
3751 cmd
->se_lun
->lun_sep
->sep_stats
.rx_data_octets
+=
3754 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3756 * Check if we need to send READ payload for BIDI-COMMAND
3758 if (!list_empty(&cmd
->t_task
.t_mem_bidi_list
)) {
3759 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
3760 if (cmd
->se_lun
->lun_sep
) {
3761 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
3764 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
3765 cmd
->se_tfo
->queue_data_in(cmd
);
3768 /* Fall through for DMA_TO_DEVICE */
3770 cmd
->se_tfo
->queue_status(cmd
);
3776 transport_lun_remove_cmd(cmd
);
3777 transport_cmd_check_stop_to_fabric(cmd
);
3780 static void transport_free_dev_tasks(struct se_cmd
*cmd
)
3782 struct se_task
*task
, *task_tmp
;
3783 unsigned long flags
;
3785 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
3786 list_for_each_entry_safe(task
, task_tmp
,
3787 &cmd
->t_task
.t_task_list
, t_list
) {
3788 if (atomic_read(&task
->task_active
))
3791 kfree(task
->task_sg_bidi
);
3792 kfree(task
->task_sg
);
3794 list_del(&task
->t_list
);
3796 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
3798 task
->se_dev
->transport
->free_task(task
);
3800 printk(KERN_ERR
"task[%u] - task->se_dev is NULL\n",
3802 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
3804 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
3807 static inline void transport_free_pages(struct se_cmd
*cmd
)
3809 struct se_mem
*se_mem
, *se_mem_tmp
;
3812 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
)
3814 if (cmd
->se_dev
->transport
->do_se_mem_map
)
3817 if (cmd
->t_task
.t_task_buf
) {
3818 kfree(cmd
->t_task
.t_task_buf
);
3819 cmd
->t_task
.t_task_buf
= NULL
;
3824 * Caller will handle releasing of struct se_mem.
3826 if (cmd
->se_cmd_flags
& SCF_CMD_PASSTHROUGH_NOALLOC
)
3829 list_for_each_entry_safe(se_mem
, se_mem_tmp
,
3830 &cmd
->t_task
.t_mem_list
, se_list
) {
3832 * We only release call __free_page(struct se_mem->se_page) when
3833 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3836 __free_page(se_mem
->se_page
);
3838 list_del(&se_mem
->se_list
);
3839 kmem_cache_free(se_mem_cache
, se_mem
);
3841 cmd
->t_task
.t_tasks_se_num
= 0;
3843 list_for_each_entry_safe(se_mem
, se_mem_tmp
,
3844 &cmd
->t_task
.t_mem_bidi_list
, se_list
) {
3846 * We only release call __free_page(struct se_mem->se_page) when
3847 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3850 __free_page(se_mem
->se_page
);
3852 list_del(&se_mem
->se_list
);
3853 kmem_cache_free(se_mem_cache
, se_mem
);
3855 cmd
->t_task
.t_tasks_se_bidi_num
= 0;
3858 static inline void transport_release_tasks(struct se_cmd
*cmd
)
3860 transport_free_dev_tasks(cmd
);
3863 static inline int transport_dec_and_check(struct se_cmd
*cmd
)
3865 unsigned long flags
;
3867 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
3868 if (atomic_read(&cmd
->t_task
.t_fe_count
)) {
3869 if (!(atomic_dec_and_test(&cmd
->t_task
.t_fe_count
))) {
3870 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
,
3876 if (atomic_read(&cmd
->t_task
.t_se_count
)) {
3877 if (!(atomic_dec_and_test(&cmd
->t_task
.t_se_count
))) {
3878 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
,
3883 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
3888 static void transport_release_fe_cmd(struct se_cmd
*cmd
)
3890 unsigned long flags
;
3892 if (transport_dec_and_check(cmd
))
3895 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
3896 if (!(atomic_read(&cmd
->t_task
.transport_dev_active
))) {
3897 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
3900 atomic_set(&cmd
->t_task
.transport_dev_active
, 0);
3901 transport_all_task_dev_remove_state(cmd
);
3902 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
3904 transport_release_tasks(cmd
);
3906 transport_free_pages(cmd
);
3907 transport_free_se_cmd(cmd
);
3908 cmd
->se_tfo
->release_cmd_direct(cmd
);
3911 static int transport_generic_remove(
3913 int release_to_pool
,
3914 int session_reinstatement
)
3916 unsigned long flags
;
3918 if (transport_dec_and_check(cmd
)) {
3919 if (session_reinstatement
) {
3920 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
3921 transport_all_task_dev_remove_state(cmd
);
3922 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
,
3928 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
3929 if (!(atomic_read(&cmd
->t_task
.transport_dev_active
))) {
3930 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
3933 atomic_set(&cmd
->t_task
.transport_dev_active
, 0);
3934 transport_all_task_dev_remove_state(cmd
);
3935 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
3937 transport_release_tasks(cmd
);
3940 transport_free_pages(cmd
);
3942 if (release_to_pool
) {
3943 transport_release_cmd_to_pool(cmd
);
3945 transport_free_se_cmd(cmd
);
3946 cmd
->se_tfo
->release_cmd_direct(cmd
);
3953 * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
3954 * @cmd: Associated se_cmd descriptor
3955 * @mem: SGL style memory for TCM WRITE / READ
3956 * @sg_mem_num: Number of SGL elements
3957 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3958 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3960 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3963 int transport_generic_map_mem_to_cmd(
3965 struct scatterlist
*sgl
,
3967 struct scatterlist
*sgl_bidi
,
3970 u32 mapped_sg_count
= 0;
3973 if (!sgl
|| !sgl_count
)
3977 * Convert sgls (sgl, sgl_bidi) to list of se_mems
3979 if ((cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
) ||
3980 (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
)) {
3982 * For CDB using TCM struct se_mem linked list scatterlist memory
3983 * processed into a TCM struct se_subsystem_dev, we do the mapping
3984 * from the passed physical memory to struct se_mem->se_page here.
3986 ret
= transport_map_sg_to_mem(cmd
,
3987 &cmd
->t_task
.t_mem_list
, sgl
, &mapped_sg_count
);
3991 cmd
->t_task
.t_tasks_se_num
= mapped_sg_count
;
3993 * Setup BIDI READ list of struct se_mem elements
3995 if (sgl_bidi
&& sgl_bidi_count
) {
3996 mapped_sg_count
= 0;
3997 ret
= transport_map_sg_to_mem(cmd
,
3998 &cmd
->t_task
.t_mem_bidi_list
, sgl_bidi
,
4003 cmd
->t_task
.t_tasks_se_bidi_num
= mapped_sg_count
;
4005 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
;
4007 } else if (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_NONSG_IO_CDB
) {
4008 if (sgl_bidi
|| sgl_bidi_count
) {
4009 printk(KERN_ERR
"BIDI-Commands not supported using "
4010 "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
4014 * For incoming CDBs using a contiguous buffer internall with TCM,
4015 * save the passed struct scatterlist memory. After TCM storage object
4016 * processing has completed for this struct se_cmd, TCM core will call
4017 * transport_memcpy_[write,read]_contig() as necessary from
4018 * transport_generic_complete_ok() and transport_write_pending() in order
4019 * to copy the TCM buffer to/from the original passed *mem in SGL ->
4020 * struct scatterlist format.
4022 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_CONTIG_TO_SG
;
4023 cmd
->t_task
.t_task_pt_sgl
= sgl
;
4024 /* don't need sgl count? We assume it contains cmd->data_length data */
4029 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd
);
4032 static inline long long transport_dev_end_lba(struct se_device
*dev
)
4034 return dev
->transport
->get_blocks(dev
) + 1;
4037 static int transport_get_sectors(struct se_cmd
*cmd
)
4039 struct se_device
*dev
= cmd
->se_dev
;
4041 cmd
->t_task
.t_tasks_sectors
=
4042 (cmd
->data_length
/ dev
->se_sub_dev
->se_dev_attrib
.block_size
);
4043 if (!(cmd
->t_task
.t_tasks_sectors
))
4044 cmd
->t_task
.t_tasks_sectors
= 1;
4046 if (dev
->transport
->get_device_type(dev
) != TYPE_DISK
)
4049 if ((cmd
->t_task
.t_task_lba
+ cmd
->t_task
.t_tasks_sectors
) >
4050 transport_dev_end_lba(dev
)) {
4051 printk(KERN_ERR
"LBA: %llu Sectors: %u exceeds"
4052 " transport_dev_end_lba(): %llu\n",
4053 cmd
->t_task
.t_task_lba
, cmd
->t_task
.t_tasks_sectors
,
4054 transport_dev_end_lba(dev
));
4055 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
4056 cmd
->scsi_sense_reason
= TCM_SECTOR_COUNT_TOO_MANY
;
4057 return PYX_TRANSPORT_REQ_TOO_MANY_SECTORS
;
4063 static int transport_new_cmd_obj(struct se_cmd
*cmd
)
4065 struct se_device
*dev
= cmd
->se_dev
;
4066 u32 task_cdbs
= 0, rc
;
4068 if (!(cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
)) {
4070 cmd
->t_task
.t_task_cdbs
++;
4075 * Setup any BIDI READ tasks and memory from
4076 * cmd->t_task.t_mem_bidi_list so the READ struct se_tasks
4077 * are queued first for the non pSCSI passthrough case.
4079 if (!list_empty(&cmd
->t_task
.t_mem_bidi_list
) &&
4080 (dev
->transport
->transport_type
!= TRANSPORT_PLUGIN_PHBA_PDEV
)) {
4081 rc
= transport_generic_get_cdb_count(cmd
,
4082 cmd
->t_task
.t_task_lba
,
4083 cmd
->t_task
.t_tasks_sectors
,
4084 DMA_FROM_DEVICE
, &cmd
->t_task
.t_mem_bidi_list
,
4087 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
4088 cmd
->scsi_sense_reason
=
4089 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
4090 return PYX_TRANSPORT_LU_COMM_FAILURE
;
4095 * Setup the tasks and memory from cmd->t_task.t_mem_list
4096 * Note for BIDI transfers this will contain the WRITE payload
4098 task_cdbs
= transport_generic_get_cdb_count(cmd
,
4099 cmd
->t_task
.t_task_lba
,
4100 cmd
->t_task
.t_tasks_sectors
,
4101 cmd
->data_direction
, &cmd
->t_task
.t_mem_list
,
4104 cmd
->se_cmd_flags
|= SCF_SCSI_CDB_EXCEPTION
;
4105 cmd
->scsi_sense_reason
=
4106 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
4107 return PYX_TRANSPORT_LU_COMM_FAILURE
;
4109 cmd
->t_task
.t_task_cdbs
+= task_cdbs
;
4112 printk(KERN_INFO
"data_length: %u, LBA: %llu t_tasks_sectors:"
4113 " %u, t_task_cdbs: %u\n", obj_ptr
, cmd
->data_length
,
4114 cmd
->t_task
.t_task_lba
, cmd
->t_task
.t_tasks_sectors
,
4115 cmd
->t_task
.t_task_cdbs
);
4119 atomic_set(&cmd
->t_task
.t_task_cdbs_left
, task_cdbs
);
4120 atomic_set(&cmd
->t_task
.t_task_cdbs_ex_left
, task_cdbs
);
4121 atomic_set(&cmd
->t_task
.t_task_cdbs_timeout_left
, task_cdbs
);
4126 transport_generic_get_mem(struct se_cmd
*cmd
, u32 length
, u32 dma_size
)
4129 struct se_mem
*se_mem
;
4132 * If the device uses memory mapping this is enough.
4134 if (cmd
->se_dev
->transport
->do_se_mem_map
)
4138 se_mem
= kmem_cache_zalloc(se_mem_cache
, GFP_KERNEL
);
4140 printk(KERN_ERR
"Unable to allocate struct se_mem\n");
4144 /* #warning FIXME Allocate contigous pages for struct se_mem elements */
4145 se_mem
->se_page
= alloc_pages(GFP_KERNEL
, 0);
4146 if (!(se_mem
->se_page
)) {
4147 printk(KERN_ERR
"alloc_pages() failed\n");
4151 buf
= kmap_atomic(se_mem
->se_page
, KM_IRQ0
);
4153 printk(KERN_ERR
"kmap_atomic() failed\n");
4156 INIT_LIST_HEAD(&se_mem
->se_list
);
4157 se_mem
->se_len
= (length
> dma_size
) ? dma_size
: length
;
4158 memset(buf
, 0, se_mem
->se_len
);
4159 kunmap_atomic(buf
, KM_IRQ0
);
4161 list_add_tail(&se_mem
->se_list
, &cmd
->t_task
.t_mem_list
);
4162 cmd
->t_task
.t_tasks_se_num
++;
4164 DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
4165 " Offset(%u)\n", se_mem
->se_page
, se_mem
->se_len
,
4168 length
-= se_mem
->se_len
;
4171 DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
4172 cmd
->t_task
.t_tasks_se_num
);
4177 __free_pages(se_mem
->se_page
, 0);
4178 kmem_cache_free(se_mem_cache
, se_mem
);
4182 int transport_init_task_sg(
4183 struct se_task
*task
,
4184 struct se_mem
*in_se_mem
,
4187 struct se_cmd
*se_cmd
= task
->task_se_cmd
;
4188 struct se_device
*se_dev
= se_cmd
->se_dev
;
4189 struct se_mem
*se_mem
= in_se_mem
;
4190 struct target_core_fabric_ops
*tfo
= se_cmd
->se_tfo
;
4191 u32 sg_length
, task_size
= task
->task_size
, task_sg_num_padded
;
4193 while (task_size
!= 0) {
4194 DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
4195 " se_mem->se_off(%u) task_offset(%u)\n",
4196 se_mem
->se_page
, se_mem
->se_len
,
4197 se_mem
->se_off
, task_offset
);
4199 if (task_offset
== 0) {
4200 if (task_size
>= se_mem
->se_len
) {
4201 sg_length
= se_mem
->se_len
;
4203 if (!(list_is_last(&se_mem
->se_list
,
4204 &se_cmd
->t_task
.t_mem_list
)))
4205 se_mem
= list_entry(se_mem
->se_list
.next
,
4206 struct se_mem
, se_list
);
4208 sg_length
= task_size
;
4209 task_size
-= sg_length
;
4213 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4214 sg_length
, task_size
);
4216 if ((se_mem
->se_len
- task_offset
) > task_size
) {
4217 sg_length
= task_size
;
4218 task_size
-= sg_length
;
4221 sg_length
= (se_mem
->se_len
- task_offset
);
4223 if (!(list_is_last(&se_mem
->se_list
,
4224 &se_cmd
->t_task
.t_mem_list
)))
4225 se_mem
= list_entry(se_mem
->se_list
.next
,
4226 struct se_mem
, se_list
);
4229 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4230 sg_length
, task_size
);
4234 task_size
-= sg_length
;
4236 DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
4237 task
->task_no
, task_size
);
4239 task
->task_sg_num
++;
4242 * Check if the fabric module driver is requesting that all
4243 * struct se_task->task_sg[] be chained together.. If so,
4244 * then allocate an extra padding SG entry for linking and
4245 * marking the end of the chained SGL.
4247 if (tfo
->task_sg_chaining
) {
4248 task_sg_num_padded
= (task
->task_sg_num
+ 1);
4249 task
->task_padded_sg
= 1;
4251 task_sg_num_padded
= task
->task_sg_num
;
4253 task
->task_sg
= kzalloc(task_sg_num_padded
*
4254 sizeof(struct scatterlist
), GFP_KERNEL
);
4255 if (!(task
->task_sg
)) {
4256 printk(KERN_ERR
"Unable to allocate memory for"
4257 " task->task_sg\n");
4260 sg_init_table(&task
->task_sg
[0], task_sg_num_padded
);
4262 * Setup task->task_sg_bidi for SCSI READ payload for
4263 * TCM/pSCSI passthrough if present for BIDI-COMMAND
4265 if (!list_empty(&se_cmd
->t_task
.t_mem_bidi_list
) &&
4266 (se_dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)) {
4267 task
->task_sg_bidi
= kzalloc(task_sg_num_padded
*
4268 sizeof(struct scatterlist
), GFP_KERNEL
);
4269 if (!(task
->task_sg_bidi
)) {
4270 kfree(task
->task_sg
);
4271 task
->task_sg
= NULL
;
4272 printk(KERN_ERR
"Unable to allocate memory for"
4273 " task->task_sg_bidi\n");
4276 sg_init_table(&task
->task_sg_bidi
[0], task_sg_num_padded
);
4279 * For the chaining case, setup the proper end of SGL for the
4280 * initial submission struct task into struct se_subsystem_api.
4281 * This will be cleared later by transport_do_task_sg_chain()
4283 if (task
->task_padded_sg
) {
4284 sg_mark_end(&task
->task_sg
[task
->task_sg_num
- 1]);
4286 * Added the 'if' check before marking end of bi-directional
4287 * scatterlist (which gets created only in case of request
4290 if (task
->task_sg_bidi
)
4291 sg_mark_end(&task
->task_sg_bidi
[task
->task_sg_num
- 1]);
4294 DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
4295 " task_sg_num_padded(%u)\n", task
->task_sg_num
,
4296 task_sg_num_padded
);
4298 return task
->task_sg_num
;
4301 static inline int transport_set_tasks_sectors_disk(
4302 struct se_task
*task
,
4303 struct se_device
*dev
,
4304 unsigned long long lba
,
4306 int *max_sectors_set
)
4308 if ((lba
+ sectors
) > transport_dev_end_lba(dev
)) {
4309 task
->task_sectors
= ((transport_dev_end_lba(dev
) - lba
) + 1);
4311 if (task
->task_sectors
> dev
->se_sub_dev
->se_dev_attrib
.max_sectors
) {
4312 task
->task_sectors
= dev
->se_sub_dev
->se_dev_attrib
.max_sectors
;
4313 *max_sectors_set
= 1;
4316 if (sectors
> dev
->se_sub_dev
->se_dev_attrib
.max_sectors
) {
4317 task
->task_sectors
= dev
->se_sub_dev
->se_dev_attrib
.max_sectors
;
4318 *max_sectors_set
= 1;
4320 task
->task_sectors
= sectors
;
4326 static inline int transport_set_tasks_sectors_non_disk(
4327 struct se_task
*task
,
4328 struct se_device
*dev
,
4329 unsigned long long lba
,
4331 int *max_sectors_set
)
4333 if (sectors
> dev
->se_sub_dev
->se_dev_attrib
.max_sectors
) {
4334 task
->task_sectors
= dev
->se_sub_dev
->se_dev_attrib
.max_sectors
;
4335 *max_sectors_set
= 1;
4337 task
->task_sectors
= sectors
;
4342 static inline int transport_set_tasks_sectors(
4343 struct se_task
*task
,
4344 struct se_device
*dev
,
4345 unsigned long long lba
,
4347 int *max_sectors_set
)
4349 return (dev
->transport
->get_device_type(dev
) == TYPE_DISK
) ?
4350 transport_set_tasks_sectors_disk(task
, dev
, lba
, sectors
,
4352 transport_set_tasks_sectors_non_disk(task
, dev
, lba
, sectors
,
4357 * Convert a sgl into a linked list of se_mems.
4359 static int transport_map_sg_to_mem(
4361 struct list_head
*se_mem_list
,
4362 struct scatterlist
*sg
,
4365 struct se_mem
*se_mem
;
4366 u32 cmd_size
= cmd
->data_length
;
4372 * NOTE: it is safe to return -ENOMEM at any time in creating this
4373 * list because transport_free_pages() will eventually be called, and is
4374 * smart enough to deallocate all list items for sg and sg_bidi lists.
4376 se_mem
= kmem_cache_zalloc(se_mem_cache
, GFP_KERNEL
);
4378 printk(KERN_ERR
"Unable to allocate struct se_mem\n");
4381 INIT_LIST_HEAD(&se_mem
->se_list
);
4382 DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
4383 " sg_page: %p offset: %d length: %d\n", cmd_size
,
4384 sg_page(sg
), sg
->offset
, sg
->length
);
4386 se_mem
->se_page
= sg_page(sg
);
4387 se_mem
->se_off
= sg
->offset
;
4389 if (cmd_size
> sg
->length
) {
4390 se_mem
->se_len
= sg
->length
;
4393 se_mem
->se_len
= cmd_size
;
4395 cmd_size
-= se_mem
->se_len
;
4398 DEBUG_MEM("sg_to_mem: sg_count: %u cmd_size: %u\n",
4399 sg_count
, cmd_size
);
4400 DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
4401 se_mem
->se_page
, se_mem
->se_off
, se_mem
->se_len
);
4403 list_add_tail(&se_mem
->se_list
, se_mem_list
);
4406 DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments\n", sg_count
);
4411 /* transport_map_mem_to_sg():
4415 int transport_map_mem_to_sg(
4416 struct se_task
*task
,
4417 struct list_head
*se_mem_list
,
4419 struct se_mem
*in_se_mem
,
4420 struct se_mem
**out_se_mem
,
4424 struct se_cmd
*se_cmd
= task
->task_se_cmd
;
4425 struct se_mem
*se_mem
= in_se_mem
;
4426 struct scatterlist
*sg
= (struct scatterlist
*)in_mem
;
4427 u32 task_size
= task
->task_size
, sg_no
= 0;
4430 printk(KERN_ERR
"Unable to locate valid struct"
4431 " scatterlist pointer\n");
4435 while (task_size
!= 0) {
4437 * Setup the contigious array of scatterlists for
4438 * this struct se_task.
4440 sg_assign_page(sg
, se_mem
->se_page
);
4442 if (*task_offset
== 0) {
4443 sg
->offset
= se_mem
->se_off
;
4445 if (task_size
>= se_mem
->se_len
) {
4446 sg
->length
= se_mem
->se_len
;
4448 if (!(list_is_last(&se_mem
->se_list
,
4449 &se_cmd
->t_task
.t_mem_list
))) {
4450 se_mem
= list_entry(se_mem
->se_list
.next
,
4451 struct se_mem
, se_list
);
4455 sg
->length
= task_size
;
4457 * Determine if we need to calculate an offset
4458 * into the struct se_mem on the next go around..
4460 task_size
-= sg
->length
;
4462 *task_offset
= sg
->length
;
4468 sg
->offset
= (*task_offset
+ se_mem
->se_off
);
4470 if ((se_mem
->se_len
- *task_offset
) > task_size
) {
4471 sg
->length
= task_size
;
4473 * Determine if we need to calculate an offset
4474 * into the struct se_mem on the next go around..
4476 task_size
-= sg
->length
;
4478 *task_offset
+= sg
->length
;
4482 sg
->length
= (se_mem
->se_len
- *task_offset
);
4484 if (!(list_is_last(&se_mem
->se_list
,
4485 &se_cmd
->t_task
.t_mem_list
))) {
4486 se_mem
= list_entry(se_mem
->se_list
.next
,
4487 struct se_mem
, se_list
);
4494 task_size
-= sg
->length
;
4496 DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
4497 " task_size to(%u), task_offset: %u\n", task
->task_no
, sg_no
,
4498 sg_page(sg
), sg
->length
, sg
->offset
, task_size
, *task_offset
);
4506 if (task_size
> se_cmd
->data_length
)
4509 *out_se_mem
= se_mem
;
4511 DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
4512 " SGs\n", task
->task_no
, *se_mem_cnt
, sg_no
);
4518 * This function can be used by HW target mode drivers to create a linked
4519 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4520 * This is intended to be called during the completion path by TCM Core
4521 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4523 void transport_do_task_sg_chain(struct se_cmd
*cmd
)
4525 struct scatterlist
*sg_head
= NULL
, *sg_link
= NULL
, *sg_first
= NULL
;
4526 struct scatterlist
*sg_head_cur
= NULL
, *sg_link_cur
= NULL
;
4527 struct scatterlist
*sg
, *sg_end
= NULL
, *sg_end_cur
= NULL
;
4528 struct se_task
*task
;
4529 struct target_core_fabric_ops
*tfo
= cmd
->se_tfo
;
4530 u32 task_sg_num
= 0, sg_count
= 0;
4533 if (tfo
->task_sg_chaining
== 0) {
4534 printk(KERN_ERR
"task_sg_chaining is diabled for fabric module:"
4535 " %s\n", tfo
->get_fabric_name());
4540 * Walk the struct se_task list and setup scatterlist chains
4541 * for each contiguosly allocated struct se_task->task_sg[].
4543 list_for_each_entry(task
, &cmd
->t_task
.t_task_list
, t_list
) {
4544 if (!(task
->task_sg
) || !(task
->task_padded_sg
))
4547 if (sg_head
&& sg_link
) {
4548 sg_head_cur
= &task
->task_sg
[0];
4549 sg_link_cur
= &task
->task_sg
[task
->task_sg_num
];
4551 * Either add chain or mark end of scatterlist
4553 if (!(list_is_last(&task
->t_list
,
4554 &cmd
->t_task
.t_task_list
))) {
4556 * Clear existing SGL termination bit set in
4557 * transport_init_task_sg(), see sg_mark_end()
4559 sg_end_cur
= &task
->task_sg
[task
->task_sg_num
- 1];
4560 sg_end_cur
->page_link
&= ~0x02;
4562 sg_chain(sg_head
, task_sg_num
, sg_head_cur
);
4563 sg_count
+= task
->task_sg_num
;
4564 task_sg_num
= (task
->task_sg_num
+ 1);
4566 sg_chain(sg_head
, task_sg_num
, sg_head_cur
);
4567 sg_count
+= task
->task_sg_num
;
4568 task_sg_num
= task
->task_sg_num
;
4571 sg_head
= sg_head_cur
;
4572 sg_link
= sg_link_cur
;
4575 sg_head
= sg_first
= &task
->task_sg
[0];
4576 sg_link
= &task
->task_sg
[task
->task_sg_num
];
4578 * Check for single task..
4580 if (!(list_is_last(&task
->t_list
, &cmd
->t_task
.t_task_list
))) {
4582 * Clear existing SGL termination bit set in
4583 * transport_init_task_sg(), see sg_mark_end()
4585 sg_end
= &task
->task_sg
[task
->task_sg_num
- 1];
4586 sg_end
->page_link
&= ~0x02;
4587 sg_count
+= task
->task_sg_num
;
4588 task_sg_num
= (task
->task_sg_num
+ 1);
4590 sg_count
+= task
->task_sg_num
;
4591 task_sg_num
= task
->task_sg_num
;
4595 * Setup the starting pointer and total t_tasks_sg_linked_no including
4596 * padding SGs for linking and to mark the end.
4598 cmd
->t_task
.t_tasks_sg_chained
= sg_first
;
4599 cmd
->t_task
.t_tasks_sg_chained_no
= sg_count
;
4601 DEBUG_CMD_M("Setup cmd: %p cmd->t_task.t_tasks_sg_chained: %p and"
4602 " t_tasks_sg_chained_no: %u\n", cmd
, cmd
->t_task
.t_tasks_sg_chained
,
4603 cmd
->t_task
.t_tasks_sg_chained_no
);
4605 for_each_sg(cmd
->t_task
.t_tasks_sg_chained
, sg
,
4606 cmd
->t_task
.t_tasks_sg_chained_no
, i
) {
4608 DEBUG_CMD_M("SG[%d]: %p page: %p length: %d offset: %d\n",
4609 i
, sg
, sg_page(sg
), sg
->length
, sg
->offset
);
4610 if (sg_is_chain(sg
))
4611 DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg
);
4613 DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg
);
4616 EXPORT_SYMBOL(transport_do_task_sg_chain
);
4618 static int transport_do_se_mem_map(
4619 struct se_device
*dev
,
4620 struct se_task
*task
,
4621 struct list_head
*se_mem_list
,
4623 struct se_mem
*in_se_mem
,
4624 struct se_mem
**out_se_mem
,
4626 u32
*task_offset_in
)
4628 u32 task_offset
= *task_offset_in
;
4631 * se_subsystem_api_t->do_se_mem_map is used when internal allocation
4632 * has been done by the transport plugin.
4634 if (dev
->transport
->do_se_mem_map
) {
4635 ret
= dev
->transport
->do_se_mem_map(task
, se_mem_list
,
4636 in_mem
, in_se_mem
, out_se_mem
, se_mem_cnt
,
4639 task
->task_se_cmd
->t_task
.t_tasks_se_num
+= *se_mem_cnt
;
4644 BUG_ON(list_empty(se_mem_list
));
4646 * This is the normal path for all normal non BIDI and BIDI-COMMAND
4647 * WRITE payloads.. If we need to do BIDI READ passthrough for
4648 * TCM/pSCSI the first call to transport_do_se_mem_map ->
4649 * transport_init_task_sg() -> transport_map_mem_to_sg() will do the
4650 * allocation for task->task_sg_bidi, and the subsequent call to
4651 * transport_do_se_mem_map() from transport_generic_get_cdb_count()
4653 if (!(task
->task_sg_bidi
)) {
4655 * Assume default that transport plugin speaks preallocated
4658 ret
= transport_init_task_sg(task
, in_se_mem
, task_offset
);
4662 * struct se_task->task_sg now contains the struct scatterlist array.
4664 return transport_map_mem_to_sg(task
, se_mem_list
, task
->task_sg
,
4665 in_se_mem
, out_se_mem
, se_mem_cnt
,
4669 * Handle the se_mem_list -> struct task->task_sg_bidi
4670 * memory map for the extra BIDI READ payload
4672 return transport_map_mem_to_sg(task
, se_mem_list
, task
->task_sg_bidi
,
4673 in_se_mem
, out_se_mem
, se_mem_cnt
,
4677 static u32
transport_generic_get_cdb_count(
4679 unsigned long long lba
,
4681 enum dma_data_direction data_direction
,
4682 struct list_head
*mem_list
,
4685 unsigned char *cdb
= NULL
;
4686 struct se_task
*task
;
4687 struct se_mem
*se_mem
= NULL
, *se_mem_lout
= NULL
;
4688 struct se_mem
*se_mem_bidi
= NULL
, *se_mem_bidi_lout
= NULL
;
4689 struct se_device
*dev
= cmd
->se_dev
;
4690 int max_sectors_set
= 0, ret
;
4691 u32 task_offset_in
= 0, se_mem_cnt
= 0, se_mem_bidi_cnt
= 0, task_cdbs
= 0;
4694 printk(KERN_ERR
"mem_list is NULL in transport_generic_get"
4699 * While using RAMDISK_DR backstores is the only case where
4700 * mem_list will ever be empty at this point.
4702 if (!(list_empty(mem_list
)))
4703 se_mem
= list_first_entry(mem_list
, struct se_mem
, se_list
);
4705 * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
4706 * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
4708 if (!list_empty(&cmd
->t_task
.t_mem_bidi_list
) &&
4709 (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
))
4710 se_mem_bidi
= list_first_entry(&cmd
->t_task
.t_mem_bidi_list
,
4711 struct se_mem
, se_list
);
4714 DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
4715 cmd
->se_tfo
->get_task_tag(cmd
), lba
, sectors
,
4716 transport_dev_end_lba(dev
));
4718 task
= transport_generic_get_task(cmd
, data_direction
);
4722 transport_set_tasks_sectors(task
, dev
, lba
, sectors
,
4725 task
->task_lba
= lba
;
4726 lba
+= task
->task_sectors
;
4727 sectors
-= task
->task_sectors
;
4728 task
->task_size
= (task
->task_sectors
*
4729 dev
->se_sub_dev
->se_dev_attrib
.block_size
);
4731 cdb
= dev
->transport
->get_cdb(task
);
4733 memcpy(cdb
, cmd
->t_task
.t_task_cdb
,
4734 scsi_command_size(cmd
->t_task
.t_task_cdb
));
4735 cmd
->transport_split_cdb(task
->task_lba
,
4736 &task
->task_sectors
, cdb
);
4740 * Perform the SE OBJ plugin and/or Transport plugin specific
4741 * mapping for cmd->t_task.t_mem_list. And setup the
4742 * task->task_sg and if necessary task->task_sg_bidi
4744 ret
= transport_do_se_mem_map(dev
, task
, mem_list
,
4745 NULL
, se_mem
, &se_mem_lout
, &se_mem_cnt
,
4750 se_mem
= se_mem_lout
;
4752 * Setup the cmd->t_task.t_mem_bidi_list -> task->task_sg_bidi
4753 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
4755 * Note that the first call to transport_do_se_mem_map() above will
4756 * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
4757 * -> transport_init_task_sg(), and the second here will do the
4758 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
4760 if (task
->task_sg_bidi
!= NULL
) {
4761 ret
= transport_do_se_mem_map(dev
, task
,
4762 &cmd
->t_task
.t_mem_bidi_list
, NULL
,
4763 se_mem_bidi
, &se_mem_bidi_lout
, &se_mem_bidi_cnt
,
4768 se_mem_bidi
= se_mem_bidi_lout
;
4772 DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
4773 task_cdbs
, task
->task_sg_num
);
4775 if (max_sectors_set
) {
4776 max_sectors_set
= 0;
4785 atomic_inc(&cmd
->t_task
.t_fe_count
);
4786 atomic_inc(&cmd
->t_task
.t_se_count
);
4789 DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
4790 cmd
->se_tfo
->get_task_tag(cmd
), (data_direction
== DMA_TO_DEVICE
)
4791 ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs
);
4799 transport_map_control_cmd_to_task(struct se_cmd
*cmd
)
4801 struct se_device
*dev
= cmd
->se_dev
;
4803 struct se_task
*task
;
4806 task
= transport_generic_get_task(cmd
, cmd
->data_direction
);
4808 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES
;
4810 cdb
= dev
->transport
->get_cdb(task
);
4812 memcpy(cdb
, cmd
->t_task
.t_task_cdb
,
4813 scsi_command_size(cmd
->t_task
.t_task_cdb
));
4815 task
->task_size
= cmd
->data_length
;
4817 (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
) ? 1 : 0;
4819 atomic_inc(&cmd
->t_task
.t_fe_count
);
4820 atomic_inc(&cmd
->t_task
.t_se_count
);
4822 if (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_SG_IO_CDB
) {
4823 struct se_mem
*se_mem
= NULL
, *se_mem_lout
= NULL
;
4824 u32 se_mem_cnt
= 0, task_offset
= 0;
4826 if (!list_empty(&cmd
->t_task
.t_mem_list
))
4827 se_mem
= list_first_entry(&cmd
->t_task
.t_mem_list
,
4828 struct se_mem
, se_list
);
4830 ret
= transport_do_se_mem_map(dev
, task
,
4831 &cmd
->t_task
.t_mem_list
, NULL
, se_mem
,
4832 &se_mem_lout
, &se_mem_cnt
, &task_offset
);
4834 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES
;
4836 if (dev
->transport
->map_task_SG
)
4837 return dev
->transport
->map_task_SG(task
);
4839 } else if (cmd
->se_cmd_flags
& SCF_SCSI_CONTROL_NONSG_IO_CDB
) {
4840 if (dev
->transport
->map_task_non_SG
)
4841 return dev
->transport
->map_task_non_SG(task
);
4843 } else if (cmd
->se_cmd_flags
& SCF_SCSI_NON_DATA_CDB
) {
4844 if (dev
->transport
->cdb_none
)
4845 return dev
->transport
->cdb_none(task
);
4849 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES
;
4853 /* transport_generic_new_cmd(): Called from transport_processing_thread()
4855 * Allocate storage transport resources from a set of values predefined
4856 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4857 * Any non zero return here is treated as an "out of resource' op here.
4860 * Generate struct se_task(s) and/or their payloads for this CDB.
4862 static int transport_generic_new_cmd(struct se_cmd
*cmd
)
4864 struct se_portal_group
*se_tpg
;
4865 struct se_task
*task
;
4866 struct se_device
*dev
= cmd
->se_dev
;
4870 * Determine is the TCM fabric module has already allocated physical
4871 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4872 * to setup beforehand the linked list of physical memory at
4873 * cmd->t_task.t_mem_list of struct se_mem->se_page
4875 if (!(cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
)) {
4876 ret
= transport_allocate_resources(cmd
);
4881 ret
= transport_get_sectors(cmd
);
4885 ret
= transport_new_cmd_obj(cmd
);
4890 * Determine if the calling TCM fabric module is talking to
4891 * Linux/NET via kernel sockets and needs to allocate a
4892 * struct iovec array to complete the struct se_cmd
4894 se_tpg
= cmd
->se_lun
->lun_sep
->sep_tpg
;
4895 if (se_tpg
->se_tpg_tfo
->alloc_cmd_iovecs
!= NULL
) {
4896 ret
= se_tpg
->se_tpg_tfo
->alloc_cmd_iovecs(cmd
);
4898 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES
;
4901 if (cmd
->se_cmd_flags
& SCF_SCSI_DATA_SG_IO_CDB
) {
4902 list_for_each_entry(task
, &cmd
->t_task
.t_task_list
, t_list
) {
4903 if (atomic_read(&task
->task_sent
))
4905 if (!dev
->transport
->map_task_SG
)
4908 ret
= dev
->transport
->map_task_SG(task
);
4913 ret
= transport_map_control_cmd_to_task(cmd
);
4919 * For WRITEs, let the iSCSI Target RX Thread know its buffer is ready..
4920 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4921 * will be added to the struct se_device execution queue after its WRITE
4922 * data has arrived. (ie: It gets handled by the transport processing
4923 * thread a second time)
4925 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
4926 transport_add_tasks_to_state_queue(cmd
);
4927 return transport_generic_write_pending(cmd
);
4930 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4931 * to the execution queue.
4933 transport_execute_tasks(cmd
);
4937 /* transport_generic_process_write():
4941 void transport_generic_process_write(struct se_cmd
*cmd
)
4945 * Copy SCSI Presented DTL sector(s) from received buffers allocated to
4948 if (cmd
->se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
4949 if (!cmd
->t_task
.t_tasks_se_num
) {
4950 unsigned char *dst
, *buf
=
4951 (unsigned char *)cmd
->t_task
.t_task_buf
;
4953 dst
= kzalloc(cmd
->cmd_spdtl
), GFP_KERNEL
);
4955 printk(KERN_ERR
"Unable to allocate memory for"
4956 " WRITE underflow\n");
4957 transport_generic_request_failure(cmd
, NULL
,
4958 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS
, 1);
4961 memcpy(dst
, buf
, cmd
->cmd_spdtl
);
4963 kfree(cmd
->t_task
.t_task_buf
);
4964 cmd
->t_task
.t_task_buf
= dst
;
4966 struct scatterlist
*sg
=
4967 (struct scatterlist
*sg
)cmd
->t_task
.t_task_buf
;
4968 struct scatterlist
*orig_sg
;
4970 orig_sg
= kzalloc(sizeof(struct scatterlist
) *
4971 cmd
->t_task
.t_tasks_se_num
,
4974 printk(KERN_ERR
"Unable to allocate memory"
4975 " for WRITE underflow\n");
4976 transport_generic_request_failure(cmd
, NULL
,
4977 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS
, 1);
4981 memcpy(orig_sg
, cmd
->t_task
.t_task_buf
,
4982 sizeof(struct scatterlist
) *
4983 cmd
->t_task
.t_tasks_se_num
);
4985 cmd
->data_length
= cmd
->cmd_spdtl
;
4987 * FIXME, clear out original struct se_task and state
4990 if (transport_generic_new_cmd(cmd
) < 0) {
4991 transport_generic_request_failure(cmd
, NULL
,
4992 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS
, 1);
4997 transport_memcpy_write_sg(cmd
, orig_sg
);
5001 transport_execute_tasks(cmd
);
5003 EXPORT_SYMBOL(transport_generic_process_write
);
5005 /* transport_generic_write_pending():
5009 static int transport_generic_write_pending(struct se_cmd
*cmd
)
5011 unsigned long flags
;
5014 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5015 cmd
->t_state
= TRANSPORT_WRITE_PENDING
;
5016 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5018 * For the TCM control CDBs using a contiguous buffer, do the memcpy
5019 * from the passed Linux/SCSI struct scatterlist located at
5020 * se_cmd->t_task.t_task_pt_buf to the contiguous buffer at
5021 * se_cmd->t_task.t_task_buf.
5023 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_CONTIG_TO_SG
)
5024 transport_memcpy_read_contig(cmd
,
5025 cmd
->t_task
.t_task_buf
,
5026 cmd
->t_task
.t_task_pt_sgl
);
5028 * Clear the se_cmd for WRITE_PENDING status in order to set
5029 * cmd->t_task.t_transport_active=0 so that transport_generic_handle_data
5030 * can be called from HW target mode interrupt code. This is safe
5031 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
5032 * because the se_cmd->se_lun pointer is not being cleared.
5034 transport_cmd_check_stop(cmd
, 1, 0);
5037 * Call the fabric write_pending function here to let the
5038 * frontend know that WRITE buffers are ready.
5040 ret
= cmd
->se_tfo
->write_pending(cmd
);
5044 return PYX_TRANSPORT_WRITE_PENDING
;
5047 /* transport_release_cmd_to_pool():
5051 void transport_release_cmd_to_pool(struct se_cmd
*cmd
)
5053 BUG_ON(!cmd
->se_tfo
);
5055 transport_free_se_cmd(cmd
);
5056 cmd
->se_tfo
->release_cmd_to_pool(cmd
);
5058 EXPORT_SYMBOL(transport_release_cmd_to_pool
);
5060 /* transport_generic_free_cmd():
5062 * Called from processing frontend to release storage engine resources
5064 void transport_generic_free_cmd(
5067 int release_to_pool
,
5068 int session_reinstatement
)
5070 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
))
5071 transport_release_cmd_to_pool(cmd
);
5073 core_dec_lacl_count(cmd
->se_sess
->se_node_acl
, cmd
);
5077 printk(KERN_INFO
"cmd: %p ITT: 0x%08x contains"
5078 " cmd->se_lun\n", cmd
,
5079 cmd
->se_tfo
->get_task_tag(cmd
));
5081 transport_lun_remove_cmd(cmd
);
5084 if (wait_for_tasks
&& cmd
->transport_wait_for_tasks
)
5085 cmd
->transport_wait_for_tasks(cmd
, 0, 0);
5087 transport_free_dev_tasks(cmd
);
5089 transport_generic_remove(cmd
, release_to_pool
,
5090 session_reinstatement
);
5093 EXPORT_SYMBOL(transport_generic_free_cmd
);
5095 static void transport_nop_wait_for_tasks(
5098 int session_reinstatement
)
5103 /* transport_lun_wait_for_tasks():
5105 * Called from ConfigFS context to stop the passed struct se_cmd to allow
5106 * an struct se_lun to be successfully shutdown.
5108 static int transport_lun_wait_for_tasks(struct se_cmd
*cmd
, struct se_lun
*lun
)
5110 unsigned long flags
;
5113 * If the frontend has already requested this struct se_cmd to
5114 * be stopped, we can safely ignore this struct se_cmd.
5116 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5117 if (atomic_read(&cmd
->t_task
.t_transport_stop
)) {
5118 atomic_set(&cmd
->t_task
.transport_lun_stop
, 0);
5119 DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
5120 " TRUE, skipping\n", cmd
->se_tfo
->get_task_tag(cmd
));
5121 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5122 transport_cmd_check_stop(cmd
, 1, 0);
5125 atomic_set(&cmd
->t_task
.transport_lun_fe_stop
, 1);
5126 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5128 wake_up_interruptible(&cmd
->se_dev
->dev_queue_obj
.thread_wq
);
5130 ret
= transport_stop_tasks_for_cmd(cmd
);
5132 DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
5133 " %d\n", cmd
, cmd
->t_task
.t_task_cdbs
, ret
);
5135 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
5136 cmd
->se_tfo
->get_task_tag(cmd
));
5137 wait_for_completion(&cmd
->t_task
.transport_lun_stop_comp
);
5138 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
5139 cmd
->se_tfo
->get_task_tag(cmd
));
5141 transport_remove_cmd_from_queue(cmd
, &cmd
->se_dev
->dev_queue_obj
);
5146 /* #define DEBUG_CLEAR_LUN */
5147 #ifdef DEBUG_CLEAR_LUN
5148 #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
5150 #define DEBUG_CLEAR_L(x...)
5153 static void __transport_clear_lun_from_sessions(struct se_lun
*lun
)
5155 struct se_cmd
*cmd
= NULL
;
5156 unsigned long lun_flags
, cmd_flags
;
5158 * Do exception processing and return CHECK_CONDITION status to the
5161 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
5162 while (!list_empty(&lun
->lun_cmd_list
)) {
5163 cmd
= list_first_entry(&lun
->lun_cmd_list
,
5164 struct se_cmd
, se_lun_node
);
5165 list_del(&cmd
->se_lun_node
);
5167 atomic_set(&cmd
->t_task
.transport_lun_active
, 0);
5169 * This will notify iscsi_target_transport.c:
5170 * transport_cmd_check_stop() that a LUN shutdown is in
5171 * progress for the iscsi_cmd_t.
5173 spin_lock(&cmd
->t_task
.t_state_lock
);
5174 DEBUG_CLEAR_L("SE_LUN[%d] - Setting cmd->t_task.transport"
5175 "_lun_stop for ITT: 0x%08x\n",
5176 cmd
->se_lun
->unpacked_lun
,
5177 cmd
->se_tfo
->get_task_tag(cmd
));
5178 atomic_set(&cmd
->t_task
.transport_lun_stop
, 1);
5179 spin_unlock(&cmd
->t_task
.t_state_lock
);
5181 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
5183 if (!(cmd
->se_lun
)) {
5184 printk(KERN_ERR
"ITT: 0x%08x, [i,t]_state: %u/%u\n",
5185 cmd
->se_tfo
->get_task_tag(cmd
),
5186 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
5190 * If the Storage engine still owns the iscsi_cmd_t, determine
5191 * and/or stop its context.
5193 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
5194 "_lun_wait_for_tasks()\n", cmd
->se_lun
->unpacked_lun
,
5195 cmd
->se_tfo
->get_task_tag(cmd
));
5197 if (transport_lun_wait_for_tasks(cmd
, cmd
->se_lun
) < 0) {
5198 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
5202 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
5203 "_wait_for_tasks(): SUCCESS\n",
5204 cmd
->se_lun
->unpacked_lun
,
5205 cmd
->se_tfo
->get_task_tag(cmd
));
5207 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, cmd_flags
);
5208 if (!(atomic_read(&cmd
->t_task
.transport_dev_active
))) {
5209 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, cmd_flags
);
5212 atomic_set(&cmd
->t_task
.transport_dev_active
, 0);
5213 transport_all_task_dev_remove_state(cmd
);
5214 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, cmd_flags
);
5216 transport_free_dev_tasks(cmd
);
5218 * The Storage engine stopped this struct se_cmd before it was
5219 * send to the fabric frontend for delivery back to the
5220 * Initiator Node. Return this SCSI CDB back with an
5221 * CHECK_CONDITION status.
5224 transport_send_check_condition_and_sense(cmd
,
5225 TCM_NON_EXISTENT_LUN
, 0);
5227 * If the fabric frontend is waiting for this iscsi_cmd_t to
5228 * be released, notify the waiting thread now that LU has
5229 * finished accessing it.
5231 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, cmd_flags
);
5232 if (atomic_read(&cmd
->t_task
.transport_lun_fe_stop
)) {
5233 DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
5234 " struct se_cmd: %p ITT: 0x%08x\n",
5236 cmd
, cmd
->se_tfo
->get_task_tag(cmd
));
5238 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
,
5240 transport_cmd_check_stop(cmd
, 1, 0);
5241 complete(&cmd
->t_task
.transport_lun_fe_stop_comp
);
5242 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
5245 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
5246 lun
->unpacked_lun
, cmd
->se_tfo
->get_task_tag(cmd
));
5248 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, cmd_flags
);
5249 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
5251 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
5254 static int transport_clear_lun_thread(void *p
)
5256 struct se_lun
*lun
= (struct se_lun
*)p
;
5258 __transport_clear_lun_from_sessions(lun
);
5259 complete(&lun
->lun_shutdown_comp
);
5264 int transport_clear_lun_from_sessions(struct se_lun
*lun
)
5266 struct task_struct
*kt
;
5268 kt
= kthread_run(transport_clear_lun_thread
, lun
,
5269 "tcm_cl_%u", lun
->unpacked_lun
);
5271 printk(KERN_ERR
"Unable to start clear_lun thread\n");
5274 wait_for_completion(&lun
->lun_shutdown_comp
);
5279 /* transport_generic_wait_for_tasks():
5281 * Called from frontend or passthrough context to wait for storage engine
5282 * to pause and/or release frontend generated struct se_cmd.
5284 static void transport_generic_wait_for_tasks(
5287 int session_reinstatement
)
5289 unsigned long flags
;
5291 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
) && !(cmd
->se_tmr_req
))
5294 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5296 * If we are already stopped due to an external event (ie: LUN shutdown)
5297 * sleep until the connection can have the passed struct se_cmd back.
5298 * The cmd->t_task.transport_lun_stopped_sem will be upped by
5299 * transport_clear_lun_from_sessions() once the ConfigFS context caller
5300 * has completed its operation on the struct se_cmd.
5302 if (atomic_read(&cmd
->t_task
.transport_lun_stop
)) {
5304 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
5305 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
5306 "_stop_comp); for ITT: 0x%08x\n",
5307 cmd
->se_tfo
->get_task_tag(cmd
));
5309 * There is a special case for WRITES where a FE exception +
5310 * LUN shutdown means ConfigFS context is still sleeping on
5311 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
5312 * We go ahead and up transport_lun_stop_comp just to be sure
5315 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5316 complete(&cmd
->t_task
.transport_lun_stop_comp
);
5317 wait_for_completion(&cmd
->t_task
.transport_lun_fe_stop_comp
);
5318 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5320 transport_all_task_dev_remove_state(cmd
);
5322 * At this point, the frontend who was the originator of this
5323 * struct se_cmd, now owns the structure and can be released through
5324 * normal means below.
5326 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
5327 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
5328 "stop_comp); for ITT: 0x%08x\n",
5329 cmd
->se_tfo
->get_task_tag(cmd
));
5331 atomic_set(&cmd
->t_task
.transport_lun_stop
, 0);
5333 if (!atomic_read(&cmd
->t_task
.t_transport_active
) ||
5334 atomic_read(&cmd
->t_task
.t_transport_aborted
))
5337 atomic_set(&cmd
->t_task
.t_transport_stop
, 1);
5339 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
5340 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
5341 " = TRUE\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
5342 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
,
5343 cmd
->deferred_t_state
);
5345 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5347 wake_up_interruptible(&cmd
->se_dev
->dev_queue_obj
.thread_wq
);
5349 wait_for_completion(&cmd
->t_task
.t_transport_stop_comp
);
5351 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5352 atomic_set(&cmd
->t_task
.t_transport_active
, 0);
5353 atomic_set(&cmd
->t_task
.t_transport_stop
, 0);
5355 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
5356 "&cmd->t_task.t_transport_stop_comp) for ITT: 0x%08x\n",
5357 cmd
->se_tfo
->get_task_tag(cmd
));
5359 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5363 transport_generic_free_cmd(cmd
, 0, 0, session_reinstatement
);
5366 static int transport_get_sense_codes(
5371 *asc
= cmd
->scsi_asc
;
5372 *ascq
= cmd
->scsi_ascq
;
5377 static int transport_set_sense_codes(
5382 cmd
->scsi_asc
= asc
;
5383 cmd
->scsi_ascq
= ascq
;
5388 int transport_send_check_condition_and_sense(
5393 unsigned char *buffer
= cmd
->sense_buffer
;
5394 unsigned long flags
;
5396 u8 asc
= 0, ascq
= 0;
5398 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5399 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
5400 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5403 cmd
->se_cmd_flags
|= SCF_SENT_CHECK_CONDITION
;
5404 spin_unlock_irqrestore(&cmd
->t_task
.t_state_lock
, flags
);
5406 if (!reason
&& from_transport
)
5409 if (!from_transport
)
5410 cmd
->se_cmd_flags
|= SCF_EMULATED_TASK_SENSE
;
5412 * Data Segment and SenseLength of the fabric response PDU.
5414 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
5415 * from include/scsi/scsi_cmnd.h
5417 offset
= cmd
->se_tfo
->set_fabric_sense_len(cmd
,
5418 TRANSPORT_SENSE_BUFFER
);
5420 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
5421 * SENSE KEY values from include/scsi/scsi.h
5424 case TCM_NON_EXISTENT_LUN
:
5425 case TCM_UNSUPPORTED_SCSI_OPCODE
:
5426 case TCM_SECTOR_COUNT_TOO_MANY
:
5428 buffer
[offset
] = 0x70;
5429 /* ILLEGAL REQUEST */
5430 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
5431 /* INVALID COMMAND OPERATION CODE */
5432 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x20;
5434 case TCM_UNKNOWN_MODE_PAGE
:
5436 buffer
[offset
] = 0x70;
5437 /* ILLEGAL REQUEST */
5438 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
5439 /* INVALID FIELD IN CDB */
5440 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x24;
5442 case TCM_CHECK_CONDITION_ABORT_CMD
:
5444 buffer
[offset
] = 0x70;
5445 /* ABORTED COMMAND */
5446 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
5447 /* BUS DEVICE RESET FUNCTION OCCURRED */
5448 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x29;
5449 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x03;
5451 case TCM_INCORRECT_AMOUNT_OF_DATA
:
5453 buffer
[offset
] = 0x70;
5454 /* ABORTED COMMAND */
5455 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
5457 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x0c;
5458 /* NOT ENOUGH UNSOLICITED DATA */
5459 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x0d;
5461 case TCM_INVALID_CDB_FIELD
:
5463 buffer
[offset
] = 0x70;
5464 /* ABORTED COMMAND */
5465 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
5466 /* INVALID FIELD IN CDB */
5467 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x24;
5469 case TCM_INVALID_PARAMETER_LIST
:
5471 buffer
[offset
] = 0x70;
5472 /* ABORTED COMMAND */
5473 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
5474 /* INVALID FIELD IN PARAMETER LIST */
5475 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x26;
5477 case TCM_UNEXPECTED_UNSOLICITED_DATA
:
5479 buffer
[offset
] = 0x70;
5480 /* ABORTED COMMAND */
5481 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
5483 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x0c;
5484 /* UNEXPECTED_UNSOLICITED_DATA */
5485 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x0c;
5487 case TCM_SERVICE_CRC_ERROR
:
5489 buffer
[offset
] = 0x70;
5490 /* ABORTED COMMAND */
5491 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
5492 /* PROTOCOL SERVICE CRC ERROR */
5493 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x47;
5495 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x05;
5497 case TCM_SNACK_REJECTED
:
5499 buffer
[offset
] = 0x70;
5500 /* ABORTED COMMAND */
5501 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
5503 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x11;
5504 /* FAILED RETRANSMISSION REQUEST */
5505 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = 0x13;
5507 case TCM_WRITE_PROTECTED
:
5509 buffer
[offset
] = 0x70;
5511 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = DATA_PROTECT
;
5512 /* WRITE PROTECTED */
5513 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x27;
5515 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
5517 buffer
[offset
] = 0x70;
5518 /* UNIT ATTENTION */
5519 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
5520 core_scsi3_ua_for_check_condition(cmd
, &asc
, &ascq
);
5521 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = asc
;
5522 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = ascq
;
5524 case TCM_CHECK_CONDITION_NOT_READY
:
5526 buffer
[offset
] = 0x70;
5528 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
5529 transport_get_sense_codes(cmd
, &asc
, &ascq
);
5530 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = asc
;
5531 buffer
[offset
+SPC_ASCQ_KEY_OFFSET
] = ascq
;
5533 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
5536 buffer
[offset
] = 0x70;
5537 /* ILLEGAL REQUEST */
5538 buffer
[offset
+SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
5539 /* LOGICAL UNIT COMMUNICATION FAILURE */
5540 buffer
[offset
+SPC_ASC_KEY_OFFSET
] = 0x80;
5544 * This code uses linux/include/scsi/scsi.h SAM status codes!
5546 cmd
->scsi_status
= SAM_STAT_CHECK_CONDITION
;
5548 * Automatically padded, this value is encoded in the fabric's
5549 * data_length response PDU containing the SCSI defined sense data.
5551 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
+ offset
;
5554 cmd
->se_tfo
->queue_status(cmd
);
5557 EXPORT_SYMBOL(transport_send_check_condition_and_sense
);
5559 int transport_check_aborted_status(struct se_cmd
*cmd
, int send_status
)
5563 if (atomic_read(&cmd
->t_task
.t_transport_aborted
) != 0) {
5564 if (!(send_status
) ||
5565 (cmd
->se_cmd_flags
& SCF_SENT_DELAYED_TAS
))
5568 printk(KERN_INFO
"Sending delayed SAM_STAT_TASK_ABORTED"
5569 " status for CDB: 0x%02x ITT: 0x%08x\n",
5570 cmd
->t_task
.t_task_cdb
[0],
5571 cmd
->se_tfo
->get_task_tag(cmd
));
5573 cmd
->se_cmd_flags
|= SCF_SENT_DELAYED_TAS
;
5574 cmd
->se_tfo
->queue_status(cmd
);
5579 EXPORT_SYMBOL(transport_check_aborted_status
);
5581 void transport_send_task_abort(struct se_cmd
*cmd
)
5584 * If there are still expected incoming fabric WRITEs, we wait
5585 * until until they have completed before sending a TASK_ABORTED
5586 * response. This response with TASK_ABORTED status will be
5587 * queued back to fabric module by transport_check_aborted_status().
5589 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
5590 if (cmd
->se_tfo
->write_pending_status(cmd
) != 0) {
5591 atomic_inc(&cmd
->t_task
.t_transport_aborted
);
5592 smp_mb__after_atomic_inc();
5593 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
5594 transport_new_cmd_failure(cmd
);
5598 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
5600 printk(KERN_INFO
"Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
5601 " ITT: 0x%08x\n", cmd
->t_task
.t_task_cdb
[0],
5602 cmd
->se_tfo
->get_task_tag(cmd
));
5604 cmd
->se_tfo
->queue_status(cmd
);
5607 /* transport_generic_do_tmr():
5611 int transport_generic_do_tmr(struct se_cmd
*cmd
)
5613 struct se_cmd
*ref_cmd
;
5614 struct se_device
*dev
= cmd
->se_dev
;
5615 struct se_tmr_req
*tmr
= cmd
->se_tmr_req
;
5618 switch (tmr
->function
) {
5619 case TMR_ABORT_TASK
:
5620 ref_cmd
= tmr
->ref_cmd
;
5621 tmr
->response
= TMR_FUNCTION_REJECTED
;
5623 case TMR_ABORT_TASK_SET
:
5625 case TMR_CLEAR_TASK_SET
:
5626 tmr
->response
= TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
5629 ret
= core_tmr_lun_reset(dev
, tmr
, NULL
, NULL
);
5630 tmr
->response
= (!ret
) ? TMR_FUNCTION_COMPLETE
:
5631 TMR_FUNCTION_REJECTED
;
5633 case TMR_TARGET_WARM_RESET
:
5634 tmr
->response
= TMR_FUNCTION_REJECTED
;
5636 case TMR_TARGET_COLD_RESET
:
5637 tmr
->response
= TMR_FUNCTION_REJECTED
;
5640 printk(KERN_ERR
"Uknown TMR function: 0x%02x.\n",
5642 tmr
->response
= TMR_FUNCTION_REJECTED
;
5646 cmd
->t_state
= TRANSPORT_ISTATE_PROCESSING
;
5647 cmd
->se_tfo
->queue_tm_rsp(cmd
);
5649 transport_cmd_check_stop(cmd
, 2, 0);
5654 * Called with spin_lock_irq(&dev->execute_task_lock); held
5657 static struct se_task
*
5658 transport_get_task_from_state_list(struct se_device
*dev
)
5660 struct se_task
*task
;
5662 if (list_empty(&dev
->state_task_list
))
5665 list_for_each_entry(task
, &dev
->state_task_list
, t_state_list
)
5668 list_del(&task
->t_state_list
);
5669 atomic_set(&task
->task_state_active
, 0);
5674 static void transport_processing_shutdown(struct se_device
*dev
)
5677 struct se_task
*task
;
5678 unsigned long flags
;
5680 * Empty the struct se_device's struct se_task state list.
5682 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5683 while ((task
= transport_get_task_from_state_list(dev
))) {
5684 if (!task
->task_se_cmd
) {
5685 printk(KERN_ERR
"task->task_se_cmd is NULL!\n");
5688 cmd
= task
->task_se_cmd
;
5690 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
5692 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5694 DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
5695 " i_state/def_i_state: %d/%d, t_state/def_t_state:"
5696 " %d/%d cdb: 0x%02x\n", cmd
, task
,
5697 cmd
->se_tfo
->get_task_tag(cmd
), cmd
->cmd_sn
,
5698 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->deferred_i_state
,
5699 cmd
->t_state
, cmd
->deferred_t_state
,
5700 cmd
->t_task
.t_task_cdb
[0]);
5701 DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
5702 " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5703 " t_transport_stop: %d t_transport_sent: %d\n",
5704 cmd
->se_tfo
->get_task_tag(cmd
),
5705 cmd
->t_task
.t_task_cdbs
,
5706 atomic_read(&cmd
->t_task
.t_task_cdbs_left
),
5707 atomic_read(&cmd
->t_task
.t_task_cdbs_sent
),
5708 atomic_read(&cmd
->t_task
.t_transport_active
),
5709 atomic_read(&cmd
->t_task
.t_transport_stop
),
5710 atomic_read(&cmd
->t_task
.t_transport_sent
));
5712 if (atomic_read(&task
->task_active
)) {
5713 atomic_set(&task
->task_stop
, 1);
5714 spin_unlock_irqrestore(
5715 &cmd
->t_task
.t_state_lock
, flags
);
5717 DEBUG_DO("Waiting for task: %p to shutdown for dev:"
5718 " %p\n", task
, dev
);
5719 wait_for_completion(&task
->task_stop_comp
);
5720 DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
5723 spin_lock_irqsave(&cmd
->t_task
.t_state_lock
, flags
);
5724 atomic_dec(&cmd
->t_task
.t_task_cdbs_left
);
5726 atomic_set(&task
->task_active
, 0);
5727 atomic_set(&task
->task_stop
, 0);
5729 if (atomic_read(&task
->task_execute_queue
) != 0)
5730 transport_remove_task_from_execute_queue(task
, dev
);
5732 __transport_stop_task_timer(task
, &flags
);
5734 if (!(atomic_dec_and_test(&cmd
->t_task
.t_task_cdbs_ex_left
))) {
5735 spin_unlock_irqrestore(
5736 &cmd
->t_task
.t_state_lock
, flags
);
5738 DEBUG_DO("Skipping task: %p, dev: %p for"
5739 " t_task_cdbs_ex_left: %d\n", task
, dev
,
5740 atomic_read(&cmd
->t_task
.t_task_cdbs_ex_left
));
5742 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5746 if (atomic_read(&cmd
->t_task
.t_transport_active
)) {
5747 DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
5748 " %p\n", task
, dev
);
5750 if (atomic_read(&cmd
->t_task
.t_fe_count
)) {
5751 spin_unlock_irqrestore(
5752 &cmd
->t_task
.t_state_lock
, flags
);
5753 transport_send_check_condition_and_sense(
5754 cmd
, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
,
5756 transport_remove_cmd_from_queue(cmd
,
5757 &cmd
->se_dev
->dev_queue_obj
);
5759 transport_lun_remove_cmd(cmd
);
5760 transport_cmd_check_stop(cmd
, 1, 0);
5762 spin_unlock_irqrestore(
5763 &cmd
->t_task
.t_state_lock
, flags
);
5765 transport_remove_cmd_from_queue(cmd
,
5766 &cmd
->se_dev
->dev_queue_obj
);
5768 transport_lun_remove_cmd(cmd
);
5770 if (transport_cmd_check_stop(cmd
, 1, 0))
5771 transport_generic_remove(cmd
, 0, 0);
5774 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5777 DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
5780 if (atomic_read(&cmd
->t_task
.t_fe_count
)) {
5781 spin_unlock_irqrestore(
5782 &cmd
->t_task
.t_state_lock
, flags
);
5783 transport_send_check_condition_and_sense(cmd
,
5784 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
5785 transport_remove_cmd_from_queue(cmd
,
5786 &cmd
->se_dev
->dev_queue_obj
);
5788 transport_lun_remove_cmd(cmd
);
5789 transport_cmd_check_stop(cmd
, 1, 0);
5791 spin_unlock_irqrestore(
5792 &cmd
->t_task
.t_state_lock
, flags
);
5794 transport_remove_cmd_from_queue(cmd
,
5795 &cmd
->se_dev
->dev_queue_obj
);
5796 transport_lun_remove_cmd(cmd
);
5798 if (transport_cmd_check_stop(cmd
, 1, 0))
5799 transport_generic_remove(cmd
, 0, 0);
5802 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
5804 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
5806 * Empty the struct se_device's struct se_cmd list.
5808 while ((cmd
= transport_get_cmd_from_queue(&dev
->dev_queue_obj
))) {
5810 DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
5813 if (atomic_read(&cmd
->t_task
.t_fe_count
)) {
5814 transport_send_check_condition_and_sense(cmd
,
5815 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
, 0);
5817 transport_lun_remove_cmd(cmd
);
5818 transport_cmd_check_stop(cmd
, 1, 0);
5820 transport_lun_remove_cmd(cmd
);
5821 if (transport_cmd_check_stop(cmd
, 1, 0))
5822 transport_generic_remove(cmd
, 0, 0);
5827 /* transport_processing_thread():
5831 static int transport_processing_thread(void *param
)
5835 struct se_device
*dev
= (struct se_device
*) param
;
5837 set_user_nice(current
, -20);
5839 while (!kthread_should_stop()) {
5840 ret
= wait_event_interruptible(dev
->dev_queue_obj
.thread_wq
,
5841 atomic_read(&dev
->dev_queue_obj
.queue_cnt
) ||
5842 kthread_should_stop());
5846 spin_lock_irq(&dev
->dev_status_lock
);
5847 if (dev
->dev_status
& TRANSPORT_DEVICE_SHUTDOWN
) {
5848 spin_unlock_irq(&dev
->dev_status_lock
);
5849 transport_processing_shutdown(dev
);
5852 spin_unlock_irq(&dev
->dev_status_lock
);
5855 __transport_execute_tasks(dev
);
5857 cmd
= transport_get_cmd_from_queue(&dev
->dev_queue_obj
);
5861 switch (cmd
->t_state
) {
5862 case TRANSPORT_NEW_CMD_MAP
:
5863 if (!(cmd
->se_tfo
->new_cmd_map
)) {
5864 printk(KERN_ERR
"cmd->se_tfo->new_cmd_map is"
5865 " NULL for TRANSPORT_NEW_CMD_MAP\n");
5868 ret
= cmd
->se_tfo
->new_cmd_map(cmd
);
5870 cmd
->transport_error_status
= ret
;
5871 transport_generic_request_failure(cmd
, NULL
,
5872 0, (cmd
->data_direction
!=
5877 case TRANSPORT_NEW_CMD
:
5878 ret
= transport_generic_new_cmd(cmd
);
5880 cmd
->transport_error_status
= ret
;
5881 transport_generic_request_failure(cmd
, NULL
,
5882 0, (cmd
->data_direction
!=
5886 case TRANSPORT_PROCESS_WRITE
:
5887 transport_generic_process_write(cmd
);
5889 case TRANSPORT_COMPLETE_OK
:
5890 transport_stop_all_task_timers(cmd
);
5891 transport_generic_complete_ok(cmd
);
5893 case TRANSPORT_REMOVE
:
5894 transport_generic_remove(cmd
, 1, 0);
5896 case TRANSPORT_FREE_CMD_INTR
:
5897 transport_generic_free_cmd(cmd
, 0, 1, 0);
5899 case TRANSPORT_PROCESS_TMR
:
5900 transport_generic_do_tmr(cmd
);
5902 case TRANSPORT_COMPLETE_FAILURE
:
5903 transport_generic_request_failure(cmd
, NULL
, 1, 1);
5905 case TRANSPORT_COMPLETE_TIMEOUT
:
5906 transport_stop_all_task_timers(cmd
);
5907 transport_generic_request_timeout(cmd
);
5910 printk(KERN_ERR
"Unknown t_state: %d deferred_t_state:"
5911 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
5912 " %u\n", cmd
->t_state
, cmd
->deferred_t_state
,
5913 cmd
->se_tfo
->get_task_tag(cmd
),
5914 cmd
->se_tfo
->get_cmd_state(cmd
),
5915 cmd
->se_lun
->unpacked_lun
);
5923 transport_release_all_cmds(dev
);
5924 dev
->process_thread
= NULL
;