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>
38 #include <linux/cdrom.h>
39 #include <linux/module.h>
40 #include <linux/ratelimit.h>
41 #include <asm/unaligned.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/scsi_tcq.h>
48 #include <target/target_core_base.h>
49 #include <target/target_core_backend.h>
50 #include <target/target_core_fabric.h>
51 #include <target/target_core_configfs.h>
53 #include "target_core_internal.h"
54 #include "target_core_alua.h"
55 #include "target_core_pr.h"
56 #include "target_core_ua.h"
58 static int sub_api_initialized;
60 static struct workqueue_struct *target_completion_wq;
61 static struct kmem_cache *se_sess_cache;
62 struct kmem_cache *se_ua_cache;
63 struct kmem_cache *t10_pr_reg_cache;
64 struct kmem_cache *t10_alua_lu_gp_cache;
65 struct kmem_cache *t10_alua_lu_gp_mem_cache;
66 struct kmem_cache *t10_alua_tg_pt_gp_cache;
67 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
69 static int transport_generic_write_pending(struct se_cmd *);
70 static int transport_processing_thread(void *param);
71 static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *);
72 static void transport_complete_task_attr(struct se_cmd *cmd);
73 static void transport_handle_queue_full(struct se_cmd *cmd,
74 struct se_device *dev);
75 static int transport_generic_get_mem(struct se_cmd *cmd);
76 static void transport_put_cmd(struct se_cmd *cmd);
77 static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
78 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
79 static void target_complete_ok_work(struct work_struct *work);
81 int init_se_kmem_caches(void)
83 se_sess_cache = kmem_cache_create("se_sess_cache",
84 sizeof(struct se_session), __alignof__(struct se_session),
87 pr_err("kmem_cache_create() for struct se_session"
91 se_ua_cache = kmem_cache_create("se_ua_cache",
92 sizeof(struct se_ua), __alignof__(struct se_ua),
95 pr_err("kmem_cache_create() for struct se_ua failed\n");
96 goto out_free_sess_cache;
98 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
99 sizeof(struct t10_pr_registration),
100 __alignof__(struct t10_pr_registration), 0, NULL);
101 if (!t10_pr_reg_cache) {
102 pr_err("kmem_cache_create() for struct t10_pr_registration"
104 goto out_free_ua_cache;
106 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
107 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
109 if (!t10_alua_lu_gp_cache) {
110 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
112 goto out_free_pr_reg_cache;
114 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
115 sizeof(struct t10_alua_lu_gp_member),
116 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
117 if (!t10_alua_lu_gp_mem_cache) {
118 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
120 goto out_free_lu_gp_cache;
122 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
123 sizeof(struct t10_alua_tg_pt_gp),
124 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
125 if (!t10_alua_tg_pt_gp_cache) {
126 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
128 goto out_free_lu_gp_mem_cache;
130 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
131 "t10_alua_tg_pt_gp_mem_cache",
132 sizeof(struct t10_alua_tg_pt_gp_member),
133 __alignof__(struct t10_alua_tg_pt_gp_member),
135 if (!t10_alua_tg_pt_gp_mem_cache) {
136 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
138 goto out_free_tg_pt_gp_cache;
141 target_completion_wq = alloc_workqueue("target_completion",
143 if (!target_completion_wq)
144 goto out_free_tg_pt_gp_mem_cache;
148 out_free_tg_pt_gp_mem_cache:
149 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
150 out_free_tg_pt_gp_cache:
151 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
152 out_free_lu_gp_mem_cache:
153 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
154 out_free_lu_gp_cache:
155 kmem_cache_destroy(t10_alua_lu_gp_cache);
156 out_free_pr_reg_cache:
157 kmem_cache_destroy(t10_pr_reg_cache);
159 kmem_cache_destroy(se_ua_cache);
161 kmem_cache_destroy(se_sess_cache);
166 void release_se_kmem_caches(void)
168 destroy_workqueue(target_completion_wq);
169 kmem_cache_destroy(se_sess_cache);
170 kmem_cache_destroy(se_ua_cache);
171 kmem_cache_destroy(t10_pr_reg_cache);
172 kmem_cache_destroy(t10_alua_lu_gp_cache);
173 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
174 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
175 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
178 /* This code ensures unique mib indexes are handed out. */
179 static DEFINE_SPINLOCK(scsi_mib_index_lock);
180 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
183 * Allocate a new row index for the entry type specified
185 u32 scsi_get_new_index(scsi_index_t type)
189 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
191 spin_lock(&scsi_mib_index_lock);
192 new_index = ++scsi_mib_index[type];
193 spin_unlock(&scsi_mib_index_lock);
198 static void transport_init_queue_obj(struct se_queue_obj *qobj)
200 atomic_set(&qobj->queue_cnt, 0);
201 INIT_LIST_HEAD(&qobj->qobj_list);
202 init_waitqueue_head(&qobj->thread_wq);
203 spin_lock_init(&qobj->cmd_queue_lock);
206 void transport_subsystem_check_init(void)
210 if (sub_api_initialized)
213 ret = request_module("target_core_iblock");
215 pr_err("Unable to load target_core_iblock\n");
217 ret = request_module("target_core_file");
219 pr_err("Unable to load target_core_file\n");
221 ret = request_module("target_core_pscsi");
223 pr_err("Unable to load target_core_pscsi\n");
225 ret = request_module("target_core_stgt");
227 pr_err("Unable to load target_core_stgt\n");
229 sub_api_initialized = 1;
233 struct se_session *transport_init_session(void)
235 struct se_session *se_sess;
237 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
239 pr_err("Unable to allocate struct se_session from"
241 return ERR_PTR(-ENOMEM);
243 INIT_LIST_HEAD(&se_sess->sess_list);
244 INIT_LIST_HEAD(&se_sess->sess_acl_list);
245 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
246 INIT_LIST_HEAD(&se_sess->sess_wait_list);
247 spin_lock_init(&se_sess->sess_cmd_lock);
248 kref_init(&se_sess->sess_kref);
252 EXPORT_SYMBOL(transport_init_session);
255 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
257 void __transport_register_session(
258 struct se_portal_group *se_tpg,
259 struct se_node_acl *se_nacl,
260 struct se_session *se_sess,
261 void *fabric_sess_ptr)
263 unsigned char buf[PR_REG_ISID_LEN];
265 se_sess->se_tpg = se_tpg;
266 se_sess->fabric_sess_ptr = fabric_sess_ptr;
268 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
270 * Only set for struct se_session's that will actually be moving I/O.
271 * eg: *NOT* discovery sessions.
275 * If the fabric module supports an ISID based TransportID,
276 * save this value in binary from the fabric I_T Nexus now.
278 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
279 memset(&buf[0], 0, PR_REG_ISID_LEN);
280 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
281 &buf[0], PR_REG_ISID_LEN);
282 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
284 kref_get(&se_nacl->acl_kref);
286 spin_lock_irq(&se_nacl->nacl_sess_lock);
288 * The se_nacl->nacl_sess pointer will be set to the
289 * last active I_T Nexus for each struct se_node_acl.
291 se_nacl->nacl_sess = se_sess;
293 list_add_tail(&se_sess->sess_acl_list,
294 &se_nacl->acl_sess_list);
295 spin_unlock_irq(&se_nacl->nacl_sess_lock);
297 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
299 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
300 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
302 EXPORT_SYMBOL(__transport_register_session);
304 void transport_register_session(
305 struct se_portal_group *se_tpg,
306 struct se_node_acl *se_nacl,
307 struct se_session *se_sess,
308 void *fabric_sess_ptr)
312 spin_lock_irqsave(&se_tpg->session_lock, flags);
313 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
314 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
316 EXPORT_SYMBOL(transport_register_session);
318 static void target_release_session(struct kref *kref)
320 struct se_session *se_sess = container_of(kref,
321 struct se_session, sess_kref);
322 struct se_portal_group *se_tpg = se_sess->se_tpg;
324 se_tpg->se_tpg_tfo->close_session(se_sess);
327 void target_get_session(struct se_session *se_sess)
329 kref_get(&se_sess->sess_kref);
331 EXPORT_SYMBOL(target_get_session);
333 void target_put_session(struct se_session *se_sess)
335 kref_put(&se_sess->sess_kref, target_release_session);
337 EXPORT_SYMBOL(target_put_session);
339 static void target_complete_nacl(struct kref *kref)
341 struct se_node_acl *nacl = container_of(kref,
342 struct se_node_acl, acl_kref);
344 complete(&nacl->acl_free_comp);
347 void target_put_nacl(struct se_node_acl *nacl)
349 kref_put(&nacl->acl_kref, target_complete_nacl);
352 void transport_deregister_session_configfs(struct se_session *se_sess)
354 struct se_node_acl *se_nacl;
357 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
359 se_nacl = se_sess->se_node_acl;
361 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
362 if (se_nacl->acl_stop == 0)
363 list_del(&se_sess->sess_acl_list);
365 * If the session list is empty, then clear the pointer.
366 * Otherwise, set the struct se_session pointer from the tail
367 * element of the per struct se_node_acl active session list.
369 if (list_empty(&se_nacl->acl_sess_list))
370 se_nacl->nacl_sess = NULL;
372 se_nacl->nacl_sess = container_of(
373 se_nacl->acl_sess_list.prev,
374 struct se_session, sess_acl_list);
376 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
379 EXPORT_SYMBOL(transport_deregister_session_configfs);
381 void transport_free_session(struct se_session *se_sess)
383 kmem_cache_free(se_sess_cache, se_sess);
385 EXPORT_SYMBOL(transport_free_session);
387 void transport_deregister_session(struct se_session *se_sess)
389 struct se_portal_group *se_tpg = se_sess->se_tpg;
390 struct target_core_fabric_ops *se_tfo;
391 struct se_node_acl *se_nacl;
393 bool comp_nacl = true;
396 transport_free_session(se_sess);
399 se_tfo = se_tpg->se_tpg_tfo;
401 spin_lock_irqsave(&se_tpg->session_lock, flags);
402 list_del(&se_sess->sess_list);
403 se_sess->se_tpg = NULL;
404 se_sess->fabric_sess_ptr = NULL;
405 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
408 * Determine if we need to do extra work for this initiator node's
409 * struct se_node_acl if it had been previously dynamically generated.
411 se_nacl = se_sess->se_node_acl;
413 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
414 if (se_nacl && se_nacl->dynamic_node_acl) {
415 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
416 list_del(&se_nacl->acl_list);
417 se_tpg->num_node_acls--;
418 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
419 core_tpg_wait_for_nacl_pr_ref(se_nacl);
420 core_free_device_list_for_node(se_nacl, se_tpg);
421 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
424 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
427 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
429 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
430 se_tpg->se_tpg_tfo->get_fabric_name());
432 * If last kref is dropping now for an explict NodeACL, awake sleeping
433 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
436 if (se_nacl && comp_nacl == true)
437 target_put_nacl(se_nacl);
439 transport_free_session(se_sess);
441 EXPORT_SYMBOL(transport_deregister_session);
444 * Called with cmd->t_state_lock held.
446 static void target_remove_from_state_list(struct se_cmd *cmd)
448 struct se_device *dev = cmd->se_dev;
454 if (cmd->transport_state & CMD_T_BUSY)
457 spin_lock_irqsave(&dev->execute_task_lock, flags);
458 if (cmd->state_active) {
459 list_del(&cmd->state_list);
460 cmd->state_active = false;
462 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
465 /* transport_cmd_check_stop():
467 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
468 * 'transport_off = 2' determines if task_dev_state should be removed.
470 * A non-zero u8 t_state sets cmd->t_state.
471 * Returns 1 when command is stopped, else 0.
473 static int transport_cmd_check_stop(
480 spin_lock_irqsave(&cmd->t_state_lock, flags);
482 * Determine if IOCTL context caller in requesting the stopping of this
483 * command for LUN shutdown purposes.
485 if (cmd->transport_state & CMD_T_LUN_STOP) {
486 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
487 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
489 cmd->transport_state &= ~CMD_T_ACTIVE;
490 if (transport_off == 2)
491 target_remove_from_state_list(cmd);
492 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
494 complete(&cmd->transport_lun_stop_comp);
498 * Determine if frontend context caller is requesting the stopping of
499 * this command for frontend exceptions.
501 if (cmd->transport_state & CMD_T_STOP) {
502 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
504 cmd->se_tfo->get_task_tag(cmd));
506 if (transport_off == 2)
507 target_remove_from_state_list(cmd);
510 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
513 if (transport_off == 2)
515 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
517 complete(&cmd->t_transport_stop_comp);
521 cmd->transport_state &= ~CMD_T_ACTIVE;
522 if (transport_off == 2) {
523 target_remove_from_state_list(cmd);
525 * Clear struct se_cmd->se_lun before the transport_off == 2
526 * handoff to fabric module.
530 * Some fabric modules like tcm_loop can release
531 * their internally allocated I/O reference now and
534 * Fabric modules are expected to return '1' here if the
535 * se_cmd being passed is released at this point,
536 * or zero if not being released.
538 if (cmd->se_tfo->check_stop_free != NULL) {
539 spin_unlock_irqrestore(
540 &cmd->t_state_lock, flags);
542 return cmd->se_tfo->check_stop_free(cmd);
545 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
549 cmd->t_state = t_state;
550 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
555 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
557 return transport_cmd_check_stop(cmd, 2, 0);
560 static void transport_lun_remove_cmd(struct se_cmd *cmd)
562 struct se_lun *lun = cmd->se_lun;
568 spin_lock_irqsave(&cmd->t_state_lock, flags);
569 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
570 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
571 target_remove_from_state_list(cmd);
573 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
575 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
576 if (!list_empty(&cmd->se_lun_node))
577 list_del_init(&cmd->se_lun_node);
578 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
581 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
583 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
584 transport_lun_remove_cmd(cmd);
586 if (transport_cmd_check_stop_to_fabric(cmd))
589 transport_remove_cmd_from_queue(cmd);
590 transport_put_cmd(cmd);
594 static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
597 struct se_device *dev = cmd->se_dev;
598 struct se_queue_obj *qobj = &dev->dev_queue_obj;
602 spin_lock_irqsave(&cmd->t_state_lock, flags);
603 cmd->t_state = t_state;
604 cmd->transport_state |= CMD_T_ACTIVE;
605 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
608 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
610 /* If the cmd is already on the list, remove it before we add it */
611 if (!list_empty(&cmd->se_queue_node))
612 list_del(&cmd->se_queue_node);
614 atomic_inc(&qobj->queue_cnt);
617 list_add(&cmd->se_queue_node, &qobj->qobj_list);
619 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
620 cmd->transport_state |= CMD_T_QUEUED;
621 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
623 wake_up_interruptible(&qobj->thread_wq);
626 static struct se_cmd *
627 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
632 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
633 if (list_empty(&qobj->qobj_list)) {
634 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
637 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
639 cmd->transport_state &= ~CMD_T_QUEUED;
640 list_del_init(&cmd->se_queue_node);
641 atomic_dec(&qobj->queue_cnt);
642 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
647 static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
649 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
652 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
653 if (!(cmd->transport_state & CMD_T_QUEUED)) {
654 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
657 cmd->transport_state &= ~CMD_T_QUEUED;
658 atomic_dec(&qobj->queue_cnt);
659 list_del_init(&cmd->se_queue_node);
660 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
663 static void target_complete_failure_work(struct work_struct *work)
665 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
667 transport_generic_request_failure(cmd);
670 void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
672 struct se_device *dev = cmd->se_dev;
673 int success = scsi_status == GOOD;
676 cmd->scsi_status = scsi_status;
679 spin_lock_irqsave(&cmd->t_state_lock, flags);
680 cmd->transport_state &= ~CMD_T_BUSY;
682 if (dev && dev->transport->transport_complete) {
683 if (dev->transport->transport_complete(cmd,
684 cmd->t_data_sg) != 0) {
685 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
691 * See if we are waiting to complete for an exception condition.
693 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
694 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
695 complete(&cmd->task_stop_comp);
700 cmd->transport_state |= CMD_T_FAILED;
703 * Check for case where an explict ABORT_TASK has been received
704 * and transport_wait_for_tasks() will be waiting for completion..
706 if (cmd->transport_state & CMD_T_ABORTED &&
707 cmd->transport_state & CMD_T_STOP) {
708 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
709 complete(&cmd->t_transport_stop_comp);
711 } else if (cmd->transport_state & CMD_T_FAILED) {
712 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
713 INIT_WORK(&cmd->work, target_complete_failure_work);
715 INIT_WORK(&cmd->work, target_complete_ok_work);
718 cmd->t_state = TRANSPORT_COMPLETE;
719 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
720 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
722 queue_work(target_completion_wq, &cmd->work);
724 EXPORT_SYMBOL(target_complete_cmd);
726 static void target_add_to_state_list(struct se_cmd *cmd)
728 struct se_device *dev = cmd->se_dev;
731 spin_lock_irqsave(&dev->execute_task_lock, flags);
732 if (!cmd->state_active) {
733 list_add_tail(&cmd->state_list, &dev->state_list);
734 cmd->state_active = true;
736 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
739 static void __target_add_to_execute_list(struct se_cmd *cmd)
741 struct se_device *dev = cmd->se_dev;
742 bool head_of_queue = false;
744 if (!list_empty(&cmd->execute_list))
747 if (dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED &&
748 cmd->sam_task_attr == MSG_HEAD_TAG)
749 head_of_queue = true;
752 list_add(&cmd->execute_list, &dev->execute_list);
754 list_add_tail(&cmd->execute_list, &dev->execute_list);
756 atomic_inc(&dev->execute_tasks);
758 if (cmd->state_active)
762 list_add(&cmd->state_list, &dev->state_list);
764 list_add_tail(&cmd->state_list, &dev->state_list);
766 cmd->state_active = true;
769 static void target_add_to_execute_list(struct se_cmd *cmd)
772 struct se_device *dev = cmd->se_dev;
774 spin_lock_irqsave(&dev->execute_task_lock, flags);
775 __target_add_to_execute_list(cmd);
776 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
779 void __target_remove_from_execute_list(struct se_cmd *cmd)
781 list_del_init(&cmd->execute_list);
782 atomic_dec(&cmd->se_dev->execute_tasks);
785 static void target_remove_from_execute_list(struct se_cmd *cmd)
787 struct se_device *dev = cmd->se_dev;
790 if (WARN_ON(list_empty(&cmd->execute_list)))
793 spin_lock_irqsave(&dev->execute_task_lock, flags);
794 __target_remove_from_execute_list(cmd);
795 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
799 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
802 static void target_qf_do_work(struct work_struct *work)
804 struct se_device *dev = container_of(work, struct se_device,
806 LIST_HEAD(qf_cmd_list);
807 struct se_cmd *cmd, *cmd_tmp;
809 spin_lock_irq(&dev->qf_cmd_lock);
810 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
811 spin_unlock_irq(&dev->qf_cmd_lock);
813 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
814 list_del(&cmd->se_qf_node);
815 atomic_dec(&dev->dev_qf_count);
816 smp_mb__after_atomic_dec();
818 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
819 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
820 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
821 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
824 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
828 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
830 switch (cmd->data_direction) {
833 case DMA_FROM_DEVICE:
837 case DMA_BIDIRECTIONAL:
846 void transport_dump_dev_state(
847 struct se_device *dev,
851 *bl += sprintf(b + *bl, "Status: ");
852 switch (dev->dev_status) {
853 case TRANSPORT_DEVICE_ACTIVATED:
854 *bl += sprintf(b + *bl, "ACTIVATED");
856 case TRANSPORT_DEVICE_DEACTIVATED:
857 *bl += sprintf(b + *bl, "DEACTIVATED");
859 case TRANSPORT_DEVICE_SHUTDOWN:
860 *bl += sprintf(b + *bl, "SHUTDOWN");
862 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
863 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
864 *bl += sprintf(b + *bl, "OFFLINE");
867 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
871 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
872 atomic_read(&dev->execute_tasks), dev->queue_depth);
873 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
874 dev->se_sub_dev->se_dev_attrib.block_size,
875 dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
876 *bl += sprintf(b + *bl, " ");
879 void transport_dump_vpd_proto_id(
881 unsigned char *p_buf,
884 unsigned char buf[VPD_TMP_BUF_SIZE];
887 memset(buf, 0, VPD_TMP_BUF_SIZE);
888 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
890 switch (vpd->protocol_identifier) {
892 sprintf(buf+len, "Fibre Channel\n");
895 sprintf(buf+len, "Parallel SCSI\n");
898 sprintf(buf+len, "SSA\n");
901 sprintf(buf+len, "IEEE 1394\n");
904 sprintf(buf+len, "SCSI Remote Direct Memory Access"
908 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
911 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
914 sprintf(buf+len, "Automation/Drive Interface Transport"
918 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
921 sprintf(buf+len, "Unknown 0x%02x\n",
922 vpd->protocol_identifier);
927 strncpy(p_buf, buf, p_buf_len);
933 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
936 * Check if the Protocol Identifier Valid (PIV) bit is set..
938 * from spc3r23.pdf section 7.5.1
940 if (page_83[1] & 0x80) {
941 vpd->protocol_identifier = (page_83[0] & 0xf0);
942 vpd->protocol_identifier_set = 1;
943 transport_dump_vpd_proto_id(vpd, NULL, 0);
946 EXPORT_SYMBOL(transport_set_vpd_proto_id);
948 int transport_dump_vpd_assoc(
950 unsigned char *p_buf,
953 unsigned char buf[VPD_TMP_BUF_SIZE];
957 memset(buf, 0, VPD_TMP_BUF_SIZE);
958 len = sprintf(buf, "T10 VPD Identifier Association: ");
960 switch (vpd->association) {
962 sprintf(buf+len, "addressed logical unit\n");
965 sprintf(buf+len, "target port\n");
968 sprintf(buf+len, "SCSI target device\n");
971 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
977 strncpy(p_buf, buf, p_buf_len);
984 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
987 * The VPD identification association..
989 * from spc3r23.pdf Section 7.6.3.1 Table 297
991 vpd->association = (page_83[1] & 0x30);
992 return transport_dump_vpd_assoc(vpd, NULL, 0);
994 EXPORT_SYMBOL(transport_set_vpd_assoc);
996 int transport_dump_vpd_ident_type(
998 unsigned char *p_buf,
1001 unsigned char buf[VPD_TMP_BUF_SIZE];
1005 memset(buf, 0, VPD_TMP_BUF_SIZE);
1006 len = sprintf(buf, "T10 VPD Identifier Type: ");
1008 switch (vpd->device_identifier_type) {
1010 sprintf(buf+len, "Vendor specific\n");
1013 sprintf(buf+len, "T10 Vendor ID based\n");
1016 sprintf(buf+len, "EUI-64 based\n");
1019 sprintf(buf+len, "NAA\n");
1022 sprintf(buf+len, "Relative target port identifier\n");
1025 sprintf(buf+len, "SCSI name string\n");
1028 sprintf(buf+len, "Unsupported: 0x%02x\n",
1029 vpd->device_identifier_type);
1035 if (p_buf_len < strlen(buf)+1)
1037 strncpy(p_buf, buf, p_buf_len);
1039 pr_debug("%s", buf);
1045 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1048 * The VPD identifier type..
1050 * from spc3r23.pdf Section 7.6.3.1 Table 298
1052 vpd->device_identifier_type = (page_83[1] & 0x0f);
1053 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1055 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1057 int transport_dump_vpd_ident(
1058 struct t10_vpd *vpd,
1059 unsigned char *p_buf,
1062 unsigned char buf[VPD_TMP_BUF_SIZE];
1065 memset(buf, 0, VPD_TMP_BUF_SIZE);
1067 switch (vpd->device_identifier_code_set) {
1068 case 0x01: /* Binary */
1069 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1070 &vpd->device_identifier[0]);
1072 case 0x02: /* ASCII */
1073 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1074 &vpd->device_identifier[0]);
1076 case 0x03: /* UTF-8 */
1077 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1078 &vpd->device_identifier[0]);
1081 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1082 " 0x%02x", vpd->device_identifier_code_set);
1088 strncpy(p_buf, buf, p_buf_len);
1090 pr_debug("%s", buf);
1096 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1098 static const char hex_str[] = "0123456789abcdef";
1099 int j = 0, i = 4; /* offset to start of the identifer */
1102 * The VPD Code Set (encoding)
1104 * from spc3r23.pdf Section 7.6.3.1 Table 296
1106 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1107 switch (vpd->device_identifier_code_set) {
1108 case 0x01: /* Binary */
1109 vpd->device_identifier[j++] =
1110 hex_str[vpd->device_identifier_type];
1111 while (i < (4 + page_83[3])) {
1112 vpd->device_identifier[j++] =
1113 hex_str[(page_83[i] & 0xf0) >> 4];
1114 vpd->device_identifier[j++] =
1115 hex_str[page_83[i] & 0x0f];
1119 case 0x02: /* ASCII */
1120 case 0x03: /* UTF-8 */
1121 while (i < (4 + page_83[3]))
1122 vpd->device_identifier[j++] = page_83[i++];
1128 return transport_dump_vpd_ident(vpd, NULL, 0);
1130 EXPORT_SYMBOL(transport_set_vpd_ident);
1132 static void core_setup_task_attr_emulation(struct se_device *dev)
1135 * If this device is from Target_Core_Mod/pSCSI, disable the
1136 * SAM Task Attribute emulation.
1138 * This is currently not available in upsream Linux/SCSI Target
1139 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1141 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1142 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1146 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1147 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1148 " device\n", dev->transport->name,
1149 dev->transport->get_device_rev(dev));
1152 static void scsi_dump_inquiry(struct se_device *dev)
1154 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1158 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1160 for (i = 0; i < 8; i++)
1161 if (wwn->vendor[i] >= 0x20)
1162 buf[i] = wwn->vendor[i];
1166 pr_debug(" Vendor: %s\n", buf);
1168 for (i = 0; i < 16; i++)
1169 if (wwn->model[i] >= 0x20)
1170 buf[i] = wwn->model[i];
1174 pr_debug(" Model: %s\n", buf);
1176 for (i = 0; i < 4; i++)
1177 if (wwn->revision[i] >= 0x20)
1178 buf[i] = wwn->revision[i];
1182 pr_debug(" Revision: %s\n", buf);
1184 device_type = dev->transport->get_device_type(dev);
1185 pr_debug(" Type: %s ", scsi_device_type(device_type));
1186 pr_debug(" ANSI SCSI revision: %02x\n",
1187 dev->transport->get_device_rev(dev));
1190 struct se_device *transport_add_device_to_core_hba(
1192 struct se_subsystem_api *transport,
1193 struct se_subsystem_dev *se_dev,
1195 void *transport_dev,
1196 struct se_dev_limits *dev_limits,
1197 const char *inquiry_prod,
1198 const char *inquiry_rev)
1201 struct se_device *dev;
1203 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1205 pr_err("Unable to allocate memory for se_dev_t\n");
1209 transport_init_queue_obj(&dev->dev_queue_obj);
1210 dev->dev_flags = device_flags;
1211 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1212 dev->dev_ptr = transport_dev;
1214 dev->se_sub_dev = se_dev;
1215 dev->transport = transport;
1216 INIT_LIST_HEAD(&dev->dev_list);
1217 INIT_LIST_HEAD(&dev->dev_sep_list);
1218 INIT_LIST_HEAD(&dev->dev_tmr_list);
1219 INIT_LIST_HEAD(&dev->execute_list);
1220 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1221 INIT_LIST_HEAD(&dev->state_list);
1222 INIT_LIST_HEAD(&dev->qf_cmd_list);
1223 spin_lock_init(&dev->execute_task_lock);
1224 spin_lock_init(&dev->delayed_cmd_lock);
1225 spin_lock_init(&dev->dev_reservation_lock);
1226 spin_lock_init(&dev->dev_status_lock);
1227 spin_lock_init(&dev->se_port_lock);
1228 spin_lock_init(&dev->se_tmr_lock);
1229 spin_lock_init(&dev->qf_cmd_lock);
1230 atomic_set(&dev->dev_ordered_id, 0);
1232 se_dev_set_default_attribs(dev, dev_limits);
1234 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1235 dev->creation_time = get_jiffies_64();
1236 spin_lock_init(&dev->stats_lock);
1238 spin_lock(&hba->device_lock);
1239 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1241 spin_unlock(&hba->device_lock);
1243 * Setup the SAM Task Attribute emulation for struct se_device
1245 core_setup_task_attr_emulation(dev);
1247 * Force PR and ALUA passthrough emulation with internal object use.
1249 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1251 * Setup the Reservations infrastructure for struct se_device
1253 core_setup_reservations(dev, force_pt);
1255 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1257 if (core_setup_alua(dev, force_pt) < 0)
1261 * Startup the struct se_device processing thread
1263 dev->process_thread = kthread_run(transport_processing_thread, dev,
1264 "LIO_%s", dev->transport->name);
1265 if (IS_ERR(dev->process_thread)) {
1266 pr_err("Unable to create kthread: LIO_%s\n",
1267 dev->transport->name);
1271 * Setup work_queue for QUEUE_FULL
1273 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1275 * Preload the initial INQUIRY const values if we are doing
1276 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1277 * passthrough because this is being provided by the backend LLD.
1278 * This is required so that transport_get_inquiry() copies these
1279 * originals once back into DEV_T10_WWN(dev) for the virtual device
1282 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1283 if (!inquiry_prod || !inquiry_rev) {
1284 pr_err("All non TCM/pSCSI plugins require"
1285 " INQUIRY consts\n");
1289 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1290 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1291 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1293 scsi_dump_inquiry(dev);
1297 kthread_stop(dev->process_thread);
1299 spin_lock(&hba->device_lock);
1300 list_del(&dev->dev_list);
1302 spin_unlock(&hba->device_lock);
1304 se_release_vpd_for_dev(dev);
1310 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1312 /* transport_generic_prepare_cdb():
1314 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1315 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1316 * The point of this is since we are mapping iSCSI LUNs to
1317 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1318 * devices and HBAs for a loop.
1320 static inline void transport_generic_prepare_cdb(
1324 case READ_10: /* SBC - RDProtect */
1325 case READ_12: /* SBC - RDProtect */
1326 case READ_16: /* SBC - RDProtect */
1327 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1328 case VERIFY: /* SBC - VRProtect */
1329 case VERIFY_16: /* SBC - VRProtect */
1330 case WRITE_VERIFY: /* SBC - VRProtect */
1331 case WRITE_VERIFY_12: /* SBC - VRProtect */
1332 case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
1335 cdb[1] &= 0x1f; /* clear logical unit number */
1340 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1343 * Used by fabric modules containing a local struct se_cmd within their
1344 * fabric dependent per I/O descriptor.
1346 void transport_init_se_cmd(
1348 struct target_core_fabric_ops *tfo,
1349 struct se_session *se_sess,
1353 unsigned char *sense_buffer)
1355 INIT_LIST_HEAD(&cmd->se_lun_node);
1356 INIT_LIST_HEAD(&cmd->se_delayed_node);
1357 INIT_LIST_HEAD(&cmd->se_qf_node);
1358 INIT_LIST_HEAD(&cmd->se_queue_node);
1359 INIT_LIST_HEAD(&cmd->se_cmd_list);
1360 INIT_LIST_HEAD(&cmd->execute_list);
1361 INIT_LIST_HEAD(&cmd->state_list);
1362 init_completion(&cmd->transport_lun_fe_stop_comp);
1363 init_completion(&cmd->transport_lun_stop_comp);
1364 init_completion(&cmd->t_transport_stop_comp);
1365 init_completion(&cmd->cmd_wait_comp);
1366 init_completion(&cmd->task_stop_comp);
1367 spin_lock_init(&cmd->t_state_lock);
1368 cmd->transport_state = CMD_T_DEV_ACTIVE;
1371 cmd->se_sess = se_sess;
1372 cmd->data_length = data_length;
1373 cmd->data_direction = data_direction;
1374 cmd->sam_task_attr = task_attr;
1375 cmd->sense_buffer = sense_buffer;
1377 cmd->state_active = false;
1379 EXPORT_SYMBOL(transport_init_se_cmd);
1381 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1384 * Check if SAM Task Attribute emulation is enabled for this
1385 * struct se_device storage object
1387 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1390 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1391 pr_debug("SAM Task Attribute ACA"
1392 " emulation is not supported\n");
1396 * Used to determine when ORDERED commands should go from
1397 * Dormant to Active status.
1399 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1400 smp_mb__after_atomic_inc();
1401 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1402 cmd->se_ordered_id, cmd->sam_task_attr,
1403 cmd->se_dev->transport->name);
1407 /* target_setup_cmd_from_cdb():
1409 * Called from fabric RX Thread.
1411 int target_setup_cmd_from_cdb(
1417 transport_generic_prepare_cdb(cdb);
1419 * Ensure that the received CDB is less than the max (252 + 8) bytes
1420 * for VARIABLE_LENGTH_CMD
1422 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1423 pr_err("Received SCSI CDB with command_size: %d that"
1424 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1425 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1426 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1427 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1431 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1432 * allocate the additional extended CDB buffer now.. Otherwise
1433 * setup the pointer from __t_task_cdb to t_task_cdb.
1435 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1436 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1438 if (!cmd->t_task_cdb) {
1439 pr_err("Unable to allocate cmd->t_task_cdb"
1440 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1441 scsi_command_size(cdb),
1442 (unsigned long)sizeof(cmd->__t_task_cdb));
1443 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1444 cmd->scsi_sense_reason =
1445 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1449 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1451 * Copy the original CDB into cmd->
1453 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1455 * Setup the received CDB based on SCSI defined opcodes and
1456 * perform unit attention, persistent reservations and ALUA
1457 * checks for virtual device backends. The cmd->t_task_cdb
1458 * pointer is expected to be setup before we reach this point.
1460 ret = transport_generic_cmd_sequencer(cmd, cdb);
1464 * Check for SAM Task Attribute Emulation
1466 if (transport_check_alloc_task_attr(cmd) < 0) {
1467 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1468 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1471 spin_lock(&cmd->se_lun->lun_sep_lock);
1472 if (cmd->se_lun->lun_sep)
1473 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1474 spin_unlock(&cmd->se_lun->lun_sep_lock);
1477 EXPORT_SYMBOL(target_setup_cmd_from_cdb);
1480 * Used by fabric module frontends to queue tasks directly.
1481 * Many only be used from process context only
1483 int transport_handle_cdb_direct(
1490 pr_err("cmd->se_lun is NULL\n");
1493 if (in_interrupt()) {
1495 pr_err("transport_generic_handle_cdb cannot be called"
1496 " from interrupt context\n");
1500 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
1501 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1502 * in existing usage to ensure that outstanding descriptors are handled
1503 * correctly during shutdown via transport_wait_for_tasks()
1505 * Also, we don't take cmd->t_state_lock here as we only expect
1506 * this to be called for initial descriptor submission.
1508 cmd->t_state = TRANSPORT_NEW_CMD;
1509 cmd->transport_state |= CMD_T_ACTIVE;
1512 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1513 * so follow TRANSPORT_NEW_CMD processing thread context usage
1514 * and call transport_generic_request_failure() if necessary..
1516 ret = transport_generic_new_cmd(cmd);
1518 transport_generic_request_failure(cmd);
1522 EXPORT_SYMBOL(transport_handle_cdb_direct);
1525 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1527 * @se_cmd: command descriptor to submit
1528 * @se_sess: associated se_sess for endpoint
1529 * @cdb: pointer to SCSI CDB
1530 * @sense: pointer to SCSI sense buffer
1531 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1532 * @data_length: fabric expected data transfer length
1533 * @task_addr: SAM task attribute
1534 * @data_dir: DMA data direction
1535 * @flags: flags for command submission from target_sc_flags_tables
1537 * This may only be called from process context, and also currently
1538 * assumes internal allocation of fabric payload buffer by target-core.
1540 void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1541 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1542 u32 data_length, int task_attr, int data_dir, int flags)
1544 struct se_portal_group *se_tpg;
1547 se_tpg = se_sess->se_tpg;
1549 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1550 BUG_ON(in_interrupt());
1552 * Initialize se_cmd for target operation. From this point
1553 * exceptions are handled by sending exception status via
1554 * target_core_fabric_ops->queue_status() callback
1556 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1557 data_length, data_dir, task_attr, sense);
1558 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1559 se_cmd->unknown_data_length = 1;
1561 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1562 * se_sess->sess_cmd_list. A second kref_get here is necessary
1563 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1564 * kref_put() to happen during fabric packet acknowledgement.
1566 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1568 * Signal bidirectional data payloads to target-core
1570 if (flags & TARGET_SCF_BIDI_OP)
1571 se_cmd->se_cmd_flags |= SCF_BIDI;
1573 * Locate se_lun pointer and attach it to struct se_cmd
1575 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1576 transport_send_check_condition_and_sense(se_cmd,
1577 se_cmd->scsi_sense_reason, 0);
1578 target_put_sess_cmd(se_sess, se_cmd);
1582 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1583 * allocate the necessary tasks to complete the received CDB+data
1585 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
1587 transport_generic_request_failure(se_cmd);
1592 * Check if we need to delay processing because of ALUA
1593 * Active/NonOptimized primary access state..
1595 core_alua_check_nonop_delay(se_cmd);
1598 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1599 * for immediate execution of READs, otherwise wait for
1600 * transport_generic_handle_data() to be called for WRITEs
1601 * when fabric has filled the incoming buffer.
1603 transport_handle_cdb_direct(se_cmd);
1606 EXPORT_SYMBOL(target_submit_cmd);
1608 static void target_complete_tmr_failure(struct work_struct *work)
1610 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1612 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1613 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1614 transport_generic_free_cmd(se_cmd, 0);
1618 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1621 * @se_cmd: command descriptor to submit
1622 * @se_sess: associated se_sess for endpoint
1623 * @sense: pointer to SCSI sense buffer
1624 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1625 * @fabric_context: fabric context for TMR req
1626 * @tm_type: Type of TM request
1627 * @gfp: gfp type for caller
1628 * @tag: referenced task tag for TMR_ABORT_TASK
1629 * @flags: submit cmd flags
1631 * Callable from all contexts.
1634 int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1635 unsigned char *sense, u32 unpacked_lun,
1636 void *fabric_tmr_ptr, unsigned char tm_type,
1637 gfp_t gfp, unsigned int tag, int flags)
1639 struct se_portal_group *se_tpg;
1642 se_tpg = se_sess->se_tpg;
1645 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1646 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1648 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1649 * allocation failure.
1651 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
1655 if (tm_type == TMR_ABORT_TASK)
1656 se_cmd->se_tmr_req->ref_task_tag = tag;
1658 /* See target_submit_cmd for commentary */
1659 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1661 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1664 * For callback during failure handling, push this work off
1665 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1667 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1668 schedule_work(&se_cmd->work);
1671 transport_generic_handle_tmr(se_cmd);
1674 EXPORT_SYMBOL(target_submit_tmr);
1677 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1678 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1679 * complete setup in TCM process context w/ TFO->new_cmd_map().
1681 int transport_generic_handle_cdb_map(
1686 pr_err("cmd->se_lun is NULL\n");
1690 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
1693 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1695 /* transport_generic_handle_data():
1699 int transport_generic_handle_data(
1703 * For the software fabric case, then we assume the nexus is being
1704 * failed/shutdown when signals are pending from the kthread context
1705 * caller, so we return a failure. For the HW target mode case running
1706 * in interrupt code, the signal_pending() check is skipped.
1708 if (!in_interrupt() && signal_pending(current))
1711 * If the received CDB has aleady been ABORTED by the generic
1712 * target engine, we now call transport_check_aborted_status()
1713 * to queue any delated TASK_ABORTED status for the received CDB to the
1714 * fabric module as we are expecting no further incoming DATA OUT
1715 * sequences at this point.
1717 if (transport_check_aborted_status(cmd, 1) != 0)
1720 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
1723 EXPORT_SYMBOL(transport_generic_handle_data);
1725 /* transport_generic_handle_tmr():
1729 int transport_generic_handle_tmr(
1732 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
1735 EXPORT_SYMBOL(transport_generic_handle_tmr);
1738 * If the cmd is active, request it to be stopped and sleep until it
1741 bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
1743 bool was_active = false;
1745 if (cmd->transport_state & CMD_T_BUSY) {
1746 cmd->transport_state |= CMD_T_REQUEST_STOP;
1747 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1749 pr_debug("cmd %p waiting to complete\n", cmd);
1750 wait_for_completion(&cmd->task_stop_comp);
1751 pr_debug("cmd %p stopped successfully\n", cmd);
1753 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1754 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1755 cmd->transport_state &= ~CMD_T_BUSY;
1763 * Handle SAM-esque emulation for generic transport request failures.
1765 void transport_generic_request_failure(struct se_cmd *cmd)
1769 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1770 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1771 cmd->t_task_cdb[0]);
1772 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
1773 cmd->se_tfo->get_cmd_state(cmd),
1774 cmd->t_state, cmd->scsi_sense_reason);
1775 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1776 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1777 (cmd->transport_state & CMD_T_STOP) != 0,
1778 (cmd->transport_state & CMD_T_SENT) != 0);
1781 * For SAM Task Attribute emulation for failed struct se_cmd
1783 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1784 transport_complete_task_attr(cmd);
1786 switch (cmd->scsi_sense_reason) {
1787 case TCM_NON_EXISTENT_LUN:
1788 case TCM_UNSUPPORTED_SCSI_OPCODE:
1789 case TCM_INVALID_CDB_FIELD:
1790 case TCM_INVALID_PARAMETER_LIST:
1791 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1792 case TCM_UNKNOWN_MODE_PAGE:
1793 case TCM_WRITE_PROTECTED:
1794 case TCM_CHECK_CONDITION_ABORT_CMD:
1795 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1796 case TCM_CHECK_CONDITION_NOT_READY:
1798 case TCM_RESERVATION_CONFLICT:
1800 * No SENSE Data payload for this case, set SCSI Status
1801 * and queue the response to $FABRIC_MOD.
1803 * Uses linux/include/scsi/scsi.h SAM status codes defs
1805 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1807 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1808 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1811 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1814 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1815 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1816 cmd->orig_fe_lun, 0x2C,
1817 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1819 ret = cmd->se_tfo->queue_status(cmd);
1820 if (ret == -EAGAIN || ret == -ENOMEM)
1824 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1825 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
1826 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1830 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1831 * make the call to transport_send_check_condition_and_sense()
1832 * directly. Otherwise expect the fabric to make the call to
1833 * transport_send_check_condition_and_sense() after handling
1834 * possible unsoliticied write data payloads.
1836 ret = transport_send_check_condition_and_sense(cmd,
1837 cmd->scsi_sense_reason, 0);
1838 if (ret == -EAGAIN || ret == -ENOMEM)
1842 transport_lun_remove_cmd(cmd);
1843 if (!transport_cmd_check_stop_to_fabric(cmd))
1848 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1849 transport_handle_queue_full(cmd, cmd->se_dev);
1851 EXPORT_SYMBOL(transport_generic_request_failure);
1853 static inline u32 transport_lba_21(unsigned char *cdb)
1855 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1858 static inline u32 transport_lba_32(unsigned char *cdb)
1860 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1863 static inline unsigned long long transport_lba_64(unsigned char *cdb)
1865 unsigned int __v1, __v2;
1867 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1868 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1870 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1874 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1876 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1878 unsigned int __v1, __v2;
1880 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1881 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1883 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1886 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1888 unsigned long flags;
1890 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1891 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1892 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1896 * Called from Fabric Module context from transport_execute_tasks()
1898 * The return of this function determins if the tasks from struct se_cmd
1899 * get added to the execution queue in transport_execute_tasks(),
1900 * or are added to the delayed or ordered lists here.
1902 static inline int transport_execute_task_attr(struct se_cmd *cmd)
1904 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1907 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1908 * to allow the passed struct se_cmd list of tasks to the front of the list.
1910 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
1911 pr_debug("Added HEAD_OF_QUEUE for CDB:"
1912 " 0x%02x, se_ordered_id: %u\n",
1914 cmd->se_ordered_id);
1916 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
1917 atomic_inc(&cmd->se_dev->dev_ordered_sync);
1918 smp_mb__after_atomic_inc();
1920 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
1921 " list, se_ordered_id: %u\n",
1923 cmd->se_ordered_id);
1925 * Add ORDERED command to tail of execution queue if
1926 * no other older commands exist that need to be
1929 if (!atomic_read(&cmd->se_dev->simple_cmds))
1933 * For SIMPLE and UNTAGGED Task Attribute commands
1935 atomic_inc(&cmd->se_dev->simple_cmds);
1936 smp_mb__after_atomic_inc();
1939 * Otherwise if one or more outstanding ORDERED task attribute exist,
1940 * add the dormant task(s) built for the passed struct se_cmd to the
1941 * execution queue and become in Active state for this struct se_device.
1943 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
1945 * Otherwise, add cmd w/ tasks to delayed cmd queue that
1946 * will be drained upon completion of HEAD_OF_QUEUE task.
1948 spin_lock(&cmd->se_dev->delayed_cmd_lock);
1949 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
1950 list_add_tail(&cmd->se_delayed_node,
1951 &cmd->se_dev->delayed_cmd_list);
1952 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
1954 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1955 " delayed CMD list, se_ordered_id: %u\n",
1956 cmd->t_task_cdb[0], cmd->sam_task_attr,
1957 cmd->se_ordered_id);
1959 * Return zero to let transport_execute_tasks() know
1960 * not to add the delayed tasks to the execution list.
1965 * Otherwise, no ORDERED task attributes exist..
1971 * Called from fabric module context in transport_generic_new_cmd() and
1972 * transport_generic_process_write()
1974 static void transport_execute_tasks(struct se_cmd *cmd)
1977 struct se_device *se_dev = cmd->se_dev;
1979 * Call transport_cmd_check_stop() to see if a fabric exception
1980 * has occurred that prevents execution.
1982 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
1984 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
1985 * attribute for the tasks of the received struct se_cmd CDB
1987 add_tasks = transport_execute_task_attr(cmd);
1989 __transport_execute_tasks(se_dev, cmd);
1993 __transport_execute_tasks(se_dev, NULL);
1996 static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
1999 struct se_cmd *cmd = NULL;
2000 unsigned long flags;
2003 spin_lock_irq(&dev->execute_task_lock);
2004 if (new_cmd != NULL)
2005 __target_add_to_execute_list(new_cmd);
2007 if (list_empty(&dev->execute_list)) {
2008 spin_unlock_irq(&dev->execute_task_lock);
2011 cmd = list_first_entry(&dev->execute_list, struct se_cmd, execute_list);
2012 __target_remove_from_execute_list(cmd);
2013 spin_unlock_irq(&dev->execute_task_lock);
2015 spin_lock_irqsave(&cmd->t_state_lock, flags);
2016 cmd->transport_state |= CMD_T_BUSY;
2017 cmd->transport_state |= CMD_T_SENT;
2019 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2021 if (cmd->execute_cmd)
2022 error = cmd->execute_cmd(cmd);
2024 error = dev->transport->execute_cmd(cmd, cmd->t_data_sg,
2025 cmd->t_data_nents, cmd->data_direction);
2029 spin_lock_irqsave(&cmd->t_state_lock, flags);
2030 cmd->transport_state &= ~CMD_T_BUSY;
2031 cmd->transport_state &= ~CMD_T_SENT;
2032 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2034 transport_generic_request_failure(cmd);
2043 static inline u32 transport_get_sectors_6(
2048 struct se_device *dev = cmd->se_dev;
2051 * Assume TYPE_DISK for non struct se_device objects.
2052 * Use 8-bit sector value.
2058 * Use 24-bit allocation length for TYPE_TAPE.
2060 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2061 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2064 * Everything else assume TYPE_DISK Sector CDB location.
2065 * Use 8-bit sector value. SBC-3 says:
2067 * A TRANSFER LENGTH field set to zero specifies that 256
2068 * logical blocks shall be written. Any other value
2069 * specifies the number of logical blocks that shall be
2073 return cdb[4] ? : 256;
2076 static inline u32 transport_get_sectors_10(
2081 struct se_device *dev = cmd->se_dev;
2084 * Assume TYPE_DISK for non struct se_device objects.
2085 * Use 16-bit sector value.
2091 * XXX_10 is not defined in SSC, throw an exception
2093 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2099 * Everything else assume TYPE_DISK Sector CDB location.
2100 * Use 16-bit sector value.
2103 return (u32)(cdb[7] << 8) + cdb[8];
2106 static inline u32 transport_get_sectors_12(
2111 struct se_device *dev = cmd->se_dev;
2114 * Assume TYPE_DISK for non struct se_device objects.
2115 * Use 32-bit sector value.
2121 * XXX_12 is not defined in SSC, throw an exception
2123 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2129 * Everything else assume TYPE_DISK Sector CDB location.
2130 * Use 32-bit sector value.
2133 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2136 static inline u32 transport_get_sectors_16(
2141 struct se_device *dev = cmd->se_dev;
2144 * Assume TYPE_DISK for non struct se_device objects.
2145 * Use 32-bit sector value.
2151 * Use 24-bit allocation length for TYPE_TAPE.
2153 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2154 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2157 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2158 (cdb[12] << 8) + cdb[13];
2162 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2164 static inline u32 transport_get_sectors_32(
2170 * Assume TYPE_DISK for non struct se_device objects.
2171 * Use 32-bit sector value.
2173 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2174 (cdb[30] << 8) + cdb[31];
2178 static inline u32 transport_get_size(
2183 struct se_device *dev = cmd->se_dev;
2185 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2186 if (cdb[1] & 1) { /* sectors */
2187 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2192 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2193 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size,
2194 sectors, dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2195 dev->transport->name);
2197 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2200 static void transport_xor_callback(struct se_cmd *cmd)
2202 unsigned char *buf, *addr;
2203 struct scatterlist *sg;
2204 unsigned int offset;
2208 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2210 * 1) read the specified logical block(s);
2211 * 2) transfer logical blocks from the data-out buffer;
2212 * 3) XOR the logical blocks transferred from the data-out buffer with
2213 * the logical blocks read, storing the resulting XOR data in a buffer;
2214 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2215 * blocks transferred from the data-out buffer; and
2216 * 5) transfer the resulting XOR data to the data-in buffer.
2218 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2220 pr_err("Unable to allocate xor_callback buf\n");
2224 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2225 * into the locally allocated *buf
2227 sg_copy_to_buffer(cmd->t_data_sg,
2233 * Now perform the XOR against the BIDI read memory located at
2234 * cmd->t_mem_bidi_list
2238 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2239 addr = kmap_atomic(sg_page(sg));
2243 for (i = 0; i < sg->length; i++)
2244 *(addr + sg->offset + i) ^= *(buf + offset + i);
2246 offset += sg->length;
2247 kunmap_atomic(addr);
2255 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2257 static int transport_get_sense_data(struct se_cmd *cmd)
2259 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2260 struct se_device *dev = cmd->se_dev;
2261 unsigned long flags;
2264 WARN_ON(!cmd->se_lun);
2269 spin_lock_irqsave(&cmd->t_state_lock, flags);
2270 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2271 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2275 if (!(cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2278 if (!dev->transport->get_sense_buffer) {
2279 pr_err("dev->transport->get_sense_buffer is NULL\n");
2283 sense_buffer = dev->transport->get_sense_buffer(cmd);
2284 if (!sense_buffer) {
2285 pr_err("ITT 0x%08x cmd %p: Unable to locate"
2286 " sense buffer for task with sense\n",
2287 cmd->se_tfo->get_task_tag(cmd), cmd);
2291 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2293 offset = cmd->se_tfo->set_fabric_sense_len(cmd, TRANSPORT_SENSE_BUFFER);
2295 memcpy(&buffer[offset], sense_buffer, TRANSPORT_SENSE_BUFFER);
2297 /* Automatically padded */
2298 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
2300 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x and sense\n",
2301 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
2305 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2309 static inline long long transport_dev_end_lba(struct se_device *dev)
2311 return dev->transport->get_blocks(dev) + 1;
2314 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2316 struct se_device *dev = cmd->se_dev;
2319 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2322 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2324 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2325 pr_err("LBA: %llu Sectors: %u exceeds"
2326 " transport_dev_end_lba(): %llu\n",
2327 cmd->t_task_lba, sectors,
2328 transport_dev_end_lba(dev));
2335 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2338 * Determine if the received WRITE_SAME is used to for direct
2339 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2340 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2341 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2343 int passthrough = (dev->transport->transport_type ==
2344 TRANSPORT_PLUGIN_PHBA_PDEV);
2347 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2348 pr_err("WRITE_SAME PBDATA and LBDATA"
2349 " bits not supported for Block Discard"
2354 * Currently for the emulated case we only accept
2355 * tpws with the UNMAP=1 bit set.
2357 if (!(flags[0] & 0x08)) {
2358 pr_err("WRITE_SAME w/o UNMAP bit not"
2359 " supported for Block Discard Emulation\n");
2367 /* transport_generic_cmd_sequencer():
2369 * Generic Command Sequencer that should work for most DAS transport
2372 * Called from target_setup_cmd_from_cdb() in the $FABRIC_MOD
2375 * FIXME: Need to support other SCSI OPCODES where as well.
2377 static int transport_generic_cmd_sequencer(
2381 struct se_device *dev = cmd->se_dev;
2382 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2383 int ret = 0, sector_ret = 0, passthrough;
2384 u32 sectors = 0, size = 0, pr_reg_type = 0;
2388 * Check for an existing UNIT ATTENTION condition
2390 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2391 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2392 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2396 * Check status of Asymmetric Logical Unit Assignment port
2398 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2401 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2402 * The ALUA additional sense code qualifier (ASCQ) is determined
2403 * by the ALUA primary or secondary access state..
2406 pr_debug("[%s]: ALUA TG Port not available,"
2407 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2408 cmd->se_tfo->get_fabric_name(), alua_ascq);
2410 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2411 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2412 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2415 goto out_invalid_cdb_field;
2418 * Check status for SPC-3 Persistent Reservations
2420 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2421 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2422 cmd, cdb, pr_reg_type) != 0) {
2423 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2424 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2425 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2426 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2430 * This means the CDB is allowed for the SCSI Initiator port
2431 * when said port is *NOT* holding the legacy SPC-2 or
2432 * SPC-3 Persistent Reservation.
2437 * If we operate in passthrough mode we skip most CDB emulation and
2438 * instead hand the commands down to the physical SCSI device.
2441 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2445 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2447 goto out_unsupported_cdb;
2448 size = transport_get_size(sectors, cdb, cmd);
2449 cmd->t_task_lba = transport_lba_21(cdb);
2450 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2453 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2455 goto out_unsupported_cdb;
2456 size = transport_get_size(sectors, cdb, cmd);
2457 cmd->t_task_lba = transport_lba_32(cdb);
2458 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2461 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2463 goto out_unsupported_cdb;
2464 size = transport_get_size(sectors, cdb, cmd);
2465 cmd->t_task_lba = transport_lba_32(cdb);
2466 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2469 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2471 goto out_unsupported_cdb;
2472 size = transport_get_size(sectors, cdb, cmd);
2473 cmd->t_task_lba = transport_lba_64(cdb);
2474 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2477 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2479 goto out_unsupported_cdb;
2480 size = transport_get_size(sectors, cdb, cmd);
2481 cmd->t_task_lba = transport_lba_21(cdb);
2482 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2486 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2488 goto out_unsupported_cdb;
2489 size = transport_get_size(sectors, cdb, cmd);
2490 cmd->t_task_lba = transport_lba_32(cdb);
2492 cmd->se_cmd_flags |= SCF_FUA;
2493 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2496 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2498 goto out_unsupported_cdb;
2499 size = transport_get_size(sectors, cdb, cmd);
2500 cmd->t_task_lba = transport_lba_32(cdb);
2502 cmd->se_cmd_flags |= SCF_FUA;
2503 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2506 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2508 goto out_unsupported_cdb;
2509 size = transport_get_size(sectors, cdb, cmd);
2510 cmd->t_task_lba = transport_lba_64(cdb);
2512 cmd->se_cmd_flags |= SCF_FUA;
2513 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2515 case XDWRITEREAD_10:
2516 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2517 !(cmd->se_cmd_flags & SCF_BIDI))
2518 goto out_invalid_cdb_field;
2519 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2521 goto out_unsupported_cdb;
2522 size = transport_get_size(sectors, cdb, cmd);
2523 cmd->t_task_lba = transport_lba_32(cdb);
2524 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2527 * Do now allow BIDI commands for passthrough mode.
2530 goto out_unsupported_cdb;
2533 * Setup BIDI XOR callback to be run after I/O completion.
2535 cmd->transport_complete_callback = &transport_xor_callback;
2537 cmd->se_cmd_flags |= SCF_FUA;
2539 case VARIABLE_LENGTH_CMD:
2540 service_action = get_unaligned_be16(&cdb[8]);
2541 switch (service_action) {
2542 case XDWRITEREAD_32:
2543 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2545 goto out_unsupported_cdb;
2546 size = transport_get_size(sectors, cdb, cmd);
2548 * Use WRITE_32 and READ_32 opcodes for the emulated
2549 * XDWRITE_READ_32 logic.
2551 cmd->t_task_lba = transport_lba_64_ext(cdb);
2552 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2555 * Do now allow BIDI commands for passthrough mode.
2558 goto out_unsupported_cdb;
2561 * Setup BIDI XOR callback to be run during after I/O
2564 cmd->transport_complete_callback = &transport_xor_callback;
2566 cmd->se_cmd_flags |= SCF_FUA;
2569 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2571 goto out_unsupported_cdb;
2574 size = transport_get_size(1, cdb, cmd);
2576 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2578 goto out_invalid_cdb_field;
2581 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2582 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2584 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2585 goto out_unsupported_cdb;
2587 cmd->execute_cmd = target_emulate_write_same;
2590 pr_err("VARIABLE_LENGTH_CMD service action"
2591 " 0x%04x not supported\n", service_action);
2592 goto out_unsupported_cdb;
2595 case MAINTENANCE_IN:
2596 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2597 /* MAINTENANCE_IN from SCC-2 */
2599 * Check for emulated MI_REPORT_TARGET_PGS.
2601 if ((cdb[1] & 0x1f) == MI_REPORT_TARGET_PGS &&
2602 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2604 target_emulate_report_target_port_groups;
2606 size = (cdb[6] << 24) | (cdb[7] << 16) |
2607 (cdb[8] << 8) | cdb[9];
2609 /* GPCMD_SEND_KEY from multi media commands */
2610 size = (cdb[8] << 8) + cdb[9];
2612 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2616 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2618 case MODE_SELECT_10:
2619 size = (cdb[7] << 8) + cdb[8];
2620 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2624 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2626 cmd->execute_cmd = target_emulate_modesense;
2629 size = (cdb[7] << 8) + cdb[8];
2630 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2632 cmd->execute_cmd = target_emulate_modesense;
2634 case GPCMD_READ_BUFFER_CAPACITY:
2635 case GPCMD_SEND_OPC:
2638 size = (cdb[7] << 8) + cdb[8];
2639 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2641 case READ_BLOCK_LIMITS:
2642 size = READ_BLOCK_LEN;
2643 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2645 case GPCMD_GET_CONFIGURATION:
2646 case GPCMD_READ_FORMAT_CAPACITIES:
2647 case GPCMD_READ_DISC_INFO:
2648 case GPCMD_READ_TRACK_RZONE_INFO:
2649 size = (cdb[7] << 8) + cdb[8];
2650 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2652 case PERSISTENT_RESERVE_IN:
2653 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2654 cmd->execute_cmd = target_scsi3_emulate_pr_in;
2655 size = (cdb[7] << 8) + cdb[8];
2656 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2658 case PERSISTENT_RESERVE_OUT:
2659 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2660 cmd->execute_cmd = target_scsi3_emulate_pr_out;
2661 size = (cdb[7] << 8) + cdb[8];
2662 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2664 case GPCMD_MECHANISM_STATUS:
2665 case GPCMD_READ_DVD_STRUCTURE:
2666 size = (cdb[8] << 8) + cdb[9];
2667 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2670 size = READ_POSITION_LEN;
2671 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2673 case MAINTENANCE_OUT:
2674 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2675 /* MAINTENANCE_OUT from SCC-2
2677 * Check for emulated MO_SET_TARGET_PGS.
2679 if (cdb[1] == MO_SET_TARGET_PGS &&
2680 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2682 target_emulate_set_target_port_groups;
2685 size = (cdb[6] << 24) | (cdb[7] << 16) |
2686 (cdb[8] << 8) | cdb[9];
2688 /* GPCMD_REPORT_KEY from multi media commands */
2689 size = (cdb[8] << 8) + cdb[9];
2691 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2694 size = (cdb[3] << 8) + cdb[4];
2696 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2697 * See spc4r17 section 5.3
2699 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2700 cmd->sam_task_attr = MSG_HEAD_TAG;
2701 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2703 cmd->execute_cmd = target_emulate_inquiry;
2706 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2707 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2710 size = READ_CAP_LEN;
2711 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2713 cmd->execute_cmd = target_emulate_readcapacity;
2715 case READ_MEDIA_SERIAL_NUMBER:
2716 case SECURITY_PROTOCOL_IN:
2717 case SECURITY_PROTOCOL_OUT:
2718 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2719 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2721 case SERVICE_ACTION_IN:
2722 switch (cmd->t_task_cdb[1] & 0x1f) {
2723 case SAI_READ_CAPACITY_16:
2726 target_emulate_readcapacity_16;
2732 pr_err("Unsupported SA: 0x%02x\n",
2733 cmd->t_task_cdb[1] & 0x1f);
2734 goto out_invalid_cdb_field;
2737 case ACCESS_CONTROL_IN:
2738 case ACCESS_CONTROL_OUT:
2740 case READ_ATTRIBUTE:
2741 case RECEIVE_COPY_RESULTS:
2742 case WRITE_ATTRIBUTE:
2743 size = (cdb[10] << 24) | (cdb[11] << 16) |
2744 (cdb[12] << 8) | cdb[13];
2745 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2747 case RECEIVE_DIAGNOSTIC:
2748 case SEND_DIAGNOSTIC:
2749 size = (cdb[3] << 8) | cdb[4];
2750 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2752 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2755 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2756 size = (2336 * sectors);
2757 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2762 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2766 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2768 cmd->execute_cmd = target_emulate_request_sense;
2770 case READ_ELEMENT_STATUS:
2771 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
2772 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2775 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2776 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2781 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2782 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2784 if (cdb[0] == RESERVE_10)
2785 size = (cdb[7] << 8) | cdb[8];
2787 size = cmd->data_length;
2790 * Setup the legacy emulated handler for SPC-2 and
2791 * >= SPC-3 compatible reservation handling (CRH=1)
2792 * Otherwise, we assume the underlying SCSI logic is
2793 * is running in SPC_PASSTHROUGH, and wants reservations
2794 * emulation disabled.
2796 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2797 cmd->execute_cmd = target_scsi2_reservation_reserve;
2798 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2803 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2804 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2806 if (cdb[0] == RELEASE_10)
2807 size = (cdb[7] << 8) | cdb[8];
2809 size = cmd->data_length;
2811 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2812 cmd->execute_cmd = target_scsi2_reservation_release;
2813 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2815 case SYNCHRONIZE_CACHE:
2816 case SYNCHRONIZE_CACHE_16:
2818 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2820 if (cdb[0] == SYNCHRONIZE_CACHE) {
2821 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2822 cmd->t_task_lba = transport_lba_32(cdb);
2824 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2825 cmd->t_task_lba = transport_lba_64(cdb);
2828 goto out_unsupported_cdb;
2830 size = transport_get_size(sectors, cdb, cmd);
2831 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2837 * Check to ensure that LBA + Range does not exceed past end of
2838 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
2840 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2841 if (transport_cmd_get_valid_sectors(cmd) < 0)
2842 goto out_invalid_cdb_field;
2844 cmd->execute_cmd = target_emulate_synchronize_cache;
2847 size = get_unaligned_be16(&cdb[7]);
2848 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2850 cmd->execute_cmd = target_emulate_unmap;
2853 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2855 goto out_unsupported_cdb;
2858 size = transport_get_size(1, cdb, cmd);
2860 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2861 goto out_invalid_cdb_field;
2864 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
2865 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2867 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2868 goto out_unsupported_cdb;
2870 cmd->execute_cmd = target_emulate_write_same;
2873 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2875 goto out_unsupported_cdb;
2878 size = transport_get_size(1, cdb, cmd);
2880 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2881 goto out_invalid_cdb_field;
2884 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2885 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2887 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2888 * of byte 1 bit 3 UNMAP instead of original reserved field
2890 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2891 goto out_unsupported_cdb;
2893 cmd->execute_cmd = target_emulate_write_same;
2895 case ALLOW_MEDIUM_REMOVAL:
2901 case TEST_UNIT_READY:
2903 case WRITE_FILEMARKS:
2904 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2906 cmd->execute_cmd = target_emulate_noop;
2908 case GPCMD_CLOSE_TRACK:
2909 case INITIALIZE_ELEMENT_STATUS:
2910 case GPCMD_LOAD_UNLOAD:
2911 case GPCMD_SET_SPEED:
2913 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2916 cmd->execute_cmd = target_report_luns;
2917 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2919 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
2920 * See spc4r17 section 5.3
2922 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2923 cmd->sam_task_attr = MSG_HEAD_TAG;
2924 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2926 case GET_EVENT_STATUS_NOTIFICATION:
2927 size = (cdb[7] << 8) | cdb[8];
2928 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2931 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
2932 " 0x%02x, sending CHECK_CONDITION.\n",
2933 cmd->se_tfo->get_fabric_name(), cdb[0]);
2934 goto out_unsupported_cdb;
2937 if (cmd->unknown_data_length)
2938 cmd->data_length = size;
2940 if (size != cmd->data_length) {
2941 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
2942 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
2943 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
2944 cmd->data_length, size, cdb[0]);
2946 cmd->cmd_spdtl = size;
2948 if (cmd->data_direction == DMA_TO_DEVICE) {
2949 pr_err("Rejecting underflow/overflow"
2951 goto out_invalid_cdb_field;
2954 * Reject READ_* or WRITE_* with overflow/underflow for
2955 * type SCF_SCSI_DATA_SG_IO_CDB.
2957 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
2958 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
2959 " CDB on non 512-byte sector setup subsystem"
2960 " plugin: %s\n", dev->transport->name);
2961 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
2962 goto out_invalid_cdb_field;
2965 if (size > cmd->data_length) {
2966 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
2967 cmd->residual_count = (size - cmd->data_length);
2969 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
2970 cmd->residual_count = (cmd->data_length - size);
2972 cmd->data_length = size;
2975 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
2976 if (sectors > su_dev->se_dev_attrib.fabric_max_sectors) {
2977 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
2978 " big sectors %u exceeds fabric_max_sectors:"
2979 " %u\n", cdb[0], sectors,
2980 su_dev->se_dev_attrib.fabric_max_sectors);
2981 goto out_invalid_cdb_field;
2983 if (sectors > su_dev->se_dev_attrib.hw_max_sectors) {
2984 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too"
2985 " big sectors %u exceeds backend hw_max_sectors:"
2986 " %u\n", cdb[0], sectors,
2987 su_dev->se_dev_attrib.hw_max_sectors);
2988 goto out_invalid_cdb_field;
2992 /* reject any command that we don't have a handler for */
2993 if (!(passthrough || cmd->execute_cmd ||
2994 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2995 goto out_unsupported_cdb;
2997 transport_set_supported_SAM_opcode(cmd);
3000 out_unsupported_cdb:
3001 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3002 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3004 out_invalid_cdb_field:
3005 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3006 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3011 * Called from I/O completion to determine which dormant/delayed
3012 * and ordered cmds need to have their tasks added to the execution queue.
3014 static void transport_complete_task_attr(struct se_cmd *cmd)
3016 struct se_device *dev = cmd->se_dev;
3017 struct se_cmd *cmd_p, *cmd_tmp;
3018 int new_active_tasks = 0;
3020 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3021 atomic_dec(&dev->simple_cmds);
3022 smp_mb__after_atomic_dec();
3023 dev->dev_cur_ordered_id++;
3024 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3025 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3026 cmd->se_ordered_id);
3027 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3028 dev->dev_cur_ordered_id++;
3029 pr_debug("Incremented dev_cur_ordered_id: %u for"
3030 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3031 cmd->se_ordered_id);
3032 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3033 atomic_dec(&dev->dev_ordered_sync);
3034 smp_mb__after_atomic_dec();
3036 dev->dev_cur_ordered_id++;
3037 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3038 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3041 * Process all commands up to the last received
3042 * ORDERED task attribute which requires another blocking
3045 spin_lock(&dev->delayed_cmd_lock);
3046 list_for_each_entry_safe(cmd_p, cmd_tmp,
3047 &dev->delayed_cmd_list, se_delayed_node) {
3049 list_del(&cmd_p->se_delayed_node);
3050 spin_unlock(&dev->delayed_cmd_lock);
3052 pr_debug("Calling add_tasks() for"
3053 " cmd_p: 0x%02x Task Attr: 0x%02x"
3054 " Dormant -> Active, se_ordered_id: %u\n",
3055 cmd_p->t_task_cdb[0],
3056 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3058 target_add_to_execute_list(cmd_p);
3061 spin_lock(&dev->delayed_cmd_lock);
3062 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3065 spin_unlock(&dev->delayed_cmd_lock);
3067 * If new tasks have become active, wake up the transport thread
3068 * to do the processing of the Active tasks.
3070 if (new_active_tasks != 0)
3071 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3074 static void transport_complete_qf(struct se_cmd *cmd)
3078 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3079 transport_complete_task_attr(cmd);
3081 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3082 ret = cmd->se_tfo->queue_status(cmd);
3087 switch (cmd->data_direction) {
3088 case DMA_FROM_DEVICE:
3089 ret = cmd->se_tfo->queue_data_in(cmd);
3092 if (cmd->t_bidi_data_sg) {
3093 ret = cmd->se_tfo->queue_data_in(cmd);
3097 /* Fall through for DMA_TO_DEVICE */
3099 ret = cmd->se_tfo->queue_status(cmd);
3107 transport_handle_queue_full(cmd, cmd->se_dev);
3110 transport_lun_remove_cmd(cmd);
3111 transport_cmd_check_stop_to_fabric(cmd);
3114 static void transport_handle_queue_full(
3116 struct se_device *dev)
3118 spin_lock_irq(&dev->qf_cmd_lock);
3119 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3120 atomic_inc(&dev->dev_qf_count);
3121 smp_mb__after_atomic_inc();
3122 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3124 schedule_work(&cmd->se_dev->qf_work_queue);
3127 static void target_complete_ok_work(struct work_struct *work)
3129 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
3130 int reason = 0, ret;
3133 * Check if we need to move delayed/dormant tasks from cmds on the
3134 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3137 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3138 transport_complete_task_attr(cmd);
3140 * Check to schedule QUEUE_FULL work, or execute an existing
3141 * cmd->transport_qf_callback()
3143 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3144 schedule_work(&cmd->se_dev->qf_work_queue);
3147 * Check if we need to retrieve a sense buffer from
3148 * the struct se_cmd in question.
3150 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3151 if (transport_get_sense_data(cmd) < 0)
3152 reason = TCM_NON_EXISTENT_LUN;
3154 if (cmd->scsi_status) {
3155 ret = transport_send_check_condition_and_sense(
3157 if (ret == -EAGAIN || ret == -ENOMEM)
3160 transport_lun_remove_cmd(cmd);
3161 transport_cmd_check_stop_to_fabric(cmd);
3166 * Check for a callback, used by amongst other things
3167 * XDWRITE_READ_10 emulation.
3169 if (cmd->transport_complete_callback)
3170 cmd->transport_complete_callback(cmd);
3172 switch (cmd->data_direction) {
3173 case DMA_FROM_DEVICE:
3174 spin_lock(&cmd->se_lun->lun_sep_lock);
3175 if (cmd->se_lun->lun_sep) {
3176 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3179 spin_unlock(&cmd->se_lun->lun_sep_lock);
3181 ret = cmd->se_tfo->queue_data_in(cmd);
3182 if (ret == -EAGAIN || ret == -ENOMEM)
3186 spin_lock(&cmd->se_lun->lun_sep_lock);
3187 if (cmd->se_lun->lun_sep) {
3188 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3191 spin_unlock(&cmd->se_lun->lun_sep_lock);
3193 * Check if we need to send READ payload for BIDI-COMMAND
3195 if (cmd->t_bidi_data_sg) {
3196 spin_lock(&cmd->se_lun->lun_sep_lock);
3197 if (cmd->se_lun->lun_sep) {
3198 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3201 spin_unlock(&cmd->se_lun->lun_sep_lock);
3202 ret = cmd->se_tfo->queue_data_in(cmd);
3203 if (ret == -EAGAIN || ret == -ENOMEM)
3207 /* Fall through for DMA_TO_DEVICE */
3209 ret = cmd->se_tfo->queue_status(cmd);
3210 if (ret == -EAGAIN || ret == -ENOMEM)
3217 transport_lun_remove_cmd(cmd);
3218 transport_cmd_check_stop_to_fabric(cmd);
3222 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3223 " data_direction: %d\n", cmd, cmd->data_direction);
3224 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3225 transport_handle_queue_full(cmd, cmd->se_dev);
3228 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3230 struct scatterlist *sg;
3233 for_each_sg(sgl, sg, nents, count)
3234 __free_page(sg_page(sg));
3239 static inline void transport_free_pages(struct se_cmd *cmd)
3241 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3244 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3245 cmd->t_data_sg = NULL;
3246 cmd->t_data_nents = 0;
3248 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3249 cmd->t_bidi_data_sg = NULL;
3250 cmd->t_bidi_data_nents = 0;
3254 * transport_release_cmd - free a command
3255 * @cmd: command to free
3257 * This routine unconditionally frees a command, and reference counting
3258 * or list removal must be done in the caller.
3260 static void transport_release_cmd(struct se_cmd *cmd)
3262 BUG_ON(!cmd->se_tfo);
3264 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
3265 core_tmr_release_req(cmd->se_tmr_req);
3266 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3267 kfree(cmd->t_task_cdb);
3269 * If this cmd has been setup with target_get_sess_cmd(), drop
3270 * the kref and call ->release_cmd() in kref callback.
3272 if (cmd->check_release != 0) {
3273 target_put_sess_cmd(cmd->se_sess, cmd);
3276 cmd->se_tfo->release_cmd(cmd);
3280 * transport_put_cmd - release a reference to a command
3281 * @cmd: command to release
3283 * This routine releases our reference to the command and frees it if possible.
3285 static void transport_put_cmd(struct se_cmd *cmd)
3287 unsigned long flags;
3289 spin_lock_irqsave(&cmd->t_state_lock, flags);
3290 if (atomic_read(&cmd->t_fe_count)) {
3291 if (!atomic_dec_and_test(&cmd->t_fe_count))
3295 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3296 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
3297 target_remove_from_state_list(cmd);
3299 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3301 transport_free_pages(cmd);
3302 transport_release_cmd(cmd);
3305 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3309 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3310 * allocating in the core.
3311 * @cmd: Associated se_cmd descriptor
3312 * @mem: SGL style memory for TCM WRITE / READ
3313 * @sg_mem_num: Number of SGL elements
3314 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3315 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3317 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3320 int transport_generic_map_mem_to_cmd(
3322 struct scatterlist *sgl,
3324 struct scatterlist *sgl_bidi,
3327 if (!sgl || !sgl_count)
3330 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3331 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3333 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3334 * scatterlists already have been set to follow what the fabric
3335 * passes for the original expected data transfer length.
3337 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3338 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3339 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3340 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3341 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3345 cmd->t_data_sg = sgl;
3346 cmd->t_data_nents = sgl_count;
3348 if (sgl_bidi && sgl_bidi_count) {
3349 cmd->t_bidi_data_sg = sgl_bidi;
3350 cmd->t_bidi_data_nents = sgl_bidi_count;
3352 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3357 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3359 void *transport_kmap_data_sg(struct se_cmd *cmd)
3361 struct scatterlist *sg = cmd->t_data_sg;
3362 struct page **pages;
3367 * We need to take into account a possible offset here for fabrics like
3368 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3369 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3371 if (!cmd->t_data_nents)
3373 else if (cmd->t_data_nents == 1)
3374 return kmap(sg_page(sg)) + sg->offset;
3376 /* >1 page. use vmap */
3377 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3381 /* convert sg[] to pages[] */
3382 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3383 pages[i] = sg_page(sg);
3386 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3388 if (!cmd->t_data_vmap)
3391 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
3393 EXPORT_SYMBOL(transport_kmap_data_sg);
3395 void transport_kunmap_data_sg(struct se_cmd *cmd)
3397 if (!cmd->t_data_nents) {
3399 } else if (cmd->t_data_nents == 1) {
3400 kunmap(sg_page(cmd->t_data_sg));
3404 vunmap(cmd->t_data_vmap);
3405 cmd->t_data_vmap = NULL;
3407 EXPORT_SYMBOL(transport_kunmap_data_sg);
3410 transport_generic_get_mem(struct se_cmd *cmd)
3412 u32 length = cmd->data_length;
3418 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3419 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3420 if (!cmd->t_data_sg)
3423 cmd->t_data_nents = nents;
3424 sg_init_table(cmd->t_data_sg, nents);
3426 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3429 u32 page_len = min_t(u32, length, PAGE_SIZE);
3430 page = alloc_page(GFP_KERNEL | zero_flag);
3434 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3442 __free_page(sg_page(&cmd->t_data_sg[i]));
3445 kfree(cmd->t_data_sg);
3446 cmd->t_data_sg = NULL;
3451 * Allocate any required resources to execute the command. For writes we
3452 * might not have the payload yet, so notify the fabric via a call to
3453 * ->write_pending instead. Otherwise place it on the execution queue.
3455 int transport_generic_new_cmd(struct se_cmd *cmd)
3457 struct se_device *dev = cmd->se_dev;
3461 * Determine is the TCM fabric module has already allocated physical
3462 * memory, and is directly calling transport_generic_map_mem_to_cmd()
3465 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3467 ret = transport_generic_get_mem(cmd);
3472 /* Workaround for handling zero-length control CDBs */
3473 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3474 !cmd->data_length) {
3475 spin_lock_irq(&cmd->t_state_lock);
3476 cmd->t_state = TRANSPORT_COMPLETE;
3477 cmd->transport_state |= CMD_T_ACTIVE;
3478 spin_unlock_irq(&cmd->t_state_lock);
3480 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3481 u8 ua_asc = 0, ua_ascq = 0;
3483 core_scsi3_ua_clear_for_request_sense(cmd,
3487 INIT_WORK(&cmd->work, target_complete_ok_work);
3488 queue_work(target_completion_wq, &cmd->work);
3492 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3493 struct se_dev_attrib *attr = &dev->se_sub_dev->se_dev_attrib;
3495 if (transport_cmd_get_valid_sectors(cmd) < 0)
3498 BUG_ON(cmd->data_length % attr->block_size);
3499 BUG_ON(DIV_ROUND_UP(cmd->data_length, attr->block_size) >
3500 attr->hw_max_sectors);
3503 atomic_inc(&cmd->t_fe_count);
3506 * For WRITEs, let the fabric know its buffer is ready.
3508 * The command will be added to the execution queue after its write
3511 if (cmd->data_direction == DMA_TO_DEVICE) {
3512 target_add_to_state_list(cmd);
3513 return transport_generic_write_pending(cmd);
3516 * Everything else but a WRITE, add the command to the execution queue.
3518 transport_execute_tasks(cmd);
3522 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3523 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3526 EXPORT_SYMBOL(transport_generic_new_cmd);
3528 /* transport_generic_process_write():
3532 void transport_generic_process_write(struct se_cmd *cmd)
3534 transport_execute_tasks(cmd);
3536 EXPORT_SYMBOL(transport_generic_process_write);
3538 static void transport_write_pending_qf(struct se_cmd *cmd)
3542 ret = cmd->se_tfo->write_pending(cmd);
3543 if (ret == -EAGAIN || ret == -ENOMEM) {
3544 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3546 transport_handle_queue_full(cmd, cmd->se_dev);
3550 static int transport_generic_write_pending(struct se_cmd *cmd)
3552 unsigned long flags;
3555 spin_lock_irqsave(&cmd->t_state_lock, flags);
3556 cmd->t_state = TRANSPORT_WRITE_PENDING;
3557 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3560 * Clear the se_cmd for WRITE_PENDING status in order to set
3561 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
3562 * from HW target mode interrupt code. This is safe to be called
3563 * with transport_off=1 before the cmd->se_tfo->write_pending
3564 * because the se_cmd->se_lun pointer is not being cleared.
3566 transport_cmd_check_stop(cmd, 1, 0);
3569 * Call the fabric write_pending function here to let the
3570 * frontend know that WRITE buffers are ready.
3572 ret = cmd->se_tfo->write_pending(cmd);
3573 if (ret == -EAGAIN || ret == -ENOMEM)
3581 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
3582 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
3583 transport_handle_queue_full(cmd, cmd->se_dev);
3587 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
3589 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3590 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
3591 transport_wait_for_tasks(cmd);
3593 transport_release_cmd(cmd);
3596 transport_wait_for_tasks(cmd);
3598 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3601 transport_lun_remove_cmd(cmd);
3603 transport_put_cmd(cmd);
3606 EXPORT_SYMBOL(transport_generic_free_cmd);
3608 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
3609 * @se_sess: session to reference
3610 * @se_cmd: command descriptor to add
3611 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
3613 void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
3616 unsigned long flags;
3618 kref_init(&se_cmd->cmd_kref);
3620 * Add a second kref if the fabric caller is expecting to handle
3621 * fabric acknowledgement that requires two target_put_sess_cmd()
3622 * invocations before se_cmd descriptor release.
3624 if (ack_kref == true) {
3625 kref_get(&se_cmd->cmd_kref);
3626 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
3629 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3630 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3631 se_cmd->check_release = 1;
3632 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3634 EXPORT_SYMBOL(target_get_sess_cmd);
3636 static void target_release_cmd_kref(struct kref *kref)
3638 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
3639 struct se_session *se_sess = se_cmd->se_sess;
3640 unsigned long flags;
3642 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3643 if (list_empty(&se_cmd->se_cmd_list)) {
3644 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3645 se_cmd->se_tfo->release_cmd(se_cmd);
3648 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3649 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3650 complete(&se_cmd->cmd_wait_comp);
3653 list_del(&se_cmd->se_cmd_list);
3654 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3656 se_cmd->se_tfo->release_cmd(se_cmd);
3659 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
3660 * @se_sess: session to reference
3661 * @se_cmd: command descriptor to drop
3663 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3665 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
3667 EXPORT_SYMBOL(target_put_sess_cmd);
3669 /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3670 * @se_sess: session to split
3672 void target_splice_sess_cmd_list(struct se_session *se_sess)
3674 struct se_cmd *se_cmd;
3675 unsigned long flags;
3677 WARN_ON(!list_empty(&se_sess->sess_wait_list));
3678 INIT_LIST_HEAD(&se_sess->sess_wait_list);
3680 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3681 se_sess->sess_tearing_down = 1;
3683 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3685 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3686 se_cmd->cmd_wait_set = 1;
3688 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3690 EXPORT_SYMBOL(target_splice_sess_cmd_list);
3692 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
3693 * @se_sess: session to wait for active I/O
3694 * @wait_for_tasks: Make extra transport_wait_for_tasks call
3696 void target_wait_for_sess_cmds(
3697 struct se_session *se_sess,
3700 struct se_cmd *se_cmd, *tmp_cmd;
3703 list_for_each_entry_safe(se_cmd, tmp_cmd,
3704 &se_sess->sess_wait_list, se_cmd_list) {
3705 list_del(&se_cmd->se_cmd_list);
3707 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
3708 " %d\n", se_cmd, se_cmd->t_state,
3709 se_cmd->se_tfo->get_cmd_state(se_cmd));
3711 if (wait_for_tasks) {
3712 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
3713 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3714 se_cmd->se_tfo->get_cmd_state(se_cmd));
3716 rc = transport_wait_for_tasks(se_cmd);
3718 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
3719 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3720 se_cmd->se_tfo->get_cmd_state(se_cmd));
3724 wait_for_completion(&se_cmd->cmd_wait_comp);
3725 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
3726 " fabric state: %d\n", se_cmd, se_cmd->t_state,
3727 se_cmd->se_tfo->get_cmd_state(se_cmd));
3730 se_cmd->se_tfo->release_cmd(se_cmd);
3733 EXPORT_SYMBOL(target_wait_for_sess_cmds);
3735 /* transport_lun_wait_for_tasks():
3737 * Called from ConfigFS context to stop the passed struct se_cmd to allow
3738 * an struct se_lun to be successfully shutdown.
3740 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
3742 unsigned long flags;
3746 * If the frontend has already requested this struct se_cmd to
3747 * be stopped, we can safely ignore this struct se_cmd.
3749 spin_lock_irqsave(&cmd->t_state_lock, flags);
3750 if (cmd->transport_state & CMD_T_STOP) {
3751 cmd->transport_state &= ~CMD_T_LUN_STOP;
3753 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
3754 cmd->se_tfo->get_task_tag(cmd));
3755 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3756 transport_cmd_check_stop(cmd, 1, 0);
3759 cmd->transport_state |= CMD_T_LUN_FE_STOP;
3760 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3762 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
3764 // XXX: audit task_flags checks.
3765 spin_lock_irqsave(&cmd->t_state_lock, flags);
3766 if ((cmd->transport_state & CMD_T_BUSY) &&
3767 (cmd->transport_state & CMD_T_SENT)) {
3768 if (!target_stop_cmd(cmd, &flags))
3770 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3772 spin_unlock_irqrestore(&cmd->t_state_lock,
3774 target_remove_from_execute_list(cmd);
3777 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
3780 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
3781 cmd->se_tfo->get_task_tag(cmd));
3782 wait_for_completion(&cmd->transport_lun_stop_comp);
3783 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
3784 cmd->se_tfo->get_task_tag(cmd));
3786 transport_remove_cmd_from_queue(cmd);
3791 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
3793 struct se_cmd *cmd = NULL;
3794 unsigned long lun_flags, cmd_flags;
3796 * Do exception processing and return CHECK_CONDITION status to the
3799 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3800 while (!list_empty(&lun->lun_cmd_list)) {
3801 cmd = list_first_entry(&lun->lun_cmd_list,
3802 struct se_cmd, se_lun_node);
3803 list_del_init(&cmd->se_lun_node);
3806 * This will notify iscsi_target_transport.c:
3807 * transport_cmd_check_stop() that a LUN shutdown is in
3808 * progress for the iscsi_cmd_t.
3810 spin_lock(&cmd->t_state_lock);
3811 pr_debug("SE_LUN[%d] - Setting cmd->transport"
3812 "_lun_stop for ITT: 0x%08x\n",
3813 cmd->se_lun->unpacked_lun,
3814 cmd->se_tfo->get_task_tag(cmd));
3815 cmd->transport_state |= CMD_T_LUN_STOP;
3816 spin_unlock(&cmd->t_state_lock);
3818 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3821 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
3822 cmd->se_tfo->get_task_tag(cmd),
3823 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
3827 * If the Storage engine still owns the iscsi_cmd_t, determine
3828 * and/or stop its context.
3830 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
3831 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
3832 cmd->se_tfo->get_task_tag(cmd));
3834 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
3835 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3839 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
3840 "_wait_for_tasks(): SUCCESS\n",
3841 cmd->se_lun->unpacked_lun,
3842 cmd->se_tfo->get_task_tag(cmd));
3844 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
3845 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
3846 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
3849 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
3850 target_remove_from_state_list(cmd);
3851 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
3854 * The Storage engine stopped this struct se_cmd before it was
3855 * send to the fabric frontend for delivery back to the
3856 * Initiator Node. Return this SCSI CDB back with an
3857 * CHECK_CONDITION status.
3860 transport_send_check_condition_and_sense(cmd,
3861 TCM_NON_EXISTENT_LUN, 0);
3863 * If the fabric frontend is waiting for this iscsi_cmd_t to
3864 * be released, notify the waiting thread now that LU has
3865 * finished accessing it.
3867 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
3868 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
3869 pr_debug("SE_LUN[%d] - Detected FE stop for"
3870 " struct se_cmd: %p ITT: 0x%08x\n",
3872 cmd, cmd->se_tfo->get_task_tag(cmd));
3874 spin_unlock_irqrestore(&cmd->t_state_lock,
3876 transport_cmd_check_stop(cmd, 1, 0);
3877 complete(&cmd->transport_lun_fe_stop_comp);
3878 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3881 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
3882 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
3884 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
3885 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
3887 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
3890 static int transport_clear_lun_thread(void *p)
3892 struct se_lun *lun = p;
3894 __transport_clear_lun_from_sessions(lun);
3895 complete(&lun->lun_shutdown_comp);
3900 int transport_clear_lun_from_sessions(struct se_lun *lun)
3902 struct task_struct *kt;
3904 kt = kthread_run(transport_clear_lun_thread, lun,
3905 "tcm_cl_%u", lun->unpacked_lun);
3907 pr_err("Unable to start clear_lun thread\n");
3910 wait_for_completion(&lun->lun_shutdown_comp);
3916 * transport_wait_for_tasks - wait for completion to occur
3917 * @cmd: command to wait
3919 * Called from frontend fabric context to wait for storage engine
3920 * to pause and/or release frontend generated struct se_cmd.
3922 bool transport_wait_for_tasks(struct se_cmd *cmd)
3924 unsigned long flags;
3926 spin_lock_irqsave(&cmd->t_state_lock, flags);
3927 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
3928 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
3929 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3933 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
3934 * has been set in transport_set_supported_SAM_opcode().
3936 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
3937 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
3938 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3942 * If we are already stopped due to an external event (ie: LUN shutdown)
3943 * sleep until the connection can have the passed struct se_cmd back.
3944 * The cmd->transport_lun_stopped_sem will be upped by
3945 * transport_clear_lun_from_sessions() once the ConfigFS context caller
3946 * has completed its operation on the struct se_cmd.
3948 if (cmd->transport_state & CMD_T_LUN_STOP) {
3949 pr_debug("wait_for_tasks: Stopping"
3950 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
3951 "_stop_comp); for ITT: 0x%08x\n",
3952 cmd->se_tfo->get_task_tag(cmd));
3954 * There is a special case for WRITES where a FE exception +
3955 * LUN shutdown means ConfigFS context is still sleeping on
3956 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
3957 * We go ahead and up transport_lun_stop_comp just to be sure
3960 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3961 complete(&cmd->transport_lun_stop_comp);
3962 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
3963 spin_lock_irqsave(&cmd->t_state_lock, flags);
3965 target_remove_from_state_list(cmd);
3967 * At this point, the frontend who was the originator of this
3968 * struct se_cmd, now owns the structure and can be released through
3969 * normal means below.
3971 pr_debug("wait_for_tasks: Stopped"
3972 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
3973 "stop_comp); for ITT: 0x%08x\n",
3974 cmd->se_tfo->get_task_tag(cmd));
3976 cmd->transport_state &= ~CMD_T_LUN_STOP;
3979 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
3980 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3984 cmd->transport_state |= CMD_T_STOP;
3986 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
3987 " i_state: %d, t_state: %d, CMD_T_STOP\n",
3988 cmd, cmd->se_tfo->get_task_tag(cmd),
3989 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
3991 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3993 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
3995 wait_for_completion(&cmd->t_transport_stop_comp);
3997 spin_lock_irqsave(&cmd->t_state_lock, flags);
3998 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
4000 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4001 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4002 cmd->se_tfo->get_task_tag(cmd));
4004 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4008 EXPORT_SYMBOL(transport_wait_for_tasks);
4010 static int transport_get_sense_codes(
4015 *asc = cmd->scsi_asc;
4016 *ascq = cmd->scsi_ascq;
4021 static int transport_set_sense_codes(
4026 cmd->scsi_asc = asc;
4027 cmd->scsi_ascq = ascq;
4032 int transport_send_check_condition_and_sense(
4037 unsigned char *buffer = cmd->sense_buffer;
4038 unsigned long flags;
4040 u8 asc = 0, ascq = 0;
4042 spin_lock_irqsave(&cmd->t_state_lock, flags);
4043 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4044 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4047 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4048 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4050 if (!reason && from_transport)
4053 if (!from_transport)
4054 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4056 * Data Segment and SenseLength of the fabric response PDU.
4058 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4059 * from include/scsi/scsi_cmnd.h
4061 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4062 TRANSPORT_SENSE_BUFFER);
4064 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4065 * SENSE KEY values from include/scsi/scsi.h
4068 case TCM_NON_EXISTENT_LUN:
4070 buffer[offset] = 0x70;
4071 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4072 /* ILLEGAL REQUEST */
4073 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4074 /* LOGICAL UNIT NOT SUPPORTED */
4075 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4077 case TCM_UNSUPPORTED_SCSI_OPCODE:
4078 case TCM_SECTOR_COUNT_TOO_MANY:
4080 buffer[offset] = 0x70;
4081 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4082 /* ILLEGAL REQUEST */
4083 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4084 /* INVALID COMMAND OPERATION CODE */
4085 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4087 case TCM_UNKNOWN_MODE_PAGE:
4089 buffer[offset] = 0x70;
4090 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4091 /* ILLEGAL REQUEST */
4092 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4093 /* INVALID FIELD IN CDB */
4094 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4096 case TCM_CHECK_CONDITION_ABORT_CMD:
4098 buffer[offset] = 0x70;
4099 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4100 /* ABORTED COMMAND */
4101 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4102 /* BUS DEVICE RESET FUNCTION OCCURRED */
4103 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4104 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4106 case TCM_INCORRECT_AMOUNT_OF_DATA:
4108 buffer[offset] = 0x70;
4109 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4110 /* ABORTED COMMAND */
4111 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4113 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4114 /* NOT ENOUGH UNSOLICITED DATA */
4115 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4117 case TCM_INVALID_CDB_FIELD:
4119 buffer[offset] = 0x70;
4120 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4121 /* ILLEGAL REQUEST */
4122 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4123 /* INVALID FIELD IN CDB */
4124 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4126 case TCM_INVALID_PARAMETER_LIST:
4128 buffer[offset] = 0x70;
4129 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4130 /* ILLEGAL REQUEST */
4131 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4132 /* INVALID FIELD IN PARAMETER LIST */
4133 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4135 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4137 buffer[offset] = 0x70;
4138 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4139 /* ABORTED COMMAND */
4140 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4142 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4143 /* UNEXPECTED_UNSOLICITED_DATA */
4144 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4146 case TCM_SERVICE_CRC_ERROR:
4148 buffer[offset] = 0x70;
4149 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4150 /* ABORTED COMMAND */
4151 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4152 /* PROTOCOL SERVICE CRC ERROR */
4153 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4155 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4157 case TCM_SNACK_REJECTED:
4159 buffer[offset] = 0x70;
4160 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4161 /* ABORTED COMMAND */
4162 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4164 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4165 /* FAILED RETRANSMISSION REQUEST */
4166 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4168 case TCM_WRITE_PROTECTED:
4170 buffer[offset] = 0x70;
4171 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4173 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4174 /* WRITE PROTECTED */
4175 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4177 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4179 buffer[offset] = 0x70;
4180 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4181 /* UNIT ATTENTION */
4182 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4183 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4184 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4185 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4187 case TCM_CHECK_CONDITION_NOT_READY:
4189 buffer[offset] = 0x70;
4190 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4192 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4193 transport_get_sense_codes(cmd, &asc, &ascq);
4194 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4195 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4197 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4200 buffer[offset] = 0x70;
4201 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4202 /* ILLEGAL REQUEST */
4203 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4204 /* LOGICAL UNIT COMMUNICATION FAILURE */
4205 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4209 * This code uses linux/include/scsi/scsi.h SAM status codes!
4211 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4213 * Automatically padded, this value is encoded in the fabric's
4214 * data_length response PDU containing the SCSI defined sense data.
4216 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4219 return cmd->se_tfo->queue_status(cmd);
4221 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4223 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4227 if (cmd->transport_state & CMD_T_ABORTED) {
4229 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4232 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4233 " status for CDB: 0x%02x ITT: 0x%08x\n",
4235 cmd->se_tfo->get_task_tag(cmd));
4237 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4238 cmd->se_tfo->queue_status(cmd);
4243 EXPORT_SYMBOL(transport_check_aborted_status);
4245 void transport_send_task_abort(struct se_cmd *cmd)
4247 unsigned long flags;
4249 spin_lock_irqsave(&cmd->t_state_lock, flags);
4250 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4251 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4254 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4257 * If there are still expected incoming fabric WRITEs, we wait
4258 * until until they have completed before sending a TASK_ABORTED
4259 * response. This response with TASK_ABORTED status will be
4260 * queued back to fabric module by transport_check_aborted_status().
4262 if (cmd->data_direction == DMA_TO_DEVICE) {
4263 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4264 cmd->transport_state |= CMD_T_ABORTED;
4265 smp_mb__after_atomic_inc();
4268 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4270 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4271 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4272 cmd->se_tfo->get_task_tag(cmd));
4274 cmd->se_tfo->queue_status(cmd);
4277 static int transport_generic_do_tmr(struct se_cmd *cmd)
4279 struct se_device *dev = cmd->se_dev;
4280 struct se_tmr_req *tmr = cmd->se_tmr_req;
4283 switch (tmr->function) {
4284 case TMR_ABORT_TASK:
4285 core_tmr_abort_task(dev, tmr, cmd->se_sess);
4287 case TMR_ABORT_TASK_SET:
4289 case TMR_CLEAR_TASK_SET:
4290 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4293 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4294 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4295 TMR_FUNCTION_REJECTED;
4297 case TMR_TARGET_WARM_RESET:
4298 tmr->response = TMR_FUNCTION_REJECTED;
4300 case TMR_TARGET_COLD_RESET:
4301 tmr->response = TMR_FUNCTION_REJECTED;
4304 pr_err("Uknown TMR function: 0x%02x.\n",
4306 tmr->response = TMR_FUNCTION_REJECTED;
4310 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4311 cmd->se_tfo->queue_tm_rsp(cmd);
4313 transport_cmd_check_stop_to_fabric(cmd);
4317 /* transport_processing_thread():
4321 static int transport_processing_thread(void *param)
4325 struct se_device *dev = param;
4327 while (!kthread_should_stop()) {
4328 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4329 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4330 kthread_should_stop());
4335 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4339 switch (cmd->t_state) {
4340 case TRANSPORT_NEW_CMD:
4343 case TRANSPORT_NEW_CMD_MAP:
4344 if (!cmd->se_tfo->new_cmd_map) {
4345 pr_err("cmd->se_tfo->new_cmd_map is"
4346 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4349 ret = cmd->se_tfo->new_cmd_map(cmd);
4351 transport_generic_request_failure(cmd);
4354 ret = transport_generic_new_cmd(cmd);
4356 transport_generic_request_failure(cmd);
4360 case TRANSPORT_PROCESS_WRITE:
4361 transport_generic_process_write(cmd);
4363 case TRANSPORT_PROCESS_TMR:
4364 transport_generic_do_tmr(cmd);
4366 case TRANSPORT_COMPLETE_QF_WP:
4367 transport_write_pending_qf(cmd);
4369 case TRANSPORT_COMPLETE_QF_OK:
4370 transport_complete_qf(cmd);
4373 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4374 "i_state: %d on SE LUN: %u\n",
4376 cmd->se_tfo->get_task_tag(cmd),
4377 cmd->se_tfo->get_cmd_state(cmd),
4378 cmd->se_lun->unpacked_lun);
4386 WARN_ON(!list_empty(&dev->state_list));
4387 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4388 dev->process_thread = NULL;