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 void transport_free_dev_tasks(struct se_cmd *cmd);
76 static int transport_generic_get_mem(struct se_cmd *cmd);
77 static void transport_put_cmd(struct se_cmd *cmd);
78 static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
79 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
80 static void target_complete_ok_work(struct work_struct *work);
82 int init_se_kmem_caches(void)
84 se_sess_cache = kmem_cache_create("se_sess_cache",
85 sizeof(struct se_session), __alignof__(struct se_session),
88 pr_err("kmem_cache_create() for struct se_session"
92 se_ua_cache = kmem_cache_create("se_ua_cache",
93 sizeof(struct se_ua), __alignof__(struct se_ua),
96 pr_err("kmem_cache_create() for struct se_ua failed\n");
97 goto out_free_sess_cache;
99 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
100 sizeof(struct t10_pr_registration),
101 __alignof__(struct t10_pr_registration), 0, NULL);
102 if (!t10_pr_reg_cache) {
103 pr_err("kmem_cache_create() for struct t10_pr_registration"
105 goto out_free_ua_cache;
107 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
108 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
110 if (!t10_alua_lu_gp_cache) {
111 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
113 goto out_free_pr_reg_cache;
115 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
116 sizeof(struct t10_alua_lu_gp_member),
117 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
118 if (!t10_alua_lu_gp_mem_cache) {
119 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
121 goto out_free_lu_gp_cache;
123 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
124 sizeof(struct t10_alua_tg_pt_gp),
125 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
126 if (!t10_alua_tg_pt_gp_cache) {
127 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
129 goto out_free_lu_gp_mem_cache;
131 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
132 "t10_alua_tg_pt_gp_mem_cache",
133 sizeof(struct t10_alua_tg_pt_gp_member),
134 __alignof__(struct t10_alua_tg_pt_gp_member),
136 if (!t10_alua_tg_pt_gp_mem_cache) {
137 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
139 goto out_free_tg_pt_gp_cache;
142 target_completion_wq = alloc_workqueue("target_completion",
144 if (!target_completion_wq)
145 goto out_free_tg_pt_gp_mem_cache;
149 out_free_tg_pt_gp_mem_cache:
150 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
151 out_free_tg_pt_gp_cache:
152 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
153 out_free_lu_gp_mem_cache:
154 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
155 out_free_lu_gp_cache:
156 kmem_cache_destroy(t10_alua_lu_gp_cache);
157 out_free_pr_reg_cache:
158 kmem_cache_destroy(t10_pr_reg_cache);
160 kmem_cache_destroy(se_ua_cache);
162 kmem_cache_destroy(se_sess_cache);
167 void release_se_kmem_caches(void)
169 destroy_workqueue(target_completion_wq);
170 kmem_cache_destroy(se_sess_cache);
171 kmem_cache_destroy(se_ua_cache);
172 kmem_cache_destroy(t10_pr_reg_cache);
173 kmem_cache_destroy(t10_alua_lu_gp_cache);
174 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
175 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
176 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
179 /* This code ensures unique mib indexes are handed out. */
180 static DEFINE_SPINLOCK(scsi_mib_index_lock);
181 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
184 * Allocate a new row index for the entry type specified
186 u32 scsi_get_new_index(scsi_index_t type)
190 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
192 spin_lock(&scsi_mib_index_lock);
193 new_index = ++scsi_mib_index[type];
194 spin_unlock(&scsi_mib_index_lock);
199 static void transport_init_queue_obj(struct se_queue_obj *qobj)
201 atomic_set(&qobj->queue_cnt, 0);
202 INIT_LIST_HEAD(&qobj->qobj_list);
203 init_waitqueue_head(&qobj->thread_wq);
204 spin_lock_init(&qobj->cmd_queue_lock);
207 void transport_subsystem_check_init(void)
211 if (sub_api_initialized)
214 ret = request_module("target_core_iblock");
216 pr_err("Unable to load target_core_iblock\n");
218 ret = request_module("target_core_file");
220 pr_err("Unable to load target_core_file\n");
222 ret = request_module("target_core_pscsi");
224 pr_err("Unable to load target_core_pscsi\n");
226 ret = request_module("target_core_stgt");
228 pr_err("Unable to load target_core_stgt\n");
230 sub_api_initialized = 1;
234 struct se_session *transport_init_session(void)
236 struct se_session *se_sess;
238 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
240 pr_err("Unable to allocate struct se_session from"
242 return ERR_PTR(-ENOMEM);
244 INIT_LIST_HEAD(&se_sess->sess_list);
245 INIT_LIST_HEAD(&se_sess->sess_acl_list);
246 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
247 INIT_LIST_HEAD(&se_sess->sess_wait_list);
248 spin_lock_init(&se_sess->sess_cmd_lock);
252 EXPORT_SYMBOL(transport_init_session);
255 * Called with spin_lock_bh(&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 spin_lock_irq(&se_nacl->nacl_sess_lock);
286 * The se_nacl->nacl_sess pointer will be set to the
287 * last active I_T Nexus for each struct se_node_acl.
289 se_nacl->nacl_sess = se_sess;
291 list_add_tail(&se_sess->sess_acl_list,
292 &se_nacl->acl_sess_list);
293 spin_unlock_irq(&se_nacl->nacl_sess_lock);
295 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
297 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
298 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
300 EXPORT_SYMBOL(__transport_register_session);
302 void transport_register_session(
303 struct se_portal_group *se_tpg,
304 struct se_node_acl *se_nacl,
305 struct se_session *se_sess,
306 void *fabric_sess_ptr)
308 spin_lock_bh(&se_tpg->session_lock);
309 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
310 spin_unlock_bh(&se_tpg->session_lock);
312 EXPORT_SYMBOL(transport_register_session);
314 void transport_deregister_session_configfs(struct se_session *se_sess)
316 struct se_node_acl *se_nacl;
319 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
321 se_nacl = se_sess->se_node_acl;
323 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
324 list_del(&se_sess->sess_acl_list);
326 * If the session list is empty, then clear the pointer.
327 * Otherwise, set the struct se_session pointer from the tail
328 * element of the per struct se_node_acl active session list.
330 if (list_empty(&se_nacl->acl_sess_list))
331 se_nacl->nacl_sess = NULL;
333 se_nacl->nacl_sess = container_of(
334 se_nacl->acl_sess_list.prev,
335 struct se_session, sess_acl_list);
337 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
340 EXPORT_SYMBOL(transport_deregister_session_configfs);
342 void transport_free_session(struct se_session *se_sess)
344 kmem_cache_free(se_sess_cache, se_sess);
346 EXPORT_SYMBOL(transport_free_session);
348 void transport_deregister_session(struct se_session *se_sess)
350 struct se_portal_group *se_tpg = se_sess->se_tpg;
351 struct se_node_acl *se_nacl;
355 transport_free_session(se_sess);
359 spin_lock_irqsave(&se_tpg->session_lock, flags);
360 list_del(&se_sess->sess_list);
361 se_sess->se_tpg = NULL;
362 se_sess->fabric_sess_ptr = NULL;
363 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
366 * Determine if we need to do extra work for this initiator node's
367 * struct se_node_acl if it had been previously dynamically generated.
369 se_nacl = se_sess->se_node_acl;
371 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
372 if (se_nacl->dynamic_node_acl) {
373 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
375 list_del(&se_nacl->acl_list);
376 se_tpg->num_node_acls--;
377 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
379 core_tpg_wait_for_nacl_pr_ref(se_nacl);
380 core_free_device_list_for_node(se_nacl, se_tpg);
381 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
383 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
386 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
389 transport_free_session(se_sess);
391 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
392 se_tpg->se_tpg_tfo->get_fabric_name());
394 EXPORT_SYMBOL(transport_deregister_session);
397 * Called with cmd->t_state_lock held.
399 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
401 struct se_device *dev = cmd->se_dev;
402 struct se_task *task;
408 list_for_each_entry(task, &cmd->t_task_list, t_list) {
409 if (task->task_flags & TF_ACTIVE)
412 spin_lock_irqsave(&dev->execute_task_lock, flags);
413 if (task->t_state_active) {
414 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
415 cmd->se_tfo->get_task_tag(cmd), dev, task);
417 list_del(&task->t_state_list);
418 atomic_dec(&cmd->t_task_cdbs_ex_left);
419 task->t_state_active = false;
421 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
426 /* transport_cmd_check_stop():
428 * 'transport_off = 1' determines if CMD_T_ACTIVE should be cleared.
429 * 'transport_off = 2' determines if task_dev_state should be removed.
431 * A non-zero u8 t_state sets cmd->t_state.
432 * Returns 1 when command is stopped, else 0.
434 static int transport_cmd_check_stop(
441 spin_lock_irqsave(&cmd->t_state_lock, flags);
443 * Determine if IOCTL context caller in requesting the stopping of this
444 * command for LUN shutdown purposes.
446 if (cmd->transport_state & CMD_T_LUN_STOP) {
447 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
448 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
450 cmd->transport_state &= ~CMD_T_ACTIVE;
451 if (transport_off == 2)
452 transport_all_task_dev_remove_state(cmd);
453 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
455 complete(&cmd->transport_lun_stop_comp);
459 * Determine if frontend context caller is requesting the stopping of
460 * this command for frontend exceptions.
462 if (cmd->transport_state & CMD_T_STOP) {
463 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
465 cmd->se_tfo->get_task_tag(cmd));
467 if (transport_off == 2)
468 transport_all_task_dev_remove_state(cmd);
471 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
474 if (transport_off == 2)
476 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
478 complete(&cmd->t_transport_stop_comp);
482 cmd->transport_state &= ~CMD_T_ACTIVE;
483 if (transport_off == 2) {
484 transport_all_task_dev_remove_state(cmd);
486 * Clear struct se_cmd->se_lun before the transport_off == 2
487 * handoff to fabric module.
491 * Some fabric modules like tcm_loop can release
492 * their internally allocated I/O reference now and
495 * Fabric modules are expected to return '1' here if the
496 * se_cmd being passed is released at this point,
497 * or zero if not being released.
499 if (cmd->se_tfo->check_stop_free != NULL) {
500 spin_unlock_irqrestore(
501 &cmd->t_state_lock, flags);
503 return cmd->se_tfo->check_stop_free(cmd);
506 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
510 cmd->t_state = t_state;
511 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
516 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
518 return transport_cmd_check_stop(cmd, 2, 0);
521 static void transport_lun_remove_cmd(struct se_cmd *cmd)
523 struct se_lun *lun = cmd->se_lun;
529 spin_lock_irqsave(&cmd->t_state_lock, flags);
530 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
531 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
532 transport_all_task_dev_remove_state(cmd);
534 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
536 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
537 if (!list_empty(&cmd->se_lun_node))
538 list_del_init(&cmd->se_lun_node);
539 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
542 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
544 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
545 transport_lun_remove_cmd(cmd);
547 if (transport_cmd_check_stop_to_fabric(cmd))
550 transport_remove_cmd_from_queue(cmd);
551 transport_put_cmd(cmd);
555 static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
558 struct se_device *dev = cmd->se_dev;
559 struct se_queue_obj *qobj = &dev->dev_queue_obj;
563 spin_lock_irqsave(&cmd->t_state_lock, flags);
564 cmd->t_state = t_state;
565 cmd->transport_state |= CMD_T_ACTIVE;
566 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
569 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
571 /* If the cmd is already on the list, remove it before we add it */
572 if (!list_empty(&cmd->se_queue_node))
573 list_del(&cmd->se_queue_node);
575 atomic_inc(&qobj->queue_cnt);
578 list_add(&cmd->se_queue_node, &qobj->qobj_list);
580 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
581 cmd->transport_state |= CMD_T_QUEUED;
582 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
584 wake_up_interruptible(&qobj->thread_wq);
587 static struct se_cmd *
588 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
593 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
594 if (list_empty(&qobj->qobj_list)) {
595 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
598 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
600 cmd->transport_state &= ~CMD_T_QUEUED;
601 list_del_init(&cmd->se_queue_node);
602 atomic_dec(&qobj->queue_cnt);
603 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
608 static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
610 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
613 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
614 if (!(cmd->transport_state & CMD_T_QUEUED)) {
615 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
618 cmd->transport_state &= ~CMD_T_QUEUED;
619 atomic_dec(&qobj->queue_cnt);
620 list_del_init(&cmd->se_queue_node);
621 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
625 * Completion function used by TCM subsystem plugins (such as FILEIO)
626 * for queueing up response from struct se_subsystem_api->do_task()
628 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
630 struct se_task *task = list_entry(cmd->t_task_list.next,
631 struct se_task, t_list);
634 cmd->scsi_status = SAM_STAT_GOOD;
635 task->task_scsi_status = GOOD;
637 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
638 task->task_se_cmd->scsi_sense_reason =
639 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
643 transport_complete_task(task, good);
645 EXPORT_SYMBOL(transport_complete_sync_cache);
647 static void target_complete_failure_work(struct work_struct *work)
649 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
651 transport_generic_request_failure(cmd);
654 /* transport_complete_task():
656 * Called from interrupt and non interrupt context depending
657 * on the transport plugin.
659 void transport_complete_task(struct se_task *task, int success)
661 struct se_cmd *cmd = task->task_se_cmd;
662 struct se_device *dev = cmd->se_dev;
665 spin_lock_irqsave(&cmd->t_state_lock, flags);
666 task->task_flags &= ~TF_ACTIVE;
669 * See if any sense data exists, if so set the TASK_SENSE flag.
670 * Also check for any other post completion work that needs to be
671 * done by the plugins.
673 if (dev && dev->transport->transport_complete) {
674 if (dev->transport->transport_complete(task) != 0) {
675 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
676 task->task_flags |= TF_HAS_SENSE;
682 * See if we are waiting for outstanding struct se_task
683 * to complete for an exception condition
685 if (task->task_flags & TF_REQUEST_STOP) {
686 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
687 complete(&task->task_stop_comp);
692 cmd->transport_state |= CMD_T_FAILED;
695 * Decrement the outstanding t_task_cdbs_left count. The last
696 * struct se_task from struct se_cmd will complete itself into the
697 * device queue depending upon int success.
699 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
700 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
704 * Check for case where an explict ABORT_TASK has been received
705 * and transport_wait_for_tasks() will be waiting for completion..
707 if (cmd->transport_state & CMD_T_ABORTED &&
708 cmd->transport_state & CMD_T_STOP) {
709 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
710 complete(&cmd->t_transport_stop_comp);
712 } else if (cmd->transport_state & CMD_T_FAILED) {
713 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
714 INIT_WORK(&cmd->work, target_complete_failure_work);
716 INIT_WORK(&cmd->work, target_complete_ok_work);
719 cmd->t_state = TRANSPORT_COMPLETE;
720 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
721 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
723 queue_work(target_completion_wq, &cmd->work);
725 EXPORT_SYMBOL(transport_complete_task);
728 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
729 * struct se_task list are ready to be added to the active execution list
732 * Called with se_dev_t->execute_task_lock called.
734 static inline int transport_add_task_check_sam_attr(
735 struct se_task *task,
736 struct se_task *task_prev,
737 struct se_device *dev)
740 * No SAM Task attribute emulation enabled, add to tail of
743 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
744 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
748 * HEAD_OF_QUEUE attribute for received CDB, which means
749 * the first task that is associated with a struct se_cmd goes to
750 * head of the struct se_device->execute_task_list, and task_prev
751 * after that for each subsequent task
753 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
754 list_add(&task->t_execute_list,
755 (task_prev != NULL) ?
756 &task_prev->t_execute_list :
757 &dev->execute_task_list);
759 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
760 " in execution queue\n",
761 task->task_se_cmd->t_task_cdb[0]);
765 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
766 * transitioned from Dermant -> Active state, and are added to the end
767 * of the struct se_device->execute_task_list
769 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
773 /* __transport_add_task_to_execute_queue():
775 * Called with se_dev_t->execute_task_lock called.
777 static void __transport_add_task_to_execute_queue(
778 struct se_task *task,
779 struct se_task *task_prev,
780 struct se_device *dev)
784 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
785 atomic_inc(&dev->execute_tasks);
787 if (task->t_state_active)
790 * Determine if this task needs to go to HEAD_OF_QUEUE for the
791 * state list as well. Running with SAM Task Attribute emulation
792 * will always return head_of_queue == 0 here
795 list_add(&task->t_state_list, (task_prev) ?
796 &task_prev->t_state_list :
797 &dev->state_task_list);
799 list_add_tail(&task->t_state_list, &dev->state_task_list);
801 task->t_state_active = true;
803 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
804 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
808 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
810 struct se_device *dev = cmd->se_dev;
811 struct se_task *task;
814 spin_lock_irqsave(&cmd->t_state_lock, flags);
815 list_for_each_entry(task, &cmd->t_task_list, t_list) {
816 spin_lock(&dev->execute_task_lock);
817 if (!task->t_state_active) {
818 list_add_tail(&task->t_state_list,
819 &dev->state_task_list);
820 task->t_state_active = true;
822 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
823 task->task_se_cmd->se_tfo->get_task_tag(
824 task->task_se_cmd), task, dev);
826 spin_unlock(&dev->execute_task_lock);
828 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
831 static void __transport_add_tasks_from_cmd(struct se_cmd *cmd)
833 struct se_device *dev = cmd->se_dev;
834 struct se_task *task, *task_prev = NULL;
836 list_for_each_entry(task, &cmd->t_task_list, t_list) {
837 if (!list_empty(&task->t_execute_list))
840 * __transport_add_task_to_execute_queue() handles the
841 * SAM Task Attribute emulation if enabled
843 __transport_add_task_to_execute_queue(task, task_prev, dev);
848 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
851 struct se_device *dev = cmd->se_dev;
853 spin_lock_irqsave(&dev->execute_task_lock, flags);
854 __transport_add_tasks_from_cmd(cmd);
855 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
858 void __transport_remove_task_from_execute_queue(struct se_task *task,
859 struct se_device *dev)
861 list_del_init(&task->t_execute_list);
862 atomic_dec(&dev->execute_tasks);
865 static void transport_remove_task_from_execute_queue(
866 struct se_task *task,
867 struct se_device *dev)
871 if (WARN_ON(list_empty(&task->t_execute_list)))
874 spin_lock_irqsave(&dev->execute_task_lock, flags);
875 __transport_remove_task_from_execute_queue(task, dev);
876 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
880 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
883 static void target_qf_do_work(struct work_struct *work)
885 struct se_device *dev = container_of(work, struct se_device,
887 LIST_HEAD(qf_cmd_list);
888 struct se_cmd *cmd, *cmd_tmp;
890 spin_lock_irq(&dev->qf_cmd_lock);
891 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
892 spin_unlock_irq(&dev->qf_cmd_lock);
894 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
895 list_del(&cmd->se_qf_node);
896 atomic_dec(&dev->dev_qf_count);
897 smp_mb__after_atomic_dec();
899 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
900 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
901 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
902 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
905 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
909 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
911 switch (cmd->data_direction) {
914 case DMA_FROM_DEVICE:
918 case DMA_BIDIRECTIONAL:
927 void transport_dump_dev_state(
928 struct se_device *dev,
932 *bl += sprintf(b + *bl, "Status: ");
933 switch (dev->dev_status) {
934 case TRANSPORT_DEVICE_ACTIVATED:
935 *bl += sprintf(b + *bl, "ACTIVATED");
937 case TRANSPORT_DEVICE_DEACTIVATED:
938 *bl += sprintf(b + *bl, "DEACTIVATED");
940 case TRANSPORT_DEVICE_SHUTDOWN:
941 *bl += sprintf(b + *bl, "SHUTDOWN");
943 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
944 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
945 *bl += sprintf(b + *bl, "OFFLINE");
948 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
952 *bl += sprintf(b + *bl, " Execute/Max Queue Depth: %d/%d",
953 atomic_read(&dev->execute_tasks), dev->queue_depth);
954 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
955 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
956 *bl += sprintf(b + *bl, " ");
959 void transport_dump_vpd_proto_id(
961 unsigned char *p_buf,
964 unsigned char buf[VPD_TMP_BUF_SIZE];
967 memset(buf, 0, VPD_TMP_BUF_SIZE);
968 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
970 switch (vpd->protocol_identifier) {
972 sprintf(buf+len, "Fibre Channel\n");
975 sprintf(buf+len, "Parallel SCSI\n");
978 sprintf(buf+len, "SSA\n");
981 sprintf(buf+len, "IEEE 1394\n");
984 sprintf(buf+len, "SCSI Remote Direct Memory Access"
988 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
991 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
994 sprintf(buf+len, "Automation/Drive Interface Transport"
998 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1001 sprintf(buf+len, "Unknown 0x%02x\n",
1002 vpd->protocol_identifier);
1007 strncpy(p_buf, buf, p_buf_len);
1009 pr_debug("%s", buf);
1013 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1016 * Check if the Protocol Identifier Valid (PIV) bit is set..
1018 * from spc3r23.pdf section 7.5.1
1020 if (page_83[1] & 0x80) {
1021 vpd->protocol_identifier = (page_83[0] & 0xf0);
1022 vpd->protocol_identifier_set = 1;
1023 transport_dump_vpd_proto_id(vpd, NULL, 0);
1026 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1028 int transport_dump_vpd_assoc(
1029 struct t10_vpd *vpd,
1030 unsigned char *p_buf,
1033 unsigned char buf[VPD_TMP_BUF_SIZE];
1037 memset(buf, 0, VPD_TMP_BUF_SIZE);
1038 len = sprintf(buf, "T10 VPD Identifier Association: ");
1040 switch (vpd->association) {
1042 sprintf(buf+len, "addressed logical unit\n");
1045 sprintf(buf+len, "target port\n");
1048 sprintf(buf+len, "SCSI target device\n");
1051 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1057 strncpy(p_buf, buf, p_buf_len);
1059 pr_debug("%s", buf);
1064 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1067 * The VPD identification association..
1069 * from spc3r23.pdf Section 7.6.3.1 Table 297
1071 vpd->association = (page_83[1] & 0x30);
1072 return transport_dump_vpd_assoc(vpd, NULL, 0);
1074 EXPORT_SYMBOL(transport_set_vpd_assoc);
1076 int transport_dump_vpd_ident_type(
1077 struct t10_vpd *vpd,
1078 unsigned char *p_buf,
1081 unsigned char buf[VPD_TMP_BUF_SIZE];
1085 memset(buf, 0, VPD_TMP_BUF_SIZE);
1086 len = sprintf(buf, "T10 VPD Identifier Type: ");
1088 switch (vpd->device_identifier_type) {
1090 sprintf(buf+len, "Vendor specific\n");
1093 sprintf(buf+len, "T10 Vendor ID based\n");
1096 sprintf(buf+len, "EUI-64 based\n");
1099 sprintf(buf+len, "NAA\n");
1102 sprintf(buf+len, "Relative target port identifier\n");
1105 sprintf(buf+len, "SCSI name string\n");
1108 sprintf(buf+len, "Unsupported: 0x%02x\n",
1109 vpd->device_identifier_type);
1115 if (p_buf_len < strlen(buf)+1)
1117 strncpy(p_buf, buf, p_buf_len);
1119 pr_debug("%s", buf);
1125 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1128 * The VPD identifier type..
1130 * from spc3r23.pdf Section 7.6.3.1 Table 298
1132 vpd->device_identifier_type = (page_83[1] & 0x0f);
1133 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1135 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1137 int transport_dump_vpd_ident(
1138 struct t10_vpd *vpd,
1139 unsigned char *p_buf,
1142 unsigned char buf[VPD_TMP_BUF_SIZE];
1145 memset(buf, 0, VPD_TMP_BUF_SIZE);
1147 switch (vpd->device_identifier_code_set) {
1148 case 0x01: /* Binary */
1149 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1150 &vpd->device_identifier[0]);
1152 case 0x02: /* ASCII */
1153 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1154 &vpd->device_identifier[0]);
1156 case 0x03: /* UTF-8 */
1157 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1158 &vpd->device_identifier[0]);
1161 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1162 " 0x%02x", vpd->device_identifier_code_set);
1168 strncpy(p_buf, buf, p_buf_len);
1170 pr_debug("%s", buf);
1176 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1178 static const char hex_str[] = "0123456789abcdef";
1179 int j = 0, i = 4; /* offset to start of the identifer */
1182 * The VPD Code Set (encoding)
1184 * from spc3r23.pdf Section 7.6.3.1 Table 296
1186 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1187 switch (vpd->device_identifier_code_set) {
1188 case 0x01: /* Binary */
1189 vpd->device_identifier[j++] =
1190 hex_str[vpd->device_identifier_type];
1191 while (i < (4 + page_83[3])) {
1192 vpd->device_identifier[j++] =
1193 hex_str[(page_83[i] & 0xf0) >> 4];
1194 vpd->device_identifier[j++] =
1195 hex_str[page_83[i] & 0x0f];
1199 case 0x02: /* ASCII */
1200 case 0x03: /* UTF-8 */
1201 while (i < (4 + page_83[3]))
1202 vpd->device_identifier[j++] = page_83[i++];
1208 return transport_dump_vpd_ident(vpd, NULL, 0);
1210 EXPORT_SYMBOL(transport_set_vpd_ident);
1212 static void core_setup_task_attr_emulation(struct se_device *dev)
1215 * If this device is from Target_Core_Mod/pSCSI, disable the
1216 * SAM Task Attribute emulation.
1218 * This is currently not available in upsream Linux/SCSI Target
1219 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1221 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1222 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1226 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1227 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1228 " device\n", dev->transport->name,
1229 dev->transport->get_device_rev(dev));
1232 static void scsi_dump_inquiry(struct se_device *dev)
1234 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1238 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1240 for (i = 0; i < 8; i++)
1241 if (wwn->vendor[i] >= 0x20)
1242 buf[i] = wwn->vendor[i];
1246 pr_debug(" Vendor: %s\n", buf);
1248 for (i = 0; i < 16; i++)
1249 if (wwn->model[i] >= 0x20)
1250 buf[i] = wwn->model[i];
1254 pr_debug(" Model: %s\n", buf);
1256 for (i = 0; i < 4; i++)
1257 if (wwn->revision[i] >= 0x20)
1258 buf[i] = wwn->revision[i];
1262 pr_debug(" Revision: %s\n", buf);
1264 device_type = dev->transport->get_device_type(dev);
1265 pr_debug(" Type: %s ", scsi_device_type(device_type));
1266 pr_debug(" ANSI SCSI revision: %02x\n",
1267 dev->transport->get_device_rev(dev));
1270 struct se_device *transport_add_device_to_core_hba(
1272 struct se_subsystem_api *transport,
1273 struct se_subsystem_dev *se_dev,
1275 void *transport_dev,
1276 struct se_dev_limits *dev_limits,
1277 const char *inquiry_prod,
1278 const char *inquiry_rev)
1281 struct se_device *dev;
1283 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1285 pr_err("Unable to allocate memory for se_dev_t\n");
1289 transport_init_queue_obj(&dev->dev_queue_obj);
1290 dev->dev_flags = device_flags;
1291 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1292 dev->dev_ptr = transport_dev;
1294 dev->se_sub_dev = se_dev;
1295 dev->transport = transport;
1296 INIT_LIST_HEAD(&dev->dev_list);
1297 INIT_LIST_HEAD(&dev->dev_sep_list);
1298 INIT_LIST_HEAD(&dev->dev_tmr_list);
1299 INIT_LIST_HEAD(&dev->execute_task_list);
1300 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1301 INIT_LIST_HEAD(&dev->state_task_list);
1302 INIT_LIST_HEAD(&dev->qf_cmd_list);
1303 spin_lock_init(&dev->execute_task_lock);
1304 spin_lock_init(&dev->delayed_cmd_lock);
1305 spin_lock_init(&dev->dev_reservation_lock);
1306 spin_lock_init(&dev->dev_status_lock);
1307 spin_lock_init(&dev->se_port_lock);
1308 spin_lock_init(&dev->se_tmr_lock);
1309 spin_lock_init(&dev->qf_cmd_lock);
1310 atomic_set(&dev->dev_ordered_id, 0);
1312 se_dev_set_default_attribs(dev, dev_limits);
1314 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1315 dev->creation_time = get_jiffies_64();
1316 spin_lock_init(&dev->stats_lock);
1318 spin_lock(&hba->device_lock);
1319 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1321 spin_unlock(&hba->device_lock);
1323 * Setup the SAM Task Attribute emulation for struct se_device
1325 core_setup_task_attr_emulation(dev);
1327 * Force PR and ALUA passthrough emulation with internal object use.
1329 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1331 * Setup the Reservations infrastructure for struct se_device
1333 core_setup_reservations(dev, force_pt);
1335 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1337 if (core_setup_alua(dev, force_pt) < 0)
1341 * Startup the struct se_device processing thread
1343 dev->process_thread = kthread_run(transport_processing_thread, dev,
1344 "LIO_%s", dev->transport->name);
1345 if (IS_ERR(dev->process_thread)) {
1346 pr_err("Unable to create kthread: LIO_%s\n",
1347 dev->transport->name);
1351 * Setup work_queue for QUEUE_FULL
1353 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1355 * Preload the initial INQUIRY const values if we are doing
1356 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1357 * passthrough because this is being provided by the backend LLD.
1358 * This is required so that transport_get_inquiry() copies these
1359 * originals once back into DEV_T10_WWN(dev) for the virtual device
1362 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1363 if (!inquiry_prod || !inquiry_rev) {
1364 pr_err("All non TCM/pSCSI plugins require"
1365 " INQUIRY consts\n");
1369 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1370 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1371 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1373 scsi_dump_inquiry(dev);
1377 kthread_stop(dev->process_thread);
1379 spin_lock(&hba->device_lock);
1380 list_del(&dev->dev_list);
1382 spin_unlock(&hba->device_lock);
1384 se_release_vpd_for_dev(dev);
1390 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1392 /* transport_generic_prepare_cdb():
1394 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1395 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1396 * The point of this is since we are mapping iSCSI LUNs to
1397 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1398 * devices and HBAs for a loop.
1400 static inline void transport_generic_prepare_cdb(
1404 case READ_10: /* SBC - RDProtect */
1405 case READ_12: /* SBC - RDProtect */
1406 case READ_16: /* SBC - RDProtect */
1407 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1408 case VERIFY: /* SBC - VRProtect */
1409 case VERIFY_16: /* SBC - VRProtect */
1410 case WRITE_VERIFY: /* SBC - VRProtect */
1411 case WRITE_VERIFY_12: /* SBC - VRProtect */
1414 cdb[1] &= 0x1f; /* clear logical unit number */
1419 static struct se_task *
1420 transport_generic_get_task(struct se_cmd *cmd,
1421 enum dma_data_direction data_direction)
1423 struct se_task *task;
1424 struct se_device *dev = cmd->se_dev;
1426 task = dev->transport->alloc_task(cmd->t_task_cdb);
1428 pr_err("Unable to allocate struct se_task\n");
1432 INIT_LIST_HEAD(&task->t_list);
1433 INIT_LIST_HEAD(&task->t_execute_list);
1434 INIT_LIST_HEAD(&task->t_state_list);
1435 init_completion(&task->task_stop_comp);
1436 task->task_se_cmd = cmd;
1437 task->task_data_direction = data_direction;
1442 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1445 * Used by fabric modules containing a local struct se_cmd within their
1446 * fabric dependent per I/O descriptor.
1448 void transport_init_se_cmd(
1450 struct target_core_fabric_ops *tfo,
1451 struct se_session *se_sess,
1455 unsigned char *sense_buffer)
1457 INIT_LIST_HEAD(&cmd->se_lun_node);
1458 INIT_LIST_HEAD(&cmd->se_delayed_node);
1459 INIT_LIST_HEAD(&cmd->se_qf_node);
1460 INIT_LIST_HEAD(&cmd->se_queue_node);
1461 INIT_LIST_HEAD(&cmd->se_cmd_list);
1462 INIT_LIST_HEAD(&cmd->t_task_list);
1463 init_completion(&cmd->transport_lun_fe_stop_comp);
1464 init_completion(&cmd->transport_lun_stop_comp);
1465 init_completion(&cmd->t_transport_stop_comp);
1466 init_completion(&cmd->cmd_wait_comp);
1467 spin_lock_init(&cmd->t_state_lock);
1468 cmd->transport_state = CMD_T_DEV_ACTIVE;
1471 cmd->se_sess = se_sess;
1472 cmd->data_length = data_length;
1473 cmd->data_direction = data_direction;
1474 cmd->sam_task_attr = task_attr;
1475 cmd->sense_buffer = sense_buffer;
1477 EXPORT_SYMBOL(transport_init_se_cmd);
1479 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1482 * Check if SAM Task Attribute emulation is enabled for this
1483 * struct se_device storage object
1485 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1488 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1489 pr_debug("SAM Task Attribute ACA"
1490 " emulation is not supported\n");
1494 * Used to determine when ORDERED commands should go from
1495 * Dormant to Active status.
1497 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1498 smp_mb__after_atomic_inc();
1499 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1500 cmd->se_ordered_id, cmd->sam_task_attr,
1501 cmd->se_dev->transport->name);
1505 /* transport_generic_allocate_tasks():
1507 * Called from fabric RX Thread.
1509 int transport_generic_allocate_tasks(
1515 transport_generic_prepare_cdb(cdb);
1517 * Ensure that the received CDB is less than the max (252 + 8) bytes
1518 * for VARIABLE_LENGTH_CMD
1520 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1521 pr_err("Received SCSI CDB with command_size: %d that"
1522 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1523 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1524 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1525 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1529 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1530 * allocate the additional extended CDB buffer now.. Otherwise
1531 * setup the pointer from __t_task_cdb to t_task_cdb.
1533 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1534 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1536 if (!cmd->t_task_cdb) {
1537 pr_err("Unable to allocate cmd->t_task_cdb"
1538 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1539 scsi_command_size(cdb),
1540 (unsigned long)sizeof(cmd->__t_task_cdb));
1541 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1542 cmd->scsi_sense_reason =
1543 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1547 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1549 * Copy the original CDB into cmd->
1551 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1553 * Setup the received CDB based on SCSI defined opcodes and
1554 * perform unit attention, persistent reservations and ALUA
1555 * checks for virtual device backends. The cmd->t_task_cdb
1556 * pointer is expected to be setup before we reach this point.
1558 ret = transport_generic_cmd_sequencer(cmd, cdb);
1562 * Check for SAM Task Attribute Emulation
1564 if (transport_check_alloc_task_attr(cmd) < 0) {
1565 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1566 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1569 spin_lock(&cmd->se_lun->lun_sep_lock);
1570 if (cmd->se_lun->lun_sep)
1571 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1572 spin_unlock(&cmd->se_lun->lun_sep_lock);
1575 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1578 * Used by fabric module frontends to queue tasks directly.
1579 * Many only be used from process context only
1581 int transport_handle_cdb_direct(
1588 pr_err("cmd->se_lun is NULL\n");
1591 if (in_interrupt()) {
1593 pr_err("transport_generic_handle_cdb cannot be called"
1594 " from interrupt context\n");
1598 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE following
1599 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1600 * in existing usage to ensure that outstanding descriptors are handled
1601 * correctly during shutdown via transport_wait_for_tasks()
1603 * Also, we don't take cmd->t_state_lock here as we only expect
1604 * this to be called for initial descriptor submission.
1606 cmd->t_state = TRANSPORT_NEW_CMD;
1607 cmd->transport_state |= CMD_T_ACTIVE;
1610 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1611 * so follow TRANSPORT_NEW_CMD processing thread context usage
1612 * and call transport_generic_request_failure() if necessary..
1614 ret = transport_generic_new_cmd(cmd);
1616 transport_generic_request_failure(cmd);
1620 EXPORT_SYMBOL(transport_handle_cdb_direct);
1623 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1625 * @se_cmd: command descriptor to submit
1626 * @se_sess: associated se_sess for endpoint
1627 * @cdb: pointer to SCSI CDB
1628 * @sense: pointer to SCSI sense buffer
1629 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1630 * @data_length: fabric expected data transfer length
1631 * @task_addr: SAM task attribute
1632 * @data_dir: DMA data direction
1633 * @flags: flags for command submission from target_sc_flags_tables
1635 * This may only be called from process context, and also currently
1636 * assumes internal allocation of fabric payload buffer by target-core.
1638 void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1639 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1640 u32 data_length, int task_attr, int data_dir, int flags)
1642 struct se_portal_group *se_tpg;
1645 se_tpg = se_sess->se_tpg;
1647 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1648 BUG_ON(in_interrupt());
1650 * Initialize se_cmd for target operation. From this point
1651 * exceptions are handled by sending exception status via
1652 * target_core_fabric_ops->queue_status() callback
1654 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1655 data_length, data_dir, task_attr, sense);
1657 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1658 * se_sess->sess_cmd_list. A second kref_get here is necessary
1659 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1660 * kref_put() to happen during fabric packet acknowledgement.
1662 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1664 * Signal bidirectional data payloads to target-core
1666 if (flags & TARGET_SCF_BIDI_OP)
1667 se_cmd->se_cmd_flags |= SCF_BIDI;
1669 * Locate se_lun pointer and attach it to struct se_cmd
1671 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1672 transport_send_check_condition_and_sense(se_cmd,
1673 se_cmd->scsi_sense_reason, 0);
1674 target_put_sess_cmd(se_sess, se_cmd);
1678 * Sanitize CDBs via transport_generic_cmd_sequencer() and
1679 * allocate the necessary tasks to complete the received CDB+data
1681 rc = transport_generic_allocate_tasks(se_cmd, cdb);
1683 transport_generic_request_failure(se_cmd);
1687 * Dispatch se_cmd descriptor to se_lun->lun_se_dev backend
1688 * for immediate execution of READs, otherwise wait for
1689 * transport_generic_handle_data() to be called for WRITEs
1690 * when fabric has filled the incoming buffer.
1692 transport_handle_cdb_direct(se_cmd);
1695 EXPORT_SYMBOL(target_submit_cmd);
1697 static void target_complete_tmr_failure(struct work_struct *work)
1699 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1701 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1702 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1703 transport_generic_free_cmd(se_cmd, 0);
1707 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1710 * @se_cmd: command descriptor to submit
1711 * @se_sess: associated se_sess for endpoint
1712 * @sense: pointer to SCSI sense buffer
1713 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1714 * @fabric_context: fabric context for TMR req
1715 * @tm_type: Type of TM request
1716 * @gfp: gfp type for caller
1717 * @tag: referenced task tag for TMR_ABORT_TASK
1718 * @flags: submit cmd flags
1720 * Callable from all contexts.
1723 int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1724 unsigned char *sense, u32 unpacked_lun,
1725 void *fabric_tmr_ptr, unsigned char tm_type,
1726 gfp_t gfp, unsigned int tag, int flags)
1728 struct se_portal_group *se_tpg;
1731 se_tpg = se_sess->se_tpg;
1734 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1735 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1737 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1738 * allocation failure.
1740 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
1744 if (tm_type == TMR_ABORT_TASK)
1745 se_cmd->se_tmr_req->ref_task_tag = tag;
1747 /* See target_submit_cmd for commentary */
1748 target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1750 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1753 * For callback during failure handling, push this work off
1754 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1756 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1757 schedule_work(&se_cmd->work);
1760 transport_generic_handle_tmr(se_cmd);
1763 EXPORT_SYMBOL(target_submit_tmr);
1766 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1767 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1768 * complete setup in TCM process context w/ TFO->new_cmd_map().
1770 int transport_generic_handle_cdb_map(
1775 pr_err("cmd->se_lun is NULL\n");
1779 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
1782 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1784 /* transport_generic_handle_data():
1788 int transport_generic_handle_data(
1792 * For the software fabric case, then we assume the nexus is being
1793 * failed/shutdown when signals are pending from the kthread context
1794 * caller, so we return a failure. For the HW target mode case running
1795 * in interrupt code, the signal_pending() check is skipped.
1797 if (!in_interrupt() && signal_pending(current))
1800 * If the received CDB has aleady been ABORTED by the generic
1801 * target engine, we now call transport_check_aborted_status()
1802 * to queue any delated TASK_ABORTED status for the received CDB to the
1803 * fabric module as we are expecting no further incoming DATA OUT
1804 * sequences at this point.
1806 if (transport_check_aborted_status(cmd, 1) != 0)
1809 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
1812 EXPORT_SYMBOL(transport_generic_handle_data);
1814 /* transport_generic_handle_tmr():
1818 int transport_generic_handle_tmr(
1821 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
1824 EXPORT_SYMBOL(transport_generic_handle_tmr);
1827 * If the task is active, request it to be stopped and sleep until it
1830 bool target_stop_task(struct se_task *task, unsigned long *flags)
1832 struct se_cmd *cmd = task->task_se_cmd;
1833 bool was_active = false;
1835 if (task->task_flags & TF_ACTIVE) {
1836 task->task_flags |= TF_REQUEST_STOP;
1837 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1839 pr_debug("Task %p waiting to complete\n", task);
1840 wait_for_completion(&task->task_stop_comp);
1841 pr_debug("Task %p stopped successfully\n", task);
1843 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1844 atomic_dec(&cmd->t_task_cdbs_left);
1845 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1852 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1854 struct se_task *task, *task_tmp;
1855 unsigned long flags;
1858 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1859 cmd->se_tfo->get_task_tag(cmd));
1862 * No tasks remain in the execution queue
1864 spin_lock_irqsave(&cmd->t_state_lock, flags);
1865 list_for_each_entry_safe(task, task_tmp,
1866 &cmd->t_task_list, t_list) {
1867 pr_debug("Processing task %p\n", task);
1869 * If the struct se_task has not been sent and is not active,
1870 * remove the struct se_task from the execution queue.
1872 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
1873 spin_unlock_irqrestore(&cmd->t_state_lock,
1875 transport_remove_task_from_execute_queue(task,
1878 pr_debug("Task %p removed from execute queue\n", task);
1879 spin_lock_irqsave(&cmd->t_state_lock, flags);
1883 if (!target_stop_task(task, &flags)) {
1884 pr_debug("Task %p - did nothing\n", task);
1888 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1894 * Handle SAM-esque emulation for generic transport request failures.
1896 void transport_generic_request_failure(struct se_cmd *cmd)
1900 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1901 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1902 cmd->t_task_cdb[0]);
1903 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
1904 cmd->se_tfo->get_cmd_state(cmd),
1905 cmd->t_state, cmd->scsi_sense_reason);
1906 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1907 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1908 " CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1909 cmd->t_task_list_num,
1910 atomic_read(&cmd->t_task_cdbs_left),
1911 atomic_read(&cmd->t_task_cdbs_sent),
1912 atomic_read(&cmd->t_task_cdbs_ex_left),
1913 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1914 (cmd->transport_state & CMD_T_STOP) != 0,
1915 (cmd->transport_state & CMD_T_SENT) != 0);
1918 * For SAM Task Attribute emulation for failed struct se_cmd
1920 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1921 transport_complete_task_attr(cmd);
1923 switch (cmd->scsi_sense_reason) {
1924 case TCM_NON_EXISTENT_LUN:
1925 case TCM_UNSUPPORTED_SCSI_OPCODE:
1926 case TCM_INVALID_CDB_FIELD:
1927 case TCM_INVALID_PARAMETER_LIST:
1928 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1929 case TCM_UNKNOWN_MODE_PAGE:
1930 case TCM_WRITE_PROTECTED:
1931 case TCM_CHECK_CONDITION_ABORT_CMD:
1932 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1933 case TCM_CHECK_CONDITION_NOT_READY:
1935 case TCM_RESERVATION_CONFLICT:
1937 * No SENSE Data payload for this case, set SCSI Status
1938 * and queue the response to $FABRIC_MOD.
1940 * Uses linux/include/scsi/scsi.h SAM status codes defs
1942 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1944 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1945 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1948 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1951 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1952 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1953 cmd->orig_fe_lun, 0x2C,
1954 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1956 ret = cmd->se_tfo->queue_status(cmd);
1957 if (ret == -EAGAIN || ret == -ENOMEM)
1961 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1962 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
1963 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1967 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1968 * make the call to transport_send_check_condition_and_sense()
1969 * directly. Otherwise expect the fabric to make the call to
1970 * transport_send_check_condition_and_sense() after handling
1971 * possible unsoliticied write data payloads.
1973 ret = transport_send_check_condition_and_sense(cmd,
1974 cmd->scsi_sense_reason, 0);
1975 if (ret == -EAGAIN || ret == -ENOMEM)
1979 transport_lun_remove_cmd(cmd);
1980 if (!transport_cmd_check_stop_to_fabric(cmd))
1985 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1986 transport_handle_queue_full(cmd, cmd->se_dev);
1988 EXPORT_SYMBOL(transport_generic_request_failure);
1990 static inline u32 transport_lba_21(unsigned char *cdb)
1992 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1995 static inline u32 transport_lba_32(unsigned char *cdb)
1997 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2000 static inline unsigned long long transport_lba_64(unsigned char *cdb)
2002 unsigned int __v1, __v2;
2004 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2005 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2007 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2011 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2013 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2015 unsigned int __v1, __v2;
2017 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2018 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2020 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2023 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2025 unsigned long flags;
2027 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2028 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2029 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2033 * Called from Fabric Module context from transport_execute_tasks()
2035 * The return of this function determins if the tasks from struct se_cmd
2036 * get added to the execution queue in transport_execute_tasks(),
2037 * or are added to the delayed or ordered lists here.
2039 static inline int transport_execute_task_attr(struct se_cmd *cmd)
2041 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2044 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2045 * to allow the passed struct se_cmd list of tasks to the front of the list.
2047 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
2048 pr_debug("Added HEAD_OF_QUEUE for CDB:"
2049 " 0x%02x, se_ordered_id: %u\n",
2051 cmd->se_ordered_id);
2053 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
2054 atomic_inc(&cmd->se_dev->dev_ordered_sync);
2055 smp_mb__after_atomic_inc();
2057 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
2058 " list, se_ordered_id: %u\n",
2060 cmd->se_ordered_id);
2062 * Add ORDERED command to tail of execution queue if
2063 * no other older commands exist that need to be
2066 if (!atomic_read(&cmd->se_dev->simple_cmds))
2070 * For SIMPLE and UNTAGGED Task Attribute commands
2072 atomic_inc(&cmd->se_dev->simple_cmds);
2073 smp_mb__after_atomic_inc();
2076 * Otherwise if one or more outstanding ORDERED task attribute exist,
2077 * add the dormant task(s) built for the passed struct se_cmd to the
2078 * execution queue and become in Active state for this struct se_device.
2080 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2082 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2083 * will be drained upon completion of HEAD_OF_QUEUE task.
2085 spin_lock(&cmd->se_dev->delayed_cmd_lock);
2086 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2087 list_add_tail(&cmd->se_delayed_node,
2088 &cmd->se_dev->delayed_cmd_list);
2089 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2091 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2092 " delayed CMD list, se_ordered_id: %u\n",
2093 cmd->t_task_cdb[0], cmd->sam_task_attr,
2094 cmd->se_ordered_id);
2096 * Return zero to let transport_execute_tasks() know
2097 * not to add the delayed tasks to the execution list.
2102 * Otherwise, no ORDERED task attributes exist..
2108 * Called from fabric module context in transport_generic_new_cmd() and
2109 * transport_generic_process_write()
2111 static int transport_execute_tasks(struct se_cmd *cmd)
2114 struct se_device *se_dev = cmd->se_dev;
2116 * Call transport_cmd_check_stop() to see if a fabric exception
2117 * has occurred that prevents execution.
2119 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2121 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2122 * attribute for the tasks of the received struct se_cmd CDB
2124 add_tasks = transport_execute_task_attr(cmd);
2128 * __transport_execute_tasks() -> __transport_add_tasks_from_cmd()
2129 * adds associated se_tasks while holding dev->execute_task_lock
2130 * before I/O dispath to avoid a double spinlock access.
2132 __transport_execute_tasks(se_dev, cmd);
2137 __transport_execute_tasks(se_dev, NULL);
2142 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2143 * from struct se_device->execute_task_list and
2145 * Called from transport_processing_thread()
2147 static int __transport_execute_tasks(struct se_device *dev, struct se_cmd *new_cmd)
2150 struct se_cmd *cmd = NULL;
2151 struct se_task *task = NULL;
2152 unsigned long flags;
2155 spin_lock_irq(&dev->execute_task_lock);
2156 if (new_cmd != NULL)
2157 __transport_add_tasks_from_cmd(new_cmd);
2159 if (list_empty(&dev->execute_task_list)) {
2160 spin_unlock_irq(&dev->execute_task_lock);
2163 task = list_first_entry(&dev->execute_task_list,
2164 struct se_task, t_execute_list);
2165 __transport_remove_task_from_execute_queue(task, dev);
2166 spin_unlock_irq(&dev->execute_task_lock);
2168 cmd = task->task_se_cmd;
2169 spin_lock_irqsave(&cmd->t_state_lock, flags);
2170 task->task_flags |= (TF_ACTIVE | TF_SENT);
2171 atomic_inc(&cmd->t_task_cdbs_sent);
2173 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2174 cmd->t_task_list_num)
2175 cmd->transport_state |= CMD_T_SENT;
2177 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2179 if (cmd->execute_task)
2180 error = cmd->execute_task(task);
2182 error = dev->transport->do_task(task);
2184 spin_lock_irqsave(&cmd->t_state_lock, flags);
2185 task->task_flags &= ~TF_ACTIVE;
2186 cmd->transport_state &= ~CMD_T_SENT;
2187 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2189 transport_stop_tasks_for_cmd(cmd);
2190 transport_generic_request_failure(cmd);
2199 static inline u32 transport_get_sectors_6(
2204 struct se_device *dev = cmd->se_dev;
2207 * Assume TYPE_DISK for non struct se_device objects.
2208 * Use 8-bit sector value.
2214 * Use 24-bit allocation length for TYPE_TAPE.
2216 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2217 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2220 * Everything else assume TYPE_DISK Sector CDB location.
2221 * Use 8-bit sector value. SBC-3 says:
2223 * A TRANSFER LENGTH field set to zero specifies that 256
2224 * logical blocks shall be written. Any other value
2225 * specifies the number of logical blocks that shall be
2229 return cdb[4] ? : 256;
2232 static inline u32 transport_get_sectors_10(
2237 struct se_device *dev = cmd->se_dev;
2240 * Assume TYPE_DISK for non struct se_device objects.
2241 * Use 16-bit sector value.
2247 * XXX_10 is not defined in SSC, throw an exception
2249 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2255 * Everything else assume TYPE_DISK Sector CDB location.
2256 * Use 16-bit sector value.
2259 return (u32)(cdb[7] << 8) + cdb[8];
2262 static inline u32 transport_get_sectors_12(
2267 struct se_device *dev = cmd->se_dev;
2270 * Assume TYPE_DISK for non struct se_device objects.
2271 * Use 32-bit sector value.
2277 * XXX_12 is not defined in SSC, throw an exception
2279 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2285 * Everything else assume TYPE_DISK Sector CDB location.
2286 * Use 32-bit sector value.
2289 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2292 static inline u32 transport_get_sectors_16(
2297 struct se_device *dev = cmd->se_dev;
2300 * Assume TYPE_DISK for non struct se_device objects.
2301 * Use 32-bit sector value.
2307 * Use 24-bit allocation length for TYPE_TAPE.
2309 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2310 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2313 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2314 (cdb[12] << 8) + cdb[13];
2318 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2320 static inline u32 transport_get_sectors_32(
2326 * Assume TYPE_DISK for non struct se_device objects.
2327 * Use 32-bit sector value.
2329 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2330 (cdb[30] << 8) + cdb[31];
2334 static inline u32 transport_get_size(
2339 struct se_device *dev = cmd->se_dev;
2341 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2342 if (cdb[1] & 1) { /* sectors */
2343 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2348 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2349 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2350 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2351 dev->transport->name);
2353 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2356 static void transport_xor_callback(struct se_cmd *cmd)
2358 unsigned char *buf, *addr;
2359 struct scatterlist *sg;
2360 unsigned int offset;
2364 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2366 * 1) read the specified logical block(s);
2367 * 2) transfer logical blocks from the data-out buffer;
2368 * 3) XOR the logical blocks transferred from the data-out buffer with
2369 * the logical blocks read, storing the resulting XOR data in a buffer;
2370 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2371 * blocks transferred from the data-out buffer; and
2372 * 5) transfer the resulting XOR data to the data-in buffer.
2374 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2376 pr_err("Unable to allocate xor_callback buf\n");
2380 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2381 * into the locally allocated *buf
2383 sg_copy_to_buffer(cmd->t_data_sg,
2389 * Now perform the XOR against the BIDI read memory located at
2390 * cmd->t_mem_bidi_list
2394 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2395 addr = kmap_atomic(sg_page(sg), KM_USER0);
2399 for (i = 0; i < sg->length; i++)
2400 *(addr + sg->offset + i) ^= *(buf + offset + i);
2402 offset += sg->length;
2403 kunmap_atomic(addr, KM_USER0);
2411 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2413 static int transport_get_sense_data(struct se_cmd *cmd)
2415 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2416 struct se_device *dev = cmd->se_dev;
2417 struct se_task *task = NULL, *task_tmp;
2418 unsigned long flags;
2421 WARN_ON(!cmd->se_lun);
2426 spin_lock_irqsave(&cmd->t_state_lock, flags);
2427 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2428 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2432 list_for_each_entry_safe(task, task_tmp,
2433 &cmd->t_task_list, t_list) {
2434 if (!(task->task_flags & TF_HAS_SENSE))
2437 if (!dev->transport->get_sense_buffer) {
2438 pr_err("dev->transport->get_sense_buffer"
2443 sense_buffer = dev->transport->get_sense_buffer(task);
2444 if (!sense_buffer) {
2445 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
2446 " sense buffer for task with sense\n",
2447 cmd->se_tfo->get_task_tag(cmd), task);
2450 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2452 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2453 TRANSPORT_SENSE_BUFFER);
2455 memcpy(&buffer[offset], sense_buffer,
2456 TRANSPORT_SENSE_BUFFER);
2457 cmd->scsi_status = task->task_scsi_status;
2458 /* Automatically padded */
2459 cmd->scsi_sense_length =
2460 (TRANSPORT_SENSE_BUFFER + offset);
2462 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2464 dev->se_hba->hba_id, dev->transport->name,
2468 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2473 static inline long long transport_dev_end_lba(struct se_device *dev)
2475 return dev->transport->get_blocks(dev) + 1;
2478 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2480 struct se_device *dev = cmd->se_dev;
2483 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2486 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2488 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2489 pr_err("LBA: %llu Sectors: %u exceeds"
2490 " transport_dev_end_lba(): %llu\n",
2491 cmd->t_task_lba, sectors,
2492 transport_dev_end_lba(dev));
2499 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2502 * Determine if the received WRITE_SAME is used to for direct
2503 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2504 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2505 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2507 int passthrough = (dev->transport->transport_type ==
2508 TRANSPORT_PLUGIN_PHBA_PDEV);
2511 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2512 pr_err("WRITE_SAME PBDATA and LBDATA"
2513 " bits not supported for Block Discard"
2518 * Currently for the emulated case we only accept
2519 * tpws with the UNMAP=1 bit set.
2521 if (!(flags[0] & 0x08)) {
2522 pr_err("WRITE_SAME w/o UNMAP bit not"
2523 " supported for Block Discard Emulation\n");
2531 /* transport_generic_cmd_sequencer():
2533 * Generic Command Sequencer that should work for most DAS transport
2536 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2539 * FIXME: Need to support other SCSI OPCODES where as well.
2541 static int transport_generic_cmd_sequencer(
2545 struct se_device *dev = cmd->se_dev;
2546 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2547 int ret = 0, sector_ret = 0, passthrough;
2548 u32 sectors = 0, size = 0, pr_reg_type = 0;
2552 * Check for an existing UNIT ATTENTION condition
2554 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2555 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2556 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2560 * Check status of Asymmetric Logical Unit Assignment port
2562 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2565 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2566 * The ALUA additional sense code qualifier (ASCQ) is determined
2567 * by the ALUA primary or secondary access state..
2571 pr_debug("[%s]: ALUA TG Port not available,"
2572 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2573 cmd->se_tfo->get_fabric_name(), alua_ascq);
2575 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2576 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2577 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2580 goto out_invalid_cdb_field;
2583 * Check status for SPC-3 Persistent Reservations
2585 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2586 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2587 cmd, cdb, pr_reg_type) != 0) {
2588 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2589 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2590 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2594 * This means the CDB is allowed for the SCSI Initiator port
2595 * when said port is *NOT* holding the legacy SPC-2 or
2596 * SPC-3 Persistent Reservation.
2601 * If we operate in passthrough mode we skip most CDB emulation and
2602 * instead hand the commands down to the physical SCSI device.
2605 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2609 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2611 goto out_unsupported_cdb;
2612 size = transport_get_size(sectors, cdb, cmd);
2613 cmd->t_task_lba = transport_lba_21(cdb);
2614 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2617 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2619 goto out_unsupported_cdb;
2620 size = transport_get_size(sectors, cdb, cmd);
2621 cmd->t_task_lba = transport_lba_32(cdb);
2622 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2625 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2627 goto out_unsupported_cdb;
2628 size = transport_get_size(sectors, cdb, cmd);
2629 cmd->t_task_lba = transport_lba_32(cdb);
2630 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2633 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2635 goto out_unsupported_cdb;
2636 size = transport_get_size(sectors, cdb, cmd);
2637 cmd->t_task_lba = transport_lba_64(cdb);
2638 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2641 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2643 goto out_unsupported_cdb;
2644 size = transport_get_size(sectors, cdb, cmd);
2645 cmd->t_task_lba = transport_lba_21(cdb);
2646 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2649 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2651 goto out_unsupported_cdb;
2652 size = transport_get_size(sectors, cdb, cmd);
2653 cmd->t_task_lba = transport_lba_32(cdb);
2655 cmd->se_cmd_flags |= SCF_FUA;
2656 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2659 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2661 goto out_unsupported_cdb;
2662 size = transport_get_size(sectors, cdb, cmd);
2663 cmd->t_task_lba = transport_lba_32(cdb);
2665 cmd->se_cmd_flags |= SCF_FUA;
2666 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2669 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2671 goto out_unsupported_cdb;
2672 size = transport_get_size(sectors, cdb, cmd);
2673 cmd->t_task_lba = transport_lba_64(cdb);
2675 cmd->se_cmd_flags |= SCF_FUA;
2676 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2678 case XDWRITEREAD_10:
2679 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2680 !(cmd->se_cmd_flags & SCF_BIDI))
2681 goto out_invalid_cdb_field;
2682 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2684 goto out_unsupported_cdb;
2685 size = transport_get_size(sectors, cdb, cmd);
2686 cmd->t_task_lba = transport_lba_32(cdb);
2687 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2690 * Do now allow BIDI commands for passthrough mode.
2693 goto out_unsupported_cdb;
2696 * Setup BIDI XOR callback to be run after I/O completion.
2698 cmd->transport_complete_callback = &transport_xor_callback;
2700 cmd->se_cmd_flags |= SCF_FUA;
2702 case VARIABLE_LENGTH_CMD:
2703 service_action = get_unaligned_be16(&cdb[8]);
2704 switch (service_action) {
2705 case XDWRITEREAD_32:
2706 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2708 goto out_unsupported_cdb;
2709 size = transport_get_size(sectors, cdb, cmd);
2711 * Use WRITE_32 and READ_32 opcodes for the emulated
2712 * XDWRITE_READ_32 logic.
2714 cmd->t_task_lba = transport_lba_64_ext(cdb);
2715 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2718 * Do now allow BIDI commands for passthrough mode.
2721 goto out_unsupported_cdb;
2724 * Setup BIDI XOR callback to be run during after I/O
2727 cmd->transport_complete_callback = &transport_xor_callback;
2729 cmd->se_cmd_flags |= SCF_FUA;
2732 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2734 goto out_unsupported_cdb;
2737 size = transport_get_size(1, cdb, cmd);
2739 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2741 goto out_invalid_cdb_field;
2744 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2745 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2747 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2748 goto out_unsupported_cdb;
2750 cmd->execute_task = target_emulate_write_same;
2753 pr_err("VARIABLE_LENGTH_CMD service action"
2754 " 0x%04x not supported\n", service_action);
2755 goto out_unsupported_cdb;
2758 case MAINTENANCE_IN:
2759 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2760 /* MAINTENANCE_IN from SCC-2 */
2762 * Check for emulated MI_REPORT_TARGET_PGS.
2764 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2765 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2767 target_emulate_report_target_port_groups;
2769 size = (cdb[6] << 24) | (cdb[7] << 16) |
2770 (cdb[8] << 8) | cdb[9];
2772 /* GPCMD_SEND_KEY from multi media commands */
2773 size = (cdb[8] << 8) + cdb[9];
2775 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2779 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2781 case MODE_SELECT_10:
2782 size = (cdb[7] << 8) + cdb[8];
2783 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2787 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2789 cmd->execute_task = target_emulate_modesense;
2792 size = (cdb[7] << 8) + cdb[8];
2793 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2795 cmd->execute_task = target_emulate_modesense;
2797 case GPCMD_READ_BUFFER_CAPACITY:
2798 case GPCMD_SEND_OPC:
2801 size = (cdb[7] << 8) + cdb[8];
2802 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2804 case READ_BLOCK_LIMITS:
2805 size = READ_BLOCK_LEN;
2806 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2808 case GPCMD_GET_CONFIGURATION:
2809 case GPCMD_READ_FORMAT_CAPACITIES:
2810 case GPCMD_READ_DISC_INFO:
2811 case GPCMD_READ_TRACK_RZONE_INFO:
2812 size = (cdb[7] << 8) + cdb[8];
2813 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2815 case PERSISTENT_RESERVE_IN:
2816 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2817 cmd->execute_task = target_scsi3_emulate_pr_in;
2818 size = (cdb[7] << 8) + cdb[8];
2819 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2821 case PERSISTENT_RESERVE_OUT:
2822 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2823 cmd->execute_task = target_scsi3_emulate_pr_out;
2824 size = (cdb[7] << 8) + cdb[8];
2825 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2827 case GPCMD_MECHANISM_STATUS:
2828 case GPCMD_READ_DVD_STRUCTURE:
2829 size = (cdb[8] << 8) + cdb[9];
2830 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2833 size = READ_POSITION_LEN;
2834 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2836 case MAINTENANCE_OUT:
2837 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2838 /* MAINTENANCE_OUT from SCC-2
2840 * Check for emulated MO_SET_TARGET_PGS.
2842 if (cdb[1] == MO_SET_TARGET_PGS &&
2843 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2845 target_emulate_set_target_port_groups;
2848 size = (cdb[6] << 24) | (cdb[7] << 16) |
2849 (cdb[8] << 8) | cdb[9];
2851 /* GPCMD_REPORT_KEY from multi media commands */
2852 size = (cdb[8] << 8) + cdb[9];
2854 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2857 size = (cdb[3] << 8) + cdb[4];
2859 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2860 * See spc4r17 section 5.3
2862 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2863 cmd->sam_task_attr = MSG_HEAD_TAG;
2864 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2866 cmd->execute_task = target_emulate_inquiry;
2869 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2870 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2873 size = READ_CAP_LEN;
2874 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2876 cmd->execute_task = target_emulate_readcapacity;
2878 case READ_MEDIA_SERIAL_NUMBER:
2879 case SECURITY_PROTOCOL_IN:
2880 case SECURITY_PROTOCOL_OUT:
2881 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2882 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2884 case SERVICE_ACTION_IN:
2885 switch (cmd->t_task_cdb[1] & 0x1f) {
2886 case SAI_READ_CAPACITY_16:
2889 target_emulate_readcapacity_16;
2895 pr_err("Unsupported SA: 0x%02x\n",
2896 cmd->t_task_cdb[1] & 0x1f);
2897 goto out_unsupported_cdb;
2900 case ACCESS_CONTROL_IN:
2901 case ACCESS_CONTROL_OUT:
2903 case READ_ATTRIBUTE:
2904 case RECEIVE_COPY_RESULTS:
2905 case WRITE_ATTRIBUTE:
2906 size = (cdb[10] << 24) | (cdb[11] << 16) |
2907 (cdb[12] << 8) | cdb[13];
2908 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2910 case RECEIVE_DIAGNOSTIC:
2911 case SEND_DIAGNOSTIC:
2912 size = (cdb[3] << 8) | cdb[4];
2913 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2915 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2918 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2919 size = (2336 * sectors);
2920 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2925 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2929 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2931 cmd->execute_task = target_emulate_request_sense;
2933 case READ_ELEMENT_STATUS:
2934 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
2935 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2938 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2939 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2944 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2945 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2947 if (cdb[0] == RESERVE_10)
2948 size = (cdb[7] << 8) | cdb[8];
2950 size = cmd->data_length;
2953 * Setup the legacy emulated handler for SPC-2 and
2954 * >= SPC-3 compatible reservation handling (CRH=1)
2955 * Otherwise, we assume the underlying SCSI logic is
2956 * is running in SPC_PASSTHROUGH, and wants reservations
2957 * emulation disabled.
2959 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2960 cmd->execute_task = target_scsi2_reservation_reserve;
2961 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2966 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2967 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2969 if (cdb[0] == RELEASE_10)
2970 size = (cdb[7] << 8) | cdb[8];
2972 size = cmd->data_length;
2974 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2975 cmd->execute_task = target_scsi2_reservation_release;
2976 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2978 case SYNCHRONIZE_CACHE:
2979 case SYNCHRONIZE_CACHE_16:
2981 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2983 if (cdb[0] == SYNCHRONIZE_CACHE) {
2984 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2985 cmd->t_task_lba = transport_lba_32(cdb);
2987 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2988 cmd->t_task_lba = transport_lba_64(cdb);
2991 goto out_unsupported_cdb;
2993 size = transport_get_size(sectors, cdb, cmd);
2994 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3000 * Check to ensure that LBA + Range does not exceed past end of
3001 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
3003 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3004 if (transport_cmd_get_valid_sectors(cmd) < 0)
3005 goto out_invalid_cdb_field;
3007 cmd->execute_task = target_emulate_synchronize_cache;
3010 size = get_unaligned_be16(&cdb[7]);
3011 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3013 cmd->execute_task = target_emulate_unmap;
3016 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3018 goto out_unsupported_cdb;
3021 size = transport_get_size(1, cdb, cmd);
3023 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3024 goto out_invalid_cdb_field;
3027 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
3028 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3030 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3031 goto out_unsupported_cdb;
3033 cmd->execute_task = target_emulate_write_same;
3036 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3038 goto out_unsupported_cdb;
3041 size = transport_get_size(1, cdb, cmd);
3043 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3044 goto out_invalid_cdb_field;
3047 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3048 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3050 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3051 * of byte 1 bit 3 UNMAP instead of original reserved field
3053 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3054 goto out_unsupported_cdb;
3056 cmd->execute_task = target_emulate_write_same;
3058 case ALLOW_MEDIUM_REMOVAL:
3064 case TEST_UNIT_READY:
3066 case WRITE_FILEMARKS:
3067 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3069 cmd->execute_task = target_emulate_noop;
3071 case GPCMD_CLOSE_TRACK:
3072 case INITIALIZE_ELEMENT_STATUS:
3073 case GPCMD_LOAD_UNLOAD:
3074 case GPCMD_SET_SPEED:
3076 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3079 cmd->execute_task = target_report_luns;
3080 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3082 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3083 * See spc4r17 section 5.3
3085 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3086 cmd->sam_task_attr = MSG_HEAD_TAG;
3087 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3090 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3091 " 0x%02x, sending CHECK_CONDITION.\n",
3092 cmd->se_tfo->get_fabric_name(), cdb[0]);
3093 goto out_unsupported_cdb;
3096 if (size != cmd->data_length) {
3097 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3098 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3099 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3100 cmd->data_length, size, cdb[0]);
3102 cmd->cmd_spdtl = size;
3104 if (cmd->data_direction == DMA_TO_DEVICE) {
3105 pr_err("Rejecting underflow/overflow"
3107 goto out_invalid_cdb_field;
3110 * Reject READ_* or WRITE_* with overflow/underflow for
3111 * type SCF_SCSI_DATA_SG_IO_CDB.
3113 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3114 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3115 " CDB on non 512-byte sector setup subsystem"
3116 " plugin: %s\n", dev->transport->name);
3117 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3118 goto out_invalid_cdb_field;
3121 if (size > cmd->data_length) {
3122 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3123 cmd->residual_count = (size - cmd->data_length);
3125 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3126 cmd->residual_count = (cmd->data_length - size);
3128 cmd->data_length = size;
3131 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB &&
3132 sectors > dev->se_sub_dev->se_dev_attrib.fabric_max_sectors) {
3133 printk_ratelimited(KERN_ERR "SCSI OP %02xh with too big sectors %u\n",
3135 goto out_invalid_cdb_field;
3138 /* reject any command that we don't have a handler for */
3139 if (!(passthrough || cmd->execute_task ||
3140 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3141 goto out_unsupported_cdb;
3143 transport_set_supported_SAM_opcode(cmd);
3146 out_unsupported_cdb:
3147 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3148 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3150 out_invalid_cdb_field:
3151 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3152 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3157 * Called from I/O completion to determine which dormant/delayed
3158 * and ordered cmds need to have their tasks added to the execution queue.
3160 static void transport_complete_task_attr(struct se_cmd *cmd)
3162 struct se_device *dev = cmd->se_dev;
3163 struct se_cmd *cmd_p, *cmd_tmp;
3164 int new_active_tasks = 0;
3166 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3167 atomic_dec(&dev->simple_cmds);
3168 smp_mb__after_atomic_dec();
3169 dev->dev_cur_ordered_id++;
3170 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3171 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3172 cmd->se_ordered_id);
3173 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3174 dev->dev_cur_ordered_id++;
3175 pr_debug("Incremented dev_cur_ordered_id: %u for"
3176 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3177 cmd->se_ordered_id);
3178 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3179 atomic_dec(&dev->dev_ordered_sync);
3180 smp_mb__after_atomic_dec();
3182 dev->dev_cur_ordered_id++;
3183 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3184 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3187 * Process all commands up to the last received
3188 * ORDERED task attribute which requires another blocking
3191 spin_lock(&dev->delayed_cmd_lock);
3192 list_for_each_entry_safe(cmd_p, cmd_tmp,
3193 &dev->delayed_cmd_list, se_delayed_node) {
3195 list_del(&cmd_p->se_delayed_node);
3196 spin_unlock(&dev->delayed_cmd_lock);
3198 pr_debug("Calling add_tasks() for"
3199 " cmd_p: 0x%02x Task Attr: 0x%02x"
3200 " Dormant -> Active, se_ordered_id: %u\n",
3201 cmd_p->t_task_cdb[0],
3202 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3204 transport_add_tasks_from_cmd(cmd_p);
3207 spin_lock(&dev->delayed_cmd_lock);
3208 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3211 spin_unlock(&dev->delayed_cmd_lock);
3213 * If new tasks have become active, wake up the transport thread
3214 * to do the processing of the Active tasks.
3216 if (new_active_tasks != 0)
3217 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3220 static void transport_complete_qf(struct se_cmd *cmd)
3224 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3225 transport_complete_task_attr(cmd);
3227 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3228 ret = cmd->se_tfo->queue_status(cmd);
3233 switch (cmd->data_direction) {
3234 case DMA_FROM_DEVICE:
3235 ret = cmd->se_tfo->queue_data_in(cmd);
3238 if (cmd->t_bidi_data_sg) {
3239 ret = cmd->se_tfo->queue_data_in(cmd);
3243 /* Fall through for DMA_TO_DEVICE */
3245 ret = cmd->se_tfo->queue_status(cmd);
3253 transport_handle_queue_full(cmd, cmd->se_dev);
3256 transport_lun_remove_cmd(cmd);
3257 transport_cmd_check_stop_to_fabric(cmd);
3260 static void transport_handle_queue_full(
3262 struct se_device *dev)
3264 spin_lock_irq(&dev->qf_cmd_lock);
3265 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3266 atomic_inc(&dev->dev_qf_count);
3267 smp_mb__after_atomic_inc();
3268 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3270 schedule_work(&cmd->se_dev->qf_work_queue);
3273 static void target_complete_ok_work(struct work_struct *work)
3275 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
3276 int reason = 0, ret;
3279 * Check if we need to move delayed/dormant tasks from cmds on the
3280 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3283 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3284 transport_complete_task_attr(cmd);
3286 * Check to schedule QUEUE_FULL work, or execute an existing
3287 * cmd->transport_qf_callback()
3289 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3290 schedule_work(&cmd->se_dev->qf_work_queue);
3293 * Check if we need to retrieve a sense buffer from
3294 * the struct se_cmd in question.
3296 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3297 if (transport_get_sense_data(cmd) < 0)
3298 reason = TCM_NON_EXISTENT_LUN;
3301 * Only set when an struct se_task->task_scsi_status returned
3302 * a non GOOD status.
3304 if (cmd->scsi_status) {
3305 ret = transport_send_check_condition_and_sense(
3307 if (ret == -EAGAIN || ret == -ENOMEM)
3310 transport_lun_remove_cmd(cmd);
3311 transport_cmd_check_stop_to_fabric(cmd);
3316 * Check for a callback, used by amongst other things
3317 * XDWRITE_READ_10 emulation.
3319 if (cmd->transport_complete_callback)
3320 cmd->transport_complete_callback(cmd);
3322 switch (cmd->data_direction) {
3323 case DMA_FROM_DEVICE:
3324 spin_lock(&cmd->se_lun->lun_sep_lock);
3325 if (cmd->se_lun->lun_sep) {
3326 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3329 spin_unlock(&cmd->se_lun->lun_sep_lock);
3331 ret = cmd->se_tfo->queue_data_in(cmd);
3332 if (ret == -EAGAIN || ret == -ENOMEM)
3336 spin_lock(&cmd->se_lun->lun_sep_lock);
3337 if (cmd->se_lun->lun_sep) {
3338 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3341 spin_unlock(&cmd->se_lun->lun_sep_lock);
3343 * Check if we need to send READ payload for BIDI-COMMAND
3345 if (cmd->t_bidi_data_sg) {
3346 spin_lock(&cmd->se_lun->lun_sep_lock);
3347 if (cmd->se_lun->lun_sep) {
3348 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3351 spin_unlock(&cmd->se_lun->lun_sep_lock);
3352 ret = cmd->se_tfo->queue_data_in(cmd);
3353 if (ret == -EAGAIN || ret == -ENOMEM)
3357 /* Fall through for DMA_TO_DEVICE */
3359 ret = cmd->se_tfo->queue_status(cmd);
3360 if (ret == -EAGAIN || ret == -ENOMEM)
3367 transport_lun_remove_cmd(cmd);
3368 transport_cmd_check_stop_to_fabric(cmd);
3372 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3373 " data_direction: %d\n", cmd, cmd->data_direction);
3374 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3375 transport_handle_queue_full(cmd, cmd->se_dev);
3378 static void transport_free_dev_tasks(struct se_cmd *cmd)
3380 struct se_task *task, *task_tmp;
3381 unsigned long flags;
3382 LIST_HEAD(dispose_list);
3384 spin_lock_irqsave(&cmd->t_state_lock, flags);
3385 list_for_each_entry_safe(task, task_tmp,
3386 &cmd->t_task_list, t_list) {
3387 if (!(task->task_flags & TF_ACTIVE))
3388 list_move_tail(&task->t_list, &dispose_list);
3390 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3392 while (!list_empty(&dispose_list)) {
3393 task = list_first_entry(&dispose_list, struct se_task, t_list);
3395 if (task->task_sg != cmd->t_data_sg &&
3396 task->task_sg != cmd->t_bidi_data_sg)
3397 kfree(task->task_sg);
3399 list_del(&task->t_list);
3401 cmd->se_dev->transport->free_task(task);
3405 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3407 struct scatterlist *sg;
3410 for_each_sg(sgl, sg, nents, count)
3411 __free_page(sg_page(sg));
3416 static inline void transport_free_pages(struct se_cmd *cmd)
3418 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3421 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3422 cmd->t_data_sg = NULL;
3423 cmd->t_data_nents = 0;
3425 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3426 cmd->t_bidi_data_sg = NULL;
3427 cmd->t_bidi_data_nents = 0;
3431 * transport_release_cmd - free a command
3432 * @cmd: command to free
3434 * This routine unconditionally frees a command, and reference counting
3435 * or list removal must be done in the caller.
3437 static void transport_release_cmd(struct se_cmd *cmd)
3439 BUG_ON(!cmd->se_tfo);
3441 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
3442 core_tmr_release_req(cmd->se_tmr_req);
3443 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3444 kfree(cmd->t_task_cdb);
3446 * If this cmd has been setup with target_get_sess_cmd(), drop
3447 * the kref and call ->release_cmd() in kref callback.
3449 if (cmd->check_release != 0) {
3450 target_put_sess_cmd(cmd->se_sess, cmd);
3453 cmd->se_tfo->release_cmd(cmd);
3457 * transport_put_cmd - release a reference to a command
3458 * @cmd: command to release
3460 * This routine releases our reference to the command and frees it if possible.
3462 static void transport_put_cmd(struct se_cmd *cmd)
3464 unsigned long flags;
3467 spin_lock_irqsave(&cmd->t_state_lock, flags);
3468 if (atomic_read(&cmd->t_fe_count)) {
3469 if (!atomic_dec_and_test(&cmd->t_fe_count))
3473 if (atomic_read(&cmd->t_se_count)) {
3474 if (!atomic_dec_and_test(&cmd->t_se_count))
3478 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
3479 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
3480 transport_all_task_dev_remove_state(cmd);
3483 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3485 if (free_tasks != 0)
3486 transport_free_dev_tasks(cmd);
3488 transport_free_pages(cmd);
3489 transport_release_cmd(cmd);
3492 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3496 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3497 * allocating in the core.
3498 * @cmd: Associated se_cmd descriptor
3499 * @mem: SGL style memory for TCM WRITE / READ
3500 * @sg_mem_num: Number of SGL elements
3501 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3502 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3504 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3507 int transport_generic_map_mem_to_cmd(
3509 struct scatterlist *sgl,
3511 struct scatterlist *sgl_bidi,
3514 if (!sgl || !sgl_count)
3517 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3518 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3520 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3521 * scatterlists already have been set to follow what the fabric
3522 * passes for the original expected data transfer length.
3524 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3525 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3526 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3527 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3528 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3532 cmd->t_data_sg = sgl;
3533 cmd->t_data_nents = sgl_count;
3535 if (sgl_bidi && sgl_bidi_count) {
3536 cmd->t_bidi_data_sg = sgl_bidi;
3537 cmd->t_bidi_data_nents = sgl_bidi_count;
3539 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3544 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3546 void *transport_kmap_data_sg(struct se_cmd *cmd)
3548 struct scatterlist *sg = cmd->t_data_sg;
3549 struct page **pages;
3554 * We need to take into account a possible offset here for fabrics like
3555 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3556 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3558 if (!cmd->t_data_nents)
3560 else if (cmd->t_data_nents == 1)
3561 return kmap(sg_page(sg)) + sg->offset;
3563 /* >1 page. use vmap */
3564 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3568 /* convert sg[] to pages[] */
3569 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3570 pages[i] = sg_page(sg);
3573 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3575 if (!cmd->t_data_vmap)
3578 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
3580 EXPORT_SYMBOL(transport_kmap_data_sg);
3582 void transport_kunmap_data_sg(struct se_cmd *cmd)
3584 if (!cmd->t_data_nents) {
3586 } else if (cmd->t_data_nents == 1) {
3587 kunmap(sg_page(cmd->t_data_sg));
3591 vunmap(cmd->t_data_vmap);
3592 cmd->t_data_vmap = NULL;
3594 EXPORT_SYMBOL(transport_kunmap_data_sg);
3597 transport_generic_get_mem(struct se_cmd *cmd)
3599 u32 length = cmd->data_length;
3605 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3606 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3607 if (!cmd->t_data_sg)
3610 cmd->t_data_nents = nents;
3611 sg_init_table(cmd->t_data_sg, nents);
3613 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB ? 0 : __GFP_ZERO;
3616 u32 page_len = min_t(u32, length, PAGE_SIZE);
3617 page = alloc_page(GFP_KERNEL | zero_flag);
3621 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3629 __free_page(sg_page(&cmd->t_data_sg[i]));
3632 kfree(cmd->t_data_sg);
3633 cmd->t_data_sg = NULL;
3637 /* Reduce sectors if they are too long for the device */
3638 static inline sector_t transport_limit_task_sectors(
3639 struct se_device *dev,
3640 unsigned long long lba,
3643 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
3645 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3646 if ((lba + sectors) > transport_dev_end_lba(dev))
3647 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
3654 * This function can be used by HW target mode drivers to create a linked
3655 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3656 * This is intended to be called during the completion path by TCM Core
3657 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3659 void transport_do_task_sg_chain(struct se_cmd *cmd)
3661 struct scatterlist *sg_first = NULL;
3662 struct scatterlist *sg_prev = NULL;
3663 int sg_prev_nents = 0;
3664 struct scatterlist *sg;
3665 struct se_task *task;
3666 u32 chained_nents = 0;
3669 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3672 * Walk the struct se_task list and setup scatterlist chains
3673 * for each contiguously allocated struct se_task->task_sg[].
3675 list_for_each_entry(task, &cmd->t_task_list, t_list) {
3680 sg_first = task->task_sg;
3681 chained_nents = task->task_sg_nents;
3683 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
3684 chained_nents += task->task_sg_nents;
3687 * For the padded tasks, use the extra SGL vector allocated
3688 * in transport_allocate_data_tasks() for the sg_prev_nents
3689 * offset into sg_chain() above.
3691 * We do not need the padding for the last task (or a single
3692 * task), but in that case we will never use the sg_prev_nents
3693 * value below which would be incorrect.
3695 sg_prev_nents = (task->task_sg_nents + 1);
3696 sg_prev = task->task_sg;
3699 * Setup the starting pointer and total t_tasks_sg_linked_no including
3700 * padding SGs for linking and to mark the end.
3702 cmd->t_tasks_sg_chained = sg_first;
3703 cmd->t_tasks_sg_chained_no = chained_nents;
3705 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
3706 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3707 cmd->t_tasks_sg_chained_no);
3709 for_each_sg(cmd->t_tasks_sg_chained, sg,
3710 cmd->t_tasks_sg_chained_no, i) {
3712 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
3713 i, sg, sg_page(sg), sg->length, sg->offset);
3714 if (sg_is_chain(sg))
3715 pr_debug("SG: %p sg_is_chain=1\n", sg);
3717 pr_debug("SG: %p sg_is_last=1\n", sg);
3720 EXPORT_SYMBOL(transport_do_task_sg_chain);
3723 * Break up cmd into chunks transport can handle
3726 transport_allocate_data_tasks(struct se_cmd *cmd,
3727 enum dma_data_direction data_direction,
3728 struct scatterlist *cmd_sg, unsigned int sgl_nents)
3730 struct se_device *dev = cmd->se_dev;
3732 unsigned long long lba;
3733 sector_t sectors, dev_max_sectors;
3736 if (transport_cmd_get_valid_sectors(cmd) < 0)
3739 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3740 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
3742 WARN_ON(cmd->data_length % sector_size);
3744 lba = cmd->t_task_lba;
3745 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
3746 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
3749 * If we need just a single task reuse the SG list in the command
3750 * and avoid a lot of work.
3752 if (task_count == 1) {
3753 struct se_task *task;
3754 unsigned long flags;
3756 task = transport_generic_get_task(cmd, data_direction);
3760 task->task_sg = cmd_sg;
3761 task->task_sg_nents = sgl_nents;
3763 task->task_lba = lba;
3764 task->task_sectors = sectors;
3765 task->task_size = task->task_sectors * sector_size;
3767 spin_lock_irqsave(&cmd->t_state_lock, flags);
3768 list_add_tail(&task->t_list, &cmd->t_task_list);
3769 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3774 for (i = 0; i < task_count; i++) {
3775 struct se_task *task;
3776 unsigned int task_size, task_sg_nents_padded;
3777 struct scatterlist *sg;
3778 unsigned long flags;
3781 task = transport_generic_get_task(cmd, data_direction);
3785 task->task_lba = lba;
3786 task->task_sectors = min(sectors, dev_max_sectors);
3787 task->task_size = task->task_sectors * sector_size;
3790 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3791 * in order to calculate the number per task SGL entries
3793 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
3795 * Check if the fabric module driver is requesting that all
3796 * struct se_task->task_sg[] be chained together.. If so,
3797 * then allocate an extra padding SG entry for linking and
3798 * marking the end of the chained SGL for every task except
3799 * the last one for (task_count > 1) operation, or skipping
3800 * the extra padding for the (task_count == 1) case.
3802 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3803 task_sg_nents_padded = (task->task_sg_nents + 1);
3805 task_sg_nents_padded = task->task_sg_nents;
3807 task->task_sg = kmalloc(sizeof(struct scatterlist) *
3808 task_sg_nents_padded, GFP_KERNEL);
3809 if (!task->task_sg) {
3810 cmd->se_dev->transport->free_task(task);
3814 sg_init_table(task->task_sg, task_sg_nents_padded);
3816 task_size = task->task_size;
3818 /* Build new sgl, only up to task_size */
3819 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
3820 if (cmd_sg->length > task_size)
3824 task_size -= cmd_sg->length;
3825 cmd_sg = sg_next(cmd_sg);
3828 lba += task->task_sectors;
3829 sectors -= task->task_sectors;
3831 spin_lock_irqsave(&cmd->t_state_lock, flags);
3832 list_add_tail(&task->t_list, &cmd->t_task_list);
3833 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3840 transport_allocate_control_task(struct se_cmd *cmd)
3842 struct se_task *task;
3843 unsigned long flags;
3845 /* Workaround for handling zero-length control CDBs */
3846 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3850 task = transport_generic_get_task(cmd, cmd->data_direction);
3854 task->task_sg = cmd->t_data_sg;
3855 task->task_size = cmd->data_length;
3856 task->task_sg_nents = cmd->t_data_nents;
3858 spin_lock_irqsave(&cmd->t_state_lock, flags);
3859 list_add_tail(&task->t_list, &cmd->t_task_list);
3860 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3862 /* Success! Return number of tasks allocated */
3867 * Allocate any required ressources to execute the command, and either place
3868 * it on the execution queue if possible. For writes we might not have the
3869 * payload yet, thus notify the fabric via a call to ->write_pending instead.
3871 int transport_generic_new_cmd(struct se_cmd *cmd)
3873 struct se_device *dev = cmd->se_dev;
3874 int task_cdbs, task_cdbs_bidi = 0;
3879 * Determine is the TCM fabric module has already allocated physical
3880 * memory, and is directly calling transport_generic_map_mem_to_cmd()
3883 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3885 ret = transport_generic_get_mem(cmd);
3891 * For BIDI command set up the read tasks first.
3893 if (cmd->t_bidi_data_sg &&
3894 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3895 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3897 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3898 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3899 cmd->t_bidi_data_nents);
3900 if (task_cdbs_bidi <= 0)
3903 atomic_inc(&cmd->t_fe_count);
3904 atomic_inc(&cmd->t_se_count);
3908 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3909 task_cdbs = transport_allocate_data_tasks(cmd,
3910 cmd->data_direction, cmd->t_data_sg,
3913 task_cdbs = transport_allocate_control_task(cmd);
3918 else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
3919 spin_lock_irq(&cmd->t_state_lock);
3920 cmd->t_state = TRANSPORT_COMPLETE;
3921 cmd->transport_state |= CMD_T_ACTIVE;
3922 spin_unlock_irq(&cmd->t_state_lock);
3924 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3925 u8 ua_asc = 0, ua_ascq = 0;
3927 core_scsi3_ua_clear_for_request_sense(cmd,
3931 INIT_WORK(&cmd->work, target_complete_ok_work);
3932 queue_work(target_completion_wq, &cmd->work);
3937 atomic_inc(&cmd->t_fe_count);
3938 atomic_inc(&cmd->t_se_count);
3941 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3942 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3943 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
3946 * For WRITEs, let the fabric know its buffer is ready..
3947 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3948 * will be added to the struct se_device execution queue after its WRITE
3949 * data has arrived. (ie: It gets handled by the transport processing
3950 * thread a second time)
3952 if (cmd->data_direction == DMA_TO_DEVICE) {
3953 transport_add_tasks_to_state_queue(cmd);
3954 return transport_generic_write_pending(cmd);
3957 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3958 * to the execution queue.
3960 transport_execute_tasks(cmd);
3964 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3965 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3968 EXPORT_SYMBOL(transport_generic_new_cmd);
3970 /* transport_generic_process_write():
3974 void transport_generic_process_write(struct se_cmd *cmd)
3976 transport_execute_tasks(cmd);
3978 EXPORT_SYMBOL(transport_generic_process_write);
3980 static void transport_write_pending_qf(struct se_cmd *cmd)
3984 ret = cmd->se_tfo->write_pending(cmd);
3985 if (ret == -EAGAIN || ret == -ENOMEM) {
3986 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3988 transport_handle_queue_full(cmd, cmd->se_dev);
3992 static int transport_generic_write_pending(struct se_cmd *cmd)
3994 unsigned long flags;
3997 spin_lock_irqsave(&cmd->t_state_lock, flags);
3998 cmd->t_state = TRANSPORT_WRITE_PENDING;
3999 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4002 * Clear the se_cmd for WRITE_PENDING status in order to set
4003 * CMD_T_ACTIVE so that transport_generic_handle_data can be called
4004 * from HW target mode interrupt code. This is safe to be called
4005 * with transport_off=1 before the cmd->se_tfo->write_pending
4006 * because the se_cmd->se_lun pointer is not being cleared.
4008 transport_cmd_check_stop(cmd, 1, 0);
4011 * Call the fabric write_pending function here to let the
4012 * frontend know that WRITE buffers are ready.
4014 ret = cmd->se_tfo->write_pending(cmd);
4015 if (ret == -EAGAIN || ret == -ENOMEM)
4023 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
4024 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
4025 transport_handle_queue_full(cmd, cmd->se_dev);
4029 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
4031 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
4032 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
4033 transport_wait_for_tasks(cmd);
4035 transport_release_cmd(cmd);
4038 transport_wait_for_tasks(cmd);
4040 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4043 transport_lun_remove_cmd(cmd);
4045 transport_free_dev_tasks(cmd);
4047 transport_put_cmd(cmd);
4050 EXPORT_SYMBOL(transport_generic_free_cmd);
4052 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
4053 * @se_sess: session to reference
4054 * @se_cmd: command descriptor to add
4055 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
4057 void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
4060 unsigned long flags;
4062 kref_init(&se_cmd->cmd_kref);
4064 * Add a second kref if the fabric caller is expecting to handle
4065 * fabric acknowledgement that requires two target_put_sess_cmd()
4066 * invocations before se_cmd descriptor release.
4068 if (ack_kref == true) {
4069 kref_get(&se_cmd->cmd_kref);
4070 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
4073 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4074 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
4075 se_cmd->check_release = 1;
4076 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4078 EXPORT_SYMBOL(target_get_sess_cmd);
4080 static void target_release_cmd_kref(struct kref *kref)
4082 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
4083 struct se_session *se_sess = se_cmd->se_sess;
4084 unsigned long flags;
4086 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4087 if (list_empty(&se_cmd->se_cmd_list)) {
4088 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4089 se_cmd->se_tfo->release_cmd(se_cmd);
4092 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
4093 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4094 complete(&se_cmd->cmd_wait_comp);
4097 list_del(&se_cmd->se_cmd_list);
4098 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4100 se_cmd->se_tfo->release_cmd(se_cmd);
4103 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
4104 * @se_sess: session to reference
4105 * @se_cmd: command descriptor to drop
4107 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
4109 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
4111 EXPORT_SYMBOL(target_put_sess_cmd);
4113 /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
4114 * @se_sess: session to split
4116 void target_splice_sess_cmd_list(struct se_session *se_sess)
4118 struct se_cmd *se_cmd;
4119 unsigned long flags;
4121 WARN_ON(!list_empty(&se_sess->sess_wait_list));
4122 INIT_LIST_HEAD(&se_sess->sess_wait_list);
4124 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4125 se_sess->sess_tearing_down = 1;
4127 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
4129 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
4130 se_cmd->cmd_wait_set = 1;
4132 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4134 EXPORT_SYMBOL(target_splice_sess_cmd_list);
4136 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
4137 * @se_sess: session to wait for active I/O
4138 * @wait_for_tasks: Make extra transport_wait_for_tasks call
4140 void target_wait_for_sess_cmds(
4141 struct se_session *se_sess,
4144 struct se_cmd *se_cmd, *tmp_cmd;
4147 list_for_each_entry_safe(se_cmd, tmp_cmd,
4148 &se_sess->sess_wait_list, se_cmd_list) {
4149 list_del(&se_cmd->se_cmd_list);
4151 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4152 " %d\n", se_cmd, se_cmd->t_state,
4153 se_cmd->se_tfo->get_cmd_state(se_cmd));
4155 if (wait_for_tasks) {
4156 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4157 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4158 se_cmd->se_tfo->get_cmd_state(se_cmd));
4160 rc = transport_wait_for_tasks(se_cmd);
4162 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4163 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4164 se_cmd->se_tfo->get_cmd_state(se_cmd));
4168 wait_for_completion(&se_cmd->cmd_wait_comp);
4169 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4170 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4171 se_cmd->se_tfo->get_cmd_state(se_cmd));
4174 se_cmd->se_tfo->release_cmd(se_cmd);
4177 EXPORT_SYMBOL(target_wait_for_sess_cmds);
4179 /* transport_lun_wait_for_tasks():
4181 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4182 * an struct se_lun to be successfully shutdown.
4184 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4186 unsigned long flags;
4189 * If the frontend has already requested this struct se_cmd to
4190 * be stopped, we can safely ignore this struct se_cmd.
4192 spin_lock_irqsave(&cmd->t_state_lock, flags);
4193 if (cmd->transport_state & CMD_T_STOP) {
4194 cmd->transport_state &= ~CMD_T_LUN_STOP;
4196 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
4197 cmd->se_tfo->get_task_tag(cmd));
4198 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4199 transport_cmd_check_stop(cmd, 1, 0);
4202 cmd->transport_state |= CMD_T_LUN_FE_STOP;
4203 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4205 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4207 ret = transport_stop_tasks_for_cmd(cmd);
4209 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4210 " %d\n", cmd, cmd->t_task_list_num, ret);
4212 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4213 cmd->se_tfo->get_task_tag(cmd));
4214 wait_for_completion(&cmd->transport_lun_stop_comp);
4215 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4216 cmd->se_tfo->get_task_tag(cmd));
4218 transport_remove_cmd_from_queue(cmd);
4223 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4225 struct se_cmd *cmd = NULL;
4226 unsigned long lun_flags, cmd_flags;
4228 * Do exception processing and return CHECK_CONDITION status to the
4231 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4232 while (!list_empty(&lun->lun_cmd_list)) {
4233 cmd = list_first_entry(&lun->lun_cmd_list,
4234 struct se_cmd, se_lun_node);
4235 list_del_init(&cmd->se_lun_node);
4238 * This will notify iscsi_target_transport.c:
4239 * transport_cmd_check_stop() that a LUN shutdown is in
4240 * progress for the iscsi_cmd_t.
4242 spin_lock(&cmd->t_state_lock);
4243 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4244 "_lun_stop for ITT: 0x%08x\n",
4245 cmd->se_lun->unpacked_lun,
4246 cmd->se_tfo->get_task_tag(cmd));
4247 cmd->transport_state |= CMD_T_LUN_STOP;
4248 spin_unlock(&cmd->t_state_lock);
4250 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4253 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4254 cmd->se_tfo->get_task_tag(cmd),
4255 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4259 * If the Storage engine still owns the iscsi_cmd_t, determine
4260 * and/or stop its context.
4262 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4263 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4264 cmd->se_tfo->get_task_tag(cmd));
4266 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4267 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4271 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4272 "_wait_for_tasks(): SUCCESS\n",
4273 cmd->se_lun->unpacked_lun,
4274 cmd->se_tfo->get_task_tag(cmd));
4276 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4277 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
4278 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4281 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
4282 transport_all_task_dev_remove_state(cmd);
4283 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4285 transport_free_dev_tasks(cmd);
4287 * The Storage engine stopped this struct se_cmd before it was
4288 * send to the fabric frontend for delivery back to the
4289 * Initiator Node. Return this SCSI CDB back with an
4290 * CHECK_CONDITION status.
4293 transport_send_check_condition_and_sense(cmd,
4294 TCM_NON_EXISTENT_LUN, 0);
4296 * If the fabric frontend is waiting for this iscsi_cmd_t to
4297 * be released, notify the waiting thread now that LU has
4298 * finished accessing it.
4300 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4301 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
4302 pr_debug("SE_LUN[%d] - Detected FE stop for"
4303 " struct se_cmd: %p ITT: 0x%08x\n",
4305 cmd, cmd->se_tfo->get_task_tag(cmd));
4307 spin_unlock_irqrestore(&cmd->t_state_lock,
4309 transport_cmd_check_stop(cmd, 1, 0);
4310 complete(&cmd->transport_lun_fe_stop_comp);
4311 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4314 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4315 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4317 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4318 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4320 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4323 static int transport_clear_lun_thread(void *p)
4325 struct se_lun *lun = p;
4327 __transport_clear_lun_from_sessions(lun);
4328 complete(&lun->lun_shutdown_comp);
4333 int transport_clear_lun_from_sessions(struct se_lun *lun)
4335 struct task_struct *kt;
4337 kt = kthread_run(transport_clear_lun_thread, lun,
4338 "tcm_cl_%u", lun->unpacked_lun);
4340 pr_err("Unable to start clear_lun thread\n");
4343 wait_for_completion(&lun->lun_shutdown_comp);
4349 * transport_wait_for_tasks - wait for completion to occur
4350 * @cmd: command to wait
4352 * Called from frontend fabric context to wait for storage engine
4353 * to pause and/or release frontend generated struct se_cmd.
4355 bool transport_wait_for_tasks(struct se_cmd *cmd)
4357 unsigned long flags;
4359 spin_lock_irqsave(&cmd->t_state_lock, flags);
4360 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
4361 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
4362 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4366 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4367 * has been set in transport_set_supported_SAM_opcode().
4369 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
4370 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
4371 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4375 * If we are already stopped due to an external event (ie: LUN shutdown)
4376 * sleep until the connection can have the passed struct se_cmd back.
4377 * The cmd->transport_lun_stopped_sem will be upped by
4378 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4379 * has completed its operation on the struct se_cmd.
4381 if (cmd->transport_state & CMD_T_LUN_STOP) {
4382 pr_debug("wait_for_tasks: Stopping"
4383 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4384 "_stop_comp); for ITT: 0x%08x\n",
4385 cmd->se_tfo->get_task_tag(cmd));
4387 * There is a special case for WRITES where a FE exception +
4388 * LUN shutdown means ConfigFS context is still sleeping on
4389 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4390 * We go ahead and up transport_lun_stop_comp just to be sure
4393 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4394 complete(&cmd->transport_lun_stop_comp);
4395 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4396 spin_lock_irqsave(&cmd->t_state_lock, flags);
4398 transport_all_task_dev_remove_state(cmd);
4400 * At this point, the frontend who was the originator of this
4401 * struct se_cmd, now owns the structure and can be released through
4402 * normal means below.
4404 pr_debug("wait_for_tasks: Stopped"
4405 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4406 "stop_comp); for ITT: 0x%08x\n",
4407 cmd->se_tfo->get_task_tag(cmd));
4409 cmd->transport_state &= ~CMD_T_LUN_STOP;
4412 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
4413 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4417 cmd->transport_state |= CMD_T_STOP;
4419 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4420 " i_state: %d, t_state: %d, CMD_T_STOP\n",
4421 cmd, cmd->se_tfo->get_task_tag(cmd),
4422 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4424 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4426 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4428 wait_for_completion(&cmd->t_transport_stop_comp);
4430 spin_lock_irqsave(&cmd->t_state_lock, flags);
4431 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
4433 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4434 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4435 cmd->se_tfo->get_task_tag(cmd));
4437 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4441 EXPORT_SYMBOL(transport_wait_for_tasks);
4443 static int transport_get_sense_codes(
4448 *asc = cmd->scsi_asc;
4449 *ascq = cmd->scsi_ascq;
4454 static int transport_set_sense_codes(
4459 cmd->scsi_asc = asc;
4460 cmd->scsi_ascq = ascq;
4465 int transport_send_check_condition_and_sense(
4470 unsigned char *buffer = cmd->sense_buffer;
4471 unsigned long flags;
4473 u8 asc = 0, ascq = 0;
4475 spin_lock_irqsave(&cmd->t_state_lock, flags);
4476 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4477 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4480 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4481 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4483 if (!reason && from_transport)
4486 if (!from_transport)
4487 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4489 * Data Segment and SenseLength of the fabric response PDU.
4491 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4492 * from include/scsi/scsi_cmnd.h
4494 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4495 TRANSPORT_SENSE_BUFFER);
4497 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4498 * SENSE KEY values from include/scsi/scsi.h
4501 case TCM_NON_EXISTENT_LUN:
4503 buffer[offset] = 0x70;
4504 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4505 /* ILLEGAL REQUEST */
4506 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4507 /* LOGICAL UNIT NOT SUPPORTED */
4508 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4510 case TCM_UNSUPPORTED_SCSI_OPCODE:
4511 case TCM_SECTOR_COUNT_TOO_MANY:
4513 buffer[offset] = 0x70;
4514 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4515 /* ILLEGAL REQUEST */
4516 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4517 /* INVALID COMMAND OPERATION CODE */
4518 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4520 case TCM_UNKNOWN_MODE_PAGE:
4522 buffer[offset] = 0x70;
4523 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4524 /* ILLEGAL REQUEST */
4525 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4526 /* INVALID FIELD IN CDB */
4527 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4529 case TCM_CHECK_CONDITION_ABORT_CMD:
4531 buffer[offset] = 0x70;
4532 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4533 /* ABORTED COMMAND */
4534 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4535 /* BUS DEVICE RESET FUNCTION OCCURRED */
4536 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4537 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4539 case TCM_INCORRECT_AMOUNT_OF_DATA:
4541 buffer[offset] = 0x70;
4542 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4543 /* ABORTED COMMAND */
4544 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4546 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4547 /* NOT ENOUGH UNSOLICITED DATA */
4548 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4550 case TCM_INVALID_CDB_FIELD:
4552 buffer[offset] = 0x70;
4553 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4554 /* ILLEGAL REQUEST */
4555 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4556 /* INVALID FIELD IN CDB */
4557 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4559 case TCM_INVALID_PARAMETER_LIST:
4561 buffer[offset] = 0x70;
4562 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4563 /* ILLEGAL REQUEST */
4564 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4565 /* INVALID FIELD IN PARAMETER LIST */
4566 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4568 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4570 buffer[offset] = 0x70;
4571 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4572 /* ABORTED COMMAND */
4573 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4575 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4576 /* UNEXPECTED_UNSOLICITED_DATA */
4577 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4579 case TCM_SERVICE_CRC_ERROR:
4581 buffer[offset] = 0x70;
4582 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4583 /* ABORTED COMMAND */
4584 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4585 /* PROTOCOL SERVICE CRC ERROR */
4586 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4588 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4590 case TCM_SNACK_REJECTED:
4592 buffer[offset] = 0x70;
4593 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4594 /* ABORTED COMMAND */
4595 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4597 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4598 /* FAILED RETRANSMISSION REQUEST */
4599 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4601 case TCM_WRITE_PROTECTED:
4603 buffer[offset] = 0x70;
4604 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4606 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4607 /* WRITE PROTECTED */
4608 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4610 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4612 buffer[offset] = 0x70;
4613 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4614 /* UNIT ATTENTION */
4615 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4616 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4617 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4618 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4620 case TCM_CHECK_CONDITION_NOT_READY:
4622 buffer[offset] = 0x70;
4623 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4625 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4626 transport_get_sense_codes(cmd, &asc, &ascq);
4627 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4628 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4630 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4633 buffer[offset] = 0x70;
4634 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4635 /* ILLEGAL REQUEST */
4636 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4637 /* LOGICAL UNIT COMMUNICATION FAILURE */
4638 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4642 * This code uses linux/include/scsi/scsi.h SAM status codes!
4644 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4646 * Automatically padded, this value is encoded in the fabric's
4647 * data_length response PDU containing the SCSI defined sense data.
4649 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4652 return cmd->se_tfo->queue_status(cmd);
4654 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4656 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4660 if (cmd->transport_state & CMD_T_ABORTED) {
4662 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4665 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4666 " status for CDB: 0x%02x ITT: 0x%08x\n",
4668 cmd->se_tfo->get_task_tag(cmd));
4670 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4671 cmd->se_tfo->queue_status(cmd);
4676 EXPORT_SYMBOL(transport_check_aborted_status);
4678 void transport_send_task_abort(struct se_cmd *cmd)
4680 unsigned long flags;
4682 spin_lock_irqsave(&cmd->t_state_lock, flags);
4683 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4684 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4687 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4690 * If there are still expected incoming fabric WRITEs, we wait
4691 * until until they have completed before sending a TASK_ABORTED
4692 * response. This response with TASK_ABORTED status will be
4693 * queued back to fabric module by transport_check_aborted_status().
4695 if (cmd->data_direction == DMA_TO_DEVICE) {
4696 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4697 cmd->transport_state |= CMD_T_ABORTED;
4698 smp_mb__after_atomic_inc();
4701 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4703 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4704 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4705 cmd->se_tfo->get_task_tag(cmd));
4707 cmd->se_tfo->queue_status(cmd);
4710 static int transport_generic_do_tmr(struct se_cmd *cmd)
4712 struct se_device *dev = cmd->se_dev;
4713 struct se_tmr_req *tmr = cmd->se_tmr_req;
4716 switch (tmr->function) {
4717 case TMR_ABORT_TASK:
4718 core_tmr_abort_task(dev, tmr, cmd->se_sess);
4720 case TMR_ABORT_TASK_SET:
4722 case TMR_CLEAR_TASK_SET:
4723 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4726 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4727 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4728 TMR_FUNCTION_REJECTED;
4730 case TMR_TARGET_WARM_RESET:
4731 tmr->response = TMR_FUNCTION_REJECTED;
4733 case TMR_TARGET_COLD_RESET:
4734 tmr->response = TMR_FUNCTION_REJECTED;
4737 pr_err("Uknown TMR function: 0x%02x.\n",
4739 tmr->response = TMR_FUNCTION_REJECTED;
4743 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4744 cmd->se_tfo->queue_tm_rsp(cmd);
4746 transport_cmd_check_stop_to_fabric(cmd);
4750 /* transport_processing_thread():
4754 static int transport_processing_thread(void *param)
4758 struct se_device *dev = param;
4760 while (!kthread_should_stop()) {
4761 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4762 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4763 kthread_should_stop());
4768 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4772 switch (cmd->t_state) {
4773 case TRANSPORT_NEW_CMD:
4776 case TRANSPORT_NEW_CMD_MAP:
4777 if (!cmd->se_tfo->new_cmd_map) {
4778 pr_err("cmd->se_tfo->new_cmd_map is"
4779 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4782 ret = cmd->se_tfo->new_cmd_map(cmd);
4784 transport_generic_request_failure(cmd);
4787 ret = transport_generic_new_cmd(cmd);
4789 transport_generic_request_failure(cmd);
4793 case TRANSPORT_PROCESS_WRITE:
4794 transport_generic_process_write(cmd);
4796 case TRANSPORT_PROCESS_TMR:
4797 transport_generic_do_tmr(cmd);
4799 case TRANSPORT_COMPLETE_QF_WP:
4800 transport_write_pending_qf(cmd);
4802 case TRANSPORT_COMPLETE_QF_OK:
4803 transport_complete_qf(cmd);
4806 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4807 "i_state: %d on SE LUN: %u\n",
4809 cmd->se_tfo->get_task_tag(cmd),
4810 cmd->se_tfo->get_cmd_state(cmd),
4811 cmd->se_lun->unpacked_lun);
4819 WARN_ON(!list_empty(&dev->state_task_list));
4820 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4821 dev->process_thread = NULL;