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 int transport_complete_qf(struct se_cmd *cmd);
208 static void transport_handle_queue_full(struct se_cmd *cmd,
209 struct se_device *dev, int (*qf_callback)(struct se_cmd *));
210 static void transport_direct_request_timeout(struct se_cmd *cmd);
211 static void transport_free_dev_tasks(struct se_cmd *cmd);
212 static u32 transport_allocate_tasks(struct se_cmd *cmd,
213 unsigned long long starting_lba, u32 sectors,
214 enum dma_data_direction data_direction,
215 struct list_head *mem_list, int set_counts);
216 static int transport_generic_get_mem(struct se_cmd *cmd, u32 length);
217 static int transport_generic_remove(struct se_cmd *cmd,
218 int session_reinstatement);
219 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd);
220 static int transport_map_sg_to_mem(struct se_cmd *cmd,
221 struct list_head *se_mem_list, struct scatterlist *sgl);
222 static void transport_memcpy_se_mem_read_contig(unsigned char *dst,
223 struct list_head *se_mem_list, u32 len);
224 static void transport_release_fe_cmd(struct se_cmd *cmd);
225 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
226 struct se_queue_obj *qobj);
227 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
228 static void transport_stop_all_task_timers(struct se_cmd *cmd);
230 int init_se_kmem_caches(void)
232 se_cmd_cache = kmem_cache_create("se_cmd_cache",
233 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
234 if (!(se_cmd_cache)) {
235 printk(KERN_ERR "kmem_cache_create for struct se_cmd failed\n");
238 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
239 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
241 if (!(se_tmr_req_cache)) {
242 printk(KERN_ERR "kmem_cache_create() for struct se_tmr_req"
246 se_sess_cache = kmem_cache_create("se_sess_cache",
247 sizeof(struct se_session), __alignof__(struct se_session),
249 if (!(se_sess_cache)) {
250 printk(KERN_ERR "kmem_cache_create() for struct se_session"
254 se_ua_cache = kmem_cache_create("se_ua_cache",
255 sizeof(struct se_ua), __alignof__(struct se_ua),
257 if (!(se_ua_cache)) {
258 printk(KERN_ERR "kmem_cache_create() for struct se_ua failed\n");
261 se_mem_cache = kmem_cache_create("se_mem_cache",
262 sizeof(struct se_mem), __alignof__(struct se_mem), 0, NULL);
263 if (!(se_mem_cache)) {
264 printk(KERN_ERR "kmem_cache_create() for struct se_mem failed\n");
267 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
268 sizeof(struct t10_pr_registration),
269 __alignof__(struct t10_pr_registration), 0, NULL);
270 if (!(t10_pr_reg_cache)) {
271 printk(KERN_ERR "kmem_cache_create() for struct t10_pr_registration"
275 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
276 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
278 if (!(t10_alua_lu_gp_cache)) {
279 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_cache"
283 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
284 sizeof(struct t10_alua_lu_gp_member),
285 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
286 if (!(t10_alua_lu_gp_mem_cache)) {
287 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_mem_"
291 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
292 sizeof(struct t10_alua_tg_pt_gp),
293 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
294 if (!(t10_alua_tg_pt_gp_cache)) {
295 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
299 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
300 "t10_alua_tg_pt_gp_mem_cache",
301 sizeof(struct t10_alua_tg_pt_gp_member),
302 __alignof__(struct t10_alua_tg_pt_gp_member),
304 if (!(t10_alua_tg_pt_gp_mem_cache)) {
305 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
313 kmem_cache_destroy(se_cmd_cache);
314 if (se_tmr_req_cache)
315 kmem_cache_destroy(se_tmr_req_cache);
317 kmem_cache_destroy(se_sess_cache);
319 kmem_cache_destroy(se_ua_cache);
321 kmem_cache_destroy(se_mem_cache);
322 if (t10_pr_reg_cache)
323 kmem_cache_destroy(t10_pr_reg_cache);
324 if (t10_alua_lu_gp_cache)
325 kmem_cache_destroy(t10_alua_lu_gp_cache);
326 if (t10_alua_lu_gp_mem_cache)
327 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
328 if (t10_alua_tg_pt_gp_cache)
329 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
330 if (t10_alua_tg_pt_gp_mem_cache)
331 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
335 void release_se_kmem_caches(void)
337 kmem_cache_destroy(se_cmd_cache);
338 kmem_cache_destroy(se_tmr_req_cache);
339 kmem_cache_destroy(se_sess_cache);
340 kmem_cache_destroy(se_ua_cache);
341 kmem_cache_destroy(se_mem_cache);
342 kmem_cache_destroy(t10_pr_reg_cache);
343 kmem_cache_destroy(t10_alua_lu_gp_cache);
344 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
345 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
346 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
349 /* This code ensures unique mib indexes are handed out. */
350 static DEFINE_SPINLOCK(scsi_mib_index_lock);
351 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
354 * Allocate a new row index for the entry type specified
356 u32 scsi_get_new_index(scsi_index_t type)
360 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
362 spin_lock(&scsi_mib_index_lock);
363 new_index = ++scsi_mib_index[type];
364 spin_unlock(&scsi_mib_index_lock);
369 void transport_init_queue_obj(struct se_queue_obj *qobj)
371 atomic_set(&qobj->queue_cnt, 0);
372 INIT_LIST_HEAD(&qobj->qobj_list);
373 init_waitqueue_head(&qobj->thread_wq);
374 spin_lock_init(&qobj->cmd_queue_lock);
376 EXPORT_SYMBOL(transport_init_queue_obj);
378 static int transport_subsystem_reqmods(void)
382 ret = request_module("target_core_iblock");
384 printk(KERN_ERR "Unable to load target_core_iblock\n");
386 ret = request_module("target_core_file");
388 printk(KERN_ERR "Unable to load target_core_file\n");
390 ret = request_module("target_core_pscsi");
392 printk(KERN_ERR "Unable to load target_core_pscsi\n");
394 ret = request_module("target_core_stgt");
396 printk(KERN_ERR "Unable to load target_core_stgt\n");
401 int transport_subsystem_check_init(void)
405 if (sub_api_initialized)
408 * Request the loading of known TCM subsystem plugins..
410 ret = transport_subsystem_reqmods();
414 sub_api_initialized = 1;
418 struct se_session *transport_init_session(void)
420 struct se_session *se_sess;
422 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
424 printk(KERN_ERR "Unable to allocate struct se_session from"
426 return ERR_PTR(-ENOMEM);
428 INIT_LIST_HEAD(&se_sess->sess_list);
429 INIT_LIST_HEAD(&se_sess->sess_acl_list);
433 EXPORT_SYMBOL(transport_init_session);
436 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
438 void __transport_register_session(
439 struct se_portal_group *se_tpg,
440 struct se_node_acl *se_nacl,
441 struct se_session *se_sess,
442 void *fabric_sess_ptr)
444 unsigned char buf[PR_REG_ISID_LEN];
446 se_sess->se_tpg = se_tpg;
447 se_sess->fabric_sess_ptr = fabric_sess_ptr;
449 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
451 * Only set for struct se_session's that will actually be moving I/O.
452 * eg: *NOT* discovery sessions.
456 * If the fabric module supports an ISID based TransportID,
457 * save this value in binary from the fabric I_T Nexus now.
459 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
460 memset(&buf[0], 0, PR_REG_ISID_LEN);
461 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
462 &buf[0], PR_REG_ISID_LEN);
463 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
465 spin_lock_irq(&se_nacl->nacl_sess_lock);
467 * The se_nacl->nacl_sess pointer will be set to the
468 * last active I_T Nexus for each struct se_node_acl.
470 se_nacl->nacl_sess = se_sess;
472 list_add_tail(&se_sess->sess_acl_list,
473 &se_nacl->acl_sess_list);
474 spin_unlock_irq(&se_nacl->nacl_sess_lock);
476 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
478 printk(KERN_INFO "TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
479 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
481 EXPORT_SYMBOL(__transport_register_session);
483 void transport_register_session(
484 struct se_portal_group *se_tpg,
485 struct se_node_acl *se_nacl,
486 struct se_session *se_sess,
487 void *fabric_sess_ptr)
489 spin_lock_bh(&se_tpg->session_lock);
490 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
491 spin_unlock_bh(&se_tpg->session_lock);
493 EXPORT_SYMBOL(transport_register_session);
495 void transport_deregister_session_configfs(struct se_session *se_sess)
497 struct se_node_acl *se_nacl;
500 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
502 se_nacl = se_sess->se_node_acl;
504 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
505 list_del(&se_sess->sess_acl_list);
507 * If the session list is empty, then clear the pointer.
508 * Otherwise, set the struct se_session pointer from the tail
509 * element of the per struct se_node_acl active session list.
511 if (list_empty(&se_nacl->acl_sess_list))
512 se_nacl->nacl_sess = NULL;
514 se_nacl->nacl_sess = container_of(
515 se_nacl->acl_sess_list.prev,
516 struct se_session, sess_acl_list);
518 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
521 EXPORT_SYMBOL(transport_deregister_session_configfs);
523 void transport_free_session(struct se_session *se_sess)
525 kmem_cache_free(se_sess_cache, se_sess);
527 EXPORT_SYMBOL(transport_free_session);
529 void transport_deregister_session(struct se_session *se_sess)
531 struct se_portal_group *se_tpg = se_sess->se_tpg;
532 struct se_node_acl *se_nacl;
535 transport_free_session(se_sess);
539 spin_lock_bh(&se_tpg->session_lock);
540 list_del(&se_sess->sess_list);
541 se_sess->se_tpg = NULL;
542 se_sess->fabric_sess_ptr = NULL;
543 spin_unlock_bh(&se_tpg->session_lock);
546 * Determine if we need to do extra work for this initiator node's
547 * struct se_node_acl if it had been previously dynamically generated.
549 se_nacl = se_sess->se_node_acl;
551 spin_lock_bh(&se_tpg->acl_node_lock);
552 if (se_nacl->dynamic_node_acl) {
553 if (!(se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
555 list_del(&se_nacl->acl_list);
556 se_tpg->num_node_acls--;
557 spin_unlock_bh(&se_tpg->acl_node_lock);
559 core_tpg_wait_for_nacl_pr_ref(se_nacl);
560 core_free_device_list_for_node(se_nacl, se_tpg);
561 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
563 spin_lock_bh(&se_tpg->acl_node_lock);
566 spin_unlock_bh(&se_tpg->acl_node_lock);
569 transport_free_session(se_sess);
571 printk(KERN_INFO "TARGET_CORE[%s]: Deregistered fabric_sess\n",
572 se_tpg->se_tpg_tfo->get_fabric_name());
574 EXPORT_SYMBOL(transport_deregister_session);
577 * Called with cmd->t_state_lock held.
579 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
581 struct se_device *dev;
582 struct se_task *task;
585 list_for_each_entry(task, &cmd->t_task_list, t_list) {
590 if (atomic_read(&task->task_active))
593 if (!(atomic_read(&task->task_state_active)))
596 spin_lock_irqsave(&dev->execute_task_lock, flags);
597 list_del(&task->t_state_list);
598 DEBUG_TSTATE("Removed ITT: 0x%08x dev: %p task[%p]\n",
599 cmd->se_tfo->tfo_get_task_tag(cmd), dev, task);
600 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
602 atomic_set(&task->task_state_active, 0);
603 atomic_dec(&cmd->t_task_cdbs_ex_left);
607 /* transport_cmd_check_stop():
609 * 'transport_off = 1' determines if t_transport_active should be cleared.
610 * 'transport_off = 2' determines if task_dev_state should be removed.
612 * A non-zero u8 t_state sets cmd->t_state.
613 * Returns 1 when command is stopped, else 0.
615 static int transport_cmd_check_stop(
622 spin_lock_irqsave(&cmd->t_state_lock, flags);
624 * Determine if IOCTL context caller in requesting the stopping of this
625 * command for LUN shutdown purposes.
627 if (atomic_read(&cmd->transport_lun_stop)) {
628 DEBUG_CS("%s:%d atomic_read(&cmd->transport_lun_stop)"
629 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
630 cmd->se_tfo->get_task_tag(cmd));
632 cmd->deferred_t_state = cmd->t_state;
633 cmd->t_state = TRANSPORT_DEFERRED_CMD;
634 atomic_set(&cmd->t_transport_active, 0);
635 if (transport_off == 2)
636 transport_all_task_dev_remove_state(cmd);
637 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
639 complete(&cmd->transport_lun_stop_comp);
643 * Determine if frontend context caller is requesting the stopping of
644 * this command for frontend exceptions.
646 if (atomic_read(&cmd->t_transport_stop)) {
647 DEBUG_CS("%s:%d atomic_read(&cmd->t_transport_stop) =="
648 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
649 cmd->se_tfo->get_task_tag(cmd));
651 cmd->deferred_t_state = cmd->t_state;
652 cmd->t_state = TRANSPORT_DEFERRED_CMD;
653 if (transport_off == 2)
654 transport_all_task_dev_remove_state(cmd);
657 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
660 if (transport_off == 2)
662 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
664 complete(&cmd->t_transport_stop_comp);
668 atomic_set(&cmd->t_transport_active, 0);
669 if (transport_off == 2) {
670 transport_all_task_dev_remove_state(cmd);
672 * Clear struct se_cmd->se_lun before the transport_off == 2
673 * handoff to fabric module.
677 * Some fabric modules like tcm_loop can release
678 * their internally allocated I/O reference now and
681 if (cmd->se_tfo->check_stop_free != NULL) {
682 spin_unlock_irqrestore(
683 &cmd->t_state_lock, flags);
685 cmd->se_tfo->check_stop_free(cmd);
689 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
693 cmd->t_state = t_state;
694 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
699 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
701 return transport_cmd_check_stop(cmd, 2, 0);
704 static void transport_lun_remove_cmd(struct se_cmd *cmd)
706 struct se_lun *lun = cmd->se_lun;
712 spin_lock_irqsave(&cmd->t_state_lock, flags);
713 if (!(atomic_read(&cmd->transport_dev_active))) {
714 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
717 atomic_set(&cmd->transport_dev_active, 0);
718 transport_all_task_dev_remove_state(cmd);
719 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
723 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
724 if (atomic_read(&cmd->transport_lun_active)) {
725 list_del(&cmd->se_lun_node);
726 atomic_set(&cmd->transport_lun_active, 0);
728 printk(KERN_INFO "Removed ITT: 0x%08x from LUN LIST[%d]\n"
729 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
732 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
735 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
737 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
738 transport_lun_remove_cmd(cmd);
740 if (transport_cmd_check_stop_to_fabric(cmd))
743 transport_generic_remove(cmd, 0);
746 void transport_cmd_finish_abort_tmr(struct se_cmd *cmd)
748 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
750 if (transport_cmd_check_stop_to_fabric(cmd))
753 transport_generic_remove(cmd, 0);
756 static void transport_add_cmd_to_queue(
760 struct se_device *dev = cmd->se_dev;
761 struct se_queue_obj *qobj = &dev->dev_queue_obj;
764 INIT_LIST_HEAD(&cmd->se_queue_node);
767 spin_lock_irqsave(&cmd->t_state_lock, flags);
768 cmd->t_state = t_state;
769 atomic_set(&cmd->t_transport_active, 1);
770 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
773 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
774 if (cmd->se_cmd_flags & SCF_EMULATE_QUEUE_FULL) {
775 cmd->se_cmd_flags &= ~SCF_EMULATE_QUEUE_FULL;
776 list_add(&cmd->se_queue_node, &qobj->qobj_list);
778 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
779 atomic_inc(&cmd->t_transport_queue_active);
780 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
782 atomic_inc(&qobj->queue_cnt);
783 wake_up_interruptible(&qobj->thread_wq);
786 static struct se_cmd *
787 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
792 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
793 if (list_empty(&qobj->qobj_list)) {
794 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
797 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
799 atomic_dec(&cmd->t_transport_queue_active);
801 list_del(&cmd->se_queue_node);
802 atomic_dec(&qobj->queue_cnt);
803 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
808 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
809 struct se_queue_obj *qobj)
814 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
815 if (!(atomic_read(&cmd->t_transport_queue_active))) {
816 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
820 list_for_each_entry(t, &qobj->qobj_list, se_queue_node)
822 atomic_dec(&cmd->t_transport_queue_active);
823 atomic_dec(&qobj->queue_cnt);
824 list_del(&cmd->se_queue_node);
827 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
829 if (atomic_read(&cmd->t_transport_queue_active)) {
830 printk(KERN_ERR "ITT: 0x%08x t_transport_queue_active: %d\n",
831 cmd->se_tfo->get_task_tag(cmd),
832 atomic_read(&cmd->t_transport_queue_active));
837 * Completion function used by TCM subsystem plugins (such as FILEIO)
838 * for queueing up response from struct se_subsystem_api->do_task()
840 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
842 struct se_task *task = list_entry(cmd->t_task_list.next,
843 struct se_task, t_list);
846 cmd->scsi_status = SAM_STAT_GOOD;
847 task->task_scsi_status = GOOD;
849 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
850 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
851 task->task_se_cmd->transport_error_status =
852 PYX_TRANSPORT_ILLEGAL_REQUEST;
855 transport_complete_task(task, good);
857 EXPORT_SYMBOL(transport_complete_sync_cache);
859 /* transport_complete_task():
861 * Called from interrupt and non interrupt context depending
862 * on the transport plugin.
864 void transport_complete_task(struct se_task *task, int success)
866 struct se_cmd *cmd = task->task_se_cmd;
867 struct se_device *dev = task->se_dev;
871 printk(KERN_INFO "task: %p CDB: 0x%02x obj_ptr: %p\n", task,
872 cmd->t_task_cdb[0], dev);
875 atomic_inc(&dev->depth_left);
877 spin_lock_irqsave(&cmd->t_state_lock, flags);
878 atomic_set(&task->task_active, 0);
881 * See if any sense data exists, if so set the TASK_SENSE flag.
882 * Also check for any other post completion work that needs to be
883 * done by the plugins.
885 if (dev && dev->transport->transport_complete) {
886 if (dev->transport->transport_complete(task) != 0) {
887 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
888 task->task_sense = 1;
894 * See if we are waiting for outstanding struct se_task
895 * to complete for an exception condition
897 if (atomic_read(&task->task_stop)) {
899 * Decrement cmd->t_se_count if this task had
900 * previously thrown its timeout exception handler.
902 if (atomic_read(&task->task_timeout)) {
903 atomic_dec(&cmd->t_se_count);
904 atomic_set(&task->task_timeout, 0);
906 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
908 complete(&task->task_stop_comp);
912 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
913 * left counter to determine when the struct se_cmd is ready to be queued to
914 * the processing thread.
916 if (atomic_read(&task->task_timeout)) {
917 if (!(atomic_dec_and_test(
918 &cmd->t_task_cdbs_timeout_left))) {
919 spin_unlock_irqrestore(&cmd->t_state_lock,
923 t_state = TRANSPORT_COMPLETE_TIMEOUT;
924 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
926 transport_add_cmd_to_queue(cmd, t_state);
929 atomic_dec(&cmd->t_task_cdbs_timeout_left);
932 * Decrement the outstanding t_task_cdbs_left count. The last
933 * struct se_task from struct se_cmd will complete itself into the
934 * device queue depending upon int success.
936 if (!(atomic_dec_and_test(&cmd->t_task_cdbs_left))) {
938 cmd->t_tasks_failed = 1;
940 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
944 if (!success || cmd->t_tasks_failed) {
945 t_state = TRANSPORT_COMPLETE_FAILURE;
946 if (!task->task_error_status) {
947 task->task_error_status =
948 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
949 cmd->transport_error_status =
950 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
953 atomic_set(&cmd->t_transport_complete, 1);
954 t_state = TRANSPORT_COMPLETE_OK;
956 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
958 transport_add_cmd_to_queue(cmd, t_state);
960 EXPORT_SYMBOL(transport_complete_task);
963 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
964 * struct se_task list are ready to be added to the active execution list
967 * Called with se_dev_t->execute_task_lock called.
969 static inline int transport_add_task_check_sam_attr(
970 struct se_task *task,
971 struct se_task *task_prev,
972 struct se_device *dev)
975 * No SAM Task attribute emulation enabled, add to tail of
978 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
979 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
983 * HEAD_OF_QUEUE attribute for received CDB, which means
984 * the first task that is associated with a struct se_cmd goes to
985 * head of the struct se_device->execute_task_list, and task_prev
986 * after that for each subsequent task
988 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
989 list_add(&task->t_execute_list,
990 (task_prev != NULL) ?
991 &task_prev->t_execute_list :
992 &dev->execute_task_list);
994 DEBUG_STA("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
995 " in execution queue\n",
996 T_TASK(task->task_se_cmd)->t_task_cdb[0]);
1000 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
1001 * transitioned from Dermant -> Active state, and are added to the end
1002 * of the struct se_device->execute_task_list
1004 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
1008 /* __transport_add_task_to_execute_queue():
1010 * Called with se_dev_t->execute_task_lock called.
1012 static void __transport_add_task_to_execute_queue(
1013 struct se_task *task,
1014 struct se_task *task_prev,
1015 struct se_device *dev)
1019 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
1020 atomic_inc(&dev->execute_tasks);
1022 if (atomic_read(&task->task_state_active))
1025 * Determine if this task needs to go to HEAD_OF_QUEUE for the
1026 * state list as well. Running with SAM Task Attribute emulation
1027 * will always return head_of_queue == 0 here
1030 list_add(&task->t_state_list, (task_prev) ?
1031 &task_prev->t_state_list :
1032 &dev->state_task_list);
1034 list_add_tail(&task->t_state_list, &dev->state_task_list);
1036 atomic_set(&task->task_state_active, 1);
1038 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1039 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
1043 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
1045 struct se_device *dev;
1046 struct se_task *task;
1047 unsigned long flags;
1049 spin_lock_irqsave(&cmd->t_state_lock, flags);
1050 list_for_each_entry(task, &cmd->t_task_list, t_list) {
1053 if (atomic_read(&task->task_state_active))
1056 spin_lock(&dev->execute_task_lock);
1057 list_add_tail(&task->t_state_list, &dev->state_task_list);
1058 atomic_set(&task->task_state_active, 1);
1060 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1061 task->se_cmd->se_tfo->get_task_tag(
1062 task->task_se_cmd), task, dev);
1064 spin_unlock(&dev->execute_task_lock);
1066 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1069 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
1071 struct se_device *dev = cmd->se_dev;
1072 struct se_task *task, *task_prev = NULL;
1073 unsigned long flags;
1075 spin_lock_irqsave(&dev->execute_task_lock, flags);
1076 list_for_each_entry(task, &cmd->t_task_list, t_list) {
1077 if (atomic_read(&task->task_execute_queue))
1080 * __transport_add_task_to_execute_queue() handles the
1081 * SAM Task Attribute emulation if enabled
1083 __transport_add_task_to_execute_queue(task, task_prev, dev);
1084 atomic_set(&task->task_execute_queue, 1);
1087 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1090 /* transport_remove_task_from_execute_queue():
1094 void transport_remove_task_from_execute_queue(
1095 struct se_task *task,
1096 struct se_device *dev)
1098 unsigned long flags;
1100 if (atomic_read(&task->task_execute_queue) == 0) {
1105 spin_lock_irqsave(&dev->execute_task_lock, flags);
1106 list_del(&task->t_execute_list);
1107 atomic_set(&task->task_execute_queue, 0);
1108 atomic_dec(&dev->execute_tasks);
1109 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1113 * Handle QUEUE_FULL / -EAGAIN status
1116 static void target_qf_do_work(struct work_struct *work)
1118 struct se_device *dev = container_of(work, struct se_device,
1120 struct se_cmd *cmd, *cmd_tmp;
1122 spin_lock_irq(&dev->qf_cmd_lock);
1123 list_for_each_entry_safe(cmd, cmd_tmp, &dev->qf_cmd_list, se_qf_node) {
1125 list_del(&cmd->se_qf_node);
1126 atomic_dec(&dev->dev_qf_count);
1127 smp_mb__after_atomic_dec();
1128 spin_unlock_irq(&dev->qf_cmd_lock);
1130 printk(KERN_INFO "Processing %s cmd: %p QUEUE_FULL in work queue"
1131 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
1132 (cmd->t_state == TRANSPORT_COMPLETE_OK) ? "COMPLETE_OK" :
1133 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
1136 * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd
1137 * has been added to head of queue
1139 transport_add_cmd_to_queue(cmd, cmd->t_state);
1141 spin_lock_irq(&dev->qf_cmd_lock);
1143 spin_unlock_irq(&dev->qf_cmd_lock);
1146 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
1148 switch (cmd->data_direction) {
1151 case DMA_FROM_DEVICE:
1155 case DMA_BIDIRECTIONAL:
1164 void transport_dump_dev_state(
1165 struct se_device *dev,
1169 *bl += sprintf(b + *bl, "Status: ");
1170 switch (dev->dev_status) {
1171 case TRANSPORT_DEVICE_ACTIVATED:
1172 *bl += sprintf(b + *bl, "ACTIVATED");
1174 case TRANSPORT_DEVICE_DEACTIVATED:
1175 *bl += sprintf(b + *bl, "DEACTIVATED");
1177 case TRANSPORT_DEVICE_SHUTDOWN:
1178 *bl += sprintf(b + *bl, "SHUTDOWN");
1180 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
1181 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
1182 *bl += sprintf(b + *bl, "OFFLINE");
1185 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
1189 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
1190 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
1192 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
1193 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
1194 *bl += sprintf(b + *bl, " ");
1197 /* transport_release_all_cmds():
1201 static void transport_release_all_cmds(struct se_device *dev)
1203 struct se_cmd *cmd, *tcmd;
1204 int bug_out = 0, t_state;
1205 unsigned long flags;
1207 spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags);
1208 list_for_each_entry_safe(cmd, tcmd, &dev->dev_queue_obj.qobj_list,
1210 t_state = cmd->t_state;
1211 list_del(&cmd->se_queue_node);
1212 spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock,
1215 printk(KERN_ERR "Releasing ITT: 0x%08x, i_state: %u,"
1216 " t_state: %u directly\n",
1217 cmd->se_tfo->get_task_tag(cmd),
1218 cmd->se_tfo->get_cmd_state(cmd), t_state);
1220 transport_release_fe_cmd(cmd);
1223 spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags);
1225 spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock, flags);
1232 void transport_dump_vpd_proto_id(
1233 struct t10_vpd *vpd,
1234 unsigned char *p_buf,
1237 unsigned char buf[VPD_TMP_BUF_SIZE];
1240 memset(buf, 0, VPD_TMP_BUF_SIZE);
1241 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1243 switch (vpd->protocol_identifier) {
1245 sprintf(buf+len, "Fibre Channel\n");
1248 sprintf(buf+len, "Parallel SCSI\n");
1251 sprintf(buf+len, "SSA\n");
1254 sprintf(buf+len, "IEEE 1394\n");
1257 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1261 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1264 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1267 sprintf(buf+len, "Automation/Drive Interface Transport"
1271 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1274 sprintf(buf+len, "Unknown 0x%02x\n",
1275 vpd->protocol_identifier);
1280 strncpy(p_buf, buf, p_buf_len);
1282 printk(KERN_INFO "%s", buf);
1286 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1289 * Check if the Protocol Identifier Valid (PIV) bit is set..
1291 * from spc3r23.pdf section 7.5.1
1293 if (page_83[1] & 0x80) {
1294 vpd->protocol_identifier = (page_83[0] & 0xf0);
1295 vpd->protocol_identifier_set = 1;
1296 transport_dump_vpd_proto_id(vpd, NULL, 0);
1299 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1301 int transport_dump_vpd_assoc(
1302 struct t10_vpd *vpd,
1303 unsigned char *p_buf,
1306 unsigned char buf[VPD_TMP_BUF_SIZE];
1310 memset(buf, 0, VPD_TMP_BUF_SIZE);
1311 len = sprintf(buf, "T10 VPD Identifier Association: ");
1313 switch (vpd->association) {
1315 sprintf(buf+len, "addressed logical unit\n");
1318 sprintf(buf+len, "target port\n");
1321 sprintf(buf+len, "SCSI target device\n");
1324 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1330 strncpy(p_buf, buf, p_buf_len);
1337 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1340 * The VPD identification association..
1342 * from spc3r23.pdf Section 7.6.3.1 Table 297
1344 vpd->association = (page_83[1] & 0x30);
1345 return transport_dump_vpd_assoc(vpd, NULL, 0);
1347 EXPORT_SYMBOL(transport_set_vpd_assoc);
1349 int transport_dump_vpd_ident_type(
1350 struct t10_vpd *vpd,
1351 unsigned char *p_buf,
1354 unsigned char buf[VPD_TMP_BUF_SIZE];
1358 memset(buf, 0, VPD_TMP_BUF_SIZE);
1359 len = sprintf(buf, "T10 VPD Identifier Type: ");
1361 switch (vpd->device_identifier_type) {
1363 sprintf(buf+len, "Vendor specific\n");
1366 sprintf(buf+len, "T10 Vendor ID based\n");
1369 sprintf(buf+len, "EUI-64 based\n");
1372 sprintf(buf+len, "NAA\n");
1375 sprintf(buf+len, "Relative target port identifier\n");
1378 sprintf(buf+len, "SCSI name string\n");
1381 sprintf(buf+len, "Unsupported: 0x%02x\n",
1382 vpd->device_identifier_type);
1388 if (p_buf_len < strlen(buf)+1)
1390 strncpy(p_buf, buf, p_buf_len);
1398 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1401 * The VPD identifier type..
1403 * from spc3r23.pdf Section 7.6.3.1 Table 298
1405 vpd->device_identifier_type = (page_83[1] & 0x0f);
1406 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1408 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1410 int transport_dump_vpd_ident(
1411 struct t10_vpd *vpd,
1412 unsigned char *p_buf,
1415 unsigned char buf[VPD_TMP_BUF_SIZE];
1418 memset(buf, 0, VPD_TMP_BUF_SIZE);
1420 switch (vpd->device_identifier_code_set) {
1421 case 0x01: /* Binary */
1422 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1423 &vpd->device_identifier[0]);
1425 case 0x02: /* ASCII */
1426 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1427 &vpd->device_identifier[0]);
1429 case 0x03: /* UTF-8 */
1430 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1431 &vpd->device_identifier[0]);
1434 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1435 " 0x%02x", vpd->device_identifier_code_set);
1441 strncpy(p_buf, buf, p_buf_len);
1449 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1451 static const char hex_str[] = "0123456789abcdef";
1452 int j = 0, i = 4; /* offset to start of the identifer */
1455 * The VPD Code Set (encoding)
1457 * from spc3r23.pdf Section 7.6.3.1 Table 296
1459 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1460 switch (vpd->device_identifier_code_set) {
1461 case 0x01: /* Binary */
1462 vpd->device_identifier[j++] =
1463 hex_str[vpd->device_identifier_type];
1464 while (i < (4 + page_83[3])) {
1465 vpd->device_identifier[j++] =
1466 hex_str[(page_83[i] & 0xf0) >> 4];
1467 vpd->device_identifier[j++] =
1468 hex_str[page_83[i] & 0x0f];
1472 case 0x02: /* ASCII */
1473 case 0x03: /* UTF-8 */
1474 while (i < (4 + page_83[3]))
1475 vpd->device_identifier[j++] = page_83[i++];
1481 return transport_dump_vpd_ident(vpd, NULL, 0);
1483 EXPORT_SYMBOL(transport_set_vpd_ident);
1485 static void core_setup_task_attr_emulation(struct se_device *dev)
1488 * If this device is from Target_Core_Mod/pSCSI, disable the
1489 * SAM Task Attribute emulation.
1491 * This is currently not available in upsream Linux/SCSI Target
1492 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1494 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1495 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1499 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1500 DEBUG_STA("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1501 " device\n", dev->transport->name,
1502 dev->transport->get_device_rev(dev));
1505 static void scsi_dump_inquiry(struct se_device *dev)
1507 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1510 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1512 printk(" Vendor: ");
1513 for (i = 0; i < 8; i++)
1514 if (wwn->vendor[i] >= 0x20)
1515 printk("%c", wwn->vendor[i]);
1520 for (i = 0; i < 16; i++)
1521 if (wwn->model[i] >= 0x20)
1522 printk("%c", wwn->model[i]);
1526 printk(" Revision: ");
1527 for (i = 0; i < 4; i++)
1528 if (wwn->revision[i] >= 0x20)
1529 printk("%c", wwn->revision[i]);
1535 device_type = dev->transport->get_device_type(dev);
1536 printk(" Type: %s ", scsi_device_type(device_type));
1537 printk(" ANSI SCSI revision: %02x\n",
1538 dev->transport->get_device_rev(dev));
1541 struct se_device *transport_add_device_to_core_hba(
1543 struct se_subsystem_api *transport,
1544 struct se_subsystem_dev *se_dev,
1546 void *transport_dev,
1547 struct se_dev_limits *dev_limits,
1548 const char *inquiry_prod,
1549 const char *inquiry_rev)
1552 struct se_device *dev;
1554 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1556 printk(KERN_ERR "Unable to allocate memory for se_dev_t\n");
1560 transport_init_queue_obj(&dev->dev_queue_obj);
1561 dev->dev_flags = device_flags;
1562 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1563 dev->dev_ptr = transport_dev;
1565 dev->se_sub_dev = se_dev;
1566 dev->transport = transport;
1567 atomic_set(&dev->active_cmds, 0);
1568 INIT_LIST_HEAD(&dev->dev_list);
1569 INIT_LIST_HEAD(&dev->dev_sep_list);
1570 INIT_LIST_HEAD(&dev->dev_tmr_list);
1571 INIT_LIST_HEAD(&dev->execute_task_list);
1572 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1573 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1574 INIT_LIST_HEAD(&dev->state_task_list);
1575 INIT_LIST_HEAD(&dev->qf_cmd_list);
1576 spin_lock_init(&dev->execute_task_lock);
1577 spin_lock_init(&dev->delayed_cmd_lock);
1578 spin_lock_init(&dev->ordered_cmd_lock);
1579 spin_lock_init(&dev->state_task_lock);
1580 spin_lock_init(&dev->dev_alua_lock);
1581 spin_lock_init(&dev->dev_reservation_lock);
1582 spin_lock_init(&dev->dev_status_lock);
1583 spin_lock_init(&dev->dev_status_thr_lock);
1584 spin_lock_init(&dev->se_port_lock);
1585 spin_lock_init(&dev->se_tmr_lock);
1586 spin_lock_init(&dev->qf_cmd_lock);
1588 dev->queue_depth = dev_limits->queue_depth;
1589 atomic_set(&dev->depth_left, dev->queue_depth);
1590 atomic_set(&dev->dev_ordered_id, 0);
1592 se_dev_set_default_attribs(dev, dev_limits);
1594 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1595 dev->creation_time = get_jiffies_64();
1596 spin_lock_init(&dev->stats_lock);
1598 spin_lock(&hba->device_lock);
1599 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1601 spin_unlock(&hba->device_lock);
1603 * Setup the SAM Task Attribute emulation for struct se_device
1605 core_setup_task_attr_emulation(dev);
1607 * Force PR and ALUA passthrough emulation with internal object use.
1609 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1611 * Setup the Reservations infrastructure for struct se_device
1613 core_setup_reservations(dev, force_pt);
1615 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1617 if (core_setup_alua(dev, force_pt) < 0)
1621 * Startup the struct se_device processing thread
1623 dev->process_thread = kthread_run(transport_processing_thread, dev,
1624 "LIO_%s", dev->transport->name);
1625 if (IS_ERR(dev->process_thread)) {
1626 printk(KERN_ERR "Unable to create kthread: LIO_%s\n",
1627 dev->transport->name);
1631 * Setup work_queue for QUEUE_FULL
1633 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1635 * Preload the initial INQUIRY const values if we are doing
1636 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1637 * passthrough because this is being provided by the backend LLD.
1638 * This is required so that transport_get_inquiry() copies these
1639 * originals once back into DEV_T10_WWN(dev) for the virtual device
1642 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1643 if (!inquiry_prod || !inquiry_rev) {
1644 printk(KERN_ERR "All non TCM/pSCSI plugins require"
1645 " INQUIRY consts\n");
1649 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1650 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1651 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1653 scsi_dump_inquiry(dev);
1657 kthread_stop(dev->process_thread);
1659 spin_lock(&hba->device_lock);
1660 list_del(&dev->dev_list);
1662 spin_unlock(&hba->device_lock);
1664 se_release_vpd_for_dev(dev);
1670 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1672 /* transport_generic_prepare_cdb():
1674 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1675 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1676 * The point of this is since we are mapping iSCSI LUNs to
1677 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1678 * devices and HBAs for a loop.
1680 static inline void transport_generic_prepare_cdb(
1684 case READ_10: /* SBC - RDProtect */
1685 case READ_12: /* SBC - RDProtect */
1686 case READ_16: /* SBC - RDProtect */
1687 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1688 case VERIFY: /* SBC - VRProtect */
1689 case VERIFY_16: /* SBC - VRProtect */
1690 case WRITE_VERIFY: /* SBC - VRProtect */
1691 case WRITE_VERIFY_12: /* SBC - VRProtect */
1694 cdb[1] &= 0x1f; /* clear logical unit number */
1699 static struct se_task *
1700 transport_generic_get_task(struct se_cmd *cmd,
1701 enum dma_data_direction data_direction)
1703 struct se_task *task;
1704 struct se_device *dev = cmd->se_dev;
1705 unsigned long flags;
1707 task = dev->transport->alloc_task(cmd);
1709 printk(KERN_ERR "Unable to allocate struct se_task\n");
1713 INIT_LIST_HEAD(&task->t_list);
1714 INIT_LIST_HEAD(&task->t_execute_list);
1715 INIT_LIST_HEAD(&task->t_state_list);
1716 init_completion(&task->task_stop_comp);
1717 task->task_se_cmd = cmd;
1719 task->task_data_direction = data_direction;
1721 spin_lock_irqsave(&cmd->t_state_lock, flags);
1722 list_add_tail(&task->t_list, &cmd->t_task_list);
1723 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1728 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1731 * Used by fabric modules containing a local struct se_cmd within their
1732 * fabric dependent per I/O descriptor.
1734 void transport_init_se_cmd(
1736 struct target_core_fabric_ops *tfo,
1737 struct se_session *se_sess,
1741 unsigned char *sense_buffer)
1743 INIT_LIST_HEAD(&cmd->se_lun_node);
1744 INIT_LIST_HEAD(&cmd->se_delayed_node);
1745 INIT_LIST_HEAD(&cmd->se_ordered_node);
1746 INIT_LIST_HEAD(&cmd->se_qf_node);
1748 INIT_LIST_HEAD(&cmd->t_mem_list);
1749 INIT_LIST_HEAD(&cmd->t_mem_bidi_list);
1750 INIT_LIST_HEAD(&cmd->t_task_list);
1751 init_completion(&cmd->transport_lun_fe_stop_comp);
1752 init_completion(&cmd->transport_lun_stop_comp);
1753 init_completion(&cmd->t_transport_stop_comp);
1754 spin_lock_init(&cmd->t_state_lock);
1755 atomic_set(&cmd->transport_dev_active, 1);
1758 cmd->se_sess = se_sess;
1759 cmd->data_length = data_length;
1760 cmd->data_direction = data_direction;
1761 cmd->sam_task_attr = task_attr;
1762 cmd->sense_buffer = sense_buffer;
1764 EXPORT_SYMBOL(transport_init_se_cmd);
1766 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1769 * Check if SAM Task Attribute emulation is enabled for this
1770 * struct se_device storage object
1772 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1775 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1776 DEBUG_STA("SAM Task Attribute ACA"
1777 " emulation is not supported\n");
1781 * Used to determine when ORDERED commands should go from
1782 * Dormant to Active status.
1784 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1785 smp_mb__after_atomic_inc();
1786 DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1787 cmd->se_ordered_id, cmd->sam_task_attr,
1788 TRANSPORT(cmd->se_dev)->name);
1792 void transport_free_se_cmd(
1793 struct se_cmd *se_cmd)
1795 if (se_cmd->se_tmr_req)
1796 core_tmr_release_req(se_cmd->se_tmr_req);
1798 * Check and free any extended CDB buffer that was allocated
1800 if (se_cmd->t_task_cdb != se_cmd->__t_task_cdb)
1801 kfree(se_cmd->t_task_cdb);
1803 EXPORT_SYMBOL(transport_free_se_cmd);
1805 static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
1807 /* transport_generic_allocate_tasks():
1809 * Called from fabric RX Thread.
1811 int transport_generic_allocate_tasks(
1817 transport_generic_prepare_cdb(cdb);
1820 * This is needed for early exceptions.
1822 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1825 * Ensure that the received CDB is less than the max (252 + 8) bytes
1826 * for VARIABLE_LENGTH_CMD
1828 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1829 printk(KERN_ERR "Received SCSI CDB with command_size: %d that"
1830 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1831 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1835 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1836 * allocate the additional extended CDB buffer now.. Otherwise
1837 * setup the pointer from __t_task_cdb to t_task_cdb.
1839 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1840 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1842 if (!(cmd->t_task_cdb)) {
1843 printk(KERN_ERR "Unable to allocate cmd->t_task_cdb"
1844 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1845 scsi_command_size(cdb),
1846 (unsigned long)sizeof(cmd->__t_task_cdb));
1850 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1852 * Copy the original CDB into cmd->
1854 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1856 * Setup the received CDB based on SCSI defined opcodes and
1857 * perform unit attention, persistent reservations and ALUA
1858 * checks for virtual device backends. The cmd->t_task_cdb
1859 * pointer is expected to be setup before we reach this point.
1861 ret = transport_generic_cmd_sequencer(cmd, cdb);
1865 * Check for SAM Task Attribute Emulation
1867 if (transport_check_alloc_task_attr(cmd) < 0) {
1868 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1869 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1872 spin_lock(&cmd->se_lun->lun_sep_lock);
1873 if (cmd->se_lun->lun_sep)
1874 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1875 spin_unlock(&cmd->se_lun->lun_sep_lock);
1878 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1881 * Used by fabric module frontends not defining a TFO->new_cmd_map()
1882 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
1884 int transport_generic_handle_cdb(
1889 printk(KERN_ERR "cmd->se_lun is NULL\n");
1893 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
1896 EXPORT_SYMBOL(transport_generic_handle_cdb);
1899 * Used by fabric module frontends to queue tasks directly.
1900 * Many only be used from process context only
1902 int transport_handle_cdb_direct(
1907 printk(KERN_ERR "cmd->se_lun is NULL\n");
1910 if (in_interrupt()) {
1912 printk(KERN_ERR "transport_generic_handle_cdb cannot be called"
1913 " from interrupt context\n");
1917 return transport_generic_new_cmd(cmd);
1919 EXPORT_SYMBOL(transport_handle_cdb_direct);
1922 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1923 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1924 * complete setup in TCM process context w/ TFO->new_cmd_map().
1926 int transport_generic_handle_cdb_map(
1931 printk(KERN_ERR "cmd->se_lun is NULL\n");
1935 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
1938 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1940 /* transport_generic_handle_data():
1944 int transport_generic_handle_data(
1948 * For the software fabric case, then we assume the nexus is being
1949 * failed/shutdown when signals are pending from the kthread context
1950 * caller, so we return a failure. For the HW target mode case running
1951 * in interrupt code, the signal_pending() check is skipped.
1953 if (!in_interrupt() && signal_pending(current))
1956 * If the received CDB has aleady been ABORTED by the generic
1957 * target engine, we now call transport_check_aborted_status()
1958 * to queue any delated TASK_ABORTED status for the received CDB to the
1959 * fabric module as we are expecting no further incoming DATA OUT
1960 * sequences at this point.
1962 if (transport_check_aborted_status(cmd, 1) != 0)
1965 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
1968 EXPORT_SYMBOL(transport_generic_handle_data);
1970 /* transport_generic_handle_tmr():
1974 int transport_generic_handle_tmr(
1978 * This is needed for early exceptions.
1980 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1982 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
1985 EXPORT_SYMBOL(transport_generic_handle_tmr);
1987 void transport_generic_free_cmd_intr(
1990 transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR);
1992 EXPORT_SYMBOL(transport_generic_free_cmd_intr);
1994 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1996 struct se_task *task, *task_tmp;
1997 unsigned long flags;
2000 DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
2001 cmd->se_tfo->get_task_tag(cmd));
2004 * No tasks remain in the execution queue
2006 spin_lock_irqsave(&cmd->t_state_lock, flags);
2007 list_for_each_entry_safe(task, task_tmp,
2008 &cmd->t_task_list, t_list) {
2009 DEBUG_TS("task_no[%d] - Processing task %p\n",
2010 task->task_no, task);
2012 * If the struct se_task has not been sent and is not active,
2013 * remove the struct se_task from the execution queue.
2015 if (!atomic_read(&task->task_sent) &&
2016 !atomic_read(&task->task_active)) {
2017 spin_unlock_irqrestore(&cmd->t_state_lock,
2019 transport_remove_task_from_execute_queue(task,
2022 DEBUG_TS("task_no[%d] - Removed from execute queue\n",
2024 spin_lock_irqsave(&cmd->t_state_lock, flags);
2029 * If the struct se_task is active, sleep until it is returned
2032 if (atomic_read(&task->task_active)) {
2033 atomic_set(&task->task_stop, 1);
2034 spin_unlock_irqrestore(&cmd->t_state_lock,
2037 DEBUG_TS("task_no[%d] - Waiting to complete\n",
2039 wait_for_completion(&task->task_stop_comp);
2040 DEBUG_TS("task_no[%d] - Stopped successfully\n",
2043 spin_lock_irqsave(&cmd->t_state_lock, flags);
2044 atomic_dec(&cmd->t_task_cdbs_left);
2046 atomic_set(&task->task_active, 0);
2047 atomic_set(&task->task_stop, 0);
2049 DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
2053 __transport_stop_task_timer(task, &flags);
2055 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2061 * Handle SAM-esque emulation for generic transport request failures.
2063 static void transport_generic_request_failure(
2065 struct se_device *dev,
2071 DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
2072 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
2073 cmd->t_task_cdb[0]);
2074 DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
2075 " %d/%d transport_error_status: %d\n",
2076 cmd->se_tfo->get_cmd_state(cmd),
2077 cmd->t_state, cmd->deferred_t_state,
2078 cmd->transport_error_status);
2079 DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
2080 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
2081 " t_transport_active: %d t_transport_stop: %d"
2082 " t_transport_sent: %d\n", cmd->t_task_cdbs,
2083 atomic_read(&cmd->t_task_cdbs_left),
2084 atomic_read(&cmd->t_task_cdbs_sent),
2085 atomic_read(&cmd->t_task_cdbs_ex_left),
2086 atomic_read(&cmd->t_transport_active),
2087 atomic_read(&cmd->t_transport_stop),
2088 atomic_read(&cmd->t_transport_sent));
2090 transport_stop_all_task_timers(cmd);
2093 atomic_inc(&dev->depth_left);
2095 * For SAM Task Attribute emulation for failed struct se_cmd
2097 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2098 transport_complete_task_attr(cmd);
2101 transport_direct_request_timeout(cmd);
2102 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2105 switch (cmd->transport_error_status) {
2106 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
2107 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2109 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
2110 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
2112 case PYX_TRANSPORT_INVALID_CDB_FIELD:
2113 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2115 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
2116 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2118 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
2120 transport_new_cmd_failure(cmd);
2122 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
2123 * we force this session to fall back to session
2126 cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
2127 cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
2130 case PYX_TRANSPORT_LU_COMM_FAILURE:
2131 case PYX_TRANSPORT_ILLEGAL_REQUEST:
2132 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2134 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
2135 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
2137 case PYX_TRANSPORT_WRITE_PROTECTED:
2138 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
2140 case PYX_TRANSPORT_RESERVATION_CONFLICT:
2142 * No SENSE Data payload for this case, set SCSI Status
2143 * and queue the response to $FABRIC_MOD.
2145 * Uses linux/include/scsi/scsi.h SAM status codes defs
2147 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2149 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2150 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2153 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2156 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2157 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2158 cmd->orig_fe_lun, 0x2C,
2159 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2161 ret = cmd->se_tfo->queue_status(cmd);
2165 case PYX_TRANSPORT_USE_SENSE_REASON:
2167 * struct se_cmd->scsi_sense_reason already set
2171 printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
2173 cmd->transport_error_status);
2174 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2179 transport_new_cmd_failure(cmd);
2181 ret = transport_send_check_condition_and_sense(cmd,
2182 cmd->scsi_sense_reason, 0);
2188 transport_lun_remove_cmd(cmd);
2189 if (!(transport_cmd_check_stop_to_fabric(cmd)))
2194 cmd->t_state = TRANSPORT_COMPLETE_OK;
2195 transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf);
2198 static void transport_direct_request_timeout(struct se_cmd *cmd)
2200 unsigned long flags;
2202 spin_lock_irqsave(&cmd->t_state_lock, flags);
2203 if (!(atomic_read(&cmd->t_transport_timeout))) {
2204 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2207 if (atomic_read(&cmd->t_task_cdbs_timeout_left)) {
2208 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2212 atomic_sub(atomic_read(&cmd->t_transport_timeout),
2214 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2217 static void transport_generic_request_timeout(struct se_cmd *cmd)
2219 unsigned long flags;
2222 * Reset cmd->t_se_count to allow transport_generic_remove()
2223 * to allow last call to free memory resources.
2225 spin_lock_irqsave(&cmd->t_state_lock, flags);
2226 if (atomic_read(&cmd->t_transport_timeout) > 1) {
2227 int tmp = (atomic_read(&cmd->t_transport_timeout) - 1);
2229 atomic_sub(tmp, &cmd->t_se_count);
2231 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2233 transport_generic_remove(cmd, 0);
2237 transport_generic_allocate_buf(struct se_cmd *cmd, u32 data_length)
2241 buf = kzalloc(data_length, GFP_KERNEL);
2243 printk(KERN_ERR "Unable to allocate memory for buffer\n");
2247 cmd->t_tasks_se_num = 0;
2248 cmd->t_task_buf = buf;
2253 static inline u32 transport_lba_21(unsigned char *cdb)
2255 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2258 static inline u32 transport_lba_32(unsigned char *cdb)
2260 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2263 static inline unsigned long long transport_lba_64(unsigned char *cdb)
2265 unsigned int __v1, __v2;
2267 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2268 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2270 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2274 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2276 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2278 unsigned int __v1, __v2;
2280 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2281 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2283 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2286 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2288 unsigned long flags;
2290 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2291 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2292 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2296 * Called from interrupt context.
2298 static void transport_task_timeout_handler(unsigned long data)
2300 struct se_task *task = (struct se_task *)data;
2301 struct se_cmd *cmd = task->task_se_cmd;
2302 unsigned long flags;
2304 DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
2306 spin_lock_irqsave(&cmd->t_state_lock, flags);
2307 if (task->task_flags & TF_STOP) {
2308 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2311 task->task_flags &= ~TF_RUNNING;
2314 * Determine if transport_complete_task() has already been called.
2316 if (!(atomic_read(&task->task_active))) {
2317 DEBUG_TT("transport task: %p cmd: %p timeout task_active"
2318 " == 0\n", task, cmd);
2319 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2323 atomic_inc(&cmd->t_se_count);
2324 atomic_inc(&cmd->t_transport_timeout);
2325 cmd->t_tasks_failed = 1;
2327 atomic_set(&task->task_timeout, 1);
2328 task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
2329 task->task_scsi_status = 1;
2331 if (atomic_read(&task->task_stop)) {
2332 DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
2333 " == 1\n", task, cmd);
2334 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2335 complete(&task->task_stop_comp);
2339 if (!(atomic_dec_and_test(&cmd->t_task_cdbs_left))) {
2340 DEBUG_TT("transport task: %p cmd: %p timeout non zero"
2341 " t_task_cdbs_left\n", task, cmd);
2342 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2345 DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2348 cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
2349 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2351 transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
2355 * Called with cmd->t_state_lock held.
2357 static void transport_start_task_timer(struct se_task *task)
2359 struct se_device *dev = task->se_dev;
2362 if (task->task_flags & TF_RUNNING)
2365 * If the task_timeout is disabled, exit now.
2367 timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
2371 init_timer(&task->task_timer);
2372 task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
2373 task->task_timer.data = (unsigned long) task;
2374 task->task_timer.function = transport_task_timeout_handler;
2376 task->task_flags |= TF_RUNNING;
2377 add_timer(&task->task_timer);
2379 printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
2380 " %d\n", task->task_se_cmd, task, timeout);
2385 * Called with spin_lock_irq(&cmd->t_state_lock) held.
2387 void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
2389 struct se_cmd *cmd = task->task_se_cmd;
2391 if (!(task->task_flags & TF_RUNNING))
2394 task->task_flags |= TF_STOP;
2395 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
2397 del_timer_sync(&task->task_timer);
2399 spin_lock_irqsave(&cmd->t_state_lock, *flags);
2400 task->task_flags &= ~TF_RUNNING;
2401 task->task_flags &= ~TF_STOP;
2404 static void transport_stop_all_task_timers(struct se_cmd *cmd)
2406 struct se_task *task = NULL, *task_tmp;
2407 unsigned long flags;
2409 spin_lock_irqsave(&cmd->t_state_lock, flags);
2410 list_for_each_entry_safe(task, task_tmp,
2411 &cmd->t_task_list, t_list)
2412 __transport_stop_task_timer(task, &flags);
2413 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2416 static inline int transport_tcq_window_closed(struct se_device *dev)
2418 if (dev->dev_tcq_window_closed++ <
2419 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
2420 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
2422 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
2424 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
2429 * Called from Fabric Module context from transport_execute_tasks()
2431 * The return of this function determins if the tasks from struct se_cmd
2432 * get added to the execution queue in transport_execute_tasks(),
2433 * or are added to the delayed or ordered lists here.
2435 static inline int transport_execute_task_attr(struct se_cmd *cmd)
2437 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2440 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2441 * to allow the passed struct se_cmd list of tasks to the front of the list.
2443 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
2444 atomic_inc(&cmd->se_dev->dev_hoq_count);
2445 smp_mb__after_atomic_inc();
2446 DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
2447 " 0x%02x, se_ordered_id: %u\n",
2449 cmd->se_ordered_id);
2451 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
2452 spin_lock(&cmd->se_dev->ordered_cmd_lock);
2453 list_add_tail(&cmd->se_ordered_node,
2454 &cmd->se_dev->ordered_cmd_list);
2455 spin_unlock(&cmd->se_dev->ordered_cmd_lock);
2457 atomic_inc(&cmd->se_dev->dev_ordered_sync);
2458 smp_mb__after_atomic_inc();
2460 DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
2461 " list, se_ordered_id: %u\n",
2463 cmd->se_ordered_id);
2465 * Add ORDERED command to tail of execution queue if
2466 * no other older commands exist that need to be
2469 if (!(atomic_read(&cmd->se_dev->simple_cmds)))
2473 * For SIMPLE and UNTAGGED Task Attribute commands
2475 atomic_inc(&cmd->se_dev->simple_cmds);
2476 smp_mb__after_atomic_inc();
2479 * Otherwise if one or more outstanding ORDERED task attribute exist,
2480 * add the dormant task(s) built for the passed struct se_cmd to the
2481 * execution queue and become in Active state for this struct se_device.
2483 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2485 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2486 * will be drained upon completion of HEAD_OF_QUEUE task.
2488 spin_lock(&cmd->se_dev->delayed_cmd_lock);
2489 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2490 list_add_tail(&cmd->se_delayed_node,
2491 &cmd->se_dev->delayed_cmd_list);
2492 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2494 DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
2495 " delayed CMD list, se_ordered_id: %u\n",
2496 cmd->t_task_cdb[0], cmd->sam_task_attr,
2497 cmd->se_ordered_id);
2499 * Return zero to let transport_execute_tasks() know
2500 * not to add the delayed tasks to the execution list.
2505 * Otherwise, no ORDERED task attributes exist..
2511 * Called from fabric module context in transport_generic_new_cmd() and
2512 * transport_generic_process_write()
2514 static int transport_execute_tasks(struct se_cmd *cmd)
2518 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2519 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2520 transport_generic_request_failure(cmd, NULL, 0, 1);
2525 * Call transport_cmd_check_stop() to see if a fabric exception
2526 * has occurred that prevents execution.
2528 if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
2530 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2531 * attribute for the tasks of the received struct se_cmd CDB
2533 add_tasks = transport_execute_task_attr(cmd);
2537 * This calls transport_add_tasks_from_cmd() to handle
2538 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2539 * (if enabled) in __transport_add_task_to_execute_queue() and
2540 * transport_add_task_check_sam_attr().
2542 transport_add_tasks_from_cmd(cmd);
2545 * Kick the execution queue for the cmd associated struct se_device
2549 __transport_execute_tasks(cmd->se_dev);
2554 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2555 * from struct se_device->execute_task_list and
2557 * Called from transport_processing_thread()
2559 static int __transport_execute_tasks(struct se_device *dev)
2562 struct se_cmd *cmd = NULL;
2563 struct se_task *task = NULL;
2564 unsigned long flags;
2567 * Check if there is enough room in the device and HBA queue to send
2568 * struct se_tasks to the selected transport.
2571 if (!atomic_read(&dev->depth_left))
2572 return transport_tcq_window_closed(dev);
2574 dev->dev_tcq_window_closed = 0;
2576 spin_lock_irq(&dev->execute_task_lock);
2577 if (list_empty(&dev->execute_task_list)) {
2578 spin_unlock_irq(&dev->execute_task_lock);
2581 task = list_first_entry(&dev->execute_task_list,
2582 struct se_task, t_execute_list);
2583 list_del(&task->t_execute_list);
2584 atomic_set(&task->task_execute_queue, 0);
2585 atomic_dec(&dev->execute_tasks);
2586 spin_unlock_irq(&dev->execute_task_lock);
2588 atomic_dec(&dev->depth_left);
2590 cmd = task->task_se_cmd;
2592 spin_lock_irqsave(&cmd->t_state_lock, flags);
2593 atomic_set(&task->task_active, 1);
2594 atomic_set(&task->task_sent, 1);
2595 atomic_inc(&cmd->t_task_cdbs_sent);
2597 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2598 cmd->t_task_list_num)
2599 atomic_set(&cmd->transport_sent, 1);
2601 transport_start_task_timer(task);
2602 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2604 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2605 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2606 * struct se_subsystem_api->do_task() caller below.
2608 if (cmd->transport_emulate_cdb) {
2609 error = cmd->transport_emulate_cdb(cmd);
2611 cmd->transport_error_status = error;
2612 atomic_set(&task->task_active, 0);
2613 atomic_set(&cmd->transport_sent, 0);
2614 transport_stop_tasks_for_cmd(cmd);
2615 transport_generic_request_failure(cmd, dev, 0, 1);
2619 * Handle the successful completion for transport_emulate_cdb()
2620 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2621 * Otherwise the caller is expected to complete the task with
2624 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2625 cmd->scsi_status = SAM_STAT_GOOD;
2626 task->task_scsi_status = GOOD;
2627 transport_complete_task(task, 1);
2631 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2632 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2633 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2634 * LUN emulation code.
2636 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2637 * call ->do_task() directly and let the underlying TCM subsystem plugin
2638 * code handle the CDB emulation.
2640 if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2641 (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2642 error = transport_emulate_control_cdb(task);
2644 error = dev->transport->do_task(task);
2647 cmd->transport_error_status = error;
2648 atomic_set(&task->task_active, 0);
2649 atomic_set(&cmd->transport_sent, 0);
2650 transport_stop_tasks_for_cmd(cmd);
2651 transport_generic_request_failure(cmd, dev, 0, 1);
2660 void transport_new_cmd_failure(struct se_cmd *se_cmd)
2662 unsigned long flags;
2664 * Any unsolicited data will get dumped for failed command inside of
2667 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2668 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2669 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2670 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2673 static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
2675 static inline u32 transport_get_sectors_6(
2680 struct se_device *dev = cmd->se_dev;
2683 * Assume TYPE_DISK for non struct se_device objects.
2684 * Use 8-bit sector value.
2690 * Use 24-bit allocation length for TYPE_TAPE.
2692 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2693 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2696 * Everything else assume TYPE_DISK Sector CDB location.
2697 * Use 8-bit sector value.
2703 static inline u32 transport_get_sectors_10(
2708 struct se_device *dev = cmd->se_dev;
2711 * Assume TYPE_DISK for non struct se_device objects.
2712 * Use 16-bit sector value.
2718 * XXX_10 is not defined in SSC, throw an exception
2720 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2726 * Everything else assume TYPE_DISK Sector CDB location.
2727 * Use 16-bit sector value.
2730 return (u32)(cdb[7] << 8) + cdb[8];
2733 static inline u32 transport_get_sectors_12(
2738 struct se_device *dev = cmd->se_dev;
2741 * Assume TYPE_DISK for non struct se_device objects.
2742 * Use 32-bit sector value.
2748 * XXX_12 is not defined in SSC, throw an exception
2750 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2756 * Everything else assume TYPE_DISK Sector CDB location.
2757 * Use 32-bit sector value.
2760 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2763 static inline u32 transport_get_sectors_16(
2768 struct se_device *dev = cmd->se_dev;
2771 * Assume TYPE_DISK for non struct se_device objects.
2772 * Use 32-bit sector value.
2778 * Use 24-bit allocation length for TYPE_TAPE.
2780 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2781 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2784 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2785 (cdb[12] << 8) + cdb[13];
2789 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2791 static inline u32 transport_get_sectors_32(
2797 * Assume TYPE_DISK for non struct se_device objects.
2798 * Use 32-bit sector value.
2800 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2801 (cdb[30] << 8) + cdb[31];
2805 static inline u32 transport_get_size(
2810 struct se_device *dev = cmd->se_dev;
2812 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2813 if (cdb[1] & 1) { /* sectors */
2814 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2819 printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
2820 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2821 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2822 dev->transport->name);
2824 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2827 unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
2829 unsigned char result = 0;
2833 if ((val[0] >= 'a') && (val[0] <= 'f'))
2834 result = ((val[0] - 'a' + 10) & 0xf) << 4;
2836 if ((val[0] >= 'A') && (val[0] <= 'F'))
2837 result = ((val[0] - 'A' + 10) & 0xf) << 4;
2839 result = ((val[0] - '0') & 0xf) << 4;
2843 if ((val[1] >= 'a') && (val[1] <= 'f'))
2844 result |= ((val[1] - 'a' + 10) & 0xf);
2846 if ((val[1] >= 'A') && (val[1] <= 'F'))
2847 result |= ((val[1] - 'A' + 10) & 0xf);
2849 result |= ((val[1] - '0') & 0xf);
2853 EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
2855 static void transport_xor_callback(struct se_cmd *cmd)
2857 unsigned char *buf, *addr;
2858 struct se_mem *se_mem;
2859 unsigned int offset;
2862 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2864 * 1) read the specified logical block(s);
2865 * 2) transfer logical blocks from the data-out buffer;
2866 * 3) XOR the logical blocks transferred from the data-out buffer with
2867 * the logical blocks read, storing the resulting XOR data in a buffer;
2868 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2869 * blocks transferred from the data-out buffer; and
2870 * 5) transfer the resulting XOR data to the data-in buffer.
2872 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2874 printk(KERN_ERR "Unable to allocate xor_callback buf\n");
2878 * Copy the scatterlist WRITE buffer located at cmd->t_mem_list
2879 * into the locally allocated *buf
2881 transport_memcpy_se_mem_read_contig(buf, &cmd->t_mem_list,
2884 * Now perform the XOR against the BIDI read memory located at
2885 * cmd->t_mem_bidi_list
2889 list_for_each_entry(se_mem, &cmd->t_mem_bidi_list, se_list) {
2890 addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
2894 for (i = 0; i < se_mem->se_len; i++)
2895 *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
2897 offset += se_mem->se_len;
2898 kunmap_atomic(addr, KM_USER0);
2905 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2907 static int transport_get_sense_data(struct se_cmd *cmd)
2909 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2910 struct se_device *dev;
2911 struct se_task *task = NULL, *task_tmp;
2912 unsigned long flags;
2915 WARN_ON(!cmd->se_lun);
2917 spin_lock_irqsave(&cmd->t_state_lock, flags);
2918 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2919 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2923 list_for_each_entry_safe(task, task_tmp,
2924 &cmd->t_task_list, t_list) {
2926 if (!task->task_sense)
2933 if (!dev->transport->get_sense_buffer) {
2934 printk(KERN_ERR "dev->transport->get_sense_buffer"
2939 sense_buffer = dev->transport->get_sense_buffer(task);
2940 if (!(sense_buffer)) {
2941 printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
2942 " sense buffer for task with sense\n",
2943 cmd->se_tfo->get_task_tag(cmd), task->task_no);
2946 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2948 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2949 TRANSPORT_SENSE_BUFFER);
2951 memcpy(&buffer[offset], sense_buffer,
2952 TRANSPORT_SENSE_BUFFER);
2953 cmd->scsi_status = task->task_scsi_status;
2954 /* Automatically padded */
2955 cmd->scsi_sense_length =
2956 (TRANSPORT_SENSE_BUFFER + offset);
2958 printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2960 dev->se_hba->hba_id, dev->transport->name,
2964 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2969 static int transport_allocate_resources(struct se_cmd *cmd)
2971 u32 length = cmd->data_length;
2973 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
2974 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB))
2975 return transport_generic_get_mem(cmd, length);
2976 else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB)
2977 return transport_generic_allocate_buf(cmd, length);
2983 transport_handle_reservation_conflict(struct se_cmd *cmd)
2985 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
2986 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2987 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2988 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2990 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2991 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2994 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2997 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2998 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2999 cmd->orig_fe_lun, 0x2C,
3000 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
3004 /* transport_generic_cmd_sequencer():
3006 * Generic Command Sequencer that should work for most DAS transport
3009 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
3012 * FIXME: Need to support other SCSI OPCODES where as well.
3014 static int transport_generic_cmd_sequencer(
3018 struct se_device *dev = cmd->se_dev;
3019 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
3020 int ret = 0, sector_ret = 0, passthrough;
3021 u32 sectors = 0, size = 0, pr_reg_type = 0;
3025 * Check for an existing UNIT ATTENTION condition
3027 if (core_scsi3_ua_check(cmd, cdb) < 0) {
3028 cmd->transport_wait_for_tasks =
3029 &transport_nop_wait_for_tasks;
3030 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3031 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
3035 * Check status of Asymmetric Logical Unit Assignment port
3037 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
3039 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3041 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
3042 * The ALUA additional sense code qualifier (ASCQ) is determined
3043 * by the ALUA primary or secondary access state..
3047 printk(KERN_INFO "[%s]: ALUA TG Port not available,"
3048 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
3049 cmd->se_tfo->get_fabric_name(), alua_ascq);
3051 transport_set_sense_codes(cmd, 0x04, alua_ascq);
3052 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3053 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
3056 goto out_invalid_cdb_field;
3059 * Check status for SPC-3 Persistent Reservations
3061 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
3062 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
3063 cmd, cdb, pr_reg_type) != 0)
3064 return transport_handle_reservation_conflict(cmd);
3066 * This means the CDB is allowed for the SCSI Initiator port
3067 * when said port is *NOT* holding the legacy SPC-2 or
3068 * SPC-3 Persistent Reservation.
3074 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
3076 goto out_unsupported_cdb;
3077 size = transport_get_size(sectors, cdb, cmd);
3078 cmd->transport_split_cdb = &split_cdb_XX_6;
3079 cmd->t_task_lba = transport_lba_21(cdb);
3080 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3083 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3085 goto out_unsupported_cdb;
3086 size = transport_get_size(sectors, cdb, cmd);
3087 cmd->transport_split_cdb = &split_cdb_XX_10;
3088 cmd->t_task_lba = transport_lba_32(cdb);
3089 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3092 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
3094 goto out_unsupported_cdb;
3095 size = transport_get_size(sectors, cdb, cmd);
3096 cmd->transport_split_cdb = &split_cdb_XX_12;
3097 cmd->t_task_lba = transport_lba_32(cdb);
3098 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3101 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3103 goto out_unsupported_cdb;
3104 size = transport_get_size(sectors, cdb, cmd);
3105 cmd->transport_split_cdb = &split_cdb_XX_16;
3106 cmd->t_task_lba = transport_lba_64(cdb);
3107 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3110 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
3112 goto out_unsupported_cdb;
3113 size = transport_get_size(sectors, cdb, cmd);
3114 cmd->transport_split_cdb = &split_cdb_XX_6;
3115 cmd->t_task_lba = transport_lba_21(cdb);
3116 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3119 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3121 goto out_unsupported_cdb;
3122 size = transport_get_size(sectors, cdb, cmd);
3123 cmd->transport_split_cdb = &split_cdb_XX_10;
3124 cmd->t_task_lba = transport_lba_32(cdb);
3125 cmd->t_tasks_fua = (cdb[1] & 0x8);
3126 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3129 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
3131 goto out_unsupported_cdb;
3132 size = transport_get_size(sectors, cdb, cmd);
3133 cmd->transport_split_cdb = &split_cdb_XX_12;
3134 cmd->t_task_lba = transport_lba_32(cdb);
3135 cmd->t_tasks_fua = (cdb[1] & 0x8);
3136 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3139 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3141 goto out_unsupported_cdb;
3142 size = transport_get_size(sectors, cdb, cmd);
3143 cmd->transport_split_cdb = &split_cdb_XX_16;
3144 cmd->t_task_lba = transport_lba_64(cdb);
3145 cmd->t_tasks_fua = (cdb[1] & 0x8);
3146 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3148 case XDWRITEREAD_10:
3149 if ((cmd->data_direction != DMA_TO_DEVICE) ||
3150 !(cmd->t_tasks_bidi))
3151 goto out_invalid_cdb_field;
3152 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3154 goto out_unsupported_cdb;
3155 size = transport_get_size(sectors, cdb, cmd);
3156 cmd->transport_split_cdb = &split_cdb_XX_10;
3157 cmd->t_task_lba = transport_lba_32(cdb);
3158 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3159 passthrough = (dev->transport->transport_type ==
3160 TRANSPORT_PLUGIN_PHBA_PDEV);
3162 * Skip the remaining assignments for TCM/PSCSI passthrough
3167 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3169 cmd->transport_complete_callback = &transport_xor_callback;
3170 cmd->t_tasks_fua = (cdb[1] & 0x8);
3172 case VARIABLE_LENGTH_CMD:
3173 service_action = get_unaligned_be16(&cdb[8]);
3175 * Determine if this is TCM/PSCSI device and we should disable
3176 * internal emulation for this CDB.
3178 passthrough = (dev->transport->transport_type ==
3179 TRANSPORT_PLUGIN_PHBA_PDEV);
3181 switch (service_action) {
3182 case XDWRITEREAD_32:
3183 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
3185 goto out_unsupported_cdb;
3186 size = transport_get_size(sectors, cdb, cmd);
3188 * Use WRITE_32 and READ_32 opcodes for the emulated
3189 * XDWRITE_READ_32 logic.
3191 cmd->transport_split_cdb = &split_cdb_XX_32;
3192 cmd->t_task_lba = transport_lba_64_ext(cdb);
3193 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3196 * Skip the remaining assignments for TCM/PSCSI passthrough
3202 * Setup BIDI XOR callback to be run during
3203 * transport_generic_complete_ok()
3205 cmd->transport_complete_callback = &transport_xor_callback;
3206 cmd->t_tasks_fua = (cdb[10] & 0x8);
3209 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
3211 goto out_unsupported_cdb;
3214 size = transport_get_size(sectors, cdb, cmd);
3216 size = dev->se_sub_dev->se_dev_attrib.block_size;
3218 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
3219 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3222 * Skip the remaining assignments for TCM/PSCSI passthrough
3227 if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
3228 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3229 " bits not supported for Block Discard"
3231 goto out_invalid_cdb_field;
3234 * Currently for the emulated case we only accept
3235 * tpws with the UNMAP=1 bit set.
3237 if (!(cdb[10] & 0x08)) {
3238 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
3239 " supported for Block Discard Emulation\n");
3240 goto out_invalid_cdb_field;
3244 printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
3245 " 0x%04x not supported\n", service_action);
3246 goto out_unsupported_cdb;
3249 case MAINTENANCE_IN:
3250 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3251 /* MAINTENANCE_IN from SCC-2 */
3253 * Check for emulated MI_REPORT_TARGET_PGS.
3255 if (cdb[1] == MI_REPORT_TARGET_PGS) {
3256 cmd->transport_emulate_cdb =
3257 (su_dev->t10_alua.alua_type ==
3258 SPC3_ALUA_EMULATED) ?
3259 core_emulate_report_target_port_groups :
3262 size = (cdb[6] << 24) | (cdb[7] << 16) |
3263 (cdb[8] << 8) | cdb[9];
3265 /* GPCMD_SEND_KEY from multi media commands */
3266 size = (cdb[8] << 8) + cdb[9];
3268 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3272 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3274 case MODE_SELECT_10:
3275 size = (cdb[7] << 8) + cdb[8];
3276 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3280 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3283 case GPCMD_READ_BUFFER_CAPACITY:
3284 case GPCMD_SEND_OPC:
3287 size = (cdb[7] << 8) + cdb[8];
3288 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3290 case READ_BLOCK_LIMITS:
3291 size = READ_BLOCK_LEN;
3292 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3294 case GPCMD_GET_CONFIGURATION:
3295 case GPCMD_READ_FORMAT_CAPACITIES:
3296 case GPCMD_READ_DISC_INFO:
3297 case GPCMD_READ_TRACK_RZONE_INFO:
3298 size = (cdb[7] << 8) + cdb[8];
3299 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3301 case PERSISTENT_RESERVE_IN:
3302 case PERSISTENT_RESERVE_OUT:
3303 cmd->transport_emulate_cdb =
3304 (su_dev->t10_pr.res_type ==
3305 SPC3_PERSISTENT_RESERVATIONS) ?
3306 core_scsi3_emulate_pr : NULL;
3307 size = (cdb[7] << 8) + cdb[8];
3308 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3310 case GPCMD_MECHANISM_STATUS:
3311 case GPCMD_READ_DVD_STRUCTURE:
3312 size = (cdb[8] << 8) + cdb[9];
3313 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3316 size = READ_POSITION_LEN;
3317 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3319 case MAINTENANCE_OUT:
3320 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3321 /* MAINTENANCE_OUT from SCC-2
3323 * Check for emulated MO_SET_TARGET_PGS.
3325 if (cdb[1] == MO_SET_TARGET_PGS) {
3326 cmd->transport_emulate_cdb =
3327 (su_dev->t10_alua.alua_type ==
3328 SPC3_ALUA_EMULATED) ?
3329 core_emulate_set_target_port_groups :
3333 size = (cdb[6] << 24) | (cdb[7] << 16) |
3334 (cdb[8] << 8) | cdb[9];
3336 /* GPCMD_REPORT_KEY from multi media commands */
3337 size = (cdb[8] << 8) + cdb[9];
3339 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3342 size = (cdb[3] << 8) + cdb[4];
3344 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3345 * See spc4r17 section 5.3
3347 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3348 cmd->sam_task_attr = MSG_HEAD_TAG;
3349 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3352 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3353 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3356 size = READ_CAP_LEN;
3357 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3359 case READ_MEDIA_SERIAL_NUMBER:
3360 case SECURITY_PROTOCOL_IN:
3361 case SECURITY_PROTOCOL_OUT:
3362 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3363 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3365 case SERVICE_ACTION_IN:
3366 case ACCESS_CONTROL_IN:
3367 case ACCESS_CONTROL_OUT:
3369 case READ_ATTRIBUTE:
3370 case RECEIVE_COPY_RESULTS:
3371 case WRITE_ATTRIBUTE:
3372 size = (cdb[10] << 24) | (cdb[11] << 16) |
3373 (cdb[12] << 8) | cdb[13];
3374 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3376 case RECEIVE_DIAGNOSTIC:
3377 case SEND_DIAGNOSTIC:
3378 size = (cdb[3] << 8) | cdb[4];
3379 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3381 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3384 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3385 size = (2336 * sectors);
3386 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3391 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3395 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3397 case READ_ELEMENT_STATUS:
3398 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
3399 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3402 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3403 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3408 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3409 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3411 if (cdb[0] == RESERVE_10)
3412 size = (cdb[7] << 8) | cdb[8];
3414 size = cmd->data_length;
3417 * Setup the legacy emulated handler for SPC-2 and
3418 * >= SPC-3 compatible reservation handling (CRH=1)
3419 * Otherwise, we assume the underlying SCSI logic is
3420 * is running in SPC_PASSTHROUGH, and wants reservations
3421 * emulation disabled.
3423 cmd->transport_emulate_cdb =
3424 (su_dev->t10_pr.res_type !=
3426 core_scsi2_emulate_crh : NULL;
3427 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3432 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3433 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3435 if (cdb[0] == RELEASE_10)
3436 size = (cdb[7] << 8) | cdb[8];
3438 size = cmd->data_length;
3440 cmd->transport_emulate_cdb =
3441 (su_dev->t10_pr.res_type !=
3443 core_scsi2_emulate_crh : NULL;
3444 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3446 case SYNCHRONIZE_CACHE:
3447 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3449 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3451 if (cdb[0] == SYNCHRONIZE_CACHE) {
3452 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3453 cmd->t_task_lba = transport_lba_32(cdb);
3455 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3456 cmd->t_task_lba = transport_lba_64(cdb);
3459 goto out_unsupported_cdb;
3461 size = transport_get_size(sectors, cdb, cmd);
3462 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3465 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3467 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3470 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3471 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3473 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3475 * Check to ensure that LBA + Range does not exceed past end of
3478 if (!transport_cmd_get_valid_sectors(cmd))
3479 goto out_invalid_cdb_field;
3482 size = get_unaligned_be16(&cdb[7]);
3483 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3486 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3488 goto out_unsupported_cdb;
3491 size = transport_get_size(sectors, cdb, cmd);
3493 size = dev->se_sub_dev->se_dev_attrib.block_size;
3495 cmd->t_task_lba = get_unaligned_be16(&cdb[2]);
3496 passthrough = (dev->transport->transport_type ==
3497 TRANSPORT_PLUGIN_PHBA_PDEV);
3499 * Determine if the received WRITE_SAME_16 is used to for direct
3500 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
3501 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
3502 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
3503 * TCM/FILEIO subsystem plugin backstores.
3505 if (!(passthrough)) {
3506 if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
3507 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3508 " bits not supported for Block Discard"
3510 goto out_invalid_cdb_field;
3513 * Currently for the emulated case we only accept
3514 * tpws with the UNMAP=1 bit set.
3516 if (!(cdb[1] & 0x08)) {
3517 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
3518 " supported for Block Discard Emulation\n");
3519 goto out_invalid_cdb_field;
3522 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3524 case ALLOW_MEDIUM_REMOVAL:
3525 case GPCMD_CLOSE_TRACK:
3527 case INITIALIZE_ELEMENT_STATUS:
3528 case GPCMD_LOAD_UNLOAD:
3531 case GPCMD_SET_SPEED:
3534 case TEST_UNIT_READY:
3536 case WRITE_FILEMARKS:
3538 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3541 cmd->transport_emulate_cdb =
3542 transport_core_report_lun_response;
3543 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3545 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3546 * See spc4r17 section 5.3
3548 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3549 cmd->sam_task_attr = MSG_HEAD_TAG;
3550 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3553 printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
3554 " 0x%02x, sending CHECK_CONDITION.\n",
3555 cmd->se_tfo->get_fabric_name(), cdb[0]);
3556 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3557 goto out_unsupported_cdb;
3560 if (size != cmd->data_length) {
3561 printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
3562 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3563 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3564 cmd->data_length, size, cdb[0]);
3566 cmd->cmd_spdtl = size;
3568 if (cmd->data_direction == DMA_TO_DEVICE) {
3569 printk(KERN_ERR "Rejecting underflow/overflow"
3571 goto out_invalid_cdb_field;
3574 * Reject READ_* or WRITE_* with overflow/underflow for
3575 * type SCF_SCSI_DATA_SG_IO_CDB.
3577 if (!(ret) && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3578 printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
3579 " CDB on non 512-byte sector setup subsystem"
3580 " plugin: %s\n", dev->transport->name);
3581 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3582 goto out_invalid_cdb_field;
3585 if (size > cmd->data_length) {
3586 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3587 cmd->residual_count = (size - cmd->data_length);
3589 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3590 cmd->residual_count = (cmd->data_length - size);
3592 cmd->data_length = size;
3595 transport_set_supported_SAM_opcode(cmd);
3598 out_unsupported_cdb:
3599 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3600 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3602 out_invalid_cdb_field:
3603 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3604 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3608 static inline void transport_release_tasks(struct se_cmd *);
3610 static void transport_memcpy_se_mem_read_contig(
3612 struct list_head *se_mem_list,
3615 struct se_mem *se_mem;
3619 list_for_each_entry(se_mem, se_mem_list, se_list) {
3620 length = min_t(u32, se_mem->se_len, tot_len);
3621 src = page_address(se_mem->se_page) + se_mem->se_off;
3622 memcpy(dst, src, length);
3631 * Called from transport_generic_complete_ok() and
3632 * transport_generic_request_failure() to determine which dormant/delayed
3633 * and ordered cmds need to have their tasks added to the execution queue.
3635 static void transport_complete_task_attr(struct se_cmd *cmd)
3637 struct se_device *dev = cmd->se_dev;
3638 struct se_cmd *cmd_p, *cmd_tmp;
3639 int new_active_tasks = 0;
3641 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3642 atomic_dec(&dev->simple_cmds);
3643 smp_mb__after_atomic_dec();
3644 dev->dev_cur_ordered_id++;
3645 DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
3646 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3647 cmd->se_ordered_id);
3648 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3649 atomic_dec(&dev->dev_hoq_count);
3650 smp_mb__after_atomic_dec();
3651 dev->dev_cur_ordered_id++;
3652 DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
3653 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3654 cmd->se_ordered_id);
3655 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3656 spin_lock(&dev->ordered_cmd_lock);
3657 list_del(&cmd->se_ordered_node);
3658 atomic_dec(&dev->dev_ordered_sync);
3659 smp_mb__after_atomic_dec();
3660 spin_unlock(&dev->ordered_cmd_lock);
3662 dev->dev_cur_ordered_id++;
3663 DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
3664 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3667 * Process all commands up to the last received
3668 * ORDERED task attribute which requires another blocking
3671 spin_lock(&dev->delayed_cmd_lock);
3672 list_for_each_entry_safe(cmd_p, cmd_tmp,
3673 &dev->delayed_cmd_list, se_delayed_node) {
3675 list_del(&cmd_p->se_delayed_node);
3676 spin_unlock(&dev->delayed_cmd_lock);
3678 DEBUG_STA("Calling add_tasks() for"
3679 " cmd_p: 0x%02x Task Attr: 0x%02x"
3680 " Dormant -> Active, se_ordered_id: %u\n",
3681 T_TASK(cmd_p)->t_task_cdb[0],
3682 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3684 transport_add_tasks_from_cmd(cmd_p);
3687 spin_lock(&dev->delayed_cmd_lock);
3688 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3691 spin_unlock(&dev->delayed_cmd_lock);
3693 * If new tasks have become active, wake up the transport thread
3694 * to do the processing of the Active tasks.
3696 if (new_active_tasks != 0)
3697 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3700 static int transport_complete_qf(struct se_cmd *cmd)
3704 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
3705 return cmd->se_tfo->queue_status(cmd);
3707 switch (cmd->data_direction) {
3708 case DMA_FROM_DEVICE:
3709 ret = cmd->se_tfo->queue_data_in(cmd);
3712 if (!list_empty(&cmd->t_mem_bidi_list)) {
3713 ret = cmd->se_tfo->queue_data_in(cmd);
3717 /* Fall through for DMA_TO_DEVICE */
3719 ret = cmd->se_tfo->queue_status(cmd);
3728 static void transport_handle_queue_full(
3730 struct se_device *dev,
3731 int (*qf_callback)(struct se_cmd *))
3733 spin_lock_irq(&dev->qf_cmd_lock);
3734 cmd->se_cmd_flags |= SCF_EMULATE_QUEUE_FULL;
3735 cmd->transport_qf_callback = qf_callback;
3736 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3737 atomic_inc(&dev->dev_qf_count);
3738 smp_mb__after_atomic_inc();
3739 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3741 schedule_work(&cmd->se_dev->qf_work_queue);
3744 static void transport_generic_complete_ok(struct se_cmd *cmd)
3746 int reason = 0, ret;
3748 * Check if we need to move delayed/dormant tasks from cmds on the
3749 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3752 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3753 transport_complete_task_attr(cmd);
3755 * Check to schedule QUEUE_FULL work, or execute an existing
3756 * cmd->transport_qf_callback()
3758 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3759 schedule_work(&cmd->se_dev->qf_work_queue);
3761 if (cmd->transport_qf_callback) {
3762 ret = cmd->transport_qf_callback(cmd);
3766 cmd->transport_qf_callback = NULL;
3770 * Check if we need to retrieve a sense buffer from
3771 * the struct se_cmd in question.
3773 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3774 if (transport_get_sense_data(cmd) < 0)
3775 reason = TCM_NON_EXISTENT_LUN;
3778 * Only set when an struct se_task->task_scsi_status returned
3779 * a non GOOD status.
3781 if (cmd->scsi_status) {
3782 ret = transport_send_check_condition_and_sense(
3787 transport_lun_remove_cmd(cmd);
3788 transport_cmd_check_stop_to_fabric(cmd);
3793 * Check for a callback, used by amongst other things
3794 * XDWRITE_READ_10 emulation.
3796 if (cmd->transport_complete_callback)
3797 cmd->transport_complete_callback(cmd);
3799 switch (cmd->data_direction) {
3800 case DMA_FROM_DEVICE:
3801 spin_lock(&cmd->se_lun->lun_sep_lock);
3802 if (cmd->se_lun->lun_sep) {
3803 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3806 spin_unlock(&cmd->se_lun->lun_sep_lock);
3808 * If enabled by TCM fabric module pre-registered SGL
3809 * memory, perform the memcpy() from the TCM internal
3810 * contiguous buffer back to the original SGL.
3812 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
3813 sg_copy_from_buffer(cmd->t_task_pt_sgl,
3814 cmd->t_task_pt_sgl_num,
3818 ret = cmd->se_tfo->queue_data_in(cmd);
3823 spin_lock(&cmd->se_lun->lun_sep_lock);
3824 if (cmd->se_lun->lun_sep) {
3825 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3828 spin_unlock(&cmd->se_lun->lun_sep_lock);
3830 * Check if we need to send READ payload for BIDI-COMMAND
3832 if (!list_empty(&cmd->t_mem_bidi_list)) {
3833 spin_lock(&cmd->se_lun->lun_sep_lock);
3834 if (cmd->se_lun->lun_sep) {
3835 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3838 spin_unlock(&cmd->se_lun->lun_sep_lock);
3839 ret = cmd->se_tfo->queue_data_in(cmd);
3844 /* Fall through for DMA_TO_DEVICE */
3846 ret = cmd->se_tfo->queue_status(cmd);
3855 transport_lun_remove_cmd(cmd);
3856 transport_cmd_check_stop_to_fabric(cmd);
3860 printk(KERN_INFO "Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3861 " data_direction: %d\n", cmd, cmd->data_direction);
3862 transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf);
3865 static void transport_free_dev_tasks(struct se_cmd *cmd)
3867 struct se_task *task, *task_tmp;
3868 unsigned long flags;
3870 spin_lock_irqsave(&cmd->t_state_lock, flags);
3871 list_for_each_entry_safe(task, task_tmp,
3872 &cmd->t_task_list, t_list) {
3873 if (atomic_read(&task->task_active))
3876 kfree(task->task_sg_bidi);
3877 kfree(task->task_sg);
3879 list_del(&task->t_list);
3881 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3883 task->se_dev->transport->free_task(task);
3885 printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
3887 spin_lock_irqsave(&cmd->t_state_lock, flags);
3889 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3892 static inline void transport_free_pages(struct se_cmd *cmd)
3894 struct se_mem *se_mem, *se_mem_tmp;
3897 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3899 if (cmd->se_dev->transport->do_se_mem_map)
3902 if (cmd->t_task_buf) {
3903 kfree(cmd->t_task_buf);
3904 cmd->t_task_buf = NULL;
3908 list_for_each_entry_safe(se_mem, se_mem_tmp,
3909 &cmd->t_mem_list, se_list) {
3911 * We only release call __free_page(struct se_mem->se_page) when
3912 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3915 __free_page(se_mem->se_page);
3917 list_del(&se_mem->se_list);
3918 kmem_cache_free(se_mem_cache, se_mem);
3920 cmd->t_tasks_se_num = 0;
3922 list_for_each_entry_safe(se_mem, se_mem_tmp,
3923 &cmd->t_mem_bidi_list, se_list) {
3925 * We only release call __free_page(struct se_mem->se_page) when
3926 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3929 __free_page(se_mem->se_page);
3931 list_del(&se_mem->se_list);
3932 kmem_cache_free(se_mem_cache, se_mem);
3934 cmd->t_tasks_se_bidi_num = 0;
3937 static inline void transport_release_tasks(struct se_cmd *cmd)
3939 transport_free_dev_tasks(cmd);
3942 static inline int transport_dec_and_check(struct se_cmd *cmd)
3944 unsigned long flags;
3946 spin_lock_irqsave(&cmd->t_state_lock, flags);
3947 if (atomic_read(&cmd->t_fe_count)) {
3948 if (!(atomic_dec_and_test(&cmd->t_fe_count))) {
3949 spin_unlock_irqrestore(&cmd->t_state_lock,
3955 if (atomic_read(&cmd->t_se_count)) {
3956 if (!(atomic_dec_and_test(&cmd->t_se_count))) {
3957 spin_unlock_irqrestore(&cmd->t_state_lock,
3962 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3967 static void transport_release_fe_cmd(struct se_cmd *cmd)
3969 unsigned long flags;
3971 if (transport_dec_and_check(cmd))
3974 spin_lock_irqsave(&cmd->t_state_lock, flags);
3975 if (!(atomic_read(&cmd->transport_dev_active))) {
3976 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3979 atomic_set(&cmd->transport_dev_active, 0);
3980 transport_all_task_dev_remove_state(cmd);
3981 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3983 transport_release_tasks(cmd);
3985 transport_free_pages(cmd);
3986 transport_free_se_cmd(cmd);
3987 cmd->se_tfo->release_cmd(cmd);
3991 transport_generic_remove(struct se_cmd *cmd, int session_reinstatement)
3993 unsigned long flags;
3995 if (transport_dec_and_check(cmd)) {
3996 if (session_reinstatement) {
3997 spin_lock_irqsave(&cmd->t_state_lock, flags);
3998 transport_all_task_dev_remove_state(cmd);
3999 spin_unlock_irqrestore(&cmd->t_state_lock,
4005 spin_lock_irqsave(&cmd->t_state_lock, flags);
4006 if (!(atomic_read(&cmd->transport_dev_active))) {
4007 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4010 atomic_set(&cmd->transport_dev_active, 0);
4011 transport_all_task_dev_remove_state(cmd);
4012 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4014 transport_release_tasks(cmd);
4017 transport_free_pages(cmd);
4018 transport_release_cmd(cmd);
4023 * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
4024 * @cmd: Associated se_cmd descriptor
4025 * @mem: SGL style memory for TCM WRITE / READ
4026 * @sg_mem_num: Number of SGL elements
4027 * @mem_bidi_in: SGL style memory for TCM BIDI READ
4028 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
4030 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
4033 int transport_generic_map_mem_to_cmd(
4035 struct scatterlist *sgl,
4037 struct scatterlist *sgl_bidi,
4042 if (!sgl || !sgl_count)
4046 * Convert sgls (sgl, sgl_bidi) to list of se_mems
4048 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
4049 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
4051 * For CDB using TCM struct se_mem linked list scatterlist memory
4052 * processed into a TCM struct se_subsystem_dev, we do the mapping
4053 * from the passed physical memory to struct se_mem->se_page here.
4055 ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_list, sgl);
4059 cmd->t_tasks_se_num = ret;
4061 * Setup BIDI READ list of struct se_mem elements
4063 if (sgl_bidi && sgl_bidi_count) {
4064 ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_bidi_list, sgl_bidi);
4068 cmd->t_tasks_se_bidi_num = ret;
4070 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
4072 } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
4073 if (sgl_bidi || sgl_bidi_count) {
4074 printk(KERN_ERR "BIDI-Commands not supported using "
4075 "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
4079 * For incoming CDBs using a contiguous buffer internal with TCM,
4080 * save the passed struct scatterlist memory. After TCM storage object
4081 * processing has completed for this struct se_cmd, TCM core will call
4082 * transport_memcpy_[write,read]_contig() as necessary from
4083 * transport_generic_complete_ok() and transport_write_pending() in order
4084 * to copy the TCM buffer to/from the original passed *mem in SGL ->
4085 * struct scatterlist format.
4087 cmd->se_cmd_flags |= SCF_PASSTHROUGH_CONTIG_TO_SG;
4088 cmd->t_task_pt_sgl = sgl;
4089 cmd->t_task_pt_sgl_num = sgl_count;
4094 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
4097 static inline long long transport_dev_end_lba(struct se_device *dev)
4099 return dev->transport->get_blocks(dev) + 1;
4102 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
4104 struct se_device *dev = cmd->se_dev;
4107 if (dev->transport->get_device_type(dev) != TYPE_DISK)
4110 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
4112 if ((cmd->t_task_lba + sectors) >
4113 transport_dev_end_lba(dev)) {
4114 printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
4115 " transport_dev_end_lba(): %llu\n",
4116 cmd->t_task_lba, sectors,
4117 transport_dev_end_lba(dev));
4124 static int transport_new_cmd_obj(struct se_cmd *cmd)
4126 struct se_device *dev = cmd->se_dev;
4130 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
4132 cmd->t_task_list_num = 1;
4137 * Setup any BIDI READ tasks and memory from
4138 * cmd->t_mem_bidi_list so the READ struct se_tasks
4139 * are queued first for the non pSCSI passthrough case.
4141 if (!list_empty(&cmd->t_mem_bidi_list) &&
4142 (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
4143 rc = transport_allocate_tasks(cmd,
4145 transport_cmd_get_valid_sectors(cmd),
4146 DMA_FROM_DEVICE, &cmd->t_mem_bidi_list,
4149 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4150 cmd->scsi_sense_reason =
4151 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4152 return PYX_TRANSPORT_LU_COMM_FAILURE;
4157 * Setup the tasks and memory from cmd->t_mem_list
4158 * Note for BIDI transfers this will contain the WRITE payload
4160 task_cdbs = transport_allocate_tasks(cmd,
4162 transport_cmd_get_valid_sectors(cmd),
4163 cmd->data_direction, &cmd->t_mem_list,
4166 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4167 cmd->scsi_sense_reason =
4168 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4169 return PYX_TRANSPORT_LU_COMM_FAILURE;
4171 cmd->t_task_list_num = task_cdbs;
4174 printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
4175 " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
4176 cmd->t_task_lba, cmd->t_tasks_sectors,
4181 atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
4182 atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
4183 atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
4188 transport_generic_get_mem(struct se_cmd *cmd, u32 length)
4190 struct se_mem *se_mem;
4193 * If the device uses memory mapping this is enough.
4195 if (cmd->se_dev->transport->do_se_mem_map)
4199 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4201 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4205 /* #warning FIXME Allocate contigous pages for struct se_mem elements */
4206 se_mem->se_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
4207 if (!(se_mem->se_page)) {
4208 printk(KERN_ERR "alloc_pages() failed\n");
4212 INIT_LIST_HEAD(&se_mem->se_list);
4213 se_mem->se_len = min_t(u32, length, PAGE_SIZE);
4214 list_add_tail(&se_mem->se_list, &cmd->t_mem_list);
4215 cmd->t_tasks_se_num++;
4217 DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
4218 " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
4221 length -= se_mem->se_len;
4224 DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
4225 cmd->t_tasks_se_num);
4230 __free_pages(se_mem->se_page, 0);
4231 kmem_cache_free(se_mem_cache, se_mem);
4235 int transport_init_task_sg(
4236 struct se_task *task,
4237 struct se_mem *in_se_mem,
4240 struct se_cmd *se_cmd = task->task_se_cmd;
4241 struct se_device *se_dev = se_cmd->se_dev;
4242 struct se_mem *se_mem = in_se_mem;
4243 struct target_core_fabric_ops *tfo = se_cmd->se_tfo;
4244 u32 sg_length, task_size = task->task_size, task_sg_num_padded;
4246 while (task_size != 0) {
4247 DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
4248 " se_mem->se_off(%u) task_offset(%u)\n",
4249 se_mem->se_page, se_mem->se_len,
4250 se_mem->se_off, task_offset);
4252 if (task_offset == 0) {
4253 if (task_size >= se_mem->se_len) {
4254 sg_length = se_mem->se_len;
4256 if (!(list_is_last(&se_mem->se_list,
4257 &se_cmd->t_mem_list)))
4258 se_mem = list_entry(se_mem->se_list.next,
4259 struct se_mem, se_list);
4261 sg_length = task_size;
4262 task_size -= sg_length;
4266 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4267 sg_length, task_size);
4269 if ((se_mem->se_len - task_offset) > task_size) {
4270 sg_length = task_size;
4271 task_size -= sg_length;
4274 sg_length = (se_mem->se_len - task_offset);
4276 if (!(list_is_last(&se_mem->se_list,
4277 &se_cmd->t_mem_list)))
4278 se_mem = list_entry(se_mem->se_list.next,
4279 struct se_mem, se_list);
4282 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4283 sg_length, task_size);
4287 task_size -= sg_length;
4289 DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
4290 task->task_no, task_size);
4292 task->task_sg_num++;
4295 * Check if the fabric module driver is requesting that all
4296 * struct se_task->task_sg[] be chained together.. If so,
4297 * then allocate an extra padding SG entry for linking and
4298 * marking the end of the chained SGL.
4300 if (tfo->task_sg_chaining) {
4301 task_sg_num_padded = (task->task_sg_num + 1);
4302 task->task_padded_sg = 1;
4304 task_sg_num_padded = task->task_sg_num;
4306 task->task_sg = kzalloc(task_sg_num_padded *
4307 sizeof(struct scatterlist), GFP_KERNEL);
4308 if (!(task->task_sg)) {
4309 printk(KERN_ERR "Unable to allocate memory for"
4310 " task->task_sg\n");
4313 sg_init_table(&task->task_sg[0], task_sg_num_padded);
4315 * Setup task->task_sg_bidi for SCSI READ payload for
4316 * TCM/pSCSI passthrough if present for BIDI-COMMAND
4318 if (!list_empty(&se_cmd->t_mem_bidi_list) &&
4319 (se_dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
4320 task->task_sg_bidi = kzalloc(task_sg_num_padded *
4321 sizeof(struct scatterlist), GFP_KERNEL);
4322 if (!(task->task_sg_bidi)) {
4323 kfree(task->task_sg);
4324 task->task_sg = NULL;
4325 printk(KERN_ERR "Unable to allocate memory for"
4326 " task->task_sg_bidi\n");
4329 sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
4332 * For the chaining case, setup the proper end of SGL for the
4333 * initial submission struct task into struct se_subsystem_api.
4334 * This will be cleared later by transport_do_task_sg_chain()
4336 if (task->task_padded_sg) {
4337 sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
4339 * Added the 'if' check before marking end of bi-directional
4340 * scatterlist (which gets created only in case of request
4343 if (task->task_sg_bidi)
4344 sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
4347 DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
4348 " task_sg_num_padded(%u)\n", task->task_sg_num,
4349 task_sg_num_padded);
4351 return task->task_sg_num;
4354 /* Reduce sectors if they are too long for the device */
4355 static inline sector_t transport_limit_task_sectors(
4356 struct se_device *dev,
4357 unsigned long long lba,
4360 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
4362 if (dev->transport->get_device_type(dev) == TYPE_DISK)
4363 if ((lba + sectors) > transport_dev_end_lba(dev))
4364 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
4370 * Convert a sgl into a linked list of se_mems.
4372 static int transport_map_sg_to_mem(
4374 struct list_head *se_mem_list,
4375 struct scatterlist *sg)
4377 struct se_mem *se_mem;
4378 u32 cmd_size = cmd->data_length;
4385 * NOTE: it is safe to return -ENOMEM at any time in creating this
4386 * list because transport_free_pages() will eventually be called, and is
4387 * smart enough to deallocate all list items for sg and sg_bidi lists.
4389 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4391 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4394 INIT_LIST_HEAD(&se_mem->se_list);
4395 DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
4396 " sg_page: %p offset: %d length: %d\n", cmd_size,
4397 sg_page(sg), sg->offset, sg->length);
4399 se_mem->se_page = sg_page(sg);
4400 se_mem->se_off = sg->offset;
4402 if (cmd_size > sg->length) {
4403 se_mem->se_len = sg->length;
4406 se_mem->se_len = cmd_size;
4408 cmd_size -= se_mem->se_len;
4411 DEBUG_MEM("sg_to_mem: sg_count: %u cmd_size: %u\n",
4412 sg_count, cmd_size);
4413 DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
4414 se_mem->se_page, se_mem->se_off, se_mem->se_len);
4416 list_add_tail(&se_mem->se_list, se_mem_list);
4419 DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments\n", sg_count);
4424 /* transport_map_mem_to_sg():
4428 int transport_map_mem_to_sg(
4429 struct se_task *task,
4430 struct list_head *se_mem_list,
4431 struct scatterlist *sg,
4432 struct se_mem *in_se_mem,
4433 struct se_mem **out_se_mem,
4437 struct se_cmd *se_cmd = task->task_se_cmd;
4438 struct se_mem *se_mem = in_se_mem;
4439 u32 task_size = task->task_size, sg_no = 0;
4442 printk(KERN_ERR "Unable to locate valid struct"
4443 " scatterlist pointer\n");
4447 while (task_size != 0) {
4449 * Setup the contiguous array of scatterlists for
4450 * this struct se_task.
4452 sg_assign_page(sg, se_mem->se_page);
4454 if (*task_offset == 0) {
4455 sg->offset = se_mem->se_off;
4457 if (task_size >= se_mem->se_len) {
4458 sg->length = se_mem->se_len;
4460 if (!(list_is_last(&se_mem->se_list,
4461 &se_cmd->t_mem_list))) {
4462 se_mem = list_entry(se_mem->se_list.next,
4463 struct se_mem, se_list);
4467 sg->length = task_size;
4469 * Determine if we need to calculate an offset
4470 * into the struct se_mem on the next go around..
4472 task_size -= sg->length;
4474 *task_offset = sg->length;
4480 sg->offset = (*task_offset + se_mem->se_off);
4482 if ((se_mem->se_len - *task_offset) > task_size) {
4483 sg->length = task_size;
4485 * Determine if we need to calculate an offset
4486 * into the struct se_mem on the next go around..
4488 task_size -= sg->length;
4490 *task_offset += sg->length;
4494 sg->length = (se_mem->se_len - *task_offset);
4496 if (!(list_is_last(&se_mem->se_list,
4497 &se_cmd->t_mem_list))) {
4498 se_mem = list_entry(se_mem->se_list.next,
4499 struct se_mem, se_list);
4506 task_size -= sg->length;
4508 DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
4509 " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
4510 sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
4518 if (task_size > se_cmd->data_length)
4521 *out_se_mem = se_mem;
4523 DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
4524 " SGs\n", task->task_no, *se_mem_cnt, sg_no);
4530 * This function can be used by HW target mode drivers to create a linked
4531 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4532 * This is intended to be called during the completion path by TCM Core
4533 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4535 void transport_do_task_sg_chain(struct se_cmd *cmd)
4537 struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
4538 struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
4539 struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
4540 struct se_task *task;
4541 struct target_core_fabric_ops *tfo = cmd->se_tfo;
4542 u32 task_sg_num = 0, sg_count = 0;
4545 if (tfo->task_sg_chaining == 0) {
4546 printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
4547 " %s\n", tfo->get_fabric_name());
4552 * Walk the struct se_task list and setup scatterlist chains
4553 * for each contiguously allocated struct se_task->task_sg[].
4555 list_for_each_entry(task, &cmd->t_task_list, t_list) {
4556 if (!(task->task_sg) || !(task->task_padded_sg))
4559 if (sg_head && sg_link) {
4560 sg_head_cur = &task->task_sg[0];
4561 sg_link_cur = &task->task_sg[task->task_sg_num];
4563 * Either add chain or mark end of scatterlist
4565 if (!(list_is_last(&task->t_list,
4566 &cmd->t_task_list))) {
4568 * Clear existing SGL termination bit set in
4569 * transport_init_task_sg(), see sg_mark_end()
4571 sg_end_cur = &task->task_sg[task->task_sg_num - 1];
4572 sg_end_cur->page_link &= ~0x02;
4574 sg_chain(sg_head, task_sg_num, sg_head_cur);
4575 sg_count += task->task_sg_num;
4576 task_sg_num = (task->task_sg_num + 1);
4578 sg_chain(sg_head, task_sg_num, sg_head_cur);
4579 sg_count += task->task_sg_num;
4580 task_sg_num = task->task_sg_num;
4583 sg_head = sg_head_cur;
4584 sg_link = sg_link_cur;
4587 sg_head = sg_first = &task->task_sg[0];
4588 sg_link = &task->task_sg[task->task_sg_num];
4590 * Check for single task..
4592 if (!(list_is_last(&task->t_list, &cmd->t_task_list))) {
4594 * Clear existing SGL termination bit set in
4595 * transport_init_task_sg(), see sg_mark_end()
4597 sg_end = &task->task_sg[task->task_sg_num - 1];
4598 sg_end->page_link &= ~0x02;
4599 sg_count += task->task_sg_num;
4600 task_sg_num = (task->task_sg_num + 1);
4602 sg_count += task->task_sg_num;
4603 task_sg_num = task->task_sg_num;
4607 * Setup the starting pointer and total t_tasks_sg_linked_no including
4608 * padding SGs for linking and to mark the end.
4610 cmd->t_tasks_sg_chained = sg_first;
4611 cmd->t_tasks_sg_chained_no = sg_count;
4613 DEBUG_CMD_M("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
4614 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
4615 cmd->t_tasks_sg_chained_no);
4617 for_each_sg(cmd->t_tasks_sg_chained, sg,
4618 cmd->t_tasks_sg_chained_no, i) {
4620 DEBUG_CMD_M("SG[%d]: %p page: %p length: %d offset: %d\n",
4621 i, sg, sg_page(sg), sg->length, sg->offset);
4622 if (sg_is_chain(sg))
4623 DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
4625 DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
4628 EXPORT_SYMBOL(transport_do_task_sg_chain);
4630 static int transport_do_se_mem_map(
4631 struct se_device *dev,
4632 struct se_task *task,
4633 struct list_head *se_mem_list,
4635 struct se_mem *in_se_mem,
4636 struct se_mem **out_se_mem,
4638 u32 *task_offset_in)
4640 u32 task_offset = *task_offset_in;
4643 * se_subsystem_api_t->do_se_mem_map is used when internal allocation
4644 * has been done by the transport plugin.
4646 if (dev->transport->do_se_mem_map) {
4647 ret = dev->transport->do_se_mem_map(task, se_mem_list,
4648 in_mem, in_se_mem, out_se_mem, se_mem_cnt,
4651 task->task_se_cmd->t_tasks_se_num += *se_mem_cnt;
4656 BUG_ON(list_empty(se_mem_list));
4658 * This is the normal path for all normal non BIDI and BIDI-COMMAND
4659 * WRITE payloads.. If we need to do BIDI READ passthrough for
4660 * TCM/pSCSI the first call to transport_do_se_mem_map ->
4661 * transport_init_task_sg() -> transport_map_mem_to_sg() will do the
4662 * allocation for task->task_sg_bidi, and the subsequent call to
4663 * transport_do_se_mem_map() from transport_generic_get_cdb_count()
4665 if (!(task->task_sg_bidi)) {
4667 * Assume default that transport plugin speaks preallocated
4670 ret = transport_init_task_sg(task, in_se_mem, task_offset);
4674 * struct se_task->task_sg now contains the struct scatterlist array.
4676 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
4677 in_se_mem, out_se_mem, se_mem_cnt,
4681 * Handle the se_mem_list -> struct task->task_sg_bidi
4682 * memory map for the extra BIDI READ payload
4684 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
4685 in_se_mem, out_se_mem, se_mem_cnt,
4690 * Break up cmd into chunks transport can handle
4692 static u32 transport_allocate_tasks(
4694 unsigned long long lba,
4696 enum dma_data_direction data_direction,
4697 struct list_head *mem_list,
4700 unsigned char *cdb = NULL;
4701 struct se_task *task;
4702 struct se_mem *se_mem = NULL;
4703 struct se_mem *se_mem_lout = NULL;
4704 struct se_mem *se_mem_bidi = NULL;
4705 struct se_mem *se_mem_bidi_lout = NULL;
4706 struct se_device *dev = cmd->se_dev;
4708 u32 task_offset_in = 0;
4710 u32 se_mem_bidi_cnt = 0;
4715 * While using RAMDISK_DR backstores is the only case where
4716 * mem_list will ever be empty at this point.
4718 if (!(list_empty(mem_list)))
4719 se_mem = list_first_entry(mem_list, struct se_mem, se_list);
4721 * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
4722 * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
4724 if (!list_empty(&cmd->t_mem_bidi_list) &&
4725 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
4726 se_mem_bidi = list_first_entry(&cmd->t_mem_bidi_list,
4727 struct se_mem, se_list);
4730 sector_t limited_sectors;
4732 DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
4733 cmd->se_tfo->get_task_tag(cmd), lba, sectors,
4734 transport_dev_end_lba(dev));
4736 limited_sectors = transport_limit_task_sectors(dev, lba, sectors);
4737 if (!limited_sectors)
4740 task = transport_generic_get_task(cmd, data_direction);
4744 task->task_lba = lba;
4745 task->task_sectors = limited_sectors;
4746 lba += task->task_sectors;
4747 sectors -= task->task_sectors;
4748 task->task_size = (task->task_sectors *
4749 dev->se_sub_dev->se_dev_attrib.block_size);
4751 cdb = dev->transport->get_cdb(task);
4752 /* Should be part of task, can't fail */
4755 memcpy(cdb, cmd->t_task_cdb,
4756 scsi_command_size(cmd->t_task_cdb));
4758 /* Update new cdb with updated lba/sectors */
4759 cmd->transport_split_cdb(task->task_lba,
4760 &task->task_sectors, cdb);
4763 * Perform the SE OBJ plugin and/or Transport plugin specific
4764 * mapping for cmd->t_mem_list. And setup the
4765 * task->task_sg and if necessary task->task_sg_bidi
4767 ret = transport_do_se_mem_map(dev, task, mem_list,
4768 NULL, se_mem, &se_mem_lout, &se_mem_cnt,
4773 se_mem = se_mem_lout;
4775 * Setup the cmd->t_mem_bidi_list -> task->task_sg_bidi
4776 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
4778 * Note that the first call to transport_do_se_mem_map() above will
4779 * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
4780 * -> transport_init_task_sg(), and the second here will do the
4781 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
4783 if (task->task_sg_bidi != NULL) {
4784 ret = transport_do_se_mem_map(dev, task,
4785 &cmd->t_mem_bidi_list, NULL,
4786 se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
4791 se_mem_bidi = se_mem_bidi_lout;
4795 DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
4796 task_cdbs, task->task_sg_num);
4800 atomic_inc(&cmd->t_fe_count);
4801 atomic_inc(&cmd->t_se_count);
4804 DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
4805 cmd->se_tfo->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
4806 ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
4814 transport_map_control_cmd_to_task(struct se_cmd *cmd)
4816 struct se_device *dev = cmd->se_dev;
4818 struct se_task *task;
4821 task = transport_generic_get_task(cmd, cmd->data_direction);
4823 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
4825 cdb = dev->transport->get_cdb(task);
4827 memcpy(cdb, cmd->t_task_cdb,
4828 scsi_command_size(cmd->t_task_cdb));
4830 task->task_size = cmd->data_length;
4832 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
4834 atomic_inc(&cmd->t_fe_count);
4835 atomic_inc(&cmd->t_se_count);
4837 if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
4838 struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
4839 u32 se_mem_cnt = 0, task_offset = 0;
4841 if (!list_empty(&cmd->t_mem_list))
4842 se_mem = list_first_entry(&cmd->t_mem_list,
4843 struct se_mem, se_list);
4845 ret = transport_do_se_mem_map(dev, task,
4846 &cmd->t_mem_list, NULL, se_mem,
4847 &se_mem_lout, &se_mem_cnt, &task_offset);
4849 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
4851 if (dev->transport->map_task_SG)
4852 return dev->transport->map_task_SG(task);
4854 } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
4855 if (dev->transport->map_task_non_SG)
4856 return dev->transport->map_task_non_SG(task);
4858 } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
4859 if (dev->transport->cdb_none)
4860 return dev->transport->cdb_none(task);
4864 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
4868 /* transport_generic_new_cmd(): Called from transport_processing_thread()
4870 * Allocate storage transport resources from a set of values predefined
4871 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4872 * Any non zero return here is treated as an "out of resource' op here.
4875 * Generate struct se_task(s) and/or their payloads for this CDB.
4877 int transport_generic_new_cmd(struct se_cmd *cmd)
4879 struct se_task *task;
4880 struct se_device *dev = cmd->se_dev;
4884 * Determine is the TCM fabric module has already allocated physical
4885 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4886 * to setup beforehand the linked list of physical memory at
4887 * cmd->t_mem_list of struct se_mem->se_page
4889 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
4890 ret = transport_allocate_resources(cmd);
4895 ret = transport_new_cmd_obj(cmd);
4899 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
4900 list_for_each_entry(task, &cmd->t_task_list, t_list) {
4901 if (atomic_read(&task->task_sent))
4903 if (!dev->transport->map_task_SG)
4906 ret = dev->transport->map_task_SG(task);
4911 ret = transport_map_control_cmd_to_task(cmd);
4917 * For WRITEs, let the fabric know its buffer is ready..
4918 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4919 * will be added to the struct se_device execution queue after its WRITE
4920 * data has arrived. (ie: It gets handled by the transport processing
4921 * thread a second time)
4923 if (cmd->data_direction == DMA_TO_DEVICE) {
4924 transport_add_tasks_to_state_queue(cmd);
4925 return transport_generic_write_pending(cmd);
4928 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4929 * to the execution queue.
4931 transport_execute_tasks(cmd);
4934 EXPORT_SYMBOL(transport_generic_new_cmd);
4936 /* transport_generic_process_write():
4940 void transport_generic_process_write(struct se_cmd *cmd)
4944 * Copy SCSI Presented DTL sector(s) from received buffers allocated to
4947 if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
4948 if (!cmd->t_tasks_se_num) {
4949 unsigned char *dst, *buf =
4950 (unsigned char *)cmd->t_task_buf;
4952 dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
4954 printk(KERN_ERR "Unable to allocate memory for"
4955 " WRITE underflow\n");
4956 transport_generic_request_failure(cmd, NULL,
4957 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
4960 memcpy(dst, buf, cmd->cmd_spdtl);
4962 kfree(cmd->t_task_buf);
4963 cmd->t_task_buf = dst;
4965 struct scatterlist *sg =
4966 (struct scatterlist *sg)cmd->t_task_buf;
4967 struct scatterlist *orig_sg;
4969 orig_sg = kzalloc(sizeof(struct scatterlist) *
4970 cmd->t_tasks_se_num,
4973 printk(KERN_ERR "Unable to allocate memory"
4974 " for WRITE underflow\n");
4975 transport_generic_request_failure(cmd, NULL,
4976 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
4980 memcpy(orig_sg, cmd->t_task_buf,
4981 sizeof(struct scatterlist) *
4982 cmd->t_tasks_se_num);
4984 cmd->data_length = cmd->cmd_spdtl;
4986 * FIXME, clear out original struct se_task and state
4989 if (transport_generic_new_cmd(cmd) < 0) {
4990 transport_generic_request_failure(cmd, NULL,
4991 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
4996 transport_memcpy_write_sg(cmd, orig_sg);
5000 transport_execute_tasks(cmd);
5002 EXPORT_SYMBOL(transport_generic_process_write);
5004 static int transport_write_pending_qf(struct se_cmd *cmd)
5006 return cmd->se_tfo->write_pending(cmd);
5009 /* transport_generic_write_pending():
5013 static int transport_generic_write_pending(struct se_cmd *cmd)
5015 unsigned long flags;
5018 spin_lock_irqsave(&cmd->t_state_lock, flags);
5019 cmd->t_state = TRANSPORT_WRITE_PENDING;
5020 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5022 if (cmd->transport_qf_callback) {
5023 ret = cmd->transport_qf_callback(cmd);
5029 cmd->transport_qf_callback = NULL;
5033 * For the TCM control CDBs using a contiguous buffer, do the memcpy
5034 * from the passed Linux/SCSI struct scatterlist located at
5035 * se_cmd->t_task_pt_sgl to the contiguous buffer at
5036 * se_cmd->t_task_buf.
5038 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
5039 sg_copy_to_buffer(cmd->t_task_pt_sgl,
5040 cmd->t_task_pt_sgl_num,
5044 * Clear the se_cmd for WRITE_PENDING status in order to set
5045 * cmd->t_transport_active=0 so that transport_generic_handle_data
5046 * can be called from HW target mode interrupt code. This is safe
5047 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
5048 * because the se_cmd->se_lun pointer is not being cleared.
5050 transport_cmd_check_stop(cmd, 1, 0);
5053 * Call the fabric write_pending function here to let the
5054 * frontend know that WRITE buffers are ready.
5056 ret = cmd->se_tfo->write_pending(cmd);
5062 return PYX_TRANSPORT_WRITE_PENDING;
5065 printk(KERN_INFO "Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
5066 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
5067 transport_handle_queue_full(cmd, cmd->se_dev,
5068 transport_write_pending_qf);
5072 void transport_release_cmd(struct se_cmd *cmd)
5074 BUG_ON(!cmd->se_tfo);
5076 transport_free_se_cmd(cmd);
5077 cmd->se_tfo->release_cmd(cmd);
5079 EXPORT_SYMBOL(transport_release_cmd);
5081 /* transport_generic_free_cmd():
5083 * Called from processing frontend to release storage engine resources
5085 void transport_generic_free_cmd(
5088 int session_reinstatement)
5090 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD))
5091 transport_release_cmd(cmd);
5093 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
5097 printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
5098 " cmd->se_lun\n", cmd,
5099 cmd->se_tfo->get_task_tag(cmd));
5101 transport_lun_remove_cmd(cmd);
5104 if (wait_for_tasks && cmd->transport_wait_for_tasks)
5105 cmd->transport_wait_for_tasks(cmd, 0, 0);
5107 transport_free_dev_tasks(cmd);
5109 transport_generic_remove(cmd, session_reinstatement);
5112 EXPORT_SYMBOL(transport_generic_free_cmd);
5114 static void transport_nop_wait_for_tasks(
5117 int session_reinstatement)
5122 /* transport_lun_wait_for_tasks():
5124 * Called from ConfigFS context to stop the passed struct se_cmd to allow
5125 * an struct se_lun to be successfully shutdown.
5127 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
5129 unsigned long flags;
5132 * If the frontend has already requested this struct se_cmd to
5133 * be stopped, we can safely ignore this struct se_cmd.
5135 spin_lock_irqsave(&cmd->t_state_lock, flags);
5136 if (atomic_read(&cmd->t_transport_stop)) {
5137 atomic_set(&cmd->transport_lun_stop, 0);
5138 DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
5139 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
5140 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5141 transport_cmd_check_stop(cmd, 1, 0);
5144 atomic_set(&cmd->transport_lun_fe_stop, 1);
5145 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5147 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
5149 ret = transport_stop_tasks_for_cmd(cmd);
5151 DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
5152 " %d\n", cmd, cmd->t_task_cdbs, ret);
5154 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
5155 cmd->se_tfo->get_task_tag(cmd));
5156 wait_for_completion(&cmd->transport_lun_stop_comp);
5157 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
5158 cmd->se_tfo->get_task_tag(cmd));
5160 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
5165 /* #define DEBUG_CLEAR_LUN */
5166 #ifdef DEBUG_CLEAR_LUN
5167 #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
5169 #define DEBUG_CLEAR_L(x...)
5172 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
5174 struct se_cmd *cmd = NULL;
5175 unsigned long lun_flags, cmd_flags;
5177 * Do exception processing and return CHECK_CONDITION status to the
5180 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5181 while (!list_empty(&lun->lun_cmd_list)) {
5182 cmd = list_first_entry(&lun->lun_cmd_list,
5183 struct se_cmd, se_lun_node);
5184 list_del(&cmd->se_lun_node);
5186 atomic_set(&cmd->transport_lun_active, 0);
5188 * This will notify iscsi_target_transport.c:
5189 * transport_cmd_check_stop() that a LUN shutdown is in
5190 * progress for the iscsi_cmd_t.
5192 spin_lock(&cmd->t_state_lock);
5193 DEBUG_CLEAR_L("SE_LUN[%d] - Setting cmd->transport"
5194 "_lun_stop for ITT: 0x%08x\n",
5195 cmd->se_lun->unpacked_lun,
5196 cmd->se_tfo->get_task_tag(cmd));
5197 atomic_set(&cmd->transport_lun_stop, 1);
5198 spin_unlock(&cmd->t_state_lock);
5200 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5202 if (!(cmd->se_lun)) {
5203 printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
5204 cmd->se_tfo->get_task_tag(cmd),
5205 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
5209 * If the Storage engine still owns the iscsi_cmd_t, determine
5210 * and/or stop its context.
5212 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
5213 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
5214 cmd->se_tfo->get_task_tag(cmd));
5216 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
5217 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5221 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
5222 "_wait_for_tasks(): SUCCESS\n",
5223 cmd->se_lun->unpacked_lun,
5224 cmd->se_tfo->get_task_tag(cmd));
5226 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
5227 if (!(atomic_read(&cmd->transport_dev_active))) {
5228 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
5231 atomic_set(&cmd->transport_dev_active, 0);
5232 transport_all_task_dev_remove_state(cmd);
5233 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
5235 transport_free_dev_tasks(cmd);
5237 * The Storage engine stopped this struct se_cmd before it was
5238 * send to the fabric frontend for delivery back to the
5239 * Initiator Node. Return this SCSI CDB back with an
5240 * CHECK_CONDITION status.
5243 transport_send_check_condition_and_sense(cmd,
5244 TCM_NON_EXISTENT_LUN, 0);
5246 * If the fabric frontend is waiting for this iscsi_cmd_t to
5247 * be released, notify the waiting thread now that LU has
5248 * finished accessing it.
5250 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
5251 if (atomic_read(&cmd->transport_lun_fe_stop)) {
5252 DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
5253 " struct se_cmd: %p ITT: 0x%08x\n",
5255 cmd, cmd->se_tfo->get_task_tag(cmd));
5257 spin_unlock_irqrestore(&cmd->t_state_lock,
5259 transport_cmd_check_stop(cmd, 1, 0);
5260 complete(&cmd->transport_lun_fe_stop_comp);
5261 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5264 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
5265 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
5267 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
5268 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5270 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5273 static int transport_clear_lun_thread(void *p)
5275 struct se_lun *lun = (struct se_lun *)p;
5277 __transport_clear_lun_from_sessions(lun);
5278 complete(&lun->lun_shutdown_comp);
5283 int transport_clear_lun_from_sessions(struct se_lun *lun)
5285 struct task_struct *kt;
5287 kt = kthread_run(transport_clear_lun_thread, lun,
5288 "tcm_cl_%u", lun->unpacked_lun);
5290 printk(KERN_ERR "Unable to start clear_lun thread\n");
5293 wait_for_completion(&lun->lun_shutdown_comp);
5298 /* transport_generic_wait_for_tasks():
5300 * Called from frontend or passthrough context to wait for storage engine
5301 * to pause and/or release frontend generated struct se_cmd.
5303 static void transport_generic_wait_for_tasks(
5306 int session_reinstatement)
5308 unsigned long flags;
5310 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
5313 spin_lock_irqsave(&cmd->t_state_lock, flags);
5315 * If we are already stopped due to an external event (ie: LUN shutdown)
5316 * sleep until the connection can have the passed struct se_cmd back.
5317 * The cmd->transport_lun_stopped_sem will be upped by
5318 * transport_clear_lun_from_sessions() once the ConfigFS context caller
5319 * has completed its operation on the struct se_cmd.
5321 if (atomic_read(&cmd->transport_lun_stop)) {
5323 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
5324 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
5325 "_stop_comp); for ITT: 0x%08x\n",
5326 cmd->se_tfo->get_task_tag(cmd));
5328 * There is a special case for WRITES where a FE exception +
5329 * LUN shutdown means ConfigFS context is still sleeping on
5330 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
5331 * We go ahead and up transport_lun_stop_comp just to be sure
5334 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5335 complete(&cmd->transport_lun_stop_comp);
5336 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
5337 spin_lock_irqsave(&cmd->t_state_lock, flags);
5339 transport_all_task_dev_remove_state(cmd);
5341 * At this point, the frontend who was the originator of this
5342 * struct se_cmd, now owns the structure and can be released through
5343 * normal means below.
5345 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
5346 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
5347 "stop_comp); for ITT: 0x%08x\n",
5348 cmd->se_tfo->get_task_tag(cmd));
5350 atomic_set(&cmd->transport_lun_stop, 0);
5352 if (!atomic_read(&cmd->t_transport_active) ||
5353 atomic_read(&cmd->t_transport_aborted))
5356 atomic_set(&cmd->t_transport_stop, 1);
5358 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
5359 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
5360 " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
5361 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
5362 cmd->deferred_t_state);
5364 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5366 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
5368 wait_for_completion(&cmd->t_transport_stop_comp);
5370 spin_lock_irqsave(&cmd->t_state_lock, flags);
5371 atomic_set(&cmd->t_transport_active, 0);
5372 atomic_set(&cmd->t_transport_stop, 0);
5374 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
5375 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
5376 cmd->se_tfo->get_task_tag(cmd));
5378 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5382 transport_generic_free_cmd(cmd, 0, session_reinstatement);
5385 static int transport_get_sense_codes(
5390 *asc = cmd->scsi_asc;
5391 *ascq = cmd->scsi_ascq;
5396 static int transport_set_sense_codes(
5401 cmd->scsi_asc = asc;
5402 cmd->scsi_ascq = ascq;
5407 int transport_send_check_condition_and_sense(
5412 unsigned char *buffer = cmd->sense_buffer;
5413 unsigned long flags;
5415 u8 asc = 0, ascq = 0;
5417 spin_lock_irqsave(&cmd->t_state_lock, flags);
5418 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
5419 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5422 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
5423 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5425 if (!reason && from_transport)
5428 if (!from_transport)
5429 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
5431 * Data Segment and SenseLength of the fabric response PDU.
5433 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
5434 * from include/scsi/scsi_cmnd.h
5436 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
5437 TRANSPORT_SENSE_BUFFER);
5439 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
5440 * SENSE KEY values from include/scsi/scsi.h
5443 case TCM_NON_EXISTENT_LUN:
5444 case TCM_UNSUPPORTED_SCSI_OPCODE:
5445 case TCM_SECTOR_COUNT_TOO_MANY:
5447 buffer[offset] = 0x70;
5448 /* ILLEGAL REQUEST */
5449 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5450 /* INVALID COMMAND OPERATION CODE */
5451 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
5453 case TCM_UNKNOWN_MODE_PAGE:
5455 buffer[offset] = 0x70;
5456 /* ILLEGAL REQUEST */
5457 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5458 /* INVALID FIELD IN CDB */
5459 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5461 case TCM_CHECK_CONDITION_ABORT_CMD:
5463 buffer[offset] = 0x70;
5464 /* ABORTED COMMAND */
5465 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5466 /* BUS DEVICE RESET FUNCTION OCCURRED */
5467 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
5468 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
5470 case TCM_INCORRECT_AMOUNT_OF_DATA:
5472 buffer[offset] = 0x70;
5473 /* ABORTED COMMAND */
5474 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5476 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
5477 /* NOT ENOUGH UNSOLICITED DATA */
5478 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
5480 case TCM_INVALID_CDB_FIELD:
5482 buffer[offset] = 0x70;
5483 /* ABORTED COMMAND */
5484 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5485 /* INVALID FIELD IN CDB */
5486 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5488 case TCM_INVALID_PARAMETER_LIST:
5490 buffer[offset] = 0x70;
5491 /* ABORTED COMMAND */
5492 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5493 /* INVALID FIELD IN PARAMETER LIST */
5494 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
5496 case TCM_UNEXPECTED_UNSOLICITED_DATA:
5498 buffer[offset] = 0x70;
5499 /* ABORTED COMMAND */
5500 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5502 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
5503 /* UNEXPECTED_UNSOLICITED_DATA */
5504 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
5506 case TCM_SERVICE_CRC_ERROR:
5508 buffer[offset] = 0x70;
5509 /* ABORTED COMMAND */
5510 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5511 /* PROTOCOL SERVICE CRC ERROR */
5512 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
5514 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
5516 case TCM_SNACK_REJECTED:
5518 buffer[offset] = 0x70;
5519 /* ABORTED COMMAND */
5520 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5522 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
5523 /* FAILED RETRANSMISSION REQUEST */
5524 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
5526 case TCM_WRITE_PROTECTED:
5528 buffer[offset] = 0x70;
5530 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
5531 /* WRITE PROTECTED */
5532 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
5534 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
5536 buffer[offset] = 0x70;
5537 /* UNIT ATTENTION */
5538 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
5539 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
5540 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5541 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5543 case TCM_CHECK_CONDITION_NOT_READY:
5545 buffer[offset] = 0x70;
5547 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
5548 transport_get_sense_codes(cmd, &asc, &ascq);
5549 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5550 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5552 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
5555 buffer[offset] = 0x70;
5556 /* ILLEGAL REQUEST */
5557 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5558 /* LOGICAL UNIT COMMUNICATION FAILURE */
5559 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
5563 * This code uses linux/include/scsi/scsi.h SAM status codes!
5565 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
5567 * Automatically padded, this value is encoded in the fabric's
5568 * data_length response PDU containing the SCSI defined sense data.
5570 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
5573 return cmd->se_tfo->queue_status(cmd);
5575 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
5577 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
5581 if (atomic_read(&cmd->t_transport_aborted) != 0) {
5582 if (!(send_status) ||
5583 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
5586 printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
5587 " status for CDB: 0x%02x ITT: 0x%08x\n",
5589 cmd->se_tfo->get_task_tag(cmd));
5591 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
5592 cmd->se_tfo->queue_status(cmd);
5597 EXPORT_SYMBOL(transport_check_aborted_status);
5599 void transport_send_task_abort(struct se_cmd *cmd)
5602 * If there are still expected incoming fabric WRITEs, we wait
5603 * until until they have completed before sending a TASK_ABORTED
5604 * response. This response with TASK_ABORTED status will be
5605 * queued back to fabric module by transport_check_aborted_status().
5607 if (cmd->data_direction == DMA_TO_DEVICE) {
5608 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
5609 atomic_inc(&cmd->t_transport_aborted);
5610 smp_mb__after_atomic_inc();
5611 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5612 transport_new_cmd_failure(cmd);
5616 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5618 printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
5619 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
5620 cmd->se_tfo->get_task_tag(cmd));
5622 cmd->se_tfo->queue_status(cmd);
5625 /* transport_generic_do_tmr():
5629 int transport_generic_do_tmr(struct se_cmd *cmd)
5631 struct se_device *dev = cmd->se_dev;
5632 struct se_tmr_req *tmr = cmd->se_tmr_req;
5635 switch (tmr->function) {
5636 case TMR_ABORT_TASK:
5637 tmr->response = TMR_FUNCTION_REJECTED;
5639 case TMR_ABORT_TASK_SET:
5641 case TMR_CLEAR_TASK_SET:
5642 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
5645 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
5646 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
5647 TMR_FUNCTION_REJECTED;
5649 case TMR_TARGET_WARM_RESET:
5650 tmr->response = TMR_FUNCTION_REJECTED;
5652 case TMR_TARGET_COLD_RESET:
5653 tmr->response = TMR_FUNCTION_REJECTED;
5656 printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
5658 tmr->response = TMR_FUNCTION_REJECTED;
5662 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
5663 cmd->se_tfo->queue_tm_rsp(cmd);
5665 transport_cmd_check_stop(cmd, 2, 0);
5670 * Called with spin_lock_irq(&dev->execute_task_lock); held
5673 static struct se_task *
5674 transport_get_task_from_state_list(struct se_device *dev)
5676 struct se_task *task;
5678 if (list_empty(&dev->state_task_list))
5681 list_for_each_entry(task, &dev->state_task_list, t_state_list)
5684 list_del(&task->t_state_list);
5685 atomic_set(&task->task_state_active, 0);
5690 static void transport_processing_shutdown(struct se_device *dev)
5693 struct se_task *task;
5694 unsigned long flags;
5696 * Empty the struct se_device's struct se_task state list.
5698 spin_lock_irqsave(&dev->execute_task_lock, flags);
5699 while ((task = transport_get_task_from_state_list(dev))) {
5700 if (!task->task_se_cmd) {
5701 printk(KERN_ERR "task->task_se_cmd is NULL!\n");
5704 cmd = task->task_se_cmd;
5706 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5708 spin_lock_irqsave(&cmd->t_state_lock, flags);
5710 DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
5711 " i_state/def_i_state: %d/%d, t_state/def_t_state:"
5712 " %d/%d cdb: 0x%02x\n", cmd, task,
5713 cmd->se_tfo->get_task_tag(cmd), cmd->cmd_sn,
5714 cmd->se_tfo->get_cmd_state(cmd), cmd->deferred_i_state,
5715 cmd->t_state, cmd->deferred_t_state,
5716 cmd->t_task_cdb[0]);
5717 DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
5718 " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5719 " t_transport_stop: %d t_transport_sent: %d\n",
5720 cmd->se_tfo->get_task_tag(cmd),
5722 atomic_read(&cmd->t_task_cdbs_left),
5723 atomic_read(&cmd->t_task_cdbs_sent),
5724 atomic_read(&cmd->t_transport_active),
5725 atomic_read(&cmd->t_transport_stop),
5726 atomic_read(&cmd->t_transport_sent));
5728 if (atomic_read(&task->task_active)) {
5729 atomic_set(&task->task_stop, 1);
5730 spin_unlock_irqrestore(
5731 &cmd->t_state_lock, flags);
5733 DEBUG_DO("Waiting for task: %p to shutdown for dev:"
5734 " %p\n", task, dev);
5735 wait_for_completion(&task->task_stop_comp);
5736 DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
5739 spin_lock_irqsave(&cmd->t_state_lock, flags);
5740 atomic_dec(&cmd->t_task_cdbs_left);
5742 atomic_set(&task->task_active, 0);
5743 atomic_set(&task->task_stop, 0);
5745 if (atomic_read(&task->task_execute_queue) != 0)
5746 transport_remove_task_from_execute_queue(task, dev);
5748 __transport_stop_task_timer(task, &flags);
5750 if (!(atomic_dec_and_test(&cmd->t_task_cdbs_ex_left))) {
5751 spin_unlock_irqrestore(
5752 &cmd->t_state_lock, flags);
5754 DEBUG_DO("Skipping task: %p, dev: %p for"
5755 " t_task_cdbs_ex_left: %d\n", task, dev,
5756 atomic_read(&cmd->t_task_cdbs_ex_left));
5758 spin_lock_irqsave(&dev->execute_task_lock, flags);
5762 if (atomic_read(&cmd->t_transport_active)) {
5763 DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
5764 " %p\n", task, dev);
5766 if (atomic_read(&cmd->t_fe_count)) {
5767 spin_unlock_irqrestore(
5768 &cmd->t_state_lock, flags);
5769 transport_send_check_condition_and_sense(
5770 cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
5772 transport_remove_cmd_from_queue(cmd,
5773 &cmd->se_dev->dev_queue_obj);
5775 transport_lun_remove_cmd(cmd);
5776 transport_cmd_check_stop(cmd, 1, 0);
5778 spin_unlock_irqrestore(
5779 &cmd->t_state_lock, flags);
5781 transport_remove_cmd_from_queue(cmd,
5782 &cmd->se_dev->dev_queue_obj);
5784 transport_lun_remove_cmd(cmd);
5786 if (transport_cmd_check_stop(cmd, 1, 0))
5787 transport_generic_remove(cmd, 0);
5790 spin_lock_irqsave(&dev->execute_task_lock, flags);
5793 DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
5796 if (atomic_read(&cmd->t_fe_count)) {
5797 spin_unlock_irqrestore(
5798 &cmd->t_state_lock, flags);
5799 transport_send_check_condition_and_sense(cmd,
5800 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
5801 transport_remove_cmd_from_queue(cmd,
5802 &cmd->se_dev->dev_queue_obj);
5804 transport_lun_remove_cmd(cmd);
5805 transport_cmd_check_stop(cmd, 1, 0);
5807 spin_unlock_irqrestore(
5808 &cmd->t_state_lock, flags);
5810 transport_remove_cmd_from_queue(cmd,
5811 &cmd->se_dev->dev_queue_obj);
5812 transport_lun_remove_cmd(cmd);
5814 if (transport_cmd_check_stop(cmd, 1, 0))
5815 transport_generic_remove(cmd, 0);
5818 spin_lock_irqsave(&dev->execute_task_lock, flags);
5820 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5822 * Empty the struct se_device's struct se_cmd list.
5824 while ((cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj))) {
5826 DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
5829 if (atomic_read(&cmd->t_fe_count)) {
5830 transport_send_check_condition_and_sense(cmd,
5831 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
5833 transport_lun_remove_cmd(cmd);
5834 transport_cmd_check_stop(cmd, 1, 0);
5836 transport_lun_remove_cmd(cmd);
5837 if (transport_cmd_check_stop(cmd, 1, 0))
5838 transport_generic_remove(cmd, 0);
5843 /* transport_processing_thread():
5847 static int transport_processing_thread(void *param)
5851 struct se_device *dev = (struct se_device *) param;
5853 set_user_nice(current, -20);
5855 while (!kthread_should_stop()) {
5856 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
5857 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
5858 kthread_should_stop());
5862 spin_lock_irq(&dev->dev_status_lock);
5863 if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
5864 spin_unlock_irq(&dev->dev_status_lock);
5865 transport_processing_shutdown(dev);
5868 spin_unlock_irq(&dev->dev_status_lock);
5871 __transport_execute_tasks(dev);
5873 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
5877 switch (cmd->t_state) {
5878 case TRANSPORT_NEW_CMD_MAP:
5879 if (!(cmd->se_tfo->new_cmd_map)) {
5880 printk(KERN_ERR "cmd->se_tfo->new_cmd_map is"
5881 " NULL for TRANSPORT_NEW_CMD_MAP\n");
5884 ret = cmd->se_tfo->new_cmd_map(cmd);
5886 cmd->transport_error_status = ret;
5887 transport_generic_request_failure(cmd, NULL,
5888 0, (cmd->data_direction !=
5893 case TRANSPORT_NEW_CMD:
5894 ret = transport_generic_new_cmd(cmd);
5898 cmd->transport_error_status = ret;
5899 transport_generic_request_failure(cmd, NULL,
5900 0, (cmd->data_direction !=
5904 case TRANSPORT_PROCESS_WRITE:
5905 transport_generic_process_write(cmd);
5907 case TRANSPORT_COMPLETE_OK:
5908 transport_stop_all_task_timers(cmd);
5909 transport_generic_complete_ok(cmd);
5911 case TRANSPORT_REMOVE:
5912 transport_generic_remove(cmd, 0);
5914 case TRANSPORT_FREE_CMD_INTR:
5915 transport_generic_free_cmd(cmd, 0, 0);
5917 case TRANSPORT_PROCESS_TMR:
5918 transport_generic_do_tmr(cmd);
5920 case TRANSPORT_COMPLETE_FAILURE:
5921 transport_generic_request_failure(cmd, NULL, 1, 1);
5923 case TRANSPORT_COMPLETE_TIMEOUT:
5924 transport_stop_all_task_timers(cmd);
5925 transport_generic_request_timeout(cmd);
5927 case TRANSPORT_COMPLETE_QF_WP:
5928 transport_generic_write_pending(cmd);
5931 printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
5932 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
5933 " %u\n", cmd->t_state, cmd->deferred_t_state,
5934 cmd->se_tfo->get_task_tag(cmd),
5935 cmd->se_tfo->get_cmd_state(cmd),
5936 cmd->se_lun->unpacked_lun);
5944 transport_release_all_cmds(dev);
5945 dev->process_thread = NULL;