1 /*******************************************************************************
2 * Filename: target_core_transport.c
4 * This file contains the Generic Target Engine Core.
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
11 * Nicholas A. Bellinger <nab@kernel.org>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ******************************************************************************/
29 #include <linux/net.h>
30 #include <linux/delay.h>
31 #include <linux/string.h>
32 #include <linux/timer.h>
33 #include <linux/slab.h>
34 #include <linux/blkdev.h>
35 #include <linux/spinlock.h>
36 #include <linux/kthread.h>
38 #include <linux/cdrom.h>
39 #include <asm/unaligned.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_tcq.h>
46 #include <target/target_core_base.h>
47 #include <target/target_core_device.h>
48 #include <target/target_core_tmr.h>
49 #include <target/target_core_tpg.h>
50 #include <target/target_core_transport.h>
51 #include <target/target_core_fabric_ops.h>
52 #include <target/target_core_configfs.h>
54 #include "target_core_alua.h"
55 #include "target_core_hba.h"
56 #include "target_core_pr.h"
57 #include "target_core_ua.h"
59 static int sub_api_initialized;
61 static struct workqueue_struct *target_completion_wq;
62 static struct kmem_cache *se_cmd_cache;
63 static struct kmem_cache *se_sess_cache;
64 struct kmem_cache *se_tmr_req_cache;
65 struct kmem_cache *se_ua_cache;
66 struct kmem_cache *t10_pr_reg_cache;
67 struct kmem_cache *t10_alua_lu_gp_cache;
68 struct kmem_cache *t10_alua_lu_gp_mem_cache;
69 struct kmem_cache *t10_alua_tg_pt_gp_cache;
70 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
72 static int transport_generic_write_pending(struct se_cmd *);
73 static int transport_processing_thread(void *param);
74 static int __transport_execute_tasks(struct se_device *dev);
75 static void transport_complete_task_attr(struct se_cmd *cmd);
76 static void transport_handle_queue_full(struct se_cmd *cmd,
77 struct se_device *dev);
78 static void transport_free_dev_tasks(struct se_cmd *cmd);
79 static int transport_generic_get_mem(struct se_cmd *cmd);
80 static void transport_put_cmd(struct se_cmd *cmd);
81 static void transport_remove_cmd_from_queue(struct se_cmd *cmd);
82 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
83 static void transport_generic_request_failure(struct se_cmd *, int, int);
84 static void target_complete_ok_work(struct work_struct *work);
86 int init_se_kmem_caches(void)
88 se_cmd_cache = kmem_cache_create("se_cmd_cache",
89 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
91 pr_err("kmem_cache_create for struct se_cmd failed\n");
94 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
95 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
97 if (!se_tmr_req_cache) {
98 pr_err("kmem_cache_create() for struct se_tmr_req"
100 goto out_free_cmd_cache;
102 se_sess_cache = kmem_cache_create("se_sess_cache",
103 sizeof(struct se_session), __alignof__(struct se_session),
105 if (!se_sess_cache) {
106 pr_err("kmem_cache_create() for struct se_session"
108 goto out_free_tmr_req_cache;
110 se_ua_cache = kmem_cache_create("se_ua_cache",
111 sizeof(struct se_ua), __alignof__(struct se_ua),
114 pr_err("kmem_cache_create() for struct se_ua failed\n");
115 goto out_free_sess_cache;
117 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
118 sizeof(struct t10_pr_registration),
119 __alignof__(struct t10_pr_registration), 0, NULL);
120 if (!t10_pr_reg_cache) {
121 pr_err("kmem_cache_create() for struct t10_pr_registration"
123 goto out_free_ua_cache;
125 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
126 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
128 if (!t10_alua_lu_gp_cache) {
129 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
131 goto out_free_pr_reg_cache;
133 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
134 sizeof(struct t10_alua_lu_gp_member),
135 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
136 if (!t10_alua_lu_gp_mem_cache) {
137 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
139 goto out_free_lu_gp_cache;
141 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
142 sizeof(struct t10_alua_tg_pt_gp),
143 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
144 if (!t10_alua_tg_pt_gp_cache) {
145 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
147 goto out_free_lu_gp_mem_cache;
149 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
150 "t10_alua_tg_pt_gp_mem_cache",
151 sizeof(struct t10_alua_tg_pt_gp_member),
152 __alignof__(struct t10_alua_tg_pt_gp_member),
154 if (!t10_alua_tg_pt_gp_mem_cache) {
155 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
157 goto out_free_tg_pt_gp_cache;
160 target_completion_wq = alloc_workqueue("target_completion",
162 if (!target_completion_wq)
163 goto out_free_tg_pt_gp_mem_cache;
167 out_free_tg_pt_gp_mem_cache:
168 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
169 out_free_tg_pt_gp_cache:
170 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
171 out_free_lu_gp_mem_cache:
172 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
173 out_free_lu_gp_cache:
174 kmem_cache_destroy(t10_alua_lu_gp_cache);
175 out_free_pr_reg_cache:
176 kmem_cache_destroy(t10_pr_reg_cache);
178 kmem_cache_destroy(se_ua_cache);
180 kmem_cache_destroy(se_sess_cache);
181 out_free_tmr_req_cache:
182 kmem_cache_destroy(se_tmr_req_cache);
184 kmem_cache_destroy(se_cmd_cache);
189 void release_se_kmem_caches(void)
191 destroy_workqueue(target_completion_wq);
192 kmem_cache_destroy(se_cmd_cache);
193 kmem_cache_destroy(se_tmr_req_cache);
194 kmem_cache_destroy(se_sess_cache);
195 kmem_cache_destroy(se_ua_cache);
196 kmem_cache_destroy(t10_pr_reg_cache);
197 kmem_cache_destroy(t10_alua_lu_gp_cache);
198 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
199 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
200 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
203 /* This code ensures unique mib indexes are handed out. */
204 static DEFINE_SPINLOCK(scsi_mib_index_lock);
205 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
208 * Allocate a new row index for the entry type specified
210 u32 scsi_get_new_index(scsi_index_t type)
214 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
216 spin_lock(&scsi_mib_index_lock);
217 new_index = ++scsi_mib_index[type];
218 spin_unlock(&scsi_mib_index_lock);
223 void transport_init_queue_obj(struct se_queue_obj *qobj)
225 atomic_set(&qobj->queue_cnt, 0);
226 INIT_LIST_HEAD(&qobj->qobj_list);
227 init_waitqueue_head(&qobj->thread_wq);
228 spin_lock_init(&qobj->cmd_queue_lock);
230 EXPORT_SYMBOL(transport_init_queue_obj);
232 void transport_subsystem_check_init(void)
236 if (sub_api_initialized)
239 ret = request_module("target_core_iblock");
241 pr_err("Unable to load target_core_iblock\n");
243 ret = request_module("target_core_file");
245 pr_err("Unable to load target_core_file\n");
247 ret = request_module("target_core_pscsi");
249 pr_err("Unable to load target_core_pscsi\n");
251 ret = request_module("target_core_stgt");
253 pr_err("Unable to load target_core_stgt\n");
255 sub_api_initialized = 1;
259 struct se_session *transport_init_session(void)
261 struct se_session *se_sess;
263 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
265 pr_err("Unable to allocate struct se_session from"
267 return ERR_PTR(-ENOMEM);
269 INIT_LIST_HEAD(&se_sess->sess_list);
270 INIT_LIST_HEAD(&se_sess->sess_acl_list);
274 EXPORT_SYMBOL(transport_init_session);
277 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
279 void __transport_register_session(
280 struct se_portal_group *se_tpg,
281 struct se_node_acl *se_nacl,
282 struct se_session *se_sess,
283 void *fabric_sess_ptr)
285 unsigned char buf[PR_REG_ISID_LEN];
287 se_sess->se_tpg = se_tpg;
288 se_sess->fabric_sess_ptr = fabric_sess_ptr;
290 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
292 * Only set for struct se_session's that will actually be moving I/O.
293 * eg: *NOT* discovery sessions.
297 * If the fabric module supports an ISID based TransportID,
298 * save this value in binary from the fabric I_T Nexus now.
300 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
301 memset(&buf[0], 0, PR_REG_ISID_LEN);
302 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
303 &buf[0], PR_REG_ISID_LEN);
304 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
306 spin_lock_irq(&se_nacl->nacl_sess_lock);
308 * The se_nacl->nacl_sess pointer will be set to the
309 * last active I_T Nexus for each struct se_node_acl.
311 se_nacl->nacl_sess = se_sess;
313 list_add_tail(&se_sess->sess_acl_list,
314 &se_nacl->acl_sess_list);
315 spin_unlock_irq(&se_nacl->nacl_sess_lock);
317 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
319 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
320 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
322 EXPORT_SYMBOL(__transport_register_session);
324 void transport_register_session(
325 struct se_portal_group *se_tpg,
326 struct se_node_acl *se_nacl,
327 struct se_session *se_sess,
328 void *fabric_sess_ptr)
330 spin_lock_bh(&se_tpg->session_lock);
331 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
332 spin_unlock_bh(&se_tpg->session_lock);
334 EXPORT_SYMBOL(transport_register_session);
336 void transport_deregister_session_configfs(struct se_session *se_sess)
338 struct se_node_acl *se_nacl;
341 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
343 se_nacl = se_sess->se_node_acl;
345 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
346 list_del(&se_sess->sess_acl_list);
348 * If the session list is empty, then clear the pointer.
349 * Otherwise, set the struct se_session pointer from the tail
350 * element of the per struct se_node_acl active session list.
352 if (list_empty(&se_nacl->acl_sess_list))
353 se_nacl->nacl_sess = NULL;
355 se_nacl->nacl_sess = container_of(
356 se_nacl->acl_sess_list.prev,
357 struct se_session, sess_acl_list);
359 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
362 EXPORT_SYMBOL(transport_deregister_session_configfs);
364 void transport_free_session(struct se_session *se_sess)
366 kmem_cache_free(se_sess_cache, se_sess);
368 EXPORT_SYMBOL(transport_free_session);
370 void transport_deregister_session(struct se_session *se_sess)
372 struct se_portal_group *se_tpg = se_sess->se_tpg;
373 struct se_node_acl *se_nacl;
377 transport_free_session(se_sess);
381 spin_lock_irqsave(&se_tpg->session_lock, flags);
382 list_del(&se_sess->sess_list);
383 se_sess->se_tpg = NULL;
384 se_sess->fabric_sess_ptr = NULL;
385 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
388 * Determine if we need to do extra work for this initiator node's
389 * struct se_node_acl if it had been previously dynamically generated.
391 se_nacl = se_sess->se_node_acl;
393 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
394 if (se_nacl->dynamic_node_acl) {
395 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
397 list_del(&se_nacl->acl_list);
398 se_tpg->num_node_acls--;
399 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
401 core_tpg_wait_for_nacl_pr_ref(se_nacl);
402 core_free_device_list_for_node(se_nacl, se_tpg);
403 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
405 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
408 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
411 transport_free_session(se_sess);
413 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
414 se_tpg->se_tpg_tfo->get_fabric_name());
416 EXPORT_SYMBOL(transport_deregister_session);
419 * Called with cmd->t_state_lock held.
421 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
423 struct se_device *dev = cmd->se_dev;
424 struct se_task *task;
430 list_for_each_entry(task, &cmd->t_task_list, t_list) {
431 if (task->task_flags & TF_ACTIVE)
434 if (!atomic_read(&task->task_state_active))
437 spin_lock_irqsave(&dev->execute_task_lock, flags);
438 list_del(&task->t_state_list);
439 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
440 cmd->se_tfo->get_task_tag(cmd), dev, task);
441 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
443 atomic_set(&task->task_state_active, 0);
444 atomic_dec(&cmd->t_task_cdbs_ex_left);
448 /* transport_cmd_check_stop():
450 * 'transport_off = 1' determines if t_transport_active should be cleared.
451 * 'transport_off = 2' determines if task_dev_state should be removed.
453 * A non-zero u8 t_state sets cmd->t_state.
454 * Returns 1 when command is stopped, else 0.
456 static int transport_cmd_check_stop(
463 spin_lock_irqsave(&cmd->t_state_lock, flags);
465 * Determine if IOCTL context caller in requesting the stopping of this
466 * command for LUN shutdown purposes.
468 if (atomic_read(&cmd->transport_lun_stop)) {
469 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
470 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
471 cmd->se_tfo->get_task_tag(cmd));
473 atomic_set(&cmd->t_transport_active, 0);
474 if (transport_off == 2)
475 transport_all_task_dev_remove_state(cmd);
476 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
478 complete(&cmd->transport_lun_stop_comp);
482 * Determine if frontend context caller is requesting the stopping of
483 * this command for frontend exceptions.
485 if (atomic_read(&cmd->t_transport_stop)) {
486 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
487 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
488 cmd->se_tfo->get_task_tag(cmd));
490 if (transport_off == 2)
491 transport_all_task_dev_remove_state(cmd);
494 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
497 if (transport_off == 2)
499 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
501 complete(&cmd->t_transport_stop_comp);
505 atomic_set(&cmd->t_transport_active, 0);
506 if (transport_off == 2) {
507 transport_all_task_dev_remove_state(cmd);
509 * Clear struct se_cmd->se_lun before the transport_off == 2
510 * handoff to fabric module.
514 * Some fabric modules like tcm_loop can release
515 * their internally allocated I/O reference now and
518 * Fabric modules are expected to return '1' here if the
519 * se_cmd being passed is released at this point,
520 * or zero if not being released.
522 if (cmd->se_tfo->check_stop_free != NULL) {
523 spin_unlock_irqrestore(
524 &cmd->t_state_lock, flags);
526 return cmd->se_tfo->check_stop_free(cmd);
529 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
533 cmd->t_state = t_state;
534 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
539 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
541 return transport_cmd_check_stop(cmd, 2, 0);
544 static void transport_lun_remove_cmd(struct se_cmd *cmd)
546 struct se_lun *lun = cmd->se_lun;
552 spin_lock_irqsave(&cmd->t_state_lock, flags);
553 if (!atomic_read(&cmd->transport_dev_active)) {
554 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
557 atomic_set(&cmd->transport_dev_active, 0);
558 transport_all_task_dev_remove_state(cmd);
559 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
563 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
564 if (atomic_read(&cmd->transport_lun_active)) {
565 list_del(&cmd->se_lun_node);
566 atomic_set(&cmd->transport_lun_active, 0);
568 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
569 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
572 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
575 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
577 if (!cmd->se_tmr_req)
578 transport_lun_remove_cmd(cmd);
580 if (transport_cmd_check_stop_to_fabric(cmd))
583 transport_remove_cmd_from_queue(cmd);
584 transport_put_cmd(cmd);
588 static void transport_add_cmd_to_queue(struct se_cmd *cmd, int t_state,
591 struct se_device *dev = cmd->se_dev;
592 struct se_queue_obj *qobj = &dev->dev_queue_obj;
596 spin_lock_irqsave(&cmd->t_state_lock, flags);
597 cmd->t_state = t_state;
598 atomic_set(&cmd->t_transport_active, 1);
599 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
602 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
604 /* If the cmd is already on the list, remove it before we add it */
605 if (!list_empty(&cmd->se_queue_node))
606 list_del(&cmd->se_queue_node);
608 atomic_inc(&qobj->queue_cnt);
611 list_add(&cmd->se_queue_node, &qobj->qobj_list);
613 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
614 atomic_set(&cmd->t_transport_queue_active, 1);
615 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
617 wake_up_interruptible(&qobj->thread_wq);
620 static struct se_cmd *
621 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
626 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
627 if (list_empty(&qobj->qobj_list)) {
628 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
631 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
633 atomic_set(&cmd->t_transport_queue_active, 0);
635 list_del_init(&cmd->se_queue_node);
636 atomic_dec(&qobj->queue_cnt);
637 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
642 static void transport_remove_cmd_from_queue(struct se_cmd *cmd)
644 struct se_queue_obj *qobj = &cmd->se_dev->dev_queue_obj;
647 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
648 if (!atomic_read(&cmd->t_transport_queue_active)) {
649 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
652 atomic_set(&cmd->t_transport_queue_active, 0);
653 atomic_dec(&qobj->queue_cnt);
654 list_del_init(&cmd->se_queue_node);
655 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
657 if (atomic_read(&cmd->t_transport_queue_active)) {
658 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
659 cmd->se_tfo->get_task_tag(cmd),
660 atomic_read(&cmd->t_transport_queue_active));
665 * Completion function used by TCM subsystem plugins (such as FILEIO)
666 * for queueing up response from struct se_subsystem_api->do_task()
668 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
670 struct se_task *task = list_entry(cmd->t_task_list.next,
671 struct se_task, t_list);
674 cmd->scsi_status = SAM_STAT_GOOD;
675 task->task_scsi_status = GOOD;
677 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
678 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
679 task->task_se_cmd->transport_error_status =
680 PYX_TRANSPORT_ILLEGAL_REQUEST;
683 transport_complete_task(task, good);
685 EXPORT_SYMBOL(transport_complete_sync_cache);
687 static void target_complete_failure_work(struct work_struct *work)
689 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
691 transport_generic_request_failure(cmd, 1, 1);
694 /* transport_complete_task():
696 * Called from interrupt and non interrupt context depending
697 * on the transport plugin.
699 void transport_complete_task(struct se_task *task, int success)
701 struct se_cmd *cmd = task->task_se_cmd;
702 struct se_device *dev = cmd->se_dev;
705 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
706 cmd->t_task_cdb[0], dev);
709 atomic_inc(&dev->depth_left);
711 spin_lock_irqsave(&cmd->t_state_lock, flags);
712 task->task_flags &= ~TF_ACTIVE;
715 * See if any sense data exists, if so set the TASK_SENSE flag.
716 * Also check for any other post completion work that needs to be
717 * done by the plugins.
719 if (dev && dev->transport->transport_complete) {
720 if (dev->transport->transport_complete(task) != 0) {
721 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
722 task->task_sense = 1;
728 * See if we are waiting for outstanding struct se_task
729 * to complete for an exception condition
731 if (task->task_flags & TF_REQUEST_STOP) {
732 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
733 complete(&task->task_stop_comp);
737 * Decrement the outstanding t_task_cdbs_left count. The last
738 * struct se_task from struct se_cmd will complete itself into the
739 * device queue depending upon int success.
741 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
742 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
746 if (!success || cmd->t_tasks_failed) {
747 if (!task->task_error_status) {
748 task->task_error_status =
749 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
750 cmd->transport_error_status =
751 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
753 INIT_WORK(&cmd->work, target_complete_failure_work);
755 atomic_set(&cmd->t_transport_complete, 1);
756 INIT_WORK(&cmd->work, target_complete_ok_work);
759 cmd->t_state = TRANSPORT_COMPLETE;
760 atomic_set(&cmd->t_transport_active, 1);
761 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
763 queue_work(target_completion_wq, &cmd->work);
765 EXPORT_SYMBOL(transport_complete_task);
768 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
769 * struct se_task list are ready to be added to the active execution list
772 * Called with se_dev_t->execute_task_lock called.
774 static inline int transport_add_task_check_sam_attr(
775 struct se_task *task,
776 struct se_task *task_prev,
777 struct se_device *dev)
780 * No SAM Task attribute emulation enabled, add to tail of
783 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
784 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
788 * HEAD_OF_QUEUE attribute for received CDB, which means
789 * the first task that is associated with a struct se_cmd goes to
790 * head of the struct se_device->execute_task_list, and task_prev
791 * after that for each subsequent task
793 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
794 list_add(&task->t_execute_list,
795 (task_prev != NULL) ?
796 &task_prev->t_execute_list :
797 &dev->execute_task_list);
799 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
800 " in execution queue\n",
801 task->task_se_cmd->t_task_cdb[0]);
805 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
806 * transitioned from Dermant -> Active state, and are added to the end
807 * of the struct se_device->execute_task_list
809 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
813 /* __transport_add_task_to_execute_queue():
815 * Called with se_dev_t->execute_task_lock called.
817 static void __transport_add_task_to_execute_queue(
818 struct se_task *task,
819 struct se_task *task_prev,
820 struct se_device *dev)
824 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
825 atomic_inc(&dev->execute_tasks);
827 if (atomic_read(&task->task_state_active))
830 * Determine if this task needs to go to HEAD_OF_QUEUE for the
831 * state list as well. Running with SAM Task Attribute emulation
832 * will always return head_of_queue == 0 here
835 list_add(&task->t_state_list, (task_prev) ?
836 &task_prev->t_state_list :
837 &dev->state_task_list);
839 list_add_tail(&task->t_state_list, &dev->state_task_list);
841 atomic_set(&task->task_state_active, 1);
843 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
844 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
848 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
850 struct se_device *dev = cmd->se_dev;
851 struct se_task *task;
854 spin_lock_irqsave(&cmd->t_state_lock, flags);
855 list_for_each_entry(task, &cmd->t_task_list, t_list) {
856 if (atomic_read(&task->task_state_active))
859 spin_lock(&dev->execute_task_lock);
860 list_add_tail(&task->t_state_list, &dev->state_task_list);
861 atomic_set(&task->task_state_active, 1);
863 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
864 task->task_se_cmd->se_tfo->get_task_tag(
865 task->task_se_cmd), task, dev);
867 spin_unlock(&dev->execute_task_lock);
869 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
872 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
874 struct se_device *dev = cmd->se_dev;
875 struct se_task *task, *task_prev = NULL;
878 spin_lock_irqsave(&dev->execute_task_lock, flags);
879 list_for_each_entry(task, &cmd->t_task_list, t_list) {
880 if (!list_empty(&task->t_execute_list))
883 * __transport_add_task_to_execute_queue() handles the
884 * SAM Task Attribute emulation if enabled
886 __transport_add_task_to_execute_queue(task, task_prev, dev);
889 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
892 void __transport_remove_task_from_execute_queue(struct se_task *task,
893 struct se_device *dev)
895 list_del_init(&task->t_execute_list);
896 atomic_dec(&dev->execute_tasks);
899 void transport_remove_task_from_execute_queue(
900 struct se_task *task,
901 struct se_device *dev)
905 if (WARN_ON(list_empty(&task->t_execute_list)))
908 spin_lock_irqsave(&dev->execute_task_lock, flags);
909 __transport_remove_task_from_execute_queue(task, dev);
910 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
914 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
917 static void target_qf_do_work(struct work_struct *work)
919 struct se_device *dev = container_of(work, struct se_device,
921 LIST_HEAD(qf_cmd_list);
922 struct se_cmd *cmd, *cmd_tmp;
924 spin_lock_irq(&dev->qf_cmd_lock);
925 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
926 spin_unlock_irq(&dev->qf_cmd_lock);
928 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
929 list_del(&cmd->se_qf_node);
930 atomic_dec(&dev->dev_qf_count);
931 smp_mb__after_atomic_dec();
933 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
934 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
935 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
936 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
939 transport_add_cmd_to_queue(cmd, cmd->t_state, true);
943 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
945 switch (cmd->data_direction) {
948 case DMA_FROM_DEVICE:
952 case DMA_BIDIRECTIONAL:
961 void transport_dump_dev_state(
962 struct se_device *dev,
966 *bl += sprintf(b + *bl, "Status: ");
967 switch (dev->dev_status) {
968 case TRANSPORT_DEVICE_ACTIVATED:
969 *bl += sprintf(b + *bl, "ACTIVATED");
971 case TRANSPORT_DEVICE_DEACTIVATED:
972 *bl += sprintf(b + *bl, "DEACTIVATED");
974 case TRANSPORT_DEVICE_SHUTDOWN:
975 *bl += sprintf(b + *bl, "SHUTDOWN");
977 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
978 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
979 *bl += sprintf(b + *bl, "OFFLINE");
982 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
986 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
987 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
989 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
990 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
991 *bl += sprintf(b + *bl, " ");
994 void transport_dump_vpd_proto_id(
996 unsigned char *p_buf,
999 unsigned char buf[VPD_TMP_BUF_SIZE];
1002 memset(buf, 0, VPD_TMP_BUF_SIZE);
1003 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1005 switch (vpd->protocol_identifier) {
1007 sprintf(buf+len, "Fibre Channel\n");
1010 sprintf(buf+len, "Parallel SCSI\n");
1013 sprintf(buf+len, "SSA\n");
1016 sprintf(buf+len, "IEEE 1394\n");
1019 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1023 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1026 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1029 sprintf(buf+len, "Automation/Drive Interface Transport"
1033 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1036 sprintf(buf+len, "Unknown 0x%02x\n",
1037 vpd->protocol_identifier);
1042 strncpy(p_buf, buf, p_buf_len);
1044 pr_debug("%s", buf);
1048 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1051 * Check if the Protocol Identifier Valid (PIV) bit is set..
1053 * from spc3r23.pdf section 7.5.1
1055 if (page_83[1] & 0x80) {
1056 vpd->protocol_identifier = (page_83[0] & 0xf0);
1057 vpd->protocol_identifier_set = 1;
1058 transport_dump_vpd_proto_id(vpd, NULL, 0);
1061 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1063 int transport_dump_vpd_assoc(
1064 struct t10_vpd *vpd,
1065 unsigned char *p_buf,
1068 unsigned char buf[VPD_TMP_BUF_SIZE];
1072 memset(buf, 0, VPD_TMP_BUF_SIZE);
1073 len = sprintf(buf, "T10 VPD Identifier Association: ");
1075 switch (vpd->association) {
1077 sprintf(buf+len, "addressed logical unit\n");
1080 sprintf(buf+len, "target port\n");
1083 sprintf(buf+len, "SCSI target device\n");
1086 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1092 strncpy(p_buf, buf, p_buf_len);
1094 pr_debug("%s", buf);
1099 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1102 * The VPD identification association..
1104 * from spc3r23.pdf Section 7.6.3.1 Table 297
1106 vpd->association = (page_83[1] & 0x30);
1107 return transport_dump_vpd_assoc(vpd, NULL, 0);
1109 EXPORT_SYMBOL(transport_set_vpd_assoc);
1111 int transport_dump_vpd_ident_type(
1112 struct t10_vpd *vpd,
1113 unsigned char *p_buf,
1116 unsigned char buf[VPD_TMP_BUF_SIZE];
1120 memset(buf, 0, VPD_TMP_BUF_SIZE);
1121 len = sprintf(buf, "T10 VPD Identifier Type: ");
1123 switch (vpd->device_identifier_type) {
1125 sprintf(buf+len, "Vendor specific\n");
1128 sprintf(buf+len, "T10 Vendor ID based\n");
1131 sprintf(buf+len, "EUI-64 based\n");
1134 sprintf(buf+len, "NAA\n");
1137 sprintf(buf+len, "Relative target port identifier\n");
1140 sprintf(buf+len, "SCSI name string\n");
1143 sprintf(buf+len, "Unsupported: 0x%02x\n",
1144 vpd->device_identifier_type);
1150 if (p_buf_len < strlen(buf)+1)
1152 strncpy(p_buf, buf, p_buf_len);
1154 pr_debug("%s", buf);
1160 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1163 * The VPD identifier type..
1165 * from spc3r23.pdf Section 7.6.3.1 Table 298
1167 vpd->device_identifier_type = (page_83[1] & 0x0f);
1168 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1170 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1172 int transport_dump_vpd_ident(
1173 struct t10_vpd *vpd,
1174 unsigned char *p_buf,
1177 unsigned char buf[VPD_TMP_BUF_SIZE];
1180 memset(buf, 0, VPD_TMP_BUF_SIZE);
1182 switch (vpd->device_identifier_code_set) {
1183 case 0x01: /* Binary */
1184 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1185 &vpd->device_identifier[0]);
1187 case 0x02: /* ASCII */
1188 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1189 &vpd->device_identifier[0]);
1191 case 0x03: /* UTF-8 */
1192 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1193 &vpd->device_identifier[0]);
1196 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1197 " 0x%02x", vpd->device_identifier_code_set);
1203 strncpy(p_buf, buf, p_buf_len);
1205 pr_debug("%s", buf);
1211 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1213 static const char hex_str[] = "0123456789abcdef";
1214 int j = 0, i = 4; /* offset to start of the identifer */
1217 * The VPD Code Set (encoding)
1219 * from spc3r23.pdf Section 7.6.3.1 Table 296
1221 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1222 switch (vpd->device_identifier_code_set) {
1223 case 0x01: /* Binary */
1224 vpd->device_identifier[j++] =
1225 hex_str[vpd->device_identifier_type];
1226 while (i < (4 + page_83[3])) {
1227 vpd->device_identifier[j++] =
1228 hex_str[(page_83[i] & 0xf0) >> 4];
1229 vpd->device_identifier[j++] =
1230 hex_str[page_83[i] & 0x0f];
1234 case 0x02: /* ASCII */
1235 case 0x03: /* UTF-8 */
1236 while (i < (4 + page_83[3]))
1237 vpd->device_identifier[j++] = page_83[i++];
1243 return transport_dump_vpd_ident(vpd, NULL, 0);
1245 EXPORT_SYMBOL(transport_set_vpd_ident);
1247 static void core_setup_task_attr_emulation(struct se_device *dev)
1250 * If this device is from Target_Core_Mod/pSCSI, disable the
1251 * SAM Task Attribute emulation.
1253 * This is currently not available in upsream Linux/SCSI Target
1254 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1256 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1257 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1261 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1262 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1263 " device\n", dev->transport->name,
1264 dev->transport->get_device_rev(dev));
1267 static void scsi_dump_inquiry(struct se_device *dev)
1269 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1272 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1274 pr_debug(" Vendor: ");
1275 for (i = 0; i < 8; i++)
1276 if (wwn->vendor[i] >= 0x20)
1277 pr_debug("%c", wwn->vendor[i]);
1281 pr_debug(" Model: ");
1282 for (i = 0; i < 16; i++)
1283 if (wwn->model[i] >= 0x20)
1284 pr_debug("%c", wwn->model[i]);
1288 pr_debug(" Revision: ");
1289 for (i = 0; i < 4; i++)
1290 if (wwn->revision[i] >= 0x20)
1291 pr_debug("%c", wwn->revision[i]);
1297 device_type = dev->transport->get_device_type(dev);
1298 pr_debug(" Type: %s ", scsi_device_type(device_type));
1299 pr_debug(" ANSI SCSI revision: %02x\n",
1300 dev->transport->get_device_rev(dev));
1303 struct se_device *transport_add_device_to_core_hba(
1305 struct se_subsystem_api *transport,
1306 struct se_subsystem_dev *se_dev,
1308 void *transport_dev,
1309 struct se_dev_limits *dev_limits,
1310 const char *inquiry_prod,
1311 const char *inquiry_rev)
1314 struct se_device *dev;
1316 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1318 pr_err("Unable to allocate memory for se_dev_t\n");
1322 transport_init_queue_obj(&dev->dev_queue_obj);
1323 dev->dev_flags = device_flags;
1324 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1325 dev->dev_ptr = transport_dev;
1327 dev->se_sub_dev = se_dev;
1328 dev->transport = transport;
1329 atomic_set(&dev->active_cmds, 0);
1330 INIT_LIST_HEAD(&dev->dev_list);
1331 INIT_LIST_HEAD(&dev->dev_sep_list);
1332 INIT_LIST_HEAD(&dev->dev_tmr_list);
1333 INIT_LIST_HEAD(&dev->execute_task_list);
1334 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1335 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1336 INIT_LIST_HEAD(&dev->state_task_list);
1337 INIT_LIST_HEAD(&dev->qf_cmd_list);
1338 spin_lock_init(&dev->execute_task_lock);
1339 spin_lock_init(&dev->delayed_cmd_lock);
1340 spin_lock_init(&dev->ordered_cmd_lock);
1341 spin_lock_init(&dev->state_task_lock);
1342 spin_lock_init(&dev->dev_alua_lock);
1343 spin_lock_init(&dev->dev_reservation_lock);
1344 spin_lock_init(&dev->dev_status_lock);
1345 spin_lock_init(&dev->dev_status_thr_lock);
1346 spin_lock_init(&dev->se_port_lock);
1347 spin_lock_init(&dev->se_tmr_lock);
1348 spin_lock_init(&dev->qf_cmd_lock);
1350 dev->queue_depth = dev_limits->queue_depth;
1351 atomic_set(&dev->depth_left, dev->queue_depth);
1352 atomic_set(&dev->dev_ordered_id, 0);
1354 se_dev_set_default_attribs(dev, dev_limits);
1356 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1357 dev->creation_time = get_jiffies_64();
1358 spin_lock_init(&dev->stats_lock);
1360 spin_lock(&hba->device_lock);
1361 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1363 spin_unlock(&hba->device_lock);
1365 * Setup the SAM Task Attribute emulation for struct se_device
1367 core_setup_task_attr_emulation(dev);
1369 * Force PR and ALUA passthrough emulation with internal object use.
1371 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1373 * Setup the Reservations infrastructure for struct se_device
1375 core_setup_reservations(dev, force_pt);
1377 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1379 if (core_setup_alua(dev, force_pt) < 0)
1383 * Startup the struct se_device processing thread
1385 dev->process_thread = kthread_run(transport_processing_thread, dev,
1386 "LIO_%s", dev->transport->name);
1387 if (IS_ERR(dev->process_thread)) {
1388 pr_err("Unable to create kthread: LIO_%s\n",
1389 dev->transport->name);
1393 * Setup work_queue for QUEUE_FULL
1395 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1397 * Preload the initial INQUIRY const values if we are doing
1398 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1399 * passthrough because this is being provided by the backend LLD.
1400 * This is required so that transport_get_inquiry() copies these
1401 * originals once back into DEV_T10_WWN(dev) for the virtual device
1404 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1405 if (!inquiry_prod || !inquiry_rev) {
1406 pr_err("All non TCM/pSCSI plugins require"
1407 " INQUIRY consts\n");
1411 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1412 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1413 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1415 scsi_dump_inquiry(dev);
1419 kthread_stop(dev->process_thread);
1421 spin_lock(&hba->device_lock);
1422 list_del(&dev->dev_list);
1424 spin_unlock(&hba->device_lock);
1426 se_release_vpd_for_dev(dev);
1432 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1434 /* transport_generic_prepare_cdb():
1436 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1437 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1438 * The point of this is since we are mapping iSCSI LUNs to
1439 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1440 * devices and HBAs for a loop.
1442 static inline void transport_generic_prepare_cdb(
1446 case READ_10: /* SBC - RDProtect */
1447 case READ_12: /* SBC - RDProtect */
1448 case READ_16: /* SBC - RDProtect */
1449 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1450 case VERIFY: /* SBC - VRProtect */
1451 case VERIFY_16: /* SBC - VRProtect */
1452 case WRITE_VERIFY: /* SBC - VRProtect */
1453 case WRITE_VERIFY_12: /* SBC - VRProtect */
1456 cdb[1] &= 0x1f; /* clear logical unit number */
1461 static struct se_task *
1462 transport_generic_get_task(struct se_cmd *cmd,
1463 enum dma_data_direction data_direction)
1465 struct se_task *task;
1466 struct se_device *dev = cmd->se_dev;
1468 task = dev->transport->alloc_task(cmd->t_task_cdb);
1470 pr_err("Unable to allocate struct se_task\n");
1474 INIT_LIST_HEAD(&task->t_list);
1475 INIT_LIST_HEAD(&task->t_execute_list);
1476 INIT_LIST_HEAD(&task->t_state_list);
1477 init_completion(&task->task_stop_comp);
1478 task->task_se_cmd = cmd;
1479 task->task_data_direction = data_direction;
1484 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1487 * Used by fabric modules containing a local struct se_cmd within their
1488 * fabric dependent per I/O descriptor.
1490 void transport_init_se_cmd(
1492 struct target_core_fabric_ops *tfo,
1493 struct se_session *se_sess,
1497 unsigned char *sense_buffer)
1499 INIT_LIST_HEAD(&cmd->se_lun_node);
1500 INIT_LIST_HEAD(&cmd->se_delayed_node);
1501 INIT_LIST_HEAD(&cmd->se_ordered_node);
1502 INIT_LIST_HEAD(&cmd->se_qf_node);
1503 INIT_LIST_HEAD(&cmd->se_queue_node);
1505 INIT_LIST_HEAD(&cmd->t_task_list);
1506 init_completion(&cmd->transport_lun_fe_stop_comp);
1507 init_completion(&cmd->transport_lun_stop_comp);
1508 init_completion(&cmd->t_transport_stop_comp);
1509 spin_lock_init(&cmd->t_state_lock);
1510 atomic_set(&cmd->transport_dev_active, 1);
1513 cmd->se_sess = se_sess;
1514 cmd->data_length = data_length;
1515 cmd->data_direction = data_direction;
1516 cmd->sam_task_attr = task_attr;
1517 cmd->sense_buffer = sense_buffer;
1519 EXPORT_SYMBOL(transport_init_se_cmd);
1521 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1524 * Check if SAM Task Attribute emulation is enabled for this
1525 * struct se_device storage object
1527 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1530 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1531 pr_debug("SAM Task Attribute ACA"
1532 " emulation is not supported\n");
1536 * Used to determine when ORDERED commands should go from
1537 * Dormant to Active status.
1539 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1540 smp_mb__after_atomic_inc();
1541 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1542 cmd->se_ordered_id, cmd->sam_task_attr,
1543 cmd->se_dev->transport->name);
1547 /* transport_generic_allocate_tasks():
1549 * Called from fabric RX Thread.
1551 int transport_generic_allocate_tasks(
1557 transport_generic_prepare_cdb(cdb);
1559 * Ensure that the received CDB is less than the max (252 + 8) bytes
1560 * for VARIABLE_LENGTH_CMD
1562 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1563 pr_err("Received SCSI CDB with command_size: %d that"
1564 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1565 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1569 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1570 * allocate the additional extended CDB buffer now.. Otherwise
1571 * setup the pointer from __t_task_cdb to t_task_cdb.
1573 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1574 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1576 if (!cmd->t_task_cdb) {
1577 pr_err("Unable to allocate cmd->t_task_cdb"
1578 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1579 scsi_command_size(cdb),
1580 (unsigned long)sizeof(cmd->__t_task_cdb));
1584 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1586 * Copy the original CDB into cmd->
1588 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1590 * Setup the received CDB based on SCSI defined opcodes and
1591 * perform unit attention, persistent reservations and ALUA
1592 * checks for virtual device backends. The cmd->t_task_cdb
1593 * pointer is expected to be setup before we reach this point.
1595 ret = transport_generic_cmd_sequencer(cmd, cdb);
1599 * Check for SAM Task Attribute Emulation
1601 if (transport_check_alloc_task_attr(cmd) < 0) {
1602 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1603 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1606 spin_lock(&cmd->se_lun->lun_sep_lock);
1607 if (cmd->se_lun->lun_sep)
1608 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1609 spin_unlock(&cmd->se_lun->lun_sep_lock);
1612 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1615 * Used by fabric module frontends to queue tasks directly.
1616 * Many only be used from process context only
1618 int transport_handle_cdb_direct(
1625 pr_err("cmd->se_lun is NULL\n");
1628 if (in_interrupt()) {
1630 pr_err("transport_generic_handle_cdb cannot be called"
1631 " from interrupt context\n");
1635 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1636 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1637 * in existing usage to ensure that outstanding descriptors are handled
1638 * correctly during shutdown via transport_wait_for_tasks()
1640 * Also, we don't take cmd->t_state_lock here as we only expect
1641 * this to be called for initial descriptor submission.
1643 cmd->t_state = TRANSPORT_NEW_CMD;
1644 atomic_set(&cmd->t_transport_active, 1);
1646 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1647 * so follow TRANSPORT_NEW_CMD processing thread context usage
1648 * and call transport_generic_request_failure() if necessary..
1650 ret = transport_generic_new_cmd(cmd);
1652 cmd->transport_error_status = ret;
1653 transport_generic_request_failure(cmd, 0,
1654 (cmd->data_direction != DMA_TO_DEVICE));
1658 EXPORT_SYMBOL(transport_handle_cdb_direct);
1661 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1662 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1663 * complete setup in TCM process context w/ TFO->new_cmd_map().
1665 int transport_generic_handle_cdb_map(
1670 pr_err("cmd->se_lun is NULL\n");
1674 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP, false);
1677 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1679 /* transport_generic_handle_data():
1683 int transport_generic_handle_data(
1687 * For the software fabric case, then we assume the nexus is being
1688 * failed/shutdown when signals are pending from the kthread context
1689 * caller, so we return a failure. For the HW target mode case running
1690 * in interrupt code, the signal_pending() check is skipped.
1692 if (!in_interrupt() && signal_pending(current))
1695 * If the received CDB has aleady been ABORTED by the generic
1696 * target engine, we now call transport_check_aborted_status()
1697 * to queue any delated TASK_ABORTED status for the received CDB to the
1698 * fabric module as we are expecting no further incoming DATA OUT
1699 * sequences at this point.
1701 if (transport_check_aborted_status(cmd, 1) != 0)
1704 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE, false);
1707 EXPORT_SYMBOL(transport_generic_handle_data);
1709 /* transport_generic_handle_tmr():
1713 int transport_generic_handle_tmr(
1716 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR, false);
1719 EXPORT_SYMBOL(transport_generic_handle_tmr);
1721 void transport_generic_free_cmd_intr(
1724 transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR, false);
1726 EXPORT_SYMBOL(transport_generic_free_cmd_intr);
1729 * If the task is active, request it to be stopped and sleep until it
1732 bool target_stop_task(struct se_task *task, unsigned long *flags)
1734 struct se_cmd *cmd = task->task_se_cmd;
1735 bool was_active = false;
1737 if (task->task_flags & TF_ACTIVE) {
1738 task->task_flags |= TF_REQUEST_STOP;
1739 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1741 pr_debug("Task %p waiting to complete\n", task);
1742 wait_for_completion(&task->task_stop_comp);
1743 pr_debug("Task %p stopped successfully\n", task);
1745 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1746 atomic_dec(&cmd->t_task_cdbs_left);
1747 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1754 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1756 struct se_task *task, *task_tmp;
1757 unsigned long flags;
1760 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1761 cmd->se_tfo->get_task_tag(cmd));
1764 * No tasks remain in the execution queue
1766 spin_lock_irqsave(&cmd->t_state_lock, flags);
1767 list_for_each_entry_safe(task, task_tmp,
1768 &cmd->t_task_list, t_list) {
1769 pr_debug("Processing task %p\n", task);
1771 * If the struct se_task has not been sent and is not active,
1772 * remove the struct se_task from the execution queue.
1774 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
1775 spin_unlock_irqrestore(&cmd->t_state_lock,
1777 transport_remove_task_from_execute_queue(task,
1780 pr_debug("Task %p removed from execute queue\n", task);
1781 spin_lock_irqsave(&cmd->t_state_lock, flags);
1785 if (!target_stop_task(task, &flags)) {
1786 pr_debug("Task %p - did nothing\n", task);
1790 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1796 * Handle SAM-esque emulation for generic transport request failures.
1798 static void transport_generic_request_failure(
1805 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1806 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1807 cmd->t_task_cdb[0]);
1808 pr_debug("-----[ i_state: %d t_state: %d transport_error_status: %d\n",
1809 cmd->se_tfo->get_cmd_state(cmd),
1811 cmd->transport_error_status);
1812 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1813 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1814 " t_transport_active: %d t_transport_stop: %d"
1815 " t_transport_sent: %d\n", cmd->t_task_list_num,
1816 atomic_read(&cmd->t_task_cdbs_left),
1817 atomic_read(&cmd->t_task_cdbs_sent),
1818 atomic_read(&cmd->t_task_cdbs_ex_left),
1819 atomic_read(&cmd->t_transport_active),
1820 atomic_read(&cmd->t_transport_stop),
1821 atomic_read(&cmd->t_transport_sent));
1824 * For SAM Task Attribute emulation for failed struct se_cmd
1826 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1827 transport_complete_task_attr(cmd);
1830 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
1833 switch (cmd->transport_error_status) {
1834 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
1835 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1837 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
1838 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
1840 case PYX_TRANSPORT_INVALID_CDB_FIELD:
1841 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1843 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
1844 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1846 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
1848 transport_new_cmd_failure(cmd);
1850 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
1851 * we force this session to fall back to session
1854 cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
1855 cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
1858 case PYX_TRANSPORT_LU_COMM_FAILURE:
1859 case PYX_TRANSPORT_ILLEGAL_REQUEST:
1860 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1862 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
1863 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
1865 case PYX_TRANSPORT_WRITE_PROTECTED:
1866 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
1868 case PYX_TRANSPORT_RESERVATION_CONFLICT:
1870 * No SENSE Data payload for this case, set SCSI Status
1871 * and queue the response to $FABRIC_MOD.
1873 * Uses linux/include/scsi/scsi.h SAM status codes defs
1875 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1877 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1878 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1881 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1884 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1885 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1886 cmd->orig_fe_lun, 0x2C,
1887 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1889 ret = cmd->se_tfo->queue_status(cmd);
1890 if (ret == -EAGAIN || ret == -ENOMEM)
1893 case PYX_TRANSPORT_USE_SENSE_REASON:
1895 * struct se_cmd->scsi_sense_reason already set
1899 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1901 cmd->transport_error_status);
1902 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1906 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1907 * make the call to transport_send_check_condition_and_sense()
1908 * directly. Otherwise expect the fabric to make the call to
1909 * transport_send_check_condition_and_sense() after handling
1910 * possible unsoliticied write data payloads.
1912 if (!sc && !cmd->se_tfo->new_cmd_map)
1913 transport_new_cmd_failure(cmd);
1915 ret = transport_send_check_condition_and_sense(cmd,
1916 cmd->scsi_sense_reason, 0);
1917 if (ret == -EAGAIN || ret == -ENOMEM)
1922 transport_lun_remove_cmd(cmd);
1923 if (!transport_cmd_check_stop_to_fabric(cmd))
1928 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1929 transport_handle_queue_full(cmd, cmd->se_dev);
1932 static inline u32 transport_lba_21(unsigned char *cdb)
1934 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
1937 static inline u32 transport_lba_32(unsigned char *cdb)
1939 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1942 static inline unsigned long long transport_lba_64(unsigned char *cdb)
1944 unsigned int __v1, __v2;
1946 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
1947 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
1949 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1953 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
1955 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
1957 unsigned int __v1, __v2;
1959 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
1960 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
1962 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
1965 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
1967 unsigned long flags;
1969 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
1970 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1971 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
1974 static inline int transport_tcq_window_closed(struct se_device *dev)
1976 if (dev->dev_tcq_window_closed++ <
1977 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
1978 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
1980 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
1982 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
1987 * Called from Fabric Module context from transport_execute_tasks()
1989 * The return of this function determins if the tasks from struct se_cmd
1990 * get added to the execution queue in transport_execute_tasks(),
1991 * or are added to the delayed or ordered lists here.
1993 static inline int transport_execute_task_attr(struct se_cmd *cmd)
1995 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1998 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1999 * to allow the passed struct se_cmd list of tasks to the front of the list.
2001 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
2002 atomic_inc(&cmd->se_dev->dev_hoq_count);
2003 smp_mb__after_atomic_inc();
2004 pr_debug("Added HEAD_OF_QUEUE for CDB:"
2005 " 0x%02x, se_ordered_id: %u\n",
2007 cmd->se_ordered_id);
2009 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
2010 spin_lock(&cmd->se_dev->ordered_cmd_lock);
2011 list_add_tail(&cmd->se_ordered_node,
2012 &cmd->se_dev->ordered_cmd_list);
2013 spin_unlock(&cmd->se_dev->ordered_cmd_lock);
2015 atomic_inc(&cmd->se_dev->dev_ordered_sync);
2016 smp_mb__after_atomic_inc();
2018 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
2019 " list, se_ordered_id: %u\n",
2021 cmd->se_ordered_id);
2023 * Add ORDERED command to tail of execution queue if
2024 * no other older commands exist that need to be
2027 if (!atomic_read(&cmd->se_dev->simple_cmds))
2031 * For SIMPLE and UNTAGGED Task Attribute commands
2033 atomic_inc(&cmd->se_dev->simple_cmds);
2034 smp_mb__after_atomic_inc();
2037 * Otherwise if one or more outstanding ORDERED task attribute exist,
2038 * add the dormant task(s) built for the passed struct se_cmd to the
2039 * execution queue and become in Active state for this struct se_device.
2041 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2043 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2044 * will be drained upon completion of HEAD_OF_QUEUE task.
2046 spin_lock(&cmd->se_dev->delayed_cmd_lock);
2047 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2048 list_add_tail(&cmd->se_delayed_node,
2049 &cmd->se_dev->delayed_cmd_list);
2050 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2052 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2053 " delayed CMD list, se_ordered_id: %u\n",
2054 cmd->t_task_cdb[0], cmd->sam_task_attr,
2055 cmd->se_ordered_id);
2057 * Return zero to let transport_execute_tasks() know
2058 * not to add the delayed tasks to the execution list.
2063 * Otherwise, no ORDERED task attributes exist..
2069 * Called from fabric module context in transport_generic_new_cmd() and
2070 * transport_generic_process_write()
2072 static int transport_execute_tasks(struct se_cmd *cmd)
2076 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2077 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2078 transport_generic_request_failure(cmd, 0, 1);
2083 * Call transport_cmd_check_stop() to see if a fabric exception
2084 * has occurred that prevents execution.
2086 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2088 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2089 * attribute for the tasks of the received struct se_cmd CDB
2091 add_tasks = transport_execute_task_attr(cmd);
2095 * This calls transport_add_tasks_from_cmd() to handle
2096 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2097 * (if enabled) in __transport_add_task_to_execute_queue() and
2098 * transport_add_task_check_sam_attr().
2100 transport_add_tasks_from_cmd(cmd);
2103 * Kick the execution queue for the cmd associated struct se_device
2107 __transport_execute_tasks(cmd->se_dev);
2112 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2113 * from struct se_device->execute_task_list and
2115 * Called from transport_processing_thread()
2117 static int __transport_execute_tasks(struct se_device *dev)
2120 struct se_cmd *cmd = NULL;
2121 struct se_task *task = NULL;
2122 unsigned long flags;
2125 * Check if there is enough room in the device and HBA queue to send
2126 * struct se_tasks to the selected transport.
2129 if (!atomic_read(&dev->depth_left))
2130 return transport_tcq_window_closed(dev);
2132 dev->dev_tcq_window_closed = 0;
2134 spin_lock_irq(&dev->execute_task_lock);
2135 if (list_empty(&dev->execute_task_list)) {
2136 spin_unlock_irq(&dev->execute_task_lock);
2139 task = list_first_entry(&dev->execute_task_list,
2140 struct se_task, t_execute_list);
2141 __transport_remove_task_from_execute_queue(task, dev);
2142 spin_unlock_irq(&dev->execute_task_lock);
2144 atomic_dec(&dev->depth_left);
2146 cmd = task->task_se_cmd;
2148 spin_lock_irqsave(&cmd->t_state_lock, flags);
2149 task->task_flags |= (TF_ACTIVE | TF_SENT);
2150 atomic_inc(&cmd->t_task_cdbs_sent);
2152 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2153 cmd->t_task_list_num)
2154 atomic_set(&cmd->t_transport_sent, 1);
2156 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2158 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2159 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2160 * struct se_subsystem_api->do_task() caller below.
2162 if (cmd->transport_emulate_cdb) {
2163 error = cmd->transport_emulate_cdb(cmd);
2165 cmd->transport_error_status = error;
2166 spin_lock_irqsave(&cmd->t_state_lock, flags);
2167 task->task_flags &= ~TF_ACTIVE;
2168 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2169 atomic_set(&cmd->t_transport_sent, 0);
2170 transport_stop_tasks_for_cmd(cmd);
2171 atomic_inc(&dev->depth_left);
2172 transport_generic_request_failure(cmd, 0, 1);
2176 * Handle the successful completion for transport_emulate_cdb()
2177 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2178 * Otherwise the caller is expected to complete the task with
2181 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2182 cmd->scsi_status = SAM_STAT_GOOD;
2183 task->task_scsi_status = GOOD;
2184 transport_complete_task(task, 1);
2188 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2189 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2190 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2191 * LUN emulation code.
2193 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2194 * call ->do_task() directly and let the underlying TCM subsystem plugin
2195 * code handle the CDB emulation.
2197 if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2198 (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2199 error = transport_emulate_control_cdb(task);
2201 error = dev->transport->do_task(task);
2204 cmd->transport_error_status = error;
2205 spin_lock_irqsave(&cmd->t_state_lock, flags);
2206 task->task_flags &= ~TF_ACTIVE;
2207 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2208 atomic_set(&cmd->t_transport_sent, 0);
2209 transport_stop_tasks_for_cmd(cmd);
2210 atomic_inc(&dev->depth_left);
2211 transport_generic_request_failure(cmd, 0, 1);
2220 void transport_new_cmd_failure(struct se_cmd *se_cmd)
2222 unsigned long flags;
2224 * Any unsolicited data will get dumped for failed command inside of
2227 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2228 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2229 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2230 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2233 static inline u32 transport_get_sectors_6(
2238 struct se_device *dev = cmd->se_dev;
2241 * Assume TYPE_DISK for non struct se_device objects.
2242 * Use 8-bit sector value.
2248 * Use 24-bit allocation length for TYPE_TAPE.
2250 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2251 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2254 * Everything else assume TYPE_DISK Sector CDB location.
2255 * Use 8-bit sector value.
2261 static inline u32 transport_get_sectors_10(
2266 struct se_device *dev = cmd->se_dev;
2269 * Assume TYPE_DISK for non struct se_device objects.
2270 * Use 16-bit sector value.
2276 * XXX_10 is not defined in SSC, throw an exception
2278 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2284 * Everything else assume TYPE_DISK Sector CDB location.
2285 * Use 16-bit sector value.
2288 return (u32)(cdb[7] << 8) + cdb[8];
2291 static inline u32 transport_get_sectors_12(
2296 struct se_device *dev = cmd->se_dev;
2299 * Assume TYPE_DISK for non struct se_device objects.
2300 * Use 32-bit sector value.
2306 * XXX_12 is not defined in SSC, throw an exception
2308 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2314 * Everything else assume TYPE_DISK Sector CDB location.
2315 * Use 32-bit sector value.
2318 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2321 static inline u32 transport_get_sectors_16(
2326 struct se_device *dev = cmd->se_dev;
2329 * Assume TYPE_DISK for non struct se_device objects.
2330 * Use 32-bit sector value.
2336 * Use 24-bit allocation length for TYPE_TAPE.
2338 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2339 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2342 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2343 (cdb[12] << 8) + cdb[13];
2347 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2349 static inline u32 transport_get_sectors_32(
2355 * Assume TYPE_DISK for non struct se_device objects.
2356 * Use 32-bit sector value.
2358 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2359 (cdb[30] << 8) + cdb[31];
2363 static inline u32 transport_get_size(
2368 struct se_device *dev = cmd->se_dev;
2370 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2371 if (cdb[1] & 1) { /* sectors */
2372 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2377 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2378 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2379 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2380 dev->transport->name);
2382 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2385 static void transport_xor_callback(struct se_cmd *cmd)
2387 unsigned char *buf, *addr;
2388 struct scatterlist *sg;
2389 unsigned int offset;
2393 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2395 * 1) read the specified logical block(s);
2396 * 2) transfer logical blocks from the data-out buffer;
2397 * 3) XOR the logical blocks transferred from the data-out buffer with
2398 * the logical blocks read, storing the resulting XOR data in a buffer;
2399 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2400 * blocks transferred from the data-out buffer; and
2401 * 5) transfer the resulting XOR data to the data-in buffer.
2403 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2405 pr_err("Unable to allocate xor_callback buf\n");
2409 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2410 * into the locally allocated *buf
2412 sg_copy_to_buffer(cmd->t_data_sg,
2418 * Now perform the XOR against the BIDI read memory located at
2419 * cmd->t_mem_bidi_list
2423 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2424 addr = kmap_atomic(sg_page(sg), KM_USER0);
2428 for (i = 0; i < sg->length; i++)
2429 *(addr + sg->offset + i) ^= *(buf + offset + i);
2431 offset += sg->length;
2432 kunmap_atomic(addr, KM_USER0);
2440 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2442 static int transport_get_sense_data(struct se_cmd *cmd)
2444 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2445 struct se_device *dev = cmd->se_dev;
2446 struct se_task *task = NULL, *task_tmp;
2447 unsigned long flags;
2450 WARN_ON(!cmd->se_lun);
2455 spin_lock_irqsave(&cmd->t_state_lock, flags);
2456 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2457 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2461 list_for_each_entry_safe(task, task_tmp,
2462 &cmd->t_task_list, t_list) {
2463 if (!task->task_sense)
2466 if (!dev->transport->get_sense_buffer) {
2467 pr_err("dev->transport->get_sense_buffer"
2472 sense_buffer = dev->transport->get_sense_buffer(task);
2473 if (!sense_buffer) {
2474 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
2475 " sense buffer for task with sense\n",
2476 cmd->se_tfo->get_task_tag(cmd), task);
2479 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2481 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2482 TRANSPORT_SENSE_BUFFER);
2484 memcpy(&buffer[offset], sense_buffer,
2485 TRANSPORT_SENSE_BUFFER);
2486 cmd->scsi_status = task->task_scsi_status;
2487 /* Automatically padded */
2488 cmd->scsi_sense_length =
2489 (TRANSPORT_SENSE_BUFFER + offset);
2491 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2493 dev->se_hba->hba_id, dev->transport->name,
2497 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2503 transport_handle_reservation_conflict(struct se_cmd *cmd)
2505 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2506 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2507 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2509 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2510 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2513 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2516 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2517 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2518 cmd->orig_fe_lun, 0x2C,
2519 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2523 static inline long long transport_dev_end_lba(struct se_device *dev)
2525 return dev->transport->get_blocks(dev) + 1;
2528 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2530 struct se_device *dev = cmd->se_dev;
2533 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2536 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2538 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2539 pr_err("LBA: %llu Sectors: %u exceeds"
2540 " transport_dev_end_lba(): %llu\n",
2541 cmd->t_task_lba, sectors,
2542 transport_dev_end_lba(dev));
2549 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2552 * Determine if the received WRITE_SAME is used to for direct
2553 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2554 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2555 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2557 int passthrough = (dev->transport->transport_type ==
2558 TRANSPORT_PLUGIN_PHBA_PDEV);
2561 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2562 pr_err("WRITE_SAME PBDATA and LBDATA"
2563 " bits not supported for Block Discard"
2568 * Currently for the emulated case we only accept
2569 * tpws with the UNMAP=1 bit set.
2571 if (!(flags[0] & 0x08)) {
2572 pr_err("WRITE_SAME w/o UNMAP bit not"
2573 " supported for Block Discard Emulation\n");
2581 /* transport_generic_cmd_sequencer():
2583 * Generic Command Sequencer that should work for most DAS transport
2586 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2589 * FIXME: Need to support other SCSI OPCODES where as well.
2591 static int transport_generic_cmd_sequencer(
2595 struct se_device *dev = cmd->se_dev;
2596 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2597 int ret = 0, sector_ret = 0, passthrough;
2598 u32 sectors = 0, size = 0, pr_reg_type = 0;
2602 * Check for an existing UNIT ATTENTION condition
2604 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2605 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2606 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2610 * Check status of Asymmetric Logical Unit Assignment port
2612 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2615 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2616 * The ALUA additional sense code qualifier (ASCQ) is determined
2617 * by the ALUA primary or secondary access state..
2621 pr_debug("[%s]: ALUA TG Port not available,"
2622 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2623 cmd->se_tfo->get_fabric_name(), alua_ascq);
2625 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2626 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2627 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2630 goto out_invalid_cdb_field;
2633 * Check status for SPC-3 Persistent Reservations
2635 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2636 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2637 cmd, cdb, pr_reg_type) != 0)
2638 return transport_handle_reservation_conflict(cmd);
2640 * This means the CDB is allowed for the SCSI Initiator port
2641 * when said port is *NOT* holding the legacy SPC-2 or
2642 * SPC-3 Persistent Reservation.
2648 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2650 goto out_unsupported_cdb;
2651 size = transport_get_size(sectors, cdb, cmd);
2652 cmd->t_task_lba = transport_lba_21(cdb);
2653 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2656 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2658 goto out_unsupported_cdb;
2659 size = transport_get_size(sectors, cdb, cmd);
2660 cmd->t_task_lba = transport_lba_32(cdb);
2661 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2664 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2666 goto out_unsupported_cdb;
2667 size = transport_get_size(sectors, cdb, cmd);
2668 cmd->t_task_lba = transport_lba_32(cdb);
2669 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2672 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2674 goto out_unsupported_cdb;
2675 size = transport_get_size(sectors, cdb, cmd);
2676 cmd->t_task_lba = transport_lba_64(cdb);
2677 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2680 sectors = transport_get_sectors_6(cdb, cmd, §or_ret);
2682 goto out_unsupported_cdb;
2683 size = transport_get_size(sectors, cdb, cmd);
2684 cmd->t_task_lba = transport_lba_21(cdb);
2685 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2688 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2690 goto out_unsupported_cdb;
2691 size = transport_get_size(sectors, cdb, cmd);
2692 cmd->t_task_lba = transport_lba_32(cdb);
2693 cmd->t_tasks_fua = (cdb[1] & 0x8);
2694 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2697 sectors = transport_get_sectors_12(cdb, cmd, §or_ret);
2699 goto out_unsupported_cdb;
2700 size = transport_get_size(sectors, cdb, cmd);
2701 cmd->t_task_lba = transport_lba_32(cdb);
2702 cmd->t_tasks_fua = (cdb[1] & 0x8);
2703 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2706 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2708 goto out_unsupported_cdb;
2709 size = transport_get_size(sectors, cdb, cmd);
2710 cmd->t_task_lba = transport_lba_64(cdb);
2711 cmd->t_tasks_fua = (cdb[1] & 0x8);
2712 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2714 case XDWRITEREAD_10:
2715 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2716 !(cmd->t_tasks_bidi))
2717 goto out_invalid_cdb_field;
2718 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2720 goto out_unsupported_cdb;
2721 size = transport_get_size(sectors, cdb, cmd);
2722 cmd->t_task_lba = transport_lba_32(cdb);
2723 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2725 if (dev->transport->transport_type ==
2726 TRANSPORT_PLUGIN_PHBA_PDEV)
2727 goto out_unsupported_cdb;
2729 * Setup BIDI XOR callback to be run after I/O completion.
2731 cmd->transport_complete_callback = &transport_xor_callback;
2732 cmd->t_tasks_fua = (cdb[1] & 0x8);
2734 case VARIABLE_LENGTH_CMD:
2735 service_action = get_unaligned_be16(&cdb[8]);
2737 * Determine if this is TCM/PSCSI device and we should disable
2738 * internal emulation for this CDB.
2740 passthrough = (dev->transport->transport_type ==
2741 TRANSPORT_PLUGIN_PHBA_PDEV);
2743 switch (service_action) {
2744 case XDWRITEREAD_32:
2745 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2747 goto out_unsupported_cdb;
2748 size = transport_get_size(sectors, cdb, cmd);
2750 * Use WRITE_32 and READ_32 opcodes for the emulated
2751 * XDWRITE_READ_32 logic.
2753 cmd->t_task_lba = transport_lba_64_ext(cdb);
2754 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2757 goto out_unsupported_cdb;
2759 * Setup BIDI XOR callback to be run during after I/O
2762 cmd->transport_complete_callback = &transport_xor_callback;
2763 cmd->t_tasks_fua = (cdb[10] & 0x8);
2766 sectors = transport_get_sectors_32(cdb, cmd, §or_ret);
2768 goto out_unsupported_cdb;
2771 size = transport_get_size(1, cdb, cmd);
2773 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2775 goto out_invalid_cdb_field;
2778 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2779 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2781 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2782 goto out_invalid_cdb_field;
2786 pr_err("VARIABLE_LENGTH_CMD service action"
2787 " 0x%04x not supported\n", service_action);
2788 goto out_unsupported_cdb;
2791 case MAINTENANCE_IN:
2792 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2793 /* MAINTENANCE_IN from SCC-2 */
2795 * Check for emulated MI_REPORT_TARGET_PGS.
2797 if (cdb[1] == MI_REPORT_TARGET_PGS) {
2798 cmd->transport_emulate_cdb =
2799 (su_dev->t10_alua.alua_type ==
2800 SPC3_ALUA_EMULATED) ?
2801 core_emulate_report_target_port_groups :
2804 size = (cdb[6] << 24) | (cdb[7] << 16) |
2805 (cdb[8] << 8) | cdb[9];
2807 /* GPCMD_SEND_KEY from multi media commands */
2808 size = (cdb[8] << 8) + cdb[9];
2810 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2814 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2816 case MODE_SELECT_10:
2817 size = (cdb[7] << 8) + cdb[8];
2818 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2822 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2825 case GPCMD_READ_BUFFER_CAPACITY:
2826 case GPCMD_SEND_OPC:
2829 size = (cdb[7] << 8) + cdb[8];
2830 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2832 case READ_BLOCK_LIMITS:
2833 size = READ_BLOCK_LEN;
2834 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2836 case GPCMD_GET_CONFIGURATION:
2837 case GPCMD_READ_FORMAT_CAPACITIES:
2838 case GPCMD_READ_DISC_INFO:
2839 case GPCMD_READ_TRACK_RZONE_INFO:
2840 size = (cdb[7] << 8) + cdb[8];
2841 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2843 case PERSISTENT_RESERVE_IN:
2844 case PERSISTENT_RESERVE_OUT:
2845 cmd->transport_emulate_cdb =
2846 (su_dev->t10_pr.res_type ==
2847 SPC3_PERSISTENT_RESERVATIONS) ?
2848 core_scsi3_emulate_pr : NULL;
2849 size = (cdb[7] << 8) + cdb[8];
2850 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2852 case GPCMD_MECHANISM_STATUS:
2853 case GPCMD_READ_DVD_STRUCTURE:
2854 size = (cdb[8] << 8) + cdb[9];
2855 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2858 size = READ_POSITION_LEN;
2859 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2861 case MAINTENANCE_OUT:
2862 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
2863 /* MAINTENANCE_OUT from SCC-2
2865 * Check for emulated MO_SET_TARGET_PGS.
2867 if (cdb[1] == MO_SET_TARGET_PGS) {
2868 cmd->transport_emulate_cdb =
2869 (su_dev->t10_alua.alua_type ==
2870 SPC3_ALUA_EMULATED) ?
2871 core_emulate_set_target_port_groups :
2875 size = (cdb[6] << 24) | (cdb[7] << 16) |
2876 (cdb[8] << 8) | cdb[9];
2878 /* GPCMD_REPORT_KEY from multi media commands */
2879 size = (cdb[8] << 8) + cdb[9];
2881 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2884 size = (cdb[3] << 8) + cdb[4];
2886 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
2887 * See spc4r17 section 5.3
2889 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
2890 cmd->sam_task_attr = MSG_HEAD_TAG;
2891 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2894 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2895 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2898 size = READ_CAP_LEN;
2899 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2901 case READ_MEDIA_SERIAL_NUMBER:
2902 case SECURITY_PROTOCOL_IN:
2903 case SECURITY_PROTOCOL_OUT:
2904 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2905 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2907 case SERVICE_ACTION_IN:
2908 case ACCESS_CONTROL_IN:
2909 case ACCESS_CONTROL_OUT:
2911 case READ_ATTRIBUTE:
2912 case RECEIVE_COPY_RESULTS:
2913 case WRITE_ATTRIBUTE:
2914 size = (cdb[10] << 24) | (cdb[11] << 16) |
2915 (cdb[12] << 8) | cdb[13];
2916 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2918 case RECEIVE_DIAGNOSTIC:
2919 case SEND_DIAGNOSTIC:
2920 size = (cdb[3] << 8) | cdb[4];
2921 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2923 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
2926 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2927 size = (2336 * sectors);
2928 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2933 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2937 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2939 case READ_ELEMENT_STATUS:
2940 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
2941 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2944 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
2945 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2950 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
2951 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2953 if (cdb[0] == RESERVE_10)
2954 size = (cdb[7] << 8) | cdb[8];
2956 size = cmd->data_length;
2959 * Setup the legacy emulated handler for SPC-2 and
2960 * >= SPC-3 compatible reservation handling (CRH=1)
2961 * Otherwise, we assume the underlying SCSI logic is
2962 * is running in SPC_PASSTHROUGH, and wants reservations
2963 * emulation disabled.
2965 cmd->transport_emulate_cdb =
2966 (su_dev->t10_pr.res_type !=
2968 core_scsi2_emulate_crh : NULL;
2969 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2974 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
2975 * Assume the passthrough or $FABRIC_MOD will tell us about it.
2977 if (cdb[0] == RELEASE_10)
2978 size = (cdb[7] << 8) | cdb[8];
2980 size = cmd->data_length;
2982 cmd->transport_emulate_cdb =
2983 (su_dev->t10_pr.res_type !=
2985 core_scsi2_emulate_crh : NULL;
2986 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
2988 case SYNCHRONIZE_CACHE:
2989 case 0x91: /* SYNCHRONIZE_CACHE_16: */
2991 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
2993 if (cdb[0] == SYNCHRONIZE_CACHE) {
2994 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
2995 cmd->t_task_lba = transport_lba_32(cdb);
2997 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
2998 cmd->t_task_lba = transport_lba_64(cdb);
3001 goto out_unsupported_cdb;
3003 size = transport_get_size(sectors, cdb, cmd);
3004 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3007 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3009 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3012 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3013 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3015 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3017 * Check to ensure that LBA + Range does not exceed past end of
3018 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
3020 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3021 if (transport_cmd_get_valid_sectors(cmd) < 0)
3022 goto out_invalid_cdb_field;
3026 size = get_unaligned_be16(&cdb[7]);
3027 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3030 sectors = transport_get_sectors_16(cdb, cmd, §or_ret);
3032 goto out_unsupported_cdb;
3035 size = transport_get_size(1, cdb, cmd);
3037 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3038 goto out_invalid_cdb_field;
3041 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
3042 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3044 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3045 goto out_invalid_cdb_field;
3048 sectors = transport_get_sectors_10(cdb, cmd, §or_ret);
3050 goto out_unsupported_cdb;
3053 size = transport_get_size(1, cdb, cmd);
3055 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3056 goto out_invalid_cdb_field;
3059 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3060 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3062 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3063 * of byte 1 bit 3 UNMAP instead of original reserved field
3065 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3066 goto out_invalid_cdb_field;
3068 case ALLOW_MEDIUM_REMOVAL:
3069 case GPCMD_CLOSE_TRACK:
3071 case INITIALIZE_ELEMENT_STATUS:
3072 case GPCMD_LOAD_UNLOAD:
3075 case GPCMD_SET_SPEED:
3078 case TEST_UNIT_READY:
3080 case WRITE_FILEMARKS:
3082 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3085 cmd->transport_emulate_cdb =
3086 transport_core_report_lun_response;
3087 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3089 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3090 * See spc4r17 section 5.3
3092 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3093 cmd->sam_task_attr = MSG_HEAD_TAG;
3094 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3097 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3098 " 0x%02x, sending CHECK_CONDITION.\n",
3099 cmd->se_tfo->get_fabric_name(), cdb[0]);
3100 goto out_unsupported_cdb;
3103 if (size != cmd->data_length) {
3104 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3105 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3106 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3107 cmd->data_length, size, cdb[0]);
3109 cmd->cmd_spdtl = size;
3111 if (cmd->data_direction == DMA_TO_DEVICE) {
3112 pr_err("Rejecting underflow/overflow"
3114 goto out_invalid_cdb_field;
3117 * Reject READ_* or WRITE_* with overflow/underflow for
3118 * type SCF_SCSI_DATA_SG_IO_CDB.
3120 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3121 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3122 " CDB on non 512-byte sector setup subsystem"
3123 " plugin: %s\n", dev->transport->name);
3124 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3125 goto out_invalid_cdb_field;
3128 if (size > cmd->data_length) {
3129 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3130 cmd->residual_count = (size - cmd->data_length);
3132 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3133 cmd->residual_count = (cmd->data_length - size);
3135 cmd->data_length = size;
3138 /* Let's limit control cdbs to a page, for simplicity's sake. */
3139 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3141 goto out_invalid_cdb_field;
3143 transport_set_supported_SAM_opcode(cmd);
3146 out_unsupported_cdb:
3147 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3148 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3150 out_invalid_cdb_field:
3151 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3152 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3157 * Called from I/O completion to determine which dormant/delayed
3158 * and ordered cmds need to have their tasks added to the execution queue.
3160 static void transport_complete_task_attr(struct se_cmd *cmd)
3162 struct se_device *dev = cmd->se_dev;
3163 struct se_cmd *cmd_p, *cmd_tmp;
3164 int new_active_tasks = 0;
3166 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3167 atomic_dec(&dev->simple_cmds);
3168 smp_mb__after_atomic_dec();
3169 dev->dev_cur_ordered_id++;
3170 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3171 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3172 cmd->se_ordered_id);
3173 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3174 atomic_dec(&dev->dev_hoq_count);
3175 smp_mb__after_atomic_dec();
3176 dev->dev_cur_ordered_id++;
3177 pr_debug("Incremented dev_cur_ordered_id: %u for"
3178 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3179 cmd->se_ordered_id);
3180 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3181 spin_lock(&dev->ordered_cmd_lock);
3182 list_del(&cmd->se_ordered_node);
3183 atomic_dec(&dev->dev_ordered_sync);
3184 smp_mb__after_atomic_dec();
3185 spin_unlock(&dev->ordered_cmd_lock);
3187 dev->dev_cur_ordered_id++;
3188 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3189 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3192 * Process all commands up to the last received
3193 * ORDERED task attribute which requires another blocking
3196 spin_lock(&dev->delayed_cmd_lock);
3197 list_for_each_entry_safe(cmd_p, cmd_tmp,
3198 &dev->delayed_cmd_list, se_delayed_node) {
3200 list_del(&cmd_p->se_delayed_node);
3201 spin_unlock(&dev->delayed_cmd_lock);
3203 pr_debug("Calling add_tasks() for"
3204 " cmd_p: 0x%02x Task Attr: 0x%02x"
3205 " Dormant -> Active, se_ordered_id: %u\n",
3206 cmd_p->t_task_cdb[0],
3207 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3209 transport_add_tasks_from_cmd(cmd_p);
3212 spin_lock(&dev->delayed_cmd_lock);
3213 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3216 spin_unlock(&dev->delayed_cmd_lock);
3218 * If new tasks have become active, wake up the transport thread
3219 * to do the processing of the Active tasks.
3221 if (new_active_tasks != 0)
3222 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3225 static void transport_complete_qf(struct se_cmd *cmd)
3229 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3230 transport_complete_task_attr(cmd);
3232 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3233 ret = cmd->se_tfo->queue_status(cmd);
3238 switch (cmd->data_direction) {
3239 case DMA_FROM_DEVICE:
3240 ret = cmd->se_tfo->queue_data_in(cmd);
3243 if (cmd->t_bidi_data_sg) {
3244 ret = cmd->se_tfo->queue_data_in(cmd);
3248 /* Fall through for DMA_TO_DEVICE */
3250 ret = cmd->se_tfo->queue_status(cmd);
3258 transport_handle_queue_full(cmd, cmd->se_dev);
3261 transport_lun_remove_cmd(cmd);
3262 transport_cmd_check_stop_to_fabric(cmd);
3265 static void transport_handle_queue_full(
3267 struct se_device *dev)
3269 spin_lock_irq(&dev->qf_cmd_lock);
3270 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3271 atomic_inc(&dev->dev_qf_count);
3272 smp_mb__after_atomic_inc();
3273 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3275 schedule_work(&cmd->se_dev->qf_work_queue);
3278 static void target_complete_ok_work(struct work_struct *work)
3280 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
3281 int reason = 0, ret;
3284 * Check if we need to move delayed/dormant tasks from cmds on the
3285 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3288 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3289 transport_complete_task_attr(cmd);
3291 * Check to schedule QUEUE_FULL work, or execute an existing
3292 * cmd->transport_qf_callback()
3294 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3295 schedule_work(&cmd->se_dev->qf_work_queue);
3298 * Check if we need to retrieve a sense buffer from
3299 * the struct se_cmd in question.
3301 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3302 if (transport_get_sense_data(cmd) < 0)
3303 reason = TCM_NON_EXISTENT_LUN;
3306 * Only set when an struct se_task->task_scsi_status returned
3307 * a non GOOD status.
3309 if (cmd->scsi_status) {
3310 ret = transport_send_check_condition_and_sense(
3312 if (ret == -EAGAIN || ret == -ENOMEM)
3315 transport_lun_remove_cmd(cmd);
3316 transport_cmd_check_stop_to_fabric(cmd);
3321 * Check for a callback, used by amongst other things
3322 * XDWRITE_READ_10 emulation.
3324 if (cmd->transport_complete_callback)
3325 cmd->transport_complete_callback(cmd);
3327 switch (cmd->data_direction) {
3328 case DMA_FROM_DEVICE:
3329 spin_lock(&cmd->se_lun->lun_sep_lock);
3330 if (cmd->se_lun->lun_sep) {
3331 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3334 spin_unlock(&cmd->se_lun->lun_sep_lock);
3336 ret = cmd->se_tfo->queue_data_in(cmd);
3337 if (ret == -EAGAIN || ret == -ENOMEM)
3341 spin_lock(&cmd->se_lun->lun_sep_lock);
3342 if (cmd->se_lun->lun_sep) {
3343 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3346 spin_unlock(&cmd->se_lun->lun_sep_lock);
3348 * Check if we need to send READ payload for BIDI-COMMAND
3350 if (cmd->t_bidi_data_sg) {
3351 spin_lock(&cmd->se_lun->lun_sep_lock);
3352 if (cmd->se_lun->lun_sep) {
3353 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3356 spin_unlock(&cmd->se_lun->lun_sep_lock);
3357 ret = cmd->se_tfo->queue_data_in(cmd);
3358 if (ret == -EAGAIN || ret == -ENOMEM)
3362 /* Fall through for DMA_TO_DEVICE */
3364 ret = cmd->se_tfo->queue_status(cmd);
3365 if (ret == -EAGAIN || ret == -ENOMEM)
3372 transport_lun_remove_cmd(cmd);
3373 transport_cmd_check_stop_to_fabric(cmd);
3377 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3378 " data_direction: %d\n", cmd, cmd->data_direction);
3379 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3380 transport_handle_queue_full(cmd, cmd->se_dev);
3383 static void transport_free_dev_tasks(struct se_cmd *cmd)
3385 struct se_task *task, *task_tmp;
3386 unsigned long flags;
3387 LIST_HEAD(dispose_list);
3389 spin_lock_irqsave(&cmd->t_state_lock, flags);
3390 list_for_each_entry_safe(task, task_tmp,
3391 &cmd->t_task_list, t_list) {
3392 if (!(task->task_flags & TF_ACTIVE))
3393 list_move_tail(&task->t_list, &dispose_list);
3395 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3397 while (!list_empty(&dispose_list)) {
3398 task = list_first_entry(&dispose_list, struct se_task, t_list);
3400 if (task->task_sg != cmd->t_data_sg &&
3401 task->task_sg != cmd->t_bidi_data_sg)
3402 kfree(task->task_sg);
3404 list_del(&task->t_list);
3406 cmd->se_dev->transport->free_task(task);
3410 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3412 struct scatterlist *sg;
3415 for_each_sg(sgl, sg, nents, count)
3416 __free_page(sg_page(sg));
3421 static inline void transport_free_pages(struct se_cmd *cmd)
3423 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3426 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3427 cmd->t_data_sg = NULL;
3428 cmd->t_data_nents = 0;
3430 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3431 cmd->t_bidi_data_sg = NULL;
3432 cmd->t_bidi_data_nents = 0;
3436 * transport_put_cmd - release a reference to a command
3437 * @cmd: command to release
3439 * This routine releases our reference to the command and frees it if possible.
3441 static void transport_put_cmd(struct se_cmd *cmd)
3443 unsigned long flags;
3446 spin_lock_irqsave(&cmd->t_state_lock, flags);
3447 if (atomic_read(&cmd->t_fe_count)) {
3448 if (!atomic_dec_and_test(&cmd->t_fe_count))
3452 if (atomic_read(&cmd->t_se_count)) {
3453 if (!atomic_dec_and_test(&cmd->t_se_count))
3457 if (atomic_read(&cmd->transport_dev_active)) {
3458 atomic_set(&cmd->transport_dev_active, 0);
3459 transport_all_task_dev_remove_state(cmd);
3462 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3464 if (free_tasks != 0)
3465 transport_free_dev_tasks(cmd);
3467 transport_free_pages(cmd);
3468 transport_release_cmd(cmd);
3471 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3475 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3476 * allocating in the core.
3477 * @cmd: Associated se_cmd descriptor
3478 * @mem: SGL style memory for TCM WRITE / READ
3479 * @sg_mem_num: Number of SGL elements
3480 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3481 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3483 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3486 int transport_generic_map_mem_to_cmd(
3488 struct scatterlist *sgl,
3490 struct scatterlist *sgl_bidi,
3493 if (!sgl || !sgl_count)
3496 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3497 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3499 cmd->t_data_sg = sgl;
3500 cmd->t_data_nents = sgl_count;
3502 if (sgl_bidi && sgl_bidi_count) {
3503 cmd->t_bidi_data_sg = sgl_bidi;
3504 cmd->t_bidi_data_nents = sgl_bidi_count;
3506 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3511 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3513 void *transport_kmap_first_data_page(struct se_cmd *cmd)
3515 struct scatterlist *sg = cmd->t_data_sg;
3519 * We need to take into account a possible offset here for fabrics like
3520 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3521 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3523 return kmap(sg_page(sg)) + sg->offset;
3525 EXPORT_SYMBOL(transport_kmap_first_data_page);
3527 void transport_kunmap_first_data_page(struct se_cmd *cmd)
3529 kunmap(sg_page(cmd->t_data_sg));
3531 EXPORT_SYMBOL(transport_kunmap_first_data_page);
3534 transport_generic_get_mem(struct se_cmd *cmd)
3536 u32 length = cmd->data_length;
3541 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3542 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3543 if (!cmd->t_data_sg)
3546 cmd->t_data_nents = nents;
3547 sg_init_table(cmd->t_data_sg, nents);
3550 u32 page_len = min_t(u32, length, PAGE_SIZE);
3551 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3555 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3563 __free_page(sg_page(&cmd->t_data_sg[i]));
3566 kfree(cmd->t_data_sg);
3567 cmd->t_data_sg = NULL;
3571 /* Reduce sectors if they are too long for the device */
3572 static inline sector_t transport_limit_task_sectors(
3573 struct se_device *dev,
3574 unsigned long long lba,
3577 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
3579 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3580 if ((lba + sectors) > transport_dev_end_lba(dev))
3581 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
3588 * This function can be used by HW target mode drivers to create a linked
3589 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3590 * This is intended to be called during the completion path by TCM Core
3591 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3593 void transport_do_task_sg_chain(struct se_cmd *cmd)
3595 struct scatterlist *sg_first = NULL;
3596 struct scatterlist *sg_prev = NULL;
3597 int sg_prev_nents = 0;
3598 struct scatterlist *sg;
3599 struct se_task *task;
3600 u32 chained_nents = 0;
3603 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3606 * Walk the struct se_task list and setup scatterlist chains
3607 * for each contiguously allocated struct se_task->task_sg[].
3609 list_for_each_entry(task, &cmd->t_task_list, t_list) {
3614 sg_first = task->task_sg;
3615 chained_nents = task->task_sg_nents;
3617 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
3618 chained_nents += task->task_sg_nents;
3621 * For the padded tasks, use the extra SGL vector allocated
3622 * in transport_allocate_data_tasks() for the sg_prev_nents
3623 * offset into sg_chain() above.
3625 * We do not need the padding for the last task (or a single
3626 * task), but in that case we will never use the sg_prev_nents
3627 * value below which would be incorrect.
3629 sg_prev_nents = (task->task_sg_nents + 1);
3630 sg_prev = task->task_sg;
3633 * Setup the starting pointer and total t_tasks_sg_linked_no including
3634 * padding SGs for linking and to mark the end.
3636 cmd->t_tasks_sg_chained = sg_first;
3637 cmd->t_tasks_sg_chained_no = chained_nents;
3639 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
3640 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3641 cmd->t_tasks_sg_chained_no);
3643 for_each_sg(cmd->t_tasks_sg_chained, sg,
3644 cmd->t_tasks_sg_chained_no, i) {
3646 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
3647 i, sg, sg_page(sg), sg->length, sg->offset);
3648 if (sg_is_chain(sg))
3649 pr_debug("SG: %p sg_is_chain=1\n", sg);
3651 pr_debug("SG: %p sg_is_last=1\n", sg);
3654 EXPORT_SYMBOL(transport_do_task_sg_chain);
3657 * Break up cmd into chunks transport can handle
3660 transport_allocate_data_tasks(struct se_cmd *cmd,
3661 enum dma_data_direction data_direction,
3662 struct scatterlist *cmd_sg, unsigned int sgl_nents)
3664 struct se_device *dev = cmd->se_dev;
3666 unsigned long long lba;
3667 sector_t sectors, dev_max_sectors;
3670 if (transport_cmd_get_valid_sectors(cmd) < 0)
3673 dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3674 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
3676 WARN_ON(cmd->data_length % sector_size);
3678 lba = cmd->t_task_lba;
3679 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
3680 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
3683 * If we need just a single task reuse the SG list in the command
3684 * and avoid a lot of work.
3686 if (task_count == 1) {
3687 struct se_task *task;
3688 unsigned long flags;
3690 task = transport_generic_get_task(cmd, data_direction);
3694 task->task_sg = cmd_sg;
3695 task->task_sg_nents = sgl_nents;
3697 task->task_lba = lba;
3698 task->task_sectors = sectors;
3699 task->task_size = task->task_sectors * sector_size;
3701 spin_lock_irqsave(&cmd->t_state_lock, flags);
3702 list_add_tail(&task->t_list, &cmd->t_task_list);
3703 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3708 for (i = 0; i < task_count; i++) {
3709 struct se_task *task;
3710 unsigned int task_size, task_sg_nents_padded;
3711 struct scatterlist *sg;
3712 unsigned long flags;
3715 task = transport_generic_get_task(cmd, data_direction);
3719 task->task_lba = lba;
3720 task->task_sectors = min(sectors, dev_max_sectors);
3721 task->task_size = task->task_sectors * sector_size;
3724 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3725 * in order to calculate the number per task SGL entries
3727 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
3729 * Check if the fabric module driver is requesting that all
3730 * struct se_task->task_sg[] be chained together.. If so,
3731 * then allocate an extra padding SG entry for linking and
3732 * marking the end of the chained SGL for every task except
3733 * the last one for (task_count > 1) operation, or skipping
3734 * the extra padding for the (task_count == 1) case.
3736 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3737 task_sg_nents_padded = (task->task_sg_nents + 1);
3739 task_sg_nents_padded = task->task_sg_nents;
3741 task->task_sg = kmalloc(sizeof(struct scatterlist) *
3742 task_sg_nents_padded, GFP_KERNEL);
3743 if (!task->task_sg) {
3744 cmd->se_dev->transport->free_task(task);
3748 sg_init_table(task->task_sg, task_sg_nents_padded);
3750 task_size = task->task_size;
3752 /* Build new sgl, only up to task_size */
3753 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
3754 if (cmd_sg->length > task_size)
3758 task_size -= cmd_sg->length;
3759 cmd_sg = sg_next(cmd_sg);
3762 lba += task->task_sectors;
3763 sectors -= task->task_sectors;
3765 spin_lock_irqsave(&cmd->t_state_lock, flags);
3766 list_add_tail(&task->t_list, &cmd->t_task_list);
3767 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3774 transport_allocate_control_task(struct se_cmd *cmd)
3776 struct se_task *task;
3777 unsigned long flags;
3779 task = transport_generic_get_task(cmd, cmd->data_direction);
3783 task->task_sg = cmd->t_data_sg;
3784 task->task_size = cmd->data_length;
3785 task->task_sg_nents = cmd->t_data_nents;
3787 spin_lock_irqsave(&cmd->t_state_lock, flags);
3788 list_add_tail(&task->t_list, &cmd->t_task_list);
3789 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3791 /* Success! Return number of tasks allocated */
3796 * Allocate any required ressources to execute the command, and either place
3797 * it on the execution queue if possible. For writes we might not have the
3798 * payload yet, thus notify the fabric via a call to ->write_pending instead.
3800 int transport_generic_new_cmd(struct se_cmd *cmd)
3802 struct se_device *dev = cmd->se_dev;
3803 int task_cdbs, task_cdbs_bidi = 0;
3808 * Determine is the TCM fabric module has already allocated physical
3809 * memory, and is directly calling transport_generic_map_mem_to_cmd()
3812 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
3814 ret = transport_generic_get_mem(cmd);
3820 * For BIDI command set up the read tasks first.
3822 if (cmd->t_bidi_data_sg &&
3823 dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
3824 BUG_ON(!(cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB));
3826 task_cdbs_bidi = transport_allocate_data_tasks(cmd,
3827 DMA_FROM_DEVICE, cmd->t_bidi_data_sg,
3828 cmd->t_bidi_data_nents);
3829 if (task_cdbs_bidi <= 0)
3832 atomic_inc(&cmd->t_fe_count);
3833 atomic_inc(&cmd->t_se_count);
3837 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
3838 task_cdbs = transport_allocate_data_tasks(cmd,
3839 cmd->data_direction, cmd->t_data_sg,
3842 task_cdbs = transport_allocate_control_task(cmd);
3849 atomic_inc(&cmd->t_fe_count);
3850 atomic_inc(&cmd->t_se_count);
3853 cmd->t_task_list_num = (task_cdbs + task_cdbs_bidi);
3854 atomic_set(&cmd->t_task_cdbs_left, cmd->t_task_list_num);
3855 atomic_set(&cmd->t_task_cdbs_ex_left, cmd->t_task_list_num);
3858 * For WRITEs, let the fabric know its buffer is ready..
3859 * This WRITE struct se_cmd (and all of its associated struct se_task's)
3860 * will be added to the struct se_device execution queue after its WRITE
3861 * data has arrived. (ie: It gets handled by the transport processing
3862 * thread a second time)
3864 if (cmd->data_direction == DMA_TO_DEVICE) {
3865 transport_add_tasks_to_state_queue(cmd);
3866 return transport_generic_write_pending(cmd);
3869 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
3870 * to the execution queue.
3872 transport_execute_tasks(cmd);
3876 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3877 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3880 EXPORT_SYMBOL(transport_generic_new_cmd);
3882 /* transport_generic_process_write():
3886 void transport_generic_process_write(struct se_cmd *cmd)
3888 transport_execute_tasks(cmd);
3890 EXPORT_SYMBOL(transport_generic_process_write);
3892 static void transport_write_pending_qf(struct se_cmd *cmd)
3896 ret = cmd->se_tfo->write_pending(cmd);
3897 if (ret == -EAGAIN || ret == -ENOMEM) {
3898 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
3900 transport_handle_queue_full(cmd, cmd->se_dev);
3904 static int transport_generic_write_pending(struct se_cmd *cmd)
3906 unsigned long flags;
3909 spin_lock_irqsave(&cmd->t_state_lock, flags);
3910 cmd->t_state = TRANSPORT_WRITE_PENDING;
3911 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3914 * Clear the se_cmd for WRITE_PENDING status in order to set
3915 * cmd->t_transport_active=0 so that transport_generic_handle_data
3916 * can be called from HW target mode interrupt code. This is safe
3917 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
3918 * because the se_cmd->se_lun pointer is not being cleared.
3920 transport_cmd_check_stop(cmd, 1, 0);
3923 * Call the fabric write_pending function here to let the
3924 * frontend know that WRITE buffers are ready.
3926 ret = cmd->se_tfo->write_pending(cmd);
3927 if (ret == -EAGAIN || ret == -ENOMEM)
3932 return PYX_TRANSPORT_WRITE_PENDING;
3935 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
3936 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
3937 transport_handle_queue_full(cmd, cmd->se_dev);
3942 * transport_release_cmd - free a command
3943 * @cmd: command to free
3945 * This routine unconditionally frees a command, and reference counting
3946 * or list removal must be done in the caller.
3948 void transport_release_cmd(struct se_cmd *cmd)
3950 BUG_ON(!cmd->se_tfo);
3952 if (cmd->se_tmr_req)
3953 core_tmr_release_req(cmd->se_tmr_req);
3954 if (cmd->t_task_cdb != cmd->__t_task_cdb)
3955 kfree(cmd->t_task_cdb);
3956 cmd->se_tfo->release_cmd(cmd);
3958 EXPORT_SYMBOL(transport_release_cmd);
3960 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
3962 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
3963 if (wait_for_tasks && cmd->se_tmr_req)
3964 transport_wait_for_tasks(cmd);
3966 transport_release_cmd(cmd);
3969 transport_wait_for_tasks(cmd);
3971 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
3974 transport_lun_remove_cmd(cmd);
3976 transport_free_dev_tasks(cmd);
3978 transport_put_cmd(cmd);
3981 EXPORT_SYMBOL(transport_generic_free_cmd);
3983 /* transport_lun_wait_for_tasks():
3985 * Called from ConfigFS context to stop the passed struct se_cmd to allow
3986 * an struct se_lun to be successfully shutdown.
3988 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
3990 unsigned long flags;
3993 * If the frontend has already requested this struct se_cmd to
3994 * be stopped, we can safely ignore this struct se_cmd.
3996 spin_lock_irqsave(&cmd->t_state_lock, flags);
3997 if (atomic_read(&cmd->t_transport_stop)) {
3998 atomic_set(&cmd->transport_lun_stop, 0);
3999 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4000 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
4001 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4002 transport_cmd_check_stop(cmd, 1, 0);
4005 atomic_set(&cmd->transport_lun_fe_stop, 1);
4006 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4008 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4010 ret = transport_stop_tasks_for_cmd(cmd);
4012 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4013 " %d\n", cmd, cmd->t_task_list_num, ret);
4015 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4016 cmd->se_tfo->get_task_tag(cmd));
4017 wait_for_completion(&cmd->transport_lun_stop_comp);
4018 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4019 cmd->se_tfo->get_task_tag(cmd));
4021 transport_remove_cmd_from_queue(cmd);
4026 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4028 struct se_cmd *cmd = NULL;
4029 unsigned long lun_flags, cmd_flags;
4031 * Do exception processing and return CHECK_CONDITION status to the
4034 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4035 while (!list_empty(&lun->lun_cmd_list)) {
4036 cmd = list_first_entry(&lun->lun_cmd_list,
4037 struct se_cmd, se_lun_node);
4038 list_del(&cmd->se_lun_node);
4040 atomic_set(&cmd->transport_lun_active, 0);
4042 * This will notify iscsi_target_transport.c:
4043 * transport_cmd_check_stop() that a LUN shutdown is in
4044 * progress for the iscsi_cmd_t.
4046 spin_lock(&cmd->t_state_lock);
4047 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4048 "_lun_stop for ITT: 0x%08x\n",
4049 cmd->se_lun->unpacked_lun,
4050 cmd->se_tfo->get_task_tag(cmd));
4051 atomic_set(&cmd->transport_lun_stop, 1);
4052 spin_unlock(&cmd->t_state_lock);
4054 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4057 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4058 cmd->se_tfo->get_task_tag(cmd),
4059 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4063 * If the Storage engine still owns the iscsi_cmd_t, determine
4064 * and/or stop its context.
4066 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4067 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4068 cmd->se_tfo->get_task_tag(cmd));
4070 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4071 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4075 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4076 "_wait_for_tasks(): SUCCESS\n",
4077 cmd->se_lun->unpacked_lun,
4078 cmd->se_tfo->get_task_tag(cmd));
4080 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4081 if (!atomic_read(&cmd->transport_dev_active)) {
4082 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4085 atomic_set(&cmd->transport_dev_active, 0);
4086 transport_all_task_dev_remove_state(cmd);
4087 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4089 transport_free_dev_tasks(cmd);
4091 * The Storage engine stopped this struct se_cmd before it was
4092 * send to the fabric frontend for delivery back to the
4093 * Initiator Node. Return this SCSI CDB back with an
4094 * CHECK_CONDITION status.
4097 transport_send_check_condition_and_sense(cmd,
4098 TCM_NON_EXISTENT_LUN, 0);
4100 * If the fabric frontend is waiting for this iscsi_cmd_t to
4101 * be released, notify the waiting thread now that LU has
4102 * finished accessing it.
4104 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4105 if (atomic_read(&cmd->transport_lun_fe_stop)) {
4106 pr_debug("SE_LUN[%d] - Detected FE stop for"
4107 " struct se_cmd: %p ITT: 0x%08x\n",
4109 cmd, cmd->se_tfo->get_task_tag(cmd));
4111 spin_unlock_irqrestore(&cmd->t_state_lock,
4113 transport_cmd_check_stop(cmd, 1, 0);
4114 complete(&cmd->transport_lun_fe_stop_comp);
4115 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4118 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4119 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4121 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4122 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4124 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4127 static int transport_clear_lun_thread(void *p)
4129 struct se_lun *lun = (struct se_lun *)p;
4131 __transport_clear_lun_from_sessions(lun);
4132 complete(&lun->lun_shutdown_comp);
4137 int transport_clear_lun_from_sessions(struct se_lun *lun)
4139 struct task_struct *kt;
4141 kt = kthread_run(transport_clear_lun_thread, lun,
4142 "tcm_cl_%u", lun->unpacked_lun);
4144 pr_err("Unable to start clear_lun thread\n");
4147 wait_for_completion(&lun->lun_shutdown_comp);
4153 * transport_wait_for_tasks - wait for completion to occur
4154 * @cmd: command to wait
4156 * Called from frontend fabric context to wait for storage engine
4157 * to pause and/or release frontend generated struct se_cmd.
4159 void transport_wait_for_tasks(struct se_cmd *cmd)
4161 unsigned long flags;
4163 spin_lock_irqsave(&cmd->t_state_lock, flags);
4164 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4165 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4169 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4170 * has been set in transport_set_supported_SAM_opcode().
4172 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4173 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4177 * If we are already stopped due to an external event (ie: LUN shutdown)
4178 * sleep until the connection can have the passed struct se_cmd back.
4179 * The cmd->transport_lun_stopped_sem will be upped by
4180 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4181 * has completed its operation on the struct se_cmd.
4183 if (atomic_read(&cmd->transport_lun_stop)) {
4185 pr_debug("wait_for_tasks: Stopping"
4186 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4187 "_stop_comp); for ITT: 0x%08x\n",
4188 cmd->se_tfo->get_task_tag(cmd));
4190 * There is a special case for WRITES where a FE exception +
4191 * LUN shutdown means ConfigFS context is still sleeping on
4192 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4193 * We go ahead and up transport_lun_stop_comp just to be sure
4196 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4197 complete(&cmd->transport_lun_stop_comp);
4198 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4199 spin_lock_irqsave(&cmd->t_state_lock, flags);
4201 transport_all_task_dev_remove_state(cmd);
4203 * At this point, the frontend who was the originator of this
4204 * struct se_cmd, now owns the structure and can be released through
4205 * normal means below.
4207 pr_debug("wait_for_tasks: Stopped"
4208 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4209 "stop_comp); for ITT: 0x%08x\n",
4210 cmd->se_tfo->get_task_tag(cmd));
4212 atomic_set(&cmd->transport_lun_stop, 0);
4214 if (!atomic_read(&cmd->t_transport_active) ||
4215 atomic_read(&cmd->t_transport_aborted)) {
4216 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4220 atomic_set(&cmd->t_transport_stop, 1);
4222 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4223 " i_state: %d, t_state: %d, t_transport_stop = TRUE\n",
4224 cmd, cmd->se_tfo->get_task_tag(cmd),
4225 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4227 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4229 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4231 wait_for_completion(&cmd->t_transport_stop_comp);
4233 spin_lock_irqsave(&cmd->t_state_lock, flags);
4234 atomic_set(&cmd->t_transport_active, 0);
4235 atomic_set(&cmd->t_transport_stop, 0);
4237 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4238 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4239 cmd->se_tfo->get_task_tag(cmd));
4241 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4243 EXPORT_SYMBOL(transport_wait_for_tasks);
4245 static int transport_get_sense_codes(
4250 *asc = cmd->scsi_asc;
4251 *ascq = cmd->scsi_ascq;
4256 static int transport_set_sense_codes(
4261 cmd->scsi_asc = asc;
4262 cmd->scsi_ascq = ascq;
4267 int transport_send_check_condition_and_sense(
4272 unsigned char *buffer = cmd->sense_buffer;
4273 unsigned long flags;
4275 u8 asc = 0, ascq = 0;
4277 spin_lock_irqsave(&cmd->t_state_lock, flags);
4278 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4279 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4282 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4283 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4285 if (!reason && from_transport)
4288 if (!from_transport)
4289 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4291 * Data Segment and SenseLength of the fabric response PDU.
4293 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4294 * from include/scsi/scsi_cmnd.h
4296 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4297 TRANSPORT_SENSE_BUFFER);
4299 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4300 * SENSE KEY values from include/scsi/scsi.h
4303 case TCM_NON_EXISTENT_LUN:
4305 buffer[offset] = 0x70;
4306 /* ILLEGAL REQUEST */
4307 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4308 /* LOGICAL UNIT NOT SUPPORTED */
4309 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4311 case TCM_UNSUPPORTED_SCSI_OPCODE:
4312 case TCM_SECTOR_COUNT_TOO_MANY:
4314 buffer[offset] = 0x70;
4315 /* ILLEGAL REQUEST */
4316 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4317 /* INVALID COMMAND OPERATION CODE */
4318 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4320 case TCM_UNKNOWN_MODE_PAGE:
4322 buffer[offset] = 0x70;
4323 /* ILLEGAL REQUEST */
4324 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4325 /* INVALID FIELD IN CDB */
4326 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4328 case TCM_CHECK_CONDITION_ABORT_CMD:
4330 buffer[offset] = 0x70;
4331 /* ABORTED COMMAND */
4332 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4333 /* BUS DEVICE RESET FUNCTION OCCURRED */
4334 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4335 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4337 case TCM_INCORRECT_AMOUNT_OF_DATA:
4339 buffer[offset] = 0x70;
4340 /* ABORTED COMMAND */
4341 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4343 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4344 /* NOT ENOUGH UNSOLICITED DATA */
4345 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4347 case TCM_INVALID_CDB_FIELD:
4349 buffer[offset] = 0x70;
4350 /* ABORTED COMMAND */
4351 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4352 /* INVALID FIELD IN CDB */
4353 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4355 case TCM_INVALID_PARAMETER_LIST:
4357 buffer[offset] = 0x70;
4358 /* ABORTED COMMAND */
4359 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4360 /* INVALID FIELD IN PARAMETER LIST */
4361 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4363 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4365 buffer[offset] = 0x70;
4366 /* ABORTED COMMAND */
4367 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4369 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4370 /* UNEXPECTED_UNSOLICITED_DATA */
4371 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4373 case TCM_SERVICE_CRC_ERROR:
4375 buffer[offset] = 0x70;
4376 /* ABORTED COMMAND */
4377 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4378 /* PROTOCOL SERVICE CRC ERROR */
4379 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4381 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4383 case TCM_SNACK_REJECTED:
4385 buffer[offset] = 0x70;
4386 /* ABORTED COMMAND */
4387 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4389 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4390 /* FAILED RETRANSMISSION REQUEST */
4391 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4393 case TCM_WRITE_PROTECTED:
4395 buffer[offset] = 0x70;
4397 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4398 /* WRITE PROTECTED */
4399 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4401 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4403 buffer[offset] = 0x70;
4404 /* UNIT ATTENTION */
4405 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4406 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4407 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4408 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4410 case TCM_CHECK_CONDITION_NOT_READY:
4412 buffer[offset] = 0x70;
4414 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4415 transport_get_sense_codes(cmd, &asc, &ascq);
4416 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4417 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4419 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4422 buffer[offset] = 0x70;
4423 /* ILLEGAL REQUEST */
4424 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4425 /* LOGICAL UNIT COMMUNICATION FAILURE */
4426 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4430 * This code uses linux/include/scsi/scsi.h SAM status codes!
4432 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4434 * Automatically padded, this value is encoded in the fabric's
4435 * data_length response PDU containing the SCSI defined sense data.
4437 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4440 return cmd->se_tfo->queue_status(cmd);
4442 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4444 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4448 if (atomic_read(&cmd->t_transport_aborted) != 0) {
4450 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4453 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4454 " status for CDB: 0x%02x ITT: 0x%08x\n",
4456 cmd->se_tfo->get_task_tag(cmd));
4458 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4459 cmd->se_tfo->queue_status(cmd);
4464 EXPORT_SYMBOL(transport_check_aborted_status);
4466 void transport_send_task_abort(struct se_cmd *cmd)
4468 unsigned long flags;
4470 spin_lock_irqsave(&cmd->t_state_lock, flags);
4471 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4472 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4475 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4478 * If there are still expected incoming fabric WRITEs, we wait
4479 * until until they have completed before sending a TASK_ABORTED
4480 * response. This response with TASK_ABORTED status will be
4481 * queued back to fabric module by transport_check_aborted_status().
4483 if (cmd->data_direction == DMA_TO_DEVICE) {
4484 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4485 atomic_inc(&cmd->t_transport_aborted);
4486 smp_mb__after_atomic_inc();
4487 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4488 transport_new_cmd_failure(cmd);
4492 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4494 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4495 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4496 cmd->se_tfo->get_task_tag(cmd));
4498 cmd->se_tfo->queue_status(cmd);
4501 /* transport_generic_do_tmr():
4505 int transport_generic_do_tmr(struct se_cmd *cmd)
4507 struct se_device *dev = cmd->se_dev;
4508 struct se_tmr_req *tmr = cmd->se_tmr_req;
4511 switch (tmr->function) {
4512 case TMR_ABORT_TASK:
4513 tmr->response = TMR_FUNCTION_REJECTED;
4515 case TMR_ABORT_TASK_SET:
4517 case TMR_CLEAR_TASK_SET:
4518 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4521 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4522 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4523 TMR_FUNCTION_REJECTED;
4525 case TMR_TARGET_WARM_RESET:
4526 tmr->response = TMR_FUNCTION_REJECTED;
4528 case TMR_TARGET_COLD_RESET:
4529 tmr->response = TMR_FUNCTION_REJECTED;
4532 pr_err("Uknown TMR function: 0x%02x.\n",
4534 tmr->response = TMR_FUNCTION_REJECTED;
4538 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4539 cmd->se_tfo->queue_tm_rsp(cmd);
4541 transport_cmd_check_stop_to_fabric(cmd);
4545 /* transport_processing_thread():
4549 static int transport_processing_thread(void *param)
4553 struct se_device *dev = (struct se_device *) param;
4555 set_user_nice(current, -20);
4557 while (!kthread_should_stop()) {
4558 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4559 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4560 kthread_should_stop());
4565 __transport_execute_tasks(dev);
4567 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4571 switch (cmd->t_state) {
4572 case TRANSPORT_NEW_CMD:
4575 case TRANSPORT_NEW_CMD_MAP:
4576 if (!cmd->se_tfo->new_cmd_map) {
4577 pr_err("cmd->se_tfo->new_cmd_map is"
4578 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4581 ret = cmd->se_tfo->new_cmd_map(cmd);
4583 cmd->transport_error_status = ret;
4584 transport_generic_request_failure(cmd,
4585 0, (cmd->data_direction !=
4589 ret = transport_generic_new_cmd(cmd);
4591 cmd->transport_error_status = ret;
4592 transport_generic_request_failure(cmd,
4593 0, (cmd->data_direction !=
4597 case TRANSPORT_PROCESS_WRITE:
4598 transport_generic_process_write(cmd);
4600 case TRANSPORT_FREE_CMD_INTR:
4601 transport_generic_free_cmd(cmd, 0);
4603 case TRANSPORT_PROCESS_TMR:
4604 transport_generic_do_tmr(cmd);
4606 case TRANSPORT_COMPLETE_QF_WP:
4607 transport_write_pending_qf(cmd);
4609 case TRANSPORT_COMPLETE_QF_OK:
4610 transport_complete_qf(cmd);
4613 pr_err("Unknown t_state: %d for ITT: 0x%08x "
4614 "i_state: %d on SE LUN: %u\n",
4616 cmd->se_tfo->get_task_tag(cmd),
4617 cmd->se_tfo->get_cmd_state(cmd),
4618 cmd->se_lun->unpacked_lun);
4626 WARN_ON(!list_empty(&dev->state_task_list));
4627 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4628 dev->process_thread = NULL;