[SCSI] target: Fix SCF_SCSI_CONTROL_SG_IO_CDB breakage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / target / target_core_transport.c
blob32dc5163b5a07a5e4e4b1da89bf8b5cf7b2dbe08
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/smp_lock.h>
38 #include <linux/kthread.h>
39 #include <linux/in.h>
40 #include <linux/cdrom.h>
41 #include <asm/unaligned.h>
42 #include <net/sock.h>
43 #include <net/tcp.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/libsas.h> /* For TASK_ATTR_* */
48 #include <target/target_core_base.h>
49 #include <target/target_core_device.h>
50 #include <target/target_core_tmr.h>
51 #include <target/target_core_tpg.h>
52 #include <target/target_core_transport.h>
53 #include <target/target_core_fabric_ops.h>
54 #include <target/target_core_configfs.h>
56 #include "target_core_alua.h"
57 #include "target_core_hba.h"
58 #include "target_core_pr.h"
59 #include "target_core_scdb.h"
60 #include "target_core_ua.h"
62 /* #define DEBUG_CDB_HANDLER */
63 #ifdef DEBUG_CDB_HANDLER
64 #define DEBUG_CDB_H(x...) printk(KERN_INFO x)
65 #else
66 #define DEBUG_CDB_H(x...)
67 #endif
69 /* #define DEBUG_CMD_MAP */
70 #ifdef DEBUG_CMD_MAP
71 #define DEBUG_CMD_M(x...) printk(KERN_INFO x)
72 #else
73 #define DEBUG_CMD_M(x...)
74 #endif
76 /* #define DEBUG_MEM_ALLOC */
77 #ifdef DEBUG_MEM_ALLOC
78 #define DEBUG_MEM(x...) printk(KERN_INFO x)
79 #else
80 #define DEBUG_MEM(x...)
81 #endif
83 /* #define DEBUG_MEM2_ALLOC */
84 #ifdef DEBUG_MEM2_ALLOC
85 #define DEBUG_MEM2(x...) printk(KERN_INFO x)
86 #else
87 #define DEBUG_MEM2(x...)
88 #endif
90 /* #define DEBUG_SG_CALC */
91 #ifdef DEBUG_SG_CALC
92 #define DEBUG_SC(x...) printk(KERN_INFO x)
93 #else
94 #define DEBUG_SC(x...)
95 #endif
97 /* #define DEBUG_SE_OBJ */
98 #ifdef DEBUG_SE_OBJ
99 #define DEBUG_SO(x...) printk(KERN_INFO x)
100 #else
101 #define DEBUG_SO(x...)
102 #endif
104 /* #define DEBUG_CMD_VOL */
105 #ifdef DEBUG_CMD_VOL
106 #define DEBUG_VOL(x...) printk(KERN_INFO x)
107 #else
108 #define DEBUG_VOL(x...)
109 #endif
111 /* #define DEBUG_CMD_STOP */
112 #ifdef DEBUG_CMD_STOP
113 #define DEBUG_CS(x...) printk(KERN_INFO x)
114 #else
115 #define DEBUG_CS(x...)
116 #endif
118 /* #define DEBUG_PASSTHROUGH */
119 #ifdef DEBUG_PASSTHROUGH
120 #define DEBUG_PT(x...) printk(KERN_INFO x)
121 #else
122 #define DEBUG_PT(x...)
123 #endif
125 /* #define DEBUG_TASK_STOP */
126 #ifdef DEBUG_TASK_STOP
127 #define DEBUG_TS(x...) printk(KERN_INFO x)
128 #else
129 #define DEBUG_TS(x...)
130 #endif
132 /* #define DEBUG_TRANSPORT_STOP */
133 #ifdef DEBUG_TRANSPORT_STOP
134 #define DEBUG_TRANSPORT_S(x...) printk(KERN_INFO x)
135 #else
136 #define DEBUG_TRANSPORT_S(x...)
137 #endif
139 /* #define DEBUG_TASK_FAILURE */
140 #ifdef DEBUG_TASK_FAILURE
141 #define DEBUG_TF(x...) printk(KERN_INFO x)
142 #else
143 #define DEBUG_TF(x...)
144 #endif
146 /* #define DEBUG_DEV_OFFLINE */
147 #ifdef DEBUG_DEV_OFFLINE
148 #define DEBUG_DO(x...) printk(KERN_INFO x)
149 #else
150 #define DEBUG_DO(x...)
151 #endif
153 /* #define DEBUG_TASK_STATE */
154 #ifdef DEBUG_TASK_STATE
155 #define DEBUG_TSTATE(x...) printk(KERN_INFO x)
156 #else
157 #define DEBUG_TSTATE(x...)
158 #endif
160 /* #define DEBUG_STATUS_THR */
161 #ifdef DEBUG_STATUS_THR
162 #define DEBUG_ST(x...) printk(KERN_INFO x)
163 #else
164 #define DEBUG_ST(x...)
165 #endif
167 /* #define DEBUG_TASK_TIMEOUT */
168 #ifdef DEBUG_TASK_TIMEOUT
169 #define DEBUG_TT(x...) printk(KERN_INFO x)
170 #else
171 #define DEBUG_TT(x...)
172 #endif
174 /* #define DEBUG_GENERIC_REQUEST_FAILURE */
175 #ifdef DEBUG_GENERIC_REQUEST_FAILURE
176 #define DEBUG_GRF(x...) printk(KERN_INFO x)
177 #else
178 #define DEBUG_GRF(x...)
179 #endif
181 /* #define DEBUG_SAM_TASK_ATTRS */
182 #ifdef DEBUG_SAM_TASK_ATTRS
183 #define DEBUG_STA(x...) printk(KERN_INFO x)
184 #else
185 #define DEBUG_STA(x...)
186 #endif
188 struct se_global *se_global;
190 static struct kmem_cache *se_cmd_cache;
191 static struct kmem_cache *se_sess_cache;
192 struct kmem_cache *se_tmr_req_cache;
193 struct kmem_cache *se_ua_cache;
194 struct kmem_cache *se_mem_cache;
195 struct kmem_cache *t10_pr_reg_cache;
196 struct kmem_cache *t10_alua_lu_gp_cache;
197 struct kmem_cache *t10_alua_lu_gp_mem_cache;
198 struct kmem_cache *t10_alua_tg_pt_gp_cache;
199 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
201 /* Used for transport_dev_get_map_*() */
202 typedef int (*map_func_t)(struct se_task *, u32);
204 static int transport_generic_write_pending(struct se_cmd *);
205 static int transport_processing_thread(void *);
206 static int __transport_execute_tasks(struct se_device *dev);
207 static void transport_complete_task_attr(struct se_cmd *cmd);
208 static void transport_direct_request_timeout(struct se_cmd *cmd);
209 static void transport_free_dev_tasks(struct se_cmd *cmd);
210 static u32 transport_generic_get_cdb_count(struct se_cmd *cmd,
211 unsigned long long starting_lba, u32 sectors,
212 enum dma_data_direction data_direction,
213 struct list_head *mem_list, int set_counts);
214 static int transport_generic_get_mem(struct se_cmd *cmd, u32 length,
215 u32 dma_size);
216 static int transport_generic_remove(struct se_cmd *cmd,
217 int release_to_pool, int session_reinstatement);
218 static int transport_get_sectors(struct se_cmd *cmd);
219 static struct list_head *transport_init_se_mem_list(void);
220 static int transport_map_sg_to_mem(struct se_cmd *cmd,
221 struct list_head *se_mem_list, void *in_mem,
222 u32 *se_mem_cnt);
223 static void transport_memcpy_se_mem_read_contig(struct se_cmd *cmd,
224 unsigned char *dst, struct list_head *se_mem_list);
225 static void transport_release_fe_cmd(struct se_cmd *cmd);
226 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
227 struct se_queue_obj *qobj);
228 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
229 static void transport_stop_all_task_timers(struct se_cmd *cmd);
231 int transport_emulate_control_cdb(struct se_task *task);
233 int init_se_global(void)
235 struct se_global *global;
237 global = kzalloc(sizeof(struct se_global), GFP_KERNEL);
238 if (!(global)) {
239 printk(KERN_ERR "Unable to allocate memory for struct se_global\n");
240 return -1;
243 INIT_LIST_HEAD(&global->g_lu_gps_list);
244 INIT_LIST_HEAD(&global->g_se_tpg_list);
245 INIT_LIST_HEAD(&global->g_hba_list);
246 INIT_LIST_HEAD(&global->g_se_dev_list);
247 spin_lock_init(&global->g_device_lock);
248 spin_lock_init(&global->hba_lock);
249 spin_lock_init(&global->se_tpg_lock);
250 spin_lock_init(&global->lu_gps_lock);
251 spin_lock_init(&global->plugin_class_lock);
253 se_cmd_cache = kmem_cache_create("se_cmd_cache",
254 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
255 if (!(se_cmd_cache)) {
256 printk(KERN_ERR "kmem_cache_create for struct se_cmd failed\n");
257 goto out;
259 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
260 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
261 0, NULL);
262 if (!(se_tmr_req_cache)) {
263 printk(KERN_ERR "kmem_cache_create() for struct se_tmr_req"
264 " failed\n");
265 goto out;
267 se_sess_cache = kmem_cache_create("se_sess_cache",
268 sizeof(struct se_session), __alignof__(struct se_session),
269 0, NULL);
270 if (!(se_sess_cache)) {
271 printk(KERN_ERR "kmem_cache_create() for struct se_session"
272 " failed\n");
273 goto out;
275 se_ua_cache = kmem_cache_create("se_ua_cache",
276 sizeof(struct se_ua), __alignof__(struct se_ua),
277 0, NULL);
278 if (!(se_ua_cache)) {
279 printk(KERN_ERR "kmem_cache_create() for struct se_ua failed\n");
280 goto out;
282 se_mem_cache = kmem_cache_create("se_mem_cache",
283 sizeof(struct se_mem), __alignof__(struct se_mem), 0, NULL);
284 if (!(se_mem_cache)) {
285 printk(KERN_ERR "kmem_cache_create() for struct se_mem failed\n");
286 goto out;
288 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
289 sizeof(struct t10_pr_registration),
290 __alignof__(struct t10_pr_registration), 0, NULL);
291 if (!(t10_pr_reg_cache)) {
292 printk(KERN_ERR "kmem_cache_create() for struct t10_pr_registration"
293 " failed\n");
294 goto out;
296 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
297 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
298 0, NULL);
299 if (!(t10_alua_lu_gp_cache)) {
300 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_cache"
301 " failed\n");
302 goto out;
304 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
305 sizeof(struct t10_alua_lu_gp_member),
306 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
307 if (!(t10_alua_lu_gp_mem_cache)) {
308 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_mem_"
309 "cache failed\n");
310 goto out;
312 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
313 sizeof(struct t10_alua_tg_pt_gp),
314 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
315 if (!(t10_alua_tg_pt_gp_cache)) {
316 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
317 "cache failed\n");
318 goto out;
320 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
321 "t10_alua_tg_pt_gp_mem_cache",
322 sizeof(struct t10_alua_tg_pt_gp_member),
323 __alignof__(struct t10_alua_tg_pt_gp_member),
324 0, NULL);
325 if (!(t10_alua_tg_pt_gp_mem_cache)) {
326 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
327 "mem_t failed\n");
328 goto out;
331 se_global = global;
333 return 0;
334 out:
335 if (se_cmd_cache)
336 kmem_cache_destroy(se_cmd_cache);
337 if (se_tmr_req_cache)
338 kmem_cache_destroy(se_tmr_req_cache);
339 if (se_sess_cache)
340 kmem_cache_destroy(se_sess_cache);
341 if (se_ua_cache)
342 kmem_cache_destroy(se_ua_cache);
343 if (se_mem_cache)
344 kmem_cache_destroy(se_mem_cache);
345 if (t10_pr_reg_cache)
346 kmem_cache_destroy(t10_pr_reg_cache);
347 if (t10_alua_lu_gp_cache)
348 kmem_cache_destroy(t10_alua_lu_gp_cache);
349 if (t10_alua_lu_gp_mem_cache)
350 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
351 if (t10_alua_tg_pt_gp_cache)
352 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
353 if (t10_alua_tg_pt_gp_mem_cache)
354 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
355 kfree(global);
356 return -1;
359 void release_se_global(void)
361 struct se_global *global;
363 global = se_global;
364 if (!(global))
365 return;
367 kmem_cache_destroy(se_cmd_cache);
368 kmem_cache_destroy(se_tmr_req_cache);
369 kmem_cache_destroy(se_sess_cache);
370 kmem_cache_destroy(se_ua_cache);
371 kmem_cache_destroy(se_mem_cache);
372 kmem_cache_destroy(t10_pr_reg_cache);
373 kmem_cache_destroy(t10_alua_lu_gp_cache);
374 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
375 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
376 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
377 kfree(global);
379 se_global = NULL;
382 void transport_init_queue_obj(struct se_queue_obj *qobj)
384 atomic_set(&qobj->queue_cnt, 0);
385 INIT_LIST_HEAD(&qobj->qobj_list);
386 init_waitqueue_head(&qobj->thread_wq);
387 spin_lock_init(&qobj->cmd_queue_lock);
389 EXPORT_SYMBOL(transport_init_queue_obj);
391 static int transport_subsystem_reqmods(void)
393 int ret;
395 ret = request_module("target_core_iblock");
396 if (ret != 0)
397 printk(KERN_ERR "Unable to load target_core_iblock\n");
399 ret = request_module("target_core_file");
400 if (ret != 0)
401 printk(KERN_ERR "Unable to load target_core_file\n");
403 ret = request_module("target_core_pscsi");
404 if (ret != 0)
405 printk(KERN_ERR "Unable to load target_core_pscsi\n");
407 ret = request_module("target_core_stgt");
408 if (ret != 0)
409 printk(KERN_ERR "Unable to load target_core_stgt\n");
411 return 0;
414 int transport_subsystem_check_init(void)
416 if (se_global->g_sub_api_initialized)
417 return 0;
419 * Request the loading of known TCM subsystem plugins..
421 if (transport_subsystem_reqmods() < 0)
422 return -1;
424 se_global->g_sub_api_initialized = 1;
425 return 0;
428 struct se_session *transport_init_session(void)
430 struct se_session *se_sess;
432 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
433 if (!(se_sess)) {
434 printk(KERN_ERR "Unable to allocate struct se_session from"
435 " se_sess_cache\n");
436 return ERR_PTR(-ENOMEM);
438 INIT_LIST_HEAD(&se_sess->sess_list);
439 INIT_LIST_HEAD(&se_sess->sess_acl_list);
440 atomic_set(&se_sess->mib_ref_count, 0);
442 return se_sess;
444 EXPORT_SYMBOL(transport_init_session);
447 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
449 void __transport_register_session(
450 struct se_portal_group *se_tpg,
451 struct se_node_acl *se_nacl,
452 struct se_session *se_sess,
453 void *fabric_sess_ptr)
455 unsigned char buf[PR_REG_ISID_LEN];
457 se_sess->se_tpg = se_tpg;
458 se_sess->fabric_sess_ptr = fabric_sess_ptr;
460 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
462 * Only set for struct se_session's that will actually be moving I/O.
463 * eg: *NOT* discovery sessions.
465 if (se_nacl) {
467 * If the fabric module supports an ISID based TransportID,
468 * save this value in binary from the fabric I_T Nexus now.
470 if (TPG_TFO(se_tpg)->sess_get_initiator_sid != NULL) {
471 memset(&buf[0], 0, PR_REG_ISID_LEN);
472 TPG_TFO(se_tpg)->sess_get_initiator_sid(se_sess,
473 &buf[0], PR_REG_ISID_LEN);
474 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
476 spin_lock_irq(&se_nacl->nacl_sess_lock);
478 * The se_nacl->nacl_sess pointer will be set to the
479 * last active I_T Nexus for each struct se_node_acl.
481 se_nacl->nacl_sess = se_sess;
483 list_add_tail(&se_sess->sess_acl_list,
484 &se_nacl->acl_sess_list);
485 spin_unlock_irq(&se_nacl->nacl_sess_lock);
487 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
489 printk(KERN_INFO "TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
490 TPG_TFO(se_tpg)->get_fabric_name(), se_sess->fabric_sess_ptr);
492 EXPORT_SYMBOL(__transport_register_session);
494 void transport_register_session(
495 struct se_portal_group *se_tpg,
496 struct se_node_acl *se_nacl,
497 struct se_session *se_sess,
498 void *fabric_sess_ptr)
500 spin_lock_bh(&se_tpg->session_lock);
501 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
502 spin_unlock_bh(&se_tpg->session_lock);
504 EXPORT_SYMBOL(transport_register_session);
506 void transport_deregister_session_configfs(struct se_session *se_sess)
508 struct se_node_acl *se_nacl;
511 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
513 se_nacl = se_sess->se_node_acl;
514 if ((se_nacl)) {
515 spin_lock_irq(&se_nacl->nacl_sess_lock);
516 list_del(&se_sess->sess_acl_list);
518 * If the session list is empty, then clear the pointer.
519 * Otherwise, set the struct se_session pointer from the tail
520 * element of the per struct se_node_acl active session list.
522 if (list_empty(&se_nacl->acl_sess_list))
523 se_nacl->nacl_sess = NULL;
524 else {
525 se_nacl->nacl_sess = container_of(
526 se_nacl->acl_sess_list.prev,
527 struct se_session, sess_acl_list);
529 spin_unlock_irq(&se_nacl->nacl_sess_lock);
532 EXPORT_SYMBOL(transport_deregister_session_configfs);
534 void transport_free_session(struct se_session *se_sess)
536 kmem_cache_free(se_sess_cache, se_sess);
538 EXPORT_SYMBOL(transport_free_session);
540 void transport_deregister_session(struct se_session *se_sess)
542 struct se_portal_group *se_tpg = se_sess->se_tpg;
543 struct se_node_acl *se_nacl;
545 if (!(se_tpg)) {
546 transport_free_session(se_sess);
547 return;
550 * Wait for possible reference in drivers/target/target_core_mib.c:
551 * scsi_att_intr_port_seq_show()
553 while (atomic_read(&se_sess->mib_ref_count) != 0)
554 cpu_relax();
556 spin_lock_bh(&se_tpg->session_lock);
557 list_del(&se_sess->sess_list);
558 se_sess->se_tpg = NULL;
559 se_sess->fabric_sess_ptr = NULL;
560 spin_unlock_bh(&se_tpg->session_lock);
563 * Determine if we need to do extra work for this initiator node's
564 * struct se_node_acl if it had been previously dynamically generated.
566 se_nacl = se_sess->se_node_acl;
567 if ((se_nacl)) {
568 spin_lock_bh(&se_tpg->acl_node_lock);
569 if (se_nacl->dynamic_node_acl) {
570 if (!(TPG_TFO(se_tpg)->tpg_check_demo_mode_cache(
571 se_tpg))) {
572 list_del(&se_nacl->acl_list);
573 se_tpg->num_node_acls--;
574 spin_unlock_bh(&se_tpg->acl_node_lock);
576 core_tpg_wait_for_nacl_pr_ref(se_nacl);
577 core_tpg_wait_for_mib_ref(se_nacl);
578 core_free_device_list_for_node(se_nacl, se_tpg);
579 TPG_TFO(se_tpg)->tpg_release_fabric_acl(se_tpg,
580 se_nacl);
581 spin_lock_bh(&se_tpg->acl_node_lock);
584 spin_unlock_bh(&se_tpg->acl_node_lock);
587 transport_free_session(se_sess);
589 printk(KERN_INFO "TARGET_CORE[%s]: Deregistered fabric_sess\n",
590 TPG_TFO(se_tpg)->get_fabric_name());
592 EXPORT_SYMBOL(transport_deregister_session);
595 * Called with T_TASK(cmd)->t_state_lock held.
597 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
599 struct se_device *dev;
600 struct se_task *task;
601 unsigned long flags;
603 if (!T_TASK(cmd))
604 return;
606 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
607 dev = task->se_dev;
608 if (!(dev))
609 continue;
611 if (atomic_read(&task->task_active))
612 continue;
614 if (!(atomic_read(&task->task_state_active)))
615 continue;
617 spin_lock_irqsave(&dev->execute_task_lock, flags);
618 list_del(&task->t_state_list);
619 DEBUG_TSTATE("Removed ITT: 0x%08x dev: %p task[%p]\n",
620 CMD_TFO(cmd)->tfo_get_task_tag(cmd), dev, task);
621 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
623 atomic_set(&task->task_state_active, 0);
624 atomic_dec(&T_TASK(cmd)->t_task_cdbs_ex_left);
628 /* transport_cmd_check_stop():
630 * 'transport_off = 1' determines if t_transport_active should be cleared.
631 * 'transport_off = 2' determines if task_dev_state should be removed.
633 * A non-zero u8 t_state sets cmd->t_state.
634 * Returns 1 when command is stopped, else 0.
636 static int transport_cmd_check_stop(
637 struct se_cmd *cmd,
638 int transport_off,
639 u8 t_state)
641 unsigned long flags;
643 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
645 * Determine if IOCTL context caller in requesting the stopping of this
646 * command for LUN shutdown purposes.
648 if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
649 DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->transport_lun_stop)"
650 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
651 CMD_TFO(cmd)->get_task_tag(cmd));
653 cmd->deferred_t_state = cmd->t_state;
654 cmd->t_state = TRANSPORT_DEFERRED_CMD;
655 atomic_set(&T_TASK(cmd)->t_transport_active, 0);
656 if (transport_off == 2)
657 transport_all_task_dev_remove_state(cmd);
658 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
660 complete(&T_TASK(cmd)->transport_lun_stop_comp);
661 return 1;
664 * Determine if frontend context caller is requesting the stopping of
665 * this command for frontend excpections.
667 if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
668 DEBUG_CS("%s:%d atomic_read(&T_TASK(cmd)->t_transport_stop) =="
669 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
670 CMD_TFO(cmd)->get_task_tag(cmd));
672 cmd->deferred_t_state = cmd->t_state;
673 cmd->t_state = TRANSPORT_DEFERRED_CMD;
674 if (transport_off == 2)
675 transport_all_task_dev_remove_state(cmd);
678 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
679 * to FE.
681 if (transport_off == 2)
682 cmd->se_lun = NULL;
683 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
685 complete(&T_TASK(cmd)->t_transport_stop_comp);
686 return 1;
688 if (transport_off) {
689 atomic_set(&T_TASK(cmd)->t_transport_active, 0);
690 if (transport_off == 2) {
691 transport_all_task_dev_remove_state(cmd);
693 * Clear struct se_cmd->se_lun before the transport_off == 2
694 * handoff to fabric module.
696 cmd->se_lun = NULL;
698 * Some fabric modules like tcm_loop can release
699 * their internally allocated I/O refrence now and
700 * struct se_cmd now.
702 if (CMD_TFO(cmd)->check_stop_free != NULL) {
703 spin_unlock_irqrestore(
704 &T_TASK(cmd)->t_state_lock, flags);
706 CMD_TFO(cmd)->check_stop_free(cmd);
707 return 1;
710 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
712 return 0;
713 } else if (t_state)
714 cmd->t_state = t_state;
715 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
717 return 0;
720 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
722 return transport_cmd_check_stop(cmd, 2, 0);
725 static void transport_lun_remove_cmd(struct se_cmd *cmd)
727 struct se_lun *lun = SE_LUN(cmd);
728 unsigned long flags;
730 if (!lun)
731 return;
733 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
734 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
735 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
736 goto check_lun;
738 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
739 transport_all_task_dev_remove_state(cmd);
740 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
742 transport_free_dev_tasks(cmd);
744 check_lun:
745 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
746 if (atomic_read(&T_TASK(cmd)->transport_lun_active)) {
747 list_del(&cmd->se_lun_list);
748 atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
749 #if 0
750 printk(KERN_INFO "Removed ITT: 0x%08x from LUN LIST[%d]\n"
751 CMD_TFO(cmd)->get_task_tag(cmd), lun->unpacked_lun);
752 #endif
754 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
757 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
759 transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
760 transport_lun_remove_cmd(cmd);
762 if (transport_cmd_check_stop_to_fabric(cmd))
763 return;
764 if (remove)
765 transport_generic_remove(cmd, 0, 0);
768 void transport_cmd_finish_abort_tmr(struct se_cmd *cmd)
770 transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
772 if (transport_cmd_check_stop_to_fabric(cmd))
773 return;
775 transport_generic_remove(cmd, 0, 0);
778 static int transport_add_cmd_to_queue(
779 struct se_cmd *cmd,
780 int t_state)
782 struct se_device *dev = cmd->se_dev;
783 struct se_queue_obj *qobj = dev->dev_queue_obj;
784 struct se_queue_req *qr;
785 unsigned long flags;
787 qr = kzalloc(sizeof(struct se_queue_req), GFP_ATOMIC);
788 if (!(qr)) {
789 printk(KERN_ERR "Unable to allocate memory for"
790 " struct se_queue_req\n");
791 return -1;
793 INIT_LIST_HEAD(&qr->qr_list);
795 qr->cmd = (void *)cmd;
796 qr->state = t_state;
798 if (t_state) {
799 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
800 cmd->t_state = t_state;
801 atomic_set(&T_TASK(cmd)->t_transport_active, 1);
802 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
805 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
806 list_add_tail(&qr->qr_list, &qobj->qobj_list);
807 atomic_inc(&T_TASK(cmd)->t_transport_queue_active);
808 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
810 atomic_inc(&qobj->queue_cnt);
811 wake_up_interruptible(&qobj->thread_wq);
812 return 0;
816 * Called with struct se_queue_obj->cmd_queue_lock held.
818 static struct se_queue_req *
819 __transport_get_qr_from_queue(struct se_queue_obj *qobj)
821 struct se_cmd *cmd;
822 struct se_queue_req *qr = NULL;
824 if (list_empty(&qobj->qobj_list))
825 return NULL;
827 list_for_each_entry(qr, &qobj->qobj_list, qr_list)
828 break;
830 if (qr->cmd) {
831 cmd = (struct se_cmd *)qr->cmd;
832 atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
834 list_del(&qr->qr_list);
835 atomic_dec(&qobj->queue_cnt);
837 return qr;
840 static struct se_queue_req *
841 transport_get_qr_from_queue(struct se_queue_obj *qobj)
843 struct se_cmd *cmd;
844 struct se_queue_req *qr;
845 unsigned long flags;
847 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
848 if (list_empty(&qobj->qobj_list)) {
849 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
850 return NULL;
853 list_for_each_entry(qr, &qobj->qobj_list, qr_list)
854 break;
856 if (qr->cmd) {
857 cmd = (struct se_cmd *)qr->cmd;
858 atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
860 list_del(&qr->qr_list);
861 atomic_dec(&qobj->queue_cnt);
862 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
864 return qr;
867 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
868 struct se_queue_obj *qobj)
870 struct se_cmd *q_cmd;
871 struct se_queue_req *qr = NULL, *qr_p = NULL;
872 unsigned long flags;
874 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
875 if (!(atomic_read(&T_TASK(cmd)->t_transport_queue_active))) {
876 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
877 return;
880 list_for_each_entry_safe(qr, qr_p, &qobj->qobj_list, qr_list) {
881 q_cmd = (struct se_cmd *)qr->cmd;
882 if (q_cmd != cmd)
883 continue;
885 atomic_dec(&T_TASK(q_cmd)->t_transport_queue_active);
886 atomic_dec(&qobj->queue_cnt);
887 list_del(&qr->qr_list);
888 kfree(qr);
890 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
892 if (atomic_read(&T_TASK(cmd)->t_transport_queue_active)) {
893 printk(KERN_ERR "ITT: 0x%08x t_transport_queue_active: %d\n",
894 CMD_TFO(cmd)->get_task_tag(cmd),
895 atomic_read(&T_TASK(cmd)->t_transport_queue_active));
900 * Completion function used by TCM subsystem plugins (such as FILEIO)
901 * for queueing up response from struct se_subsystem_api->do_task()
903 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
905 struct se_task *task = list_entry(T_TASK(cmd)->t_task_list.next,
906 struct se_task, t_list);
908 if (good) {
909 cmd->scsi_status = SAM_STAT_GOOD;
910 task->task_scsi_status = GOOD;
911 } else {
912 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
913 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
914 TASK_CMD(task)->transport_error_status =
915 PYX_TRANSPORT_ILLEGAL_REQUEST;
918 transport_complete_task(task, good);
920 EXPORT_SYMBOL(transport_complete_sync_cache);
922 /* transport_complete_task():
924 * Called from interrupt and non interrupt context depending
925 * on the transport plugin.
927 void transport_complete_task(struct se_task *task, int success)
929 struct se_cmd *cmd = TASK_CMD(task);
930 struct se_device *dev = task->se_dev;
931 int t_state;
932 unsigned long flags;
933 #if 0
934 printk(KERN_INFO "task: %p CDB: 0x%02x obj_ptr: %p\n", task,
935 T_TASK(cmd)->t_task_cdb[0], dev);
936 #endif
937 if (dev) {
938 spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
939 atomic_inc(&dev->depth_left);
940 atomic_inc(&SE_HBA(dev)->left_queue_depth);
941 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
944 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
945 atomic_set(&task->task_active, 0);
948 * See if any sense data exists, if so set the TASK_SENSE flag.
949 * Also check for any other post completion work that needs to be
950 * done by the plugins.
952 if (dev && dev->transport->transport_complete) {
953 if (dev->transport->transport_complete(task) != 0) {
954 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
955 task->task_sense = 1;
956 success = 1;
961 * See if we are waiting for outstanding struct se_task
962 * to complete for an exception condition
964 if (atomic_read(&task->task_stop)) {
966 * Decrement T_TASK(cmd)->t_se_count if this task had
967 * previously thrown its timeout exception handler.
969 if (atomic_read(&task->task_timeout)) {
970 atomic_dec(&T_TASK(cmd)->t_se_count);
971 atomic_set(&task->task_timeout, 0);
973 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
975 complete(&task->task_stop_comp);
976 return;
979 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
980 * left counter to determine when the struct se_cmd is ready to be queued to
981 * the processing thread.
983 if (atomic_read(&task->task_timeout)) {
984 if (!(atomic_dec_and_test(
985 &T_TASK(cmd)->t_task_cdbs_timeout_left))) {
986 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
987 flags);
988 return;
990 t_state = TRANSPORT_COMPLETE_TIMEOUT;
991 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
993 transport_add_cmd_to_queue(cmd, t_state);
994 return;
996 atomic_dec(&T_TASK(cmd)->t_task_cdbs_timeout_left);
999 * Decrement the outstanding t_task_cdbs_left count. The last
1000 * struct se_task from struct se_cmd will complete itself into the
1001 * device queue depending upon int success.
1003 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
1004 if (!success)
1005 T_TASK(cmd)->t_tasks_failed = 1;
1007 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1008 return;
1011 if (!success || T_TASK(cmd)->t_tasks_failed) {
1012 t_state = TRANSPORT_COMPLETE_FAILURE;
1013 if (!task->task_error_status) {
1014 task->task_error_status =
1015 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1016 cmd->transport_error_status =
1017 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
1019 } else {
1020 atomic_set(&T_TASK(cmd)->t_transport_complete, 1);
1021 t_state = TRANSPORT_COMPLETE_OK;
1023 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1025 transport_add_cmd_to_queue(cmd, t_state);
1027 EXPORT_SYMBOL(transport_complete_task);
1030 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
1031 * struct se_task list are ready to be added to the active execution list
1032 * struct se_device
1034 * Called with se_dev_t->execute_task_lock called.
1036 static inline int transport_add_task_check_sam_attr(
1037 struct se_task *task,
1038 struct se_task *task_prev,
1039 struct se_device *dev)
1042 * No SAM Task attribute emulation enabled, add to tail of
1043 * execution queue
1045 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
1046 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
1047 return 0;
1050 * HEAD_OF_QUEUE attribute for received CDB, which means
1051 * the first task that is associated with a struct se_cmd goes to
1052 * head of the struct se_device->execute_task_list, and task_prev
1053 * after that for each subsequent task
1055 if (task->task_se_cmd->sam_task_attr == TASK_ATTR_HOQ) {
1056 list_add(&task->t_execute_list,
1057 (task_prev != NULL) ?
1058 &task_prev->t_execute_list :
1059 &dev->execute_task_list);
1061 DEBUG_STA("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
1062 " in execution queue\n",
1063 T_TASK(task->task_se_cmd)->t_task_cdb[0]);
1064 return 1;
1067 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
1068 * transitioned from Dermant -> Active state, and are added to the end
1069 * of the struct se_device->execute_task_list
1071 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
1072 return 0;
1075 /* __transport_add_task_to_execute_queue():
1077 * Called with se_dev_t->execute_task_lock called.
1079 static void __transport_add_task_to_execute_queue(
1080 struct se_task *task,
1081 struct se_task *task_prev,
1082 struct se_device *dev)
1084 int head_of_queue;
1086 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
1087 atomic_inc(&dev->execute_tasks);
1089 if (atomic_read(&task->task_state_active))
1090 return;
1092 * Determine if this task needs to go to HEAD_OF_QUEUE for the
1093 * state list as well. Running with SAM Task Attribute emulation
1094 * will always return head_of_queue == 0 here
1096 if (head_of_queue)
1097 list_add(&task->t_state_list, (task_prev) ?
1098 &task_prev->t_state_list :
1099 &dev->state_task_list);
1100 else
1101 list_add_tail(&task->t_state_list, &dev->state_task_list);
1103 atomic_set(&task->task_state_active, 1);
1105 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1106 CMD_TFO(task->task_se_cmd)->get_task_tag(task->task_se_cmd),
1107 task, dev);
1110 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
1112 struct se_device *dev;
1113 struct se_task *task;
1114 unsigned long flags;
1116 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
1117 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
1118 dev = task->se_dev;
1120 if (atomic_read(&task->task_state_active))
1121 continue;
1123 spin_lock(&dev->execute_task_lock);
1124 list_add_tail(&task->t_state_list, &dev->state_task_list);
1125 atomic_set(&task->task_state_active, 1);
1127 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1128 CMD_TFO(task->task_se_cmd)->get_task_tag(
1129 task->task_se_cmd), task, dev);
1131 spin_unlock(&dev->execute_task_lock);
1133 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1136 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
1138 struct se_device *dev = SE_DEV(cmd);
1139 struct se_task *task, *task_prev = NULL;
1140 unsigned long flags;
1142 spin_lock_irqsave(&dev->execute_task_lock, flags);
1143 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
1144 if (atomic_read(&task->task_execute_queue))
1145 continue;
1147 * __transport_add_task_to_execute_queue() handles the
1148 * SAM Task Attribute emulation if enabled
1150 __transport_add_task_to_execute_queue(task, task_prev, dev);
1151 atomic_set(&task->task_execute_queue, 1);
1152 task_prev = task;
1154 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1156 return;
1159 /* transport_get_task_from_execute_queue():
1161 * Called with dev->execute_task_lock held.
1163 static struct se_task *
1164 transport_get_task_from_execute_queue(struct se_device *dev)
1166 struct se_task *task;
1168 if (list_empty(&dev->execute_task_list))
1169 return NULL;
1171 list_for_each_entry(task, &dev->execute_task_list, t_execute_list)
1172 break;
1174 list_del(&task->t_execute_list);
1175 atomic_dec(&dev->execute_tasks);
1177 return task;
1180 /* transport_remove_task_from_execute_queue():
1184 static void transport_remove_task_from_execute_queue(
1185 struct se_task *task,
1186 struct se_device *dev)
1188 unsigned long flags;
1190 spin_lock_irqsave(&dev->execute_task_lock, flags);
1191 list_del(&task->t_execute_list);
1192 atomic_dec(&dev->execute_tasks);
1193 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1196 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
1198 switch (cmd->data_direction) {
1199 case DMA_NONE:
1200 return "NONE";
1201 case DMA_FROM_DEVICE:
1202 return "READ";
1203 case DMA_TO_DEVICE:
1204 return "WRITE";
1205 case DMA_BIDIRECTIONAL:
1206 return "BIDI";
1207 default:
1208 break;
1211 return "UNKNOWN";
1214 void transport_dump_dev_state(
1215 struct se_device *dev,
1216 char *b,
1217 int *bl)
1219 *bl += sprintf(b + *bl, "Status: ");
1220 switch (dev->dev_status) {
1221 case TRANSPORT_DEVICE_ACTIVATED:
1222 *bl += sprintf(b + *bl, "ACTIVATED");
1223 break;
1224 case TRANSPORT_DEVICE_DEACTIVATED:
1225 *bl += sprintf(b + *bl, "DEACTIVATED");
1226 break;
1227 case TRANSPORT_DEVICE_SHUTDOWN:
1228 *bl += sprintf(b + *bl, "SHUTDOWN");
1229 break;
1230 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
1231 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
1232 *bl += sprintf(b + *bl, "OFFLINE");
1233 break;
1234 default:
1235 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
1236 break;
1239 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
1240 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
1241 dev->queue_depth);
1242 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
1243 DEV_ATTRIB(dev)->block_size, DEV_ATTRIB(dev)->max_sectors);
1244 *bl += sprintf(b + *bl, " ");
1247 /* transport_release_all_cmds():
1251 static void transport_release_all_cmds(struct se_device *dev)
1253 struct se_cmd *cmd = NULL;
1254 struct se_queue_req *qr = NULL, *qr_p = NULL;
1255 int bug_out = 0, t_state;
1256 unsigned long flags;
1258 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
1259 list_for_each_entry_safe(qr, qr_p, &dev->dev_queue_obj->qobj_list,
1260 qr_list) {
1262 cmd = (struct se_cmd *)qr->cmd;
1263 t_state = qr->state;
1264 list_del(&qr->qr_list);
1265 kfree(qr);
1266 spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock,
1267 flags);
1269 printk(KERN_ERR "Releasing ITT: 0x%08x, i_state: %u,"
1270 " t_state: %u directly\n",
1271 CMD_TFO(cmd)->get_task_tag(cmd),
1272 CMD_TFO(cmd)->get_cmd_state(cmd), t_state);
1274 transport_release_fe_cmd(cmd);
1275 bug_out = 1;
1277 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
1279 spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
1280 #if 0
1281 if (bug_out)
1282 BUG();
1283 #endif
1286 void transport_dump_vpd_proto_id(
1287 struct t10_vpd *vpd,
1288 unsigned char *p_buf,
1289 int p_buf_len)
1291 unsigned char buf[VPD_TMP_BUF_SIZE];
1292 int len;
1294 memset(buf, 0, VPD_TMP_BUF_SIZE);
1295 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1297 switch (vpd->protocol_identifier) {
1298 case 0x00:
1299 sprintf(buf+len, "Fibre Channel\n");
1300 break;
1301 case 0x10:
1302 sprintf(buf+len, "Parallel SCSI\n");
1303 break;
1304 case 0x20:
1305 sprintf(buf+len, "SSA\n");
1306 break;
1307 case 0x30:
1308 sprintf(buf+len, "IEEE 1394\n");
1309 break;
1310 case 0x40:
1311 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1312 " Protocol\n");
1313 break;
1314 case 0x50:
1315 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1316 break;
1317 case 0x60:
1318 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1319 break;
1320 case 0x70:
1321 sprintf(buf+len, "Automation/Drive Interface Transport"
1322 " Protocol\n");
1323 break;
1324 case 0x80:
1325 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1326 break;
1327 default:
1328 sprintf(buf+len, "Unknown 0x%02x\n",
1329 vpd->protocol_identifier);
1330 break;
1333 if (p_buf)
1334 strncpy(p_buf, buf, p_buf_len);
1335 else
1336 printk(KERN_INFO "%s", buf);
1339 void
1340 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1343 * Check if the Protocol Identifier Valid (PIV) bit is set..
1345 * from spc3r23.pdf section 7.5.1
1347 if (page_83[1] & 0x80) {
1348 vpd->protocol_identifier = (page_83[0] & 0xf0);
1349 vpd->protocol_identifier_set = 1;
1350 transport_dump_vpd_proto_id(vpd, NULL, 0);
1353 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1355 int transport_dump_vpd_assoc(
1356 struct t10_vpd *vpd,
1357 unsigned char *p_buf,
1358 int p_buf_len)
1360 unsigned char buf[VPD_TMP_BUF_SIZE];
1361 int ret = 0, len;
1363 memset(buf, 0, VPD_TMP_BUF_SIZE);
1364 len = sprintf(buf, "T10 VPD Identifier Association: ");
1366 switch (vpd->association) {
1367 case 0x00:
1368 sprintf(buf+len, "addressed logical unit\n");
1369 break;
1370 case 0x10:
1371 sprintf(buf+len, "target port\n");
1372 break;
1373 case 0x20:
1374 sprintf(buf+len, "SCSI target device\n");
1375 break;
1376 default:
1377 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1378 ret = -1;
1379 break;
1382 if (p_buf)
1383 strncpy(p_buf, buf, p_buf_len);
1384 else
1385 printk("%s", buf);
1387 return ret;
1390 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1393 * The VPD identification association..
1395 * from spc3r23.pdf Section 7.6.3.1 Table 297
1397 vpd->association = (page_83[1] & 0x30);
1398 return transport_dump_vpd_assoc(vpd, NULL, 0);
1400 EXPORT_SYMBOL(transport_set_vpd_assoc);
1402 int transport_dump_vpd_ident_type(
1403 struct t10_vpd *vpd,
1404 unsigned char *p_buf,
1405 int p_buf_len)
1407 unsigned char buf[VPD_TMP_BUF_SIZE];
1408 int ret = 0, len;
1410 memset(buf, 0, VPD_TMP_BUF_SIZE);
1411 len = sprintf(buf, "T10 VPD Identifier Type: ");
1413 switch (vpd->device_identifier_type) {
1414 case 0x00:
1415 sprintf(buf+len, "Vendor specific\n");
1416 break;
1417 case 0x01:
1418 sprintf(buf+len, "T10 Vendor ID based\n");
1419 break;
1420 case 0x02:
1421 sprintf(buf+len, "EUI-64 based\n");
1422 break;
1423 case 0x03:
1424 sprintf(buf+len, "NAA\n");
1425 break;
1426 case 0x04:
1427 sprintf(buf+len, "Relative target port identifier\n");
1428 break;
1429 case 0x08:
1430 sprintf(buf+len, "SCSI name string\n");
1431 break;
1432 default:
1433 sprintf(buf+len, "Unsupported: 0x%02x\n",
1434 vpd->device_identifier_type);
1435 ret = -1;
1436 break;
1439 if (p_buf)
1440 strncpy(p_buf, buf, p_buf_len);
1441 else
1442 printk("%s", buf);
1444 return ret;
1447 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1450 * The VPD identifier type..
1452 * from spc3r23.pdf Section 7.6.3.1 Table 298
1454 vpd->device_identifier_type = (page_83[1] & 0x0f);
1455 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1457 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1459 int transport_dump_vpd_ident(
1460 struct t10_vpd *vpd,
1461 unsigned char *p_buf,
1462 int p_buf_len)
1464 unsigned char buf[VPD_TMP_BUF_SIZE];
1465 int ret = 0;
1467 memset(buf, 0, VPD_TMP_BUF_SIZE);
1469 switch (vpd->device_identifier_code_set) {
1470 case 0x01: /* Binary */
1471 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1472 &vpd->device_identifier[0]);
1473 break;
1474 case 0x02: /* ASCII */
1475 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1476 &vpd->device_identifier[0]);
1477 break;
1478 case 0x03: /* UTF-8 */
1479 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1480 &vpd->device_identifier[0]);
1481 break;
1482 default:
1483 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1484 " 0x%02x", vpd->device_identifier_code_set);
1485 ret = -1;
1486 break;
1489 if (p_buf)
1490 strncpy(p_buf, buf, p_buf_len);
1491 else
1492 printk("%s", buf);
1494 return ret;
1498 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1500 static const char hex_str[] = "0123456789abcdef";
1501 int j = 0, i = 4; /* offset to start of the identifer */
1504 * The VPD Code Set (encoding)
1506 * from spc3r23.pdf Section 7.6.3.1 Table 296
1508 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1509 switch (vpd->device_identifier_code_set) {
1510 case 0x01: /* Binary */
1511 vpd->device_identifier[j++] =
1512 hex_str[vpd->device_identifier_type];
1513 while (i < (4 + page_83[3])) {
1514 vpd->device_identifier[j++] =
1515 hex_str[(page_83[i] & 0xf0) >> 4];
1516 vpd->device_identifier[j++] =
1517 hex_str[page_83[i] & 0x0f];
1518 i++;
1520 break;
1521 case 0x02: /* ASCII */
1522 case 0x03: /* UTF-8 */
1523 while (i < (4 + page_83[3]))
1524 vpd->device_identifier[j++] = page_83[i++];
1525 break;
1526 default:
1527 break;
1530 return transport_dump_vpd_ident(vpd, NULL, 0);
1532 EXPORT_SYMBOL(transport_set_vpd_ident);
1534 static void core_setup_task_attr_emulation(struct se_device *dev)
1537 * If this device is from Target_Core_Mod/pSCSI, disable the
1538 * SAM Task Attribute emulation.
1540 * This is currently not available in upsream Linux/SCSI Target
1541 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1543 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1544 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1545 return;
1548 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1549 DEBUG_STA("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1550 " device\n", TRANSPORT(dev)->name,
1551 TRANSPORT(dev)->get_device_rev(dev));
1554 static void scsi_dump_inquiry(struct se_device *dev)
1556 struct t10_wwn *wwn = DEV_T10_WWN(dev);
1557 int i, device_type;
1559 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1561 printk(" Vendor: ");
1562 for (i = 0; i < 8; i++)
1563 if (wwn->vendor[i] >= 0x20)
1564 printk("%c", wwn->vendor[i]);
1565 else
1566 printk(" ");
1568 printk(" Model: ");
1569 for (i = 0; i < 16; i++)
1570 if (wwn->model[i] >= 0x20)
1571 printk("%c", wwn->model[i]);
1572 else
1573 printk(" ");
1575 printk(" Revision: ");
1576 for (i = 0; i < 4; i++)
1577 if (wwn->revision[i] >= 0x20)
1578 printk("%c", wwn->revision[i]);
1579 else
1580 printk(" ");
1582 printk("\n");
1584 device_type = TRANSPORT(dev)->get_device_type(dev);
1585 printk(" Type: %s ", scsi_device_type(device_type));
1586 printk(" ANSI SCSI revision: %02x\n",
1587 TRANSPORT(dev)->get_device_rev(dev));
1590 struct se_device *transport_add_device_to_core_hba(
1591 struct se_hba *hba,
1592 struct se_subsystem_api *transport,
1593 struct se_subsystem_dev *se_dev,
1594 u32 device_flags,
1595 void *transport_dev,
1596 struct se_dev_limits *dev_limits,
1597 const char *inquiry_prod,
1598 const char *inquiry_rev)
1600 int ret = 0, force_pt;
1601 struct se_device *dev;
1603 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1604 if (!(dev)) {
1605 printk(KERN_ERR "Unable to allocate memory for se_dev_t\n");
1606 return NULL;
1608 dev->dev_queue_obj = kzalloc(sizeof(struct se_queue_obj), GFP_KERNEL);
1609 if (!(dev->dev_queue_obj)) {
1610 printk(KERN_ERR "Unable to allocate memory for"
1611 " dev->dev_queue_obj\n");
1612 kfree(dev);
1613 return NULL;
1615 transport_init_queue_obj(dev->dev_queue_obj);
1617 dev->dev_status_queue_obj = kzalloc(sizeof(struct se_queue_obj),
1618 GFP_KERNEL);
1619 if (!(dev->dev_status_queue_obj)) {
1620 printk(KERN_ERR "Unable to allocate memory for"
1621 " dev->dev_status_queue_obj\n");
1622 kfree(dev->dev_queue_obj);
1623 kfree(dev);
1624 return NULL;
1626 transport_init_queue_obj(dev->dev_status_queue_obj);
1628 dev->dev_flags = device_flags;
1629 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1630 dev->dev_ptr = (void *) transport_dev;
1631 dev->se_hba = hba;
1632 dev->se_sub_dev = se_dev;
1633 dev->transport = transport;
1634 atomic_set(&dev->active_cmds, 0);
1635 INIT_LIST_HEAD(&dev->dev_list);
1636 INIT_LIST_HEAD(&dev->dev_sep_list);
1637 INIT_LIST_HEAD(&dev->dev_tmr_list);
1638 INIT_LIST_HEAD(&dev->execute_task_list);
1639 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1640 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1641 INIT_LIST_HEAD(&dev->state_task_list);
1642 spin_lock_init(&dev->execute_task_lock);
1643 spin_lock_init(&dev->delayed_cmd_lock);
1644 spin_lock_init(&dev->ordered_cmd_lock);
1645 spin_lock_init(&dev->state_task_lock);
1646 spin_lock_init(&dev->dev_alua_lock);
1647 spin_lock_init(&dev->dev_reservation_lock);
1648 spin_lock_init(&dev->dev_status_lock);
1649 spin_lock_init(&dev->dev_status_thr_lock);
1650 spin_lock_init(&dev->se_port_lock);
1651 spin_lock_init(&dev->se_tmr_lock);
1653 dev->queue_depth = dev_limits->queue_depth;
1654 atomic_set(&dev->depth_left, dev->queue_depth);
1655 atomic_set(&dev->dev_ordered_id, 0);
1657 se_dev_set_default_attribs(dev, dev_limits);
1659 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1660 dev->creation_time = get_jiffies_64();
1661 spin_lock_init(&dev->stats_lock);
1663 spin_lock(&hba->device_lock);
1664 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1665 hba->dev_count++;
1666 spin_unlock(&hba->device_lock);
1668 * Setup the SAM Task Attribute emulation for struct se_device
1670 core_setup_task_attr_emulation(dev);
1672 * Force PR and ALUA passthrough emulation with internal object use.
1674 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1676 * Setup the Reservations infrastructure for struct se_device
1678 core_setup_reservations(dev, force_pt);
1680 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1682 if (core_setup_alua(dev, force_pt) < 0)
1683 goto out;
1686 * Startup the struct se_device processing thread
1688 dev->process_thread = kthread_run(transport_processing_thread, dev,
1689 "LIO_%s", TRANSPORT(dev)->name);
1690 if (IS_ERR(dev->process_thread)) {
1691 printk(KERN_ERR "Unable to create kthread: LIO_%s\n",
1692 TRANSPORT(dev)->name);
1693 goto out;
1697 * Preload the initial INQUIRY const values if we are doing
1698 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1699 * passthrough because this is being provided by the backend LLD.
1700 * This is required so that transport_get_inquiry() copies these
1701 * originals once back into DEV_T10_WWN(dev) for the virtual device
1702 * setup.
1704 if (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1705 if (!(inquiry_prod) || !(inquiry_prod)) {
1706 printk(KERN_ERR "All non TCM/pSCSI plugins require"
1707 " INQUIRY consts\n");
1708 goto out;
1711 strncpy(&DEV_T10_WWN(dev)->vendor[0], "LIO-ORG", 8);
1712 strncpy(&DEV_T10_WWN(dev)->model[0], inquiry_prod, 16);
1713 strncpy(&DEV_T10_WWN(dev)->revision[0], inquiry_rev, 4);
1715 scsi_dump_inquiry(dev);
1717 out:
1718 if (!ret)
1719 return dev;
1720 kthread_stop(dev->process_thread);
1722 spin_lock(&hba->device_lock);
1723 list_del(&dev->dev_list);
1724 hba->dev_count--;
1725 spin_unlock(&hba->device_lock);
1727 se_release_vpd_for_dev(dev);
1729 kfree(dev->dev_status_queue_obj);
1730 kfree(dev->dev_queue_obj);
1731 kfree(dev);
1733 return NULL;
1735 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1737 /* transport_generic_prepare_cdb():
1739 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1740 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1741 * The point of this is since we are mapping iSCSI LUNs to
1742 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1743 * devices and HBAs for a loop.
1745 static inline void transport_generic_prepare_cdb(
1746 unsigned char *cdb)
1748 switch (cdb[0]) {
1749 case READ_10: /* SBC - RDProtect */
1750 case READ_12: /* SBC - RDProtect */
1751 case READ_16: /* SBC - RDProtect */
1752 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1753 case VERIFY: /* SBC - VRProtect */
1754 case VERIFY_16: /* SBC - VRProtect */
1755 case WRITE_VERIFY: /* SBC - VRProtect */
1756 case WRITE_VERIFY_12: /* SBC - VRProtect */
1757 break;
1758 default:
1759 cdb[1] &= 0x1f; /* clear logical unit number */
1760 break;
1764 static struct se_task *
1765 transport_generic_get_task(struct se_cmd *cmd,
1766 enum dma_data_direction data_direction)
1768 struct se_task *task;
1769 struct se_device *dev = SE_DEV(cmd);
1770 unsigned long flags;
1772 task = dev->transport->alloc_task(cmd);
1773 if (!task) {
1774 printk(KERN_ERR "Unable to allocate struct se_task\n");
1775 return NULL;
1778 INIT_LIST_HEAD(&task->t_list);
1779 INIT_LIST_HEAD(&task->t_execute_list);
1780 INIT_LIST_HEAD(&task->t_state_list);
1781 init_completion(&task->task_stop_comp);
1782 task->task_no = T_TASK(cmd)->t_tasks_no++;
1783 task->task_se_cmd = cmd;
1784 task->se_dev = dev;
1785 task->task_data_direction = data_direction;
1787 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
1788 list_add_tail(&task->t_list, &T_TASK(cmd)->t_task_list);
1789 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
1791 return task;
1794 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1796 void transport_device_setup_cmd(struct se_cmd *cmd)
1798 cmd->se_dev = SE_LUN(cmd)->lun_se_dev;
1800 EXPORT_SYMBOL(transport_device_setup_cmd);
1803 * Used by fabric modules containing a local struct se_cmd within their
1804 * fabric dependent per I/O descriptor.
1806 void transport_init_se_cmd(
1807 struct se_cmd *cmd,
1808 struct target_core_fabric_ops *tfo,
1809 struct se_session *se_sess,
1810 u32 data_length,
1811 int data_direction,
1812 int task_attr,
1813 unsigned char *sense_buffer)
1815 INIT_LIST_HEAD(&cmd->se_lun_list);
1816 INIT_LIST_HEAD(&cmd->se_delayed_list);
1817 INIT_LIST_HEAD(&cmd->se_ordered_list);
1819 * Setup t_task pointer to t_task_backstore
1821 cmd->t_task = &cmd->t_task_backstore;
1823 INIT_LIST_HEAD(&T_TASK(cmd)->t_task_list);
1824 init_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
1825 init_completion(&T_TASK(cmd)->transport_lun_stop_comp);
1826 init_completion(&T_TASK(cmd)->t_transport_stop_comp);
1827 spin_lock_init(&T_TASK(cmd)->t_state_lock);
1828 atomic_set(&T_TASK(cmd)->transport_dev_active, 1);
1830 cmd->se_tfo = tfo;
1831 cmd->se_sess = se_sess;
1832 cmd->data_length = data_length;
1833 cmd->data_direction = data_direction;
1834 cmd->sam_task_attr = task_attr;
1835 cmd->sense_buffer = sense_buffer;
1837 EXPORT_SYMBOL(transport_init_se_cmd);
1839 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1842 * Check if SAM Task Attribute emulation is enabled for this
1843 * struct se_device storage object
1845 if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1846 return 0;
1848 if (cmd->sam_task_attr == TASK_ATTR_ACA) {
1849 DEBUG_STA("SAM Task Attribute ACA"
1850 " emulation is not supported\n");
1851 return -1;
1854 * Used to determine when ORDERED commands should go from
1855 * Dormant to Active status.
1857 cmd->se_ordered_id = atomic_inc_return(&SE_DEV(cmd)->dev_ordered_id);
1858 smp_mb__after_atomic_inc();
1859 DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1860 cmd->se_ordered_id, cmd->sam_task_attr,
1861 TRANSPORT(cmd->se_dev)->name);
1862 return 0;
1865 void transport_free_se_cmd(
1866 struct se_cmd *se_cmd)
1868 if (se_cmd->se_tmr_req)
1869 core_tmr_release_req(se_cmd->se_tmr_req);
1871 * Check and free any extended CDB buffer that was allocated
1873 if (T_TASK(se_cmd)->t_task_cdb != T_TASK(se_cmd)->__t_task_cdb)
1874 kfree(T_TASK(se_cmd)->t_task_cdb);
1876 EXPORT_SYMBOL(transport_free_se_cmd);
1878 static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
1880 /* transport_generic_allocate_tasks():
1882 * Called from fabric RX Thread.
1884 int transport_generic_allocate_tasks(
1885 struct se_cmd *cmd,
1886 unsigned char *cdb)
1888 int ret;
1890 transport_generic_prepare_cdb(cdb);
1893 * This is needed for early exceptions.
1895 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1897 transport_device_setup_cmd(cmd);
1899 * Ensure that the received CDB is less than the max (252 + 8) bytes
1900 * for VARIABLE_LENGTH_CMD
1902 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1903 printk(KERN_ERR "Received SCSI CDB with command_size: %d that"
1904 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1905 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1906 return -1;
1909 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1910 * allocate the additional extended CDB buffer now.. Otherwise
1911 * setup the pointer from __t_task_cdb to t_task_cdb.
1913 if (scsi_command_size(cdb) > sizeof(T_TASK(cmd)->__t_task_cdb)) {
1914 T_TASK(cmd)->t_task_cdb = kzalloc(scsi_command_size(cdb),
1915 GFP_KERNEL);
1916 if (!(T_TASK(cmd)->t_task_cdb)) {
1917 printk(KERN_ERR "Unable to allocate T_TASK(cmd)->t_task_cdb"
1918 " %u > sizeof(T_TASK(cmd)->__t_task_cdb): %lu ops\n",
1919 scsi_command_size(cdb),
1920 (unsigned long)sizeof(T_TASK(cmd)->__t_task_cdb));
1921 return -1;
1923 } else
1924 T_TASK(cmd)->t_task_cdb = &T_TASK(cmd)->__t_task_cdb[0];
1926 * Copy the original CDB into T_TASK(cmd).
1928 memcpy(T_TASK(cmd)->t_task_cdb, cdb, scsi_command_size(cdb));
1930 * Setup the received CDB based on SCSI defined opcodes and
1931 * perform unit attention, persistent reservations and ALUA
1932 * checks for virtual device backends. The T_TASK(cmd)->t_task_cdb
1933 * pointer is expected to be setup before we reach this point.
1935 ret = transport_generic_cmd_sequencer(cmd, cdb);
1936 if (ret < 0)
1937 return ret;
1939 * Check for SAM Task Attribute Emulation
1941 if (transport_check_alloc_task_attr(cmd) < 0) {
1942 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1943 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1944 return -2;
1946 spin_lock(&cmd->se_lun->lun_sep_lock);
1947 if (cmd->se_lun->lun_sep)
1948 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1949 spin_unlock(&cmd->se_lun->lun_sep_lock);
1950 return 0;
1952 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1955 * Used by fabric module frontends not defining a TFO->new_cmd_map()
1956 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
1958 int transport_generic_handle_cdb(
1959 struct se_cmd *cmd)
1961 if (!SE_LUN(cmd)) {
1962 dump_stack();
1963 printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
1964 return -1;
1967 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
1968 return 0;
1970 EXPORT_SYMBOL(transport_generic_handle_cdb);
1973 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1974 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1975 * complete setup in TCM process context w/ TFO->new_cmd_map().
1977 int transport_generic_handle_cdb_map(
1978 struct se_cmd *cmd)
1980 if (!SE_LUN(cmd)) {
1981 dump_stack();
1982 printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
1983 return -1;
1986 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
1987 return 0;
1989 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1991 /* transport_generic_handle_data():
1995 int transport_generic_handle_data(
1996 struct se_cmd *cmd)
1999 * For the software fabric case, then we assume the nexus is being
2000 * failed/shutdown when signals are pending from the kthread context
2001 * caller, so we return a failure. For the HW target mode case running
2002 * in interrupt code, the signal_pending() check is skipped.
2004 if (!in_interrupt() && signal_pending(current))
2005 return -1;
2007 * If the received CDB has aleady been ABORTED by the generic
2008 * target engine, we now call transport_check_aborted_status()
2009 * to queue any delated TASK_ABORTED status for the received CDB to the
2010 * fabric module as we are expecting no futher incoming DATA OUT
2011 * sequences at this point.
2013 if (transport_check_aborted_status(cmd, 1) != 0)
2014 return 0;
2016 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
2017 return 0;
2019 EXPORT_SYMBOL(transport_generic_handle_data);
2021 /* transport_generic_handle_tmr():
2025 int transport_generic_handle_tmr(
2026 struct se_cmd *cmd)
2029 * This is needed for early exceptions.
2031 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
2032 transport_device_setup_cmd(cmd);
2034 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
2035 return 0;
2037 EXPORT_SYMBOL(transport_generic_handle_tmr);
2039 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
2041 struct se_task *task, *task_tmp;
2042 unsigned long flags;
2043 int ret = 0;
2045 DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
2046 CMD_TFO(cmd)->get_task_tag(cmd));
2049 * No tasks remain in the execution queue
2051 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2052 list_for_each_entry_safe(task, task_tmp,
2053 &T_TASK(cmd)->t_task_list, t_list) {
2054 DEBUG_TS("task_no[%d] - Processing task %p\n",
2055 task->task_no, task);
2057 * If the struct se_task has not been sent and is not active,
2058 * remove the struct se_task from the execution queue.
2060 if (!atomic_read(&task->task_sent) &&
2061 !atomic_read(&task->task_active)) {
2062 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
2063 flags);
2064 transport_remove_task_from_execute_queue(task,
2065 task->se_dev);
2067 DEBUG_TS("task_no[%d] - Removed from execute queue\n",
2068 task->task_no);
2069 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2070 continue;
2074 * If the struct se_task is active, sleep until it is returned
2075 * from the plugin.
2077 if (atomic_read(&task->task_active)) {
2078 atomic_set(&task->task_stop, 1);
2079 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
2080 flags);
2082 DEBUG_TS("task_no[%d] - Waiting to complete\n",
2083 task->task_no);
2084 wait_for_completion(&task->task_stop_comp);
2085 DEBUG_TS("task_no[%d] - Stopped successfully\n",
2086 task->task_no);
2088 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2089 atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
2091 atomic_set(&task->task_active, 0);
2092 atomic_set(&task->task_stop, 0);
2093 } else {
2094 DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
2095 ret++;
2098 __transport_stop_task_timer(task, &flags);
2100 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2102 return ret;
2105 static void transport_failure_reset_queue_depth(struct se_device *dev)
2107 unsigned long flags;
2109 spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);;
2110 atomic_inc(&dev->depth_left);
2111 atomic_inc(&SE_HBA(dev)->left_queue_depth);
2112 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2116 * Handle SAM-esque emulation for generic transport request failures.
2118 static void transport_generic_request_failure(
2119 struct se_cmd *cmd,
2120 struct se_device *dev,
2121 int complete,
2122 int sc)
2124 DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
2125 " CDB: 0x%02x\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
2126 T_TASK(cmd)->t_task_cdb[0]);
2127 DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
2128 " %d/%d transport_error_status: %d\n",
2129 CMD_TFO(cmd)->get_cmd_state(cmd),
2130 cmd->t_state, cmd->deferred_t_state,
2131 cmd->transport_error_status);
2132 DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
2133 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
2134 " t_transport_active: %d t_transport_stop: %d"
2135 " t_transport_sent: %d\n", T_TASK(cmd)->t_task_cdbs,
2136 atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
2137 atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
2138 atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left),
2139 atomic_read(&T_TASK(cmd)->t_transport_active),
2140 atomic_read(&T_TASK(cmd)->t_transport_stop),
2141 atomic_read(&T_TASK(cmd)->t_transport_sent));
2143 transport_stop_all_task_timers(cmd);
2145 if (dev)
2146 transport_failure_reset_queue_depth(dev);
2148 * For SAM Task Attribute emulation for failed struct se_cmd
2150 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2151 transport_complete_task_attr(cmd);
2153 if (complete) {
2154 transport_direct_request_timeout(cmd);
2155 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2158 switch (cmd->transport_error_status) {
2159 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
2160 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2161 break;
2162 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
2163 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
2164 break;
2165 case PYX_TRANSPORT_INVALID_CDB_FIELD:
2166 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2167 break;
2168 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
2169 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2170 break;
2171 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
2172 if (!sc)
2173 transport_new_cmd_failure(cmd);
2175 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
2176 * we force this session to fall back to session
2177 * recovery.
2179 CMD_TFO(cmd)->fall_back_to_erl0(cmd->se_sess);
2180 CMD_TFO(cmd)->stop_session(cmd->se_sess, 0, 0);
2182 goto check_stop;
2183 case PYX_TRANSPORT_LU_COMM_FAILURE:
2184 case PYX_TRANSPORT_ILLEGAL_REQUEST:
2185 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2186 break;
2187 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
2188 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
2189 break;
2190 case PYX_TRANSPORT_WRITE_PROTECTED:
2191 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
2192 break;
2193 case PYX_TRANSPORT_RESERVATION_CONFLICT:
2195 * No SENSE Data payload for this case, set SCSI Status
2196 * and queue the response to $FABRIC_MOD.
2198 * Uses linux/include/scsi/scsi.h SAM status codes defs
2200 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2202 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2203 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2204 * CONFLICT STATUS.
2206 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2208 if (SE_SESS(cmd) &&
2209 DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
2210 core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
2211 cmd->orig_fe_lun, 0x2C,
2212 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2214 CMD_TFO(cmd)->queue_status(cmd);
2215 goto check_stop;
2216 case PYX_TRANSPORT_USE_SENSE_REASON:
2218 * struct se_cmd->scsi_sense_reason already set
2220 break;
2221 default:
2222 printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
2223 T_TASK(cmd)->t_task_cdb[0],
2224 cmd->transport_error_status);
2225 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2226 break;
2229 if (!sc)
2230 transport_new_cmd_failure(cmd);
2231 else
2232 transport_send_check_condition_and_sense(cmd,
2233 cmd->scsi_sense_reason, 0);
2234 check_stop:
2235 transport_lun_remove_cmd(cmd);
2236 if (!(transport_cmd_check_stop_to_fabric(cmd)))
2240 static void transport_direct_request_timeout(struct se_cmd *cmd)
2242 unsigned long flags;
2244 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2245 if (!(atomic_read(&T_TASK(cmd)->t_transport_timeout))) {
2246 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2247 return;
2249 if (atomic_read(&T_TASK(cmd)->t_task_cdbs_timeout_left)) {
2250 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2251 return;
2254 atomic_sub(atomic_read(&T_TASK(cmd)->t_transport_timeout),
2255 &T_TASK(cmd)->t_se_count);
2256 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2259 static void transport_generic_request_timeout(struct se_cmd *cmd)
2261 unsigned long flags;
2264 * Reset T_TASK(cmd)->t_se_count to allow transport_generic_remove()
2265 * to allow last call to free memory resources.
2267 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2268 if (atomic_read(&T_TASK(cmd)->t_transport_timeout) > 1) {
2269 int tmp = (atomic_read(&T_TASK(cmd)->t_transport_timeout) - 1);
2271 atomic_sub(tmp, &T_TASK(cmd)->t_se_count);
2273 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2275 transport_generic_remove(cmd, 0, 0);
2278 static int
2279 transport_generic_allocate_buf(struct se_cmd *cmd, u32 data_length)
2281 unsigned char *buf;
2283 buf = kzalloc(data_length, GFP_KERNEL);
2284 if (!(buf)) {
2285 printk(KERN_ERR "Unable to allocate memory for buffer\n");
2286 return -1;
2289 T_TASK(cmd)->t_tasks_se_num = 0;
2290 T_TASK(cmd)->t_task_buf = buf;
2292 return 0;
2295 static inline u32 transport_lba_21(unsigned char *cdb)
2297 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2300 static inline u32 transport_lba_32(unsigned char *cdb)
2302 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2305 static inline unsigned long long transport_lba_64(unsigned char *cdb)
2307 unsigned int __v1, __v2;
2309 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2310 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2312 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2316 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2318 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2320 unsigned int __v1, __v2;
2322 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2323 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2325 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2328 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2330 unsigned long flags;
2332 spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
2333 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2334 spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
2338 * Called from interrupt context.
2340 static void transport_task_timeout_handler(unsigned long data)
2342 struct se_task *task = (struct se_task *)data;
2343 struct se_cmd *cmd = TASK_CMD(task);
2344 unsigned long flags;
2346 DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
2348 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2349 if (task->task_flags & TF_STOP) {
2350 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2351 return;
2353 task->task_flags &= ~TF_RUNNING;
2356 * Determine if transport_complete_task() has already been called.
2358 if (!(atomic_read(&task->task_active))) {
2359 DEBUG_TT("transport task: %p cmd: %p timeout task_active"
2360 " == 0\n", task, cmd);
2361 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2362 return;
2365 atomic_inc(&T_TASK(cmd)->t_se_count);
2366 atomic_inc(&T_TASK(cmd)->t_transport_timeout);
2367 T_TASK(cmd)->t_tasks_failed = 1;
2369 atomic_set(&task->task_timeout, 1);
2370 task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
2371 task->task_scsi_status = 1;
2373 if (atomic_read(&task->task_stop)) {
2374 DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
2375 " == 1\n", task, cmd);
2376 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2377 complete(&task->task_stop_comp);
2378 return;
2381 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_left))) {
2382 DEBUG_TT("transport task: %p cmd: %p timeout non zero"
2383 " t_task_cdbs_left\n", task, cmd);
2384 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2385 return;
2387 DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2388 task, cmd);
2390 cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
2391 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2393 transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
2397 * Called with T_TASK(cmd)->t_state_lock held.
2399 static void transport_start_task_timer(struct se_task *task)
2401 struct se_device *dev = task->se_dev;
2402 int timeout;
2404 if (task->task_flags & TF_RUNNING)
2405 return;
2407 * If the task_timeout is disabled, exit now.
2409 timeout = DEV_ATTRIB(dev)->task_timeout;
2410 if (!(timeout))
2411 return;
2413 init_timer(&task->task_timer);
2414 task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
2415 task->task_timer.data = (unsigned long) task;
2416 task->task_timer.function = transport_task_timeout_handler;
2418 task->task_flags |= TF_RUNNING;
2419 add_timer(&task->task_timer);
2420 #if 0
2421 printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
2422 " %d\n", task->task_se_cmd, task, timeout);
2423 #endif
2427 * Called with spin_lock_irq(&T_TASK(cmd)->t_state_lock) held.
2429 void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
2431 struct se_cmd *cmd = TASK_CMD(task);
2433 if (!(task->task_flags & TF_RUNNING))
2434 return;
2436 task->task_flags |= TF_STOP;
2437 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, *flags);
2439 del_timer_sync(&task->task_timer);
2441 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, *flags);
2442 task->task_flags &= ~TF_RUNNING;
2443 task->task_flags &= ~TF_STOP;
2446 static void transport_stop_all_task_timers(struct se_cmd *cmd)
2448 struct se_task *task = NULL, *task_tmp;
2449 unsigned long flags;
2451 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2452 list_for_each_entry_safe(task, task_tmp,
2453 &T_TASK(cmd)->t_task_list, t_list)
2454 __transport_stop_task_timer(task, &flags);
2455 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2458 static inline int transport_tcq_window_closed(struct se_device *dev)
2460 if (dev->dev_tcq_window_closed++ <
2461 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
2462 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
2463 } else
2464 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
2466 wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
2467 return 0;
2471 * Called from Fabric Module context from transport_execute_tasks()
2473 * The return of this function determins if the tasks from struct se_cmd
2474 * get added to the execution queue in transport_execute_tasks(),
2475 * or are added to the delayed or ordered lists here.
2477 static inline int transport_execute_task_attr(struct se_cmd *cmd)
2479 if (SE_DEV(cmd)->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2480 return 1;
2482 * Check for the existance of HEAD_OF_QUEUE, and if true return 1
2483 * to allow the passed struct se_cmd list of tasks to the front of the list.
2485 if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
2486 atomic_inc(&SE_DEV(cmd)->dev_hoq_count);
2487 smp_mb__after_atomic_inc();
2488 DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
2489 " 0x%02x, se_ordered_id: %u\n",
2490 T_TASK(cmd)->t_task_cdb[0],
2491 cmd->se_ordered_id);
2492 return 1;
2493 } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
2494 spin_lock(&SE_DEV(cmd)->ordered_cmd_lock);
2495 list_add_tail(&cmd->se_ordered_list,
2496 &SE_DEV(cmd)->ordered_cmd_list);
2497 spin_unlock(&SE_DEV(cmd)->ordered_cmd_lock);
2499 atomic_inc(&SE_DEV(cmd)->dev_ordered_sync);
2500 smp_mb__after_atomic_inc();
2502 DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
2503 " list, se_ordered_id: %u\n",
2504 T_TASK(cmd)->t_task_cdb[0],
2505 cmd->se_ordered_id);
2507 * Add ORDERED command to tail of execution queue if
2508 * no other older commands exist that need to be
2509 * completed first.
2511 if (!(atomic_read(&SE_DEV(cmd)->simple_cmds)))
2512 return 1;
2513 } else {
2515 * For SIMPLE and UNTAGGED Task Attribute commands
2517 atomic_inc(&SE_DEV(cmd)->simple_cmds);
2518 smp_mb__after_atomic_inc();
2521 * Otherwise if one or more outstanding ORDERED task attribute exist,
2522 * add the dormant task(s) built for the passed struct se_cmd to the
2523 * execution queue and become in Active state for this struct se_device.
2525 if (atomic_read(&SE_DEV(cmd)->dev_ordered_sync) != 0) {
2527 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2528 * will be drained upon competion of HEAD_OF_QUEUE task.
2530 spin_lock(&SE_DEV(cmd)->delayed_cmd_lock);
2531 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2532 list_add_tail(&cmd->se_delayed_list,
2533 &SE_DEV(cmd)->delayed_cmd_list);
2534 spin_unlock(&SE_DEV(cmd)->delayed_cmd_lock);
2536 DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
2537 " delayed CMD list, se_ordered_id: %u\n",
2538 T_TASK(cmd)->t_task_cdb[0], cmd->sam_task_attr,
2539 cmd->se_ordered_id);
2541 * Return zero to let transport_execute_tasks() know
2542 * not to add the delayed tasks to the execution list.
2544 return 0;
2547 * Otherwise, no ORDERED task attributes exist..
2549 return 1;
2553 * Called from fabric module context in transport_generic_new_cmd() and
2554 * transport_generic_process_write()
2556 static int transport_execute_tasks(struct se_cmd *cmd)
2558 int add_tasks;
2560 if (!(cmd->se_cmd_flags & SCF_SE_DISABLE_ONLINE_CHECK)) {
2561 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2562 cmd->transport_error_status =
2563 PYX_TRANSPORT_LU_COMM_FAILURE;
2564 transport_generic_request_failure(cmd, NULL, 0, 1);
2565 return 0;
2569 * Call transport_cmd_check_stop() to see if a fabric exception
2570 * has occured that prevents execution.
2572 if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
2574 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2575 * attribute for the tasks of the received struct se_cmd CDB
2577 add_tasks = transport_execute_task_attr(cmd);
2578 if (add_tasks == 0)
2579 goto execute_tasks;
2581 * This calls transport_add_tasks_from_cmd() to handle
2582 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2583 * (if enabled) in __transport_add_task_to_execute_queue() and
2584 * transport_add_task_check_sam_attr().
2586 transport_add_tasks_from_cmd(cmd);
2589 * Kick the execution queue for the cmd associated struct se_device
2590 * storage object.
2592 execute_tasks:
2593 __transport_execute_tasks(SE_DEV(cmd));
2594 return 0;
2598 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2599 * from struct se_device->execute_task_list and
2601 * Called from transport_processing_thread()
2603 static int __transport_execute_tasks(struct se_device *dev)
2605 int error;
2606 struct se_cmd *cmd = NULL;
2607 struct se_task *task;
2608 unsigned long flags;
2611 * Check if there is enough room in the device and HBA queue to send
2612 * struct se_transport_task's to the selected transport.
2614 check_depth:
2615 spin_lock_irqsave(&SE_HBA(dev)->hba_queue_lock, flags);
2616 if (!(atomic_read(&dev->depth_left)) ||
2617 !(atomic_read(&SE_HBA(dev)->left_queue_depth))) {
2618 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2619 return transport_tcq_window_closed(dev);
2621 dev->dev_tcq_window_closed = 0;
2623 spin_lock(&dev->execute_task_lock);
2624 task = transport_get_task_from_execute_queue(dev);
2625 spin_unlock(&dev->execute_task_lock);
2627 if (!task) {
2628 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2629 return 0;
2632 atomic_dec(&dev->depth_left);
2633 atomic_dec(&SE_HBA(dev)->left_queue_depth);
2634 spin_unlock_irqrestore(&SE_HBA(dev)->hba_queue_lock, flags);
2636 cmd = TASK_CMD(task);
2638 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2639 atomic_set(&task->task_active, 1);
2640 atomic_set(&task->task_sent, 1);
2641 atomic_inc(&T_TASK(cmd)->t_task_cdbs_sent);
2643 if (atomic_read(&T_TASK(cmd)->t_task_cdbs_sent) ==
2644 T_TASK(cmd)->t_task_cdbs)
2645 atomic_set(&cmd->transport_sent, 1);
2647 transport_start_task_timer(task);
2648 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2650 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2651 * to grab REPORT_LUNS CDBs before they hit the
2652 * struct se_subsystem_api->do_task() caller below.
2654 if (cmd->transport_emulate_cdb) {
2655 error = cmd->transport_emulate_cdb(cmd);
2656 if (error != 0) {
2657 cmd->transport_error_status = error;
2658 atomic_set(&task->task_active, 0);
2659 atomic_set(&cmd->transport_sent, 0);
2660 transport_stop_tasks_for_cmd(cmd);
2661 transport_generic_request_failure(cmd, dev, 0, 1);
2662 goto check_depth;
2665 * Handle the successful completion for transport_emulate_cdb()
2666 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2667 * Otherwise the caller is expected to complete the task with
2668 * proper status.
2670 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2671 cmd->scsi_status = SAM_STAT_GOOD;
2672 task->task_scsi_status = GOOD;
2673 transport_complete_task(task, 1);
2675 } else {
2677 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2678 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2679 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2680 * LUN emulation code.
2682 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2683 * call ->do_task() directly and let the underlying TCM subsystem plugin
2684 * code handle the CDB emulation.
2686 if ((TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2687 (!(TASK_CMD(task)->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2688 error = transport_emulate_control_cdb(task);
2689 else
2690 error = TRANSPORT(dev)->do_task(task);
2692 if (error != 0) {
2693 cmd->transport_error_status = error;
2694 atomic_set(&task->task_active, 0);
2695 atomic_set(&cmd->transport_sent, 0);
2696 transport_stop_tasks_for_cmd(cmd);
2697 transport_generic_request_failure(cmd, dev, 0, 1);
2701 goto check_depth;
2703 return 0;
2706 void transport_new_cmd_failure(struct se_cmd *se_cmd)
2708 unsigned long flags;
2710 * Any unsolicited data will get dumped for failed command inside of
2711 * the fabric plugin
2713 spin_lock_irqsave(&T_TASK(se_cmd)->t_state_lock, flags);
2714 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2715 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2716 spin_unlock_irqrestore(&T_TASK(se_cmd)->t_state_lock, flags);
2718 CMD_TFO(se_cmd)->new_cmd_failure(se_cmd);
2721 static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
2723 static inline u32 transport_get_sectors_6(
2724 unsigned char *cdb,
2725 struct se_cmd *cmd,
2726 int *ret)
2728 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2731 * Assume TYPE_DISK for non struct se_device objects.
2732 * Use 8-bit sector value.
2734 if (!dev)
2735 goto type_disk;
2738 * Use 24-bit allocation length for TYPE_TAPE.
2740 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
2741 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2744 * Everything else assume TYPE_DISK Sector CDB location.
2745 * Use 8-bit sector value.
2747 type_disk:
2748 return (u32)cdb[4];
2751 static inline u32 transport_get_sectors_10(
2752 unsigned char *cdb,
2753 struct se_cmd *cmd,
2754 int *ret)
2756 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2759 * Assume TYPE_DISK for non struct se_device objects.
2760 * Use 16-bit sector value.
2762 if (!dev)
2763 goto type_disk;
2766 * XXX_10 is not defined in SSC, throw an exception
2768 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
2769 *ret = -1;
2770 return 0;
2774 * Everything else assume TYPE_DISK Sector CDB location.
2775 * Use 16-bit sector value.
2777 type_disk:
2778 return (u32)(cdb[7] << 8) + cdb[8];
2781 static inline u32 transport_get_sectors_12(
2782 unsigned char *cdb,
2783 struct se_cmd *cmd,
2784 int *ret)
2786 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2789 * Assume TYPE_DISK for non struct se_device objects.
2790 * Use 32-bit sector value.
2792 if (!dev)
2793 goto type_disk;
2796 * XXX_12 is not defined in SSC, throw an exception
2798 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
2799 *ret = -1;
2800 return 0;
2804 * Everything else assume TYPE_DISK Sector CDB location.
2805 * Use 32-bit sector value.
2807 type_disk:
2808 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2811 static inline u32 transport_get_sectors_16(
2812 unsigned char *cdb,
2813 struct se_cmd *cmd,
2814 int *ret)
2816 struct se_device *dev = SE_LUN(cmd)->lun_se_dev;
2819 * Assume TYPE_DISK for non struct se_device objects.
2820 * Use 32-bit sector value.
2822 if (!dev)
2823 goto type_disk;
2826 * Use 24-bit allocation length for TYPE_TAPE.
2828 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE)
2829 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2831 type_disk:
2832 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2833 (cdb[12] << 8) + cdb[13];
2837 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2839 static inline u32 transport_get_sectors_32(
2840 unsigned char *cdb,
2841 struct se_cmd *cmd,
2842 int *ret)
2845 * Assume TYPE_DISK for non struct se_device objects.
2846 * Use 32-bit sector value.
2848 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2849 (cdb[30] << 8) + cdb[31];
2853 static inline u32 transport_get_size(
2854 u32 sectors,
2855 unsigned char *cdb,
2856 struct se_cmd *cmd)
2858 struct se_device *dev = SE_DEV(cmd);
2860 if (TRANSPORT(dev)->get_device_type(dev) == TYPE_TAPE) {
2861 if (cdb[1] & 1) { /* sectors */
2862 return DEV_ATTRIB(dev)->block_size * sectors;
2863 } else /* bytes */
2864 return sectors;
2866 #if 0
2867 printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
2868 " %s object\n", DEV_ATTRIB(dev)->block_size, sectors,
2869 DEV_ATTRIB(dev)->block_size * sectors,
2870 TRANSPORT(dev)->name);
2871 #endif
2872 return DEV_ATTRIB(dev)->block_size * sectors;
2875 unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
2877 unsigned char result = 0;
2879 * MSB
2881 if ((val[0] >= 'a') && (val[0] <= 'f'))
2882 result = ((val[0] - 'a' + 10) & 0xf) << 4;
2883 else
2884 if ((val[0] >= 'A') && (val[0] <= 'F'))
2885 result = ((val[0] - 'A' + 10) & 0xf) << 4;
2886 else /* digit */
2887 result = ((val[0] - '0') & 0xf) << 4;
2889 * LSB
2891 if ((val[1] >= 'a') && (val[1] <= 'f'))
2892 result |= ((val[1] - 'a' + 10) & 0xf);
2893 else
2894 if ((val[1] >= 'A') && (val[1] <= 'F'))
2895 result |= ((val[1] - 'A' + 10) & 0xf);
2896 else /* digit */
2897 result |= ((val[1] - '0') & 0xf);
2899 return result;
2901 EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
2903 static void transport_xor_callback(struct se_cmd *cmd)
2905 unsigned char *buf, *addr;
2906 struct se_mem *se_mem;
2907 unsigned int offset;
2908 int i;
2910 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2912 * 1) read the specified logical block(s);
2913 * 2) transfer logical blocks from the data-out buffer;
2914 * 3) XOR the logical blocks transferred from the data-out buffer with
2915 * the logical blocks read, storing the resulting XOR data in a buffer;
2916 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2917 * blocks transferred from the data-out buffer; and
2918 * 5) transfer the resulting XOR data to the data-in buffer.
2920 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2921 if (!(buf)) {
2922 printk(KERN_ERR "Unable to allocate xor_callback buf\n");
2923 return;
2926 * Copy the scatterlist WRITE buffer located at T_TASK(cmd)->t_mem_list
2927 * into the locally allocated *buf
2929 transport_memcpy_se_mem_read_contig(cmd, buf, T_TASK(cmd)->t_mem_list);
2931 * Now perform the XOR against the BIDI read memory located at
2932 * T_TASK(cmd)->t_mem_bidi_list
2935 offset = 0;
2936 list_for_each_entry(se_mem, T_TASK(cmd)->t_mem_bidi_list, se_list) {
2937 addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
2938 if (!(addr))
2939 goto out;
2941 for (i = 0; i < se_mem->se_len; i++)
2942 *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
2944 offset += se_mem->se_len;
2945 kunmap_atomic(addr, KM_USER0);
2947 out:
2948 kfree(buf);
2952 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2954 static int transport_get_sense_data(struct se_cmd *cmd)
2956 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2957 struct se_device *dev;
2958 struct se_task *task = NULL, *task_tmp;
2959 unsigned long flags;
2960 u32 offset = 0;
2962 if (!SE_LUN(cmd)) {
2963 printk(KERN_ERR "SE_LUN(cmd) is NULL\n");
2964 return -1;
2966 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
2967 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2968 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2969 return 0;
2972 list_for_each_entry_safe(task, task_tmp,
2973 &T_TASK(cmd)->t_task_list, t_list) {
2975 if (!task->task_sense)
2976 continue;
2978 dev = task->se_dev;
2979 if (!(dev))
2980 continue;
2982 if (!TRANSPORT(dev)->get_sense_buffer) {
2983 printk(KERN_ERR "TRANSPORT(dev)->get_sense_buffer"
2984 " is NULL\n");
2985 continue;
2988 sense_buffer = TRANSPORT(dev)->get_sense_buffer(task);
2989 if (!(sense_buffer)) {
2990 printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
2991 " sense buffer for task with sense\n",
2992 CMD_TFO(cmd)->get_task_tag(cmd), task->task_no);
2993 continue;
2995 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
2997 offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
2998 TRANSPORT_SENSE_BUFFER);
3000 memcpy((void *)&buffer[offset], (void *)sense_buffer,
3001 TRANSPORT_SENSE_BUFFER);
3002 cmd->scsi_status = task->task_scsi_status;
3003 /* Automatically padded */
3004 cmd->scsi_sense_length =
3005 (TRANSPORT_SENSE_BUFFER + offset);
3007 printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
3008 " and sense\n",
3009 dev->se_hba->hba_id, TRANSPORT(dev)->name,
3010 cmd->scsi_status);
3011 return 0;
3013 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
3015 return -1;
3018 static int transport_allocate_resources(struct se_cmd *cmd)
3020 u32 length = cmd->data_length;
3022 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3023 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB))
3024 return transport_generic_get_mem(cmd, length, PAGE_SIZE);
3025 else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB)
3026 return transport_generic_allocate_buf(cmd, length);
3027 else
3028 return 0;
3031 static int
3032 transport_handle_reservation_conflict(struct se_cmd *cmd)
3034 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3035 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3036 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
3037 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
3039 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
3040 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
3041 * CONFLICT STATUS.
3043 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
3045 if (SE_SESS(cmd) &&
3046 DEV_ATTRIB(cmd->se_dev)->emulate_ua_intlck_ctrl == 2)
3047 core_scsi3_ua_allocate(SE_SESS(cmd)->se_node_acl,
3048 cmd->orig_fe_lun, 0x2C,
3049 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
3050 return -2;
3053 /* transport_generic_cmd_sequencer():
3055 * Generic Command Sequencer that should work for most DAS transport
3056 * drivers.
3058 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
3059 * RX Thread.
3061 * FIXME: Need to support other SCSI OPCODES where as well.
3063 static int transport_generic_cmd_sequencer(
3064 struct se_cmd *cmd,
3065 unsigned char *cdb)
3067 struct se_device *dev = SE_DEV(cmd);
3068 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
3069 int ret = 0, sector_ret = 0, passthrough;
3070 u32 sectors = 0, size = 0, pr_reg_type = 0;
3071 u16 service_action;
3072 u8 alua_ascq = 0;
3074 * Check for an existing UNIT ATTENTION condition
3076 if (core_scsi3_ua_check(cmd, cdb) < 0) {
3077 cmd->transport_wait_for_tasks =
3078 &transport_nop_wait_for_tasks;
3079 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3080 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
3081 return -2;
3084 * Check status of Asymmetric Logical Unit Assignment port
3086 ret = T10_ALUA(su_dev)->alua_state_check(cmd, cdb, &alua_ascq);
3087 if (ret != 0) {
3088 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3090 * Set SCSI additional sense code (ASC) to 'LUN Not Accessable';
3091 * The ALUA additional sense code qualifier (ASCQ) is determined
3092 * by the ALUA primary or secondary access state..
3094 if (ret > 0) {
3095 #if 0
3096 printk(KERN_INFO "[%s]: ALUA TG Port not available,"
3097 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
3098 CMD_TFO(cmd)->get_fabric_name(), alua_ascq);
3099 #endif
3100 transport_set_sense_codes(cmd, 0x04, alua_ascq);
3101 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3102 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
3103 return -2;
3105 goto out_invalid_cdb_field;
3108 * Check status for SPC-3 Persistent Reservations
3110 if (T10_PR_OPS(su_dev)->t10_reservation_check(cmd, &pr_reg_type) != 0) {
3111 if (T10_PR_OPS(su_dev)->t10_seq_non_holder(
3112 cmd, cdb, pr_reg_type) != 0)
3113 return transport_handle_reservation_conflict(cmd);
3115 * This means the CDB is allowed for the SCSI Initiator port
3116 * when said port is *NOT* holding the legacy SPC-2 or
3117 * SPC-3 Persistent Reservation.
3121 switch (cdb[0]) {
3122 case READ_6:
3123 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
3124 if (sector_ret)
3125 goto out_unsupported_cdb;
3126 size = transport_get_size(sectors, cdb, cmd);
3127 cmd->transport_split_cdb = &split_cdb_XX_6;
3128 T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
3129 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3130 break;
3131 case READ_10:
3132 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3133 if (sector_ret)
3134 goto out_unsupported_cdb;
3135 size = transport_get_size(sectors, cdb, cmd);
3136 cmd->transport_split_cdb = &split_cdb_XX_10;
3137 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3138 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3139 break;
3140 case READ_12:
3141 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
3142 if (sector_ret)
3143 goto out_unsupported_cdb;
3144 size = transport_get_size(sectors, cdb, cmd);
3145 cmd->transport_split_cdb = &split_cdb_XX_12;
3146 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3147 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3148 break;
3149 case READ_16:
3150 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3151 if (sector_ret)
3152 goto out_unsupported_cdb;
3153 size = transport_get_size(sectors, cdb, cmd);
3154 cmd->transport_split_cdb = &split_cdb_XX_16;
3155 T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
3156 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3157 break;
3158 case WRITE_6:
3159 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
3160 if (sector_ret)
3161 goto out_unsupported_cdb;
3162 size = transport_get_size(sectors, cdb, cmd);
3163 cmd->transport_split_cdb = &split_cdb_XX_6;
3164 T_TASK(cmd)->t_task_lba = transport_lba_21(cdb);
3165 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3166 break;
3167 case WRITE_10:
3168 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3169 if (sector_ret)
3170 goto out_unsupported_cdb;
3171 size = transport_get_size(sectors, cdb, cmd);
3172 cmd->transport_split_cdb = &split_cdb_XX_10;
3173 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3174 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3175 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3176 break;
3177 case WRITE_12:
3178 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
3179 if (sector_ret)
3180 goto out_unsupported_cdb;
3181 size = transport_get_size(sectors, cdb, cmd);
3182 cmd->transport_split_cdb = &split_cdb_XX_12;
3183 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3184 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3185 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3186 break;
3187 case WRITE_16:
3188 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3189 if (sector_ret)
3190 goto out_unsupported_cdb;
3191 size = transport_get_size(sectors, cdb, cmd);
3192 cmd->transport_split_cdb = &split_cdb_XX_16;
3193 T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
3194 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3195 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3196 break;
3197 case XDWRITEREAD_10:
3198 if ((cmd->data_direction != DMA_TO_DEVICE) ||
3199 !(T_TASK(cmd)->t_tasks_bidi))
3200 goto out_invalid_cdb_field;
3201 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3202 if (sector_ret)
3203 goto out_unsupported_cdb;
3204 size = transport_get_size(sectors, cdb, cmd);
3205 cmd->transport_split_cdb = &split_cdb_XX_10;
3206 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3207 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3208 passthrough = (TRANSPORT(dev)->transport_type ==
3209 TRANSPORT_PLUGIN_PHBA_PDEV);
3211 * Skip the remaining assignments for TCM/PSCSI passthrough
3213 if (passthrough)
3214 break;
3216 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3218 cmd->transport_complete_callback = &transport_xor_callback;
3219 T_TASK(cmd)->t_tasks_fua = (cdb[1] & 0x8);
3220 break;
3221 case VARIABLE_LENGTH_CMD:
3222 service_action = get_unaligned_be16(&cdb[8]);
3224 * Determine if this is TCM/PSCSI device and we should disable
3225 * internal emulation for this CDB.
3227 passthrough = (TRANSPORT(dev)->transport_type ==
3228 TRANSPORT_PLUGIN_PHBA_PDEV);
3230 switch (service_action) {
3231 case XDWRITEREAD_32:
3232 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3233 if (sector_ret)
3234 goto out_unsupported_cdb;
3235 size = transport_get_size(sectors, cdb, cmd);
3237 * Use WRITE_32 and READ_32 opcodes for the emulated
3238 * XDWRITE_READ_32 logic.
3240 cmd->transport_split_cdb = &split_cdb_XX_32;
3241 T_TASK(cmd)->t_task_lba = transport_lba_64_ext(cdb);
3242 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3245 * Skip the remaining assignments for TCM/PSCSI passthrough
3247 if (passthrough)
3248 break;
3251 * Setup BIDI XOR callback to be run during
3252 * transport_generic_complete_ok()
3254 cmd->transport_complete_callback = &transport_xor_callback;
3255 T_TASK(cmd)->t_tasks_fua = (cdb[10] & 0x8);
3256 break;
3257 case WRITE_SAME_32:
3258 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3259 if (sector_ret)
3260 goto out_unsupported_cdb;
3261 size = transport_get_size(sectors, cdb, cmd);
3262 T_TASK(cmd)->t_task_lba = get_unaligned_be64(&cdb[12]);
3263 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3266 * Skip the remaining assignments for TCM/PSCSI passthrough
3268 if (passthrough)
3269 break;
3271 if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
3272 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3273 " bits not supported for Block Discard"
3274 " Emulation\n");
3275 goto out_invalid_cdb_field;
3278 * Currently for the emulated case we only accept
3279 * tpws with the UNMAP=1 bit set.
3281 if (!(cdb[10] & 0x08)) {
3282 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
3283 " supported for Block Discard Emulation\n");
3284 goto out_invalid_cdb_field;
3286 break;
3287 default:
3288 printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
3289 " 0x%04x not supported\n", service_action);
3290 goto out_unsupported_cdb;
3292 break;
3293 case 0xa3:
3294 if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
3295 /* MAINTENANCE_IN from SCC-2 */
3297 * Check for emulated MI_REPORT_TARGET_PGS.
3299 if (cdb[1] == MI_REPORT_TARGET_PGS) {
3300 cmd->transport_emulate_cdb =
3301 (T10_ALUA(su_dev)->alua_type ==
3302 SPC3_ALUA_EMULATED) ?
3303 &core_emulate_report_target_port_groups :
3304 NULL;
3306 size = (cdb[6] << 24) | (cdb[7] << 16) |
3307 (cdb[8] << 8) | cdb[9];
3308 } else {
3309 /* GPCMD_SEND_KEY from multi media commands */
3310 size = (cdb[8] << 8) + cdb[9];
3312 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3313 break;
3314 case MODE_SELECT:
3315 size = cdb[4];
3316 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3317 break;
3318 case MODE_SELECT_10:
3319 size = (cdb[7] << 8) + cdb[8];
3320 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3321 break;
3322 case MODE_SENSE:
3323 size = cdb[4];
3324 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3325 break;
3326 case MODE_SENSE_10:
3327 case GPCMD_READ_BUFFER_CAPACITY:
3328 case GPCMD_SEND_OPC:
3329 case LOG_SELECT:
3330 case LOG_SENSE:
3331 size = (cdb[7] << 8) + cdb[8];
3332 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3333 break;
3334 case READ_BLOCK_LIMITS:
3335 size = READ_BLOCK_LEN;
3336 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3337 break;
3338 case GPCMD_GET_CONFIGURATION:
3339 case GPCMD_READ_FORMAT_CAPACITIES:
3340 case GPCMD_READ_DISC_INFO:
3341 case GPCMD_READ_TRACK_RZONE_INFO:
3342 size = (cdb[7] << 8) + cdb[8];
3343 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3344 break;
3345 case PERSISTENT_RESERVE_IN:
3346 case PERSISTENT_RESERVE_OUT:
3347 cmd->transport_emulate_cdb =
3348 (T10_RES(su_dev)->res_type ==
3349 SPC3_PERSISTENT_RESERVATIONS) ?
3350 &core_scsi3_emulate_pr : NULL;
3351 size = (cdb[7] << 8) + cdb[8];
3352 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3353 break;
3354 case GPCMD_MECHANISM_STATUS:
3355 case GPCMD_READ_DVD_STRUCTURE:
3356 size = (cdb[8] << 8) + cdb[9];
3357 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3358 break;
3359 case READ_POSITION:
3360 size = READ_POSITION_LEN;
3361 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3362 break;
3363 case 0xa4:
3364 if (TRANSPORT(dev)->get_device_type(dev) != TYPE_ROM) {
3365 /* MAINTENANCE_OUT from SCC-2
3367 * Check for emulated MO_SET_TARGET_PGS.
3369 if (cdb[1] == MO_SET_TARGET_PGS) {
3370 cmd->transport_emulate_cdb =
3371 (T10_ALUA(su_dev)->alua_type ==
3372 SPC3_ALUA_EMULATED) ?
3373 &core_emulate_set_target_port_groups :
3374 NULL;
3377 size = (cdb[6] << 24) | (cdb[7] << 16) |
3378 (cdb[8] << 8) | cdb[9];
3379 } else {
3380 /* GPCMD_REPORT_KEY from multi media commands */
3381 size = (cdb[8] << 8) + cdb[9];
3383 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3384 break;
3385 case INQUIRY:
3386 size = (cdb[3] << 8) + cdb[4];
3388 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3389 * See spc4r17 section 5.3
3391 if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3392 cmd->sam_task_attr = TASK_ATTR_HOQ;
3393 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3394 break;
3395 case READ_BUFFER:
3396 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3397 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3398 break;
3399 case READ_CAPACITY:
3400 size = READ_CAP_LEN;
3401 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3402 break;
3403 case READ_MEDIA_SERIAL_NUMBER:
3404 case SECURITY_PROTOCOL_IN:
3405 case SECURITY_PROTOCOL_OUT:
3406 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3407 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3408 break;
3409 case SERVICE_ACTION_IN:
3410 case ACCESS_CONTROL_IN:
3411 case ACCESS_CONTROL_OUT:
3412 case EXTENDED_COPY:
3413 case READ_ATTRIBUTE:
3414 case RECEIVE_COPY_RESULTS:
3415 case WRITE_ATTRIBUTE:
3416 size = (cdb[10] << 24) | (cdb[11] << 16) |
3417 (cdb[12] << 8) | cdb[13];
3418 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3419 break;
3420 case RECEIVE_DIAGNOSTIC:
3421 case SEND_DIAGNOSTIC:
3422 size = (cdb[3] << 8) | cdb[4];
3423 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3424 break;
3425 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3426 #if 0
3427 case GPCMD_READ_CD:
3428 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3429 size = (2336 * sectors);
3430 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3431 break;
3432 #endif
3433 case READ_TOC:
3434 size = cdb[8];
3435 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3436 break;
3437 case REQUEST_SENSE:
3438 size = cdb[4];
3439 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3440 break;
3441 case READ_ELEMENT_STATUS:
3442 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
3443 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3444 break;
3445 case WRITE_BUFFER:
3446 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3447 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3448 break;
3449 case RESERVE:
3450 case RESERVE_10:
3452 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3453 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3455 if (cdb[0] == RESERVE_10)
3456 size = (cdb[7] << 8) | cdb[8];
3457 else
3458 size = cmd->data_length;
3461 * Setup the legacy emulated handler for SPC-2 and
3462 * >= SPC-3 compatible reservation handling (CRH=1)
3463 * Otherwise, we assume the underlying SCSI logic is
3464 * is running in SPC_PASSTHROUGH, and wants reservations
3465 * emulation disabled.
3467 cmd->transport_emulate_cdb =
3468 (T10_RES(su_dev)->res_type !=
3469 SPC_PASSTHROUGH) ?
3470 &core_scsi2_emulate_crh : NULL;
3471 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3472 break;
3473 case RELEASE:
3474 case RELEASE_10:
3476 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3477 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3479 if (cdb[0] == RELEASE_10)
3480 size = (cdb[7] << 8) | cdb[8];
3481 else
3482 size = cmd->data_length;
3484 cmd->transport_emulate_cdb =
3485 (T10_RES(su_dev)->res_type !=
3486 SPC_PASSTHROUGH) ?
3487 &core_scsi2_emulate_crh : NULL;
3488 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3489 break;
3490 case SYNCHRONIZE_CACHE:
3491 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3493 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3495 if (cdb[0] == SYNCHRONIZE_CACHE) {
3496 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3497 T_TASK(cmd)->t_task_lba = transport_lba_32(cdb);
3498 } else {
3499 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3500 T_TASK(cmd)->t_task_lba = transport_lba_64(cdb);
3502 if (sector_ret)
3503 goto out_unsupported_cdb;
3505 size = transport_get_size(sectors, cdb, cmd);
3506 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3509 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3511 if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3512 break;
3514 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3515 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3517 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3519 * Check to ensure that LBA + Range does not exceed past end of
3520 * device.
3522 if (transport_get_sectors(cmd) < 0)
3523 goto out_invalid_cdb_field;
3524 break;
3525 case UNMAP:
3526 size = get_unaligned_be16(&cdb[7]);
3527 passthrough = (TRANSPORT(dev)->transport_type ==
3528 TRANSPORT_PLUGIN_PHBA_PDEV);
3530 * Determine if the received UNMAP used to for direct passthrough
3531 * into Linux/SCSI with struct request via TCM/pSCSI or we are
3532 * signaling the use of internal transport_generic_unmap() emulation
3533 * for UNMAP -> Linux/BLOCK disbard with TCM/IBLOCK and TCM/FILEIO
3534 * subsystem plugin backstores.
3536 if (!(passthrough))
3537 cmd->se_cmd_flags |= SCF_EMULATE_SYNC_UNMAP;
3539 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3540 break;
3541 case WRITE_SAME_16:
3542 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3543 if (sector_ret)
3544 goto out_unsupported_cdb;
3545 size = transport_get_size(sectors, cdb, cmd);
3546 T_TASK(cmd)->t_task_lba = get_unaligned_be16(&cdb[2]);
3547 passthrough = (TRANSPORT(dev)->transport_type ==
3548 TRANSPORT_PLUGIN_PHBA_PDEV);
3550 * Determine if the received WRITE_SAME_16 is used to for direct
3551 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
3552 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
3553 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
3554 * TCM/FILEIO subsystem plugin backstores.
3556 if (!(passthrough)) {
3557 if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
3558 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3559 " bits not supported for Block Discard"
3560 " Emulation\n");
3561 goto out_invalid_cdb_field;
3564 * Currently for the emulated case we only accept
3565 * tpws with the UNMAP=1 bit set.
3567 if (!(cdb[1] & 0x08)) {
3568 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
3569 " supported for Block Discard Emulation\n");
3570 goto out_invalid_cdb_field;
3573 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3574 break;
3575 case ALLOW_MEDIUM_REMOVAL:
3576 case GPCMD_CLOSE_TRACK:
3577 case ERASE:
3578 case INITIALIZE_ELEMENT_STATUS:
3579 case GPCMD_LOAD_UNLOAD:
3580 case REZERO_UNIT:
3581 case SEEK_10:
3582 case GPCMD_SET_SPEED:
3583 case SPACE:
3584 case START_STOP:
3585 case TEST_UNIT_READY:
3586 case VERIFY:
3587 case WRITE_FILEMARKS:
3588 case MOVE_MEDIUM:
3589 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3590 break;
3591 case REPORT_LUNS:
3592 cmd->transport_emulate_cdb =
3593 &transport_core_report_lun_response;
3594 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3596 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3597 * See spc4r17 section 5.3
3599 if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3600 cmd->sam_task_attr = TASK_ATTR_HOQ;
3601 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_NONSG_IO_CDB;
3602 break;
3603 default:
3604 printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
3605 " 0x%02x, sending CHECK_CONDITION.\n",
3606 CMD_TFO(cmd)->get_fabric_name(), cdb[0]);
3607 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3608 goto out_unsupported_cdb;
3611 if (size != cmd->data_length) {
3612 printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
3613 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3614 " 0x%02x\n", CMD_TFO(cmd)->get_fabric_name(),
3615 cmd->data_length, size, cdb[0]);
3617 cmd->cmd_spdtl = size;
3619 if (cmd->data_direction == DMA_TO_DEVICE) {
3620 printk(KERN_ERR "Rejecting underflow/overflow"
3621 " WRITE data\n");
3622 goto out_invalid_cdb_field;
3625 * Reject READ_* or WRITE_* with overflow/underflow for
3626 * type SCF_SCSI_DATA_SG_IO_CDB.
3628 if (!(ret) && (DEV_ATTRIB(dev)->block_size != 512)) {
3629 printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
3630 " CDB on non 512-byte sector setup subsystem"
3631 " plugin: %s\n", TRANSPORT(dev)->name);
3632 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3633 goto out_invalid_cdb_field;
3636 if (size > cmd->data_length) {
3637 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3638 cmd->residual_count = (size - cmd->data_length);
3639 } else {
3640 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3641 cmd->residual_count = (cmd->data_length - size);
3643 cmd->data_length = size;
3646 transport_set_supported_SAM_opcode(cmd);
3647 return ret;
3649 out_unsupported_cdb:
3650 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3651 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3652 return -2;
3653 out_invalid_cdb_field:
3654 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3655 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3656 return -2;
3659 static inline void transport_release_tasks(struct se_cmd *);
3662 * This function will copy a contiguous *src buffer into a destination
3663 * struct scatterlist array.
3665 static void transport_memcpy_write_contig(
3666 struct se_cmd *cmd,
3667 struct scatterlist *sg_d,
3668 unsigned char *src)
3670 u32 i = 0, length = 0, total_length = cmd->data_length;
3671 void *dst;
3673 while (total_length) {
3674 length = sg_d[i].length;
3676 if (length > total_length)
3677 length = total_length;
3679 dst = sg_virt(&sg_d[i]);
3681 memcpy(dst, src, length);
3683 if (!(total_length -= length))
3684 return;
3686 src += length;
3687 i++;
3692 * This function will copy a struct scatterlist array *sg_s into a destination
3693 * contiguous *dst buffer.
3695 static void transport_memcpy_read_contig(
3696 struct se_cmd *cmd,
3697 unsigned char *dst,
3698 struct scatterlist *sg_s)
3700 u32 i = 0, length = 0, total_length = cmd->data_length;
3701 void *src;
3703 while (total_length) {
3704 length = sg_s[i].length;
3706 if (length > total_length)
3707 length = total_length;
3709 src = sg_virt(&sg_s[i]);
3711 memcpy(dst, src, length);
3713 if (!(total_length -= length))
3714 return;
3716 dst += length;
3717 i++;
3721 static void transport_memcpy_se_mem_read_contig(
3722 struct se_cmd *cmd,
3723 unsigned char *dst,
3724 struct list_head *se_mem_list)
3726 struct se_mem *se_mem;
3727 void *src;
3728 u32 length = 0, total_length = cmd->data_length;
3730 list_for_each_entry(se_mem, se_mem_list, se_list) {
3731 length = se_mem->se_len;
3733 if (length > total_length)
3734 length = total_length;
3736 src = page_address(se_mem->se_page) + se_mem->se_off;
3738 memcpy(dst, src, length);
3740 if (!(total_length -= length))
3741 return;
3743 dst += length;
3748 * Called from transport_generic_complete_ok() and
3749 * transport_generic_request_failure() to determine which dormant/delayed
3750 * and ordered cmds need to have their tasks added to the execution queue.
3752 static void transport_complete_task_attr(struct se_cmd *cmd)
3754 struct se_device *dev = SE_DEV(cmd);
3755 struct se_cmd *cmd_p, *cmd_tmp;
3756 int new_active_tasks = 0;
3758 if (cmd->sam_task_attr == TASK_ATTR_SIMPLE) {
3759 atomic_dec(&dev->simple_cmds);
3760 smp_mb__after_atomic_dec();
3761 dev->dev_cur_ordered_id++;
3762 DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
3763 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3764 cmd->se_ordered_id);
3765 } else if (cmd->sam_task_attr == TASK_ATTR_HOQ) {
3766 atomic_dec(&dev->dev_hoq_count);
3767 smp_mb__after_atomic_dec();
3768 dev->dev_cur_ordered_id++;
3769 DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
3770 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3771 cmd->se_ordered_id);
3772 } else if (cmd->sam_task_attr == TASK_ATTR_ORDERED) {
3773 spin_lock(&dev->ordered_cmd_lock);
3774 list_del(&cmd->se_ordered_list);
3775 atomic_dec(&dev->dev_ordered_sync);
3776 smp_mb__after_atomic_dec();
3777 spin_unlock(&dev->ordered_cmd_lock);
3779 dev->dev_cur_ordered_id++;
3780 DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
3781 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3784 * Process all commands up to the last received
3785 * ORDERED task attribute which requires another blocking
3786 * boundary
3788 spin_lock(&dev->delayed_cmd_lock);
3789 list_for_each_entry_safe(cmd_p, cmd_tmp,
3790 &dev->delayed_cmd_list, se_delayed_list) {
3792 list_del(&cmd_p->se_delayed_list);
3793 spin_unlock(&dev->delayed_cmd_lock);
3795 DEBUG_STA("Calling add_tasks() for"
3796 " cmd_p: 0x%02x Task Attr: 0x%02x"
3797 " Dormant -> Active, se_ordered_id: %u\n",
3798 T_TASK(cmd_p)->t_task_cdb[0],
3799 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3801 transport_add_tasks_from_cmd(cmd_p);
3802 new_active_tasks++;
3804 spin_lock(&dev->delayed_cmd_lock);
3805 if (cmd_p->sam_task_attr == TASK_ATTR_ORDERED)
3806 break;
3808 spin_unlock(&dev->delayed_cmd_lock);
3810 * If new tasks have become active, wake up the transport thread
3811 * to do the processing of the Active tasks.
3813 if (new_active_tasks != 0)
3814 wake_up_interruptible(&dev->dev_queue_obj->thread_wq);
3817 static void transport_generic_complete_ok(struct se_cmd *cmd)
3819 int reason = 0;
3821 * Check if we need to move delayed/dormant tasks from cmds on the
3822 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3823 * Attribute.
3825 if (SE_DEV(cmd)->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3826 transport_complete_task_attr(cmd);
3828 * Check if we need to retrieve a sense buffer from
3829 * the struct se_cmd in question.
3831 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3832 if (transport_get_sense_data(cmd) < 0)
3833 reason = TCM_NON_EXISTENT_LUN;
3836 * Only set when an struct se_task->task_scsi_status returned
3837 * a non GOOD status.
3839 if (cmd->scsi_status) {
3840 transport_send_check_condition_and_sense(
3841 cmd, reason, 1);
3842 transport_lun_remove_cmd(cmd);
3843 transport_cmd_check_stop_to_fabric(cmd);
3844 return;
3848 * Check for a callback, used by amoungst other things
3849 * XDWRITE_READ_10 emulation.
3851 if (cmd->transport_complete_callback)
3852 cmd->transport_complete_callback(cmd);
3854 switch (cmd->data_direction) {
3855 case DMA_FROM_DEVICE:
3856 spin_lock(&cmd->se_lun->lun_sep_lock);
3857 if (SE_LUN(cmd)->lun_sep) {
3858 SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
3859 cmd->data_length;
3861 spin_unlock(&cmd->se_lun->lun_sep_lock);
3863 * If enabled by TCM fabirc module pre-registered SGL
3864 * memory, perform the memcpy() from the TCM internal
3865 * contigious buffer back to the original SGL.
3867 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
3868 transport_memcpy_write_contig(cmd,
3869 T_TASK(cmd)->t_task_pt_sgl,
3870 T_TASK(cmd)->t_task_buf);
3872 CMD_TFO(cmd)->queue_data_in(cmd);
3873 break;
3874 case DMA_TO_DEVICE:
3875 spin_lock(&cmd->se_lun->lun_sep_lock);
3876 if (SE_LUN(cmd)->lun_sep) {
3877 SE_LUN(cmd)->lun_sep->sep_stats.rx_data_octets +=
3878 cmd->data_length;
3880 spin_unlock(&cmd->se_lun->lun_sep_lock);
3882 * Check if we need to send READ payload for BIDI-COMMAND
3884 if (T_TASK(cmd)->t_mem_bidi_list != NULL) {
3885 spin_lock(&cmd->se_lun->lun_sep_lock);
3886 if (SE_LUN(cmd)->lun_sep) {
3887 SE_LUN(cmd)->lun_sep->sep_stats.tx_data_octets +=
3888 cmd->data_length;
3890 spin_unlock(&cmd->se_lun->lun_sep_lock);
3891 CMD_TFO(cmd)->queue_data_in(cmd);
3892 break;
3894 /* Fall through for DMA_TO_DEVICE */
3895 case DMA_NONE:
3896 CMD_TFO(cmd)->queue_status(cmd);
3897 break;
3898 default:
3899 break;
3902 transport_lun_remove_cmd(cmd);
3903 transport_cmd_check_stop_to_fabric(cmd);
3906 static void transport_free_dev_tasks(struct se_cmd *cmd)
3908 struct se_task *task, *task_tmp;
3909 unsigned long flags;
3911 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
3912 list_for_each_entry_safe(task, task_tmp,
3913 &T_TASK(cmd)->t_task_list, t_list) {
3914 if (atomic_read(&task->task_active))
3915 continue;
3917 kfree(task->task_sg_bidi);
3918 kfree(task->task_sg);
3920 list_del(&task->t_list);
3922 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
3923 if (task->se_dev)
3924 TRANSPORT(task->se_dev)->free_task(task);
3925 else
3926 printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
3927 task->task_no);
3928 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
3930 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
3933 static inline void transport_free_pages(struct se_cmd *cmd)
3935 struct se_mem *se_mem, *se_mem_tmp;
3936 int free_page = 1;
3938 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3939 free_page = 0;
3940 if (cmd->se_dev->transport->do_se_mem_map)
3941 free_page = 0;
3943 if (T_TASK(cmd)->t_task_buf) {
3944 kfree(T_TASK(cmd)->t_task_buf);
3945 T_TASK(cmd)->t_task_buf = NULL;
3946 return;
3950 * Caller will handle releasing of struct se_mem.
3952 if (cmd->se_cmd_flags & SCF_CMD_PASSTHROUGH_NOALLOC)
3953 return;
3955 if (!(T_TASK(cmd)->t_tasks_se_num))
3956 return;
3958 list_for_each_entry_safe(se_mem, se_mem_tmp,
3959 T_TASK(cmd)->t_mem_list, se_list) {
3961 * We only release call __free_page(struct se_mem->se_page) when
3962 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3964 if (free_page)
3965 __free_page(se_mem->se_page);
3967 list_del(&se_mem->se_list);
3968 kmem_cache_free(se_mem_cache, se_mem);
3971 if (T_TASK(cmd)->t_mem_bidi_list && T_TASK(cmd)->t_tasks_se_bidi_num) {
3972 list_for_each_entry_safe(se_mem, se_mem_tmp,
3973 T_TASK(cmd)->t_mem_bidi_list, se_list) {
3975 * We only release call __free_page(struct se_mem->se_page) when
3976 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3978 if (free_page)
3979 __free_page(se_mem->se_page);
3981 list_del(&se_mem->se_list);
3982 kmem_cache_free(se_mem_cache, se_mem);
3986 kfree(T_TASK(cmd)->t_mem_bidi_list);
3987 T_TASK(cmd)->t_mem_bidi_list = NULL;
3988 kfree(T_TASK(cmd)->t_mem_list);
3989 T_TASK(cmd)->t_mem_list = NULL;
3990 T_TASK(cmd)->t_tasks_se_num = 0;
3993 static inline void transport_release_tasks(struct se_cmd *cmd)
3995 transport_free_dev_tasks(cmd);
3998 static inline int transport_dec_and_check(struct se_cmd *cmd)
4000 unsigned long flags;
4002 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4003 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
4004 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_fe_count))) {
4005 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
4006 flags);
4007 return 1;
4011 if (atomic_read(&T_TASK(cmd)->t_se_count)) {
4012 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_se_count))) {
4013 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
4014 flags);
4015 return 1;
4018 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4020 return 0;
4023 static void transport_release_fe_cmd(struct se_cmd *cmd)
4025 unsigned long flags;
4027 if (transport_dec_and_check(cmd))
4028 return;
4030 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4031 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
4032 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4033 goto free_pages;
4035 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
4036 transport_all_task_dev_remove_state(cmd);
4037 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4039 transport_release_tasks(cmd);
4040 free_pages:
4041 transport_free_pages(cmd);
4042 transport_free_se_cmd(cmd);
4043 CMD_TFO(cmd)->release_cmd_direct(cmd);
4046 static int transport_generic_remove(
4047 struct se_cmd *cmd,
4048 int release_to_pool,
4049 int session_reinstatement)
4051 unsigned long flags;
4053 if (!(T_TASK(cmd)))
4054 goto release_cmd;
4056 if (transport_dec_and_check(cmd)) {
4057 if (session_reinstatement) {
4058 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4059 transport_all_task_dev_remove_state(cmd);
4060 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
4061 flags);
4063 return 1;
4066 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
4067 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
4068 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4069 goto free_pages;
4071 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
4072 transport_all_task_dev_remove_state(cmd);
4073 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
4075 transport_release_tasks(cmd);
4076 free_pages:
4077 transport_free_pages(cmd);
4079 release_cmd:
4080 if (release_to_pool) {
4081 transport_release_cmd_to_pool(cmd);
4082 } else {
4083 transport_free_se_cmd(cmd);
4084 CMD_TFO(cmd)->release_cmd_direct(cmd);
4087 return 0;
4091 * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
4092 * @cmd: Associated se_cmd descriptor
4093 * @mem: SGL style memory for TCM WRITE / READ
4094 * @sg_mem_num: Number of SGL elements
4095 * @mem_bidi_in: SGL style memory for TCM BIDI READ
4096 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
4098 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
4099 * of parameters.
4101 int transport_generic_map_mem_to_cmd(
4102 struct se_cmd *cmd,
4103 struct scatterlist *mem,
4104 u32 sg_mem_num,
4105 struct scatterlist *mem_bidi_in,
4106 u32 sg_mem_bidi_num)
4108 u32 se_mem_cnt_out = 0;
4109 int ret;
4111 if (!(mem) || !(sg_mem_num))
4112 return 0;
4114 * Passed *mem will contain a list_head containing preformatted
4115 * struct se_mem elements...
4117 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM)) {
4118 if ((mem_bidi_in) || (sg_mem_bidi_num)) {
4119 printk(KERN_ERR "SCF_CMD_PASSTHROUGH_NOALLOC not supported"
4120 " with BIDI-COMMAND\n");
4121 return -ENOSYS;
4124 T_TASK(cmd)->t_mem_list = (struct list_head *)mem;
4125 T_TASK(cmd)->t_tasks_se_num = sg_mem_num;
4126 cmd->se_cmd_flags |= SCF_CMD_PASSTHROUGH_NOALLOC;
4127 return 0;
4130 * Otherwise, assume the caller is passing a struct scatterlist
4131 * array from include/linux/scatterlist.h
4133 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
4134 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
4136 * For CDB using TCM struct se_mem linked list scatterlist memory
4137 * processed into a TCM struct se_subsystem_dev, we do the mapping
4138 * from the passed physical memory to struct se_mem->se_page here.
4140 T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
4141 if (!(T_TASK(cmd)->t_mem_list))
4142 return -ENOMEM;
4144 ret = transport_map_sg_to_mem(cmd,
4145 T_TASK(cmd)->t_mem_list, mem, &se_mem_cnt_out);
4146 if (ret < 0)
4147 return -ENOMEM;
4149 T_TASK(cmd)->t_tasks_se_num = se_mem_cnt_out;
4151 * Setup BIDI READ list of struct se_mem elements
4153 if ((mem_bidi_in) && (sg_mem_bidi_num)) {
4154 T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
4155 if (!(T_TASK(cmd)->t_mem_bidi_list)) {
4156 kfree(T_TASK(cmd)->t_mem_list);
4157 return -ENOMEM;
4159 se_mem_cnt_out = 0;
4161 ret = transport_map_sg_to_mem(cmd,
4162 T_TASK(cmd)->t_mem_bidi_list, mem_bidi_in,
4163 &se_mem_cnt_out);
4164 if (ret < 0) {
4165 kfree(T_TASK(cmd)->t_mem_list);
4166 return -ENOMEM;
4169 T_TASK(cmd)->t_tasks_se_bidi_num = se_mem_cnt_out;
4171 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
4173 } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
4174 if (mem_bidi_in || sg_mem_bidi_num) {
4175 printk(KERN_ERR "BIDI-Commands not supported using "
4176 "SCF_SCSI_CONTROL_NONSG_IO_CDB\n");
4177 return -ENOSYS;
4180 * For incoming CDBs using a contiguous buffer internall with TCM,
4181 * save the passed struct scatterlist memory. After TCM storage object
4182 * processing has completed for this struct se_cmd, TCM core will call
4183 * transport_memcpy_[write,read]_contig() as necessary from
4184 * transport_generic_complete_ok() and transport_write_pending() in order
4185 * to copy the TCM buffer to/from the original passed *mem in SGL ->
4186 * struct scatterlist format.
4188 cmd->se_cmd_flags |= SCF_PASSTHROUGH_CONTIG_TO_SG;
4189 T_TASK(cmd)->t_task_pt_sgl = mem;
4192 return 0;
4194 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
4197 static inline long long transport_dev_end_lba(struct se_device *dev)
4199 return dev->transport->get_blocks(dev) + 1;
4202 static int transport_get_sectors(struct se_cmd *cmd)
4204 struct se_device *dev = SE_DEV(cmd);
4206 T_TASK(cmd)->t_tasks_sectors =
4207 (cmd->data_length / DEV_ATTRIB(dev)->block_size);
4208 if (!(T_TASK(cmd)->t_tasks_sectors))
4209 T_TASK(cmd)->t_tasks_sectors = 1;
4211 if (TRANSPORT(dev)->get_device_type(dev) != TYPE_DISK)
4212 return 0;
4214 if ((T_TASK(cmd)->t_task_lba + T_TASK(cmd)->t_tasks_sectors) >
4215 transport_dev_end_lba(dev)) {
4216 printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
4217 " transport_dev_end_lba(): %llu\n",
4218 T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
4219 transport_dev_end_lba(dev));
4220 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4221 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
4222 return PYX_TRANSPORT_REQ_TOO_MANY_SECTORS;
4225 return 0;
4228 static int transport_new_cmd_obj(struct se_cmd *cmd)
4230 struct se_device *dev = SE_DEV(cmd);
4231 u32 task_cdbs = 0, rc;
4233 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
4234 task_cdbs++;
4235 T_TASK(cmd)->t_task_cdbs++;
4236 } else {
4237 int set_counts = 1;
4240 * Setup any BIDI READ tasks and memory from
4241 * T_TASK(cmd)->t_mem_bidi_list so the READ struct se_tasks
4242 * are queued first for the non pSCSI passthrough case.
4244 if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
4245 (TRANSPORT(dev)->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
4246 rc = transport_generic_get_cdb_count(cmd,
4247 T_TASK(cmd)->t_task_lba,
4248 T_TASK(cmd)->t_tasks_sectors,
4249 DMA_FROM_DEVICE, T_TASK(cmd)->t_mem_bidi_list,
4250 set_counts);
4251 if (!(rc)) {
4252 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4253 cmd->scsi_sense_reason =
4254 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4255 return PYX_TRANSPORT_LU_COMM_FAILURE;
4257 set_counts = 0;
4260 * Setup the tasks and memory from T_TASK(cmd)->t_mem_list
4261 * Note for BIDI transfers this will contain the WRITE payload
4263 task_cdbs = transport_generic_get_cdb_count(cmd,
4264 T_TASK(cmd)->t_task_lba,
4265 T_TASK(cmd)->t_tasks_sectors,
4266 cmd->data_direction, T_TASK(cmd)->t_mem_list,
4267 set_counts);
4268 if (!(task_cdbs)) {
4269 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4270 cmd->scsi_sense_reason =
4271 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4272 return PYX_TRANSPORT_LU_COMM_FAILURE;
4274 T_TASK(cmd)->t_task_cdbs += task_cdbs;
4276 #if 0
4277 printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
4278 " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
4279 T_TASK(cmd)->t_task_lba, T_TASK(cmd)->t_tasks_sectors,
4280 T_TASK(cmd)->t_task_cdbs);
4281 #endif
4284 atomic_set(&T_TASK(cmd)->t_task_cdbs_left, task_cdbs);
4285 atomic_set(&T_TASK(cmd)->t_task_cdbs_ex_left, task_cdbs);
4286 atomic_set(&T_TASK(cmd)->t_task_cdbs_timeout_left, task_cdbs);
4287 return 0;
4290 static struct list_head *transport_init_se_mem_list(void)
4292 struct list_head *se_mem_list;
4294 se_mem_list = kzalloc(sizeof(struct list_head), GFP_KERNEL);
4295 if (!(se_mem_list)) {
4296 printk(KERN_ERR "Unable to allocate memory for se_mem_list\n");
4297 return NULL;
4299 INIT_LIST_HEAD(se_mem_list);
4301 return se_mem_list;
4304 static int
4305 transport_generic_get_mem(struct se_cmd *cmd, u32 length, u32 dma_size)
4307 unsigned char *buf;
4308 struct se_mem *se_mem;
4310 T_TASK(cmd)->t_mem_list = transport_init_se_mem_list();
4311 if (!(T_TASK(cmd)->t_mem_list))
4312 return -ENOMEM;
4315 * If the device uses memory mapping this is enough.
4317 if (cmd->se_dev->transport->do_se_mem_map)
4318 return 0;
4321 * Setup BIDI-COMMAND READ list of struct se_mem elements
4323 if (T_TASK(cmd)->t_tasks_bidi) {
4324 T_TASK(cmd)->t_mem_bidi_list = transport_init_se_mem_list();
4325 if (!(T_TASK(cmd)->t_mem_bidi_list)) {
4326 kfree(T_TASK(cmd)->t_mem_list);
4327 return -ENOMEM;
4331 while (length) {
4332 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4333 if (!(se_mem)) {
4334 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4335 goto out;
4337 INIT_LIST_HEAD(&se_mem->se_list);
4338 se_mem->se_len = (length > dma_size) ? dma_size : length;
4340 /* #warning FIXME Allocate contigous pages for struct se_mem elements */
4341 se_mem->se_page = (struct page *) alloc_pages(GFP_KERNEL, 0);
4342 if (!(se_mem->se_page)) {
4343 printk(KERN_ERR "alloc_pages() failed\n");
4344 goto out;
4347 buf = kmap_atomic(se_mem->se_page, KM_IRQ0);
4348 if (!(buf)) {
4349 printk(KERN_ERR "kmap_atomic() failed\n");
4350 goto out;
4352 memset(buf, 0, se_mem->se_len);
4353 kunmap_atomic(buf, KM_IRQ0);
4355 list_add_tail(&se_mem->se_list, T_TASK(cmd)->t_mem_list);
4356 T_TASK(cmd)->t_tasks_se_num++;
4358 DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
4359 " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
4360 se_mem->se_off);
4362 length -= se_mem->se_len;
4365 DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
4366 T_TASK(cmd)->t_tasks_se_num);
4368 return 0;
4369 out:
4370 return -1;
4373 extern u32 transport_calc_sg_num(
4374 struct se_task *task,
4375 struct se_mem *in_se_mem,
4376 u32 task_offset)
4378 struct se_cmd *se_cmd = task->task_se_cmd;
4379 struct se_device *se_dev = SE_DEV(se_cmd);
4380 struct se_mem *se_mem = in_se_mem;
4381 struct target_core_fabric_ops *tfo = CMD_TFO(se_cmd);
4382 u32 sg_length, task_size = task->task_size, task_sg_num_padded;
4384 while (task_size != 0) {
4385 DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
4386 " se_mem->se_off(%u) task_offset(%u)\n",
4387 se_mem->se_page, se_mem->se_len,
4388 se_mem->se_off, task_offset);
4390 if (task_offset == 0) {
4391 if (task_size >= se_mem->se_len) {
4392 sg_length = se_mem->se_len;
4394 if (!(list_is_last(&se_mem->se_list,
4395 T_TASK(se_cmd)->t_mem_list)))
4396 se_mem = list_entry(se_mem->se_list.next,
4397 struct se_mem, se_list);
4398 } else {
4399 sg_length = task_size;
4400 task_size -= sg_length;
4401 goto next;
4404 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4405 sg_length, task_size);
4406 } else {
4407 if ((se_mem->se_len - task_offset) > task_size) {
4408 sg_length = task_size;
4409 task_size -= sg_length;
4410 goto next;
4411 } else {
4412 sg_length = (se_mem->se_len - task_offset);
4414 if (!(list_is_last(&se_mem->se_list,
4415 T_TASK(se_cmd)->t_mem_list)))
4416 se_mem = list_entry(se_mem->se_list.next,
4417 struct se_mem, se_list);
4420 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4421 sg_length, task_size);
4423 task_offset = 0;
4425 task_size -= sg_length;
4426 next:
4427 DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
4428 task->task_no, task_size);
4430 task->task_sg_num++;
4433 * Check if the fabric module driver is requesting that all
4434 * struct se_task->task_sg[] be chained together.. If so,
4435 * then allocate an extra padding SG entry for linking and
4436 * marking the end of the chained SGL.
4438 if (tfo->task_sg_chaining) {
4439 task_sg_num_padded = (task->task_sg_num + 1);
4440 task->task_padded_sg = 1;
4441 } else
4442 task_sg_num_padded = task->task_sg_num;
4444 task->task_sg = kzalloc(task_sg_num_padded *
4445 sizeof(struct scatterlist), GFP_KERNEL);
4446 if (!(task->task_sg)) {
4447 printk(KERN_ERR "Unable to allocate memory for"
4448 " task->task_sg\n");
4449 return 0;
4451 sg_init_table(&task->task_sg[0], task_sg_num_padded);
4453 * Setup task->task_sg_bidi for SCSI READ payload for
4454 * TCM/pSCSI passthrough if present for BIDI-COMMAND
4456 if ((T_TASK(se_cmd)->t_mem_bidi_list != NULL) &&
4457 (TRANSPORT(se_dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
4458 task->task_sg_bidi = kzalloc(task_sg_num_padded *
4459 sizeof(struct scatterlist), GFP_KERNEL);
4460 if (!(task->task_sg_bidi)) {
4461 printk(KERN_ERR "Unable to allocate memory for"
4462 " task->task_sg_bidi\n");
4463 return 0;
4465 sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
4468 * For the chaining case, setup the proper end of SGL for the
4469 * initial submission struct task into struct se_subsystem_api.
4470 * This will be cleared later by transport_do_task_sg_chain()
4472 if (task->task_padded_sg) {
4473 sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
4475 * Added the 'if' check before marking end of bi-directional
4476 * scatterlist (which gets created only in case of request
4477 * (RD + WR).
4479 if (task->task_sg_bidi)
4480 sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
4483 DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
4484 " task_sg_num_padded(%u)\n", task->task_sg_num,
4485 task_sg_num_padded);
4487 return task->task_sg_num;
4490 static inline int transport_set_tasks_sectors_disk(
4491 struct se_task *task,
4492 struct se_device *dev,
4493 unsigned long long lba,
4494 u32 sectors,
4495 int *max_sectors_set)
4497 if ((lba + sectors) > transport_dev_end_lba(dev)) {
4498 task->task_sectors = ((transport_dev_end_lba(dev) - lba) + 1);
4500 if (task->task_sectors > DEV_ATTRIB(dev)->max_sectors) {
4501 task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
4502 *max_sectors_set = 1;
4504 } else {
4505 if (sectors > DEV_ATTRIB(dev)->max_sectors) {
4506 task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
4507 *max_sectors_set = 1;
4508 } else
4509 task->task_sectors = sectors;
4512 return 0;
4515 static inline int transport_set_tasks_sectors_non_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 (sectors > DEV_ATTRIB(dev)->max_sectors) {
4523 task->task_sectors = DEV_ATTRIB(dev)->max_sectors;
4524 *max_sectors_set = 1;
4525 } else
4526 task->task_sectors = sectors;
4528 return 0;
4531 static inline int transport_set_tasks_sectors(
4532 struct se_task *task,
4533 struct se_device *dev,
4534 unsigned long long lba,
4535 u32 sectors,
4536 int *max_sectors_set)
4538 return (TRANSPORT(dev)->get_device_type(dev) == TYPE_DISK) ?
4539 transport_set_tasks_sectors_disk(task, dev, lba, sectors,
4540 max_sectors_set) :
4541 transport_set_tasks_sectors_non_disk(task, dev, lba, sectors,
4542 max_sectors_set);
4545 static int transport_map_sg_to_mem(
4546 struct se_cmd *cmd,
4547 struct list_head *se_mem_list,
4548 void *in_mem,
4549 u32 *se_mem_cnt)
4551 struct se_mem *se_mem;
4552 struct scatterlist *sg;
4553 u32 sg_count = 1, cmd_size = cmd->data_length;
4555 if (!in_mem) {
4556 printk(KERN_ERR "No source scatterlist\n");
4557 return -1;
4559 sg = (struct scatterlist *)in_mem;
4561 while (cmd_size) {
4562 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4563 if (!(se_mem)) {
4564 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4565 return -1;
4567 INIT_LIST_HEAD(&se_mem->se_list);
4568 DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
4569 " sg_page: %p offset: %d length: %d\n", cmd_size,
4570 sg_page(sg), sg->offset, sg->length);
4572 se_mem->se_page = sg_page(sg);
4573 se_mem->se_off = sg->offset;
4575 if (cmd_size > sg->length) {
4576 se_mem->se_len = sg->length;
4577 sg = sg_next(sg);
4578 sg_count++;
4579 } else
4580 se_mem->se_len = cmd_size;
4582 cmd_size -= se_mem->se_len;
4584 DEBUG_MEM("sg_to_mem: *se_mem_cnt: %u cmd_size: %u\n",
4585 *se_mem_cnt, cmd_size);
4586 DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
4587 se_mem->se_page, se_mem->se_off, se_mem->se_len);
4589 list_add_tail(&se_mem->se_list, se_mem_list);
4590 (*se_mem_cnt)++;
4593 DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments to(%u)"
4594 " struct se_mem\n", sg_count, *se_mem_cnt);
4596 if (sg_count != *se_mem_cnt)
4597 BUG();
4599 return 0;
4602 /* transport_map_mem_to_sg():
4606 int transport_map_mem_to_sg(
4607 struct se_task *task,
4608 struct list_head *se_mem_list,
4609 void *in_mem,
4610 struct se_mem *in_se_mem,
4611 struct se_mem **out_se_mem,
4612 u32 *se_mem_cnt,
4613 u32 *task_offset)
4615 struct se_cmd *se_cmd = task->task_se_cmd;
4616 struct se_mem *se_mem = in_se_mem;
4617 struct scatterlist *sg = (struct scatterlist *)in_mem;
4618 u32 task_size = task->task_size, sg_no = 0;
4620 if (!sg) {
4621 printk(KERN_ERR "Unable to locate valid struct"
4622 " scatterlist pointer\n");
4623 return -1;
4626 while (task_size != 0) {
4628 * Setup the contigious array of scatterlists for
4629 * this struct se_task.
4631 sg_assign_page(sg, se_mem->se_page);
4633 if (*task_offset == 0) {
4634 sg->offset = se_mem->se_off;
4636 if (task_size >= se_mem->se_len) {
4637 sg->length = se_mem->se_len;
4639 if (!(list_is_last(&se_mem->se_list,
4640 T_TASK(se_cmd)->t_mem_list))) {
4641 se_mem = list_entry(se_mem->se_list.next,
4642 struct se_mem, se_list);
4643 (*se_mem_cnt)++;
4645 } else {
4646 sg->length = task_size;
4648 * Determine if we need to calculate an offset
4649 * into the struct se_mem on the next go around..
4651 task_size -= sg->length;
4652 if (!(task_size))
4653 *task_offset = sg->length;
4655 goto next;
4658 } else {
4659 sg->offset = (*task_offset + se_mem->se_off);
4661 if ((se_mem->se_len - *task_offset) > task_size) {
4662 sg->length = task_size;
4664 * Determine if we need to calculate an offset
4665 * into the struct se_mem on the next go around..
4667 task_size -= sg->length;
4668 if (!(task_size))
4669 *task_offset += sg->length;
4671 goto next;
4672 } else {
4673 sg->length = (se_mem->se_len - *task_offset);
4675 if (!(list_is_last(&se_mem->se_list,
4676 T_TASK(se_cmd)->t_mem_list))) {
4677 se_mem = list_entry(se_mem->se_list.next,
4678 struct se_mem, se_list);
4679 (*se_mem_cnt)++;
4683 *task_offset = 0;
4685 task_size -= sg->length;
4686 next:
4687 DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
4688 " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
4689 sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
4691 sg_no++;
4692 if (!(task_size))
4693 break;
4695 sg = sg_next(sg);
4697 if (task_size > se_cmd->data_length)
4698 BUG();
4700 *out_se_mem = se_mem;
4702 DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
4703 " SGs\n", task->task_no, *se_mem_cnt, sg_no);
4705 return 0;
4709 * This function can be used by HW target mode drivers to create a linked
4710 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4711 * This is intended to be called during the completion path by TCM Core
4712 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4714 void transport_do_task_sg_chain(struct se_cmd *cmd)
4716 struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
4717 struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
4718 struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
4719 struct se_task *task;
4720 struct target_core_fabric_ops *tfo = CMD_TFO(cmd);
4721 u32 task_sg_num = 0, sg_count = 0;
4722 int i;
4724 if (tfo->task_sg_chaining == 0) {
4725 printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
4726 " %s\n", tfo->get_fabric_name());
4727 dump_stack();
4728 return;
4731 * Walk the struct se_task list and setup scatterlist chains
4732 * for each contiguosly allocated struct se_task->task_sg[].
4734 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
4735 if (!(task->task_sg) || !(task->task_padded_sg))
4736 continue;
4738 if (sg_head && sg_link) {
4739 sg_head_cur = &task->task_sg[0];
4740 sg_link_cur = &task->task_sg[task->task_sg_num];
4742 * Either add chain or mark end of scatterlist
4744 if (!(list_is_last(&task->t_list,
4745 &T_TASK(cmd)->t_task_list))) {
4747 * Clear existing SGL termination bit set in
4748 * transport_calc_sg_num(), see sg_mark_end()
4750 sg_end_cur = &task->task_sg[task->task_sg_num - 1];
4751 sg_end_cur->page_link &= ~0x02;
4753 sg_chain(sg_head, task_sg_num, sg_head_cur);
4754 sg_count += (task->task_sg_num + 1);
4755 } else
4756 sg_count += task->task_sg_num;
4758 sg_head = sg_head_cur;
4759 sg_link = sg_link_cur;
4760 task_sg_num = task->task_sg_num;
4761 continue;
4763 sg_head = sg_first = &task->task_sg[0];
4764 sg_link = &task->task_sg[task->task_sg_num];
4765 task_sg_num = task->task_sg_num;
4767 * Check for single task..
4769 if (!(list_is_last(&task->t_list, &T_TASK(cmd)->t_task_list))) {
4771 * Clear existing SGL termination bit set in
4772 * transport_calc_sg_num(), see sg_mark_end()
4774 sg_end = &task->task_sg[task->task_sg_num - 1];
4775 sg_end->page_link &= ~0x02;
4776 sg_count += (task->task_sg_num + 1);
4777 } else
4778 sg_count += task->task_sg_num;
4781 * Setup the starting pointer and total t_tasks_sg_linked_no including
4782 * padding SGs for linking and to mark the end.
4784 T_TASK(cmd)->t_tasks_sg_chained = sg_first;
4785 T_TASK(cmd)->t_tasks_sg_chained_no = sg_count;
4787 DEBUG_CMD_M("Setup T_TASK(cmd)->t_tasks_sg_chained: %p and"
4788 " t_tasks_sg_chained_no: %u\n", T_TASK(cmd)->t_tasks_sg_chained,
4789 T_TASK(cmd)->t_tasks_sg_chained_no);
4791 for_each_sg(T_TASK(cmd)->t_tasks_sg_chained, sg,
4792 T_TASK(cmd)->t_tasks_sg_chained_no, i) {
4794 DEBUG_CMD_M("SG: %p page: %p length: %d offset: %d\n",
4795 sg, sg_page(sg), sg->length, sg->offset);
4796 if (sg_is_chain(sg))
4797 DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
4798 if (sg_is_last(sg))
4799 DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
4803 EXPORT_SYMBOL(transport_do_task_sg_chain);
4805 static int transport_do_se_mem_map(
4806 struct se_device *dev,
4807 struct se_task *task,
4808 struct list_head *se_mem_list,
4809 void *in_mem,
4810 struct se_mem *in_se_mem,
4811 struct se_mem **out_se_mem,
4812 u32 *se_mem_cnt,
4813 u32 *task_offset_in)
4815 u32 task_offset = *task_offset_in;
4816 int ret = 0;
4818 * se_subsystem_api_t->do_se_mem_map is used when internal allocation
4819 * has been done by the transport plugin.
4821 if (TRANSPORT(dev)->do_se_mem_map) {
4822 ret = TRANSPORT(dev)->do_se_mem_map(task, se_mem_list,
4823 in_mem, in_se_mem, out_se_mem, se_mem_cnt,
4824 task_offset_in);
4825 if (ret == 0)
4826 T_TASK(task->task_se_cmd)->t_tasks_se_num += *se_mem_cnt;
4828 return ret;
4831 BUG_ON(list_empty(se_mem_list));
4833 * This is the normal path for all normal non BIDI and BIDI-COMMAND
4834 * WRITE payloads.. If we need to do BIDI READ passthrough for
4835 * TCM/pSCSI the first call to transport_do_se_mem_map ->
4836 * transport_calc_sg_num() -> transport_map_mem_to_sg() will do the
4837 * allocation for task->task_sg_bidi, and the subsequent call to
4838 * transport_do_se_mem_map() from transport_generic_get_cdb_count()
4840 if (!(task->task_sg_bidi)) {
4842 * Assume default that transport plugin speaks preallocated
4843 * scatterlists.
4845 if (!(transport_calc_sg_num(task, in_se_mem, task_offset)))
4846 return -1;
4848 * struct se_task->task_sg now contains the struct scatterlist array.
4850 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
4851 in_se_mem, out_se_mem, se_mem_cnt,
4852 task_offset_in);
4855 * Handle the se_mem_list -> struct task->task_sg_bidi
4856 * memory map for the extra BIDI READ payload
4858 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
4859 in_se_mem, out_se_mem, se_mem_cnt,
4860 task_offset_in);
4863 static u32 transport_generic_get_cdb_count(
4864 struct se_cmd *cmd,
4865 unsigned long long lba,
4866 u32 sectors,
4867 enum dma_data_direction data_direction,
4868 struct list_head *mem_list,
4869 int set_counts)
4871 unsigned char *cdb = NULL;
4872 struct se_task *task;
4873 struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
4874 struct se_mem *se_mem_bidi = NULL, *se_mem_bidi_lout = NULL;
4875 struct se_device *dev = SE_DEV(cmd);
4876 int max_sectors_set = 0, ret;
4877 u32 task_offset_in = 0, se_mem_cnt = 0, se_mem_bidi_cnt = 0, task_cdbs = 0;
4879 if (!mem_list) {
4880 printk(KERN_ERR "mem_list is NULL in transport_generic_get"
4881 "_cdb_count()\n");
4882 return 0;
4885 * While using RAMDISK_DR backstores is the only case where
4886 * mem_list will ever be empty at this point.
4888 if (!(list_empty(mem_list)))
4889 se_mem = list_entry(mem_list->next, struct se_mem, se_list);
4891 * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
4892 * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
4894 if ((T_TASK(cmd)->t_mem_bidi_list != NULL) &&
4895 !(list_empty(T_TASK(cmd)->t_mem_bidi_list)) &&
4896 (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
4897 se_mem_bidi = list_entry(T_TASK(cmd)->t_mem_bidi_list->next,
4898 struct se_mem, se_list);
4900 while (sectors) {
4901 DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
4902 CMD_TFO(cmd)->get_task_tag(cmd), lba, sectors,
4903 transport_dev_end_lba(dev));
4905 task = transport_generic_get_task(cmd, data_direction);
4906 if (!(task))
4907 goto out;
4909 transport_set_tasks_sectors(task, dev, lba, sectors,
4910 &max_sectors_set);
4912 task->task_lba = lba;
4913 lba += task->task_sectors;
4914 sectors -= task->task_sectors;
4915 task->task_size = (task->task_sectors *
4916 DEV_ATTRIB(dev)->block_size);
4918 cdb = TRANSPORT(dev)->get_cdb(task);
4919 if ((cdb)) {
4920 memcpy(cdb, T_TASK(cmd)->t_task_cdb,
4921 scsi_command_size(T_TASK(cmd)->t_task_cdb));
4922 cmd->transport_split_cdb(task->task_lba,
4923 &task->task_sectors, cdb);
4927 * Perform the SE OBJ plugin and/or Transport plugin specific
4928 * mapping for T_TASK(cmd)->t_mem_list. And setup the
4929 * task->task_sg and if necessary task->task_sg_bidi
4931 ret = transport_do_se_mem_map(dev, task, mem_list,
4932 NULL, se_mem, &se_mem_lout, &se_mem_cnt,
4933 &task_offset_in);
4934 if (ret < 0)
4935 goto out;
4937 se_mem = se_mem_lout;
4939 * Setup the T_TASK(cmd)->t_mem_bidi_list -> task->task_sg_bidi
4940 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
4942 * Note that the first call to transport_do_se_mem_map() above will
4943 * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
4944 * -> transport_calc_sg_num(), and the second here will do the
4945 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
4947 if (task->task_sg_bidi != NULL) {
4948 ret = transport_do_se_mem_map(dev, task,
4949 T_TASK(cmd)->t_mem_bidi_list, NULL,
4950 se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
4951 &task_offset_in);
4952 if (ret < 0)
4953 goto out;
4955 se_mem_bidi = se_mem_bidi_lout;
4957 task_cdbs++;
4959 DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
4960 task_cdbs, task->task_sg_num);
4962 if (max_sectors_set) {
4963 max_sectors_set = 0;
4964 continue;
4967 if (!sectors)
4968 break;
4971 if (set_counts) {
4972 atomic_inc(&T_TASK(cmd)->t_fe_count);
4973 atomic_inc(&T_TASK(cmd)->t_se_count);
4976 DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
4977 CMD_TFO(cmd)->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
4978 ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
4980 return task_cdbs;
4981 out:
4982 return 0;
4985 static int
4986 transport_map_control_cmd_to_task(struct se_cmd *cmd)
4988 struct se_device *dev = SE_DEV(cmd);
4989 unsigned char *cdb;
4990 struct se_task *task;
4991 int ret;
4993 task = transport_generic_get_task(cmd, cmd->data_direction);
4994 if (!task)
4995 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
4997 cdb = TRANSPORT(dev)->get_cdb(task);
4998 if (cdb)
4999 memcpy(cdb, cmd->t_task->t_task_cdb,
5000 scsi_command_size(cmd->t_task->t_task_cdb));
5002 task->task_size = cmd->data_length;
5003 task->task_sg_num =
5004 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
5006 atomic_inc(&cmd->t_task->t_fe_count);
5007 atomic_inc(&cmd->t_task->t_se_count);
5009 if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
5010 struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
5011 u32 se_mem_cnt = 0, task_offset = 0;
5013 if (!list_empty(T_TASK(cmd)->t_mem_list))
5014 se_mem = list_entry(T_TASK(cmd)->t_mem_list->next,
5015 struct se_mem, se_list);
5017 ret = transport_do_se_mem_map(dev, task,
5018 cmd->t_task->t_mem_list, NULL, se_mem,
5019 &se_mem_lout, &se_mem_cnt, &task_offset);
5020 if (ret < 0)
5021 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
5023 if (dev->transport->map_task_SG)
5024 return dev->transport->map_task_SG(task);
5025 return 0;
5026 } else if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_NONSG_IO_CDB) {
5027 if (dev->transport->map_task_non_SG)
5028 return dev->transport->map_task_non_SG(task);
5029 return 0;
5030 } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
5031 if (dev->transport->cdb_none)
5032 return dev->transport->cdb_none(task);
5033 return 0;
5034 } else {
5035 BUG();
5036 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
5040 /* transport_generic_new_cmd(): Called from transport_processing_thread()
5042 * Allocate storage transport resources from a set of values predefined
5043 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
5044 * Any non zero return here is treated as an "out of resource' op here.
5047 * Generate struct se_task(s) and/or their payloads for this CDB.
5049 static int transport_generic_new_cmd(struct se_cmd *cmd)
5051 struct se_portal_group *se_tpg;
5052 struct se_task *task;
5053 struct se_device *dev = SE_DEV(cmd);
5054 int ret = 0;
5057 * Determine is the TCM fabric module has already allocated physical
5058 * memory, and is directly calling transport_generic_map_mem_to_cmd()
5059 * to setup beforehand the linked list of physical memory at
5060 * T_TASK(cmd)->t_mem_list of struct se_mem->se_page
5062 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
5063 ret = transport_allocate_resources(cmd);
5064 if (ret < 0)
5065 return ret;
5068 ret = transport_get_sectors(cmd);
5069 if (ret < 0)
5070 return ret;
5072 ret = transport_new_cmd_obj(cmd);
5073 if (ret < 0)
5074 return ret;
5077 * Determine if the calling TCM fabric module is talking to
5078 * Linux/NET via kernel sockets and needs to allocate a
5079 * struct iovec array to complete the struct se_cmd
5081 se_tpg = SE_LUN(cmd)->lun_sep->sep_tpg;
5082 if (TPG_TFO(se_tpg)->alloc_cmd_iovecs != NULL) {
5083 ret = TPG_TFO(se_tpg)->alloc_cmd_iovecs(cmd);
5084 if (ret < 0)
5085 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
5088 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
5089 list_for_each_entry(task, &T_TASK(cmd)->t_task_list, t_list) {
5090 if (atomic_read(&task->task_sent))
5091 continue;
5092 if (!dev->transport->map_task_SG)
5093 continue;
5095 ret = dev->transport->map_task_SG(task);
5096 if (ret < 0)
5097 return ret;
5099 } else {
5100 ret = transport_map_control_cmd_to_task(cmd);
5101 if (ret < 0)
5102 return ret;
5106 * For WRITEs, let the iSCSI Target RX Thread know its buffer is ready..
5107 * This WRITE struct se_cmd (and all of its associated struct se_task's)
5108 * will be added to the struct se_device execution queue after its WRITE
5109 * data has arrived. (ie: It gets handled by the transport processing
5110 * thread a second time)
5112 if (cmd->data_direction == DMA_TO_DEVICE) {
5113 transport_add_tasks_to_state_queue(cmd);
5114 return transport_generic_write_pending(cmd);
5117 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
5118 * to the execution queue.
5120 transport_execute_tasks(cmd);
5121 return 0;
5124 /* transport_generic_process_write():
5128 void transport_generic_process_write(struct se_cmd *cmd)
5130 #if 0
5132 * Copy SCSI Presented DTL sector(s) from received buffers allocated to
5133 * original EDTL
5135 if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
5136 if (!T_TASK(cmd)->t_tasks_se_num) {
5137 unsigned char *dst, *buf =
5138 (unsigned char *)T_TASK(cmd)->t_task_buf;
5140 dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
5141 if (!(dst)) {
5142 printk(KERN_ERR "Unable to allocate memory for"
5143 " WRITE underflow\n");
5144 transport_generic_request_failure(cmd, NULL,
5145 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
5146 return;
5148 memcpy(dst, buf, cmd->cmd_spdtl);
5150 kfree(T_TASK(cmd)->t_task_buf);
5151 T_TASK(cmd)->t_task_buf = dst;
5152 } else {
5153 struct scatterlist *sg =
5154 (struct scatterlist *sg)T_TASK(cmd)->t_task_buf;
5155 struct scatterlist *orig_sg;
5157 orig_sg = kzalloc(sizeof(struct scatterlist) *
5158 T_TASK(cmd)->t_tasks_se_num,
5159 GFP_KERNEL))) {
5160 if (!(orig_sg)) {
5161 printk(KERN_ERR "Unable to allocate memory"
5162 " for WRITE underflow\n");
5163 transport_generic_request_failure(cmd, NULL,
5164 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
5165 return;
5168 memcpy(orig_sg, T_TASK(cmd)->t_task_buf,
5169 sizeof(struct scatterlist) *
5170 T_TASK(cmd)->t_tasks_se_num);
5172 cmd->data_length = cmd->cmd_spdtl;
5174 * FIXME, clear out original struct se_task and state
5175 * information.
5177 if (transport_generic_new_cmd(cmd) < 0) {
5178 transport_generic_request_failure(cmd, NULL,
5179 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
5180 kfree(orig_sg);
5181 return;
5184 transport_memcpy_write_sg(cmd, orig_sg);
5187 #endif
5188 transport_execute_tasks(cmd);
5190 EXPORT_SYMBOL(transport_generic_process_write);
5192 /* transport_generic_write_pending():
5196 static int transport_generic_write_pending(struct se_cmd *cmd)
5198 unsigned long flags;
5199 int ret;
5201 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5202 cmd->t_state = TRANSPORT_WRITE_PENDING;
5203 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5205 * For the TCM control CDBs using a contiguous buffer, do the memcpy
5206 * from the passed Linux/SCSI struct scatterlist located at
5207 * T_TASK(se_cmd)->t_task_pt_buf to the contiguous buffer at
5208 * T_TASK(se_cmd)->t_task_buf.
5210 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_CONTIG_TO_SG)
5211 transport_memcpy_read_contig(cmd,
5212 T_TASK(cmd)->t_task_buf,
5213 T_TASK(cmd)->t_task_pt_sgl);
5215 * Clear the se_cmd for WRITE_PENDING status in order to set
5216 * T_TASK(cmd)->t_transport_active=0 so that transport_generic_handle_data
5217 * can be called from HW target mode interrupt code. This is safe
5218 * to be called with transport_off=1 before the CMD_TFO(cmd)->write_pending
5219 * because the se_cmd->se_lun pointer is not being cleared.
5221 transport_cmd_check_stop(cmd, 1, 0);
5224 * Call the fabric write_pending function here to let the
5225 * frontend know that WRITE buffers are ready.
5227 ret = CMD_TFO(cmd)->write_pending(cmd);
5228 if (ret < 0)
5229 return ret;
5231 return PYX_TRANSPORT_WRITE_PENDING;
5234 /* transport_release_cmd_to_pool():
5238 void transport_release_cmd_to_pool(struct se_cmd *cmd)
5240 BUG_ON(!T_TASK(cmd));
5241 BUG_ON(!CMD_TFO(cmd));
5243 transport_free_se_cmd(cmd);
5244 CMD_TFO(cmd)->release_cmd_to_pool(cmd);
5246 EXPORT_SYMBOL(transport_release_cmd_to_pool);
5248 /* transport_generic_free_cmd():
5250 * Called from processing frontend to release storage engine resources
5252 void transport_generic_free_cmd(
5253 struct se_cmd *cmd,
5254 int wait_for_tasks,
5255 int release_to_pool,
5256 int session_reinstatement)
5258 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) || !T_TASK(cmd))
5259 transport_release_cmd_to_pool(cmd);
5260 else {
5261 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
5263 if (SE_LUN(cmd)) {
5264 #if 0
5265 printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
5266 " SE_LUN(cmd)\n", cmd,
5267 CMD_TFO(cmd)->get_task_tag(cmd));
5268 #endif
5269 transport_lun_remove_cmd(cmd);
5272 if (wait_for_tasks && cmd->transport_wait_for_tasks)
5273 cmd->transport_wait_for_tasks(cmd, 0, 0);
5275 transport_generic_remove(cmd, release_to_pool,
5276 session_reinstatement);
5279 EXPORT_SYMBOL(transport_generic_free_cmd);
5281 static void transport_nop_wait_for_tasks(
5282 struct se_cmd *cmd,
5283 int remove_cmd,
5284 int session_reinstatement)
5286 return;
5289 /* transport_lun_wait_for_tasks():
5291 * Called from ConfigFS context to stop the passed struct se_cmd to allow
5292 * an struct se_lun to be successfully shutdown.
5294 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
5296 unsigned long flags;
5297 int ret;
5299 * If the frontend has already requested this struct se_cmd to
5300 * be stopped, we can safely ignore this struct se_cmd.
5302 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5303 if (atomic_read(&T_TASK(cmd)->t_transport_stop)) {
5304 atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
5305 DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
5306 " TRUE, skipping\n", CMD_TFO(cmd)->get_task_tag(cmd));
5307 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5308 transport_cmd_check_stop(cmd, 1, 0);
5309 return -1;
5311 atomic_set(&T_TASK(cmd)->transport_lun_fe_stop, 1);
5312 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5314 wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
5316 ret = transport_stop_tasks_for_cmd(cmd);
5318 DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
5319 " %d\n", cmd, T_TASK(cmd)->t_task_cdbs, ret);
5320 if (!ret) {
5321 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
5322 CMD_TFO(cmd)->get_task_tag(cmd));
5323 wait_for_completion(&T_TASK(cmd)->transport_lun_stop_comp);
5324 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
5325 CMD_TFO(cmd)->get_task_tag(cmd));
5327 transport_remove_cmd_from_queue(cmd, SE_DEV(cmd)->dev_queue_obj);
5329 return 0;
5332 /* #define DEBUG_CLEAR_LUN */
5333 #ifdef DEBUG_CLEAR_LUN
5334 #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
5335 #else
5336 #define DEBUG_CLEAR_L(x...)
5337 #endif
5339 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
5341 struct se_cmd *cmd = NULL;
5342 unsigned long lun_flags, cmd_flags;
5344 * Do exception processing and return CHECK_CONDITION status to the
5345 * Initiator Port.
5347 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5348 while (!list_empty_careful(&lun->lun_cmd_list)) {
5349 cmd = list_entry(lun->lun_cmd_list.next,
5350 struct se_cmd, se_lun_list);
5351 list_del(&cmd->se_lun_list);
5353 if (!(T_TASK(cmd))) {
5354 printk(KERN_ERR "ITT: 0x%08x, T_TASK(cmd) = NULL"
5355 "[i,t]_state: %u/%u\n",
5356 CMD_TFO(cmd)->get_task_tag(cmd),
5357 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
5358 BUG();
5360 atomic_set(&T_TASK(cmd)->transport_lun_active, 0);
5362 * This will notify iscsi_target_transport.c:
5363 * transport_cmd_check_stop() that a LUN shutdown is in
5364 * progress for the iscsi_cmd_t.
5366 spin_lock(&T_TASK(cmd)->t_state_lock);
5367 DEBUG_CLEAR_L("SE_LUN[%d] - Setting T_TASK(cmd)->transport"
5368 "_lun_stop for ITT: 0x%08x\n",
5369 SE_LUN(cmd)->unpacked_lun,
5370 CMD_TFO(cmd)->get_task_tag(cmd));
5371 atomic_set(&T_TASK(cmd)->transport_lun_stop, 1);
5372 spin_unlock(&T_TASK(cmd)->t_state_lock);
5374 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5376 if (!(SE_LUN(cmd))) {
5377 printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
5378 CMD_TFO(cmd)->get_task_tag(cmd),
5379 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state);
5380 BUG();
5383 * If the Storage engine still owns the iscsi_cmd_t, determine
5384 * and/or stop its context.
5386 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
5387 "_lun_wait_for_tasks()\n", SE_LUN(cmd)->unpacked_lun,
5388 CMD_TFO(cmd)->get_task_tag(cmd));
5390 if (transport_lun_wait_for_tasks(cmd, SE_LUN(cmd)) < 0) {
5391 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5392 continue;
5395 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
5396 "_wait_for_tasks(): SUCCESS\n",
5397 SE_LUN(cmd)->unpacked_lun,
5398 CMD_TFO(cmd)->get_task_tag(cmd));
5400 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
5401 if (!(atomic_read(&T_TASK(cmd)->transport_dev_active))) {
5402 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
5403 goto check_cond;
5405 atomic_set(&T_TASK(cmd)->transport_dev_active, 0);
5406 transport_all_task_dev_remove_state(cmd);
5407 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
5409 transport_free_dev_tasks(cmd);
5411 * The Storage engine stopped this struct se_cmd before it was
5412 * send to the fabric frontend for delivery back to the
5413 * Initiator Node. Return this SCSI CDB back with an
5414 * CHECK_CONDITION status.
5416 check_cond:
5417 transport_send_check_condition_and_sense(cmd,
5418 TCM_NON_EXISTENT_LUN, 0);
5420 * If the fabric frontend is waiting for this iscsi_cmd_t to
5421 * be released, notify the waiting thread now that LU has
5422 * finished accessing it.
5424 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, cmd_flags);
5425 if (atomic_read(&T_TASK(cmd)->transport_lun_fe_stop)) {
5426 DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
5427 " struct se_cmd: %p ITT: 0x%08x\n",
5428 lun->unpacked_lun,
5429 cmd, CMD_TFO(cmd)->get_task_tag(cmd));
5431 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
5432 cmd_flags);
5433 transport_cmd_check_stop(cmd, 1, 0);
5434 complete(&T_TASK(cmd)->transport_lun_fe_stop_comp);
5435 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5436 continue;
5438 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
5439 lun->unpacked_lun, CMD_TFO(cmd)->get_task_tag(cmd));
5441 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, cmd_flags);
5442 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5444 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5447 static int transport_clear_lun_thread(void *p)
5449 struct se_lun *lun = (struct se_lun *)p;
5451 __transport_clear_lun_from_sessions(lun);
5452 complete(&lun->lun_shutdown_comp);
5454 return 0;
5457 int transport_clear_lun_from_sessions(struct se_lun *lun)
5459 struct task_struct *kt;
5461 kt = kthread_run(transport_clear_lun_thread, (void *)lun,
5462 "tcm_cl_%u", lun->unpacked_lun);
5463 if (IS_ERR(kt)) {
5464 printk(KERN_ERR "Unable to start clear_lun thread\n");
5465 return -1;
5467 wait_for_completion(&lun->lun_shutdown_comp);
5469 return 0;
5472 /* transport_generic_wait_for_tasks():
5474 * Called from frontend or passthrough context to wait for storage engine
5475 * to pause and/or release frontend generated struct se_cmd.
5477 static void transport_generic_wait_for_tasks(
5478 struct se_cmd *cmd,
5479 int remove_cmd,
5480 int session_reinstatement)
5482 unsigned long flags;
5484 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
5485 return;
5487 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5489 * If we are already stopped due to an external event (ie: LUN shutdown)
5490 * sleep until the connection can have the passed struct se_cmd back.
5491 * The T_TASK(cmd)->transport_lun_stopped_sem will be upped by
5492 * transport_clear_lun_from_sessions() once the ConfigFS context caller
5493 * has completed its operation on the struct se_cmd.
5495 if (atomic_read(&T_TASK(cmd)->transport_lun_stop)) {
5497 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
5498 " wait_for_completion(&T_TASK(cmd)transport_lun_fe"
5499 "_stop_comp); for ITT: 0x%08x\n",
5500 CMD_TFO(cmd)->get_task_tag(cmd));
5502 * There is a special case for WRITES where a FE exception +
5503 * LUN shutdown means ConfigFS context is still sleeping on
5504 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
5505 * We go ahead and up transport_lun_stop_comp just to be sure
5506 * here.
5508 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5509 complete(&T_TASK(cmd)->transport_lun_stop_comp);
5510 wait_for_completion(&T_TASK(cmd)->transport_lun_fe_stop_comp);
5511 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5513 transport_all_task_dev_remove_state(cmd);
5515 * At this point, the frontend who was the originator of this
5516 * struct se_cmd, now owns the structure and can be released through
5517 * normal means below.
5519 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
5520 " wait_for_completion(&T_TASK(cmd)transport_lun_fe_"
5521 "stop_comp); for ITT: 0x%08x\n",
5522 CMD_TFO(cmd)->get_task_tag(cmd));
5524 atomic_set(&T_TASK(cmd)->transport_lun_stop, 0);
5526 if (!atomic_read(&T_TASK(cmd)->t_transport_active))
5527 goto remove;
5529 atomic_set(&T_TASK(cmd)->t_transport_stop, 1);
5531 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
5532 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
5533 " = TRUE\n", cmd, CMD_TFO(cmd)->get_task_tag(cmd),
5534 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
5535 cmd->deferred_t_state);
5537 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5539 wake_up_interruptible(&SE_DEV(cmd)->dev_queue_obj->thread_wq);
5541 wait_for_completion(&T_TASK(cmd)->t_transport_stop_comp);
5543 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5544 atomic_set(&T_TASK(cmd)->t_transport_active, 0);
5545 atomic_set(&T_TASK(cmd)->t_transport_stop, 0);
5547 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
5548 "&T_TASK(cmd)->t_transport_stop_comp) for ITT: 0x%08x\n",
5549 CMD_TFO(cmd)->get_task_tag(cmd));
5550 remove:
5551 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5552 if (!remove_cmd)
5553 return;
5555 transport_generic_free_cmd(cmd, 0, 0, session_reinstatement);
5558 static int transport_get_sense_codes(
5559 struct se_cmd *cmd,
5560 u8 *asc,
5561 u8 *ascq)
5563 *asc = cmd->scsi_asc;
5564 *ascq = cmd->scsi_ascq;
5566 return 0;
5569 static int transport_set_sense_codes(
5570 struct se_cmd *cmd,
5571 u8 asc,
5572 u8 ascq)
5574 cmd->scsi_asc = asc;
5575 cmd->scsi_ascq = ascq;
5577 return 0;
5580 int transport_send_check_condition_and_sense(
5581 struct se_cmd *cmd,
5582 u8 reason,
5583 int from_transport)
5585 unsigned char *buffer = cmd->sense_buffer;
5586 unsigned long flags;
5587 int offset;
5588 u8 asc = 0, ascq = 0;
5590 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5591 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
5592 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5593 return 0;
5595 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
5596 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
5598 if (!reason && from_transport)
5599 goto after_reason;
5601 if (!from_transport)
5602 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
5604 * Data Segment and SenseLength of the fabric response PDU.
5606 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
5607 * from include/scsi/scsi_cmnd.h
5609 offset = CMD_TFO(cmd)->set_fabric_sense_len(cmd,
5610 TRANSPORT_SENSE_BUFFER);
5612 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
5613 * SENSE KEY values from include/scsi/scsi.h
5615 switch (reason) {
5616 case TCM_NON_EXISTENT_LUN:
5617 case TCM_UNSUPPORTED_SCSI_OPCODE:
5618 case TCM_SECTOR_COUNT_TOO_MANY:
5619 /* CURRENT ERROR */
5620 buffer[offset] = 0x70;
5621 /* ILLEGAL REQUEST */
5622 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5623 /* INVALID COMMAND OPERATION CODE */
5624 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
5625 break;
5626 case TCM_UNKNOWN_MODE_PAGE:
5627 /* CURRENT ERROR */
5628 buffer[offset] = 0x70;
5629 /* ILLEGAL REQUEST */
5630 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5631 /* INVALID FIELD IN CDB */
5632 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5633 break;
5634 case TCM_CHECK_CONDITION_ABORT_CMD:
5635 /* CURRENT ERROR */
5636 buffer[offset] = 0x70;
5637 /* ABORTED COMMAND */
5638 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5639 /* BUS DEVICE RESET FUNCTION OCCURRED */
5640 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
5641 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
5642 break;
5643 case TCM_INCORRECT_AMOUNT_OF_DATA:
5644 /* CURRENT ERROR */
5645 buffer[offset] = 0x70;
5646 /* ABORTED COMMAND */
5647 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5648 /* WRITE ERROR */
5649 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
5650 /* NOT ENOUGH UNSOLICITED DATA */
5651 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
5652 break;
5653 case TCM_INVALID_CDB_FIELD:
5654 /* CURRENT ERROR */
5655 buffer[offset] = 0x70;
5656 /* ABORTED COMMAND */
5657 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5658 /* INVALID FIELD IN CDB */
5659 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5660 break;
5661 case TCM_INVALID_PARAMETER_LIST:
5662 /* CURRENT ERROR */
5663 buffer[offset] = 0x70;
5664 /* ABORTED COMMAND */
5665 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5666 /* INVALID FIELD IN PARAMETER LIST */
5667 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
5668 break;
5669 case TCM_UNEXPECTED_UNSOLICITED_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 /* UNEXPECTED_UNSOLICITED_DATA */
5677 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
5678 break;
5679 case TCM_SERVICE_CRC_ERROR:
5680 /* CURRENT ERROR */
5681 buffer[offset] = 0x70;
5682 /* ABORTED COMMAND */
5683 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5684 /* PROTOCOL SERVICE CRC ERROR */
5685 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
5686 /* N/A */
5687 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
5688 break;
5689 case TCM_SNACK_REJECTED:
5690 /* CURRENT ERROR */
5691 buffer[offset] = 0x70;
5692 /* ABORTED COMMAND */
5693 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5694 /* READ ERROR */
5695 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
5696 /* FAILED RETRANSMISSION REQUEST */
5697 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
5698 break;
5699 case TCM_WRITE_PROTECTED:
5700 /* CURRENT ERROR */
5701 buffer[offset] = 0x70;
5702 /* DATA PROTECT */
5703 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
5704 /* WRITE PROTECTED */
5705 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
5706 break;
5707 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
5708 /* CURRENT ERROR */
5709 buffer[offset] = 0x70;
5710 /* UNIT ATTENTION */
5711 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
5712 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
5713 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5714 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5715 break;
5716 case TCM_CHECK_CONDITION_NOT_READY:
5717 /* CURRENT ERROR */
5718 buffer[offset] = 0x70;
5719 /* Not Ready */
5720 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
5721 transport_get_sense_codes(cmd, &asc, &ascq);
5722 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5723 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5724 break;
5725 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
5726 default:
5727 /* CURRENT ERROR */
5728 buffer[offset] = 0x70;
5729 /* ILLEGAL REQUEST */
5730 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5731 /* LOGICAL UNIT COMMUNICATION FAILURE */
5732 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
5733 break;
5736 * This code uses linux/include/scsi/scsi.h SAM status codes!
5738 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
5740 * Automatically padded, this value is encoded in the fabric's
5741 * data_length response PDU containing the SCSI defined sense data.
5743 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
5745 after_reason:
5746 CMD_TFO(cmd)->queue_status(cmd);
5747 return 0;
5749 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
5751 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
5753 int ret = 0;
5755 if (atomic_read(&T_TASK(cmd)->t_transport_aborted) != 0) {
5756 if (!(send_status) ||
5757 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
5758 return 1;
5759 #if 0
5760 printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
5761 " status for CDB: 0x%02x ITT: 0x%08x\n",
5762 T_TASK(cmd)->t_task_cdb[0],
5763 CMD_TFO(cmd)->get_task_tag(cmd));
5764 #endif
5765 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
5766 CMD_TFO(cmd)->queue_status(cmd);
5767 ret = 1;
5769 return ret;
5771 EXPORT_SYMBOL(transport_check_aborted_status);
5773 void transport_send_task_abort(struct se_cmd *cmd)
5776 * If there are still expected incoming fabric WRITEs, we wait
5777 * until until they have completed before sending a TASK_ABORTED
5778 * response. This response with TASK_ABORTED status will be
5779 * queued back to fabric module by transport_check_aborted_status().
5781 if (cmd->data_direction == DMA_TO_DEVICE) {
5782 if (CMD_TFO(cmd)->write_pending_status(cmd) != 0) {
5783 atomic_inc(&T_TASK(cmd)->t_transport_aborted);
5784 smp_mb__after_atomic_inc();
5785 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5786 transport_new_cmd_failure(cmd);
5787 return;
5790 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5791 #if 0
5792 printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
5793 " ITT: 0x%08x\n", T_TASK(cmd)->t_task_cdb[0],
5794 CMD_TFO(cmd)->get_task_tag(cmd));
5795 #endif
5796 CMD_TFO(cmd)->queue_status(cmd);
5799 /* transport_generic_do_tmr():
5803 int transport_generic_do_tmr(struct se_cmd *cmd)
5805 struct se_cmd *ref_cmd;
5806 struct se_device *dev = SE_DEV(cmd);
5807 struct se_tmr_req *tmr = cmd->se_tmr_req;
5808 int ret;
5810 switch (tmr->function) {
5811 case ABORT_TASK:
5812 ref_cmd = tmr->ref_cmd;
5813 tmr->response = TMR_FUNCTION_REJECTED;
5814 break;
5815 case ABORT_TASK_SET:
5816 case CLEAR_ACA:
5817 case CLEAR_TASK_SET:
5818 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
5819 break;
5820 case LUN_RESET:
5821 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
5822 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
5823 TMR_FUNCTION_REJECTED;
5824 break;
5825 #if 0
5826 case TARGET_WARM_RESET:
5827 transport_generic_host_reset(dev->se_hba);
5828 tmr->response = TMR_FUNCTION_REJECTED;
5829 break;
5830 case TARGET_COLD_RESET:
5831 transport_generic_host_reset(dev->se_hba);
5832 transport_generic_cold_reset(dev->se_hba);
5833 tmr->response = TMR_FUNCTION_REJECTED;
5834 break;
5835 #endif
5836 default:
5837 printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
5838 tmr->function);
5839 tmr->response = TMR_FUNCTION_REJECTED;
5840 break;
5843 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
5844 CMD_TFO(cmd)->queue_tm_rsp(cmd);
5846 transport_cmd_check_stop(cmd, 2, 0);
5847 return 0;
5851 * Called with spin_lock_irq(&dev->execute_task_lock); held
5854 static struct se_task *
5855 transport_get_task_from_state_list(struct se_device *dev)
5857 struct se_task *task;
5859 if (list_empty(&dev->state_task_list))
5860 return NULL;
5862 list_for_each_entry(task, &dev->state_task_list, t_state_list)
5863 break;
5865 list_del(&task->t_state_list);
5866 atomic_set(&task->task_state_active, 0);
5868 return task;
5871 static void transport_processing_shutdown(struct se_device *dev)
5873 struct se_cmd *cmd;
5874 struct se_queue_req *qr;
5875 struct se_task *task;
5876 u8 state;
5877 unsigned long flags;
5879 * Empty the struct se_device's struct se_task state list.
5881 spin_lock_irqsave(&dev->execute_task_lock, flags);
5882 while ((task = transport_get_task_from_state_list(dev))) {
5883 if (!(TASK_CMD(task))) {
5884 printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
5885 continue;
5887 cmd = TASK_CMD(task);
5889 if (!T_TASK(cmd)) {
5890 printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
5891 " %p ITT: 0x%08x\n", task, cmd,
5892 CMD_TFO(cmd)->get_task_tag(cmd));
5893 continue;
5895 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5897 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5899 DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
5900 " i_state/def_i_state: %d/%d, t_state/def_t_state:"
5901 " %d/%d cdb: 0x%02x\n", cmd, task,
5902 CMD_TFO(cmd)->get_task_tag(cmd), cmd->cmd_sn,
5903 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->deferred_i_state,
5904 cmd->t_state, cmd->deferred_t_state,
5905 T_TASK(cmd)->t_task_cdb[0]);
5906 DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
5907 " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5908 " t_transport_stop: %d t_transport_sent: %d\n",
5909 CMD_TFO(cmd)->get_task_tag(cmd),
5910 T_TASK(cmd)->t_task_cdbs,
5911 atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
5912 atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
5913 atomic_read(&T_TASK(cmd)->t_transport_active),
5914 atomic_read(&T_TASK(cmd)->t_transport_stop),
5915 atomic_read(&T_TASK(cmd)->t_transport_sent));
5917 if (atomic_read(&task->task_active)) {
5918 atomic_set(&task->task_stop, 1);
5919 spin_unlock_irqrestore(
5920 &T_TASK(cmd)->t_state_lock, flags);
5922 DEBUG_DO("Waiting for task: %p to shutdown for dev:"
5923 " %p\n", task, dev);
5924 wait_for_completion(&task->task_stop_comp);
5925 DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
5926 task, dev);
5928 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
5929 atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
5931 atomic_set(&task->task_active, 0);
5932 atomic_set(&task->task_stop, 0);
5934 __transport_stop_task_timer(task, &flags);
5936 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
5937 spin_unlock_irqrestore(
5938 &T_TASK(cmd)->t_state_lock, flags);
5940 DEBUG_DO("Skipping task: %p, dev: %p for"
5941 " t_task_cdbs_ex_left: %d\n", task, dev,
5942 atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
5944 spin_lock_irqsave(&dev->execute_task_lock, flags);
5945 continue;
5948 if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
5949 DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
5950 " %p\n", task, dev);
5952 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
5953 spin_unlock_irqrestore(
5954 &T_TASK(cmd)->t_state_lock, flags);
5955 transport_send_check_condition_and_sense(
5956 cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
5958 transport_remove_cmd_from_queue(cmd,
5959 SE_DEV(cmd)->dev_queue_obj);
5961 transport_lun_remove_cmd(cmd);
5962 transport_cmd_check_stop(cmd, 1, 0);
5963 } else {
5964 spin_unlock_irqrestore(
5965 &T_TASK(cmd)->t_state_lock, flags);
5967 transport_remove_cmd_from_queue(cmd,
5968 SE_DEV(cmd)->dev_queue_obj);
5970 transport_lun_remove_cmd(cmd);
5972 if (transport_cmd_check_stop(cmd, 1, 0))
5973 transport_generic_remove(cmd, 0, 0);
5976 spin_lock_irqsave(&dev->execute_task_lock, flags);
5977 continue;
5979 DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
5980 task, dev);
5982 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
5983 spin_unlock_irqrestore(
5984 &T_TASK(cmd)->t_state_lock, flags);
5985 transport_send_check_condition_and_sense(cmd,
5986 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
5987 transport_remove_cmd_from_queue(cmd,
5988 SE_DEV(cmd)->dev_queue_obj);
5990 transport_lun_remove_cmd(cmd);
5991 transport_cmd_check_stop(cmd, 1, 0);
5992 } else {
5993 spin_unlock_irqrestore(
5994 &T_TASK(cmd)->t_state_lock, flags);
5996 transport_remove_cmd_from_queue(cmd,
5997 SE_DEV(cmd)->dev_queue_obj);
5998 transport_lun_remove_cmd(cmd);
6000 if (transport_cmd_check_stop(cmd, 1, 0))
6001 transport_generic_remove(cmd, 0, 0);
6004 spin_lock_irqsave(&dev->execute_task_lock, flags);
6006 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
6008 * Empty the struct se_device's struct se_cmd list.
6010 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
6011 while ((qr = __transport_get_qr_from_queue(dev->dev_queue_obj))) {
6012 spin_unlock_irqrestore(
6013 &dev->dev_queue_obj->cmd_queue_lock, flags);
6014 cmd = (struct se_cmd *)qr->cmd;
6015 state = qr->state;
6016 kfree(qr);
6018 DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
6019 cmd, state);
6021 if (atomic_read(&T_TASK(cmd)->t_fe_count)) {
6022 transport_send_check_condition_and_sense(cmd,
6023 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
6025 transport_lun_remove_cmd(cmd);
6026 transport_cmd_check_stop(cmd, 1, 0);
6027 } else {
6028 transport_lun_remove_cmd(cmd);
6029 if (transport_cmd_check_stop(cmd, 1, 0))
6030 transport_generic_remove(cmd, 0, 0);
6032 spin_lock_irqsave(&dev->dev_queue_obj->cmd_queue_lock, flags);
6034 spin_unlock_irqrestore(&dev->dev_queue_obj->cmd_queue_lock, flags);
6037 /* transport_processing_thread():
6041 static int transport_processing_thread(void *param)
6043 int ret, t_state;
6044 struct se_cmd *cmd;
6045 struct se_device *dev = (struct se_device *) param;
6046 struct se_queue_req *qr;
6048 set_user_nice(current, -20);
6050 while (!kthread_should_stop()) {
6051 ret = wait_event_interruptible(dev->dev_queue_obj->thread_wq,
6052 atomic_read(&dev->dev_queue_obj->queue_cnt) ||
6053 kthread_should_stop());
6054 if (ret < 0)
6055 goto out;
6057 spin_lock_irq(&dev->dev_status_lock);
6058 if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
6059 spin_unlock_irq(&dev->dev_status_lock);
6060 transport_processing_shutdown(dev);
6061 continue;
6063 spin_unlock_irq(&dev->dev_status_lock);
6065 get_cmd:
6066 __transport_execute_tasks(dev);
6068 qr = transport_get_qr_from_queue(dev->dev_queue_obj);
6069 if (!(qr))
6070 continue;
6072 cmd = (struct se_cmd *)qr->cmd;
6073 t_state = qr->state;
6074 kfree(qr);
6076 switch (t_state) {
6077 case TRANSPORT_NEW_CMD_MAP:
6078 if (!(CMD_TFO(cmd)->new_cmd_map)) {
6079 printk(KERN_ERR "CMD_TFO(cmd)->new_cmd_map is"
6080 " NULL for TRANSPORT_NEW_CMD_MAP\n");
6081 BUG();
6083 ret = CMD_TFO(cmd)->new_cmd_map(cmd);
6084 if (ret < 0) {
6085 cmd->transport_error_status = ret;
6086 transport_generic_request_failure(cmd, NULL,
6087 0, (cmd->data_direction !=
6088 DMA_TO_DEVICE));
6089 break;
6091 /* Fall through */
6092 case TRANSPORT_NEW_CMD:
6093 ret = transport_generic_new_cmd(cmd);
6094 if (ret < 0) {
6095 cmd->transport_error_status = ret;
6096 transport_generic_request_failure(cmd, NULL,
6097 0, (cmd->data_direction !=
6098 DMA_TO_DEVICE));
6100 break;
6101 case TRANSPORT_PROCESS_WRITE:
6102 transport_generic_process_write(cmd);
6103 break;
6104 case TRANSPORT_COMPLETE_OK:
6105 transport_stop_all_task_timers(cmd);
6106 transport_generic_complete_ok(cmd);
6107 break;
6108 case TRANSPORT_REMOVE:
6109 transport_generic_remove(cmd, 1, 0);
6110 break;
6111 case TRANSPORT_PROCESS_TMR:
6112 transport_generic_do_tmr(cmd);
6113 break;
6114 case TRANSPORT_COMPLETE_FAILURE:
6115 transport_generic_request_failure(cmd, NULL, 1, 1);
6116 break;
6117 case TRANSPORT_COMPLETE_TIMEOUT:
6118 transport_stop_all_task_timers(cmd);
6119 transport_generic_request_timeout(cmd);
6120 break;
6121 default:
6122 printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
6123 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
6124 " %u\n", t_state, cmd->deferred_t_state,
6125 CMD_TFO(cmd)->get_task_tag(cmd),
6126 CMD_TFO(cmd)->get_cmd_state(cmd),
6127 SE_LUN(cmd)->unpacked_lun);
6128 BUG();
6131 goto get_cmd;
6134 out:
6135 transport_release_all_cmds(dev);
6136 dev->process_thread = NULL;
6137 return 0;