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