PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / target / target_core_transport.c
blob24b4f65d8777bd357efbb85f7324ac4d803da101
1 /*******************************************************************************
2 * Filename: target_core_transport.c
4 * This file contains the Generic Target Engine Core.
6 * (c) Copyright 2002-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/net.h>
27 #include <linux/delay.h>
28 #include <linux/string.h>
29 #include <linux/timer.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/kthread.h>
33 #include <linux/in.h>
34 #include <linux/cdrom.h>
35 #include <linux/module.h>
36 #include <linux/ratelimit.h>
37 #include <asm/unaligned.h>
38 #include <net/sock.h>
39 #include <net/tcp.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_tcq.h>
44 #include <target/target_core_base.h>
45 #include <target/target_core_backend.h>
46 #include <target/target_core_fabric.h>
47 #include <target/target_core_configfs.h>
49 #include "target_core_internal.h"
50 #include "target_core_alua.h"
51 #include "target_core_pr.h"
52 #include "target_core_ua.h"
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/target.h>
57 static struct workqueue_struct *target_completion_wq;
58 static struct kmem_cache *se_sess_cache;
59 struct kmem_cache *se_ua_cache;
60 struct kmem_cache *t10_pr_reg_cache;
61 struct kmem_cache *t10_alua_lu_gp_cache;
62 struct kmem_cache *t10_alua_lu_gp_mem_cache;
63 struct kmem_cache *t10_alua_tg_pt_gp_cache;
64 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
65 struct kmem_cache *t10_alua_lba_map_cache;
66 struct kmem_cache *t10_alua_lba_map_mem_cache;
68 static void transport_complete_task_attr(struct se_cmd *cmd);
69 static void transport_handle_queue_full(struct se_cmd *cmd,
70 struct se_device *dev);
71 static int transport_put_cmd(struct se_cmd *cmd);
72 static void target_complete_ok_work(struct work_struct *work);
74 int init_se_kmem_caches(void)
76 se_sess_cache = kmem_cache_create("se_sess_cache",
77 sizeof(struct se_session), __alignof__(struct se_session),
78 0, NULL);
79 if (!se_sess_cache) {
80 pr_err("kmem_cache_create() for struct se_session"
81 " failed\n");
82 goto out;
84 se_ua_cache = kmem_cache_create("se_ua_cache",
85 sizeof(struct se_ua), __alignof__(struct se_ua),
86 0, NULL);
87 if (!se_ua_cache) {
88 pr_err("kmem_cache_create() for struct se_ua failed\n");
89 goto out_free_sess_cache;
91 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
92 sizeof(struct t10_pr_registration),
93 __alignof__(struct t10_pr_registration), 0, NULL);
94 if (!t10_pr_reg_cache) {
95 pr_err("kmem_cache_create() for struct t10_pr_registration"
96 " failed\n");
97 goto out_free_ua_cache;
99 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
100 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
101 0, NULL);
102 if (!t10_alua_lu_gp_cache) {
103 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
104 " failed\n");
105 goto out_free_pr_reg_cache;
107 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
108 sizeof(struct t10_alua_lu_gp_member),
109 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
110 if (!t10_alua_lu_gp_mem_cache) {
111 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
112 "cache failed\n");
113 goto out_free_lu_gp_cache;
115 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
116 sizeof(struct t10_alua_tg_pt_gp),
117 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
118 if (!t10_alua_tg_pt_gp_cache) {
119 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
120 "cache failed\n");
121 goto out_free_lu_gp_mem_cache;
123 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
124 "t10_alua_tg_pt_gp_mem_cache",
125 sizeof(struct t10_alua_tg_pt_gp_member),
126 __alignof__(struct t10_alua_tg_pt_gp_member),
127 0, NULL);
128 if (!t10_alua_tg_pt_gp_mem_cache) {
129 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
130 "mem_t failed\n");
131 goto out_free_tg_pt_gp_cache;
133 t10_alua_lba_map_cache = kmem_cache_create(
134 "t10_alua_lba_map_cache",
135 sizeof(struct t10_alua_lba_map),
136 __alignof__(struct t10_alua_lba_map), 0, NULL);
137 if (!t10_alua_lba_map_cache) {
138 pr_err("kmem_cache_create() for t10_alua_lba_map_"
139 "cache failed\n");
140 goto out_free_tg_pt_gp_mem_cache;
142 t10_alua_lba_map_mem_cache = kmem_cache_create(
143 "t10_alua_lba_map_mem_cache",
144 sizeof(struct t10_alua_lba_map_member),
145 __alignof__(struct t10_alua_lba_map_member), 0, NULL);
146 if (!t10_alua_lba_map_mem_cache) {
147 pr_err("kmem_cache_create() for t10_alua_lba_map_mem_"
148 "cache failed\n");
149 goto out_free_lba_map_cache;
152 target_completion_wq = alloc_workqueue("target_completion",
153 WQ_MEM_RECLAIM, 0);
154 if (!target_completion_wq)
155 goto out_free_lba_map_mem_cache;
157 return 0;
159 out_free_lba_map_mem_cache:
160 kmem_cache_destroy(t10_alua_lba_map_mem_cache);
161 out_free_lba_map_cache:
162 kmem_cache_destroy(t10_alua_lba_map_cache);
163 out_free_tg_pt_gp_mem_cache:
164 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
165 out_free_tg_pt_gp_cache:
166 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
167 out_free_lu_gp_mem_cache:
168 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
169 out_free_lu_gp_cache:
170 kmem_cache_destroy(t10_alua_lu_gp_cache);
171 out_free_pr_reg_cache:
172 kmem_cache_destroy(t10_pr_reg_cache);
173 out_free_ua_cache:
174 kmem_cache_destroy(se_ua_cache);
175 out_free_sess_cache:
176 kmem_cache_destroy(se_sess_cache);
177 out:
178 return -ENOMEM;
181 void release_se_kmem_caches(void)
183 destroy_workqueue(target_completion_wq);
184 kmem_cache_destroy(se_sess_cache);
185 kmem_cache_destroy(se_ua_cache);
186 kmem_cache_destroy(t10_pr_reg_cache);
187 kmem_cache_destroy(t10_alua_lu_gp_cache);
188 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
189 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
190 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
191 kmem_cache_destroy(t10_alua_lba_map_cache);
192 kmem_cache_destroy(t10_alua_lba_map_mem_cache);
195 /* This code ensures unique mib indexes are handed out. */
196 static DEFINE_SPINLOCK(scsi_mib_index_lock);
197 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
200 * Allocate a new row index for the entry type specified
202 u32 scsi_get_new_index(scsi_index_t type)
204 u32 new_index;
206 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
208 spin_lock(&scsi_mib_index_lock);
209 new_index = ++scsi_mib_index[type];
210 spin_unlock(&scsi_mib_index_lock);
212 return new_index;
215 void transport_subsystem_check_init(void)
217 int ret;
218 static int sub_api_initialized;
220 if (sub_api_initialized)
221 return;
223 ret = request_module("target_core_iblock");
224 if (ret != 0)
225 pr_err("Unable to load target_core_iblock\n");
227 ret = request_module("target_core_file");
228 if (ret != 0)
229 pr_err("Unable to load target_core_file\n");
231 ret = request_module("target_core_pscsi");
232 if (ret != 0)
233 pr_err("Unable to load target_core_pscsi\n");
235 sub_api_initialized = 1;
238 struct se_session *transport_init_session(void)
240 struct se_session *se_sess;
242 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
243 if (!se_sess) {
244 pr_err("Unable to allocate struct se_session from"
245 " se_sess_cache\n");
246 return ERR_PTR(-ENOMEM);
248 INIT_LIST_HEAD(&se_sess->sess_list);
249 INIT_LIST_HEAD(&se_sess->sess_acl_list);
250 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
251 INIT_LIST_HEAD(&se_sess->sess_wait_list);
252 spin_lock_init(&se_sess->sess_cmd_lock);
253 kref_init(&se_sess->sess_kref);
255 return se_sess;
257 EXPORT_SYMBOL(transport_init_session);
259 int transport_alloc_session_tags(struct se_session *se_sess,
260 unsigned int tag_num, unsigned int tag_size)
262 int rc;
264 se_sess->sess_cmd_map = kzalloc(tag_num * tag_size,
265 GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
266 if (!se_sess->sess_cmd_map) {
267 se_sess->sess_cmd_map = vzalloc(tag_num * tag_size);
268 if (!se_sess->sess_cmd_map) {
269 pr_err("Unable to allocate se_sess->sess_cmd_map\n");
270 return -ENOMEM;
274 rc = percpu_ida_init(&se_sess->sess_tag_pool, tag_num);
275 if (rc < 0) {
276 pr_err("Unable to init se_sess->sess_tag_pool,"
277 " tag_num: %u\n", tag_num);
278 if (is_vmalloc_addr(se_sess->sess_cmd_map))
279 vfree(se_sess->sess_cmd_map);
280 else
281 kfree(se_sess->sess_cmd_map);
282 se_sess->sess_cmd_map = NULL;
283 return -ENOMEM;
286 return 0;
288 EXPORT_SYMBOL(transport_alloc_session_tags);
290 struct se_session *transport_init_session_tags(unsigned int tag_num,
291 unsigned int tag_size)
293 struct se_session *se_sess;
294 int rc;
296 se_sess = transport_init_session();
297 if (IS_ERR(se_sess))
298 return se_sess;
300 rc = transport_alloc_session_tags(se_sess, tag_num, tag_size);
301 if (rc < 0) {
302 transport_free_session(se_sess);
303 return ERR_PTR(-ENOMEM);
306 return se_sess;
308 EXPORT_SYMBOL(transport_init_session_tags);
311 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
313 void __transport_register_session(
314 struct se_portal_group *se_tpg,
315 struct se_node_acl *se_nacl,
316 struct se_session *se_sess,
317 void *fabric_sess_ptr)
319 unsigned char buf[PR_REG_ISID_LEN];
321 se_sess->se_tpg = se_tpg;
322 se_sess->fabric_sess_ptr = fabric_sess_ptr;
324 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
326 * Only set for struct se_session's that will actually be moving I/O.
327 * eg: *NOT* discovery sessions.
329 if (se_nacl) {
331 * If the fabric module supports an ISID based TransportID,
332 * save this value in binary from the fabric I_T Nexus now.
334 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
335 memset(&buf[0], 0, PR_REG_ISID_LEN);
336 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
337 &buf[0], PR_REG_ISID_LEN);
338 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
340 kref_get(&se_nacl->acl_kref);
342 spin_lock_irq(&se_nacl->nacl_sess_lock);
344 * The se_nacl->nacl_sess pointer will be set to the
345 * last active I_T Nexus for each struct se_node_acl.
347 se_nacl->nacl_sess = se_sess;
349 list_add_tail(&se_sess->sess_acl_list,
350 &se_nacl->acl_sess_list);
351 spin_unlock_irq(&se_nacl->nacl_sess_lock);
353 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
355 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
356 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
358 EXPORT_SYMBOL(__transport_register_session);
360 void transport_register_session(
361 struct se_portal_group *se_tpg,
362 struct se_node_acl *se_nacl,
363 struct se_session *se_sess,
364 void *fabric_sess_ptr)
366 unsigned long flags;
368 spin_lock_irqsave(&se_tpg->session_lock, flags);
369 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
370 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
372 EXPORT_SYMBOL(transport_register_session);
374 static void target_release_session(struct kref *kref)
376 struct se_session *se_sess = container_of(kref,
377 struct se_session, sess_kref);
378 struct se_portal_group *se_tpg = se_sess->se_tpg;
380 se_tpg->se_tpg_tfo->close_session(se_sess);
383 void target_get_session(struct se_session *se_sess)
385 kref_get(&se_sess->sess_kref);
387 EXPORT_SYMBOL(target_get_session);
389 void target_put_session(struct se_session *se_sess)
391 struct se_portal_group *tpg = se_sess->se_tpg;
393 if (tpg->se_tpg_tfo->put_session != NULL) {
394 tpg->se_tpg_tfo->put_session(se_sess);
395 return;
397 kref_put(&se_sess->sess_kref, target_release_session);
399 EXPORT_SYMBOL(target_put_session);
401 static void target_complete_nacl(struct kref *kref)
403 struct se_node_acl *nacl = container_of(kref,
404 struct se_node_acl, acl_kref);
406 complete(&nacl->acl_free_comp);
409 void target_put_nacl(struct se_node_acl *nacl)
411 kref_put(&nacl->acl_kref, target_complete_nacl);
414 void transport_deregister_session_configfs(struct se_session *se_sess)
416 struct se_node_acl *se_nacl;
417 unsigned long flags;
419 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
421 se_nacl = se_sess->se_node_acl;
422 if (se_nacl) {
423 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
424 if (se_nacl->acl_stop == 0)
425 list_del(&se_sess->sess_acl_list);
427 * If the session list is empty, then clear the pointer.
428 * Otherwise, set the struct se_session pointer from the tail
429 * element of the per struct se_node_acl active session list.
431 if (list_empty(&se_nacl->acl_sess_list))
432 se_nacl->nacl_sess = NULL;
433 else {
434 se_nacl->nacl_sess = container_of(
435 se_nacl->acl_sess_list.prev,
436 struct se_session, sess_acl_list);
438 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
441 EXPORT_SYMBOL(transport_deregister_session_configfs);
443 void transport_free_session(struct se_session *se_sess)
445 if (se_sess->sess_cmd_map) {
446 percpu_ida_destroy(&se_sess->sess_tag_pool);
447 if (is_vmalloc_addr(se_sess->sess_cmd_map))
448 vfree(se_sess->sess_cmd_map);
449 else
450 kfree(se_sess->sess_cmd_map);
452 kmem_cache_free(se_sess_cache, se_sess);
454 EXPORT_SYMBOL(transport_free_session);
456 void transport_deregister_session(struct se_session *se_sess)
458 struct se_portal_group *se_tpg = se_sess->se_tpg;
459 struct target_core_fabric_ops *se_tfo;
460 struct se_node_acl *se_nacl;
461 unsigned long flags;
462 bool comp_nacl = true;
464 if (!se_tpg) {
465 transport_free_session(se_sess);
466 return;
468 se_tfo = se_tpg->se_tpg_tfo;
470 spin_lock_irqsave(&se_tpg->session_lock, flags);
471 list_del(&se_sess->sess_list);
472 se_sess->se_tpg = NULL;
473 se_sess->fabric_sess_ptr = NULL;
474 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
477 * Determine if we need to do extra work for this initiator node's
478 * struct se_node_acl if it had been previously dynamically generated.
480 se_nacl = se_sess->se_node_acl;
482 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
483 if (se_nacl && se_nacl->dynamic_node_acl) {
484 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
485 list_del(&se_nacl->acl_list);
486 se_tpg->num_node_acls--;
487 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
488 core_tpg_wait_for_nacl_pr_ref(se_nacl);
489 core_free_device_list_for_node(se_nacl, se_tpg);
490 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
492 comp_nacl = false;
493 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
496 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
498 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
499 se_tpg->se_tpg_tfo->get_fabric_name());
501 * If last kref is dropping now for an explicit NodeACL, awake sleeping
502 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
503 * removal context.
505 if (se_nacl && comp_nacl == true)
506 target_put_nacl(se_nacl);
508 transport_free_session(se_sess);
510 EXPORT_SYMBOL(transport_deregister_session);
513 * Called with cmd->t_state_lock held.
515 static void target_remove_from_state_list(struct se_cmd *cmd)
517 struct se_device *dev = cmd->se_dev;
518 unsigned long flags;
520 if (!dev)
521 return;
523 if (cmd->transport_state & CMD_T_BUSY)
524 return;
526 spin_lock_irqsave(&dev->execute_task_lock, flags);
527 if (cmd->state_active) {
528 list_del(&cmd->state_list);
529 cmd->state_active = false;
531 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
534 static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists,
535 bool write_pending)
537 unsigned long flags;
539 spin_lock_irqsave(&cmd->t_state_lock, flags);
540 if (write_pending)
541 cmd->t_state = TRANSPORT_WRITE_PENDING;
543 if (remove_from_lists) {
544 target_remove_from_state_list(cmd);
547 * Clear struct se_cmd->se_lun before the handoff to FE.
549 cmd->se_lun = NULL;
553 * Determine if frontend context caller is requesting the stopping of
554 * this command for frontend exceptions.
556 if (cmd->transport_state & CMD_T_STOP) {
557 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
558 __func__, __LINE__,
559 cmd->se_tfo->get_task_tag(cmd));
561 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
563 complete(&cmd->t_transport_stop_comp);
564 return 1;
567 cmd->transport_state &= ~CMD_T_ACTIVE;
568 if (remove_from_lists) {
570 * Some fabric modules like tcm_loop can release
571 * their internally allocated I/O reference now and
572 * struct se_cmd now.
574 * Fabric modules are expected to return '1' here if the
575 * se_cmd being passed is released at this point,
576 * or zero if not being released.
578 if (cmd->se_tfo->check_stop_free != NULL) {
579 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
580 return cmd->se_tfo->check_stop_free(cmd);
584 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
585 return 0;
588 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
590 return transport_cmd_check_stop(cmd, true, false);
593 static void transport_lun_remove_cmd(struct se_cmd *cmd)
595 struct se_lun *lun = cmd->se_lun;
597 if (!lun)
598 return;
600 if (cmpxchg(&cmd->lun_ref_active, true, false))
601 percpu_ref_put(&lun->lun_ref);
604 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
606 if (transport_cmd_check_stop_to_fabric(cmd))
607 return;
608 if (remove)
609 transport_put_cmd(cmd);
612 static void target_complete_failure_work(struct work_struct *work)
614 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
616 transport_generic_request_failure(cmd,
617 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
621 * Used when asking transport to copy Sense Data from the underlying
622 * Linux/SCSI struct scsi_cmnd
624 static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd)
626 struct se_device *dev = cmd->se_dev;
628 WARN_ON(!cmd->se_lun);
630 if (!dev)
631 return NULL;
633 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION)
634 return NULL;
636 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
638 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
639 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
640 return cmd->sense_buffer;
643 void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
645 struct se_device *dev = cmd->se_dev;
646 int success = scsi_status == GOOD;
647 unsigned long flags;
649 cmd->scsi_status = scsi_status;
652 spin_lock_irqsave(&cmd->t_state_lock, flags);
653 cmd->transport_state &= ~CMD_T_BUSY;
655 if (dev && dev->transport->transport_complete) {
656 dev->transport->transport_complete(cmd,
657 cmd->t_data_sg,
658 transport_get_sense_buffer(cmd));
659 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
660 success = 1;
664 * See if we are waiting to complete for an exception condition.
666 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
667 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
668 complete(&cmd->task_stop_comp);
669 return;
673 * Check for case where an explicit ABORT_TASK has been received
674 * and transport_wait_for_tasks() will be waiting for completion..
676 if (cmd->transport_state & CMD_T_ABORTED &&
677 cmd->transport_state & CMD_T_STOP) {
678 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
679 complete(&cmd->t_transport_stop_comp);
680 return;
681 } else if (!success) {
682 INIT_WORK(&cmd->work, target_complete_failure_work);
683 } else {
684 INIT_WORK(&cmd->work, target_complete_ok_work);
687 cmd->t_state = TRANSPORT_COMPLETE;
688 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
689 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
691 queue_work(target_completion_wq, &cmd->work);
693 EXPORT_SYMBOL(target_complete_cmd);
695 static void target_add_to_state_list(struct se_cmd *cmd)
697 struct se_device *dev = cmd->se_dev;
698 unsigned long flags;
700 spin_lock_irqsave(&dev->execute_task_lock, flags);
701 if (!cmd->state_active) {
702 list_add_tail(&cmd->state_list, &dev->state_list);
703 cmd->state_active = true;
705 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
709 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
711 static void transport_write_pending_qf(struct se_cmd *cmd);
712 static void transport_complete_qf(struct se_cmd *cmd);
714 void target_qf_do_work(struct work_struct *work)
716 struct se_device *dev = container_of(work, struct se_device,
717 qf_work_queue);
718 LIST_HEAD(qf_cmd_list);
719 struct se_cmd *cmd, *cmd_tmp;
721 spin_lock_irq(&dev->qf_cmd_lock);
722 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
723 spin_unlock_irq(&dev->qf_cmd_lock);
725 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
726 list_del(&cmd->se_qf_node);
727 atomic_dec(&dev->dev_qf_count);
728 smp_mb__after_atomic_dec();
730 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
731 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
732 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
733 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
734 : "UNKNOWN");
736 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
737 transport_write_pending_qf(cmd);
738 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK)
739 transport_complete_qf(cmd);
743 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
745 switch (cmd->data_direction) {
746 case DMA_NONE:
747 return "NONE";
748 case DMA_FROM_DEVICE:
749 return "READ";
750 case DMA_TO_DEVICE:
751 return "WRITE";
752 case DMA_BIDIRECTIONAL:
753 return "BIDI";
754 default:
755 break;
758 return "UNKNOWN";
761 void transport_dump_dev_state(
762 struct se_device *dev,
763 char *b,
764 int *bl)
766 *bl += sprintf(b + *bl, "Status: ");
767 if (dev->export_count)
768 *bl += sprintf(b + *bl, "ACTIVATED");
769 else
770 *bl += sprintf(b + *bl, "DEACTIVATED");
772 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
773 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
774 dev->dev_attrib.block_size,
775 dev->dev_attrib.hw_max_sectors);
776 *bl += sprintf(b + *bl, " ");
779 void transport_dump_vpd_proto_id(
780 struct t10_vpd *vpd,
781 unsigned char *p_buf,
782 int p_buf_len)
784 unsigned char buf[VPD_TMP_BUF_SIZE];
785 int len;
787 memset(buf, 0, VPD_TMP_BUF_SIZE);
788 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
790 switch (vpd->protocol_identifier) {
791 case 0x00:
792 sprintf(buf+len, "Fibre Channel\n");
793 break;
794 case 0x10:
795 sprintf(buf+len, "Parallel SCSI\n");
796 break;
797 case 0x20:
798 sprintf(buf+len, "SSA\n");
799 break;
800 case 0x30:
801 sprintf(buf+len, "IEEE 1394\n");
802 break;
803 case 0x40:
804 sprintf(buf+len, "SCSI Remote Direct Memory Access"
805 " Protocol\n");
806 break;
807 case 0x50:
808 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
809 break;
810 case 0x60:
811 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
812 break;
813 case 0x70:
814 sprintf(buf+len, "Automation/Drive Interface Transport"
815 " Protocol\n");
816 break;
817 case 0x80:
818 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
819 break;
820 default:
821 sprintf(buf+len, "Unknown 0x%02x\n",
822 vpd->protocol_identifier);
823 break;
826 if (p_buf)
827 strncpy(p_buf, buf, p_buf_len);
828 else
829 pr_debug("%s", buf);
832 void
833 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
836 * Check if the Protocol Identifier Valid (PIV) bit is set..
838 * from spc3r23.pdf section 7.5.1
840 if (page_83[1] & 0x80) {
841 vpd->protocol_identifier = (page_83[0] & 0xf0);
842 vpd->protocol_identifier_set = 1;
843 transport_dump_vpd_proto_id(vpd, NULL, 0);
846 EXPORT_SYMBOL(transport_set_vpd_proto_id);
848 int transport_dump_vpd_assoc(
849 struct t10_vpd *vpd,
850 unsigned char *p_buf,
851 int p_buf_len)
853 unsigned char buf[VPD_TMP_BUF_SIZE];
854 int ret = 0;
855 int len;
857 memset(buf, 0, VPD_TMP_BUF_SIZE);
858 len = sprintf(buf, "T10 VPD Identifier Association: ");
860 switch (vpd->association) {
861 case 0x00:
862 sprintf(buf+len, "addressed logical unit\n");
863 break;
864 case 0x10:
865 sprintf(buf+len, "target port\n");
866 break;
867 case 0x20:
868 sprintf(buf+len, "SCSI target device\n");
869 break;
870 default:
871 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
872 ret = -EINVAL;
873 break;
876 if (p_buf)
877 strncpy(p_buf, buf, p_buf_len);
878 else
879 pr_debug("%s", buf);
881 return ret;
884 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
887 * The VPD identification association..
889 * from spc3r23.pdf Section 7.6.3.1 Table 297
891 vpd->association = (page_83[1] & 0x30);
892 return transport_dump_vpd_assoc(vpd, NULL, 0);
894 EXPORT_SYMBOL(transport_set_vpd_assoc);
896 int transport_dump_vpd_ident_type(
897 struct t10_vpd *vpd,
898 unsigned char *p_buf,
899 int p_buf_len)
901 unsigned char buf[VPD_TMP_BUF_SIZE];
902 int ret = 0;
903 int len;
905 memset(buf, 0, VPD_TMP_BUF_SIZE);
906 len = sprintf(buf, "T10 VPD Identifier Type: ");
908 switch (vpd->device_identifier_type) {
909 case 0x00:
910 sprintf(buf+len, "Vendor specific\n");
911 break;
912 case 0x01:
913 sprintf(buf+len, "T10 Vendor ID based\n");
914 break;
915 case 0x02:
916 sprintf(buf+len, "EUI-64 based\n");
917 break;
918 case 0x03:
919 sprintf(buf+len, "NAA\n");
920 break;
921 case 0x04:
922 sprintf(buf+len, "Relative target port identifier\n");
923 break;
924 case 0x08:
925 sprintf(buf+len, "SCSI name string\n");
926 break;
927 default:
928 sprintf(buf+len, "Unsupported: 0x%02x\n",
929 vpd->device_identifier_type);
930 ret = -EINVAL;
931 break;
934 if (p_buf) {
935 if (p_buf_len < strlen(buf)+1)
936 return -EINVAL;
937 strncpy(p_buf, buf, p_buf_len);
938 } else {
939 pr_debug("%s", buf);
942 return ret;
945 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
948 * The VPD identifier type..
950 * from spc3r23.pdf Section 7.6.3.1 Table 298
952 vpd->device_identifier_type = (page_83[1] & 0x0f);
953 return transport_dump_vpd_ident_type(vpd, NULL, 0);
955 EXPORT_SYMBOL(transport_set_vpd_ident_type);
957 int transport_dump_vpd_ident(
958 struct t10_vpd *vpd,
959 unsigned char *p_buf,
960 int p_buf_len)
962 unsigned char buf[VPD_TMP_BUF_SIZE];
963 int ret = 0;
965 memset(buf, 0, VPD_TMP_BUF_SIZE);
967 switch (vpd->device_identifier_code_set) {
968 case 0x01: /* Binary */
969 snprintf(buf, sizeof(buf),
970 "T10 VPD Binary Device Identifier: %s\n",
971 &vpd->device_identifier[0]);
972 break;
973 case 0x02: /* ASCII */
974 snprintf(buf, sizeof(buf),
975 "T10 VPD ASCII Device Identifier: %s\n",
976 &vpd->device_identifier[0]);
977 break;
978 case 0x03: /* UTF-8 */
979 snprintf(buf, sizeof(buf),
980 "T10 VPD UTF-8 Device Identifier: %s\n",
981 &vpd->device_identifier[0]);
982 break;
983 default:
984 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
985 " 0x%02x", vpd->device_identifier_code_set);
986 ret = -EINVAL;
987 break;
990 if (p_buf)
991 strncpy(p_buf, buf, p_buf_len);
992 else
993 pr_debug("%s", buf);
995 return ret;
999 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1001 static const char hex_str[] = "0123456789abcdef";
1002 int j = 0, i = 4; /* offset to start of the identifier */
1005 * The VPD Code Set (encoding)
1007 * from spc3r23.pdf Section 7.6.3.1 Table 296
1009 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1010 switch (vpd->device_identifier_code_set) {
1011 case 0x01: /* Binary */
1012 vpd->device_identifier[j++] =
1013 hex_str[vpd->device_identifier_type];
1014 while (i < (4 + page_83[3])) {
1015 vpd->device_identifier[j++] =
1016 hex_str[(page_83[i] & 0xf0) >> 4];
1017 vpd->device_identifier[j++] =
1018 hex_str[page_83[i] & 0x0f];
1019 i++;
1021 break;
1022 case 0x02: /* ASCII */
1023 case 0x03: /* UTF-8 */
1024 while (i < (4 + page_83[3]))
1025 vpd->device_identifier[j++] = page_83[i++];
1026 break;
1027 default:
1028 break;
1031 return transport_dump_vpd_ident(vpd, NULL, 0);
1033 EXPORT_SYMBOL(transport_set_vpd_ident);
1035 sense_reason_t
1036 target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
1038 struct se_device *dev = cmd->se_dev;
1040 if (cmd->unknown_data_length) {
1041 cmd->data_length = size;
1042 } else if (size != cmd->data_length) {
1043 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1044 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1045 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
1046 cmd->data_length, size, cmd->t_task_cdb[0]);
1048 if (cmd->data_direction == DMA_TO_DEVICE) {
1049 pr_err("Rejecting underflow/overflow"
1050 " WRITE data\n");
1051 return TCM_INVALID_CDB_FIELD;
1054 * Reject READ_* or WRITE_* with overflow/underflow for
1055 * type SCF_SCSI_DATA_CDB.
1057 if (dev->dev_attrib.block_size != 512) {
1058 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1059 " CDB on non 512-byte sector setup subsystem"
1060 " plugin: %s\n", dev->transport->name);
1061 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1062 return TCM_INVALID_CDB_FIELD;
1065 * For the overflow case keep the existing fabric provided
1066 * ->data_length. Otherwise for the underflow case, reset
1067 * ->data_length to the smaller SCSI expected data transfer
1068 * length.
1070 if (size > cmd->data_length) {
1071 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1072 cmd->residual_count = (size - cmd->data_length);
1073 } else {
1074 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1075 cmd->residual_count = (cmd->data_length - size);
1076 cmd->data_length = size;
1080 return 0;
1085 * Used by fabric modules containing a local struct se_cmd within their
1086 * fabric dependent per I/O descriptor.
1088 void transport_init_se_cmd(
1089 struct se_cmd *cmd,
1090 struct target_core_fabric_ops *tfo,
1091 struct se_session *se_sess,
1092 u32 data_length,
1093 int data_direction,
1094 int task_attr,
1095 unsigned char *sense_buffer)
1097 INIT_LIST_HEAD(&cmd->se_delayed_node);
1098 INIT_LIST_HEAD(&cmd->se_qf_node);
1099 INIT_LIST_HEAD(&cmd->se_cmd_list);
1100 INIT_LIST_HEAD(&cmd->state_list);
1101 init_completion(&cmd->t_transport_stop_comp);
1102 init_completion(&cmd->cmd_wait_comp);
1103 init_completion(&cmd->task_stop_comp);
1104 spin_lock_init(&cmd->t_state_lock);
1105 cmd->transport_state = CMD_T_DEV_ACTIVE;
1107 cmd->se_tfo = tfo;
1108 cmd->se_sess = se_sess;
1109 cmd->data_length = data_length;
1110 cmd->data_direction = data_direction;
1111 cmd->sam_task_attr = task_attr;
1112 cmd->sense_buffer = sense_buffer;
1114 cmd->state_active = false;
1116 EXPORT_SYMBOL(transport_init_se_cmd);
1118 static sense_reason_t
1119 transport_check_alloc_task_attr(struct se_cmd *cmd)
1121 struct se_device *dev = cmd->se_dev;
1124 * Check if SAM Task Attribute emulation is enabled for this
1125 * struct se_device storage object
1127 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1128 return 0;
1130 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1131 pr_debug("SAM Task Attribute ACA"
1132 " emulation is not supported\n");
1133 return TCM_INVALID_CDB_FIELD;
1136 * Used to determine when ORDERED commands should go from
1137 * Dormant to Active status.
1139 cmd->se_ordered_id = atomic_inc_return(&dev->dev_ordered_id);
1140 smp_mb__after_atomic_inc();
1141 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1142 cmd->se_ordered_id, cmd->sam_task_attr,
1143 dev->transport->name);
1144 return 0;
1147 sense_reason_t
1148 target_setup_cmd_from_cdb(struct se_cmd *cmd, unsigned char *cdb)
1150 struct se_device *dev = cmd->se_dev;
1151 sense_reason_t ret;
1154 * Ensure that the received CDB is less than the max (252 + 8) bytes
1155 * for VARIABLE_LENGTH_CMD
1157 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1158 pr_err("Received SCSI CDB with command_size: %d that"
1159 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1160 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1161 return TCM_INVALID_CDB_FIELD;
1164 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1165 * allocate the additional extended CDB buffer now.. Otherwise
1166 * setup the pointer from __t_task_cdb to t_task_cdb.
1168 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1169 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1170 GFP_KERNEL);
1171 if (!cmd->t_task_cdb) {
1172 pr_err("Unable to allocate cmd->t_task_cdb"
1173 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1174 scsi_command_size(cdb),
1175 (unsigned long)sizeof(cmd->__t_task_cdb));
1176 return TCM_OUT_OF_RESOURCES;
1178 } else
1179 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1181 * Copy the original CDB into cmd->
1183 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1185 trace_target_sequencer_start(cmd);
1188 * Check for an existing UNIT ATTENTION condition
1190 ret = target_scsi3_ua_check(cmd);
1191 if (ret)
1192 return ret;
1194 ret = target_alua_state_check(cmd);
1195 if (ret)
1196 return ret;
1198 ret = target_check_reservation(cmd);
1199 if (ret) {
1200 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1201 return ret;
1204 ret = dev->transport->parse_cdb(cmd);
1205 if (ret)
1206 return ret;
1208 ret = transport_check_alloc_task_attr(cmd);
1209 if (ret)
1210 return ret;
1212 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1214 spin_lock(&cmd->se_lun->lun_sep_lock);
1215 if (cmd->se_lun->lun_sep)
1216 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1217 spin_unlock(&cmd->se_lun->lun_sep_lock);
1218 return 0;
1220 EXPORT_SYMBOL(target_setup_cmd_from_cdb);
1223 * Used by fabric module frontends to queue tasks directly.
1224 * Many only be used from process context only
1226 int transport_handle_cdb_direct(
1227 struct se_cmd *cmd)
1229 sense_reason_t ret;
1231 if (!cmd->se_lun) {
1232 dump_stack();
1233 pr_err("cmd->se_lun is NULL\n");
1234 return -EINVAL;
1236 if (in_interrupt()) {
1237 dump_stack();
1238 pr_err("transport_generic_handle_cdb cannot be called"
1239 " from interrupt context\n");
1240 return -EINVAL;
1243 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1244 * outstanding descriptors are handled correctly during shutdown via
1245 * transport_wait_for_tasks()
1247 * Also, we don't take cmd->t_state_lock here as we only expect
1248 * this to be called for initial descriptor submission.
1250 cmd->t_state = TRANSPORT_NEW_CMD;
1251 cmd->transport_state |= CMD_T_ACTIVE;
1254 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1255 * so follow TRANSPORT_NEW_CMD processing thread context usage
1256 * and call transport_generic_request_failure() if necessary..
1258 ret = transport_generic_new_cmd(cmd);
1259 if (ret)
1260 transport_generic_request_failure(cmd, ret);
1261 return 0;
1263 EXPORT_SYMBOL(transport_handle_cdb_direct);
1265 sense_reason_t
1266 transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
1267 u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count)
1269 if (!sgl || !sgl_count)
1270 return 0;
1273 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
1274 * scatterlists already have been set to follow what the fabric
1275 * passes for the original expected data transfer length.
1277 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1278 pr_warn("Rejecting SCSI DATA overflow for fabric using"
1279 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
1280 return TCM_INVALID_CDB_FIELD;
1283 cmd->t_data_sg = sgl;
1284 cmd->t_data_nents = sgl_count;
1286 if (sgl_bidi && sgl_bidi_count) {
1287 cmd->t_bidi_data_sg = sgl_bidi;
1288 cmd->t_bidi_data_nents = sgl_bidi_count;
1290 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1291 return 0;
1295 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1296 * se_cmd + use pre-allocated SGL memory.
1298 * @se_cmd: command descriptor to submit
1299 * @se_sess: associated se_sess for endpoint
1300 * @cdb: pointer to SCSI CDB
1301 * @sense: pointer to SCSI sense buffer
1302 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1303 * @data_length: fabric expected data transfer length
1304 * @task_addr: SAM task attribute
1305 * @data_dir: DMA data direction
1306 * @flags: flags for command submission from target_sc_flags_tables
1307 * @sgl: struct scatterlist memory for unidirectional mapping
1308 * @sgl_count: scatterlist count for unidirectional mapping
1309 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1310 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1311 * @sgl_prot: struct scatterlist memory protection information
1312 * @sgl_prot_count: scatterlist count for protection information
1314 * Returns non zero to signal active I/O shutdown failure. All other
1315 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1316 * but still return zero here.
1318 * This may only be called from process context, and also currently
1319 * assumes internal allocation of fabric payload buffer by target-core.
1321 int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess,
1322 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1323 u32 data_length, int task_attr, int data_dir, int flags,
1324 struct scatterlist *sgl, u32 sgl_count,
1325 struct scatterlist *sgl_bidi, u32 sgl_bidi_count,
1326 struct scatterlist *sgl_prot, u32 sgl_prot_count)
1328 struct se_portal_group *se_tpg;
1329 sense_reason_t rc;
1330 int ret;
1332 se_tpg = se_sess->se_tpg;
1333 BUG_ON(!se_tpg);
1334 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1335 BUG_ON(in_interrupt());
1337 * Initialize se_cmd for target operation. From this point
1338 * exceptions are handled by sending exception status via
1339 * target_core_fabric_ops->queue_status() callback
1341 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1342 data_length, data_dir, task_attr, sense);
1343 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1344 se_cmd->unknown_data_length = 1;
1346 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1347 * se_sess->sess_cmd_list. A second kref_get here is necessary
1348 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1349 * kref_put() to happen during fabric packet acknowledgement.
1351 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1352 if (ret)
1353 return ret;
1355 * Signal bidirectional data payloads to target-core
1357 if (flags & TARGET_SCF_BIDI_OP)
1358 se_cmd->se_cmd_flags |= SCF_BIDI;
1360 * Locate se_lun pointer and attach it to struct se_cmd
1362 rc = transport_lookup_cmd_lun(se_cmd, unpacked_lun);
1363 if (rc) {
1364 transport_send_check_condition_and_sense(se_cmd, rc, 0);
1365 target_put_sess_cmd(se_sess, se_cmd);
1366 return 0;
1369 * Save pointers for SGLs containing protection information,
1370 * if present.
1372 if (sgl_prot_count) {
1373 se_cmd->t_prot_sg = sgl_prot;
1374 se_cmd->t_prot_nents = sgl_prot_count;
1377 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
1378 if (rc != 0) {
1379 transport_generic_request_failure(se_cmd, rc);
1380 return 0;
1383 * When a non zero sgl_count has been passed perform SGL passthrough
1384 * mapping for pre-allocated fabric memory instead of having target
1385 * core perform an internal SGL allocation..
1387 if (sgl_count != 0) {
1388 BUG_ON(!sgl);
1391 * A work-around for tcm_loop as some userspace code via
1392 * scsi-generic do not memset their associated read buffers,
1393 * so go ahead and do that here for type non-data CDBs. Also
1394 * note that this is currently guaranteed to be a single SGL
1395 * for this case by target core in target_setup_cmd_from_cdb()
1396 * -> transport_generic_cmd_sequencer().
1398 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
1399 se_cmd->data_direction == DMA_FROM_DEVICE) {
1400 unsigned char *buf = NULL;
1402 if (sgl)
1403 buf = kmap(sg_page(sgl)) + sgl->offset;
1405 if (buf) {
1406 memset(buf, 0, sgl->length);
1407 kunmap(sg_page(sgl));
1411 rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
1412 sgl_bidi, sgl_bidi_count);
1413 if (rc != 0) {
1414 transport_generic_request_failure(se_cmd, rc);
1415 return 0;
1420 * Check if we need to delay processing because of ALUA
1421 * Active/NonOptimized primary access state..
1423 core_alua_check_nonop_delay(se_cmd);
1425 transport_handle_cdb_direct(se_cmd);
1426 return 0;
1428 EXPORT_SYMBOL(target_submit_cmd_map_sgls);
1431 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1433 * @se_cmd: command descriptor to submit
1434 * @se_sess: associated se_sess for endpoint
1435 * @cdb: pointer to SCSI CDB
1436 * @sense: pointer to SCSI sense buffer
1437 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1438 * @data_length: fabric expected data transfer length
1439 * @task_addr: SAM task attribute
1440 * @data_dir: DMA data direction
1441 * @flags: flags for command submission from target_sc_flags_tables
1443 * Returns non zero to signal active I/O shutdown failure. All other
1444 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1445 * but still return zero here.
1447 * This may only be called from process context, and also currently
1448 * assumes internal allocation of fabric payload buffer by target-core.
1450 * It also assumes interal target core SGL memory allocation.
1452 int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1453 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1454 u32 data_length, int task_attr, int data_dir, int flags)
1456 return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense,
1457 unpacked_lun, data_length, task_attr, data_dir,
1458 flags, NULL, 0, NULL, 0, NULL, 0);
1460 EXPORT_SYMBOL(target_submit_cmd);
1462 static void target_complete_tmr_failure(struct work_struct *work)
1464 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1466 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1467 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1469 transport_cmd_check_stop_to_fabric(se_cmd);
1473 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1474 * for TMR CDBs
1476 * @se_cmd: command descriptor to submit
1477 * @se_sess: associated se_sess for endpoint
1478 * @sense: pointer to SCSI sense buffer
1479 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1480 * @fabric_context: fabric context for TMR req
1481 * @tm_type: Type of TM request
1482 * @gfp: gfp type for caller
1483 * @tag: referenced task tag for TMR_ABORT_TASK
1484 * @flags: submit cmd flags
1486 * Callable from all contexts.
1489 int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1490 unsigned char *sense, u32 unpacked_lun,
1491 void *fabric_tmr_ptr, unsigned char tm_type,
1492 gfp_t gfp, unsigned int tag, int flags)
1494 struct se_portal_group *se_tpg;
1495 int ret;
1497 se_tpg = se_sess->se_tpg;
1498 BUG_ON(!se_tpg);
1500 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1501 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1503 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1504 * allocation failure.
1506 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
1507 if (ret < 0)
1508 return -ENOMEM;
1510 if (tm_type == TMR_ABORT_TASK)
1511 se_cmd->se_tmr_req->ref_task_tag = tag;
1513 /* See target_submit_cmd for commentary */
1514 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1515 if (ret) {
1516 core_tmr_release_req(se_cmd->se_tmr_req);
1517 return ret;
1520 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1521 if (ret) {
1523 * For callback during failure handling, push this work off
1524 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1526 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1527 schedule_work(&se_cmd->work);
1528 return 0;
1530 transport_generic_handle_tmr(se_cmd);
1531 return 0;
1533 EXPORT_SYMBOL(target_submit_tmr);
1536 * If the cmd is active, request it to be stopped and sleep until it
1537 * has completed.
1539 bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
1541 bool was_active = false;
1543 if (cmd->transport_state & CMD_T_BUSY) {
1544 cmd->transport_state |= CMD_T_REQUEST_STOP;
1545 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1547 pr_debug("cmd %p waiting to complete\n", cmd);
1548 wait_for_completion(&cmd->task_stop_comp);
1549 pr_debug("cmd %p stopped successfully\n", cmd);
1551 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1552 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1553 cmd->transport_state &= ~CMD_T_BUSY;
1554 was_active = true;
1557 return was_active;
1561 * Handle SAM-esque emulation for generic transport request failures.
1563 void transport_generic_request_failure(struct se_cmd *cmd,
1564 sense_reason_t sense_reason)
1566 int ret = 0;
1568 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1569 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1570 cmd->t_task_cdb[0]);
1571 pr_debug("-----[ i_state: %d t_state: %d sense_reason: %d\n",
1572 cmd->se_tfo->get_cmd_state(cmd),
1573 cmd->t_state, sense_reason);
1574 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1575 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1576 (cmd->transport_state & CMD_T_STOP) != 0,
1577 (cmd->transport_state & CMD_T_SENT) != 0);
1580 * For SAM Task Attribute emulation for failed struct se_cmd
1582 transport_complete_task_attr(cmd);
1584 * Handle special case for COMPARE_AND_WRITE failure, where the
1585 * callback is expected to drop the per device ->caw_mutex.
1587 if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
1588 cmd->transport_complete_callback)
1589 cmd->transport_complete_callback(cmd);
1591 switch (sense_reason) {
1592 case TCM_NON_EXISTENT_LUN:
1593 case TCM_UNSUPPORTED_SCSI_OPCODE:
1594 case TCM_INVALID_CDB_FIELD:
1595 case TCM_INVALID_PARAMETER_LIST:
1596 case TCM_PARAMETER_LIST_LENGTH_ERROR:
1597 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1598 case TCM_UNKNOWN_MODE_PAGE:
1599 case TCM_WRITE_PROTECTED:
1600 case TCM_ADDRESS_OUT_OF_RANGE:
1601 case TCM_CHECK_CONDITION_ABORT_CMD:
1602 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1603 case TCM_CHECK_CONDITION_NOT_READY:
1604 break;
1605 case TCM_OUT_OF_RESOURCES:
1606 sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1607 break;
1608 case TCM_RESERVATION_CONFLICT:
1610 * No SENSE Data payload for this case, set SCSI Status
1611 * and queue the response to $FABRIC_MOD.
1613 * Uses linux/include/scsi/scsi.h SAM status codes defs
1615 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1617 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1618 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1619 * CONFLICT STATUS.
1621 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1623 if (cmd->se_sess &&
1624 cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2)
1625 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1626 cmd->orig_fe_lun, 0x2C,
1627 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1629 trace_target_cmd_complete(cmd);
1630 ret = cmd->se_tfo-> queue_status(cmd);
1631 if (ret == -EAGAIN || ret == -ENOMEM)
1632 goto queue_full;
1633 goto check_stop;
1634 default:
1635 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1636 cmd->t_task_cdb[0], sense_reason);
1637 sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1638 break;
1641 ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0);
1642 if (ret == -EAGAIN || ret == -ENOMEM)
1643 goto queue_full;
1645 check_stop:
1646 transport_lun_remove_cmd(cmd);
1647 if (!transport_cmd_check_stop_to_fabric(cmd))
1649 return;
1651 queue_full:
1652 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1653 transport_handle_queue_full(cmd, cmd->se_dev);
1655 EXPORT_SYMBOL(transport_generic_request_failure);
1657 void __target_execute_cmd(struct se_cmd *cmd)
1659 sense_reason_t ret;
1661 if (cmd->execute_cmd) {
1662 ret = cmd->execute_cmd(cmd);
1663 if (ret) {
1664 spin_lock_irq(&cmd->t_state_lock);
1665 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1666 spin_unlock_irq(&cmd->t_state_lock);
1668 transport_generic_request_failure(cmd, ret);
1673 static bool target_handle_task_attr(struct se_cmd *cmd)
1675 struct se_device *dev = cmd->se_dev;
1677 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1678 return false;
1681 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1682 * to allow the passed struct se_cmd list of tasks to the front of the list.
1684 switch (cmd->sam_task_attr) {
1685 case MSG_HEAD_TAG:
1686 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1687 "se_ordered_id: %u\n",
1688 cmd->t_task_cdb[0], cmd->se_ordered_id);
1689 return false;
1690 case MSG_ORDERED_TAG:
1691 atomic_inc(&dev->dev_ordered_sync);
1692 smp_mb__after_atomic_inc();
1694 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1695 " se_ordered_id: %u\n",
1696 cmd->t_task_cdb[0], cmd->se_ordered_id);
1699 * Execute an ORDERED command if no other older commands
1700 * exist that need to be completed first.
1702 if (!atomic_read(&dev->simple_cmds))
1703 return false;
1704 break;
1705 default:
1707 * For SIMPLE and UNTAGGED Task Attribute commands
1709 atomic_inc(&dev->simple_cmds);
1710 smp_mb__after_atomic_inc();
1711 break;
1714 if (atomic_read(&dev->dev_ordered_sync) == 0)
1715 return false;
1717 spin_lock(&dev->delayed_cmd_lock);
1718 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
1719 spin_unlock(&dev->delayed_cmd_lock);
1721 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1722 " delayed CMD list, se_ordered_id: %u\n",
1723 cmd->t_task_cdb[0], cmd->sam_task_attr,
1724 cmd->se_ordered_id);
1725 return true;
1728 void target_execute_cmd(struct se_cmd *cmd)
1731 * If the received CDB has aleady been aborted stop processing it here.
1733 if (transport_check_aborted_status(cmd, 1))
1734 return;
1737 * Determine if frontend context caller is requesting the stopping of
1738 * this command for frontend exceptions.
1740 spin_lock_irq(&cmd->t_state_lock);
1741 if (cmd->transport_state & CMD_T_STOP) {
1742 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1743 __func__, __LINE__,
1744 cmd->se_tfo->get_task_tag(cmd));
1746 spin_unlock_irq(&cmd->t_state_lock);
1747 complete(&cmd->t_transport_stop_comp);
1748 return;
1751 cmd->t_state = TRANSPORT_PROCESSING;
1752 cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT;
1753 spin_unlock_irq(&cmd->t_state_lock);
1755 if (target_handle_task_attr(cmd)) {
1756 spin_lock_irq(&cmd->t_state_lock);
1757 cmd->transport_state &= ~CMD_T_BUSY|CMD_T_SENT;
1758 spin_unlock_irq(&cmd->t_state_lock);
1759 return;
1762 __target_execute_cmd(cmd);
1764 EXPORT_SYMBOL(target_execute_cmd);
1767 * Process all commands up to the last received ORDERED task attribute which
1768 * requires another blocking boundary
1770 static void target_restart_delayed_cmds(struct se_device *dev)
1772 for (;;) {
1773 struct se_cmd *cmd;
1775 spin_lock(&dev->delayed_cmd_lock);
1776 if (list_empty(&dev->delayed_cmd_list)) {
1777 spin_unlock(&dev->delayed_cmd_lock);
1778 break;
1781 cmd = list_entry(dev->delayed_cmd_list.next,
1782 struct se_cmd, se_delayed_node);
1783 list_del(&cmd->se_delayed_node);
1784 spin_unlock(&dev->delayed_cmd_lock);
1786 __target_execute_cmd(cmd);
1788 if (cmd->sam_task_attr == MSG_ORDERED_TAG)
1789 break;
1794 * Called from I/O completion to determine which dormant/delayed
1795 * and ordered cmds need to have their tasks added to the execution queue.
1797 static void transport_complete_task_attr(struct se_cmd *cmd)
1799 struct se_device *dev = cmd->se_dev;
1801 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1802 return;
1804 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
1805 atomic_dec(&dev->simple_cmds);
1806 smp_mb__after_atomic_dec();
1807 dev->dev_cur_ordered_id++;
1808 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
1809 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
1810 cmd->se_ordered_id);
1811 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
1812 dev->dev_cur_ordered_id++;
1813 pr_debug("Incremented dev_cur_ordered_id: %u for"
1814 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
1815 cmd->se_ordered_id);
1816 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
1817 atomic_dec(&dev->dev_ordered_sync);
1818 smp_mb__after_atomic_dec();
1820 dev->dev_cur_ordered_id++;
1821 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
1822 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
1825 target_restart_delayed_cmds(dev);
1828 static void transport_complete_qf(struct se_cmd *cmd)
1830 int ret = 0;
1832 transport_complete_task_attr(cmd);
1834 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1835 trace_target_cmd_complete(cmd);
1836 ret = cmd->se_tfo->queue_status(cmd);
1837 if (ret)
1838 goto out;
1841 switch (cmd->data_direction) {
1842 case DMA_FROM_DEVICE:
1843 trace_target_cmd_complete(cmd);
1844 ret = cmd->se_tfo->queue_data_in(cmd);
1845 break;
1846 case DMA_TO_DEVICE:
1847 if (cmd->se_cmd_flags & SCF_BIDI) {
1848 ret = cmd->se_tfo->queue_data_in(cmd);
1849 if (ret < 0)
1850 break;
1852 /* Fall through for DMA_TO_DEVICE */
1853 case DMA_NONE:
1854 trace_target_cmd_complete(cmd);
1855 ret = cmd->se_tfo->queue_status(cmd);
1856 break;
1857 default:
1858 break;
1861 out:
1862 if (ret < 0) {
1863 transport_handle_queue_full(cmd, cmd->se_dev);
1864 return;
1866 transport_lun_remove_cmd(cmd);
1867 transport_cmd_check_stop_to_fabric(cmd);
1870 static void transport_handle_queue_full(
1871 struct se_cmd *cmd,
1872 struct se_device *dev)
1874 spin_lock_irq(&dev->qf_cmd_lock);
1875 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
1876 atomic_inc(&dev->dev_qf_count);
1877 smp_mb__after_atomic_inc();
1878 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
1880 schedule_work(&cmd->se_dev->qf_work_queue);
1883 static void target_complete_ok_work(struct work_struct *work)
1885 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
1886 int ret;
1889 * Check if we need to move delayed/dormant tasks from cmds on the
1890 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1891 * Attribute.
1893 transport_complete_task_attr(cmd);
1896 * Check to schedule QUEUE_FULL work, or execute an existing
1897 * cmd->transport_qf_callback()
1899 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
1900 schedule_work(&cmd->se_dev->qf_work_queue);
1903 * Check if we need to send a sense buffer from
1904 * the struct se_cmd in question.
1906 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1907 WARN_ON(!cmd->scsi_status);
1908 ret = transport_send_check_condition_and_sense(
1909 cmd, 0, 1);
1910 if (ret == -EAGAIN || ret == -ENOMEM)
1911 goto queue_full;
1913 transport_lun_remove_cmd(cmd);
1914 transport_cmd_check_stop_to_fabric(cmd);
1915 return;
1918 * Check for a callback, used by amongst other things
1919 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
1921 if (cmd->transport_complete_callback) {
1922 sense_reason_t rc;
1924 rc = cmd->transport_complete_callback(cmd);
1925 if (!rc && !(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE_POST)) {
1926 return;
1927 } else if (rc) {
1928 ret = transport_send_check_condition_and_sense(cmd,
1929 rc, 0);
1930 if (ret == -EAGAIN || ret == -ENOMEM)
1931 goto queue_full;
1933 transport_lun_remove_cmd(cmd);
1934 transport_cmd_check_stop_to_fabric(cmd);
1935 return;
1939 switch (cmd->data_direction) {
1940 case DMA_FROM_DEVICE:
1941 spin_lock(&cmd->se_lun->lun_sep_lock);
1942 if (cmd->se_lun->lun_sep) {
1943 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
1944 cmd->data_length;
1946 spin_unlock(&cmd->se_lun->lun_sep_lock);
1948 trace_target_cmd_complete(cmd);
1949 ret = cmd->se_tfo->queue_data_in(cmd);
1950 if (ret == -EAGAIN || ret == -ENOMEM)
1951 goto queue_full;
1952 break;
1953 case DMA_TO_DEVICE:
1954 spin_lock(&cmd->se_lun->lun_sep_lock);
1955 if (cmd->se_lun->lun_sep) {
1956 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
1957 cmd->data_length;
1959 spin_unlock(&cmd->se_lun->lun_sep_lock);
1961 * Check if we need to send READ payload for BIDI-COMMAND
1963 if (cmd->se_cmd_flags & SCF_BIDI) {
1964 spin_lock(&cmd->se_lun->lun_sep_lock);
1965 if (cmd->se_lun->lun_sep) {
1966 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
1967 cmd->data_length;
1969 spin_unlock(&cmd->se_lun->lun_sep_lock);
1970 ret = cmd->se_tfo->queue_data_in(cmd);
1971 if (ret == -EAGAIN || ret == -ENOMEM)
1972 goto queue_full;
1973 break;
1975 /* Fall through for DMA_TO_DEVICE */
1976 case DMA_NONE:
1977 trace_target_cmd_complete(cmd);
1978 ret = cmd->se_tfo->queue_status(cmd);
1979 if (ret == -EAGAIN || ret == -ENOMEM)
1980 goto queue_full;
1981 break;
1982 default:
1983 break;
1986 transport_lun_remove_cmd(cmd);
1987 transport_cmd_check_stop_to_fabric(cmd);
1988 return;
1990 queue_full:
1991 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
1992 " data_direction: %d\n", cmd, cmd->data_direction);
1993 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1994 transport_handle_queue_full(cmd, cmd->se_dev);
1997 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
1999 struct scatterlist *sg;
2000 int count;
2002 for_each_sg(sgl, sg, nents, count)
2003 __free_page(sg_page(sg));
2005 kfree(sgl);
2008 static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
2011 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
2012 * emulation, and free + reset pointers if necessary..
2014 if (!cmd->t_data_sg_orig)
2015 return;
2017 kfree(cmd->t_data_sg);
2018 cmd->t_data_sg = cmd->t_data_sg_orig;
2019 cmd->t_data_sg_orig = NULL;
2020 cmd->t_data_nents = cmd->t_data_nents_orig;
2021 cmd->t_data_nents_orig = 0;
2024 static inline void transport_free_pages(struct se_cmd *cmd)
2026 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) {
2027 transport_reset_sgl_orig(cmd);
2028 return;
2030 transport_reset_sgl_orig(cmd);
2032 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
2033 cmd->t_data_sg = NULL;
2034 cmd->t_data_nents = 0;
2036 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
2037 cmd->t_bidi_data_sg = NULL;
2038 cmd->t_bidi_data_nents = 0;
2042 * transport_release_cmd - free a command
2043 * @cmd: command to free
2045 * This routine unconditionally frees a command, and reference counting
2046 * or list removal must be done in the caller.
2048 static int transport_release_cmd(struct se_cmd *cmd)
2050 BUG_ON(!cmd->se_tfo);
2052 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
2053 core_tmr_release_req(cmd->se_tmr_req);
2054 if (cmd->t_task_cdb != cmd->__t_task_cdb)
2055 kfree(cmd->t_task_cdb);
2057 * If this cmd has been setup with target_get_sess_cmd(), drop
2058 * the kref and call ->release_cmd() in kref callback.
2060 return target_put_sess_cmd(cmd->se_sess, cmd);
2064 * transport_put_cmd - release a reference to a command
2065 * @cmd: command to release
2067 * This routine releases our reference to the command and frees it if possible.
2069 static int transport_put_cmd(struct se_cmd *cmd)
2071 transport_free_pages(cmd);
2072 return transport_release_cmd(cmd);
2075 void *transport_kmap_data_sg(struct se_cmd *cmd)
2077 struct scatterlist *sg = cmd->t_data_sg;
2078 struct page **pages;
2079 int i;
2082 * We need to take into account a possible offset here for fabrics like
2083 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2084 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
2086 if (!cmd->t_data_nents)
2087 return NULL;
2089 BUG_ON(!sg);
2090 if (cmd->t_data_nents == 1)
2091 return kmap(sg_page(sg)) + sg->offset;
2093 /* >1 page. use vmap */
2094 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
2095 if (!pages)
2096 return NULL;
2098 /* convert sg[] to pages[] */
2099 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
2100 pages[i] = sg_page(sg);
2103 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
2104 kfree(pages);
2105 if (!cmd->t_data_vmap)
2106 return NULL;
2108 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
2110 EXPORT_SYMBOL(transport_kmap_data_sg);
2112 void transport_kunmap_data_sg(struct se_cmd *cmd)
2114 if (!cmd->t_data_nents) {
2115 return;
2116 } else if (cmd->t_data_nents == 1) {
2117 kunmap(sg_page(cmd->t_data_sg));
2118 return;
2121 vunmap(cmd->t_data_vmap);
2122 cmd->t_data_vmap = NULL;
2124 EXPORT_SYMBOL(transport_kunmap_data_sg);
2127 target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
2128 bool zero_page)
2130 struct scatterlist *sg;
2131 struct page *page;
2132 gfp_t zero_flag = (zero_page) ? __GFP_ZERO : 0;
2133 unsigned int nent;
2134 int i = 0;
2136 nent = DIV_ROUND_UP(length, PAGE_SIZE);
2137 sg = kmalloc(sizeof(struct scatterlist) * nent, GFP_KERNEL);
2138 if (!sg)
2139 return -ENOMEM;
2141 sg_init_table(sg, nent);
2143 while (length) {
2144 u32 page_len = min_t(u32, length, PAGE_SIZE);
2145 page = alloc_page(GFP_KERNEL | zero_flag);
2146 if (!page)
2147 goto out;
2149 sg_set_page(&sg[i], page, page_len, 0);
2150 length -= page_len;
2151 i++;
2153 *sgl = sg;
2154 *nents = nent;
2155 return 0;
2157 out:
2158 while (i > 0) {
2159 i--;
2160 __free_page(sg_page(&sg[i]));
2162 kfree(sg);
2163 return -ENOMEM;
2167 * Allocate any required resources to execute the command. For writes we
2168 * might not have the payload yet, so notify the fabric via a call to
2169 * ->write_pending instead. Otherwise place it on the execution queue.
2171 sense_reason_t
2172 transport_generic_new_cmd(struct se_cmd *cmd)
2174 int ret = 0;
2177 * Determine is the TCM fabric module has already allocated physical
2178 * memory, and is directly calling transport_generic_map_mem_to_cmd()
2179 * beforehand.
2181 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2182 cmd->data_length) {
2183 bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB);
2185 if ((cmd->se_cmd_flags & SCF_BIDI) ||
2186 (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) {
2187 u32 bidi_length;
2189 if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)
2190 bidi_length = cmd->t_task_nolb *
2191 cmd->se_dev->dev_attrib.block_size;
2192 else
2193 bidi_length = cmd->data_length;
2195 ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
2196 &cmd->t_bidi_data_nents,
2197 bidi_length, zero_flag);
2198 if (ret < 0)
2199 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2202 ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
2203 cmd->data_length, zero_flag);
2204 if (ret < 0)
2205 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2208 * If this command is not a write we can execute it right here,
2209 * for write buffers we need to notify the fabric driver first
2210 * and let it call back once the write buffers are ready.
2212 target_add_to_state_list(cmd);
2213 if (cmd->data_direction != DMA_TO_DEVICE) {
2214 target_execute_cmd(cmd);
2215 return 0;
2217 transport_cmd_check_stop(cmd, false, true);
2219 ret = cmd->se_tfo->write_pending(cmd);
2220 if (ret == -EAGAIN || ret == -ENOMEM)
2221 goto queue_full;
2223 /* fabric drivers should only return -EAGAIN or -ENOMEM as error */
2224 WARN_ON(ret);
2226 return (!ret) ? 0 : TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2228 queue_full:
2229 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
2230 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
2231 transport_handle_queue_full(cmd, cmd->se_dev);
2232 return 0;
2234 EXPORT_SYMBOL(transport_generic_new_cmd);
2236 static void transport_write_pending_qf(struct se_cmd *cmd)
2238 int ret;
2240 ret = cmd->se_tfo->write_pending(cmd);
2241 if (ret == -EAGAIN || ret == -ENOMEM) {
2242 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2243 cmd);
2244 transport_handle_queue_full(cmd, cmd->se_dev);
2248 int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
2250 unsigned long flags;
2251 int ret = 0;
2253 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
2254 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
2255 transport_wait_for_tasks(cmd);
2257 ret = transport_release_cmd(cmd);
2258 } else {
2259 if (wait_for_tasks)
2260 transport_wait_for_tasks(cmd);
2262 * Handle WRITE failure case where transport_generic_new_cmd()
2263 * has already added se_cmd to state_list, but fabric has
2264 * failed command before I/O submission.
2266 if (cmd->state_active) {
2267 spin_lock_irqsave(&cmd->t_state_lock, flags);
2268 target_remove_from_state_list(cmd);
2269 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2272 if (cmd->se_lun)
2273 transport_lun_remove_cmd(cmd);
2275 ret = transport_put_cmd(cmd);
2277 return ret;
2279 EXPORT_SYMBOL(transport_generic_free_cmd);
2281 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
2282 * @se_sess: session to reference
2283 * @se_cmd: command descriptor to add
2284 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2286 int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
2287 bool ack_kref)
2289 unsigned long flags;
2290 int ret = 0;
2292 kref_init(&se_cmd->cmd_kref);
2294 * Add a second kref if the fabric caller is expecting to handle
2295 * fabric acknowledgement that requires two target_put_sess_cmd()
2296 * invocations before se_cmd descriptor release.
2298 if (ack_kref == true) {
2299 kref_get(&se_cmd->cmd_kref);
2300 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
2303 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2304 if (se_sess->sess_tearing_down) {
2305 ret = -ESHUTDOWN;
2306 goto out;
2308 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
2309 out:
2310 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2311 return ret;
2313 EXPORT_SYMBOL(target_get_sess_cmd);
2315 static void target_release_cmd_kref(struct kref *kref)
2317 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
2318 struct se_session *se_sess = se_cmd->se_sess;
2320 if (list_empty(&se_cmd->se_cmd_list)) {
2321 spin_unlock(&se_sess->sess_cmd_lock);
2322 se_cmd->se_tfo->release_cmd(se_cmd);
2323 return;
2325 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
2326 spin_unlock(&se_sess->sess_cmd_lock);
2327 complete(&se_cmd->cmd_wait_comp);
2328 return;
2330 list_del(&se_cmd->se_cmd_list);
2331 spin_unlock(&se_sess->sess_cmd_lock);
2333 se_cmd->se_tfo->release_cmd(se_cmd);
2336 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2337 * @se_sess: session to reference
2338 * @se_cmd: command descriptor to drop
2340 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
2342 return kref_put_spinlock_irqsave(&se_cmd->cmd_kref, target_release_cmd_kref,
2343 &se_sess->sess_cmd_lock);
2345 EXPORT_SYMBOL(target_put_sess_cmd);
2347 /* target_sess_cmd_list_set_waiting - Flag all commands in
2348 * sess_cmd_list to complete cmd_wait_comp. Set
2349 * sess_tearing_down so no more commands are queued.
2350 * @se_sess: session to flag
2352 void target_sess_cmd_list_set_waiting(struct se_session *se_sess)
2354 struct se_cmd *se_cmd;
2355 unsigned long flags;
2357 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2358 if (se_sess->sess_tearing_down) {
2359 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2360 return;
2362 se_sess->sess_tearing_down = 1;
2363 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
2365 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
2366 se_cmd->cmd_wait_set = 1;
2368 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2370 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting);
2372 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
2373 * @se_sess: session to wait for active I/O
2375 void target_wait_for_sess_cmds(struct se_session *se_sess)
2377 struct se_cmd *se_cmd, *tmp_cmd;
2378 unsigned long flags;
2380 list_for_each_entry_safe(se_cmd, tmp_cmd,
2381 &se_sess->sess_wait_list, se_cmd_list) {
2382 list_del(&se_cmd->se_cmd_list);
2384 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2385 " %d\n", se_cmd, se_cmd->t_state,
2386 se_cmd->se_tfo->get_cmd_state(se_cmd));
2388 wait_for_completion(&se_cmd->cmd_wait_comp);
2389 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2390 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2391 se_cmd->se_tfo->get_cmd_state(se_cmd));
2393 se_cmd->se_tfo->release_cmd(se_cmd);
2396 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2397 WARN_ON(!list_empty(&se_sess->sess_cmd_list));
2398 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2401 EXPORT_SYMBOL(target_wait_for_sess_cmds);
2403 static int transport_clear_lun_ref_thread(void *p)
2405 struct se_lun *lun = p;
2407 percpu_ref_kill(&lun->lun_ref);
2409 wait_for_completion(&lun->lun_ref_comp);
2410 complete(&lun->lun_shutdown_comp);
2412 return 0;
2415 int transport_clear_lun_ref(struct se_lun *lun)
2417 struct task_struct *kt;
2419 kt = kthread_run(transport_clear_lun_ref_thread, lun,
2420 "tcm_cl_%u", lun->unpacked_lun);
2421 if (IS_ERR(kt)) {
2422 pr_err("Unable to start clear_lun thread\n");
2423 return PTR_ERR(kt);
2425 wait_for_completion(&lun->lun_shutdown_comp);
2427 return 0;
2431 * transport_wait_for_tasks - wait for completion to occur
2432 * @cmd: command to wait
2434 * Called from frontend fabric context to wait for storage engine
2435 * to pause and/or release frontend generated struct se_cmd.
2437 bool transport_wait_for_tasks(struct se_cmd *cmd)
2439 unsigned long flags;
2441 spin_lock_irqsave(&cmd->t_state_lock, flags);
2442 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2443 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
2444 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2445 return false;
2448 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2449 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
2450 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2451 return false;
2454 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
2455 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2456 return false;
2459 cmd->transport_state |= CMD_T_STOP;
2461 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
2462 " i_state: %d, t_state: %d, CMD_T_STOP\n",
2463 cmd, cmd->se_tfo->get_task_tag(cmd),
2464 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
2466 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2468 wait_for_completion(&cmd->t_transport_stop_comp);
2470 spin_lock_irqsave(&cmd->t_state_lock, flags);
2471 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
2473 pr_debug("wait_for_tasks: Stopped wait_for_completion("
2474 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
2475 cmd->se_tfo->get_task_tag(cmd));
2477 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2479 return true;
2481 EXPORT_SYMBOL(transport_wait_for_tasks);
2483 static int transport_get_sense_codes(
2484 struct se_cmd *cmd,
2485 u8 *asc,
2486 u8 *ascq)
2488 *asc = cmd->scsi_asc;
2489 *ascq = cmd->scsi_ascq;
2491 return 0;
2494 static
2495 void transport_err_sector_info(unsigned char *buffer, sector_t bad_sector)
2497 /* Place failed LBA in sense data information descriptor 0. */
2498 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 0xc;
2499 buffer[SPC_DESC_TYPE_OFFSET] = 0; /* Information */
2500 buffer[SPC_ADDITIONAL_DESC_LEN_OFFSET] = 0xa;
2501 buffer[SPC_VALIDITY_OFFSET] = 0x80;
2503 /* Descriptor Information: failing sector */
2504 put_unaligned_be64(bad_sector, &buffer[12]);
2508 transport_send_check_condition_and_sense(struct se_cmd *cmd,
2509 sense_reason_t reason, int from_transport)
2511 unsigned char *buffer = cmd->sense_buffer;
2512 unsigned long flags;
2513 u8 asc = 0, ascq = 0;
2515 spin_lock_irqsave(&cmd->t_state_lock, flags);
2516 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2517 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2518 return 0;
2520 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
2521 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2523 if (!reason && from_transport)
2524 goto after_reason;
2526 if (!from_transport)
2527 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
2530 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2531 * SENSE KEY values from include/scsi/scsi.h
2533 switch (reason) {
2534 case TCM_NO_SENSE:
2535 /* CURRENT ERROR */
2536 buffer[0] = 0x70;
2537 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2538 /* Not Ready */
2539 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
2540 /* NO ADDITIONAL SENSE INFORMATION */
2541 buffer[SPC_ASC_KEY_OFFSET] = 0;
2542 buffer[SPC_ASCQ_KEY_OFFSET] = 0;
2543 break;
2544 case TCM_NON_EXISTENT_LUN:
2545 /* CURRENT ERROR */
2546 buffer[0] = 0x70;
2547 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2548 /* ILLEGAL REQUEST */
2549 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2550 /* LOGICAL UNIT NOT SUPPORTED */
2551 buffer[SPC_ASC_KEY_OFFSET] = 0x25;
2552 break;
2553 case TCM_UNSUPPORTED_SCSI_OPCODE:
2554 case TCM_SECTOR_COUNT_TOO_MANY:
2555 /* CURRENT ERROR */
2556 buffer[0] = 0x70;
2557 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2558 /* ILLEGAL REQUEST */
2559 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2560 /* INVALID COMMAND OPERATION CODE */
2561 buffer[SPC_ASC_KEY_OFFSET] = 0x20;
2562 break;
2563 case TCM_UNKNOWN_MODE_PAGE:
2564 /* CURRENT ERROR */
2565 buffer[0] = 0x70;
2566 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2567 /* ILLEGAL REQUEST */
2568 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2569 /* INVALID FIELD IN CDB */
2570 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
2571 break;
2572 case TCM_CHECK_CONDITION_ABORT_CMD:
2573 /* CURRENT ERROR */
2574 buffer[0] = 0x70;
2575 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2576 /* ABORTED COMMAND */
2577 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2578 /* BUS DEVICE RESET FUNCTION OCCURRED */
2579 buffer[SPC_ASC_KEY_OFFSET] = 0x29;
2580 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03;
2581 break;
2582 case TCM_INCORRECT_AMOUNT_OF_DATA:
2583 /* CURRENT ERROR */
2584 buffer[0] = 0x70;
2585 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2586 /* ABORTED COMMAND */
2587 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2588 /* WRITE ERROR */
2589 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
2590 /* NOT ENOUGH UNSOLICITED DATA */
2591 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0d;
2592 break;
2593 case TCM_INVALID_CDB_FIELD:
2594 /* CURRENT ERROR */
2595 buffer[0] = 0x70;
2596 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2597 /* ILLEGAL REQUEST */
2598 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2599 /* INVALID FIELD IN CDB */
2600 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
2601 break;
2602 case TCM_INVALID_PARAMETER_LIST:
2603 /* CURRENT ERROR */
2604 buffer[0] = 0x70;
2605 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2606 /* ILLEGAL REQUEST */
2607 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2608 /* INVALID FIELD IN PARAMETER LIST */
2609 buffer[SPC_ASC_KEY_OFFSET] = 0x26;
2610 break;
2611 case TCM_PARAMETER_LIST_LENGTH_ERROR:
2612 /* CURRENT ERROR */
2613 buffer[0] = 0x70;
2614 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2615 /* ILLEGAL REQUEST */
2616 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2617 /* PARAMETER LIST LENGTH ERROR */
2618 buffer[SPC_ASC_KEY_OFFSET] = 0x1a;
2619 break;
2620 case TCM_UNEXPECTED_UNSOLICITED_DATA:
2621 /* CURRENT ERROR */
2622 buffer[0] = 0x70;
2623 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2624 /* ABORTED COMMAND */
2625 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2626 /* WRITE ERROR */
2627 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
2628 /* UNEXPECTED_UNSOLICITED_DATA */
2629 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0c;
2630 break;
2631 case TCM_SERVICE_CRC_ERROR:
2632 /* CURRENT ERROR */
2633 buffer[0] = 0x70;
2634 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2635 /* ABORTED COMMAND */
2636 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2637 /* PROTOCOL SERVICE CRC ERROR */
2638 buffer[SPC_ASC_KEY_OFFSET] = 0x47;
2639 /* N/A */
2640 buffer[SPC_ASCQ_KEY_OFFSET] = 0x05;
2641 break;
2642 case TCM_SNACK_REJECTED:
2643 /* CURRENT ERROR */
2644 buffer[0] = 0x70;
2645 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2646 /* ABORTED COMMAND */
2647 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2648 /* READ ERROR */
2649 buffer[SPC_ASC_KEY_OFFSET] = 0x11;
2650 /* FAILED RETRANSMISSION REQUEST */
2651 buffer[SPC_ASCQ_KEY_OFFSET] = 0x13;
2652 break;
2653 case TCM_WRITE_PROTECTED:
2654 /* CURRENT ERROR */
2655 buffer[0] = 0x70;
2656 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2657 /* DATA PROTECT */
2658 buffer[SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
2659 /* WRITE PROTECTED */
2660 buffer[SPC_ASC_KEY_OFFSET] = 0x27;
2661 break;
2662 case TCM_ADDRESS_OUT_OF_RANGE:
2663 /* CURRENT ERROR */
2664 buffer[0] = 0x70;
2665 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2666 /* ILLEGAL REQUEST */
2667 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2668 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2669 buffer[SPC_ASC_KEY_OFFSET] = 0x21;
2670 break;
2671 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
2672 /* CURRENT ERROR */
2673 buffer[0] = 0x70;
2674 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2675 /* UNIT ATTENTION */
2676 buffer[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
2677 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
2678 buffer[SPC_ASC_KEY_OFFSET] = asc;
2679 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
2680 break;
2681 case TCM_CHECK_CONDITION_NOT_READY:
2682 /* CURRENT ERROR */
2683 buffer[0] = 0x70;
2684 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2685 /* Not Ready */
2686 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
2687 transport_get_sense_codes(cmd, &asc, &ascq);
2688 buffer[SPC_ASC_KEY_OFFSET] = asc;
2689 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
2690 break;
2691 case TCM_MISCOMPARE_VERIFY:
2692 /* CURRENT ERROR */
2693 buffer[0] = 0x70;
2694 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2695 buffer[SPC_SENSE_KEY_OFFSET] = MISCOMPARE;
2696 /* MISCOMPARE DURING VERIFY OPERATION */
2697 buffer[SPC_ASC_KEY_OFFSET] = 0x1d;
2698 buffer[SPC_ASCQ_KEY_OFFSET] = 0x00;
2699 break;
2700 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED:
2701 /* CURRENT ERROR */
2702 buffer[0] = 0x70;
2703 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2704 /* ILLEGAL REQUEST */
2705 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2706 /* LOGICAL BLOCK GUARD CHECK FAILED */
2707 buffer[SPC_ASC_KEY_OFFSET] = 0x10;
2708 buffer[SPC_ASCQ_KEY_OFFSET] = 0x01;
2709 transport_err_sector_info(buffer, cmd->bad_sector);
2710 break;
2711 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED:
2712 /* CURRENT ERROR */
2713 buffer[0] = 0x70;
2714 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2715 /* ILLEGAL REQUEST */
2716 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2717 /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */
2718 buffer[SPC_ASC_KEY_OFFSET] = 0x10;
2719 buffer[SPC_ASCQ_KEY_OFFSET] = 0x02;
2720 transport_err_sector_info(buffer, cmd->bad_sector);
2721 break;
2722 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED:
2723 /* CURRENT ERROR */
2724 buffer[0] = 0x70;
2725 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2726 /* ILLEGAL REQUEST */
2727 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2728 /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */
2729 buffer[SPC_ASC_KEY_OFFSET] = 0x10;
2730 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03;
2731 transport_err_sector_info(buffer, cmd->bad_sector);
2732 break;
2733 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
2734 default:
2735 /* CURRENT ERROR */
2736 buffer[0] = 0x70;
2737 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2739 * Returning ILLEGAL REQUEST would cause immediate IO errors on
2740 * Solaris initiators. Returning NOT READY instead means the
2741 * operations will be retried a finite number of times and we
2742 * can survive intermittent errors.
2744 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
2745 /* LOGICAL UNIT COMMUNICATION FAILURE */
2746 buffer[SPC_ASC_KEY_OFFSET] = 0x08;
2747 break;
2750 * This code uses linux/include/scsi/scsi.h SAM status codes!
2752 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
2754 * Automatically padded, this value is encoded in the fabric's
2755 * data_length response PDU containing the SCSI defined sense data.
2757 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
2759 after_reason:
2760 trace_target_cmd_complete(cmd);
2761 return cmd->se_tfo->queue_status(cmd);
2763 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
2765 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
2767 if (!(cmd->transport_state & CMD_T_ABORTED))
2768 return 0;
2770 if (!send_status || (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
2771 return 1;
2773 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB: 0x%02x ITT: 0x%08x\n",
2774 cmd->t_task_cdb[0], cmd->se_tfo->get_task_tag(cmd));
2776 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
2777 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
2778 trace_target_cmd_complete(cmd);
2779 cmd->se_tfo->queue_status(cmd);
2781 return 1;
2783 EXPORT_SYMBOL(transport_check_aborted_status);
2785 void transport_send_task_abort(struct se_cmd *cmd)
2787 unsigned long flags;
2789 spin_lock_irqsave(&cmd->t_state_lock, flags);
2790 if (cmd->se_cmd_flags & (SCF_SENT_CHECK_CONDITION | SCF_SENT_DELAYED_TAS)) {
2791 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2792 return;
2794 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2797 * If there are still expected incoming fabric WRITEs, we wait
2798 * until until they have completed before sending a TASK_ABORTED
2799 * response. This response with TASK_ABORTED status will be
2800 * queued back to fabric module by transport_check_aborted_status().
2802 if (cmd->data_direction == DMA_TO_DEVICE) {
2803 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
2804 cmd->transport_state |= CMD_T_ABORTED;
2805 smp_mb__after_atomic_inc();
2806 return;
2809 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
2811 transport_lun_remove_cmd(cmd);
2813 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
2814 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
2815 cmd->se_tfo->get_task_tag(cmd));
2817 trace_target_cmd_complete(cmd);
2818 cmd->se_tfo->queue_status(cmd);
2821 static void target_tmr_work(struct work_struct *work)
2823 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
2824 struct se_device *dev = cmd->se_dev;
2825 struct se_tmr_req *tmr = cmd->se_tmr_req;
2826 int ret;
2828 switch (tmr->function) {
2829 case TMR_ABORT_TASK:
2830 core_tmr_abort_task(dev, tmr, cmd->se_sess);
2831 break;
2832 case TMR_ABORT_TASK_SET:
2833 case TMR_CLEAR_ACA:
2834 case TMR_CLEAR_TASK_SET:
2835 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
2836 break;
2837 case TMR_LUN_RESET:
2838 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
2839 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
2840 TMR_FUNCTION_REJECTED;
2841 break;
2842 case TMR_TARGET_WARM_RESET:
2843 tmr->response = TMR_FUNCTION_REJECTED;
2844 break;
2845 case TMR_TARGET_COLD_RESET:
2846 tmr->response = TMR_FUNCTION_REJECTED;
2847 break;
2848 default:
2849 pr_err("Uknown TMR function: 0x%02x.\n",
2850 tmr->function);
2851 tmr->response = TMR_FUNCTION_REJECTED;
2852 break;
2855 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
2856 cmd->se_tfo->queue_tm_rsp(cmd);
2858 transport_cmd_check_stop_to_fabric(cmd);
2861 int transport_generic_handle_tmr(
2862 struct se_cmd *cmd)
2864 INIT_WORK(&cmd->work, target_tmr_work);
2865 queue_work(cmd->se_dev->tmr_wq, &cmd->work);
2866 return 0;
2868 EXPORT_SYMBOL(transport_generic_handle_tmr);