nilfs2: unbreak compat ioctl
[zen-stable.git] / drivers / target / target_core_transport.c
blob0257658e2e3ea8a75642ae0dcabc77547ac2379b
1 /*******************************************************************************
2 * Filename: target_core_transport.c
4 * This file contains the Generic Target Engine Core.
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
11 * Nicholas A. Bellinger <nab@kernel.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ******************************************************************************/
29 #include <linux/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 <linux/module.h>
40 #include <asm/unaligned.h>
41 #include <net/sock.h>
42 #include <net/tcp.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_tcq.h>
47 #include <target/target_core_base.h>
48 #include <target/target_core_device.h>
49 #include <target/target_core_tmr.h>
50 #include <target/target_core_tpg.h>
51 #include <target/target_core_transport.h>
52 #include <target/target_core_fabric_ops.h>
53 #include <target/target_core_configfs.h>
55 #include "target_core_alua.h"
56 #include "target_core_cdb.h"
57 #include "target_core_hba.h"
58 #include "target_core_pr.h"
59 #include "target_core_ua.h"
61 static int sub_api_initialized;
63 static struct workqueue_struct *target_completion_wq;
64 static struct kmem_cache *se_sess_cache;
65 struct kmem_cache *se_tmr_req_cache;
66 struct kmem_cache *se_ua_cache;
67 struct kmem_cache *t10_pr_reg_cache;
68 struct kmem_cache *t10_alua_lu_gp_cache;
69 struct kmem_cache *t10_alua_lu_gp_mem_cache;
70 struct kmem_cache *t10_alua_tg_pt_gp_cache;
71 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
73 static int transport_generic_write_pending(struct se_cmd *);
74 static int transport_processing_thread(void *param);
75 static int __transport_execute_tasks(struct se_device *dev);
76 static void transport_complete_task_attr(struct se_cmd *cmd);
77 static void transport_handle_queue_full(struct se_cmd *cmd,
78 struct se_device *dev);
79 static void transport_free_dev_tasks(struct se_cmd *cmd);
80 static int transport_generic_get_mem(struct se_cmd *cmd);
81 static void transport_put_cmd(struct se_cmd *cmd);
82 static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
83 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
84 static void transport_generic_request_failure(struct se_cmd *);
85 static void target_complete_ok_work(struct work_struct *work);
87 int init_se_kmem_caches(void)
89 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
90 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
91 0, NULL);
92 if (!se_tmr_req_cache) {
93 pr_err("kmem_cache_create() for struct se_tmr_req"
94 " failed\n");
95 goto out;
97 se_sess_cache = kmem_cache_create("se_sess_cache",
98 sizeof(struct se_session), __alignof__(struct se_session),
99 0, NULL);
100 if (!se_sess_cache) {
101 pr_err("kmem_cache_create() for struct se_session"
102 " failed\n");
103 goto out_free_tmr_req_cache;
105 se_ua_cache = kmem_cache_create("se_ua_cache",
106 sizeof(struct se_ua), __alignof__(struct se_ua),
107 0, NULL);
108 if (!se_ua_cache) {
109 pr_err("kmem_cache_create() for struct se_ua failed\n");
110 goto out_free_sess_cache;
112 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
113 sizeof(struct t10_pr_registration),
114 __alignof__(struct t10_pr_registration), 0, NULL);
115 if (!t10_pr_reg_cache) {
116 pr_err("kmem_cache_create() for struct t10_pr_registration"
117 " failed\n");
118 goto out_free_ua_cache;
120 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
121 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
122 0, NULL);
123 if (!t10_alua_lu_gp_cache) {
124 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
125 " failed\n");
126 goto out_free_pr_reg_cache;
128 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
129 sizeof(struct t10_alua_lu_gp_member),
130 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
131 if (!t10_alua_lu_gp_mem_cache) {
132 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
133 "cache failed\n");
134 goto out_free_lu_gp_cache;
136 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
137 sizeof(struct t10_alua_tg_pt_gp),
138 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
139 if (!t10_alua_tg_pt_gp_cache) {
140 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
141 "cache failed\n");
142 goto out_free_lu_gp_mem_cache;
144 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
145 "t10_alua_tg_pt_gp_mem_cache",
146 sizeof(struct t10_alua_tg_pt_gp_member),
147 __alignof__(struct t10_alua_tg_pt_gp_member),
148 0, NULL);
149 if (!t10_alua_tg_pt_gp_mem_cache) {
150 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
151 "mem_t failed\n");
152 goto out_free_tg_pt_gp_cache;
155 target_completion_wq = alloc_workqueue("target_completion",
156 WQ_MEM_RECLAIM, 0);
157 if (!target_completion_wq)
158 goto out_free_tg_pt_gp_mem_cache;
160 return 0;
162 out_free_tg_pt_gp_mem_cache:
163 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
164 out_free_tg_pt_gp_cache:
165 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
166 out_free_lu_gp_mem_cache:
167 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
168 out_free_lu_gp_cache:
169 kmem_cache_destroy(t10_alua_lu_gp_cache);
170 out_free_pr_reg_cache:
171 kmem_cache_destroy(t10_pr_reg_cache);
172 out_free_ua_cache:
173 kmem_cache_destroy(se_ua_cache);
174 out_free_sess_cache:
175 kmem_cache_destroy(se_sess_cache);
176 out_free_tmr_req_cache:
177 kmem_cache_destroy(se_tmr_req_cache);
178 out:
179 return -ENOMEM;
182 void release_se_kmem_caches(void)
184 destroy_workqueue(target_completion_wq);
185 kmem_cache_destroy(se_tmr_req_cache);
186 kmem_cache_destroy(se_sess_cache);
187 kmem_cache_destroy(se_ua_cache);
188 kmem_cache_destroy(t10_pr_reg_cache);
189 kmem_cache_destroy(t10_alua_lu_gp_cache);
190 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
191 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
192 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
195 /* This code ensures unique mib indexes are handed out. */
196 static DEFINE_SPINLOCK(scsi_mib_index_lock);
197 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
200 * Allocate a new row index for the entry type specified
202 u32 scsi_get_new_index(scsi_index_t type)
204 u32 new_index;
206 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
208 spin_lock(&scsi_mib_index_lock);
209 new_index = ++scsi_mib_index[type];
210 spin_unlock(&scsi_mib_index_lock);
212 return new_index;
215 void transport_init_queue_obj(struct se_queue_obj *qobj)
217 atomic_set(&qobj->queue_cnt, 0);
218 INIT_LIST_HEAD(&qobj->qobj_list);
219 init_waitqueue_head(&qobj->thread_wq);
220 spin_lock_init(&qobj->cmd_queue_lock);
222 EXPORT_SYMBOL(transport_init_queue_obj);
224 void transport_subsystem_check_init(void)
226 int ret;
228 if (sub_api_initialized)
229 return;
231 ret = request_module("target_core_iblock");
232 if (ret != 0)
233 pr_err("Unable to load target_core_iblock\n");
235 ret = request_module("target_core_file");
236 if (ret != 0)
237 pr_err("Unable to load target_core_file\n");
239 ret = request_module("target_core_pscsi");
240 if (ret != 0)
241 pr_err("Unable to load target_core_pscsi\n");
243 ret = request_module("target_core_stgt");
244 if (ret != 0)
245 pr_err("Unable to load target_core_stgt\n");
247 sub_api_initialized = 1;
248 return;
251 struct se_session *transport_init_session(void)
253 struct se_session *se_sess;
255 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
256 if (!se_sess) {
257 pr_err("Unable to allocate struct se_session from"
258 " se_sess_cache\n");
259 return ERR_PTR(-ENOMEM);
261 INIT_LIST_HEAD(&se_sess->sess_list);
262 INIT_LIST_HEAD(&se_sess->sess_acl_list);
263 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
264 INIT_LIST_HEAD(&se_sess->sess_wait_list);
265 spin_lock_init(&se_sess->sess_cmd_lock);
267 return se_sess;
269 EXPORT_SYMBOL(transport_init_session);
272 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
274 void __transport_register_session(
275 struct se_portal_group *se_tpg,
276 struct se_node_acl *se_nacl,
277 struct se_session *se_sess,
278 void *fabric_sess_ptr)
280 unsigned char buf[PR_REG_ISID_LEN];
282 se_sess->se_tpg = se_tpg;
283 se_sess->fabric_sess_ptr = fabric_sess_ptr;
285 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
287 * Only set for struct se_session's that will actually be moving I/O.
288 * eg: *NOT* discovery sessions.
290 if (se_nacl) {
292 * If the fabric module supports an ISID based TransportID,
293 * save this value in binary from the fabric I_T Nexus now.
295 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
296 memset(&buf[0], 0, PR_REG_ISID_LEN);
297 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
298 &buf[0], PR_REG_ISID_LEN);
299 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
301 spin_lock_irq(&se_nacl->nacl_sess_lock);
303 * The se_nacl->nacl_sess pointer will be set to the
304 * last active I_T Nexus for each struct se_node_acl.
306 se_nacl->nacl_sess = se_sess;
308 list_add_tail(&se_sess->sess_acl_list,
309 &se_nacl->acl_sess_list);
310 spin_unlock_irq(&se_nacl->nacl_sess_lock);
312 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
314 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
315 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
317 EXPORT_SYMBOL(__transport_register_session);
319 void transport_register_session(
320 struct se_portal_group *se_tpg,
321 struct se_node_acl *se_nacl,
322 struct se_session *se_sess,
323 void *fabric_sess_ptr)
325 spin_lock_bh(&se_tpg->session_lock);
326 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
327 spin_unlock_bh(&se_tpg->session_lock);
329 EXPORT_SYMBOL(transport_register_session);
331 void transport_deregister_session_configfs(struct se_session *se_sess)
333 struct se_node_acl *se_nacl;
334 unsigned long flags;
336 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
338 se_nacl = se_sess->se_node_acl;
339 if (se_nacl) {
340 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
341 list_del(&se_sess->sess_acl_list);
343 * If the session list is empty, then clear the pointer.
344 * Otherwise, set the struct se_session pointer from the tail
345 * element of the per struct se_node_acl active session list.
347 if (list_empty(&se_nacl->acl_sess_list))
348 se_nacl->nacl_sess = NULL;
349 else {
350 se_nacl->nacl_sess = container_of(
351 se_nacl->acl_sess_list.prev,
352 struct se_session, sess_acl_list);
354 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
357 EXPORT_SYMBOL(transport_deregister_session_configfs);
359 void transport_free_session(struct se_session *se_sess)
361 kmem_cache_free(se_sess_cache, se_sess);
363 EXPORT_SYMBOL(transport_free_session);
365 void transport_deregister_session(struct se_session *se_sess)
367 struct se_portal_group *se_tpg = se_sess->se_tpg;
368 struct se_node_acl *se_nacl;
369 unsigned long flags;
371 if (!se_tpg) {
372 transport_free_session(se_sess);
373 return;
376 spin_lock_irqsave(&se_tpg->session_lock, flags);
377 list_del(&se_sess->sess_list);
378 se_sess->se_tpg = NULL;
379 se_sess->fabric_sess_ptr = NULL;
380 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
383 * Determine if we need to do extra work for this initiator node's
384 * struct se_node_acl if it had been previously dynamically generated.
386 se_nacl = se_sess->se_node_acl;
387 if (se_nacl) {
388 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
389 if (se_nacl->dynamic_node_acl) {
390 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
391 se_tpg)) {
392 list_del(&se_nacl->acl_list);
393 se_tpg->num_node_acls--;
394 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
396 core_tpg_wait_for_nacl_pr_ref(se_nacl);
397 core_free_device_list_for_node(se_nacl, se_tpg);
398 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
399 se_nacl);
400 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
403 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
406 transport_free_session(se_sess);
408 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
409 se_tpg->se_tpg_tfo->get_fabric_name());
411 EXPORT_SYMBOL(transport_deregister_session);
414 * Called with cmd->t_state_lock held.
416 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
418 struct se_device *dev = cmd->se_dev;
419 struct se_task *task;
420 unsigned long flags;
422 if (!dev)
423 return;
425 list_for_each_entry(task, &cmd->t_task_list, t_list) {
426 if (task->task_flags & TF_ACTIVE)
427 continue;
429 if (!atomic_read(&task->task_state_active))
430 continue;
432 spin_lock_irqsave(&dev->execute_task_lock, flags);
433 list_del(&task->t_state_list);
434 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
435 cmd->se_tfo->get_task_tag(cmd), dev, task);
436 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
438 atomic_set(&task->task_state_active, 0);
439 atomic_dec(&cmd->t_task_cdbs_ex_left);
443 /* transport_cmd_check_stop():
445 * 'transport_off = 1' determines if t_transport_active should be cleared.
446 * 'transport_off = 2' determines if task_dev_state should be removed.
448 * A non-zero u8 t_state sets cmd->t_state.
449 * Returns 1 when command is stopped, else 0.
451 static int transport_cmd_check_stop(
452 struct se_cmd *cmd,
453 int transport_off,
454 u8 t_state)
456 unsigned long flags;
458 spin_lock_irqsave(&cmd->t_state_lock, flags);
460 * Determine if IOCTL context caller in requesting the stopping of this
461 * command for LUN shutdown purposes.
463 if (atomic_read(&cmd->transport_lun_stop)) {
464 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
465 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
466 cmd->se_tfo->get_task_tag(cmd));
468 atomic_set(&cmd->t_transport_active, 0);
469 if (transport_off == 2)
470 transport_all_task_dev_remove_state(cmd);
471 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
473 complete(&cmd->transport_lun_stop_comp);
474 return 1;
477 * Determine if frontend context caller is requesting the stopping of
478 * this command for frontend exceptions.
480 if (atomic_read(&cmd->t_transport_stop)) {
481 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
482 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
483 cmd->se_tfo->get_task_tag(cmd));
485 if (transport_off == 2)
486 transport_all_task_dev_remove_state(cmd);
489 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
490 * to FE.
492 if (transport_off == 2)
493 cmd->se_lun = NULL;
494 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
496 complete(&cmd->t_transport_stop_comp);
497 return 1;
499 if (transport_off) {
500 atomic_set(&cmd->t_transport_active, 0);
501 if (transport_off == 2) {
502 transport_all_task_dev_remove_state(cmd);
504 * Clear struct se_cmd->se_lun before the transport_off == 2
505 * handoff to fabric module.
507 cmd->se_lun = NULL;
509 * Some fabric modules like tcm_loop can release
510 * their internally allocated I/O reference now and
511 * struct se_cmd now.
513 * Fabric modules are expected to return '1' here if the
514 * se_cmd being passed is released at this point,
515 * or zero if not being released.
517 if (cmd->se_tfo->check_stop_free != NULL) {
518 spin_unlock_irqrestore(
519 &cmd->t_state_lock, flags);
521 return cmd->se_tfo->check_stop_free(cmd);
524 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
526 return 0;
527 } else if (t_state)
528 cmd->t_state = t_state;
529 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
531 return 0;
534 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
536 return transport_cmd_check_stop(cmd, 2, 0);
539 static void transport_lun_remove_cmd(struct se_cmd *cmd)
541 struct se_lun *lun = cmd->se_lun;
542 unsigned long flags;
544 if (!lun)
545 return;
547 spin_lock_irqsave(&cmd->t_state_lock, flags);
548 if (!atomic_read(&cmd->transport_dev_active)) {
549 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
550 goto check_lun;
552 atomic_set(&cmd->transport_dev_active, 0);
553 transport_all_task_dev_remove_state(cmd);
554 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
557 check_lun:
558 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
559 if (atomic_read(&cmd->transport_lun_active)) {
560 list_del(&cmd->se_lun_node);
561 atomic_set(&cmd->transport_lun_active, 0);
562 #if 0
563 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
564 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
565 #endif
567 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
570 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
572 if (!cmd->se_tmr_req)
573 transport_lun_remove_cmd(cmd);
575 if (transport_cmd_check_stop_to_fabric(cmd))
576 return;
577 if (remove) {
578 transport_remove_cmd_from_queue(cmd);
579 transport_put_cmd(cmd);
583 static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
584 bool at_head)
586 struct se_device *dev = cmd->se_dev;
587 struct se_queue_obj *qobj = &dev->dev_queue_obj;
588 unsigned long flags;
590 if (t_state) {
591 spin_lock_irqsave(&cmd->t_state_lock, flags);
592 cmd->t_state = t_state;
593 atomic_set(&cmd->t_transport_active, 1);
594 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
597 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
599 /* If the cmd is already on the list, remove it before we add it */
600 if (!list_empty(&cmd->se_queue_node))
601 list_del(&cmd->se_queue_node);
602 else
603 atomic_inc(&qobj->queue_cnt);
605 if (at_head)
606 list_add(&cmd->se_queue_node, &qobj->qobj_list);
607 else
608 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
609 atomic_set(&cmd->t_transport_queue_active, 1);
610 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
612 wake_up_interruptible(&qobj->thread_wq);
615 static struct se_cmd *
616 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
618 struct se_cmd *cmd;
619 unsigned long flags;
621 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
622 if (list_empty(&qobj->qobj_list)) {
623 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
624 return NULL;
626 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
628 atomic_set(&cmd->t_transport_queue_active, 0);
630 list_del_init(&cmd->se_queue_node);
631 atomic_dec(&qobj->queue_cnt);
632 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
634 return cmd;
637 static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
639 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
640 unsigned long flags;
642 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
643 if (!atomic_read(&cmd->t_transport_queue_active)) {
644 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
645 return;
647 atomic_set(&cmd->t_transport_queue_active, 0);
648 atomic_dec(&qobj->queue_cnt);
649 list_del_init(&cmd->se_queue_node);
650 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
652 if (atomic_read(&cmd->t_transport_queue_active)) {
653 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
654 cmd->se_tfo->get_task_tag(cmd),
655 atomic_read(&cmd->t_transport_queue_active));
660 * Completion function used by TCM subsystem plugins (such as FILEIO)
661 * for queueing up response from struct se_subsystem_api->do_task()
663 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
665 struct se_task *task = list_entry(cmd->t_task_list.next,
666 struct se_task, t_list);
668 if (good) {
669 cmd->scsi_status = SAM_STAT_GOOD;
670 task->task_scsi_status = GOOD;
671 } else {
672 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
673 task->task_se_cmd->scsi_sense_reason =
674 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
678 transport_complete_task(task, good);
680 EXPORT_SYMBOL(transport_complete_sync_cache);
682 static void target_complete_failure_work(struct work_struct *work)
684 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
686 transport_generic_request_failure(cmd);
689 /* transport_complete_task():
691 * Called from interrupt and non interrupt context depending
692 * on the transport plugin.
694 void transport_complete_task(struct se_task *task, int success)
696 struct se_cmd *cmd = task->task_se_cmd;
697 struct se_device *dev = cmd->se_dev;
698 unsigned long flags;
699 #if 0
700 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
701 cmd->t_task_cdb[0], dev);
702 #endif
703 if (dev)
704 atomic_inc(&dev->depth_left);
706 spin_lock_irqsave(&cmd->t_state_lock, flags);
707 task->task_flags &= ~TF_ACTIVE;
710 * See if any sense data exists, if so set the TASK_SENSE flag.
711 * Also check for any other post completion work that needs to be
712 * done by the plugins.
714 if (dev && dev->transport->transport_complete) {
715 if (dev->transport->transport_complete(task) != 0) {
716 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
717 task->task_sense = 1;
718 success = 1;
723 * See if we are waiting for outstanding struct se_task
724 * to complete for an exception condition
726 if (task->task_flags & TF_REQUEST_STOP) {
727 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
728 complete(&task->task_stop_comp);
729 return;
732 if (!success)
733 cmd->t_tasks_failed = 1;
736 * Decrement the outstanding t_task_cdbs_left count. The last
737 * struct se_task from struct se_cmd will complete itself into the
738 * device queue depending upon int success.
740 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
741 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
742 return;
745 if (cmd->t_tasks_failed) {
746 if (!task->task_error_status) {
747 task->task_error_status =
748 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
749 cmd->scsi_sense_reason =
750 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
753 INIT_WORK(&cmd->work, target_complete_failure_work);
754 } else {
755 atomic_set(&cmd->t_transport_complete, 1);
756 INIT_WORK(&cmd->work, target_complete_ok_work);
759 cmd->t_state = TRANSPORT_COMPLETE;
760 atomic_set(&cmd->t_transport_active, 1);
761 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
763 queue_work(target_completion_wq, &cmd->work);
765 EXPORT_SYMBOL(transport_complete_task);
768 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
769 * struct se_task list are ready to be added to the active execution list
770 * struct se_device
772 * Called with se_dev_t->execute_task_lock called.
774 static inline int transport_add_task_check_sam_attr(
775 struct se_task *task,
776 struct se_task *task_prev,
777 struct se_device *dev)
780 * No SAM Task attribute emulation enabled, add to tail of
781 * execution queue
783 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
784 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
785 return 0;
788 * HEAD_OF_QUEUE attribute for received CDB, which means
789 * the first task that is associated with a struct se_cmd goes to
790 * head of the struct se_device->execute_task_list, and task_prev
791 * after that for each subsequent task
793 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
794 list_add(&task->t_execute_list,
795 (task_prev != NULL) ?
796 &task_prev->t_execute_list :
797 &dev->execute_task_list);
799 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
800 " in execution queue\n",
801 task->task_se_cmd->t_task_cdb[0]);
802 return 1;
805 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
806 * transitioned from Dermant -> Active state, and are added to the end
807 * of the struct se_device->execute_task_list
809 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
810 return 0;
813 /* __transport_add_task_to_execute_queue():
815 * Called with se_dev_t->execute_task_lock called.
817 static void __transport_add_task_to_execute_queue(
818 struct se_task *task,
819 struct se_task *task_prev,
820 struct se_device *dev)
822 int head_of_queue;
824 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
825 atomic_inc(&dev->execute_tasks);
827 if (atomic_read(&task->task_state_active))
828 return;
830 * Determine if this task needs to go to HEAD_OF_QUEUE for the
831 * state list as well. Running with SAM Task Attribute emulation
832 * will always return head_of_queue == 0 here
834 if (head_of_queue)
835 list_add(&task->t_state_list, (task_prev) ?
836 &task_prev->t_state_list :
837 &dev->state_task_list);
838 else
839 list_add_tail(&task->t_state_list, &dev->state_task_list);
841 atomic_set(&task->task_state_active, 1);
843 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
844 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
845 task, dev);
848 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
850 struct se_device *dev = cmd->se_dev;
851 struct se_task *task;
852 unsigned long flags;
854 spin_lock_irqsave(&cmd->t_state_lock, flags);
855 list_for_each_entry(task, &cmd->t_task_list, t_list) {
856 if (atomic_read(&task->task_state_active))
857 continue;
859 spin_lock(&dev->execute_task_lock);
860 list_add_tail(&task->t_state_list, &dev->state_task_list);
861 atomic_set(&task->task_state_active, 1);
863 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
864 task->task_se_cmd->se_tfo->get_task_tag(
865 task->task_se_cmd), task, dev);
867 spin_unlock(&dev->execute_task_lock);
869 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
872 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
874 struct se_device *dev = cmd->se_dev;
875 struct se_task *task, *task_prev = NULL;
876 unsigned long flags;
878 spin_lock_irqsave(&dev->execute_task_lock, flags);
879 list_for_each_entry(task, &cmd->t_task_list, t_list) {
880 if (!list_empty(&task->t_execute_list))
881 continue;
883 * __transport_add_task_to_execute_queue() handles the
884 * SAM Task Attribute emulation if enabled
886 __transport_add_task_to_execute_queue(task, task_prev, dev);
887 task_prev = task;
889 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
892 void __transport_remove_task_from_execute_queue(struct se_task *task,
893 struct se_device *dev)
895 list_del_init(&task->t_execute_list);
896 atomic_dec(&dev->execute_tasks);
899 void transport_remove_task_from_execute_queue(
900 struct se_task *task,
901 struct se_device *dev)
903 unsigned long flags;
905 if (WARN_ON(list_empty(&task->t_execute_list)))
906 return;
908 spin_lock_irqsave(&dev->execute_task_lock, flags);
909 __transport_remove_task_from_execute_queue(task, dev);
910 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
914 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
917 static void target_qf_do_work(struct work_struct *work)
919 struct se_device *dev = container_of(work, struct se_device,
920 qf_work_queue);
921 LIST_HEAD(qf_cmd_list);
922 struct se_cmd *cmd, *cmd_tmp;
924 spin_lock_irq(&dev->qf_cmd_lock);
925 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
926 spin_unlock_irq(&dev->qf_cmd_lock);
928 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
929 list_del(&cmd->se_qf_node);
930 atomic_dec(&dev->dev_qf_count);
931 smp_mb__after_atomic_dec();
933 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
934 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
935 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
936 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
937 : "UNKNOWN");
939 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
943 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
945 switch (cmd->data_direction) {
946 case DMA_NONE:
947 return "NONE";
948 case DMA_FROM_DEVICE:
949 return "READ";
950 case DMA_TO_DEVICE:
951 return "WRITE";
952 case DMA_BIDIRECTIONAL:
953 return "BIDI";
954 default:
955 break;
958 return "UNKNOWN";
961 void transport_dump_dev_state(
962 struct se_device *dev,
963 char *b,
964 int *bl)
966 *bl += sprintf(b + *bl, "Status: ");
967 switch (dev->dev_status) {
968 case TRANSPORT_DEVICE_ACTIVATED:
969 *bl += sprintf(b + *bl, "ACTIVATED");
970 break;
971 case TRANSPORT_DEVICE_DEACTIVATED:
972 *bl += sprintf(b + *bl, "DEACTIVATED");
973 break;
974 case TRANSPORT_DEVICE_SHUTDOWN:
975 *bl += sprintf(b + *bl, "SHUTDOWN");
976 break;
977 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
978 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
979 *bl += sprintf(b + *bl, "OFFLINE");
980 break;
981 default:
982 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
983 break;
986 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
987 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
988 dev->queue_depth);
989 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
990 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
991 *bl += sprintf(b + *bl, " ");
994 void transport_dump_vpd_proto_id(
995 struct t10_vpd *vpd,
996 unsigned char *p_buf,
997 int p_buf_len)
999 unsigned char buf[VPD_TMP_BUF_SIZE];
1000 int len;
1002 memset(buf, 0, VPD_TMP_BUF_SIZE);
1003 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1005 switch (vpd->protocol_identifier) {
1006 case 0x00:
1007 sprintf(buf+len, "Fibre Channel\n");
1008 break;
1009 case 0x10:
1010 sprintf(buf+len, "Parallel SCSI\n");
1011 break;
1012 case 0x20:
1013 sprintf(buf+len, "SSA\n");
1014 break;
1015 case 0x30:
1016 sprintf(buf+len, "IEEE 1394\n");
1017 break;
1018 case 0x40:
1019 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1020 " Protocol\n");
1021 break;
1022 case 0x50:
1023 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1024 break;
1025 case 0x60:
1026 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1027 break;
1028 case 0x70:
1029 sprintf(buf+len, "Automation/Drive Interface Transport"
1030 " Protocol\n");
1031 break;
1032 case 0x80:
1033 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1034 break;
1035 default:
1036 sprintf(buf+len, "Unknown 0x%02x\n",
1037 vpd->protocol_identifier);
1038 break;
1041 if (p_buf)
1042 strncpy(p_buf, buf, p_buf_len);
1043 else
1044 pr_debug("%s", buf);
1047 void
1048 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1051 * Check if the Protocol Identifier Valid (PIV) bit is set..
1053 * from spc3r23.pdf section 7.5.1
1055 if (page_83[1] & 0x80) {
1056 vpd->protocol_identifier = (page_83[0] & 0xf0);
1057 vpd->protocol_identifier_set = 1;
1058 transport_dump_vpd_proto_id(vpd, NULL, 0);
1061 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1063 int transport_dump_vpd_assoc(
1064 struct t10_vpd *vpd,
1065 unsigned char *p_buf,
1066 int p_buf_len)
1068 unsigned char buf[VPD_TMP_BUF_SIZE];
1069 int ret = 0;
1070 int len;
1072 memset(buf, 0, VPD_TMP_BUF_SIZE);
1073 len = sprintf(buf, "T10 VPD Identifier Association: ");
1075 switch (vpd->association) {
1076 case 0x00:
1077 sprintf(buf+len, "addressed logical unit\n");
1078 break;
1079 case 0x10:
1080 sprintf(buf+len, "target port\n");
1081 break;
1082 case 0x20:
1083 sprintf(buf+len, "SCSI target device\n");
1084 break;
1085 default:
1086 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1087 ret = -EINVAL;
1088 break;
1091 if (p_buf)
1092 strncpy(p_buf, buf, p_buf_len);
1093 else
1094 pr_debug("%s", buf);
1096 return ret;
1099 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1102 * The VPD identification association..
1104 * from spc3r23.pdf Section 7.6.3.1 Table 297
1106 vpd->association = (page_83[1] & 0x30);
1107 return transport_dump_vpd_assoc(vpd, NULL, 0);
1109 EXPORT_SYMBOL(transport_set_vpd_assoc);
1111 int transport_dump_vpd_ident_type(
1112 struct t10_vpd *vpd,
1113 unsigned char *p_buf,
1114 int p_buf_len)
1116 unsigned char buf[VPD_TMP_BUF_SIZE];
1117 int ret = 0;
1118 int len;
1120 memset(buf, 0, VPD_TMP_BUF_SIZE);
1121 len = sprintf(buf, "T10 VPD Identifier Type: ");
1123 switch (vpd->device_identifier_type) {
1124 case 0x00:
1125 sprintf(buf+len, "Vendor specific\n");
1126 break;
1127 case 0x01:
1128 sprintf(buf+len, "T10 Vendor ID based\n");
1129 break;
1130 case 0x02:
1131 sprintf(buf+len, "EUI-64 based\n");
1132 break;
1133 case 0x03:
1134 sprintf(buf+len, "NAA\n");
1135 break;
1136 case 0x04:
1137 sprintf(buf+len, "Relative target port identifier\n");
1138 break;
1139 case 0x08:
1140 sprintf(buf+len, "SCSI name string\n");
1141 break;
1142 default:
1143 sprintf(buf+len, "Unsupported: 0x%02x\n",
1144 vpd->device_identifier_type);
1145 ret = -EINVAL;
1146 break;
1149 if (p_buf) {
1150 if (p_buf_len < strlen(buf)+1)
1151 return -EINVAL;
1152 strncpy(p_buf, buf, p_buf_len);
1153 } else {
1154 pr_debug("%s", buf);
1157 return ret;
1160 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1163 * The VPD identifier type..
1165 * from spc3r23.pdf Section 7.6.3.1 Table 298
1167 vpd->device_identifier_type = (page_83[1] & 0x0f);
1168 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1170 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1172 int transport_dump_vpd_ident(
1173 struct t10_vpd *vpd,
1174 unsigned char *p_buf,
1175 int p_buf_len)
1177 unsigned char buf[VPD_TMP_BUF_SIZE];
1178 int ret = 0;
1180 memset(buf, 0, VPD_TMP_BUF_SIZE);
1182 switch (vpd->device_identifier_code_set) {
1183 case 0x01: /* Binary */
1184 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1185 &vpd->device_identifier[0]);
1186 break;
1187 case 0x02: /* ASCII */
1188 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1189 &vpd->device_identifier[0]);
1190 break;
1191 case 0x03: /* UTF-8 */
1192 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1193 &vpd->device_identifier[0]);
1194 break;
1195 default:
1196 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1197 " 0x%02x", vpd->device_identifier_code_set);
1198 ret = -EINVAL;
1199 break;
1202 if (p_buf)
1203 strncpy(p_buf, buf, p_buf_len);
1204 else
1205 pr_debug("%s", buf);
1207 return ret;
1211 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1213 static const char hex_str[] = "0123456789abcdef";
1214 int j = 0, i = 4; /* offset to start of the identifer */
1217 * The VPD Code Set (encoding)
1219 * from spc3r23.pdf Section 7.6.3.1 Table 296
1221 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1222 switch (vpd->device_identifier_code_set) {
1223 case 0x01: /* Binary */
1224 vpd->device_identifier[j++] =
1225 hex_str[vpd->device_identifier_type];
1226 while (i < (4 + page_83[3])) {
1227 vpd->device_identifier[j++] =
1228 hex_str[(page_83[i] & 0xf0) >> 4];
1229 vpd->device_identifier[j++] =
1230 hex_str[page_83[i] & 0x0f];
1231 i++;
1233 break;
1234 case 0x02: /* ASCII */
1235 case 0x03: /* UTF-8 */
1236 while (i < (4 + page_83[3]))
1237 vpd->device_identifier[j++] = page_83[i++];
1238 break;
1239 default:
1240 break;
1243 return transport_dump_vpd_ident(vpd, NULL, 0);
1245 EXPORT_SYMBOL(transport_set_vpd_ident);
1247 static void core_setup_task_attr_emulation(struct se_device *dev)
1250 * If this device is from Target_Core_Mod/pSCSI, disable the
1251 * SAM Task Attribute emulation.
1253 * This is currently not available in upsream Linux/SCSI Target
1254 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1256 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1257 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1258 return;
1261 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1262 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1263 " device\n", dev->transport->name,
1264 dev->transport->get_device_rev(dev));
1267 static void scsi_dump_inquiry(struct se_device *dev)
1269 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1270 int i, device_type;
1272 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1274 pr_debug(" Vendor: ");
1275 for (i = 0; i < 8; i++)
1276 if (wwn->vendor[i] >= 0x20)
1277 pr_debug("%c", wwn->vendor[i]);
1278 else
1279 pr_debug(" ");
1281 pr_debug(" Model: ");
1282 for (i = 0; i < 16; i++)
1283 if (wwn->model[i] >= 0x20)
1284 pr_debug("%c", wwn->model[i]);
1285 else
1286 pr_debug(" ");
1288 pr_debug(" Revision: ");
1289 for (i = 0; i < 4; i++)
1290 if (wwn->revision[i] >= 0x20)
1291 pr_debug("%c", wwn->revision[i]);
1292 else
1293 pr_debug(" ");
1295 pr_debug("\n");
1297 device_type = dev->transport->get_device_type(dev);
1298 pr_debug(" Type: %s ", scsi_device_type(device_type));
1299 pr_debug(" ANSI SCSI revision: %02x\n",
1300 dev->transport->get_device_rev(dev));
1303 struct se_device *transport_add_device_to_core_hba(
1304 struct se_hba *hba,
1305 struct se_subsystem_api *transport,
1306 struct se_subsystem_dev *se_dev,
1307 u32 device_flags,
1308 void *transport_dev,
1309 struct se_dev_limits *dev_limits,
1310 const char *inquiry_prod,
1311 const char *inquiry_rev)
1313 int force_pt;
1314 struct se_device *dev;
1316 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1317 if (!dev) {
1318 pr_err("Unable to allocate memory for se_dev_t\n");
1319 return NULL;
1322 transport_init_queue_obj(&dev->dev_queue_obj);
1323 dev->dev_flags = device_flags;
1324 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1325 dev->dev_ptr = transport_dev;
1326 dev->se_hba = hba;
1327 dev->se_sub_dev = se_dev;
1328 dev->transport = transport;
1329 INIT_LIST_HEAD(&dev->dev_list);
1330 INIT_LIST_HEAD(&dev->dev_sep_list);
1331 INIT_LIST_HEAD(&dev->dev_tmr_list);
1332 INIT_LIST_HEAD(&dev->execute_task_list);
1333 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1334 INIT_LIST_HEAD(&dev->state_task_list);
1335 INIT_LIST_HEAD(&dev->qf_cmd_list);
1336 spin_lock_init(&dev->execute_task_lock);
1337 spin_lock_init(&dev->delayed_cmd_lock);
1338 spin_lock_init(&dev->dev_reservation_lock);
1339 spin_lock_init(&dev->dev_status_lock);
1340 spin_lock_init(&dev->se_port_lock);
1341 spin_lock_init(&dev->se_tmr_lock);
1342 spin_lock_init(&dev->qf_cmd_lock);
1344 dev->queue_depth = dev_limits->queue_depth;
1345 atomic_set(&dev->depth_left, dev->queue_depth);
1346 atomic_set(&dev->dev_ordered_id, 0);
1348 se_dev_set_default_attribs(dev, dev_limits);
1350 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1351 dev->creation_time = get_jiffies_64();
1352 spin_lock_init(&dev->stats_lock);
1354 spin_lock(&hba->device_lock);
1355 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1356 hba->dev_count++;
1357 spin_unlock(&hba->device_lock);
1359 * Setup the SAM Task Attribute emulation for struct se_device
1361 core_setup_task_attr_emulation(dev);
1363 * Force PR and ALUA passthrough emulation with internal object use.
1365 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1367 * Setup the Reservations infrastructure for struct se_device
1369 core_setup_reservations(dev, force_pt);
1371 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1373 if (core_setup_alua(dev, force_pt) < 0)
1374 goto out;
1377 * Startup the struct se_device processing thread
1379 dev->process_thread = kthread_run(transport_processing_thread, dev,
1380 "LIO_%s", dev->transport->name);
1381 if (IS_ERR(dev->process_thread)) {
1382 pr_err("Unable to create kthread: LIO_%s\n",
1383 dev->transport->name);
1384 goto out;
1387 * Setup work_queue for QUEUE_FULL
1389 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1391 * Preload the initial INQUIRY const values if we are doing
1392 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1393 * passthrough because this is being provided by the backend LLD.
1394 * This is required so that transport_get_inquiry() copies these
1395 * originals once back into DEV_T10_WWN(dev) for the virtual device
1396 * setup.
1398 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1399 if (!inquiry_prod || !inquiry_rev) {
1400 pr_err("All non TCM/pSCSI plugins require"
1401 " INQUIRY consts\n");
1402 goto out;
1405 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1406 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1407 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1409 scsi_dump_inquiry(dev);
1411 return dev;
1412 out:
1413 kthread_stop(dev->process_thread);
1415 spin_lock(&hba->device_lock);
1416 list_del(&dev->dev_list);
1417 hba->dev_count--;
1418 spin_unlock(&hba->device_lock);
1420 se_release_vpd_for_dev(dev);
1422 kfree(dev);
1424 return NULL;
1426 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1428 /* transport_generic_prepare_cdb():
1430 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1431 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1432 * The point of this is since we are mapping iSCSI LUNs to
1433 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1434 * devices and HBAs for a loop.
1436 static inline void transport_generic_prepare_cdb(
1437 unsigned char *cdb)
1439 switch (cdb[0]) {
1440 case READ_10: /* SBC - RDProtect */
1441 case READ_12: /* SBC - RDProtect */
1442 case READ_16: /* SBC - RDProtect */
1443 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1444 case VERIFY: /* SBC - VRProtect */
1445 case VERIFY_16: /* SBC - VRProtect */
1446 case WRITE_VERIFY: /* SBC - VRProtect */
1447 case WRITE_VERIFY_12: /* SBC - VRProtect */
1448 break;
1449 default:
1450 cdb[1] &= 0x1f; /* clear logical unit number */
1451 break;
1455 static struct se_task *
1456 transport_generic_get_task(struct se_cmd *cmd,
1457 enum dma_data_direction data_direction)
1459 struct se_task *task;
1460 struct se_device *dev = cmd->se_dev;
1462 task = dev->transport->alloc_task(cmd->t_task_cdb);
1463 if (!task) {
1464 pr_err("Unable to allocate struct se_task\n");
1465 return NULL;
1468 INIT_LIST_HEAD(&task->t_list);
1469 INIT_LIST_HEAD(&task->t_execute_list);
1470 INIT_LIST_HEAD(&task->t_state_list);
1471 init_completion(&task->task_stop_comp);
1472 task->task_se_cmd = cmd;
1473 task->task_data_direction = data_direction;
1475 return task;
1478 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1481 * Used by fabric modules containing a local struct se_cmd within their
1482 * fabric dependent per I/O descriptor.
1484 void transport_init_se_cmd(
1485 struct se_cmd *cmd,
1486 struct target_core_fabric_ops *tfo,
1487 struct se_session *se_sess,
1488 u32 data_length,
1489 int data_direction,
1490 int task_attr,
1491 unsigned char *sense_buffer)
1493 INIT_LIST_HEAD(&cmd->se_lun_node);
1494 INIT_LIST_HEAD(&cmd->se_delayed_node);
1495 INIT_LIST_HEAD(&cmd->se_qf_node);
1496 INIT_LIST_HEAD(&cmd->se_queue_node);
1497 INIT_LIST_HEAD(&cmd->se_cmd_list);
1498 INIT_LIST_HEAD(&cmd->t_task_list);
1499 init_completion(&cmd->transport_lun_fe_stop_comp);
1500 init_completion(&cmd->transport_lun_stop_comp);
1501 init_completion(&cmd->t_transport_stop_comp);
1502 init_completion(&cmd->cmd_wait_comp);
1503 spin_lock_init(&cmd->t_state_lock);
1504 atomic_set(&cmd->transport_dev_active, 1);
1506 cmd->se_tfo = tfo;
1507 cmd->se_sess = se_sess;
1508 cmd->data_length = data_length;
1509 cmd->data_direction = data_direction;
1510 cmd->sam_task_attr = task_attr;
1511 cmd->sense_buffer = sense_buffer;
1513 EXPORT_SYMBOL(transport_init_se_cmd);
1515 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1518 * Check if SAM Task Attribute emulation is enabled for this
1519 * struct se_device storage object
1521 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1522 return 0;
1524 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1525 pr_debug("SAM Task Attribute ACA"
1526 " emulation is not supported\n");
1527 return -EINVAL;
1530 * Used to determine when ORDERED commands should go from
1531 * Dormant to Active status.
1533 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1534 smp_mb__after_atomic_inc();
1535 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1536 cmd->se_ordered_id, cmd->sam_task_attr,
1537 cmd->se_dev->transport->name);
1538 return 0;
1541 /* transport_generic_allocate_tasks():
1543 * Called from fabric RX Thread.
1545 int transport_generic_allocate_tasks(
1546 struct se_cmd *cmd,
1547 unsigned char *cdb)
1549 int ret;
1551 transport_generic_prepare_cdb(cdb);
1553 * Ensure that the received CDB is less than the max (252 + 8) bytes
1554 * for VARIABLE_LENGTH_CMD
1556 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1557 pr_err("Received SCSI CDB with command_size: %d that"
1558 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1559 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1560 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1561 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1562 return -EINVAL;
1565 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1566 * allocate the additional extended CDB buffer now.. Otherwise
1567 * setup the pointer from __t_task_cdb to t_task_cdb.
1569 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1570 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1571 GFP_KERNEL);
1572 if (!cmd->t_task_cdb) {
1573 pr_err("Unable to allocate cmd->t_task_cdb"
1574 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1575 scsi_command_size(cdb),
1576 (unsigned long)sizeof(cmd->__t_task_cdb));
1577 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1578 cmd->scsi_sense_reason =
1579 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1580 return -ENOMEM;
1582 } else
1583 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1585 * Copy the original CDB into cmd->
1587 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1589 * Setup the received CDB based on SCSI defined opcodes and
1590 * perform unit attention, persistent reservations and ALUA
1591 * checks for virtual device backends. The cmd->t_task_cdb
1592 * pointer is expected to be setup before we reach this point.
1594 ret = transport_generic_cmd_sequencer(cmd, cdb);
1595 if (ret < 0)
1596 return ret;
1598 * Check for SAM Task Attribute Emulation
1600 if (transport_check_alloc_task_attr(cmd) < 0) {
1601 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1602 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1603 return -EINVAL;
1605 spin_lock(&cmd->se_lun->lun_sep_lock);
1606 if (cmd->se_lun->lun_sep)
1607 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1608 spin_unlock(&cmd->se_lun->lun_sep_lock);
1609 return 0;
1611 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1614 * Used by fabric module frontends to queue tasks directly.
1615 * Many only be used from process context only
1617 int transport_handle_cdb_direct(
1618 struct se_cmd *cmd)
1620 int ret;
1622 if (!cmd->se_lun) {
1623 dump_stack();
1624 pr_err("cmd->se_lun is NULL\n");
1625 return -EINVAL;
1627 if (in_interrupt()) {
1628 dump_stack();
1629 pr_err("transport_generic_handle_cdb cannot be called"
1630 " from interrupt context\n");
1631 return -EINVAL;
1634 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1635 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1636 * in existing usage to ensure that outstanding descriptors are handled
1637 * correctly during shutdown via transport_wait_for_tasks()
1639 * Also, we don't take cmd->t_state_lock here as we only expect
1640 * this to be called for initial descriptor submission.
1642 cmd->t_state = TRANSPORT_NEW_CMD;
1643 atomic_set(&cmd->t_transport_active, 1);
1645 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1646 * so follow TRANSPORT_NEW_CMD processing thread context usage
1647 * and call transport_generic_request_failure() if necessary..
1649 ret = transport_generic_new_cmd(cmd);
1650 if (ret < 0)
1651 transport_generic_request_failure(cmd);
1653 return 0;
1655 EXPORT_SYMBOL(transport_handle_cdb_direct);
1658 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1659 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1660 * complete setup in TCM process context w/ TFO->new_cmd_map().
1662 int transport_generic_handle_cdb_map(
1663 struct se_cmd *cmd)
1665 if (!cmd->se_lun) {
1666 dump_stack();
1667 pr_err("cmd->se_lun is NULL\n");
1668 return -EINVAL;
1671 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
1672 return 0;
1674 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1676 /* transport_generic_handle_data():
1680 int transport_generic_handle_data(
1681 struct se_cmd *cmd)
1684 * For the software fabric case, then we assume the nexus is being
1685 * failed/shutdown when signals are pending from the kthread context
1686 * caller, so we return a failure. For the HW target mode case running
1687 * in interrupt code, the signal_pending() check is skipped.
1689 if (!in_interrupt() && signal_pending(current))
1690 return -EPERM;
1692 * If the received CDB has aleady been ABORTED by the generic
1693 * target engine, we now call transport_check_aborted_status()
1694 * to queue any delated TASK_ABORTED status for the received CDB to the
1695 * fabric module as we are expecting no further incoming DATA OUT
1696 * sequences at this point.
1698 if (transport_check_aborted_status(cmd, 1) != 0)
1699 return 0;
1701 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
1702 return 0;
1704 EXPORT_SYMBOL(transport_generic_handle_data);
1706 /* transport_generic_handle_tmr():
1710 int transport_generic_handle_tmr(
1711 struct se_cmd *cmd)
1713 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
1714 return 0;
1716 EXPORT_SYMBOL(transport_generic_handle_tmr);
1719 * If the task is active, request it to be stopped and sleep until it
1720 * has completed.
1722 bool target_stop_task(struct se_task *task, unsigned long *flags)
1724 struct se_cmd *cmd = task->task_se_cmd;
1725 bool was_active = false;
1727 if (task->task_flags & TF_ACTIVE) {
1728 task->task_flags |= TF_REQUEST_STOP;
1729 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1731 pr_debug("Task %p waiting to complete\n", task);
1732 wait_for_completion(&task->task_stop_comp);
1733 pr_debug("Task %p stopped successfully\n", task);
1735 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1736 atomic_dec(&cmd->t_task_cdbs_left);
1737 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1738 was_active = true;
1741 return was_active;
1744 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1746 struct se_task *task, *task_tmp;
1747 unsigned long flags;
1748 int ret = 0;
1750 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1751 cmd->se_tfo->get_task_tag(cmd));
1754 * No tasks remain in the execution queue
1756 spin_lock_irqsave(&cmd->t_state_lock, flags);
1757 list_for_each_entry_safe(task, task_tmp,
1758 &cmd->t_task_list, t_list) {
1759 pr_debug("Processing task %p\n", task);
1761 * If the struct se_task has not been sent and is not active,
1762 * remove the struct se_task from the execution queue.
1764 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
1765 spin_unlock_irqrestore(&cmd->t_state_lock,
1766 flags);
1767 transport_remove_task_from_execute_queue(task,
1768 cmd->se_dev);
1770 pr_debug("Task %p removed from execute queue\n", task);
1771 spin_lock_irqsave(&cmd->t_state_lock, flags);
1772 continue;
1775 if (!target_stop_task(task, &flags)) {
1776 pr_debug("Task %p - did nothing\n", task);
1777 ret++;
1780 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1782 return ret;
1786 * Handle SAM-esque emulation for generic transport request failures.
1788 static void transport_generic_request_failure(struct se_cmd *cmd)
1790 int ret = 0;
1792 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1793 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1794 cmd->t_task_cdb[0]);
1795 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
1796 cmd->se_tfo->get_cmd_state(cmd),
1797 cmd->t_state, cmd->scsi_sense_reason);
1798 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1799 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1800 " t_transport_active: %d t_transport_stop: %d"
1801 " t_transport_sent: %d\n", cmd->t_task_list_num,
1802 atomic_read(&cmd->t_task_cdbs_left),
1803 atomic_read(&cmd->t_task_cdbs_sent),
1804 atomic_read(&cmd->t_task_cdbs_ex_left),
1805 atomic_read(&cmd->t_transport_active),
1806 atomic_read(&cmd->t_transport_stop),
1807 atomic_read(&cmd->t_transport_sent));
1810 * For SAM Task Attribute emulation for failed struct se_cmd
1812 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1813 transport_complete_task_attr(cmd);
1815 switch (cmd->scsi_sense_reason) {
1816 case TCM_NON_EXISTENT_LUN:
1817 case TCM_UNSUPPORTED_SCSI_OPCODE:
1818 case TCM_INVALID_CDB_FIELD:
1819 case TCM_INVALID_PARAMETER_LIST:
1820 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1821 case TCM_UNKNOWN_MODE_PAGE:
1822 case TCM_WRITE_PROTECTED:
1823 case TCM_CHECK_CONDITION_ABORT_CMD:
1824 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1825 case TCM_CHECK_CONDITION_NOT_READY:
1826 break;
1827 case TCM_RESERVATION_CONFLICT:
1829 * No SENSE Data payload for this case, set SCSI Status
1830 * and queue the response to $FABRIC_MOD.
1832 * Uses linux/include/scsi/scsi.h SAM status codes defs
1834 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1836 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1837 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1838 * CONFLICT STATUS.
1840 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1842 if (cmd->se_sess &&
1843 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1844 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1845 cmd->orig_fe_lun, 0x2C,
1846 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1848 ret = cmd->se_tfo->queue_status(cmd);
1849 if (ret == -EAGAIN || ret == -ENOMEM)
1850 goto queue_full;
1851 goto check_stop;
1852 default:
1853 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1854 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
1855 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1856 break;
1859 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1860 * make the call to transport_send_check_condition_and_sense()
1861 * directly. Otherwise expect the fabric to make the call to
1862 * transport_send_check_condition_and_sense() after handling
1863 * possible unsoliticied write data payloads.
1865 ret = transport_send_check_condition_and_sense(cmd,
1866 cmd->scsi_sense_reason, 0);
1867 if (ret == -EAGAIN || ret == -ENOMEM)
1868 goto queue_full;
1870 check_stop:
1871 transport_lun_remove_cmd(cmd);
1872 if (!transport_cmd_check_stop_to_fabric(cmd))
1874 return;
1876 queue_full:
1877 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1878 transport_handle_queue_full(cmd, cmd->se_dev);
1881 static inline u32 transport_lba_21(unsigned char *cdb)
1883 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1886 static inline u32 transport_lba_32(unsigned char *cdb)
1888 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1891 static inline unsigned long long transport_lba_64(unsigned char *cdb)
1893 unsigned int __v1, __v2;
1895 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1896 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1898 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1902 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1904 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1906 unsigned int __v1, __v2;
1908 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1909 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1911 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1914 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1916 unsigned long flags;
1918 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1919 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1920 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1923 static inline int transport_tcq_window_closed(struct se_device *dev)
1925 if (dev->dev_tcq_window_closed++ <
1926 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
1927 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
1928 } else
1929 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
1931 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
1932 return 0;
1936 * Called from Fabric Module context from transport_execute_tasks()
1938 * The return of this function determins if the tasks from struct se_cmd
1939 * get added to the execution queue in transport_execute_tasks(),
1940 * or are added to the delayed or ordered lists here.
1942 static inline int transport_execute_task_attr(struct se_cmd *cmd)
1944 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1945 return 1;
1947 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1948 * to allow the passed struct se_cmd list of tasks to the front of the list.
1950 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
1951 pr_debug("Added HEAD_OF_QUEUE for CDB:"
1952 " 0x%02x, se_ordered_id: %u\n",
1953 cmd->t_task_cdb[0],
1954 cmd->se_ordered_id);
1955 return 1;
1956 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
1957 atomic_inc(&cmd->se_dev->dev_ordered_sync);
1958 smp_mb__after_atomic_inc();
1960 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
1961 " list, se_ordered_id: %u\n",
1962 cmd->t_task_cdb[0],
1963 cmd->se_ordered_id);
1965 * Add ORDERED command to tail of execution queue if
1966 * no other older commands exist that need to be
1967 * completed first.
1969 if (!atomic_read(&cmd->se_dev->simple_cmds))
1970 return 1;
1971 } else {
1973 * For SIMPLE and UNTAGGED Task Attribute commands
1975 atomic_inc(&cmd->se_dev->simple_cmds);
1976 smp_mb__after_atomic_inc();
1979 * Otherwise if one or more outstanding ORDERED task attribute exist,
1980 * add the dormant task(s) built for the passed struct se_cmd to the
1981 * execution queue and become in Active state for this struct se_device.
1983 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
1985 * Otherwise, add cmd w/ tasks to delayed cmd queue that
1986 * will be drained upon completion of HEAD_OF_QUEUE task.
1988 spin_lock(&cmd->se_dev->delayed_cmd_lock);
1989 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
1990 list_add_tail(&cmd->se_delayed_node,
1991 &cmd->se_dev->delayed_cmd_list);
1992 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
1994 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1995 " delayed CMD list, se_ordered_id: %u\n",
1996 cmd->t_task_cdb[0], cmd->sam_task_attr,
1997 cmd->se_ordered_id);
1999 * Return zero to let transport_execute_tasks() know
2000 * not to add the delayed tasks to the execution list.
2002 return 0;
2005 * Otherwise, no ORDERED task attributes exist..
2007 return 1;
2011 * Called from fabric module context in transport_generic_new_cmd() and
2012 * transport_generic_process_write()
2014 static int transport_execute_tasks(struct se_cmd *cmd)
2016 int add_tasks;
2018 if (se_dev_check_online(cmd->se_dev) != 0) {
2019 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2020 transport_generic_request_failure(cmd);
2021 return 0;
2025 * Call transport_cmd_check_stop() to see if a fabric exception
2026 * has occurred that prevents execution.
2028 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2030 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2031 * attribute for the tasks of the received struct se_cmd CDB
2033 add_tasks = transport_execute_task_attr(cmd);
2034 if (!add_tasks)
2035 goto execute_tasks;
2037 * This calls transport_add_tasks_from_cmd() to handle
2038 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2039 * (if enabled) in __transport_add_task_to_execute_queue() and
2040 * transport_add_task_check_sam_attr().
2042 transport_add_tasks_from_cmd(cmd);
2045 * Kick the execution queue for the cmd associated struct se_device
2046 * storage object.
2048 execute_tasks:
2049 __transport_execute_tasks(cmd->se_dev);
2050 return 0;
2054 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2055 * from struct se_device->execute_task_list and
2057 * Called from transport_processing_thread()
2059 static int __transport_execute_tasks(struct se_device *dev)
2061 int error;
2062 struct se_cmd *cmd = NULL;
2063 struct se_task *task = NULL;
2064 unsigned long flags;
2067 * Check if there is enough room in the device and HBA queue to send
2068 * struct se_tasks to the selected transport.
2070 check_depth:
2071 if (!atomic_read(&dev->depth_left))
2072 return transport_tcq_window_closed(dev);
2074 dev->dev_tcq_window_closed = 0;
2076 spin_lock_irq(&dev->execute_task_lock);
2077 if (list_empty(&dev->execute_task_list)) {
2078 spin_unlock_irq(&dev->execute_task_lock);
2079 return 0;
2081 task = list_first_entry(&dev->execute_task_list,
2082 struct se_task, t_execute_list);
2083 __transport_remove_task_from_execute_queue(task, dev);
2084 spin_unlock_irq(&dev->execute_task_lock);
2086 atomic_dec(&dev->depth_left);
2088 cmd = task->task_se_cmd;
2090 spin_lock_irqsave(&cmd->t_state_lock, flags);
2091 task->task_flags |= (TF_ACTIVE | TF_SENT);
2092 atomic_inc(&cmd->t_task_cdbs_sent);
2094 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2095 cmd->t_task_list_num)
2096 atomic_set(&cmd->t_transport_sent, 1);
2098 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2100 if (cmd->execute_task)
2101 error = cmd->execute_task(task);
2102 else
2103 error = dev->transport->do_task(task);
2104 if (error != 0) {
2105 spin_lock_irqsave(&cmd->t_state_lock, flags);
2106 task->task_flags &= ~TF_ACTIVE;
2107 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2108 atomic_set(&cmd->t_transport_sent, 0);
2109 transport_stop_tasks_for_cmd(cmd);
2110 atomic_inc(&dev->depth_left);
2111 transport_generic_request_failure(cmd);
2114 goto check_depth;
2116 return 0;
2119 static inline u32 transport_get_sectors_6(
2120 unsigned char *cdb,
2121 struct se_cmd *cmd,
2122 int *ret)
2124 struct se_device *dev = cmd->se_dev;
2127 * Assume TYPE_DISK for non struct se_device objects.
2128 * Use 8-bit sector value.
2130 if (!dev)
2131 goto type_disk;
2134 * Use 24-bit allocation length for TYPE_TAPE.
2136 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2137 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2140 * Everything else assume TYPE_DISK Sector CDB location.
2141 * Use 8-bit sector value. SBC-3 says:
2143 * A TRANSFER LENGTH field set to zero specifies that 256
2144 * logical blocks shall be written. Any other value
2145 * specifies the number of logical blocks that shall be
2146 * written.
2148 type_disk:
2149 return cdb[4] ? : 256;
2152 static inline u32 transport_get_sectors_10(
2153 unsigned char *cdb,
2154 struct se_cmd *cmd,
2155 int *ret)
2157 struct se_device *dev = cmd->se_dev;
2160 * Assume TYPE_DISK for non struct se_device objects.
2161 * Use 16-bit sector value.
2163 if (!dev)
2164 goto type_disk;
2167 * XXX_10 is not defined in SSC, throw an exception
2169 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2170 *ret = -EINVAL;
2171 return 0;
2175 * Everything else assume TYPE_DISK Sector CDB location.
2176 * Use 16-bit sector value.
2178 type_disk:
2179 return (u32)(cdb[7] << 8) + cdb[8];
2182 static inline u32 transport_get_sectors_12(
2183 unsigned char *cdb,
2184 struct se_cmd *cmd,
2185 int *ret)
2187 struct se_device *dev = cmd->se_dev;
2190 * Assume TYPE_DISK for non struct se_device objects.
2191 * Use 32-bit sector value.
2193 if (!dev)
2194 goto type_disk;
2197 * XXX_12 is not defined in SSC, throw an exception
2199 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2200 *ret = -EINVAL;
2201 return 0;
2205 * Everything else assume TYPE_DISK Sector CDB location.
2206 * Use 32-bit sector value.
2208 type_disk:
2209 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2212 static inline u32 transport_get_sectors_16(
2213 unsigned char *cdb,
2214 struct se_cmd *cmd,
2215 int *ret)
2217 struct se_device *dev = cmd->se_dev;
2220 * Assume TYPE_DISK for non struct se_device objects.
2221 * Use 32-bit sector value.
2223 if (!dev)
2224 goto type_disk;
2227 * Use 24-bit allocation length for TYPE_TAPE.
2229 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2230 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2232 type_disk:
2233 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2234 (cdb[12] << 8) + cdb[13];
2238 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2240 static inline u32 transport_get_sectors_32(
2241 unsigned char *cdb,
2242 struct se_cmd *cmd,
2243 int *ret)
2246 * Assume TYPE_DISK for non struct se_device objects.
2247 * Use 32-bit sector value.
2249 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2250 (cdb[30] << 8) + cdb[31];
2254 static inline u32 transport_get_size(
2255 u32 sectors,
2256 unsigned char *cdb,
2257 struct se_cmd *cmd)
2259 struct se_device *dev = cmd->se_dev;
2261 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2262 if (cdb[1] & 1) { /* sectors */
2263 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2264 } else /* bytes */
2265 return sectors;
2267 #if 0
2268 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2269 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2270 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2271 dev->transport->name);
2272 #endif
2273 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2276 static void transport_xor_callback(struct se_cmd *cmd)
2278 unsigned char *buf, *addr;
2279 struct scatterlist *sg;
2280 unsigned int offset;
2281 int i;
2282 int count;
2284 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2286 * 1) read the specified logical block(s);
2287 * 2) transfer logical blocks from the data-out buffer;
2288 * 3) XOR the logical blocks transferred from the data-out buffer with
2289 * the logical blocks read, storing the resulting XOR data in a buffer;
2290 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2291 * blocks transferred from the data-out buffer; and
2292 * 5) transfer the resulting XOR data to the data-in buffer.
2294 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2295 if (!buf) {
2296 pr_err("Unable to allocate xor_callback buf\n");
2297 return;
2300 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2301 * into the locally allocated *buf
2303 sg_copy_to_buffer(cmd->t_data_sg,
2304 cmd->t_data_nents,
2305 buf,
2306 cmd->data_length);
2309 * Now perform the XOR against the BIDI read memory located at
2310 * cmd->t_mem_bidi_list
2313 offset = 0;
2314 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2315 addr = kmap_atomic(sg_page(sg), KM_USER0);
2316 if (!addr)
2317 goto out;
2319 for (i = 0; i < sg->length; i++)
2320 *(addr + sg->offset + i) ^= *(buf + offset + i);
2322 offset += sg->length;
2323 kunmap_atomic(addr, KM_USER0);
2326 out:
2327 kfree(buf);
2331 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2333 static int transport_get_sense_data(struct se_cmd *cmd)
2335 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2336 struct se_device *dev = cmd->se_dev;
2337 struct se_task *task = NULL, *task_tmp;
2338 unsigned long flags;
2339 u32 offset = 0;
2341 WARN_ON(!cmd->se_lun);
2343 if (!dev)
2344 return 0;
2346 spin_lock_irqsave(&cmd->t_state_lock, flags);
2347 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2348 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2349 return 0;
2352 list_for_each_entry_safe(task, task_tmp,
2353 &cmd->t_task_list, t_list) {
2354 if (!task->task_sense)
2355 continue;
2357 if (!dev->transport->get_sense_buffer) {
2358 pr_err("dev->transport->get_sense_buffer"
2359 " is NULL\n");
2360 continue;
2363 sense_buffer = dev->transport->get_sense_buffer(task);
2364 if (!sense_buffer) {
2365 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
2366 " sense buffer for task with sense\n",
2367 cmd->se_tfo->get_task_tag(cmd), task);
2368 continue;
2370 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2372 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2373 TRANSPORT_SENSE_BUFFER);
2375 memcpy(&buffer[offset], sense_buffer,
2376 TRANSPORT_SENSE_BUFFER);
2377 cmd->scsi_status = task->task_scsi_status;
2378 /* Automatically padded */
2379 cmd->scsi_sense_length =
2380 (TRANSPORT_SENSE_BUFFER + offset);
2382 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2383 " and sense\n",
2384 dev->se_hba->hba_id, dev->transport->name,
2385 cmd->scsi_status);
2386 return 0;
2388 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2390 return -1;
2393 static inline long long transport_dev_end_lba(struct se_device *dev)
2395 return dev->transport->get_blocks(dev) + 1;
2398 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2400 struct se_device *dev = cmd->se_dev;
2401 u32 sectors;
2403 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2404 return 0;
2406 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2408 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2409 pr_err("LBA: %llu Sectors: %u exceeds"
2410 " transport_dev_end_lba(): %llu\n",
2411 cmd->t_task_lba, sectors,
2412 transport_dev_end_lba(dev));
2413 return -EINVAL;
2416 return 0;
2419 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2422 * Determine if the received WRITE_SAME is used to for direct
2423 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2424 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2425 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2427 int passthrough = (dev->transport->transport_type ==
2428 TRANSPORT_PLUGIN_PHBA_PDEV);
2430 if (!passthrough) {
2431 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2432 pr_err("WRITE_SAME PBDATA and LBDATA"
2433 " bits not supported for Block Discard"
2434 " Emulation\n");
2435 return -ENOSYS;
2438 * Currently for the emulated case we only accept
2439 * tpws with the UNMAP=1 bit set.
2441 if (!(flags[0] & 0x08)) {
2442 pr_err("WRITE_SAME w/o UNMAP bit not"
2443 " supported for Block Discard Emulation\n");
2444 return -ENOSYS;
2448 return 0;
2451 /* transport_generic_cmd_sequencer():
2453 * Generic Command Sequencer that should work for most DAS transport
2454 * drivers.
2456 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2457 * RX Thread.
2459 * FIXME: Need to support other SCSI OPCODES where as well.
2461 static int transport_generic_cmd_sequencer(
2462 struct se_cmd *cmd,
2463 unsigned char *cdb)
2465 struct se_device *dev = cmd->se_dev;
2466 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2467 int ret = 0, sector_ret = 0, passthrough;
2468 u32 sectors = 0, size = 0, pr_reg_type = 0;
2469 u16 service_action;
2470 u8 alua_ascq = 0;
2472 * Check for an existing UNIT ATTENTION condition
2474 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2475 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2476 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2477 return -EINVAL;
2480 * Check status of Asymmetric Logical Unit Assignment port
2482 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2483 if (ret != 0) {
2485 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2486 * The ALUA additional sense code qualifier (ASCQ) is determined
2487 * by the ALUA primary or secondary access state..
2489 if (ret > 0) {
2490 #if 0
2491 pr_debug("[%s]: ALUA TG Port not available,"
2492 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2493 cmd->se_tfo->get_fabric_name(), alua_ascq);
2494 #endif
2495 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2496 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2497 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2498 return -EINVAL;
2500 goto out_invalid_cdb_field;
2503 * Check status for SPC-3 Persistent Reservations
2505 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2506 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2507 cmd, cdb, pr_reg_type) != 0) {
2508 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2509 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2510 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2511 return -EBUSY;
2514 * This means the CDB is allowed for the SCSI Initiator port
2515 * when said port is *NOT* holding the legacy SPC-2 or
2516 * SPC-3 Persistent Reservation.
2521 * If we operate in passthrough mode we skip most CDB emulation and
2522 * instead hand the commands down to the physical SCSI device.
2524 passthrough =
2525 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2527 switch (cdb[0]) {
2528 case READ_6:
2529 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2530 if (sector_ret)
2531 goto out_unsupported_cdb;
2532 size = transport_get_size(sectors, cdb, cmd);
2533 cmd->t_task_lba = transport_lba_21(cdb);
2534 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2535 break;
2536 case READ_10:
2537 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2538 if (sector_ret)
2539 goto out_unsupported_cdb;
2540 size = transport_get_size(sectors, cdb, cmd);
2541 cmd->t_task_lba = transport_lba_32(cdb);
2542 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2543 break;
2544 case READ_12:
2545 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2546 if (sector_ret)
2547 goto out_unsupported_cdb;
2548 size = transport_get_size(sectors, cdb, cmd);
2549 cmd->t_task_lba = transport_lba_32(cdb);
2550 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2551 break;
2552 case READ_16:
2553 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2554 if (sector_ret)
2555 goto out_unsupported_cdb;
2556 size = transport_get_size(sectors, cdb, cmd);
2557 cmd->t_task_lba = transport_lba_64(cdb);
2558 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2559 break;
2560 case WRITE_6:
2561 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2562 if (sector_ret)
2563 goto out_unsupported_cdb;
2564 size = transport_get_size(sectors, cdb, cmd);
2565 cmd->t_task_lba = transport_lba_21(cdb);
2566 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2567 break;
2568 case WRITE_10:
2569 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2570 if (sector_ret)
2571 goto out_unsupported_cdb;
2572 size = transport_get_size(sectors, cdb, cmd);
2573 cmd->t_task_lba = transport_lba_32(cdb);
2574 if (cdb[1] & 0x8)
2575 cmd->se_cmd_flags |= SCF_FUA;
2576 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2577 break;
2578 case WRITE_12:
2579 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2580 if (sector_ret)
2581 goto out_unsupported_cdb;
2582 size = transport_get_size(sectors, cdb, cmd);
2583 cmd->t_task_lba = transport_lba_32(cdb);
2584 if (cdb[1] & 0x8)
2585 cmd->se_cmd_flags |= SCF_FUA;
2586 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2587 break;
2588 case WRITE_16:
2589 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2590 if (sector_ret)
2591 goto out_unsupported_cdb;
2592 size = transport_get_size(sectors, cdb, cmd);
2593 cmd->t_task_lba = transport_lba_64(cdb);
2594 if (cdb[1] & 0x8)
2595 cmd->se_cmd_flags |= SCF_FUA;
2596 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2597 break;
2598 case XDWRITEREAD_10:
2599 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2600 !(cmd->se_cmd_flags & SCF_BIDI))
2601 goto out_invalid_cdb_field;
2602 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2603 if (sector_ret)
2604 goto out_unsupported_cdb;
2605 size = transport_get_size(sectors, cdb, cmd);
2606 cmd->t_task_lba = transport_lba_32(cdb);
2607 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2610 * Do now allow BIDI commands for passthrough mode.
2612 if (passthrough)
2613 goto out_unsupported_cdb;
2616 * Setup BIDI XOR callback to be run after I/O completion.
2618 cmd->transport_complete_callback = &transport_xor_callback;
2619 if (cdb[1] & 0x8)
2620 cmd->se_cmd_flags |= SCF_FUA;
2621 break;
2622 case VARIABLE_LENGTH_CMD:
2623 service_action = get_unaligned_be16(&cdb[8]);
2624 switch (service_action) {
2625 case XDWRITEREAD_32:
2626 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2627 if (sector_ret)
2628 goto out_unsupported_cdb;
2629 size = transport_get_size(sectors, cdb, cmd);
2631 * Use WRITE_32 and READ_32 opcodes for the emulated
2632 * XDWRITE_READ_32 logic.
2634 cmd->t_task_lba = transport_lba_64_ext(cdb);
2635 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2638 * Do now allow BIDI commands for passthrough mode.
2640 if (passthrough)
2641 goto out_unsupported_cdb;
2644 * Setup BIDI XOR callback to be run during after I/O
2645 * completion.
2647 cmd->transport_complete_callback = &transport_xor_callback;
2648 if (cdb[1] & 0x8)
2649 cmd->se_cmd_flags |= SCF_FUA;
2650 break;
2651 case WRITE_SAME_32:
2652 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2653 if (sector_ret)
2654 goto out_unsupported_cdb;
2656 if (sectors)
2657 size = transport_get_size(1, cdb, cmd);
2658 else {
2659 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2660 " supported\n");
2661 goto out_invalid_cdb_field;
2664 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2665 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2667 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2668 goto out_invalid_cdb_field;
2669 if (!passthrough)
2670 cmd->execute_task = target_emulate_write_same;
2671 break;
2672 default:
2673 pr_err("VARIABLE_LENGTH_CMD service action"
2674 " 0x%04x not supported\n", service_action);
2675 goto out_unsupported_cdb;
2677 break;
2678 case MAINTENANCE_IN:
2679 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2680 /* MAINTENANCE_IN from SCC-2 */
2682 * Check for emulated MI_REPORT_TARGET_PGS.
2684 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2685 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2686 cmd->execute_task =
2687 target_emulate_report_target_port_groups;
2689 size = (cdb[6] << 24) | (cdb[7] << 16) |
2690 (cdb[8] << 8) | cdb[9];
2691 } else {
2692 /* GPCMD_SEND_KEY from multi media commands */
2693 size = (cdb[8] << 8) + cdb[9];
2695 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2696 break;
2697 case MODE_SELECT:
2698 size = cdb[4];
2699 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2700 break;
2701 case MODE_SELECT_10:
2702 size = (cdb[7] << 8) + cdb[8];
2703 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2704 break;
2705 case MODE_SENSE:
2706 size = cdb[4];
2707 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2708 if (!passthrough)
2709 cmd->execute_task = target_emulate_modesense;
2710 break;
2711 case MODE_SENSE_10:
2712 size = (cdb[7] << 8) + cdb[8];
2713 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2714 if (!passthrough)
2715 cmd->execute_task = target_emulate_modesense;
2716 break;
2717 case GPCMD_READ_BUFFER_CAPACITY:
2718 case GPCMD_SEND_OPC:
2719 case LOG_SELECT:
2720 case LOG_SENSE:
2721 size = (cdb[7] << 8) + cdb[8];
2722 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2723 break;
2724 case READ_BLOCK_LIMITS:
2725 size = READ_BLOCK_LEN;
2726 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2727 break;
2728 case GPCMD_GET_CONFIGURATION:
2729 case GPCMD_READ_FORMAT_CAPACITIES:
2730 case GPCMD_READ_DISC_INFO:
2731 case GPCMD_READ_TRACK_RZONE_INFO:
2732 size = (cdb[7] << 8) + cdb[8];
2733 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2734 break;
2735 case PERSISTENT_RESERVE_IN:
2736 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2737 cmd->execute_task = target_scsi3_emulate_pr_in;
2738 size = (cdb[7] << 8) + cdb[8];
2739 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2740 break;
2741 case PERSISTENT_RESERVE_OUT:
2742 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2743 cmd->execute_task = target_scsi3_emulate_pr_out;
2744 size = (cdb[7] << 8) + cdb[8];
2745 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2746 break;
2747 case GPCMD_MECHANISM_STATUS:
2748 case GPCMD_READ_DVD_STRUCTURE:
2749 size = (cdb[8] << 8) + cdb[9];
2750 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2751 break;
2752 case READ_POSITION:
2753 size = READ_POSITION_LEN;
2754 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2755 break;
2756 case MAINTENANCE_OUT:
2757 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2758 /* MAINTENANCE_OUT from SCC-2
2760 * Check for emulated MO_SET_TARGET_PGS.
2762 if (cdb[1] == MO_SET_TARGET_PGS &&
2763 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2764 cmd->execute_task =
2765 target_emulate_set_target_port_groups;
2768 size = (cdb[6] << 24) | (cdb[7] << 16) |
2769 (cdb[8] << 8) | cdb[9];
2770 } else {
2771 /* GPCMD_REPORT_KEY from multi media commands */
2772 size = (cdb[8] << 8) + cdb[9];
2774 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2775 break;
2776 case INQUIRY:
2777 size = (cdb[3] << 8) + cdb[4];
2779 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2780 * See spc4r17 section 5.3
2782 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2783 cmd->sam_task_attr = MSG_HEAD_TAG;
2784 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2785 if (!passthrough)
2786 cmd->execute_task = target_emulate_inquiry;
2787 break;
2788 case READ_BUFFER:
2789 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2790 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2791 break;
2792 case READ_CAPACITY:
2793 size = READ_CAP_LEN;
2794 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2795 if (!passthrough)
2796 cmd->execute_task = target_emulate_readcapacity;
2797 break;
2798 case READ_MEDIA_SERIAL_NUMBER:
2799 case SECURITY_PROTOCOL_IN:
2800 case SECURITY_PROTOCOL_OUT:
2801 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2802 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2803 break;
2804 case SERVICE_ACTION_IN:
2805 switch (cmd->t_task_cdb[1] & 0x1f) {
2806 case SAI_READ_CAPACITY_16:
2807 if (!passthrough)
2808 cmd->execute_task =
2809 target_emulate_readcapacity_16;
2810 break;
2811 default:
2812 if (passthrough)
2813 break;
2815 pr_err("Unsupported SA: 0x%02x\n",
2816 cmd->t_task_cdb[1] & 0x1f);
2817 goto out_unsupported_cdb;
2819 /*FALLTHROUGH*/
2820 case ACCESS_CONTROL_IN:
2821 case ACCESS_CONTROL_OUT:
2822 case EXTENDED_COPY:
2823 case READ_ATTRIBUTE:
2824 case RECEIVE_COPY_RESULTS:
2825 case WRITE_ATTRIBUTE:
2826 size = (cdb[10] << 24) | (cdb[11] << 16) |
2827 (cdb[12] << 8) | cdb[13];
2828 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2829 break;
2830 case RECEIVE_DIAGNOSTIC:
2831 case SEND_DIAGNOSTIC:
2832 size = (cdb[3] << 8) | cdb[4];
2833 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2834 break;
2835 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2836 #if 0
2837 case GPCMD_READ_CD:
2838 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2839 size = (2336 * sectors);
2840 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2841 break;
2842 #endif
2843 case READ_TOC:
2844 size = cdb[8];
2845 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2846 break;
2847 case REQUEST_SENSE:
2848 size = cdb[4];
2849 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2850 if (!passthrough)
2851 cmd->execute_task = target_emulate_request_sense;
2852 break;
2853 case READ_ELEMENT_STATUS:
2854 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
2855 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2856 break;
2857 case WRITE_BUFFER:
2858 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2859 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2860 break;
2861 case RESERVE:
2862 case RESERVE_10:
2864 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2865 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2867 if (cdb[0] == RESERVE_10)
2868 size = (cdb[7] << 8) | cdb[8];
2869 else
2870 size = cmd->data_length;
2873 * Setup the legacy emulated handler for SPC-2 and
2874 * >= SPC-3 compatible reservation handling (CRH=1)
2875 * Otherwise, we assume the underlying SCSI logic is
2876 * is running in SPC_PASSTHROUGH, and wants reservations
2877 * emulation disabled.
2879 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2880 cmd->execute_task = target_scsi2_reservation_reserve;
2881 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2882 break;
2883 case RELEASE:
2884 case RELEASE_10:
2886 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2887 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2889 if (cdb[0] == RELEASE_10)
2890 size = (cdb[7] << 8) | cdb[8];
2891 else
2892 size = cmd->data_length;
2894 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2895 cmd->execute_task = target_scsi2_reservation_release;
2896 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2897 break;
2898 case SYNCHRONIZE_CACHE:
2899 case 0x91: /* SYNCHRONIZE_CACHE_16: */
2901 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2903 if (cdb[0] == SYNCHRONIZE_CACHE) {
2904 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2905 cmd->t_task_lba = transport_lba_32(cdb);
2906 } else {
2907 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2908 cmd->t_task_lba = transport_lba_64(cdb);
2910 if (sector_ret)
2911 goto out_unsupported_cdb;
2913 size = transport_get_size(sectors, cdb, cmd);
2914 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2916 if (passthrough)
2917 break;
2920 * Check to ensure that LBA + Range does not exceed past end of
2921 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
2923 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2924 if (transport_cmd_get_valid_sectors(cmd) < 0)
2925 goto out_invalid_cdb_field;
2927 cmd->execute_task = target_emulate_synchronize_cache;
2928 break;
2929 case UNMAP:
2930 size = get_unaligned_be16(&cdb[7]);
2931 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2932 if (!passthrough)
2933 cmd->execute_task = target_emulate_unmap;
2934 break;
2935 case WRITE_SAME_16:
2936 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2937 if (sector_ret)
2938 goto out_unsupported_cdb;
2940 if (sectors)
2941 size = transport_get_size(1, cdb, cmd);
2942 else {
2943 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2944 goto out_invalid_cdb_field;
2947 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
2948 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2950 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2951 goto out_invalid_cdb_field;
2952 if (!passthrough)
2953 cmd->execute_task = target_emulate_write_same;
2954 break;
2955 case WRITE_SAME:
2956 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2957 if (sector_ret)
2958 goto out_unsupported_cdb;
2960 if (sectors)
2961 size = transport_get_size(1, cdb, cmd);
2962 else {
2963 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2964 goto out_invalid_cdb_field;
2967 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2968 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2970 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2971 * of byte 1 bit 3 UNMAP instead of original reserved field
2973 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2974 goto out_invalid_cdb_field;
2975 if (!passthrough)
2976 cmd->execute_task = target_emulate_write_same;
2977 break;
2978 case ALLOW_MEDIUM_REMOVAL:
2979 case ERASE:
2980 case REZERO_UNIT:
2981 case SEEK_10:
2982 case SPACE:
2983 case START_STOP:
2984 case TEST_UNIT_READY:
2985 case VERIFY:
2986 case WRITE_FILEMARKS:
2987 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2988 if (!passthrough)
2989 cmd->execute_task = target_emulate_noop;
2990 break;
2991 case GPCMD_CLOSE_TRACK:
2992 case INITIALIZE_ELEMENT_STATUS:
2993 case GPCMD_LOAD_UNLOAD:
2994 case GPCMD_SET_SPEED:
2995 case MOVE_MEDIUM:
2996 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2997 break;
2998 case REPORT_LUNS:
2999 cmd->execute_task = target_report_luns;
3000 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3002 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3003 * See spc4r17 section 5.3
3005 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3006 cmd->sam_task_attr = MSG_HEAD_TAG;
3007 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3008 break;
3009 default:
3010 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3011 " 0x%02x, sending CHECK_CONDITION.\n",
3012 cmd->se_tfo->get_fabric_name(), cdb[0]);
3013 goto out_unsupported_cdb;
3016 if (size != cmd->data_length) {
3017 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3018 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3019 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3020 cmd->data_length, size, cdb[0]);
3022 cmd->cmd_spdtl = size;
3024 if (cmd->data_direction == DMA_TO_DEVICE) {
3025 pr_err("Rejecting underflow/overflow"
3026 " WRITE data\n");
3027 goto out_invalid_cdb_field;
3030 * Reject READ_* or WRITE_* with overflow/underflow for
3031 * type SCF_SCSI_DATA_SG_IO_CDB.
3033 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3034 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3035 " CDB on non 512-byte sector setup subsystem"
3036 " plugin: %s\n", dev->transport->name);
3037 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3038 goto out_invalid_cdb_field;
3041 if (size > cmd->data_length) {
3042 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3043 cmd->residual_count = (size - cmd->data_length);
3044 } else {
3045 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3046 cmd->residual_count = (cmd->data_length - size);
3048 cmd->data_length = size;
3051 /* reject any command that we don't have a handler for */
3052 if (!(passthrough || cmd->execute_task ||
3053 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3054 goto out_unsupported_cdb;
3056 /* Let's limit control cdbs to a page, for simplicity's sake. */
3057 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3058 size > PAGE_SIZE)
3059 goto out_invalid_cdb_field;
3061 transport_set_supported_SAM_opcode(cmd);
3062 return ret;
3064 out_unsupported_cdb:
3065 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3066 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3067 return -EINVAL;
3068 out_invalid_cdb_field:
3069 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3070 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3071 return -EINVAL;
3075 * Called from I/O completion to determine which dormant/delayed
3076 * and ordered cmds need to have their tasks added to the execution queue.
3078 static void transport_complete_task_attr(struct se_cmd *cmd)
3080 struct se_device *dev = cmd->se_dev;
3081 struct se_cmd *cmd_p, *cmd_tmp;
3082 int new_active_tasks = 0;
3084 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3085 atomic_dec(&dev->simple_cmds);
3086 smp_mb__after_atomic_dec();
3087 dev->dev_cur_ordered_id++;
3088 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3089 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3090 cmd->se_ordered_id);
3091 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3092 dev->dev_cur_ordered_id++;
3093 pr_debug("Incremented dev_cur_ordered_id: %u for"
3094 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3095 cmd->se_ordered_id);
3096 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3097 atomic_dec(&dev->dev_ordered_sync);
3098 smp_mb__after_atomic_dec();
3100 dev->dev_cur_ordered_id++;
3101 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3102 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3105 * Process all commands up to the last received
3106 * ORDERED task attribute which requires another blocking
3107 * boundary
3109 spin_lock(&dev->delayed_cmd_lock);
3110 list_for_each_entry_safe(cmd_p, cmd_tmp,
3111 &dev->delayed_cmd_list, se_delayed_node) {
3113 list_del(&cmd_p->se_delayed_node);
3114 spin_unlock(&dev->delayed_cmd_lock);
3116 pr_debug("Calling add_tasks() for"
3117 " cmd_p: 0x%02x Task Attr: 0x%02x"
3118 " Dormant -> Active, se_ordered_id: %u\n",
3119 cmd_p->t_task_cdb[0],
3120 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3122 transport_add_tasks_from_cmd(cmd_p);
3123 new_active_tasks++;
3125 spin_lock(&dev->delayed_cmd_lock);
3126 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3127 break;
3129 spin_unlock(&dev->delayed_cmd_lock);
3131 * If new tasks have become active, wake up the transport thread
3132 * to do the processing of the Active tasks.
3134 if (new_active_tasks != 0)
3135 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3138 static void transport_complete_qf(struct se_cmd *cmd)
3140 int ret = 0;
3142 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3143 transport_complete_task_attr(cmd);
3145 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3146 ret = cmd->se_tfo->queue_status(cmd);
3147 if (ret)
3148 goto out;
3151 switch (cmd->data_direction) {
3152 case DMA_FROM_DEVICE:
3153 ret = cmd->se_tfo->queue_data_in(cmd);
3154 break;
3155 case DMA_TO_DEVICE:
3156 if (cmd->t_bidi_data_sg) {
3157 ret = cmd->se_tfo->queue_data_in(cmd);
3158 if (ret < 0)
3159 break;
3161 /* Fall through for DMA_TO_DEVICE */
3162 case DMA_NONE:
3163 ret = cmd->se_tfo->queue_status(cmd);
3164 break;
3165 default:
3166 break;
3169 out:
3170 if (ret < 0) {
3171 transport_handle_queue_full(cmd, cmd->se_dev);
3172 return;
3174 transport_lun_remove_cmd(cmd);
3175 transport_cmd_check_stop_to_fabric(cmd);
3178 static void transport_handle_queue_full(
3179 struct se_cmd *cmd,
3180 struct se_device *dev)
3182 spin_lock_irq(&dev->qf_cmd_lock);
3183 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3184 atomic_inc(&dev->dev_qf_count);
3185 smp_mb__after_atomic_inc();
3186 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3188 schedule_work(&cmd->se_dev->qf_work_queue);
3191 static void target_complete_ok_work(struct work_struct *work)
3193 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
3194 int reason = 0, ret;
3197 * Check if we need to move delayed/dormant tasks from cmds on the
3198 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3199 * Attribute.
3201 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3202 transport_complete_task_attr(cmd);
3204 * Check to schedule QUEUE_FULL work, or execute an existing
3205 * cmd->transport_qf_callback()
3207 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3208 schedule_work(&cmd->se_dev->qf_work_queue);
3211 * Check if we need to retrieve a sense buffer from
3212 * the struct se_cmd in question.
3214 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3215 if (transport_get_sense_data(cmd) < 0)
3216 reason = TCM_NON_EXISTENT_LUN;
3219 * Only set when an struct se_task->task_scsi_status returned
3220 * a non GOOD status.
3222 if (cmd->scsi_status) {
3223 ret = transport_send_check_condition_and_sense(
3224 cmd, reason, 1);
3225 if (ret == -EAGAIN || ret == -ENOMEM)
3226 goto queue_full;
3228 transport_lun_remove_cmd(cmd);
3229 transport_cmd_check_stop_to_fabric(cmd);
3230 return;
3234 * Check for a callback, used by amongst other things
3235 * XDWRITE_READ_10 emulation.
3237 if (cmd->transport_complete_callback)
3238 cmd->transport_complete_callback(cmd);
3240 switch (cmd->data_direction) {
3241 case DMA_FROM_DEVICE:
3242 spin_lock(&cmd->se_lun->lun_sep_lock);
3243 if (cmd->se_lun->lun_sep) {
3244 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3245 cmd->data_length;
3247 spin_unlock(&cmd->se_lun->lun_sep_lock);
3249 ret = cmd->se_tfo->queue_data_in(cmd);
3250 if (ret == -EAGAIN || ret == -ENOMEM)
3251 goto queue_full;
3252 break;
3253 case DMA_TO_DEVICE:
3254 spin_lock(&cmd->se_lun->lun_sep_lock);
3255 if (cmd->se_lun->lun_sep) {
3256 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3257 cmd->data_length;
3259 spin_unlock(&cmd->se_lun->lun_sep_lock);
3261 * Check if we need to send READ payload for BIDI-COMMAND
3263 if (cmd->t_bidi_data_sg) {
3264 spin_lock(&cmd->se_lun->lun_sep_lock);
3265 if (cmd->se_lun->lun_sep) {
3266 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3267 cmd->data_length;
3269 spin_unlock(&cmd->se_lun->lun_sep_lock);
3270 ret = cmd->se_tfo->queue_data_in(cmd);
3271 if (ret == -EAGAIN || ret == -ENOMEM)
3272 goto queue_full;
3273 break;
3275 /* Fall through for DMA_TO_DEVICE */
3276 case DMA_NONE:
3277 ret = cmd->se_tfo->queue_status(cmd);
3278 if (ret == -EAGAIN || ret == -ENOMEM)
3279 goto queue_full;
3280 break;
3281 default:
3282 break;
3285 transport_lun_remove_cmd(cmd);
3286 transport_cmd_check_stop_to_fabric(cmd);
3287 return;
3289 queue_full:
3290 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3291 " data_direction: %d\n", cmd, cmd->data_direction);
3292 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3293 transport_handle_queue_full(cmd, cmd->se_dev);
3296 static void transport_free_dev_tasks(struct se_cmd *cmd)
3298 struct se_task *task, *task_tmp;
3299 unsigned long flags;
3300 LIST_HEAD(dispose_list);
3302 spin_lock_irqsave(&cmd->t_state_lock, flags);
3303 list_for_each_entry_safe(task, task_tmp,
3304 &cmd->t_task_list, t_list) {
3305 if (!(task->task_flags & TF_ACTIVE))
3306 list_move_tail(&task->t_list, &dispose_list);
3308 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3310 while (!list_empty(&dispose_list)) {
3311 task = list_first_entry(&dispose_list, struct se_task, t_list);
3313 if (task->task_sg != cmd->t_data_sg &&
3314 task->task_sg != cmd->t_bidi_data_sg)
3315 kfree(task->task_sg);
3317 list_del(&task->t_list);
3319 cmd->se_dev->transport->free_task(task);
3323 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3325 struct scatterlist *sg;
3326 int count;
3328 for_each_sg(sgl, sg, nents, count)
3329 __free_page(sg_page(sg));
3331 kfree(sgl);
3334 static inline void transport_free_pages(struct se_cmd *cmd)
3336 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3337 return;
3339 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3340 cmd->t_data_sg = NULL;
3341 cmd->t_data_nents = 0;
3343 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3344 cmd->t_bidi_data_sg = NULL;
3345 cmd->t_bidi_data_nents = 0;
3349 * transport_put_cmd - release a reference to a command
3350 * @cmd: command to release
3352 * This routine releases our reference to the command and frees it if possible.
3354 static void transport_put_cmd(struct se_cmd *cmd)
3356 unsigned long flags;
3357 int free_tasks = 0;
3359 spin_lock_irqsave(&cmd->t_state_lock, flags);
3360 if (atomic_read(&cmd->t_fe_count)) {
3361 if (!atomic_dec_and_test(&cmd->t_fe_count))
3362 goto out_busy;
3365 if (atomic_read(&cmd->t_se_count)) {
3366 if (!atomic_dec_and_test(&cmd->t_se_count))
3367 goto out_busy;
3370 if (atomic_read(&cmd->transport_dev_active)) {
3371 atomic_set(&cmd->transport_dev_active, 0);
3372 transport_all_task_dev_remove_state(cmd);
3373 free_tasks = 1;
3375 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3377 if (free_tasks != 0)
3378 transport_free_dev_tasks(cmd);
3380 transport_free_pages(cmd);
3381 transport_release_cmd(cmd);
3382 return;
3383 out_busy:
3384 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3388 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3389 * allocating in the core.
3390 * @cmd: Associated se_cmd descriptor
3391 * @mem: SGL style memory for TCM WRITE / READ
3392 * @sg_mem_num: Number of SGL elements
3393 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3394 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3396 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3397 * of parameters.
3399 int transport_generic_map_mem_to_cmd(
3400 struct se_cmd *cmd,
3401 struct scatterlist *sgl,
3402 u32 sgl_count,
3403 struct scatterlist *sgl_bidi,
3404 u32 sgl_bidi_count)
3406 if (!sgl || !sgl_count)
3407 return 0;
3409 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3410 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3412 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3413 * scatterlists already have been set to follow what the fabric
3414 * passes for the original expected data transfer length.
3416 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3417 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3418 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3419 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3420 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3421 return -EINVAL;
3424 cmd->t_data_sg = sgl;
3425 cmd->t_data_nents = sgl_count;
3427 if (sgl_bidi && sgl_bidi_count) {
3428 cmd->t_bidi_data_sg = sgl_bidi;
3429 cmd->t_bidi_data_nents = sgl_bidi_count;
3431 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3434 return 0;
3436 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3438 void *transport_kmap_first_data_page(struct se_cmd *cmd)
3440 struct scatterlist *sg = cmd->t_data_sg;
3442 BUG_ON(!sg);
3444 * We need to take into account a possible offset here for fabrics like
3445 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3446 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3448 return kmap(sg_page(sg)) + sg->offset;
3450 EXPORT_SYMBOL(transport_kmap_first_data_page);
3452 void transport_kunmap_first_data_page(struct se_cmd *cmd)
3454 kunmap(sg_page(cmd->t_data_sg));
3456 EXPORT_SYMBOL(transport_kunmap_first_data_page);
3458 static int
3459 transport_generic_get_mem(struct se_cmd *cmd)
3461 u32 length = cmd->data_length;
3462 unsigned int nents;
3463 struct page *page;
3464 int i = 0;
3466 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3467 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3468 if (!cmd->t_data_sg)
3469 return -ENOMEM;
3471 cmd->t_data_nents = nents;
3472 sg_init_table(cmd->t_data_sg, nents);
3474 while (length) {
3475 u32 page_len = min_t(u32, length, PAGE_SIZE);
3476 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3477 if (!page)
3478 goto out;
3480 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3481 length -= page_len;
3482 i++;
3484 return 0;
3486 out:
3487 while (i >= 0) {
3488 __free_page(sg_page(&cmd->t_data_sg[i]));
3489 i--;
3491 kfree(cmd->t_data_sg);
3492 cmd->t_data_sg = NULL;
3493 return -ENOMEM;
3496 /* Reduce sectors if they are too long for the device */
3497 static inline sector_t transport_limit_task_sectors(
3498 struct se_device *dev,
3499 unsigned long long lba,
3500 sector_t sectors)
3502 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
3504 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3505 if ((lba + sectors) > transport_dev_end_lba(dev))
3506 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
3508 return sectors;
3513 * This function can be used by HW target mode drivers to create a linked
3514 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3515 * This is intended to be called during the completion path by TCM Core
3516 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3518 void transport_do_task_sg_chain(struct se_cmd *cmd)
3520 struct scatterlist *sg_first = NULL;
3521 struct scatterlist *sg_prev = NULL;
3522 int sg_prev_nents = 0;
3523 struct scatterlist *sg;
3524 struct se_task *task;
3525 u32 chained_nents = 0;
3526 int i;
3528 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3531 * Walk the struct se_task list and setup scatterlist chains
3532 * for each contiguously allocated struct se_task->task_sg[].
3534 list_for_each_entry(task, &cmd->t_task_list, t_list) {
3535 if (!task->task_sg)
3536 continue;
3538 if (!sg_first) {
3539 sg_first = task->task_sg;
3540 chained_nents = task->task_sg_nents;
3541 } else {
3542 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
3543 chained_nents += task->task_sg_nents;
3546 * For the padded tasks, use the extra SGL vector allocated
3547 * in transport_allocate_data_tasks() for the sg_prev_nents
3548 * offset into sg_chain() above.
3550 * We do not need the padding for the last task (or a single
3551 * task), but in that case we will never use the sg_prev_nents
3552 * value below which would be incorrect.
3554 sg_prev_nents = (task->task_sg_nents + 1);
3555 sg_prev = task->task_sg;
3558 * Setup the starting pointer and total t_tasks_sg_linked_no including
3559 * padding SGs for linking and to mark the end.
3561 cmd->t_tasks_sg_chained = sg_first;
3562 cmd->t_tasks_sg_chained_no = chained_nents;
3564 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
3565 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3566 cmd->t_tasks_sg_chained_no);
3568 for_each_sg(cmd->t_tasks_sg_chained, sg,
3569 cmd->t_tasks_sg_chained_no, i) {
3571 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
3572 i, sg, sg_page(sg), sg->length, sg->offset);
3573 if (sg_is_chain(sg))
3574 pr_debug("SG: %p sg_is_chain=1\n", sg);
3575 if (sg_is_last(sg))
3576 pr_debug("SG: %p sg_is_last=1\n", sg);
3579 EXPORT_SYMBOL(transport_do_task_sg_chain);
3582 * Break up cmd into chunks transport can handle
3584 static int
3585 transport_allocate_data_tasks(struct se_cmd *cmd,
3586 enum dma_data_direction data_direction,
3587 struct scatterlist *cmd_sg, unsigned int sgl_nents)
3589 struct se_device *dev = cmd->se_dev;
3590 int task_count, i;
3591 unsigned long long lba;
3592 sector_t sectors, dev_max_sectors;
3593 u32 sector_size;
3595 if (transport_cmd_get_valid_sectors(cmd) < 0)
3596 return -EINVAL;
3598 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3599 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
3601 WARN_ON(cmd->data_length % sector_size);
3603 lba = cmd->t_task_lba;
3604 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
3605 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
3608 * If we need just a single task reuse the SG list in the command
3609 * and avoid a lot of work.
3611 if (task_count == 1) {
3612 struct se_task *task;
3613 unsigned long flags;
3615 task = transport_generic_get_task(cmd, data_direction);
3616 if (!task)
3617 return -ENOMEM;
3619 task->task_sg = cmd_sg;
3620 task->task_sg_nents = sgl_nents;
3622 task->task_lba = lba;
3623 task->task_sectors = sectors;
3624 task->task_size = task->task_sectors * sector_size;
3626 spin_lock_irqsave(&cmd->t_state_lock, flags);
3627 list_add_tail(&task->t_list, &cmd->t_task_list);
3628 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3630 return task_count;
3633 for (i = 0; i < task_count; i++) {
3634 struct se_task *task;
3635 unsigned int task_size, task_sg_nents_padded;
3636 struct scatterlist *sg;
3637 unsigned long flags;
3638 int count;
3640 task = transport_generic_get_task(cmd, data_direction);
3641 if (!task)
3642 return -ENOMEM;
3644 task->task_lba = lba;
3645 task->task_sectors = min(sectors, dev_max_sectors);
3646 task->task_size = task->task_sectors * sector_size;
3649 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3650 * in order to calculate the number per task SGL entries
3652 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
3654 * Check if the fabric module driver is requesting that all
3655 * struct se_task->task_sg[] be chained together.. If so,
3656 * then allocate an extra padding SG entry for linking and
3657 * marking the end of the chained SGL for every task except
3658 * the last one for (task_count > 1) operation, or skipping
3659 * the extra padding for the (task_count == 1) case.
3661 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3662 task_sg_nents_padded = (task->task_sg_nents + 1);
3663 } else
3664 task_sg_nents_padded = task->task_sg_nents;
3666 task->task_sg = kmalloc(sizeof(struct scatterlist) *
3667 task_sg_nents_padded, GFP_KERNEL);
3668 if (!task->task_sg) {
3669 cmd->se_dev->transport->free_task(task);
3670 return -ENOMEM;
3673 sg_init_table(task->task_sg, task_sg_nents_padded);
3675 task_size = task->task_size;
3677 /* Build new sgl, only up to task_size */
3678 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
3679 if (cmd_sg->length > task_size)
3680 break;
3682 *sg = *cmd_sg;
3683 task_size -= cmd_sg->length;
3684 cmd_sg = sg_next(cmd_sg);
3687 lba += task->task_sectors;
3688 sectors -= task->task_sectors;
3690 spin_lock_irqsave(&cmd->t_state_lock, flags);
3691 list_add_tail(&task->t_list, &cmd->t_task_list);
3692 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3695 return task_count;
3698 static int
3699 transport_allocate_control_task(struct se_cmd *cmd)
3701 struct se_task *task;
3702 unsigned long flags;
3704 task = transport_generic_get_task(cmd, cmd->data_direction);
3705 if (!task)
3706 return -ENOMEM;
3708 task->task_sg = cmd->t_data_sg;
3709 task->task_size = cmd->data_length;
3710 task->task_sg_nents = cmd->t_data_nents;
3712 spin_lock_irqsave(&cmd->t_state_lock, flags);
3713 list_add_tail(&task->t_list, &cmd->t_task_list);
3714 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3716 /* Success! Return number of tasks allocated */
3717 return 1;
3721 * Allocate any required ressources to execute the command, and either place
3722 * it on the execution queue if possible. For writes we might not have the
3723 * payload yet, thus notify the fabric via a call to ->write_pending instead.
3725 int transport_generic_new_cmd(struct se_cmd *cmd)
3727 struct se_device *dev = cmd->se_dev;
3728 int task_cdbs, task_cdbs_bidi = 0;
3729 int set_counts = 1;
3730 int ret = 0;
3733 * Determine is the TCM fabric module has already allocated physical
3734 * memory, and is directly calling transport_generic_map_mem_to_cmd()
3735 * beforehand.
3737 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3738 cmd->data_length) {
3739 ret = transport_generic_get_mem(cmd);
3740 if (ret < 0)
3741 goto out_fail;
3745 * For BIDI command set up the read tasks first.
3747 if (cmd->t_bidi_data_sg &&
3748 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3749 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3751 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3752 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3753 cmd->t_bidi_data_nents);
3754 if (task_cdbs_bidi <= 0)
3755 goto out_fail;
3757 atomic_inc(&cmd->t_fe_count);
3758 atomic_inc(&cmd->t_se_count);
3759 set_counts = 0;
3762 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3763 task_cdbs = transport_allocate_data_tasks(cmd,
3764 cmd->data_direction, cmd->t_data_sg,
3765 cmd->t_data_nents);
3766 } else {
3767 task_cdbs = transport_allocate_control_task(cmd);
3770 if (task_cdbs < 0)
3771 goto out_fail;
3772 else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
3773 cmd->t_state = TRANSPORT_COMPLETE;
3774 atomic_set(&cmd->t_transport_active, 1);
3775 INIT_WORK(&cmd->work, target_complete_ok_work);
3776 queue_work(target_completion_wq, &cmd->work);
3777 return 0;
3780 if (set_counts) {
3781 atomic_inc(&cmd->t_fe_count);
3782 atomic_inc(&cmd->t_se_count);
3785 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3786 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3787 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
3790 * For WRITEs, let the fabric know its buffer is ready..
3791 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3792 * will be added to the struct se_device execution queue after its WRITE
3793 * data has arrived. (ie: It gets handled by the transport processing
3794 * thread a second time)
3796 if (cmd->data_direction == DMA_TO_DEVICE) {
3797 transport_add_tasks_to_state_queue(cmd);
3798 return transport_generic_write_pending(cmd);
3801 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3802 * to the execution queue.
3804 transport_execute_tasks(cmd);
3805 return 0;
3807 out_fail:
3808 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3809 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3810 return -EINVAL;
3812 EXPORT_SYMBOL(transport_generic_new_cmd);
3814 /* transport_generic_process_write():
3818 void transport_generic_process_write(struct se_cmd *cmd)
3820 transport_execute_tasks(cmd);
3822 EXPORT_SYMBOL(transport_generic_process_write);
3824 static void transport_write_pending_qf(struct se_cmd *cmd)
3826 int ret;
3828 ret = cmd->se_tfo->write_pending(cmd);
3829 if (ret == -EAGAIN || ret == -ENOMEM) {
3830 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3831 cmd);
3832 transport_handle_queue_full(cmd, cmd->se_dev);
3836 static int transport_generic_write_pending(struct se_cmd *cmd)
3838 unsigned long flags;
3839 int ret;
3841 spin_lock_irqsave(&cmd->t_state_lock, flags);
3842 cmd->t_state = TRANSPORT_WRITE_PENDING;
3843 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3846 * Clear the se_cmd for WRITE_PENDING status in order to set
3847 * cmd->t_transport_active=0 so that transport_generic_handle_data
3848 * can be called from HW target mode interrupt code. This is safe
3849 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
3850 * because the se_cmd->se_lun pointer is not being cleared.
3852 transport_cmd_check_stop(cmd, 1, 0);
3855 * Call the fabric write_pending function here to let the
3856 * frontend know that WRITE buffers are ready.
3858 ret = cmd->se_tfo->write_pending(cmd);
3859 if (ret == -EAGAIN || ret == -ENOMEM)
3860 goto queue_full;
3861 else if (ret < 0)
3862 return ret;
3864 return 1;
3866 queue_full:
3867 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
3868 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
3869 transport_handle_queue_full(cmd, cmd->se_dev);
3870 return 0;
3874 * transport_release_cmd - free a command
3875 * @cmd: command to free
3877 * This routine unconditionally frees a command, and reference counting
3878 * or list removal must be done in the caller.
3880 void transport_release_cmd(struct se_cmd *cmd)
3882 BUG_ON(!cmd->se_tfo);
3884 if (cmd->se_tmr_req)
3885 core_tmr_release_req(cmd->se_tmr_req);
3886 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3887 kfree(cmd->t_task_cdb);
3889 * Check if target_wait_for_sess_cmds() is expecting to
3890 * release se_cmd directly here..
3892 if (cmd->check_release != 0 && cmd->se_tfo->check_release_cmd)
3893 if (cmd->se_tfo->check_release_cmd(cmd) != 0)
3894 return;
3896 cmd->se_tfo->release_cmd(cmd);
3898 EXPORT_SYMBOL(transport_release_cmd);
3900 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
3902 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3903 if (wait_for_tasks && cmd->se_tmr_req)
3904 transport_wait_for_tasks(cmd);
3906 transport_release_cmd(cmd);
3907 } else {
3908 if (wait_for_tasks)
3909 transport_wait_for_tasks(cmd);
3911 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3913 if (cmd->se_lun)
3914 transport_lun_remove_cmd(cmd);
3916 transport_free_dev_tasks(cmd);
3918 transport_put_cmd(cmd);
3921 EXPORT_SYMBOL(transport_generic_free_cmd);
3923 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
3924 * @se_sess: session to reference
3925 * @se_cmd: command descriptor to add
3927 void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3929 unsigned long flags;
3931 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3932 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3933 se_cmd->check_release = 1;
3934 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3936 EXPORT_SYMBOL(target_get_sess_cmd);
3938 /* target_put_sess_cmd - Check for active I/O shutdown or list delete
3939 * @se_sess: session to reference
3940 * @se_cmd: command descriptor to drop
3942 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3944 unsigned long flags;
3946 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3947 if (list_empty(&se_cmd->se_cmd_list)) {
3948 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3949 WARN_ON(1);
3950 return 0;
3953 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3954 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3955 complete(&se_cmd->cmd_wait_comp);
3956 return 1;
3958 list_del(&se_cmd->se_cmd_list);
3959 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3961 return 0;
3963 EXPORT_SYMBOL(target_put_sess_cmd);
3965 /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3966 * @se_sess: session to split
3968 void target_splice_sess_cmd_list(struct se_session *se_sess)
3970 struct se_cmd *se_cmd;
3971 unsigned long flags;
3973 WARN_ON(!list_empty(&se_sess->sess_wait_list));
3974 INIT_LIST_HEAD(&se_sess->sess_wait_list);
3976 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3977 se_sess->sess_tearing_down = 1;
3979 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3981 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3982 se_cmd->cmd_wait_set = 1;
3984 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3986 EXPORT_SYMBOL(target_splice_sess_cmd_list);
3988 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
3989 * @se_sess: session to wait for active I/O
3990 * @wait_for_tasks: Make extra transport_wait_for_tasks call
3992 void target_wait_for_sess_cmds(
3993 struct se_session *se_sess,
3994 int wait_for_tasks)
3996 struct se_cmd *se_cmd, *tmp_cmd;
3997 bool rc = false;
3999 list_for_each_entry_safe(se_cmd, tmp_cmd,
4000 &se_sess->sess_wait_list, se_cmd_list) {
4001 list_del(&se_cmd->se_cmd_list);
4003 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4004 " %d\n", se_cmd, se_cmd->t_state,
4005 se_cmd->se_tfo->get_cmd_state(se_cmd));
4007 if (wait_for_tasks) {
4008 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4009 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4010 se_cmd->se_tfo->get_cmd_state(se_cmd));
4012 rc = transport_wait_for_tasks(se_cmd);
4014 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4015 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4016 se_cmd->se_tfo->get_cmd_state(se_cmd));
4019 if (!rc) {
4020 wait_for_completion(&se_cmd->cmd_wait_comp);
4021 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4022 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4023 se_cmd->se_tfo->get_cmd_state(se_cmd));
4026 se_cmd->se_tfo->release_cmd(se_cmd);
4029 EXPORT_SYMBOL(target_wait_for_sess_cmds);
4031 /* transport_lun_wait_for_tasks():
4033 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4034 * an struct se_lun to be successfully shutdown.
4036 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4038 unsigned long flags;
4039 int ret;
4041 * If the frontend has already requested this struct se_cmd to
4042 * be stopped, we can safely ignore this struct se_cmd.
4044 spin_lock_irqsave(&cmd->t_state_lock, flags);
4045 if (atomic_read(&cmd->t_transport_stop)) {
4046 atomic_set(&cmd->transport_lun_stop, 0);
4047 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4048 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
4049 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4050 transport_cmd_check_stop(cmd, 1, 0);
4051 return -EPERM;
4053 atomic_set(&cmd->transport_lun_fe_stop, 1);
4054 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4056 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4058 ret = transport_stop_tasks_for_cmd(cmd);
4060 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4061 " %d\n", cmd, cmd->t_task_list_num, ret);
4062 if (!ret) {
4063 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4064 cmd->se_tfo->get_task_tag(cmd));
4065 wait_for_completion(&cmd->transport_lun_stop_comp);
4066 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4067 cmd->se_tfo->get_task_tag(cmd));
4069 transport_remove_cmd_from_queue(cmd);
4071 return 0;
4074 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4076 struct se_cmd *cmd = NULL;
4077 unsigned long lun_flags, cmd_flags;
4079 * Do exception processing and return CHECK_CONDITION status to the
4080 * Initiator Port.
4082 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4083 while (!list_empty(&lun->lun_cmd_list)) {
4084 cmd = list_first_entry(&lun->lun_cmd_list,
4085 struct se_cmd, se_lun_node);
4086 list_del(&cmd->se_lun_node);
4088 atomic_set(&cmd->transport_lun_active, 0);
4090 * This will notify iscsi_target_transport.c:
4091 * transport_cmd_check_stop() that a LUN shutdown is in
4092 * progress for the iscsi_cmd_t.
4094 spin_lock(&cmd->t_state_lock);
4095 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4096 "_lun_stop for ITT: 0x%08x\n",
4097 cmd->se_lun->unpacked_lun,
4098 cmd->se_tfo->get_task_tag(cmd));
4099 atomic_set(&cmd->transport_lun_stop, 1);
4100 spin_unlock(&cmd->t_state_lock);
4102 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4104 if (!cmd->se_lun) {
4105 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4106 cmd->se_tfo->get_task_tag(cmd),
4107 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4108 BUG();
4111 * If the Storage engine still owns the iscsi_cmd_t, determine
4112 * and/or stop its context.
4114 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4115 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4116 cmd->se_tfo->get_task_tag(cmd));
4118 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4119 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4120 continue;
4123 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4124 "_wait_for_tasks(): SUCCESS\n",
4125 cmd->se_lun->unpacked_lun,
4126 cmd->se_tfo->get_task_tag(cmd));
4128 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4129 if (!atomic_read(&cmd->transport_dev_active)) {
4130 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4131 goto check_cond;
4133 atomic_set(&cmd->transport_dev_active, 0);
4134 transport_all_task_dev_remove_state(cmd);
4135 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4137 transport_free_dev_tasks(cmd);
4139 * The Storage engine stopped this struct se_cmd before it was
4140 * send to the fabric frontend for delivery back to the
4141 * Initiator Node. Return this SCSI CDB back with an
4142 * CHECK_CONDITION status.
4144 check_cond:
4145 transport_send_check_condition_and_sense(cmd,
4146 TCM_NON_EXISTENT_LUN, 0);
4148 * If the fabric frontend is waiting for this iscsi_cmd_t to
4149 * be released, notify the waiting thread now that LU has
4150 * finished accessing it.
4152 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4153 if (atomic_read(&cmd->transport_lun_fe_stop)) {
4154 pr_debug("SE_LUN[%d] - Detected FE stop for"
4155 " struct se_cmd: %p ITT: 0x%08x\n",
4156 lun->unpacked_lun,
4157 cmd, cmd->se_tfo->get_task_tag(cmd));
4159 spin_unlock_irqrestore(&cmd->t_state_lock,
4160 cmd_flags);
4161 transport_cmd_check_stop(cmd, 1, 0);
4162 complete(&cmd->transport_lun_fe_stop_comp);
4163 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4164 continue;
4166 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4167 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4169 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4170 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4172 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4175 static int transport_clear_lun_thread(void *p)
4177 struct se_lun *lun = (struct se_lun *)p;
4179 __transport_clear_lun_from_sessions(lun);
4180 complete(&lun->lun_shutdown_comp);
4182 return 0;
4185 int transport_clear_lun_from_sessions(struct se_lun *lun)
4187 struct task_struct *kt;
4189 kt = kthread_run(transport_clear_lun_thread, lun,
4190 "tcm_cl_%u", lun->unpacked_lun);
4191 if (IS_ERR(kt)) {
4192 pr_err("Unable to start clear_lun thread\n");
4193 return PTR_ERR(kt);
4195 wait_for_completion(&lun->lun_shutdown_comp);
4197 return 0;
4201 * transport_wait_for_tasks - wait for completion to occur
4202 * @cmd: command to wait
4204 * Called from frontend fabric context to wait for storage engine
4205 * to pause and/or release frontend generated struct se_cmd.
4207 bool transport_wait_for_tasks(struct se_cmd *cmd)
4209 unsigned long flags;
4211 spin_lock_irqsave(&cmd->t_state_lock, flags);
4212 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4213 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4214 return false;
4217 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4218 * has been set in transport_set_supported_SAM_opcode().
4220 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4221 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4222 return false;
4225 * If we are already stopped due to an external event (ie: LUN shutdown)
4226 * sleep until the connection can have the passed struct se_cmd back.
4227 * The cmd->transport_lun_stopped_sem will be upped by
4228 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4229 * has completed its operation on the struct se_cmd.
4231 if (atomic_read(&cmd->transport_lun_stop)) {
4233 pr_debug("wait_for_tasks: Stopping"
4234 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4235 "_stop_comp); for ITT: 0x%08x\n",
4236 cmd->se_tfo->get_task_tag(cmd));
4238 * There is a special case for WRITES where a FE exception +
4239 * LUN shutdown means ConfigFS context is still sleeping on
4240 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4241 * We go ahead and up transport_lun_stop_comp just to be sure
4242 * here.
4244 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4245 complete(&cmd->transport_lun_stop_comp);
4246 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4247 spin_lock_irqsave(&cmd->t_state_lock, flags);
4249 transport_all_task_dev_remove_state(cmd);
4251 * At this point, the frontend who was the originator of this
4252 * struct se_cmd, now owns the structure and can be released through
4253 * normal means below.
4255 pr_debug("wait_for_tasks: Stopped"
4256 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4257 "stop_comp); for ITT: 0x%08x\n",
4258 cmd->se_tfo->get_task_tag(cmd));
4260 atomic_set(&cmd->transport_lun_stop, 0);
4262 if (!atomic_read(&cmd->t_transport_active) ||
4263 atomic_read(&cmd->t_transport_aborted)) {
4264 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4265 return false;
4268 atomic_set(&cmd->t_transport_stop, 1);
4270 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4271 " i_state: %d, t_state: %d, t_transport_stop = TRUE\n",
4272 cmd, cmd->se_tfo->get_task_tag(cmd),
4273 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4275 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4277 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4279 wait_for_completion(&cmd->t_transport_stop_comp);
4281 spin_lock_irqsave(&cmd->t_state_lock, flags);
4282 atomic_set(&cmd->t_transport_active, 0);
4283 atomic_set(&cmd->t_transport_stop, 0);
4285 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4286 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4287 cmd->se_tfo->get_task_tag(cmd));
4289 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4291 return true;
4293 EXPORT_SYMBOL(transport_wait_for_tasks);
4295 static int transport_get_sense_codes(
4296 struct se_cmd *cmd,
4297 u8 *asc,
4298 u8 *ascq)
4300 *asc = cmd->scsi_asc;
4301 *ascq = cmd->scsi_ascq;
4303 return 0;
4306 static int transport_set_sense_codes(
4307 struct se_cmd *cmd,
4308 u8 asc,
4309 u8 ascq)
4311 cmd->scsi_asc = asc;
4312 cmd->scsi_ascq = ascq;
4314 return 0;
4317 int transport_send_check_condition_and_sense(
4318 struct se_cmd *cmd,
4319 u8 reason,
4320 int from_transport)
4322 unsigned char *buffer = cmd->sense_buffer;
4323 unsigned long flags;
4324 int offset;
4325 u8 asc = 0, ascq = 0;
4327 spin_lock_irqsave(&cmd->t_state_lock, flags);
4328 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4329 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4330 return 0;
4332 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4333 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4335 if (!reason && from_transport)
4336 goto after_reason;
4338 if (!from_transport)
4339 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4341 * Data Segment and SenseLength of the fabric response PDU.
4343 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4344 * from include/scsi/scsi_cmnd.h
4346 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4347 TRANSPORT_SENSE_BUFFER);
4349 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4350 * SENSE KEY values from include/scsi/scsi.h
4352 switch (reason) {
4353 case TCM_NON_EXISTENT_LUN:
4354 /* CURRENT ERROR */
4355 buffer[offset] = 0x70;
4356 /* ILLEGAL REQUEST */
4357 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4358 /* LOGICAL UNIT NOT SUPPORTED */
4359 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4360 break;
4361 case TCM_UNSUPPORTED_SCSI_OPCODE:
4362 case TCM_SECTOR_COUNT_TOO_MANY:
4363 /* CURRENT ERROR */
4364 buffer[offset] = 0x70;
4365 /* ILLEGAL REQUEST */
4366 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4367 /* INVALID COMMAND OPERATION CODE */
4368 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4369 break;
4370 case TCM_UNKNOWN_MODE_PAGE:
4371 /* CURRENT ERROR */
4372 buffer[offset] = 0x70;
4373 /* ILLEGAL REQUEST */
4374 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4375 /* INVALID FIELD IN CDB */
4376 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4377 break;
4378 case TCM_CHECK_CONDITION_ABORT_CMD:
4379 /* CURRENT ERROR */
4380 buffer[offset] = 0x70;
4381 /* ABORTED COMMAND */
4382 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4383 /* BUS DEVICE RESET FUNCTION OCCURRED */
4384 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4385 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4386 break;
4387 case TCM_INCORRECT_AMOUNT_OF_DATA:
4388 /* CURRENT ERROR */
4389 buffer[offset] = 0x70;
4390 /* ABORTED COMMAND */
4391 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4392 /* WRITE ERROR */
4393 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4394 /* NOT ENOUGH UNSOLICITED DATA */
4395 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4396 break;
4397 case TCM_INVALID_CDB_FIELD:
4398 /* CURRENT ERROR */
4399 buffer[offset] = 0x70;
4400 /* ABORTED COMMAND */
4401 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4402 /* INVALID FIELD IN CDB */
4403 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4404 break;
4405 case TCM_INVALID_PARAMETER_LIST:
4406 /* CURRENT ERROR */
4407 buffer[offset] = 0x70;
4408 /* ABORTED COMMAND */
4409 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4410 /* INVALID FIELD IN PARAMETER LIST */
4411 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4412 break;
4413 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4414 /* CURRENT ERROR */
4415 buffer[offset] = 0x70;
4416 /* ABORTED COMMAND */
4417 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4418 /* WRITE ERROR */
4419 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4420 /* UNEXPECTED_UNSOLICITED_DATA */
4421 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4422 break;
4423 case TCM_SERVICE_CRC_ERROR:
4424 /* CURRENT ERROR */
4425 buffer[offset] = 0x70;
4426 /* ABORTED COMMAND */
4427 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4428 /* PROTOCOL SERVICE CRC ERROR */
4429 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4430 /* N/A */
4431 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4432 break;
4433 case TCM_SNACK_REJECTED:
4434 /* CURRENT ERROR */
4435 buffer[offset] = 0x70;
4436 /* ABORTED COMMAND */
4437 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4438 /* READ ERROR */
4439 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4440 /* FAILED RETRANSMISSION REQUEST */
4441 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4442 break;
4443 case TCM_WRITE_PROTECTED:
4444 /* CURRENT ERROR */
4445 buffer[offset] = 0x70;
4446 /* DATA PROTECT */
4447 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4448 /* WRITE PROTECTED */
4449 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4450 break;
4451 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4452 /* CURRENT ERROR */
4453 buffer[offset] = 0x70;
4454 /* UNIT ATTENTION */
4455 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4456 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4457 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4458 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4459 break;
4460 case TCM_CHECK_CONDITION_NOT_READY:
4461 /* CURRENT ERROR */
4462 buffer[offset] = 0x70;
4463 /* Not Ready */
4464 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4465 transport_get_sense_codes(cmd, &asc, &ascq);
4466 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4467 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4468 break;
4469 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4470 default:
4471 /* CURRENT ERROR */
4472 buffer[offset] = 0x70;
4473 /* ILLEGAL REQUEST */
4474 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4475 /* LOGICAL UNIT COMMUNICATION FAILURE */
4476 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4477 break;
4480 * This code uses linux/include/scsi/scsi.h SAM status codes!
4482 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4484 * Automatically padded, this value is encoded in the fabric's
4485 * data_length response PDU containing the SCSI defined sense data.
4487 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4489 after_reason:
4490 return cmd->se_tfo->queue_status(cmd);
4492 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4494 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4496 int ret = 0;
4498 if (atomic_read(&cmd->t_transport_aborted) != 0) {
4499 if (!send_status ||
4500 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4501 return 1;
4502 #if 0
4503 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4504 " status for CDB: 0x%02x ITT: 0x%08x\n",
4505 cmd->t_task_cdb[0],
4506 cmd->se_tfo->get_task_tag(cmd));
4507 #endif
4508 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4509 cmd->se_tfo->queue_status(cmd);
4510 ret = 1;
4512 return ret;
4514 EXPORT_SYMBOL(transport_check_aborted_status);
4516 void transport_send_task_abort(struct se_cmd *cmd)
4518 unsigned long flags;
4520 spin_lock_irqsave(&cmd->t_state_lock, flags);
4521 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4522 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4523 return;
4525 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4528 * If there are still expected incoming fabric WRITEs, we wait
4529 * until until they have completed before sending a TASK_ABORTED
4530 * response. This response with TASK_ABORTED status will be
4531 * queued back to fabric module by transport_check_aborted_status().
4533 if (cmd->data_direction == DMA_TO_DEVICE) {
4534 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4535 atomic_inc(&cmd->t_transport_aborted);
4536 smp_mb__after_atomic_inc();
4539 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4540 #if 0
4541 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4542 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4543 cmd->se_tfo->get_task_tag(cmd));
4544 #endif
4545 cmd->se_tfo->queue_status(cmd);
4548 /* transport_generic_do_tmr():
4552 int transport_generic_do_tmr(struct se_cmd *cmd)
4554 struct se_device *dev = cmd->se_dev;
4555 struct se_tmr_req *tmr = cmd->se_tmr_req;
4556 int ret;
4558 switch (tmr->function) {
4559 case TMR_ABORT_TASK:
4560 tmr->response = TMR_FUNCTION_REJECTED;
4561 break;
4562 case TMR_ABORT_TASK_SET:
4563 case TMR_CLEAR_ACA:
4564 case TMR_CLEAR_TASK_SET:
4565 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4566 break;
4567 case TMR_LUN_RESET:
4568 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4569 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4570 TMR_FUNCTION_REJECTED;
4571 break;
4572 case TMR_TARGET_WARM_RESET:
4573 tmr->response = TMR_FUNCTION_REJECTED;
4574 break;
4575 case TMR_TARGET_COLD_RESET:
4576 tmr->response = TMR_FUNCTION_REJECTED;
4577 break;
4578 default:
4579 pr_err("Uknown TMR function: 0x%02x.\n",
4580 tmr->function);
4581 tmr->response = TMR_FUNCTION_REJECTED;
4582 break;
4585 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4586 cmd->se_tfo->queue_tm_rsp(cmd);
4588 transport_cmd_check_stop_to_fabric(cmd);
4589 return 0;
4592 /* transport_processing_thread():
4596 static int transport_processing_thread(void *param)
4598 int ret;
4599 struct se_cmd *cmd;
4600 struct se_device *dev = (struct se_device *) param;
4602 while (!kthread_should_stop()) {
4603 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4604 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4605 kthread_should_stop());
4606 if (ret < 0)
4607 goto out;
4609 get_cmd:
4610 __transport_execute_tasks(dev);
4612 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4613 if (!cmd)
4614 continue;
4616 switch (cmd->t_state) {
4617 case TRANSPORT_NEW_CMD:
4618 BUG();
4619 break;
4620 case TRANSPORT_NEW_CMD_MAP:
4621 if (!cmd->se_tfo->new_cmd_map) {
4622 pr_err("cmd->se_tfo->new_cmd_map is"
4623 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4624 BUG();
4626 ret = cmd->se_tfo->new_cmd_map(cmd);
4627 if (ret < 0) {
4628 transport_generic_request_failure(cmd);
4629 break;
4631 ret = transport_generic_new_cmd(cmd);
4632 if (ret < 0) {
4633 transport_generic_request_failure(cmd);
4634 break;
4636 break;
4637 case TRANSPORT_PROCESS_WRITE:
4638 transport_generic_process_write(cmd);
4639 break;
4640 case TRANSPORT_PROCESS_TMR:
4641 transport_generic_do_tmr(cmd);
4642 break;
4643 case TRANSPORT_COMPLETE_QF_WP:
4644 transport_write_pending_qf(cmd);
4645 break;
4646 case TRANSPORT_COMPLETE_QF_OK:
4647 transport_complete_qf(cmd);
4648 break;
4649 default:
4650 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4651 "i_state: %d on SE LUN: %u\n",
4652 cmd->t_state,
4653 cmd->se_tfo->get_task_tag(cmd),
4654 cmd->se_tfo->get_cmd_state(cmd),
4655 cmd->se_lun->unpacked_lun);
4656 BUG();
4659 goto get_cmd;
4662 out:
4663 WARN_ON(!list_empty(&dev->state_task_list));
4664 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4665 dev->process_thread = NULL;
4666 return 0;