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 <asm/unaligned.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_tcq.h>
47 #include <target/target_core_base.h>
48 #include <target/target_core_device.h>
49 #include <target/target_core_tmr.h>
50 #include <target/target_core_tpg.h>
51 #include <target/target_core_transport.h>
52 #include <target/target_core_fabric_ops.h>
53 #include <target/target_core_configfs.h>
55 #include "target_core_alua.h"
56 #include "target_core_cdb.h"
57 #include "target_core_hba.h"
58 #include "target_core_pr.h"
59 #include "target_core_ua.h"
61 static int sub_api_initialized;
63 static struct workqueue_struct *target_completion_wq;
64 static struct kmem_cache *se_sess_cache;
65 struct kmem_cache *se_tmr_req_cache;
66 struct kmem_cache *se_ua_cache;
67 struct kmem_cache *t10_pr_reg_cache;
68 struct kmem_cache *t10_alua_lu_gp_cache;
69 struct kmem_cache *t10_alua_lu_gp_mem_cache;
70 struct kmem_cache *t10_alua_tg_pt_gp_cache;
71 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
73 static int transport_generic_write_pending(struct se_cmd *);
74 static int transport_processing_thread(void *param);
75 static int __transport_execute_tasks(struct se_device *dev);
76 static void transport_complete_task_attr(struct se_cmd *cmd);
77 static void transport_handle_queue_full(struct se_cmd *cmd,
78 struct se_device *dev);
79 static void transport_free_dev_tasks(struct se_cmd *cmd);
80 static int transport_generic_get_mem(struct se_cmd *cmd);
81 static void transport_put_cmd(struct se_cmd *cmd);
82 static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
83 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
84 static void transport_generic_request_failure(struct se_cmd *);
85 static void target_complete_ok_work(struct work_struct *work);
87 int init_se_kmem_caches(void)
89 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
90 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
92 if (!se_tmr_req_cache) {
93 pr_err("kmem_cache_create() for struct se_tmr_req"
97 se_sess_cache = kmem_cache_create("se_sess_cache",
98 sizeof(struct se_session), __alignof__(struct se_session),
100 if (!se_sess_cache) {
101 pr_err("kmem_cache_create() for struct se_session"
103 goto out_free_tmr_req_cache;
105 se_ua_cache = kmem_cache_create("se_ua_cache",
106 sizeof(struct se_ua), __alignof__(struct se_ua),
109 pr_err("kmem_cache_create() for struct se_ua failed\n");
110 goto out_free_sess_cache;
112 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
113 sizeof(struct t10_pr_registration),
114 __alignof__(struct t10_pr_registration), 0, NULL);
115 if (!t10_pr_reg_cache) {
116 pr_err("kmem_cache_create() for struct t10_pr_registration"
118 goto out_free_ua_cache;
120 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
121 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
123 if (!t10_alua_lu_gp_cache) {
124 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
126 goto out_free_pr_reg_cache;
128 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
129 sizeof(struct t10_alua_lu_gp_member),
130 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
131 if (!t10_alua_lu_gp_mem_cache) {
132 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
134 goto out_free_lu_gp_cache;
136 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
137 sizeof(struct t10_alua_tg_pt_gp),
138 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
139 if (!t10_alua_tg_pt_gp_cache) {
140 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
142 goto out_free_lu_gp_mem_cache;
144 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
145 "t10_alua_tg_pt_gp_mem_cache",
146 sizeof(struct t10_alua_tg_pt_gp_member),
147 __alignof__(struct t10_alua_tg_pt_gp_member),
149 if (!t10_alua_tg_pt_gp_mem_cache) {
150 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
152 goto out_free_tg_pt_gp_cache;
155 target_completion_wq = alloc_workqueue("target_completion",
157 if (!target_completion_wq)
158 goto out_free_tg_pt_gp_mem_cache;
162 out_free_tg_pt_gp_mem_cache:
163 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
164 out_free_tg_pt_gp_cache:
165 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
166 out_free_lu_gp_mem_cache:
167 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
168 out_free_lu_gp_cache:
169 kmem_cache_destroy(t10_alua_lu_gp_cache);
170 out_free_pr_reg_cache:
171 kmem_cache_destroy(t10_pr_reg_cache);
173 kmem_cache_destroy(se_ua_cache);
175 kmem_cache_destroy(se_sess_cache);
176 out_free_tmr_req_cache:
177 kmem_cache_destroy(se_tmr_req_cache);
182 void release_se_kmem_caches(void)
184 destroy_workqueue(target_completion_wq);
185 kmem_cache_destroy(se_tmr_req_cache);
186 kmem_cache_destroy(se_sess_cache);
187 kmem_cache_destroy(se_ua_cache);
188 kmem_cache_destroy(t10_pr_reg_cache);
189 kmem_cache_destroy(t10_alua_lu_gp_cache);
190 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
191 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
192 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
195 /* This code ensures unique mib indexes are handed out. */
196 static DEFINE_SPINLOCK(scsi_mib_index_lock);
197 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
200 * Allocate a new row index for the entry type specified
202 u32 scsi_get_new_index(scsi_index_t type)
206 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
208 spin_lock(&scsi_mib_index_lock);
209 new_index = ++scsi_mib_index[type];
210 spin_unlock(&scsi_mib_index_lock);
215 void transport_init_queue_obj(struct se_queue_obj *qobj)
217 atomic_set(&qobj->queue_cnt, 0);
218 INIT_LIST_HEAD(&qobj->qobj_list);
219 init_waitqueue_head(&qobj->thread_wq);
220 spin_lock_init(&qobj->cmd_queue_lock);
222 EXPORT_SYMBOL(transport_init_queue_obj);
224 void transport_subsystem_check_init(void)
228 if (sub_api_initialized)
231 ret = request_module("target_core_iblock");
233 pr_err("Unable to load target_core_iblock\n");
235 ret = request_module("target_core_file");
237 pr_err("Unable to load target_core_file\n");
239 ret = request_module("target_core_pscsi");
241 pr_err("Unable to load target_core_pscsi\n");
243 ret = request_module("target_core_stgt");
245 pr_err("Unable to load target_core_stgt\n");
247 sub_api_initialized = 1;
251 struct se_session *transport_init_session(void)
253 struct se_session *se_sess;
255 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
257 pr_err("Unable to allocate struct se_session from"
259 return ERR_PTR(-ENOMEM);
261 INIT_LIST_HEAD(&se_sess->sess_list);
262 INIT_LIST_HEAD(&se_sess->sess_acl_list);
263 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
264 INIT_LIST_HEAD(&se_sess->sess_wait_list);
265 spin_lock_init(&se_sess->sess_cmd_lock);
269 EXPORT_SYMBOL(transport_init_session);
272 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
274 void __transport_register_session(
275 struct se_portal_group *se_tpg,
276 struct se_node_acl *se_nacl,
277 struct se_session *se_sess,
278 void *fabric_sess_ptr)
280 unsigned char buf[PR_REG_ISID_LEN];
282 se_sess->se_tpg = se_tpg;
283 se_sess->fabric_sess_ptr = fabric_sess_ptr;
285 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
287 * Only set for struct se_session's that will actually be moving I/O.
288 * eg: *NOT* discovery sessions.
292 * If the fabric module supports an ISID based TransportID,
293 * save this value in binary from the fabric I_T Nexus now.
295 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
296 memset(&buf[0], 0, PR_REG_ISID_LEN);
297 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
298 &buf[0], PR_REG_ISID_LEN);
299 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
301 spin_lock_irq(&se_nacl->nacl_sess_lock);
303 * The se_nacl->nacl_sess pointer will be set to the
304 * last active I_T Nexus for each struct se_node_acl.
306 se_nacl->nacl_sess = se_sess;
308 list_add_tail(&se_sess->sess_acl_list,
309 &se_nacl->acl_sess_list);
310 spin_unlock_irq(&se_nacl->nacl_sess_lock);
312 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
314 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
315 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
317 EXPORT_SYMBOL(__transport_register_session);
319 void transport_register_session(
320 struct se_portal_group *se_tpg,
321 struct se_node_acl *se_nacl,
322 struct se_session *se_sess,
323 void *fabric_sess_ptr)
325 spin_lock_bh(&se_tpg->session_lock);
326 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
327 spin_unlock_bh(&se_tpg->session_lock);
329 EXPORT_SYMBOL(transport_register_session);
331 void transport_deregister_session_configfs(struct se_session *se_sess)
333 struct se_node_acl *se_nacl;
336 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
338 se_nacl = se_sess->se_node_acl;
340 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
341 list_del(&se_sess->sess_acl_list);
343 * If the session list is empty, then clear the pointer.
344 * Otherwise, set the struct se_session pointer from the tail
345 * element of the per struct se_node_acl active session list.
347 if (list_empty(&se_nacl->acl_sess_list))
348 se_nacl->nacl_sess = NULL;
350 se_nacl->nacl_sess = container_of(
351 se_nacl->acl_sess_list.prev,
352 struct se_session, sess_acl_list);
354 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
357 EXPORT_SYMBOL(transport_deregister_session_configfs);
359 void transport_free_session(struct se_session *se_sess)
361 kmem_cache_free(se_sess_cache, se_sess);
363 EXPORT_SYMBOL(transport_free_session);
365 void transport_deregister_session(struct se_session *se_sess)
367 struct se_portal_group *se_tpg = se_sess->se_tpg;
368 struct se_node_acl *se_nacl;
372 transport_free_session(se_sess);
376 spin_lock_irqsave(&se_tpg->session_lock, flags);
377 list_del(&se_sess->sess_list);
378 se_sess->se_tpg = NULL;
379 se_sess->fabric_sess_ptr = NULL;
380 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
383 * Determine if we need to do extra work for this initiator node's
384 * struct se_node_acl if it had been previously dynamically generated.
386 se_nacl = se_sess->se_node_acl;
388 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
389 if (se_nacl->dynamic_node_acl) {
390 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
392 list_del(&se_nacl->acl_list);
393 se_tpg->num_node_acls--;
394 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
396 core_tpg_wait_for_nacl_pr_ref(se_nacl);
397 core_free_device_list_for_node(se_nacl, se_tpg);
398 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
400 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
403 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
406 transport_free_session(se_sess);
408 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
409 se_tpg->se_tpg_tfo->get_fabric_name());
411 EXPORT_SYMBOL(transport_deregister_session);
414 * Called with cmd->t_state_lock held.
416 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
418 struct se_device *dev = cmd->se_dev;
419 struct se_task *task;
425 list_for_each_entry(task, &cmd->t_task_list, t_list) {
426 if (task->task_flags & TF_ACTIVE)
429 if (!atomic_read(&task->task_state_active))
432 spin_lock_irqsave(&dev->execute_task_lock, flags);
433 list_del(&task->t_state_list);
434 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
435 cmd->se_tfo->get_task_tag(cmd), dev, task);
436 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
438 atomic_set(&task->task_state_active, 0);
439 atomic_dec(&cmd->t_task_cdbs_ex_left);
443 /* transport_cmd_check_stop():
445 * 'transport_off = 1' determines if t_transport_active should be cleared.
446 * 'transport_off = 2' determines if task_dev_state should be removed.
448 * A non-zero u8 t_state sets cmd->t_state.
449 * Returns 1 when command is stopped, else 0.
451 static int transport_cmd_check_stop(
458 spin_lock_irqsave(&cmd->t_state_lock, flags);
460 * Determine if IOCTL context caller in requesting the stopping of this
461 * command for LUN shutdown purposes.
463 if (atomic_read(&cmd->transport_lun_stop)) {
464 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
465 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
466 cmd->se_tfo->get_task_tag(cmd));
468 atomic_set(&cmd->t_transport_active, 0);
469 if (transport_off == 2)
470 transport_all_task_dev_remove_state(cmd);
471 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
473 complete(&cmd->transport_lun_stop_comp);
477 * Determine if frontend context caller is requesting the stopping of
478 * this command for frontend exceptions.
480 if (atomic_read(&cmd->t_transport_stop)) {
481 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
482 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
483 cmd->se_tfo->get_task_tag(cmd));
485 if (transport_off == 2)
486 transport_all_task_dev_remove_state(cmd);
489 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
492 if (transport_off == 2)
494 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
496 complete(&cmd->t_transport_stop_comp);
500 atomic_set(&cmd->t_transport_active, 0);
501 if (transport_off == 2) {
502 transport_all_task_dev_remove_state(cmd);
504 * Clear struct se_cmd->se_lun before the transport_off == 2
505 * handoff to fabric module.
509 * Some fabric modules like tcm_loop can release
510 * their internally allocated I/O reference now and
513 * Fabric modules are expected to return '1' here if the
514 * se_cmd being passed is released at this point,
515 * or zero if not being released.
517 if (cmd->se_tfo->check_stop_free != NULL) {
518 spin_unlock_irqrestore(
519 &cmd->t_state_lock, flags);
521 return cmd->se_tfo->check_stop_free(cmd);
524 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
528 cmd->t_state = t_state;
529 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
534 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
536 return transport_cmd_check_stop(cmd, 2, 0);
539 static void transport_lun_remove_cmd(struct se_cmd *cmd)
541 struct se_lun *lun = cmd->se_lun;
547 spin_lock_irqsave(&cmd->t_state_lock, flags);
548 if (!atomic_read(&cmd->transport_dev_active)) {
549 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
552 atomic_set(&cmd->transport_dev_active, 0);
553 transport_all_task_dev_remove_state(cmd);
554 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
558 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
559 if (atomic_read(&cmd->transport_lun_active)) {
560 list_del(&cmd->se_lun_node);
561 atomic_set(&cmd->transport_lun_active, 0);
563 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
564 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
567 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
570 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
572 if (!cmd->se_tmr_req)
573 transport_lun_remove_cmd(cmd);
575 if (transport_cmd_check_stop_to_fabric(cmd))
578 transport_remove_cmd_from_queue(cmd);
579 transport_put_cmd(cmd);
583 static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
586 struct se_device *dev = cmd->se_dev;
587 struct se_queue_obj *qobj = &dev->dev_queue_obj;
591 spin_lock_irqsave(&cmd->t_state_lock, flags);
592 cmd->t_state = t_state;
593 atomic_set(&cmd->t_transport_active, 1);
594 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
597 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
599 /* If the cmd is already on the list, remove it before we add it */
600 if (!list_empty(&cmd->se_queue_node))
601 list_del(&cmd->se_queue_node);
603 atomic_inc(&qobj->queue_cnt);
606 list_add(&cmd->se_queue_node, &qobj->qobj_list);
608 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
609 atomic_set(&cmd->t_transport_queue_active, 1);
610 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
612 wake_up_interruptible(&qobj->thread_wq);
615 static struct se_cmd *
616 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
621 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
622 if (list_empty(&qobj->qobj_list)) {
623 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
626 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
628 atomic_set(&cmd->t_transport_queue_active, 0);
630 list_del_init(&cmd->se_queue_node);
631 atomic_dec(&qobj->queue_cnt);
632 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
637 static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
639 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
642 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
643 if (!atomic_read(&cmd->t_transport_queue_active)) {
644 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
647 atomic_set(&cmd->t_transport_queue_active, 0);
648 atomic_dec(&qobj->queue_cnt);
649 list_del_init(&cmd->se_queue_node);
650 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
652 if (atomic_read(&cmd->t_transport_queue_active)) {
653 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
654 cmd->se_tfo->get_task_tag(cmd),
655 atomic_read(&cmd->t_transport_queue_active));
660 * Completion function used by TCM subsystem plugins (such as FILEIO)
661 * for queueing up response from struct se_subsystem_api->do_task()
663 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
665 struct se_task *task = list_entry(cmd->t_task_list.next,
666 struct se_task, t_list);
669 cmd->scsi_status = SAM_STAT_GOOD;
670 task->task_scsi_status = GOOD;
672 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
673 task->task_se_cmd->scsi_sense_reason =
674 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
678 transport_complete_task(task, good);
680 EXPORT_SYMBOL(transport_complete_sync_cache);
682 static void target_complete_failure_work(struct work_struct *work)
684 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
686 transport_generic_request_failure(cmd);
689 /* transport_complete_task():
691 * Called from interrupt and non interrupt context depending
692 * on the transport plugin.
694 void transport_complete_task(struct se_task *task, int success)
696 struct se_cmd *cmd = task->task_se_cmd;
697 struct se_device *dev = cmd->se_dev;
700 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
701 cmd->t_task_cdb[0], dev);
704 atomic_inc(&dev->depth_left);
706 spin_lock_irqsave(&cmd->t_state_lock, flags);
707 task->task_flags &= ~TF_ACTIVE;
710 * See if any sense data exists, if so set the TASK_SENSE flag.
711 * Also check for any other post completion work that needs to be
712 * done by the plugins.
714 if (dev && dev->transport->transport_complete) {
715 if (dev->transport->transport_complete(task) != 0) {
716 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
717 task->task_sense = 1;
723 * See if we are waiting for outstanding struct se_task
724 * to complete for an exception condition
726 if (task->task_flags & TF_REQUEST_STOP) {
727 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
728 complete(&task->task_stop_comp);
733 cmd->t_tasks_failed = 1;
736 * Decrement the outstanding t_task_cdbs_left count. The last
737 * struct se_task from struct se_cmd will complete itself into the
738 * device queue depending upon int success.
740 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
741 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
745 if (cmd->t_tasks_failed) {
746 if (!task->task_error_status) {
747 task->task_error_status =
748 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
749 cmd->scsi_sense_reason =
750 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
753 INIT_WORK(&cmd->work, target_complete_failure_work);
755 atomic_set(&cmd->t_transport_complete, 1);
756 INIT_WORK(&cmd->work, target_complete_ok_work);
759 cmd->t_state = TRANSPORT_COMPLETE;
760 atomic_set(&cmd->t_transport_active, 1);
761 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
763 queue_work(target_completion_wq, &cmd->work);
765 EXPORT_SYMBOL(transport_complete_task);
768 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
769 * struct se_task list are ready to be added to the active execution list
772 * Called with se_dev_t->execute_task_lock called.
774 static inline int transport_add_task_check_sam_attr(
775 struct se_task *task,
776 struct se_task *task_prev,
777 struct se_device *dev)
780 * No SAM Task attribute emulation enabled, add to tail of
783 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
784 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
788 * HEAD_OF_QUEUE attribute for received CDB, which means
789 * the first task that is associated with a struct se_cmd goes to
790 * head of the struct se_device->execute_task_list, and task_prev
791 * after that for each subsequent task
793 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
794 list_add(&task->t_execute_list,
795 (task_prev != NULL) ?
796 &task_prev->t_execute_list :
797 &dev->execute_task_list);
799 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
800 " in execution queue\n",
801 task->task_se_cmd->t_task_cdb[0]);
805 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
806 * transitioned from Dermant -> Active state, and are added to the end
807 * of the struct se_device->execute_task_list
809 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
813 /* __transport_add_task_to_execute_queue():
815 * Called with se_dev_t->execute_task_lock called.
817 static void __transport_add_task_to_execute_queue(
818 struct se_task *task,
819 struct se_task *task_prev,
820 struct se_device *dev)
824 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
825 atomic_inc(&dev->execute_tasks);
827 if (atomic_read(&task->task_state_active))
830 * Determine if this task needs to go to HEAD_OF_QUEUE for the
831 * state list as well. Running with SAM Task Attribute emulation
832 * will always return head_of_queue == 0 here
835 list_add(&task->t_state_list, (task_prev) ?
836 &task_prev->t_state_list :
837 &dev->state_task_list);
839 list_add_tail(&task->t_state_list, &dev->state_task_list);
841 atomic_set(&task->task_state_active, 1);
843 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
844 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
848 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
850 struct se_device *dev = cmd->se_dev;
851 struct se_task *task;
854 spin_lock_irqsave(&cmd->t_state_lock, flags);
855 list_for_each_entry(task, &cmd->t_task_list, t_list) {
856 if (atomic_read(&task->task_state_active))
859 spin_lock(&dev->execute_task_lock);
860 list_add_tail(&task->t_state_list, &dev->state_task_list);
861 atomic_set(&task->task_state_active, 1);
863 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
864 task->task_se_cmd->se_tfo->get_task_tag(
865 task->task_se_cmd), task, dev);
867 spin_unlock(&dev->execute_task_lock);
869 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
872 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
874 struct se_device *dev = cmd->se_dev;
875 struct se_task *task, *task_prev = NULL;
878 spin_lock_irqsave(&dev->execute_task_lock, flags);
879 list_for_each_entry(task, &cmd->t_task_list, t_list) {
880 if (!list_empty(&task->t_execute_list))
883 * __transport_add_task_to_execute_queue() handles the
884 * SAM Task Attribute emulation if enabled
886 __transport_add_task_to_execute_queue(task, task_prev, dev);
889 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
892 void __transport_remove_task_from_execute_queue(struct se_task *task,
893 struct se_device *dev)
895 list_del_init(&task->t_execute_list);
896 atomic_dec(&dev->execute_tasks);
899 void transport_remove_task_from_execute_queue(
900 struct se_task *task,
901 struct se_device *dev)
905 if (WARN_ON(list_empty(&task->t_execute_list)))
908 spin_lock_irqsave(&dev->execute_task_lock, flags);
909 __transport_remove_task_from_execute_queue(task, dev);
910 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
914 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
917 static void target_qf_do_work(struct work_struct *work)
919 struct se_device *dev = container_of(work, struct se_device,
921 LIST_HEAD(qf_cmd_list);
922 struct se_cmd *cmd, *cmd_tmp;
924 spin_lock_irq(&dev->qf_cmd_lock);
925 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
926 spin_unlock_irq(&dev->qf_cmd_lock);
928 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
929 list_del(&cmd->se_qf_node);
930 atomic_dec(&dev->dev_qf_count);
931 smp_mb__after_atomic_dec();
933 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
934 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
935 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
936 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
939 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
943 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
945 switch (cmd->data_direction) {
948 case DMA_FROM_DEVICE:
952 case DMA_BIDIRECTIONAL:
961 void transport_dump_dev_state(
962 struct se_device *dev,
966 *bl += sprintf(b + *bl, "Status: ");
967 switch (dev->dev_status) {
968 case TRANSPORT_DEVICE_ACTIVATED:
969 *bl += sprintf(b + *bl, "ACTIVATED");
971 case TRANSPORT_DEVICE_DEACTIVATED:
972 *bl += sprintf(b + *bl, "DEACTIVATED");
974 case TRANSPORT_DEVICE_SHUTDOWN:
975 *bl += sprintf(b + *bl, "SHUTDOWN");
977 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
978 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
979 *bl += sprintf(b + *bl, "OFFLINE");
982 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
986 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
987 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
989 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
990 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
991 *bl += sprintf(b + *bl, " ");
994 void transport_dump_vpd_proto_id(
996 unsigned char *p_buf,
999 unsigned char buf[VPD_TMP_BUF_SIZE];
1002 memset(buf, 0, VPD_TMP_BUF_SIZE);
1003 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1005 switch (vpd->protocol_identifier) {
1007 sprintf(buf+len, "Fibre Channel\n");
1010 sprintf(buf+len, "Parallel SCSI\n");
1013 sprintf(buf+len, "SSA\n");
1016 sprintf(buf+len, "IEEE 1394\n");
1019 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1023 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1026 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1029 sprintf(buf+len, "Automation/Drive Interface Transport"
1033 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1036 sprintf(buf+len, "Unknown 0x%02x\n",
1037 vpd->protocol_identifier);
1042 strncpy(p_buf, buf, p_buf_len);
1044 pr_debug("%s", buf);
1048 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1051 * Check if the Protocol Identifier Valid (PIV) bit is set..
1053 * from spc3r23.pdf section 7.5.1
1055 if (page_83[1] & 0x80) {
1056 vpd->protocol_identifier = (page_83[0] & 0xf0);
1057 vpd->protocol_identifier_set = 1;
1058 transport_dump_vpd_proto_id(vpd, NULL, 0);
1061 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1063 int transport_dump_vpd_assoc(
1064 struct t10_vpd *vpd,
1065 unsigned char *p_buf,
1068 unsigned char buf[VPD_TMP_BUF_SIZE];
1072 memset(buf, 0, VPD_TMP_BUF_SIZE);
1073 len = sprintf(buf, "T10 VPD Identifier Association: ");
1075 switch (vpd->association) {
1077 sprintf(buf+len, "addressed logical unit\n");
1080 sprintf(buf+len, "target port\n");
1083 sprintf(buf+len, "SCSI target device\n");
1086 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1092 strncpy(p_buf, buf, p_buf_len);
1094 pr_debug("%s", buf);
1099 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1102 * The VPD identification association..
1104 * from spc3r23.pdf Section 7.6.3.1 Table 297
1106 vpd->association = (page_83[1] & 0x30);
1107 return transport_dump_vpd_assoc(vpd, NULL, 0);
1109 EXPORT_SYMBOL(transport_set_vpd_assoc);
1111 int transport_dump_vpd_ident_type(
1112 struct t10_vpd *vpd,
1113 unsigned char *p_buf,
1116 unsigned char buf[VPD_TMP_BUF_SIZE];
1120 memset(buf, 0, VPD_TMP_BUF_SIZE);
1121 len = sprintf(buf, "T10 VPD Identifier Type: ");
1123 switch (vpd->device_identifier_type) {
1125 sprintf(buf+len, "Vendor specific\n");
1128 sprintf(buf+len, "T10 Vendor ID based\n");
1131 sprintf(buf+len, "EUI-64 based\n");
1134 sprintf(buf+len, "NAA\n");
1137 sprintf(buf+len, "Relative target port identifier\n");
1140 sprintf(buf+len, "SCSI name string\n");
1143 sprintf(buf+len, "Unsupported: 0x%02x\n",
1144 vpd->device_identifier_type);
1150 if (p_buf_len < strlen(buf)+1)
1152 strncpy(p_buf, buf, p_buf_len);
1154 pr_debug("%s", buf);
1160 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1163 * The VPD identifier type..
1165 * from spc3r23.pdf Section 7.6.3.1 Table 298
1167 vpd->device_identifier_type = (page_83[1] & 0x0f);
1168 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1170 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1172 int transport_dump_vpd_ident(
1173 struct t10_vpd *vpd,
1174 unsigned char *p_buf,
1177 unsigned char buf[VPD_TMP_BUF_SIZE];
1180 memset(buf, 0, VPD_TMP_BUF_SIZE);
1182 switch (vpd->device_identifier_code_set) {
1183 case 0x01: /* Binary */
1184 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1185 &vpd->device_identifier[0]);
1187 case 0x02: /* ASCII */
1188 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1189 &vpd->device_identifier[0]);
1191 case 0x03: /* UTF-8 */
1192 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1193 &vpd->device_identifier[0]);
1196 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1197 " 0x%02x", vpd->device_identifier_code_set);
1203 strncpy(p_buf, buf, p_buf_len);
1205 pr_debug("%s", buf);
1211 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1213 static const char hex_str[] = "0123456789abcdef";
1214 int j = 0, i = 4; /* offset to start of the identifer */
1217 * The VPD Code Set (encoding)
1219 * from spc3r23.pdf Section 7.6.3.1 Table 296
1221 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1222 switch (vpd->device_identifier_code_set) {
1223 case 0x01: /* Binary */
1224 vpd->device_identifier[j++] =
1225 hex_str[vpd->device_identifier_type];
1226 while (i < (4 + page_83[3])) {
1227 vpd->device_identifier[j++] =
1228 hex_str[(page_83[i] & 0xf0) >> 4];
1229 vpd->device_identifier[j++] =
1230 hex_str[page_83[i] & 0x0f];
1234 case 0x02: /* ASCII */
1235 case 0x03: /* UTF-8 */
1236 while (i < (4 + page_83[3]))
1237 vpd->device_identifier[j++] = page_83[i++];
1243 return transport_dump_vpd_ident(vpd, NULL, 0);
1245 EXPORT_SYMBOL(transport_set_vpd_ident);
1247 static void core_setup_task_attr_emulation(struct se_device *dev)
1250 * If this device is from Target_Core_Mod/pSCSI, disable the
1251 * SAM Task Attribute emulation.
1253 * This is currently not available in upsream Linux/SCSI Target
1254 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1256 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1257 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1261 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1262 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1263 " device\n", dev->transport->name,
1264 dev->transport->get_device_rev(dev));
1267 static void scsi_dump_inquiry(struct se_device *dev)
1269 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1272 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1274 pr_debug(" Vendor: ");
1275 for (i = 0; i < 8; i++)
1276 if (wwn->vendor[i] >= 0x20)
1277 pr_debug("%c", wwn->vendor[i]);
1281 pr_debug(" Model: ");
1282 for (i = 0; i < 16; i++)
1283 if (wwn->model[i] >= 0x20)
1284 pr_debug("%c", wwn->model[i]);
1288 pr_debug(" Revision: ");
1289 for (i = 0; i < 4; i++)
1290 if (wwn->revision[i] >= 0x20)
1291 pr_debug("%c", wwn->revision[i]);
1297 device_type = dev->transport->get_device_type(dev);
1298 pr_debug(" Type: %s ", scsi_device_type(device_type));
1299 pr_debug(" ANSI SCSI revision: %02x\n",
1300 dev->transport->get_device_rev(dev));
1303 struct se_device *transport_add_device_to_core_hba(
1305 struct se_subsystem_api *transport,
1306 struct se_subsystem_dev *se_dev,
1308 void *transport_dev,
1309 struct se_dev_limits *dev_limits,
1310 const char *inquiry_prod,
1311 const char *inquiry_rev)
1314 struct se_device *dev;
1316 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1318 pr_err("Unable to allocate memory for se_dev_t\n");
1322 transport_init_queue_obj(&dev->dev_queue_obj);
1323 dev->dev_flags = device_flags;
1324 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1325 dev->dev_ptr = transport_dev;
1327 dev->se_sub_dev = se_dev;
1328 dev->transport = transport;
1329 INIT_LIST_HEAD(&dev->dev_list);
1330 INIT_LIST_HEAD(&dev->dev_sep_list);
1331 INIT_LIST_HEAD(&dev->dev_tmr_list);
1332 INIT_LIST_HEAD(&dev->execute_task_list);
1333 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1334 INIT_LIST_HEAD(&dev->state_task_list);
1335 INIT_LIST_HEAD(&dev->qf_cmd_list);
1336 spin_lock_init(&dev->execute_task_lock);
1337 spin_lock_init(&dev->delayed_cmd_lock);
1338 spin_lock_init(&dev->dev_reservation_lock);
1339 spin_lock_init(&dev->dev_status_lock);
1340 spin_lock_init(&dev->se_port_lock);
1341 spin_lock_init(&dev->se_tmr_lock);
1342 spin_lock_init(&dev->qf_cmd_lock);
1344 dev->queue_depth = dev_limits->queue_depth;
1345 atomic_set(&dev->depth_left, dev->queue_depth);
1346 atomic_set(&dev->dev_ordered_id, 0);
1348 se_dev_set_default_attribs(dev, dev_limits);
1350 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1351 dev->creation_time = get_jiffies_64();
1352 spin_lock_init(&dev->stats_lock);
1354 spin_lock(&hba->device_lock);
1355 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1357 spin_unlock(&hba->device_lock);
1359 * Setup the SAM Task Attribute emulation for struct se_device
1361 core_setup_task_attr_emulation(dev);
1363 * Force PR and ALUA passthrough emulation with internal object use.
1365 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1367 * Setup the Reservations infrastructure for struct se_device
1369 core_setup_reservations(dev, force_pt);
1371 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1373 if (core_setup_alua(dev, force_pt) < 0)
1377 * Startup the struct se_device processing thread
1379 dev->process_thread = kthread_run(transport_processing_thread, dev,
1380 "LIO_%s", dev->transport->name);
1381 if (IS_ERR(dev->process_thread)) {
1382 pr_err("Unable to create kthread: LIO_%s\n",
1383 dev->transport->name);
1387 * Setup work_queue for QUEUE_FULL
1389 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1391 * Preload the initial INQUIRY const values if we are doing
1392 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1393 * passthrough because this is being provided by the backend LLD.
1394 * This is required so that transport_get_inquiry() copies these
1395 * originals once back into DEV_T10_WWN(dev) for the virtual device
1398 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1399 if (!inquiry_prod || !inquiry_rev) {
1400 pr_err("All non TCM/pSCSI plugins require"
1401 " INQUIRY consts\n");
1405 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1406 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1407 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1409 scsi_dump_inquiry(dev);
1413 kthread_stop(dev->process_thread);
1415 spin_lock(&hba->device_lock);
1416 list_del(&dev->dev_list);
1418 spin_unlock(&hba->device_lock);
1420 se_release_vpd_for_dev(dev);
1426 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1428 /* transport_generic_prepare_cdb():
1430 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1431 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1432 * The point of this is since we are mapping iSCSI LUNs to
1433 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1434 * devices and HBAs for a loop.
1436 static inline void transport_generic_prepare_cdb(
1440 case READ_10: /* SBC - RDProtect */
1441 case READ_12: /* SBC - RDProtect */
1442 case READ_16: /* SBC - RDProtect */
1443 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1444 case VERIFY: /* SBC - VRProtect */
1445 case VERIFY_16: /* SBC - VRProtect */
1446 case WRITE_VERIFY: /* SBC - VRProtect */
1447 case WRITE_VERIFY_12: /* SBC - VRProtect */
1450 cdb[1] &= 0x1f; /* clear logical unit number */
1455 static struct se_task *
1456 transport_generic_get_task(struct se_cmd *cmd,
1457 enum dma_data_direction data_direction)
1459 struct se_task *task;
1460 struct se_device *dev = cmd->se_dev;
1462 task = dev->transport->alloc_task(cmd->t_task_cdb);
1464 pr_err("Unable to allocate struct se_task\n");
1468 INIT_LIST_HEAD(&task->t_list);
1469 INIT_LIST_HEAD(&task->t_execute_list);
1470 INIT_LIST_HEAD(&task->t_state_list);
1471 init_completion(&task->task_stop_comp);
1472 task->task_se_cmd = cmd;
1473 task->task_data_direction = data_direction;
1478 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1481 * Used by fabric modules containing a local struct se_cmd within their
1482 * fabric dependent per I/O descriptor.
1484 void transport_init_se_cmd(
1486 struct target_core_fabric_ops *tfo,
1487 struct se_session *se_sess,
1491 unsigned char *sense_buffer)
1493 INIT_LIST_HEAD(&cmd->se_lun_node);
1494 INIT_LIST_HEAD(&cmd->se_delayed_node);
1495 INIT_LIST_HEAD(&cmd->se_qf_node);
1496 INIT_LIST_HEAD(&cmd->se_queue_node);
1497 INIT_LIST_HEAD(&cmd->se_cmd_list);
1498 INIT_LIST_HEAD(&cmd->t_task_list);
1499 init_completion(&cmd->transport_lun_fe_stop_comp);
1500 init_completion(&cmd->transport_lun_stop_comp);
1501 init_completion(&cmd->t_transport_stop_comp);
1502 init_completion(&cmd->cmd_wait_comp);
1503 spin_lock_init(&cmd->t_state_lock);
1504 atomic_set(&cmd->transport_dev_active, 1);
1507 cmd->se_sess = se_sess;
1508 cmd->data_length = data_length;
1509 cmd->data_direction = data_direction;
1510 cmd->sam_task_attr = task_attr;
1511 cmd->sense_buffer = sense_buffer;
1513 EXPORT_SYMBOL(transport_init_se_cmd);
1515 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1518 * Check if SAM Task Attribute emulation is enabled for this
1519 * struct se_device storage object
1521 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1524 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1525 pr_debug("SAM Task Attribute ACA"
1526 " emulation is not supported\n");
1530 * Used to determine when ORDERED commands should go from
1531 * Dormant to Active status.
1533 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1534 smp_mb__after_atomic_inc();
1535 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1536 cmd->se_ordered_id, cmd->sam_task_attr,
1537 cmd->se_dev->transport->name);
1541 /* transport_generic_allocate_tasks():
1543 * Called from fabric RX Thread.
1545 int transport_generic_allocate_tasks(
1551 transport_generic_prepare_cdb(cdb);
1553 * Ensure that the received CDB is less than the max (252 + 8) bytes
1554 * for VARIABLE_LENGTH_CMD
1556 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1557 pr_err("Received SCSI CDB with command_size: %d that"
1558 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1559 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1560 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1561 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1565 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1566 * allocate the additional extended CDB buffer now.. Otherwise
1567 * setup the pointer from __t_task_cdb to t_task_cdb.
1569 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1570 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1572 if (!cmd->t_task_cdb) {
1573 pr_err("Unable to allocate cmd->t_task_cdb"
1574 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1575 scsi_command_size(cdb),
1576 (unsigned long)sizeof(cmd->__t_task_cdb));
1577 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1578 cmd->scsi_sense_reason =
1579 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1583 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1585 * Copy the original CDB into cmd->
1587 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1589 * Setup the received CDB based on SCSI defined opcodes and
1590 * perform unit attention, persistent reservations and ALUA
1591 * checks for virtual device backends. The cmd->t_task_cdb
1592 * pointer is expected to be setup before we reach this point.
1594 ret = transport_generic_cmd_sequencer(cmd, cdb);
1598 * Check for SAM Task Attribute Emulation
1600 if (transport_check_alloc_task_attr(cmd) < 0) {
1601 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1602 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1605 spin_lock(&cmd->se_lun->lun_sep_lock);
1606 if (cmd->se_lun->lun_sep)
1607 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1608 spin_unlock(&cmd->se_lun->lun_sep_lock);
1611 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1614 * Used by fabric module frontends to queue tasks directly.
1615 * Many only be used from process context only
1617 int transport_handle_cdb_direct(
1624 pr_err("cmd->se_lun is NULL\n");
1627 if (in_interrupt()) {
1629 pr_err("transport_generic_handle_cdb cannot be called"
1630 " from interrupt context\n");
1634 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1635 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1636 * in existing usage to ensure that outstanding descriptors are handled
1637 * correctly during shutdown via transport_wait_for_tasks()
1639 * Also, we don't take cmd->t_state_lock here as we only expect
1640 * this to be called for initial descriptor submission.
1642 cmd->t_state = TRANSPORT_NEW_CMD;
1643 atomic_set(&cmd->t_transport_active, 1);
1645 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1646 * so follow TRANSPORT_NEW_CMD processing thread context usage
1647 * and call transport_generic_request_failure() if necessary..
1649 ret = transport_generic_new_cmd(cmd);
1651 transport_generic_request_failure(cmd);
1655 EXPORT_SYMBOL(transport_handle_cdb_direct);
1658 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1659 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1660 * complete setup in TCM process context w/ TFO->new_cmd_map().
1662 int transport_generic_handle_cdb_map(
1667 pr_err("cmd->se_lun is NULL\n");
1671 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
1674 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1676 /* transport_generic_handle_data():
1680 int transport_generic_handle_data(
1684 * For the software fabric case, then we assume the nexus is being
1685 * failed/shutdown when signals are pending from the kthread context
1686 * caller, so we return a failure. For the HW target mode case running
1687 * in interrupt code, the signal_pending() check is skipped.
1689 if (!in_interrupt() && signal_pending(current))
1692 * If the received CDB has aleady been ABORTED by the generic
1693 * target engine, we now call transport_check_aborted_status()
1694 * to queue any delated TASK_ABORTED status for the received CDB to the
1695 * fabric module as we are expecting no further incoming DATA OUT
1696 * sequences at this point.
1698 if (transport_check_aborted_status(cmd, 1) != 0)
1701 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
1704 EXPORT_SYMBOL(transport_generic_handle_data);
1706 /* transport_generic_handle_tmr():
1710 int transport_generic_handle_tmr(
1713 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
1716 EXPORT_SYMBOL(transport_generic_handle_tmr);
1719 * If the task is active, request it to be stopped and sleep until it
1722 bool target_stop_task(struct se_task *task, unsigned long *flags)
1724 struct se_cmd *cmd = task->task_se_cmd;
1725 bool was_active = false;
1727 if (task->task_flags & TF_ACTIVE) {
1728 task->task_flags |= TF_REQUEST_STOP;
1729 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1731 pr_debug("Task %p waiting to complete\n", task);
1732 wait_for_completion(&task->task_stop_comp);
1733 pr_debug("Task %p stopped successfully\n", task);
1735 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1736 atomic_dec(&cmd->t_task_cdbs_left);
1737 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1744 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1746 struct se_task *task, *task_tmp;
1747 unsigned long flags;
1750 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1751 cmd->se_tfo->get_task_tag(cmd));
1754 * No tasks remain in the execution queue
1756 spin_lock_irqsave(&cmd->t_state_lock, flags);
1757 list_for_each_entry_safe(task, task_tmp,
1758 &cmd->t_task_list, t_list) {
1759 pr_debug("Processing task %p\n", task);
1761 * If the struct se_task has not been sent and is not active,
1762 * remove the struct se_task from the execution queue.
1764 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
1765 spin_unlock_irqrestore(&cmd->t_state_lock,
1767 transport_remove_task_from_execute_queue(task,
1770 pr_debug("Task %p removed from execute queue\n", task);
1771 spin_lock_irqsave(&cmd->t_state_lock, flags);
1775 if (!target_stop_task(task, &flags)) {
1776 pr_debug("Task %p - did nothing\n", task);
1780 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1786 * Handle SAM-esque emulation for generic transport request failures.
1788 static void transport_generic_request_failure(struct se_cmd *cmd)
1792 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1793 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1794 cmd->t_task_cdb[0]);
1795 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
1796 cmd->se_tfo->get_cmd_state(cmd),
1797 cmd->t_state, cmd->scsi_sense_reason);
1798 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1799 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1800 " t_transport_active: %d t_transport_stop: %d"
1801 " t_transport_sent: %d\n", cmd->t_task_list_num,
1802 atomic_read(&cmd->t_task_cdbs_left),
1803 atomic_read(&cmd->t_task_cdbs_sent),
1804 atomic_read(&cmd->t_task_cdbs_ex_left),
1805 atomic_read(&cmd->t_transport_active),
1806 atomic_read(&cmd->t_transport_stop),
1807 atomic_read(&cmd->t_transport_sent));
1810 * For SAM Task Attribute emulation for failed struct se_cmd
1812 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1813 transport_complete_task_attr(cmd);
1815 switch (cmd->scsi_sense_reason) {
1816 case TCM_NON_EXISTENT_LUN:
1817 case TCM_UNSUPPORTED_SCSI_OPCODE:
1818 case TCM_INVALID_CDB_FIELD:
1819 case TCM_INVALID_PARAMETER_LIST:
1820 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1821 case TCM_UNKNOWN_MODE_PAGE:
1822 case TCM_WRITE_PROTECTED:
1823 case TCM_ADDRESS_OUT_OF_RANGE:
1824 case TCM_CHECK_CONDITION_ABORT_CMD:
1825 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1826 case TCM_CHECK_CONDITION_NOT_READY:
1828 case TCM_RESERVATION_CONFLICT:
1830 * No SENSE Data payload for this case, set SCSI Status
1831 * and queue the response to $FABRIC_MOD.
1833 * Uses linux/include/scsi/scsi.h SAM status codes defs
1835 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1837 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1838 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1841 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1844 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1845 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1846 cmd->orig_fe_lun, 0x2C,
1847 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1849 ret = cmd->se_tfo->queue_status(cmd);
1850 if (ret == -EAGAIN || ret == -ENOMEM)
1854 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1855 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
1856 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1860 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1861 * make the call to transport_send_check_condition_and_sense()
1862 * directly. Otherwise expect the fabric to make the call to
1863 * transport_send_check_condition_and_sense() after handling
1864 * possible unsoliticied write data payloads.
1866 ret = transport_send_check_condition_and_sense(cmd,
1867 cmd->scsi_sense_reason, 0);
1868 if (ret == -EAGAIN || ret == -ENOMEM)
1872 transport_lun_remove_cmd(cmd);
1873 if (!transport_cmd_check_stop_to_fabric(cmd))
1878 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1879 transport_handle_queue_full(cmd, cmd->se_dev);
1882 static inline u32 transport_lba_21(unsigned char *cdb)
1884 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1887 static inline u32 transport_lba_32(unsigned char *cdb)
1889 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1892 static inline unsigned long long transport_lba_64(unsigned char *cdb)
1894 unsigned int __v1, __v2;
1896 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1897 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1899 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1903 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1905 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1907 unsigned int __v1, __v2;
1909 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1910 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1912 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1915 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1917 unsigned long flags;
1919 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1920 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1921 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1924 static inline int transport_tcq_window_closed(struct se_device *dev)
1926 if (dev->dev_tcq_window_closed++ <
1927 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
1928 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
1930 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
1932 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
1937 * Called from Fabric Module context from transport_execute_tasks()
1939 * The return of this function determins if the tasks from struct se_cmd
1940 * get added to the execution queue in transport_execute_tasks(),
1941 * or are added to the delayed or ordered lists here.
1943 static inline int transport_execute_task_attr(struct se_cmd *cmd)
1945 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1948 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1949 * to allow the passed struct se_cmd list of tasks to the front of the list.
1951 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
1952 pr_debug("Added HEAD_OF_QUEUE for CDB:"
1953 " 0x%02x, se_ordered_id: %u\n",
1955 cmd->se_ordered_id);
1957 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
1958 atomic_inc(&cmd->se_dev->dev_ordered_sync);
1959 smp_mb__after_atomic_inc();
1961 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
1962 " list, se_ordered_id: %u\n",
1964 cmd->se_ordered_id);
1966 * Add ORDERED command to tail of execution queue if
1967 * no other older commands exist that need to be
1970 if (!atomic_read(&cmd->se_dev->simple_cmds))
1974 * For SIMPLE and UNTAGGED Task Attribute commands
1976 atomic_inc(&cmd->se_dev->simple_cmds);
1977 smp_mb__after_atomic_inc();
1980 * Otherwise if one or more outstanding ORDERED task attribute exist,
1981 * add the dormant task(s) built for the passed struct se_cmd to the
1982 * execution queue and become in Active state for this struct se_device.
1984 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
1986 * Otherwise, add cmd w/ tasks to delayed cmd queue that
1987 * will be drained upon completion of HEAD_OF_QUEUE task.
1989 spin_lock(&cmd->se_dev->delayed_cmd_lock);
1990 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
1991 list_add_tail(&cmd->se_delayed_node,
1992 &cmd->se_dev->delayed_cmd_list);
1993 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
1995 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1996 " delayed CMD list, se_ordered_id: %u\n",
1997 cmd->t_task_cdb[0], cmd->sam_task_attr,
1998 cmd->se_ordered_id);
2000 * Return zero to let transport_execute_tasks() know
2001 * not to add the delayed tasks to the execution list.
2006 * Otherwise, no ORDERED task attributes exist..
2012 * Called from fabric module context in transport_generic_new_cmd() and
2013 * transport_generic_process_write()
2015 static int transport_execute_tasks(struct se_cmd *cmd)
2019 if (se_dev_check_online(cmd->se_dev) != 0) {
2020 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2021 transport_generic_request_failure(cmd);
2026 * Call transport_cmd_check_stop() to see if a fabric exception
2027 * has occurred that prevents execution.
2029 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2031 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2032 * attribute for the tasks of the received struct se_cmd CDB
2034 add_tasks = transport_execute_task_attr(cmd);
2038 * This calls transport_add_tasks_from_cmd() to handle
2039 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2040 * (if enabled) in __transport_add_task_to_execute_queue() and
2041 * transport_add_task_check_sam_attr().
2043 transport_add_tasks_from_cmd(cmd);
2046 * Kick the execution queue for the cmd associated struct se_device
2050 __transport_execute_tasks(cmd->se_dev);
2055 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2056 * from struct se_device->execute_task_list and
2058 * Called from transport_processing_thread()
2060 static int __transport_execute_tasks(struct se_device *dev)
2063 struct se_cmd *cmd = NULL;
2064 struct se_task *task = NULL;
2065 unsigned long flags;
2068 * Check if there is enough room in the device and HBA queue to send
2069 * struct se_tasks to the selected transport.
2072 if (!atomic_read(&dev->depth_left))
2073 return transport_tcq_window_closed(dev);
2075 dev->dev_tcq_window_closed = 0;
2077 spin_lock_irq(&dev->execute_task_lock);
2078 if (list_empty(&dev->execute_task_list)) {
2079 spin_unlock_irq(&dev->execute_task_lock);
2082 task = list_first_entry(&dev->execute_task_list,
2083 struct se_task, t_execute_list);
2084 __transport_remove_task_from_execute_queue(task, dev);
2085 spin_unlock_irq(&dev->execute_task_lock);
2087 atomic_dec(&dev->depth_left);
2089 cmd = task->task_se_cmd;
2091 spin_lock_irqsave(&cmd->t_state_lock, flags);
2092 task->task_flags |= (TF_ACTIVE | TF_SENT);
2093 atomic_inc(&cmd->t_task_cdbs_sent);
2095 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2096 cmd->t_task_list_num)
2097 atomic_set(&cmd->t_transport_sent, 1);
2099 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2101 if (cmd->execute_task)
2102 error = cmd->execute_task(task);
2104 error = dev->transport->do_task(task);
2106 spin_lock_irqsave(&cmd->t_state_lock, flags);
2107 task->task_flags &= ~TF_ACTIVE;
2108 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2109 atomic_set(&cmd->t_transport_sent, 0);
2110 transport_stop_tasks_for_cmd(cmd);
2111 atomic_inc(&dev->depth_left);
2112 transport_generic_request_failure(cmd);
2120 static inline u32 transport_get_sectors_6(
2125 struct se_device *dev = cmd->se_dev;
2128 * Assume TYPE_DISK for non struct se_device objects.
2129 * Use 8-bit sector value.
2135 * Use 24-bit allocation length for TYPE_TAPE.
2137 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2138 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2141 * Everything else assume TYPE_DISK Sector CDB location.
2142 * Use 8-bit sector value. SBC-3 says:
2144 * A TRANSFER LENGTH field set to zero specifies that 256
2145 * logical blocks shall be written. Any other value
2146 * specifies the number of logical blocks that shall be
2150 return cdb[4] ? : 256;
2153 static inline u32 transport_get_sectors_10(
2158 struct se_device *dev = cmd->se_dev;
2161 * Assume TYPE_DISK for non struct se_device objects.
2162 * Use 16-bit sector value.
2168 * XXX_10 is not defined in SSC, throw an exception
2170 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2176 * Everything else assume TYPE_DISK Sector CDB location.
2177 * Use 16-bit sector value.
2180 return (u32)(cdb[7] << 8) + cdb[8];
2183 static inline u32 transport_get_sectors_12(
2188 struct se_device *dev = cmd->se_dev;
2191 * Assume TYPE_DISK for non struct se_device objects.
2192 * Use 32-bit sector value.
2198 * XXX_12 is not defined in SSC, throw an exception
2200 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2206 * Everything else assume TYPE_DISK Sector CDB location.
2207 * Use 32-bit sector value.
2210 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2213 static inline u32 transport_get_sectors_16(
2218 struct se_device *dev = cmd->se_dev;
2221 * Assume TYPE_DISK for non struct se_device objects.
2222 * Use 32-bit sector value.
2228 * Use 24-bit allocation length for TYPE_TAPE.
2230 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2231 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2234 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2235 (cdb[12] << 8) + cdb[13];
2239 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2241 static inline u32 transport_get_sectors_32(
2247 * Assume TYPE_DISK for non struct se_device objects.
2248 * Use 32-bit sector value.
2250 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2251 (cdb[30] << 8) + cdb[31];
2255 static inline u32 transport_get_size(
2260 struct se_device *dev = cmd->se_dev;
2262 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2263 if (cdb[1] & 1) { /* sectors */
2264 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2269 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2270 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2271 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2272 dev->transport->name);
2274 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2277 static void transport_xor_callback(struct se_cmd *cmd)
2279 unsigned char *buf, *addr;
2280 struct scatterlist *sg;
2281 unsigned int offset;
2285 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2287 * 1) read the specified logical block(s);
2288 * 2) transfer logical blocks from the data-out buffer;
2289 * 3) XOR the logical blocks transferred from the data-out buffer with
2290 * the logical blocks read, storing the resulting XOR data in a buffer;
2291 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2292 * blocks transferred from the data-out buffer; and
2293 * 5) transfer the resulting XOR data to the data-in buffer.
2295 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2297 pr_err("Unable to allocate xor_callback buf\n");
2301 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2302 * into the locally allocated *buf
2304 sg_copy_to_buffer(cmd->t_data_sg,
2310 * Now perform the XOR against the BIDI read memory located at
2311 * cmd->t_mem_bidi_list
2315 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2316 addr = kmap_atomic(sg_page(sg), KM_USER0);
2320 for (i = 0; i < sg->length; i++)
2321 *(addr + sg->offset + i) ^= *(buf + offset + i);
2323 offset += sg->length;
2324 kunmap_atomic(addr, KM_USER0);
2332 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2334 static int transport_get_sense_data(struct se_cmd *cmd)
2336 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2337 struct se_device *dev = cmd->se_dev;
2338 struct se_task *task = NULL, *task_tmp;
2339 unsigned long flags;
2342 WARN_ON(!cmd->se_lun);
2347 spin_lock_irqsave(&cmd->t_state_lock, flags);
2348 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2349 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2353 list_for_each_entry_safe(task, task_tmp,
2354 &cmd->t_task_list, t_list) {
2355 if (!task->task_sense)
2358 if (!dev->transport->get_sense_buffer) {
2359 pr_err("dev->transport->get_sense_buffer"
2364 sense_buffer = dev->transport->get_sense_buffer(task);
2365 if (!sense_buffer) {
2366 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
2367 " sense buffer for task with sense\n",
2368 cmd->se_tfo->get_task_tag(cmd), task);
2371 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2373 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2374 TRANSPORT_SENSE_BUFFER);
2376 memcpy(&buffer[offset], sense_buffer,
2377 TRANSPORT_SENSE_BUFFER);
2378 cmd->scsi_status = task->task_scsi_status;
2379 /* Automatically padded */
2380 cmd->scsi_sense_length =
2381 (TRANSPORT_SENSE_BUFFER + offset);
2383 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2385 dev->se_hba->hba_id, dev->transport->name,
2389 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2394 static inline long long transport_dev_end_lba(struct se_device *dev)
2396 return dev->transport->get_blocks(dev) + 1;
2399 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2401 struct se_device *dev = cmd->se_dev;
2404 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2407 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2409 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2410 pr_err("LBA: %llu Sectors: %u exceeds"
2411 " transport_dev_end_lba(): %llu\n",
2412 cmd->t_task_lba, sectors,
2413 transport_dev_end_lba(dev));
2420 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2423 * Determine if the received WRITE_SAME is used to for direct
2424 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2425 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2426 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2428 int passthrough = (dev->transport->transport_type ==
2429 TRANSPORT_PLUGIN_PHBA_PDEV);
2432 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2433 pr_err("WRITE_SAME PBDATA and LBDATA"
2434 " bits not supported for Block Discard"
2439 * Currently for the emulated case we only accept
2440 * tpws with the UNMAP=1 bit set.
2442 if (!(flags[0] & 0x08)) {
2443 pr_err("WRITE_SAME w/o UNMAP bit not"
2444 " supported for Block Discard Emulation\n");
2452 /* transport_generic_cmd_sequencer():
2454 * Generic Command Sequencer that should work for most DAS transport
2457 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2460 * FIXME: Need to support other SCSI OPCODES where as well.
2462 static int transport_generic_cmd_sequencer(
2466 struct se_device *dev = cmd->se_dev;
2467 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2468 int ret = 0, sector_ret = 0, passthrough;
2469 u32 sectors = 0, size = 0, pr_reg_type = 0;
2473 * Check for an existing UNIT ATTENTION condition
2475 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2476 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2477 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2481 * Check status of Asymmetric Logical Unit Assignment port
2483 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2486 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2487 * The ALUA additional sense code qualifier (ASCQ) is determined
2488 * by the ALUA primary or secondary access state..
2492 pr_debug("[%s]: ALUA TG Port not available,"
2493 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2494 cmd->se_tfo->get_fabric_name(), alua_ascq);
2496 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2497 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2498 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2501 goto out_invalid_cdb_field;
2504 * Check status for SPC-3 Persistent Reservations
2506 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2507 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2508 cmd, cdb, pr_reg_type) != 0) {
2509 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2510 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2511 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2512 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2516 * This means the CDB is allowed for the SCSI Initiator port
2517 * when said port is *NOT* holding the legacy SPC-2 or
2518 * SPC-3 Persistent Reservation.
2523 * If we operate in passthrough mode we skip most CDB emulation and
2524 * instead hand the commands down to the physical SCSI device.
2527 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2531 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2533 goto out_unsupported_cdb;
2534 size = transport_get_size(sectors, cdb, cmd);
2535 cmd->t_task_lba = transport_lba_21(cdb);
2536 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2539 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2541 goto out_unsupported_cdb;
2542 size = transport_get_size(sectors, cdb, cmd);
2543 cmd->t_task_lba = transport_lba_32(cdb);
2544 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2547 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2549 goto out_unsupported_cdb;
2550 size = transport_get_size(sectors, cdb, cmd);
2551 cmd->t_task_lba = transport_lba_32(cdb);
2552 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2555 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2557 goto out_unsupported_cdb;
2558 size = transport_get_size(sectors, cdb, cmd);
2559 cmd->t_task_lba = transport_lba_64(cdb);
2560 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2563 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2565 goto out_unsupported_cdb;
2566 size = transport_get_size(sectors, cdb, cmd);
2567 cmd->t_task_lba = transport_lba_21(cdb);
2568 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2571 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2573 goto out_unsupported_cdb;
2574 size = transport_get_size(sectors, cdb, cmd);
2575 cmd->t_task_lba = transport_lba_32(cdb);
2577 cmd->se_cmd_flags |= SCF_FUA;
2578 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2581 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2583 goto out_unsupported_cdb;
2584 size = transport_get_size(sectors, cdb, cmd);
2585 cmd->t_task_lba = transport_lba_32(cdb);
2587 cmd->se_cmd_flags |= SCF_FUA;
2588 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2591 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2593 goto out_unsupported_cdb;
2594 size = transport_get_size(sectors, cdb, cmd);
2595 cmd->t_task_lba = transport_lba_64(cdb);
2597 cmd->se_cmd_flags |= SCF_FUA;
2598 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2600 case XDWRITEREAD_10:
2601 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2602 !(cmd->se_cmd_flags & SCF_BIDI))
2603 goto out_invalid_cdb_field;
2604 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2606 goto out_unsupported_cdb;
2607 size = transport_get_size(sectors, cdb, cmd);
2608 cmd->t_task_lba = transport_lba_32(cdb);
2609 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2612 * Do now allow BIDI commands for passthrough mode.
2615 goto out_unsupported_cdb;
2618 * Setup BIDI XOR callback to be run after I/O completion.
2620 cmd->transport_complete_callback = &transport_xor_callback;
2622 cmd->se_cmd_flags |= SCF_FUA;
2624 case VARIABLE_LENGTH_CMD:
2625 service_action = get_unaligned_be16(&cdb[8]);
2626 switch (service_action) {
2627 case XDWRITEREAD_32:
2628 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2630 goto out_unsupported_cdb;
2631 size = transport_get_size(sectors, cdb, cmd);
2633 * Use WRITE_32 and READ_32 opcodes for the emulated
2634 * XDWRITE_READ_32 logic.
2636 cmd->t_task_lba = transport_lba_64_ext(cdb);
2637 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2640 * Do now allow BIDI commands for passthrough mode.
2643 goto out_unsupported_cdb;
2646 * Setup BIDI XOR callback to be run during after I/O
2649 cmd->transport_complete_callback = &transport_xor_callback;
2651 cmd->se_cmd_flags |= SCF_FUA;
2654 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2656 goto out_unsupported_cdb;
2659 size = transport_get_size(1, cdb, cmd);
2661 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2663 goto out_invalid_cdb_field;
2666 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2667 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2669 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2670 goto out_unsupported_cdb;
2672 cmd->execute_task = target_emulate_write_same;
2675 pr_err("VARIABLE_LENGTH_CMD service action"
2676 " 0x%04x not supported\n", service_action);
2677 goto out_unsupported_cdb;
2680 case MAINTENANCE_IN:
2681 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2682 /* MAINTENANCE_IN from SCC-2 */
2684 * Check for emulated MI_REPORT_TARGET_PGS.
2686 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2687 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2689 target_emulate_report_target_port_groups;
2691 size = (cdb[6] << 24) | (cdb[7] << 16) |
2692 (cdb[8] << 8) | cdb[9];
2694 /* GPCMD_SEND_KEY from multi media commands */
2695 size = (cdb[8] << 8) + cdb[9];
2697 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2701 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2703 case MODE_SELECT_10:
2704 size = (cdb[7] << 8) + cdb[8];
2705 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2709 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2711 cmd->execute_task = target_emulate_modesense;
2714 size = (cdb[7] << 8) + cdb[8];
2715 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2717 cmd->execute_task = target_emulate_modesense;
2719 case GPCMD_READ_BUFFER_CAPACITY:
2720 case GPCMD_SEND_OPC:
2723 size = (cdb[7] << 8) + cdb[8];
2724 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2726 case READ_BLOCK_LIMITS:
2727 size = READ_BLOCK_LEN;
2728 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2730 case GPCMD_GET_CONFIGURATION:
2731 case GPCMD_READ_FORMAT_CAPACITIES:
2732 case GPCMD_READ_DISC_INFO:
2733 case GPCMD_READ_TRACK_RZONE_INFO:
2734 size = (cdb[7] << 8) + cdb[8];
2735 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2737 case PERSISTENT_RESERVE_IN:
2738 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2739 cmd->execute_task = target_scsi3_emulate_pr_in;
2740 size = (cdb[7] << 8) + cdb[8];
2741 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2743 case PERSISTENT_RESERVE_OUT:
2744 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2745 cmd->execute_task = target_scsi3_emulate_pr_out;
2746 size = (cdb[7] << 8) + cdb[8];
2747 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2749 case GPCMD_MECHANISM_STATUS:
2750 case GPCMD_READ_DVD_STRUCTURE:
2751 size = (cdb[8] << 8) + cdb[9];
2752 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2755 size = READ_POSITION_LEN;
2756 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2758 case MAINTENANCE_OUT:
2759 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2760 /* MAINTENANCE_OUT from SCC-2
2762 * Check for emulated MO_SET_TARGET_PGS.
2764 if (cdb[1] == MO_SET_TARGET_PGS &&
2765 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2767 target_emulate_set_target_port_groups;
2770 size = (cdb[6] << 24) | (cdb[7] << 16) |
2771 (cdb[8] << 8) | cdb[9];
2773 /* GPCMD_REPORT_KEY from multi media commands */
2774 size = (cdb[8] << 8) + cdb[9];
2776 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2779 size = (cdb[3] << 8) + cdb[4];
2781 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2782 * See spc4r17 section 5.3
2784 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2785 cmd->sam_task_attr = MSG_HEAD_TAG;
2786 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2788 cmd->execute_task = target_emulate_inquiry;
2791 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2792 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2795 size = READ_CAP_LEN;
2796 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2798 cmd->execute_task = target_emulate_readcapacity;
2800 case READ_MEDIA_SERIAL_NUMBER:
2801 case SECURITY_PROTOCOL_IN:
2802 case SECURITY_PROTOCOL_OUT:
2803 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2804 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2806 case SERVICE_ACTION_IN:
2807 switch (cmd->t_task_cdb[1] & 0x1f) {
2808 case SAI_READ_CAPACITY_16:
2811 target_emulate_readcapacity_16;
2817 pr_err("Unsupported SA: 0x%02x\n",
2818 cmd->t_task_cdb[1] & 0x1f);
2819 goto out_unsupported_cdb;
2822 case ACCESS_CONTROL_IN:
2823 case ACCESS_CONTROL_OUT:
2825 case READ_ATTRIBUTE:
2826 case RECEIVE_COPY_RESULTS:
2827 case WRITE_ATTRIBUTE:
2828 size = (cdb[10] << 24) | (cdb[11] << 16) |
2829 (cdb[12] << 8) | cdb[13];
2830 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2832 case RECEIVE_DIAGNOSTIC:
2833 case SEND_DIAGNOSTIC:
2834 size = (cdb[3] << 8) | cdb[4];
2835 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2837 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2840 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2841 size = (2336 * sectors);
2842 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2847 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2851 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2853 cmd->execute_task = target_emulate_request_sense;
2855 case READ_ELEMENT_STATUS:
2856 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
2857 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2860 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2861 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2866 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2867 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2869 if (cdb[0] == RESERVE_10)
2870 size = (cdb[7] << 8) | cdb[8];
2872 size = cmd->data_length;
2875 * Setup the legacy emulated handler for SPC-2 and
2876 * >= SPC-3 compatible reservation handling (CRH=1)
2877 * Otherwise, we assume the underlying SCSI logic is
2878 * is running in SPC_PASSTHROUGH, and wants reservations
2879 * emulation disabled.
2881 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2882 cmd->execute_task = target_scsi2_reservation_reserve;
2883 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2888 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2889 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2891 if (cdb[0] == RELEASE_10)
2892 size = (cdb[7] << 8) | cdb[8];
2894 size = cmd->data_length;
2896 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2897 cmd->execute_task = target_scsi2_reservation_release;
2898 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2900 case SYNCHRONIZE_CACHE:
2901 case 0x91: /* SYNCHRONIZE_CACHE_16: */
2903 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2905 if (cdb[0] == SYNCHRONIZE_CACHE) {
2906 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2907 cmd->t_task_lba = transport_lba_32(cdb);
2909 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2910 cmd->t_task_lba = transport_lba_64(cdb);
2913 goto out_unsupported_cdb;
2915 size = transport_get_size(sectors, cdb, cmd);
2916 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2922 * Check to ensure that LBA + Range does not exceed past end of
2923 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
2925 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2926 if (transport_cmd_get_valid_sectors(cmd) < 0)
2927 goto out_invalid_cdb_field;
2929 cmd->execute_task = target_emulate_synchronize_cache;
2932 size = get_unaligned_be16(&cdb[7]);
2933 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2935 cmd->execute_task = target_emulate_unmap;
2938 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2940 goto out_unsupported_cdb;
2943 size = transport_get_size(1, cdb, cmd);
2945 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2946 goto out_invalid_cdb_field;
2949 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
2950 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2952 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2953 goto out_unsupported_cdb;
2955 cmd->execute_task = target_emulate_write_same;
2958 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2960 goto out_unsupported_cdb;
2963 size = transport_get_size(1, cdb, cmd);
2965 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2966 goto out_invalid_cdb_field;
2969 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2970 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2972 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2973 * of byte 1 bit 3 UNMAP instead of original reserved field
2975 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2976 goto out_unsupported_cdb;
2978 cmd->execute_task = target_emulate_write_same;
2980 case ALLOW_MEDIUM_REMOVAL:
2986 case TEST_UNIT_READY:
2988 case WRITE_FILEMARKS:
2989 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2991 cmd->execute_task = target_emulate_noop;
2993 case GPCMD_CLOSE_TRACK:
2994 case INITIALIZE_ELEMENT_STATUS:
2995 case GPCMD_LOAD_UNLOAD:
2996 case GPCMD_SET_SPEED:
2998 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3001 cmd->execute_task = target_report_luns;
3002 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3004 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3005 * See spc4r17 section 5.3
3007 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3008 cmd->sam_task_attr = MSG_HEAD_TAG;
3009 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3012 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3013 " 0x%02x, sending CHECK_CONDITION.\n",
3014 cmd->se_tfo->get_fabric_name(), cdb[0]);
3015 goto out_unsupported_cdb;
3018 if (size != cmd->data_length) {
3019 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3020 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3021 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3022 cmd->data_length, size, cdb[0]);
3024 cmd->cmd_spdtl = size;
3026 if (cmd->data_direction == DMA_TO_DEVICE) {
3027 pr_err("Rejecting underflow/overflow"
3029 goto out_invalid_cdb_field;
3032 * Reject READ_* or WRITE_* with overflow/underflow for
3033 * type SCF_SCSI_DATA_SG_IO_CDB.
3035 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3036 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3037 " CDB on non 512-byte sector setup subsystem"
3038 " plugin: %s\n", dev->transport->name);
3039 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3040 goto out_invalid_cdb_field;
3043 if (size > cmd->data_length) {
3044 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3045 cmd->residual_count = (size - cmd->data_length);
3047 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3048 cmd->residual_count = (cmd->data_length - size);
3050 cmd->data_length = size;
3053 /* reject any command that we don't have a handler for */
3054 if (!(passthrough || cmd->execute_task ||
3055 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3056 goto out_unsupported_cdb;
3058 transport_set_supported_SAM_opcode(cmd);
3061 out_unsupported_cdb:
3062 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3063 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3065 out_invalid_cdb_field:
3066 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3067 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3072 * Called from I/O completion to determine which dormant/delayed
3073 * and ordered cmds need to have their tasks added to the execution queue.
3075 static void transport_complete_task_attr(struct se_cmd *cmd)
3077 struct se_device *dev = cmd->se_dev;
3078 struct se_cmd *cmd_p, *cmd_tmp;
3079 int new_active_tasks = 0;
3081 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3082 atomic_dec(&dev->simple_cmds);
3083 smp_mb__after_atomic_dec();
3084 dev->dev_cur_ordered_id++;
3085 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3086 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3087 cmd->se_ordered_id);
3088 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3089 dev->dev_cur_ordered_id++;
3090 pr_debug("Incremented dev_cur_ordered_id: %u for"
3091 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3092 cmd->se_ordered_id);
3093 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3094 atomic_dec(&dev->dev_ordered_sync);
3095 smp_mb__after_atomic_dec();
3097 dev->dev_cur_ordered_id++;
3098 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3099 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3102 * Process all commands up to the last received
3103 * ORDERED task attribute which requires another blocking
3106 spin_lock(&dev->delayed_cmd_lock);
3107 list_for_each_entry_safe(cmd_p, cmd_tmp,
3108 &dev->delayed_cmd_list, se_delayed_node) {
3110 list_del(&cmd_p->se_delayed_node);
3111 spin_unlock(&dev->delayed_cmd_lock);
3113 pr_debug("Calling add_tasks() for"
3114 " cmd_p: 0x%02x Task Attr: 0x%02x"
3115 " Dormant -> Active, se_ordered_id: %u\n",
3116 cmd_p->t_task_cdb[0],
3117 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3119 transport_add_tasks_from_cmd(cmd_p);
3122 spin_lock(&dev->delayed_cmd_lock);
3123 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3126 spin_unlock(&dev->delayed_cmd_lock);
3128 * If new tasks have become active, wake up the transport thread
3129 * to do the processing of the Active tasks.
3131 if (new_active_tasks != 0)
3132 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3135 static void transport_complete_qf(struct se_cmd *cmd)
3139 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3140 transport_complete_task_attr(cmd);
3142 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3143 ret = cmd->se_tfo->queue_status(cmd);
3148 switch (cmd->data_direction) {
3149 case DMA_FROM_DEVICE:
3150 ret = cmd->se_tfo->queue_data_in(cmd);
3153 if (cmd->t_bidi_data_sg) {
3154 ret = cmd->se_tfo->queue_data_in(cmd);
3158 /* Fall through for DMA_TO_DEVICE */
3160 ret = cmd->se_tfo->queue_status(cmd);
3168 transport_handle_queue_full(cmd, cmd->se_dev);
3171 transport_lun_remove_cmd(cmd);
3172 transport_cmd_check_stop_to_fabric(cmd);
3175 static void transport_handle_queue_full(
3177 struct se_device *dev)
3179 spin_lock_irq(&dev->qf_cmd_lock);
3180 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3181 atomic_inc(&dev->dev_qf_count);
3182 smp_mb__after_atomic_inc();
3183 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3185 schedule_work(&cmd->se_dev->qf_work_queue);
3188 static void target_complete_ok_work(struct work_struct *work)
3190 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
3191 int reason = 0, ret;
3194 * Check if we need to move delayed/dormant tasks from cmds on the
3195 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3198 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3199 transport_complete_task_attr(cmd);
3201 * Check to schedule QUEUE_FULL work, or execute an existing
3202 * cmd->transport_qf_callback()
3204 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3205 schedule_work(&cmd->se_dev->qf_work_queue);
3208 * Check if we need to retrieve a sense buffer from
3209 * the struct se_cmd in question.
3211 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3212 if (transport_get_sense_data(cmd) < 0)
3213 reason = TCM_NON_EXISTENT_LUN;
3216 * Only set when an struct se_task->task_scsi_status returned
3217 * a non GOOD status.
3219 if (cmd->scsi_status) {
3220 ret = transport_send_check_condition_and_sense(
3222 if (ret == -EAGAIN || ret == -ENOMEM)
3225 transport_lun_remove_cmd(cmd);
3226 transport_cmd_check_stop_to_fabric(cmd);
3231 * Check for a callback, used by amongst other things
3232 * XDWRITE_READ_10 emulation.
3234 if (cmd->transport_complete_callback)
3235 cmd->transport_complete_callback(cmd);
3237 switch (cmd->data_direction) {
3238 case DMA_FROM_DEVICE:
3239 spin_lock(&cmd->se_lun->lun_sep_lock);
3240 if (cmd->se_lun->lun_sep) {
3241 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3244 spin_unlock(&cmd->se_lun->lun_sep_lock);
3246 ret = cmd->se_tfo->queue_data_in(cmd);
3247 if (ret == -EAGAIN || ret == -ENOMEM)
3251 spin_lock(&cmd->se_lun->lun_sep_lock);
3252 if (cmd->se_lun->lun_sep) {
3253 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3256 spin_unlock(&cmd->se_lun->lun_sep_lock);
3258 * Check if we need to send READ payload for BIDI-COMMAND
3260 if (cmd->t_bidi_data_sg) {
3261 spin_lock(&cmd->se_lun->lun_sep_lock);
3262 if (cmd->se_lun->lun_sep) {
3263 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3266 spin_unlock(&cmd->se_lun->lun_sep_lock);
3267 ret = cmd->se_tfo->queue_data_in(cmd);
3268 if (ret == -EAGAIN || ret == -ENOMEM)
3272 /* Fall through for DMA_TO_DEVICE */
3274 ret = cmd->se_tfo->queue_status(cmd);
3275 if (ret == -EAGAIN || ret == -ENOMEM)
3282 transport_lun_remove_cmd(cmd);
3283 transport_cmd_check_stop_to_fabric(cmd);
3287 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3288 " data_direction: %d\n", cmd, cmd->data_direction);
3289 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3290 transport_handle_queue_full(cmd, cmd->se_dev);
3293 static void transport_free_dev_tasks(struct se_cmd *cmd)
3295 struct se_task *task, *task_tmp;
3296 unsigned long flags;
3297 LIST_HEAD(dispose_list);
3299 spin_lock_irqsave(&cmd->t_state_lock, flags);
3300 list_for_each_entry_safe(task, task_tmp,
3301 &cmd->t_task_list, t_list) {
3302 if (!(task->task_flags & TF_ACTIVE))
3303 list_move_tail(&task->t_list, &dispose_list);
3305 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3307 while (!list_empty(&dispose_list)) {
3308 task = list_first_entry(&dispose_list, struct se_task, t_list);
3310 if (task->task_sg != cmd->t_data_sg &&
3311 task->task_sg != cmd->t_bidi_data_sg)
3312 kfree(task->task_sg);
3314 list_del(&task->t_list);
3316 cmd->se_dev->transport->free_task(task);
3320 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3322 struct scatterlist *sg;
3325 for_each_sg(sgl, sg, nents, count)
3326 __free_page(sg_page(sg));
3331 static inline void transport_free_pages(struct se_cmd *cmd)
3333 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3336 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3337 cmd->t_data_sg = NULL;
3338 cmd->t_data_nents = 0;
3340 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3341 cmd->t_bidi_data_sg = NULL;
3342 cmd->t_bidi_data_nents = 0;
3346 * transport_put_cmd - release a reference to a command
3347 * @cmd: command to release
3349 * This routine releases our reference to the command and frees it if possible.
3351 static void transport_put_cmd(struct se_cmd *cmd)
3353 unsigned long flags;
3356 spin_lock_irqsave(&cmd->t_state_lock, flags);
3357 if (atomic_read(&cmd->t_fe_count)) {
3358 if (!atomic_dec_and_test(&cmd->t_fe_count))
3362 if (atomic_read(&cmd->t_se_count)) {
3363 if (!atomic_dec_and_test(&cmd->t_se_count))
3367 if (atomic_read(&cmd->transport_dev_active)) {
3368 atomic_set(&cmd->transport_dev_active, 0);
3369 transport_all_task_dev_remove_state(cmd);
3372 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3374 if (free_tasks != 0)
3375 transport_free_dev_tasks(cmd);
3377 transport_free_pages(cmd);
3378 transport_release_cmd(cmd);
3381 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3385 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3386 * allocating in the core.
3387 * @cmd: Associated se_cmd descriptor
3388 * @mem: SGL style memory for TCM WRITE / READ
3389 * @sg_mem_num: Number of SGL elements
3390 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3391 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3393 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3396 int transport_generic_map_mem_to_cmd(
3398 struct scatterlist *sgl,
3400 struct scatterlist *sgl_bidi,
3403 if (!sgl || !sgl_count)
3406 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3407 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3409 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
3410 * scatterlists already have been set to follow what the fabric
3411 * passes for the original expected data transfer length.
3413 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
3414 pr_warn("Rejecting SCSI DATA overflow for fabric using"
3415 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
3416 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3417 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3421 cmd->t_data_sg = sgl;
3422 cmd->t_data_nents = sgl_count;
3424 if (sgl_bidi && sgl_bidi_count) {
3425 cmd->t_bidi_data_sg = sgl_bidi;
3426 cmd->t_bidi_data_nents = sgl_bidi_count;
3428 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3433 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3435 void *transport_kmap_data_sg(struct se_cmd *cmd)
3437 struct scatterlist *sg = cmd->t_data_sg;
3438 struct page **pages;
3443 * We need to take into account a possible offset here for fabrics like
3444 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3445 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3447 if (!cmd->t_data_nents)
3449 else if (cmd->t_data_nents == 1)
3450 return kmap(sg_page(sg)) + sg->offset;
3452 /* >1 page. use vmap */
3453 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
3457 /* convert sg[] to pages[] */
3458 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
3459 pages[i] = sg_page(sg);
3462 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
3464 if (!cmd->t_data_vmap)
3467 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
3469 EXPORT_SYMBOL(transport_kmap_data_sg);
3471 void transport_kunmap_data_sg(struct se_cmd *cmd)
3473 if (!cmd->t_data_nents)
3475 else if (cmd->t_data_nents == 1)
3476 kunmap(sg_page(cmd->t_data_sg));
3478 vunmap(cmd->t_data_vmap);
3479 cmd->t_data_vmap = NULL;
3481 EXPORT_SYMBOL(transport_kunmap_data_sg);
3484 transport_generic_get_mem(struct se_cmd *cmd)
3486 u32 length = cmd->data_length;
3491 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3492 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3493 if (!cmd->t_data_sg)
3496 cmd->t_data_nents = nents;
3497 sg_init_table(cmd->t_data_sg, nents);
3500 u32 page_len = min_t(u32, length, PAGE_SIZE);
3501 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3505 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3514 __free_page(sg_page(&cmd->t_data_sg[i]));
3516 kfree(cmd->t_data_sg);
3517 cmd->t_data_sg = NULL;
3521 /* Reduce sectors if they are too long for the device */
3522 static inline sector_t transport_limit_task_sectors(
3523 struct se_device *dev,
3524 unsigned long long lba,
3527 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
3529 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3530 if ((lba + sectors) > transport_dev_end_lba(dev))
3531 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
3538 * This function can be used by HW target mode drivers to create a linked
3539 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3540 * This is intended to be called during the completion path by TCM Core
3541 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3543 void transport_do_task_sg_chain(struct se_cmd *cmd)
3545 struct scatterlist *sg_first = NULL;
3546 struct scatterlist *sg_prev = NULL;
3547 int sg_prev_nents = 0;
3548 struct scatterlist *sg;
3549 struct se_task *task;
3550 u32 chained_nents = 0;
3553 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3556 * Walk the struct se_task list and setup scatterlist chains
3557 * for each contiguously allocated struct se_task->task_sg[].
3559 list_for_each_entry(task, &cmd->t_task_list, t_list) {
3564 sg_first = task->task_sg;
3565 chained_nents = task->task_sg_nents;
3567 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
3568 chained_nents += task->task_sg_nents;
3571 * For the padded tasks, use the extra SGL vector allocated
3572 * in transport_allocate_data_tasks() for the sg_prev_nents
3573 * offset into sg_chain() above.
3575 * We do not need the padding for the last task (or a single
3576 * task), but in that case we will never use the sg_prev_nents
3577 * value below which would be incorrect.
3579 sg_prev_nents = (task->task_sg_nents + 1);
3580 sg_prev = task->task_sg;
3583 * Setup the starting pointer and total t_tasks_sg_linked_no including
3584 * padding SGs for linking and to mark the end.
3586 cmd->t_tasks_sg_chained = sg_first;
3587 cmd->t_tasks_sg_chained_no = chained_nents;
3589 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
3590 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3591 cmd->t_tasks_sg_chained_no);
3593 for_each_sg(cmd->t_tasks_sg_chained, sg,
3594 cmd->t_tasks_sg_chained_no, i) {
3596 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
3597 i, sg, sg_page(sg), sg->length, sg->offset);
3598 if (sg_is_chain(sg))
3599 pr_debug("SG: %p sg_is_chain=1\n", sg);
3601 pr_debug("SG: %p sg_is_last=1\n", sg);
3604 EXPORT_SYMBOL(transport_do_task_sg_chain);
3607 * Break up cmd into chunks transport can handle
3610 transport_allocate_data_tasks(struct se_cmd *cmd,
3611 enum dma_data_direction data_direction,
3612 struct scatterlist *cmd_sg, unsigned int sgl_nents)
3614 struct se_device *dev = cmd->se_dev;
3616 unsigned long long lba;
3617 sector_t sectors, dev_max_sectors;
3620 if (transport_cmd_get_valid_sectors(cmd) < 0)
3623 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3624 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
3626 WARN_ON(cmd->data_length % sector_size);
3628 lba = cmd->t_task_lba;
3629 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
3630 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
3633 * If we need just a single task reuse the SG list in the command
3634 * and avoid a lot of work.
3636 if (task_count == 1) {
3637 struct se_task *task;
3638 unsigned long flags;
3640 task = transport_generic_get_task(cmd, data_direction);
3644 task->task_sg = cmd_sg;
3645 task->task_sg_nents = sgl_nents;
3647 task->task_lba = lba;
3648 task->task_sectors = sectors;
3649 task->task_size = task->task_sectors * sector_size;
3651 spin_lock_irqsave(&cmd->t_state_lock, flags);
3652 list_add_tail(&task->t_list, &cmd->t_task_list);
3653 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3658 for (i = 0; i < task_count; i++) {
3659 struct se_task *task;
3660 unsigned int task_size, task_sg_nents_padded;
3661 struct scatterlist *sg;
3662 unsigned long flags;
3665 task = transport_generic_get_task(cmd, data_direction);
3669 task->task_lba = lba;
3670 task->task_sectors = min(sectors, dev_max_sectors);
3671 task->task_size = task->task_sectors * sector_size;
3674 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3675 * in order to calculate the number per task SGL entries
3677 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
3679 * Check if the fabric module driver is requesting that all
3680 * struct se_task->task_sg[] be chained together.. If so,
3681 * then allocate an extra padding SG entry for linking and
3682 * marking the end of the chained SGL for every task except
3683 * the last one for (task_count > 1) operation, or skipping
3684 * the extra padding for the (task_count == 1) case.
3686 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3687 task_sg_nents_padded = (task->task_sg_nents + 1);
3689 task_sg_nents_padded = task->task_sg_nents;
3691 task->task_sg = kmalloc(sizeof(struct scatterlist) *
3692 task_sg_nents_padded, GFP_KERNEL);
3693 if (!task->task_sg) {
3694 cmd->se_dev->transport->free_task(task);
3698 sg_init_table(task->task_sg, task_sg_nents_padded);
3700 task_size = task->task_size;
3702 /* Build new sgl, only up to task_size */
3703 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
3704 if (cmd_sg->length > task_size)
3708 task_size -= cmd_sg->length;
3709 cmd_sg = sg_next(cmd_sg);
3712 lba += task->task_sectors;
3713 sectors -= task->task_sectors;
3715 spin_lock_irqsave(&cmd->t_state_lock, flags);
3716 list_add_tail(&task->t_list, &cmd->t_task_list);
3717 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3724 transport_allocate_control_task(struct se_cmd *cmd)
3726 struct se_task *task;
3727 unsigned long flags;
3729 /* Workaround for handling zero-length control CDBs */
3730 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3734 task = transport_generic_get_task(cmd, cmd->data_direction);
3738 task->task_sg = cmd->t_data_sg;
3739 task->task_size = cmd->data_length;
3740 task->task_sg_nents = cmd->t_data_nents;
3742 spin_lock_irqsave(&cmd->t_state_lock, flags);
3743 list_add_tail(&task->t_list, &cmd->t_task_list);
3744 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3746 /* Success! Return number of tasks allocated */
3751 * Allocate any required ressources to execute the command, and either place
3752 * it on the execution queue if possible. For writes we might not have the
3753 * payload yet, thus notify the fabric via a call to ->write_pending instead.
3755 int transport_generic_new_cmd(struct se_cmd *cmd)
3757 struct se_device *dev = cmd->se_dev;
3758 int task_cdbs, task_cdbs_bidi = 0;
3763 * Determine is the TCM fabric module has already allocated physical
3764 * memory, and is directly calling transport_generic_map_mem_to_cmd()
3767 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3769 ret = transport_generic_get_mem(cmd);
3775 * For BIDI command set up the read tasks first.
3777 if (cmd->t_bidi_data_sg &&
3778 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3779 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3781 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3782 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3783 cmd->t_bidi_data_nents);
3784 if (task_cdbs_bidi <= 0)
3787 atomic_inc(&cmd->t_fe_count);
3788 atomic_inc(&cmd->t_se_count);
3792 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3793 task_cdbs = transport_allocate_data_tasks(cmd,
3794 cmd->data_direction, cmd->t_data_sg,
3797 task_cdbs = transport_allocate_control_task(cmd);
3802 else if (!task_cdbs && (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)) {
3803 cmd->t_state = TRANSPORT_COMPLETE;
3804 atomic_set(&cmd->t_transport_active, 1);
3806 if (cmd->t_task_cdb[0] == REQUEST_SENSE) {
3807 u8 ua_asc = 0, ua_ascq = 0;
3809 core_scsi3_ua_clear_for_request_sense(cmd,
3813 INIT_WORK(&cmd->work, target_complete_ok_work);
3814 queue_work(target_completion_wq, &cmd->work);
3819 atomic_inc(&cmd->t_fe_count);
3820 atomic_inc(&cmd->t_se_count);
3823 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3824 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3825 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
3828 * For WRITEs, let the fabric know its buffer is ready..
3829 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3830 * will be added to the struct se_device execution queue after its WRITE
3831 * data has arrived. (ie: It gets handled by the transport processing
3832 * thread a second time)
3834 if (cmd->data_direction == DMA_TO_DEVICE) {
3835 transport_add_tasks_to_state_queue(cmd);
3836 return transport_generic_write_pending(cmd);
3839 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3840 * to the execution queue.
3842 transport_execute_tasks(cmd);
3846 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3847 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3850 EXPORT_SYMBOL(transport_generic_new_cmd);
3852 /* transport_generic_process_write():
3856 void transport_generic_process_write(struct se_cmd *cmd)
3858 transport_execute_tasks(cmd);
3860 EXPORT_SYMBOL(transport_generic_process_write);
3862 static void transport_write_pending_qf(struct se_cmd *cmd)
3866 ret = cmd->se_tfo->write_pending(cmd);
3867 if (ret == -EAGAIN || ret == -ENOMEM) {
3868 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3870 transport_handle_queue_full(cmd, cmd->se_dev);
3874 static int transport_generic_write_pending(struct se_cmd *cmd)
3876 unsigned long flags;
3879 spin_lock_irqsave(&cmd->t_state_lock, flags);
3880 cmd->t_state = TRANSPORT_WRITE_PENDING;
3881 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3884 * Clear the se_cmd for WRITE_PENDING status in order to set
3885 * cmd->t_transport_active=0 so that transport_generic_handle_data
3886 * can be called from HW target mode interrupt code. This is safe
3887 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
3888 * because the se_cmd->se_lun pointer is not being cleared.
3890 transport_cmd_check_stop(cmd, 1, 0);
3893 * Call the fabric write_pending function here to let the
3894 * frontend know that WRITE buffers are ready.
3896 ret = cmd->se_tfo->write_pending(cmd);
3897 if (ret == -EAGAIN || ret == -ENOMEM)
3905 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
3906 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
3907 transport_handle_queue_full(cmd, cmd->se_dev);
3912 * transport_release_cmd - free a command
3913 * @cmd: command to free
3915 * This routine unconditionally frees a command, and reference counting
3916 * or list removal must be done in the caller.
3918 void transport_release_cmd(struct se_cmd *cmd)
3920 BUG_ON(!cmd->se_tfo);
3922 if (cmd->se_tmr_req)
3923 core_tmr_release_req(cmd->se_tmr_req);
3924 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3925 kfree(cmd->t_task_cdb);
3927 * Check if target_wait_for_sess_cmds() is expecting to
3928 * release se_cmd directly here..
3930 if (cmd->check_release != 0 && cmd->se_tfo->check_release_cmd)
3931 if (cmd->se_tfo->check_release_cmd(cmd) != 0)
3934 cmd->se_tfo->release_cmd(cmd);
3936 EXPORT_SYMBOL(transport_release_cmd);
3938 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
3940 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3941 if (wait_for_tasks && cmd->se_tmr_req)
3942 transport_wait_for_tasks(cmd);
3944 transport_release_cmd(cmd);
3947 transport_wait_for_tasks(cmd);
3949 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3952 transport_lun_remove_cmd(cmd);
3954 transport_free_dev_tasks(cmd);
3956 transport_put_cmd(cmd);
3959 EXPORT_SYMBOL(transport_generic_free_cmd);
3961 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
3962 * @se_sess: session to reference
3963 * @se_cmd: command descriptor to add
3965 void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3967 unsigned long flags;
3969 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3970 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3971 se_cmd->check_release = 1;
3972 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3974 EXPORT_SYMBOL(target_get_sess_cmd);
3976 /* target_put_sess_cmd - Check for active I/O shutdown or list delete
3977 * @se_sess: session to reference
3978 * @se_cmd: command descriptor to drop
3980 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3982 unsigned long flags;
3984 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3985 if (list_empty(&se_cmd->se_cmd_list)) {
3986 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3991 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3992 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3993 complete(&se_cmd->cmd_wait_comp);
3996 list_del(&se_cmd->se_cmd_list);
3997 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4001 EXPORT_SYMBOL(target_put_sess_cmd);
4003 /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
4004 * @se_sess: session to split
4006 void target_splice_sess_cmd_list(struct se_session *se_sess)
4008 struct se_cmd *se_cmd;
4009 unsigned long flags;
4011 WARN_ON(!list_empty(&se_sess->sess_wait_list));
4012 INIT_LIST_HEAD(&se_sess->sess_wait_list);
4014 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
4015 se_sess->sess_tearing_down = 1;
4017 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
4019 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
4020 se_cmd->cmd_wait_set = 1;
4022 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
4024 EXPORT_SYMBOL(target_splice_sess_cmd_list);
4026 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
4027 * @se_sess: session to wait for active I/O
4028 * @wait_for_tasks: Make extra transport_wait_for_tasks call
4030 void target_wait_for_sess_cmds(
4031 struct se_session *se_sess,
4034 struct se_cmd *se_cmd, *tmp_cmd;
4037 list_for_each_entry_safe(se_cmd, tmp_cmd,
4038 &se_sess->sess_wait_list, se_cmd_list) {
4039 list_del(&se_cmd->se_cmd_list);
4041 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4042 " %d\n", se_cmd, se_cmd->t_state,
4043 se_cmd->se_tfo->get_cmd_state(se_cmd));
4045 if (wait_for_tasks) {
4046 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4047 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4048 se_cmd->se_tfo->get_cmd_state(se_cmd));
4050 rc = transport_wait_for_tasks(se_cmd);
4052 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4053 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4054 se_cmd->se_tfo->get_cmd_state(se_cmd));
4058 wait_for_completion(&se_cmd->cmd_wait_comp);
4059 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4060 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4061 se_cmd->se_tfo->get_cmd_state(se_cmd));
4064 se_cmd->se_tfo->release_cmd(se_cmd);
4067 EXPORT_SYMBOL(target_wait_for_sess_cmds);
4069 /* transport_lun_wait_for_tasks():
4071 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4072 * an struct se_lun to be successfully shutdown.
4074 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4076 unsigned long flags;
4079 * If the frontend has already requested this struct se_cmd to
4080 * be stopped, we can safely ignore this struct se_cmd.
4082 spin_lock_irqsave(&cmd->t_state_lock, flags);
4083 if (atomic_read(&cmd->t_transport_stop)) {
4084 atomic_set(&cmd->transport_lun_stop, 0);
4085 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4086 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
4087 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4088 transport_cmd_check_stop(cmd, 1, 0);
4091 atomic_set(&cmd->transport_lun_fe_stop, 1);
4092 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4094 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4096 ret = transport_stop_tasks_for_cmd(cmd);
4098 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4099 " %d\n", cmd, cmd->t_task_list_num, ret);
4101 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4102 cmd->se_tfo->get_task_tag(cmd));
4103 wait_for_completion(&cmd->transport_lun_stop_comp);
4104 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4105 cmd->se_tfo->get_task_tag(cmd));
4107 transport_remove_cmd_from_queue(cmd);
4112 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4114 struct se_cmd *cmd = NULL;
4115 unsigned long lun_flags, cmd_flags;
4117 * Do exception processing and return CHECK_CONDITION status to the
4120 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4121 while (!list_empty(&lun->lun_cmd_list)) {
4122 cmd = list_first_entry(&lun->lun_cmd_list,
4123 struct se_cmd, se_lun_node);
4124 list_del(&cmd->se_lun_node);
4126 atomic_set(&cmd->transport_lun_active, 0);
4128 * This will notify iscsi_target_transport.c:
4129 * transport_cmd_check_stop() that a LUN shutdown is in
4130 * progress for the iscsi_cmd_t.
4132 spin_lock(&cmd->t_state_lock);
4133 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4134 "_lun_stop for ITT: 0x%08x\n",
4135 cmd->se_lun->unpacked_lun,
4136 cmd->se_tfo->get_task_tag(cmd));
4137 atomic_set(&cmd->transport_lun_stop, 1);
4138 spin_unlock(&cmd->t_state_lock);
4140 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4143 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4144 cmd->se_tfo->get_task_tag(cmd),
4145 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4149 * If the Storage engine still owns the iscsi_cmd_t, determine
4150 * and/or stop its context.
4152 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4153 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4154 cmd->se_tfo->get_task_tag(cmd));
4156 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4157 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4161 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4162 "_wait_for_tasks(): SUCCESS\n",
4163 cmd->se_lun->unpacked_lun,
4164 cmd->se_tfo->get_task_tag(cmd));
4166 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4167 if (!atomic_read(&cmd->transport_dev_active)) {
4168 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4171 atomic_set(&cmd->transport_dev_active, 0);
4172 transport_all_task_dev_remove_state(cmd);
4173 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4175 transport_free_dev_tasks(cmd);
4177 * The Storage engine stopped this struct se_cmd before it was
4178 * send to the fabric frontend for delivery back to the
4179 * Initiator Node. Return this SCSI CDB back with an
4180 * CHECK_CONDITION status.
4183 transport_send_check_condition_and_sense(cmd,
4184 TCM_NON_EXISTENT_LUN, 0);
4186 * If the fabric frontend is waiting for this iscsi_cmd_t to
4187 * be released, notify the waiting thread now that LU has
4188 * finished accessing it.
4190 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4191 if (atomic_read(&cmd->transport_lun_fe_stop)) {
4192 pr_debug("SE_LUN[%d] - Detected FE stop for"
4193 " struct se_cmd: %p ITT: 0x%08x\n",
4195 cmd, cmd->se_tfo->get_task_tag(cmd));
4197 spin_unlock_irqrestore(&cmd->t_state_lock,
4199 transport_cmd_check_stop(cmd, 1, 0);
4200 complete(&cmd->transport_lun_fe_stop_comp);
4201 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4204 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4205 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4207 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4208 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4210 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4213 static int transport_clear_lun_thread(void *p)
4215 struct se_lun *lun = (struct se_lun *)p;
4217 __transport_clear_lun_from_sessions(lun);
4218 complete(&lun->lun_shutdown_comp);
4223 int transport_clear_lun_from_sessions(struct se_lun *lun)
4225 struct task_struct *kt;
4227 kt = kthread_run(transport_clear_lun_thread, lun,
4228 "tcm_cl_%u", lun->unpacked_lun);
4230 pr_err("Unable to start clear_lun thread\n");
4233 wait_for_completion(&lun->lun_shutdown_comp);
4239 * transport_wait_for_tasks - wait for completion to occur
4240 * @cmd: command to wait
4242 * Called from frontend fabric context to wait for storage engine
4243 * to pause and/or release frontend generated struct se_cmd.
4245 bool transport_wait_for_tasks(struct se_cmd *cmd)
4247 unsigned long flags;
4249 spin_lock_irqsave(&cmd->t_state_lock, flags);
4250 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4251 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4255 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4256 * has been set in transport_set_supported_SAM_opcode().
4258 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4259 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4263 * If we are already stopped due to an external event (ie: LUN shutdown)
4264 * sleep until the connection can have the passed struct se_cmd back.
4265 * The cmd->transport_lun_stopped_sem will be upped by
4266 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4267 * has completed its operation on the struct se_cmd.
4269 if (atomic_read(&cmd->transport_lun_stop)) {
4271 pr_debug("wait_for_tasks: Stopping"
4272 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4273 "_stop_comp); for ITT: 0x%08x\n",
4274 cmd->se_tfo->get_task_tag(cmd));
4276 * There is a special case for WRITES where a FE exception +
4277 * LUN shutdown means ConfigFS context is still sleeping on
4278 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4279 * We go ahead and up transport_lun_stop_comp just to be sure
4282 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4283 complete(&cmd->transport_lun_stop_comp);
4284 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4285 spin_lock_irqsave(&cmd->t_state_lock, flags);
4287 transport_all_task_dev_remove_state(cmd);
4289 * At this point, the frontend who was the originator of this
4290 * struct se_cmd, now owns the structure and can be released through
4291 * normal means below.
4293 pr_debug("wait_for_tasks: Stopped"
4294 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4295 "stop_comp); for ITT: 0x%08x\n",
4296 cmd->se_tfo->get_task_tag(cmd));
4298 atomic_set(&cmd->transport_lun_stop, 0);
4300 if (!atomic_read(&cmd->t_transport_active) ||
4301 atomic_read(&cmd->t_transport_aborted)) {
4302 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4306 atomic_set(&cmd->t_transport_stop, 1);
4308 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4309 " i_state: %d, t_state: %d, t_transport_stop = TRUE\n",
4310 cmd, cmd->se_tfo->get_task_tag(cmd),
4311 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4313 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4315 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4317 wait_for_completion(&cmd->t_transport_stop_comp);
4319 spin_lock_irqsave(&cmd->t_state_lock, flags);
4320 atomic_set(&cmd->t_transport_active, 0);
4321 atomic_set(&cmd->t_transport_stop, 0);
4323 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4324 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4325 cmd->se_tfo->get_task_tag(cmd));
4327 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4331 EXPORT_SYMBOL(transport_wait_for_tasks);
4333 static int transport_get_sense_codes(
4338 *asc = cmd->scsi_asc;
4339 *ascq = cmd->scsi_ascq;
4344 static int transport_set_sense_codes(
4349 cmd->scsi_asc = asc;
4350 cmd->scsi_ascq = ascq;
4355 int transport_send_check_condition_and_sense(
4360 unsigned char *buffer = cmd->sense_buffer;
4361 unsigned long flags;
4363 u8 asc = 0, ascq = 0;
4365 spin_lock_irqsave(&cmd->t_state_lock, flags);
4366 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4367 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4370 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4371 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4373 if (!reason && from_transport)
4376 if (!from_transport)
4377 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4379 * Data Segment and SenseLength of the fabric response PDU.
4381 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4382 * from include/scsi/scsi_cmnd.h
4384 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4385 TRANSPORT_SENSE_BUFFER);
4387 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4388 * SENSE KEY values from include/scsi/scsi.h
4391 case TCM_NON_EXISTENT_LUN:
4393 buffer[offset] = 0x70;
4394 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4395 /* ILLEGAL REQUEST */
4396 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4397 /* LOGICAL UNIT NOT SUPPORTED */
4398 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4400 case TCM_UNSUPPORTED_SCSI_OPCODE:
4401 case TCM_SECTOR_COUNT_TOO_MANY:
4403 buffer[offset] = 0x70;
4404 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4405 /* ILLEGAL REQUEST */
4406 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4407 /* INVALID COMMAND OPERATION CODE */
4408 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4410 case TCM_UNKNOWN_MODE_PAGE:
4412 buffer[offset] = 0x70;
4413 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4414 /* ILLEGAL REQUEST */
4415 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4416 /* INVALID FIELD IN CDB */
4417 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4419 case TCM_CHECK_CONDITION_ABORT_CMD:
4421 buffer[offset] = 0x70;
4422 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4423 /* ABORTED COMMAND */
4424 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4425 /* BUS DEVICE RESET FUNCTION OCCURRED */
4426 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4427 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4429 case TCM_INCORRECT_AMOUNT_OF_DATA:
4431 buffer[offset] = 0x70;
4432 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4433 /* ABORTED COMMAND */
4434 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4436 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4437 /* NOT ENOUGH UNSOLICITED DATA */
4438 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4440 case TCM_INVALID_CDB_FIELD:
4442 buffer[offset] = 0x70;
4443 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4444 /* ILLEGAL REQUEST */
4445 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4446 /* INVALID FIELD IN CDB */
4447 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4449 case TCM_INVALID_PARAMETER_LIST:
4451 buffer[offset] = 0x70;
4452 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4453 /* ILLEGAL REQUEST */
4454 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4455 /* INVALID FIELD IN PARAMETER LIST */
4456 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4458 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4460 buffer[offset] = 0x70;
4461 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4462 /* ABORTED COMMAND */
4463 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4465 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4466 /* UNEXPECTED_UNSOLICITED_DATA */
4467 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4469 case TCM_SERVICE_CRC_ERROR:
4471 buffer[offset] = 0x70;
4472 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4473 /* ABORTED COMMAND */
4474 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4475 /* PROTOCOL SERVICE CRC ERROR */
4476 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4478 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4480 case TCM_SNACK_REJECTED:
4482 buffer[offset] = 0x70;
4483 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4484 /* ABORTED COMMAND */
4485 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4487 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4488 /* FAILED RETRANSMISSION REQUEST */
4489 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4491 case TCM_WRITE_PROTECTED:
4493 buffer[offset] = 0x70;
4494 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4496 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4497 /* WRITE PROTECTED */
4498 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4500 case TCM_ADDRESS_OUT_OF_RANGE:
4502 buffer[offset] = 0x70;
4503 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4504 /* ILLEGAL REQUEST */
4505 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4506 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
4507 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x21;
4509 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4511 buffer[offset] = 0x70;
4512 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4513 /* UNIT ATTENTION */
4514 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4515 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4516 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4517 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4519 case TCM_CHECK_CONDITION_NOT_READY:
4521 buffer[offset] = 0x70;
4522 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4524 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4525 transport_get_sense_codes(cmd, &asc, &ascq);
4526 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4527 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4529 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4532 buffer[offset] = 0x70;
4533 buffer[offset+SPC_ADD_SENSE_LEN_OFFSET] = 10;
4534 /* ILLEGAL REQUEST */
4535 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4536 /* LOGICAL UNIT COMMUNICATION FAILURE */
4537 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4541 * This code uses linux/include/scsi/scsi.h SAM status codes!
4543 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4545 * Automatically padded, this value is encoded in the fabric's
4546 * data_length response PDU containing the SCSI defined sense data.
4548 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4551 return cmd->se_tfo->queue_status(cmd);
4553 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4555 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4559 if (atomic_read(&cmd->t_transport_aborted) != 0) {
4561 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4564 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4565 " status for CDB: 0x%02x ITT: 0x%08x\n",
4567 cmd->se_tfo->get_task_tag(cmd));
4569 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4570 cmd->se_tfo->queue_status(cmd);
4575 EXPORT_SYMBOL(transport_check_aborted_status);
4577 void transport_send_task_abort(struct se_cmd *cmd)
4579 unsigned long flags;
4581 spin_lock_irqsave(&cmd->t_state_lock, flags);
4582 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4583 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4586 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4589 * If there are still expected incoming fabric WRITEs, we wait
4590 * until until they have completed before sending a TASK_ABORTED
4591 * response. This response with TASK_ABORTED status will be
4592 * queued back to fabric module by transport_check_aborted_status().
4594 if (cmd->data_direction == DMA_TO_DEVICE) {
4595 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4596 atomic_inc(&cmd->t_transport_aborted);
4597 smp_mb__after_atomic_inc();
4600 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4602 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4603 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4604 cmd->se_tfo->get_task_tag(cmd));
4606 cmd->se_tfo->queue_status(cmd);
4609 /* transport_generic_do_tmr():
4613 int transport_generic_do_tmr(struct se_cmd *cmd)
4615 struct se_device *dev = cmd->se_dev;
4616 struct se_tmr_req *tmr = cmd->se_tmr_req;
4619 switch (tmr->function) {
4620 case TMR_ABORT_TASK:
4621 tmr->response = TMR_FUNCTION_REJECTED;
4623 case TMR_ABORT_TASK_SET:
4625 case TMR_CLEAR_TASK_SET:
4626 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4629 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4630 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4631 TMR_FUNCTION_REJECTED;
4633 case TMR_TARGET_WARM_RESET:
4634 tmr->response = TMR_FUNCTION_REJECTED;
4636 case TMR_TARGET_COLD_RESET:
4637 tmr->response = TMR_FUNCTION_REJECTED;
4640 pr_err("Uknown TMR function: 0x%02x.\n",
4642 tmr->response = TMR_FUNCTION_REJECTED;
4646 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4647 cmd->se_tfo->queue_tm_rsp(cmd);
4649 transport_cmd_check_stop_to_fabric(cmd);
4653 /* transport_processing_thread():
4657 static int transport_processing_thread(void *param)
4661 struct se_device *dev = (struct se_device *) param;
4663 while (!kthread_should_stop()) {
4664 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4665 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4666 kthread_should_stop());
4671 __transport_execute_tasks(dev);
4673 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4677 switch (cmd->t_state) {
4678 case TRANSPORT_NEW_CMD:
4681 case TRANSPORT_NEW_CMD_MAP:
4682 if (!cmd->se_tfo->new_cmd_map) {
4683 pr_err("cmd->se_tfo->new_cmd_map is"
4684 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4687 ret = cmd->se_tfo->new_cmd_map(cmd);
4689 transport_generic_request_failure(cmd);
4692 ret = transport_generic_new_cmd(cmd);
4694 transport_generic_request_failure(cmd);
4698 case TRANSPORT_PROCESS_WRITE:
4699 transport_generic_process_write(cmd);
4701 case TRANSPORT_PROCESS_TMR:
4702 transport_generic_do_tmr(cmd);
4704 case TRANSPORT_COMPLETE_QF_WP:
4705 transport_write_pending_qf(cmd);
4707 case TRANSPORT_COMPLETE_QF_OK:
4708 transport_complete_qf(cmd);
4711 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4712 "i_state: %d on SE LUN: %u\n",
4714 cmd->se_tfo->get_task_tag(cmd),
4715 cmd->se_tfo->get_cmd_state(cmd),
4716 cmd->se_lun->unpacked_lun);
4724 WARN_ON(!list_empty(&dev->state_task_list));
4725 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4726 dev->process_thread = NULL;