FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / target / target_core_transport.c
blobbf6aa8a9f1d8a3a8a968ce7846feb4be47ce7d2c
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>
38 #include <linux/in.h>
39 #include <linux/cdrom.h>
40 #include <asm/unaligned.h>
41 #include <net/sock.h>
42 #include <net/tcp.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/libsas.h> /* For TASK_ATTR_* */
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)
64 #else
65 #define DEBUG_CDB_H(x...)
66 #endif
68 /* #define DEBUG_CMD_MAP */
69 #ifdef DEBUG_CMD_MAP
70 #define DEBUG_CMD_M(x...) printk(KERN_INFO x)
71 #else
72 #define DEBUG_CMD_M(x...)
73 #endif
75 /* #define DEBUG_MEM_ALLOC */
76 #ifdef DEBUG_MEM_ALLOC
77 #define DEBUG_MEM(x...) printk(KERN_INFO x)
78 #else
79 #define DEBUG_MEM(x...)
80 #endif
82 /* #define DEBUG_MEM2_ALLOC */
83 #ifdef DEBUG_MEM2_ALLOC
84 #define DEBUG_MEM2(x...) printk(KERN_INFO x)
85 #else
86 #define DEBUG_MEM2(x...)
87 #endif
89 /* #define DEBUG_SG_CALC */
90 #ifdef DEBUG_SG_CALC
91 #define DEBUG_SC(x...) printk(KERN_INFO x)
92 #else
93 #define DEBUG_SC(x...)
94 #endif
96 /* #define DEBUG_SE_OBJ */
97 #ifdef DEBUG_SE_OBJ
98 #define DEBUG_SO(x...) printk(KERN_INFO x)
99 #else
100 #define DEBUG_SO(x...)
101 #endif
103 /* #define DEBUG_CMD_VOL */
104 #ifdef DEBUG_CMD_VOL
105 #define DEBUG_VOL(x...) printk(KERN_INFO x)
106 #else
107 #define DEBUG_VOL(x...)
108 #endif
110 /* #define DEBUG_CMD_STOP */
111 #ifdef DEBUG_CMD_STOP
112 #define DEBUG_CS(x...) printk(KERN_INFO x)
113 #else
114 #define DEBUG_CS(x...)
115 #endif
117 /* #define DEBUG_PASSTHROUGH */
118 #ifdef DEBUG_PASSTHROUGH
119 #define DEBUG_PT(x...) printk(KERN_INFO x)
120 #else
121 #define DEBUG_PT(x...)
122 #endif
124 /* #define DEBUG_TASK_STOP */
125 #ifdef DEBUG_TASK_STOP
126 #define DEBUG_TS(x...) printk(KERN_INFO x)
127 #else
128 #define DEBUG_TS(x...)
129 #endif
131 /* #define DEBUG_TRANSPORT_STOP */
132 #ifdef DEBUG_TRANSPORT_STOP
133 #define DEBUG_TRANSPORT_S(x...) printk(KERN_INFO x)
134 #else
135 #define DEBUG_TRANSPORT_S(x...)
136 #endif
138 /* #define DEBUG_TASK_FAILURE */
139 #ifdef DEBUG_TASK_FAILURE
140 #define DEBUG_TF(x...) printk(KERN_INFO x)
141 #else
142 #define DEBUG_TF(x...)
143 #endif
145 /* #define DEBUG_DEV_OFFLINE */
146 #ifdef DEBUG_DEV_OFFLINE
147 #define DEBUG_DO(x...) printk(KERN_INFO x)
148 #else
149 #define DEBUG_DO(x...)
150 #endif
152 /* #define DEBUG_TASK_STATE */
153 #ifdef DEBUG_TASK_STATE
154 #define DEBUG_TSTATE(x...) printk(KERN_INFO x)
155 #else
156 #define DEBUG_TSTATE(x...)
157 #endif
159 /* #define DEBUG_STATUS_THR */
160 #ifdef DEBUG_STATUS_THR
161 #define DEBUG_ST(x...) printk(KERN_INFO x)
162 #else
163 #define DEBUG_ST(x...)
164 #endif
166 /* #define DEBUG_TASK_TIMEOUT */
167 #ifdef DEBUG_TASK_TIMEOUT
168 #define DEBUG_TT(x...) printk(KERN_INFO x)
169 #else
170 #define DEBUG_TT(x...)
171 #endif
173 /* #define DEBUG_GENERIC_REQUEST_FAILURE */
174 #ifdef DEBUG_GENERIC_REQUEST_FAILURE
175 #define DEBUG_GRF(x...) printk(KERN_INFO x)
176 #else
177 #define DEBUG_GRF(x...)
178 #endif
180 /* #define DEBUG_SAM_TASK_ATTRS */
181 #ifdef DEBUG_SAM_TASK_ATTRS
182 #define DEBUG_STA(x...) printk(KERN_INFO x)
183 #else
184 #define DEBUG_STA(x...)
185 #endif
187 struct se_global *se_global;
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 *);
205 static int __transport_execute_tasks(struct se_device *dev);
206 static void transport_complete_task_attr(struct se_cmd *cmd);
207 static void transport_direct_request_timeout(struct se_cmd *cmd);
208 static void transport_free_dev_tasks(struct se_cmd *cmd);
209 static u32 transport_generic_get_cdb_count(struct se_cmd *cmd,
210 unsigned long long starting_lba, u32 sectors,
211 enum dma_data_direction data_direction,
212 struct list_head *mem_list, int set_counts);
213 static int transport_generic_get_mem(struct se_cmd *cmd, u32 length,
214 u32 dma_size);
215 static int transport_generic_remove(struct se_cmd *cmd,
216 int release_to_pool, int session_reinstatement);
217 static int transport_get_sectors(struct se_cmd *cmd);
218 static struct list_head *transport_init_se_mem_list(void);
219 static int transport_map_sg_to_mem(struct se_cmd *cmd,
220 struct list_head *se_mem_list, void *in_mem,
221 u32 *se_mem_cnt);
222 static void transport_memcpy_se_mem_read_contig(struct se_cmd *cmd,
223 unsigned char *dst, struct list_head *se_mem_list);
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_global(void)
232 struct se_global *global;
234 global = kzalloc(sizeof(struct se_global), GFP_KERNEL);
235 if (!(global)) {
236 printk(KERN_ERR "Unable to allocate memory for struct se_global\n");
237 return -1;
240 INIT_LIST_HEAD(&global->g_lu_gps_list);
241 INIT_LIST_HEAD(&global->g_se_tpg_list);
242 INIT_LIST_HEAD(&global->g_hba_list);
243 INIT_LIST_HEAD(&global->g_se_dev_list);
244 spin_lock_init(&global->g_device_lock);
245 spin_lock_init(&global->hba_lock);
246 spin_lock_init(&global->se_tpg_lock);
247 spin_lock_init(&global->lu_gps_lock);
248 spin_lock_init(&global->plugin_class_lock);
250 se_cmd_cache = kmem_cache_create("se_cmd_cache",
251 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
252 if (!(se_cmd_cache)) {
253 printk(KERN_ERR "kmem_cache_create for struct se_cmd failed\n");
254 goto out;
256 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
257 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
258 0, NULL);
259 if (!(se_tmr_req_cache)) {
260 printk(KERN_ERR "kmem_cache_create() for struct se_tmr_req"
261 " failed\n");
262 goto out;
264 se_sess_cache = kmem_cache_create("se_sess_cache",
265 sizeof(struct se_session), __alignof__(struct se_session),
266 0, NULL);
267 if (!(se_sess_cache)) {
268 printk(KERN_ERR "kmem_cache_create() for struct se_session"
269 " failed\n");
270 goto out;
272 se_ua_cache = kmem_cache_create("se_ua_cache",
273 sizeof(struct se_ua), __alignof__(struct se_ua),
274 0, NULL);
275 if (!(se_ua_cache)) {
276 printk(KERN_ERR "kmem_cache_create() for struct se_ua failed\n");
277 goto out;
279 se_mem_cache = kmem_cache_create("se_mem_cache",
280 sizeof(struct se_mem), __alignof__(struct se_mem), 0, NULL);
281 if (!(se_mem_cache)) {
282 printk(KERN_ERR "kmem_cache_create() for struct se_mem failed\n");
283 goto out;
285 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
286 sizeof(struct t10_pr_registration),
287 __alignof__(struct t10_pr_registration), 0, NULL);
288 if (!(t10_pr_reg_cache)) {
289 printk(KERN_ERR "kmem_cache_create() for struct t10_pr_registration"
290 " failed\n");
291 goto out;
293 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
294 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
295 0, NULL);
296 if (!(t10_alua_lu_gp_cache)) {
297 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_cache"
298 " failed\n");
299 goto out;
301 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
302 sizeof(struct t10_alua_lu_gp_member),
303 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
304 if (!(t10_alua_lu_gp_mem_cache)) {
305 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_mem_"
306 "cache failed\n");
307 goto out;
309 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
310 sizeof(struct t10_alua_tg_pt_gp),
311 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
312 if (!(t10_alua_tg_pt_gp_cache)) {
313 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
314 "cache failed\n");
315 goto out;
317 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
318 "t10_alua_tg_pt_gp_mem_cache",
319 sizeof(struct t10_alua_tg_pt_gp_member),
320 __alignof__(struct t10_alua_tg_pt_gp_member),
321 0, NULL);
322 if (!(t10_alua_tg_pt_gp_mem_cache)) {
323 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
324 "mem_t failed\n");
325 goto out;
328 se_global = global;
330 return 0;
331 out:
332 if (se_cmd_cache)
333 kmem_cache_destroy(se_cmd_cache);
334 if (se_tmr_req_cache)
335 kmem_cache_destroy(se_tmr_req_cache);
336 if (se_sess_cache)
337 kmem_cache_destroy(se_sess_cache);
338 if (se_ua_cache)
339 kmem_cache_destroy(se_ua_cache);
340 if (se_mem_cache)
341 kmem_cache_destroy(se_mem_cache);
342 if (t10_pr_reg_cache)
343 kmem_cache_destroy(t10_pr_reg_cache);
344 if (t10_alua_lu_gp_cache)
345 kmem_cache_destroy(t10_alua_lu_gp_cache);
346 if (t10_alua_lu_gp_mem_cache)
347 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
348 if (t10_alua_tg_pt_gp_cache)
349 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
350 if (t10_alua_tg_pt_gp_mem_cache)
351 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
352 kfree(global);
353 return -1;
356 void release_se_global(void)
358 struct se_global *global;
360 global = se_global;
361 if (!(global))
362 return;
364 kmem_cache_destroy(se_cmd_cache);
365 kmem_cache_destroy(se_tmr_req_cache);
366 kmem_cache_destroy(se_sess_cache);
367 kmem_cache_destroy(se_ua_cache);
368 kmem_cache_destroy(se_mem_cache);
369 kmem_cache_destroy(t10_pr_reg_cache);
370 kmem_cache_destroy(t10_alua_lu_gp_cache);
371 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
372 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
373 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
374 kfree(global);
376 se_global = NULL;
379 /* SCSI statistics table index */
380 static struct scsi_index_table scsi_index_table;
383 * Initialize the index table for allocating unique row indexes to various mib
384 * tables.
386 void init_scsi_index_table(void)
388 memset(&scsi_index_table, 0, sizeof(struct scsi_index_table));
389 spin_lock_init(&scsi_index_table.lock);
393 * Allocate a new row index for the entry type specified
395 u32 scsi_get_new_index(scsi_index_t type)
397 u32 new_index;
399 if ((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)) {
400 printk(KERN_ERR "Invalid index type %d\n", type);
401 return -EINVAL;
404 spin_lock(&scsi_index_table.lock);
405 new_index = ++scsi_index_table.scsi_mib_index[type];
406 if (new_index == 0)
407 new_index = ++scsi_index_table.scsi_mib_index[type];
408 spin_unlock(&scsi_index_table.lock);
410 return new_index;
413 void transport_init_queue_obj(struct se_queue_obj *qobj)
415 atomic_set(&qobj->queue_cnt, 0);
416 INIT_LIST_HEAD(&qobj->qobj_list);
417 init_waitqueue_head(&qobj->thread_wq);
418 spin_lock_init(&qobj->cmd_queue_lock);
420 EXPORT_SYMBOL(transport_init_queue_obj);
422 static int transport_subsystem_reqmods(void)
424 int ret;
426 ret = request_module("target_core_iblock");
427 if (ret != 0)
428 printk(KERN_ERR "Unable to load target_core_iblock\n");
430 ret = request_module("target_core_file");
431 if (ret != 0)
432 printk(KERN_ERR "Unable to load target_core_file\n");
434 ret = request_module("target_core_pscsi");
435 if (ret != 0)
436 printk(KERN_ERR "Unable to load target_core_pscsi\n");
438 ret = request_module("target_core_stgt");
439 if (ret != 0)
440 printk(KERN_ERR "Unable to load target_core_stgt\n");
442 return 0;
445 int transport_subsystem_check_init(void)
447 if (se_global->g_sub_api_initialized)
448 return 0;
450 * Request the loading of known TCM subsystem plugins..
452 if (transport_subsystem_reqmods() < 0)
453 return -1;
455 se_global->g_sub_api_initialized = 1;
456 return 0;
459 struct se_session *transport_init_session(void)
461 struct se_session *se_sess;
463 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
464 if (!(se_sess)) {
465 printk(KERN_ERR "Unable to allocate struct se_session from"
466 " se_sess_cache\n");
467 return ERR_PTR(-ENOMEM);
469 INIT_LIST_HEAD(&se_sess->sess_list);
470 INIT_LIST_HEAD(&se_sess->sess_acl_list);
472 return se_sess;
474 EXPORT_SYMBOL(transport_init_session);
477 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
479 void __transport_register_session(
480 struct se_portal_group *se_tpg,
481 struct se_node_acl *se_nacl,
482 struct se_session *se_sess,
483 void *fabric_sess_ptr)
485 unsigned char buf[PR_REG_ISID_LEN];
487 se_sess->se_tpg = se_tpg;
488 se_sess->fabric_sess_ptr = fabric_sess_ptr;
490 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
492 * Only set for struct se_session's that will actually be moving I/O.
493 * eg: *NOT* discovery sessions.
495 if (se_nacl) {
497 * If the fabric module supports an ISID based TransportID,
498 * save this value in binary from the fabric I_T Nexus now.
500 if (TPG_TFO(se_tpg)->sess_get_initiator_sid != NULL) {
501 memset(&buf[0], 0, PR_REG_ISID_LEN);
502 TPG_TFO(se_tpg)->sess_get_initiator_sid(se_sess,
503 &buf[0], PR_REG_ISID_LEN);
504 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
506 spin_lock_irq(&se_nacl->nacl_sess_lock);
508 * The se_nacl->nacl_sess pointer will be set to the
509 * last active I_T Nexus for each struct se_node_acl.
511 se_nacl->nacl_sess = se_sess;
513 list_add_tail(&se_sess->sess_acl_list,
514 &se_nacl->acl_sess_list);
515 spin_unlock_irq(&se_nacl->nacl_sess_lock);
517 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
519 printk(KERN_INFO "TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
520 TPG_TFO(se_tpg)->get_fabric_name(), se_sess->fabric_sess_ptr);
522 EXPORT_SYMBOL(__transport_register_session);
524 void transport_register_session(
525 struct se_portal_group *se_tpg,
526 struct se_node_acl *se_nacl,
527 struct se_session *se_sess,
528 void *fabric_sess_ptr)
530 spin_lock_bh(&se_tpg->session_lock);
531 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
532 spin_unlock_bh(&se_tpg->session_lock);
534 EXPORT_SYMBOL(transport_register_session);
536 void transport_deregister_session_configfs(struct se_session *se_sess)
538 struct se_node_acl *se_nacl;
541 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
543 se_nacl = se_sess->se_node_acl;
544 if ((se_nacl)) {
545 spin_lock_irq(&se_nacl->nacl_sess_lock);
546 list_del(&se_sess->sess_acl_list);
548 * If the session list is empty, then clear the pointer.
549 * Otherwise, set the struct se_session pointer from the tail
550 * element of the per struct se_node_acl active session list.
552 if (list_empty(&se_nacl->acl_sess_list))
553 se_nacl->nacl_sess = NULL;
554 else {
555 se_nacl->nacl_sess = container_of(
556 se_nacl->acl_sess_list.prev,
557 struct se_session, sess_acl_list);
559 spin_unlock_irq(&se_nacl->nacl_sess_lock);
562 EXPORT_SYMBOL(transport_deregister_session_configfs);
564 void transport_free_session(struct se_session *se_sess)
566 kmem_cache_free(se_sess_cache, se_sess);
568 EXPORT_SYMBOL(transport_free_session);
570 void transport_deregister_session(struct se_session *se_sess)
572 struct se_portal_group *se_tpg = se_sess->se_tpg;
573 struct se_node_acl *se_nacl;
575 if (!(se_tpg)) {
576 transport_free_session(se_sess);
577 return;
580 spin_lock_bh(&se_tpg->session_lock);
581 list_del(&se_sess->sess_list);
582 se_sess->se_tpg = NULL;
583 se_sess->fabric_sess_ptr = NULL;
584 spin_unlock_bh(&se_tpg->session_lock);
587 * Determine if we need to do extra work for this initiator node's
588 * struct se_node_acl if it had been previously dynamically generated.
590 se_nacl = se_sess->se_node_acl;
591 if ((se_nacl)) {
592 spin_lock_bh(&se_tpg->acl_node_lock);
593 if (se_nacl->dynamic_node_acl) {
594 if (!(TPG_TFO(se_tpg)->tpg_check_demo_mode_cache(
595 se_tpg))) {
596 list_del(&se_nacl->acl_list);
597 se_tpg->num_node_acls--;
598 spin_unlock_bh(&se_tpg->acl_node_lock);
600 core_tpg_wait_for_nacl_pr_ref(se_nacl);
601 core_free_device_list_for_node(se_nacl, se_tpg);
602 TPG_TFO(se_tpg)->tpg_release_fabric_acl(se_tpg,
603 se_nacl);
604 spin_lock_bh(&se_tpg->acl_node_lock);
607 spin_unlock_bh(&se_tpg->acl_node_lock);
610 transport_free_session(se_sess);
612 printk(KERN_INFO "TARGET_CORE[%s]: Deregistered fabric_sess\n",
613 TPG_TFO(se_tpg)->get_fabric_name());
615 EXPORT_SYMBOL(transport_deregister_session);
618 * Called with T_TASK(cmd)->t_state_lock held.
620 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
622 struct se_device *dev;
623 struct se_task *task;
624 unsigned long flags;
626 if (!T_TASK(cmd))
627 return;
629 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
630 dev = task->se_dev;
631 if (!(dev))
632 continue;
634 if (atomic_read(&task->task_active))
635 continue;
637 if (!(atomic_read(&task->task_state_active)))
638 continue;
640 spin_lock_irqsave(&dev->execute_task_lock, flags);
641 list_del(&task->t_state_list);
642 DEBUG_TSTATE("Removed ITT: 0x%08x dev: %p task[%p]\n",
643 CMD_TFO(cmd)->tfo_get_task_tag(cmd), dev, task);
644 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
646 atomic_set(&task->task_state_active, 0);
647 atomic_dec(&T_TASK(cmd)->t_task_cdbs_ex_left);
651 /* transport_cmd_check_stop():
653 * 'transport_off = 1' determines if t_transport_active should be cleared.
654 * 'transport_off = 2' determines if task_dev_state should be removed.
656 * A non-zero u8 t_state sets cmd->t_state.
657 * Returns 1 when command is stopped, else 0.
659 static int transport_cmd_check_stop(
660 struct se_cmd *cmd,
661 int transport_off,
662 u8 t_state)
664 unsigned long flags;
666 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
668 * Determine if IOCTL context caller in requesting the stopping of this
669 * command for LUN shutdown purposes.
671 if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
672 DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->transport_lun_stop)"
673 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
674 CMD_TFO(cmd)->get_task_tag(cmd));
676 cmd->deferred_t_state = cmd->t_state;
677 cmd->t_state = TRANSPORT_DEFERRED_CMD;
678 atomic_set(&T_TASK(cmd)->t_transport_active, 0);
679 if (transport_off == 2)
680 transport_all_task_dev_remove_state(cmd);
681 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
683 complete(&T_TASK(cmd)->transport_lun_stop_comp);
684 return 1;
687 * Determine if frontend context caller is requesting the stopping of
688 * this command for frontend excpections.
690 if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
691 DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->t_transport_stop) =="
692 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
693 CMD_TFO(cmd)->get_task_tag(cmd));
695 cmd->deferred_t_state = cmd->t_state;
696 cmd->t_state = TRANSPORT_DEFERRED_CMD;
697 if (transport_off == 2)
698 transport_all_task_dev_remove_state(cmd);
701 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
702 * to FE.
704 if (transport_off == 2)
705 cmd->se_lun = NULL;
706 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
708 complete(&T_TASK(cmd)->t_transport_stop_comp);
709 return 1;
711 if (transport_off) {
712 atomic_set(&T_TASK(cmd)->t_transport_active, 0);
713 if (transport_off == 2) {
714 transport_all_task_dev_remove_state(cmd);
716 * Clear struct se_cmd->se_lun before the transport_off == 2
717 * handoff to fabric module.
719 cmd->se_lun = NULL;
721 * Some fabric modules like tcm_loop can release
722 * their internally allocated I/O refrence now and
723 * struct se_cmd now.
725 if (CMD_TFO(cmd)->check_stop_free != NULL) {
726 spin_unlock_irqrestore(
727 &T_TASK(cmd)->t_state_lock, flags);
729 CMD_TFO(cmd)->check_stop_free(cmd);
730 return 1;
733 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
735 return 0;
736 } else if (t_state)
737 cmd->t_state = t_state;
738 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
740 return 0;
743 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
745 return transport_cmd_check_stop(cmd, 2, 0);
748 static void transport_lun_remove_cmd(struct se_cmd *cmd)
750 struct se_lun *lun = SE_LUN(cmd);
751 unsigned long flags;
753 if (!lun)
754 return;
756 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
757 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
758 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
759 goto check_lun;
761 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
762 transport_all_task_dev_remove_state(cmd);
763 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
765 transport_free_dev_tasks(cmd);
767 check_lun:
768 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
769 if (atomic_read(&T_TASK(cmd)->transport_lun_active)) {
770 list_del(&cmd->se_lun_list);
771 atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
772 #if 0
773 printk(KERN_INFO "Removed ITT: 0x%08x from LUN LIST[%d]\n"
774 CMD_TFO(cmd)->get_task_tag(cmd), lun->unpacked_lun);
775 #endif
777 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
780 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
782 transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
783 transport_lun_remove_cmd(cmd);
785 if (transport_cmd_check_stop_to_fabric(cmd))
786 return;
787 if (remove)
788 transport_generic_remove(cmd, 0, 0);
791 void transport_cmd_finish_abort_tmr(struct se_cmd *cmd)
793 transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
795 if (transport_cmd_check_stop_to_fabric(cmd))
796 return;
798 transport_generic_remove(cmd, 0, 0);
801 static int transport_add_cmd_to_queue(
802 struct se_cmd *cmd,
803 int t_state)
805 struct se_device *dev = cmd->se_dev;
806 struct se_queue_obj *qobj = dev->dev_queue_obj;
807 struct se_queue_req *qr;
808 unsigned long flags;
810 qr = kzalloc(sizeof(struct se_queue_req), GFP_ATOMIC);
811 if (!(qr)) {
812 printk(KERN_ERR "Unable to allocate memory for"
813 " struct se_queue_req\n");
814 return -1;
816 INIT_LIST_HEAD(&qr->qr_list);
818 qr->cmd = (void *)cmd;
819 qr->state = t_state;
821 if (t_state) {
822 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
823 cmd->t_state = t_state;
824 atomic_set(&T_TASK(cmd)->t_transport_active, 1);
825 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
828 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
829 list_add_tail(&qr->qr_list, &qobj->qobj_list);
830 atomic_inc(&T_TASK(cmd)->t_transport_queue_active);
831 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
833 atomic_inc(&qobj->queue_cnt);
834 wake_up_interruptible(&qobj->thread_wq);
835 return 0;
839 * Called with struct se_queue_obj->cmd_queue_lock held.
841 static struct se_queue_req *
842 __transport_get_qr_from_queue(struct se_queue_obj *qobj)
844 struct se_cmd *cmd;
845 struct se_queue_req *qr = NULL;
847 if (list_empty(&qobj->qobj_list))
848 return NULL;
850 list_for_each_entry(qr, &qobj->qobj_list, qr_list)
851 break;
853 if (qr->cmd) {
854 cmd = (struct se_cmd *)qr->cmd;
855 atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
857 list_del(&qr->qr_list);
858 atomic_dec(&qobj->queue_cnt);
860 return qr;
863 static struct se_queue_req *
864 transport_get_qr_from_queue(struct se_queue_obj *qobj)
866 struct se_cmd *cmd;
867 struct se_queue_req *qr;
868 unsigned long flags;
870 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
871 if (list_empty(&qobj->qobj_list)) {
872 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
873 return NULL;
876 list_for_each_entry(qr, &qobj->qobj_list, qr_list)
877 break;
879 if (qr->cmd) {
880 cmd = (struct se_cmd *)qr->cmd;
881 atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
883 list_del(&qr->qr_list);
884 atomic_dec(&qobj->queue_cnt);
885 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
887 return qr;
890 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
891 struct se_queue_obj *qobj)
893 struct se_cmd *q_cmd;
894 struct se_queue_req *qr = NULL, *qr_p = NULL;
895 unsigned long flags;
897 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
898 if (!(atomic_read(&T_TASK(cmd)->t_transport_queue_active))) {
899 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
900 return;
903 list_for_each_entry_safe(qr, qr_p, &qobj->qobj_list, qr_list) {
904 q_cmd = (struct se_cmd *)qr->cmd;
905 if (q_cmd != cmd)
906 continue;
908 atomic_dec(&T_TASK(q_cmd)->t_transport_queue_active);
909 atomic_dec(&qobj->queue_cnt);
910 list_del(&qr->qr_list);
911 kfree(qr);
913 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
915 if (atomic_read(&T_TASK(cmd)->t_transport_queue_active)) {
916 printk(KERN_ERR "ITT: 0x%08x t_transport_queue_active: %d\n",
917 CMD_TFO(cmd)->get_task_tag(cmd),
918 atomic_read(&T_TASK(cmd)->t_transport_queue_active));
923 * Completion function used by TCM subsystem plugins (such as FILEIO)
924 * for queueing up response from struct se_subsystem_api->do_task()
926 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
928 struct se_task *task = list_entry(T_TASK(cmd)->t_task_list.next,
929 struct se_task, t_list);
931 if (good) {
932 cmd->scsi_status = SAM_STAT_GOOD;
933 task->task_scsi_status = GOOD;
934 } else {
935 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
936 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
937 TASK_CMD(task)->transport_error_status =
938 PYX_TRANSPORT_ILLEGAL_REQUEST;
941 transport_complete_task(task, good);
943 EXPORT_SYMBOL(transport_complete_sync_cache);
945 /* transport_complete_task():
947 * Called from interrupt and non interrupt context depending
948 * on the transport plugin.
950 void transport_complete_task(struct se_task *task, int success)
952 struct se_cmd *cmd = TASK_CMD(task);
953 struct se_device *dev = task->se_dev;
954 int t_state;
955 unsigned long flags;
956 #if 0
957 printk(KERN_INFO "task: %p CDB: 0x%02x obj_ptr: %p\n", task,
958 T_TASK(cmd)->t_task_cdb[0], dev);
959 #endif
960 if (dev) {
961 spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
962 atomic_inc(&dev->depth_left);
963 atomic_inc(&SE_HBA(dev)->left_queue_depth);
964 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
967 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
968 atomic_set(&task->task_active, 0);
971 * See if any sense data exists, if so set the TASK_SENSE flag.
972 * Also check for any other post completion work that needs to be
973 * done by the plugins.
975 if (dev && dev->transport->transport_complete) {
976 if (dev->transport->transport_complete(task) != 0) {
977 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
978 task->task_sense = 1;
979 success = 1;
984 * See if we are waiting for outstanding struct se_task
985 * to complete for an exception condition
987 if (atomic_read(&task->task_stop)) {
989 * Decrement T_TASK(cmd)->t_se_count if this task had
990 * previously thrown its timeout exception handler.
992 if (atomic_read(&task->task_timeout)) {
993 atomic_dec(&T_TASK(cmd)->t_se_count);
994 atomic_set(&task->task_timeout, 0);
996 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
998 complete(&task->task_stop_comp);
999 return;
1002 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
1003 * left counter to determine when the struct se_cmd is ready to be queued to
1004 * the processing thread.
1006 if (atomic_read(&task->task_timeout)) {
1007 if (!(atomic_dec_and_test(
1008 &T_TASK(cmd)->t_task_cdbs_timeout_left))) {
1009 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
1010 flags);
1011 return;
1013 t_state = TRANSPORT_COMPLETE_TIMEOUT;
1014 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1016 transport_add_cmd_to_queue(cmd, t_state);
1017 return;
1019 atomic_dec(&T_TASK(cmd)->t_task_cdbs_timeout_left);
1022 * Decrement the outstanding t_task_cdbs_left count. The last
1023 * struct se_task from struct se_cmd will complete itself into the
1024 * device queue depending upon int success.
1026 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
1027 if (!success)
1028 T_TASK(cmd)->t_tasks_failed = 1;
1030 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1031 return;
1034 if (!success || T_TASK(cmd)->t_tasks_failed) {
1035 t_state = TRANSPORT_COMPLETE_FAILURE;
1036 if (!task->task_error_status) {
1037 task->task_error_status =
1038 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1039 cmd->transport_error_status =
1040 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1042 } else {
1043 atomic_set(&T_TASK(cmd)->t_transport_complete, 1);
1044 t_state = TRANSPORT_COMPLETE_OK;
1046 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1048 transport_add_cmd_to_queue(cmd, t_state);
1050 EXPORT_SYMBOL(transport_complete_task);
1053 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
1054 * struct se_task list are ready to be added to the active execution list
1055 * struct se_device
1057 * Called with se_dev_t->execute_task_lock called.
1059 static inline int transport_add_task_check_sam_attr(
1060 struct se_task *task,
1061 struct se_task *task_prev,
1062 struct se_device *dev)
1065 * No SAM Task attribute emulation enabled, add to tail of
1066 * execution queue
1068 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
1069 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
1070 return 0;
1073 * HEAD_OF_QUEUE attribute for received CDB, which means
1074 * the first task that is associated with a struct se_cmd goes to
1075 * head of the struct se_device->execute_task_list, and task_prev
1076 * after that for each subsequent task
1078 if (task->task_se_cmd->sam_task_attr == TASK_ATTR_HOQ) {
1079 list_add(&task->t_execute_list,
1080 (task_prev != NULL) ?
1081 &task_prev->t_execute_list :
1082 &dev->execute_task_list);
1084 DEBUG_STA("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
1085 " in execution queue\n",
1086 T_TASK(task->task_se_cmd)->t_task_cdb[0]);
1087 return 1;
1090 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
1091 * transitioned from Dermant -> Active state, and are added to the end
1092 * of the struct se_device->execute_task_list
1094 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
1095 return 0;
1098 /* __transport_add_task_to_execute_queue():
1100 * Called with se_dev_t->execute_task_lock called.
1102 static void __transport_add_task_to_execute_queue(
1103 struct se_task *task,
1104 struct se_task *task_prev,
1105 struct se_device *dev)
1107 int head_of_queue;
1109 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
1110 atomic_inc(&dev->execute_tasks);
1112 if (atomic_read(&task->task_state_active))
1113 return;
1115 * Determine if this task needs to go to HEAD_OF_QUEUE for the
1116 * state list as well. Running with SAM Task Attribute emulation
1117 * will always return head_of_queue == 0 here
1119 if (head_of_queue)
1120 list_add(&task->t_state_list, (task_prev) ?
1121 &task_prev->t_state_list :
1122 &dev->state_task_list);
1123 else
1124 list_add_tail(&task->t_state_list, &dev->state_task_list);
1126 atomic_set(&task->task_state_active, 1);
1128 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1129 CMD_TFO(task->task_se_cmd)->get_task_tag(task->task_se_cmd),
1130 task, dev);
1133 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
1135 struct se_device *dev;
1136 struct se_task *task;
1137 unsigned long flags;
1139 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
1140 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
1141 dev = task->se_dev;
1143 if (atomic_read(&task->task_state_active))
1144 continue;
1146 spin_lock(&dev->execute_task_lock);
1147 list_add_tail(&task->t_state_list, &dev->state_task_list);
1148 atomic_set(&task->task_state_active, 1);
1150 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1151 CMD_TFO(task->task_se_cmd)->get_task_tag(
1152 task->task_se_cmd), task, dev);
1154 spin_unlock(&dev->execute_task_lock);
1156 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1159 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
1161 struct se_device *dev = SE_DEV(cmd);
1162 struct se_task *task, *task_prev = NULL;
1163 unsigned long flags;
1165 spin_lock_irqsave(&dev->execute_task_lock, flags);
1166 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
1167 if (atomic_read(&task->task_execute_queue))
1168 continue;
1170 * __transport_add_task_to_execute_queue() handles the
1171 * SAM Task Attribute emulation if enabled
1173 __transport_add_task_to_execute_queue(task, task_prev, dev);
1174 atomic_set(&task->task_execute_queue, 1);
1175 task_prev = task;
1177 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1179 return;
1182 /* transport_get_task_from_execute_queue():
1184 * Called with dev->execute_task_lock held.
1186 static struct se_task *
1187 transport_get_task_from_execute_queue(struct se_device *dev)
1189 struct se_task *task;
1191 if (list_empty(&dev->execute_task_list))
1192 return NULL;
1194 list_for_each_entry(task, &dev->execute_task_list, t_execute_list)
1195 break;
1197 list_del(&task->t_execute_list);
1198 atomic_dec(&dev->execute_tasks);
1200 return task;
1203 /* transport_remove_task_from_execute_queue():
1207 void transport_remove_task_from_execute_queue(
1208 struct se_task *task,
1209 struct se_device *dev)
1211 unsigned long flags;
1213 spin_lock_irqsave(&dev->execute_task_lock, flags);
1214 list_del(&task->t_execute_list);
1215 atomic_dec(&dev->execute_tasks);
1216 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1219 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
1221 switch (cmd->data_direction) {
1222 case DMA_NONE:
1223 return "NONE";
1224 case DMA_FROM_DEVICE:
1225 return "READ";
1226 case DMA_TO_DEVICE:
1227 return "WRITE";
1228 case DMA_BIDIRECTIONAL:
1229 return "BIDI";
1230 default:
1231 break;
1234 return "UNKNOWN";
1237 void transport_dump_dev_state(
1238 struct se_device *dev,
1239 char *b,
1240 int *bl)
1242 *bl += sprintf(b + *bl, "Status: ");
1243 switch (dev->dev_status) {
1244 case TRANSPORT_DEVICE_ACTIVATED:
1245 *bl += sprintf(b + *bl, "ACTIVATED");
1246 break;
1247 case TRANSPORT_DEVICE_DEACTIVATED:
1248 *bl += sprintf(b + *bl, "DEACTIVATED");
1249 break;
1250 case TRANSPORT_DEVICE_SHUTDOWN:
1251 *bl += sprintf(b + *bl, "SHUTDOWN");
1252 break;
1253 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
1254 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
1255 *bl += sprintf(b + *bl, "OFFLINE");
1256 break;
1257 default:
1258 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
1259 break;
1262 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
1263 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
1264 dev->queue_depth);
1265 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
1266 DEV_ATTRIB(dev)->block_size, DEV_ATTRIB(dev)->max_sectors);
1267 *bl += sprintf(b + *bl, " ");
1270 /* transport_release_all_cmds():
1274 static void transport_release_all_cmds(struct se_device *dev)
1276 struct se_cmd *cmd = NULL;
1277 struct se_queue_req *qr = NULL, *qr_p = NULL;
1278 int bug_out = 0, t_state;
1279 unsigned long flags;
1281 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
1282 list_for_each_entry_safe(qr, qr_p, &dev->dev_queue_obj->qobj_list,
1283 qr_list) {
1285 cmd = (struct se_cmd *)qr->cmd;
1286 t_state = qr->state;
1287 list_del(&qr->qr_list);
1288 kfree(qr);
1289 spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock,
1290 flags);
1292 printk(KERN_ERR "Releasing ITT: 0x%08x, i_state: %u,"
1293 " t_state: %u directly\n",
1294 CMD_TFO(cmd)->get_task_tag(cmd),
1295 CMD_TFO(cmd)->get_cmd_state(cmd), t_state);
1297 transport_release_fe_cmd(cmd);
1298 bug_out = 1;
1300 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
1302 spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
1303 #if 0
1304 if (bug_out)
1305 BUG();
1306 #endif
1309 void transport_dump_vpd_proto_id(
1310 struct t10_vpd *vpd,
1311 unsigned char *p_buf,
1312 int p_buf_len)
1314 unsigned char buf[VPD_TMP_BUF_SIZE];
1315 int len;
1317 memset(buf, 0, VPD_TMP_BUF_SIZE);
1318 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1320 switch (vpd->protocol_identifier) {
1321 case 0x00:
1322 sprintf(buf+len, "Fibre Channel\n");
1323 break;
1324 case 0x10:
1325 sprintf(buf+len, "Parallel SCSI\n");
1326 break;
1327 case 0x20:
1328 sprintf(buf+len, "SSA\n");
1329 break;
1330 case 0x30:
1331 sprintf(buf+len, "IEEE 1394\n");
1332 break;
1333 case 0x40:
1334 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1335 " Protocol\n");
1336 break;
1337 case 0x50:
1338 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1339 break;
1340 case 0x60:
1341 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1342 break;
1343 case 0x70:
1344 sprintf(buf+len, "Automation/Drive Interface Transport"
1345 " Protocol\n");
1346 break;
1347 case 0x80:
1348 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1349 break;
1350 default:
1351 sprintf(buf+len, "Unknown 0x%02x\n",
1352 vpd->protocol_identifier);
1353 break;
1356 if (p_buf)
1357 strncpy(p_buf, buf, p_buf_len);
1358 else
1359 printk(KERN_INFO "%s", buf);
1362 void
1363 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1366 * Check if the Protocol Identifier Valid (PIV) bit is set..
1368 * from spc3r23.pdf section 7.5.1
1370 if (page_83[1] & 0x80) {
1371 vpd->protocol_identifier = (page_83[0] & 0xf0);
1372 vpd->protocol_identifier_set = 1;
1373 transport_dump_vpd_proto_id(vpd, NULL, 0);
1376 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1378 int transport_dump_vpd_assoc(
1379 struct t10_vpd *vpd,
1380 unsigned char *p_buf,
1381 int p_buf_len)
1383 unsigned char buf[VPD_TMP_BUF_SIZE];
1384 int ret = 0, len;
1386 memset(buf, 0, VPD_TMP_BUF_SIZE);
1387 len = sprintf(buf, "T10 VPD Identifier Association: ");
1389 switch (vpd->association) {
1390 case 0x00:
1391 sprintf(buf+len, "addressed logical unit\n");
1392 break;
1393 case 0x10:
1394 sprintf(buf+len, "target port\n");
1395 break;
1396 case 0x20:
1397 sprintf(buf+len, "SCSI target device\n");
1398 break;
1399 default:
1400 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1401 ret = -1;
1402 break;
1405 if (p_buf)
1406 strncpy(p_buf, buf, p_buf_len);
1407 else
1408 printk("%s", buf);
1410 return ret;
1413 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1416 * The VPD identification association..
1418 * from spc3r23.pdf Section 7.6.3.1 Table 297
1420 vpd->association = (page_83[1] & 0x30);
1421 return transport_dump_vpd_assoc(vpd, NULL, 0);
1423 EXPORT_SYMBOL(transport_set_vpd_assoc);
1425 int transport_dump_vpd_ident_type(
1426 struct t10_vpd *vpd,
1427 unsigned char *p_buf,
1428 int p_buf_len)
1430 unsigned char buf[VPD_TMP_BUF_SIZE];
1431 int ret = 0, len;
1433 memset(buf, 0, VPD_TMP_BUF_SIZE);
1434 len = sprintf(buf, "T10 VPD Identifier Type: ");
1436 switch (vpd->device_identifier_type) {
1437 case 0x00:
1438 sprintf(buf+len, "Vendor specific\n");
1439 break;
1440 case 0x01:
1441 sprintf(buf+len, "T10 Vendor ID based\n");
1442 break;
1443 case 0x02:
1444 sprintf(buf+len, "EUI-64 based\n");
1445 break;
1446 case 0x03:
1447 sprintf(buf+len, "NAA\n");
1448 break;
1449 case 0x04:
1450 sprintf(buf+len, "Relative target port identifier\n");
1451 break;
1452 case 0x08:
1453 sprintf(buf+len, "SCSI name string\n");
1454 break;
1455 default:
1456 sprintf(buf+len, "Unsupported: 0x%02x\n",
1457 vpd->device_identifier_type);
1458 ret = -1;
1459 break;
1462 if (p_buf)
1463 strncpy(p_buf, buf, p_buf_len);
1464 else
1465 printk("%s", buf);
1467 return ret;
1470 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1473 * The VPD identifier type..
1475 * from spc3r23.pdf Section 7.6.3.1 Table 298
1477 vpd->device_identifier_type = (page_83[1] & 0x0f);
1478 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1480 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1482 int transport_dump_vpd_ident(
1483 struct t10_vpd *vpd,
1484 unsigned char *p_buf,
1485 int p_buf_len)
1487 unsigned char buf[VPD_TMP_BUF_SIZE];
1488 int ret = 0;
1490 memset(buf, 0, VPD_TMP_BUF_SIZE);
1492 switch (vpd->device_identifier_code_set) {
1493 case 0x01: /* Binary */
1494 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1495 &vpd->device_identifier[0]);
1496 break;
1497 case 0x02: /* ASCII */
1498 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1499 &vpd->device_identifier[0]);
1500 break;
1501 case 0x03: /* UTF-8 */
1502 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1503 &vpd->device_identifier[0]);
1504 break;
1505 default:
1506 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1507 " 0x%02x", vpd->device_identifier_code_set);
1508 ret = -1;
1509 break;
1512 if (p_buf)
1513 strncpy(p_buf, buf, p_buf_len);
1514 else
1515 printk("%s", buf);
1517 return ret;
1521 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1523 static const char hex_str[] = "0123456789abcdef";
1524 int j = 0, i = 4; /* offset to start of the identifer */
1527 * The VPD Code Set (encoding)
1529 * from spc3r23.pdf Section 7.6.3.1 Table 296
1531 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1532 switch (vpd->device_identifier_code_set) {
1533 case 0x01: /* Binary */
1534 vpd->device_identifier[j++] =
1535 hex_str[vpd->device_identifier_type];
1536 while (i < (4 + page_83[3])) {
1537 vpd->device_identifier[j++] =
1538 hex_str[(page_83[i] & 0xf0) >> 4];
1539 vpd->device_identifier[j++] =
1540 hex_str[page_83[i] & 0x0f];
1541 i++;
1543 break;
1544 case 0x02: /* ASCII */
1545 case 0x03: /* UTF-8 */
1546 while (i < (4 + page_83[3]))
1547 vpd->device_identifier[j++] = page_83[i++];
1548 break;
1549 default:
1550 break;
1553 return transport_dump_vpd_ident(vpd, NULL, 0);
1555 EXPORT_SYMBOL(transport_set_vpd_ident);
1557 static void core_setup_task_attr_emulation(struct se_device *dev)
1560 * If this device is from Target_Core_Mod/pSCSI, disable the
1561 * SAM Task Attribute emulation.
1563 * This is currently not available in upsream Linux/SCSI Target
1564 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1566 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1567 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1568 return;
1571 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1572 DEBUG_STA("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1573 " device\n", TRANSPORT(dev)->name,
1574 TRANSPORT(dev)->get_device_rev(dev));
1577 static void scsi_dump_inquiry(struct se_device *dev)
1579 struct t10_wwn *wwn = DEV_T10_WWN(dev);
1580 int i, device_type;
1582 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1584 printk(" Vendor: ");
1585 for (i = 0; i < 8; i++)
1586 if (wwn->vendor[i] >= 0x20)
1587 printk("%c", wwn->vendor[i]);
1588 else
1589 printk(" ");
1591 printk(" Model: ");
1592 for (i = 0; i < 16; i++)
1593 if (wwn->model[i] >= 0x20)
1594 printk("%c", wwn->model[i]);
1595 else
1596 printk(" ");
1598 printk(" Revision: ");
1599 for (i = 0; i < 4; i++)
1600 if (wwn->revision[i] >= 0x20)
1601 printk("%c", wwn->revision[i]);
1602 else
1603 printk(" ");
1605 printk("\n");
1607 device_type = TRANSPORT(dev)->get_device_type(dev);
1608 printk(" Type: %s ", scsi_device_type(device_type));
1609 printk(" ANSI SCSI revision: %02x\n",
1610 TRANSPORT(dev)->get_device_rev(dev));
1613 struct se_device *transport_add_device_to_core_hba(
1614 struct se_hba *hba,
1615 struct se_subsystem_api *transport,
1616 struct se_subsystem_dev *se_dev,
1617 u32 device_flags,
1618 void *transport_dev,
1619 struct se_dev_limits *dev_limits,
1620 const char *inquiry_prod,
1621 const char *inquiry_rev)
1623 int force_pt;
1624 struct se_device *dev;
1626 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1627 if (!(dev)) {
1628 printk(KERN_ERR "Unable to allocate memory for se_dev_t\n");
1629 return NULL;
1631 dev->dev_queue_obj = kzalloc(sizeof(struct se_queue_obj), GFP_KERNEL);
1632 if (!(dev->dev_queue_obj)) {
1633 printk(KERN_ERR "Unable to allocate memory for"
1634 " dev->dev_queue_obj\n");
1635 kfree(dev);
1636 return NULL;
1638 transport_init_queue_obj(dev->dev_queue_obj);
1640 dev->dev_status_queue_obj = kzalloc(sizeof(struct se_queue_obj),
1641 GFP_KERNEL);
1642 if (!(dev->dev_status_queue_obj)) {
1643 printk(KERN_ERR "Unable to allocate memory for"
1644 " dev->dev_status_queue_obj\n");
1645 kfree(dev->dev_queue_obj);
1646 kfree(dev);
1647 return NULL;
1649 transport_init_queue_obj(dev->dev_status_queue_obj);
1651 dev->dev_flags = device_flags;
1652 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1653 dev->dev_ptr = (void *) transport_dev;
1654 dev->se_hba = hba;
1655 dev->se_sub_dev = se_dev;
1656 dev->transport = transport;
1657 atomic_set(&dev->active_cmds, 0);
1658 INIT_LIST_HEAD(&dev->dev_list);
1659 INIT_LIST_HEAD(&dev->dev_sep_list);
1660 INIT_LIST_HEAD(&dev->dev_tmr_list);
1661 INIT_LIST_HEAD(&dev->execute_task_list);
1662 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1663 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1664 INIT_LIST_HEAD(&dev->state_task_list);
1665 spin_lock_init(&dev->execute_task_lock);
1666 spin_lock_init(&dev->delayed_cmd_lock);
1667 spin_lock_init(&dev->ordered_cmd_lock);
1668 spin_lock_init(&dev->state_task_lock);
1669 spin_lock_init(&dev->dev_alua_lock);
1670 spin_lock_init(&dev->dev_reservation_lock);
1671 spin_lock_init(&dev->dev_status_lock);
1672 spin_lock_init(&dev->dev_status_thr_lock);
1673 spin_lock_init(&dev->se_port_lock);
1674 spin_lock_init(&dev->se_tmr_lock);
1676 dev->queue_depth = dev_limits->queue_depth;
1677 atomic_set(&dev->depth_left, dev->queue_depth);
1678 atomic_set(&dev->dev_ordered_id, 0);
1680 se_dev_set_default_attribs(dev, dev_limits);
1682 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1683 dev->creation_time = get_jiffies_64();
1684 spin_lock_init(&dev->stats_lock);
1686 spin_lock(&hba->device_lock);
1687 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1688 hba->dev_count++;
1689 spin_unlock(&hba->device_lock);
1691 * Setup the SAM Task Attribute emulation for struct se_device
1693 core_setup_task_attr_emulation(dev);
1695 * Force PR and ALUA passthrough emulation with internal object use.
1697 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1699 * Setup the Reservations infrastructure for struct se_device
1701 core_setup_reservations(dev, force_pt);
1703 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1705 if (core_setup_alua(dev, force_pt) < 0)
1706 goto out;
1709 * Startup the struct se_device processing thread
1711 dev->process_thread = kthread_run(transport_processing_thread, dev,
1712 "LIO_%s", TRANSPORT(dev)->name);
1713 if (IS_ERR(dev->process_thread)) {
1714 printk(KERN_ERR "Unable to create kthread: LIO_%s\n",
1715 TRANSPORT(dev)->name);
1716 goto out;
1720 * Preload the initial INQUIRY const values if we are doing
1721 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1722 * passthrough because this is being provided by the backend LLD.
1723 * This is required so that transport_get_inquiry() copies these
1724 * originals once back into DEV_T10_WWN(dev) for the virtual device
1725 * setup.
1727 if (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1728 if (!(inquiry_prod) || !(inquiry_prod)) {
1729 printk(KERN_ERR "All non TCM/pSCSI plugins require"
1730 " INQUIRY consts\n");
1731 goto out;
1734 strncpy(&DEV_T10_WWN(dev)->vendor[0], "LIO-ORG", 8);
1735 strncpy(&DEV_T10_WWN(dev)->model[0], inquiry_prod, 16);
1736 strncpy(&DEV_T10_WWN(dev)->revision[0], inquiry_rev, 4);
1738 scsi_dump_inquiry(dev);
1740 return dev;
1741 out:
1742 kthread_stop(dev->process_thread);
1744 spin_lock(&hba->device_lock);
1745 list_del(&dev->dev_list);
1746 hba->dev_count--;
1747 spin_unlock(&hba->device_lock);
1749 se_release_vpd_for_dev(dev);
1751 kfree(dev->dev_status_queue_obj);
1752 kfree(dev->dev_queue_obj);
1753 kfree(dev);
1755 return NULL;
1757 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1759 /* transport_generic_prepare_cdb():
1761 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1762 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1763 * The point of this is since we are mapping iSCSI LUNs to
1764 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1765 * devices and HBAs for a loop.
1767 static inline void transport_generic_prepare_cdb(
1768 unsigned char *cdb)
1770 switch (cdb[0]) {
1771 case READ_10: /* SBC - RDProtect */
1772 case READ_12: /* SBC - RDProtect */
1773 case READ_16: /* SBC - RDProtect */
1774 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1775 case VERIFY: /* SBC - VRProtect */
1776 case VERIFY_16: /* SBC - VRProtect */
1777 case WRITE_VERIFY: /* SBC - VRProtect */
1778 case WRITE_VERIFY_12: /* SBC - VRProtect */
1779 break;
1780 default:
1781 cdb[1] &= 0x1f; /* clear logical unit number */
1782 break;
1786 static struct se_task *
1787 transport_generic_get_task(struct se_cmd *cmd,
1788 enum dma_data_direction data_direction)
1790 struct se_task *task;
1791 struct se_device *dev = SE_DEV(cmd);
1792 unsigned long flags;
1794 task = dev->transport->alloc_task(cmd);
1795 if (!task) {
1796 printk(KERN_ERR "Unable to allocate struct se_task\n");
1797 return NULL;
1800 INIT_LIST_HEAD(&task->t_list);
1801 INIT_LIST_HEAD(&task->t_execute_list);
1802 INIT_LIST_HEAD(&task->t_state_list);
1803 init_completion(&task->task_stop_comp);
1804 task->task_no = T_TASK(cmd)->t_tasks_no++;
1805 task->task_se_cmd = cmd;
1806 task->se_dev = dev;
1807 task->task_data_direction = data_direction;
1809 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
1810 list_add_tail(&task->t_list, &T_TASK(cmd)->t_task_list);
1811 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1813 return task;
1816 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1818 void transport_device_setup_cmd(struct se_cmd *cmd)
1820 cmd->se_dev = SE_LUN(cmd)->lun_se_dev;
1822 EXPORT_SYMBOL(transport_device_setup_cmd);
1825 * Used by fabric modules containing a local struct se_cmd within their
1826 * fabric dependent per I/O descriptor.
1828 void transport_init_se_cmd(
1829 struct se_cmd *cmd,
1830 struct target_core_fabric_ops *tfo,
1831 struct se_session *se_sess,
1832 u32 data_length,
1833 int data_direction,
1834 int task_attr,
1835 unsigned char *sense_buffer)
1837 INIT_LIST_HEAD(&cmd->se_lun_list);
1838 INIT_LIST_HEAD(&cmd->se_delayed_list);
1839 INIT_LIST_HEAD(&cmd->se_ordered_list);
1841 * Setup t_task pointer to t_task_backstore
1843 cmd->t_task = &cmd->t_task_backstore;
1845 INIT_LIST_HEAD(&T_TASK(cmd)->t_task_list);
1846 init_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
1847 init_completion(&T_TASK(cmd)->transport_lun_stop_comp);
1848 init_completion(&T_TASK(cmd)->t_transport_stop_comp);
1849 spin_lock_init(&T_TASK(cmd)->t_state_lock);
1850 atomic_set(&T_TASK(cmd)->transport_dev_active, 1);
1852 cmd->se_tfo = tfo;
1853 cmd->se_sess = se_sess;
1854 cmd->data_length = data_length;
1855 cmd->data_direction = data_direction;
1856 cmd->sam_task_attr = task_attr;
1857 cmd->sense_buffer = sense_buffer;
1859 EXPORT_SYMBOL(transport_init_se_cmd);
1861 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1864 * Check if SAM Task Attribute emulation is enabled for this
1865 * struct se_device storage object
1867 if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1868 return 0;
1870 if (cmd->sam_task_attr == TASK_ATTR_ACA) {
1871 DEBUG_STA("SAM Task Attribute ACA"
1872 " emulation is not supported\n");
1873 return -1;
1876 * Used to determine when ORDERED commands should go from
1877 * Dormant to Active status.
1879 cmd->se_ordered_id = atomic_inc_return(&SE_DEV(cmd)->dev_ordered_id);
1880 smp_mb__after_atomic_inc();
1881 DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1882 cmd->se_ordered_id, cmd->sam_task_attr,
1883 TRANSPORT(cmd->se_dev)->name);
1884 return 0;
1887 void transport_free_se_cmd(
1888 struct se_cmd *se_cmd)
1890 if (se_cmd->se_tmr_req)
1891 core_tmr_release_req(se_cmd->se_tmr_req);
1893 * Check and free any extended CDB buffer that was allocated
1895 if (T_TASK(se_cmd)->t_task_cdb != T_TASK(se_cmd)->__t_task_cdb)
1896 kfree(T_TASK(se_cmd)->t_task_cdb);
1898 EXPORT_SYMBOL(transport_free_se_cmd);
1900 static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
1902 /* transport_generic_allocate_tasks():
1904 * Called from fabric RX Thread.
1906 int transport_generic_allocate_tasks(
1907 struct se_cmd *cmd,
1908 unsigned char *cdb)
1910 int ret;
1912 transport_generic_prepare_cdb(cdb);
1915 * This is needed for early exceptions.
1917 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1919 transport_device_setup_cmd(cmd);
1921 * Ensure that the received CDB is less than the max (252 + 8) bytes
1922 * for VARIABLE_LENGTH_CMD
1924 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1925 printk(KERN_ERR "Received SCSI CDB with command_size: %d that"
1926 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1927 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1928 return -1;
1931 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1932 * allocate the additional extended CDB buffer now.. Otherwise
1933 * setup the pointer from __t_task_cdb to t_task_cdb.
1935 if (scsi_command_size(cdb) > sizeof(T_TASK(cmd)->__t_task_cdb)) {
1936 T_TASK(cmd)->t_task_cdb = kzalloc(scsi_command_size(cdb),
1937 GFP_KERNEL);
1938 if (!(T_TASK(cmd)->t_task_cdb)) {
1939 printk(KERN_ERR "Unable to allocate T_TASK(cmd)->t_task_cdb"
1940 " %u > sizeof(T_TASK(cmd)->__t_task_cdb): %lu ops\n",
1941 scsi_command_size(cdb),
1942 (unsigned long)sizeof(T_TASK(cmd)->__t_task_cdb));
1943 return -1;
1945 } else
1946 T_TASK(cmd)->t_task_cdb = &T_TASK(cmd)->__t_task_cdb[0];
1948 * Copy the original CDB into T_TASK(cmd).
1950 memcpy(T_TASK(cmd)->t_task_cdb, cdb, scsi_command_size(cdb));
1952 * Setup the received CDB based on SCSI defined opcodes and
1953 * perform unit attention, persistent reservations and ALUA
1954 * checks for virtual device backends. The T_TASK(cmd)->t_task_cdb
1955 * pointer is expected to be setup before we reach this point.
1957 ret = transport_generic_cmd_sequencer(cmd, cdb);
1958 if (ret < 0)
1959 return ret;
1961 * Check for SAM Task Attribute Emulation
1963 if (transport_check_alloc_task_attr(cmd) < 0) {
1964 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1965 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1966 return -2;
1968 spin_lock(&cmd->se_lun->lun_sep_lock);
1969 if (cmd->se_lun->lun_sep)
1970 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1971 spin_unlock(&cmd->se_lun->lun_sep_lock);
1972 return 0;
1974 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1977 * Used by fabric module frontends not defining a TFO->new_cmd_map()
1978 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
1980 int transport_generic_handle_cdb(
1981 struct se_cmd *cmd)
1983 if (!SE_LUN(cmd)) {
1984 dump_stack();
1985 printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
1986 return -1;
1989 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
1990 return 0;
1992 EXPORT_SYMBOL(transport_generic_handle_cdb);
1995 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1996 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1997 * complete setup in TCM process context w/ TFO->new_cmd_map().
1999 int transport_generic_handle_cdb_map(
2000 struct se_cmd *cmd)
2002 if (!SE_LUN(cmd)) {
2003 dump_stack();
2004 printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
2005 return -1;
2008 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
2009 return 0;
2011 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
2013 /* transport_generic_handle_data():
2017 int transport_generic_handle_data(
2018 struct se_cmd *cmd)
2021 * For the software fabric case, then we assume the nexus is being
2022 * failed/shutdown when signals are pending from the kthread context
2023 * caller, so we return a failure. For the HW target mode case running
2024 * in interrupt code, the signal_pending() check is skipped.
2026 if (!in_interrupt() && signal_pending(current))
2027 return -1;
2029 * If the received CDB has aleady been ABORTED by the generic
2030 * target engine, we now call transport_check_aborted_status()
2031 * to queue any delated TASK_ABORTED status for the received CDB to the
2032 * fabric module as we are expecting no futher incoming DATA OUT
2033 * sequences at this point.
2035 if (transport_check_aborted_status(cmd, 1) != 0)
2036 return 0;
2038 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
2039 return 0;
2041 EXPORT_SYMBOL(transport_generic_handle_data);
2043 /* transport_generic_handle_tmr():
2047 int transport_generic_handle_tmr(
2048 struct se_cmd *cmd)
2051 * This is needed for early exceptions.
2053 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
2054 transport_device_setup_cmd(cmd);
2056 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
2057 return 0;
2059 EXPORT_SYMBOL(transport_generic_handle_tmr);
2061 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
2063 struct se_task *task, *task_tmp;
2064 unsigned long flags;
2065 int ret = 0;
2067 DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
2068 CMD_TFO(cmd)->get_task_tag(cmd));
2071 * No tasks remain in the execution queue
2073 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2074 list_for_each_entry_safe(task, task_tmp,
2075 &T_TASK(cmd)->t_task_list, t_list) {
2076 DEBUG_TS("task_no[%d] - Processing task %p\n",
2077 task->task_no, task);
2079 * If the struct se_task has not been sent and is not active,
2080 * remove the struct se_task from the execution queue.
2082 if (!atomic_read(&task->task_sent) &&
2083 !atomic_read(&task->task_active)) {
2084 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
2085 flags);
2086 transport_remove_task_from_execute_queue(task,
2087 task->se_dev);
2089 DEBUG_TS("task_no[%d] - Removed from execute queue\n",
2090 task->task_no);
2091 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2092 continue;
2096 * If the struct se_task is active, sleep until it is returned
2097 * from the plugin.
2099 if (atomic_read(&task->task_active)) {
2100 atomic_set(&task->task_stop, 1);
2101 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
2102 flags);
2104 DEBUG_TS("task_no[%d] - Waiting to complete\n",
2105 task->task_no);
2106 wait_for_completion(&task->task_stop_comp);
2107 DEBUG_TS("task_no[%d] - Stopped successfully\n",
2108 task->task_no);
2110 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2111 atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
2113 atomic_set(&task->task_active, 0);
2114 atomic_set(&task->task_stop, 0);
2115 } else {
2116 DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
2117 ret++;
2120 __transport_stop_task_timer(task, &flags);
2122 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2124 return ret;
2127 static void transport_failure_reset_queue_depth(struct se_device *dev)
2129 unsigned long flags;
2131 spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);;
2132 atomic_inc(&dev->depth_left);
2133 atomic_inc(&SE_HBA(dev)->left_queue_depth);
2134 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2138 * Handle SAM-esque emulation for generic transport request failures.
2140 static void transport_generic_request_failure(
2141 struct se_cmd *cmd,
2142 struct se_device *dev,
2143 int complete,
2144 int sc)
2146 DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
2147 " CDB: 0x%02x\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
2148 T_TASK(cmd)->t_task_cdb[0]);
2149 DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
2150 " %d/%d transport_error_status: %d\n",
2151 CMD_TFO(cmd)->get_cmd_state(cmd),
2152 cmd->t_state, cmd->deferred_t_state,
2153 cmd->transport_error_status);
2154 DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
2155 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
2156 " t_transport_active: %d t_transport_stop: %d"
2157 " t_transport_sent: %d\n", T_TASK(cmd)->t_task_cdbs,
2158 atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
2159 atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
2160 atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left),
2161 atomic_read(&T_TASK(cmd)->t_transport_active),
2162 atomic_read(&T_TASK(cmd)->t_transport_stop),
2163 atomic_read(&T_TASK(cmd)->t_transport_sent));
2165 transport_stop_all_task_timers(cmd);
2167 if (dev)
2168 transport_failure_reset_queue_depth(dev);
2170 * For SAM Task Attribute emulation for failed struct se_cmd
2172 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2173 transport_complete_task_attr(cmd);
2175 if (complete) {
2176 transport_direct_request_timeout(cmd);
2177 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2180 switch (cmd->transport_error_status) {
2181 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
2182 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2183 break;
2184 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
2185 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
2186 break;
2187 case PYX_TRANSPORT_INVALID_CDB_FIELD:
2188 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2189 break;
2190 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
2191 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2192 break;
2193 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
2194 if (!sc)
2195 transport_new_cmd_failure(cmd);
2197 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
2198 * we force this session to fall back to session
2199 * recovery.
2201 CMD_TFO(cmd)->fall_back_to_erl0(cmd->se_sess);
2202 CMD_TFO(cmd)->stop_session(cmd->se_sess, 0, 0);
2204 goto check_stop;
2205 case PYX_TRANSPORT_LU_COMM_FAILURE:
2206 case PYX_TRANSPORT_ILLEGAL_REQUEST:
2207 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2208 break;
2209 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
2210 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
2211 break;
2212 case PYX_TRANSPORT_WRITE_PROTECTED:
2213 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
2214 break;
2215 case PYX_TRANSPORT_RESERVATION_CONFLICT:
2217 * No SENSE Data payload for this case, set SCSI Status
2218 * and queue the response to $FABRIC_MOD.
2220 * Uses linux/include/scsi/scsi.h SAM status codes defs
2222 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2224 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2225 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2226 * CONFLICT STATUS.
2228 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2230 if (SE_SESS(cmd) &&
2231 DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
2232 core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
2233 cmd->orig_fe_lun, 0x2C,
2234 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2236 CMD_TFO(cmd)->queue_status(cmd);
2237 goto check_stop;
2238 case PYX_TRANSPORT_USE_SENSE_REASON:
2240 * struct se_cmd->scsi_sense_reason already set
2242 break;
2243 default:
2244 printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
2245 T_TASK(cmd)->t_task_cdb[0],
2246 cmd->transport_error_status);
2247 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2248 break;
2251 if (!sc)
2252 transport_new_cmd_failure(cmd);
2253 else
2254 transport_send_check_condition_and_sense(cmd,
2255 cmd->scsi_sense_reason, 0);
2256 check_stop:
2257 transport_lun_remove_cmd(cmd);
2258 if (!(transport_cmd_check_stop_to_fabric(cmd)))
2262 static void transport_direct_request_timeout(struct se_cmd *cmd)
2264 unsigned long flags;
2266 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2267 if (!(atomic_read(&T_TASK(cmd)->t_transport_timeout))) {
2268 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2269 return;
2271 if (atomic_read(&T_TASK(cmd)->t_task_cdbs_timeout_left)) {
2272 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2273 return;
2276 atomic_sub(atomic_read(&T_TASK(cmd)->t_transport_timeout),
2277 &T_TASK(cmd)->t_se_count);
2278 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2281 static void transport_generic_request_timeout(struct se_cmd *cmd)
2283 unsigned long flags;
2286 * Reset T_TASK(cmd)->t_se_count to allow transport_generic_remove()
2287 * to allow last call to free memory resources.
2289 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2290 if (atomic_read(&T_TASK(cmd)->t_transport_timeout) > 1) {
2291 int tmp = (atomic_read(&T_TASK(cmd)->t_transport_timeout) - 1);
2293 atomic_sub(tmp, &T_TASK(cmd)->t_se_count);
2295 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2297 transport_generic_remove(cmd, 0, 0);
2300 static int
2301 transport_generic_allocate_buf(struct se_cmd *cmd, u32 data_length)
2303 unsigned char *buf;
2305 buf = kzalloc(data_length, GFP_KERNEL);
2306 if (!(buf)) {
2307 printk(KERN_ERR "Unable to allocate memory for buffer\n");
2308 return -1;
2311 T_TASK(cmd)->t_tasks_se_num = 0;
2312 T_TASK(cmd)->t_task_buf = buf;
2314 return 0;
2317 static inline u32 transport_lba_21(unsigned char *cdb)
2319 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2322 static inline u32 transport_lba_32(unsigned char *cdb)
2324 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2327 static inline unsigned long long transport_lba_64(unsigned char *cdb)
2329 unsigned int __v1, __v2;
2331 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2332 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2334 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2338 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2340 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2342 unsigned int __v1, __v2;
2344 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2345 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2347 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2350 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2352 unsigned long flags;
2354 spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
2355 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2356 spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
2360 * Called from interrupt context.
2362 static void transport_task_timeout_handler(unsigned long data)
2364 struct se_task *task = (struct se_task *)data;
2365 struct se_cmd *cmd = TASK_CMD(task);
2366 unsigned long flags;
2368 DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
2370 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2371 if (task->task_flags & TF_STOP) {
2372 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2373 return;
2375 task->task_flags &= ~TF_RUNNING;
2378 * Determine if transport_complete_task() has already been called.
2380 if (!(atomic_read(&task->task_active))) {
2381 DEBUG_TT("transport task: %p cmd: %p timeout task_active"
2382 " == 0\n", task, cmd);
2383 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2384 return;
2387 atomic_inc(&T_TASK(cmd)->t_se_count);
2388 atomic_inc(&T_TASK(cmd)->t_transport_timeout);
2389 T_TASK(cmd)->t_tasks_failed = 1;
2391 atomic_set(&task->task_timeout, 1);
2392 task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
2393 task->task_scsi_status = 1;
2395 if (atomic_read(&task->task_stop)) {
2396 DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
2397 " == 1\n", task, cmd);
2398 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2399 complete(&task->task_stop_comp);
2400 return;
2403 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
2404 DEBUG_TT("transport task: %p cmd: %p timeout non zero"
2405 " t_task_cdbs_left\n", task, cmd);
2406 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2407 return;
2409 DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2410 task, cmd);
2412 cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
2413 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2415 transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
2419 * Called with T_TASK(cmd)->t_state_lock held.
2421 static void transport_start_task_timer(struct se_task *task)
2423 struct se_device *dev = task->se_dev;
2424 int timeout;
2426 if (task->task_flags & TF_RUNNING)
2427 return;
2429 * If the task_timeout is disabled, exit now.
2431 timeout = DEV_ATTRIB(dev)->task_timeout;
2432 if (!(timeout))
2433 return;
2435 init_timer(&task->task_timer);
2436 task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
2437 task->task_timer.data = (unsigned long) task;
2438 task->task_timer.function = transport_task_timeout_handler;
2440 task->task_flags |= TF_RUNNING;
2441 add_timer(&task->task_timer);
2442 #if 0
2443 printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
2444 " %d\n", task->task_se_cmd, task, timeout);
2445 #endif
2449 * Called with spin_lock_irq(&T_TASK(cmd)->t_state_lock) held.
2451 void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
2453 struct se_cmd *cmd = TASK_CMD(task);
2455 if (!(task->task_flags & TF_RUNNING))
2456 return;
2458 task->task_flags |= TF_STOP;
2459 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, *flags);
2461 del_timer_sync(&task->task_timer);
2463 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, *flags);
2464 task->task_flags &= ~TF_RUNNING;
2465 task->task_flags &= ~TF_STOP;
2468 static void transport_stop_all_task_timers(struct se_cmd *cmd)
2470 struct se_task *task = NULL, *task_tmp;
2471 unsigned long flags;
2473 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2474 list_for_each_entry_safe(task, task_tmp,
2475 &T_TASK(cmd)->t_task_list, t_list)
2476 __transport_stop_task_timer(task, &flags);
2477 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2480 static inline int transport_tcq_window_closed(struct se_device *dev)
2482 if (dev->dev_tcq_window_closed++ <
2483 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
2484 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
2485 } else
2486 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
2488 wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
2489 return 0;
2493 * Called from Fabric Module context from transport_execute_tasks()
2495 * The return of this function determins if the tasks from struct se_cmd
2496 * get added to the execution queue in transport_execute_tasks(),
2497 * or are added to the delayed or ordered lists here.
2499 static inline int transport_execute_task_attr(struct se_cmd *cmd)
2501 if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2502 return 1;
2504 * Check for the existance of HEAD_OF_QUEUE, and if true return 1
2505 * to allow the passed struct se_cmd list of tasks to the front of the list.
2507 if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
2508 atomic_inc(&SE_DEV(cmd)->dev_hoq_count);
2509 smp_mb__after_atomic_inc();
2510 DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
2511 " 0x%02x, se_ordered_id: %u\n",
2512 T_TASK(cmd)->t_task_cdb[0],
2513 cmd->se_ordered_id);
2514 return 1;
2515 } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
2516 spin_lock(&SE_DEV(cmd)->ordered_cmd_lock);
2517 list_add_tail(&cmd->se_ordered_list,
2518 &SE_DEV(cmd)->ordered_cmd_list);
2519 spin_unlock(&SE_DEV(cmd)->ordered_cmd_lock);
2521 atomic_inc(&SE_DEV(cmd)->dev_ordered_sync);
2522 smp_mb__after_atomic_inc();
2524 DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
2525 " list, se_ordered_id: %u\n",
2526 T_TASK(cmd)->t_task_cdb[0],
2527 cmd->se_ordered_id);
2529 * Add ORDERED command to tail of execution queue if
2530 * no other older commands exist that need to be
2531 * completed first.
2533 if (!(atomic_read(&SE_DEV(cmd)->simple_cmds)))
2534 return 1;
2535 } else {
2537 * For SIMPLE and UNTAGGED Task Attribute commands
2539 atomic_inc(&SE_DEV(cmd)->simple_cmds);
2540 smp_mb__after_atomic_inc();
2543 * Otherwise if one or more outstanding ORDERED task attribute exist,
2544 * add the dormant task(s) built for the passed struct se_cmd to the
2545 * execution queue and become in Active state for this struct se_device.
2547 if (atomic_read(&SE_DEV(cmd)->dev_ordered_sync) != 0) {
2549 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2550 * will be drained upon competion of HEAD_OF_QUEUE task.
2552 spin_lock(&SE_DEV(cmd)->delayed_cmd_lock);
2553 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2554 list_add_tail(&cmd->se_delayed_list,
2555 &SE_DEV(cmd)->delayed_cmd_list);
2556 spin_unlock(&SE_DEV(cmd)->delayed_cmd_lock);
2558 DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
2559 " delayed CMD list, se_ordered_id: %u\n",
2560 T_TASK(cmd)->t_task_cdb[0], cmd->sam_task_attr,
2561 cmd->se_ordered_id);
2563 * Return zero to let transport_execute_tasks() know
2564 * not to add the delayed tasks to the execution list.
2566 return 0;
2569 * Otherwise, no ORDERED task attributes exist..
2571 return 1;
2575 * Called from fabric module context in transport_generic_new_cmd() and
2576 * transport_generic_process_write()
2578 static int transport_execute_tasks(struct se_cmd *cmd)
2580 int add_tasks;
2582 if (!(cmd->se_cmd_flags & SCF_SE_DISABLE_ONLINE_CHECK)) {
2583 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2584 cmd->transport_error_status =
2585 PYX_TRANSPORT_LU_COMM_FAILURE;
2586 transport_generic_request_failure(cmd, NULL, 0, 1);
2587 return 0;
2591 * Call transport_cmd_check_stop() to see if a fabric exception
2592 * has occured that prevents execution.
2594 if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
2596 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2597 * attribute for the tasks of the received struct se_cmd CDB
2599 add_tasks = transport_execute_task_attr(cmd);
2600 if (add_tasks == 0)
2601 goto execute_tasks;
2603 * This calls transport_add_tasks_from_cmd() to handle
2604 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2605 * (if enabled) in __transport_add_task_to_execute_queue() and
2606 * transport_add_task_check_sam_attr().
2608 transport_add_tasks_from_cmd(cmd);
2611 * Kick the execution queue for the cmd associated struct se_device
2612 * storage object.
2614 execute_tasks:
2615 __transport_execute_tasks(SE_DEV(cmd));
2616 return 0;
2620 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2621 * from struct se_device->execute_task_list and
2623 * Called from transport_processing_thread()
2625 static int __transport_execute_tasks(struct se_device *dev)
2627 int error;
2628 struct se_cmd *cmd = NULL;
2629 struct se_task *task;
2630 unsigned long flags;
2633 * Check if there is enough room in the device and HBA queue to send
2634 * struct se_transport_task's to the selected transport.
2636 check_depth:
2637 spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
2638 if (!(atomic_read(&dev->depth_left)) ||
2639 !(atomic_read(&SE_HBA(dev)->left_queue_depth))) {
2640 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2641 return transport_tcq_window_closed(dev);
2643 dev->dev_tcq_window_closed = 0;
2645 spin_lock(&dev->execute_task_lock);
2646 task = transport_get_task_from_execute_queue(dev);
2647 spin_unlock(&dev->execute_task_lock);
2649 if (!task) {
2650 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2651 return 0;
2654 atomic_dec(&dev->depth_left);
2655 atomic_dec(&SE_HBA(dev)->left_queue_depth);
2656 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2658 cmd = TASK_CMD(task);
2660 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2661 atomic_set(&task->task_active, 1);
2662 atomic_set(&task->task_sent, 1);
2663 atomic_inc(&T_TASK(cmd)->t_task_cdbs_sent);
2665 if (atomic_read(&T_TASK(cmd)->t_task_cdbs_sent) ==
2666 T_TASK(cmd)->t_task_cdbs)
2667 atomic_set(&cmd->transport_sent, 1);
2669 transport_start_task_timer(task);
2670 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2672 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2673 * to grab REPORT_LUNS CDBs before they hit the
2674 * struct se_subsystem_api->do_task() caller below.
2676 if (cmd->transport_emulate_cdb) {
2677 error = cmd->transport_emulate_cdb(cmd);
2678 if (error != 0) {
2679 cmd->transport_error_status = error;
2680 atomic_set(&task->task_active, 0);
2681 atomic_set(&cmd->transport_sent, 0);
2682 transport_stop_tasks_for_cmd(cmd);
2683 transport_generic_request_failure(cmd, dev, 0, 1);
2684 goto check_depth;
2687 * Handle the successful completion for transport_emulate_cdb()
2688 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2689 * Otherwise the caller is expected to complete the task with
2690 * proper status.
2692 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2693 cmd->scsi_status = SAM_STAT_GOOD;
2694 task->task_scsi_status = GOOD;
2695 transport_complete_task(task, 1);
2697 } else {
2699 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2700 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2701 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2702 * LUN emulation code.
2704 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2705 * call ->do_task() directly and let the underlying TCM subsystem plugin
2706 * code handle the CDB emulation.
2708 if ((TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2709 (!(TASK_CMD(task)->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2710 error = transport_emulate_control_cdb(task);
2711 else
2712 error = TRANSPORT(dev)->do_task(task);
2714 if (error != 0) {
2715 cmd->transport_error_status = error;
2716 atomic_set(&task->task_active, 0);
2717 atomic_set(&cmd->transport_sent, 0);
2718 transport_stop_tasks_for_cmd(cmd);
2719 transport_generic_request_failure(cmd, dev, 0, 1);
2723 goto check_depth;
2725 return 0;
2728 void transport_new_cmd_failure(struct se_cmd *se_cmd)
2730 unsigned long flags;
2732 * Any unsolicited data will get dumped for failed command inside of
2733 * the fabric plugin
2735 spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
2736 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2737 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2738 spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
2740 CMD_TFO(se_cmd)->new_cmd_failure(se_cmd);
2743 static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
2745 static inline u32 transport_get_sectors_6(
2746 unsigned char *cdb,
2747 struct se_cmd *cmd,
2748 int *ret)
2750 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2753 * Assume TYPE_DISK for non struct se_device objects.
2754 * Use 8-bit sector value.
2756 if (!dev)
2757 goto type_disk;
2760 * Use 24-bit allocation length for TYPE_TAPE.
2762 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
2763 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2766 * Everything else assume TYPE_DISK Sector CDB location.
2767 * Use 8-bit sector value.
2769 type_disk:
2770 return (u32)cdb[4];
2773 static inline u32 transport_get_sectors_10(
2774 unsigned char *cdb,
2775 struct se_cmd *cmd,
2776 int *ret)
2778 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2781 * Assume TYPE_DISK for non struct se_device objects.
2782 * Use 16-bit sector value.
2784 if (!dev)
2785 goto type_disk;
2788 * XXX_10 is not defined in SSC, throw an exception
2790 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
2791 *ret = -1;
2792 return 0;
2796 * Everything else assume TYPE_DISK Sector CDB location.
2797 * Use 16-bit sector value.
2799 type_disk:
2800 return (u32)(cdb[7] << 8) + cdb[8];
2803 static inline u32 transport_get_sectors_12(
2804 unsigned char *cdb,
2805 struct se_cmd *cmd,
2806 int *ret)
2808 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2811 * Assume TYPE_DISK for non struct se_device objects.
2812 * Use 32-bit sector value.
2814 if (!dev)
2815 goto type_disk;
2818 * XXX_12 is not defined in SSC, throw an exception
2820 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
2821 *ret = -1;
2822 return 0;
2826 * Everything else assume TYPE_DISK Sector CDB location.
2827 * Use 32-bit sector value.
2829 type_disk:
2830 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2833 static inline u32 transport_get_sectors_16(
2834 unsigned char *cdb,
2835 struct se_cmd *cmd,
2836 int *ret)
2838 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2841 * Assume TYPE_DISK for non struct se_device objects.
2842 * Use 32-bit sector value.
2844 if (!dev)
2845 goto type_disk;
2848 * Use 24-bit allocation length for TYPE_TAPE.
2850 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
2851 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2853 type_disk:
2854 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2855 (cdb[12] << 8) + cdb[13];
2859 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2861 static inline u32 transport_get_sectors_32(
2862 unsigned char *cdb,
2863 struct se_cmd *cmd,
2864 int *ret)
2867 * Assume TYPE_DISK for non struct se_device objects.
2868 * Use 32-bit sector value.
2870 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2871 (cdb[30] << 8) + cdb[31];
2875 static inline u32 transport_get_size(
2876 u32 sectors,
2877 unsigned char *cdb,
2878 struct se_cmd *cmd)
2880 struct se_device *dev = SE_DEV(cmd);
2882 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
2883 if (cdb[1] & 1) { /* sectors */
2884 return DEV_ATTRIB(dev)->block_size * sectors;
2885 } else /* bytes */
2886 return sectors;
2888 #if 0
2889 printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
2890 " %s object\n", DEV_ATTRIB(dev)->block_size, sectors,
2891 DEV_ATTRIB(dev)->block_size * sectors,
2892 TRANSPORT(dev)->name);
2893 #endif
2894 return DEV_ATTRIB(dev)->block_size * sectors;
2897 unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
2899 unsigned char result = 0;
2901 * MSB
2903 if ((val[0] >= 'a') && (val[0] <= 'f'))
2904 result = ((val[0] - 'a' + 10) & 0xf) << 4;
2905 else
2906 if ((val[0] >= 'A') && (val[0] <= 'F'))
2907 result = ((val[0] - 'A' + 10) & 0xf) << 4;
2908 else /* digit */
2909 result = ((val[0] - '0') & 0xf) << 4;
2911 * LSB
2913 if ((val[1] >= 'a') && (val[1] <= 'f'))
2914 result |= ((val[1] - 'a' + 10) & 0xf);
2915 else
2916 if ((val[1] >= 'A') && (val[1] <= 'F'))
2917 result |= ((val[1] - 'A' + 10) & 0xf);
2918 else /* digit */
2919 result |= ((val[1] - '0') & 0xf);
2921 return result;
2923 EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
2925 static void transport_xor_callback(struct se_cmd *cmd)
2927 unsigned char *buf, *addr;
2928 struct se_mem *se_mem;
2929 unsigned int offset;
2930 int i;
2932 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2934 * 1) read the specified logical block(s);
2935 * 2) transfer logical blocks from the data-out buffer;
2936 * 3) XOR the logical blocks transferred from the data-out buffer with
2937 * the logical blocks read, storing the resulting XOR data in a buffer;
2938 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2939 * blocks transferred from the data-out buffer; and
2940 * 5) transfer the resulting XOR data to the data-in buffer.
2942 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2943 if (!(buf)) {
2944 printk(KERN_ERR "Unable to allocate xor_callback buf\n");
2945 return;
2948 * Copy the scatterlist WRITE buffer located at T_TASK(cmd)->t_mem_list
2949 * into the locally allocated *buf
2951 transport_memcpy_se_mem_read_contig(cmd, buf, T_TASK(cmd)->t_mem_list);
2953 * Now perform the XOR against the BIDI read memory located at
2954 * T_TASK(cmd)->t_mem_bidi_list
2957 offset = 0;
2958 list_for_each_entry(se_mem, T_TASK(cmd)->t_mem_bidi_list, se_list) {
2959 addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
2960 if (!(addr))
2961 goto out;
2963 for (i = 0; i < se_mem->se_len; i++)
2964 *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
2966 offset += se_mem->se_len;
2967 kunmap_atomic(addr, KM_USER0);
2969 out:
2970 kfree(buf);
2974 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2976 static int transport_get_sense_data(struct se_cmd *cmd)
2978 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2979 struct se_device *dev;
2980 struct se_task *task = NULL, *task_tmp;
2981 unsigned long flags;
2982 u32 offset = 0;
2984 if (!SE_LUN(cmd)) {
2985 printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
2986 return -1;
2988 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2989 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2990 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2991 return 0;
2994 list_for_each_entry_safe(task, task_tmp,
2995 &T_TASK(cmd)->t_task_list, t_list) {
2997 if (!task->task_sense)
2998 continue;
3000 dev = task->se_dev;
3001 if (!(dev))
3002 continue;
3004 if (!TRANSPORT(dev)->get_sense_buffer) {
3005 printk(KERN_ERR "TRANSPORT(dev)->get_sense_buffer"
3006 " is NULL\n");
3007 continue;
3010 sense_buffer = TRANSPORT(dev)->get_sense_buffer(task);
3011 if (!(sense_buffer)) {
3012 printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
3013 " sense buffer for task with sense\n",
3014 CMD_TFO(cmd)->get_task_tag(cmd), task->task_no);
3015 continue;
3017 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
3019 offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
3020 TRANSPORT_SENSE_BUFFER);
3022 memcpy((void *)&buffer[offset], (void *)sense_buffer,
3023 TRANSPORT_SENSE_BUFFER);
3024 cmd->scsi_status = task->task_scsi_status;
3025 /* Automatically padded */
3026 cmd->scsi_sense_length =
3027 (TRANSPORT_SENSE_BUFFER + offset);
3029 printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
3030 " and sense\n",
3031 dev->se_hba->hba_id, TRANSPORT(dev)->name,
3032 cmd->scsi_status);
3033 return 0;
3035 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
3037 return -1;
3040 static int transport_allocate_resources(struct se_cmd *cmd)
3042 u32 length = cmd->data_length;
3044 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3045 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB))
3046 return transport_generic_get_mem(cmd, length, PAGE_SIZE);
3047 else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB)
3048 return transport_generic_allocate_buf(cmd, length);
3049 else
3050 return 0;
3053 static int
3054 transport_handle_reservation_conflict(struct se_cmd *cmd)
3056 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3057 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3058 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
3059 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
3061 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
3062 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
3063 * CONFLICT STATUS.
3065 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
3067 if (SE_SESS(cmd) &&
3068 DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
3069 core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
3070 cmd->orig_fe_lun, 0x2C,
3071 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
3072 return -2;
3075 /* transport_generic_cmd_sequencer():
3077 * Generic Command Sequencer that should work for most DAS transport
3078 * drivers.
3080 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
3081 * RX Thread.
3083 * FIXME: Need to support other SCSI OPCODES where as well.
3085 static int transport_generic_cmd_sequencer(
3086 struct se_cmd *cmd,
3087 unsigned char *cdb)
3089 struct se_device *dev = SE_DEV(cmd);
3090 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
3091 int ret = 0, sector_ret = 0, passthrough;
3092 u32 sectors = 0, size = 0, pr_reg_type = 0;
3093 u16 service_action;
3094 u8 alua_ascq = 0;
3096 * Check for an existing UNIT ATTENTION condition
3098 if (core_scsi3_ua_check(cmd, cdb) < 0) {
3099 cmd->transport_wait_for_tasks =
3100 &transport_nop_wait_for_tasks;
3101 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3102 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
3103 return -2;
3106 * Check status of Asymmetric Logical Unit Assignment port
3108 ret = T10_ALUA(su_dev)->alua_state_check(cmd, cdb, &alua_ascq);
3109 if (ret != 0) {
3110 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3112 * Set SCSI additional sense code (ASC) to 'LUN Not Accessable';
3113 * The ALUA additional sense code qualifier (ASCQ) is determined
3114 * by the ALUA primary or secondary access state..
3116 if (ret > 0) {
3117 #if 0
3118 printk(KERN_INFO "[%s]: ALUA TG Port not available,"
3119 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
3120 CMD_TFO(cmd)->get_fabric_name(), alua_ascq);
3121 #endif
3122 transport_set_sense_codes(cmd, 0x04, alua_ascq);
3123 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3124 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
3125 return -2;
3127 goto out_invalid_cdb_field;
3130 * Check status for SPC-3 Persistent Reservations
3132 if (T10_PR_OPS(su_dev)->t10_reservation_check(cmd, &pr_reg_type) != 0) {
3133 if (T10_PR_OPS(su_dev)->t10_seq_non_holder(
3134 cmd, cdb, pr_reg_type) != 0)
3135 return transport_handle_reservation_conflict(cmd);
3137 * This means the CDB is allowed for the SCSI Initiator port
3138 * when said port is *NOT* holding the legacy SPC-2 or
3139 * SPC-3 Persistent Reservation.
3143 switch (cdb[0]) {
3144 case READ_6:
3145 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
3146 if (sector_ret)
3147 goto out_unsupported_cdb;
3148 size = transport_get_size(sectors, cdb, cmd);
3149 cmd->transport_split_cdb = &split_cdb_XX_6;
3150 T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
3151 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3152 break;
3153 case READ_10:
3154 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3155 if (sector_ret)
3156 goto out_unsupported_cdb;
3157 size = transport_get_size(sectors, cdb, cmd);
3158 cmd->transport_split_cdb = &split_cdb_XX_10;
3159 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3160 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3161 break;
3162 case READ_12:
3163 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
3164 if (sector_ret)
3165 goto out_unsupported_cdb;
3166 size = transport_get_size(sectors, cdb, cmd);
3167 cmd->transport_split_cdb = &split_cdb_XX_12;
3168 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3169 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3170 break;
3171 case READ_16:
3172 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3173 if (sector_ret)
3174 goto out_unsupported_cdb;
3175 size = transport_get_size(sectors, cdb, cmd);
3176 cmd->transport_split_cdb = &split_cdb_XX_16;
3177 T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
3178 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3179 break;
3180 case WRITE_6:
3181 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
3182 if (sector_ret)
3183 goto out_unsupported_cdb;
3184 size = transport_get_size(sectors, cdb, cmd);
3185 cmd->transport_split_cdb = &split_cdb_XX_6;
3186 T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
3187 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3188 break;
3189 case WRITE_10:
3190 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3191 if (sector_ret)
3192 goto out_unsupported_cdb;
3193 size = transport_get_size(sectors, cdb, cmd);
3194 cmd->transport_split_cdb = &split_cdb_XX_10;
3195 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3196 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3197 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3198 break;
3199 case WRITE_12:
3200 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
3201 if (sector_ret)
3202 goto out_unsupported_cdb;
3203 size = transport_get_size(sectors, cdb, cmd);
3204 cmd->transport_split_cdb = &split_cdb_XX_12;
3205 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3206 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3207 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3208 break;
3209 case WRITE_16:
3210 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3211 if (sector_ret)
3212 goto out_unsupported_cdb;
3213 size = transport_get_size(sectors, cdb, cmd);
3214 cmd->transport_split_cdb = &split_cdb_XX_16;
3215 T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
3216 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3217 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3218 break;
3219 case XDWRITEREAD_10:
3220 if ((cmd->data_direction != DMA_TO_DEVICE) ||
3221 !(T_TASK(cmd)->t_tasks_bidi))
3222 goto out_invalid_cdb_field;
3223 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3224 if (sector_ret)
3225 goto out_unsupported_cdb;
3226 size = transport_get_size(sectors, cdb, cmd);
3227 cmd->transport_split_cdb = &split_cdb_XX_10;
3228 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3229 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3230 passthrough = (TRANSPORT(dev)->transport_type ==
3231 TRANSPORT_PLUGIN_PHBA_PDEV);
3233 * Skip the remaining assignments for TCM/PSCSI passthrough
3235 if (passthrough)
3236 break;
3238 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3240 cmd->transport_complete_callback = &transport_xor_callback;
3241 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3242 break;
3243 case VARIABLE_LENGTH_CMD:
3244 service_action = get_unaligned_be16(&cdb[8]);
3246 * Determine if this is TCM/PSCSI device and we should disable
3247 * internal emulation for this CDB.
3249 passthrough = (TRANSPORT(dev)->transport_type ==
3250 TRANSPORT_PLUGIN_PHBA_PDEV);
3252 switch (service_action) {
3253 case XDWRITEREAD_32:
3254 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3255 if (sector_ret)
3256 goto out_unsupported_cdb;
3257 size = transport_get_size(sectors, cdb, cmd);
3259 * Use WRITE_32 and READ_32 opcodes for the emulated
3260 * XDWRITE_READ_32 logic.
3262 cmd->transport_split_cdb = &split_cdb_XX_32;
3263 T_TASK(cmd)->t_task_lba = transport_lba_64_ext(cdb);
3264 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3267 * Skip the remaining assignments for TCM/PSCSI passthrough
3269 if (passthrough)
3270 break;
3273 * Setup BIDI XOR callback to be run during
3274 * transport_generic_complete_ok()
3276 cmd->transport_complete_callback = &transport_xor_callback;
3277 T_TASK(cmd)->t_tasks_fua = (cdb[10] & 0x8);
3278 break;
3279 case WRITE_SAME_32:
3280 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3281 if (sector_ret)
3282 goto out_unsupported_cdb;
3283 size = transport_get_size(sectors, cdb, cmd);
3284 T_TASK(cmd)->t_task_lba = get_unaligned_be64(&cdb[12]);
3285 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3288 * Skip the remaining assignments for TCM/PSCSI passthrough
3290 if (passthrough)
3291 break;
3293 if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
3294 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3295 " bits not supported for Block Discard"
3296 " Emulation\n");
3297 goto out_invalid_cdb_field;
3300 * Currently for the emulated case we only accept
3301 * tpws with the UNMAP=1 bit set.
3303 if (!(cdb[10] & 0x08)) {
3304 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
3305 " supported for Block Discard Emulation\n");
3306 goto out_invalid_cdb_field;
3308 break;
3309 default:
3310 printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
3311 " 0x%04x not supported\n", service_action);
3312 goto out_unsupported_cdb;
3314 break;
3315 case 0xa3:
3316 if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
3317 /* MAINTENANCE_IN from SCC-2 */
3319 * Check for emulated MI_REPORT_TARGET_PGS.
3321 if (cdb[1] == MI_REPORT_TARGET_PGS) {
3322 cmd->transport_emulate_cdb =
3323 (T10_ALUA(su_dev)->alua_type ==
3324 SPC3_ALUA_EMULATED) ?
3325 &core_emulate_report_target_port_groups :
3326 NULL;
3328 size = (cdb[6] << 24) | (cdb[7] << 16) |
3329 (cdb[8] << 8) | cdb[9];
3330 } else {
3331 /* GPCMD_SEND_KEY from multi media commands */
3332 size = (cdb[8] << 8) + cdb[9];
3334 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3335 break;
3336 case MODE_SELECT:
3337 size = cdb[4];
3338 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3339 break;
3340 case MODE_SELECT_10:
3341 size = (cdb[7] << 8) + cdb[8];
3342 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3343 break;
3344 case MODE_SENSE:
3345 size = cdb[4];
3346 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3347 break;
3348 case MODE_SENSE_10:
3349 case GPCMD_READ_BUFFER_CAPACITY:
3350 case GPCMD_SEND_OPC:
3351 case LOG_SELECT:
3352 case LOG_SENSE:
3353 size = (cdb[7] << 8) + cdb[8];
3354 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3355 break;
3356 case READ_BLOCK_LIMITS:
3357 size = READ_BLOCK_LEN;
3358 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3359 break;
3360 case GPCMD_GET_CONFIGURATION:
3361 case GPCMD_READ_FORMAT_CAPACITIES:
3362 case GPCMD_READ_DISC_INFO:
3363 case GPCMD_READ_TRACK_RZONE_INFO:
3364 size = (cdb[7] << 8) + cdb[8];
3365 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3366 break;
3367 case PERSISTENT_RESERVE_IN:
3368 case PERSISTENT_RESERVE_OUT:
3369 cmd->transport_emulate_cdb =
3370 (T10_RES(su_dev)->res_type ==
3371 SPC3_PERSISTENT_RESERVATIONS) ?
3372 &core_scsi3_emulate_pr : NULL;
3373 size = (cdb[7] << 8) + cdb[8];
3374 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3375 break;
3376 case GPCMD_MECHANISM_STATUS:
3377 case GPCMD_READ_DVD_STRUCTURE:
3378 size = (cdb[8] << 8) + cdb[9];
3379 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3380 break;
3381 case READ_POSITION:
3382 size = READ_POSITION_LEN;
3383 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3384 break;
3385 case 0xa4:
3386 if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
3387 /* MAINTENANCE_OUT from SCC-2
3389 * Check for emulated MO_SET_TARGET_PGS.
3391 if (cdb[1] == MO_SET_TARGET_PGS) {
3392 cmd->transport_emulate_cdb =
3393 (T10_ALUA(su_dev)->alua_type ==
3394 SPC3_ALUA_EMULATED) ?
3395 &core_emulate_set_target_port_groups :
3396 NULL;
3399 size = (cdb[6] << 24) | (cdb[7] << 16) |
3400 (cdb[8] << 8) | cdb[9];
3401 } else {
3402 /* GPCMD_REPORT_KEY from multi media commands */
3403 size = (cdb[8] << 8) + cdb[9];
3405 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3406 break;
3407 case INQUIRY:
3408 size = (cdb[3] << 8) + cdb[4];
3410 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3411 * See spc4r17 section 5.3
3413 if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3414 cmd->sam_task_attr = TASK_ATTR_HOQ;
3415 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3416 break;
3417 case READ_BUFFER:
3418 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3419 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3420 break;
3421 case READ_CAPACITY:
3422 size = READ_CAP_LEN;
3423 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3424 break;
3425 case READ_MEDIA_SERIAL_NUMBER:
3426 case SECURITY_PROTOCOL_IN:
3427 case SECURITY_PROTOCOL_OUT:
3428 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3429 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3430 break;
3431 case SERVICE_ACTION_IN:
3432 case ACCESS_CONTROL_IN:
3433 case ACCESS_CONTROL_OUT:
3434 case EXTENDED_COPY:
3435 case READ_ATTRIBUTE:
3436 case RECEIVE_COPY_RESULTS:
3437 case WRITE_ATTRIBUTE:
3438 size = (cdb[10] << 24) | (cdb[11] << 16) |
3439 (cdb[12] << 8) | cdb[13];
3440 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3441 break;
3442 case RECEIVE_DIAGNOSTIC:
3443 case SEND_DIAGNOSTIC:
3444 size = (cdb[3] << 8) | cdb[4];
3445 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3446 break;
3447 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3448 #if 0
3449 case GPCMD_READ_CD:
3450 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3451 size = (2336 * sectors);
3452 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3453 break;
3454 #endif
3455 case READ_TOC:
3456 size = cdb[8];
3457 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3458 break;
3459 case REQUEST_SENSE:
3460 size = cdb[4];
3461 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3462 break;
3463 case READ_ELEMENT_STATUS:
3464 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
3465 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3466 break;
3467 case WRITE_BUFFER:
3468 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3469 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3470 break;
3471 case RESERVE:
3472 case RESERVE_10:
3474 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3475 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3477 if (cdb[0] == RESERVE_10)
3478 size = (cdb[7] << 8) | cdb[8];
3479 else
3480 size = cmd->data_length;
3483 * Setup the legacy emulated handler for SPC-2 and
3484 * >= SPC-3 compatible reservation handling (CRH=1)
3485 * Otherwise, we assume the underlying SCSI logic is
3486 * is running in SPC_PASSTHROUGH, and wants reservations
3487 * emulation disabled.
3489 cmd->transport_emulate_cdb =
3490 (T10_RES(su_dev)->res_type !=
3491 SPC_PASSTHROUGH) ?
3492 &core_scsi2_emulate_crh : NULL;
3493 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3494 break;
3495 case RELEASE:
3496 case RELEASE_10:
3498 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3499 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3501 if (cdb[0] == RELEASE_10)
3502 size = (cdb[7] << 8) | cdb[8];
3503 else
3504 size = cmd->data_length;
3506 cmd->transport_emulate_cdb =
3507 (T10_RES(su_dev)->res_type !=
3508 SPC_PASSTHROUGH) ?
3509 &core_scsi2_emulate_crh : NULL;
3510 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3511 break;
3512 case SYNCHRONIZE_CACHE:
3513 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3515 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3517 if (cdb[0] == SYNCHRONIZE_CACHE) {
3518 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3519 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3520 } else {
3521 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3522 T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
3524 if (sector_ret)
3525 goto out_unsupported_cdb;
3527 size = transport_get_size(sectors, cdb, cmd);
3528 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3531 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3533 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3534 break;
3536 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3537 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3539 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3541 * Check to ensure that LBA + Range does not exceed past end of
3542 * device.
3544 if (transport_get_sectors(cmd) < 0)
3545 goto out_invalid_cdb_field;
3546 break;
3547 case UNMAP:
3548 size = get_unaligned_be16(&cdb[7]);
3549 passthrough = (TRANSPORT(dev)->transport_type ==
3550 TRANSPORT_PLUGIN_PHBA_PDEV);
3552 * Determine if the received UNMAP used to for direct passthrough
3553 * into Linux/SCSI with struct request via TCM/pSCSI or we are
3554 * signaling the use of internal transport_generic_unmap() emulation
3555 * for UNMAP -> Linux/BLOCK disbard with TCM/IBLOCK and TCM/FILEIO
3556 * subsystem plugin backstores.
3558 if (!(passthrough))
3559 cmd->se_cmd_flags |= SCF_EMULATE_SYNC_UNMAP;
3561 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3562 break;
3563 case WRITE_SAME_16:
3564 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3565 if (sector_ret)
3566 goto out_unsupported_cdb;
3567 size = transport_get_size(sectors, cdb, cmd);
3568 T_TASK(cmd)->t_task_lba = get_unaligned_be16(&cdb[2]);
3569 passthrough = (TRANSPORT(dev)->transport_type ==
3570 TRANSPORT_PLUGIN_PHBA_PDEV);
3572 * Determine if the received WRITE_SAME_16 is used to for direct
3573 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
3574 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
3575 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
3576 * TCM/FILEIO subsystem plugin backstores.
3578 if (!(passthrough)) {
3579 if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
3580 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3581 " bits not supported for Block Discard"
3582 " Emulation\n");
3583 goto out_invalid_cdb_field;
3586 * Currently for the emulated case we only accept
3587 * tpws with the UNMAP=1 bit set.
3589 if (!(cdb[1] & 0x08)) {
3590 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
3591 " supported for Block Discard Emulation\n");
3592 goto out_invalid_cdb_field;
3595 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3596 break;
3597 case ALLOW_MEDIUM_REMOVAL:
3598 case GPCMD_CLOSE_TRACK:
3599 case ERASE:
3600 case INITIALIZE_ELEMENT_STATUS:
3601 case GPCMD_LOAD_UNLOAD:
3602 case REZERO_UNIT:
3603 case SEEK_10:
3604 case GPCMD_SET_SPEED:
3605 case SPACE:
3606 case START_STOP:
3607 case TEST_UNIT_READY:
3608 case VERIFY:
3609 case WRITE_FILEMARKS:
3610 case MOVE_MEDIUM:
3611 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3612 break;
3613 case REPORT_LUNS:
3614 cmd->transport_emulate_cdb =
3615 &transport_core_report_lun_response;
3616 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3618 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3619 * See spc4r17 section 5.3
3621 if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3622 cmd->sam_task_attr = TASK_ATTR_HOQ;
3623 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3624 break;
3625 default:
3626 printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
3627 " 0x%02x, sending CHECK_CONDITION.\n",
3628 CMD_TFO(cmd)->get_fabric_name(), cdb[0]);
3629 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3630 goto out_unsupported_cdb;
3633 if (size != cmd->data_length) {
3634 printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
3635 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3636 " 0x%02x\n", CMD_TFO(cmd)->get_fabric_name(),
3637 cmd->data_length, size, cdb[0]);
3639 cmd->cmd_spdtl = size;
3641 if (cmd->data_direction == DMA_TO_DEVICE) {
3642 printk(KERN_ERR "Rejecting underflow/overflow"
3643 " WRITE data\n");
3644 goto out_invalid_cdb_field;
3647 * Reject READ_* or WRITE_* with overflow/underflow for
3648 * type SCF_SCSI_DATA_SG_IO_CDB.
3650 if (!(ret) && (DEV_ATTRIB(dev)->block_size != 512)) {
3651 printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
3652 " CDB on non 512-byte sector setup subsystem"
3653 " plugin: %s\n", TRANSPORT(dev)->name);
3654 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3655 goto out_invalid_cdb_field;
3658 if (size > cmd->data_length) {
3659 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3660 cmd->residual_count = (size - cmd->data_length);
3661 } else {
3662 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3663 cmd->residual_count = (cmd->data_length - size);
3665 cmd->data_length = size;
3668 transport_set_supported_SAM_opcode(cmd);
3669 return ret;
3671 out_unsupported_cdb:
3672 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3673 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3674 return -2;
3675 out_invalid_cdb_field:
3676 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3677 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3678 return -2;
3681 static inline void transport_release_tasks(struct se_cmd *);
3684 * This function will copy a contiguous *src buffer into a destination
3685 * struct scatterlist array.
3687 static void transport_memcpy_write_contig(
3688 struct se_cmd *cmd,
3689 struct scatterlist *sg_d,
3690 unsigned char *src)
3692 u32 i = 0, length = 0, total_length = cmd->data_length;
3693 void *dst;
3695 while (total_length) {
3696 length = sg_d[i].length;
3698 if (length > total_length)
3699 length = total_length;
3701 dst = sg_virt(&sg_d[i]);
3703 memcpy(dst, src, length);
3705 if (!(total_length -= length))
3706 return;
3708 src += length;
3709 i++;
3714 * This function will copy a struct scatterlist array *sg_s into a destination
3715 * contiguous *dst buffer.
3717 static void transport_memcpy_read_contig(
3718 struct se_cmd *cmd,
3719 unsigned char *dst,
3720 struct scatterlist *sg_s)
3722 u32 i = 0, length = 0, total_length = cmd->data_length;
3723 void *src;
3725 while (total_length) {
3726 length = sg_s[i].length;
3728 if (length > total_length)
3729 length = total_length;
3731 src = sg_virt(&sg_s[i]);
3733 memcpy(dst, src, length);
3735 if (!(total_length -= length))
3736 return;
3738 dst += length;
3739 i++;
3743 static void transport_memcpy_se_mem_read_contig(
3744 struct se_cmd *cmd,
3745 unsigned char *dst,
3746 struct list_head *se_mem_list)
3748 struct se_mem *se_mem;
3749 void *src;
3750 u32 length = 0, total_length = cmd->data_length;
3752 list_for_each_entry(se_mem, se_mem_list, se_list) {
3753 length = se_mem->se_len;
3755 if (length > total_length)
3756 length = total_length;
3758 src = page_address(se_mem->se_page) + se_mem->se_off;
3760 memcpy(dst, src, length);
3762 if (!(total_length -= length))
3763 return;
3765 dst += length;
3770 * Called from transport_generic_complete_ok() and
3771 * transport_generic_request_failure() to determine which dormant/delayed
3772 * and ordered cmds need to have their tasks added to the execution queue.
3774 static void transport_complete_task_attr(struct se_cmd *cmd)
3776 struct se_device *dev = SE_DEV(cmd);
3777 struct se_cmd *cmd_p, *cmd_tmp;
3778 int new_active_tasks = 0;
3780 if (cmd->sam_task_attr == TASK_ATTR_SIMPLE) {
3781 atomic_dec(&dev->simple_cmds);
3782 smp_mb__after_atomic_dec();
3783 dev->dev_cur_ordered_id++;
3784 DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
3785 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3786 cmd->se_ordered_id);
3787 } else if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
3788 atomic_dec(&dev->dev_hoq_count);
3789 smp_mb__after_atomic_dec();
3790 dev->dev_cur_ordered_id++;
3791 DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
3792 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3793 cmd->se_ordered_id);
3794 } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
3795 spin_lock(&dev->ordered_cmd_lock);
3796 list_del(&cmd->se_ordered_list);
3797 atomic_dec(&dev->dev_ordered_sync);
3798 smp_mb__after_atomic_dec();
3799 spin_unlock(&dev->ordered_cmd_lock);
3801 dev->dev_cur_ordered_id++;
3802 DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
3803 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3806 * Process all commands up to the last received
3807 * ORDERED task attribute which requires another blocking
3808 * boundary
3810 spin_lock(&dev->delayed_cmd_lock);
3811 list_for_each_entry_safe(cmd_p, cmd_tmp,
3812 &dev->delayed_cmd_list, se_delayed_list) {
3814 list_del(&cmd_p->se_delayed_list);
3815 spin_unlock(&dev->delayed_cmd_lock);
3817 DEBUG_STA("Calling add_tasks() for"
3818 " cmd_p: 0x%02x Task Attr: 0x%02x"
3819 " Dormant -> Active, se_ordered_id: %u\n",
3820 T_TASK(cmd_p)->t_task_cdb[0],
3821 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3823 transport_add_tasks_from_cmd(cmd_p);
3824 new_active_tasks++;
3826 spin_lock(&dev->delayed_cmd_lock);
3827 if (cmd_p->sam_task_attr == TASK_ATTR_ORDERED)
3828 break;
3830 spin_unlock(&dev->delayed_cmd_lock);
3832 * If new tasks have become active, wake up the transport thread
3833 * to do the processing of the Active tasks.
3835 if (new_active_tasks != 0)
3836 wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
3839 static void transport_generic_complete_ok(struct se_cmd *cmd)
3841 int reason = 0;
3843 * Check if we need to move delayed/dormant tasks from cmds on the
3844 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3845 * Attribute.
3847 if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3848 transport_complete_task_attr(cmd);
3850 * Check if we need to retrieve a sense buffer from
3851 * the struct se_cmd in question.
3853 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3854 if (transport_get_sense_data(cmd) < 0)
3855 reason = TCM_NON_EXISTENT_LUN;
3858 * Only set when an struct se_task->task_scsi_status returned
3859 * a non GOOD status.
3861 if (cmd->scsi_status) {
3862 transport_send_check_condition_and_sense(
3863 cmd, reason, 1);
3864 transport_lun_remove_cmd(cmd);
3865 transport_cmd_check_stop_to_fabric(cmd);
3866 return;
3870 * Check for a callback, used by amoungst other things
3871 * XDWRITE_READ_10 emulation.
3873 if (cmd->transport_complete_callback)
3874 cmd->transport_complete_callback(cmd);
3876 switch (cmd->data_direction) {
3877 case DMA_FROM_DEVICE:
3878 spin_lock(&cmd->se_lun->lun_sep_lock);
3879 if (SE_LUN(cmd)->lun_sep) {
3880 SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
3881 cmd->data_length;
3883 spin_unlock(&cmd->se_lun->lun_sep_lock);
3885 * If enabled by TCM fabirc module pre-registered SGL
3886 * memory, perform the memcpy() from the TCM internal
3887 * contigious buffer back to the original SGL.
3889 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
3890 transport_memcpy_write_contig(cmd,
3891 T_TASK(cmd)->t_task_pt_sgl,
3892 T_TASK(cmd)->t_task_buf);
3894 CMD_TFO(cmd)->queue_data_in(cmd);
3895 break;
3896 case DMA_TO_DEVICE:
3897 spin_lock(&cmd->se_lun->lun_sep_lock);
3898 if (SE_LUN(cmd)->lun_sep) {
3899 SE_LUN(cmd)->lun_sep->sep_stats.rx_data_octets +=
3900 cmd->data_length;
3902 spin_unlock(&cmd->se_lun->lun_sep_lock);
3904 * Check if we need to send READ payload for BIDI-COMMAND
3906 if (T_TASK(cmd)->t_mem_bidi_list != NULL) {
3907 spin_lock(&cmd->se_lun->lun_sep_lock);
3908 if (SE_LUN(cmd)->lun_sep) {
3909 SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
3910 cmd->data_length;
3912 spin_unlock(&cmd->se_lun->lun_sep_lock);
3913 CMD_TFO(cmd)->queue_data_in(cmd);
3914 break;
3916 /* Fall through for DMA_TO_DEVICE */
3917 case DMA_NONE:
3918 CMD_TFO(cmd)->queue_status(cmd);
3919 break;
3920 default:
3921 break;
3924 transport_lun_remove_cmd(cmd);
3925 transport_cmd_check_stop_to_fabric(cmd);
3928 static void transport_free_dev_tasks(struct se_cmd *cmd)
3930 struct se_task *task, *task_tmp;
3931 unsigned long flags;
3933 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
3934 list_for_each_entry_safe(task, task_tmp,
3935 &T_TASK(cmd)->t_task_list, t_list) {
3936 if (atomic_read(&task->task_active))
3937 continue;
3939 kfree(task->task_sg_bidi);
3940 kfree(task->task_sg);
3942 list_del(&task->t_list);
3944 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
3945 if (task->se_dev)
3946 TRANSPORT(task->se_dev)->free_task(task);
3947 else
3948 printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
3949 task->task_no);
3950 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
3952 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
3955 static inline void transport_free_pages(struct se_cmd *cmd)
3957 struct se_mem *se_mem, *se_mem_tmp;
3958 int free_page = 1;
3960 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3961 free_page = 0;
3962 if (cmd->se_dev->transport->do_se_mem_map)
3963 free_page = 0;
3965 if (T_TASK(cmd)->t_task_buf) {
3966 kfree(T_TASK(cmd)->t_task_buf);
3967 T_TASK(cmd)->t_task_buf = NULL;
3968 return;
3972 * Caller will handle releasing of struct se_mem.
3974 if (cmd->se_cmd_flags & SCF_CMD_PASSTHROUGH_NOALLOC)
3975 return;
3977 if (!(T_TASK(cmd)->t_tasks_se_num))
3978 return;
3980 list_for_each_entry_safe(se_mem, se_mem_tmp,
3981 T_TASK(cmd)->t_mem_list, se_list) {
3983 * We only release call __free_page(struct se_mem->se_page) when
3984 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3986 if (free_page)
3987 __free_page(se_mem->se_page);
3989 list_del(&se_mem->se_list);
3990 kmem_cache_free(se_mem_cache, se_mem);
3993 if (T_TASK(cmd)->t_mem_bidi_list && T_TASK(cmd)->t_tasks_se_bidi_num) {
3994 list_for_each_entry_safe(se_mem, se_mem_tmp,
3995 T_TASK(cmd)->t_mem_bidi_list, se_list) {
3997 * We only release call __free_page(struct se_mem->se_page) when
3998 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
4000 if (free_page)
4001 __free_page(se_mem->se_page);
4003 list_del(&se_mem->se_list);
4004 kmem_cache_free(se_mem_cache, se_mem);
4008 kfree(T_TASK(cmd)->t_mem_bidi_list);
4009 T_TASK(cmd)->t_mem_bidi_list = NULL;
4010 kfree(T_TASK(cmd)->t_mem_list);
4011 T_TASK(cmd)->t_mem_list = NULL;
4012 T_TASK(cmd)->t_tasks_se_num = 0;
4015 static inline void transport_release_tasks(struct se_cmd *cmd)
4017 transport_free_dev_tasks(cmd);
4020 static inline int transport_dec_and_check(struct se_cmd *cmd)
4022 unsigned long flags;
4024 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4025 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
4026 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_fe_count))) {
4027 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
4028 flags);
4029 return 1;
4033 if (atomic_read(&T_TASK(cmd)->t_se_count)) {
4034 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_se_count))) {
4035 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
4036 flags);
4037 return 1;
4040 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4042 return 0;
4045 static void transport_release_fe_cmd(struct se_cmd *cmd)
4047 unsigned long flags;
4049 if (transport_dec_and_check(cmd))
4050 return;
4052 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4053 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
4054 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4055 goto free_pages;
4057 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
4058 transport_all_task_dev_remove_state(cmd);
4059 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4061 transport_release_tasks(cmd);
4062 free_pages:
4063 transport_free_pages(cmd);
4064 transport_free_se_cmd(cmd);
4065 CMD_TFO(cmd)->release_cmd_direct(cmd);
4068 static int transport_generic_remove(
4069 struct se_cmd *cmd,
4070 int release_to_pool,
4071 int session_reinstatement)
4073 unsigned long flags;
4075 if (!(T_TASK(cmd)))
4076 goto release_cmd;
4078 if (transport_dec_and_check(cmd)) {
4079 if (session_reinstatement) {
4080 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4081 transport_all_task_dev_remove_state(cmd);
4082 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
4083 flags);
4085 return 1;
4088 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4089 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
4090 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4091 goto free_pages;
4093 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
4094 transport_all_task_dev_remove_state(cmd);
4095 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4097 transport_release_tasks(cmd);
4098 free_pages:
4099 transport_free_pages(cmd);
4101 release_cmd:
4102 if (release_to_pool) {
4103 transport_release_cmd_to_pool(cmd);
4104 } else {
4105 transport_free_se_cmd(cmd);
4106 CMD_TFO(cmd)->release_cmd_direct(cmd);
4109 return 0;
4113 * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
4114 * @cmd: Associated se_cmd descriptor
4115 * @mem: SGL style memory for TCM WRITE / READ
4116 * @sg_mem_num: Number of SGL elements
4117 * @mem_bidi_in: SGL style memory for TCM BIDI READ
4118 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
4120 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
4121 * of parameters.
4123 int transport_generic_map_mem_to_cmd(
4124 struct se_cmd *cmd,
4125 struct scatterlist *mem,
4126 u32 sg_mem_num,
4127 struct scatterlist *mem_bidi_in,
4128 u32 sg_mem_bidi_num)
4130 u32 se_mem_cnt_out = 0;
4131 int ret;
4133 if (!(mem) || !(sg_mem_num))
4134 return 0;
4136 * Passed *mem will contain a list_head containing preformatted
4137 * struct se_mem elements...
4139 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM)) {
4140 if ((mem_bidi_in) || (sg_mem_bidi_num)) {
4141 printk(KERN_ERR "SCF_CMD_PASSTHROUGH_NOALLOC not supported"
4142 " with BIDI-COMMAND\n");
4143 return -ENOSYS;
4146 T_TASK(cmd)->t_mem_list = (struct list_head *)mem;
4147 T_TASK(cmd)->t_tasks_se_num = sg_mem_num;
4148 cmd->se_cmd_flags |= SCF_CMD_PASSTHROUGH_NOALLOC;
4149 return 0;
4152 * Otherwise, assume the caller is passing a struct scatterlist
4153 * array from include/linux/scatterlist.h
4155 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
4156 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
4158 * For CDB using TCM struct se_mem linked list scatterlist memory
4159 * processed into a TCM struct se_subsystem_dev, we do the mapping
4160 * from the passed physical memory to struct se_mem->se_page here.
4162 T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
4163 if (!(T_TASK(cmd)->t_mem_list))
4164 return -ENOMEM;
4166 ret = transport_map_sg_to_mem(cmd,
4167 T_TASK(cmd)->t_mem_list, mem, &se_mem_cnt_out);
4168 if (ret < 0)
4169 return -ENOMEM;
4171 T_TASK(cmd)->t_tasks_se_num = se_mem_cnt_out;
4173 * Setup BIDI READ list of struct se_mem elements
4175 if ((mem_bidi_in) && (sg_mem_bidi_num)) {
4176 T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
4177 if (!(T_TASK(cmd)->t_mem_bidi_list)) {
4178 kfree(T_TASK(cmd)->t_mem_list);
4179 return -ENOMEM;
4181 se_mem_cnt_out = 0;
4183 ret = transport_map_sg_to_mem(cmd,
4184 T_TASK(cmd)->t_mem_bidi_list, mem_bidi_in,
4185 &se_mem_cnt_out);
4186 if (ret < 0) {
4187 kfree(T_TASK(cmd)->t_mem_list);
4188 return -ENOMEM;
4191 T_TASK(cmd)->t_tasks_se_bidi_num = se_mem_cnt_out;
4193 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
4195 } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
4196 if (mem_bidi_in || sg_mem_bidi_num) {
4197 printk(KERN_ERR "BIDI-Commands not supported using "
4198 "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
4199 return -ENOSYS;
4202 * For incoming CDBs using a contiguous buffer internall with TCM,
4203 * save the passed struct scatterlist memory. After TCM storage object
4204 * processing has completed for this struct se_cmd, TCM core will call
4205 * transport_memcpy_[write,read]_contig() as necessary from
4206 * transport_generic_complete_ok() and transport_write_pending() in order
4207 * to copy the TCM buffer to/from the original passed *mem in SGL ->
4208 * struct scatterlist format.
4210 cmd->se_cmd_flags |= SCF_PASSTHROUGH_CONTIG_TO_SG;
4211 T_TASK(cmd)->t_task_pt_sgl = mem;
4214 return 0;
4216 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
4219 static inline long long transport_dev_end_lba(struct se_device *dev)
4221 return dev->transport->get_blocks(dev) + 1;
4224 static int transport_get_sectors(struct se_cmd *cmd)
4226 struct se_device *dev = SE_DEV(cmd);
4228 T_TASK(cmd)->t_tasks_sectors =
4229 (cmd->data_length / DEV_ATTRIB(dev)->block_size);
4230 if (!(T_TASK(cmd)->t_tasks_sectors))
4231 T_TASK(cmd)->t_tasks_sectors = 1;
4233 if (TRANSPORT(dev)->get_device_type(dev) != TYPE_DISK)
4234 return 0;
4236 if ((T_TASK(cmd)->t_task_lba + T_TASK(cmd)->t_tasks_sectors) >
4237 transport_dev_end_lba(dev)) {
4238 printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
4239 " transport_dev_end_lba(): %llu\n",
4240 T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
4241 transport_dev_end_lba(dev));
4242 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4243 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
4244 return PYX_TRANSPORT_REQ_TOO_MANY_SECTORS;
4247 return 0;
4250 static int transport_new_cmd_obj(struct se_cmd *cmd)
4252 struct se_device *dev = SE_DEV(cmd);
4253 u32 task_cdbs = 0, rc;
4255 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
4256 task_cdbs++;
4257 T_TASK(cmd)->t_task_cdbs++;
4258 } else {
4259 int set_counts = 1;
4262 * Setup any BIDI READ tasks and memory from
4263 * T_TASK(cmd)->t_mem_bidi_list so the READ struct se_tasks
4264 * are queued first for the non pSCSI passthrough case.
4266 if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
4267 (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
4268 rc = transport_generic_get_cdb_count(cmd,
4269 T_TASK(cmd)->t_task_lba,
4270 T_TASK(cmd)->t_tasks_sectors,
4271 DMA_FROM_DEVICE, T_TASK(cmd)->t_mem_bidi_list,
4272 set_counts);
4273 if (!(rc)) {
4274 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4275 cmd->scsi_sense_reason =
4276 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4277 return PYX_TRANSPORT_LU_COMM_FAILURE;
4279 set_counts = 0;
4282 * Setup the tasks and memory from T_TASK(cmd)->t_mem_list
4283 * Note for BIDI transfers this will contain the WRITE payload
4285 task_cdbs = transport_generic_get_cdb_count(cmd,
4286 T_TASK(cmd)->t_task_lba,
4287 T_TASK(cmd)->t_tasks_sectors,
4288 cmd->data_direction, T_TASK(cmd)->t_mem_list,
4289 set_counts);
4290 if (!(task_cdbs)) {
4291 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4292 cmd->scsi_sense_reason =
4293 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4294 return PYX_TRANSPORT_LU_COMM_FAILURE;
4296 T_TASK(cmd)->t_task_cdbs += task_cdbs;
4298 #if 0
4299 printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
4300 " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
4301 T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
4302 T_TASK(cmd)->t_task_cdbs);
4303 #endif
4306 atomic_set(&T_TASK(cmd)->t_task_cdbs_left, task_cdbs);
4307 atomic_set(&T_TASK(cmd)->t_task_cdbs_ex_left, task_cdbs);
4308 atomic_set(&T_TASK(cmd)->t_task_cdbs_timeout_left, task_cdbs);
4309 return 0;
4312 static struct list_head *transport_init_se_mem_list(void)
4314 struct list_head *se_mem_list;
4316 se_mem_list = kzalloc(sizeof(struct list_head), GFP_KERNEL);
4317 if (!(se_mem_list)) {
4318 printk(KERN_ERR "Unable to allocate memory for se_mem_list\n");
4319 return NULL;
4321 INIT_LIST_HEAD(se_mem_list);
4323 return se_mem_list;
4326 static int
4327 transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
4329 unsigned char *buf;
4330 struct se_mem *se_mem;
4332 T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
4333 if (!(T_TASK(cmd)->t_mem_list))
4334 return -ENOMEM;
4337 * If the device uses memory mapping this is enough.
4339 if (cmd->se_dev->transport->do_se_mem_map)
4340 return 0;
4343 * Setup BIDI-COMMAND READ list of struct se_mem elements
4345 if (T_TASK(cmd)->t_tasks_bidi) {
4346 T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
4347 if (!(T_TASK(cmd)->t_mem_bidi_list)) {
4348 kfree(T_TASK(cmd)->t_mem_list);
4349 return -ENOMEM;
4353 while (length) {
4354 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4355 if (!(se_mem)) {
4356 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4357 goto out;
4360 /* #warning FIXME Allocate contigous pages for struct se_mem elements */
4361 se_mem->se_page = alloc_pages(GFP_KERNEL, 0);
4362 if (!(se_mem->se_page)) {
4363 printk(KERN_ERR "alloc_pages() failed\n");
4364 goto out;
4367 buf = kmap_atomic(se_mem->se_page, KM_IRQ0);
4368 if (!(buf)) {
4369 printk(KERN_ERR "kmap_atomic() failed\n");
4370 goto out;
4372 INIT_LIST_HEAD(&se_mem->se_list);
4373 se_mem->se_len = (length > dma_size) ? dma_size : length;
4374 memset(buf, 0, se_mem->se_len);
4375 kunmap_atomic(buf, KM_IRQ0);
4377 list_add_tail(&se_mem->se_list, T_TASK(cmd)->t_mem_list);
4378 T_TASK(cmd)->t_tasks_se_num++;
4380 DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
4381 " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
4382 se_mem->se_off);
4384 length -= se_mem->se_len;
4387 DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
4388 T_TASK(cmd)->t_tasks_se_num);
4390 return 0;
4391 out:
4392 if (se_mem)
4393 __free_pages(se_mem->se_page, 0);
4394 kmem_cache_free(se_mem_cache, se_mem);
4395 return -1;
4398 u32 transport_calc_sg_num(
4399 struct se_task *task,
4400 struct se_mem *in_se_mem,
4401 u32 task_offset)
4403 struct se_cmd *se_cmd = task->task_se_cmd;
4404 struct se_device *se_dev = SE_DEV(se_cmd);
4405 struct se_mem *se_mem = in_se_mem;
4406 struct target_core_fabric_ops *tfo = CMD_TFO(se_cmd);
4407 u32 sg_length, task_size = task->task_size, task_sg_num_padded;
4409 while (task_size != 0) {
4410 DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
4411 " se_mem->se_off(%u) task_offset(%u)\n",
4412 se_mem->se_page, se_mem->se_len,
4413 se_mem->se_off, task_offset);
4415 if (task_offset == 0) {
4416 if (task_size >= se_mem->se_len) {
4417 sg_length = se_mem->se_len;
4419 if (!(list_is_last(&se_mem->se_list,
4420 T_TASK(se_cmd)->t_mem_list)))
4421 se_mem = list_entry(se_mem->se_list.next,
4422 struct se_mem, se_list);
4423 } else {
4424 sg_length = task_size;
4425 task_size -= sg_length;
4426 goto next;
4429 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4430 sg_length, task_size);
4431 } else {
4432 if ((se_mem->se_len - task_offset) > task_size) {
4433 sg_length = task_size;
4434 task_size -= sg_length;
4435 goto next;
4436 } else {
4437 sg_length = (se_mem->se_len - task_offset);
4439 if (!(list_is_last(&se_mem->se_list,
4440 T_TASK(se_cmd)->t_mem_list)))
4441 se_mem = list_entry(se_mem->se_list.next,
4442 struct se_mem, se_list);
4445 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4446 sg_length, task_size);
4448 task_offset = 0;
4450 task_size -= sg_length;
4451 next:
4452 DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
4453 task->task_no, task_size);
4455 task->task_sg_num++;
4458 * Check if the fabric module driver is requesting that all
4459 * struct se_task->task_sg[] be chained together.. If so,
4460 * then allocate an extra padding SG entry for linking and
4461 * marking the end of the chained SGL.
4463 if (tfo->task_sg_chaining) {
4464 task_sg_num_padded = (task->task_sg_num + 1);
4465 task->task_padded_sg = 1;
4466 } else
4467 task_sg_num_padded = task->task_sg_num;
4469 task->task_sg = kzalloc(task_sg_num_padded *
4470 sizeof(struct scatterlist), GFP_KERNEL);
4471 if (!(task->task_sg)) {
4472 printk(KERN_ERR "Unable to allocate memory for"
4473 " task->task_sg\n");
4474 return 0;
4476 sg_init_table(&task->task_sg[0], task_sg_num_padded);
4478 * Setup task->task_sg_bidi for SCSI READ payload for
4479 * TCM/pSCSI passthrough if present for BIDI-COMMAND
4481 if ((T_TASK(se_cmd)->t_mem_bidi_list != NULL) &&
4482 (TRANSPORT(se_dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
4483 task->task_sg_bidi = kzalloc(task_sg_num_padded *
4484 sizeof(struct scatterlist), GFP_KERNEL);
4485 if (!(task->task_sg_bidi)) {
4486 printk(KERN_ERR "Unable to allocate memory for"
4487 " task->task_sg_bidi\n");
4488 return 0;
4490 sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
4493 * For the chaining case, setup the proper end of SGL for the
4494 * initial submission struct task into struct se_subsystem_api.
4495 * This will be cleared later by transport_do_task_sg_chain()
4497 if (task->task_padded_sg) {
4498 sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
4500 * Added the 'if' check before marking end of bi-directional
4501 * scatterlist (which gets created only in case of request
4502 * (RD + WR).
4504 if (task->task_sg_bidi)
4505 sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
4508 DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
4509 " task_sg_num_padded(%u)\n", task->task_sg_num,
4510 task_sg_num_padded);
4512 return task->task_sg_num;
4515 static inline int transport_set_tasks_sectors_disk(
4516 struct se_task *task,
4517 struct se_device *dev,
4518 unsigned long long lba,
4519 u32 sectors,
4520 int *max_sectors_set)
4522 if ((lba + sectors) > transport_dev_end_lba(dev)) {
4523 task->task_sectors = ((transport_dev_end_lba(dev) - lba) + 1);
4525 if (task->task_sectors > DEV_ATTRIB(dev)->max_sectors) {
4526 task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
4527 *max_sectors_set = 1;
4529 } else {
4530 if (sectors > DEV_ATTRIB(dev)->max_sectors) {
4531 task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
4532 *max_sectors_set = 1;
4533 } else
4534 task->task_sectors = sectors;
4537 return 0;
4540 static inline int transport_set_tasks_sectors_non_disk(
4541 struct se_task *task,
4542 struct se_device *dev,
4543 unsigned long long lba,
4544 u32 sectors,
4545 int *max_sectors_set)
4547 if (sectors > DEV_ATTRIB(dev)->max_sectors) {
4548 task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
4549 *max_sectors_set = 1;
4550 } else
4551 task->task_sectors = sectors;
4553 return 0;
4556 static inline int transport_set_tasks_sectors(
4557 struct se_task *task,
4558 struct se_device *dev,
4559 unsigned long long lba,
4560 u32 sectors,
4561 int *max_sectors_set)
4563 return (TRANSPORT(dev)->get_device_type(dev) == TYPE_DISK) ?
4564 transport_set_tasks_sectors_disk(task, dev, lba, sectors,
4565 max_sectors_set) :
4566 transport_set_tasks_sectors_non_disk(task, dev, lba, sectors,
4567 max_sectors_set);
4570 static int transport_map_sg_to_mem(
4571 struct se_cmd *cmd,
4572 struct list_head *se_mem_list,
4573 void *in_mem,
4574 u32 *se_mem_cnt)
4576 struct se_mem *se_mem;
4577 struct scatterlist *sg;
4578 u32 sg_count = 1, cmd_size = cmd->data_length;
4580 if (!in_mem) {
4581 printk(KERN_ERR "No source scatterlist\n");
4582 return -1;
4584 sg = (struct scatterlist *)in_mem;
4586 while (cmd_size) {
4587 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4588 if (!(se_mem)) {
4589 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4590 return -1;
4592 INIT_LIST_HEAD(&se_mem->se_list);
4593 DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
4594 " sg_page: %p offset: %d length: %d\n", cmd_size,
4595 sg_page(sg), sg->offset, sg->length);
4597 se_mem->se_page = sg_page(sg);
4598 se_mem->se_off = sg->offset;
4600 if (cmd_size > sg->length) {
4601 se_mem->se_len = sg->length;
4602 sg = sg_next(sg);
4603 sg_count++;
4604 } else
4605 se_mem->se_len = cmd_size;
4607 cmd_size -= se_mem->se_len;
4609 DEBUG_MEM("sg_to_mem: *se_mem_cnt: %u cmd_size: %u\n",
4610 *se_mem_cnt, cmd_size);
4611 DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
4612 se_mem->se_page, se_mem->se_off, se_mem->se_len);
4614 list_add_tail(&se_mem->se_list, se_mem_list);
4615 (*se_mem_cnt)++;
4618 DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments to(%u)"
4619 " struct se_mem\n", sg_count, *se_mem_cnt);
4621 if (sg_count != *se_mem_cnt)
4622 BUG();
4624 return 0;
4627 /* transport_map_mem_to_sg():
4631 int transport_map_mem_to_sg(
4632 struct se_task *task,
4633 struct list_head *se_mem_list,
4634 void *in_mem,
4635 struct se_mem *in_se_mem,
4636 struct se_mem **out_se_mem,
4637 u32 *se_mem_cnt,
4638 u32 *task_offset)
4640 struct se_cmd *se_cmd = task->task_se_cmd;
4641 struct se_mem *se_mem = in_se_mem;
4642 struct scatterlist *sg = (struct scatterlist *)in_mem;
4643 u32 task_size = task->task_size, sg_no = 0;
4645 if (!sg) {
4646 printk(KERN_ERR "Unable to locate valid struct"
4647 " scatterlist pointer\n");
4648 return -1;
4651 while (task_size != 0) {
4653 * Setup the contigious array of scatterlists for
4654 * this struct se_task.
4656 sg_assign_page(sg, se_mem->se_page);
4658 if (*task_offset == 0) {
4659 sg->offset = se_mem->se_off;
4661 if (task_size >= se_mem->se_len) {
4662 sg->length = se_mem->se_len;
4664 if (!(list_is_last(&se_mem->se_list,
4665 T_TASK(se_cmd)->t_mem_list))) {
4666 se_mem = list_entry(se_mem->se_list.next,
4667 struct se_mem, se_list);
4668 (*se_mem_cnt)++;
4670 } else {
4671 sg->length = task_size;
4673 * Determine if we need to calculate an offset
4674 * into the struct se_mem on the next go around..
4676 task_size -= sg->length;
4677 if (!(task_size))
4678 *task_offset = sg->length;
4680 goto next;
4683 } else {
4684 sg->offset = (*task_offset + se_mem->se_off);
4686 if ((se_mem->se_len - *task_offset) > task_size) {
4687 sg->length = task_size;
4689 * Determine if we need to calculate an offset
4690 * into the struct se_mem on the next go around..
4692 task_size -= sg->length;
4693 if (!(task_size))
4694 *task_offset += sg->length;
4696 goto next;
4697 } else {
4698 sg->length = (se_mem->se_len - *task_offset);
4700 if (!(list_is_last(&se_mem->se_list,
4701 T_TASK(se_cmd)->t_mem_list))) {
4702 se_mem = list_entry(se_mem->se_list.next,
4703 struct se_mem, se_list);
4704 (*se_mem_cnt)++;
4708 *task_offset = 0;
4710 task_size -= sg->length;
4711 next:
4712 DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
4713 " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
4714 sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
4716 sg_no++;
4717 if (!(task_size))
4718 break;
4720 sg = sg_next(sg);
4722 if (task_size > se_cmd->data_length)
4723 BUG();
4725 *out_se_mem = se_mem;
4727 DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
4728 " SGs\n", task->task_no, *se_mem_cnt, sg_no);
4730 return 0;
4734 * This function can be used by HW target mode drivers to create a linked
4735 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4736 * This is intended to be called during the completion path by TCM Core
4737 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4739 void transport_do_task_sg_chain(struct se_cmd *cmd)
4741 struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
4742 struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
4743 struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
4744 struct se_task *task;
4745 struct target_core_fabric_ops *tfo = CMD_TFO(cmd);
4746 u32 task_sg_num = 0, sg_count = 0;
4747 int i;
4749 if (tfo->task_sg_chaining == 0) {
4750 printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
4751 " %s\n", tfo->get_fabric_name());
4752 dump_stack();
4753 return;
4756 * Walk the struct se_task list and setup scatterlist chains
4757 * for each contiguosly allocated struct se_task->task_sg[].
4759 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
4760 if (!(task->task_sg) || !(task->task_padded_sg))
4761 continue;
4763 if (sg_head && sg_link) {
4764 sg_head_cur = &task->task_sg[0];
4765 sg_link_cur = &task->task_sg[task->task_sg_num];
4767 * Either add chain or mark end of scatterlist
4769 if (!(list_is_last(&task->t_list,
4770 &T_TASK(cmd)->t_task_list))) {
4772 * Clear existing SGL termination bit set in
4773 * transport_calc_sg_num(), see sg_mark_end()
4775 sg_end_cur = &task->task_sg[task->task_sg_num - 1];
4776 sg_end_cur->page_link &= ~0x02;
4778 sg_chain(sg_head, task_sg_num, sg_head_cur);
4779 sg_count += (task->task_sg_num + 1);
4780 } else
4781 sg_count += task->task_sg_num;
4783 sg_head = sg_head_cur;
4784 sg_link = sg_link_cur;
4785 task_sg_num = task->task_sg_num;
4786 continue;
4788 sg_head = sg_first = &task->task_sg[0];
4789 sg_link = &task->task_sg[task->task_sg_num];
4790 task_sg_num = task->task_sg_num;
4792 * Check for single task..
4794 if (!(list_is_last(&task->t_list, &T_TASK(cmd)->t_task_list))) {
4796 * Clear existing SGL termination bit set in
4797 * transport_calc_sg_num(), see sg_mark_end()
4799 sg_end = &task->task_sg[task->task_sg_num - 1];
4800 sg_end->page_link &= ~0x02;
4801 sg_count += (task->task_sg_num + 1);
4802 } else
4803 sg_count += task->task_sg_num;
4806 * Setup the starting pointer and total t_tasks_sg_linked_no including
4807 * padding SGs for linking and to mark the end.
4809 T_TASK(cmd)->t_tasks_sg_chained = sg_first;
4810 T_TASK(cmd)->t_tasks_sg_chained_no = sg_count;
4812 DEBUG_CMD_M("Setup T_TASK(cmd)->t_tasks_sg_chained: %p and"
4813 " t_tasks_sg_chained_no: %u\n", T_TASK(cmd)->t_tasks_sg_chained,
4814 T_TASK(cmd)->t_tasks_sg_chained_no);
4816 for_each_sg(T_TASK(cmd)->t_tasks_sg_chained, sg,
4817 T_TASK(cmd)->t_tasks_sg_chained_no, i) {
4819 DEBUG_CMD_M("SG: %p page: %p length: %d offset: %d\n",
4820 sg, sg_page(sg), sg->length, sg->offset);
4821 if (sg_is_chain(sg))
4822 DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
4823 if (sg_is_last(sg))
4824 DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
4828 EXPORT_SYMBOL(transport_do_task_sg_chain);
4830 static int transport_do_se_mem_map(
4831 struct se_device *dev,
4832 struct se_task *task,
4833 struct list_head *se_mem_list,
4834 void *in_mem,
4835 struct se_mem *in_se_mem,
4836 struct se_mem **out_se_mem,
4837 u32 *se_mem_cnt,
4838 u32 *task_offset_in)
4840 u32 task_offset = *task_offset_in;
4841 int ret = 0;
4843 * se_subsystem_api_t->do_se_mem_map is used when internal allocation
4844 * has been done by the transport plugin.
4846 if (TRANSPORT(dev)->do_se_mem_map) {
4847 ret = TRANSPORT(dev)->do_se_mem_map(task, se_mem_list,
4848 in_mem, in_se_mem, out_se_mem, se_mem_cnt,
4849 task_offset_in);
4850 if (ret == 0)
4851 T_TASK(task->task_se_cmd)->t_tasks_se_num += *se_mem_cnt;
4853 return ret;
4856 BUG_ON(list_empty(se_mem_list));
4858 * This is the normal path for all normal non BIDI and BIDI-COMMAND
4859 * WRITE payloads.. If we need to do BIDI READ passthrough for
4860 * TCM/pSCSI the first call to transport_do_se_mem_map ->
4861 * transport_calc_sg_num() -> transport_map_mem_to_sg() will do the
4862 * allocation for task->task_sg_bidi, and the subsequent call to
4863 * transport_do_se_mem_map() from transport_generic_get_cdb_count()
4865 if (!(task->task_sg_bidi)) {
4867 * Assume default that transport plugin speaks preallocated
4868 * scatterlists.
4870 if (!(transport_calc_sg_num(task, in_se_mem, task_offset)))
4871 return -1;
4873 * struct se_task->task_sg now contains the struct scatterlist array.
4875 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
4876 in_se_mem, out_se_mem, se_mem_cnt,
4877 task_offset_in);
4880 * Handle the se_mem_list -> struct task->task_sg_bidi
4881 * memory map for the extra BIDI READ payload
4883 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
4884 in_se_mem, out_se_mem, se_mem_cnt,
4885 task_offset_in);
4888 static u32 transport_generic_get_cdb_count(
4889 struct se_cmd *cmd,
4890 unsigned long long lba,
4891 u32 sectors,
4892 enum dma_data_direction data_direction,
4893 struct list_head *mem_list,
4894 int set_counts)
4896 unsigned char *cdb = NULL;
4897 struct se_task *task;
4898 struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
4899 struct se_mem *se_mem_bidi = NULL, *se_mem_bidi_lout = NULL;
4900 struct se_device *dev = SE_DEV(cmd);
4901 int max_sectors_set = 0, ret;
4902 u32 task_offset_in = 0, se_mem_cnt = 0, se_mem_bidi_cnt = 0, task_cdbs = 0;
4904 if (!mem_list) {
4905 printk(KERN_ERR "mem_list is NULL in transport_generic_get"
4906 "_cdb_count()\n");
4907 return 0;
4910 * While using RAMDISK_DR backstores is the only case where
4911 * mem_list will ever be empty at this point.
4913 if (!(list_empty(mem_list)))
4914 se_mem = list_entry(mem_list->next, struct se_mem, se_list);
4916 * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
4917 * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
4919 if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
4920 !(list_empty(T_TASK(cmd)->t_mem_bidi_list)) &&
4921 (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
4922 se_mem_bidi = list_entry(T_TASK(cmd)->t_mem_bidi_list->next,
4923 struct se_mem, se_list);
4925 while (sectors) {
4926 DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
4927 CMD_TFO(cmd)->get_task_tag(cmd), lba, sectors,
4928 transport_dev_end_lba(dev));
4930 task = transport_generic_get_task(cmd, data_direction);
4931 if (!(task))
4932 goto out;
4934 transport_set_tasks_sectors(task, dev, lba, sectors,
4935 &max_sectors_set);
4937 task->task_lba = lba;
4938 lba += task->task_sectors;
4939 sectors -= task->task_sectors;
4940 task->task_size = (task->task_sectors *
4941 DEV_ATTRIB(dev)->block_size);
4943 cdb = TRANSPORT(dev)->get_cdb(task);
4944 if ((cdb)) {
4945 memcpy(cdb, T_TASK(cmd)->t_task_cdb,
4946 scsi_command_size(T_TASK(cmd)->t_task_cdb));
4947 cmd->transport_split_cdb(task->task_lba,
4948 &task->task_sectors, cdb);
4952 * Perform the SE OBJ plugin and/or Transport plugin specific
4953 * mapping for T_TASK(cmd)->t_mem_list. And setup the
4954 * task->task_sg and if necessary task->task_sg_bidi
4956 ret = transport_do_se_mem_map(dev, task, mem_list,
4957 NULL, se_mem, &se_mem_lout, &se_mem_cnt,
4958 &task_offset_in);
4959 if (ret < 0)
4960 goto out;
4962 se_mem = se_mem_lout;
4964 * Setup the T_TASK(cmd)->t_mem_bidi_list -> task->task_sg_bidi
4965 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
4967 * Note that the first call to transport_do_se_mem_map() above will
4968 * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
4969 * -> transport_calc_sg_num(), and the second here will do the
4970 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
4972 if (task->task_sg_bidi != NULL) {
4973 ret = transport_do_se_mem_map(dev, task,
4974 T_TASK(cmd)->t_mem_bidi_list, NULL,
4975 se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
4976 &task_offset_in);
4977 if (ret < 0)
4978 goto out;
4980 se_mem_bidi = se_mem_bidi_lout;
4982 task_cdbs++;
4984 DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
4985 task_cdbs, task->task_sg_num);
4987 if (max_sectors_set) {
4988 max_sectors_set = 0;
4989 continue;
4992 if (!sectors)
4993 break;
4996 if (set_counts) {
4997 atomic_inc(&T_TASK(cmd)->t_fe_count);
4998 atomic_inc(&T_TASK(cmd)->t_se_count);
5001 DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
5002 CMD_TFO(cmd)->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
5003 ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
5005 return task_cdbs;
5006 out:
5007 return 0;
5010 static int
5011 transport_map_control_cmd_to_task(struct se_cmd *cmd)
5013 struct se_device *dev = SE_DEV(cmd);
5014 unsigned char *cdb;
5015 struct se_task *task;
5016 int ret;
5018 task = transport_generic_get_task(cmd, cmd->data_direction);
5019 if (!task)
5020 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
5022 cdb = TRANSPORT(dev)->get_cdb(task);
5023 if (cdb)
5024 memcpy(cdb, cmd->t_task->t_task_cdb,
5025 scsi_command_size(cmd->t_task->t_task_cdb));
5027 task->task_size = cmd->data_length;
5028 task->task_sg_num =
5029 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
5031 atomic_inc(&cmd->t_task->t_fe_count);
5032 atomic_inc(&cmd->t_task->t_se_count);
5034 if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
5035 struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
5036 u32 se_mem_cnt = 0, task_offset = 0;
5038 if (!list_empty(T_TASK(cmd)->t_mem_list))
5039 se_mem = list_entry(T_TASK(cmd)->t_mem_list->next,
5040 struct se_mem, se_list);
5042 ret = transport_do_se_mem_map(dev, task,
5043 cmd->t_task->t_mem_list, NULL, se_mem,
5044 &se_mem_lout, &se_mem_cnt, &task_offset);
5045 if (ret < 0)
5046 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
5048 if (dev->transport->map_task_SG)
5049 return dev->transport->map_task_SG(task);
5050 return 0;
5051 } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
5052 if (dev->transport->map_task_non_SG)
5053 return dev->transport->map_task_non_SG(task);
5054 return 0;
5055 } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
5056 if (dev->transport->cdb_none)
5057 return dev->transport->cdb_none(task);
5058 return 0;
5059 } else {
5060 BUG();
5061 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
5065 /* transport_generic_new_cmd(): Called from transport_processing_thread()
5067 * Allocate storage transport resources from a set of values predefined
5068 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
5069 * Any non zero return here is treated as an "out of resource' op here.
5072 * Generate struct se_task(s) and/or their payloads for this CDB.
5074 static int transport_generic_new_cmd(struct se_cmd *cmd)
5076 struct se_portal_group *se_tpg;
5077 struct se_task *task;
5078 struct se_device *dev = SE_DEV(cmd);
5079 int ret = 0;
5082 * Determine is the TCM fabric module has already allocated physical
5083 * memory, and is directly calling transport_generic_map_mem_to_cmd()
5084 * to setup beforehand the linked list of physical memory at
5085 * T_TASK(cmd)->t_mem_list of struct se_mem->se_page
5087 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
5088 ret = transport_allocate_resources(cmd);
5089 if (ret < 0)
5090 return ret;
5093 ret = transport_get_sectors(cmd);
5094 if (ret < 0)
5095 return ret;
5097 ret = transport_new_cmd_obj(cmd);
5098 if (ret < 0)
5099 return ret;
5102 * Determine if the calling TCM fabric module is talking to
5103 * Linux/NET via kernel sockets and needs to allocate a
5104 * struct iovec array to complete the struct se_cmd
5106 se_tpg = SE_LUN(cmd)->lun_sep->sep_tpg;
5107 if (TPG_TFO(se_tpg)->alloc_cmd_iovecs != NULL) {
5108 ret = TPG_TFO(se_tpg)->alloc_cmd_iovecs(cmd);
5109 if (ret < 0)
5110 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
5113 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
5114 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
5115 if (atomic_read(&task->task_sent))
5116 continue;
5117 if (!dev->transport->map_task_SG)
5118 continue;
5120 ret = dev->transport->map_task_SG(task);
5121 if (ret < 0)
5122 return ret;
5124 } else {
5125 ret = transport_map_control_cmd_to_task(cmd);
5126 if (ret < 0)
5127 return ret;
5131 * For WRITEs, let the iSCSI Target RX Thread know its buffer is ready..
5132 * This WRITE struct se_cmd (and all of its associated struct se_task's)
5133 * will be added to the struct se_device execution queue after its WRITE
5134 * data has arrived. (ie: It gets handled by the transport processing
5135 * thread a second time)
5137 if (cmd->data_direction == DMA_TO_DEVICE) {
5138 transport_add_tasks_to_state_queue(cmd);
5139 return transport_generic_write_pending(cmd);
5142 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
5143 * to the execution queue.
5145 transport_execute_tasks(cmd);
5146 return 0;
5149 /* transport_generic_process_write():
5153 void transport_generic_process_write(struct se_cmd *cmd)
5155 #if 0
5157 * Copy SCSI Presented DTL sector(s) from received buffers allocated to
5158 * original EDTL
5160 if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
5161 if (!T_TASK(cmd)->t_tasks_se_num) {
5162 unsigned char *dst, *buf =
5163 (unsigned char *)T_TASK(cmd)->t_task_buf;
5165 dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
5166 if (!(dst)) {
5167 printk(KERN_ERR "Unable to allocate memory for"
5168 " WRITE underflow\n");
5169 transport_generic_request_failure(cmd, NULL,
5170 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
5171 return;
5173 memcpy(dst, buf, cmd->cmd_spdtl);
5175 kfree(T_TASK(cmd)->t_task_buf);
5176 T_TASK(cmd)->t_task_buf = dst;
5177 } else {
5178 struct scatterlist *sg =
5179 (struct scatterlist *sg)T_TASK(cmd)->t_task_buf;
5180 struct scatterlist *orig_sg;
5182 orig_sg = kzalloc(sizeof(struct scatterlist) *
5183 T_TASK(cmd)->t_tasks_se_num,
5184 GFP_KERNEL))) {
5185 if (!(orig_sg)) {
5186 printk(KERN_ERR "Unable to allocate memory"
5187 " for WRITE underflow\n");
5188 transport_generic_request_failure(cmd, NULL,
5189 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
5190 return;
5193 memcpy(orig_sg, T_TASK(cmd)->t_task_buf,
5194 sizeof(struct scatterlist) *
5195 T_TASK(cmd)->t_tasks_se_num);
5197 cmd->data_length = cmd->cmd_spdtl;
5199 * FIXME, clear out original struct se_task and state
5200 * information.
5202 if (transport_generic_new_cmd(cmd) < 0) {
5203 transport_generic_request_failure(cmd, NULL,
5204 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
5205 kfree(orig_sg);
5206 return;
5209 transport_memcpy_write_sg(cmd, orig_sg);
5212 #endif
5213 transport_execute_tasks(cmd);
5215 EXPORT_SYMBOL(transport_generic_process_write);
5217 /* transport_generic_write_pending():
5221 static int transport_generic_write_pending(struct se_cmd *cmd)
5223 unsigned long flags;
5224 int ret;
5226 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5227 cmd->t_state = TRANSPORT_WRITE_PENDING;
5228 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5230 * For the TCM control CDBs using a contiguous buffer, do the memcpy
5231 * from the passed Linux/SCSI struct scatterlist located at
5232 * T_TASK(se_cmd)->t_task_pt_buf to the contiguous buffer at
5233 * T_TASK(se_cmd)->t_task_buf.
5235 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
5236 transport_memcpy_read_contig(cmd,
5237 T_TASK(cmd)->t_task_buf,
5238 T_TASK(cmd)->t_task_pt_sgl);
5240 * Clear the se_cmd for WRITE_PENDING status in order to set
5241 * T_TASK(cmd)->t_transport_active=0 so that transport_generic_handle_data
5242 * can be called from HW target mode interrupt code. This is safe
5243 * to be called with transport_off=1 before the CMD_TFO(cmd)->write_pending
5244 * because the se_cmd->se_lun pointer is not being cleared.
5246 transport_cmd_check_stop(cmd, 1, 0);
5249 * Call the fabric write_pending function here to let the
5250 * frontend know that WRITE buffers are ready.
5252 ret = CMD_TFO(cmd)->write_pending(cmd);
5253 if (ret < 0)
5254 return ret;
5256 return PYX_TRANSPORT_WRITE_PENDING;
5259 /* transport_release_cmd_to_pool():
5263 void transport_release_cmd_to_pool(struct se_cmd *cmd)
5265 BUG_ON(!T_TASK(cmd));
5266 BUG_ON(!CMD_TFO(cmd));
5268 transport_free_se_cmd(cmd);
5269 CMD_TFO(cmd)->release_cmd_to_pool(cmd);
5271 EXPORT_SYMBOL(transport_release_cmd_to_pool);
5273 /* transport_generic_free_cmd():
5275 * Called from processing frontend to release storage engine resources
5277 void transport_generic_free_cmd(
5278 struct se_cmd *cmd,
5279 int wait_for_tasks,
5280 int release_to_pool,
5281 int session_reinstatement)
5283 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) || !T_TASK(cmd))
5284 transport_release_cmd_to_pool(cmd);
5285 else {
5286 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
5288 if (SE_LUN(cmd)) {
5289 #if 0
5290 printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
5291 " SE_LUN(cmd)\n", cmd,
5292 CMD_TFO(cmd)->get_task_tag(cmd));
5293 #endif
5294 transport_lun_remove_cmd(cmd);
5297 if (wait_for_tasks && cmd->transport_wait_for_tasks)
5298 cmd->transport_wait_for_tasks(cmd, 0, 0);
5300 transport_generic_remove(cmd, release_to_pool,
5301 session_reinstatement);
5304 EXPORT_SYMBOL(transport_generic_free_cmd);
5306 static void transport_nop_wait_for_tasks(
5307 struct se_cmd *cmd,
5308 int remove_cmd,
5309 int session_reinstatement)
5311 return;
5314 /* transport_lun_wait_for_tasks():
5316 * Called from ConfigFS context to stop the passed struct se_cmd to allow
5317 * an struct se_lun to be successfully shutdown.
5319 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
5321 unsigned long flags;
5322 int ret;
5324 * If the frontend has already requested this struct se_cmd to
5325 * be stopped, we can safely ignore this struct se_cmd.
5327 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5328 if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
5329 atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
5330 DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
5331 " TRUE, skipping\n", CMD_TFO(cmd)->get_task_tag(cmd));
5332 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5333 transport_cmd_check_stop(cmd, 1, 0);
5334 return -1;
5336 atomic_set(&T_TASK(cmd)->transport_lun_fe_stop, 1);
5337 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5339 wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
5341 ret = transport_stop_tasks_for_cmd(cmd);
5343 DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
5344 " %d\n", cmd, T_TASK(cmd)->t_task_cdbs, ret);
5345 if (!ret) {
5346 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
5347 CMD_TFO(cmd)->get_task_tag(cmd));
5348 wait_for_completion(&T_TASK(cmd)->transport_lun_stop_comp);
5349 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
5350 CMD_TFO(cmd)->get_task_tag(cmd));
5352 transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
5354 return 0;
5357 /* #define DEBUG_CLEAR_LUN */
5358 #ifdef DEBUG_CLEAR_LUN
5359 #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
5360 #else
5361 #define DEBUG_CLEAR_L(x...)
5362 #endif
5364 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
5366 struct se_cmd *cmd = NULL;
5367 unsigned long lun_flags, cmd_flags;
5369 * Do exception processing and return CHECK_CONDITION status to the
5370 * Initiator Port.
5372 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5373 while (!list_empty_careful(&lun->lun_cmd_list)) {
5374 cmd = list_entry(lun->lun_cmd_list.next,
5375 struct se_cmd, se_lun_list);
5376 list_del(&cmd->se_lun_list);
5378 if (!(T_TASK(cmd))) {
5379 printk(KERN_ERR "ITT: 0x%08x, T_TASK(cmd) = NULL"
5380 "[i,t]_state: %u/%u\n",
5381 CMD_TFO(cmd)->get_task_tag(cmd),
5382 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
5383 BUG();
5385 atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
5387 * This will notify iscsi_target_transport.c:
5388 * transport_cmd_check_stop() that a LUN shutdown is in
5389 * progress for the iscsi_cmd_t.
5391 spin_lock(&T_TASK(cmd)->t_state_lock);
5392 DEBUG_CLEAR_L("SE_LUN[%d] - Setting T_TASK(cmd)->transport"
5393 "_lun_stop for ITT: 0x%08x\n",
5394 SE_LUN(cmd)->unpacked_lun,
5395 CMD_TFO(cmd)->get_task_tag(cmd));
5396 atomic_set(&T_TASK(cmd)->transport_lun_stop, 1);
5397 spin_unlock(&T_TASK(cmd)->t_state_lock);
5399 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5401 if (!(SE_LUN(cmd))) {
5402 printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
5403 CMD_TFO(cmd)->get_task_tag(cmd),
5404 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
5405 BUG();
5408 * If the Storage engine still owns the iscsi_cmd_t, determine
5409 * and/or stop its context.
5411 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
5412 "_lun_wait_for_tasks()\n", SE_LUN(cmd)->unpacked_lun,
5413 CMD_TFO(cmd)->get_task_tag(cmd));
5415 if (transport_lun_wait_for_tasks(cmd, SE_LUN(cmd)) < 0) {
5416 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5417 continue;
5420 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
5421 "_wait_for_tasks(): SUCCESS\n",
5422 SE_LUN(cmd)->unpacked_lun,
5423 CMD_TFO(cmd)->get_task_tag(cmd));
5425 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
5426 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
5427 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
5428 goto check_cond;
5430 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
5431 transport_all_task_dev_remove_state(cmd);
5432 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
5434 transport_free_dev_tasks(cmd);
5436 * The Storage engine stopped this struct se_cmd before it was
5437 * send to the fabric frontend for delivery back to the
5438 * Initiator Node. Return this SCSI CDB back with an
5439 * CHECK_CONDITION status.
5441 check_cond:
5442 transport_send_check_condition_and_sense(cmd,
5443 TCM_NON_EXISTENT_LUN, 0);
5445 * If the fabric frontend is waiting for this iscsi_cmd_t to
5446 * be released, notify the waiting thread now that LU has
5447 * finished accessing it.
5449 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
5450 if (atomic_read(&T_TASK(cmd)->transport_lun_fe_stop)) {
5451 DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
5452 " struct se_cmd: %p ITT: 0x%08x\n",
5453 lun->unpacked_lun,
5454 cmd, CMD_TFO(cmd)->get_task_tag(cmd));
5456 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
5457 cmd_flags);
5458 transport_cmd_check_stop(cmd, 1, 0);
5459 complete(&T_TASK(cmd)->transport_lun_fe_stop_comp);
5460 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5461 continue;
5463 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
5464 lun->unpacked_lun, CMD_TFO(cmd)->get_task_tag(cmd));
5466 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
5467 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5469 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5472 static int transport_clear_lun_thread(void *p)
5474 struct se_lun *lun = (struct se_lun *)p;
5476 __transport_clear_lun_from_sessions(lun);
5477 complete(&lun->lun_shutdown_comp);
5479 return 0;
5482 int transport_clear_lun_from_sessions(struct se_lun *lun)
5484 struct task_struct *kt;
5486 kt = kthread_run(transport_clear_lun_thread, (void *)lun,
5487 "tcm_cl_%u", lun->unpacked_lun);
5488 if (IS_ERR(kt)) {
5489 printk(KERN_ERR "Unable to start clear_lun thread\n");
5490 return -1;
5492 wait_for_completion(&lun->lun_shutdown_comp);
5494 return 0;
5497 /* transport_generic_wait_for_tasks():
5499 * Called from frontend or passthrough context to wait for storage engine
5500 * to pause and/or release frontend generated struct se_cmd.
5502 static void transport_generic_wait_for_tasks(
5503 struct se_cmd *cmd,
5504 int remove_cmd,
5505 int session_reinstatement)
5507 unsigned long flags;
5509 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
5510 return;
5512 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5514 * If we are already stopped due to an external event (ie: LUN shutdown)
5515 * sleep until the connection can have the passed struct se_cmd back.
5516 * The T_TASK(cmd)->transport_lun_stopped_sem will be upped by
5517 * transport_clear_lun_from_sessions() once the ConfigFS context caller
5518 * has completed its operation on the struct se_cmd.
5520 if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
5522 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
5523 " wait_for_completion(&T_TASK(cmd)transport_lun_fe"
5524 "_stop_comp); for ITT: 0x%08x\n",
5525 CMD_TFO(cmd)->get_task_tag(cmd));
5527 * There is a special case for WRITES where a FE exception +
5528 * LUN shutdown means ConfigFS context is still sleeping on
5529 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
5530 * We go ahead and up transport_lun_stop_comp just to be sure
5531 * here.
5533 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5534 complete(&T_TASK(cmd)->transport_lun_stop_comp);
5535 wait_for_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
5536 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5538 transport_all_task_dev_remove_state(cmd);
5540 * At this point, the frontend who was the originator of this
5541 * struct se_cmd, now owns the structure and can be released through
5542 * normal means below.
5544 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
5545 " wait_for_completion(&T_TASK(cmd)transport_lun_fe_"
5546 "stop_comp); for ITT: 0x%08x\n",
5547 CMD_TFO(cmd)->get_task_tag(cmd));
5549 atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
5551 if (!atomic_read(&T_TASK(cmd)->t_transport_active) ||
5552 atomic_read(&T_TASK(cmd)->t_transport_aborted))
5553 goto remove;
5555 atomic_set(&T_TASK(cmd)->t_transport_stop, 1);
5557 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
5558 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
5559 " = TRUE\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
5560 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
5561 cmd->deferred_t_state);
5563 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5565 wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
5567 wait_for_completion(&T_TASK(cmd)->t_transport_stop_comp);
5569 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5570 atomic_set(&T_TASK(cmd)->t_transport_active, 0);
5571 atomic_set(&T_TASK(cmd)->t_transport_stop, 0);
5573 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
5574 "&T_TASK(cmd)->t_transport_stop_comp) for ITT: 0x%08x\n",
5575 CMD_TFO(cmd)->get_task_tag(cmd));
5576 remove:
5577 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5578 if (!remove_cmd)
5579 return;
5581 transport_generic_free_cmd(cmd, 0, 0, session_reinstatement);
5584 static int transport_get_sense_codes(
5585 struct se_cmd *cmd,
5586 u8 *asc,
5587 u8 *ascq)
5589 *asc = cmd->scsi_asc;
5590 *ascq = cmd->scsi_ascq;
5592 return 0;
5595 static int transport_set_sense_codes(
5596 struct se_cmd *cmd,
5597 u8 asc,
5598 u8 ascq)
5600 cmd->scsi_asc = asc;
5601 cmd->scsi_ascq = ascq;
5603 return 0;
5606 int transport_send_check_condition_and_sense(
5607 struct se_cmd *cmd,
5608 u8 reason,
5609 int from_transport)
5611 unsigned char *buffer = cmd->sense_buffer;
5612 unsigned long flags;
5613 int offset;
5614 u8 asc = 0, ascq = 0;
5616 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5617 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
5618 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5619 return 0;
5621 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
5622 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5624 if (!reason && from_transport)
5625 goto after_reason;
5627 if (!from_transport)
5628 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
5630 * Data Segment and SenseLength of the fabric response PDU.
5632 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
5633 * from include/scsi/scsi_cmnd.h
5635 offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
5636 TRANSPORT_SENSE_BUFFER);
5638 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
5639 * SENSE KEY values from include/scsi/scsi.h
5641 switch (reason) {
5642 case TCM_NON_EXISTENT_LUN:
5643 case TCM_UNSUPPORTED_SCSI_OPCODE:
5644 case TCM_SECTOR_COUNT_TOO_MANY:
5645 /* CURRENT ERROR */
5646 buffer[offset] = 0x70;
5647 /* ILLEGAL REQUEST */
5648 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5649 /* INVALID COMMAND OPERATION CODE */
5650 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
5651 break;
5652 case TCM_UNKNOWN_MODE_PAGE:
5653 /* CURRENT ERROR */
5654 buffer[offset] = 0x70;
5655 /* ILLEGAL REQUEST */
5656 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5657 /* INVALID FIELD IN CDB */
5658 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5659 break;
5660 case TCM_CHECK_CONDITION_ABORT_CMD:
5661 /* CURRENT ERROR */
5662 buffer[offset] = 0x70;
5663 /* ABORTED COMMAND */
5664 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5665 /* BUS DEVICE RESET FUNCTION OCCURRED */
5666 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
5667 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
5668 break;
5669 case TCM_INCORRECT_AMOUNT_OF_DATA:
5670 /* CURRENT ERROR */
5671 buffer[offset] = 0x70;
5672 /* ABORTED COMMAND */
5673 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5674 /* WRITE ERROR */
5675 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
5676 /* NOT ENOUGH UNSOLICITED DATA */
5677 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
5678 break;
5679 case TCM_INVALID_CDB_FIELD:
5680 /* CURRENT ERROR */
5681 buffer[offset] = 0x70;
5682 /* ABORTED COMMAND */
5683 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5684 /* INVALID FIELD IN CDB */
5685 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5686 break;
5687 case TCM_INVALID_PARAMETER_LIST:
5688 /* CURRENT ERROR */
5689 buffer[offset] = 0x70;
5690 /* ABORTED COMMAND */
5691 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5692 /* INVALID FIELD IN PARAMETER LIST */
5693 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
5694 break;
5695 case TCM_UNEXPECTED_UNSOLICITED_DATA:
5696 /* CURRENT ERROR */
5697 buffer[offset] = 0x70;
5698 /* ABORTED COMMAND */
5699 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5700 /* WRITE ERROR */
5701 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
5702 /* UNEXPECTED_UNSOLICITED_DATA */
5703 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
5704 break;
5705 case TCM_SERVICE_CRC_ERROR:
5706 /* CURRENT ERROR */
5707 buffer[offset] = 0x70;
5708 /* ABORTED COMMAND */
5709 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5710 /* PROTOCOL SERVICE CRC ERROR */
5711 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
5712 /* N/A */
5713 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
5714 break;
5715 case TCM_SNACK_REJECTED:
5716 /* CURRENT ERROR */
5717 buffer[offset] = 0x70;
5718 /* ABORTED COMMAND */
5719 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5720 /* READ ERROR */
5721 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
5722 /* FAILED RETRANSMISSION REQUEST */
5723 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
5724 break;
5725 case TCM_WRITE_PROTECTED:
5726 /* CURRENT ERROR */
5727 buffer[offset] = 0x70;
5728 /* DATA PROTECT */
5729 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
5730 /* WRITE PROTECTED */
5731 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
5732 break;
5733 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
5734 /* CURRENT ERROR */
5735 buffer[offset] = 0x70;
5736 /* UNIT ATTENTION */
5737 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
5738 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
5739 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5740 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5741 break;
5742 case TCM_CHECK_CONDITION_NOT_READY:
5743 /* CURRENT ERROR */
5744 buffer[offset] = 0x70;
5745 /* Not Ready */
5746 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
5747 transport_get_sense_codes(cmd, &asc, &ascq);
5748 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5749 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5750 break;
5751 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
5752 default:
5753 /* CURRENT ERROR */
5754 buffer[offset] = 0x70;
5755 /* ILLEGAL REQUEST */
5756 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5757 /* LOGICAL UNIT COMMUNICATION FAILURE */
5758 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
5759 break;
5762 * This code uses linux/include/scsi/scsi.h SAM status codes!
5764 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
5766 * Automatically padded, this value is encoded in the fabric's
5767 * data_length response PDU containing the SCSI defined sense data.
5769 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
5771 after_reason:
5772 CMD_TFO(cmd)->queue_status(cmd);
5773 return 0;
5775 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
5777 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
5779 int ret = 0;
5781 if (atomic_read(&T_TASK(cmd)->t_transport_aborted) != 0) {
5782 if (!(send_status) ||
5783 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
5784 return 1;
5785 #if 0
5786 printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
5787 " status for CDB: 0x%02x ITT: 0x%08x\n",
5788 T_TASK(cmd)->t_task_cdb[0],
5789 CMD_TFO(cmd)->get_task_tag(cmd));
5790 #endif
5791 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
5792 CMD_TFO(cmd)->queue_status(cmd);
5793 ret = 1;
5795 return ret;
5797 EXPORT_SYMBOL(transport_check_aborted_status);
5799 void transport_send_task_abort(struct se_cmd *cmd)
5802 * If there are still expected incoming fabric WRITEs, we wait
5803 * until until they have completed before sending a TASK_ABORTED
5804 * response. This response with TASK_ABORTED status will be
5805 * queued back to fabric module by transport_check_aborted_status().
5807 if (cmd->data_direction == DMA_TO_DEVICE) {
5808 if (CMD_TFO(cmd)->write_pending_status(cmd) != 0) {
5809 atomic_inc(&T_TASK(cmd)->t_transport_aborted);
5810 smp_mb__after_atomic_inc();
5811 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5812 transport_new_cmd_failure(cmd);
5813 return;
5816 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5817 #if 0
5818 printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
5819 " ITT: 0x%08x\n", T_TASK(cmd)->t_task_cdb[0],
5820 CMD_TFO(cmd)->get_task_tag(cmd));
5821 #endif
5822 CMD_TFO(cmd)->queue_status(cmd);
5825 /* transport_generic_do_tmr():
5829 int transport_generic_do_tmr(struct se_cmd *cmd)
5831 struct se_cmd *ref_cmd;
5832 struct se_device *dev = SE_DEV(cmd);
5833 struct se_tmr_req *tmr = cmd->se_tmr_req;
5834 int ret;
5836 switch (tmr->function) {
5837 case TMR_ABORT_TASK:
5838 ref_cmd = tmr->ref_cmd;
5839 tmr->response = TMR_FUNCTION_REJECTED;
5840 break;
5841 case TMR_ABORT_TASK_SET:
5842 case TMR_CLEAR_ACA:
5843 case TMR_CLEAR_TASK_SET:
5844 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
5845 break;
5846 case TMR_LUN_RESET:
5847 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
5848 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
5849 TMR_FUNCTION_REJECTED;
5850 break;
5851 case TMR_TARGET_WARM_RESET:
5852 tmr->response = TMR_FUNCTION_REJECTED;
5853 break;
5854 case TMR_TARGET_COLD_RESET:
5855 tmr->response = TMR_FUNCTION_REJECTED;
5856 break;
5857 default:
5858 printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
5859 tmr->function);
5860 tmr->response = TMR_FUNCTION_REJECTED;
5861 break;
5864 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
5865 CMD_TFO(cmd)->queue_tm_rsp(cmd);
5867 transport_cmd_check_stop(cmd, 2, 0);
5868 return 0;
5872 * Called with spin_lock_irq(&dev->execute_task_lock); held
5875 static struct se_task *
5876 transport_get_task_from_state_list(struct se_device *dev)
5878 struct se_task *task;
5880 if (list_empty(&dev->state_task_list))
5881 return NULL;
5883 list_for_each_entry(task, &dev->state_task_list, t_state_list)
5884 break;
5886 list_del(&task->t_state_list);
5887 atomic_set(&task->task_state_active, 0);
5889 return task;
5892 static void transport_processing_shutdown(struct se_device *dev)
5894 struct se_cmd *cmd;
5895 struct se_queue_req *qr;
5896 struct se_task *task;
5897 u8 state;
5898 unsigned long flags;
5900 * Empty the struct se_device's struct se_task state list.
5902 spin_lock_irqsave(&dev->execute_task_lock, flags);
5903 while ((task = transport_get_task_from_state_list(dev))) {
5904 if (!(TASK_CMD(task))) {
5905 printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
5906 continue;
5908 cmd = TASK_CMD(task);
5910 if (!T_TASK(cmd)) {
5911 printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
5912 " %p ITT: 0x%08x\n", task, cmd,
5913 CMD_TFO(cmd)->get_task_tag(cmd));
5914 continue;
5916 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5918 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5920 DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
5921 " i_state/def_i_state: %d/%d, t_state/def_t_state:"
5922 " %d/%d cdb: 0x%02x\n", cmd, task,
5923 CMD_TFO(cmd)->get_task_tag(cmd), cmd->cmd_sn,
5924 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->deferred_i_state,
5925 cmd->t_state, cmd->deferred_t_state,
5926 T_TASK(cmd)->t_task_cdb[0]);
5927 DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
5928 " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5929 " t_transport_stop: %d t_transport_sent: %d\n",
5930 CMD_TFO(cmd)->get_task_tag(cmd),
5931 T_TASK(cmd)->t_task_cdbs,
5932 atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
5933 atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
5934 atomic_read(&T_TASK(cmd)->t_transport_active),
5935 atomic_read(&T_TASK(cmd)->t_transport_stop),
5936 atomic_read(&T_TASK(cmd)->t_transport_sent));
5938 if (atomic_read(&task->task_active)) {
5939 atomic_set(&task->task_stop, 1);
5940 spin_unlock_irqrestore(
5941 &T_TASK(cmd)->t_state_lock, flags);
5943 DEBUG_DO("Waiting for task: %p to shutdown for dev:"
5944 " %p\n", task, dev);
5945 wait_for_completion(&task->task_stop_comp);
5946 DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
5947 task, dev);
5949 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5950 atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
5952 atomic_set(&task->task_active, 0);
5953 atomic_set(&task->task_stop, 0);
5954 } else {
5955 if (atomic_read(&task->task_execute_queue) != 0)
5956 transport_remove_task_from_execute_queue(task, dev);
5958 __transport_stop_task_timer(task, &flags);
5960 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
5961 spin_unlock_irqrestore(
5962 &T_TASK(cmd)->t_state_lock, flags);
5964 DEBUG_DO("Skipping task: %p, dev: %p for"
5965 " t_task_cdbs_ex_left: %d\n", task, dev,
5966 atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
5968 spin_lock_irqsave(&dev->execute_task_lock, flags);
5969 continue;
5972 if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
5973 DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
5974 " %p\n", task, dev);
5976 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
5977 spin_unlock_irqrestore(
5978 &T_TASK(cmd)->t_state_lock, flags);
5979 transport_send_check_condition_and_sense(
5980 cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
5982 transport_remove_cmd_from_queue(cmd,
5983 SE_DEV(cmd)->dev_queue_obj);
5985 transport_lun_remove_cmd(cmd);
5986 transport_cmd_check_stop(cmd, 1, 0);
5987 } else {
5988 spin_unlock_irqrestore(
5989 &T_TASK(cmd)->t_state_lock, flags);
5991 transport_remove_cmd_from_queue(cmd,
5992 SE_DEV(cmd)->dev_queue_obj);
5994 transport_lun_remove_cmd(cmd);
5996 if (transport_cmd_check_stop(cmd, 1, 0))
5997 transport_generic_remove(cmd, 0, 0);
6000 spin_lock_irqsave(&dev->execute_task_lock, flags);
6001 continue;
6003 DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
6004 task, dev);
6006 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
6007 spin_unlock_irqrestore(
6008 &T_TASK(cmd)->t_state_lock, flags);
6009 transport_send_check_condition_and_sense(cmd,
6010 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
6011 transport_remove_cmd_from_queue(cmd,
6012 SE_DEV(cmd)->dev_queue_obj);
6014 transport_lun_remove_cmd(cmd);
6015 transport_cmd_check_stop(cmd, 1, 0);
6016 } else {
6017 spin_unlock_irqrestore(
6018 &T_TASK(cmd)->t_state_lock, flags);
6020 transport_remove_cmd_from_queue(cmd,
6021 SE_DEV(cmd)->dev_queue_obj);
6022 transport_lun_remove_cmd(cmd);
6024 if (transport_cmd_check_stop(cmd, 1, 0))
6025 transport_generic_remove(cmd, 0, 0);
6028 spin_lock_irqsave(&dev->execute_task_lock, flags);
6030 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
6032 * Empty the struct se_device's struct se_cmd list.
6034 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
6035 while ((qr = __transport_get_qr_from_queue(dev->dev_queue_obj))) {
6036 spin_unlock_irqrestore(
6037 &dev->dev_queue_obj->cmd_queue_lock, flags);
6038 cmd = (struct se_cmd *)qr->cmd;
6039 state = qr->state;
6040 kfree(qr);
6042 DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
6043 cmd, state);
6045 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
6046 transport_send_check_condition_and_sense(cmd,
6047 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
6049 transport_lun_remove_cmd(cmd);
6050 transport_cmd_check_stop(cmd, 1, 0);
6051 } else {
6052 transport_lun_remove_cmd(cmd);
6053 if (transport_cmd_check_stop(cmd, 1, 0))
6054 transport_generic_remove(cmd, 0, 0);
6056 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
6058 spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
6061 /* transport_processing_thread():
6065 static int transport_processing_thread(void *param)
6067 int ret, t_state;
6068 struct se_cmd *cmd;
6069 struct se_device *dev = (struct se_device *) param;
6070 struct se_queue_req *qr;
6072 set_user_nice(current, -20);
6074 while (!kthread_should_stop()) {
6075 ret = wait_event_interruptible(dev->dev_queue_obj->thread_wq,
6076 atomic_read(&dev->dev_queue_obj->queue_cnt) ||
6077 kthread_should_stop());
6078 if (ret < 0)
6079 goto out;
6081 spin_lock_irq(&dev->dev_status_lock);
6082 if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
6083 spin_unlock_irq(&dev->dev_status_lock);
6084 transport_processing_shutdown(dev);
6085 continue;
6087 spin_unlock_irq(&dev->dev_status_lock);
6089 get_cmd:
6090 __transport_execute_tasks(dev);
6092 qr = transport_get_qr_from_queue(dev->dev_queue_obj);
6093 if (!(qr))
6094 continue;
6096 cmd = (struct se_cmd *)qr->cmd;
6097 t_state = qr->state;
6098 kfree(qr);
6100 switch (t_state) {
6101 case TRANSPORT_NEW_CMD_MAP:
6102 if (!(CMD_TFO(cmd)->new_cmd_map)) {
6103 printk(KERN_ERR "CMD_TFO(cmd)->new_cmd_map is"
6104 " NULL for TRANSPORT_NEW_CMD_MAP\n");
6105 BUG();
6107 ret = CMD_TFO(cmd)->new_cmd_map(cmd);
6108 if (ret < 0) {
6109 cmd->transport_error_status = ret;
6110 transport_generic_request_failure(cmd, NULL,
6111 0, (cmd->data_direction !=
6112 DMA_TO_DEVICE));
6113 break;
6115 /* Fall through */
6116 case TRANSPORT_NEW_CMD:
6117 ret = transport_generic_new_cmd(cmd);
6118 if (ret < 0) {
6119 cmd->transport_error_status = ret;
6120 transport_generic_request_failure(cmd, NULL,
6121 0, (cmd->data_direction !=
6122 DMA_TO_DEVICE));
6124 break;
6125 case TRANSPORT_PROCESS_WRITE:
6126 transport_generic_process_write(cmd);
6127 break;
6128 case TRANSPORT_COMPLETE_OK:
6129 transport_stop_all_task_timers(cmd);
6130 transport_generic_complete_ok(cmd);
6131 break;
6132 case TRANSPORT_REMOVE:
6133 transport_generic_remove(cmd, 1, 0);
6134 break;
6135 case TRANSPORT_PROCESS_TMR:
6136 transport_generic_do_tmr(cmd);
6137 break;
6138 case TRANSPORT_COMPLETE_FAILURE:
6139 transport_generic_request_failure(cmd, NULL, 1, 1);
6140 break;
6141 case TRANSPORT_COMPLETE_TIMEOUT:
6142 transport_stop_all_task_timers(cmd);
6143 transport_generic_request_timeout(cmd);
6144 break;
6145 default:
6146 printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
6147 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
6148 " %u\n", t_state, cmd->deferred_t_state,
6149 CMD_TFO(cmd)->get_task_tag(cmd),
6150 CMD_TFO(cmd)->get_cmd_state(cmd),
6151 SE_LUN(cmd)->unpacked_lun);
6152 BUG();
6155 goto get_cmd;
6158 out:
6159 transport_release_all_cmds(dev);
6160 dev->process_thread = NULL;
6161 return 0;