target: Make all control CDBs scatter-gather
[deliverable/linux.git] / drivers / target / target_core_transport.c
1 /*******************************************************************************
2 * Filename: target_core_transport.c
3 *
4 * This file contains the Generic Target Engine Core.
5 *
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
10 *
11 * Nicholas A. Bellinger <nab@kernel.org>
12 *
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.
17 *
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.
22 *
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.
26 *
27 ******************************************************************************/
28
29 #include <linux/version.h>
30 #include <linux/net.h>
31 #include <linux/delay.h>
32 #include <linux/string.h>
33 #include <linux/timer.h>
34 #include <linux/slab.h>
35 #include <linux/blkdev.h>
36 #include <linux/spinlock.h>
37 #include <linux/kthread.h>
38 #include <linux/in.h>
39 #include <linux/cdrom.h>
40 #include <asm/unaligned.h>
41 #include <net/sock.h>
42 #include <net/tcp.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_tcq.h>
46
47 #include <target/target_core_base.h>
48 #include <target/target_core_device.h>
49 #include <target/target_core_tmr.h>
50 #include <target/target_core_tpg.h>
51 #include <target/target_core_transport.h>
52 #include <target/target_core_fabric_ops.h>
53 #include <target/target_core_configfs.h>
54
55 #include "target_core_alua.h"
56 #include "target_core_hba.h"
57 #include "target_core_pr.h"
58 #include "target_core_scdb.h"
59 #include "target_core_ua.h"
60
61 /* #define DEBUG_CDB_HANDLER */
62 #ifdef DEBUG_CDB_HANDLER
63 #define DEBUG_CDB_H(x...) printk(KERN_INFO x)
64 #else
65 #define DEBUG_CDB_H(x...)
66 #endif
67
68 /* #define DEBUG_CMD_MAP */
69 #ifdef DEBUG_CMD_MAP
70 #define DEBUG_CMD_M(x...) printk(KERN_INFO x)
71 #else
72 #define DEBUG_CMD_M(x...)
73 #endif
74
75 /* #define DEBUG_MEM_ALLOC */
76 #ifdef DEBUG_MEM_ALLOC
77 #define DEBUG_MEM(x...) printk(KERN_INFO x)
78 #else
79 #define DEBUG_MEM(x...)
80 #endif
81
82 /* #define DEBUG_MEM2_ALLOC */
83 #ifdef DEBUG_MEM2_ALLOC
84 #define DEBUG_MEM2(x...) printk(KERN_INFO x)
85 #else
86 #define DEBUG_MEM2(x...)
87 #endif
88
89 /* #define DEBUG_SG_CALC */
90 #ifdef DEBUG_SG_CALC
91 #define DEBUG_SC(x...) printk(KERN_INFO x)
92 #else
93 #define DEBUG_SC(x...)
94 #endif
95
96 /* #define DEBUG_SE_OBJ */
97 #ifdef DEBUG_SE_OBJ
98 #define DEBUG_SO(x...) printk(KERN_INFO x)
99 #else
100 #define DEBUG_SO(x...)
101 #endif
102
103 /* #define DEBUG_CMD_VOL */
104 #ifdef DEBUG_CMD_VOL
105 #define DEBUG_VOL(x...) printk(KERN_INFO x)
106 #else
107 #define DEBUG_VOL(x...)
108 #endif
109
110 /* #define DEBUG_CMD_STOP */
111 #ifdef DEBUG_CMD_STOP
112 #define DEBUG_CS(x...) printk(KERN_INFO x)
113 #else
114 #define DEBUG_CS(x...)
115 #endif
116
117 /* #define DEBUG_PASSTHROUGH */
118 #ifdef DEBUG_PASSTHROUGH
119 #define DEBUG_PT(x...) printk(KERN_INFO x)
120 #else
121 #define DEBUG_PT(x...)
122 #endif
123
124 /* #define DEBUG_TASK_STOP */
125 #ifdef DEBUG_TASK_STOP
126 #define DEBUG_TS(x...) printk(KERN_INFO x)
127 #else
128 #define DEBUG_TS(x...)
129 #endif
130
131 /* #define DEBUG_TRANSPORT_STOP */
132 #ifdef DEBUG_TRANSPORT_STOP
133 #define DEBUG_TRANSPORT_S(x...) printk(KERN_INFO x)
134 #else
135 #define DEBUG_TRANSPORT_S(x...)
136 #endif
137
138 /* #define DEBUG_TASK_FAILURE */
139 #ifdef DEBUG_TASK_FAILURE
140 #define DEBUG_TF(x...) printk(KERN_INFO x)
141 #else
142 #define DEBUG_TF(x...)
143 #endif
144
145 /* #define DEBUG_DEV_OFFLINE */
146 #ifdef DEBUG_DEV_OFFLINE
147 #define DEBUG_DO(x...) printk(KERN_INFO x)
148 #else
149 #define DEBUG_DO(x...)
150 #endif
151
152 /* #define DEBUG_TASK_STATE */
153 #ifdef DEBUG_TASK_STATE
154 #define DEBUG_TSTATE(x...) printk(KERN_INFO x)
155 #else
156 #define DEBUG_TSTATE(x...)
157 #endif
158
159 /* #define DEBUG_STATUS_THR */
160 #ifdef DEBUG_STATUS_THR
161 #define DEBUG_ST(x...) printk(KERN_INFO x)
162 #else
163 #define DEBUG_ST(x...)
164 #endif
165
166 /* #define DEBUG_TASK_TIMEOUT */
167 #ifdef DEBUG_TASK_TIMEOUT
168 #define DEBUG_TT(x...) printk(KERN_INFO x)
169 #else
170 #define DEBUG_TT(x...)
171 #endif
172
173 /* #define DEBUG_GENERIC_REQUEST_FAILURE */
174 #ifdef DEBUG_GENERIC_REQUEST_FAILURE
175 #define DEBUG_GRF(x...) printk(KERN_INFO x)
176 #else
177 #define DEBUG_GRF(x...)
178 #endif
179
180 /* #define DEBUG_SAM_TASK_ATTRS */
181 #ifdef DEBUG_SAM_TASK_ATTRS
182 #define DEBUG_STA(x...) printk(KERN_INFO x)
183 #else
184 #define DEBUG_STA(x...)
185 #endif
186
187 static int sub_api_initialized;
188
189 static struct kmem_cache *se_cmd_cache;
190 static struct kmem_cache *se_sess_cache;
191 struct kmem_cache *se_tmr_req_cache;
192 struct kmem_cache *se_ua_cache;
193 struct kmem_cache *se_mem_cache;
194 struct kmem_cache *t10_pr_reg_cache;
195 struct kmem_cache *t10_alua_lu_gp_cache;
196 struct kmem_cache *t10_alua_lu_gp_mem_cache;
197 struct kmem_cache *t10_alua_tg_pt_gp_cache;
198 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
199
200 /* Used for transport_dev_get_map_*() */
201 typedef int (*map_func_t)(struct se_task *, u32);
202
203 static int transport_generic_write_pending(struct se_cmd *);
204 static int transport_processing_thread(void *param);
205 static int __transport_execute_tasks(struct se_device *dev);
206 static void transport_complete_task_attr(struct se_cmd *cmd);
207 static int transport_complete_qf(struct se_cmd *cmd);
208 static void transport_handle_queue_full(struct se_cmd *cmd,
209 struct se_device *dev, int (*qf_callback)(struct se_cmd *));
210 static void transport_direct_request_timeout(struct se_cmd *cmd);
211 static void transport_free_dev_tasks(struct se_cmd *cmd);
212 static u32 transport_allocate_tasks(struct se_cmd *cmd,
213 unsigned long long starting_lba, u32 sectors,
214 enum dma_data_direction data_direction,
215 struct list_head *mem_list, int set_counts);
216 static int transport_generic_get_mem(struct se_cmd *cmd);
217 static int transport_generic_remove(struct se_cmd *cmd,
218 int session_reinstatement);
219 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd);
220 static int transport_map_sg_to_mem(struct se_cmd *cmd,
221 struct list_head *se_mem_list, struct scatterlist *sgl);
222 static void transport_memcpy_se_mem_read_contig(unsigned char *dst,
223 struct list_head *se_mem_list, u32 len);
224 static void transport_release_fe_cmd(struct se_cmd *cmd);
225 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
226 struct se_queue_obj *qobj);
227 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
228 static void transport_stop_all_task_timers(struct se_cmd *cmd);
229
230 int init_se_kmem_caches(void)
231 {
232 se_cmd_cache = kmem_cache_create("se_cmd_cache",
233 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
234 if (!(se_cmd_cache)) {
235 printk(KERN_ERR "kmem_cache_create for struct se_cmd failed\n");
236 goto out;
237 }
238 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
239 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
240 0, NULL);
241 if (!(se_tmr_req_cache)) {
242 printk(KERN_ERR "kmem_cache_create() for struct se_tmr_req"
243 " failed\n");
244 goto out;
245 }
246 se_sess_cache = kmem_cache_create("se_sess_cache",
247 sizeof(struct se_session), __alignof__(struct se_session),
248 0, NULL);
249 if (!(se_sess_cache)) {
250 printk(KERN_ERR "kmem_cache_create() for struct se_session"
251 " failed\n");
252 goto out;
253 }
254 se_ua_cache = kmem_cache_create("se_ua_cache",
255 sizeof(struct se_ua), __alignof__(struct se_ua),
256 0, NULL);
257 if (!(se_ua_cache)) {
258 printk(KERN_ERR "kmem_cache_create() for struct se_ua failed\n");
259 goto out;
260 }
261 se_mem_cache = kmem_cache_create("se_mem_cache",
262 sizeof(struct se_mem), __alignof__(struct se_mem), 0, NULL);
263 if (!(se_mem_cache)) {
264 printk(KERN_ERR "kmem_cache_create() for struct se_mem failed\n");
265 goto out;
266 }
267 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
268 sizeof(struct t10_pr_registration),
269 __alignof__(struct t10_pr_registration), 0, NULL);
270 if (!(t10_pr_reg_cache)) {
271 printk(KERN_ERR "kmem_cache_create() for struct t10_pr_registration"
272 " failed\n");
273 goto out;
274 }
275 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
276 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
277 0, NULL);
278 if (!(t10_alua_lu_gp_cache)) {
279 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_cache"
280 " failed\n");
281 goto out;
282 }
283 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
284 sizeof(struct t10_alua_lu_gp_member),
285 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
286 if (!(t10_alua_lu_gp_mem_cache)) {
287 printk(KERN_ERR "kmem_cache_create() for t10_alua_lu_gp_mem_"
288 "cache failed\n");
289 goto out;
290 }
291 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
292 sizeof(struct t10_alua_tg_pt_gp),
293 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
294 if (!(t10_alua_tg_pt_gp_cache)) {
295 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
296 "cache failed\n");
297 goto out;
298 }
299 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
300 "t10_alua_tg_pt_gp_mem_cache",
301 sizeof(struct t10_alua_tg_pt_gp_member),
302 __alignof__(struct t10_alua_tg_pt_gp_member),
303 0, NULL);
304 if (!(t10_alua_tg_pt_gp_mem_cache)) {
305 printk(KERN_ERR "kmem_cache_create() for t10_alua_tg_pt_gp_"
306 "mem_t failed\n");
307 goto out;
308 }
309
310 return 0;
311 out:
312 if (se_cmd_cache)
313 kmem_cache_destroy(se_cmd_cache);
314 if (se_tmr_req_cache)
315 kmem_cache_destroy(se_tmr_req_cache);
316 if (se_sess_cache)
317 kmem_cache_destroy(se_sess_cache);
318 if (se_ua_cache)
319 kmem_cache_destroy(se_ua_cache);
320 if (se_mem_cache)
321 kmem_cache_destroy(se_mem_cache);
322 if (t10_pr_reg_cache)
323 kmem_cache_destroy(t10_pr_reg_cache);
324 if (t10_alua_lu_gp_cache)
325 kmem_cache_destroy(t10_alua_lu_gp_cache);
326 if (t10_alua_lu_gp_mem_cache)
327 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
328 if (t10_alua_tg_pt_gp_cache)
329 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
330 if (t10_alua_tg_pt_gp_mem_cache)
331 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
332 return -ENOMEM;
333 }
334
335 void release_se_kmem_caches(void)
336 {
337 kmem_cache_destroy(se_cmd_cache);
338 kmem_cache_destroy(se_tmr_req_cache);
339 kmem_cache_destroy(se_sess_cache);
340 kmem_cache_destroy(se_ua_cache);
341 kmem_cache_destroy(se_mem_cache);
342 kmem_cache_destroy(t10_pr_reg_cache);
343 kmem_cache_destroy(t10_alua_lu_gp_cache);
344 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
345 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
346 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
347 }
348
349 /* This code ensures unique mib indexes are handed out. */
350 static DEFINE_SPINLOCK(scsi_mib_index_lock);
351 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
352
353 /*
354 * Allocate a new row index for the entry type specified
355 */
356 u32 scsi_get_new_index(scsi_index_t type)
357 {
358 u32 new_index;
359
360 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
361
362 spin_lock(&scsi_mib_index_lock);
363 new_index = ++scsi_mib_index[type];
364 spin_unlock(&scsi_mib_index_lock);
365
366 return new_index;
367 }
368
369 void transport_init_queue_obj(struct se_queue_obj *qobj)
370 {
371 atomic_set(&qobj->queue_cnt, 0);
372 INIT_LIST_HEAD(&qobj->qobj_list);
373 init_waitqueue_head(&qobj->thread_wq);
374 spin_lock_init(&qobj->cmd_queue_lock);
375 }
376 EXPORT_SYMBOL(transport_init_queue_obj);
377
378 static int transport_subsystem_reqmods(void)
379 {
380 int ret;
381
382 ret = request_module("target_core_iblock");
383 if (ret != 0)
384 printk(KERN_ERR "Unable to load target_core_iblock\n");
385
386 ret = request_module("target_core_file");
387 if (ret != 0)
388 printk(KERN_ERR "Unable to load target_core_file\n");
389
390 ret = request_module("target_core_pscsi");
391 if (ret != 0)
392 printk(KERN_ERR "Unable to load target_core_pscsi\n");
393
394 ret = request_module("target_core_stgt");
395 if (ret != 0)
396 printk(KERN_ERR "Unable to load target_core_stgt\n");
397
398 return 0;
399 }
400
401 int transport_subsystem_check_init(void)
402 {
403 int ret;
404
405 if (sub_api_initialized)
406 return 0;
407 /*
408 * Request the loading of known TCM subsystem plugins..
409 */
410 ret = transport_subsystem_reqmods();
411 if (ret < 0)
412 return ret;
413
414 sub_api_initialized = 1;
415 return 0;
416 }
417
418 struct se_session *transport_init_session(void)
419 {
420 struct se_session *se_sess;
421
422 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
423 if (!(se_sess)) {
424 printk(KERN_ERR "Unable to allocate struct se_session from"
425 " se_sess_cache\n");
426 return ERR_PTR(-ENOMEM);
427 }
428 INIT_LIST_HEAD(&se_sess->sess_list);
429 INIT_LIST_HEAD(&se_sess->sess_acl_list);
430
431 return se_sess;
432 }
433 EXPORT_SYMBOL(transport_init_session);
434
435 /*
436 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
437 */
438 void __transport_register_session(
439 struct se_portal_group *se_tpg,
440 struct se_node_acl *se_nacl,
441 struct se_session *se_sess,
442 void *fabric_sess_ptr)
443 {
444 unsigned char buf[PR_REG_ISID_LEN];
445
446 se_sess->se_tpg = se_tpg;
447 se_sess->fabric_sess_ptr = fabric_sess_ptr;
448 /*
449 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
450 *
451 * Only set for struct se_session's that will actually be moving I/O.
452 * eg: *NOT* discovery sessions.
453 */
454 if (se_nacl) {
455 /*
456 * If the fabric module supports an ISID based TransportID,
457 * save this value in binary from the fabric I_T Nexus now.
458 */
459 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
460 memset(&buf[0], 0, PR_REG_ISID_LEN);
461 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
462 &buf[0], PR_REG_ISID_LEN);
463 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
464 }
465 spin_lock_irq(&se_nacl->nacl_sess_lock);
466 /*
467 * The se_nacl->nacl_sess pointer will be set to the
468 * last active I_T Nexus for each struct se_node_acl.
469 */
470 se_nacl->nacl_sess = se_sess;
471
472 list_add_tail(&se_sess->sess_acl_list,
473 &se_nacl->acl_sess_list);
474 spin_unlock_irq(&se_nacl->nacl_sess_lock);
475 }
476 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
477
478 printk(KERN_INFO "TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
479 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
480 }
481 EXPORT_SYMBOL(__transport_register_session);
482
483 void transport_register_session(
484 struct se_portal_group *se_tpg,
485 struct se_node_acl *se_nacl,
486 struct se_session *se_sess,
487 void *fabric_sess_ptr)
488 {
489 spin_lock_bh(&se_tpg->session_lock);
490 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
491 spin_unlock_bh(&se_tpg->session_lock);
492 }
493 EXPORT_SYMBOL(transport_register_session);
494
495 void transport_deregister_session_configfs(struct se_session *se_sess)
496 {
497 struct se_node_acl *se_nacl;
498 unsigned long flags;
499 /*
500 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
501 */
502 se_nacl = se_sess->se_node_acl;
503 if ((se_nacl)) {
504 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
505 list_del(&se_sess->sess_acl_list);
506 /*
507 * If the session list is empty, then clear the pointer.
508 * Otherwise, set the struct se_session pointer from the tail
509 * element of the per struct se_node_acl active session list.
510 */
511 if (list_empty(&se_nacl->acl_sess_list))
512 se_nacl->nacl_sess = NULL;
513 else {
514 se_nacl->nacl_sess = container_of(
515 se_nacl->acl_sess_list.prev,
516 struct se_session, sess_acl_list);
517 }
518 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
519 }
520 }
521 EXPORT_SYMBOL(transport_deregister_session_configfs);
522
523 void transport_free_session(struct se_session *se_sess)
524 {
525 kmem_cache_free(se_sess_cache, se_sess);
526 }
527 EXPORT_SYMBOL(transport_free_session);
528
529 void transport_deregister_session(struct se_session *se_sess)
530 {
531 struct se_portal_group *se_tpg = se_sess->se_tpg;
532 struct se_node_acl *se_nacl;
533
534 if (!(se_tpg)) {
535 transport_free_session(se_sess);
536 return;
537 }
538
539 spin_lock_bh(&se_tpg->session_lock);
540 list_del(&se_sess->sess_list);
541 se_sess->se_tpg = NULL;
542 se_sess->fabric_sess_ptr = NULL;
543 spin_unlock_bh(&se_tpg->session_lock);
544
545 /*
546 * Determine if we need to do extra work for this initiator node's
547 * struct se_node_acl if it had been previously dynamically generated.
548 */
549 se_nacl = se_sess->se_node_acl;
550 if ((se_nacl)) {
551 spin_lock_bh(&se_tpg->acl_node_lock);
552 if (se_nacl->dynamic_node_acl) {
553 if (!(se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
554 se_tpg))) {
555 list_del(&se_nacl->acl_list);
556 se_tpg->num_node_acls--;
557 spin_unlock_bh(&se_tpg->acl_node_lock);
558
559 core_tpg_wait_for_nacl_pr_ref(se_nacl);
560 core_free_device_list_for_node(se_nacl, se_tpg);
561 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
562 se_nacl);
563 spin_lock_bh(&se_tpg->acl_node_lock);
564 }
565 }
566 spin_unlock_bh(&se_tpg->acl_node_lock);
567 }
568
569 transport_free_session(se_sess);
570
571 printk(KERN_INFO "TARGET_CORE[%s]: Deregistered fabric_sess\n",
572 se_tpg->se_tpg_tfo->get_fabric_name());
573 }
574 EXPORT_SYMBOL(transport_deregister_session);
575
576 /*
577 * Called with cmd->t_state_lock held.
578 */
579 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
580 {
581 struct se_device *dev;
582 struct se_task *task;
583 unsigned long flags;
584
585 list_for_each_entry(task, &cmd->t_task_list, t_list) {
586 dev = task->se_dev;
587 if (!(dev))
588 continue;
589
590 if (atomic_read(&task->task_active))
591 continue;
592
593 if (!(atomic_read(&task->task_state_active)))
594 continue;
595
596 spin_lock_irqsave(&dev->execute_task_lock, flags);
597 list_del(&task->t_state_list);
598 DEBUG_TSTATE("Removed ITT: 0x%08x dev: %p task[%p]\n",
599 cmd->se_tfo->tfo_get_task_tag(cmd), dev, task);
600 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
601
602 atomic_set(&task->task_state_active, 0);
603 atomic_dec(&cmd->t_task_cdbs_ex_left);
604 }
605 }
606
607 /* transport_cmd_check_stop():
608 *
609 * 'transport_off = 1' determines if t_transport_active should be cleared.
610 * 'transport_off = 2' determines if task_dev_state should be removed.
611 *
612 * A non-zero u8 t_state sets cmd->t_state.
613 * Returns 1 when command is stopped, else 0.
614 */
615 static int transport_cmd_check_stop(
616 struct se_cmd *cmd,
617 int transport_off,
618 u8 t_state)
619 {
620 unsigned long flags;
621
622 spin_lock_irqsave(&cmd->t_state_lock, flags);
623 /*
624 * Determine if IOCTL context caller in requesting the stopping of this
625 * command for LUN shutdown purposes.
626 */
627 if (atomic_read(&cmd->transport_lun_stop)) {
628 DEBUG_CS("%s:%d atomic_read(&cmd->transport_lun_stop)"
629 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
630 cmd->se_tfo->get_task_tag(cmd));
631
632 cmd->deferred_t_state = cmd->t_state;
633 cmd->t_state = TRANSPORT_DEFERRED_CMD;
634 atomic_set(&cmd->t_transport_active, 0);
635 if (transport_off == 2)
636 transport_all_task_dev_remove_state(cmd);
637 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
638
639 complete(&cmd->transport_lun_stop_comp);
640 return 1;
641 }
642 /*
643 * Determine if frontend context caller is requesting the stopping of
644 * this command for frontend exceptions.
645 */
646 if (atomic_read(&cmd->t_transport_stop)) {
647 DEBUG_CS("%s:%d atomic_read(&cmd->t_transport_stop) =="
648 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
649 cmd->se_tfo->get_task_tag(cmd));
650
651 cmd->deferred_t_state = cmd->t_state;
652 cmd->t_state = TRANSPORT_DEFERRED_CMD;
653 if (transport_off == 2)
654 transport_all_task_dev_remove_state(cmd);
655
656 /*
657 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
658 * to FE.
659 */
660 if (transport_off == 2)
661 cmd->se_lun = NULL;
662 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
663
664 complete(&cmd->t_transport_stop_comp);
665 return 1;
666 }
667 if (transport_off) {
668 atomic_set(&cmd->t_transport_active, 0);
669 if (transport_off == 2) {
670 transport_all_task_dev_remove_state(cmd);
671 /*
672 * Clear struct se_cmd->se_lun before the transport_off == 2
673 * handoff to fabric module.
674 */
675 cmd->se_lun = NULL;
676 /*
677 * Some fabric modules like tcm_loop can release
678 * their internally allocated I/O reference now and
679 * struct se_cmd now.
680 */
681 if (cmd->se_tfo->check_stop_free != NULL) {
682 spin_unlock_irqrestore(
683 &cmd->t_state_lock, flags);
684
685 cmd->se_tfo->check_stop_free(cmd);
686 return 1;
687 }
688 }
689 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
690
691 return 0;
692 } else if (t_state)
693 cmd->t_state = t_state;
694 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
695
696 return 0;
697 }
698
699 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
700 {
701 return transport_cmd_check_stop(cmd, 2, 0);
702 }
703
704 static void transport_lun_remove_cmd(struct se_cmd *cmd)
705 {
706 struct se_lun *lun = cmd->se_lun;
707 unsigned long flags;
708
709 if (!lun)
710 return;
711
712 spin_lock_irqsave(&cmd->t_state_lock, flags);
713 if (!(atomic_read(&cmd->transport_dev_active))) {
714 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
715 goto check_lun;
716 }
717 atomic_set(&cmd->transport_dev_active, 0);
718 transport_all_task_dev_remove_state(cmd);
719 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
720
721
722 check_lun:
723 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
724 if (atomic_read(&cmd->transport_lun_active)) {
725 list_del(&cmd->se_lun_node);
726 atomic_set(&cmd->transport_lun_active, 0);
727 #if 0
728 printk(KERN_INFO "Removed ITT: 0x%08x from LUN LIST[%d]\n"
729 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
730 #endif
731 }
732 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
733 }
734
735 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
736 {
737 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
738 transport_lun_remove_cmd(cmd);
739
740 if (transport_cmd_check_stop_to_fabric(cmd))
741 return;
742 if (remove)
743 transport_generic_remove(cmd, 0);
744 }
745
746 void transport_cmd_finish_abort_tmr(struct se_cmd *cmd)
747 {
748 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
749
750 if (transport_cmd_check_stop_to_fabric(cmd))
751 return;
752
753 transport_generic_remove(cmd, 0);
754 }
755
756 static void transport_add_cmd_to_queue(
757 struct se_cmd *cmd,
758 int t_state)
759 {
760 struct se_device *dev = cmd->se_dev;
761 struct se_queue_obj *qobj = &dev->dev_queue_obj;
762 unsigned long flags;
763
764 INIT_LIST_HEAD(&cmd->se_queue_node);
765
766 if (t_state) {
767 spin_lock_irqsave(&cmd->t_state_lock, flags);
768 cmd->t_state = t_state;
769 atomic_set(&cmd->t_transport_active, 1);
770 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
771 }
772
773 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
774 if (cmd->se_cmd_flags & SCF_EMULATE_QUEUE_FULL) {
775 cmd->se_cmd_flags &= ~SCF_EMULATE_QUEUE_FULL;
776 list_add(&cmd->se_queue_node, &qobj->qobj_list);
777 } else
778 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
779 atomic_inc(&cmd->t_transport_queue_active);
780 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
781
782 atomic_inc(&qobj->queue_cnt);
783 wake_up_interruptible(&qobj->thread_wq);
784 }
785
786 static struct se_cmd *
787 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
788 {
789 struct se_cmd *cmd;
790 unsigned long flags;
791
792 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
793 if (list_empty(&qobj->qobj_list)) {
794 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
795 return NULL;
796 }
797 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
798
799 atomic_dec(&cmd->t_transport_queue_active);
800
801 list_del(&cmd->se_queue_node);
802 atomic_dec(&qobj->queue_cnt);
803 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
804
805 return cmd;
806 }
807
808 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
809 struct se_queue_obj *qobj)
810 {
811 struct se_cmd *t;
812 unsigned long flags;
813
814 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
815 if (!(atomic_read(&cmd->t_transport_queue_active))) {
816 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
817 return;
818 }
819
820 list_for_each_entry(t, &qobj->qobj_list, se_queue_node)
821 if (t == cmd) {
822 atomic_dec(&cmd->t_transport_queue_active);
823 atomic_dec(&qobj->queue_cnt);
824 list_del(&cmd->se_queue_node);
825 break;
826 }
827 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
828
829 if (atomic_read(&cmd->t_transport_queue_active)) {
830 printk(KERN_ERR "ITT: 0x%08x t_transport_queue_active: %d\n",
831 cmd->se_tfo->get_task_tag(cmd),
832 atomic_read(&cmd->t_transport_queue_active));
833 }
834 }
835
836 /*
837 * Completion function used by TCM subsystem plugins (such as FILEIO)
838 * for queueing up response from struct se_subsystem_api->do_task()
839 */
840 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
841 {
842 struct se_task *task = list_entry(cmd->t_task_list.next,
843 struct se_task, t_list);
844
845 if (good) {
846 cmd->scsi_status = SAM_STAT_GOOD;
847 task->task_scsi_status = GOOD;
848 } else {
849 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
850 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
851 task->task_se_cmd->transport_error_status =
852 PYX_TRANSPORT_ILLEGAL_REQUEST;
853 }
854
855 transport_complete_task(task, good);
856 }
857 EXPORT_SYMBOL(transport_complete_sync_cache);
858
859 /* transport_complete_task():
860 *
861 * Called from interrupt and non interrupt context depending
862 * on the transport plugin.
863 */
864 void transport_complete_task(struct se_task *task, int success)
865 {
866 struct se_cmd *cmd = task->task_se_cmd;
867 struct se_device *dev = task->se_dev;
868 int t_state;
869 unsigned long flags;
870 #if 0
871 printk(KERN_INFO "task: %p CDB: 0x%02x obj_ptr: %p\n", task,
872 cmd->t_task_cdb[0], dev);
873 #endif
874 if (dev)
875 atomic_inc(&dev->depth_left);
876
877 spin_lock_irqsave(&cmd->t_state_lock, flags);
878 atomic_set(&task->task_active, 0);
879
880 /*
881 * See if any sense data exists, if so set the TASK_SENSE flag.
882 * Also check for any other post completion work that needs to be
883 * done by the plugins.
884 */
885 if (dev && dev->transport->transport_complete) {
886 if (dev->transport->transport_complete(task) != 0) {
887 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
888 task->task_sense = 1;
889 success = 1;
890 }
891 }
892
893 /*
894 * See if we are waiting for outstanding struct se_task
895 * to complete for an exception condition
896 */
897 if (atomic_read(&task->task_stop)) {
898 /*
899 * Decrement cmd->t_se_count if this task had
900 * previously thrown its timeout exception handler.
901 */
902 if (atomic_read(&task->task_timeout)) {
903 atomic_dec(&cmd->t_se_count);
904 atomic_set(&task->task_timeout, 0);
905 }
906 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
907
908 complete(&task->task_stop_comp);
909 return;
910 }
911 /*
912 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
913 * left counter to determine when the struct se_cmd is ready to be queued to
914 * the processing thread.
915 */
916 if (atomic_read(&task->task_timeout)) {
917 if (!(atomic_dec_and_test(
918 &cmd->t_task_cdbs_timeout_left))) {
919 spin_unlock_irqrestore(&cmd->t_state_lock,
920 flags);
921 return;
922 }
923 t_state = TRANSPORT_COMPLETE_TIMEOUT;
924 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
925
926 transport_add_cmd_to_queue(cmd, t_state);
927 return;
928 }
929 atomic_dec(&cmd->t_task_cdbs_timeout_left);
930
931 /*
932 * Decrement the outstanding t_task_cdbs_left count. The last
933 * struct se_task from struct se_cmd will complete itself into the
934 * device queue depending upon int success.
935 */
936 if (!(atomic_dec_and_test(&cmd->t_task_cdbs_left))) {
937 if (!success)
938 cmd->t_tasks_failed = 1;
939
940 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
941 return;
942 }
943
944 if (!success || cmd->t_tasks_failed) {
945 t_state = TRANSPORT_COMPLETE_FAILURE;
946 if (!task->task_error_status) {
947 task->task_error_status =
948 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
949 cmd->transport_error_status =
950 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
951 }
952 } else {
953 atomic_set(&cmd->t_transport_complete, 1);
954 t_state = TRANSPORT_COMPLETE_OK;
955 }
956 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
957
958 transport_add_cmd_to_queue(cmd, t_state);
959 }
960 EXPORT_SYMBOL(transport_complete_task);
961
962 /*
963 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
964 * struct se_task list are ready to be added to the active execution list
965 * struct se_device
966
967 * Called with se_dev_t->execute_task_lock called.
968 */
969 static inline int transport_add_task_check_sam_attr(
970 struct se_task *task,
971 struct se_task *task_prev,
972 struct se_device *dev)
973 {
974 /*
975 * No SAM Task attribute emulation enabled, add to tail of
976 * execution queue
977 */
978 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
979 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
980 return 0;
981 }
982 /*
983 * HEAD_OF_QUEUE attribute for received CDB, which means
984 * the first task that is associated with a struct se_cmd goes to
985 * head of the struct se_device->execute_task_list, and task_prev
986 * after that for each subsequent task
987 */
988 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
989 list_add(&task->t_execute_list,
990 (task_prev != NULL) ?
991 &task_prev->t_execute_list :
992 &dev->execute_task_list);
993
994 DEBUG_STA("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
995 " in execution queue\n",
996 T_TASK(task->task_se_cmd)->t_task_cdb[0]);
997 return 1;
998 }
999 /*
1000 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
1001 * transitioned from Dermant -> Active state, and are added to the end
1002 * of the struct se_device->execute_task_list
1003 */
1004 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
1005 return 0;
1006 }
1007
1008 /* __transport_add_task_to_execute_queue():
1009 *
1010 * Called with se_dev_t->execute_task_lock called.
1011 */
1012 static void __transport_add_task_to_execute_queue(
1013 struct se_task *task,
1014 struct se_task *task_prev,
1015 struct se_device *dev)
1016 {
1017 int head_of_queue;
1018
1019 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
1020 atomic_inc(&dev->execute_tasks);
1021
1022 if (atomic_read(&task->task_state_active))
1023 return;
1024 /*
1025 * Determine if this task needs to go to HEAD_OF_QUEUE for the
1026 * state list as well. Running with SAM Task Attribute emulation
1027 * will always return head_of_queue == 0 here
1028 */
1029 if (head_of_queue)
1030 list_add(&task->t_state_list, (task_prev) ?
1031 &task_prev->t_state_list :
1032 &dev->state_task_list);
1033 else
1034 list_add_tail(&task->t_state_list, &dev->state_task_list);
1035
1036 atomic_set(&task->task_state_active, 1);
1037
1038 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1039 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
1040 task, dev);
1041 }
1042
1043 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
1044 {
1045 struct se_device *dev;
1046 struct se_task *task;
1047 unsigned long flags;
1048
1049 spin_lock_irqsave(&cmd->t_state_lock, flags);
1050 list_for_each_entry(task, &cmd->t_task_list, t_list) {
1051 dev = task->se_dev;
1052
1053 if (atomic_read(&task->task_state_active))
1054 continue;
1055
1056 spin_lock(&dev->execute_task_lock);
1057 list_add_tail(&task->t_state_list, &dev->state_task_list);
1058 atomic_set(&task->task_state_active, 1);
1059
1060 DEBUG_TSTATE("Added ITT: 0x%08x task[%p] to dev: %p\n",
1061 task->se_cmd->se_tfo->get_task_tag(
1062 task->task_se_cmd), task, dev);
1063
1064 spin_unlock(&dev->execute_task_lock);
1065 }
1066 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1067 }
1068
1069 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
1070 {
1071 struct se_device *dev = cmd->se_dev;
1072 struct se_task *task, *task_prev = NULL;
1073 unsigned long flags;
1074
1075 spin_lock_irqsave(&dev->execute_task_lock, flags);
1076 list_for_each_entry(task, &cmd->t_task_list, t_list) {
1077 if (atomic_read(&task->task_execute_queue))
1078 continue;
1079 /*
1080 * __transport_add_task_to_execute_queue() handles the
1081 * SAM Task Attribute emulation if enabled
1082 */
1083 __transport_add_task_to_execute_queue(task, task_prev, dev);
1084 atomic_set(&task->task_execute_queue, 1);
1085 task_prev = task;
1086 }
1087 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1088 }
1089
1090 /* transport_remove_task_from_execute_queue():
1091 *
1092 *
1093 */
1094 void transport_remove_task_from_execute_queue(
1095 struct se_task *task,
1096 struct se_device *dev)
1097 {
1098 unsigned long flags;
1099
1100 if (atomic_read(&task->task_execute_queue) == 0) {
1101 dump_stack();
1102 return;
1103 }
1104
1105 spin_lock_irqsave(&dev->execute_task_lock, flags);
1106 list_del(&task->t_execute_list);
1107 atomic_set(&task->task_execute_queue, 0);
1108 atomic_dec(&dev->execute_tasks);
1109 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
1110 }
1111
1112 /*
1113 * Handle QUEUE_FULL / -EAGAIN status
1114 */
1115
1116 static void target_qf_do_work(struct work_struct *work)
1117 {
1118 struct se_device *dev = container_of(work, struct se_device,
1119 qf_work_queue);
1120 struct se_cmd *cmd, *cmd_tmp;
1121
1122 spin_lock_irq(&dev->qf_cmd_lock);
1123 list_for_each_entry_safe(cmd, cmd_tmp, &dev->qf_cmd_list, se_qf_node) {
1124
1125 list_del(&cmd->se_qf_node);
1126 atomic_dec(&dev->dev_qf_count);
1127 smp_mb__after_atomic_dec();
1128 spin_unlock_irq(&dev->qf_cmd_lock);
1129
1130 printk(KERN_INFO "Processing %s cmd: %p QUEUE_FULL in work queue"
1131 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
1132 (cmd->t_state == TRANSPORT_COMPLETE_OK) ? "COMPLETE_OK" :
1133 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
1134 : "UNKNOWN");
1135 /*
1136 * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd
1137 * has been added to head of queue
1138 */
1139 transport_add_cmd_to_queue(cmd, cmd->t_state);
1140
1141 spin_lock_irq(&dev->qf_cmd_lock);
1142 }
1143 spin_unlock_irq(&dev->qf_cmd_lock);
1144 }
1145
1146 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
1147 {
1148 switch (cmd->data_direction) {
1149 case DMA_NONE:
1150 return "NONE";
1151 case DMA_FROM_DEVICE:
1152 return "READ";
1153 case DMA_TO_DEVICE:
1154 return "WRITE";
1155 case DMA_BIDIRECTIONAL:
1156 return "BIDI";
1157 default:
1158 break;
1159 }
1160
1161 return "UNKNOWN";
1162 }
1163
1164 void transport_dump_dev_state(
1165 struct se_device *dev,
1166 char *b,
1167 int *bl)
1168 {
1169 *bl += sprintf(b + *bl, "Status: ");
1170 switch (dev->dev_status) {
1171 case TRANSPORT_DEVICE_ACTIVATED:
1172 *bl += sprintf(b + *bl, "ACTIVATED");
1173 break;
1174 case TRANSPORT_DEVICE_DEACTIVATED:
1175 *bl += sprintf(b + *bl, "DEACTIVATED");
1176 break;
1177 case TRANSPORT_DEVICE_SHUTDOWN:
1178 *bl += sprintf(b + *bl, "SHUTDOWN");
1179 break;
1180 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
1181 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
1182 *bl += sprintf(b + *bl, "OFFLINE");
1183 break;
1184 default:
1185 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
1186 break;
1187 }
1188
1189 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
1190 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
1191 dev->queue_depth);
1192 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
1193 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
1194 *bl += sprintf(b + *bl, " ");
1195 }
1196
1197 /* transport_release_all_cmds():
1198 *
1199 *
1200 */
1201 static void transport_release_all_cmds(struct se_device *dev)
1202 {
1203 struct se_cmd *cmd, *tcmd;
1204 int bug_out = 0, t_state;
1205 unsigned long flags;
1206
1207 spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags);
1208 list_for_each_entry_safe(cmd, tcmd, &dev->dev_queue_obj.qobj_list,
1209 se_queue_node) {
1210 t_state = cmd->t_state;
1211 list_del(&cmd->se_queue_node);
1212 spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock,
1213 flags);
1214
1215 printk(KERN_ERR "Releasing ITT: 0x%08x, i_state: %u,"
1216 " t_state: %u directly\n",
1217 cmd->se_tfo->get_task_tag(cmd),
1218 cmd->se_tfo->get_cmd_state(cmd), t_state);
1219
1220 transport_release_fe_cmd(cmd);
1221 bug_out = 1;
1222
1223 spin_lock_irqsave(&dev->dev_queue_obj.cmd_queue_lock, flags);
1224 }
1225 spin_unlock_irqrestore(&dev->dev_queue_obj.cmd_queue_lock, flags);
1226 #if 0
1227 if (bug_out)
1228 BUG();
1229 #endif
1230 }
1231
1232 void transport_dump_vpd_proto_id(
1233 struct t10_vpd *vpd,
1234 unsigned char *p_buf,
1235 int p_buf_len)
1236 {
1237 unsigned char buf[VPD_TMP_BUF_SIZE];
1238 int len;
1239
1240 memset(buf, 0, VPD_TMP_BUF_SIZE);
1241 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1242
1243 switch (vpd->protocol_identifier) {
1244 case 0x00:
1245 sprintf(buf+len, "Fibre Channel\n");
1246 break;
1247 case 0x10:
1248 sprintf(buf+len, "Parallel SCSI\n");
1249 break;
1250 case 0x20:
1251 sprintf(buf+len, "SSA\n");
1252 break;
1253 case 0x30:
1254 sprintf(buf+len, "IEEE 1394\n");
1255 break;
1256 case 0x40:
1257 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1258 " Protocol\n");
1259 break;
1260 case 0x50:
1261 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1262 break;
1263 case 0x60:
1264 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1265 break;
1266 case 0x70:
1267 sprintf(buf+len, "Automation/Drive Interface Transport"
1268 " Protocol\n");
1269 break;
1270 case 0x80:
1271 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1272 break;
1273 default:
1274 sprintf(buf+len, "Unknown 0x%02x\n",
1275 vpd->protocol_identifier);
1276 break;
1277 }
1278
1279 if (p_buf)
1280 strncpy(p_buf, buf, p_buf_len);
1281 else
1282 printk(KERN_INFO "%s", buf);
1283 }
1284
1285 void
1286 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1287 {
1288 /*
1289 * Check if the Protocol Identifier Valid (PIV) bit is set..
1290 *
1291 * from spc3r23.pdf section 7.5.1
1292 */
1293 if (page_83[1] & 0x80) {
1294 vpd->protocol_identifier = (page_83[0] & 0xf0);
1295 vpd->protocol_identifier_set = 1;
1296 transport_dump_vpd_proto_id(vpd, NULL, 0);
1297 }
1298 }
1299 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1300
1301 int transport_dump_vpd_assoc(
1302 struct t10_vpd *vpd,
1303 unsigned char *p_buf,
1304 int p_buf_len)
1305 {
1306 unsigned char buf[VPD_TMP_BUF_SIZE];
1307 int ret = 0;
1308 int len;
1309
1310 memset(buf, 0, VPD_TMP_BUF_SIZE);
1311 len = sprintf(buf, "T10 VPD Identifier Association: ");
1312
1313 switch (vpd->association) {
1314 case 0x00:
1315 sprintf(buf+len, "addressed logical unit\n");
1316 break;
1317 case 0x10:
1318 sprintf(buf+len, "target port\n");
1319 break;
1320 case 0x20:
1321 sprintf(buf+len, "SCSI target device\n");
1322 break;
1323 default:
1324 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1325 ret = -EINVAL;
1326 break;
1327 }
1328
1329 if (p_buf)
1330 strncpy(p_buf, buf, p_buf_len);
1331 else
1332 printk("%s", buf);
1333
1334 return ret;
1335 }
1336
1337 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1338 {
1339 /*
1340 * The VPD identification association..
1341 *
1342 * from spc3r23.pdf Section 7.6.3.1 Table 297
1343 */
1344 vpd->association = (page_83[1] & 0x30);
1345 return transport_dump_vpd_assoc(vpd, NULL, 0);
1346 }
1347 EXPORT_SYMBOL(transport_set_vpd_assoc);
1348
1349 int transport_dump_vpd_ident_type(
1350 struct t10_vpd *vpd,
1351 unsigned char *p_buf,
1352 int p_buf_len)
1353 {
1354 unsigned char buf[VPD_TMP_BUF_SIZE];
1355 int ret = 0;
1356 int len;
1357
1358 memset(buf, 0, VPD_TMP_BUF_SIZE);
1359 len = sprintf(buf, "T10 VPD Identifier Type: ");
1360
1361 switch (vpd->device_identifier_type) {
1362 case 0x00:
1363 sprintf(buf+len, "Vendor specific\n");
1364 break;
1365 case 0x01:
1366 sprintf(buf+len, "T10 Vendor ID based\n");
1367 break;
1368 case 0x02:
1369 sprintf(buf+len, "EUI-64 based\n");
1370 break;
1371 case 0x03:
1372 sprintf(buf+len, "NAA\n");
1373 break;
1374 case 0x04:
1375 sprintf(buf+len, "Relative target port identifier\n");
1376 break;
1377 case 0x08:
1378 sprintf(buf+len, "SCSI name string\n");
1379 break;
1380 default:
1381 sprintf(buf+len, "Unsupported: 0x%02x\n",
1382 vpd->device_identifier_type);
1383 ret = -EINVAL;
1384 break;
1385 }
1386
1387 if (p_buf) {
1388 if (p_buf_len < strlen(buf)+1)
1389 return -EINVAL;
1390 strncpy(p_buf, buf, p_buf_len);
1391 } else {
1392 printk("%s", buf);
1393 }
1394
1395 return ret;
1396 }
1397
1398 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1399 {
1400 /*
1401 * The VPD identifier type..
1402 *
1403 * from spc3r23.pdf Section 7.6.3.1 Table 298
1404 */
1405 vpd->device_identifier_type = (page_83[1] & 0x0f);
1406 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1407 }
1408 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1409
1410 int transport_dump_vpd_ident(
1411 struct t10_vpd *vpd,
1412 unsigned char *p_buf,
1413 int p_buf_len)
1414 {
1415 unsigned char buf[VPD_TMP_BUF_SIZE];
1416 int ret = 0;
1417
1418 memset(buf, 0, VPD_TMP_BUF_SIZE);
1419
1420 switch (vpd->device_identifier_code_set) {
1421 case 0x01: /* Binary */
1422 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1423 &vpd->device_identifier[0]);
1424 break;
1425 case 0x02: /* ASCII */
1426 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1427 &vpd->device_identifier[0]);
1428 break;
1429 case 0x03: /* UTF-8 */
1430 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1431 &vpd->device_identifier[0]);
1432 break;
1433 default:
1434 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1435 " 0x%02x", vpd->device_identifier_code_set);
1436 ret = -EINVAL;
1437 break;
1438 }
1439
1440 if (p_buf)
1441 strncpy(p_buf, buf, p_buf_len);
1442 else
1443 printk("%s", buf);
1444
1445 return ret;
1446 }
1447
1448 int
1449 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1450 {
1451 static const char hex_str[] = "0123456789abcdef";
1452 int j = 0, i = 4; /* offset to start of the identifer */
1453
1454 /*
1455 * The VPD Code Set (encoding)
1456 *
1457 * from spc3r23.pdf Section 7.6.3.1 Table 296
1458 */
1459 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1460 switch (vpd->device_identifier_code_set) {
1461 case 0x01: /* Binary */
1462 vpd->device_identifier[j++] =
1463 hex_str[vpd->device_identifier_type];
1464 while (i < (4 + page_83[3])) {
1465 vpd->device_identifier[j++] =
1466 hex_str[(page_83[i] & 0xf0) >> 4];
1467 vpd->device_identifier[j++] =
1468 hex_str[page_83[i] & 0x0f];
1469 i++;
1470 }
1471 break;
1472 case 0x02: /* ASCII */
1473 case 0x03: /* UTF-8 */
1474 while (i < (4 + page_83[3]))
1475 vpd->device_identifier[j++] = page_83[i++];
1476 break;
1477 default:
1478 break;
1479 }
1480
1481 return transport_dump_vpd_ident(vpd, NULL, 0);
1482 }
1483 EXPORT_SYMBOL(transport_set_vpd_ident);
1484
1485 static void core_setup_task_attr_emulation(struct se_device *dev)
1486 {
1487 /*
1488 * If this device is from Target_Core_Mod/pSCSI, disable the
1489 * SAM Task Attribute emulation.
1490 *
1491 * This is currently not available in upsream Linux/SCSI Target
1492 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1493 */
1494 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1495 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1496 return;
1497 }
1498
1499 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1500 DEBUG_STA("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1501 " device\n", dev->transport->name,
1502 dev->transport->get_device_rev(dev));
1503 }
1504
1505 static void scsi_dump_inquiry(struct se_device *dev)
1506 {
1507 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1508 int i, device_type;
1509 /*
1510 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1511 */
1512 printk(" Vendor: ");
1513 for (i = 0; i < 8; i++)
1514 if (wwn->vendor[i] >= 0x20)
1515 printk("%c", wwn->vendor[i]);
1516 else
1517 printk(" ");
1518
1519 printk(" Model: ");
1520 for (i = 0; i < 16; i++)
1521 if (wwn->model[i] >= 0x20)
1522 printk("%c", wwn->model[i]);
1523 else
1524 printk(" ");
1525
1526 printk(" Revision: ");
1527 for (i = 0; i < 4; i++)
1528 if (wwn->revision[i] >= 0x20)
1529 printk("%c", wwn->revision[i]);
1530 else
1531 printk(" ");
1532
1533 printk("\n");
1534
1535 device_type = dev->transport->get_device_type(dev);
1536 printk(" Type: %s ", scsi_device_type(device_type));
1537 printk(" ANSI SCSI revision: %02x\n",
1538 dev->transport->get_device_rev(dev));
1539 }
1540
1541 struct se_device *transport_add_device_to_core_hba(
1542 struct se_hba *hba,
1543 struct se_subsystem_api *transport,
1544 struct se_subsystem_dev *se_dev,
1545 u32 device_flags,
1546 void *transport_dev,
1547 struct se_dev_limits *dev_limits,
1548 const char *inquiry_prod,
1549 const char *inquiry_rev)
1550 {
1551 int force_pt;
1552 struct se_device *dev;
1553
1554 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1555 if (!(dev)) {
1556 printk(KERN_ERR "Unable to allocate memory for se_dev_t\n");
1557 return NULL;
1558 }
1559
1560 transport_init_queue_obj(&dev->dev_queue_obj);
1561 dev->dev_flags = device_flags;
1562 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1563 dev->dev_ptr = transport_dev;
1564 dev->se_hba = hba;
1565 dev->se_sub_dev = se_dev;
1566 dev->transport = transport;
1567 atomic_set(&dev->active_cmds, 0);
1568 INIT_LIST_HEAD(&dev->dev_list);
1569 INIT_LIST_HEAD(&dev->dev_sep_list);
1570 INIT_LIST_HEAD(&dev->dev_tmr_list);
1571 INIT_LIST_HEAD(&dev->execute_task_list);
1572 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1573 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1574 INIT_LIST_HEAD(&dev->state_task_list);
1575 INIT_LIST_HEAD(&dev->qf_cmd_list);
1576 spin_lock_init(&dev->execute_task_lock);
1577 spin_lock_init(&dev->delayed_cmd_lock);
1578 spin_lock_init(&dev->ordered_cmd_lock);
1579 spin_lock_init(&dev->state_task_lock);
1580 spin_lock_init(&dev->dev_alua_lock);
1581 spin_lock_init(&dev->dev_reservation_lock);
1582 spin_lock_init(&dev->dev_status_lock);
1583 spin_lock_init(&dev->dev_status_thr_lock);
1584 spin_lock_init(&dev->se_port_lock);
1585 spin_lock_init(&dev->se_tmr_lock);
1586 spin_lock_init(&dev->qf_cmd_lock);
1587
1588 dev->queue_depth = dev_limits->queue_depth;
1589 atomic_set(&dev->depth_left, dev->queue_depth);
1590 atomic_set(&dev->dev_ordered_id, 0);
1591
1592 se_dev_set_default_attribs(dev, dev_limits);
1593
1594 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1595 dev->creation_time = get_jiffies_64();
1596 spin_lock_init(&dev->stats_lock);
1597
1598 spin_lock(&hba->device_lock);
1599 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1600 hba->dev_count++;
1601 spin_unlock(&hba->device_lock);
1602 /*
1603 * Setup the SAM Task Attribute emulation for struct se_device
1604 */
1605 core_setup_task_attr_emulation(dev);
1606 /*
1607 * Force PR and ALUA passthrough emulation with internal object use.
1608 */
1609 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1610 /*
1611 * Setup the Reservations infrastructure for struct se_device
1612 */
1613 core_setup_reservations(dev, force_pt);
1614 /*
1615 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1616 */
1617 if (core_setup_alua(dev, force_pt) < 0)
1618 goto out;
1619
1620 /*
1621 * Startup the struct se_device processing thread
1622 */
1623 dev->process_thread = kthread_run(transport_processing_thread, dev,
1624 "LIO_%s", dev->transport->name);
1625 if (IS_ERR(dev->process_thread)) {
1626 printk(KERN_ERR "Unable to create kthread: LIO_%s\n",
1627 dev->transport->name);
1628 goto out;
1629 }
1630 /*
1631 * Setup work_queue for QUEUE_FULL
1632 */
1633 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1634 /*
1635 * Preload the initial INQUIRY const values if we are doing
1636 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1637 * passthrough because this is being provided by the backend LLD.
1638 * This is required so that transport_get_inquiry() copies these
1639 * originals once back into DEV_T10_WWN(dev) for the virtual device
1640 * setup.
1641 */
1642 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1643 if (!inquiry_prod || !inquiry_rev) {
1644 printk(KERN_ERR "All non TCM/pSCSI plugins require"
1645 " INQUIRY consts\n");
1646 goto out;
1647 }
1648
1649 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1650 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1651 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1652 }
1653 scsi_dump_inquiry(dev);
1654
1655 return dev;
1656 out:
1657 kthread_stop(dev->process_thread);
1658
1659 spin_lock(&hba->device_lock);
1660 list_del(&dev->dev_list);
1661 hba->dev_count--;
1662 spin_unlock(&hba->device_lock);
1663
1664 se_release_vpd_for_dev(dev);
1665
1666 kfree(dev);
1667
1668 return NULL;
1669 }
1670 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1671
1672 /* transport_generic_prepare_cdb():
1673 *
1674 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1675 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1676 * The point of this is since we are mapping iSCSI LUNs to
1677 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1678 * devices and HBAs for a loop.
1679 */
1680 static inline void transport_generic_prepare_cdb(
1681 unsigned char *cdb)
1682 {
1683 switch (cdb[0]) {
1684 case READ_10: /* SBC - RDProtect */
1685 case READ_12: /* SBC - RDProtect */
1686 case READ_16: /* SBC - RDProtect */
1687 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1688 case VERIFY: /* SBC - VRProtect */
1689 case VERIFY_16: /* SBC - VRProtect */
1690 case WRITE_VERIFY: /* SBC - VRProtect */
1691 case WRITE_VERIFY_12: /* SBC - VRProtect */
1692 break;
1693 default:
1694 cdb[1] &= 0x1f; /* clear logical unit number */
1695 break;
1696 }
1697 }
1698
1699 static struct se_task *
1700 transport_generic_get_task(struct se_cmd *cmd,
1701 enum dma_data_direction data_direction)
1702 {
1703 struct se_task *task;
1704 struct se_device *dev = cmd->se_dev;
1705 unsigned long flags;
1706
1707 task = dev->transport->alloc_task(cmd);
1708 if (!task) {
1709 printk(KERN_ERR "Unable to allocate struct se_task\n");
1710 return NULL;
1711 }
1712
1713 INIT_LIST_HEAD(&task->t_list);
1714 INIT_LIST_HEAD(&task->t_execute_list);
1715 INIT_LIST_HEAD(&task->t_state_list);
1716 init_completion(&task->task_stop_comp);
1717 task->task_se_cmd = cmd;
1718 task->se_dev = dev;
1719 task->task_data_direction = data_direction;
1720
1721 spin_lock_irqsave(&cmd->t_state_lock, flags);
1722 list_add_tail(&task->t_list, &cmd->t_task_list);
1723 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1724
1725 return task;
1726 }
1727
1728 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1729
1730 /*
1731 * Used by fabric modules containing a local struct se_cmd within their
1732 * fabric dependent per I/O descriptor.
1733 */
1734 void transport_init_se_cmd(
1735 struct se_cmd *cmd,
1736 struct target_core_fabric_ops *tfo,
1737 struct se_session *se_sess,
1738 u32 data_length,
1739 int data_direction,
1740 int task_attr,
1741 unsigned char *sense_buffer)
1742 {
1743 INIT_LIST_HEAD(&cmd->se_lun_node);
1744 INIT_LIST_HEAD(&cmd->se_delayed_node);
1745 INIT_LIST_HEAD(&cmd->se_ordered_node);
1746 INIT_LIST_HEAD(&cmd->se_qf_node);
1747
1748 INIT_LIST_HEAD(&cmd->t_mem_list);
1749 INIT_LIST_HEAD(&cmd->t_mem_bidi_list);
1750 INIT_LIST_HEAD(&cmd->t_task_list);
1751 init_completion(&cmd->transport_lun_fe_stop_comp);
1752 init_completion(&cmd->transport_lun_stop_comp);
1753 init_completion(&cmd->t_transport_stop_comp);
1754 spin_lock_init(&cmd->t_state_lock);
1755 atomic_set(&cmd->transport_dev_active, 1);
1756
1757 cmd->se_tfo = tfo;
1758 cmd->se_sess = se_sess;
1759 cmd->data_length = data_length;
1760 cmd->data_direction = data_direction;
1761 cmd->sam_task_attr = task_attr;
1762 cmd->sense_buffer = sense_buffer;
1763 }
1764 EXPORT_SYMBOL(transport_init_se_cmd);
1765
1766 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1767 {
1768 /*
1769 * Check if SAM Task Attribute emulation is enabled for this
1770 * struct se_device storage object
1771 */
1772 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1773 return 0;
1774
1775 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1776 DEBUG_STA("SAM Task Attribute ACA"
1777 " emulation is not supported\n");
1778 return -EINVAL;
1779 }
1780 /*
1781 * Used to determine when ORDERED commands should go from
1782 * Dormant to Active status.
1783 */
1784 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1785 smp_mb__after_atomic_inc();
1786 DEBUG_STA("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1787 cmd->se_ordered_id, cmd->sam_task_attr,
1788 TRANSPORT(cmd->se_dev)->name);
1789 return 0;
1790 }
1791
1792 void transport_free_se_cmd(
1793 struct se_cmd *se_cmd)
1794 {
1795 if (se_cmd->se_tmr_req)
1796 core_tmr_release_req(se_cmd->se_tmr_req);
1797 /*
1798 * Check and free any extended CDB buffer that was allocated
1799 */
1800 if (se_cmd->t_task_cdb != se_cmd->__t_task_cdb)
1801 kfree(se_cmd->t_task_cdb);
1802 }
1803 EXPORT_SYMBOL(transport_free_se_cmd);
1804
1805 static void transport_generic_wait_for_tasks(struct se_cmd *, int, int);
1806
1807 /* transport_generic_allocate_tasks():
1808 *
1809 * Called from fabric RX Thread.
1810 */
1811 int transport_generic_allocate_tasks(
1812 struct se_cmd *cmd,
1813 unsigned char *cdb)
1814 {
1815 int ret;
1816
1817 transport_generic_prepare_cdb(cdb);
1818
1819 /*
1820 * This is needed for early exceptions.
1821 */
1822 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1823
1824 /*
1825 * Ensure that the received CDB is less than the max (252 + 8) bytes
1826 * for VARIABLE_LENGTH_CMD
1827 */
1828 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1829 printk(KERN_ERR "Received SCSI CDB with command_size: %d that"
1830 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1831 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1832 return -EINVAL;
1833 }
1834 /*
1835 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1836 * allocate the additional extended CDB buffer now.. Otherwise
1837 * setup the pointer from __t_task_cdb to t_task_cdb.
1838 */
1839 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1840 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1841 GFP_KERNEL);
1842 if (!(cmd->t_task_cdb)) {
1843 printk(KERN_ERR "Unable to allocate cmd->t_task_cdb"
1844 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1845 scsi_command_size(cdb),
1846 (unsigned long)sizeof(cmd->__t_task_cdb));
1847 return -ENOMEM;
1848 }
1849 } else
1850 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1851 /*
1852 * Copy the original CDB into cmd->
1853 */
1854 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1855 /*
1856 * Setup the received CDB based on SCSI defined opcodes and
1857 * perform unit attention, persistent reservations and ALUA
1858 * checks for virtual device backends. The cmd->t_task_cdb
1859 * pointer is expected to be setup before we reach this point.
1860 */
1861 ret = transport_generic_cmd_sequencer(cmd, cdb);
1862 if (ret < 0)
1863 return ret;
1864 /*
1865 * Check for SAM Task Attribute Emulation
1866 */
1867 if (transport_check_alloc_task_attr(cmd) < 0) {
1868 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1869 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1870 return -EINVAL;
1871 }
1872 spin_lock(&cmd->se_lun->lun_sep_lock);
1873 if (cmd->se_lun->lun_sep)
1874 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1875 spin_unlock(&cmd->se_lun->lun_sep_lock);
1876 return 0;
1877 }
1878 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1879
1880 /*
1881 * Used by fabric module frontends not defining a TFO->new_cmd_map()
1882 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD statis
1883 */
1884 int transport_generic_handle_cdb(
1885 struct se_cmd *cmd)
1886 {
1887 if (!cmd->se_lun) {
1888 dump_stack();
1889 printk(KERN_ERR "cmd->se_lun is NULL\n");
1890 return -EINVAL;
1891 }
1892
1893 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD);
1894 return 0;
1895 }
1896 EXPORT_SYMBOL(transport_generic_handle_cdb);
1897
1898 /*
1899 * Used by fabric module frontends to queue tasks directly.
1900 * Many only be used from process context only
1901 */
1902 int transport_handle_cdb_direct(
1903 struct se_cmd *cmd)
1904 {
1905 if (!cmd->se_lun) {
1906 dump_stack();
1907 printk(KERN_ERR "cmd->se_lun is NULL\n");
1908 return -EINVAL;
1909 }
1910 if (in_interrupt()) {
1911 dump_stack();
1912 printk(KERN_ERR "transport_generic_handle_cdb cannot be called"
1913 " from interrupt context\n");
1914 return -EINVAL;
1915 }
1916
1917 return transport_generic_new_cmd(cmd);
1918 }
1919 EXPORT_SYMBOL(transport_handle_cdb_direct);
1920
1921 /*
1922 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1923 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1924 * complete setup in TCM process context w/ TFO->new_cmd_map().
1925 */
1926 int transport_generic_handle_cdb_map(
1927 struct se_cmd *cmd)
1928 {
1929 if (!cmd->se_lun) {
1930 dump_stack();
1931 printk(KERN_ERR "cmd->se_lun is NULL\n");
1932 return -EINVAL;
1933 }
1934
1935 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
1936 return 0;
1937 }
1938 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1939
1940 /* transport_generic_handle_data():
1941 *
1942 *
1943 */
1944 int transport_generic_handle_data(
1945 struct se_cmd *cmd)
1946 {
1947 /*
1948 * For the software fabric case, then we assume the nexus is being
1949 * failed/shutdown when signals are pending from the kthread context
1950 * caller, so we return a failure. For the HW target mode case running
1951 * in interrupt code, the signal_pending() check is skipped.
1952 */
1953 if (!in_interrupt() && signal_pending(current))
1954 return -EPERM;
1955 /*
1956 * If the received CDB has aleady been ABORTED by the generic
1957 * target engine, we now call transport_check_aborted_status()
1958 * to queue any delated TASK_ABORTED status for the received CDB to the
1959 * fabric module as we are expecting no further incoming DATA OUT
1960 * sequences at this point.
1961 */
1962 if (transport_check_aborted_status(cmd, 1) != 0)
1963 return 0;
1964
1965 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
1966 return 0;
1967 }
1968 EXPORT_SYMBOL(transport_generic_handle_data);
1969
1970 /* transport_generic_handle_tmr():
1971 *
1972 *
1973 */
1974 int transport_generic_handle_tmr(
1975 struct se_cmd *cmd)
1976 {
1977 /*
1978 * This is needed for early exceptions.
1979 */
1980 cmd->transport_wait_for_tasks = &transport_generic_wait_for_tasks;
1981
1982 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
1983 return 0;
1984 }
1985 EXPORT_SYMBOL(transport_generic_handle_tmr);
1986
1987 void transport_generic_free_cmd_intr(
1988 struct se_cmd *cmd)
1989 {
1990 transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR);
1991 }
1992 EXPORT_SYMBOL(transport_generic_free_cmd_intr);
1993
1994 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1995 {
1996 struct se_task *task, *task_tmp;
1997 unsigned long flags;
1998 int ret = 0;
1999
2000 DEBUG_TS("ITT[0x%08x] - Stopping tasks\n",
2001 cmd->se_tfo->get_task_tag(cmd));
2002
2003 /*
2004 * No tasks remain in the execution queue
2005 */
2006 spin_lock_irqsave(&cmd->t_state_lock, flags);
2007 list_for_each_entry_safe(task, task_tmp,
2008 &cmd->t_task_list, t_list) {
2009 DEBUG_TS("task_no[%d] - Processing task %p\n",
2010 task->task_no, task);
2011 /*
2012 * If the struct se_task has not been sent and is not active,
2013 * remove the struct se_task from the execution queue.
2014 */
2015 if (!atomic_read(&task->task_sent) &&
2016 !atomic_read(&task->task_active)) {
2017 spin_unlock_irqrestore(&cmd->t_state_lock,
2018 flags);
2019 transport_remove_task_from_execute_queue(task,
2020 task->se_dev);
2021
2022 DEBUG_TS("task_no[%d] - Removed from execute queue\n",
2023 task->task_no);
2024 spin_lock_irqsave(&cmd->t_state_lock, flags);
2025 continue;
2026 }
2027
2028 /*
2029 * If the struct se_task is active, sleep until it is returned
2030 * from the plugin.
2031 */
2032 if (atomic_read(&task->task_active)) {
2033 atomic_set(&task->task_stop, 1);
2034 spin_unlock_irqrestore(&cmd->t_state_lock,
2035 flags);
2036
2037 DEBUG_TS("task_no[%d] - Waiting to complete\n",
2038 task->task_no);
2039 wait_for_completion(&task->task_stop_comp);
2040 DEBUG_TS("task_no[%d] - Stopped successfully\n",
2041 task->task_no);
2042
2043 spin_lock_irqsave(&cmd->t_state_lock, flags);
2044 atomic_dec(&cmd->t_task_cdbs_left);
2045
2046 atomic_set(&task->task_active, 0);
2047 atomic_set(&task->task_stop, 0);
2048 } else {
2049 DEBUG_TS("task_no[%d] - Did nothing\n", task->task_no);
2050 ret++;
2051 }
2052
2053 __transport_stop_task_timer(task, &flags);
2054 }
2055 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2056
2057 return ret;
2058 }
2059
2060 /*
2061 * Handle SAM-esque emulation for generic transport request failures.
2062 */
2063 static void transport_generic_request_failure(
2064 struct se_cmd *cmd,
2065 struct se_device *dev,
2066 int complete,
2067 int sc)
2068 {
2069 int ret = 0;
2070
2071 DEBUG_GRF("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
2072 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
2073 cmd->t_task_cdb[0]);
2074 DEBUG_GRF("-----[ i_state: %d t_state/def_t_state:"
2075 " %d/%d transport_error_status: %d\n",
2076 cmd->se_tfo->get_cmd_state(cmd),
2077 cmd->t_state, cmd->deferred_t_state,
2078 cmd->transport_error_status);
2079 DEBUG_GRF("-----[ t_task_cdbs: %d t_task_cdbs_left: %d"
2080 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
2081 " t_transport_active: %d t_transport_stop: %d"
2082 " t_transport_sent: %d\n", cmd->t_task_cdbs,
2083 atomic_read(&cmd->t_task_cdbs_left),
2084 atomic_read(&cmd->t_task_cdbs_sent),
2085 atomic_read(&cmd->t_task_cdbs_ex_left),
2086 atomic_read(&cmd->t_transport_active),
2087 atomic_read(&cmd->t_transport_stop),
2088 atomic_read(&cmd->t_transport_sent));
2089
2090 transport_stop_all_task_timers(cmd);
2091
2092 if (dev)
2093 atomic_inc(&dev->depth_left);
2094 /*
2095 * For SAM Task Attribute emulation for failed struct se_cmd
2096 */
2097 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2098 transport_complete_task_attr(cmd);
2099
2100 if (complete) {
2101 transport_direct_request_timeout(cmd);
2102 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2103 }
2104
2105 switch (cmd->transport_error_status) {
2106 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
2107 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2108 break;
2109 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
2110 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
2111 break;
2112 case PYX_TRANSPORT_INVALID_CDB_FIELD:
2113 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2114 break;
2115 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
2116 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
2117 break;
2118 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
2119 if (!sc)
2120 transport_new_cmd_failure(cmd);
2121 /*
2122 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
2123 * we force this session to fall back to session
2124 * recovery.
2125 */
2126 cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
2127 cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
2128
2129 goto check_stop;
2130 case PYX_TRANSPORT_LU_COMM_FAILURE:
2131 case PYX_TRANSPORT_ILLEGAL_REQUEST:
2132 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2133 break;
2134 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
2135 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
2136 break;
2137 case PYX_TRANSPORT_WRITE_PROTECTED:
2138 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
2139 break;
2140 case PYX_TRANSPORT_RESERVATION_CONFLICT:
2141 /*
2142 * No SENSE Data payload for this case, set SCSI Status
2143 * and queue the response to $FABRIC_MOD.
2144 *
2145 * Uses linux/include/scsi/scsi.h SAM status codes defs
2146 */
2147 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2148 /*
2149 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2150 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2151 * CONFLICT STATUS.
2152 *
2153 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2154 */
2155 if (cmd->se_sess &&
2156 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2157 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2158 cmd->orig_fe_lun, 0x2C,
2159 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2160
2161 ret = cmd->se_tfo->queue_status(cmd);
2162 if (ret == -EAGAIN)
2163 goto queue_full;
2164 goto check_stop;
2165 case PYX_TRANSPORT_USE_SENSE_REASON:
2166 /*
2167 * struct se_cmd->scsi_sense_reason already set
2168 */
2169 break;
2170 default:
2171 printk(KERN_ERR "Unknown transport error for CDB 0x%02x: %d\n",
2172 cmd->t_task_cdb[0],
2173 cmd->transport_error_status);
2174 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2175 break;
2176 }
2177
2178 if (!sc)
2179 transport_new_cmd_failure(cmd);
2180 else {
2181 ret = transport_send_check_condition_and_sense(cmd,
2182 cmd->scsi_sense_reason, 0);
2183 if (ret == -EAGAIN)
2184 goto queue_full;
2185 }
2186
2187 check_stop:
2188 transport_lun_remove_cmd(cmd);
2189 if (!(transport_cmd_check_stop_to_fabric(cmd)))
2190 ;
2191 return;
2192
2193 queue_full:
2194 cmd->t_state = TRANSPORT_COMPLETE_OK;
2195 transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf);
2196 }
2197
2198 static void transport_direct_request_timeout(struct se_cmd *cmd)
2199 {
2200 unsigned long flags;
2201
2202 spin_lock_irqsave(&cmd->t_state_lock, flags);
2203 if (!(atomic_read(&cmd->t_transport_timeout))) {
2204 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2205 return;
2206 }
2207 if (atomic_read(&cmd->t_task_cdbs_timeout_left)) {
2208 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2209 return;
2210 }
2211
2212 atomic_sub(atomic_read(&cmd->t_transport_timeout),
2213 &cmd->t_se_count);
2214 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2215 }
2216
2217 static void transport_generic_request_timeout(struct se_cmd *cmd)
2218 {
2219 unsigned long flags;
2220
2221 /*
2222 * Reset cmd->t_se_count to allow transport_generic_remove()
2223 * to allow last call to free memory resources.
2224 */
2225 spin_lock_irqsave(&cmd->t_state_lock, flags);
2226 if (atomic_read(&cmd->t_transport_timeout) > 1) {
2227 int tmp = (atomic_read(&cmd->t_transport_timeout) - 1);
2228
2229 atomic_sub(tmp, &cmd->t_se_count);
2230 }
2231 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2232
2233 transport_generic_remove(cmd, 0);
2234 }
2235
2236 static inline u32 transport_lba_21(unsigned char *cdb)
2237 {
2238 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2239 }
2240
2241 static inline u32 transport_lba_32(unsigned char *cdb)
2242 {
2243 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2244 }
2245
2246 static inline unsigned long long transport_lba_64(unsigned char *cdb)
2247 {
2248 unsigned int __v1, __v2;
2249
2250 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2251 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2252
2253 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2254 }
2255
2256 /*
2257 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2258 */
2259 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2260 {
2261 unsigned int __v1, __v2;
2262
2263 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2264 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2265
2266 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2267 }
2268
2269 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2270 {
2271 unsigned long flags;
2272
2273 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2274 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2275 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2276 }
2277
2278 /*
2279 * Called from interrupt context.
2280 */
2281 static void transport_task_timeout_handler(unsigned long data)
2282 {
2283 struct se_task *task = (struct se_task *)data;
2284 struct se_cmd *cmd = task->task_se_cmd;
2285 unsigned long flags;
2286
2287 DEBUG_TT("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
2288
2289 spin_lock_irqsave(&cmd->t_state_lock, flags);
2290 if (task->task_flags & TF_STOP) {
2291 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2292 return;
2293 }
2294 task->task_flags &= ~TF_RUNNING;
2295
2296 /*
2297 * Determine if transport_complete_task() has already been called.
2298 */
2299 if (!(atomic_read(&task->task_active))) {
2300 DEBUG_TT("transport task: %p cmd: %p timeout task_active"
2301 " == 0\n", task, cmd);
2302 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2303 return;
2304 }
2305
2306 atomic_inc(&cmd->t_se_count);
2307 atomic_inc(&cmd->t_transport_timeout);
2308 cmd->t_tasks_failed = 1;
2309
2310 atomic_set(&task->task_timeout, 1);
2311 task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
2312 task->task_scsi_status = 1;
2313
2314 if (atomic_read(&task->task_stop)) {
2315 DEBUG_TT("transport task: %p cmd: %p timeout task_stop"
2316 " == 1\n", task, cmd);
2317 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2318 complete(&task->task_stop_comp);
2319 return;
2320 }
2321
2322 if (!(atomic_dec_and_test(&cmd->t_task_cdbs_left))) {
2323 DEBUG_TT("transport task: %p cmd: %p timeout non zero"
2324 " t_task_cdbs_left\n", task, cmd);
2325 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2326 return;
2327 }
2328 DEBUG_TT("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2329 task, cmd);
2330
2331 cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
2332 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2333
2334 transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
2335 }
2336
2337 /*
2338 * Called with cmd->t_state_lock held.
2339 */
2340 static void transport_start_task_timer(struct se_task *task)
2341 {
2342 struct se_device *dev = task->se_dev;
2343 int timeout;
2344
2345 if (task->task_flags & TF_RUNNING)
2346 return;
2347 /*
2348 * If the task_timeout is disabled, exit now.
2349 */
2350 timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
2351 if (!(timeout))
2352 return;
2353
2354 init_timer(&task->task_timer);
2355 task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
2356 task->task_timer.data = (unsigned long) task;
2357 task->task_timer.function = transport_task_timeout_handler;
2358
2359 task->task_flags |= TF_RUNNING;
2360 add_timer(&task->task_timer);
2361 #if 0
2362 printk(KERN_INFO "Starting task timer for cmd: %p task: %p seconds:"
2363 " %d\n", task->task_se_cmd, task, timeout);
2364 #endif
2365 }
2366
2367 /*
2368 * Called with spin_lock_irq(&cmd->t_state_lock) held.
2369 */
2370 void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
2371 {
2372 struct se_cmd *cmd = task->task_se_cmd;
2373
2374 if (!(task->task_flags & TF_RUNNING))
2375 return;
2376
2377 task->task_flags |= TF_STOP;
2378 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
2379
2380 del_timer_sync(&task->task_timer);
2381
2382 spin_lock_irqsave(&cmd->t_state_lock, *flags);
2383 task->task_flags &= ~TF_RUNNING;
2384 task->task_flags &= ~TF_STOP;
2385 }
2386
2387 static void transport_stop_all_task_timers(struct se_cmd *cmd)
2388 {
2389 struct se_task *task = NULL, *task_tmp;
2390 unsigned long flags;
2391
2392 spin_lock_irqsave(&cmd->t_state_lock, flags);
2393 list_for_each_entry_safe(task, task_tmp,
2394 &cmd->t_task_list, t_list)
2395 __transport_stop_task_timer(task, &flags);
2396 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2397 }
2398
2399 static inline int transport_tcq_window_closed(struct se_device *dev)
2400 {
2401 if (dev->dev_tcq_window_closed++ <
2402 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
2403 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
2404 } else
2405 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
2406
2407 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
2408 return 0;
2409 }
2410
2411 /*
2412 * Called from Fabric Module context from transport_execute_tasks()
2413 *
2414 * The return of this function determins if the tasks from struct se_cmd
2415 * get added to the execution queue in transport_execute_tasks(),
2416 * or are added to the delayed or ordered lists here.
2417 */
2418 static inline int transport_execute_task_attr(struct se_cmd *cmd)
2419 {
2420 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2421 return 1;
2422 /*
2423 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2424 * to allow the passed struct se_cmd list of tasks to the front of the list.
2425 */
2426 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
2427 atomic_inc(&cmd->se_dev->dev_hoq_count);
2428 smp_mb__after_atomic_inc();
2429 DEBUG_STA("Added HEAD_OF_QUEUE for CDB:"
2430 " 0x%02x, se_ordered_id: %u\n",
2431 cmd->_task_cdb[0],
2432 cmd->se_ordered_id);
2433 return 1;
2434 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
2435 spin_lock(&cmd->se_dev->ordered_cmd_lock);
2436 list_add_tail(&cmd->se_ordered_node,
2437 &cmd->se_dev->ordered_cmd_list);
2438 spin_unlock(&cmd->se_dev->ordered_cmd_lock);
2439
2440 atomic_inc(&cmd->se_dev->dev_ordered_sync);
2441 smp_mb__after_atomic_inc();
2442
2443 DEBUG_STA("Added ORDERED for CDB: 0x%02x to ordered"
2444 " list, se_ordered_id: %u\n",
2445 cmd->t_task_cdb[0],
2446 cmd->se_ordered_id);
2447 /*
2448 * Add ORDERED command to tail of execution queue if
2449 * no other older commands exist that need to be
2450 * completed first.
2451 */
2452 if (!(atomic_read(&cmd->se_dev->simple_cmds)))
2453 return 1;
2454 } else {
2455 /*
2456 * For SIMPLE and UNTAGGED Task Attribute commands
2457 */
2458 atomic_inc(&cmd->se_dev->simple_cmds);
2459 smp_mb__after_atomic_inc();
2460 }
2461 /*
2462 * Otherwise if one or more outstanding ORDERED task attribute exist,
2463 * add the dormant task(s) built for the passed struct se_cmd to the
2464 * execution queue and become in Active state for this struct se_device.
2465 */
2466 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2467 /*
2468 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2469 * will be drained upon completion of HEAD_OF_QUEUE task.
2470 */
2471 spin_lock(&cmd->se_dev->delayed_cmd_lock);
2472 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2473 list_add_tail(&cmd->se_delayed_node,
2474 &cmd->se_dev->delayed_cmd_list);
2475 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2476
2477 DEBUG_STA("Added CDB: 0x%02x Task Attr: 0x%02x to"
2478 " delayed CMD list, se_ordered_id: %u\n",
2479 cmd->t_task_cdb[0], cmd->sam_task_attr,
2480 cmd->se_ordered_id);
2481 /*
2482 * Return zero to let transport_execute_tasks() know
2483 * not to add the delayed tasks to the execution list.
2484 */
2485 return 0;
2486 }
2487 /*
2488 * Otherwise, no ORDERED task attributes exist..
2489 */
2490 return 1;
2491 }
2492
2493 /*
2494 * Called from fabric module context in transport_generic_new_cmd() and
2495 * transport_generic_process_write()
2496 */
2497 static int transport_execute_tasks(struct se_cmd *cmd)
2498 {
2499 int add_tasks;
2500
2501 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2502 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2503 transport_generic_request_failure(cmd, NULL, 0, 1);
2504 return 0;
2505 }
2506
2507 /*
2508 * Call transport_cmd_check_stop() to see if a fabric exception
2509 * has occurred that prevents execution.
2510 */
2511 if (!(transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING))) {
2512 /*
2513 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2514 * attribute for the tasks of the received struct se_cmd CDB
2515 */
2516 add_tasks = transport_execute_task_attr(cmd);
2517 if (!add_tasks)
2518 goto execute_tasks;
2519 /*
2520 * This calls transport_add_tasks_from_cmd() to handle
2521 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2522 * (if enabled) in __transport_add_task_to_execute_queue() and
2523 * transport_add_task_check_sam_attr().
2524 */
2525 transport_add_tasks_from_cmd(cmd);
2526 }
2527 /*
2528 * Kick the execution queue for the cmd associated struct se_device
2529 * storage object.
2530 */
2531 execute_tasks:
2532 __transport_execute_tasks(cmd->se_dev);
2533 return 0;
2534 }
2535
2536 /*
2537 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2538 * from struct se_device->execute_task_list and
2539 *
2540 * Called from transport_processing_thread()
2541 */
2542 static int __transport_execute_tasks(struct se_device *dev)
2543 {
2544 int error;
2545 struct se_cmd *cmd = NULL;
2546 struct se_task *task = NULL;
2547 unsigned long flags;
2548
2549 /*
2550 * Check if there is enough room in the device and HBA queue to send
2551 * struct se_tasks to the selected transport.
2552 */
2553 check_depth:
2554 if (!atomic_read(&dev->depth_left))
2555 return transport_tcq_window_closed(dev);
2556
2557 dev->dev_tcq_window_closed = 0;
2558
2559 spin_lock_irq(&dev->execute_task_lock);
2560 if (list_empty(&dev->execute_task_list)) {
2561 spin_unlock_irq(&dev->execute_task_lock);
2562 return 0;
2563 }
2564 task = list_first_entry(&dev->execute_task_list,
2565 struct se_task, t_execute_list);
2566 list_del(&task->t_execute_list);
2567 atomic_set(&task->task_execute_queue, 0);
2568 atomic_dec(&dev->execute_tasks);
2569 spin_unlock_irq(&dev->execute_task_lock);
2570
2571 atomic_dec(&dev->depth_left);
2572
2573 cmd = task->task_se_cmd;
2574
2575 spin_lock_irqsave(&cmd->t_state_lock, flags);
2576 atomic_set(&task->task_active, 1);
2577 atomic_set(&task->task_sent, 1);
2578 atomic_inc(&cmd->t_task_cdbs_sent);
2579
2580 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2581 cmd->t_task_list_num)
2582 atomic_set(&cmd->transport_sent, 1);
2583
2584 transport_start_task_timer(task);
2585 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2586 /*
2587 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2588 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2589 * struct se_subsystem_api->do_task() caller below.
2590 */
2591 if (cmd->transport_emulate_cdb) {
2592 error = cmd->transport_emulate_cdb(cmd);
2593 if (error != 0) {
2594 cmd->transport_error_status = error;
2595 atomic_set(&task->task_active, 0);
2596 atomic_set(&cmd->transport_sent, 0);
2597 transport_stop_tasks_for_cmd(cmd);
2598 transport_generic_request_failure(cmd, dev, 0, 1);
2599 goto check_depth;
2600 }
2601 /*
2602 * Handle the successful completion for transport_emulate_cdb()
2603 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2604 * Otherwise the caller is expected to complete the task with
2605 * proper status.
2606 */
2607 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2608 cmd->scsi_status = SAM_STAT_GOOD;
2609 task->task_scsi_status = GOOD;
2610 transport_complete_task(task, 1);
2611 }
2612 } else {
2613 /*
2614 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2615 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2616 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2617 * LUN emulation code.
2618 *
2619 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2620 * call ->do_task() directly and let the underlying TCM subsystem plugin
2621 * code handle the CDB emulation.
2622 */
2623 if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2624 (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2625 error = transport_emulate_control_cdb(task);
2626 else
2627 error = dev->transport->do_task(task);
2628
2629 if (error != 0) {
2630 cmd->transport_error_status = error;
2631 atomic_set(&task->task_active, 0);
2632 atomic_set(&cmd->transport_sent, 0);
2633 transport_stop_tasks_for_cmd(cmd);
2634 transport_generic_request_failure(cmd, dev, 0, 1);
2635 }
2636 }
2637
2638 goto check_depth;
2639
2640 return 0;
2641 }
2642
2643 void transport_new_cmd_failure(struct se_cmd *se_cmd)
2644 {
2645 unsigned long flags;
2646 /*
2647 * Any unsolicited data will get dumped for failed command inside of
2648 * the fabric plugin
2649 */
2650 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2651 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2652 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2653 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2654 }
2655
2656 static void transport_nop_wait_for_tasks(struct se_cmd *, int, int);
2657
2658 static inline u32 transport_get_sectors_6(
2659 unsigned char *cdb,
2660 struct se_cmd *cmd,
2661 int *ret)
2662 {
2663 struct se_device *dev = cmd->se_dev;
2664
2665 /*
2666 * Assume TYPE_DISK for non struct se_device objects.
2667 * Use 8-bit sector value.
2668 */
2669 if (!dev)
2670 goto type_disk;
2671
2672 /*
2673 * Use 24-bit allocation length for TYPE_TAPE.
2674 */
2675 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2676 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2677
2678 /*
2679 * Everything else assume TYPE_DISK Sector CDB location.
2680 * Use 8-bit sector value.
2681 */
2682 type_disk:
2683 return (u32)cdb[4];
2684 }
2685
2686 static inline u32 transport_get_sectors_10(
2687 unsigned char *cdb,
2688 struct se_cmd *cmd,
2689 int *ret)
2690 {
2691 struct se_device *dev = cmd->se_dev;
2692
2693 /*
2694 * Assume TYPE_DISK for non struct se_device objects.
2695 * Use 16-bit sector value.
2696 */
2697 if (!dev)
2698 goto type_disk;
2699
2700 /*
2701 * XXX_10 is not defined in SSC, throw an exception
2702 */
2703 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2704 *ret = -EINVAL;
2705 return 0;
2706 }
2707
2708 /*
2709 * Everything else assume TYPE_DISK Sector CDB location.
2710 * Use 16-bit sector value.
2711 */
2712 type_disk:
2713 return (u32)(cdb[7] << 8) + cdb[8];
2714 }
2715
2716 static inline u32 transport_get_sectors_12(
2717 unsigned char *cdb,
2718 struct se_cmd *cmd,
2719 int *ret)
2720 {
2721 struct se_device *dev = cmd->se_dev;
2722
2723 /*
2724 * Assume TYPE_DISK for non struct se_device objects.
2725 * Use 32-bit sector value.
2726 */
2727 if (!dev)
2728 goto type_disk;
2729
2730 /*
2731 * XXX_12 is not defined in SSC, throw an exception
2732 */
2733 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2734 *ret = -EINVAL;
2735 return 0;
2736 }
2737
2738 /*
2739 * Everything else assume TYPE_DISK Sector CDB location.
2740 * Use 32-bit sector value.
2741 */
2742 type_disk:
2743 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2744 }
2745
2746 static inline u32 transport_get_sectors_16(
2747 unsigned char *cdb,
2748 struct se_cmd *cmd,
2749 int *ret)
2750 {
2751 struct se_device *dev = cmd->se_dev;
2752
2753 /*
2754 * Assume TYPE_DISK for non struct se_device objects.
2755 * Use 32-bit sector value.
2756 */
2757 if (!dev)
2758 goto type_disk;
2759
2760 /*
2761 * Use 24-bit allocation length for TYPE_TAPE.
2762 */
2763 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2764 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2765
2766 type_disk:
2767 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2768 (cdb[12] << 8) + cdb[13];
2769 }
2770
2771 /*
2772 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2773 */
2774 static inline u32 transport_get_sectors_32(
2775 unsigned char *cdb,
2776 struct se_cmd *cmd,
2777 int *ret)
2778 {
2779 /*
2780 * Assume TYPE_DISK for non struct se_device objects.
2781 * Use 32-bit sector value.
2782 */
2783 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2784 (cdb[30] << 8) + cdb[31];
2785
2786 }
2787
2788 static inline u32 transport_get_size(
2789 u32 sectors,
2790 unsigned char *cdb,
2791 struct se_cmd *cmd)
2792 {
2793 struct se_device *dev = cmd->se_dev;
2794
2795 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2796 if (cdb[1] & 1) { /* sectors */
2797 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2798 } else /* bytes */
2799 return sectors;
2800 }
2801 #if 0
2802 printk(KERN_INFO "Returning block_size: %u, sectors: %u == %u for"
2803 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2804 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2805 dev->transport->name);
2806 #endif
2807 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2808 }
2809
2810 unsigned char transport_asciihex_to_binaryhex(unsigned char val[2])
2811 {
2812 unsigned char result = 0;
2813 /*
2814 * MSB
2815 */
2816 if ((val[0] >= 'a') && (val[0] <= 'f'))
2817 result = ((val[0] - 'a' + 10) & 0xf) << 4;
2818 else
2819 if ((val[0] >= 'A') && (val[0] <= 'F'))
2820 result = ((val[0] - 'A' + 10) & 0xf) << 4;
2821 else /* digit */
2822 result = ((val[0] - '0') & 0xf) << 4;
2823 /*
2824 * LSB
2825 */
2826 if ((val[1] >= 'a') && (val[1] <= 'f'))
2827 result |= ((val[1] - 'a' + 10) & 0xf);
2828 else
2829 if ((val[1] >= 'A') && (val[1] <= 'F'))
2830 result |= ((val[1] - 'A' + 10) & 0xf);
2831 else /* digit */
2832 result |= ((val[1] - '0') & 0xf);
2833
2834 return result;
2835 }
2836 EXPORT_SYMBOL(transport_asciihex_to_binaryhex);
2837
2838 static void transport_xor_callback(struct se_cmd *cmd)
2839 {
2840 unsigned char *buf, *addr;
2841 struct se_mem *se_mem;
2842 unsigned int offset;
2843 int i;
2844 /*
2845 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2846 *
2847 * 1) read the specified logical block(s);
2848 * 2) transfer logical blocks from the data-out buffer;
2849 * 3) XOR the logical blocks transferred from the data-out buffer with
2850 * the logical blocks read, storing the resulting XOR data in a buffer;
2851 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2852 * blocks transferred from the data-out buffer; and
2853 * 5) transfer the resulting XOR data to the data-in buffer.
2854 */
2855 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2856 if (!(buf)) {
2857 printk(KERN_ERR "Unable to allocate xor_callback buf\n");
2858 return;
2859 }
2860 /*
2861 * Copy the scatterlist WRITE buffer located at cmd->t_mem_list
2862 * into the locally allocated *buf
2863 */
2864 transport_memcpy_se_mem_read_contig(buf, &cmd->t_mem_list,
2865 cmd->data_length);
2866 /*
2867 * Now perform the XOR against the BIDI read memory located at
2868 * cmd->t_mem_bidi_list
2869 */
2870
2871 offset = 0;
2872 list_for_each_entry(se_mem, &cmd->t_mem_bidi_list, se_list) {
2873 addr = (unsigned char *)kmap_atomic(se_mem->se_page, KM_USER0);
2874 if (!(addr))
2875 goto out;
2876
2877 for (i = 0; i < se_mem->se_len; i++)
2878 *(addr + se_mem->se_off + i) ^= *(buf + offset + i);
2879
2880 offset += se_mem->se_len;
2881 kunmap_atomic(addr, KM_USER0);
2882 }
2883 out:
2884 kfree(buf);
2885 }
2886
2887 /*
2888 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2889 */
2890 static int transport_get_sense_data(struct se_cmd *cmd)
2891 {
2892 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2893 struct se_device *dev;
2894 struct se_task *task = NULL, *task_tmp;
2895 unsigned long flags;
2896 u32 offset = 0;
2897
2898 WARN_ON(!cmd->se_lun);
2899
2900 spin_lock_irqsave(&cmd->t_state_lock, flags);
2901 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2902 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2903 return 0;
2904 }
2905
2906 list_for_each_entry_safe(task, task_tmp,
2907 &cmd->t_task_list, t_list) {
2908
2909 if (!task->task_sense)
2910 continue;
2911
2912 dev = task->se_dev;
2913 if (!(dev))
2914 continue;
2915
2916 if (!dev->transport->get_sense_buffer) {
2917 printk(KERN_ERR "dev->transport->get_sense_buffer"
2918 " is NULL\n");
2919 continue;
2920 }
2921
2922 sense_buffer = dev->transport->get_sense_buffer(task);
2923 if (!(sense_buffer)) {
2924 printk(KERN_ERR "ITT[0x%08x]_TASK[%d]: Unable to locate"
2925 " sense buffer for task with sense\n",
2926 cmd->se_tfo->get_task_tag(cmd), task->task_no);
2927 continue;
2928 }
2929 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2930
2931 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2932 TRANSPORT_SENSE_BUFFER);
2933
2934 memcpy(&buffer[offset], sense_buffer,
2935 TRANSPORT_SENSE_BUFFER);
2936 cmd->scsi_status = task->task_scsi_status;
2937 /* Automatically padded */
2938 cmd->scsi_sense_length =
2939 (TRANSPORT_SENSE_BUFFER + offset);
2940
2941 printk(KERN_INFO "HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2942 " and sense\n",
2943 dev->se_hba->hba_id, dev->transport->name,
2944 cmd->scsi_status);
2945 return 0;
2946 }
2947 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2948
2949 return -1;
2950 }
2951
2952 static int
2953 transport_handle_reservation_conflict(struct se_cmd *cmd)
2954 {
2955 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
2956 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2957 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2958 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2959 /*
2960 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2961 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2962 * CONFLICT STATUS.
2963 *
2964 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2965 */
2966 if (cmd->se_sess &&
2967 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2968 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2969 cmd->orig_fe_lun, 0x2C,
2970 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2971 return -EINVAL;
2972 }
2973
2974 /* transport_generic_cmd_sequencer():
2975 *
2976 * Generic Command Sequencer that should work for most DAS transport
2977 * drivers.
2978 *
2979 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2980 * RX Thread.
2981 *
2982 * FIXME: Need to support other SCSI OPCODES where as well.
2983 */
2984 static int transport_generic_cmd_sequencer(
2985 struct se_cmd *cmd,
2986 unsigned char *cdb)
2987 {
2988 struct se_device *dev = cmd->se_dev;
2989 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2990 int ret = 0, sector_ret = 0, passthrough;
2991 u32 sectors = 0, size = 0, pr_reg_type = 0;
2992 u16 service_action;
2993 u8 alua_ascq = 0;
2994 /*
2995 * Check for an existing UNIT ATTENTION condition
2996 */
2997 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2998 cmd->transport_wait_for_tasks =
2999 &transport_nop_wait_for_tasks;
3000 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3001 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
3002 return -EINVAL;
3003 }
3004 /*
3005 * Check status of Asymmetric Logical Unit Assignment port
3006 */
3007 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
3008 if (ret != 0) {
3009 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3010 /*
3011 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
3012 * The ALUA additional sense code qualifier (ASCQ) is determined
3013 * by the ALUA primary or secondary access state..
3014 */
3015 if (ret > 0) {
3016 #if 0
3017 printk(KERN_INFO "[%s]: ALUA TG Port not available,"
3018 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
3019 cmd->se_tfo->get_fabric_name(), alua_ascq);
3020 #endif
3021 transport_set_sense_codes(cmd, 0x04, alua_ascq);
3022 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3023 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
3024 return -EINVAL;
3025 }
3026 goto out_invalid_cdb_field;
3027 }
3028 /*
3029 * Check status for SPC-3 Persistent Reservations
3030 */
3031 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
3032 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
3033 cmd, cdb, pr_reg_type) != 0)
3034 return transport_handle_reservation_conflict(cmd);
3035 /*
3036 * This means the CDB is allowed for the SCSI Initiator port
3037 * when said port is *NOT* holding the legacy SPC-2 or
3038 * SPC-3 Persistent Reservation.
3039 */
3040 }
3041
3042 switch (cdb[0]) {
3043 case READ_6:
3044 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
3045 if (sector_ret)
3046 goto out_unsupported_cdb;
3047 size = transport_get_size(sectors, cdb, cmd);
3048 cmd->transport_split_cdb = &split_cdb_XX_6;
3049 cmd->t_task_lba = transport_lba_21(cdb);
3050 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3051 break;
3052 case READ_10:
3053 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3054 if (sector_ret)
3055 goto out_unsupported_cdb;
3056 size = transport_get_size(sectors, cdb, cmd);
3057 cmd->transport_split_cdb = &split_cdb_XX_10;
3058 cmd->t_task_lba = transport_lba_32(cdb);
3059 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3060 break;
3061 case READ_12:
3062 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
3063 if (sector_ret)
3064 goto out_unsupported_cdb;
3065 size = transport_get_size(sectors, cdb, cmd);
3066 cmd->transport_split_cdb = &split_cdb_XX_12;
3067 cmd->t_task_lba = transport_lba_32(cdb);
3068 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3069 break;
3070 case READ_16:
3071 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3072 if (sector_ret)
3073 goto out_unsupported_cdb;
3074 size = transport_get_size(sectors, cdb, cmd);
3075 cmd->transport_split_cdb = &split_cdb_XX_16;
3076 cmd->t_task_lba = transport_lba_64(cdb);
3077 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3078 break;
3079 case WRITE_6:
3080 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
3081 if (sector_ret)
3082 goto out_unsupported_cdb;
3083 size = transport_get_size(sectors, cdb, cmd);
3084 cmd->transport_split_cdb = &split_cdb_XX_6;
3085 cmd->t_task_lba = transport_lba_21(cdb);
3086 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3087 break;
3088 case WRITE_10:
3089 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3090 if (sector_ret)
3091 goto out_unsupported_cdb;
3092 size = transport_get_size(sectors, cdb, cmd);
3093 cmd->transport_split_cdb = &split_cdb_XX_10;
3094 cmd->t_task_lba = transport_lba_32(cdb);
3095 cmd->t_tasks_fua = (cdb[1] & 0x8);
3096 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3097 break;
3098 case WRITE_12:
3099 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
3100 if (sector_ret)
3101 goto out_unsupported_cdb;
3102 size = transport_get_size(sectors, cdb, cmd);
3103 cmd->transport_split_cdb = &split_cdb_XX_12;
3104 cmd->t_task_lba = transport_lba_32(cdb);
3105 cmd->t_tasks_fua = (cdb[1] & 0x8);
3106 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3107 break;
3108 case WRITE_16:
3109 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3110 if (sector_ret)
3111 goto out_unsupported_cdb;
3112 size = transport_get_size(sectors, cdb, cmd);
3113 cmd->transport_split_cdb = &split_cdb_XX_16;
3114 cmd->t_task_lba = transport_lba_64(cdb);
3115 cmd->t_tasks_fua = (cdb[1] & 0x8);
3116 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3117 break;
3118 case XDWRITEREAD_10:
3119 if ((cmd->data_direction != DMA_TO_DEVICE) ||
3120 !(cmd->t_tasks_bidi))
3121 goto out_invalid_cdb_field;
3122 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3123 if (sector_ret)
3124 goto out_unsupported_cdb;
3125 size = transport_get_size(sectors, cdb, cmd);
3126 cmd->transport_split_cdb = &split_cdb_XX_10;
3127 cmd->t_task_lba = transport_lba_32(cdb);
3128 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3129 passthrough = (dev->transport->transport_type ==
3130 TRANSPORT_PLUGIN_PHBA_PDEV);
3131 /*
3132 * Skip the remaining assignments for TCM/PSCSI passthrough
3133 */
3134 if (passthrough)
3135 break;
3136 /*
3137 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
3138 */
3139 cmd->transport_complete_callback = &transport_xor_callback;
3140 cmd->t_tasks_fua = (cdb[1] & 0x8);
3141 break;
3142 case VARIABLE_LENGTH_CMD:
3143 service_action = get_unaligned_be16(&cdb[8]);
3144 /*
3145 * Determine if this is TCM/PSCSI device and we should disable
3146 * internal emulation for this CDB.
3147 */
3148 passthrough = (dev->transport->transport_type ==
3149 TRANSPORT_PLUGIN_PHBA_PDEV);
3150
3151 switch (service_action) {
3152 case XDWRITEREAD_32:
3153 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3154 if (sector_ret)
3155 goto out_unsupported_cdb;
3156 size = transport_get_size(sectors, cdb, cmd);
3157 /*
3158 * Use WRITE_32 and READ_32 opcodes for the emulated
3159 * XDWRITE_READ_32 logic.
3160 */
3161 cmd->transport_split_cdb = &split_cdb_XX_32;
3162 cmd->t_task_lba = transport_lba_64_ext(cdb);
3163 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
3164
3165 /*
3166 * Skip the remaining assignments for TCM/PSCSI passthrough
3167 */
3168 if (passthrough)
3169 break;
3170
3171 /*
3172 * Setup BIDI XOR callback to be run during
3173 * transport_generic_complete_ok()
3174 */
3175 cmd->transport_complete_callback = &transport_xor_callback;
3176 cmd->t_tasks_fua = (cdb[10] & 0x8);
3177 break;
3178 case WRITE_SAME_32:
3179 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
3180 if (sector_ret)
3181 goto out_unsupported_cdb;
3182
3183 if (sectors != 0)
3184 size = transport_get_size(sectors, cdb, cmd);
3185 else
3186 size = dev->se_sub_dev->se_dev_attrib.block_size;
3187
3188 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
3189 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3190
3191 /*
3192 * Skip the remaining assignments for TCM/PSCSI passthrough
3193 */
3194 if (passthrough)
3195 break;
3196
3197 if ((cdb[10] & 0x04) || (cdb[10] & 0x02)) {
3198 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3199 " bits not supported for Block Discard"
3200 " Emulation\n");
3201 goto out_invalid_cdb_field;
3202 }
3203 /*
3204 * Currently for the emulated case we only accept
3205 * tpws with the UNMAP=1 bit set.
3206 */
3207 if (!(cdb[10] & 0x08)) {
3208 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not"
3209 " supported for Block Discard Emulation\n");
3210 goto out_invalid_cdb_field;
3211 }
3212 break;
3213 default:
3214 printk(KERN_ERR "VARIABLE_LENGTH_CMD service action"
3215 " 0x%04x not supported\n", service_action);
3216 goto out_unsupported_cdb;
3217 }
3218 break;
3219 case MAINTENANCE_IN:
3220 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3221 /* MAINTENANCE_IN from SCC-2 */
3222 /*
3223 * Check for emulated MI_REPORT_TARGET_PGS.
3224 */
3225 if (cdb[1] == MI_REPORT_TARGET_PGS) {
3226 cmd->transport_emulate_cdb =
3227 (su_dev->t10_alua.alua_type ==
3228 SPC3_ALUA_EMULATED) ?
3229 core_emulate_report_target_port_groups :
3230 NULL;
3231 }
3232 size = (cdb[6] << 24) | (cdb[7] << 16) |
3233 (cdb[8] << 8) | cdb[9];
3234 } else {
3235 /* GPCMD_SEND_KEY from multi media commands */
3236 size = (cdb[8] << 8) + cdb[9];
3237 }
3238 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3239 break;
3240 case MODE_SELECT:
3241 size = cdb[4];
3242 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3243 break;
3244 case MODE_SELECT_10:
3245 size = (cdb[7] << 8) + cdb[8];
3246 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3247 break;
3248 case MODE_SENSE:
3249 size = cdb[4];
3250 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3251 break;
3252 case MODE_SENSE_10:
3253 case GPCMD_READ_BUFFER_CAPACITY:
3254 case GPCMD_SEND_OPC:
3255 case LOG_SELECT:
3256 case LOG_SENSE:
3257 size = (cdb[7] << 8) + cdb[8];
3258 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3259 break;
3260 case READ_BLOCK_LIMITS:
3261 size = READ_BLOCK_LEN;
3262 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3263 break;
3264 case GPCMD_GET_CONFIGURATION:
3265 case GPCMD_READ_FORMAT_CAPACITIES:
3266 case GPCMD_READ_DISC_INFO:
3267 case GPCMD_READ_TRACK_RZONE_INFO:
3268 size = (cdb[7] << 8) + cdb[8];
3269 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3270 break;
3271 case PERSISTENT_RESERVE_IN:
3272 case PERSISTENT_RESERVE_OUT:
3273 cmd->transport_emulate_cdb =
3274 (su_dev->t10_pr.res_type ==
3275 SPC3_PERSISTENT_RESERVATIONS) ?
3276 core_scsi3_emulate_pr : NULL;
3277 size = (cdb[7] << 8) + cdb[8];
3278 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3279 break;
3280 case GPCMD_MECHANISM_STATUS:
3281 case GPCMD_READ_DVD_STRUCTURE:
3282 size = (cdb[8] << 8) + cdb[9];
3283 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3284 break;
3285 case READ_POSITION:
3286 size = READ_POSITION_LEN;
3287 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3288 break;
3289 case MAINTENANCE_OUT:
3290 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3291 /* MAINTENANCE_OUT from SCC-2
3292 *
3293 * Check for emulated MO_SET_TARGET_PGS.
3294 */
3295 if (cdb[1] == MO_SET_TARGET_PGS) {
3296 cmd->transport_emulate_cdb =
3297 (su_dev->t10_alua.alua_type ==
3298 SPC3_ALUA_EMULATED) ?
3299 core_emulate_set_target_port_groups :
3300 NULL;
3301 }
3302
3303 size = (cdb[6] << 24) | (cdb[7] << 16) |
3304 (cdb[8] << 8) | cdb[9];
3305 } else {
3306 /* GPCMD_REPORT_KEY from multi media commands */
3307 size = (cdb[8] << 8) + cdb[9];
3308 }
3309 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3310 break;
3311 case INQUIRY:
3312 size = (cdb[3] << 8) + cdb[4];
3313 /*
3314 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3315 * See spc4r17 section 5.3
3316 */
3317 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3318 cmd->sam_task_attr = MSG_HEAD_TAG;
3319 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3320 break;
3321 case READ_BUFFER:
3322 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3323 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3324 break;
3325 case READ_CAPACITY:
3326 size = READ_CAP_LEN;
3327 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3328 break;
3329 case READ_MEDIA_SERIAL_NUMBER:
3330 case SECURITY_PROTOCOL_IN:
3331 case SECURITY_PROTOCOL_OUT:
3332 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3333 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3334 break;
3335 case SERVICE_ACTION_IN:
3336 case ACCESS_CONTROL_IN:
3337 case ACCESS_CONTROL_OUT:
3338 case EXTENDED_COPY:
3339 case READ_ATTRIBUTE:
3340 case RECEIVE_COPY_RESULTS:
3341 case WRITE_ATTRIBUTE:
3342 size = (cdb[10] << 24) | (cdb[11] << 16) |
3343 (cdb[12] << 8) | cdb[13];
3344 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3345 break;
3346 case RECEIVE_DIAGNOSTIC:
3347 case SEND_DIAGNOSTIC:
3348 size = (cdb[3] << 8) | cdb[4];
3349 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3350 break;
3351 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3352 #if 0
3353 case GPCMD_READ_CD:
3354 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3355 size = (2336 * sectors);
3356 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3357 break;
3358 #endif
3359 case READ_TOC:
3360 size = cdb[8];
3361 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3362 break;
3363 case REQUEST_SENSE:
3364 size = cdb[4];
3365 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3366 break;
3367 case READ_ELEMENT_STATUS:
3368 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
3369 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3370 break;
3371 case WRITE_BUFFER:
3372 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3373 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3374 break;
3375 case RESERVE:
3376 case RESERVE_10:
3377 /*
3378 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3379 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3380 */
3381 if (cdb[0] == RESERVE_10)
3382 size = (cdb[7] << 8) | cdb[8];
3383 else
3384 size = cmd->data_length;
3385
3386 /*
3387 * Setup the legacy emulated handler for SPC-2 and
3388 * >= SPC-3 compatible reservation handling (CRH=1)
3389 * Otherwise, we assume the underlying SCSI logic is
3390 * is running in SPC_PASSTHROUGH, and wants reservations
3391 * emulation disabled.
3392 */
3393 cmd->transport_emulate_cdb =
3394 (su_dev->t10_pr.res_type !=
3395 SPC_PASSTHROUGH) ?
3396 core_scsi2_emulate_crh : NULL;
3397 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3398 break;
3399 case RELEASE:
3400 case RELEASE_10:
3401 /*
3402 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3403 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3404 */
3405 if (cdb[0] == RELEASE_10)
3406 size = (cdb[7] << 8) | cdb[8];
3407 else
3408 size = cmd->data_length;
3409
3410 cmd->transport_emulate_cdb =
3411 (su_dev->t10_pr.res_type !=
3412 SPC_PASSTHROUGH) ?
3413 core_scsi2_emulate_crh : NULL;
3414 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3415 break;
3416 case SYNCHRONIZE_CACHE:
3417 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3418 /*
3419 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3420 */
3421 if (cdb[0] == SYNCHRONIZE_CACHE) {
3422 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3423 cmd->t_task_lba = transport_lba_32(cdb);
3424 } else {
3425 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3426 cmd->t_task_lba = transport_lba_64(cdb);
3427 }
3428 if (sector_ret)
3429 goto out_unsupported_cdb;
3430
3431 size = transport_get_size(sectors, cdb, cmd);
3432 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3433
3434 /*
3435 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3436 */
3437 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3438 break;
3439 /*
3440 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3441 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3442 */
3443 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3444 /*
3445 * Check to ensure that LBA + Range does not exceed past end of
3446 * device.
3447 */
3448 if (!transport_cmd_get_valid_sectors(cmd))
3449 goto out_invalid_cdb_field;
3450 break;
3451 case UNMAP:
3452 size = get_unaligned_be16(&cdb[7]);
3453 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3454 break;
3455 case WRITE_SAME_16:
3456 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3457 if (sector_ret)
3458 goto out_unsupported_cdb;
3459
3460 if (sectors != 0)
3461 size = transport_get_size(sectors, cdb, cmd);
3462 else
3463 size = dev->se_sub_dev->se_dev_attrib.block_size;
3464
3465 cmd->t_task_lba = get_unaligned_be16(&cdb[2]);
3466 passthrough = (dev->transport->transport_type ==
3467 TRANSPORT_PLUGIN_PHBA_PDEV);
3468 /*
3469 * Determine if the received WRITE_SAME_16 is used to for direct
3470 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
3471 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
3472 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK and
3473 * TCM/FILEIO subsystem plugin backstores.
3474 */
3475 if (!(passthrough)) {
3476 if ((cdb[1] & 0x04) || (cdb[1] & 0x02)) {
3477 printk(KERN_ERR "WRITE_SAME PBDATA and LBDATA"
3478 " bits not supported for Block Discard"
3479 " Emulation\n");
3480 goto out_invalid_cdb_field;
3481 }
3482 /*
3483 * Currently for the emulated case we only accept
3484 * tpws with the UNMAP=1 bit set.
3485 */
3486 if (!(cdb[1] & 0x08)) {
3487 printk(KERN_ERR "WRITE_SAME w/o UNMAP bit not "
3488 " supported for Block Discard Emulation\n");
3489 goto out_invalid_cdb_field;
3490 }
3491 }
3492 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3493 break;
3494 case ALLOW_MEDIUM_REMOVAL:
3495 case GPCMD_CLOSE_TRACK:
3496 case ERASE:
3497 case INITIALIZE_ELEMENT_STATUS:
3498 case GPCMD_LOAD_UNLOAD:
3499 case REZERO_UNIT:
3500 case SEEK_10:
3501 case GPCMD_SET_SPEED:
3502 case SPACE:
3503 case START_STOP:
3504 case TEST_UNIT_READY:
3505 case VERIFY:
3506 case WRITE_FILEMARKS:
3507 case MOVE_MEDIUM:
3508 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3509 break;
3510 case REPORT_LUNS:
3511 cmd->transport_emulate_cdb =
3512 transport_core_report_lun_response;
3513 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3514 /*
3515 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3516 * See spc4r17 section 5.3
3517 */
3518 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3519 cmd->sam_task_attr = MSG_HEAD_TAG;
3520 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3521 break;
3522 default:
3523 printk(KERN_WARNING "TARGET_CORE[%s]: Unsupported SCSI Opcode"
3524 " 0x%02x, sending CHECK_CONDITION.\n",
3525 cmd->se_tfo->get_fabric_name(), cdb[0]);
3526 cmd->transport_wait_for_tasks = &transport_nop_wait_for_tasks;
3527 goto out_unsupported_cdb;
3528 }
3529
3530 if (size != cmd->data_length) {
3531 printk(KERN_WARNING "TARGET_CORE[%s]: Expected Transfer Length:"
3532 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3533 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3534 cmd->data_length, size, cdb[0]);
3535
3536 cmd->cmd_spdtl = size;
3537
3538 if (cmd->data_direction == DMA_TO_DEVICE) {
3539 printk(KERN_ERR "Rejecting underflow/overflow"
3540 " WRITE data\n");
3541 goto out_invalid_cdb_field;
3542 }
3543 /*
3544 * Reject READ_* or WRITE_* with overflow/underflow for
3545 * type SCF_SCSI_DATA_SG_IO_CDB.
3546 */
3547 if (!(ret) && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3548 printk(KERN_ERR "Failing OVERFLOW/UNDERFLOW for LBA op"
3549 " CDB on non 512-byte sector setup subsystem"
3550 " plugin: %s\n", dev->transport->name);
3551 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3552 goto out_invalid_cdb_field;
3553 }
3554
3555 if (size > cmd->data_length) {
3556 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3557 cmd->residual_count = (size - cmd->data_length);
3558 } else {
3559 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3560 cmd->residual_count = (cmd->data_length - size);
3561 }
3562 cmd->data_length = size;
3563 }
3564
3565 transport_set_supported_SAM_opcode(cmd);
3566 return ret;
3567
3568 out_unsupported_cdb:
3569 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3570 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3571 return -EINVAL;
3572 out_invalid_cdb_field:
3573 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3574 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3575 return -EINVAL;
3576 }
3577
3578 static inline void transport_release_tasks(struct se_cmd *);
3579
3580 static void transport_memcpy_se_mem_read_contig(
3581 unsigned char *dst,
3582 struct list_head *se_mem_list,
3583 u32 tot_len)
3584 {
3585 struct se_mem *se_mem;
3586 void *src;
3587 u32 length;
3588
3589 list_for_each_entry(se_mem, se_mem_list, se_list) {
3590 length = min_t(u32, se_mem->se_len, tot_len);
3591 src = page_address(se_mem->se_page) + se_mem->se_off;
3592 memcpy(dst, src, length);
3593 tot_len -= length;
3594 if (!tot_len)
3595 break;
3596 dst += length;
3597 }
3598 }
3599
3600 /*
3601 * Called from transport_generic_complete_ok() and
3602 * transport_generic_request_failure() to determine which dormant/delayed
3603 * and ordered cmds need to have their tasks added to the execution queue.
3604 */
3605 static void transport_complete_task_attr(struct se_cmd *cmd)
3606 {
3607 struct se_device *dev = cmd->se_dev;
3608 struct se_cmd *cmd_p, *cmd_tmp;
3609 int new_active_tasks = 0;
3610
3611 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3612 atomic_dec(&dev->simple_cmds);
3613 smp_mb__after_atomic_dec();
3614 dev->dev_cur_ordered_id++;
3615 DEBUG_STA("Incremented dev->dev_cur_ordered_id: %u for"
3616 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3617 cmd->se_ordered_id);
3618 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3619 atomic_dec(&dev->dev_hoq_count);
3620 smp_mb__after_atomic_dec();
3621 dev->dev_cur_ordered_id++;
3622 DEBUG_STA("Incremented dev_cur_ordered_id: %u for"
3623 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3624 cmd->se_ordered_id);
3625 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3626 spin_lock(&dev->ordered_cmd_lock);
3627 list_del(&cmd->se_ordered_node);
3628 atomic_dec(&dev->dev_ordered_sync);
3629 smp_mb__after_atomic_dec();
3630 spin_unlock(&dev->ordered_cmd_lock);
3631
3632 dev->dev_cur_ordered_id++;
3633 DEBUG_STA("Incremented dev_cur_ordered_id: %u for ORDERED:"
3634 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3635 }
3636 /*
3637 * Process all commands up to the last received
3638 * ORDERED task attribute which requires another blocking
3639 * boundary
3640 */
3641 spin_lock(&dev->delayed_cmd_lock);
3642 list_for_each_entry_safe(cmd_p, cmd_tmp,
3643 &dev->delayed_cmd_list, se_delayed_node) {
3644
3645 list_del(&cmd_p->se_delayed_node);
3646 spin_unlock(&dev->delayed_cmd_lock);
3647
3648 DEBUG_STA("Calling add_tasks() for"
3649 " cmd_p: 0x%02x Task Attr: 0x%02x"
3650 " Dormant -> Active, se_ordered_id: %u\n",
3651 T_TASK(cmd_p)->t_task_cdb[0],
3652 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3653
3654 transport_add_tasks_from_cmd(cmd_p);
3655 new_active_tasks++;
3656
3657 spin_lock(&dev->delayed_cmd_lock);
3658 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3659 break;
3660 }
3661 spin_unlock(&dev->delayed_cmd_lock);
3662 /*
3663 * If new tasks have become active, wake up the transport thread
3664 * to do the processing of the Active tasks.
3665 */
3666 if (new_active_tasks != 0)
3667 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3668 }
3669
3670 static int transport_complete_qf(struct se_cmd *cmd)
3671 {
3672 int ret = 0;
3673
3674 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
3675 return cmd->se_tfo->queue_status(cmd);
3676
3677 switch (cmd->data_direction) {
3678 case DMA_FROM_DEVICE:
3679 ret = cmd->se_tfo->queue_data_in(cmd);
3680 break;
3681 case DMA_TO_DEVICE:
3682 if (!list_empty(&cmd->t_mem_bidi_list)) {
3683 ret = cmd->se_tfo->queue_data_in(cmd);
3684 if (ret < 0)
3685 return ret;
3686 }
3687 /* Fall through for DMA_TO_DEVICE */
3688 case DMA_NONE:
3689 ret = cmd->se_tfo->queue_status(cmd);
3690 break;
3691 default:
3692 break;
3693 }
3694
3695 return ret;
3696 }
3697
3698 static void transport_handle_queue_full(
3699 struct se_cmd *cmd,
3700 struct se_device *dev,
3701 int (*qf_callback)(struct se_cmd *))
3702 {
3703 spin_lock_irq(&dev->qf_cmd_lock);
3704 cmd->se_cmd_flags |= SCF_EMULATE_QUEUE_FULL;
3705 cmd->transport_qf_callback = qf_callback;
3706 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3707 atomic_inc(&dev->dev_qf_count);
3708 smp_mb__after_atomic_inc();
3709 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3710
3711 schedule_work(&cmd->se_dev->qf_work_queue);
3712 }
3713
3714 static void transport_generic_complete_ok(struct se_cmd *cmd)
3715 {
3716 int reason = 0, ret;
3717 /*
3718 * Check if we need to move delayed/dormant tasks from cmds on the
3719 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3720 * Attribute.
3721 */
3722 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3723 transport_complete_task_attr(cmd);
3724 /*
3725 * Check to schedule QUEUE_FULL work, or execute an existing
3726 * cmd->transport_qf_callback()
3727 */
3728 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3729 schedule_work(&cmd->se_dev->qf_work_queue);
3730
3731 if (cmd->transport_qf_callback) {
3732 ret = cmd->transport_qf_callback(cmd);
3733 if (ret < 0)
3734 goto queue_full;
3735
3736 cmd->transport_qf_callback = NULL;
3737 goto done;
3738 }
3739 /*
3740 * Check if we need to retrieve a sense buffer from
3741 * the struct se_cmd in question.
3742 */
3743 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3744 if (transport_get_sense_data(cmd) < 0)
3745 reason = TCM_NON_EXISTENT_LUN;
3746
3747 /*
3748 * Only set when an struct se_task->task_scsi_status returned
3749 * a non GOOD status.
3750 */
3751 if (cmd->scsi_status) {
3752 ret = transport_send_check_condition_and_sense(
3753 cmd, reason, 1);
3754 if (ret == -EAGAIN)
3755 goto queue_full;
3756
3757 transport_lun_remove_cmd(cmd);
3758 transport_cmd_check_stop_to_fabric(cmd);
3759 return;
3760 }
3761 }
3762 /*
3763 * Check for a callback, used by amongst other things
3764 * XDWRITE_READ_10 emulation.
3765 */
3766 if (cmd->transport_complete_callback)
3767 cmd->transport_complete_callback(cmd);
3768
3769 switch (cmd->data_direction) {
3770 case DMA_FROM_DEVICE:
3771 spin_lock(&cmd->se_lun->lun_sep_lock);
3772 if (cmd->se_lun->lun_sep) {
3773 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3774 cmd->data_length;
3775 }
3776 spin_unlock(&cmd->se_lun->lun_sep_lock);
3777
3778 ret = cmd->se_tfo->queue_data_in(cmd);
3779 if (ret == -EAGAIN)
3780 goto queue_full;
3781 break;
3782 case DMA_TO_DEVICE:
3783 spin_lock(&cmd->se_lun->lun_sep_lock);
3784 if (cmd->se_lun->lun_sep) {
3785 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3786 cmd->data_length;
3787 }
3788 spin_unlock(&cmd->se_lun->lun_sep_lock);
3789 /*
3790 * Check if we need to send READ payload for BIDI-COMMAND
3791 */
3792 if (!list_empty(&cmd->t_mem_bidi_list)) {
3793 spin_lock(&cmd->se_lun->lun_sep_lock);
3794 if (cmd->se_lun->lun_sep) {
3795 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3796 cmd->data_length;
3797 }
3798 spin_unlock(&cmd->se_lun->lun_sep_lock);
3799 ret = cmd->se_tfo->queue_data_in(cmd);
3800 if (ret == -EAGAIN)
3801 goto queue_full;
3802 break;
3803 }
3804 /* Fall through for DMA_TO_DEVICE */
3805 case DMA_NONE:
3806 ret = cmd->se_tfo->queue_status(cmd);
3807 if (ret == -EAGAIN)
3808 goto queue_full;
3809 break;
3810 default:
3811 break;
3812 }
3813
3814 done:
3815 transport_lun_remove_cmd(cmd);
3816 transport_cmd_check_stop_to_fabric(cmd);
3817 return;
3818
3819 queue_full:
3820 printk(KERN_INFO "Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3821 " data_direction: %d\n", cmd, cmd->data_direction);
3822 transport_handle_queue_full(cmd, cmd->se_dev, transport_complete_qf);
3823 }
3824
3825 static void transport_free_dev_tasks(struct se_cmd *cmd)
3826 {
3827 struct se_task *task, *task_tmp;
3828 unsigned long flags;
3829
3830 spin_lock_irqsave(&cmd->t_state_lock, flags);
3831 list_for_each_entry_safe(task, task_tmp,
3832 &cmd->t_task_list, t_list) {
3833 if (atomic_read(&task->task_active))
3834 continue;
3835
3836 kfree(task->task_sg_bidi);
3837 kfree(task->task_sg);
3838
3839 list_del(&task->t_list);
3840
3841 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3842 if (task->se_dev)
3843 task->se_dev->transport->free_task(task);
3844 else
3845 printk(KERN_ERR "task[%u] - task->se_dev is NULL\n",
3846 task->task_no);
3847 spin_lock_irqsave(&cmd->t_state_lock, flags);
3848 }
3849 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3850 }
3851
3852 static inline void transport_free_pages(struct se_cmd *cmd)
3853 {
3854 struct se_mem *se_mem, *se_mem_tmp;
3855 int free_page = 1;
3856
3857 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3858 free_page = 0;
3859 if (cmd->se_dev->transport->do_se_mem_map)
3860 free_page = 0;
3861
3862 list_for_each_entry_safe(se_mem, se_mem_tmp,
3863 &cmd->t_mem_list, se_list) {
3864 /*
3865 * We only release call __free_page(struct se_mem->se_page) when
3866 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3867 */
3868 if (free_page)
3869 __free_page(se_mem->se_page);
3870
3871 list_del(&se_mem->se_list);
3872 kmem_cache_free(se_mem_cache, se_mem);
3873 }
3874 cmd->t_tasks_se_num = 0;
3875
3876 list_for_each_entry_safe(se_mem, se_mem_tmp,
3877 &cmd->t_mem_bidi_list, se_list) {
3878 /*
3879 * We only release call __free_page(struct se_mem->se_page) when
3880 * SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC is NOT in use,
3881 */
3882 if (free_page)
3883 __free_page(se_mem->se_page);
3884
3885 list_del(&se_mem->se_list);
3886 kmem_cache_free(se_mem_cache, se_mem);
3887 }
3888 cmd->t_tasks_se_bidi_num = 0;
3889 }
3890
3891 static inline void transport_release_tasks(struct se_cmd *cmd)
3892 {
3893 transport_free_dev_tasks(cmd);
3894 }
3895
3896 static inline int transport_dec_and_check(struct se_cmd *cmd)
3897 {
3898 unsigned long flags;
3899
3900 spin_lock_irqsave(&cmd->t_state_lock, flags);
3901 if (atomic_read(&cmd->t_fe_count)) {
3902 if (!(atomic_dec_and_test(&cmd->t_fe_count))) {
3903 spin_unlock_irqrestore(&cmd->t_state_lock,
3904 flags);
3905 return 1;
3906 }
3907 }
3908
3909 if (atomic_read(&cmd->t_se_count)) {
3910 if (!(atomic_dec_and_test(&cmd->t_se_count))) {
3911 spin_unlock_irqrestore(&cmd->t_state_lock,
3912 flags);
3913 return 1;
3914 }
3915 }
3916 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3917
3918 return 0;
3919 }
3920
3921 static void transport_release_fe_cmd(struct se_cmd *cmd)
3922 {
3923 unsigned long flags;
3924
3925 if (transport_dec_and_check(cmd))
3926 return;
3927
3928 spin_lock_irqsave(&cmd->t_state_lock, flags);
3929 if (!(atomic_read(&cmd->transport_dev_active))) {
3930 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3931 goto free_pages;
3932 }
3933 atomic_set(&cmd->transport_dev_active, 0);
3934 transport_all_task_dev_remove_state(cmd);
3935 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3936
3937 transport_release_tasks(cmd);
3938 free_pages:
3939 transport_free_pages(cmd);
3940 transport_free_se_cmd(cmd);
3941 cmd->se_tfo->release_cmd(cmd);
3942 }
3943
3944 static int
3945 transport_generic_remove(struct se_cmd *cmd, int session_reinstatement)
3946 {
3947 unsigned long flags;
3948
3949 if (transport_dec_and_check(cmd)) {
3950 if (session_reinstatement) {
3951 spin_lock_irqsave(&cmd->t_state_lock, flags);
3952 transport_all_task_dev_remove_state(cmd);
3953 spin_unlock_irqrestore(&cmd->t_state_lock,
3954 flags);
3955 }
3956 return 1;
3957 }
3958
3959 spin_lock_irqsave(&cmd->t_state_lock, flags);
3960 if (!(atomic_read(&cmd->transport_dev_active))) {
3961 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3962 goto free_pages;
3963 }
3964 atomic_set(&cmd->transport_dev_active, 0);
3965 transport_all_task_dev_remove_state(cmd);
3966 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3967
3968 transport_release_tasks(cmd);
3969
3970 free_pages:
3971 transport_free_pages(cmd);
3972 transport_release_cmd(cmd);
3973 return 0;
3974 }
3975
3976 /*
3977 * transport_generic_map_mem_to_cmd - Perform SGL -> struct se_mem map
3978 * @cmd: Associated se_cmd descriptor
3979 * @mem: SGL style memory for TCM WRITE / READ
3980 * @sg_mem_num: Number of SGL elements
3981 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3982 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3983 *
3984 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3985 * of parameters.
3986 */
3987 int transport_generic_map_mem_to_cmd(
3988 struct se_cmd *cmd,
3989 struct scatterlist *sgl,
3990 u32 sgl_count,
3991 struct scatterlist *sgl_bidi,
3992 u32 sgl_bidi_count)
3993 {
3994 int ret;
3995
3996 if (!sgl || !sgl_count)
3997 return 0;
3998
3999 /*
4000 * Convert sgls (sgl, sgl_bidi) to list of se_mems
4001 */
4002 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
4003 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
4004 /*
4005 * For CDB using TCM struct se_mem linked list scatterlist memory
4006 * processed into a TCM struct se_subsystem_dev, we do the mapping
4007 * from the passed physical memory to struct se_mem->se_page here.
4008 */
4009 ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_list, sgl);
4010 if (ret < 0)
4011 return -ENOMEM;
4012
4013 cmd->t_tasks_se_num = ret;
4014 /*
4015 * Setup BIDI READ list of struct se_mem elements
4016 */
4017 if (sgl_bidi && sgl_bidi_count) {
4018 ret = transport_map_sg_to_mem(cmd, &cmd->t_mem_bidi_list, sgl_bidi);
4019 if (ret < 0)
4020 return -ENOMEM;
4021
4022 cmd->t_tasks_se_bidi_num = ret;
4023 }
4024 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
4025 }
4026
4027 return 0;
4028 }
4029 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
4030
4031
4032 static inline long long transport_dev_end_lba(struct se_device *dev)
4033 {
4034 return dev->transport->get_blocks(dev) + 1;
4035 }
4036
4037 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
4038 {
4039 struct se_device *dev = cmd->se_dev;
4040 u32 sectors;
4041
4042 if (dev->transport->get_device_type(dev) != TYPE_DISK)
4043 return 0;
4044
4045 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
4046
4047 if ((cmd->t_task_lba + sectors) >
4048 transport_dev_end_lba(dev)) {
4049 printk(KERN_ERR "LBA: %llu Sectors: %u exceeds"
4050 " transport_dev_end_lba(): %llu\n",
4051 cmd->t_task_lba, sectors,
4052 transport_dev_end_lba(dev));
4053 return 0;
4054 }
4055
4056 return sectors;
4057 }
4058
4059 static int transport_new_cmd_obj(struct se_cmd *cmd)
4060 {
4061 struct se_device *dev = cmd->se_dev;
4062 u32 task_cdbs;
4063 u32 rc;
4064
4065 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
4066 task_cdbs = 1;
4067 cmd->t_task_list_num = 1;
4068 } else {
4069 int set_counts = 1;
4070
4071 /*
4072 * Setup any BIDI READ tasks and memory from
4073 * cmd->t_mem_bidi_list so the READ struct se_tasks
4074 * are queued first for the non pSCSI passthrough case.
4075 */
4076 if (!list_empty(&cmd->t_mem_bidi_list) &&
4077 (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
4078 rc = transport_allocate_tasks(cmd,
4079 cmd->t_task_lba,
4080 transport_cmd_get_valid_sectors(cmd),
4081 DMA_FROM_DEVICE, &cmd->t_mem_bidi_list,
4082 set_counts);
4083 if (!(rc)) {
4084 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4085 cmd->scsi_sense_reason =
4086 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4087 return PYX_TRANSPORT_LU_COMM_FAILURE;
4088 }
4089 set_counts = 0;
4090 }
4091 /*
4092 * Setup the tasks and memory from cmd->t_mem_list
4093 * Note for BIDI transfers this will contain the WRITE payload
4094 */
4095 task_cdbs = transport_allocate_tasks(cmd,
4096 cmd->t_task_lba,
4097 transport_cmd_get_valid_sectors(cmd),
4098 cmd->data_direction, &cmd->t_mem_list,
4099 set_counts);
4100 if (!(task_cdbs)) {
4101 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
4102 cmd->scsi_sense_reason =
4103 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
4104 return PYX_TRANSPORT_LU_COMM_FAILURE;
4105 }
4106 cmd->t_task_list_num = task_cdbs;
4107
4108 #if 0
4109 printk(KERN_INFO "data_length: %u, LBA: %llu t_tasks_sectors:"
4110 " %u, t_task_cdbs: %u\n", obj_ptr, cmd->data_length,
4111 cmd->t_task_lba, cmd->t_tasks_sectors,
4112 cmd->t_task_cdbs);
4113 #endif
4114 }
4115
4116 atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
4117 atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
4118 atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
4119 return 0;
4120 }
4121
4122 void *transport_kmap_first_data_page(struct se_cmd *cmd)
4123 {
4124 struct se_mem *se_mem;
4125
4126 BUG_ON(list_empty(&cmd->t_mem_list));
4127
4128 se_mem = list_first_entry(&cmd->t_mem_list, struct se_mem, se_list);
4129
4130 /*
4131 * 1st se_mem should point to a page, and we shouldn't need more than
4132 * that for this cmd
4133 */
4134 BUG_ON(cmd->data_length > PAGE_SIZE);
4135
4136 return kmap(se_mem->se_page);
4137 }
4138 EXPORT_SYMBOL(transport_kmap_first_data_page);
4139
4140 void transport_kunmap_first_data_page(struct se_cmd *cmd)
4141 {
4142 struct se_mem *se_mem;
4143
4144 BUG_ON(list_empty(&cmd->t_mem_list));
4145
4146 se_mem = list_first_entry(&cmd->t_mem_list, struct se_mem, se_list);
4147
4148 kunmap(se_mem->se_page);
4149 }
4150 EXPORT_SYMBOL(transport_kunmap_first_data_page);
4151
4152 static int
4153 transport_generic_get_mem(struct se_cmd *cmd)
4154 {
4155 struct se_mem *se_mem;
4156 int length = cmd->data_length;
4157
4158 /*
4159 * If the device uses memory mapping this is enough.
4160 */
4161 if (cmd->se_dev->transport->do_se_mem_map)
4162 return 0;
4163
4164 /* Even cmds with length 0 will get here, btw */
4165 while (length) {
4166 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4167 if (!(se_mem)) {
4168 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4169 goto out;
4170 }
4171
4172 /* #warning FIXME Allocate contigous pages for struct se_mem elements */
4173 se_mem->se_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 0);
4174 if (!(se_mem->se_page)) {
4175 printk(KERN_ERR "alloc_pages() failed\n");
4176 goto out;
4177 }
4178
4179 INIT_LIST_HEAD(&se_mem->se_list);
4180 se_mem->se_len = min_t(u32, length, PAGE_SIZE);
4181 list_add_tail(&se_mem->se_list, &cmd->t_mem_list);
4182 cmd->t_tasks_se_num++;
4183
4184 DEBUG_MEM("Allocated struct se_mem page(%p) Length(%u)"
4185 " Offset(%u)\n", se_mem->se_page, se_mem->se_len,
4186 se_mem->se_off);
4187
4188 length -= se_mem->se_len;
4189 }
4190
4191 DEBUG_MEM("Allocated total struct se_mem elements(%u)\n",
4192 cmd->t_tasks_se_num);
4193
4194 return 0;
4195 out:
4196 if (se_mem)
4197 __free_pages(se_mem->se_page, 0);
4198 kmem_cache_free(se_mem_cache, se_mem);
4199 return -ENOMEM;
4200 }
4201
4202 int transport_init_task_sg(
4203 struct se_task *task,
4204 struct se_mem *in_se_mem,
4205 u32 task_offset)
4206 {
4207 struct se_cmd *se_cmd = task->task_se_cmd;
4208 struct se_device *se_dev = se_cmd->se_dev;
4209 struct se_mem *se_mem = in_se_mem;
4210 struct target_core_fabric_ops *tfo = se_cmd->se_tfo;
4211 u32 sg_length, task_size = task->task_size, task_sg_num_padded;
4212
4213 while (task_size != 0) {
4214 DEBUG_SC("se_mem->se_page(%p) se_mem->se_len(%u)"
4215 " se_mem->se_off(%u) task_offset(%u)\n",
4216 se_mem->se_page, se_mem->se_len,
4217 se_mem->se_off, task_offset);
4218
4219 if (task_offset == 0) {
4220 if (task_size >= se_mem->se_len) {
4221 sg_length = se_mem->se_len;
4222
4223 if (!(list_is_last(&se_mem->se_list,
4224 &se_cmd->t_mem_list)))
4225 se_mem = list_entry(se_mem->se_list.next,
4226 struct se_mem, se_list);
4227 } else {
4228 sg_length = task_size;
4229 task_size -= sg_length;
4230 goto next;
4231 }
4232
4233 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4234 sg_length, task_size);
4235 } else {
4236 if ((se_mem->se_len - task_offset) > task_size) {
4237 sg_length = task_size;
4238 task_size -= sg_length;
4239 goto next;
4240 } else {
4241 sg_length = (se_mem->se_len - task_offset);
4242
4243 if (!(list_is_last(&se_mem->se_list,
4244 &se_cmd->t_mem_list)))
4245 se_mem = list_entry(se_mem->se_list.next,
4246 struct se_mem, se_list);
4247 }
4248
4249 DEBUG_SC("sg_length(%u) task_size(%u)\n",
4250 sg_length, task_size);
4251
4252 task_offset = 0;
4253 }
4254 task_size -= sg_length;
4255 next:
4256 DEBUG_SC("task[%u] - Reducing task_size to(%u)\n",
4257 task->task_no, task_size);
4258
4259 task->task_sg_num++;
4260 }
4261 /*
4262 * Check if the fabric module driver is requesting that all
4263 * struct se_task->task_sg[] be chained together.. If so,
4264 * then allocate an extra padding SG entry for linking and
4265 * marking the end of the chained SGL.
4266 */
4267 if (tfo->task_sg_chaining) {
4268 task_sg_num_padded = (task->task_sg_num + 1);
4269 task->task_padded_sg = 1;
4270 } else
4271 task_sg_num_padded = task->task_sg_num;
4272
4273 task->task_sg = kzalloc(task_sg_num_padded *
4274 sizeof(struct scatterlist), GFP_KERNEL);
4275 if (!(task->task_sg)) {
4276 printk(KERN_ERR "Unable to allocate memory for"
4277 " task->task_sg\n");
4278 return -ENOMEM;
4279 }
4280 sg_init_table(&task->task_sg[0], task_sg_num_padded);
4281 /*
4282 * Setup task->task_sg_bidi for SCSI READ payload for
4283 * TCM/pSCSI passthrough if present for BIDI-COMMAND
4284 */
4285 if (!list_empty(&se_cmd->t_mem_bidi_list) &&
4286 (se_dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)) {
4287 task->task_sg_bidi = kzalloc(task_sg_num_padded *
4288 sizeof(struct scatterlist), GFP_KERNEL);
4289 if (!(task->task_sg_bidi)) {
4290 kfree(task->task_sg);
4291 task->task_sg = NULL;
4292 printk(KERN_ERR "Unable to allocate memory for"
4293 " task->task_sg_bidi\n");
4294 return -ENOMEM;
4295 }
4296 sg_init_table(&task->task_sg_bidi[0], task_sg_num_padded);
4297 }
4298 /*
4299 * For the chaining case, setup the proper end of SGL for the
4300 * initial submission struct task into struct se_subsystem_api.
4301 * This will be cleared later by transport_do_task_sg_chain()
4302 */
4303 if (task->task_padded_sg) {
4304 sg_mark_end(&task->task_sg[task->task_sg_num - 1]);
4305 /*
4306 * Added the 'if' check before marking end of bi-directional
4307 * scatterlist (which gets created only in case of request
4308 * (RD + WR).
4309 */
4310 if (task->task_sg_bidi)
4311 sg_mark_end(&task->task_sg_bidi[task->task_sg_num - 1]);
4312 }
4313
4314 DEBUG_SC("Successfully allocated task->task_sg_num(%u),"
4315 " task_sg_num_padded(%u)\n", task->task_sg_num,
4316 task_sg_num_padded);
4317
4318 return task->task_sg_num;
4319 }
4320
4321 /* Reduce sectors if they are too long for the device */
4322 static inline sector_t transport_limit_task_sectors(
4323 struct se_device *dev,
4324 unsigned long long lba,
4325 sector_t sectors)
4326 {
4327 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
4328
4329 if (dev->transport->get_device_type(dev) == TYPE_DISK)
4330 if ((lba + sectors) > transport_dev_end_lba(dev))
4331 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
4332
4333 return sectors;
4334 }
4335
4336 /*
4337 * Convert a sgl into a linked list of se_mems.
4338 */
4339 static int transport_map_sg_to_mem(
4340 struct se_cmd *cmd,
4341 struct list_head *se_mem_list,
4342 struct scatterlist *sg)
4343 {
4344 struct se_mem *se_mem;
4345 u32 cmd_size = cmd->data_length;
4346 int sg_count = 0;
4347
4348 WARN_ON(!sg);
4349
4350 while (cmd_size) {
4351 /*
4352 * NOTE: it is safe to return -ENOMEM at any time in creating this
4353 * list because transport_free_pages() will eventually be called, and is
4354 * smart enough to deallocate all list items for sg and sg_bidi lists.
4355 */
4356 se_mem = kmem_cache_zalloc(se_mem_cache, GFP_KERNEL);
4357 if (!(se_mem)) {
4358 printk(KERN_ERR "Unable to allocate struct se_mem\n");
4359 return -ENOMEM;
4360 }
4361 INIT_LIST_HEAD(&se_mem->se_list);
4362 DEBUG_MEM("sg_to_mem: Starting loop with cmd_size: %u"
4363 " sg_page: %p offset: %d length: %d\n", cmd_size,
4364 sg_page(sg), sg->offset, sg->length);
4365
4366 se_mem->se_page = sg_page(sg);
4367 se_mem->se_off = sg->offset;
4368
4369 if (cmd_size > sg->length) {
4370 se_mem->se_len = sg->length;
4371 sg = sg_next(sg);
4372 } else
4373 se_mem->se_len = cmd_size;
4374
4375 cmd_size -= se_mem->se_len;
4376 sg_count++;
4377
4378 DEBUG_MEM("sg_to_mem: sg_count: %u cmd_size: %u\n",
4379 sg_count, cmd_size);
4380 DEBUG_MEM("sg_to_mem: Final se_page: %p se_off: %d se_len: %d\n",
4381 se_mem->se_page, se_mem->se_off, se_mem->se_len);
4382
4383 list_add_tail(&se_mem->se_list, se_mem_list);
4384 }
4385
4386 DEBUG_MEM("task[0] - Mapped(%u) struct scatterlist segments\n", sg_count);
4387
4388 return sg_count;
4389 }
4390
4391 /* transport_map_mem_to_sg():
4392 *
4393 *
4394 */
4395 int transport_map_mem_to_sg(
4396 struct se_task *task,
4397 struct list_head *se_mem_list,
4398 struct scatterlist *sg,
4399 struct se_mem *in_se_mem,
4400 struct se_mem **out_se_mem,
4401 u32 *se_mem_cnt,
4402 u32 *task_offset)
4403 {
4404 struct se_cmd *se_cmd = task->task_se_cmd;
4405 struct se_mem *se_mem = in_se_mem;
4406 u32 task_size = task->task_size, sg_no = 0;
4407
4408 if (!sg) {
4409 printk(KERN_ERR "Unable to locate valid struct"
4410 " scatterlist pointer\n");
4411 return -EINVAL;
4412 }
4413
4414 while (task_size != 0) {
4415 /*
4416 * Setup the contiguous array of scatterlists for
4417 * this struct se_task.
4418 */
4419 sg_assign_page(sg, se_mem->se_page);
4420
4421 if (*task_offset == 0) {
4422 sg->offset = se_mem->se_off;
4423
4424 if (task_size >= se_mem->se_len) {
4425 sg->length = se_mem->se_len;
4426
4427 if (!(list_is_last(&se_mem->se_list,
4428 &se_cmd->t_mem_list))) {
4429 se_mem = list_entry(se_mem->se_list.next,
4430 struct se_mem, se_list);
4431 (*se_mem_cnt)++;
4432 }
4433 } else {
4434 sg->length = task_size;
4435 /*
4436 * Determine if we need to calculate an offset
4437 * into the struct se_mem on the next go around..
4438 */
4439 task_size -= sg->length;
4440 if (!(task_size))
4441 *task_offset = sg->length;
4442
4443 goto next;
4444 }
4445
4446 } else {
4447 sg->offset = (*task_offset + se_mem->se_off);
4448
4449 if ((se_mem->se_len - *task_offset) > task_size) {
4450 sg->length = task_size;
4451 /*
4452 * Determine if we need to calculate an offset
4453 * into the struct se_mem on the next go around..
4454 */
4455 task_size -= sg->length;
4456 if (!(task_size))
4457 *task_offset += sg->length;
4458
4459 goto next;
4460 } else {
4461 sg->length = (se_mem->se_len - *task_offset);
4462
4463 if (!(list_is_last(&se_mem->se_list,
4464 &se_cmd->t_mem_list))) {
4465 se_mem = list_entry(se_mem->se_list.next,
4466 struct se_mem, se_list);
4467 (*se_mem_cnt)++;
4468 }
4469 }
4470
4471 *task_offset = 0;
4472 }
4473 task_size -= sg->length;
4474 next:
4475 DEBUG_MEM("task[%u] mem_to_sg - sg[%u](%p)(%u)(%u) - Reducing"
4476 " task_size to(%u), task_offset: %u\n", task->task_no, sg_no,
4477 sg_page(sg), sg->length, sg->offset, task_size, *task_offset);
4478
4479 sg_no++;
4480 if (!(task_size))
4481 break;
4482
4483 sg = sg_next(sg);
4484
4485 if (task_size > se_cmd->data_length)
4486 BUG();
4487 }
4488 *out_se_mem = se_mem;
4489
4490 DEBUG_MEM("task[%u] - Mapped(%u) struct se_mem segments to total(%u)"
4491 " SGs\n", task->task_no, *se_mem_cnt, sg_no);
4492
4493 return 0;
4494 }
4495
4496 /*
4497 * This function can be used by HW target mode drivers to create a linked
4498 * scatterlist from all contiguously allocated struct se_task->task_sg[].
4499 * This is intended to be called during the completion path by TCM Core
4500 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
4501 */
4502 void transport_do_task_sg_chain(struct se_cmd *cmd)
4503 {
4504 struct scatterlist *sg_head = NULL, *sg_link = NULL, *sg_first = NULL;
4505 struct scatterlist *sg_head_cur = NULL, *sg_link_cur = NULL;
4506 struct scatterlist *sg, *sg_end = NULL, *sg_end_cur = NULL;
4507 struct se_task *task;
4508 struct target_core_fabric_ops *tfo = cmd->se_tfo;
4509 u32 task_sg_num = 0, sg_count = 0;
4510 int i;
4511
4512 if (tfo->task_sg_chaining == 0) {
4513 printk(KERN_ERR "task_sg_chaining is diabled for fabric module:"
4514 " %s\n", tfo->get_fabric_name());
4515 dump_stack();
4516 return;
4517 }
4518 /*
4519 * Walk the struct se_task list and setup scatterlist chains
4520 * for each contiguously allocated struct se_task->task_sg[].
4521 */
4522 list_for_each_entry(task, &cmd->t_task_list, t_list) {
4523 if (!(task->task_sg) || !(task->task_padded_sg))
4524 continue;
4525
4526 if (sg_head && sg_link) {
4527 sg_head_cur = &task->task_sg[0];
4528 sg_link_cur = &task->task_sg[task->task_sg_num];
4529 /*
4530 * Either add chain or mark end of scatterlist
4531 */
4532 if (!(list_is_last(&task->t_list,
4533 &cmd->t_task_list))) {
4534 /*
4535 * Clear existing SGL termination bit set in
4536 * transport_init_task_sg(), see sg_mark_end()
4537 */
4538 sg_end_cur = &task->task_sg[task->task_sg_num - 1];
4539 sg_end_cur->page_link &= ~0x02;
4540
4541 sg_chain(sg_head, task_sg_num, sg_head_cur);
4542 sg_count += task->task_sg_num;
4543 task_sg_num = (task->task_sg_num + 1);
4544 } else {
4545 sg_chain(sg_head, task_sg_num, sg_head_cur);
4546 sg_count += task->task_sg_num;
4547 task_sg_num = task->task_sg_num;
4548 }
4549
4550 sg_head = sg_head_cur;
4551 sg_link = sg_link_cur;
4552 continue;
4553 }
4554 sg_head = sg_first = &task->task_sg[0];
4555 sg_link = &task->task_sg[task->task_sg_num];
4556 /*
4557 * Check for single task..
4558 */
4559 if (!(list_is_last(&task->t_list, &cmd->t_task_list))) {
4560 /*
4561 * Clear existing SGL termination bit set in
4562 * transport_init_task_sg(), see sg_mark_end()
4563 */
4564 sg_end = &task->task_sg[task->task_sg_num - 1];
4565 sg_end->page_link &= ~0x02;
4566 sg_count += task->task_sg_num;
4567 task_sg_num = (task->task_sg_num + 1);
4568 } else {
4569 sg_count += task->task_sg_num;
4570 task_sg_num = task->task_sg_num;
4571 }
4572 }
4573 /*
4574 * Setup the starting pointer and total t_tasks_sg_linked_no including
4575 * padding SGs for linking and to mark the end.
4576 */
4577 cmd->t_tasks_sg_chained = sg_first;
4578 cmd->t_tasks_sg_chained_no = sg_count;
4579
4580 DEBUG_CMD_M("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
4581 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
4582 cmd->t_tasks_sg_chained_no);
4583
4584 for_each_sg(cmd->t_tasks_sg_chained, sg,
4585 cmd->t_tasks_sg_chained_no, i) {
4586
4587 DEBUG_CMD_M("SG[%d]: %p page: %p length: %d offset: %d\n",
4588 i, sg, sg_page(sg), sg->length, sg->offset);
4589 if (sg_is_chain(sg))
4590 DEBUG_CMD_M("SG: %p sg_is_chain=1\n", sg);
4591 if (sg_is_last(sg))
4592 DEBUG_CMD_M("SG: %p sg_is_last=1\n", sg);
4593 }
4594 }
4595 EXPORT_SYMBOL(transport_do_task_sg_chain);
4596
4597 static int transport_do_se_mem_map(
4598 struct se_device *dev,
4599 struct se_task *task,
4600 struct list_head *se_mem_list,
4601 void *in_mem,
4602 struct se_mem *in_se_mem,
4603 struct se_mem **out_se_mem,
4604 u32 *se_mem_cnt,
4605 u32 *task_offset_in)
4606 {
4607 u32 task_offset = *task_offset_in;
4608 int ret = 0;
4609 /*
4610 * se_subsystem_api_t->do_se_mem_map is used when internal allocation
4611 * has been done by the transport plugin.
4612 */
4613 if (dev->transport->do_se_mem_map) {
4614 ret = dev->transport->do_se_mem_map(task, se_mem_list,
4615 in_mem, in_se_mem, out_se_mem, se_mem_cnt,
4616 task_offset_in);
4617 if (ret == 0)
4618 task->task_se_cmd->t_tasks_se_num += *se_mem_cnt;
4619
4620 return ret;
4621 }
4622
4623 BUG_ON(list_empty(se_mem_list));
4624 /*
4625 * This is the normal path for all normal non BIDI and BIDI-COMMAND
4626 * WRITE payloads.. If we need to do BIDI READ passthrough for
4627 * TCM/pSCSI the first call to transport_do_se_mem_map ->
4628 * transport_init_task_sg() -> transport_map_mem_to_sg() will do the
4629 * allocation for task->task_sg_bidi, and the subsequent call to
4630 * transport_do_se_mem_map() from transport_generic_get_cdb_count()
4631 */
4632 if (!(task->task_sg_bidi)) {
4633 /*
4634 * Assume default that transport plugin speaks preallocated
4635 * scatterlists.
4636 */
4637 ret = transport_init_task_sg(task, in_se_mem, task_offset);
4638 if (ret <= 0)
4639 return ret;
4640 /*
4641 * struct se_task->task_sg now contains the struct scatterlist array.
4642 */
4643 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg,
4644 in_se_mem, out_se_mem, se_mem_cnt,
4645 task_offset_in);
4646 }
4647 /*
4648 * Handle the se_mem_list -> struct task->task_sg_bidi
4649 * memory map for the extra BIDI READ payload
4650 */
4651 return transport_map_mem_to_sg(task, se_mem_list, task->task_sg_bidi,
4652 in_se_mem, out_se_mem, se_mem_cnt,
4653 task_offset_in);
4654 }
4655
4656 /*
4657 * Break up cmd into chunks transport can handle
4658 */
4659 static u32 transport_allocate_tasks(
4660 struct se_cmd *cmd,
4661 unsigned long long lba,
4662 u32 sectors,
4663 enum dma_data_direction data_direction,
4664 struct list_head *mem_list,
4665 int set_counts)
4666 {
4667 unsigned char *cdb = NULL;
4668 struct se_task *task;
4669 struct se_mem *se_mem = NULL;
4670 struct se_mem *se_mem_lout = NULL;
4671 struct se_mem *se_mem_bidi = NULL;
4672 struct se_mem *se_mem_bidi_lout = NULL;
4673 struct se_device *dev = cmd->se_dev;
4674 int ret;
4675 u32 task_offset_in = 0;
4676 u32 se_mem_cnt = 0;
4677 u32 se_mem_bidi_cnt = 0;
4678 u32 task_cdbs = 0;
4679
4680 BUG_ON(!mem_list);
4681 /*
4682 * While using RAMDISK_DR backstores is the only case where
4683 * mem_list will ever be empty at this point.
4684 */
4685 if (!(list_empty(mem_list)))
4686 se_mem = list_first_entry(mem_list, struct se_mem, se_list);
4687 /*
4688 * Check for extra se_mem_bidi mapping for BIDI-COMMANDs to
4689 * struct se_task->task_sg_bidi for TCM/pSCSI passthrough operation
4690 */
4691 if (!list_empty(&cmd->t_mem_bidi_list) &&
4692 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV))
4693 se_mem_bidi = list_first_entry(&cmd->t_mem_bidi_list,
4694 struct se_mem, se_list);
4695
4696 while (sectors) {
4697 sector_t limited_sectors;
4698
4699 DEBUG_VOL("ITT[0x%08x] LBA(%llu) SectorsLeft(%u) EOBJ(%llu)\n",
4700 cmd->se_tfo->get_task_tag(cmd), lba, sectors,
4701 transport_dev_end_lba(dev));
4702
4703 limited_sectors = transport_limit_task_sectors(dev, lba, sectors);
4704 if (!limited_sectors)
4705 break;
4706
4707 task = transport_generic_get_task(cmd, data_direction);
4708 if (!task)
4709 goto out;
4710
4711 task->task_lba = lba;
4712 task->task_sectors = limited_sectors;
4713 lba += task->task_sectors;
4714 sectors -= task->task_sectors;
4715 task->task_size = (task->task_sectors *
4716 dev->se_sub_dev->se_dev_attrib.block_size);
4717
4718 cdb = dev->transport->get_cdb(task);
4719 /* Should be part of task, can't fail */
4720 BUG_ON(!cdb);
4721
4722 memcpy(cdb, cmd->t_task_cdb,
4723 scsi_command_size(cmd->t_task_cdb));
4724
4725 /* Update new cdb with updated lba/sectors */
4726 cmd->transport_split_cdb(task->task_lba,
4727 &task->task_sectors, cdb);
4728
4729 /*
4730 * Perform the SE OBJ plugin and/or Transport plugin specific
4731 * mapping for cmd->t_mem_list. And setup the
4732 * task->task_sg and if necessary task->task_sg_bidi
4733 */
4734 ret = transport_do_se_mem_map(dev, task, mem_list,
4735 NULL, se_mem, &se_mem_lout, &se_mem_cnt,
4736 &task_offset_in);
4737 if (ret < 0)
4738 goto out;
4739
4740 se_mem = se_mem_lout;
4741 /*
4742 * Setup the cmd->t_mem_bidi_list -> task->task_sg_bidi
4743 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI
4744 *
4745 * Note that the first call to transport_do_se_mem_map() above will
4746 * allocate struct se_task->task_sg_bidi in transport_do_se_mem_map()
4747 * -> transport_init_task_sg(), and the second here will do the
4748 * mapping for SCSI READ for BIDI-COMMAND passthrough with TCM/pSCSI.
4749 */
4750 if (task->task_sg_bidi != NULL) {
4751 ret = transport_do_se_mem_map(dev, task,
4752 &cmd->t_mem_bidi_list, NULL,
4753 se_mem_bidi, &se_mem_bidi_lout, &se_mem_bidi_cnt,
4754 &task_offset_in);
4755 if (ret < 0)
4756 goto out;
4757
4758 se_mem_bidi = se_mem_bidi_lout;
4759 }
4760 task_cdbs++;
4761
4762 DEBUG_VOL("Incremented task_cdbs(%u) task->task_sg_num(%u)\n",
4763 task_cdbs, task->task_sg_num);
4764 }
4765
4766 if (set_counts) {
4767 atomic_inc(&cmd->t_fe_count);
4768 atomic_inc(&cmd->t_se_count);
4769 }
4770
4771 DEBUG_VOL("ITT[0x%08x] total %s cdbs(%u)\n",
4772 cmd->se_tfo->get_task_tag(cmd), (data_direction == DMA_TO_DEVICE)
4773 ? "DMA_TO_DEVICE" : "DMA_FROM_DEVICE", task_cdbs);
4774
4775 return task_cdbs;
4776 out:
4777 return 0;
4778 }
4779
4780 static int
4781 transport_map_control_cmd_to_task(struct se_cmd *cmd)
4782 {
4783 struct se_device *dev = cmd->se_dev;
4784 unsigned char *cdb;
4785 struct se_task *task;
4786 int ret;
4787
4788 task = transport_generic_get_task(cmd, cmd->data_direction);
4789 if (!task)
4790 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
4791
4792 cdb = dev->transport->get_cdb(task);
4793 BUG_ON(!cdb);
4794 memcpy(cdb, cmd->t_task_cdb,
4795 scsi_command_size(cmd->t_task_cdb));
4796
4797 task->task_size = cmd->data_length;
4798 task->task_sg_num =
4799 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) ? 1 : 0;
4800
4801 atomic_inc(&cmd->t_fe_count);
4802 atomic_inc(&cmd->t_se_count);
4803
4804 if (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) {
4805 struct se_mem *se_mem = NULL, *se_mem_lout = NULL;
4806 u32 se_mem_cnt = 0, task_offset = 0;
4807
4808 if (!list_empty(&cmd->t_mem_list))
4809 se_mem = list_first_entry(&cmd->t_mem_list,
4810 struct se_mem, se_list);
4811
4812 ret = transport_do_se_mem_map(dev, task,
4813 &cmd->t_mem_list, NULL, se_mem,
4814 &se_mem_lout, &se_mem_cnt, &task_offset);
4815 if (ret < 0)
4816 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
4817
4818 if (dev->transport->map_task_SG)
4819 return dev->transport->map_task_SG(task);
4820 return 0;
4821 } else if (cmd->se_cmd_flags & SCF_SCSI_NON_DATA_CDB) {
4822 if (dev->transport->cdb_none)
4823 return dev->transport->cdb_none(task);
4824 return 0;
4825 } else {
4826 BUG();
4827 return PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES;
4828 }
4829 }
4830
4831 /* transport_generic_new_cmd(): Called from transport_processing_thread()
4832 *
4833 * Allocate storage transport resources from a set of values predefined
4834 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4835 * Any non zero return here is treated as an "out of resource' op here.
4836 */
4837 /*
4838 * Generate struct se_task(s) and/or their payloads for this CDB.
4839 */
4840 int transport_generic_new_cmd(struct se_cmd *cmd)
4841 {
4842 struct se_task *task;
4843 struct se_device *dev = cmd->se_dev;
4844 int ret = 0;
4845
4846 /*
4847 * Determine is the TCM fabric module has already allocated physical
4848 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4849 * to setup beforehand the linked list of physical memory at
4850 * cmd->t_mem_list of struct se_mem->se_page
4851 */
4852 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)) {
4853 ret = transport_generic_get_mem(cmd);
4854 if (ret < 0)
4855 return ret;
4856 }
4857
4858 ret = transport_new_cmd_obj(cmd);
4859 if (ret < 0)
4860 return ret;
4861
4862 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
4863 list_for_each_entry(task, &cmd->t_task_list, t_list) {
4864 if (atomic_read(&task->task_sent))
4865 continue;
4866 if (!dev->transport->map_task_SG)
4867 continue;
4868
4869 ret = dev->transport->map_task_SG(task);
4870 if (ret < 0)
4871 return ret;
4872 }
4873 } else {
4874 ret = transport_map_control_cmd_to_task(cmd);
4875 if (ret < 0)
4876 return ret;
4877 }
4878
4879 /*
4880 * For WRITEs, let the fabric know its buffer is ready..
4881 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4882 * will be added to the struct se_device execution queue after its WRITE
4883 * data has arrived. (ie: It gets handled by the transport processing
4884 * thread a second time)
4885 */
4886 if (cmd->data_direction == DMA_TO_DEVICE) {
4887 transport_add_tasks_to_state_queue(cmd);
4888 return transport_generic_write_pending(cmd);
4889 }
4890 /*
4891 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4892 * to the execution queue.
4893 */
4894 transport_execute_tasks(cmd);
4895 return 0;
4896 }
4897 EXPORT_SYMBOL(transport_generic_new_cmd);
4898
4899 /* transport_generic_process_write():
4900 *
4901 *
4902 */
4903 void transport_generic_process_write(struct se_cmd *cmd)
4904 {
4905 #if 0
4906 /*
4907 * Copy SCSI Presented DTL sector(s) from received buffers allocated to
4908 * original EDTL
4909 */
4910 if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
4911 if (!cmd->t_tasks_se_num) {
4912 unsigned char *dst, *buf =
4913 (unsigned char *)cmd->t_task_buf;
4914
4915 dst = kzalloc(cmd->cmd_spdtl), GFP_KERNEL);
4916 if (!(dst)) {
4917 printk(KERN_ERR "Unable to allocate memory for"
4918 " WRITE underflow\n");
4919 transport_generic_request_failure(cmd, NULL,
4920 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
4921 return;
4922 }
4923 memcpy(dst, buf, cmd->cmd_spdtl);
4924
4925 kfree(cmd->t_task_buf);
4926 cmd->t_task_buf = dst;
4927 } else {
4928 struct scatterlist *sg =
4929 (struct scatterlist *sg)cmd->t_task_buf;
4930 struct scatterlist *orig_sg;
4931
4932 orig_sg = kzalloc(sizeof(struct scatterlist) *
4933 cmd->t_tasks_se_num,
4934 GFP_KERNEL))) {
4935 if (!(orig_sg)) {
4936 printk(KERN_ERR "Unable to allocate memory"
4937 " for WRITE underflow\n");
4938 transport_generic_request_failure(cmd, NULL,
4939 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
4940 return;
4941 }
4942
4943 memcpy(orig_sg, cmd->t_task_buf,
4944 sizeof(struct scatterlist) *
4945 cmd->t_tasks_se_num);
4946
4947 cmd->data_length = cmd->cmd_spdtl;
4948 /*
4949 * FIXME, clear out original struct se_task and state
4950 * information.
4951 */
4952 if (transport_generic_new_cmd(cmd) < 0) {
4953 transport_generic_request_failure(cmd, NULL,
4954 PYX_TRANSPORT_REQ_TOO_MANY_SECTORS, 1);
4955 kfree(orig_sg);
4956 return;
4957 }
4958
4959 transport_memcpy_write_sg(cmd, orig_sg);
4960 }
4961 }
4962 #endif
4963 transport_execute_tasks(cmd);
4964 }
4965 EXPORT_SYMBOL(transport_generic_process_write);
4966
4967 static int transport_write_pending_qf(struct se_cmd *cmd)
4968 {
4969 return cmd->se_tfo->write_pending(cmd);
4970 }
4971
4972 /* transport_generic_write_pending():
4973 *
4974 *
4975 */
4976 static int transport_generic_write_pending(struct se_cmd *cmd)
4977 {
4978 unsigned long flags;
4979 int ret;
4980
4981 spin_lock_irqsave(&cmd->t_state_lock, flags);
4982 cmd->t_state = TRANSPORT_WRITE_PENDING;
4983 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4984
4985 if (cmd->transport_qf_callback) {
4986 ret = cmd->transport_qf_callback(cmd);
4987 if (ret == -EAGAIN)
4988 goto queue_full;
4989 else if (ret < 0)
4990 return ret;
4991
4992 cmd->transport_qf_callback = NULL;
4993 return 0;
4994 }
4995
4996 /*
4997 * Clear the se_cmd for WRITE_PENDING status in order to set
4998 * cmd->t_transport_active=0 so that transport_generic_handle_data
4999 * can be called from HW target mode interrupt code. This is safe
5000 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
5001 * because the se_cmd->se_lun pointer is not being cleared.
5002 */
5003 transport_cmd_check_stop(cmd, 1, 0);
5004
5005 /*
5006 * Call the fabric write_pending function here to let the
5007 * frontend know that WRITE buffers are ready.
5008 */
5009 ret = cmd->se_tfo->write_pending(cmd);
5010 if (ret == -EAGAIN)
5011 goto queue_full;
5012 else if (ret < 0)
5013 return ret;
5014
5015 return PYX_TRANSPORT_WRITE_PENDING;
5016
5017 queue_full:
5018 printk(KERN_INFO "Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
5019 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
5020 transport_handle_queue_full(cmd, cmd->se_dev,
5021 transport_write_pending_qf);
5022 return ret;
5023 }
5024
5025 void transport_release_cmd(struct se_cmd *cmd)
5026 {
5027 BUG_ON(!cmd->se_tfo);
5028
5029 transport_free_se_cmd(cmd);
5030 cmd->se_tfo->release_cmd(cmd);
5031 }
5032 EXPORT_SYMBOL(transport_release_cmd);
5033
5034 /* transport_generic_free_cmd():
5035 *
5036 * Called from processing frontend to release storage engine resources
5037 */
5038 void transport_generic_free_cmd(
5039 struct se_cmd *cmd,
5040 int wait_for_tasks,
5041 int session_reinstatement)
5042 {
5043 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD))
5044 transport_release_cmd(cmd);
5045 else {
5046 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
5047
5048 if (cmd->se_lun) {
5049 #if 0
5050 printk(KERN_INFO "cmd: %p ITT: 0x%08x contains"
5051 " cmd->se_lun\n", cmd,
5052 cmd->se_tfo->get_task_tag(cmd));
5053 #endif
5054 transport_lun_remove_cmd(cmd);
5055 }
5056
5057 if (wait_for_tasks && cmd->transport_wait_for_tasks)
5058 cmd->transport_wait_for_tasks(cmd, 0, 0);
5059
5060 transport_free_dev_tasks(cmd);
5061
5062 transport_generic_remove(cmd, session_reinstatement);
5063 }
5064 }
5065 EXPORT_SYMBOL(transport_generic_free_cmd);
5066
5067 static void transport_nop_wait_for_tasks(
5068 struct se_cmd *cmd,
5069 int remove_cmd,
5070 int session_reinstatement)
5071 {
5072 return;
5073 }
5074
5075 /* transport_lun_wait_for_tasks():
5076 *
5077 * Called from ConfigFS context to stop the passed struct se_cmd to allow
5078 * an struct se_lun to be successfully shutdown.
5079 */
5080 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
5081 {
5082 unsigned long flags;
5083 int ret;
5084 /*
5085 * If the frontend has already requested this struct se_cmd to
5086 * be stopped, we can safely ignore this struct se_cmd.
5087 */
5088 spin_lock_irqsave(&cmd->t_state_lock, flags);
5089 if (atomic_read(&cmd->t_transport_stop)) {
5090 atomic_set(&cmd->transport_lun_stop, 0);
5091 DEBUG_TRANSPORT_S("ConfigFS ITT[0x%08x] - t_transport_stop =="
5092 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
5093 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5094 transport_cmd_check_stop(cmd, 1, 0);
5095 return -EPERM;
5096 }
5097 atomic_set(&cmd->transport_lun_fe_stop, 1);
5098 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5099
5100 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
5101
5102 ret = transport_stop_tasks_for_cmd(cmd);
5103
5104 DEBUG_TRANSPORT_S("ConfigFS: cmd: %p t_task_cdbs: %d stop tasks ret:"
5105 " %d\n", cmd, cmd->t_task_cdbs, ret);
5106 if (!ret) {
5107 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
5108 cmd->se_tfo->get_task_tag(cmd));
5109 wait_for_completion(&cmd->transport_lun_stop_comp);
5110 DEBUG_TRANSPORT_S("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
5111 cmd->se_tfo->get_task_tag(cmd));
5112 }
5113 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
5114
5115 return 0;
5116 }
5117
5118 /* #define DEBUG_CLEAR_LUN */
5119 #ifdef DEBUG_CLEAR_LUN
5120 #define DEBUG_CLEAR_L(x...) printk(KERN_INFO x)
5121 #else
5122 #define DEBUG_CLEAR_L(x...)
5123 #endif
5124
5125 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
5126 {
5127 struct se_cmd *cmd = NULL;
5128 unsigned long lun_flags, cmd_flags;
5129 /*
5130 * Do exception processing and return CHECK_CONDITION status to the
5131 * Initiator Port.
5132 */
5133 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5134 while (!list_empty(&lun->lun_cmd_list)) {
5135 cmd = list_first_entry(&lun->lun_cmd_list,
5136 struct se_cmd, se_lun_node);
5137 list_del(&cmd->se_lun_node);
5138
5139 atomic_set(&cmd->transport_lun_active, 0);
5140 /*
5141 * This will notify iscsi_target_transport.c:
5142 * transport_cmd_check_stop() that a LUN shutdown is in
5143 * progress for the iscsi_cmd_t.
5144 */
5145 spin_lock(&cmd->t_state_lock);
5146 DEBUG_CLEAR_L("SE_LUN[%d] - Setting cmd->transport"
5147 "_lun_stop for ITT: 0x%08x\n",
5148 cmd->se_lun->unpacked_lun,
5149 cmd->se_tfo->get_task_tag(cmd));
5150 atomic_set(&cmd->transport_lun_stop, 1);
5151 spin_unlock(&cmd->t_state_lock);
5152
5153 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5154
5155 if (!(cmd->se_lun)) {
5156 printk(KERN_ERR "ITT: 0x%08x, [i,t]_state: %u/%u\n",
5157 cmd->se_tfo->get_task_tag(cmd),
5158 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
5159 BUG();
5160 }
5161 /*
5162 * If the Storage engine still owns the iscsi_cmd_t, determine
5163 * and/or stop its context.
5164 */
5165 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x before transport"
5166 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
5167 cmd->se_tfo->get_task_tag(cmd));
5168
5169 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
5170 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5171 continue;
5172 }
5173
5174 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
5175 "_wait_for_tasks(): SUCCESS\n",
5176 cmd->se_lun->unpacked_lun,
5177 cmd->se_tfo->get_task_tag(cmd));
5178
5179 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
5180 if (!(atomic_read(&cmd->transport_dev_active))) {
5181 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
5182 goto check_cond;
5183 }
5184 atomic_set(&cmd->transport_dev_active, 0);
5185 transport_all_task_dev_remove_state(cmd);
5186 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
5187
5188 transport_free_dev_tasks(cmd);
5189 /*
5190 * The Storage engine stopped this struct se_cmd before it was
5191 * send to the fabric frontend for delivery back to the
5192 * Initiator Node. Return this SCSI CDB back with an
5193 * CHECK_CONDITION status.
5194 */
5195 check_cond:
5196 transport_send_check_condition_and_sense(cmd,
5197 TCM_NON_EXISTENT_LUN, 0);
5198 /*
5199 * If the fabric frontend is waiting for this iscsi_cmd_t to
5200 * be released, notify the waiting thread now that LU has
5201 * finished accessing it.
5202 */
5203 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
5204 if (atomic_read(&cmd->transport_lun_fe_stop)) {
5205 DEBUG_CLEAR_L("SE_LUN[%d] - Detected FE stop for"
5206 " struct se_cmd: %p ITT: 0x%08x\n",
5207 lun->unpacked_lun,
5208 cmd, cmd->se_tfo->get_task_tag(cmd));
5209
5210 spin_unlock_irqrestore(&cmd->t_state_lock,
5211 cmd_flags);
5212 transport_cmd_check_stop(cmd, 1, 0);
5213 complete(&cmd->transport_lun_fe_stop_comp);
5214 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5215 continue;
5216 }
5217 DEBUG_CLEAR_L("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
5218 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
5219
5220 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
5221 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
5222 }
5223 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
5224 }
5225
5226 static int transport_clear_lun_thread(void *p)
5227 {
5228 struct se_lun *lun = (struct se_lun *)p;
5229
5230 __transport_clear_lun_from_sessions(lun);
5231 complete(&lun->lun_shutdown_comp);
5232
5233 return 0;
5234 }
5235
5236 int transport_clear_lun_from_sessions(struct se_lun *lun)
5237 {
5238 struct task_struct *kt;
5239
5240 kt = kthread_run(transport_clear_lun_thread, lun,
5241 "tcm_cl_%u", lun->unpacked_lun);
5242 if (IS_ERR(kt)) {
5243 printk(KERN_ERR "Unable to start clear_lun thread\n");
5244 return PTR_ERR(kt);
5245 }
5246 wait_for_completion(&lun->lun_shutdown_comp);
5247
5248 return 0;
5249 }
5250
5251 /* transport_generic_wait_for_tasks():
5252 *
5253 * Called from frontend or passthrough context to wait for storage engine
5254 * to pause and/or release frontend generated struct se_cmd.
5255 */
5256 static void transport_generic_wait_for_tasks(
5257 struct se_cmd *cmd,
5258 int remove_cmd,
5259 int session_reinstatement)
5260 {
5261 unsigned long flags;
5262
5263 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req))
5264 return;
5265
5266 spin_lock_irqsave(&cmd->t_state_lock, flags);
5267 /*
5268 * If we are already stopped due to an external event (ie: LUN shutdown)
5269 * sleep until the connection can have the passed struct se_cmd back.
5270 * The cmd->transport_lun_stopped_sem will be upped by
5271 * transport_clear_lun_from_sessions() once the ConfigFS context caller
5272 * has completed its operation on the struct se_cmd.
5273 */
5274 if (atomic_read(&cmd->transport_lun_stop)) {
5275
5276 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping"
5277 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
5278 "_stop_comp); for ITT: 0x%08x\n",
5279 cmd->se_tfo->get_task_tag(cmd));
5280 /*
5281 * There is a special case for WRITES where a FE exception +
5282 * LUN shutdown means ConfigFS context is still sleeping on
5283 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
5284 * We go ahead and up transport_lun_stop_comp just to be sure
5285 * here.
5286 */
5287 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5288 complete(&cmd->transport_lun_stop_comp);
5289 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
5290 spin_lock_irqsave(&cmd->t_state_lock, flags);
5291
5292 transport_all_task_dev_remove_state(cmd);
5293 /*
5294 * At this point, the frontend who was the originator of this
5295 * struct se_cmd, now owns the structure and can be released through
5296 * normal means below.
5297 */
5298 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped"
5299 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
5300 "stop_comp); for ITT: 0x%08x\n",
5301 cmd->se_tfo->get_task_tag(cmd));
5302
5303 atomic_set(&cmd->transport_lun_stop, 0);
5304 }
5305 if (!atomic_read(&cmd->t_transport_active) ||
5306 atomic_read(&cmd->t_transport_aborted))
5307 goto remove;
5308
5309 atomic_set(&cmd->t_transport_stop, 1);
5310
5311 DEBUG_TRANSPORT_S("wait_for_tasks: Stopping %p ITT: 0x%08x"
5312 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
5313 " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
5314 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
5315 cmd->deferred_t_state);
5316
5317 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5318
5319 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
5320
5321 wait_for_completion(&cmd->t_transport_stop_comp);
5322
5323 spin_lock_irqsave(&cmd->t_state_lock, flags);
5324 atomic_set(&cmd->t_transport_active, 0);
5325 atomic_set(&cmd->t_transport_stop, 0);
5326
5327 DEBUG_TRANSPORT_S("wait_for_tasks: Stopped wait_for_compltion("
5328 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
5329 cmd->se_tfo->get_task_tag(cmd));
5330 remove:
5331 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5332 if (!remove_cmd)
5333 return;
5334
5335 transport_generic_free_cmd(cmd, 0, session_reinstatement);
5336 }
5337
5338 static int transport_get_sense_codes(
5339 struct se_cmd *cmd,
5340 u8 *asc,
5341 u8 *ascq)
5342 {
5343 *asc = cmd->scsi_asc;
5344 *ascq = cmd->scsi_ascq;
5345
5346 return 0;
5347 }
5348
5349 static int transport_set_sense_codes(
5350 struct se_cmd *cmd,
5351 u8 asc,
5352 u8 ascq)
5353 {
5354 cmd->scsi_asc = asc;
5355 cmd->scsi_ascq = ascq;
5356
5357 return 0;
5358 }
5359
5360 int transport_send_check_condition_and_sense(
5361 struct se_cmd *cmd,
5362 u8 reason,
5363 int from_transport)
5364 {
5365 unsigned char *buffer = cmd->sense_buffer;
5366 unsigned long flags;
5367 int offset;
5368 u8 asc = 0, ascq = 0;
5369
5370 spin_lock_irqsave(&cmd->t_state_lock, flags);
5371 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
5372 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5373 return 0;
5374 }
5375 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
5376 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
5377
5378 if (!reason && from_transport)
5379 goto after_reason;
5380
5381 if (!from_transport)
5382 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
5383 /*
5384 * Data Segment and SenseLength of the fabric response PDU.
5385 *
5386 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
5387 * from include/scsi/scsi_cmnd.h
5388 */
5389 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
5390 TRANSPORT_SENSE_BUFFER);
5391 /*
5392 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
5393 * SENSE KEY values from include/scsi/scsi.h
5394 */
5395 switch (reason) {
5396 case TCM_NON_EXISTENT_LUN:
5397 case TCM_UNSUPPORTED_SCSI_OPCODE:
5398 case TCM_SECTOR_COUNT_TOO_MANY:
5399 /* CURRENT ERROR */
5400 buffer[offset] = 0x70;
5401 /* ILLEGAL REQUEST */
5402 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5403 /* INVALID COMMAND OPERATION CODE */
5404 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
5405 break;
5406 case TCM_UNKNOWN_MODE_PAGE:
5407 /* CURRENT ERROR */
5408 buffer[offset] = 0x70;
5409 /* ILLEGAL REQUEST */
5410 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5411 /* INVALID FIELD IN CDB */
5412 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5413 break;
5414 case TCM_CHECK_CONDITION_ABORT_CMD:
5415 /* CURRENT ERROR */
5416 buffer[offset] = 0x70;
5417 /* ABORTED COMMAND */
5418 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5419 /* BUS DEVICE RESET FUNCTION OCCURRED */
5420 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
5421 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
5422 break;
5423 case TCM_INCORRECT_AMOUNT_OF_DATA:
5424 /* CURRENT ERROR */
5425 buffer[offset] = 0x70;
5426 /* ABORTED COMMAND */
5427 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5428 /* WRITE ERROR */
5429 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
5430 /* NOT ENOUGH UNSOLICITED DATA */
5431 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
5432 break;
5433 case TCM_INVALID_CDB_FIELD:
5434 /* CURRENT ERROR */
5435 buffer[offset] = 0x70;
5436 /* ABORTED COMMAND */
5437 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5438 /* INVALID FIELD IN CDB */
5439 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
5440 break;
5441 case TCM_INVALID_PARAMETER_LIST:
5442 /* CURRENT ERROR */
5443 buffer[offset] = 0x70;
5444 /* ABORTED COMMAND */
5445 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5446 /* INVALID FIELD IN PARAMETER LIST */
5447 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
5448 break;
5449 case TCM_UNEXPECTED_UNSOLICITED_DATA:
5450 /* CURRENT ERROR */
5451 buffer[offset] = 0x70;
5452 /* ABORTED COMMAND */
5453 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5454 /* WRITE ERROR */
5455 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
5456 /* UNEXPECTED_UNSOLICITED_DATA */
5457 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
5458 break;
5459 case TCM_SERVICE_CRC_ERROR:
5460 /* CURRENT ERROR */
5461 buffer[offset] = 0x70;
5462 /* ABORTED COMMAND */
5463 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5464 /* PROTOCOL SERVICE CRC ERROR */
5465 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
5466 /* N/A */
5467 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
5468 break;
5469 case TCM_SNACK_REJECTED:
5470 /* CURRENT ERROR */
5471 buffer[offset] = 0x70;
5472 /* ABORTED COMMAND */
5473 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
5474 /* READ ERROR */
5475 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
5476 /* FAILED RETRANSMISSION REQUEST */
5477 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
5478 break;
5479 case TCM_WRITE_PROTECTED:
5480 /* CURRENT ERROR */
5481 buffer[offset] = 0x70;
5482 /* DATA PROTECT */
5483 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
5484 /* WRITE PROTECTED */
5485 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
5486 break;
5487 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
5488 /* CURRENT ERROR */
5489 buffer[offset] = 0x70;
5490 /* UNIT ATTENTION */
5491 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
5492 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
5493 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5494 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5495 break;
5496 case TCM_CHECK_CONDITION_NOT_READY:
5497 /* CURRENT ERROR */
5498 buffer[offset] = 0x70;
5499 /* Not Ready */
5500 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
5501 transport_get_sense_codes(cmd, &asc, &ascq);
5502 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
5503 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
5504 break;
5505 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
5506 default:
5507 /* CURRENT ERROR */
5508 buffer[offset] = 0x70;
5509 /* ILLEGAL REQUEST */
5510 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
5511 /* LOGICAL UNIT COMMUNICATION FAILURE */
5512 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
5513 break;
5514 }
5515 /*
5516 * This code uses linux/include/scsi/scsi.h SAM status codes!
5517 */
5518 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
5519 /*
5520 * Automatically padded, this value is encoded in the fabric's
5521 * data_length response PDU containing the SCSI defined sense data.
5522 */
5523 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
5524
5525 after_reason:
5526 return cmd->se_tfo->queue_status(cmd);
5527 }
5528 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
5529
5530 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
5531 {
5532 int ret = 0;
5533
5534 if (atomic_read(&cmd->t_transport_aborted) != 0) {
5535 if (!(send_status) ||
5536 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
5537 return 1;
5538 #if 0
5539 printk(KERN_INFO "Sending delayed SAM_STAT_TASK_ABORTED"
5540 " status for CDB: 0x%02x ITT: 0x%08x\n",
5541 cmd->t_task_cdb[0],
5542 cmd->se_tfo->get_task_tag(cmd));
5543 #endif
5544 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
5545 cmd->se_tfo->queue_status(cmd);
5546 ret = 1;
5547 }
5548 return ret;
5549 }
5550 EXPORT_SYMBOL(transport_check_aborted_status);
5551
5552 void transport_send_task_abort(struct se_cmd *cmd)
5553 {
5554 /*
5555 * If there are still expected incoming fabric WRITEs, we wait
5556 * until until they have completed before sending a TASK_ABORTED
5557 * response. This response with TASK_ABORTED status will be
5558 * queued back to fabric module by transport_check_aborted_status().
5559 */
5560 if (cmd->data_direction == DMA_TO_DEVICE) {
5561 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
5562 atomic_inc(&cmd->t_transport_aborted);
5563 smp_mb__after_atomic_inc();
5564 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5565 transport_new_cmd_failure(cmd);
5566 return;
5567 }
5568 }
5569 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
5570 #if 0
5571 printk(KERN_INFO "Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
5572 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
5573 cmd->se_tfo->get_task_tag(cmd));
5574 #endif
5575 cmd->se_tfo->queue_status(cmd);
5576 }
5577
5578 /* transport_generic_do_tmr():
5579 *
5580 *
5581 */
5582 int transport_generic_do_tmr(struct se_cmd *cmd)
5583 {
5584 struct se_device *dev = cmd->se_dev;
5585 struct se_tmr_req *tmr = cmd->se_tmr_req;
5586 int ret;
5587
5588 switch (tmr->function) {
5589 case TMR_ABORT_TASK:
5590 tmr->response = TMR_FUNCTION_REJECTED;
5591 break;
5592 case TMR_ABORT_TASK_SET:
5593 case TMR_CLEAR_ACA:
5594 case TMR_CLEAR_TASK_SET:
5595 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
5596 break;
5597 case TMR_LUN_RESET:
5598 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
5599 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
5600 TMR_FUNCTION_REJECTED;
5601 break;
5602 case TMR_TARGET_WARM_RESET:
5603 tmr->response = TMR_FUNCTION_REJECTED;
5604 break;
5605 case TMR_TARGET_COLD_RESET:
5606 tmr->response = TMR_FUNCTION_REJECTED;
5607 break;
5608 default:
5609 printk(KERN_ERR "Uknown TMR function: 0x%02x.\n",
5610 tmr->function);
5611 tmr->response = TMR_FUNCTION_REJECTED;
5612 break;
5613 }
5614
5615 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
5616 cmd->se_tfo->queue_tm_rsp(cmd);
5617
5618 transport_cmd_check_stop(cmd, 2, 0);
5619 return 0;
5620 }
5621
5622 /*
5623 * Called with spin_lock_irq(&dev->execute_task_lock); held
5624 *
5625 */
5626 static struct se_task *
5627 transport_get_task_from_state_list(struct se_device *dev)
5628 {
5629 struct se_task *task;
5630
5631 if (list_empty(&dev->state_task_list))
5632 return NULL;
5633
5634 list_for_each_entry(task, &dev->state_task_list, t_state_list)
5635 break;
5636
5637 list_del(&task->t_state_list);
5638 atomic_set(&task->task_state_active, 0);
5639
5640 return task;
5641 }
5642
5643 static void transport_processing_shutdown(struct se_device *dev)
5644 {
5645 struct se_cmd *cmd;
5646 struct se_task *task;
5647 unsigned long flags;
5648 /*
5649 * Empty the struct se_device's struct se_task state list.
5650 */
5651 spin_lock_irqsave(&dev->execute_task_lock, flags);
5652 while ((task = transport_get_task_from_state_list(dev))) {
5653 if (!task->task_se_cmd) {
5654 printk(KERN_ERR "task->task_se_cmd is NULL!\n");
5655 continue;
5656 }
5657 cmd = task->task_se_cmd;
5658
5659 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5660
5661 spin_lock_irqsave(&cmd->t_state_lock, flags);
5662
5663 DEBUG_DO("PT: cmd: %p task: %p ITT/CmdSN: 0x%08x/0x%08x,"
5664 " i_state/def_i_state: %d/%d, t_state/def_t_state:"
5665 " %d/%d cdb: 0x%02x\n", cmd, task,
5666 cmd->se_tfo->get_task_tag(cmd), cmd->cmd_sn,
5667 cmd->se_tfo->get_cmd_state(cmd), cmd->deferred_i_state,
5668 cmd->t_state, cmd->deferred_t_state,
5669 cmd->t_task_cdb[0]);
5670 DEBUG_DO("PT: ITT[0x%08x] - t_task_cdbs: %d t_task_cdbs_left:"
5671 " %d t_task_cdbs_sent: %d -- t_transport_active: %d"
5672 " t_transport_stop: %d t_transport_sent: %d\n",
5673 cmd->se_tfo->get_task_tag(cmd),
5674 cmd->t_task_cdbs,
5675 atomic_read(&cmd->t_task_cdbs_left),
5676 atomic_read(&cmd->t_task_cdbs_sent),
5677 atomic_read(&cmd->t_transport_active),
5678 atomic_read(&cmd->t_transport_stop),
5679 atomic_read(&cmd->t_transport_sent));
5680
5681 if (atomic_read(&task->task_active)) {
5682 atomic_set(&task->task_stop, 1);
5683 spin_unlock_irqrestore(
5684 &cmd->t_state_lock, flags);
5685
5686 DEBUG_DO("Waiting for task: %p to shutdown for dev:"
5687 " %p\n", task, dev);
5688 wait_for_completion(&task->task_stop_comp);
5689 DEBUG_DO("Completed task: %p shutdown for dev: %p\n",
5690 task, dev);
5691
5692 spin_lock_irqsave(&cmd->t_state_lock, flags);
5693 atomic_dec(&cmd->t_task_cdbs_left);
5694
5695 atomic_set(&task->task_active, 0);
5696 atomic_set(&task->task_stop, 0);
5697 } else {
5698 if (atomic_read(&task->task_execute_queue) != 0)
5699 transport_remove_task_from_execute_queue(task, dev);
5700 }
5701 __transport_stop_task_timer(task, &flags);
5702
5703 if (!(atomic_dec_and_test(&cmd->t_task_cdbs_ex_left))) {
5704 spin_unlock_irqrestore(
5705 &cmd->t_state_lock, flags);
5706
5707 DEBUG_DO("Skipping task: %p, dev: %p for"
5708 " t_task_cdbs_ex_left: %d\n", task, dev,
5709 atomic_read(&cmd->t_task_cdbs_ex_left));
5710
5711 spin_lock_irqsave(&dev->execute_task_lock, flags);
5712 continue;
5713 }
5714
5715 if (atomic_read(&cmd->t_transport_active)) {
5716 DEBUG_DO("got t_transport_active = 1 for task: %p, dev:"
5717 " %p\n", task, dev);
5718
5719 if (atomic_read(&cmd->t_fe_count)) {
5720 spin_unlock_irqrestore(
5721 &cmd->t_state_lock, flags);
5722 transport_send_check_condition_and_sense(
5723 cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE,
5724 0);
5725 transport_remove_cmd_from_queue(cmd,
5726 &cmd->se_dev->dev_queue_obj);
5727
5728 transport_lun_remove_cmd(cmd);
5729 transport_cmd_check_stop(cmd, 1, 0);
5730 } else {
5731 spin_unlock_irqrestore(
5732 &cmd->t_state_lock, flags);
5733
5734 transport_remove_cmd_from_queue(cmd,
5735 &cmd->se_dev->dev_queue_obj);
5736
5737 transport_lun_remove_cmd(cmd);
5738
5739 if (transport_cmd_check_stop(cmd, 1, 0))
5740 transport_generic_remove(cmd, 0);
5741 }
5742
5743 spin_lock_irqsave(&dev->execute_task_lock, flags);
5744 continue;
5745 }
5746 DEBUG_DO("Got t_transport_active = 0 for task: %p, dev: %p\n",
5747 task, dev);
5748
5749 if (atomic_read(&cmd->t_fe_count)) {
5750 spin_unlock_irqrestore(
5751 &cmd->t_state_lock, flags);
5752 transport_send_check_condition_and_sense(cmd,
5753 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
5754 transport_remove_cmd_from_queue(cmd,
5755 &cmd->se_dev->dev_queue_obj);
5756
5757 transport_lun_remove_cmd(cmd);
5758 transport_cmd_check_stop(cmd, 1, 0);
5759 } else {
5760 spin_unlock_irqrestore(
5761 &cmd->t_state_lock, flags);
5762
5763 transport_remove_cmd_from_queue(cmd,
5764 &cmd->se_dev->dev_queue_obj);
5765 transport_lun_remove_cmd(cmd);
5766
5767 if (transport_cmd_check_stop(cmd, 1, 0))
5768 transport_generic_remove(cmd, 0);
5769 }
5770
5771 spin_lock_irqsave(&dev->execute_task_lock, flags);
5772 }
5773 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
5774 /*
5775 * Empty the struct se_device's struct se_cmd list.
5776 */
5777 while ((cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj))) {
5778
5779 DEBUG_DO("From Device Queue: cmd: %p t_state: %d\n",
5780 cmd, cmd->t_state);
5781
5782 if (atomic_read(&cmd->t_fe_count)) {
5783 transport_send_check_condition_and_sense(cmd,
5784 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE, 0);
5785
5786 transport_lun_remove_cmd(cmd);
5787 transport_cmd_check_stop(cmd, 1, 0);
5788 } else {
5789 transport_lun_remove_cmd(cmd);
5790 if (transport_cmd_check_stop(cmd, 1, 0))
5791 transport_generic_remove(cmd, 0);
5792 }
5793 }
5794 }
5795
5796 /* transport_processing_thread():
5797 *
5798 *
5799 */
5800 static int transport_processing_thread(void *param)
5801 {
5802 int ret;
5803 struct se_cmd *cmd;
5804 struct se_device *dev = (struct se_device *) param;
5805
5806 set_user_nice(current, -20);
5807
5808 while (!kthread_should_stop()) {
5809 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
5810 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
5811 kthread_should_stop());
5812 if (ret < 0)
5813 goto out;
5814
5815 spin_lock_irq(&dev->dev_status_lock);
5816 if (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) {
5817 spin_unlock_irq(&dev->dev_status_lock);
5818 transport_processing_shutdown(dev);
5819 continue;
5820 }
5821 spin_unlock_irq(&dev->dev_status_lock);
5822
5823 get_cmd:
5824 __transport_execute_tasks(dev);
5825
5826 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
5827 if (!cmd)
5828 continue;
5829
5830 switch (cmd->t_state) {
5831 case TRANSPORT_NEW_CMD_MAP:
5832 if (!(cmd->se_tfo->new_cmd_map)) {
5833 printk(KERN_ERR "cmd->se_tfo->new_cmd_map is"
5834 " NULL for TRANSPORT_NEW_CMD_MAP\n");
5835 BUG();
5836 }
5837 ret = cmd->se_tfo->new_cmd_map(cmd);
5838 if (ret < 0) {
5839 cmd->transport_error_status = ret;
5840 transport_generic_request_failure(cmd, NULL,
5841 0, (cmd->data_direction !=
5842 DMA_TO_DEVICE));
5843 break;
5844 }
5845 /* Fall through */
5846 case TRANSPORT_NEW_CMD:
5847 ret = transport_generic_new_cmd(cmd);
5848 if (ret == -EAGAIN)
5849 break;
5850 else if (ret < 0) {
5851 cmd->transport_error_status = ret;
5852 transport_generic_request_failure(cmd, NULL,
5853 0, (cmd->data_direction !=
5854 DMA_TO_DEVICE));
5855 }
5856 break;
5857 case TRANSPORT_PROCESS_WRITE:
5858 transport_generic_process_write(cmd);
5859 break;
5860 case TRANSPORT_COMPLETE_OK:
5861 transport_stop_all_task_timers(cmd);
5862 transport_generic_complete_ok(cmd);
5863 break;
5864 case TRANSPORT_REMOVE:
5865 transport_generic_remove(cmd, 0);
5866 break;
5867 case TRANSPORT_FREE_CMD_INTR:
5868 transport_generic_free_cmd(cmd, 0, 0);
5869 break;
5870 case TRANSPORT_PROCESS_TMR:
5871 transport_generic_do_tmr(cmd);
5872 break;
5873 case TRANSPORT_COMPLETE_FAILURE:
5874 transport_generic_request_failure(cmd, NULL, 1, 1);
5875 break;
5876 case TRANSPORT_COMPLETE_TIMEOUT:
5877 transport_stop_all_task_timers(cmd);
5878 transport_generic_request_timeout(cmd);
5879 break;
5880 case TRANSPORT_COMPLETE_QF_WP:
5881 transport_generic_write_pending(cmd);
5882 break;
5883 default:
5884 printk(KERN_ERR "Unknown t_state: %d deferred_t_state:"
5885 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
5886 " %u\n", cmd->t_state, cmd->deferred_t_state,
5887 cmd->se_tfo->get_task_tag(cmd),
5888 cmd->se_tfo->get_cmd_state(cmd),
5889 cmd->se_lun->unpacked_lun);
5890 BUG();
5891 }
5892
5893 goto get_cmd;
5894 }
5895
5896 out:
5897 transport_release_all_cmds(dev);
5898 dev->process_thread = NULL;
5899 return 0;
5900 }
This page took 0.156103 seconds and 5 git commands to generate.