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 <asm/unaligned.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_tcq.h>
46 #include <target/target_core_base.h>
47 #include <target/target_core_device.h>
48 #include <target/target_core_tmr.h>
49 #include <target/target_core_tpg.h>
50 #include <target/target_core_transport.h>
51 #include <target/target_core_fabric_ops.h>
52 #include <target/target_core_configfs.h>
54 #include "target_core_alua.h"
55 #include "target_core_hba.h"
56 #include "target_core_pr.h"
57 #include "target_core_ua.h"
59 static int sub_api_initialized;
61 static struct kmem_cache *se_cmd_cache;
62 static struct kmem_cache *se_sess_cache;
63 struct kmem_cache *se_tmr_req_cache;
64 struct kmem_cache *se_ua_cache;
65 struct kmem_cache *t10_pr_reg_cache;
66 struct kmem_cache *t10_alua_lu_gp_cache;
67 struct kmem_cache *t10_alua_lu_gp_mem_cache;
68 struct kmem_cache *t10_alua_tg_pt_gp_cache;
69 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
71 static int transport_generic_write_pending(struct se_cmd *);
72 static int transport_processing_thread(void *param);
73 static int __transport_execute_tasks(struct se_device *dev);
74 static void transport_complete_task_attr(struct se_cmd *cmd);
75 static void transport_handle_queue_full(struct se_cmd *cmd,
76 struct se_device *dev);
77 static void transport_direct_request_timeout(struct se_cmd *cmd);
78 static void transport_free_dev_tasks(struct se_cmd *cmd);
79 static u32 transport_allocate_tasks(struct se_cmd *cmd,
80 unsigned long long starting_lba,
81 enum dma_data_direction data_direction,
82 struct scatterlist *sgl, unsigned int nents);
83 static int transport_generic_get_mem(struct se_cmd *cmd);
84 static void transport_put_cmd(struct se_cmd *cmd);
85 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
86 struct se_queue_obj *qobj);
87 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
88 static void transport_stop_all_task_timers(struct se_cmd *cmd);
90 int init_se_kmem_caches(void)
92 se_cmd_cache = kmem_cache_create("se_cmd_cache",
93 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
95 pr_err("kmem_cache_create for struct se_cmd failed\n");
98 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
99 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
101 if (!se_tmr_req_cache) {
102 pr_err("kmem_cache_create() for struct se_tmr_req"
106 se_sess_cache = kmem_cache_create("se_sess_cache",
107 sizeof(struct se_session), __alignof__(struct se_session),
109 if (!se_sess_cache) {
110 pr_err("kmem_cache_create() for struct se_session"
114 se_ua_cache = kmem_cache_create("se_ua_cache",
115 sizeof(struct se_ua), __alignof__(struct se_ua),
118 pr_err("kmem_cache_create() for struct se_ua failed\n");
121 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
122 sizeof(struct t10_pr_registration),
123 __alignof__(struct t10_pr_registration), 0, NULL);
124 if (!t10_pr_reg_cache) {
125 pr_err("kmem_cache_create() for struct t10_pr_registration"
129 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
130 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
132 if (!t10_alua_lu_gp_cache) {
133 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
137 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
138 sizeof(struct t10_alua_lu_gp_member),
139 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
140 if (!t10_alua_lu_gp_mem_cache) {
141 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
145 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
146 sizeof(struct t10_alua_tg_pt_gp),
147 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
148 if (!t10_alua_tg_pt_gp_cache) {
149 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
153 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
154 "t10_alua_tg_pt_gp_mem_cache",
155 sizeof(struct t10_alua_tg_pt_gp_member),
156 __alignof__(struct t10_alua_tg_pt_gp_member),
158 if (!t10_alua_tg_pt_gp_mem_cache) {
159 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
167 kmem_cache_destroy(se_cmd_cache);
168 if (se_tmr_req_cache)
169 kmem_cache_destroy(se_tmr_req_cache);
171 kmem_cache_destroy(se_sess_cache);
173 kmem_cache_destroy(se_ua_cache);
174 if (t10_pr_reg_cache)
175 kmem_cache_destroy(t10_pr_reg_cache);
176 if (t10_alua_lu_gp_cache)
177 kmem_cache_destroy(t10_alua_lu_gp_cache);
178 if (t10_alua_lu_gp_mem_cache)
179 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
180 if (t10_alua_tg_pt_gp_cache)
181 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
182 if (t10_alua_tg_pt_gp_mem_cache)
183 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
187 void release_se_kmem_caches(void)
189 kmem_cache_destroy(se_cmd_cache);
190 kmem_cache_destroy(se_tmr_req_cache);
191 kmem_cache_destroy(se_sess_cache);
192 kmem_cache_destroy(se_ua_cache);
193 kmem_cache_destroy(t10_pr_reg_cache);
194 kmem_cache_destroy(t10_alua_lu_gp_cache);
195 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
196 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
197 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
200 /* This code ensures unique mib indexes are handed out. */
201 static DEFINE_SPINLOCK(scsi_mib_index_lock);
202 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
205 * Allocate a new row index for the entry type specified
207 u32 scsi_get_new_index(scsi_index_t type)
211 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
213 spin_lock(&scsi_mib_index_lock);
214 new_index = ++scsi_mib_index[type];
215 spin_unlock(&scsi_mib_index_lock);
220 void transport_init_queue_obj(struct se_queue_obj *qobj)
222 atomic_set(&qobj->queue_cnt, 0);
223 INIT_LIST_HEAD(&qobj->qobj_list);
224 init_waitqueue_head(&qobj->thread_wq);
225 spin_lock_init(&qobj->cmd_queue_lock);
227 EXPORT_SYMBOL(transport_init_queue_obj);
229 static int transport_subsystem_reqmods(void)
233 ret = request_module("target_core_iblock");
235 pr_err("Unable to load target_core_iblock\n");
237 ret = request_module("target_core_file");
239 pr_err("Unable to load target_core_file\n");
241 ret = request_module("target_core_pscsi");
243 pr_err("Unable to load target_core_pscsi\n");
245 ret = request_module("target_core_stgt");
247 pr_err("Unable to load target_core_stgt\n");
252 int transport_subsystem_check_init(void)
256 if (sub_api_initialized)
259 * Request the loading of known TCM subsystem plugins..
261 ret = transport_subsystem_reqmods();
265 sub_api_initialized = 1;
269 struct se_session *transport_init_session(void)
271 struct se_session *se_sess;
273 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
275 pr_err("Unable to allocate struct se_session from"
277 return ERR_PTR(-ENOMEM);
279 INIT_LIST_HEAD(&se_sess->sess_list);
280 INIT_LIST_HEAD(&se_sess->sess_acl_list);
284 EXPORT_SYMBOL(transport_init_session);
287 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
289 void __transport_register_session(
290 struct se_portal_group *se_tpg,
291 struct se_node_acl *se_nacl,
292 struct se_session *se_sess,
293 void *fabric_sess_ptr)
295 unsigned char buf[PR_REG_ISID_LEN];
297 se_sess->se_tpg = se_tpg;
298 se_sess->fabric_sess_ptr = fabric_sess_ptr;
300 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
302 * Only set for struct se_session's that will actually be moving I/O.
303 * eg: *NOT* discovery sessions.
307 * If the fabric module supports an ISID based TransportID,
308 * save this value in binary from the fabric I_T Nexus now.
310 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
311 memset(&buf[0], 0, PR_REG_ISID_LEN);
312 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
313 &buf[0], PR_REG_ISID_LEN);
314 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
316 spin_lock_irq(&se_nacl->nacl_sess_lock);
318 * The se_nacl->nacl_sess pointer will be set to the
319 * last active I_T Nexus for each struct se_node_acl.
321 se_nacl->nacl_sess = se_sess;
323 list_add_tail(&se_sess->sess_acl_list,
324 &se_nacl->acl_sess_list);
325 spin_unlock_irq(&se_nacl->nacl_sess_lock);
327 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
329 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
330 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
332 EXPORT_SYMBOL(__transport_register_session);
334 void transport_register_session(
335 struct se_portal_group *se_tpg,
336 struct se_node_acl *se_nacl,
337 struct se_session *se_sess,
338 void *fabric_sess_ptr)
340 spin_lock_bh(&se_tpg->session_lock);
341 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
342 spin_unlock_bh(&se_tpg->session_lock);
344 EXPORT_SYMBOL(transport_register_session);
346 void transport_deregister_session_configfs(struct se_session *se_sess)
348 struct se_node_acl *se_nacl;
351 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
353 se_nacl = se_sess->se_node_acl;
355 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
356 list_del(&se_sess->sess_acl_list);
358 * If the session list is empty, then clear the pointer.
359 * Otherwise, set the struct se_session pointer from the tail
360 * element of the per struct se_node_acl active session list.
362 if (list_empty(&se_nacl->acl_sess_list))
363 se_nacl->nacl_sess = NULL;
365 se_nacl->nacl_sess = container_of(
366 se_nacl->acl_sess_list.prev,
367 struct se_session, sess_acl_list);
369 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
372 EXPORT_SYMBOL(transport_deregister_session_configfs);
374 void transport_free_session(struct se_session *se_sess)
376 kmem_cache_free(se_sess_cache, se_sess);
378 EXPORT_SYMBOL(transport_free_session);
380 void transport_deregister_session(struct se_session *se_sess)
382 struct se_portal_group *se_tpg = se_sess->se_tpg;
383 struct se_node_acl *se_nacl;
387 transport_free_session(se_sess);
391 spin_lock_irqsave(&se_tpg->session_lock, flags);
392 list_del(&se_sess->sess_list);
393 se_sess->se_tpg = NULL;
394 se_sess->fabric_sess_ptr = NULL;
395 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
398 * Determine if we need to do extra work for this initiator node's
399 * struct se_node_acl if it had been previously dynamically generated.
401 se_nacl = se_sess->se_node_acl;
403 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
404 if (se_nacl->dynamic_node_acl) {
405 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
407 list_del(&se_nacl->acl_list);
408 se_tpg->num_node_acls--;
409 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
411 core_tpg_wait_for_nacl_pr_ref(se_nacl);
412 core_free_device_list_for_node(se_nacl, se_tpg);
413 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
415 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
418 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
421 transport_free_session(se_sess);
423 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
424 se_tpg->se_tpg_tfo->get_fabric_name());
426 EXPORT_SYMBOL(transport_deregister_session);
429 * Called with cmd->t_state_lock held.
431 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
433 struct se_device *dev = cmd->se_dev;
434 struct se_task *task;
440 list_for_each_entry(task, &cmd->t_task_list, t_list) {
441 if (task->task_flags & TF_ACTIVE)
444 if (!atomic_read(&task->task_state_active))
447 spin_lock_irqsave(&dev->execute_task_lock, flags);
448 list_del(&task->t_state_list);
449 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
450 cmd->se_tfo->get_task_tag(cmd), dev, task);
451 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
453 atomic_set(&task->task_state_active, 0);
454 atomic_dec(&cmd->t_task_cdbs_ex_left);
458 /* transport_cmd_check_stop():
460 * 'transport_off = 1' determines if t_transport_active should be cleared.
461 * 'transport_off = 2' determines if task_dev_state should be removed.
463 * A non-zero u8 t_state sets cmd->t_state.
464 * Returns 1 when command is stopped, else 0.
466 static int transport_cmd_check_stop(
473 spin_lock_irqsave(&cmd->t_state_lock, flags);
475 * Determine if IOCTL context caller in requesting the stopping of this
476 * command for LUN shutdown purposes.
478 if (atomic_read(&cmd->transport_lun_stop)) {
479 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
480 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
481 cmd->se_tfo->get_task_tag(cmd));
483 cmd->deferred_t_state = cmd->t_state;
484 cmd->t_state = TRANSPORT_DEFERRED_CMD;
485 atomic_set(&cmd->t_transport_active, 0);
486 if (transport_off == 2)
487 transport_all_task_dev_remove_state(cmd);
488 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
490 complete(&cmd->transport_lun_stop_comp);
494 * Determine if frontend context caller is requesting the stopping of
495 * this command for frontend exceptions.
497 if (atomic_read(&cmd->t_transport_stop)) {
498 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
499 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
500 cmd->se_tfo->get_task_tag(cmd));
502 cmd->deferred_t_state = cmd->t_state;
503 cmd->t_state = TRANSPORT_DEFERRED_CMD;
504 if (transport_off == 2)
505 transport_all_task_dev_remove_state(cmd);
508 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
511 if (transport_off == 2)
513 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
515 complete(&cmd->t_transport_stop_comp);
519 atomic_set(&cmd->t_transport_active, 0);
520 if (transport_off == 2) {
521 transport_all_task_dev_remove_state(cmd);
523 * Clear struct se_cmd->se_lun before the transport_off == 2
524 * handoff to fabric module.
528 * Some fabric modules like tcm_loop can release
529 * their internally allocated I/O reference now and
532 if (cmd->se_tfo->check_stop_free != NULL) {
533 spin_unlock_irqrestore(
534 &cmd->t_state_lock, flags);
536 cmd->se_tfo->check_stop_free(cmd);
540 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
544 cmd->t_state = t_state;
545 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
550 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
552 return transport_cmd_check_stop(cmd, 2, 0);
555 static void transport_lun_remove_cmd(struct se_cmd *cmd)
557 struct se_lun *lun = cmd->se_lun;
563 spin_lock_irqsave(&cmd->t_state_lock, flags);
564 if (!atomic_read(&cmd->transport_dev_active)) {
565 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
568 atomic_set(&cmd->transport_dev_active, 0);
569 transport_all_task_dev_remove_state(cmd);
570 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
574 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
575 if (atomic_read(&cmd->transport_lun_active)) {
576 list_del(&cmd->se_lun_node);
577 atomic_set(&cmd->transport_lun_active, 0);
579 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
580 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
583 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
586 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
588 if (!cmd->se_tmr_req)
589 transport_lun_remove_cmd(cmd);
591 if (transport_cmd_check_stop_to_fabric(cmd))
594 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
595 transport_put_cmd(cmd);
599 static void transport_add_cmd_to_queue(
603 struct se_device *dev = cmd->se_dev;
604 struct se_queue_obj *qobj = &dev->dev_queue_obj;
608 spin_lock_irqsave(&cmd->t_state_lock, flags);
609 cmd->t_state = t_state;
610 atomic_set(&cmd->t_transport_active, 1);
611 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
614 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
616 /* If the cmd is already on the list, remove it before we add it */
617 if (!list_empty(&cmd->se_queue_node))
618 list_del(&cmd->se_queue_node);
620 atomic_inc(&qobj->queue_cnt);
622 if (cmd->se_cmd_flags & SCF_EMULATE_QUEUE_FULL) {
623 cmd->se_cmd_flags &= ~SCF_EMULATE_QUEUE_FULL;
624 list_add(&cmd->se_queue_node, &qobj->qobj_list);
626 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
627 atomic_set(&cmd->t_transport_queue_active, 1);
628 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
630 wake_up_interruptible(&qobj->thread_wq);
633 static struct se_cmd *
634 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
639 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
640 if (list_empty(&qobj->qobj_list)) {
641 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
644 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
646 atomic_set(&cmd->t_transport_queue_active, 0);
648 list_del_init(&cmd->se_queue_node);
649 atomic_dec(&qobj->queue_cnt);
650 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
655 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
656 struct se_queue_obj *qobj)
660 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
661 if (!atomic_read(&cmd->t_transport_queue_active)) {
662 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
665 atomic_set(&cmd->t_transport_queue_active, 0);
666 atomic_dec(&qobj->queue_cnt);
667 list_del_init(&cmd->se_queue_node);
668 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
670 if (atomic_read(&cmd->t_transport_queue_active)) {
671 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
672 cmd->se_tfo->get_task_tag(cmd),
673 atomic_read(&cmd->t_transport_queue_active));
678 * Completion function used by TCM subsystem plugins (such as FILEIO)
679 * for queueing up response from struct se_subsystem_api->do_task()
681 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
683 struct se_task *task = list_entry(cmd->t_task_list.next,
684 struct se_task, t_list);
687 cmd->scsi_status = SAM_STAT_GOOD;
688 task->task_scsi_status = GOOD;
690 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
691 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
692 task->task_se_cmd->transport_error_status =
693 PYX_TRANSPORT_ILLEGAL_REQUEST;
696 transport_complete_task(task, good);
698 EXPORT_SYMBOL(transport_complete_sync_cache);
700 /* transport_complete_task():
702 * Called from interrupt and non interrupt context depending
703 * on the transport plugin.
705 void transport_complete_task(struct se_task *task, int success)
707 struct se_cmd *cmd = task->task_se_cmd;
708 struct se_device *dev = cmd->se_dev;
712 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
713 cmd->t_task_cdb[0], dev);
716 atomic_inc(&dev->depth_left);
718 spin_lock_irqsave(&cmd->t_state_lock, flags);
719 task->task_flags &= ~TF_ACTIVE;
722 * See if any sense data exists, if so set the TASK_SENSE flag.
723 * Also check for any other post completion work that needs to be
724 * done by the plugins.
726 if (dev && dev->transport->transport_complete) {
727 if (dev->transport->transport_complete(task) != 0) {
728 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
729 task->task_sense = 1;
735 * See if we are waiting for outstanding struct se_task
736 * to complete for an exception condition
738 if (task->task_flags & TF_REQUEST_STOP) {
740 * Decrement cmd->t_se_count if this task had
741 * previously thrown its timeout exception handler.
743 if (task->task_flags & TF_TIMEOUT) {
744 atomic_dec(&cmd->t_se_count);
745 task->task_flags &= ~TF_TIMEOUT;
747 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
749 complete(&task->task_stop_comp);
753 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
754 * left counter to determine when the struct se_cmd is ready to be queued to
755 * the processing thread.
757 if (task->task_flags & TF_TIMEOUT) {
758 if (!atomic_dec_and_test(
759 &cmd->t_task_cdbs_timeout_left)) {
760 spin_unlock_irqrestore(&cmd->t_state_lock,
764 t_state = TRANSPORT_COMPLETE_TIMEOUT;
765 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
767 transport_add_cmd_to_queue(cmd, t_state);
770 atomic_dec(&cmd->t_task_cdbs_timeout_left);
773 * Decrement the outstanding t_task_cdbs_left count. The last
774 * struct se_task from struct se_cmd will complete itself into the
775 * device queue depending upon int success.
777 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
779 cmd->t_tasks_failed = 1;
781 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
785 if (!success || cmd->t_tasks_failed) {
786 t_state = TRANSPORT_COMPLETE_FAILURE;
787 if (!task->task_error_status) {
788 task->task_error_status =
789 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
790 cmd->transport_error_status =
791 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
794 atomic_set(&cmd->t_transport_complete, 1);
795 t_state = TRANSPORT_COMPLETE_OK;
797 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
799 transport_add_cmd_to_queue(cmd, t_state);
801 EXPORT_SYMBOL(transport_complete_task);
804 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
805 * struct se_task list are ready to be added to the active execution list
808 * Called with se_dev_t->execute_task_lock called.
810 static inline int transport_add_task_check_sam_attr(
811 struct se_task *task,
812 struct se_task *task_prev,
813 struct se_device *dev)
816 * No SAM Task attribute emulation enabled, add to tail of
819 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
820 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
824 * HEAD_OF_QUEUE attribute for received CDB, which means
825 * the first task that is associated with a struct se_cmd goes to
826 * head of the struct se_device->execute_task_list, and task_prev
827 * after that for each subsequent task
829 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
830 list_add(&task->t_execute_list,
831 (task_prev != NULL) ?
832 &task_prev->t_execute_list :
833 &dev->execute_task_list);
835 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
836 " in execution queue\n",
837 task->task_se_cmd->t_task_cdb[0]);
841 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
842 * transitioned from Dermant -> Active state, and are added to the end
843 * of the struct se_device->execute_task_list
845 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
849 /* __transport_add_task_to_execute_queue():
851 * Called with se_dev_t->execute_task_lock called.
853 static void __transport_add_task_to_execute_queue(
854 struct se_task *task,
855 struct se_task *task_prev,
856 struct se_device *dev)
860 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
861 atomic_inc(&dev->execute_tasks);
863 if (atomic_read(&task->task_state_active))
866 * Determine if this task needs to go to HEAD_OF_QUEUE for the
867 * state list as well. Running with SAM Task Attribute emulation
868 * will always return head_of_queue == 0 here
871 list_add(&task->t_state_list, (task_prev) ?
872 &task_prev->t_state_list :
873 &dev->state_task_list);
875 list_add_tail(&task->t_state_list, &dev->state_task_list);
877 atomic_set(&task->task_state_active, 1);
879 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
880 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
884 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
886 struct se_device *dev = cmd->se_dev;
887 struct se_task *task;
890 spin_lock_irqsave(&cmd->t_state_lock, flags);
891 list_for_each_entry(task, &cmd->t_task_list, t_list) {
892 if (atomic_read(&task->task_state_active))
895 spin_lock(&dev->execute_task_lock);
896 list_add_tail(&task->t_state_list, &dev->state_task_list);
897 atomic_set(&task->task_state_active, 1);
899 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
900 task->task_se_cmd->se_tfo->get_task_tag(
901 task->task_se_cmd), task, dev);
903 spin_unlock(&dev->execute_task_lock);
905 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
908 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
910 struct se_device *dev = cmd->se_dev;
911 struct se_task *task, *task_prev = NULL;
914 spin_lock_irqsave(&dev->execute_task_lock, flags);
915 list_for_each_entry(task, &cmd->t_task_list, t_list) {
916 if (!list_empty(&task->t_execute_list))
919 * __transport_add_task_to_execute_queue() handles the
920 * SAM Task Attribute emulation if enabled
922 __transport_add_task_to_execute_queue(task, task_prev, dev);
925 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
928 void __transport_remove_task_from_execute_queue(struct se_task *task,
929 struct se_device *dev)
931 list_del_init(&task->t_execute_list);
932 atomic_dec(&dev->execute_tasks);
935 void transport_remove_task_from_execute_queue(
936 struct se_task *task,
937 struct se_device *dev)
941 if (WARN_ON(list_empty(&task->t_execute_list)))
944 spin_lock_irqsave(&dev->execute_task_lock, flags);
945 __transport_remove_task_from_execute_queue(task, dev);
946 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
950 * Handle QUEUE_FULL / -EAGAIN status
953 static void target_qf_do_work(struct work_struct *work)
955 struct se_device *dev = container_of(work, struct se_device,
957 LIST_HEAD(qf_cmd_list);
958 struct se_cmd *cmd, *cmd_tmp;
960 spin_lock_irq(&dev->qf_cmd_lock);
961 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
962 spin_unlock_irq(&dev->qf_cmd_lock);
964 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
965 list_del(&cmd->se_qf_node);
966 atomic_dec(&dev->dev_qf_count);
967 smp_mb__after_atomic_dec();
969 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
970 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
971 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
972 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
975 * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd
976 * has been added to head of queue
978 transport_add_cmd_to_queue(cmd, cmd->t_state);
982 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
984 switch (cmd->data_direction) {
987 case DMA_FROM_DEVICE:
991 case DMA_BIDIRECTIONAL:
1000 void transport_dump_dev_state(
1001 struct se_device *dev,
1005 *bl += sprintf(b + *bl, "Status: ");
1006 switch (dev->dev_status) {
1007 case TRANSPORT_DEVICE_ACTIVATED:
1008 *bl += sprintf(b + *bl, "ACTIVATED");
1010 case TRANSPORT_DEVICE_DEACTIVATED:
1011 *bl += sprintf(b + *bl, "DEACTIVATED");
1013 case TRANSPORT_DEVICE_SHUTDOWN:
1014 *bl += sprintf(b + *bl, "SHUTDOWN");
1016 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
1017 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
1018 *bl += sprintf(b + *bl, "OFFLINE");
1021 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
1025 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
1026 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
1028 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
1029 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
1030 *bl += sprintf(b + *bl, " ");
1033 void transport_dump_vpd_proto_id(
1034 struct t10_vpd *vpd,
1035 unsigned char *p_buf,
1038 unsigned char buf[VPD_TMP_BUF_SIZE];
1041 memset(buf, 0, VPD_TMP_BUF_SIZE);
1042 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1044 switch (vpd->protocol_identifier) {
1046 sprintf(buf+len, "Fibre Channel\n");
1049 sprintf(buf+len, "Parallel SCSI\n");
1052 sprintf(buf+len, "SSA\n");
1055 sprintf(buf+len, "IEEE 1394\n");
1058 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1062 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1065 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1068 sprintf(buf+len, "Automation/Drive Interface Transport"
1072 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1075 sprintf(buf+len, "Unknown 0x%02x\n",
1076 vpd->protocol_identifier);
1081 strncpy(p_buf, buf, p_buf_len);
1083 pr_debug("%s", buf);
1087 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1090 * Check if the Protocol Identifier Valid (PIV) bit is set..
1092 * from spc3r23.pdf section 7.5.1
1094 if (page_83[1] & 0x80) {
1095 vpd->protocol_identifier = (page_83[0] & 0xf0);
1096 vpd->protocol_identifier_set = 1;
1097 transport_dump_vpd_proto_id(vpd, NULL, 0);
1100 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1102 int transport_dump_vpd_assoc(
1103 struct t10_vpd *vpd,
1104 unsigned char *p_buf,
1107 unsigned char buf[VPD_TMP_BUF_SIZE];
1111 memset(buf, 0, VPD_TMP_BUF_SIZE);
1112 len = sprintf(buf, "T10 VPD Identifier Association: ");
1114 switch (vpd->association) {
1116 sprintf(buf+len, "addressed logical unit\n");
1119 sprintf(buf+len, "target port\n");
1122 sprintf(buf+len, "SCSI target device\n");
1125 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1131 strncpy(p_buf, buf, p_buf_len);
1133 pr_debug("%s", buf);
1138 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1141 * The VPD identification association..
1143 * from spc3r23.pdf Section 7.6.3.1 Table 297
1145 vpd->association = (page_83[1] & 0x30);
1146 return transport_dump_vpd_assoc(vpd, NULL, 0);
1148 EXPORT_SYMBOL(transport_set_vpd_assoc);
1150 int transport_dump_vpd_ident_type(
1151 struct t10_vpd *vpd,
1152 unsigned char *p_buf,
1155 unsigned char buf[VPD_TMP_BUF_SIZE];
1159 memset(buf, 0, VPD_TMP_BUF_SIZE);
1160 len = sprintf(buf, "T10 VPD Identifier Type: ");
1162 switch (vpd->device_identifier_type) {
1164 sprintf(buf+len, "Vendor specific\n");
1167 sprintf(buf+len, "T10 Vendor ID based\n");
1170 sprintf(buf+len, "EUI-64 based\n");
1173 sprintf(buf+len, "NAA\n");
1176 sprintf(buf+len, "Relative target port identifier\n");
1179 sprintf(buf+len, "SCSI name string\n");
1182 sprintf(buf+len, "Unsupported: 0x%02x\n",
1183 vpd->device_identifier_type);
1189 if (p_buf_len < strlen(buf)+1)
1191 strncpy(p_buf, buf, p_buf_len);
1193 pr_debug("%s", buf);
1199 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1202 * The VPD identifier type..
1204 * from spc3r23.pdf Section 7.6.3.1 Table 298
1206 vpd->device_identifier_type = (page_83[1] & 0x0f);
1207 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1209 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1211 int transport_dump_vpd_ident(
1212 struct t10_vpd *vpd,
1213 unsigned char *p_buf,
1216 unsigned char buf[VPD_TMP_BUF_SIZE];
1219 memset(buf, 0, VPD_TMP_BUF_SIZE);
1221 switch (vpd->device_identifier_code_set) {
1222 case 0x01: /* Binary */
1223 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1224 &vpd->device_identifier[0]);
1226 case 0x02: /* ASCII */
1227 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1228 &vpd->device_identifier[0]);
1230 case 0x03: /* UTF-8 */
1231 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1232 &vpd->device_identifier[0]);
1235 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1236 " 0x%02x", vpd->device_identifier_code_set);
1242 strncpy(p_buf, buf, p_buf_len);
1244 pr_debug("%s", buf);
1250 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1252 static const char hex_str[] = "0123456789abcdef";
1253 int j = 0, i = 4; /* offset to start of the identifer */
1256 * The VPD Code Set (encoding)
1258 * from spc3r23.pdf Section 7.6.3.1 Table 296
1260 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1261 switch (vpd->device_identifier_code_set) {
1262 case 0x01: /* Binary */
1263 vpd->device_identifier[j++] =
1264 hex_str[vpd->device_identifier_type];
1265 while (i < (4 + page_83[3])) {
1266 vpd->device_identifier[j++] =
1267 hex_str[(page_83[i] & 0xf0) >> 4];
1268 vpd->device_identifier[j++] =
1269 hex_str[page_83[i] & 0x0f];
1273 case 0x02: /* ASCII */
1274 case 0x03: /* UTF-8 */
1275 while (i < (4 + page_83[3]))
1276 vpd->device_identifier[j++] = page_83[i++];
1282 return transport_dump_vpd_ident(vpd, NULL, 0);
1284 EXPORT_SYMBOL(transport_set_vpd_ident);
1286 static void core_setup_task_attr_emulation(struct se_device *dev)
1289 * If this device is from Target_Core_Mod/pSCSI, disable the
1290 * SAM Task Attribute emulation.
1292 * This is currently not available in upsream Linux/SCSI Target
1293 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1295 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1296 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1300 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1301 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1302 " device\n", dev->transport->name,
1303 dev->transport->get_device_rev(dev));
1306 static void scsi_dump_inquiry(struct se_device *dev)
1308 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1311 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1313 pr_debug(" Vendor: ");
1314 for (i = 0; i < 8; i++)
1315 if (wwn->vendor[i] >= 0x20)
1316 pr_debug("%c", wwn->vendor[i]);
1320 pr_debug(" Model: ");
1321 for (i = 0; i < 16; i++)
1322 if (wwn->model[i] >= 0x20)
1323 pr_debug("%c", wwn->model[i]);
1327 pr_debug(" Revision: ");
1328 for (i = 0; i < 4; i++)
1329 if (wwn->revision[i] >= 0x20)
1330 pr_debug("%c", wwn->revision[i]);
1336 device_type = dev->transport->get_device_type(dev);
1337 pr_debug(" Type: %s ", scsi_device_type(device_type));
1338 pr_debug(" ANSI SCSI revision: %02x\n",
1339 dev->transport->get_device_rev(dev));
1342 struct se_device *transport_add_device_to_core_hba(
1344 struct se_subsystem_api *transport,
1345 struct se_subsystem_dev *se_dev,
1347 void *transport_dev,
1348 struct se_dev_limits *dev_limits,
1349 const char *inquiry_prod,
1350 const char *inquiry_rev)
1353 struct se_device *dev;
1355 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1357 pr_err("Unable to allocate memory for se_dev_t\n");
1361 transport_init_queue_obj(&dev->dev_queue_obj);
1362 dev->dev_flags = device_flags;
1363 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1364 dev->dev_ptr = transport_dev;
1366 dev->se_sub_dev = se_dev;
1367 dev->transport = transport;
1368 atomic_set(&dev->active_cmds, 0);
1369 INIT_LIST_HEAD(&dev->dev_list);
1370 INIT_LIST_HEAD(&dev->dev_sep_list);
1371 INIT_LIST_HEAD(&dev->dev_tmr_list);
1372 INIT_LIST_HEAD(&dev->execute_task_list);
1373 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1374 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1375 INIT_LIST_HEAD(&dev->state_task_list);
1376 INIT_LIST_HEAD(&dev->qf_cmd_list);
1377 spin_lock_init(&dev->execute_task_lock);
1378 spin_lock_init(&dev->delayed_cmd_lock);
1379 spin_lock_init(&dev->ordered_cmd_lock);
1380 spin_lock_init(&dev->state_task_lock);
1381 spin_lock_init(&dev->dev_alua_lock);
1382 spin_lock_init(&dev->dev_reservation_lock);
1383 spin_lock_init(&dev->dev_status_lock);
1384 spin_lock_init(&dev->dev_status_thr_lock);
1385 spin_lock_init(&dev->se_port_lock);
1386 spin_lock_init(&dev->se_tmr_lock);
1387 spin_lock_init(&dev->qf_cmd_lock);
1389 dev->queue_depth = dev_limits->queue_depth;
1390 atomic_set(&dev->depth_left, dev->queue_depth);
1391 atomic_set(&dev->dev_ordered_id, 0);
1393 se_dev_set_default_attribs(dev, dev_limits);
1395 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1396 dev->creation_time = get_jiffies_64();
1397 spin_lock_init(&dev->stats_lock);
1399 spin_lock(&hba->device_lock);
1400 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1402 spin_unlock(&hba->device_lock);
1404 * Setup the SAM Task Attribute emulation for struct se_device
1406 core_setup_task_attr_emulation(dev);
1408 * Force PR and ALUA passthrough emulation with internal object use.
1410 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1412 * Setup the Reservations infrastructure for struct se_device
1414 core_setup_reservations(dev, force_pt);
1416 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1418 if (core_setup_alua(dev, force_pt) < 0)
1422 * Startup the struct se_device processing thread
1424 dev->process_thread = kthread_run(transport_processing_thread, dev,
1425 "LIO_%s", dev->transport->name);
1426 if (IS_ERR(dev->process_thread)) {
1427 pr_err("Unable to create kthread: LIO_%s\n",
1428 dev->transport->name);
1432 * Setup work_queue for QUEUE_FULL
1434 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1436 * Preload the initial INQUIRY const values if we are doing
1437 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1438 * passthrough because this is being provided by the backend LLD.
1439 * This is required so that transport_get_inquiry() copies these
1440 * originals once back into DEV_T10_WWN(dev) for the virtual device
1443 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1444 if (!inquiry_prod || !inquiry_rev) {
1445 pr_err("All non TCM/pSCSI plugins require"
1446 " INQUIRY consts\n");
1450 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1451 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1452 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1454 scsi_dump_inquiry(dev);
1458 kthread_stop(dev->process_thread);
1460 spin_lock(&hba->device_lock);
1461 list_del(&dev->dev_list);
1463 spin_unlock(&hba->device_lock);
1465 se_release_vpd_for_dev(dev);
1471 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1473 /* transport_generic_prepare_cdb():
1475 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1476 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1477 * The point of this is since we are mapping iSCSI LUNs to
1478 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1479 * devices and HBAs for a loop.
1481 static inline void transport_generic_prepare_cdb(
1485 case READ_10: /* SBC - RDProtect */
1486 case READ_12: /* SBC - RDProtect */
1487 case READ_16: /* SBC - RDProtect */
1488 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1489 case VERIFY: /* SBC - VRProtect */
1490 case VERIFY_16: /* SBC - VRProtect */
1491 case WRITE_VERIFY: /* SBC - VRProtect */
1492 case WRITE_VERIFY_12: /* SBC - VRProtect */
1495 cdb[1] &= 0x1f; /* clear logical unit number */
1500 static struct se_task *
1501 transport_generic_get_task(struct se_cmd *cmd,
1502 enum dma_data_direction data_direction)
1504 struct se_task *task;
1505 struct se_device *dev = cmd->se_dev;
1507 task = dev->transport->alloc_task(cmd->t_task_cdb);
1509 pr_err("Unable to allocate struct se_task\n");
1513 INIT_LIST_HEAD(&task->t_list);
1514 INIT_LIST_HEAD(&task->t_execute_list);
1515 INIT_LIST_HEAD(&task->t_state_list);
1516 init_completion(&task->task_stop_comp);
1517 task->task_se_cmd = cmd;
1518 task->task_data_direction = data_direction;
1523 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1526 * Used by fabric modules containing a local struct se_cmd within their
1527 * fabric dependent per I/O descriptor.
1529 void transport_init_se_cmd(
1531 struct target_core_fabric_ops *tfo,
1532 struct se_session *se_sess,
1536 unsigned char *sense_buffer)
1538 INIT_LIST_HEAD(&cmd->se_lun_node);
1539 INIT_LIST_HEAD(&cmd->se_delayed_node);
1540 INIT_LIST_HEAD(&cmd->se_ordered_node);
1541 INIT_LIST_HEAD(&cmd->se_qf_node);
1542 INIT_LIST_HEAD(&cmd->se_queue_node);
1544 INIT_LIST_HEAD(&cmd->t_task_list);
1545 init_completion(&cmd->transport_lun_fe_stop_comp);
1546 init_completion(&cmd->transport_lun_stop_comp);
1547 init_completion(&cmd->t_transport_stop_comp);
1548 spin_lock_init(&cmd->t_state_lock);
1549 atomic_set(&cmd->transport_dev_active, 1);
1552 cmd->se_sess = se_sess;
1553 cmd->data_length = data_length;
1554 cmd->data_direction = data_direction;
1555 cmd->sam_task_attr = task_attr;
1556 cmd->sense_buffer = sense_buffer;
1558 EXPORT_SYMBOL(transport_init_se_cmd);
1560 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1563 * Check if SAM Task Attribute emulation is enabled for this
1564 * struct se_device storage object
1566 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1569 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1570 pr_debug("SAM Task Attribute ACA"
1571 " emulation is not supported\n");
1575 * Used to determine when ORDERED commands should go from
1576 * Dormant to Active status.
1578 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1579 smp_mb__after_atomic_inc();
1580 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1581 cmd->se_ordered_id, cmd->sam_task_attr,
1582 cmd->se_dev->transport->name);
1586 /* transport_generic_allocate_tasks():
1588 * Called from fabric RX Thread.
1590 int transport_generic_allocate_tasks(
1596 transport_generic_prepare_cdb(cdb);
1598 * Ensure that the received CDB is less than the max (252 + 8) bytes
1599 * for VARIABLE_LENGTH_CMD
1601 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1602 pr_err("Received SCSI CDB with command_size: %d that"
1603 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1604 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1608 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1609 * allocate the additional extended CDB buffer now.. Otherwise
1610 * setup the pointer from __t_task_cdb to t_task_cdb.
1612 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1613 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1615 if (!cmd->t_task_cdb) {
1616 pr_err("Unable to allocate cmd->t_task_cdb"
1617 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1618 scsi_command_size(cdb),
1619 (unsigned long)sizeof(cmd->__t_task_cdb));
1623 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1625 * Copy the original CDB into cmd->
1627 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1629 * Setup the received CDB based on SCSI defined opcodes and
1630 * perform unit attention, persistent reservations and ALUA
1631 * checks for virtual device backends. The cmd->t_task_cdb
1632 * pointer is expected to be setup before we reach this point.
1634 ret = transport_generic_cmd_sequencer(cmd, cdb);
1638 * Check for SAM Task Attribute Emulation
1640 if (transport_check_alloc_task_attr(cmd) < 0) {
1641 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1642 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1645 spin_lock(&cmd->se_lun->lun_sep_lock);
1646 if (cmd->se_lun->lun_sep)
1647 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1648 spin_unlock(&cmd->se_lun->lun_sep_lock);
1651 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1653 static void transport_generic_request_failure(struct se_cmd *,
1654 struct se_device *, int, int);
1656 * Used by fabric module frontends to queue tasks directly.
1657 * Many only be used from process context only
1659 int transport_handle_cdb_direct(
1666 pr_err("cmd->se_lun is NULL\n");
1669 if (in_interrupt()) {
1671 pr_err("transport_generic_handle_cdb cannot be called"
1672 " from interrupt context\n");
1676 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1677 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1678 * in existing usage to ensure that outstanding descriptors are handled
1679 * correctly during shutdown via transport_wait_for_tasks()
1681 * Also, we don't take cmd->t_state_lock here as we only expect
1682 * this to be called for initial descriptor submission.
1684 cmd->t_state = TRANSPORT_NEW_CMD;
1685 atomic_set(&cmd->t_transport_active, 1);
1687 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1688 * so follow TRANSPORT_NEW_CMD processing thread context usage
1689 * and call transport_generic_request_failure() if necessary..
1691 ret = transport_generic_new_cmd(cmd);
1695 cmd->transport_error_status = ret;
1696 transport_generic_request_failure(cmd, NULL, 0,
1697 (cmd->data_direction != DMA_TO_DEVICE));
1701 EXPORT_SYMBOL(transport_handle_cdb_direct);
1704 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1705 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1706 * complete setup in TCM process context w/ TFO->new_cmd_map().
1708 int transport_generic_handle_cdb_map(
1713 pr_err("cmd->se_lun is NULL\n");
1717 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
1720 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1722 /* transport_generic_handle_data():
1726 int transport_generic_handle_data(
1730 * For the software fabric case, then we assume the nexus is being
1731 * failed/shutdown when signals are pending from the kthread context
1732 * caller, so we return a failure. For the HW target mode case running
1733 * in interrupt code, the signal_pending() check is skipped.
1735 if (!in_interrupt() && signal_pending(current))
1738 * If the received CDB has aleady been ABORTED by the generic
1739 * target engine, we now call transport_check_aborted_status()
1740 * to queue any delated TASK_ABORTED status for the received CDB to the
1741 * fabric module as we are expecting no further incoming DATA OUT
1742 * sequences at this point.
1744 if (transport_check_aborted_status(cmd, 1) != 0)
1747 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
1750 EXPORT_SYMBOL(transport_generic_handle_data);
1752 /* transport_generic_handle_tmr():
1756 int transport_generic_handle_tmr(
1759 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
1762 EXPORT_SYMBOL(transport_generic_handle_tmr);
1764 void transport_generic_free_cmd_intr(
1767 transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR);
1769 EXPORT_SYMBOL(transport_generic_free_cmd_intr);
1771 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1773 struct se_task *task, *task_tmp;
1774 unsigned long flags;
1777 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1778 cmd->se_tfo->get_task_tag(cmd));
1781 * No tasks remain in the execution queue
1783 spin_lock_irqsave(&cmd->t_state_lock, flags);
1784 list_for_each_entry_safe(task, task_tmp,
1785 &cmd->t_task_list, t_list) {
1786 pr_debug("Processing task %p\n", task);
1788 * If the struct se_task has not been sent and is not active,
1789 * remove the struct se_task from the execution queue.
1791 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
1792 spin_unlock_irqrestore(&cmd->t_state_lock,
1794 transport_remove_task_from_execute_queue(task,
1797 pr_debug("Task %p removed from execute queue\n", task);
1798 spin_lock_irqsave(&cmd->t_state_lock, flags);
1803 * If the struct se_task is active, sleep until it is returned
1806 if (task->task_flags & TF_ACTIVE) {
1807 task->task_flags |= TF_REQUEST_STOP;
1808 spin_unlock_irqrestore(&cmd->t_state_lock,
1811 pr_debug("Task %p waiting to complete\n", task);
1812 wait_for_completion(&task->task_stop_comp);
1813 pr_debug("Task %p stopped successfully\n", task);
1815 spin_lock_irqsave(&cmd->t_state_lock, flags);
1816 atomic_dec(&cmd->t_task_cdbs_left);
1817 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1819 pr_debug("Task %p - did nothing\n", task);
1823 __transport_stop_task_timer(task, &flags);
1825 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1831 * Handle SAM-esque emulation for generic transport request failures.
1833 static void transport_generic_request_failure(
1835 struct se_device *dev,
1841 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1842 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1843 cmd->t_task_cdb[0]);
1844 pr_debug("-----[ i_state: %d t_state/def_t_state:"
1845 " %d/%d transport_error_status: %d\n",
1846 cmd->se_tfo->get_cmd_state(cmd),
1847 cmd->t_state, cmd->deferred_t_state,
1848 cmd->transport_error_status);
1849 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1850 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1851 " t_transport_active: %d t_transport_stop: %d"
1852 " t_transport_sent: %d\n", cmd->t_task_list_num,
1853 atomic_read(&cmd->t_task_cdbs_left),
1854 atomic_read(&cmd->t_task_cdbs_sent),
1855 atomic_read(&cmd->t_task_cdbs_ex_left),
1856 atomic_read(&cmd->t_transport_active),
1857 atomic_read(&cmd->t_transport_stop),
1858 atomic_read(&cmd->t_transport_sent));
1860 transport_stop_all_task_timers(cmd);
1863 atomic_inc(&dev->depth_left);
1865 * For SAM Task Attribute emulation for failed struct se_cmd
1867 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1868 transport_complete_task_attr(cmd);
1871 transport_direct_request_timeout(cmd);
1872 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
1875 switch (cmd->transport_error_status) {
1876 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
1877 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1879 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
1880 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
1882 case PYX_TRANSPORT_INVALID_CDB_FIELD:
1883 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1885 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
1886 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1888 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
1890 transport_new_cmd_failure(cmd);
1892 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
1893 * we force this session to fall back to session
1896 cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
1897 cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
1900 case PYX_TRANSPORT_LU_COMM_FAILURE:
1901 case PYX_TRANSPORT_ILLEGAL_REQUEST:
1902 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1904 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
1905 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
1907 case PYX_TRANSPORT_WRITE_PROTECTED:
1908 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
1910 case PYX_TRANSPORT_RESERVATION_CONFLICT:
1912 * No SENSE Data payload for this case, set SCSI Status
1913 * and queue the response to $FABRIC_MOD.
1915 * Uses linux/include/scsi/scsi.h SAM status codes defs
1917 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1919 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1920 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1923 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1926 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1927 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1928 cmd->orig_fe_lun, 0x2C,
1929 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1931 ret = cmd->se_tfo->queue_status(cmd);
1935 case PYX_TRANSPORT_USE_SENSE_REASON:
1937 * struct se_cmd->scsi_sense_reason already set
1941 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1943 cmd->transport_error_status);
1944 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1948 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1949 * make the call to transport_send_check_condition_and_sense()
1950 * directly. Otherwise expect the fabric to make the call to
1951 * transport_send_check_condition_and_sense() after handling
1952 * possible unsoliticied write data payloads.
1954 if (!sc && !cmd->se_tfo->new_cmd_map)
1955 transport_new_cmd_failure(cmd);
1957 ret = transport_send_check_condition_and_sense(cmd,
1958 cmd->scsi_sense_reason, 0);
1964 transport_lun_remove_cmd(cmd);
1965 if (!transport_cmd_check_stop_to_fabric(cmd))
1970 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1971 transport_handle_queue_full(cmd, cmd->se_dev);
1974 static void transport_direct_request_timeout(struct se_cmd *cmd)
1976 unsigned long flags;
1978 spin_lock_irqsave(&cmd->t_state_lock, flags);
1979 if (!atomic_read(&cmd->t_transport_timeout)) {
1980 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1983 if (atomic_read(&cmd->t_task_cdbs_timeout_left)) {
1984 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1988 atomic_sub(atomic_read(&cmd->t_transport_timeout),
1990 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1993 static void transport_generic_request_timeout(struct se_cmd *cmd)
1995 unsigned long flags;
1998 * Reset cmd->t_se_count to allow transport_put_cmd()
1999 * to allow last call to free memory resources.
2001 spin_lock_irqsave(&cmd->t_state_lock, flags);
2002 if (atomic_read(&cmd->t_transport_timeout) > 1) {
2003 int tmp = (atomic_read(&cmd->t_transport_timeout) - 1);
2005 atomic_sub(tmp, &cmd->t_se_count);
2007 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2009 transport_put_cmd(cmd);
2012 static inline u32 transport_lba_21(unsigned char *cdb)
2014 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2017 static inline u32 transport_lba_32(unsigned char *cdb)
2019 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2022 static inline unsigned long long transport_lba_64(unsigned char *cdb)
2024 unsigned int __v1, __v2;
2026 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2027 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2029 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2033 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2035 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2037 unsigned int __v1, __v2;
2039 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2040 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2042 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2045 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2047 unsigned long flags;
2049 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2050 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2051 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2055 * Called from interrupt context.
2057 static void transport_task_timeout_handler(unsigned long data)
2059 struct se_task *task = (struct se_task *)data;
2060 struct se_cmd *cmd = task->task_se_cmd;
2061 unsigned long flags;
2063 pr_debug("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
2065 spin_lock_irqsave(&cmd->t_state_lock, flags);
2066 if (task->task_flags & TF_TIMER_STOP) {
2067 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2070 task->task_flags &= ~TF_TIMER_RUNNING;
2073 * Determine if transport_complete_task() has already been called.
2075 if (!(task->task_flags & TF_ACTIVE)) {
2076 pr_debug("transport task: %p cmd: %p timeout !TF_ACTIVE\n",
2078 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2082 atomic_inc(&cmd->t_se_count);
2083 atomic_inc(&cmd->t_transport_timeout);
2084 cmd->t_tasks_failed = 1;
2086 task->task_flags |= TF_TIMEOUT;
2087 task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
2088 task->task_scsi_status = 1;
2090 if (task->task_flags & TF_REQUEST_STOP) {
2091 pr_debug("transport task: %p cmd: %p timeout TF_REQUEST_STOP"
2092 " == 1\n", task, cmd);
2093 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2094 complete(&task->task_stop_comp);
2098 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
2099 pr_debug("transport task: %p cmd: %p timeout non zero"
2100 " t_task_cdbs_left\n", task, cmd);
2101 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2104 pr_debug("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2107 cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
2108 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2110 transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
2114 * Called with cmd->t_state_lock held.
2116 static void transport_start_task_timer(struct se_task *task)
2118 struct se_device *dev = task->task_se_cmd->se_dev;
2121 if (task->task_flags & TF_TIMER_RUNNING)
2124 * If the task_timeout is disabled, exit now.
2126 timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
2130 init_timer(&task->task_timer);
2131 task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
2132 task->task_timer.data = (unsigned long) task;
2133 task->task_timer.function = transport_task_timeout_handler;
2135 task->task_flags |= TF_TIMER_RUNNING;
2136 add_timer(&task->task_timer);
2138 pr_debug("Starting task timer for cmd: %p task: %p seconds:"
2139 " %d\n", task->task_se_cmd, task, timeout);
2144 * Called with spin_lock_irq(&cmd->t_state_lock) held.
2146 void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
2148 struct se_cmd *cmd = task->task_se_cmd;
2150 if (!(task->task_flags & TF_TIMER_RUNNING))
2153 task->task_flags |= TF_TIMER_STOP;
2154 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
2156 del_timer_sync(&task->task_timer);
2158 spin_lock_irqsave(&cmd->t_state_lock, *flags);
2159 task->task_flags &= ~TF_TIMER_RUNNING;
2160 task->task_flags &= ~TF_TIMER_STOP;
2163 static void transport_stop_all_task_timers(struct se_cmd *cmd)
2165 struct se_task *task = NULL, *task_tmp;
2166 unsigned long flags;
2168 spin_lock_irqsave(&cmd->t_state_lock, flags);
2169 list_for_each_entry_safe(task, task_tmp,
2170 &cmd->t_task_list, t_list)
2171 __transport_stop_task_timer(task, &flags);
2172 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2175 static inline int transport_tcq_window_closed(struct se_device *dev)
2177 if (dev->dev_tcq_window_closed++ <
2178 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
2179 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
2181 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
2183 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
2188 * Called from Fabric Module context from transport_execute_tasks()
2190 * The return of this function determins if the tasks from struct se_cmd
2191 * get added to the execution queue in transport_execute_tasks(),
2192 * or are added to the delayed or ordered lists here.
2194 static inline int transport_execute_task_attr(struct se_cmd *cmd)
2196 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2199 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2200 * to allow the passed struct se_cmd list of tasks to the front of the list.
2202 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
2203 atomic_inc(&cmd->se_dev->dev_hoq_count);
2204 smp_mb__after_atomic_inc();
2205 pr_debug("Added HEAD_OF_QUEUE for CDB:"
2206 " 0x%02x, se_ordered_id: %u\n",
2208 cmd->se_ordered_id);
2210 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
2211 spin_lock(&cmd->se_dev->ordered_cmd_lock);
2212 list_add_tail(&cmd->se_ordered_node,
2213 &cmd->se_dev->ordered_cmd_list);
2214 spin_unlock(&cmd->se_dev->ordered_cmd_lock);
2216 atomic_inc(&cmd->se_dev->dev_ordered_sync);
2217 smp_mb__after_atomic_inc();
2219 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
2220 " list, se_ordered_id: %u\n",
2222 cmd->se_ordered_id);
2224 * Add ORDERED command to tail of execution queue if
2225 * no other older commands exist that need to be
2228 if (!atomic_read(&cmd->se_dev->simple_cmds))
2232 * For SIMPLE and UNTAGGED Task Attribute commands
2234 atomic_inc(&cmd->se_dev->simple_cmds);
2235 smp_mb__after_atomic_inc();
2238 * Otherwise if one or more outstanding ORDERED task attribute exist,
2239 * add the dormant task(s) built for the passed struct se_cmd to the
2240 * execution queue and become in Active state for this struct se_device.
2242 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2244 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2245 * will be drained upon completion of HEAD_OF_QUEUE task.
2247 spin_lock(&cmd->se_dev->delayed_cmd_lock);
2248 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2249 list_add_tail(&cmd->se_delayed_node,
2250 &cmd->se_dev->delayed_cmd_list);
2251 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2253 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2254 " delayed CMD list, se_ordered_id: %u\n",
2255 cmd->t_task_cdb[0], cmd->sam_task_attr,
2256 cmd->se_ordered_id);
2258 * Return zero to let transport_execute_tasks() know
2259 * not to add the delayed tasks to the execution list.
2264 * Otherwise, no ORDERED task attributes exist..
2270 * Called from fabric module context in transport_generic_new_cmd() and
2271 * transport_generic_process_write()
2273 static int transport_execute_tasks(struct se_cmd *cmd)
2277 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2278 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2279 transport_generic_request_failure(cmd, NULL, 0, 1);
2284 * Call transport_cmd_check_stop() to see if a fabric exception
2285 * has occurred that prevents execution.
2287 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2289 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2290 * attribute for the tasks of the received struct se_cmd CDB
2292 add_tasks = transport_execute_task_attr(cmd);
2296 * This calls transport_add_tasks_from_cmd() to handle
2297 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2298 * (if enabled) in __transport_add_task_to_execute_queue() and
2299 * transport_add_task_check_sam_attr().
2301 transport_add_tasks_from_cmd(cmd);
2304 * Kick the execution queue for the cmd associated struct se_device
2308 __transport_execute_tasks(cmd->se_dev);
2313 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2314 * from struct se_device->execute_task_list and
2316 * Called from transport_processing_thread()
2318 static int __transport_execute_tasks(struct se_device *dev)
2321 struct se_cmd *cmd = NULL;
2322 struct se_task *task = NULL;
2323 unsigned long flags;
2326 * Check if there is enough room in the device and HBA queue to send
2327 * struct se_tasks to the selected transport.
2330 if (!atomic_read(&dev->depth_left))
2331 return transport_tcq_window_closed(dev);
2333 dev->dev_tcq_window_closed = 0;
2335 spin_lock_irq(&dev->execute_task_lock);
2336 if (list_empty(&dev->execute_task_list)) {
2337 spin_unlock_irq(&dev->execute_task_lock);
2340 task = list_first_entry(&dev->execute_task_list,
2341 struct se_task, t_execute_list);
2342 __transport_remove_task_from_execute_queue(task, dev);
2343 spin_unlock_irq(&dev->execute_task_lock);
2345 atomic_dec(&dev->depth_left);
2347 cmd = task->task_se_cmd;
2349 spin_lock_irqsave(&cmd->t_state_lock, flags);
2350 task->task_flags |= (TF_ACTIVE | TF_SENT);
2351 atomic_inc(&cmd->t_task_cdbs_sent);
2353 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2354 cmd->t_task_list_num)
2355 atomic_set(&cmd->transport_sent, 1);
2357 transport_start_task_timer(task);
2358 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2360 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2361 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2362 * struct se_subsystem_api->do_task() caller below.
2364 if (cmd->transport_emulate_cdb) {
2365 error = cmd->transport_emulate_cdb(cmd);
2367 cmd->transport_error_status = error;
2368 spin_lock_irqsave(&cmd->t_state_lock, flags);
2369 task->task_flags &= ~TF_ACTIVE;
2370 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2371 atomic_set(&cmd->transport_sent, 0);
2372 transport_stop_tasks_for_cmd(cmd);
2373 transport_generic_request_failure(cmd, dev, 0, 1);
2377 * Handle the successful completion for transport_emulate_cdb()
2378 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2379 * Otherwise the caller is expected to complete the task with
2382 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2383 cmd->scsi_status = SAM_STAT_GOOD;
2384 task->task_scsi_status = GOOD;
2385 transport_complete_task(task, 1);
2389 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2390 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2391 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2392 * LUN emulation code.
2394 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2395 * call ->do_task() directly and let the underlying TCM subsystem plugin
2396 * code handle the CDB emulation.
2398 if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2399 (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2400 error = transport_emulate_control_cdb(task);
2402 error = dev->transport->do_task(task);
2405 cmd->transport_error_status = error;
2406 spin_lock_irqsave(&cmd->t_state_lock, flags);
2407 task->task_flags &= ~TF_ACTIVE;
2408 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2409 atomic_set(&cmd->transport_sent, 0);
2410 transport_stop_tasks_for_cmd(cmd);
2411 transport_generic_request_failure(cmd, dev, 0, 1);
2420 void transport_new_cmd_failure(struct se_cmd *se_cmd)
2422 unsigned long flags;
2424 * Any unsolicited data will get dumped for failed command inside of
2427 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2428 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2429 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2430 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2433 static inline u32 transport_get_sectors_6(
2438 struct se_device *dev = cmd->se_dev;
2441 * Assume TYPE_DISK for non struct se_device objects.
2442 * Use 8-bit sector value.
2448 * Use 24-bit allocation length for TYPE_TAPE.
2450 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2451 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2454 * Everything else assume TYPE_DISK Sector CDB location.
2455 * Use 8-bit sector value.
2461 static inline u32 transport_get_sectors_10(
2466 struct se_device *dev = cmd->se_dev;
2469 * Assume TYPE_DISK for non struct se_device objects.
2470 * Use 16-bit sector value.
2476 * XXX_10 is not defined in SSC, throw an exception
2478 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2484 * Everything else assume TYPE_DISK Sector CDB location.
2485 * Use 16-bit sector value.
2488 return (u32)(cdb[7] << 8) + cdb[8];
2491 static inline u32 transport_get_sectors_12(
2496 struct se_device *dev = cmd->se_dev;
2499 * Assume TYPE_DISK for non struct se_device objects.
2500 * Use 32-bit sector value.
2506 * XXX_12 is not defined in SSC, throw an exception
2508 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2514 * Everything else assume TYPE_DISK Sector CDB location.
2515 * Use 32-bit sector value.
2518 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2521 static inline u32 transport_get_sectors_16(
2526 struct se_device *dev = cmd->se_dev;
2529 * Assume TYPE_DISK for non struct se_device objects.
2530 * Use 32-bit sector value.
2536 * Use 24-bit allocation length for TYPE_TAPE.
2538 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2539 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2542 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2543 (cdb[12] << 8) + cdb[13];
2547 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2549 static inline u32 transport_get_sectors_32(
2555 * Assume TYPE_DISK for non struct se_device objects.
2556 * Use 32-bit sector value.
2558 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2559 (cdb[30] << 8) + cdb[31];
2563 static inline u32 transport_get_size(
2568 struct se_device *dev = cmd->se_dev;
2570 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2571 if (cdb[1] & 1) { /* sectors */
2572 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2577 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2578 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2579 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2580 dev->transport->name);
2582 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2585 static void transport_xor_callback(struct se_cmd *cmd)
2587 unsigned char *buf, *addr;
2588 struct scatterlist *sg;
2589 unsigned int offset;
2593 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2595 * 1) read the specified logical block(s);
2596 * 2) transfer logical blocks from the data-out buffer;
2597 * 3) XOR the logical blocks transferred from the data-out buffer with
2598 * the logical blocks read, storing the resulting XOR data in a buffer;
2599 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2600 * blocks transferred from the data-out buffer; and
2601 * 5) transfer the resulting XOR data to the data-in buffer.
2603 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2605 pr_err("Unable to allocate xor_callback buf\n");
2609 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2610 * into the locally allocated *buf
2612 sg_copy_to_buffer(cmd->t_data_sg,
2618 * Now perform the XOR against the BIDI read memory located at
2619 * cmd->t_mem_bidi_list
2623 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2624 addr = kmap_atomic(sg_page(sg), KM_USER0);
2628 for (i = 0; i < sg->length; i++)
2629 *(addr + sg->offset + i) ^= *(buf + offset + i);
2631 offset += sg->length;
2632 kunmap_atomic(addr, KM_USER0);
2640 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2642 static int transport_get_sense_data(struct se_cmd *cmd)
2644 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2645 struct se_device *dev = cmd->se_dev;
2646 struct se_task *task = NULL, *task_tmp;
2647 unsigned long flags;
2650 WARN_ON(!cmd->se_lun);
2655 spin_lock_irqsave(&cmd->t_state_lock, flags);
2656 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2657 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2661 list_for_each_entry_safe(task, task_tmp,
2662 &cmd->t_task_list, t_list) {
2663 if (!task->task_sense)
2666 if (!dev->transport->get_sense_buffer) {
2667 pr_err("dev->transport->get_sense_buffer"
2672 sense_buffer = dev->transport->get_sense_buffer(task);
2673 if (!sense_buffer) {
2674 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
2675 " sense buffer for task with sense\n",
2676 cmd->se_tfo->get_task_tag(cmd), task);
2679 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2681 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2682 TRANSPORT_SENSE_BUFFER);
2684 memcpy(&buffer[offset], sense_buffer,
2685 TRANSPORT_SENSE_BUFFER);
2686 cmd->scsi_status = task->task_scsi_status;
2687 /* Automatically padded */
2688 cmd->scsi_sense_length =
2689 (TRANSPORT_SENSE_BUFFER + offset);
2691 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2693 dev->se_hba->hba_id, dev->transport->name,
2697 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2703 transport_handle_reservation_conflict(struct se_cmd *cmd)
2705 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2706 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2707 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2709 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2710 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2713 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2716 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2717 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2718 cmd->orig_fe_lun, 0x2C,
2719 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2723 static inline long long transport_dev_end_lba(struct se_device *dev)
2725 return dev->transport->get_blocks(dev) + 1;
2728 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2730 struct se_device *dev = cmd->se_dev;
2733 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2736 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2738 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2739 pr_err("LBA: %llu Sectors: %u exceeds"
2740 " transport_dev_end_lba(): %llu\n",
2741 cmd->t_task_lba, sectors,
2742 transport_dev_end_lba(dev));
2749 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2752 * Determine if the received WRITE_SAME is used to for direct
2753 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2754 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2755 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2757 int passthrough = (dev->transport->transport_type ==
2758 TRANSPORT_PLUGIN_PHBA_PDEV);
2761 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2762 pr_err("WRITE_SAME PBDATA and LBDATA"
2763 " bits not supported for Block Discard"
2768 * Currently for the emulated case we only accept
2769 * tpws with the UNMAP=1 bit set.
2771 if (!(flags[0] & 0x08)) {
2772 pr_err("WRITE_SAME w/o UNMAP bit not"
2773 " supported for Block Discard Emulation\n");
2781 /* transport_generic_cmd_sequencer():
2783 * Generic Command Sequencer that should work for most DAS transport
2786 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2789 * FIXME: Need to support other SCSI OPCODES where as well.
2791 static int transport_generic_cmd_sequencer(
2795 struct se_device *dev = cmd->se_dev;
2796 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2797 int ret = 0, sector_ret = 0, passthrough;
2798 u32 sectors = 0, size = 0, pr_reg_type = 0;
2802 * Check for an existing UNIT ATTENTION condition
2804 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2805 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2806 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2810 * Check status of Asymmetric Logical Unit Assignment port
2812 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2815 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2816 * The ALUA additional sense code qualifier (ASCQ) is determined
2817 * by the ALUA primary or secondary access state..
2821 pr_debug("[%s]: ALUA TG Port not available,"
2822 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2823 cmd->se_tfo->get_fabric_name(), alua_ascq);
2825 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2826 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2827 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2830 goto out_invalid_cdb_field;
2833 * Check status for SPC-3 Persistent Reservations
2835 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2836 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2837 cmd, cdb, pr_reg_type) != 0)
2838 return transport_handle_reservation_conflict(cmd);
2840 * This means the CDB is allowed for the SCSI Initiator port
2841 * when said port is *NOT* holding the legacy SPC-2 or
2842 * SPC-3 Persistent Reservation.
2848 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2850 goto out_unsupported_cdb;
2851 size = transport_get_size(sectors, cdb, cmd);
2852 cmd->t_task_lba = transport_lba_21(cdb);
2853 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2856 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2858 goto out_unsupported_cdb;
2859 size = transport_get_size(sectors, cdb, cmd);
2860 cmd->t_task_lba = transport_lba_32(cdb);
2861 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2864 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2866 goto out_unsupported_cdb;
2867 size = transport_get_size(sectors, cdb, cmd);
2868 cmd->t_task_lba = transport_lba_32(cdb);
2869 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2872 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2874 goto out_unsupported_cdb;
2875 size = transport_get_size(sectors, cdb, cmd);
2876 cmd->t_task_lba = transport_lba_64(cdb);
2877 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2880 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2882 goto out_unsupported_cdb;
2883 size = transport_get_size(sectors, cdb, cmd);
2884 cmd->t_task_lba = transport_lba_21(cdb);
2885 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2888 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2890 goto out_unsupported_cdb;
2891 size = transport_get_size(sectors, cdb, cmd);
2892 cmd->t_task_lba = transport_lba_32(cdb);
2893 cmd->t_tasks_fua = (cdb[1] & 0x8);
2894 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2897 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2899 goto out_unsupported_cdb;
2900 size = transport_get_size(sectors, cdb, cmd);
2901 cmd->t_task_lba = transport_lba_32(cdb);
2902 cmd->t_tasks_fua = (cdb[1] & 0x8);
2903 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2906 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2908 goto out_unsupported_cdb;
2909 size = transport_get_size(sectors, cdb, cmd);
2910 cmd->t_task_lba = transport_lba_64(cdb);
2911 cmd->t_tasks_fua = (cdb[1] & 0x8);
2912 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2914 case XDWRITEREAD_10:
2915 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2916 !(cmd->t_tasks_bidi))
2917 goto out_invalid_cdb_field;
2918 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2920 goto out_unsupported_cdb;
2921 size = transport_get_size(sectors, cdb, cmd);
2922 cmd->t_task_lba = transport_lba_32(cdb);
2923 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2924 passthrough = (dev->transport->transport_type ==
2925 TRANSPORT_PLUGIN_PHBA_PDEV);
2927 * Skip the remaining assignments for TCM/PSCSI passthrough
2932 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
2934 cmd->transport_complete_callback = &transport_xor_callback;
2935 cmd->t_tasks_fua = (cdb[1] & 0x8);
2937 case VARIABLE_LENGTH_CMD:
2938 service_action = get_unaligned_be16(&cdb[8]);
2940 * Determine if this is TCM/PSCSI device and we should disable
2941 * internal emulation for this CDB.
2943 passthrough = (dev->transport->transport_type ==
2944 TRANSPORT_PLUGIN_PHBA_PDEV);
2946 switch (service_action) {
2947 case XDWRITEREAD_32:
2948 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2950 goto out_unsupported_cdb;
2951 size = transport_get_size(sectors, cdb, cmd);
2953 * Use WRITE_32 and READ_32 opcodes for the emulated
2954 * XDWRITE_READ_32 logic.
2956 cmd->t_task_lba = transport_lba_64_ext(cdb);
2957 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2960 * Skip the remaining assignments for TCM/PSCSI passthrough
2966 * Setup BIDI XOR callback to be run during
2967 * transport_generic_complete_ok()
2969 cmd->transport_complete_callback = &transport_xor_callback;
2970 cmd->t_tasks_fua = (cdb[10] & 0x8);
2973 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2975 goto out_unsupported_cdb;
2978 size = transport_get_size(1, cdb, cmd);
2980 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2982 goto out_invalid_cdb_field;
2985 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2986 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2988 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2989 goto out_invalid_cdb_field;
2993 pr_err("VARIABLE_LENGTH_CMD service action"
2994 " 0x%04x not supported\n", service_action);
2995 goto out_unsupported_cdb;
2998 case MAINTENANCE_IN:
2999 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3000 /* MAINTENANCE_IN from SCC-2 */
3002 * Check for emulated MI_REPORT_TARGET_PGS.
3004 if (cdb[1] == MI_REPORT_TARGET_PGS) {
3005 cmd->transport_emulate_cdb =
3006 (su_dev->t10_alua.alua_type ==
3007 SPC3_ALUA_EMULATED) ?
3008 core_emulate_report_target_port_groups :
3011 size = (cdb[6] << 24) | (cdb[7] << 16) |
3012 (cdb[8] << 8) | cdb[9];
3014 /* GPCMD_SEND_KEY from multi media commands */
3015 size = (cdb[8] << 8) + cdb[9];
3017 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3021 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3023 case MODE_SELECT_10:
3024 size = (cdb[7] << 8) + cdb[8];
3025 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3029 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3032 case GPCMD_READ_BUFFER_CAPACITY:
3033 case GPCMD_SEND_OPC:
3036 size = (cdb[7] << 8) + cdb[8];
3037 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3039 case READ_BLOCK_LIMITS:
3040 size = READ_BLOCK_LEN;
3041 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3043 case GPCMD_GET_CONFIGURATION:
3044 case GPCMD_READ_FORMAT_CAPACITIES:
3045 case GPCMD_READ_DISC_INFO:
3046 case GPCMD_READ_TRACK_RZONE_INFO:
3047 size = (cdb[7] << 8) + cdb[8];
3048 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3050 case PERSISTENT_RESERVE_IN:
3051 case PERSISTENT_RESERVE_OUT:
3052 cmd->transport_emulate_cdb =
3053 (su_dev->t10_pr.res_type ==
3054 SPC3_PERSISTENT_RESERVATIONS) ?
3055 core_scsi3_emulate_pr : NULL;
3056 size = (cdb[7] << 8) + cdb[8];
3057 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3059 case GPCMD_MECHANISM_STATUS:
3060 case GPCMD_READ_DVD_STRUCTURE:
3061 size = (cdb[8] << 8) + cdb[9];
3062 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3065 size = READ_POSITION_LEN;
3066 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3068 case MAINTENANCE_OUT:
3069 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3070 /* MAINTENANCE_OUT from SCC-2
3072 * Check for emulated MO_SET_TARGET_PGS.
3074 if (cdb[1] == MO_SET_TARGET_PGS) {
3075 cmd->transport_emulate_cdb =
3076 (su_dev->t10_alua.alua_type ==
3077 SPC3_ALUA_EMULATED) ?
3078 core_emulate_set_target_port_groups :
3082 size = (cdb[6] << 24) | (cdb[7] << 16) |
3083 (cdb[8] << 8) | cdb[9];
3085 /* GPCMD_REPORT_KEY from multi media commands */
3086 size = (cdb[8] << 8) + cdb[9];
3088 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3091 size = (cdb[3] << 8) + cdb[4];
3093 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3094 * See spc4r17 section 5.3
3096 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3097 cmd->sam_task_attr = MSG_HEAD_TAG;
3098 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3101 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3102 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3105 size = READ_CAP_LEN;
3106 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3108 case READ_MEDIA_SERIAL_NUMBER:
3109 case SECURITY_PROTOCOL_IN:
3110 case SECURITY_PROTOCOL_OUT:
3111 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3112 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3114 case SERVICE_ACTION_IN:
3115 case ACCESS_CONTROL_IN:
3116 case ACCESS_CONTROL_OUT:
3118 case READ_ATTRIBUTE:
3119 case RECEIVE_COPY_RESULTS:
3120 case WRITE_ATTRIBUTE:
3121 size = (cdb[10] << 24) | (cdb[11] << 16) |
3122 (cdb[12] << 8) | cdb[13];
3123 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3125 case RECEIVE_DIAGNOSTIC:
3126 case SEND_DIAGNOSTIC:
3127 size = (cdb[3] << 8) | cdb[4];
3128 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3130 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3133 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3134 size = (2336 * sectors);
3135 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3140 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3144 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3146 case READ_ELEMENT_STATUS:
3147 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
3148 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3151 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3152 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3157 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3158 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3160 if (cdb[0] == RESERVE_10)
3161 size = (cdb[7] << 8) | cdb[8];
3163 size = cmd->data_length;
3166 * Setup the legacy emulated handler for SPC-2 and
3167 * >= SPC-3 compatible reservation handling (CRH=1)
3168 * Otherwise, we assume the underlying SCSI logic is
3169 * is running in SPC_PASSTHROUGH, and wants reservations
3170 * emulation disabled.
3172 cmd->transport_emulate_cdb =
3173 (su_dev->t10_pr.res_type !=
3175 core_scsi2_emulate_crh : NULL;
3176 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3181 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3182 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3184 if (cdb[0] == RELEASE_10)
3185 size = (cdb[7] << 8) | cdb[8];
3187 size = cmd->data_length;
3189 cmd->transport_emulate_cdb =
3190 (su_dev->t10_pr.res_type !=
3192 core_scsi2_emulate_crh : NULL;
3193 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3195 case SYNCHRONIZE_CACHE:
3196 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3198 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3200 if (cdb[0] == SYNCHRONIZE_CACHE) {
3201 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3202 cmd->t_task_lba = transport_lba_32(cdb);
3204 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3205 cmd->t_task_lba = transport_lba_64(cdb);
3208 goto out_unsupported_cdb;
3210 size = transport_get_size(sectors, cdb, cmd);
3211 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3214 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3216 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3219 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3220 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3222 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3224 * Check to ensure that LBA + Range does not exceed past end of
3225 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
3227 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3228 if (transport_cmd_get_valid_sectors(cmd) < 0)
3229 goto out_invalid_cdb_field;
3233 size = get_unaligned_be16(&cdb[7]);
3234 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3237 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3239 goto out_unsupported_cdb;
3242 size = transport_get_size(1, cdb, cmd);
3244 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3245 goto out_invalid_cdb_field;
3248 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
3249 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3251 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3252 goto out_invalid_cdb_field;
3255 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3257 goto out_unsupported_cdb;
3260 size = transport_get_size(1, cdb, cmd);
3262 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3263 goto out_invalid_cdb_field;
3266 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3267 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3269 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3270 * of byte 1 bit 3 UNMAP instead of original reserved field
3272 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3273 goto out_invalid_cdb_field;
3275 case ALLOW_MEDIUM_REMOVAL:
3276 case GPCMD_CLOSE_TRACK:
3278 case INITIALIZE_ELEMENT_STATUS:
3279 case GPCMD_LOAD_UNLOAD:
3282 case GPCMD_SET_SPEED:
3285 case TEST_UNIT_READY:
3287 case WRITE_FILEMARKS:
3289 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3292 cmd->transport_emulate_cdb =
3293 transport_core_report_lun_response;
3294 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3296 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3297 * See spc4r17 section 5.3
3299 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3300 cmd->sam_task_attr = MSG_HEAD_TAG;
3301 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3304 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3305 " 0x%02x, sending CHECK_CONDITION.\n",
3306 cmd->se_tfo->get_fabric_name(), cdb[0]);
3307 goto out_unsupported_cdb;
3310 if (size != cmd->data_length) {
3311 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3312 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3313 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3314 cmd->data_length, size, cdb[0]);
3316 cmd->cmd_spdtl = size;
3318 if (cmd->data_direction == DMA_TO_DEVICE) {
3319 pr_err("Rejecting underflow/overflow"
3321 goto out_invalid_cdb_field;
3324 * Reject READ_* or WRITE_* with overflow/underflow for
3325 * type SCF_SCSI_DATA_SG_IO_CDB.
3327 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3328 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3329 " CDB on non 512-byte sector setup subsystem"
3330 " plugin: %s\n", dev->transport->name);
3331 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3332 goto out_invalid_cdb_field;
3335 if (size > cmd->data_length) {
3336 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3337 cmd->residual_count = (size - cmd->data_length);
3339 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3340 cmd->residual_count = (cmd->data_length - size);
3342 cmd->data_length = size;
3345 /* Let's limit control cdbs to a page, for simplicity's sake. */
3346 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3348 goto out_invalid_cdb_field;
3350 transport_set_supported_SAM_opcode(cmd);
3353 out_unsupported_cdb:
3354 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3355 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3357 out_invalid_cdb_field:
3358 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3359 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3364 * Called from transport_generic_complete_ok() and
3365 * transport_generic_request_failure() to determine which dormant/delayed
3366 * and ordered cmds need to have their tasks added to the execution queue.
3368 static void transport_complete_task_attr(struct se_cmd *cmd)
3370 struct se_device *dev = cmd->se_dev;
3371 struct se_cmd *cmd_p, *cmd_tmp;
3372 int new_active_tasks = 0;
3374 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3375 atomic_dec(&dev->simple_cmds);
3376 smp_mb__after_atomic_dec();
3377 dev->dev_cur_ordered_id++;
3378 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3379 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3380 cmd->se_ordered_id);
3381 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3382 atomic_dec(&dev->dev_hoq_count);
3383 smp_mb__after_atomic_dec();
3384 dev->dev_cur_ordered_id++;
3385 pr_debug("Incremented dev_cur_ordered_id: %u for"
3386 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3387 cmd->se_ordered_id);
3388 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3389 spin_lock(&dev->ordered_cmd_lock);
3390 list_del(&cmd->se_ordered_node);
3391 atomic_dec(&dev->dev_ordered_sync);
3392 smp_mb__after_atomic_dec();
3393 spin_unlock(&dev->ordered_cmd_lock);
3395 dev->dev_cur_ordered_id++;
3396 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3397 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3400 * Process all commands up to the last received
3401 * ORDERED task attribute which requires another blocking
3404 spin_lock(&dev->delayed_cmd_lock);
3405 list_for_each_entry_safe(cmd_p, cmd_tmp,
3406 &dev->delayed_cmd_list, se_delayed_node) {
3408 list_del(&cmd_p->se_delayed_node);
3409 spin_unlock(&dev->delayed_cmd_lock);
3411 pr_debug("Calling add_tasks() for"
3412 " cmd_p: 0x%02x Task Attr: 0x%02x"
3413 " Dormant -> Active, se_ordered_id: %u\n",
3414 cmd_p->t_task_cdb[0],
3415 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3417 transport_add_tasks_from_cmd(cmd_p);
3420 spin_lock(&dev->delayed_cmd_lock);
3421 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3424 spin_unlock(&dev->delayed_cmd_lock);
3426 * If new tasks have become active, wake up the transport thread
3427 * to do the processing of the Active tasks.
3429 if (new_active_tasks != 0)
3430 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3433 static void transport_complete_qf(struct se_cmd *cmd)
3437 transport_stop_all_task_timers(cmd);
3438 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3439 transport_complete_task_attr(cmd);
3441 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3442 ret = cmd->se_tfo->queue_status(cmd);
3447 switch (cmd->data_direction) {
3448 case DMA_FROM_DEVICE:
3449 ret = cmd->se_tfo->queue_data_in(cmd);
3452 if (cmd->t_bidi_data_sg) {
3453 ret = cmd->se_tfo->queue_data_in(cmd);
3457 /* Fall through for DMA_TO_DEVICE */
3459 ret = cmd->se_tfo->queue_status(cmd);
3467 transport_handle_queue_full(cmd, cmd->se_dev);
3470 transport_lun_remove_cmd(cmd);
3471 transport_cmd_check_stop_to_fabric(cmd);
3474 static void transport_handle_queue_full(
3476 struct se_device *dev)
3478 spin_lock_irq(&dev->qf_cmd_lock);
3479 cmd->se_cmd_flags |= SCF_EMULATE_QUEUE_FULL;
3480 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3481 atomic_inc(&dev->dev_qf_count);
3482 smp_mb__after_atomic_inc();
3483 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3485 schedule_work(&cmd->se_dev->qf_work_queue);
3488 static void transport_generic_complete_ok(struct se_cmd *cmd)
3490 int reason = 0, ret;
3492 * Check if we need to move delayed/dormant tasks from cmds on the
3493 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3496 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3497 transport_complete_task_attr(cmd);
3499 * Check to schedule QUEUE_FULL work, or execute an existing
3500 * cmd->transport_qf_callback()
3502 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3503 schedule_work(&cmd->se_dev->qf_work_queue);
3506 * Check if we need to retrieve a sense buffer from
3507 * the struct se_cmd in question.
3509 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3510 if (transport_get_sense_data(cmd) < 0)
3511 reason = TCM_NON_EXISTENT_LUN;
3514 * Only set when an struct se_task->task_scsi_status returned
3515 * a non GOOD status.
3517 if (cmd->scsi_status) {
3518 ret = transport_send_check_condition_and_sense(
3523 transport_lun_remove_cmd(cmd);
3524 transport_cmd_check_stop_to_fabric(cmd);
3529 * Check for a callback, used by amongst other things
3530 * XDWRITE_READ_10 emulation.
3532 if (cmd->transport_complete_callback)
3533 cmd->transport_complete_callback(cmd);
3535 switch (cmd->data_direction) {
3536 case DMA_FROM_DEVICE:
3537 spin_lock(&cmd->se_lun->lun_sep_lock);
3538 if (cmd->se_lun->lun_sep) {
3539 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3542 spin_unlock(&cmd->se_lun->lun_sep_lock);
3544 ret = cmd->se_tfo->queue_data_in(cmd);
3549 spin_lock(&cmd->se_lun->lun_sep_lock);
3550 if (cmd->se_lun->lun_sep) {
3551 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3554 spin_unlock(&cmd->se_lun->lun_sep_lock);
3556 * Check if we need to send READ payload for BIDI-COMMAND
3558 if (cmd->t_bidi_data_sg) {
3559 spin_lock(&cmd->se_lun->lun_sep_lock);
3560 if (cmd->se_lun->lun_sep) {
3561 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3564 spin_unlock(&cmd->se_lun->lun_sep_lock);
3565 ret = cmd->se_tfo->queue_data_in(cmd);
3570 /* Fall through for DMA_TO_DEVICE */
3572 ret = cmd->se_tfo->queue_status(cmd);
3580 transport_lun_remove_cmd(cmd);
3581 transport_cmd_check_stop_to_fabric(cmd);
3585 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3586 " data_direction: %d\n", cmd, cmd->data_direction);
3587 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3588 transport_handle_queue_full(cmd, cmd->se_dev);
3591 static void transport_free_dev_tasks(struct se_cmd *cmd)
3593 struct se_task *task, *task_tmp;
3594 unsigned long flags;
3596 spin_lock_irqsave(&cmd->t_state_lock, flags);
3597 list_for_each_entry_safe(task, task_tmp,
3598 &cmd->t_task_list, t_list) {
3599 if (task->task_flags & TF_ACTIVE)
3602 kfree(task->task_sg_bidi);
3603 kfree(task->task_sg);
3605 list_del(&task->t_list);
3607 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3608 cmd->se_dev->transport->free_task(task);
3609 spin_lock_irqsave(&cmd->t_state_lock, flags);
3611 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3614 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3616 struct scatterlist *sg;
3619 for_each_sg(sgl, sg, nents, count)
3620 __free_page(sg_page(sg));
3625 static inline void transport_free_pages(struct se_cmd *cmd)
3627 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3630 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3631 cmd->t_data_sg = NULL;
3632 cmd->t_data_nents = 0;
3634 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3635 cmd->t_bidi_data_sg = NULL;
3636 cmd->t_bidi_data_nents = 0;
3640 * transport_put_cmd - release a reference to a command
3641 * @cmd: command to release
3643 * This routine releases our reference to the command and frees it if possible.
3645 static void transport_put_cmd(struct se_cmd *cmd)
3647 unsigned long flags;
3650 spin_lock_irqsave(&cmd->t_state_lock, flags);
3651 if (atomic_read(&cmd->t_fe_count)) {
3652 if (!atomic_dec_and_test(&cmd->t_fe_count))
3656 if (atomic_read(&cmd->t_se_count)) {
3657 if (!atomic_dec_and_test(&cmd->t_se_count))
3661 if (atomic_read(&cmd->transport_dev_active)) {
3662 atomic_set(&cmd->transport_dev_active, 0);
3663 transport_all_task_dev_remove_state(cmd);
3666 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3668 if (free_tasks != 0)
3669 transport_free_dev_tasks(cmd);
3671 transport_free_pages(cmd);
3672 transport_release_cmd(cmd);
3675 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3679 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3680 * allocating in the core.
3681 * @cmd: Associated se_cmd descriptor
3682 * @mem: SGL style memory for TCM WRITE / READ
3683 * @sg_mem_num: Number of SGL elements
3684 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3685 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3687 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3690 int transport_generic_map_mem_to_cmd(
3692 struct scatterlist *sgl,
3694 struct scatterlist *sgl_bidi,
3697 if (!sgl || !sgl_count)
3700 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3701 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3703 cmd->t_data_sg = sgl;
3704 cmd->t_data_nents = sgl_count;
3706 if (sgl_bidi && sgl_bidi_count) {
3707 cmd->t_bidi_data_sg = sgl_bidi;
3708 cmd->t_bidi_data_nents = sgl_bidi_count;
3710 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3715 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3717 static int transport_new_cmd_obj(struct se_cmd *cmd)
3719 struct se_device *dev = cmd->se_dev;
3720 int set_counts = 1, rc, task_cdbs;
3723 * Setup any BIDI READ tasks and memory from
3724 * cmd->t_mem_bidi_list so the READ struct se_tasks
3725 * are queued first for the non pSCSI passthrough case.
3727 if (cmd->t_bidi_data_sg &&
3728 (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
3729 rc = transport_allocate_tasks(cmd,
3732 cmd->t_bidi_data_sg,
3733 cmd->t_bidi_data_nents);
3735 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3736 cmd->scsi_sense_reason =
3737 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3740 atomic_inc(&cmd->t_fe_count);
3741 atomic_inc(&cmd->t_se_count);
3745 * Setup the tasks and memory from cmd->t_mem_list
3746 * Note for BIDI transfers this will contain the WRITE payload
3748 task_cdbs = transport_allocate_tasks(cmd,
3750 cmd->data_direction,
3753 if (task_cdbs <= 0) {
3754 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3755 cmd->scsi_sense_reason =
3756 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3761 atomic_inc(&cmd->t_fe_count);
3762 atomic_inc(&cmd->t_se_count);
3765 cmd->t_task_list_num = task_cdbs;
3767 atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
3768 atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
3769 atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
3773 void *transport_kmap_first_data_page(struct se_cmd *cmd)
3775 struct scatterlist *sg = cmd->t_data_sg;
3779 * We need to take into account a possible offset here for fabrics like
3780 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3781 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3783 return kmap(sg_page(sg)) + sg->offset;
3785 EXPORT_SYMBOL(transport_kmap_first_data_page);
3787 void transport_kunmap_first_data_page(struct se_cmd *cmd)
3789 kunmap(sg_page(cmd->t_data_sg));
3791 EXPORT_SYMBOL(transport_kunmap_first_data_page);
3794 transport_generic_get_mem(struct se_cmd *cmd)
3796 u32 length = cmd->data_length;
3801 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3802 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3803 if (!cmd->t_data_sg)
3806 cmd->t_data_nents = nents;
3807 sg_init_table(cmd->t_data_sg, nents);
3810 u32 page_len = min_t(u32, length, PAGE_SIZE);
3811 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3815 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3823 __free_page(sg_page(&cmd->t_data_sg[i]));
3826 kfree(cmd->t_data_sg);
3827 cmd->t_data_sg = NULL;
3831 /* Reduce sectors if they are too long for the device */
3832 static inline sector_t transport_limit_task_sectors(
3833 struct se_device *dev,
3834 unsigned long long lba,
3837 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
3839 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3840 if ((lba + sectors) > transport_dev_end_lba(dev))
3841 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
3848 * This function can be used by HW target mode drivers to create a linked
3849 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3850 * This is intended to be called during the completion path by TCM Core
3851 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3853 void transport_do_task_sg_chain(struct se_cmd *cmd)
3855 struct scatterlist *sg_first = NULL;
3856 struct scatterlist *sg_prev = NULL;
3857 int sg_prev_nents = 0;
3858 struct scatterlist *sg;
3859 struct se_task *task;
3860 u32 chained_nents = 0;
3863 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3866 * Walk the struct se_task list and setup scatterlist chains
3867 * for each contiguously allocated struct se_task->task_sg[].
3869 list_for_each_entry(task, &cmd->t_task_list, t_list) {
3874 sg_first = task->task_sg;
3875 chained_nents = task->task_sg_nents;
3877 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
3878 chained_nents += task->task_sg_nents;
3881 * For the padded tasks, use the extra SGL vector allocated
3882 * in transport_allocate_data_tasks() for the sg_prev_nents
3883 * offset into sg_chain() above.
3885 * We do not need the padding for the last task (or a single
3886 * task), but in that case we will never use the sg_prev_nents
3887 * value below which would be incorrect.
3889 sg_prev_nents = (task->task_sg_nents + 1);
3890 sg_prev = task->task_sg;
3893 * Setup the starting pointer and total t_tasks_sg_linked_no including
3894 * padding SGs for linking and to mark the end.
3896 cmd->t_tasks_sg_chained = sg_first;
3897 cmd->t_tasks_sg_chained_no = chained_nents;
3899 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
3900 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3901 cmd->t_tasks_sg_chained_no);
3903 for_each_sg(cmd->t_tasks_sg_chained, sg,
3904 cmd->t_tasks_sg_chained_no, i) {
3906 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
3907 i, sg, sg_page(sg), sg->length, sg->offset);
3908 if (sg_is_chain(sg))
3909 pr_debug("SG: %p sg_is_chain=1\n", sg);
3911 pr_debug("SG: %p sg_is_last=1\n", sg);
3914 EXPORT_SYMBOL(transport_do_task_sg_chain);
3917 * Break up cmd into chunks transport can handle
3919 static int transport_allocate_data_tasks(
3921 unsigned long long lba,
3922 enum dma_data_direction data_direction,
3923 struct scatterlist *sgl,
3924 unsigned int sgl_nents)
3926 struct se_task *task;
3927 struct se_device *dev = cmd->se_dev;
3928 unsigned long flags;
3930 sector_t sectors, dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3931 u32 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
3932 struct scatterlist *sg;
3933 struct scatterlist *cmd_sg;
3935 WARN_ON(cmd->data_length % sector_size);
3936 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
3937 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
3940 for (i = 0; i < task_count; i++) {
3941 unsigned int task_size, task_sg_nents_padded;
3944 task = transport_generic_get_task(cmd, data_direction);
3948 task->task_lba = lba;
3949 task->task_sectors = min(sectors, dev_max_sectors);
3950 task->task_size = task->task_sectors * sector_size;
3953 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3954 * in order to calculate the number per task SGL entries
3956 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
3958 * Check if the fabric module driver is requesting that all
3959 * struct se_task->task_sg[] be chained together.. If so,
3960 * then allocate an extra padding SG entry for linking and
3961 * marking the end of the chained SGL for every task except
3962 * the last one for (task_count > 1) operation, or skipping
3963 * the extra padding for the (task_count == 1) case.
3965 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3966 task_sg_nents_padded = (task->task_sg_nents + 1);
3968 task_sg_nents_padded = task->task_sg_nents;
3970 task->task_sg = kmalloc(sizeof(struct scatterlist) *
3971 task_sg_nents_padded, GFP_KERNEL);
3972 if (!task->task_sg) {
3973 cmd->se_dev->transport->free_task(task);
3977 sg_init_table(task->task_sg, task_sg_nents_padded);
3979 task_size = task->task_size;
3981 /* Build new sgl, only up to task_size */
3982 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
3983 if (cmd_sg->length > task_size)
3987 task_size -= cmd_sg->length;
3988 cmd_sg = sg_next(cmd_sg);
3991 lba += task->task_sectors;
3992 sectors -= task->task_sectors;
3994 spin_lock_irqsave(&cmd->t_state_lock, flags);
3995 list_add_tail(&task->t_list, &cmd->t_task_list);
3996 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4003 transport_allocate_control_task(struct se_cmd *cmd)
4005 struct se_task *task;
4006 unsigned long flags;
4008 task = transport_generic_get_task(cmd, cmd->data_direction);
4012 task->task_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents,
4014 if (!task->task_sg) {
4015 cmd->se_dev->transport->free_task(task);
4019 memcpy(task->task_sg, cmd->t_data_sg,
4020 sizeof(struct scatterlist) * cmd->t_data_nents);
4021 task->task_size = cmd->data_length;
4022 task->task_sg_nents = cmd->t_data_nents;
4024 spin_lock_irqsave(&cmd->t_state_lock, flags);
4025 list_add_tail(&task->t_list, &cmd->t_task_list);
4026 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4028 /* Success! Return number of tasks allocated */
4032 static u32 transport_allocate_tasks(
4034 unsigned long long lba,
4035 enum dma_data_direction data_direction,
4036 struct scatterlist *sgl,
4037 unsigned int sgl_nents)
4039 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
4040 if (transport_cmd_get_valid_sectors(cmd) < 0)
4043 return transport_allocate_data_tasks(cmd, lba, data_direction,
4046 return transport_allocate_control_task(cmd);
4051 /* transport_generic_new_cmd(): Called from transport_processing_thread()
4053 * Allocate storage transport resources from a set of values predefined
4054 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4055 * Any non zero return here is treated as an "out of resource' op here.
4058 * Generate struct se_task(s) and/or their payloads for this CDB.
4060 int transport_generic_new_cmd(struct se_cmd *cmd)
4065 * Determine is the TCM fabric module has already allocated physical
4066 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4069 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
4071 ret = transport_generic_get_mem(cmd);
4076 * Call transport_new_cmd_obj() to invoke transport_allocate_tasks() for
4077 * control or data CDB types, and perform the map to backend subsystem
4078 * code from SGL memory allocated here by transport_generic_get_mem(), or
4079 * via pre-existing SGL memory setup explictly by fabric module code with
4080 * transport_generic_map_mem_to_cmd().
4082 ret = transport_new_cmd_obj(cmd);
4086 * For WRITEs, let the fabric know its buffer is ready..
4087 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4088 * will be added to the struct se_device execution queue after its WRITE
4089 * data has arrived. (ie: It gets handled by the transport processing
4090 * thread a second time)
4092 if (cmd->data_direction == DMA_TO_DEVICE) {
4093 transport_add_tasks_to_state_queue(cmd);
4094 return transport_generic_write_pending(cmd);
4097 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4098 * to the execution queue.
4100 transport_execute_tasks(cmd);
4103 EXPORT_SYMBOL(transport_generic_new_cmd);
4105 /* transport_generic_process_write():
4109 void transport_generic_process_write(struct se_cmd *cmd)
4111 transport_execute_tasks(cmd);
4113 EXPORT_SYMBOL(transport_generic_process_write);
4115 static void transport_write_pending_qf(struct se_cmd *cmd)
4117 if (cmd->se_tfo->write_pending(cmd) == -EAGAIN) {
4118 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
4120 transport_handle_queue_full(cmd, cmd->se_dev);
4124 static int transport_generic_write_pending(struct se_cmd *cmd)
4126 unsigned long flags;
4129 spin_lock_irqsave(&cmd->t_state_lock, flags);
4130 cmd->t_state = TRANSPORT_WRITE_PENDING;
4131 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4134 * Clear the se_cmd for WRITE_PENDING status in order to set
4135 * cmd->t_transport_active=0 so that transport_generic_handle_data
4136 * can be called from HW target mode interrupt code. This is safe
4137 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
4138 * because the se_cmd->se_lun pointer is not being cleared.
4140 transport_cmd_check_stop(cmd, 1, 0);
4143 * Call the fabric write_pending function here to let the
4144 * frontend know that WRITE buffers are ready.
4146 ret = cmd->se_tfo->write_pending(cmd);
4152 return PYX_TRANSPORT_WRITE_PENDING;
4155 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
4156 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
4157 transport_handle_queue_full(cmd, cmd->se_dev);
4162 * transport_release_cmd - free a command
4163 * @cmd: command to free
4165 * This routine unconditionally frees a command, and reference counting
4166 * or list removal must be done in the caller.
4168 void transport_release_cmd(struct se_cmd *cmd)
4170 BUG_ON(!cmd->se_tfo);
4172 if (cmd->se_tmr_req)
4173 core_tmr_release_req(cmd->se_tmr_req);
4174 if (cmd->t_task_cdb != cmd->__t_task_cdb)
4175 kfree(cmd->t_task_cdb);
4176 cmd->se_tfo->release_cmd(cmd);
4178 EXPORT_SYMBOL(transport_release_cmd);
4180 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
4182 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
4183 if (wait_for_tasks && cmd->se_tmr_req)
4184 transport_wait_for_tasks(cmd);
4186 transport_release_cmd(cmd);
4189 transport_wait_for_tasks(cmd);
4191 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4194 transport_lun_remove_cmd(cmd);
4196 transport_free_dev_tasks(cmd);
4198 transport_put_cmd(cmd);
4201 EXPORT_SYMBOL(transport_generic_free_cmd);
4203 /* transport_lun_wait_for_tasks():
4205 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4206 * an struct se_lun to be successfully shutdown.
4208 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4210 unsigned long flags;
4213 * If the frontend has already requested this struct se_cmd to
4214 * be stopped, we can safely ignore this struct se_cmd.
4216 spin_lock_irqsave(&cmd->t_state_lock, flags);
4217 if (atomic_read(&cmd->t_transport_stop)) {
4218 atomic_set(&cmd->transport_lun_stop, 0);
4219 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4220 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
4221 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4222 transport_cmd_check_stop(cmd, 1, 0);
4225 atomic_set(&cmd->transport_lun_fe_stop, 1);
4226 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4228 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4230 ret = transport_stop_tasks_for_cmd(cmd);
4232 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4233 " %d\n", cmd, cmd->t_task_list_num, ret);
4235 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4236 cmd->se_tfo->get_task_tag(cmd));
4237 wait_for_completion(&cmd->transport_lun_stop_comp);
4238 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4239 cmd->se_tfo->get_task_tag(cmd));
4241 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
4246 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4248 struct se_cmd *cmd = NULL;
4249 unsigned long lun_flags, cmd_flags;
4251 * Do exception processing and return CHECK_CONDITION status to the
4254 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4255 while (!list_empty(&lun->lun_cmd_list)) {
4256 cmd = list_first_entry(&lun->lun_cmd_list,
4257 struct se_cmd, se_lun_node);
4258 list_del(&cmd->se_lun_node);
4260 atomic_set(&cmd->transport_lun_active, 0);
4262 * This will notify iscsi_target_transport.c:
4263 * transport_cmd_check_stop() that a LUN shutdown is in
4264 * progress for the iscsi_cmd_t.
4266 spin_lock(&cmd->t_state_lock);
4267 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4268 "_lun_stop for ITT: 0x%08x\n",
4269 cmd->se_lun->unpacked_lun,
4270 cmd->se_tfo->get_task_tag(cmd));
4271 atomic_set(&cmd->transport_lun_stop, 1);
4272 spin_unlock(&cmd->t_state_lock);
4274 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4277 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4278 cmd->se_tfo->get_task_tag(cmd),
4279 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4283 * If the Storage engine still owns the iscsi_cmd_t, determine
4284 * and/or stop its context.
4286 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4287 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4288 cmd->se_tfo->get_task_tag(cmd));
4290 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4291 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4295 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4296 "_wait_for_tasks(): SUCCESS\n",
4297 cmd->se_lun->unpacked_lun,
4298 cmd->se_tfo->get_task_tag(cmd));
4300 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4301 if (!atomic_read(&cmd->transport_dev_active)) {
4302 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4305 atomic_set(&cmd->transport_dev_active, 0);
4306 transport_all_task_dev_remove_state(cmd);
4307 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4309 transport_free_dev_tasks(cmd);
4311 * The Storage engine stopped this struct se_cmd before it was
4312 * send to the fabric frontend for delivery back to the
4313 * Initiator Node. Return this SCSI CDB back with an
4314 * CHECK_CONDITION status.
4317 transport_send_check_condition_and_sense(cmd,
4318 TCM_NON_EXISTENT_LUN, 0);
4320 * If the fabric frontend is waiting for this iscsi_cmd_t to
4321 * be released, notify the waiting thread now that LU has
4322 * finished accessing it.
4324 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4325 if (atomic_read(&cmd->transport_lun_fe_stop)) {
4326 pr_debug("SE_LUN[%d] - Detected FE stop for"
4327 " struct se_cmd: %p ITT: 0x%08x\n",
4329 cmd, cmd->se_tfo->get_task_tag(cmd));
4331 spin_unlock_irqrestore(&cmd->t_state_lock,
4333 transport_cmd_check_stop(cmd, 1, 0);
4334 complete(&cmd->transport_lun_fe_stop_comp);
4335 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4338 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4339 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4341 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4342 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4344 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4347 static int transport_clear_lun_thread(void *p)
4349 struct se_lun *lun = (struct se_lun *)p;
4351 __transport_clear_lun_from_sessions(lun);
4352 complete(&lun->lun_shutdown_comp);
4357 int transport_clear_lun_from_sessions(struct se_lun *lun)
4359 struct task_struct *kt;
4361 kt = kthread_run(transport_clear_lun_thread, lun,
4362 "tcm_cl_%u", lun->unpacked_lun);
4364 pr_err("Unable to start clear_lun thread\n");
4367 wait_for_completion(&lun->lun_shutdown_comp);
4373 * transport_wait_for_tasks - wait for completion to occur
4374 * @cmd: command to wait
4376 * Called from frontend fabric context to wait for storage engine
4377 * to pause and/or release frontend generated struct se_cmd.
4379 void transport_wait_for_tasks(struct se_cmd *cmd)
4381 unsigned long flags;
4383 spin_lock_irqsave(&cmd->t_state_lock, flags);
4384 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4385 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4389 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4390 * has been set in transport_set_supported_SAM_opcode().
4392 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4393 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4397 * If we are already stopped due to an external event (ie: LUN shutdown)
4398 * sleep until the connection can have the passed struct se_cmd back.
4399 * The cmd->transport_lun_stopped_sem will be upped by
4400 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4401 * has completed its operation on the struct se_cmd.
4403 if (atomic_read(&cmd->transport_lun_stop)) {
4405 pr_debug("wait_for_tasks: Stopping"
4406 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4407 "_stop_comp); for ITT: 0x%08x\n",
4408 cmd->se_tfo->get_task_tag(cmd));
4410 * There is a special case for WRITES where a FE exception +
4411 * LUN shutdown means ConfigFS context is still sleeping on
4412 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4413 * We go ahead and up transport_lun_stop_comp just to be sure
4416 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4417 complete(&cmd->transport_lun_stop_comp);
4418 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4419 spin_lock_irqsave(&cmd->t_state_lock, flags);
4421 transport_all_task_dev_remove_state(cmd);
4423 * At this point, the frontend who was the originator of this
4424 * struct se_cmd, now owns the structure and can be released through
4425 * normal means below.
4427 pr_debug("wait_for_tasks: Stopped"
4428 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4429 "stop_comp); for ITT: 0x%08x\n",
4430 cmd->se_tfo->get_task_tag(cmd));
4432 atomic_set(&cmd->transport_lun_stop, 0);
4434 if (!atomic_read(&cmd->t_transport_active) ||
4435 atomic_read(&cmd->t_transport_aborted)) {
4436 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4440 atomic_set(&cmd->t_transport_stop, 1);
4442 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4443 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
4444 " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
4445 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
4446 cmd->deferred_t_state);
4448 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4450 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4452 wait_for_completion(&cmd->t_transport_stop_comp);
4454 spin_lock_irqsave(&cmd->t_state_lock, flags);
4455 atomic_set(&cmd->t_transport_active, 0);
4456 atomic_set(&cmd->t_transport_stop, 0);
4458 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4459 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4460 cmd->se_tfo->get_task_tag(cmd));
4462 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4464 EXPORT_SYMBOL(transport_wait_for_tasks);
4466 static int transport_get_sense_codes(
4471 *asc = cmd->scsi_asc;
4472 *ascq = cmd->scsi_ascq;
4477 static int transport_set_sense_codes(
4482 cmd->scsi_asc = asc;
4483 cmd->scsi_ascq = ascq;
4488 int transport_send_check_condition_and_sense(
4493 unsigned char *buffer = cmd->sense_buffer;
4494 unsigned long flags;
4496 u8 asc = 0, ascq = 0;
4498 spin_lock_irqsave(&cmd->t_state_lock, flags);
4499 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4500 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4503 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4504 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4506 if (!reason && from_transport)
4509 if (!from_transport)
4510 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4512 * Data Segment and SenseLength of the fabric response PDU.
4514 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4515 * from include/scsi/scsi_cmnd.h
4517 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4518 TRANSPORT_SENSE_BUFFER);
4520 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4521 * SENSE KEY values from include/scsi/scsi.h
4524 case TCM_NON_EXISTENT_LUN:
4526 buffer[offset] = 0x70;
4527 /* ILLEGAL REQUEST */
4528 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4529 /* LOGICAL UNIT NOT SUPPORTED */
4530 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4532 case TCM_UNSUPPORTED_SCSI_OPCODE:
4533 case TCM_SECTOR_COUNT_TOO_MANY:
4535 buffer[offset] = 0x70;
4536 /* ILLEGAL REQUEST */
4537 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4538 /* INVALID COMMAND OPERATION CODE */
4539 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4541 case TCM_UNKNOWN_MODE_PAGE:
4543 buffer[offset] = 0x70;
4544 /* ILLEGAL REQUEST */
4545 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4546 /* INVALID FIELD IN CDB */
4547 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4549 case TCM_CHECK_CONDITION_ABORT_CMD:
4551 buffer[offset] = 0x70;
4552 /* ABORTED COMMAND */
4553 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4554 /* BUS DEVICE RESET FUNCTION OCCURRED */
4555 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4556 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4558 case TCM_INCORRECT_AMOUNT_OF_DATA:
4560 buffer[offset] = 0x70;
4561 /* ABORTED COMMAND */
4562 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4564 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4565 /* NOT ENOUGH UNSOLICITED DATA */
4566 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4568 case TCM_INVALID_CDB_FIELD:
4570 buffer[offset] = 0x70;
4571 /* ABORTED COMMAND */
4572 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4573 /* INVALID FIELD IN CDB */
4574 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4576 case TCM_INVALID_PARAMETER_LIST:
4578 buffer[offset] = 0x70;
4579 /* ABORTED COMMAND */
4580 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4581 /* INVALID FIELD IN PARAMETER LIST */
4582 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4584 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4586 buffer[offset] = 0x70;
4587 /* ABORTED COMMAND */
4588 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4590 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4591 /* UNEXPECTED_UNSOLICITED_DATA */
4592 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4594 case TCM_SERVICE_CRC_ERROR:
4596 buffer[offset] = 0x70;
4597 /* ABORTED COMMAND */
4598 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4599 /* PROTOCOL SERVICE CRC ERROR */
4600 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4602 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4604 case TCM_SNACK_REJECTED:
4606 buffer[offset] = 0x70;
4607 /* ABORTED COMMAND */
4608 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4610 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4611 /* FAILED RETRANSMISSION REQUEST */
4612 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4614 case TCM_WRITE_PROTECTED:
4616 buffer[offset] = 0x70;
4618 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4619 /* WRITE PROTECTED */
4620 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4622 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4624 buffer[offset] = 0x70;
4625 /* UNIT ATTENTION */
4626 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4627 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4628 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4629 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4631 case TCM_CHECK_CONDITION_NOT_READY:
4633 buffer[offset] = 0x70;
4635 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4636 transport_get_sense_codes(cmd, &asc, &ascq);
4637 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4638 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4640 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4643 buffer[offset] = 0x70;
4644 /* ILLEGAL REQUEST */
4645 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4646 /* LOGICAL UNIT COMMUNICATION FAILURE */
4647 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4651 * This code uses linux/include/scsi/scsi.h SAM status codes!
4653 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4655 * Automatically padded, this value is encoded in the fabric's
4656 * data_length response PDU containing the SCSI defined sense data.
4658 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4661 return cmd->se_tfo->queue_status(cmd);
4663 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4665 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4669 if (atomic_read(&cmd->t_transport_aborted) != 0) {
4671 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4674 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4675 " status for CDB: 0x%02x ITT: 0x%08x\n",
4677 cmd->se_tfo->get_task_tag(cmd));
4679 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4680 cmd->se_tfo->queue_status(cmd);
4685 EXPORT_SYMBOL(transport_check_aborted_status);
4687 void transport_send_task_abort(struct se_cmd *cmd)
4689 unsigned long flags;
4691 spin_lock_irqsave(&cmd->t_state_lock, flags);
4692 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4693 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4696 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4699 * If there are still expected incoming fabric WRITEs, we wait
4700 * until until they have completed before sending a TASK_ABORTED
4701 * response. This response with TASK_ABORTED status will be
4702 * queued back to fabric module by transport_check_aborted_status().
4704 if (cmd->data_direction == DMA_TO_DEVICE) {
4705 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4706 atomic_inc(&cmd->t_transport_aborted);
4707 smp_mb__after_atomic_inc();
4708 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4709 transport_new_cmd_failure(cmd);
4713 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4715 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4716 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4717 cmd->se_tfo->get_task_tag(cmd));
4719 cmd->se_tfo->queue_status(cmd);
4722 /* transport_generic_do_tmr():
4726 int transport_generic_do_tmr(struct se_cmd *cmd)
4728 struct se_device *dev = cmd->se_dev;
4729 struct se_tmr_req *tmr = cmd->se_tmr_req;
4732 switch (tmr->function) {
4733 case TMR_ABORT_TASK:
4734 tmr->response = TMR_FUNCTION_REJECTED;
4736 case TMR_ABORT_TASK_SET:
4738 case TMR_CLEAR_TASK_SET:
4739 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4742 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4743 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4744 TMR_FUNCTION_REJECTED;
4746 case TMR_TARGET_WARM_RESET:
4747 tmr->response = TMR_FUNCTION_REJECTED;
4749 case TMR_TARGET_COLD_RESET:
4750 tmr->response = TMR_FUNCTION_REJECTED;
4753 pr_err("Uknown TMR function: 0x%02x.\n",
4755 tmr->response = TMR_FUNCTION_REJECTED;
4759 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4760 cmd->se_tfo->queue_tm_rsp(cmd);
4762 transport_cmd_check_stop(cmd, 2, 0);
4766 /* transport_processing_thread():
4770 static int transport_processing_thread(void *param)
4774 struct se_device *dev = (struct se_device *) param;
4776 set_user_nice(current, -20);
4778 while (!kthread_should_stop()) {
4779 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4780 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4781 kthread_should_stop());
4786 __transport_execute_tasks(dev);
4788 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4792 switch (cmd->t_state) {
4793 case TRANSPORT_NEW_CMD:
4796 case TRANSPORT_NEW_CMD_MAP:
4797 if (!cmd->se_tfo->new_cmd_map) {
4798 pr_err("cmd->se_tfo->new_cmd_map is"
4799 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4802 ret = cmd->se_tfo->new_cmd_map(cmd);
4804 cmd->transport_error_status = ret;
4805 transport_generic_request_failure(cmd, NULL,
4806 0, (cmd->data_direction !=
4810 ret = transport_generic_new_cmd(cmd);
4814 cmd->transport_error_status = ret;
4815 transport_generic_request_failure(cmd, NULL,
4816 0, (cmd->data_direction !=
4820 case TRANSPORT_PROCESS_WRITE:
4821 transport_generic_process_write(cmd);
4823 case TRANSPORT_COMPLETE_OK:
4824 transport_stop_all_task_timers(cmd);
4825 transport_generic_complete_ok(cmd);
4827 case TRANSPORT_REMOVE:
4828 transport_put_cmd(cmd);
4830 case TRANSPORT_FREE_CMD_INTR:
4831 transport_generic_free_cmd(cmd, 0);
4833 case TRANSPORT_PROCESS_TMR:
4834 transport_generic_do_tmr(cmd);
4836 case TRANSPORT_COMPLETE_FAILURE:
4837 transport_generic_request_failure(cmd, NULL, 1, 1);
4839 case TRANSPORT_COMPLETE_TIMEOUT:
4840 transport_stop_all_task_timers(cmd);
4841 transport_generic_request_timeout(cmd);
4843 case TRANSPORT_COMPLETE_QF_WP:
4844 transport_write_pending_qf(cmd);
4846 case TRANSPORT_COMPLETE_QF_OK:
4847 transport_complete_qf(cmd);
4850 pr_err("Unknown t_state: %d deferred_t_state:"
4851 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
4852 " %u\n", cmd->t_state, cmd->deferred_t_state,
4853 cmd->se_tfo->get_task_tag(cmd),
4854 cmd->se_tfo->get_cmd_state(cmd),
4855 cmd->se_lun->unpacked_lun);
4863 WARN_ON(!list_empty(&dev->state_task_list));
4864 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4865 dev->process_thread = NULL;