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