1 /*******************************************************************************
2 * Filename: target_core_transport.c
4 * This file contains the Generic Target Engine Core.
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
11 * Nicholas A. Bellinger <nab@kernel.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ******************************************************************************/
29 #include <linux/net.h>
30 #include <linux/delay.h>
31 #include <linux/string.h>
32 #include <linux/timer.h>
33 #include <linux/slab.h>
34 #include <linux/blkdev.h>
35 #include <linux/spinlock.h>
36 #include <linux/kthread.h>
38 #include <linux/cdrom.h>
39 #include <linux/module.h>
40 #include <asm/unaligned.h>
43 #include <scsi/scsi.h>
44 #include <scsi/scsi_cmnd.h>
45 #include <scsi/scsi_tcq.h>
47 #include <target/target_core_base.h>
48 #include <target/target_core_device.h>
49 #include <target/target_core_tmr.h>
50 #include <target/target_core_tpg.h>
51 #include <target/target_core_transport.h>
52 #include <target/target_core_fabric_ops.h>
53 #include <target/target_core_configfs.h>
55 #include "target_core_alua.h"
56 #include "target_core_cdb.h"
57 #include "target_core_hba.h"
58 #include "target_core_pr.h"
59 #include "target_core_ua.h"
61 static int sub_api_initialized;
63 static struct workqueue_struct *target_completion_wq;
64 static struct kmem_cache *se_cmd_cache;
65 static struct kmem_cache *se_sess_cache;
66 struct kmem_cache *se_tmr_req_cache;
67 struct kmem_cache *se_ua_cache;
68 struct kmem_cache *t10_pr_reg_cache;
69 struct kmem_cache *t10_alua_lu_gp_cache;
70 struct kmem_cache *t10_alua_lu_gp_mem_cache;
71 struct kmem_cache *t10_alua_tg_pt_gp_cache;
72 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
74 static int transport_generic_write_pending(struct se_cmd *);
75 static int transport_processing_thread(void *param);
76 static int __transport_execute_tasks(struct se_device *dev);
77 static void transport_complete_task_attr(struct se_cmd *cmd);
78 static void transport_handle_queue_full(struct se_cmd *cmd,
79 struct se_device *dev);
80 static void transport_free_dev_tasks(struct se_cmd *cmd);
81 static int transport_generic_get_mem(struct se_cmd *cmd);
82 static void transport_put_cmd(struct se_cmd *cmd);
83 static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
84 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
85 static void transport_generic_request_failure(struct se_cmd *);
86 static void target_complete_ok_work(struct work_struct *work);
88 int init_se_kmem_caches(void)
90 se_cmd_cache = kmem_cache_create("se_cmd_cache",
91 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
93 pr_err("kmem_cache_create for struct se_cmd failed\n");
96 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
97 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
99 if (!se_tmr_req_cache) {
100 pr_err("kmem_cache_create() for struct se_tmr_req"
102 goto out_free_cmd_cache;
104 se_sess_cache = kmem_cache_create("se_sess_cache",
105 sizeof(struct se_session), __alignof__(struct se_session),
107 if (!se_sess_cache) {
108 pr_err("kmem_cache_create() for struct se_session"
110 goto out_free_tmr_req_cache;
112 se_ua_cache = kmem_cache_create("se_ua_cache",
113 sizeof(struct se_ua), __alignof__(struct se_ua),
116 pr_err("kmem_cache_create() for struct se_ua failed\n");
117 goto out_free_sess_cache;
119 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
120 sizeof(struct t10_pr_registration),
121 __alignof__(struct t10_pr_registration), 0, NULL);
122 if (!t10_pr_reg_cache) {
123 pr_err("kmem_cache_create() for struct t10_pr_registration"
125 goto out_free_ua_cache;
127 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
128 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
130 if (!t10_alua_lu_gp_cache) {
131 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
133 goto out_free_pr_reg_cache;
135 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
136 sizeof(struct t10_alua_lu_gp_member),
137 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
138 if (!t10_alua_lu_gp_mem_cache) {
139 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
141 goto out_free_lu_gp_cache;
143 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
144 sizeof(struct t10_alua_tg_pt_gp),
145 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
146 if (!t10_alua_tg_pt_gp_cache) {
147 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
149 goto out_free_lu_gp_mem_cache;
151 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
152 "t10_alua_tg_pt_gp_mem_cache",
153 sizeof(struct t10_alua_tg_pt_gp_member),
154 __alignof__(struct t10_alua_tg_pt_gp_member),
156 if (!t10_alua_tg_pt_gp_mem_cache) {
157 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
159 goto out_free_tg_pt_gp_cache;
162 target_completion_wq = alloc_workqueue("target_completion",
164 if (!target_completion_wq)
165 goto out_free_tg_pt_gp_mem_cache;
169 out_free_tg_pt_gp_mem_cache:
170 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
171 out_free_tg_pt_gp_cache:
172 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
173 out_free_lu_gp_mem_cache:
174 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
175 out_free_lu_gp_cache:
176 kmem_cache_destroy(t10_alua_lu_gp_cache);
177 out_free_pr_reg_cache:
178 kmem_cache_destroy(t10_pr_reg_cache);
180 kmem_cache_destroy(se_ua_cache);
182 kmem_cache_destroy(se_sess_cache);
183 out_free_tmr_req_cache:
184 kmem_cache_destroy(se_tmr_req_cache);
186 kmem_cache_destroy(se_cmd_cache);
191 void release_se_kmem_caches(void)
193 destroy_workqueue(target_completion_wq);
194 kmem_cache_destroy(se_cmd_cache);
195 kmem_cache_destroy(se_tmr_req_cache);
196 kmem_cache_destroy(se_sess_cache);
197 kmem_cache_destroy(se_ua_cache);
198 kmem_cache_destroy(t10_pr_reg_cache);
199 kmem_cache_destroy(t10_alua_lu_gp_cache);
200 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
201 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
202 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
205 /* This code ensures unique mib indexes are handed out. */
206 static DEFINE_SPINLOCK(scsi_mib_index_lock);
207 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
210 * Allocate a new row index for the entry type specified
212 u32 scsi_get_new_index(scsi_index_t type)
216 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
218 spin_lock(&scsi_mib_index_lock);
219 new_index = ++scsi_mib_index[type];
220 spin_unlock(&scsi_mib_index_lock);
225 void transport_init_queue_obj(struct se_queue_obj *qobj)
227 atomic_set(&qobj->queue_cnt, 0);
228 INIT_LIST_HEAD(&qobj->qobj_list);
229 init_waitqueue_head(&qobj->thread_wq);
230 spin_lock_init(&qobj->cmd_queue_lock);
232 EXPORT_SYMBOL(transport_init_queue_obj);
234 void transport_subsystem_check_init(void)
238 if (sub_api_initialized)
241 ret = request_module("target_core_iblock");
243 pr_err("Unable to load target_core_iblock\n");
245 ret = request_module("target_core_file");
247 pr_err("Unable to load target_core_file\n");
249 ret = request_module("target_core_pscsi");
251 pr_err("Unable to load target_core_pscsi\n");
253 ret = request_module("target_core_stgt");
255 pr_err("Unable to load target_core_stgt\n");
257 sub_api_initialized = 1;
261 struct se_session *transport_init_session(void)
263 struct se_session *se_sess;
265 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
267 pr_err("Unable to allocate struct se_session from"
269 return ERR_PTR(-ENOMEM);
271 INIT_LIST_HEAD(&se_sess->sess_list);
272 INIT_LIST_HEAD(&se_sess->sess_acl_list);
273 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
274 INIT_LIST_HEAD(&se_sess->sess_wait_list);
275 spin_lock_init(&se_sess->sess_cmd_lock);
279 EXPORT_SYMBOL(transport_init_session);
282 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
284 void __transport_register_session(
285 struct se_portal_group *se_tpg,
286 struct se_node_acl *se_nacl,
287 struct se_session *se_sess,
288 void *fabric_sess_ptr)
290 unsigned char buf[PR_REG_ISID_LEN];
292 se_sess->se_tpg = se_tpg;
293 se_sess->fabric_sess_ptr = fabric_sess_ptr;
295 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
297 * Only set for struct se_session's that will actually be moving I/O.
298 * eg: *NOT* discovery sessions.
302 * If the fabric module supports an ISID based TransportID,
303 * save this value in binary from the fabric I_T Nexus now.
305 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
306 memset(&buf[0], 0, PR_REG_ISID_LEN);
307 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
308 &buf[0], PR_REG_ISID_LEN);
309 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
311 spin_lock_irq(&se_nacl->nacl_sess_lock);
313 * The se_nacl->nacl_sess pointer will be set to the
314 * last active I_T Nexus for each struct se_node_acl.
316 se_nacl->nacl_sess = se_sess;
318 list_add_tail(&se_sess->sess_acl_list,
319 &se_nacl->acl_sess_list);
320 spin_unlock_irq(&se_nacl->nacl_sess_lock);
322 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
324 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
325 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
327 EXPORT_SYMBOL(__transport_register_session);
329 void transport_register_session(
330 struct se_portal_group *se_tpg,
331 struct se_node_acl *se_nacl,
332 struct se_session *se_sess,
333 void *fabric_sess_ptr)
335 spin_lock_bh(&se_tpg->session_lock);
336 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
337 spin_unlock_bh(&se_tpg->session_lock);
339 EXPORT_SYMBOL(transport_register_session);
341 void transport_deregister_session_configfs(struct se_session *se_sess)
343 struct se_node_acl *se_nacl;
346 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
348 se_nacl = se_sess->se_node_acl;
350 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
351 list_del(&se_sess->sess_acl_list);
353 * If the session list is empty, then clear the pointer.
354 * Otherwise, set the struct se_session pointer from the tail
355 * element of the per struct se_node_acl active session list.
357 if (list_empty(&se_nacl->acl_sess_list))
358 se_nacl->nacl_sess = NULL;
360 se_nacl->nacl_sess = container_of(
361 se_nacl->acl_sess_list.prev,
362 struct se_session, sess_acl_list);
364 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
367 EXPORT_SYMBOL(transport_deregister_session_configfs);
369 void transport_free_session(struct se_session *se_sess)
371 kmem_cache_free(se_sess_cache, se_sess);
373 EXPORT_SYMBOL(transport_free_session);
375 void transport_deregister_session(struct se_session *se_sess)
377 struct se_portal_group *se_tpg = se_sess->se_tpg;
378 struct se_node_acl *se_nacl;
382 transport_free_session(se_sess);
386 spin_lock_irqsave(&se_tpg->session_lock, flags);
387 list_del(&se_sess->sess_list);
388 se_sess->se_tpg = NULL;
389 se_sess->fabric_sess_ptr = NULL;
390 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
393 * Determine if we need to do extra work for this initiator node's
394 * struct se_node_acl if it had been previously dynamically generated.
396 se_nacl = se_sess->se_node_acl;
398 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
399 if (se_nacl->dynamic_node_acl) {
400 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
402 list_del(&se_nacl->acl_list);
403 se_tpg->num_node_acls--;
404 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
406 core_tpg_wait_for_nacl_pr_ref(se_nacl);
407 core_free_device_list_for_node(se_nacl, se_tpg);
408 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
410 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
413 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
416 transport_free_session(se_sess);
418 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
419 se_tpg->se_tpg_tfo->get_fabric_name());
421 EXPORT_SYMBOL(transport_deregister_session);
424 * Called with cmd->t_state_lock held.
426 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
428 struct se_device *dev = cmd->se_dev;
429 struct se_task *task;
435 list_for_each_entry(task, &cmd->t_task_list, t_list) {
436 if (task->task_flags & TF_ACTIVE)
439 if (!atomic_read(&task->task_state_active))
442 spin_lock_irqsave(&dev->execute_task_lock, flags);
443 list_del(&task->t_state_list);
444 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
445 cmd->se_tfo->get_task_tag(cmd), dev, task);
446 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
448 atomic_set(&task->task_state_active, 0);
449 atomic_dec(&cmd->t_task_cdbs_ex_left);
453 /* transport_cmd_check_stop():
455 * 'transport_off = 1' determines if t_transport_active should be cleared.
456 * 'transport_off = 2' determines if task_dev_state should be removed.
458 * A non-zero u8 t_state sets cmd->t_state.
459 * Returns 1 when command is stopped, else 0.
461 static int transport_cmd_check_stop(
468 spin_lock_irqsave(&cmd->t_state_lock, flags);
470 * Determine if IOCTL context caller in requesting the stopping of this
471 * command for LUN shutdown purposes.
473 if (atomic_read(&cmd->transport_lun_stop)) {
474 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
475 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
476 cmd->se_tfo->get_task_tag(cmd));
478 atomic_set(&cmd->t_transport_active, 0);
479 if (transport_off == 2)
480 transport_all_task_dev_remove_state(cmd);
481 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
483 complete(&cmd->transport_lun_stop_comp);
487 * Determine if frontend context caller is requesting the stopping of
488 * this command for frontend exceptions.
490 if (atomic_read(&cmd->t_transport_stop)) {
491 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
492 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
493 cmd->se_tfo->get_task_tag(cmd));
495 if (transport_off == 2)
496 transport_all_task_dev_remove_state(cmd);
499 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
502 if (transport_off == 2)
504 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
506 complete(&cmd->t_transport_stop_comp);
510 atomic_set(&cmd->t_transport_active, 0);
511 if (transport_off == 2) {
512 transport_all_task_dev_remove_state(cmd);
514 * Clear struct se_cmd->se_lun before the transport_off == 2
515 * handoff to fabric module.
519 * Some fabric modules like tcm_loop can release
520 * their internally allocated I/O reference now and
523 * Fabric modules are expected to return '1' here if the
524 * se_cmd being passed is released at this point,
525 * or zero if not being released.
527 if (cmd->se_tfo->check_stop_free != NULL) {
528 spin_unlock_irqrestore(
529 &cmd->t_state_lock, flags);
531 return cmd->se_tfo->check_stop_free(cmd);
534 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
538 cmd->t_state = t_state;
539 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
544 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
546 return transport_cmd_check_stop(cmd, 2, 0);
549 static void transport_lun_remove_cmd(struct se_cmd *cmd)
551 struct se_lun *lun = cmd->se_lun;
557 spin_lock_irqsave(&cmd->t_state_lock, flags);
558 if (!atomic_read(&cmd->transport_dev_active)) {
559 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
562 atomic_set(&cmd->transport_dev_active, 0);
563 transport_all_task_dev_remove_state(cmd);
564 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
568 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
569 if (atomic_read(&cmd->transport_lun_active)) {
570 list_del(&cmd->se_lun_node);
571 atomic_set(&cmd->transport_lun_active, 0);
573 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
574 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
577 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
580 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
582 if (!cmd->se_tmr_req)
583 transport_lun_remove_cmd(cmd);
585 if (transport_cmd_check_stop_to_fabric(cmd))
588 transport_remove_cmd_from_queue(cmd);
589 transport_put_cmd(cmd);
593 static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
596 struct se_device *dev = cmd->se_dev;
597 struct se_queue_obj *qobj = &dev->dev_queue_obj;
601 spin_lock_irqsave(&cmd->t_state_lock, flags);
602 cmd->t_state = t_state;
603 atomic_set(&cmd->t_transport_active, 1);
604 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
607 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
609 /* If the cmd is already on the list, remove it before we add it */
610 if (!list_empty(&cmd->se_queue_node))
611 list_del(&cmd->se_queue_node);
613 atomic_inc(&qobj->queue_cnt);
616 list_add(&cmd->se_queue_node, &qobj->qobj_list);
618 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
619 atomic_set(&cmd->t_transport_queue_active, 1);
620 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
622 wake_up_interruptible(&qobj->thread_wq);
625 static struct se_cmd *
626 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
631 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
632 if (list_empty(&qobj->qobj_list)) {
633 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
636 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
638 atomic_set(&cmd->t_transport_queue_active, 0);
640 list_del_init(&cmd->se_queue_node);
641 atomic_dec(&qobj->queue_cnt);
642 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
647 static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
649 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
652 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
653 if (!atomic_read(&cmd->t_transport_queue_active)) {
654 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
657 atomic_set(&cmd->t_transport_queue_active, 0);
658 atomic_dec(&qobj->queue_cnt);
659 list_del_init(&cmd->se_queue_node);
660 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
662 if (atomic_read(&cmd->t_transport_queue_active)) {
663 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
664 cmd->se_tfo->get_task_tag(cmd),
665 atomic_read(&cmd->t_transport_queue_active));
670 * Completion function used by TCM subsystem plugins (such as FILEIO)
671 * for queueing up response from struct se_subsystem_api->do_task()
673 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
675 struct se_task *task = list_entry(cmd->t_task_list.next,
676 struct se_task, t_list);
679 cmd->scsi_status = SAM_STAT_GOOD;
680 task->task_scsi_status = GOOD;
682 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
683 task->task_se_cmd->scsi_sense_reason =
684 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
688 transport_complete_task(task, good);
690 EXPORT_SYMBOL(transport_complete_sync_cache);
692 static void target_complete_failure_work(struct work_struct *work)
694 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
696 transport_generic_request_failure(cmd);
699 /* transport_complete_task():
701 * Called from interrupt and non interrupt context depending
702 * on the transport plugin.
704 void transport_complete_task(struct se_task *task, int success)
706 struct se_cmd *cmd = task->task_se_cmd;
707 struct se_device *dev = cmd->se_dev;
710 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
711 cmd->t_task_cdb[0], dev);
714 atomic_inc(&dev->depth_left);
716 spin_lock_irqsave(&cmd->t_state_lock, flags);
717 task->task_flags &= ~TF_ACTIVE;
720 * See if any sense data exists, if so set the TASK_SENSE flag.
721 * Also check for any other post completion work that needs to be
722 * done by the plugins.
724 if (dev && dev->transport->transport_complete) {
725 if (dev->transport->transport_complete(task) != 0) {
726 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
727 task->task_sense = 1;
733 * See if we are waiting for outstanding struct se_task
734 * to complete for an exception condition
736 if (task->task_flags & TF_REQUEST_STOP) {
737 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
738 complete(&task->task_stop_comp);
743 cmd->t_tasks_failed = 1;
746 * Decrement the outstanding t_task_cdbs_left count. The last
747 * struct se_task from struct se_cmd will complete itself into the
748 * device queue depending upon int success.
750 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
751 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
755 if (cmd->t_tasks_failed) {
756 if (!task->task_error_status) {
757 task->task_error_status =
758 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
759 cmd->scsi_sense_reason =
760 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
763 INIT_WORK(&cmd->work, target_complete_failure_work);
765 atomic_set(&cmd->t_transport_complete, 1);
766 INIT_WORK(&cmd->work, target_complete_ok_work);
769 cmd->t_state = TRANSPORT_COMPLETE;
770 atomic_set(&cmd->t_transport_active, 1);
771 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
773 queue_work(target_completion_wq, &cmd->work);
775 EXPORT_SYMBOL(transport_complete_task);
778 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
779 * struct se_task list are ready to be added to the active execution list
782 * Called with se_dev_t->execute_task_lock called.
784 static inline int transport_add_task_check_sam_attr(
785 struct se_task *task,
786 struct se_task *task_prev,
787 struct se_device *dev)
790 * No SAM Task attribute emulation enabled, add to tail of
793 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
794 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
798 * HEAD_OF_QUEUE attribute for received CDB, which means
799 * the first task that is associated with a struct se_cmd goes to
800 * head of the struct se_device->execute_task_list, and task_prev
801 * after that for each subsequent task
803 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
804 list_add(&task->t_execute_list,
805 (task_prev != NULL) ?
806 &task_prev->t_execute_list :
807 &dev->execute_task_list);
809 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
810 " in execution queue\n",
811 task->task_se_cmd->t_task_cdb[0]);
815 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
816 * transitioned from Dermant -> Active state, and are added to the end
817 * of the struct se_device->execute_task_list
819 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
823 /* __transport_add_task_to_execute_queue():
825 * Called with se_dev_t->execute_task_lock called.
827 static void __transport_add_task_to_execute_queue(
828 struct se_task *task,
829 struct se_task *task_prev,
830 struct se_device *dev)
834 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
835 atomic_inc(&dev->execute_tasks);
837 if (atomic_read(&task->task_state_active))
840 * Determine if this task needs to go to HEAD_OF_QUEUE for the
841 * state list as well. Running with SAM Task Attribute emulation
842 * will always return head_of_queue == 0 here
845 list_add(&task->t_state_list, (task_prev) ?
846 &task_prev->t_state_list :
847 &dev->state_task_list);
849 list_add_tail(&task->t_state_list, &dev->state_task_list);
851 atomic_set(&task->task_state_active, 1);
853 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
854 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
858 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
860 struct se_device *dev = cmd->se_dev;
861 struct se_task *task;
864 spin_lock_irqsave(&cmd->t_state_lock, flags);
865 list_for_each_entry(task, &cmd->t_task_list, t_list) {
866 if (atomic_read(&task->task_state_active))
869 spin_lock(&dev->execute_task_lock);
870 list_add_tail(&task->t_state_list, &dev->state_task_list);
871 atomic_set(&task->task_state_active, 1);
873 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
874 task->task_se_cmd->se_tfo->get_task_tag(
875 task->task_se_cmd), task, dev);
877 spin_unlock(&dev->execute_task_lock);
879 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
882 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
884 struct se_device *dev = cmd->se_dev;
885 struct se_task *task, *task_prev = NULL;
888 spin_lock_irqsave(&dev->execute_task_lock, flags);
889 list_for_each_entry(task, &cmd->t_task_list, t_list) {
890 if (!list_empty(&task->t_execute_list))
893 * __transport_add_task_to_execute_queue() handles the
894 * SAM Task Attribute emulation if enabled
896 __transport_add_task_to_execute_queue(task, task_prev, dev);
899 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
902 void __transport_remove_task_from_execute_queue(struct se_task *task,
903 struct se_device *dev)
905 list_del_init(&task->t_execute_list);
906 atomic_dec(&dev->execute_tasks);
909 void transport_remove_task_from_execute_queue(
910 struct se_task *task,
911 struct se_device *dev)
915 if (WARN_ON(list_empty(&task->t_execute_list)))
918 spin_lock_irqsave(&dev->execute_task_lock, flags);
919 __transport_remove_task_from_execute_queue(task, dev);
920 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
924 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
927 static void target_qf_do_work(struct work_struct *work)
929 struct se_device *dev = container_of(work, struct se_device,
931 LIST_HEAD(qf_cmd_list);
932 struct se_cmd *cmd, *cmd_tmp;
934 spin_lock_irq(&dev->qf_cmd_lock);
935 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
936 spin_unlock_irq(&dev->qf_cmd_lock);
938 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
939 list_del(&cmd->se_qf_node);
940 atomic_dec(&dev->dev_qf_count);
941 smp_mb__after_atomic_dec();
943 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
944 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
945 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
946 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
949 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
953 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
955 switch (cmd->data_direction) {
958 case DMA_FROM_DEVICE:
962 case DMA_BIDIRECTIONAL:
971 void transport_dump_dev_state(
972 struct se_device *dev,
976 *bl += sprintf(b + *bl, "Status: ");
977 switch (dev->dev_status) {
978 case TRANSPORT_DEVICE_ACTIVATED:
979 *bl += sprintf(b + *bl, "ACTIVATED");
981 case TRANSPORT_DEVICE_DEACTIVATED:
982 *bl += sprintf(b + *bl, "DEACTIVATED");
984 case TRANSPORT_DEVICE_SHUTDOWN:
985 *bl += sprintf(b + *bl, "SHUTDOWN");
987 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
988 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
989 *bl += sprintf(b + *bl, "OFFLINE");
992 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
996 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
997 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
999 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
1000 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
1001 *bl += sprintf(b + *bl, " ");
1004 void transport_dump_vpd_proto_id(
1005 struct t10_vpd *vpd,
1006 unsigned char *p_buf,
1009 unsigned char buf[VPD_TMP_BUF_SIZE];
1012 memset(buf, 0, VPD_TMP_BUF_SIZE);
1013 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1015 switch (vpd->protocol_identifier) {
1017 sprintf(buf+len, "Fibre Channel\n");
1020 sprintf(buf+len, "Parallel SCSI\n");
1023 sprintf(buf+len, "SSA\n");
1026 sprintf(buf+len, "IEEE 1394\n");
1029 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1033 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1036 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1039 sprintf(buf+len, "Automation/Drive Interface Transport"
1043 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1046 sprintf(buf+len, "Unknown 0x%02x\n",
1047 vpd->protocol_identifier);
1052 strncpy(p_buf, buf, p_buf_len);
1054 pr_debug("%s", buf);
1058 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1061 * Check if the Protocol Identifier Valid (PIV) bit is set..
1063 * from spc3r23.pdf section 7.5.1
1065 if (page_83[1] & 0x80) {
1066 vpd->protocol_identifier = (page_83[0] & 0xf0);
1067 vpd->protocol_identifier_set = 1;
1068 transport_dump_vpd_proto_id(vpd, NULL, 0);
1071 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1073 int transport_dump_vpd_assoc(
1074 struct t10_vpd *vpd,
1075 unsigned char *p_buf,
1078 unsigned char buf[VPD_TMP_BUF_SIZE];
1082 memset(buf, 0, VPD_TMP_BUF_SIZE);
1083 len = sprintf(buf, "T10 VPD Identifier Association: ");
1085 switch (vpd->association) {
1087 sprintf(buf+len, "addressed logical unit\n");
1090 sprintf(buf+len, "target port\n");
1093 sprintf(buf+len, "SCSI target device\n");
1096 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1102 strncpy(p_buf, buf, p_buf_len);
1104 pr_debug("%s", buf);
1109 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1112 * The VPD identification association..
1114 * from spc3r23.pdf Section 7.6.3.1 Table 297
1116 vpd->association = (page_83[1] & 0x30);
1117 return transport_dump_vpd_assoc(vpd, NULL, 0);
1119 EXPORT_SYMBOL(transport_set_vpd_assoc);
1121 int transport_dump_vpd_ident_type(
1122 struct t10_vpd *vpd,
1123 unsigned char *p_buf,
1126 unsigned char buf[VPD_TMP_BUF_SIZE];
1130 memset(buf, 0, VPD_TMP_BUF_SIZE);
1131 len = sprintf(buf, "T10 VPD Identifier Type: ");
1133 switch (vpd->device_identifier_type) {
1135 sprintf(buf+len, "Vendor specific\n");
1138 sprintf(buf+len, "T10 Vendor ID based\n");
1141 sprintf(buf+len, "EUI-64 based\n");
1144 sprintf(buf+len, "NAA\n");
1147 sprintf(buf+len, "Relative target port identifier\n");
1150 sprintf(buf+len, "SCSI name string\n");
1153 sprintf(buf+len, "Unsupported: 0x%02x\n",
1154 vpd->device_identifier_type);
1160 if (p_buf_len < strlen(buf)+1)
1162 strncpy(p_buf, buf, p_buf_len);
1164 pr_debug("%s", buf);
1170 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1173 * The VPD identifier type..
1175 * from spc3r23.pdf Section 7.6.3.1 Table 298
1177 vpd->device_identifier_type = (page_83[1] & 0x0f);
1178 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1180 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1182 int transport_dump_vpd_ident(
1183 struct t10_vpd *vpd,
1184 unsigned char *p_buf,
1187 unsigned char buf[VPD_TMP_BUF_SIZE];
1190 memset(buf, 0, VPD_TMP_BUF_SIZE);
1192 switch (vpd->device_identifier_code_set) {
1193 case 0x01: /* Binary */
1194 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1195 &vpd->device_identifier[0]);
1197 case 0x02: /* ASCII */
1198 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1199 &vpd->device_identifier[0]);
1201 case 0x03: /* UTF-8 */
1202 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1203 &vpd->device_identifier[0]);
1206 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1207 " 0x%02x", vpd->device_identifier_code_set);
1213 strncpy(p_buf, buf, p_buf_len);
1215 pr_debug("%s", buf);
1221 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1223 static const char hex_str[] = "0123456789abcdef";
1224 int j = 0, i = 4; /* offset to start of the identifer */
1227 * The VPD Code Set (encoding)
1229 * from spc3r23.pdf Section 7.6.3.1 Table 296
1231 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1232 switch (vpd->device_identifier_code_set) {
1233 case 0x01: /* Binary */
1234 vpd->device_identifier[j++] =
1235 hex_str[vpd->device_identifier_type];
1236 while (i < (4 + page_83[3])) {
1237 vpd->device_identifier[j++] =
1238 hex_str[(page_83[i] & 0xf0) >> 4];
1239 vpd->device_identifier[j++] =
1240 hex_str[page_83[i] & 0x0f];
1244 case 0x02: /* ASCII */
1245 case 0x03: /* UTF-8 */
1246 while (i < (4 + page_83[3]))
1247 vpd->device_identifier[j++] = page_83[i++];
1253 return transport_dump_vpd_ident(vpd, NULL, 0);
1255 EXPORT_SYMBOL(transport_set_vpd_ident);
1257 static void core_setup_task_attr_emulation(struct se_device *dev)
1260 * If this device is from Target_Core_Mod/pSCSI, disable the
1261 * SAM Task Attribute emulation.
1263 * This is currently not available in upsream Linux/SCSI Target
1264 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1266 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1267 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1271 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1272 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1273 " device\n", dev->transport->name,
1274 dev->transport->get_device_rev(dev));
1277 static void scsi_dump_inquiry(struct se_device *dev)
1279 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1282 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1284 pr_debug(" Vendor: ");
1285 for (i = 0; i < 8; i++)
1286 if (wwn->vendor[i] >= 0x20)
1287 pr_debug("%c", wwn->vendor[i]);
1291 pr_debug(" Model: ");
1292 for (i = 0; i < 16; i++)
1293 if (wwn->model[i] >= 0x20)
1294 pr_debug("%c", wwn->model[i]);
1298 pr_debug(" Revision: ");
1299 for (i = 0; i < 4; i++)
1300 if (wwn->revision[i] >= 0x20)
1301 pr_debug("%c", wwn->revision[i]);
1307 device_type = dev->transport->get_device_type(dev);
1308 pr_debug(" Type: %s ", scsi_device_type(device_type));
1309 pr_debug(" ANSI SCSI revision: %02x\n",
1310 dev->transport->get_device_rev(dev));
1313 struct se_device *transport_add_device_to_core_hba(
1315 struct se_subsystem_api *transport,
1316 struct se_subsystem_dev *se_dev,
1318 void *transport_dev,
1319 struct se_dev_limits *dev_limits,
1320 const char *inquiry_prod,
1321 const char *inquiry_rev)
1324 struct se_device *dev;
1326 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1328 pr_err("Unable to allocate memory for se_dev_t\n");
1332 transport_init_queue_obj(&dev->dev_queue_obj);
1333 dev->dev_flags = device_flags;
1334 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1335 dev->dev_ptr = transport_dev;
1337 dev->se_sub_dev = se_dev;
1338 dev->transport = transport;
1339 atomic_set(&dev->active_cmds, 0);
1340 INIT_LIST_HEAD(&dev->dev_list);
1341 INIT_LIST_HEAD(&dev->dev_sep_list);
1342 INIT_LIST_HEAD(&dev->dev_tmr_list);
1343 INIT_LIST_HEAD(&dev->execute_task_list);
1344 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1345 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1346 INIT_LIST_HEAD(&dev->state_task_list);
1347 INIT_LIST_HEAD(&dev->qf_cmd_list);
1348 spin_lock_init(&dev->execute_task_lock);
1349 spin_lock_init(&dev->delayed_cmd_lock);
1350 spin_lock_init(&dev->ordered_cmd_lock);
1351 spin_lock_init(&dev->state_task_lock);
1352 spin_lock_init(&dev->dev_alua_lock);
1353 spin_lock_init(&dev->dev_reservation_lock);
1354 spin_lock_init(&dev->dev_status_lock);
1355 spin_lock_init(&dev->dev_status_thr_lock);
1356 spin_lock_init(&dev->se_port_lock);
1357 spin_lock_init(&dev->se_tmr_lock);
1358 spin_lock_init(&dev->qf_cmd_lock);
1360 dev->queue_depth = dev_limits->queue_depth;
1361 atomic_set(&dev->depth_left, dev->queue_depth);
1362 atomic_set(&dev->dev_ordered_id, 0);
1364 se_dev_set_default_attribs(dev, dev_limits);
1366 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1367 dev->creation_time = get_jiffies_64();
1368 spin_lock_init(&dev->stats_lock);
1370 spin_lock(&hba->device_lock);
1371 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1373 spin_unlock(&hba->device_lock);
1375 * Setup the SAM Task Attribute emulation for struct se_device
1377 core_setup_task_attr_emulation(dev);
1379 * Force PR and ALUA passthrough emulation with internal object use.
1381 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1383 * Setup the Reservations infrastructure for struct se_device
1385 core_setup_reservations(dev, force_pt);
1387 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1389 if (core_setup_alua(dev, force_pt) < 0)
1393 * Startup the struct se_device processing thread
1395 dev->process_thread = kthread_run(transport_processing_thread, dev,
1396 "LIO_%s", dev->transport->name);
1397 if (IS_ERR(dev->process_thread)) {
1398 pr_err("Unable to create kthread: LIO_%s\n",
1399 dev->transport->name);
1403 * Setup work_queue for QUEUE_FULL
1405 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1407 * Preload the initial INQUIRY const values if we are doing
1408 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1409 * passthrough because this is being provided by the backend LLD.
1410 * This is required so that transport_get_inquiry() copies these
1411 * originals once back into DEV_T10_WWN(dev) for the virtual device
1414 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1415 if (!inquiry_prod || !inquiry_rev) {
1416 pr_err("All non TCM/pSCSI plugins require"
1417 " INQUIRY consts\n");
1421 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1422 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1423 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1425 scsi_dump_inquiry(dev);
1429 kthread_stop(dev->process_thread);
1431 spin_lock(&hba->device_lock);
1432 list_del(&dev->dev_list);
1434 spin_unlock(&hba->device_lock);
1436 se_release_vpd_for_dev(dev);
1442 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1444 /* transport_generic_prepare_cdb():
1446 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1447 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1448 * The point of this is since we are mapping iSCSI LUNs to
1449 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1450 * devices and HBAs for a loop.
1452 static inline void transport_generic_prepare_cdb(
1456 case READ_10: /* SBC - RDProtect */
1457 case READ_12: /* SBC - RDProtect */
1458 case READ_16: /* SBC - RDProtect */
1459 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1460 case VERIFY: /* SBC - VRProtect */
1461 case VERIFY_16: /* SBC - VRProtect */
1462 case WRITE_VERIFY: /* SBC - VRProtect */
1463 case WRITE_VERIFY_12: /* SBC - VRProtect */
1466 cdb[1] &= 0x1f; /* clear logical unit number */
1471 static struct se_task *
1472 transport_generic_get_task(struct se_cmd *cmd,
1473 enum dma_data_direction data_direction)
1475 struct se_task *task;
1476 struct se_device *dev = cmd->se_dev;
1478 task = dev->transport->alloc_task(cmd->t_task_cdb);
1480 pr_err("Unable to allocate struct se_task\n");
1484 INIT_LIST_HEAD(&task->t_list);
1485 INIT_LIST_HEAD(&task->t_execute_list);
1486 INIT_LIST_HEAD(&task->t_state_list);
1487 init_completion(&task->task_stop_comp);
1488 task->task_se_cmd = cmd;
1489 task->task_data_direction = data_direction;
1494 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1497 * Used by fabric modules containing a local struct se_cmd within their
1498 * fabric dependent per I/O descriptor.
1500 void transport_init_se_cmd(
1502 struct target_core_fabric_ops *tfo,
1503 struct se_session *se_sess,
1507 unsigned char *sense_buffer)
1509 INIT_LIST_HEAD(&cmd->se_lun_node);
1510 INIT_LIST_HEAD(&cmd->se_delayed_node);
1511 INIT_LIST_HEAD(&cmd->se_ordered_node);
1512 INIT_LIST_HEAD(&cmd->se_qf_node);
1513 INIT_LIST_HEAD(&cmd->se_queue_node);
1514 INIT_LIST_HEAD(&cmd->se_cmd_list);
1515 INIT_LIST_HEAD(&cmd->t_task_list);
1516 init_completion(&cmd->transport_lun_fe_stop_comp);
1517 init_completion(&cmd->transport_lun_stop_comp);
1518 init_completion(&cmd->t_transport_stop_comp);
1519 init_completion(&cmd->cmd_wait_comp);
1520 spin_lock_init(&cmd->t_state_lock);
1521 atomic_set(&cmd->transport_dev_active, 1);
1524 cmd->se_sess = se_sess;
1525 cmd->data_length = data_length;
1526 cmd->data_direction = data_direction;
1527 cmd->sam_task_attr = task_attr;
1528 cmd->sense_buffer = sense_buffer;
1530 EXPORT_SYMBOL(transport_init_se_cmd);
1532 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1535 * Check if SAM Task Attribute emulation is enabled for this
1536 * struct se_device storage object
1538 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1541 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1542 pr_debug("SAM Task Attribute ACA"
1543 " emulation is not supported\n");
1547 * Used to determine when ORDERED commands should go from
1548 * Dormant to Active status.
1550 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1551 smp_mb__after_atomic_inc();
1552 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1553 cmd->se_ordered_id, cmd->sam_task_attr,
1554 cmd->se_dev->transport->name);
1558 /* transport_generic_allocate_tasks():
1560 * Called from fabric RX Thread.
1562 int transport_generic_allocate_tasks(
1568 transport_generic_prepare_cdb(cdb);
1570 * Ensure that the received CDB is less than the max (252 + 8) bytes
1571 * for VARIABLE_LENGTH_CMD
1573 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1574 pr_err("Received SCSI CDB with command_size: %d that"
1575 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1576 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1577 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1578 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1582 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1583 * allocate the additional extended CDB buffer now.. Otherwise
1584 * setup the pointer from __t_task_cdb to t_task_cdb.
1586 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1587 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1589 if (!cmd->t_task_cdb) {
1590 pr_err("Unable to allocate cmd->t_task_cdb"
1591 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1592 scsi_command_size(cdb),
1593 (unsigned long)sizeof(cmd->__t_task_cdb));
1594 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1595 cmd->scsi_sense_reason =
1596 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1600 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1602 * Copy the original CDB into cmd->
1604 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1606 * Setup the received CDB based on SCSI defined opcodes and
1607 * perform unit attention, persistent reservations and ALUA
1608 * checks for virtual device backends. The cmd->t_task_cdb
1609 * pointer is expected to be setup before we reach this point.
1611 ret = transport_generic_cmd_sequencer(cmd, cdb);
1615 * Check for SAM Task Attribute Emulation
1617 if (transport_check_alloc_task_attr(cmd) < 0) {
1618 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1619 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1622 spin_lock(&cmd->se_lun->lun_sep_lock);
1623 if (cmd->se_lun->lun_sep)
1624 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1625 spin_unlock(&cmd->se_lun->lun_sep_lock);
1628 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1631 * Used by fabric module frontends to queue tasks directly.
1632 * Many only be used from process context only
1634 int transport_handle_cdb_direct(
1641 pr_err("cmd->se_lun is NULL\n");
1644 if (in_interrupt()) {
1646 pr_err("transport_generic_handle_cdb cannot be called"
1647 " from interrupt context\n");
1651 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1652 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1653 * in existing usage to ensure that outstanding descriptors are handled
1654 * correctly during shutdown via transport_wait_for_tasks()
1656 * Also, we don't take cmd->t_state_lock here as we only expect
1657 * this to be called for initial descriptor submission.
1659 cmd->t_state = TRANSPORT_NEW_CMD;
1660 atomic_set(&cmd->t_transport_active, 1);
1662 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1663 * so follow TRANSPORT_NEW_CMD processing thread context usage
1664 * and call transport_generic_request_failure() if necessary..
1666 ret = transport_generic_new_cmd(cmd);
1668 transport_generic_request_failure(cmd);
1672 EXPORT_SYMBOL(transport_handle_cdb_direct);
1675 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1676 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1677 * complete setup in TCM process context w/ TFO->new_cmd_map().
1679 int transport_generic_handle_cdb_map(
1684 pr_err("cmd->se_lun is NULL\n");
1688 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
1691 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1693 /* transport_generic_handle_data():
1697 int transport_generic_handle_data(
1701 * For the software fabric case, then we assume the nexus is being
1702 * failed/shutdown when signals are pending from the kthread context
1703 * caller, so we return a failure. For the HW target mode case running
1704 * in interrupt code, the signal_pending() check is skipped.
1706 if (!in_interrupt() && signal_pending(current))
1709 * If the received CDB has aleady been ABORTED by the generic
1710 * target engine, we now call transport_check_aborted_status()
1711 * to queue any delated TASK_ABORTED status for the received CDB to the
1712 * fabric module as we are expecting no further incoming DATA OUT
1713 * sequences at this point.
1715 if (transport_check_aborted_status(cmd, 1) != 0)
1718 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
1721 EXPORT_SYMBOL(transport_generic_handle_data);
1723 /* transport_generic_handle_tmr():
1727 int transport_generic_handle_tmr(
1730 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
1733 EXPORT_SYMBOL(transport_generic_handle_tmr);
1736 * If the task is active, request it to be stopped and sleep until it
1739 bool target_stop_task(struct se_task *task, unsigned long *flags)
1741 struct se_cmd *cmd = task->task_se_cmd;
1742 bool was_active = false;
1744 if (task->task_flags & TF_ACTIVE) {
1745 task->task_flags |= TF_REQUEST_STOP;
1746 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1748 pr_debug("Task %p waiting to complete\n", task);
1749 wait_for_completion(&task->task_stop_comp);
1750 pr_debug("Task %p stopped successfully\n", task);
1752 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1753 atomic_dec(&cmd->t_task_cdbs_left);
1754 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1761 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1763 struct se_task *task, *task_tmp;
1764 unsigned long flags;
1767 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1768 cmd->se_tfo->get_task_tag(cmd));
1771 * No tasks remain in the execution queue
1773 spin_lock_irqsave(&cmd->t_state_lock, flags);
1774 list_for_each_entry_safe(task, task_tmp,
1775 &cmd->t_task_list, t_list) {
1776 pr_debug("Processing task %p\n", task);
1778 * If the struct se_task has not been sent and is not active,
1779 * remove the struct se_task from the execution queue.
1781 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
1782 spin_unlock_irqrestore(&cmd->t_state_lock,
1784 transport_remove_task_from_execute_queue(task,
1787 pr_debug("Task %p removed from execute queue\n", task);
1788 spin_lock_irqsave(&cmd->t_state_lock, flags);
1792 if (!target_stop_task(task, &flags)) {
1793 pr_debug("Task %p - did nothing\n", task);
1797 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1803 * Handle SAM-esque emulation for generic transport request failures.
1805 static void transport_generic_request_failure(struct se_cmd *cmd)
1809 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1810 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1811 cmd->t_task_cdb[0]);
1812 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
1813 cmd->se_tfo->get_cmd_state(cmd),
1814 cmd->t_state, cmd->scsi_sense_reason);
1815 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1816 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1817 " t_transport_active: %d t_transport_stop: %d"
1818 " t_transport_sent: %d\n", cmd->t_task_list_num,
1819 atomic_read(&cmd->t_task_cdbs_left),
1820 atomic_read(&cmd->t_task_cdbs_sent),
1821 atomic_read(&cmd->t_task_cdbs_ex_left),
1822 atomic_read(&cmd->t_transport_active),
1823 atomic_read(&cmd->t_transport_stop),
1824 atomic_read(&cmd->t_transport_sent));
1827 * For SAM Task Attribute emulation for failed struct se_cmd
1829 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1830 transport_complete_task_attr(cmd);
1832 switch (cmd->scsi_sense_reason) {
1833 case TCM_NON_EXISTENT_LUN:
1834 case TCM_UNSUPPORTED_SCSI_OPCODE:
1835 case TCM_INVALID_CDB_FIELD:
1836 case TCM_INVALID_PARAMETER_LIST:
1837 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1838 case TCM_UNKNOWN_MODE_PAGE:
1839 case TCM_WRITE_PROTECTED:
1840 case TCM_CHECK_CONDITION_ABORT_CMD:
1841 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1842 case TCM_CHECK_CONDITION_NOT_READY:
1844 case TCM_RESERVATION_CONFLICT:
1846 * No SENSE Data payload for this case, set SCSI Status
1847 * and queue the response to $FABRIC_MOD.
1849 * Uses linux/include/scsi/scsi.h SAM status codes defs
1851 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1853 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1854 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1857 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1860 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1861 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1862 cmd->orig_fe_lun, 0x2C,
1863 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1865 ret = cmd->se_tfo->queue_status(cmd);
1866 if (ret == -EAGAIN || ret == -ENOMEM)
1870 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1871 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
1872 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1876 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1877 * make the call to transport_send_check_condition_and_sense()
1878 * directly. Otherwise expect the fabric to make the call to
1879 * transport_send_check_condition_and_sense() after handling
1880 * possible unsoliticied write data payloads.
1882 ret = transport_send_check_condition_and_sense(cmd,
1883 cmd->scsi_sense_reason, 0);
1884 if (ret == -EAGAIN || ret == -ENOMEM)
1888 transport_lun_remove_cmd(cmd);
1889 if (!transport_cmd_check_stop_to_fabric(cmd))
1894 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1895 transport_handle_queue_full(cmd, cmd->se_dev);
1898 static inline u32 transport_lba_21(unsigned char *cdb)
1900 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1903 static inline u32 transport_lba_32(unsigned char *cdb)
1905 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1908 static inline unsigned long long transport_lba_64(unsigned char *cdb)
1910 unsigned int __v1, __v2;
1912 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1913 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1915 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1919 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1921 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1923 unsigned int __v1, __v2;
1925 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1926 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1928 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1931 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1933 unsigned long flags;
1935 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1936 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1937 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1940 static inline int transport_tcq_window_closed(struct se_device *dev)
1942 if (dev->dev_tcq_window_closed++ <
1943 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
1944 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
1946 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
1948 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
1953 * Called from Fabric Module context from transport_execute_tasks()
1955 * The return of this function determins if the tasks from struct se_cmd
1956 * get added to the execution queue in transport_execute_tasks(),
1957 * or are added to the delayed or ordered lists here.
1959 static inline int transport_execute_task_attr(struct se_cmd *cmd)
1961 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1964 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1965 * to allow the passed struct se_cmd list of tasks to the front of the list.
1967 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
1968 atomic_inc(&cmd->se_dev->dev_hoq_count);
1969 smp_mb__after_atomic_inc();
1970 pr_debug("Added HEAD_OF_QUEUE for CDB:"
1971 " 0x%02x, se_ordered_id: %u\n",
1973 cmd->se_ordered_id);
1975 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
1976 spin_lock(&cmd->se_dev->ordered_cmd_lock);
1977 list_add_tail(&cmd->se_ordered_node,
1978 &cmd->se_dev->ordered_cmd_list);
1979 spin_unlock(&cmd->se_dev->ordered_cmd_lock);
1981 atomic_inc(&cmd->se_dev->dev_ordered_sync);
1982 smp_mb__after_atomic_inc();
1984 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
1985 " list, se_ordered_id: %u\n",
1987 cmd->se_ordered_id);
1989 * Add ORDERED command to tail of execution queue if
1990 * no other older commands exist that need to be
1993 if (!atomic_read(&cmd->se_dev->simple_cmds))
1997 * For SIMPLE and UNTAGGED Task Attribute commands
1999 atomic_inc(&cmd->se_dev->simple_cmds);
2000 smp_mb__after_atomic_inc();
2003 * Otherwise if one or more outstanding ORDERED task attribute exist,
2004 * add the dormant task(s) built for the passed struct se_cmd to the
2005 * execution queue and become in Active state for this struct se_device.
2007 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2009 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2010 * will be drained upon completion of HEAD_OF_QUEUE task.
2012 spin_lock(&cmd->se_dev->delayed_cmd_lock);
2013 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2014 list_add_tail(&cmd->se_delayed_node,
2015 &cmd->se_dev->delayed_cmd_list);
2016 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2018 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2019 " delayed CMD list, se_ordered_id: %u\n",
2020 cmd->t_task_cdb[0], cmd->sam_task_attr,
2021 cmd->se_ordered_id);
2023 * Return zero to let transport_execute_tasks() know
2024 * not to add the delayed tasks to the execution list.
2029 * Otherwise, no ORDERED task attributes exist..
2035 * Called from fabric module context in transport_generic_new_cmd() and
2036 * transport_generic_process_write()
2038 static int transport_execute_tasks(struct se_cmd *cmd)
2042 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2043 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2044 transport_generic_request_failure(cmd);
2049 * Call transport_cmd_check_stop() to see if a fabric exception
2050 * has occurred that prevents execution.
2052 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2054 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2055 * attribute for the tasks of the received struct se_cmd CDB
2057 add_tasks = transport_execute_task_attr(cmd);
2061 * This calls transport_add_tasks_from_cmd() to handle
2062 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2063 * (if enabled) in __transport_add_task_to_execute_queue() and
2064 * transport_add_task_check_sam_attr().
2066 transport_add_tasks_from_cmd(cmd);
2069 * Kick the execution queue for the cmd associated struct se_device
2073 __transport_execute_tasks(cmd->se_dev);
2078 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2079 * from struct se_device->execute_task_list and
2081 * Called from transport_processing_thread()
2083 static int __transport_execute_tasks(struct se_device *dev)
2086 struct se_cmd *cmd = NULL;
2087 struct se_task *task = NULL;
2088 unsigned long flags;
2091 * Check if there is enough room in the device and HBA queue to send
2092 * struct se_tasks to the selected transport.
2095 if (!atomic_read(&dev->depth_left))
2096 return transport_tcq_window_closed(dev);
2098 dev->dev_tcq_window_closed = 0;
2100 spin_lock_irq(&dev->execute_task_lock);
2101 if (list_empty(&dev->execute_task_list)) {
2102 spin_unlock_irq(&dev->execute_task_lock);
2105 task = list_first_entry(&dev->execute_task_list,
2106 struct se_task, t_execute_list);
2107 __transport_remove_task_from_execute_queue(task, dev);
2108 spin_unlock_irq(&dev->execute_task_lock);
2110 atomic_dec(&dev->depth_left);
2112 cmd = task->task_se_cmd;
2114 spin_lock_irqsave(&cmd->t_state_lock, flags);
2115 task->task_flags |= (TF_ACTIVE | TF_SENT);
2116 atomic_inc(&cmd->t_task_cdbs_sent);
2118 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2119 cmd->t_task_list_num)
2120 atomic_set(&cmd->t_transport_sent, 1);
2122 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2124 if (cmd->execute_task)
2125 error = cmd->execute_task(task);
2127 error = dev->transport->do_task(task);
2129 spin_lock_irqsave(&cmd->t_state_lock, flags);
2130 task->task_flags &= ~TF_ACTIVE;
2131 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2132 atomic_set(&cmd->t_transport_sent, 0);
2133 transport_stop_tasks_for_cmd(cmd);
2134 atomic_inc(&dev->depth_left);
2135 transport_generic_request_failure(cmd);
2143 static inline u32 transport_get_sectors_6(
2148 struct se_device *dev = cmd->se_dev;
2151 * Assume TYPE_DISK for non struct se_device objects.
2152 * Use 8-bit sector value.
2158 * Use 24-bit allocation length for TYPE_TAPE.
2160 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2161 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2164 * Everything else assume TYPE_DISK Sector CDB location.
2165 * Use 8-bit sector value.
2171 static inline u32 transport_get_sectors_10(
2176 struct se_device *dev = cmd->se_dev;
2179 * Assume TYPE_DISK for non struct se_device objects.
2180 * Use 16-bit sector value.
2186 * XXX_10 is not defined in SSC, throw an exception
2188 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2194 * Everything else assume TYPE_DISK Sector CDB location.
2195 * Use 16-bit sector value.
2198 return (u32)(cdb[7] << 8) + cdb[8];
2201 static inline u32 transport_get_sectors_12(
2206 struct se_device *dev = cmd->se_dev;
2209 * Assume TYPE_DISK for non struct se_device objects.
2210 * Use 32-bit sector value.
2216 * XXX_12 is not defined in SSC, throw an exception
2218 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2224 * Everything else assume TYPE_DISK Sector CDB location.
2225 * Use 32-bit sector value.
2228 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2231 static inline u32 transport_get_sectors_16(
2236 struct se_device *dev = cmd->se_dev;
2239 * Assume TYPE_DISK for non struct se_device objects.
2240 * Use 32-bit sector value.
2246 * Use 24-bit allocation length for TYPE_TAPE.
2248 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2249 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2252 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2253 (cdb[12] << 8) + cdb[13];
2257 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2259 static inline u32 transport_get_sectors_32(
2265 * Assume TYPE_DISK for non struct se_device objects.
2266 * Use 32-bit sector value.
2268 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2269 (cdb[30] << 8) + cdb[31];
2273 static inline u32 transport_get_size(
2278 struct se_device *dev = cmd->se_dev;
2280 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2281 if (cdb[1] & 1) { /* sectors */
2282 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2287 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2288 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2289 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2290 dev->transport->name);
2292 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2295 static void transport_xor_callback(struct se_cmd *cmd)
2297 unsigned char *buf, *addr;
2298 struct scatterlist *sg;
2299 unsigned int offset;
2303 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2305 * 1) read the specified logical block(s);
2306 * 2) transfer logical blocks from the data-out buffer;
2307 * 3) XOR the logical blocks transferred from the data-out buffer with
2308 * the logical blocks read, storing the resulting XOR data in a buffer;
2309 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2310 * blocks transferred from the data-out buffer; and
2311 * 5) transfer the resulting XOR data to the data-in buffer.
2313 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2315 pr_err("Unable to allocate xor_callback buf\n");
2319 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2320 * into the locally allocated *buf
2322 sg_copy_to_buffer(cmd->t_data_sg,
2328 * Now perform the XOR against the BIDI read memory located at
2329 * cmd->t_mem_bidi_list
2333 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2334 addr = kmap_atomic(sg_page(sg), KM_USER0);
2338 for (i = 0; i < sg->length; i++)
2339 *(addr + sg->offset + i) ^= *(buf + offset + i);
2341 offset += sg->length;
2342 kunmap_atomic(addr, KM_USER0);
2350 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2352 static int transport_get_sense_data(struct se_cmd *cmd)
2354 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2355 struct se_device *dev = cmd->se_dev;
2356 struct se_task *task = NULL, *task_tmp;
2357 unsigned long flags;
2360 WARN_ON(!cmd->se_lun);
2365 spin_lock_irqsave(&cmd->t_state_lock, flags);
2366 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2367 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2371 list_for_each_entry_safe(task, task_tmp,
2372 &cmd->t_task_list, t_list) {
2373 if (!task->task_sense)
2376 if (!dev->transport->get_sense_buffer) {
2377 pr_err("dev->transport->get_sense_buffer"
2382 sense_buffer = dev->transport->get_sense_buffer(task);
2383 if (!sense_buffer) {
2384 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
2385 " sense buffer for task with sense\n",
2386 cmd->se_tfo->get_task_tag(cmd), task);
2389 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2391 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2392 TRANSPORT_SENSE_BUFFER);
2394 memcpy(&buffer[offset], sense_buffer,
2395 TRANSPORT_SENSE_BUFFER);
2396 cmd->scsi_status = task->task_scsi_status;
2397 /* Automatically padded */
2398 cmd->scsi_sense_length =
2399 (TRANSPORT_SENSE_BUFFER + offset);
2401 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2403 dev->se_hba->hba_id, dev->transport->name,
2407 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2412 static inline long long transport_dev_end_lba(struct se_device *dev)
2414 return dev->transport->get_blocks(dev) + 1;
2417 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2419 struct se_device *dev = cmd->se_dev;
2422 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2425 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2427 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2428 pr_err("LBA: %llu Sectors: %u exceeds"
2429 " transport_dev_end_lba(): %llu\n",
2430 cmd->t_task_lba, sectors,
2431 transport_dev_end_lba(dev));
2438 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2441 * Determine if the received WRITE_SAME is used to for direct
2442 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2443 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2444 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2446 int passthrough = (dev->transport->transport_type ==
2447 TRANSPORT_PLUGIN_PHBA_PDEV);
2450 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2451 pr_err("WRITE_SAME PBDATA and LBDATA"
2452 " bits not supported for Block Discard"
2457 * Currently for the emulated case we only accept
2458 * tpws with the UNMAP=1 bit set.
2460 if (!(flags[0] & 0x08)) {
2461 pr_err("WRITE_SAME w/o UNMAP bit not"
2462 " supported for Block Discard Emulation\n");
2470 /* transport_generic_cmd_sequencer():
2472 * Generic Command Sequencer that should work for most DAS transport
2475 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2478 * FIXME: Need to support other SCSI OPCODES where as well.
2480 static int transport_generic_cmd_sequencer(
2484 struct se_device *dev = cmd->se_dev;
2485 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2486 int ret = 0, sector_ret = 0, passthrough;
2487 u32 sectors = 0, size = 0, pr_reg_type = 0;
2491 * Check for an existing UNIT ATTENTION condition
2493 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2494 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2495 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2499 * Check status of Asymmetric Logical Unit Assignment port
2501 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2504 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2505 * The ALUA additional sense code qualifier (ASCQ) is determined
2506 * by the ALUA primary or secondary access state..
2510 pr_debug("[%s]: ALUA TG Port not available,"
2511 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2512 cmd->se_tfo->get_fabric_name(), alua_ascq);
2514 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2515 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2516 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2519 goto out_invalid_cdb_field;
2522 * Check status for SPC-3 Persistent Reservations
2524 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2525 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2526 cmd, cdb, pr_reg_type) != 0) {
2527 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2528 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2529 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
2533 * This means the CDB is allowed for the SCSI Initiator port
2534 * when said port is *NOT* holding the legacy SPC-2 or
2535 * SPC-3 Persistent Reservation.
2540 * If we operate in passthrough mode we skip most CDB emulation and
2541 * instead hand the commands down to the physical SCSI device.
2544 (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV);
2548 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2550 goto out_unsupported_cdb;
2551 size = transport_get_size(sectors, cdb, cmd);
2552 cmd->t_task_lba = transport_lba_21(cdb);
2553 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2556 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2558 goto out_unsupported_cdb;
2559 size = transport_get_size(sectors, cdb, cmd);
2560 cmd->t_task_lba = transport_lba_32(cdb);
2561 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2564 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2566 goto out_unsupported_cdb;
2567 size = transport_get_size(sectors, cdb, cmd);
2568 cmd->t_task_lba = transport_lba_32(cdb);
2569 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2572 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2574 goto out_unsupported_cdb;
2575 size = transport_get_size(sectors, cdb, cmd);
2576 cmd->t_task_lba = transport_lba_64(cdb);
2577 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2580 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2582 goto out_unsupported_cdb;
2583 size = transport_get_size(sectors, cdb, cmd);
2584 cmd->t_task_lba = transport_lba_21(cdb);
2585 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2588 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2590 goto out_unsupported_cdb;
2591 size = transport_get_size(sectors, cdb, cmd);
2592 cmd->t_task_lba = transport_lba_32(cdb);
2593 cmd->t_tasks_fua = (cdb[1] & 0x8);
2594 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2597 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2599 goto out_unsupported_cdb;
2600 size = transport_get_size(sectors, cdb, cmd);
2601 cmd->t_task_lba = transport_lba_32(cdb);
2602 cmd->t_tasks_fua = (cdb[1] & 0x8);
2603 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2606 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2608 goto out_unsupported_cdb;
2609 size = transport_get_size(sectors, cdb, cmd);
2610 cmd->t_task_lba = transport_lba_64(cdb);
2611 cmd->t_tasks_fua = (cdb[1] & 0x8);
2612 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2614 case XDWRITEREAD_10:
2615 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2616 !(cmd->t_tasks_bidi))
2617 goto out_invalid_cdb_field;
2618 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2620 goto out_unsupported_cdb;
2621 size = transport_get_size(sectors, cdb, cmd);
2622 cmd->t_task_lba = transport_lba_32(cdb);
2623 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2626 * Do now allow BIDI commands for passthrough mode.
2629 goto out_unsupported_cdb;
2632 * Setup BIDI XOR callback to be run after I/O completion.
2634 cmd->transport_complete_callback = &transport_xor_callback;
2635 cmd->t_tasks_fua = (cdb[1] & 0x8);
2637 case VARIABLE_LENGTH_CMD:
2638 service_action = get_unaligned_be16(&cdb[8]);
2639 switch (service_action) {
2640 case XDWRITEREAD_32:
2641 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2643 goto out_unsupported_cdb;
2644 size = transport_get_size(sectors, cdb, cmd);
2646 * Use WRITE_32 and READ_32 opcodes for the emulated
2647 * XDWRITE_READ_32 logic.
2649 cmd->t_task_lba = transport_lba_64_ext(cdb);
2650 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2653 * Do now allow BIDI commands for passthrough mode.
2656 goto out_unsupported_cdb;
2659 * Setup BIDI XOR callback to be run during after I/O
2662 cmd->transport_complete_callback = &transport_xor_callback;
2663 cmd->t_tasks_fua = (cdb[10] & 0x8);
2666 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2668 goto out_unsupported_cdb;
2671 size = transport_get_size(1, cdb, cmd);
2673 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2675 goto out_invalid_cdb_field;
2678 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2679 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2681 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2682 goto out_invalid_cdb_field;
2684 cmd->execute_task = target_emulate_write_same;
2687 pr_err("VARIABLE_LENGTH_CMD service action"
2688 " 0x%04x not supported\n", service_action);
2689 goto out_unsupported_cdb;
2692 case MAINTENANCE_IN:
2693 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2694 /* MAINTENANCE_IN from SCC-2 */
2696 * Check for emulated MI_REPORT_TARGET_PGS.
2698 if (cdb[1] == MI_REPORT_TARGET_PGS &&
2699 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2701 target_emulate_report_target_port_groups;
2703 size = (cdb[6] << 24) | (cdb[7] << 16) |
2704 (cdb[8] << 8) | cdb[9];
2706 /* GPCMD_SEND_KEY from multi media commands */
2707 size = (cdb[8] << 8) + cdb[9];
2709 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2713 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2715 case MODE_SELECT_10:
2716 size = (cdb[7] << 8) + cdb[8];
2717 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2721 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2723 cmd->execute_task = target_emulate_modesense;
2726 size = (cdb[7] << 8) + cdb[8];
2727 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2729 cmd->execute_task = target_emulate_modesense;
2731 case GPCMD_READ_BUFFER_CAPACITY:
2732 case GPCMD_SEND_OPC:
2735 size = (cdb[7] << 8) + cdb[8];
2736 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2738 case READ_BLOCK_LIMITS:
2739 size = READ_BLOCK_LEN;
2740 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2742 case GPCMD_GET_CONFIGURATION:
2743 case GPCMD_READ_FORMAT_CAPACITIES:
2744 case GPCMD_READ_DISC_INFO:
2745 case GPCMD_READ_TRACK_RZONE_INFO:
2746 size = (cdb[7] << 8) + cdb[8];
2747 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2749 case PERSISTENT_RESERVE_IN:
2750 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2751 cmd->execute_task = target_scsi3_emulate_pr_in;
2752 size = (cdb[7] << 8) + cdb[8];
2753 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2755 case PERSISTENT_RESERVE_OUT:
2756 if (su_dev->t10_pr.res_type == SPC3_PERSISTENT_RESERVATIONS)
2757 cmd->execute_task = target_scsi3_emulate_pr_out;
2758 size = (cdb[7] << 8) + cdb[8];
2759 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2761 case GPCMD_MECHANISM_STATUS:
2762 case GPCMD_READ_DVD_STRUCTURE:
2763 size = (cdb[8] << 8) + cdb[9];
2764 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2767 size = READ_POSITION_LEN;
2768 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2770 case MAINTENANCE_OUT:
2771 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2772 /* MAINTENANCE_OUT from SCC-2
2774 * Check for emulated MO_SET_TARGET_PGS.
2776 if (cdb[1] == MO_SET_TARGET_PGS &&
2777 su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
2779 target_emulate_set_target_port_groups;
2782 size = (cdb[6] << 24) | (cdb[7] << 16) |
2783 (cdb[8] << 8) | cdb[9];
2785 /* GPCMD_REPORT_KEY from multi media commands */
2786 size = (cdb[8] << 8) + cdb[9];
2788 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2791 size = (cdb[3] << 8) + cdb[4];
2793 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2794 * See spc4r17 section 5.3
2796 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2797 cmd->sam_task_attr = MSG_HEAD_TAG;
2798 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2800 cmd->execute_task = target_emulate_inquiry;
2803 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2804 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2807 size = READ_CAP_LEN;
2808 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2810 cmd->execute_task = target_emulate_readcapacity;
2812 case READ_MEDIA_SERIAL_NUMBER:
2813 case SECURITY_PROTOCOL_IN:
2814 case SECURITY_PROTOCOL_OUT:
2815 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2816 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2818 case SERVICE_ACTION_IN:
2819 switch (cmd->t_task_cdb[1] & 0x1f) {
2820 case SAI_READ_CAPACITY_16:
2823 target_emulate_readcapacity_16;
2829 pr_err("Unsupported SA: 0x%02x\n",
2830 cmd->t_task_cdb[1] & 0x1f);
2831 goto out_unsupported_cdb;
2834 case ACCESS_CONTROL_IN:
2835 case ACCESS_CONTROL_OUT:
2837 case READ_ATTRIBUTE:
2838 case RECEIVE_COPY_RESULTS:
2839 case WRITE_ATTRIBUTE:
2840 size = (cdb[10] << 24) | (cdb[11] << 16) |
2841 (cdb[12] << 8) | cdb[13];
2842 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2844 case RECEIVE_DIAGNOSTIC:
2845 case SEND_DIAGNOSTIC:
2846 size = (cdb[3] << 8) | cdb[4];
2847 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2849 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2852 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2853 size = (2336 * sectors);
2854 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2859 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2863 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2865 cmd->execute_task = target_emulate_request_sense;
2867 case READ_ELEMENT_STATUS:
2868 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
2869 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2872 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2873 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2878 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2879 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2881 if (cdb[0] == RESERVE_10)
2882 size = (cdb[7] << 8) | cdb[8];
2884 size = cmd->data_length;
2887 * Setup the legacy emulated handler for SPC-2 and
2888 * >= SPC-3 compatible reservation handling (CRH=1)
2889 * Otherwise, we assume the underlying SCSI logic is
2890 * is running in SPC_PASSTHROUGH, and wants reservations
2891 * emulation disabled.
2893 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2894 cmd->execute_task = target_scsi2_reservation_reserve;
2895 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2900 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2901 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2903 if (cdb[0] == RELEASE_10)
2904 size = (cdb[7] << 8) | cdb[8];
2906 size = cmd->data_length;
2908 if (su_dev->t10_pr.res_type != SPC_PASSTHROUGH)
2909 cmd->execute_task = target_scsi2_reservation_release;
2910 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2912 case SYNCHRONIZE_CACHE:
2913 case 0x91: /* SYNCHRONIZE_CACHE_16: */
2915 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2917 if (cdb[0] == SYNCHRONIZE_CACHE) {
2918 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2919 cmd->t_task_lba = transport_lba_32(cdb);
2921 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2922 cmd->t_task_lba = transport_lba_64(cdb);
2925 goto out_unsupported_cdb;
2927 size = transport_get_size(sectors, cdb, cmd);
2928 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2934 * Check to ensure that LBA + Range does not exceed past end of
2935 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
2937 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
2938 if (transport_cmd_get_valid_sectors(cmd) < 0)
2939 goto out_invalid_cdb_field;
2941 cmd->execute_task = target_emulate_synchronize_cache;
2944 size = get_unaligned_be16(&cdb[7]);
2945 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2947 cmd->execute_task = target_emulate_unmap;
2950 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2952 goto out_unsupported_cdb;
2955 size = transport_get_size(1, cdb, cmd);
2957 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2958 goto out_invalid_cdb_field;
2961 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
2962 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2964 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2965 goto out_invalid_cdb_field;
2967 cmd->execute_task = target_emulate_write_same;
2970 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2972 goto out_unsupported_cdb;
2975 size = transport_get_size(1, cdb, cmd);
2977 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
2978 goto out_invalid_cdb_field;
2981 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
2982 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2984 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
2985 * of byte 1 bit 3 UNMAP instead of original reserved field
2987 if (target_check_write_same_discard(&cdb[1], dev) < 0)
2988 goto out_invalid_cdb_field;
2990 cmd->execute_task = target_emulate_write_same;
2992 case ALLOW_MEDIUM_REMOVAL:
2998 case TEST_UNIT_READY:
3000 case WRITE_FILEMARKS:
3001 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3003 cmd->execute_task = target_emulate_noop;
3005 case GPCMD_CLOSE_TRACK:
3006 case INITIALIZE_ELEMENT_STATUS:
3007 case GPCMD_LOAD_UNLOAD:
3008 case GPCMD_SET_SPEED:
3010 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3013 cmd->execute_task = target_report_luns;
3014 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3016 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3017 * See spc4r17 section 5.3
3019 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3020 cmd->sam_task_attr = MSG_HEAD_TAG;
3021 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3024 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3025 " 0x%02x, sending CHECK_CONDITION.\n",
3026 cmd->se_tfo->get_fabric_name(), cdb[0]);
3027 goto out_unsupported_cdb;
3030 if (size != cmd->data_length) {
3031 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3032 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3033 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3034 cmd->data_length, size, cdb[0]);
3036 cmd->cmd_spdtl = size;
3038 if (cmd->data_direction == DMA_TO_DEVICE) {
3039 pr_err("Rejecting underflow/overflow"
3041 goto out_invalid_cdb_field;
3044 * Reject READ_* or WRITE_* with overflow/underflow for
3045 * type SCF_SCSI_DATA_SG_IO_CDB.
3047 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3048 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3049 " CDB on non 512-byte sector setup subsystem"
3050 " plugin: %s\n", dev->transport->name);
3051 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3052 goto out_invalid_cdb_field;
3055 if (size > cmd->data_length) {
3056 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3057 cmd->residual_count = (size - cmd->data_length);
3059 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3060 cmd->residual_count = (cmd->data_length - size);
3062 cmd->data_length = size;
3065 /* reject any command that we don't have a handler for */
3066 if (!(passthrough || cmd->execute_task ||
3067 (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
3068 goto out_unsupported_cdb;
3070 /* Let's limit control cdbs to a page, for simplicity's sake. */
3071 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3073 goto out_invalid_cdb_field;
3075 transport_set_supported_SAM_opcode(cmd);
3078 out_unsupported_cdb:
3079 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3080 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3082 out_invalid_cdb_field:
3083 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3084 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3089 * Called from I/O completion to determine which dormant/delayed
3090 * and ordered cmds need to have their tasks added to the execution queue.
3092 static void transport_complete_task_attr(struct se_cmd *cmd)
3094 struct se_device *dev = cmd->se_dev;
3095 struct se_cmd *cmd_p, *cmd_tmp;
3096 int new_active_tasks = 0;
3098 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3099 atomic_dec(&dev->simple_cmds);
3100 smp_mb__after_atomic_dec();
3101 dev->dev_cur_ordered_id++;
3102 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3103 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3104 cmd->se_ordered_id);
3105 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3106 atomic_dec(&dev->dev_hoq_count);
3107 smp_mb__after_atomic_dec();
3108 dev->dev_cur_ordered_id++;
3109 pr_debug("Incremented dev_cur_ordered_id: %u for"
3110 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3111 cmd->se_ordered_id);
3112 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3113 spin_lock(&dev->ordered_cmd_lock);
3114 list_del(&cmd->se_ordered_node);
3115 atomic_dec(&dev->dev_ordered_sync);
3116 smp_mb__after_atomic_dec();
3117 spin_unlock(&dev->ordered_cmd_lock);
3119 dev->dev_cur_ordered_id++;
3120 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3121 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3124 * Process all commands up to the last received
3125 * ORDERED task attribute which requires another blocking
3128 spin_lock(&dev->delayed_cmd_lock);
3129 list_for_each_entry_safe(cmd_p, cmd_tmp,
3130 &dev->delayed_cmd_list, se_delayed_node) {
3132 list_del(&cmd_p->se_delayed_node);
3133 spin_unlock(&dev->delayed_cmd_lock);
3135 pr_debug("Calling add_tasks() for"
3136 " cmd_p: 0x%02x Task Attr: 0x%02x"
3137 " Dormant -> Active, se_ordered_id: %u\n",
3138 cmd_p->t_task_cdb[0],
3139 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3141 transport_add_tasks_from_cmd(cmd_p);
3144 spin_lock(&dev->delayed_cmd_lock);
3145 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3148 spin_unlock(&dev->delayed_cmd_lock);
3150 * If new tasks have become active, wake up the transport thread
3151 * to do the processing of the Active tasks.
3153 if (new_active_tasks != 0)
3154 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3157 static void transport_complete_qf(struct se_cmd *cmd)
3161 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3162 transport_complete_task_attr(cmd);
3164 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3165 ret = cmd->se_tfo->queue_status(cmd);
3170 switch (cmd->data_direction) {
3171 case DMA_FROM_DEVICE:
3172 ret = cmd->se_tfo->queue_data_in(cmd);
3175 if (cmd->t_bidi_data_sg) {
3176 ret = cmd->se_tfo->queue_data_in(cmd);
3180 /* Fall through for DMA_TO_DEVICE */
3182 ret = cmd->se_tfo->queue_status(cmd);
3190 transport_handle_queue_full(cmd, cmd->se_dev);
3193 transport_lun_remove_cmd(cmd);
3194 transport_cmd_check_stop_to_fabric(cmd);
3197 static void transport_handle_queue_full(
3199 struct se_device *dev)
3201 spin_lock_irq(&dev->qf_cmd_lock);
3202 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3203 atomic_inc(&dev->dev_qf_count);
3204 smp_mb__after_atomic_inc();
3205 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3207 schedule_work(&cmd->se_dev->qf_work_queue);
3210 static void target_complete_ok_work(struct work_struct *work)
3212 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
3213 int reason = 0, ret;
3216 * Check if we need to move delayed/dormant tasks from cmds on the
3217 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3220 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3221 transport_complete_task_attr(cmd);
3223 * Check to schedule QUEUE_FULL work, or execute an existing
3224 * cmd->transport_qf_callback()
3226 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3227 schedule_work(&cmd->se_dev->qf_work_queue);
3230 * Check if we need to retrieve a sense buffer from
3231 * the struct se_cmd in question.
3233 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3234 if (transport_get_sense_data(cmd) < 0)
3235 reason = TCM_NON_EXISTENT_LUN;
3238 * Only set when an struct se_task->task_scsi_status returned
3239 * a non GOOD status.
3241 if (cmd->scsi_status) {
3242 ret = transport_send_check_condition_and_sense(
3244 if (ret == -EAGAIN || ret == -ENOMEM)
3247 transport_lun_remove_cmd(cmd);
3248 transport_cmd_check_stop_to_fabric(cmd);
3253 * Check for a callback, used by amongst other things
3254 * XDWRITE_READ_10 emulation.
3256 if (cmd->transport_complete_callback)
3257 cmd->transport_complete_callback(cmd);
3259 switch (cmd->data_direction) {
3260 case DMA_FROM_DEVICE:
3261 spin_lock(&cmd->se_lun->lun_sep_lock);
3262 if (cmd->se_lun->lun_sep) {
3263 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3266 spin_unlock(&cmd->se_lun->lun_sep_lock);
3268 ret = cmd->se_tfo->queue_data_in(cmd);
3269 if (ret == -EAGAIN || ret == -ENOMEM)
3273 spin_lock(&cmd->se_lun->lun_sep_lock);
3274 if (cmd->se_lun->lun_sep) {
3275 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3278 spin_unlock(&cmd->se_lun->lun_sep_lock);
3280 * Check if we need to send READ payload for BIDI-COMMAND
3282 if (cmd->t_bidi_data_sg) {
3283 spin_lock(&cmd->se_lun->lun_sep_lock);
3284 if (cmd->se_lun->lun_sep) {
3285 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3288 spin_unlock(&cmd->se_lun->lun_sep_lock);
3289 ret = cmd->se_tfo->queue_data_in(cmd);
3290 if (ret == -EAGAIN || ret == -ENOMEM)
3294 /* Fall through for DMA_TO_DEVICE */
3296 ret = cmd->se_tfo->queue_status(cmd);
3297 if (ret == -EAGAIN || ret == -ENOMEM)
3304 transport_lun_remove_cmd(cmd);
3305 transport_cmd_check_stop_to_fabric(cmd);
3309 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3310 " data_direction: %d\n", cmd, cmd->data_direction);
3311 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3312 transport_handle_queue_full(cmd, cmd->se_dev);
3315 static void transport_free_dev_tasks(struct se_cmd *cmd)
3317 struct se_task *task, *task_tmp;
3318 unsigned long flags;
3319 LIST_HEAD(dispose_list);
3321 spin_lock_irqsave(&cmd->t_state_lock, flags);
3322 list_for_each_entry_safe(task, task_tmp,
3323 &cmd->t_task_list, t_list) {
3324 if (!(task->task_flags & TF_ACTIVE))
3325 list_move_tail(&task->t_list, &dispose_list);
3327 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3329 while (!list_empty(&dispose_list)) {
3330 task = list_first_entry(&dispose_list, struct se_task, t_list);
3332 if (task->task_sg != cmd->t_data_sg &&
3333 task->task_sg != cmd->t_bidi_data_sg)
3334 kfree(task->task_sg);
3336 list_del(&task->t_list);
3338 cmd->se_dev->transport->free_task(task);
3342 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3344 struct scatterlist *sg;
3347 for_each_sg(sgl, sg, nents, count)
3348 __free_page(sg_page(sg));
3353 static inline void transport_free_pages(struct se_cmd *cmd)
3355 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3358 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3359 cmd->t_data_sg = NULL;
3360 cmd->t_data_nents = 0;
3362 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3363 cmd->t_bidi_data_sg = NULL;
3364 cmd->t_bidi_data_nents = 0;
3368 * transport_put_cmd - release a reference to a command
3369 * @cmd: command to release
3371 * This routine releases our reference to the command and frees it if possible.
3373 static void transport_put_cmd(struct se_cmd *cmd)
3375 unsigned long flags;
3378 spin_lock_irqsave(&cmd->t_state_lock, flags);
3379 if (atomic_read(&cmd->t_fe_count)) {
3380 if (!atomic_dec_and_test(&cmd->t_fe_count))
3384 if (atomic_read(&cmd->t_se_count)) {
3385 if (!atomic_dec_and_test(&cmd->t_se_count))
3389 if (atomic_read(&cmd->transport_dev_active)) {
3390 atomic_set(&cmd->transport_dev_active, 0);
3391 transport_all_task_dev_remove_state(cmd);
3394 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3396 if (free_tasks != 0)
3397 transport_free_dev_tasks(cmd);
3399 transport_free_pages(cmd);
3400 transport_release_cmd(cmd);
3403 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3407 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3408 * allocating in the core.
3409 * @cmd: Associated se_cmd descriptor
3410 * @mem: SGL style memory for TCM WRITE / READ
3411 * @sg_mem_num: Number of SGL elements
3412 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3413 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3415 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3418 int transport_generic_map_mem_to_cmd(
3420 struct scatterlist *sgl,
3422 struct scatterlist *sgl_bidi,
3425 if (!sgl || !sgl_count)
3428 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3429 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3431 cmd->t_data_sg = sgl;
3432 cmd->t_data_nents = sgl_count;
3434 if (sgl_bidi && sgl_bidi_count) {
3435 cmd->t_bidi_data_sg = sgl_bidi;
3436 cmd->t_bidi_data_nents = sgl_bidi_count;
3438 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3443 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3445 void *transport_kmap_first_data_page(struct se_cmd *cmd)
3447 struct scatterlist *sg = cmd->t_data_sg;
3451 * We need to take into account a possible offset here for fabrics like
3452 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3453 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3455 return kmap(sg_page(sg)) + sg->offset;
3457 EXPORT_SYMBOL(transport_kmap_first_data_page);
3459 void transport_kunmap_first_data_page(struct se_cmd *cmd)
3461 kunmap(sg_page(cmd->t_data_sg));
3463 EXPORT_SYMBOL(transport_kunmap_first_data_page);
3466 transport_generic_get_mem(struct se_cmd *cmd)
3468 u32 length = cmd->data_length;
3473 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3474 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3475 if (!cmd->t_data_sg)
3478 cmd->t_data_nents = nents;
3479 sg_init_table(cmd->t_data_sg, nents);
3482 u32 page_len = min_t(u32, length, PAGE_SIZE);
3483 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3487 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3495 __free_page(sg_page(&cmd->t_data_sg[i]));
3498 kfree(cmd->t_data_sg);
3499 cmd->t_data_sg = NULL;
3503 /* Reduce sectors if they are too long for the device */
3504 static inline sector_t transport_limit_task_sectors(
3505 struct se_device *dev,
3506 unsigned long long lba,
3509 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
3511 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3512 if ((lba + sectors) > transport_dev_end_lba(dev))
3513 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
3520 * This function can be used by HW target mode drivers to create a linked
3521 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3522 * This is intended to be called during the completion path by TCM Core
3523 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3525 void transport_do_task_sg_chain(struct se_cmd *cmd)
3527 struct scatterlist *sg_first = NULL;
3528 struct scatterlist *sg_prev = NULL;
3529 int sg_prev_nents = 0;
3530 struct scatterlist *sg;
3531 struct se_task *task;
3532 u32 chained_nents = 0;
3535 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3538 * Walk the struct se_task list and setup scatterlist chains
3539 * for each contiguously allocated struct se_task->task_sg[].
3541 list_for_each_entry(task, &cmd->t_task_list, t_list) {
3546 sg_first = task->task_sg;
3547 chained_nents = task->task_sg_nents;
3549 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
3550 chained_nents += task->task_sg_nents;
3553 * For the padded tasks, use the extra SGL vector allocated
3554 * in transport_allocate_data_tasks() for the sg_prev_nents
3555 * offset into sg_chain() above.
3557 * We do not need the padding for the last task (or a single
3558 * task), but in that case we will never use the sg_prev_nents
3559 * value below which would be incorrect.
3561 sg_prev_nents = (task->task_sg_nents + 1);
3562 sg_prev = task->task_sg;
3565 * Setup the starting pointer and total t_tasks_sg_linked_no including
3566 * padding SGs for linking and to mark the end.
3568 cmd->t_tasks_sg_chained = sg_first;
3569 cmd->t_tasks_sg_chained_no = chained_nents;
3571 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
3572 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3573 cmd->t_tasks_sg_chained_no);
3575 for_each_sg(cmd->t_tasks_sg_chained, sg,
3576 cmd->t_tasks_sg_chained_no, i) {
3578 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
3579 i, sg, sg_page(sg), sg->length, sg->offset);
3580 if (sg_is_chain(sg))
3581 pr_debug("SG: %p sg_is_chain=1\n", sg);
3583 pr_debug("SG: %p sg_is_last=1\n", sg);
3586 EXPORT_SYMBOL(transport_do_task_sg_chain);
3589 * Break up cmd into chunks transport can handle
3592 transport_allocate_data_tasks(struct se_cmd *cmd,
3593 enum dma_data_direction data_direction,
3594 struct scatterlist *cmd_sg, unsigned int sgl_nents)
3596 struct se_device *dev = cmd->se_dev;
3598 unsigned long long lba;
3599 sector_t sectors, dev_max_sectors;
3602 if (transport_cmd_get_valid_sectors(cmd) < 0)
3605 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3606 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
3608 WARN_ON(cmd->data_length % sector_size);
3610 lba = cmd->t_task_lba;
3611 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
3612 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
3615 * If we need just a single task reuse the SG list in the command
3616 * and avoid a lot of work.
3618 if (task_count == 1) {
3619 struct se_task *task;
3620 unsigned long flags;
3622 task = transport_generic_get_task(cmd, data_direction);
3626 task->task_sg = cmd_sg;
3627 task->task_sg_nents = sgl_nents;
3629 task->task_lba = lba;
3630 task->task_sectors = sectors;
3631 task->task_size = task->task_sectors * sector_size;
3633 spin_lock_irqsave(&cmd->t_state_lock, flags);
3634 list_add_tail(&task->t_list, &cmd->t_task_list);
3635 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3640 for (i = 0; i < task_count; i++) {
3641 struct se_task *task;
3642 unsigned int task_size, task_sg_nents_padded;
3643 struct scatterlist *sg;
3644 unsigned long flags;
3647 task = transport_generic_get_task(cmd, data_direction);
3651 task->task_lba = lba;
3652 task->task_sectors = min(sectors, dev_max_sectors);
3653 task->task_size = task->task_sectors * sector_size;
3656 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3657 * in order to calculate the number per task SGL entries
3659 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
3661 * Check if the fabric module driver is requesting that all
3662 * struct se_task->task_sg[] be chained together.. If so,
3663 * then allocate an extra padding SG entry for linking and
3664 * marking the end of the chained SGL for every task except
3665 * the last one for (task_count > 1) operation, or skipping
3666 * the extra padding for the (task_count == 1) case.
3668 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3669 task_sg_nents_padded = (task->task_sg_nents + 1);
3671 task_sg_nents_padded = task->task_sg_nents;
3673 task->task_sg = kmalloc(sizeof(struct scatterlist) *
3674 task_sg_nents_padded, GFP_KERNEL);
3675 if (!task->task_sg) {
3676 cmd->se_dev->transport->free_task(task);
3680 sg_init_table(task->task_sg, task_sg_nents_padded);
3682 task_size = task->task_size;
3684 /* Build new sgl, only up to task_size */
3685 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
3686 if (cmd_sg->length > task_size)
3690 task_size -= cmd_sg->length;
3691 cmd_sg = sg_next(cmd_sg);
3694 lba += task->task_sectors;
3695 sectors -= task->task_sectors;
3697 spin_lock_irqsave(&cmd->t_state_lock, flags);
3698 list_add_tail(&task->t_list, &cmd->t_task_list);
3699 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3706 transport_allocate_control_task(struct se_cmd *cmd)
3708 struct se_task *task;
3709 unsigned long flags;
3711 task = transport_generic_get_task(cmd, cmd->data_direction);
3715 task->task_sg = cmd->t_data_sg;
3716 task->task_size = cmd->data_length;
3717 task->task_sg_nents = cmd->t_data_nents;
3719 spin_lock_irqsave(&cmd->t_state_lock, flags);
3720 list_add_tail(&task->t_list, &cmd->t_task_list);
3721 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3723 /* Success! Return number of tasks allocated */
3728 * Allocate any required ressources to execute the command, and either place
3729 * it on the execution queue if possible. For writes we might not have the
3730 * payload yet, thus notify the fabric via a call to ->write_pending instead.
3732 int transport_generic_new_cmd(struct se_cmd *cmd)
3734 struct se_device *dev = cmd->se_dev;
3735 int task_cdbs, task_cdbs_bidi = 0;
3740 * Determine is the TCM fabric module has already allocated physical
3741 * memory, and is directly calling transport_generic_map_mem_to_cmd()
3744 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3746 ret = transport_generic_get_mem(cmd);
3752 * For BIDI command set up the read tasks first.
3754 if (cmd->t_bidi_data_sg &&
3755 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3756 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3758 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3759 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3760 cmd->t_bidi_data_nents);
3761 if (task_cdbs_bidi <= 0)
3764 atomic_inc(&cmd->t_fe_count);
3765 atomic_inc(&cmd->t_se_count);
3769 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3770 task_cdbs = transport_allocate_data_tasks(cmd,
3771 cmd->data_direction, cmd->t_data_sg,
3774 task_cdbs = transport_allocate_control_task(cmd);
3781 atomic_inc(&cmd->t_fe_count);
3782 atomic_inc(&cmd->t_se_count);
3785 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3786 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3787 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
3790 * For WRITEs, let the fabric know its buffer is ready..
3791 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3792 * will be added to the struct se_device execution queue after its WRITE
3793 * data has arrived. (ie: It gets handled by the transport processing
3794 * thread a second time)
3796 if (cmd->data_direction == DMA_TO_DEVICE) {
3797 transport_add_tasks_to_state_queue(cmd);
3798 return transport_generic_write_pending(cmd);
3801 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3802 * to the execution queue.
3804 transport_execute_tasks(cmd);
3808 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3809 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3812 EXPORT_SYMBOL(transport_generic_new_cmd);
3814 /* transport_generic_process_write():
3818 void transport_generic_process_write(struct se_cmd *cmd)
3820 transport_execute_tasks(cmd);
3822 EXPORT_SYMBOL(transport_generic_process_write);
3824 static void transport_write_pending_qf(struct se_cmd *cmd)
3828 ret = cmd->se_tfo->write_pending(cmd);
3829 if (ret == -EAGAIN || ret == -ENOMEM) {
3830 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3832 transport_handle_queue_full(cmd, cmd->se_dev);
3836 static int transport_generic_write_pending(struct se_cmd *cmd)
3838 unsigned long flags;
3841 spin_lock_irqsave(&cmd->t_state_lock, flags);
3842 cmd->t_state = TRANSPORT_WRITE_PENDING;
3843 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3846 * Clear the se_cmd for WRITE_PENDING status in order to set
3847 * cmd->t_transport_active=0 so that transport_generic_handle_data
3848 * can be called from HW target mode interrupt code. This is safe
3849 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
3850 * because the se_cmd->se_lun pointer is not being cleared.
3852 transport_cmd_check_stop(cmd, 1, 0);
3855 * Call the fabric write_pending function here to let the
3856 * frontend know that WRITE buffers are ready.
3858 ret = cmd->se_tfo->write_pending(cmd);
3859 if (ret == -EAGAIN || ret == -ENOMEM)
3867 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
3868 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
3869 transport_handle_queue_full(cmd, cmd->se_dev);
3874 * transport_release_cmd - free a command
3875 * @cmd: command to free
3877 * This routine unconditionally frees a command, and reference counting
3878 * or list removal must be done in the caller.
3880 void transport_release_cmd(struct se_cmd *cmd)
3882 BUG_ON(!cmd->se_tfo);
3884 if (cmd->se_tmr_req)
3885 core_tmr_release_req(cmd->se_tmr_req);
3886 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3887 kfree(cmd->t_task_cdb);
3889 * Check if target_wait_for_sess_cmds() is expecting to
3890 * release se_cmd directly here..
3892 if (cmd->check_release != 0 && cmd->se_tfo->check_release_cmd)
3893 if (cmd->se_tfo->check_release_cmd(cmd) != 0)
3896 cmd->se_tfo->release_cmd(cmd);
3898 EXPORT_SYMBOL(transport_release_cmd);
3900 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
3902 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3903 if (wait_for_tasks && cmd->se_tmr_req)
3904 transport_wait_for_tasks(cmd);
3906 transport_release_cmd(cmd);
3909 transport_wait_for_tasks(cmd);
3911 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3914 transport_lun_remove_cmd(cmd);
3916 transport_free_dev_tasks(cmd);
3918 transport_put_cmd(cmd);
3921 EXPORT_SYMBOL(transport_generic_free_cmd);
3923 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
3924 * @se_sess: session to reference
3925 * @se_cmd: command descriptor to add
3927 void target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3929 unsigned long flags;
3931 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3932 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
3933 se_cmd->check_release = 1;
3934 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3936 EXPORT_SYMBOL(target_get_sess_cmd);
3938 /* target_put_sess_cmd - Check for active I/O shutdown or list delete
3939 * @se_sess: session to reference
3940 * @se_cmd: command descriptor to drop
3942 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
3944 unsigned long flags;
3946 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3947 if (list_empty(&se_cmd->se_cmd_list)) {
3948 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3953 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
3954 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3955 complete(&se_cmd->cmd_wait_comp);
3958 list_del(&se_cmd->se_cmd_list);
3959 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3963 EXPORT_SYMBOL(target_put_sess_cmd);
3965 /* target_splice_sess_cmd_list - Split active cmds into sess_wait_list
3966 * @se_sess: session to split
3968 void target_splice_sess_cmd_list(struct se_session *se_sess)
3970 struct se_cmd *se_cmd;
3971 unsigned long flags;
3973 WARN_ON(!list_empty(&se_sess->sess_wait_list));
3974 INIT_LIST_HEAD(&se_sess->sess_wait_list);
3976 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
3977 se_sess->sess_tearing_down = 1;
3979 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
3981 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
3982 se_cmd->cmd_wait_set = 1;
3984 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
3986 EXPORT_SYMBOL(target_splice_sess_cmd_list);
3988 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
3989 * @se_sess: session to wait for active I/O
3990 * @wait_for_tasks: Make extra transport_wait_for_tasks call
3992 void target_wait_for_sess_cmds(
3993 struct se_session *se_sess,
3996 struct se_cmd *se_cmd, *tmp_cmd;
3999 list_for_each_entry_safe(se_cmd, tmp_cmd,
4000 &se_sess->sess_wait_list, se_cmd_list) {
4001 list_del(&se_cmd->se_cmd_list);
4003 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
4004 " %d\n", se_cmd, se_cmd->t_state,
4005 se_cmd->se_tfo->get_cmd_state(se_cmd));
4007 if (wait_for_tasks) {
4008 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
4009 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4010 se_cmd->se_tfo->get_cmd_state(se_cmd));
4012 rc = transport_wait_for_tasks(se_cmd);
4014 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
4015 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4016 se_cmd->se_tfo->get_cmd_state(se_cmd));
4020 wait_for_completion(&se_cmd->cmd_wait_comp);
4021 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
4022 " fabric state: %d\n", se_cmd, se_cmd->t_state,
4023 se_cmd->se_tfo->get_cmd_state(se_cmd));
4026 se_cmd->se_tfo->release_cmd(se_cmd);
4029 EXPORT_SYMBOL(target_wait_for_sess_cmds);
4031 /* transport_lun_wait_for_tasks():
4033 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4034 * an struct se_lun to be successfully shutdown.
4036 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4038 unsigned long flags;
4041 * If the frontend has already requested this struct se_cmd to
4042 * be stopped, we can safely ignore this struct se_cmd.
4044 spin_lock_irqsave(&cmd->t_state_lock, flags);
4045 if (atomic_read(&cmd->t_transport_stop)) {
4046 atomic_set(&cmd->transport_lun_stop, 0);
4047 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4048 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
4049 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4050 transport_cmd_check_stop(cmd, 1, 0);
4053 atomic_set(&cmd->transport_lun_fe_stop, 1);
4054 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4056 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4058 ret = transport_stop_tasks_for_cmd(cmd);
4060 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4061 " %d\n", cmd, cmd->t_task_list_num, ret);
4063 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4064 cmd->se_tfo->get_task_tag(cmd));
4065 wait_for_completion(&cmd->transport_lun_stop_comp);
4066 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4067 cmd->se_tfo->get_task_tag(cmd));
4069 transport_remove_cmd_from_queue(cmd);
4074 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4076 struct se_cmd *cmd = NULL;
4077 unsigned long lun_flags, cmd_flags;
4079 * Do exception processing and return CHECK_CONDITION status to the
4082 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4083 while (!list_empty(&lun->lun_cmd_list)) {
4084 cmd = list_first_entry(&lun->lun_cmd_list,
4085 struct se_cmd, se_lun_node);
4086 list_del(&cmd->se_lun_node);
4088 atomic_set(&cmd->transport_lun_active, 0);
4090 * This will notify iscsi_target_transport.c:
4091 * transport_cmd_check_stop() that a LUN shutdown is in
4092 * progress for the iscsi_cmd_t.
4094 spin_lock(&cmd->t_state_lock);
4095 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4096 "_lun_stop for ITT: 0x%08x\n",
4097 cmd->se_lun->unpacked_lun,
4098 cmd->se_tfo->get_task_tag(cmd));
4099 atomic_set(&cmd->transport_lun_stop, 1);
4100 spin_unlock(&cmd->t_state_lock);
4102 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4105 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4106 cmd->se_tfo->get_task_tag(cmd),
4107 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4111 * If the Storage engine still owns the iscsi_cmd_t, determine
4112 * and/or stop its context.
4114 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4115 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4116 cmd->se_tfo->get_task_tag(cmd));
4118 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4119 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4123 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4124 "_wait_for_tasks(): SUCCESS\n",
4125 cmd->se_lun->unpacked_lun,
4126 cmd->se_tfo->get_task_tag(cmd));
4128 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4129 if (!atomic_read(&cmd->transport_dev_active)) {
4130 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4133 atomic_set(&cmd->transport_dev_active, 0);
4134 transport_all_task_dev_remove_state(cmd);
4135 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4137 transport_free_dev_tasks(cmd);
4139 * The Storage engine stopped this struct se_cmd before it was
4140 * send to the fabric frontend for delivery back to the
4141 * Initiator Node. Return this SCSI CDB back with an
4142 * CHECK_CONDITION status.
4145 transport_send_check_condition_and_sense(cmd,
4146 TCM_NON_EXISTENT_LUN, 0);
4148 * If the fabric frontend is waiting for this iscsi_cmd_t to
4149 * be released, notify the waiting thread now that LU has
4150 * finished accessing it.
4152 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4153 if (atomic_read(&cmd->transport_lun_fe_stop)) {
4154 pr_debug("SE_LUN[%d] - Detected FE stop for"
4155 " struct se_cmd: %p ITT: 0x%08x\n",
4157 cmd, cmd->se_tfo->get_task_tag(cmd));
4159 spin_unlock_irqrestore(&cmd->t_state_lock,
4161 transport_cmd_check_stop(cmd, 1, 0);
4162 complete(&cmd->transport_lun_fe_stop_comp);
4163 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4166 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4167 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4169 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4170 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4172 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4175 static int transport_clear_lun_thread(void *p)
4177 struct se_lun *lun = (struct se_lun *)p;
4179 __transport_clear_lun_from_sessions(lun);
4180 complete(&lun->lun_shutdown_comp);
4185 int transport_clear_lun_from_sessions(struct se_lun *lun)
4187 struct task_struct *kt;
4189 kt = kthread_run(transport_clear_lun_thread, lun,
4190 "tcm_cl_%u", lun->unpacked_lun);
4192 pr_err("Unable to start clear_lun thread\n");
4195 wait_for_completion(&lun->lun_shutdown_comp);
4201 * transport_wait_for_tasks - wait for completion to occur
4202 * @cmd: command to wait
4204 * Called from frontend fabric context to wait for storage engine
4205 * to pause and/or release frontend generated struct se_cmd.
4207 bool transport_wait_for_tasks(struct se_cmd *cmd)
4209 unsigned long flags;
4211 spin_lock_irqsave(&cmd->t_state_lock, flags);
4212 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4213 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4217 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4218 * has been set in transport_set_supported_SAM_opcode().
4220 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4221 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4225 * If we are already stopped due to an external event (ie: LUN shutdown)
4226 * sleep until the connection can have the passed struct se_cmd back.
4227 * The cmd->transport_lun_stopped_sem will be upped by
4228 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4229 * has completed its operation on the struct se_cmd.
4231 if (atomic_read(&cmd->transport_lun_stop)) {
4233 pr_debug("wait_for_tasks: Stopping"
4234 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4235 "_stop_comp); for ITT: 0x%08x\n",
4236 cmd->se_tfo->get_task_tag(cmd));
4238 * There is a special case for WRITES where a FE exception +
4239 * LUN shutdown means ConfigFS context is still sleeping on
4240 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4241 * We go ahead and up transport_lun_stop_comp just to be sure
4244 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4245 complete(&cmd->transport_lun_stop_comp);
4246 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4247 spin_lock_irqsave(&cmd->t_state_lock, flags);
4249 transport_all_task_dev_remove_state(cmd);
4251 * At this point, the frontend who was the originator of this
4252 * struct se_cmd, now owns the structure and can be released through
4253 * normal means below.
4255 pr_debug("wait_for_tasks: Stopped"
4256 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4257 "stop_comp); for ITT: 0x%08x\n",
4258 cmd->se_tfo->get_task_tag(cmd));
4260 atomic_set(&cmd->transport_lun_stop, 0);
4262 if (!atomic_read(&cmd->t_transport_active) ||
4263 atomic_read(&cmd->t_transport_aborted)) {
4264 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4268 atomic_set(&cmd->t_transport_stop, 1);
4270 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4271 " i_state: %d, t_state: %d, t_transport_stop = TRUE\n",
4272 cmd, cmd->se_tfo->get_task_tag(cmd),
4273 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4275 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4277 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4279 wait_for_completion(&cmd->t_transport_stop_comp);
4281 spin_lock_irqsave(&cmd->t_state_lock, flags);
4282 atomic_set(&cmd->t_transport_active, 0);
4283 atomic_set(&cmd->t_transport_stop, 0);
4285 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4286 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4287 cmd->se_tfo->get_task_tag(cmd));
4289 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4293 EXPORT_SYMBOL(transport_wait_for_tasks);
4295 static int transport_get_sense_codes(
4300 *asc = cmd->scsi_asc;
4301 *ascq = cmd->scsi_ascq;
4306 static int transport_set_sense_codes(
4311 cmd->scsi_asc = asc;
4312 cmd->scsi_ascq = ascq;
4317 int transport_send_check_condition_and_sense(
4322 unsigned char *buffer = cmd->sense_buffer;
4323 unsigned long flags;
4325 u8 asc = 0, ascq = 0;
4327 spin_lock_irqsave(&cmd->t_state_lock, flags);
4328 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4329 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4332 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4333 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4335 if (!reason && from_transport)
4338 if (!from_transport)
4339 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4341 * Data Segment and SenseLength of the fabric response PDU.
4343 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4344 * from include/scsi/scsi_cmnd.h
4346 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4347 TRANSPORT_SENSE_BUFFER);
4349 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4350 * SENSE KEY values from include/scsi/scsi.h
4353 case TCM_NON_EXISTENT_LUN:
4355 buffer[offset] = 0x70;
4356 /* ILLEGAL REQUEST */
4357 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4358 /* LOGICAL UNIT NOT SUPPORTED */
4359 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4361 case TCM_UNSUPPORTED_SCSI_OPCODE:
4362 case TCM_SECTOR_COUNT_TOO_MANY:
4364 buffer[offset] = 0x70;
4365 /* ILLEGAL REQUEST */
4366 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4367 /* INVALID COMMAND OPERATION CODE */
4368 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4370 case TCM_UNKNOWN_MODE_PAGE:
4372 buffer[offset] = 0x70;
4373 /* ILLEGAL REQUEST */
4374 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4375 /* INVALID FIELD IN CDB */
4376 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4378 case TCM_CHECK_CONDITION_ABORT_CMD:
4380 buffer[offset] = 0x70;
4381 /* ABORTED COMMAND */
4382 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4383 /* BUS DEVICE RESET FUNCTION OCCURRED */
4384 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4385 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4387 case TCM_INCORRECT_AMOUNT_OF_DATA:
4389 buffer[offset] = 0x70;
4390 /* ABORTED COMMAND */
4391 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4393 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4394 /* NOT ENOUGH UNSOLICITED DATA */
4395 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4397 case TCM_INVALID_CDB_FIELD:
4399 buffer[offset] = 0x70;
4400 /* ABORTED COMMAND */
4401 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4402 /* INVALID FIELD IN CDB */
4403 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4405 case TCM_INVALID_PARAMETER_LIST:
4407 buffer[offset] = 0x70;
4408 /* ABORTED COMMAND */
4409 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4410 /* INVALID FIELD IN PARAMETER LIST */
4411 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4413 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4415 buffer[offset] = 0x70;
4416 /* ABORTED COMMAND */
4417 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4419 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4420 /* UNEXPECTED_UNSOLICITED_DATA */
4421 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4423 case TCM_SERVICE_CRC_ERROR:
4425 buffer[offset] = 0x70;
4426 /* ABORTED COMMAND */
4427 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4428 /* PROTOCOL SERVICE CRC ERROR */
4429 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4431 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4433 case TCM_SNACK_REJECTED:
4435 buffer[offset] = 0x70;
4436 /* ABORTED COMMAND */
4437 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4439 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4440 /* FAILED RETRANSMISSION REQUEST */
4441 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4443 case TCM_WRITE_PROTECTED:
4445 buffer[offset] = 0x70;
4447 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4448 /* WRITE PROTECTED */
4449 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4451 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4453 buffer[offset] = 0x70;
4454 /* UNIT ATTENTION */
4455 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4456 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4457 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4458 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4460 case TCM_CHECK_CONDITION_NOT_READY:
4462 buffer[offset] = 0x70;
4464 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4465 transport_get_sense_codes(cmd, &asc, &ascq);
4466 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4467 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4469 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4472 buffer[offset] = 0x70;
4473 /* ILLEGAL REQUEST */
4474 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4475 /* LOGICAL UNIT COMMUNICATION FAILURE */
4476 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4480 * This code uses linux/include/scsi/scsi.h SAM status codes!
4482 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4484 * Automatically padded, this value is encoded in the fabric's
4485 * data_length response PDU containing the SCSI defined sense data.
4487 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4490 return cmd->se_tfo->queue_status(cmd);
4492 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4494 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4498 if (atomic_read(&cmd->t_transport_aborted) != 0) {
4500 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4503 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4504 " status for CDB: 0x%02x ITT: 0x%08x\n",
4506 cmd->se_tfo->get_task_tag(cmd));
4508 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4509 cmd->se_tfo->queue_status(cmd);
4514 EXPORT_SYMBOL(transport_check_aborted_status);
4516 void transport_send_task_abort(struct se_cmd *cmd)
4518 unsigned long flags;
4520 spin_lock_irqsave(&cmd->t_state_lock, flags);
4521 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4522 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4525 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4528 * If there are still expected incoming fabric WRITEs, we wait
4529 * until until they have completed before sending a TASK_ABORTED
4530 * response. This response with TASK_ABORTED status will be
4531 * queued back to fabric module by transport_check_aborted_status().
4533 if (cmd->data_direction == DMA_TO_DEVICE) {
4534 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4535 atomic_inc(&cmd->t_transport_aborted);
4536 smp_mb__after_atomic_inc();
4539 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4541 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4542 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4543 cmd->se_tfo->get_task_tag(cmd));
4545 cmd->se_tfo->queue_status(cmd);
4548 /* transport_generic_do_tmr():
4552 int transport_generic_do_tmr(struct se_cmd *cmd)
4554 struct se_device *dev = cmd->se_dev;
4555 struct se_tmr_req *tmr = cmd->se_tmr_req;
4558 switch (tmr->function) {
4559 case TMR_ABORT_TASK:
4560 tmr->response = TMR_FUNCTION_REJECTED;
4562 case TMR_ABORT_TASK_SET:
4564 case TMR_CLEAR_TASK_SET:
4565 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4568 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4569 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4570 TMR_FUNCTION_REJECTED;
4572 case TMR_TARGET_WARM_RESET:
4573 tmr->response = TMR_FUNCTION_REJECTED;
4575 case TMR_TARGET_COLD_RESET:
4576 tmr->response = TMR_FUNCTION_REJECTED;
4579 pr_err("Uknown TMR function: 0x%02x.\n",
4581 tmr->response = TMR_FUNCTION_REJECTED;
4585 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4586 cmd->se_tfo->queue_tm_rsp(cmd);
4588 transport_cmd_check_stop_to_fabric(cmd);
4592 /* transport_processing_thread():
4596 static int transport_processing_thread(void *param)
4600 struct se_device *dev = (struct se_device *) param;
4602 set_user_nice(current, -20);
4604 while (!kthread_should_stop()) {
4605 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4606 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4607 kthread_should_stop());
4612 __transport_execute_tasks(dev);
4614 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4618 switch (cmd->t_state) {
4619 case TRANSPORT_NEW_CMD:
4622 case TRANSPORT_NEW_CMD_MAP:
4623 if (!cmd->se_tfo->new_cmd_map) {
4624 pr_err("cmd->se_tfo->new_cmd_map is"
4625 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4628 ret = cmd->se_tfo->new_cmd_map(cmd);
4630 transport_generic_request_failure(cmd);
4633 ret = transport_generic_new_cmd(cmd);
4635 transport_generic_request_failure(cmd);
4639 case TRANSPORT_PROCESS_WRITE:
4640 transport_generic_process_write(cmd);
4642 case TRANSPORT_PROCESS_TMR:
4643 transport_generic_do_tmr(cmd);
4645 case TRANSPORT_COMPLETE_QF_WP:
4646 transport_write_pending_qf(cmd);
4648 case TRANSPORT_COMPLETE_QF_OK:
4649 transport_complete_qf(cmd);
4652 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4653 "i_state: %d on SE LUN: %u\n",
4655 cmd->se_tfo->get_task_tag(cmd),
4656 cmd->se_tfo->get_cmd_state(cmd),
4657 cmd->se_lun->unpacked_lun);
4665 WARN_ON(!list_empty(&dev->state_task_list));
4666 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4667 dev->process_thread = NULL;