target: Enable WRITE_INSERT emulation in target_execute_cmd
[linux/fpc-iii.git] / drivers / target / target_core_transport.c
bloba7a4ef2d589e48a182b1a95cde79eccb2df793c3
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(enum target_prot_op sup_prot_ops)
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);
254 se_sess->sup_prot_ops = sup_prot_ops;
256 return se_sess;
258 EXPORT_SYMBOL(transport_init_session);
260 int transport_alloc_session_tags(struct se_session *se_sess,
261 unsigned int tag_num, unsigned int tag_size)
263 int rc;
265 se_sess->sess_cmd_map = kzalloc(tag_num * tag_size,
266 GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
267 if (!se_sess->sess_cmd_map) {
268 se_sess->sess_cmd_map = vzalloc(tag_num * tag_size);
269 if (!se_sess->sess_cmd_map) {
270 pr_err("Unable to allocate se_sess->sess_cmd_map\n");
271 return -ENOMEM;
275 rc = percpu_ida_init(&se_sess->sess_tag_pool, tag_num);
276 if (rc < 0) {
277 pr_err("Unable to init se_sess->sess_tag_pool,"
278 " tag_num: %u\n", tag_num);
279 if (is_vmalloc_addr(se_sess->sess_cmd_map))
280 vfree(se_sess->sess_cmd_map);
281 else
282 kfree(se_sess->sess_cmd_map);
283 se_sess->sess_cmd_map = NULL;
284 return -ENOMEM;
287 return 0;
289 EXPORT_SYMBOL(transport_alloc_session_tags);
291 struct se_session *transport_init_session_tags(unsigned int tag_num,
292 unsigned int tag_size,
293 enum target_prot_op sup_prot_ops)
295 struct se_session *se_sess;
296 int rc;
298 se_sess = transport_init_session(sup_prot_ops);
299 if (IS_ERR(se_sess))
300 return se_sess;
302 rc = transport_alloc_session_tags(se_sess, tag_num, tag_size);
303 if (rc < 0) {
304 transport_free_session(se_sess);
305 return ERR_PTR(-ENOMEM);
308 return se_sess;
310 EXPORT_SYMBOL(transport_init_session_tags);
313 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
315 void __transport_register_session(
316 struct se_portal_group *se_tpg,
317 struct se_node_acl *se_nacl,
318 struct se_session *se_sess,
319 void *fabric_sess_ptr)
321 unsigned char buf[PR_REG_ISID_LEN];
323 se_sess->se_tpg = se_tpg;
324 se_sess->fabric_sess_ptr = fabric_sess_ptr;
326 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
328 * Only set for struct se_session's that will actually be moving I/O.
329 * eg: *NOT* discovery sessions.
331 if (se_nacl) {
333 * If the fabric module supports an ISID based TransportID,
334 * save this value in binary from the fabric I_T Nexus now.
336 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
337 memset(&buf[0], 0, PR_REG_ISID_LEN);
338 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
339 &buf[0], PR_REG_ISID_LEN);
340 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
342 kref_get(&se_nacl->acl_kref);
344 spin_lock_irq(&se_nacl->nacl_sess_lock);
346 * The se_nacl->nacl_sess pointer will be set to the
347 * last active I_T Nexus for each struct se_node_acl.
349 se_nacl->nacl_sess = se_sess;
351 list_add_tail(&se_sess->sess_acl_list,
352 &se_nacl->acl_sess_list);
353 spin_unlock_irq(&se_nacl->nacl_sess_lock);
355 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
357 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
358 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
360 EXPORT_SYMBOL(__transport_register_session);
362 void transport_register_session(
363 struct se_portal_group *se_tpg,
364 struct se_node_acl *se_nacl,
365 struct se_session *se_sess,
366 void *fabric_sess_ptr)
368 unsigned long flags;
370 spin_lock_irqsave(&se_tpg->session_lock, flags);
371 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
372 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
374 EXPORT_SYMBOL(transport_register_session);
376 static void target_release_session(struct kref *kref)
378 struct se_session *se_sess = container_of(kref,
379 struct se_session, sess_kref);
380 struct se_portal_group *se_tpg = se_sess->se_tpg;
382 se_tpg->se_tpg_tfo->close_session(se_sess);
385 void target_get_session(struct se_session *se_sess)
387 kref_get(&se_sess->sess_kref);
389 EXPORT_SYMBOL(target_get_session);
391 void target_put_session(struct se_session *se_sess)
393 struct se_portal_group *tpg = se_sess->se_tpg;
395 if (tpg->se_tpg_tfo->put_session != NULL) {
396 tpg->se_tpg_tfo->put_session(se_sess);
397 return;
399 kref_put(&se_sess->sess_kref, target_release_session);
401 EXPORT_SYMBOL(target_put_session);
403 static void target_complete_nacl(struct kref *kref)
405 struct se_node_acl *nacl = container_of(kref,
406 struct se_node_acl, acl_kref);
408 complete(&nacl->acl_free_comp);
411 void target_put_nacl(struct se_node_acl *nacl)
413 kref_put(&nacl->acl_kref, target_complete_nacl);
416 void transport_deregister_session_configfs(struct se_session *se_sess)
418 struct se_node_acl *se_nacl;
419 unsigned long flags;
421 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
423 se_nacl = se_sess->se_node_acl;
424 if (se_nacl) {
425 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
426 if (se_nacl->acl_stop == 0)
427 list_del(&se_sess->sess_acl_list);
429 * If the session list is empty, then clear the pointer.
430 * Otherwise, set the struct se_session pointer from the tail
431 * element of the per struct se_node_acl active session list.
433 if (list_empty(&se_nacl->acl_sess_list))
434 se_nacl->nacl_sess = NULL;
435 else {
436 se_nacl->nacl_sess = container_of(
437 se_nacl->acl_sess_list.prev,
438 struct se_session, sess_acl_list);
440 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
443 EXPORT_SYMBOL(transport_deregister_session_configfs);
445 void transport_free_session(struct se_session *se_sess)
447 if (se_sess->sess_cmd_map) {
448 percpu_ida_destroy(&se_sess->sess_tag_pool);
449 if (is_vmalloc_addr(se_sess->sess_cmd_map))
450 vfree(se_sess->sess_cmd_map);
451 else
452 kfree(se_sess->sess_cmd_map);
454 kmem_cache_free(se_sess_cache, se_sess);
456 EXPORT_SYMBOL(transport_free_session);
458 void transport_deregister_session(struct se_session *se_sess)
460 struct se_portal_group *se_tpg = se_sess->se_tpg;
461 struct target_core_fabric_ops *se_tfo;
462 struct se_node_acl *se_nacl;
463 unsigned long flags;
464 bool comp_nacl = true;
466 if (!se_tpg) {
467 transport_free_session(se_sess);
468 return;
470 se_tfo = se_tpg->se_tpg_tfo;
472 spin_lock_irqsave(&se_tpg->session_lock, flags);
473 list_del(&se_sess->sess_list);
474 se_sess->se_tpg = NULL;
475 se_sess->fabric_sess_ptr = NULL;
476 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
479 * Determine if we need to do extra work for this initiator node's
480 * struct se_node_acl if it had been previously dynamically generated.
482 se_nacl = se_sess->se_node_acl;
484 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
485 if (se_nacl && se_nacl->dynamic_node_acl) {
486 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
487 list_del(&se_nacl->acl_list);
488 se_tpg->num_node_acls--;
489 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
490 core_tpg_wait_for_nacl_pr_ref(se_nacl);
491 core_free_device_list_for_node(se_nacl, se_tpg);
492 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
494 comp_nacl = false;
495 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
498 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
500 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
501 se_tpg->se_tpg_tfo->get_fabric_name());
503 * If last kref is dropping now for an explicit NodeACL, awake sleeping
504 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
505 * removal context.
507 if (se_nacl && comp_nacl == true)
508 target_put_nacl(se_nacl);
510 transport_free_session(se_sess);
512 EXPORT_SYMBOL(transport_deregister_session);
515 * Called with cmd->t_state_lock held.
517 static void target_remove_from_state_list(struct se_cmd *cmd)
519 struct se_device *dev = cmd->se_dev;
520 unsigned long flags;
522 if (!dev)
523 return;
525 if (cmd->transport_state & CMD_T_BUSY)
526 return;
528 spin_lock_irqsave(&dev->execute_task_lock, flags);
529 if (cmd->state_active) {
530 list_del(&cmd->state_list);
531 cmd->state_active = false;
533 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
536 static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists,
537 bool write_pending)
539 unsigned long flags;
541 spin_lock_irqsave(&cmd->t_state_lock, flags);
542 if (write_pending)
543 cmd->t_state = TRANSPORT_WRITE_PENDING;
545 if (remove_from_lists) {
546 target_remove_from_state_list(cmd);
549 * Clear struct se_cmd->se_lun before the handoff to FE.
551 cmd->se_lun = NULL;
555 * Determine if frontend context caller is requesting the stopping of
556 * this command for frontend exceptions.
558 if (cmd->transport_state & CMD_T_STOP) {
559 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
560 __func__, __LINE__,
561 cmd->se_tfo->get_task_tag(cmd));
563 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
565 complete(&cmd->t_transport_stop_comp);
566 return 1;
569 cmd->transport_state &= ~CMD_T_ACTIVE;
570 if (remove_from_lists) {
572 * Some fabric modules like tcm_loop can release
573 * their internally allocated I/O reference now and
574 * struct se_cmd now.
576 * Fabric modules are expected to return '1' here if the
577 * se_cmd being passed is released at this point,
578 * or zero if not being released.
580 if (cmd->se_tfo->check_stop_free != NULL) {
581 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
582 return cmd->se_tfo->check_stop_free(cmd);
586 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
587 return 0;
590 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
592 return transport_cmd_check_stop(cmd, true, false);
595 static void transport_lun_remove_cmd(struct se_cmd *cmd)
597 struct se_lun *lun = cmd->se_lun;
599 if (!lun)
600 return;
602 if (cmpxchg(&cmd->lun_ref_active, true, false))
603 percpu_ref_put(&lun->lun_ref);
606 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
608 if (cmd->se_cmd_flags & SCF_SE_LUN_CMD)
609 transport_lun_remove_cmd(cmd);
611 * Allow the fabric driver to unmap any resources before
612 * releasing the descriptor via TFO->release_cmd()
614 if (remove)
615 cmd->se_tfo->aborted_task(cmd);
617 if (transport_cmd_check_stop_to_fabric(cmd))
618 return;
619 if (remove)
620 transport_put_cmd(cmd);
623 static void target_complete_failure_work(struct work_struct *work)
625 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
627 transport_generic_request_failure(cmd,
628 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
632 * Used when asking transport to copy Sense Data from the underlying
633 * Linux/SCSI struct scsi_cmnd
635 static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd)
637 struct se_device *dev = cmd->se_dev;
639 WARN_ON(!cmd->se_lun);
641 if (!dev)
642 return NULL;
644 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION)
645 return NULL;
647 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
649 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
650 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
651 return cmd->sense_buffer;
654 void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
656 struct se_device *dev = cmd->se_dev;
657 int success = scsi_status == GOOD;
658 unsigned long flags;
660 cmd->scsi_status = scsi_status;
663 spin_lock_irqsave(&cmd->t_state_lock, flags);
664 cmd->transport_state &= ~CMD_T_BUSY;
666 if (dev && dev->transport->transport_complete) {
667 dev->transport->transport_complete(cmd,
668 cmd->t_data_sg,
669 transport_get_sense_buffer(cmd));
670 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
671 success = 1;
675 * See if we are waiting to complete for an exception condition.
677 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
678 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
679 complete(&cmd->task_stop_comp);
680 return;
684 * Check for case where an explicit ABORT_TASK has been received
685 * and transport_wait_for_tasks() will be waiting for completion..
687 if (cmd->transport_state & CMD_T_ABORTED &&
688 cmd->transport_state & CMD_T_STOP) {
689 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
690 complete(&cmd->t_transport_stop_comp);
691 return;
692 } else if (!success) {
693 INIT_WORK(&cmd->work, target_complete_failure_work);
694 } else {
695 INIT_WORK(&cmd->work, target_complete_ok_work);
698 cmd->t_state = TRANSPORT_COMPLETE;
699 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
700 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
702 queue_work(target_completion_wq, &cmd->work);
704 EXPORT_SYMBOL(target_complete_cmd);
706 static void target_add_to_state_list(struct se_cmd *cmd)
708 struct se_device *dev = cmd->se_dev;
709 unsigned long flags;
711 spin_lock_irqsave(&dev->execute_task_lock, flags);
712 if (!cmd->state_active) {
713 list_add_tail(&cmd->state_list, &dev->state_list);
714 cmd->state_active = true;
716 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
720 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
722 static void transport_write_pending_qf(struct se_cmd *cmd);
723 static void transport_complete_qf(struct se_cmd *cmd);
725 void target_qf_do_work(struct work_struct *work)
727 struct se_device *dev = container_of(work, struct se_device,
728 qf_work_queue);
729 LIST_HEAD(qf_cmd_list);
730 struct se_cmd *cmd, *cmd_tmp;
732 spin_lock_irq(&dev->qf_cmd_lock);
733 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
734 spin_unlock_irq(&dev->qf_cmd_lock);
736 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
737 list_del(&cmd->se_qf_node);
738 atomic_dec(&dev->dev_qf_count);
739 smp_mb__after_atomic_dec();
741 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
742 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
743 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
744 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
745 : "UNKNOWN");
747 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
748 transport_write_pending_qf(cmd);
749 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK)
750 transport_complete_qf(cmd);
754 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
756 switch (cmd->data_direction) {
757 case DMA_NONE:
758 return "NONE";
759 case DMA_FROM_DEVICE:
760 return "READ";
761 case DMA_TO_DEVICE:
762 return "WRITE";
763 case DMA_BIDIRECTIONAL:
764 return "BIDI";
765 default:
766 break;
769 return "UNKNOWN";
772 void transport_dump_dev_state(
773 struct se_device *dev,
774 char *b,
775 int *bl)
777 *bl += sprintf(b + *bl, "Status: ");
778 if (dev->export_count)
779 *bl += sprintf(b + *bl, "ACTIVATED");
780 else
781 *bl += sprintf(b + *bl, "DEACTIVATED");
783 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
784 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
785 dev->dev_attrib.block_size,
786 dev->dev_attrib.hw_max_sectors);
787 *bl += sprintf(b + *bl, " ");
790 void transport_dump_vpd_proto_id(
791 struct t10_vpd *vpd,
792 unsigned char *p_buf,
793 int p_buf_len)
795 unsigned char buf[VPD_TMP_BUF_SIZE];
796 int len;
798 memset(buf, 0, VPD_TMP_BUF_SIZE);
799 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
801 switch (vpd->protocol_identifier) {
802 case 0x00:
803 sprintf(buf+len, "Fibre Channel\n");
804 break;
805 case 0x10:
806 sprintf(buf+len, "Parallel SCSI\n");
807 break;
808 case 0x20:
809 sprintf(buf+len, "SSA\n");
810 break;
811 case 0x30:
812 sprintf(buf+len, "IEEE 1394\n");
813 break;
814 case 0x40:
815 sprintf(buf+len, "SCSI Remote Direct Memory Access"
816 " Protocol\n");
817 break;
818 case 0x50:
819 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
820 break;
821 case 0x60:
822 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
823 break;
824 case 0x70:
825 sprintf(buf+len, "Automation/Drive Interface Transport"
826 " Protocol\n");
827 break;
828 case 0x80:
829 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
830 break;
831 default:
832 sprintf(buf+len, "Unknown 0x%02x\n",
833 vpd->protocol_identifier);
834 break;
837 if (p_buf)
838 strncpy(p_buf, buf, p_buf_len);
839 else
840 pr_debug("%s", buf);
843 void
844 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
847 * Check if the Protocol Identifier Valid (PIV) bit is set..
849 * from spc3r23.pdf section 7.5.1
851 if (page_83[1] & 0x80) {
852 vpd->protocol_identifier = (page_83[0] & 0xf0);
853 vpd->protocol_identifier_set = 1;
854 transport_dump_vpd_proto_id(vpd, NULL, 0);
857 EXPORT_SYMBOL(transport_set_vpd_proto_id);
859 int transport_dump_vpd_assoc(
860 struct t10_vpd *vpd,
861 unsigned char *p_buf,
862 int p_buf_len)
864 unsigned char buf[VPD_TMP_BUF_SIZE];
865 int ret = 0;
866 int len;
868 memset(buf, 0, VPD_TMP_BUF_SIZE);
869 len = sprintf(buf, "T10 VPD Identifier Association: ");
871 switch (vpd->association) {
872 case 0x00:
873 sprintf(buf+len, "addressed logical unit\n");
874 break;
875 case 0x10:
876 sprintf(buf+len, "target port\n");
877 break;
878 case 0x20:
879 sprintf(buf+len, "SCSI target device\n");
880 break;
881 default:
882 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
883 ret = -EINVAL;
884 break;
887 if (p_buf)
888 strncpy(p_buf, buf, p_buf_len);
889 else
890 pr_debug("%s", buf);
892 return ret;
895 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
898 * The VPD identification association..
900 * from spc3r23.pdf Section 7.6.3.1 Table 297
902 vpd->association = (page_83[1] & 0x30);
903 return transport_dump_vpd_assoc(vpd, NULL, 0);
905 EXPORT_SYMBOL(transport_set_vpd_assoc);
907 int transport_dump_vpd_ident_type(
908 struct t10_vpd *vpd,
909 unsigned char *p_buf,
910 int p_buf_len)
912 unsigned char buf[VPD_TMP_BUF_SIZE];
913 int ret = 0;
914 int len;
916 memset(buf, 0, VPD_TMP_BUF_SIZE);
917 len = sprintf(buf, "T10 VPD Identifier Type: ");
919 switch (vpd->device_identifier_type) {
920 case 0x00:
921 sprintf(buf+len, "Vendor specific\n");
922 break;
923 case 0x01:
924 sprintf(buf+len, "T10 Vendor ID based\n");
925 break;
926 case 0x02:
927 sprintf(buf+len, "EUI-64 based\n");
928 break;
929 case 0x03:
930 sprintf(buf+len, "NAA\n");
931 break;
932 case 0x04:
933 sprintf(buf+len, "Relative target port identifier\n");
934 break;
935 case 0x08:
936 sprintf(buf+len, "SCSI name string\n");
937 break;
938 default:
939 sprintf(buf+len, "Unsupported: 0x%02x\n",
940 vpd->device_identifier_type);
941 ret = -EINVAL;
942 break;
945 if (p_buf) {
946 if (p_buf_len < strlen(buf)+1)
947 return -EINVAL;
948 strncpy(p_buf, buf, p_buf_len);
949 } else {
950 pr_debug("%s", buf);
953 return ret;
956 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
959 * The VPD identifier type..
961 * from spc3r23.pdf Section 7.6.3.1 Table 298
963 vpd->device_identifier_type = (page_83[1] & 0x0f);
964 return transport_dump_vpd_ident_type(vpd, NULL, 0);
966 EXPORT_SYMBOL(transport_set_vpd_ident_type);
968 int transport_dump_vpd_ident(
969 struct t10_vpd *vpd,
970 unsigned char *p_buf,
971 int p_buf_len)
973 unsigned char buf[VPD_TMP_BUF_SIZE];
974 int ret = 0;
976 memset(buf, 0, VPD_TMP_BUF_SIZE);
978 switch (vpd->device_identifier_code_set) {
979 case 0x01: /* Binary */
980 snprintf(buf, sizeof(buf),
981 "T10 VPD Binary Device Identifier: %s\n",
982 &vpd->device_identifier[0]);
983 break;
984 case 0x02: /* ASCII */
985 snprintf(buf, sizeof(buf),
986 "T10 VPD ASCII Device Identifier: %s\n",
987 &vpd->device_identifier[0]);
988 break;
989 case 0x03: /* UTF-8 */
990 snprintf(buf, sizeof(buf),
991 "T10 VPD UTF-8 Device Identifier: %s\n",
992 &vpd->device_identifier[0]);
993 break;
994 default:
995 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
996 " 0x%02x", vpd->device_identifier_code_set);
997 ret = -EINVAL;
998 break;
1001 if (p_buf)
1002 strncpy(p_buf, buf, p_buf_len);
1003 else
1004 pr_debug("%s", buf);
1006 return ret;
1010 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1012 static const char hex_str[] = "0123456789abcdef";
1013 int j = 0, i = 4; /* offset to start of the identifier */
1016 * The VPD Code Set (encoding)
1018 * from spc3r23.pdf Section 7.6.3.1 Table 296
1020 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1021 switch (vpd->device_identifier_code_set) {
1022 case 0x01: /* Binary */
1023 vpd->device_identifier[j++] =
1024 hex_str[vpd->device_identifier_type];
1025 while (i < (4 + page_83[3])) {
1026 vpd->device_identifier[j++] =
1027 hex_str[(page_83[i] & 0xf0) >> 4];
1028 vpd->device_identifier[j++] =
1029 hex_str[page_83[i] & 0x0f];
1030 i++;
1032 break;
1033 case 0x02: /* ASCII */
1034 case 0x03: /* UTF-8 */
1035 while (i < (4 + page_83[3]))
1036 vpd->device_identifier[j++] = page_83[i++];
1037 break;
1038 default:
1039 break;
1042 return transport_dump_vpd_ident(vpd, NULL, 0);
1044 EXPORT_SYMBOL(transport_set_vpd_ident);
1046 sense_reason_t
1047 target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
1049 struct se_device *dev = cmd->se_dev;
1051 if (cmd->unknown_data_length) {
1052 cmd->data_length = size;
1053 } else if (size != cmd->data_length) {
1054 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
1055 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
1056 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
1057 cmd->data_length, size, cmd->t_task_cdb[0]);
1059 if (cmd->data_direction == DMA_TO_DEVICE) {
1060 pr_err("Rejecting underflow/overflow"
1061 " WRITE data\n");
1062 return TCM_INVALID_CDB_FIELD;
1065 * Reject READ_* or WRITE_* with overflow/underflow for
1066 * type SCF_SCSI_DATA_CDB.
1068 if (dev->dev_attrib.block_size != 512) {
1069 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1070 " CDB on non 512-byte sector setup subsystem"
1071 " plugin: %s\n", dev->transport->name);
1072 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1073 return TCM_INVALID_CDB_FIELD;
1076 * For the overflow case keep the existing fabric provided
1077 * ->data_length. Otherwise for the underflow case, reset
1078 * ->data_length to the smaller SCSI expected data transfer
1079 * length.
1081 if (size > cmd->data_length) {
1082 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1083 cmd->residual_count = (size - cmd->data_length);
1084 } else {
1085 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1086 cmd->residual_count = (cmd->data_length - size);
1087 cmd->data_length = size;
1091 return 0;
1096 * Used by fabric modules containing a local struct se_cmd within their
1097 * fabric dependent per I/O descriptor.
1099 void transport_init_se_cmd(
1100 struct se_cmd *cmd,
1101 struct target_core_fabric_ops *tfo,
1102 struct se_session *se_sess,
1103 u32 data_length,
1104 int data_direction,
1105 int task_attr,
1106 unsigned char *sense_buffer)
1108 INIT_LIST_HEAD(&cmd->se_delayed_node);
1109 INIT_LIST_HEAD(&cmd->se_qf_node);
1110 INIT_LIST_HEAD(&cmd->se_cmd_list);
1111 INIT_LIST_HEAD(&cmd->state_list);
1112 init_completion(&cmd->t_transport_stop_comp);
1113 init_completion(&cmd->cmd_wait_comp);
1114 init_completion(&cmd->task_stop_comp);
1115 spin_lock_init(&cmd->t_state_lock);
1116 cmd->transport_state = CMD_T_DEV_ACTIVE;
1118 cmd->se_tfo = tfo;
1119 cmd->se_sess = se_sess;
1120 cmd->data_length = data_length;
1121 cmd->data_direction = data_direction;
1122 cmd->sam_task_attr = task_attr;
1123 cmd->sense_buffer = sense_buffer;
1125 cmd->state_active = false;
1127 EXPORT_SYMBOL(transport_init_se_cmd);
1129 static sense_reason_t
1130 transport_check_alloc_task_attr(struct se_cmd *cmd)
1132 struct se_device *dev = cmd->se_dev;
1135 * Check if SAM Task Attribute emulation is enabled for this
1136 * struct se_device storage object
1138 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1139 return 0;
1141 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1142 pr_debug("SAM Task Attribute ACA"
1143 " emulation is not supported\n");
1144 return TCM_INVALID_CDB_FIELD;
1147 * Used to determine when ORDERED commands should go from
1148 * Dormant to Active status.
1150 cmd->se_ordered_id = atomic_inc_return(&dev->dev_ordered_id);
1151 smp_mb__after_atomic_inc();
1152 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1153 cmd->se_ordered_id, cmd->sam_task_attr,
1154 dev->transport->name);
1155 return 0;
1158 sense_reason_t
1159 target_setup_cmd_from_cdb(struct se_cmd *cmd, unsigned char *cdb)
1161 struct se_device *dev = cmd->se_dev;
1162 sense_reason_t ret;
1165 * Ensure that the received CDB is less than the max (252 + 8) bytes
1166 * for VARIABLE_LENGTH_CMD
1168 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1169 pr_err("Received SCSI CDB with command_size: %d that"
1170 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1171 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1172 return TCM_INVALID_CDB_FIELD;
1175 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1176 * allocate the additional extended CDB buffer now.. Otherwise
1177 * setup the pointer from __t_task_cdb to t_task_cdb.
1179 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1180 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1181 GFP_KERNEL);
1182 if (!cmd->t_task_cdb) {
1183 pr_err("Unable to allocate cmd->t_task_cdb"
1184 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1185 scsi_command_size(cdb),
1186 (unsigned long)sizeof(cmd->__t_task_cdb));
1187 return TCM_OUT_OF_RESOURCES;
1189 } else
1190 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1192 * Copy the original CDB into cmd->
1194 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1196 trace_target_sequencer_start(cmd);
1199 * Check for an existing UNIT ATTENTION condition
1201 ret = target_scsi3_ua_check(cmd);
1202 if (ret)
1203 return ret;
1205 ret = target_alua_state_check(cmd);
1206 if (ret)
1207 return ret;
1209 ret = target_check_reservation(cmd);
1210 if (ret) {
1211 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1212 return ret;
1215 ret = dev->transport->parse_cdb(cmd);
1216 if (ret)
1217 return ret;
1219 ret = transport_check_alloc_task_attr(cmd);
1220 if (ret)
1221 return ret;
1223 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1225 spin_lock(&cmd->se_lun->lun_sep_lock);
1226 if (cmd->se_lun->lun_sep)
1227 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1228 spin_unlock(&cmd->se_lun->lun_sep_lock);
1229 return 0;
1231 EXPORT_SYMBOL(target_setup_cmd_from_cdb);
1234 * Used by fabric module frontends to queue tasks directly.
1235 * Many only be used from process context only
1237 int transport_handle_cdb_direct(
1238 struct se_cmd *cmd)
1240 sense_reason_t ret;
1242 if (!cmd->se_lun) {
1243 dump_stack();
1244 pr_err("cmd->se_lun is NULL\n");
1245 return -EINVAL;
1247 if (in_interrupt()) {
1248 dump_stack();
1249 pr_err("transport_generic_handle_cdb cannot be called"
1250 " from interrupt context\n");
1251 return -EINVAL;
1254 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1255 * outstanding descriptors are handled correctly during shutdown via
1256 * transport_wait_for_tasks()
1258 * Also, we don't take cmd->t_state_lock here as we only expect
1259 * this to be called for initial descriptor submission.
1261 cmd->t_state = TRANSPORT_NEW_CMD;
1262 cmd->transport_state |= CMD_T_ACTIVE;
1265 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1266 * so follow TRANSPORT_NEW_CMD processing thread context usage
1267 * and call transport_generic_request_failure() if necessary..
1269 ret = transport_generic_new_cmd(cmd);
1270 if (ret)
1271 transport_generic_request_failure(cmd, ret);
1272 return 0;
1274 EXPORT_SYMBOL(transport_handle_cdb_direct);
1276 sense_reason_t
1277 transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
1278 u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count)
1280 if (!sgl || !sgl_count)
1281 return 0;
1284 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
1285 * scatterlists already have been set to follow what the fabric
1286 * passes for the original expected data transfer length.
1288 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1289 pr_warn("Rejecting SCSI DATA overflow for fabric using"
1290 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
1291 return TCM_INVALID_CDB_FIELD;
1294 cmd->t_data_sg = sgl;
1295 cmd->t_data_nents = sgl_count;
1297 if (sgl_bidi && sgl_bidi_count) {
1298 cmd->t_bidi_data_sg = sgl_bidi;
1299 cmd->t_bidi_data_nents = sgl_bidi_count;
1301 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1302 return 0;
1306 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1307 * se_cmd + use pre-allocated SGL memory.
1309 * @se_cmd: command descriptor to submit
1310 * @se_sess: associated se_sess for endpoint
1311 * @cdb: pointer to SCSI CDB
1312 * @sense: pointer to SCSI sense buffer
1313 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1314 * @data_length: fabric expected data transfer length
1315 * @task_addr: SAM task attribute
1316 * @data_dir: DMA data direction
1317 * @flags: flags for command submission from target_sc_flags_tables
1318 * @sgl: struct scatterlist memory for unidirectional mapping
1319 * @sgl_count: scatterlist count for unidirectional mapping
1320 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1321 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1322 * @sgl_prot: struct scatterlist memory protection information
1323 * @sgl_prot_count: scatterlist count for protection information
1325 * Returns non zero to signal active I/O shutdown failure. All other
1326 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1327 * but still return zero here.
1329 * This may only be called from process context, and also currently
1330 * assumes internal allocation of fabric payload buffer by target-core.
1332 int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess,
1333 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1334 u32 data_length, int task_attr, int data_dir, int flags,
1335 struct scatterlist *sgl, u32 sgl_count,
1336 struct scatterlist *sgl_bidi, u32 sgl_bidi_count,
1337 struct scatterlist *sgl_prot, u32 sgl_prot_count)
1339 struct se_portal_group *se_tpg;
1340 sense_reason_t rc;
1341 int ret;
1343 se_tpg = se_sess->se_tpg;
1344 BUG_ON(!se_tpg);
1345 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1346 BUG_ON(in_interrupt());
1348 * Initialize se_cmd for target operation. From this point
1349 * exceptions are handled by sending exception status via
1350 * target_core_fabric_ops->queue_status() callback
1352 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1353 data_length, data_dir, task_attr, sense);
1354 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1355 se_cmd->unknown_data_length = 1;
1357 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1358 * se_sess->sess_cmd_list. A second kref_get here is necessary
1359 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1360 * kref_put() to happen during fabric packet acknowledgement.
1362 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1363 if (ret)
1364 return ret;
1366 * Signal bidirectional data payloads to target-core
1368 if (flags & TARGET_SCF_BIDI_OP)
1369 se_cmd->se_cmd_flags |= SCF_BIDI;
1371 * Locate se_lun pointer and attach it to struct se_cmd
1373 rc = transport_lookup_cmd_lun(se_cmd, unpacked_lun);
1374 if (rc) {
1375 transport_send_check_condition_and_sense(se_cmd, rc, 0);
1376 target_put_sess_cmd(se_sess, se_cmd);
1377 return 0;
1380 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
1381 if (rc != 0) {
1382 transport_generic_request_failure(se_cmd, rc);
1383 return 0;
1387 * Save pointers for SGLs containing protection information,
1388 * if present.
1390 if (sgl_prot_count) {
1391 se_cmd->t_prot_sg = sgl_prot;
1392 se_cmd->t_prot_nents = sgl_prot_count;
1396 * When a non zero sgl_count has been passed perform SGL passthrough
1397 * mapping for pre-allocated fabric memory instead of having target
1398 * core perform an internal SGL allocation..
1400 if (sgl_count != 0) {
1401 BUG_ON(!sgl);
1404 * A work-around for tcm_loop as some userspace code via
1405 * scsi-generic do not memset their associated read buffers,
1406 * so go ahead and do that here for type non-data CDBs. Also
1407 * note that this is currently guaranteed to be a single SGL
1408 * for this case by target core in target_setup_cmd_from_cdb()
1409 * -> transport_generic_cmd_sequencer().
1411 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
1412 se_cmd->data_direction == DMA_FROM_DEVICE) {
1413 unsigned char *buf = NULL;
1415 if (sgl)
1416 buf = kmap(sg_page(sgl)) + sgl->offset;
1418 if (buf) {
1419 memset(buf, 0, sgl->length);
1420 kunmap(sg_page(sgl));
1424 rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
1425 sgl_bidi, sgl_bidi_count);
1426 if (rc != 0) {
1427 transport_generic_request_failure(se_cmd, rc);
1428 return 0;
1433 * Check if we need to delay processing because of ALUA
1434 * Active/NonOptimized primary access state..
1436 core_alua_check_nonop_delay(se_cmd);
1438 transport_handle_cdb_direct(se_cmd);
1439 return 0;
1441 EXPORT_SYMBOL(target_submit_cmd_map_sgls);
1444 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1446 * @se_cmd: command descriptor to submit
1447 * @se_sess: associated se_sess for endpoint
1448 * @cdb: pointer to SCSI CDB
1449 * @sense: pointer to SCSI sense buffer
1450 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1451 * @data_length: fabric expected data transfer length
1452 * @task_addr: SAM task attribute
1453 * @data_dir: DMA data direction
1454 * @flags: flags for command submission from target_sc_flags_tables
1456 * Returns non zero to signal active I/O shutdown failure. All other
1457 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1458 * but still return zero here.
1460 * This may only be called from process context, and also currently
1461 * assumes internal allocation of fabric payload buffer by target-core.
1463 * It also assumes interal target core SGL memory allocation.
1465 int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1466 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1467 u32 data_length, int task_attr, int data_dir, int flags)
1469 return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense,
1470 unpacked_lun, data_length, task_attr, data_dir,
1471 flags, NULL, 0, NULL, 0, NULL, 0);
1473 EXPORT_SYMBOL(target_submit_cmd);
1475 static void target_complete_tmr_failure(struct work_struct *work)
1477 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1479 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1480 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1482 transport_cmd_check_stop_to_fabric(se_cmd);
1486 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1487 * for TMR CDBs
1489 * @se_cmd: command descriptor to submit
1490 * @se_sess: associated se_sess for endpoint
1491 * @sense: pointer to SCSI sense buffer
1492 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1493 * @fabric_context: fabric context for TMR req
1494 * @tm_type: Type of TM request
1495 * @gfp: gfp type for caller
1496 * @tag: referenced task tag for TMR_ABORT_TASK
1497 * @flags: submit cmd flags
1499 * Callable from all contexts.
1502 int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1503 unsigned char *sense, u32 unpacked_lun,
1504 void *fabric_tmr_ptr, unsigned char tm_type,
1505 gfp_t gfp, unsigned int tag, int flags)
1507 struct se_portal_group *se_tpg;
1508 int ret;
1510 se_tpg = se_sess->se_tpg;
1511 BUG_ON(!se_tpg);
1513 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1514 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1516 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1517 * allocation failure.
1519 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
1520 if (ret < 0)
1521 return -ENOMEM;
1523 if (tm_type == TMR_ABORT_TASK)
1524 se_cmd->se_tmr_req->ref_task_tag = tag;
1526 /* See target_submit_cmd for commentary */
1527 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1528 if (ret) {
1529 core_tmr_release_req(se_cmd->se_tmr_req);
1530 return ret;
1533 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1534 if (ret) {
1536 * For callback during failure handling, push this work off
1537 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1539 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1540 schedule_work(&se_cmd->work);
1541 return 0;
1543 transport_generic_handle_tmr(se_cmd);
1544 return 0;
1546 EXPORT_SYMBOL(target_submit_tmr);
1549 * If the cmd is active, request it to be stopped and sleep until it
1550 * has completed.
1552 bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
1554 bool was_active = false;
1556 if (cmd->transport_state & CMD_T_BUSY) {
1557 cmd->transport_state |= CMD_T_REQUEST_STOP;
1558 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1560 pr_debug("cmd %p waiting to complete\n", cmd);
1561 wait_for_completion(&cmd->task_stop_comp);
1562 pr_debug("cmd %p stopped successfully\n", cmd);
1564 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1565 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1566 cmd->transport_state &= ~CMD_T_BUSY;
1567 was_active = true;
1570 return was_active;
1574 * Handle SAM-esque emulation for generic transport request failures.
1576 void transport_generic_request_failure(struct se_cmd *cmd,
1577 sense_reason_t sense_reason)
1579 int ret = 0;
1581 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1582 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1583 cmd->t_task_cdb[0]);
1584 pr_debug("-----[ i_state: %d t_state: %d sense_reason: %d\n",
1585 cmd->se_tfo->get_cmd_state(cmd),
1586 cmd->t_state, sense_reason);
1587 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1588 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1589 (cmd->transport_state & CMD_T_STOP) != 0,
1590 (cmd->transport_state & CMD_T_SENT) != 0);
1593 * For SAM Task Attribute emulation for failed struct se_cmd
1595 transport_complete_task_attr(cmd);
1597 * Handle special case for COMPARE_AND_WRITE failure, where the
1598 * callback is expected to drop the per device ->caw_mutex.
1600 if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
1601 cmd->transport_complete_callback)
1602 cmd->transport_complete_callback(cmd);
1604 switch (sense_reason) {
1605 case TCM_NON_EXISTENT_LUN:
1606 case TCM_UNSUPPORTED_SCSI_OPCODE:
1607 case TCM_INVALID_CDB_FIELD:
1608 case TCM_INVALID_PARAMETER_LIST:
1609 case TCM_PARAMETER_LIST_LENGTH_ERROR:
1610 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1611 case TCM_UNKNOWN_MODE_PAGE:
1612 case TCM_WRITE_PROTECTED:
1613 case TCM_ADDRESS_OUT_OF_RANGE:
1614 case TCM_CHECK_CONDITION_ABORT_CMD:
1615 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1616 case TCM_CHECK_CONDITION_NOT_READY:
1617 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED:
1618 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED:
1619 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED:
1620 break;
1621 case TCM_OUT_OF_RESOURCES:
1622 sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1623 break;
1624 case TCM_RESERVATION_CONFLICT:
1626 * No SENSE Data payload for this case, set SCSI Status
1627 * and queue the response to $FABRIC_MOD.
1629 * Uses linux/include/scsi/scsi.h SAM status codes defs
1631 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1633 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1634 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1635 * CONFLICT STATUS.
1637 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1639 if (cmd->se_sess &&
1640 cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2)
1641 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1642 cmd->orig_fe_lun, 0x2C,
1643 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1645 trace_target_cmd_complete(cmd);
1646 ret = cmd->se_tfo-> queue_status(cmd);
1647 if (ret == -EAGAIN || ret == -ENOMEM)
1648 goto queue_full;
1649 goto check_stop;
1650 default:
1651 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1652 cmd->t_task_cdb[0], sense_reason);
1653 sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1654 break;
1657 ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0);
1658 if (ret == -EAGAIN || ret == -ENOMEM)
1659 goto queue_full;
1661 check_stop:
1662 transport_lun_remove_cmd(cmd);
1663 if (!transport_cmd_check_stop_to_fabric(cmd))
1665 return;
1667 queue_full:
1668 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1669 transport_handle_queue_full(cmd, cmd->se_dev);
1671 EXPORT_SYMBOL(transport_generic_request_failure);
1673 void __target_execute_cmd(struct se_cmd *cmd)
1675 sense_reason_t ret;
1677 if (cmd->execute_cmd) {
1678 ret = cmd->execute_cmd(cmd);
1679 if (ret) {
1680 spin_lock_irq(&cmd->t_state_lock);
1681 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1682 spin_unlock_irq(&cmd->t_state_lock);
1684 transport_generic_request_failure(cmd, ret);
1689 static bool target_handle_task_attr(struct se_cmd *cmd)
1691 struct se_device *dev = cmd->se_dev;
1693 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1694 return false;
1697 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1698 * to allow the passed struct se_cmd list of tasks to the front of the list.
1700 switch (cmd->sam_task_attr) {
1701 case MSG_HEAD_TAG:
1702 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1703 "se_ordered_id: %u\n",
1704 cmd->t_task_cdb[0], cmd->se_ordered_id);
1705 return false;
1706 case MSG_ORDERED_TAG:
1707 atomic_inc(&dev->dev_ordered_sync);
1708 smp_mb__after_atomic_inc();
1710 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1711 " se_ordered_id: %u\n",
1712 cmd->t_task_cdb[0], cmd->se_ordered_id);
1715 * Execute an ORDERED command if no other older commands
1716 * exist that need to be completed first.
1718 if (!atomic_read(&dev->simple_cmds))
1719 return false;
1720 break;
1721 default:
1723 * For SIMPLE and UNTAGGED Task Attribute commands
1725 atomic_inc(&dev->simple_cmds);
1726 smp_mb__after_atomic_inc();
1727 break;
1730 if (atomic_read(&dev->dev_ordered_sync) == 0)
1731 return false;
1733 spin_lock(&dev->delayed_cmd_lock);
1734 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
1735 spin_unlock(&dev->delayed_cmd_lock);
1737 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1738 " delayed CMD list, se_ordered_id: %u\n",
1739 cmd->t_task_cdb[0], cmd->sam_task_attr,
1740 cmd->se_ordered_id);
1741 return true;
1744 void target_execute_cmd(struct se_cmd *cmd)
1747 * If the received CDB has aleady been aborted stop processing it here.
1749 if (transport_check_aborted_status(cmd, 1))
1750 return;
1753 * Determine if frontend context caller is requesting the stopping of
1754 * this command for frontend exceptions.
1756 spin_lock_irq(&cmd->t_state_lock);
1757 if (cmd->transport_state & CMD_T_STOP) {
1758 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1759 __func__, __LINE__,
1760 cmd->se_tfo->get_task_tag(cmd));
1762 spin_unlock_irq(&cmd->t_state_lock);
1763 complete(&cmd->t_transport_stop_comp);
1764 return;
1767 cmd->t_state = TRANSPORT_PROCESSING;
1768 cmd->transport_state |= CMD_T_ACTIVE|CMD_T_BUSY|CMD_T_SENT;
1769 spin_unlock_irq(&cmd->t_state_lock);
1771 * Perform WRITE_INSERT of PI using software emulation when backend
1772 * device has PI enabled, if the transport has not already generated
1773 * PI using hardware WRITE_INSERT offload.
1775 if (cmd->prot_op == TARGET_PROT_DOUT_INSERT) {
1776 if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_INSERT))
1777 sbc_dif_generate(cmd);
1780 if (target_handle_task_attr(cmd)) {
1781 spin_lock_irq(&cmd->t_state_lock);
1782 cmd->transport_state &= ~CMD_T_BUSY|CMD_T_SENT;
1783 spin_unlock_irq(&cmd->t_state_lock);
1784 return;
1787 __target_execute_cmd(cmd);
1789 EXPORT_SYMBOL(target_execute_cmd);
1792 * Process all commands up to the last received ORDERED task attribute which
1793 * requires another blocking boundary
1795 static void target_restart_delayed_cmds(struct se_device *dev)
1797 for (;;) {
1798 struct se_cmd *cmd;
1800 spin_lock(&dev->delayed_cmd_lock);
1801 if (list_empty(&dev->delayed_cmd_list)) {
1802 spin_unlock(&dev->delayed_cmd_lock);
1803 break;
1806 cmd = list_entry(dev->delayed_cmd_list.next,
1807 struct se_cmd, se_delayed_node);
1808 list_del(&cmd->se_delayed_node);
1809 spin_unlock(&dev->delayed_cmd_lock);
1811 __target_execute_cmd(cmd);
1813 if (cmd->sam_task_attr == MSG_ORDERED_TAG)
1814 break;
1819 * Called from I/O completion to determine which dormant/delayed
1820 * and ordered cmds need to have their tasks added to the execution queue.
1822 static void transport_complete_task_attr(struct se_cmd *cmd)
1824 struct se_device *dev = cmd->se_dev;
1826 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1827 return;
1829 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
1830 atomic_dec(&dev->simple_cmds);
1831 smp_mb__after_atomic_dec();
1832 dev->dev_cur_ordered_id++;
1833 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
1834 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
1835 cmd->se_ordered_id);
1836 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
1837 dev->dev_cur_ordered_id++;
1838 pr_debug("Incremented dev_cur_ordered_id: %u for"
1839 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
1840 cmd->se_ordered_id);
1841 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
1842 atomic_dec(&dev->dev_ordered_sync);
1843 smp_mb__after_atomic_dec();
1845 dev->dev_cur_ordered_id++;
1846 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
1847 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
1850 target_restart_delayed_cmds(dev);
1853 static void transport_complete_qf(struct se_cmd *cmd)
1855 int ret = 0;
1857 transport_complete_task_attr(cmd);
1859 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1860 trace_target_cmd_complete(cmd);
1861 ret = cmd->se_tfo->queue_status(cmd);
1862 if (ret)
1863 goto out;
1866 switch (cmd->data_direction) {
1867 case DMA_FROM_DEVICE:
1868 trace_target_cmd_complete(cmd);
1869 ret = cmd->se_tfo->queue_data_in(cmd);
1870 break;
1871 case DMA_TO_DEVICE:
1872 if (cmd->se_cmd_flags & SCF_BIDI) {
1873 ret = cmd->se_tfo->queue_data_in(cmd);
1874 if (ret < 0)
1875 break;
1877 /* Fall through for DMA_TO_DEVICE */
1878 case DMA_NONE:
1879 trace_target_cmd_complete(cmd);
1880 ret = cmd->se_tfo->queue_status(cmd);
1881 break;
1882 default:
1883 break;
1886 out:
1887 if (ret < 0) {
1888 transport_handle_queue_full(cmd, cmd->se_dev);
1889 return;
1891 transport_lun_remove_cmd(cmd);
1892 transport_cmd_check_stop_to_fabric(cmd);
1895 static void transport_handle_queue_full(
1896 struct se_cmd *cmd,
1897 struct se_device *dev)
1899 spin_lock_irq(&dev->qf_cmd_lock);
1900 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
1901 atomic_inc(&dev->dev_qf_count);
1902 smp_mb__after_atomic_inc();
1903 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
1905 schedule_work(&cmd->se_dev->qf_work_queue);
1908 static void target_complete_ok_work(struct work_struct *work)
1910 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
1911 int ret;
1914 * Check if we need to move delayed/dormant tasks from cmds on the
1915 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1916 * Attribute.
1918 transport_complete_task_attr(cmd);
1921 * Check to schedule QUEUE_FULL work, or execute an existing
1922 * cmd->transport_qf_callback()
1924 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
1925 schedule_work(&cmd->se_dev->qf_work_queue);
1928 * Check if we need to send a sense buffer from
1929 * the struct se_cmd in question.
1931 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1932 WARN_ON(!cmd->scsi_status);
1933 ret = transport_send_check_condition_and_sense(
1934 cmd, 0, 1);
1935 if (ret == -EAGAIN || ret == -ENOMEM)
1936 goto queue_full;
1938 transport_lun_remove_cmd(cmd);
1939 transport_cmd_check_stop_to_fabric(cmd);
1940 return;
1943 * Check for a callback, used by amongst other things
1944 * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
1946 if (cmd->transport_complete_callback) {
1947 sense_reason_t rc;
1949 rc = cmd->transport_complete_callback(cmd);
1950 if (!rc && !(cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE_POST)) {
1951 return;
1952 } else if (rc) {
1953 ret = transport_send_check_condition_and_sense(cmd,
1954 rc, 0);
1955 if (ret == -EAGAIN || ret == -ENOMEM)
1956 goto queue_full;
1958 transport_lun_remove_cmd(cmd);
1959 transport_cmd_check_stop_to_fabric(cmd);
1960 return;
1964 switch (cmd->data_direction) {
1965 case DMA_FROM_DEVICE:
1966 spin_lock(&cmd->se_lun->lun_sep_lock);
1967 if (cmd->se_lun->lun_sep) {
1968 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
1969 cmd->data_length;
1971 spin_unlock(&cmd->se_lun->lun_sep_lock);
1973 trace_target_cmd_complete(cmd);
1974 ret = cmd->se_tfo->queue_data_in(cmd);
1975 if (ret == -EAGAIN || ret == -ENOMEM)
1976 goto queue_full;
1977 break;
1978 case DMA_TO_DEVICE:
1979 spin_lock(&cmd->se_lun->lun_sep_lock);
1980 if (cmd->se_lun->lun_sep) {
1981 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
1982 cmd->data_length;
1984 spin_unlock(&cmd->se_lun->lun_sep_lock);
1986 * Check if we need to send READ payload for BIDI-COMMAND
1988 if (cmd->se_cmd_flags & SCF_BIDI) {
1989 spin_lock(&cmd->se_lun->lun_sep_lock);
1990 if (cmd->se_lun->lun_sep) {
1991 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
1992 cmd->data_length;
1994 spin_unlock(&cmd->se_lun->lun_sep_lock);
1995 ret = cmd->se_tfo->queue_data_in(cmd);
1996 if (ret == -EAGAIN || ret == -ENOMEM)
1997 goto queue_full;
1998 break;
2000 /* Fall through for DMA_TO_DEVICE */
2001 case DMA_NONE:
2002 trace_target_cmd_complete(cmd);
2003 ret = cmd->se_tfo->queue_status(cmd);
2004 if (ret == -EAGAIN || ret == -ENOMEM)
2005 goto queue_full;
2006 break;
2007 default:
2008 break;
2011 transport_lun_remove_cmd(cmd);
2012 transport_cmd_check_stop_to_fabric(cmd);
2013 return;
2015 queue_full:
2016 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
2017 " data_direction: %d\n", cmd, cmd->data_direction);
2018 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
2019 transport_handle_queue_full(cmd, cmd->se_dev);
2022 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
2024 struct scatterlist *sg;
2025 int count;
2027 for_each_sg(sgl, sg, nents, count)
2028 __free_page(sg_page(sg));
2030 kfree(sgl);
2033 static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
2036 * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
2037 * emulation, and free + reset pointers if necessary..
2039 if (!cmd->t_data_sg_orig)
2040 return;
2042 kfree(cmd->t_data_sg);
2043 cmd->t_data_sg = cmd->t_data_sg_orig;
2044 cmd->t_data_sg_orig = NULL;
2045 cmd->t_data_nents = cmd->t_data_nents_orig;
2046 cmd->t_data_nents_orig = 0;
2049 static inline void transport_free_pages(struct se_cmd *cmd)
2051 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) {
2052 transport_reset_sgl_orig(cmd);
2053 return;
2055 transport_reset_sgl_orig(cmd);
2057 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
2058 cmd->t_data_sg = NULL;
2059 cmd->t_data_nents = 0;
2061 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
2062 cmd->t_bidi_data_sg = NULL;
2063 cmd->t_bidi_data_nents = 0;
2065 transport_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents);
2066 cmd->t_prot_sg = NULL;
2067 cmd->t_prot_nents = 0;
2071 * transport_release_cmd - free a command
2072 * @cmd: command to free
2074 * This routine unconditionally frees a command, and reference counting
2075 * or list removal must be done in the caller.
2077 static int transport_release_cmd(struct se_cmd *cmd)
2079 BUG_ON(!cmd->se_tfo);
2081 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
2082 core_tmr_release_req(cmd->se_tmr_req);
2083 if (cmd->t_task_cdb != cmd->__t_task_cdb)
2084 kfree(cmd->t_task_cdb);
2086 * If this cmd has been setup with target_get_sess_cmd(), drop
2087 * the kref and call ->release_cmd() in kref callback.
2089 return target_put_sess_cmd(cmd->se_sess, cmd);
2093 * transport_put_cmd - release a reference to a command
2094 * @cmd: command to release
2096 * This routine releases our reference to the command and frees it if possible.
2098 static int transport_put_cmd(struct se_cmd *cmd)
2100 transport_free_pages(cmd);
2101 return transport_release_cmd(cmd);
2104 void *transport_kmap_data_sg(struct se_cmd *cmd)
2106 struct scatterlist *sg = cmd->t_data_sg;
2107 struct page **pages;
2108 int i;
2111 * We need to take into account a possible offset here for fabrics like
2112 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2113 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
2115 if (!cmd->t_data_nents)
2116 return NULL;
2118 BUG_ON(!sg);
2119 if (cmd->t_data_nents == 1)
2120 return kmap(sg_page(sg)) + sg->offset;
2122 /* >1 page. use vmap */
2123 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
2124 if (!pages)
2125 return NULL;
2127 /* convert sg[] to pages[] */
2128 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
2129 pages[i] = sg_page(sg);
2132 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
2133 kfree(pages);
2134 if (!cmd->t_data_vmap)
2135 return NULL;
2137 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
2139 EXPORT_SYMBOL(transport_kmap_data_sg);
2141 void transport_kunmap_data_sg(struct se_cmd *cmd)
2143 if (!cmd->t_data_nents) {
2144 return;
2145 } else if (cmd->t_data_nents == 1) {
2146 kunmap(sg_page(cmd->t_data_sg));
2147 return;
2150 vunmap(cmd->t_data_vmap);
2151 cmd->t_data_vmap = NULL;
2153 EXPORT_SYMBOL(transport_kunmap_data_sg);
2156 target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
2157 bool zero_page)
2159 struct scatterlist *sg;
2160 struct page *page;
2161 gfp_t zero_flag = (zero_page) ? __GFP_ZERO : 0;
2162 unsigned int nent;
2163 int i = 0;
2165 nent = DIV_ROUND_UP(length, PAGE_SIZE);
2166 sg = kmalloc(sizeof(struct scatterlist) * nent, GFP_KERNEL);
2167 if (!sg)
2168 return -ENOMEM;
2170 sg_init_table(sg, nent);
2172 while (length) {
2173 u32 page_len = min_t(u32, length, PAGE_SIZE);
2174 page = alloc_page(GFP_KERNEL | zero_flag);
2175 if (!page)
2176 goto out;
2178 sg_set_page(&sg[i], page, page_len, 0);
2179 length -= page_len;
2180 i++;
2182 *sgl = sg;
2183 *nents = nent;
2184 return 0;
2186 out:
2187 while (i > 0) {
2188 i--;
2189 __free_page(sg_page(&sg[i]));
2191 kfree(sg);
2192 return -ENOMEM;
2196 * Allocate any required resources to execute the command. For writes we
2197 * might not have the payload yet, so notify the fabric via a call to
2198 * ->write_pending instead. Otherwise place it on the execution queue.
2200 sense_reason_t
2201 transport_generic_new_cmd(struct se_cmd *cmd)
2203 int ret = 0;
2206 * Determine is the TCM fabric module has already allocated physical
2207 * memory, and is directly calling transport_generic_map_mem_to_cmd()
2208 * beforehand.
2210 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2211 cmd->data_length) {
2212 bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB);
2214 if ((cmd->se_cmd_flags & SCF_BIDI) ||
2215 (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) {
2216 u32 bidi_length;
2218 if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)
2219 bidi_length = cmd->t_task_nolb *
2220 cmd->se_dev->dev_attrib.block_size;
2221 else
2222 bidi_length = cmd->data_length;
2224 ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
2225 &cmd->t_bidi_data_nents,
2226 bidi_length, zero_flag);
2227 if (ret < 0)
2228 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2231 if (cmd->prot_op != TARGET_PROT_NORMAL) {
2232 ret = target_alloc_sgl(&cmd->t_prot_sg,
2233 &cmd->t_prot_nents,
2234 cmd->prot_length, true);
2235 if (ret < 0)
2236 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2239 ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
2240 cmd->data_length, zero_flag);
2241 if (ret < 0)
2242 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2245 * If this command is not a write we can execute it right here,
2246 * for write buffers we need to notify the fabric driver first
2247 * and let it call back once the write buffers are ready.
2249 target_add_to_state_list(cmd);
2250 if (cmd->data_direction != DMA_TO_DEVICE) {
2251 target_execute_cmd(cmd);
2252 return 0;
2254 transport_cmd_check_stop(cmd, false, true);
2256 ret = cmd->se_tfo->write_pending(cmd);
2257 if (ret == -EAGAIN || ret == -ENOMEM)
2258 goto queue_full;
2260 /* fabric drivers should only return -EAGAIN or -ENOMEM as error */
2261 WARN_ON(ret);
2263 return (!ret) ? 0 : TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2265 queue_full:
2266 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
2267 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
2268 transport_handle_queue_full(cmd, cmd->se_dev);
2269 return 0;
2271 EXPORT_SYMBOL(transport_generic_new_cmd);
2273 static void transport_write_pending_qf(struct se_cmd *cmd)
2275 int ret;
2277 ret = cmd->se_tfo->write_pending(cmd);
2278 if (ret == -EAGAIN || ret == -ENOMEM) {
2279 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2280 cmd);
2281 transport_handle_queue_full(cmd, cmd->se_dev);
2285 int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
2287 unsigned long flags;
2288 int ret = 0;
2290 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
2291 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
2292 transport_wait_for_tasks(cmd);
2294 ret = transport_release_cmd(cmd);
2295 } else {
2296 if (wait_for_tasks)
2297 transport_wait_for_tasks(cmd);
2299 * Handle WRITE failure case where transport_generic_new_cmd()
2300 * has already added se_cmd to state_list, but fabric has
2301 * failed command before I/O submission.
2303 if (cmd->state_active) {
2304 spin_lock_irqsave(&cmd->t_state_lock, flags);
2305 target_remove_from_state_list(cmd);
2306 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2309 if (cmd->se_lun)
2310 transport_lun_remove_cmd(cmd);
2312 ret = transport_put_cmd(cmd);
2314 return ret;
2316 EXPORT_SYMBOL(transport_generic_free_cmd);
2318 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
2319 * @se_sess: session to reference
2320 * @se_cmd: command descriptor to add
2321 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2323 int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
2324 bool ack_kref)
2326 unsigned long flags;
2327 int ret = 0;
2329 kref_init(&se_cmd->cmd_kref);
2331 * Add a second kref if the fabric caller is expecting to handle
2332 * fabric acknowledgement that requires two target_put_sess_cmd()
2333 * invocations before se_cmd descriptor release.
2335 if (ack_kref == true) {
2336 kref_get(&se_cmd->cmd_kref);
2337 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
2340 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2341 if (se_sess->sess_tearing_down) {
2342 ret = -ESHUTDOWN;
2343 goto out;
2345 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
2346 out:
2347 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2348 return ret;
2350 EXPORT_SYMBOL(target_get_sess_cmd);
2352 static void target_release_cmd_kref(struct kref *kref)
2354 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
2355 struct se_session *se_sess = se_cmd->se_sess;
2357 if (list_empty(&se_cmd->se_cmd_list)) {
2358 spin_unlock(&se_sess->sess_cmd_lock);
2359 se_cmd->se_tfo->release_cmd(se_cmd);
2360 return;
2362 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
2363 spin_unlock(&se_sess->sess_cmd_lock);
2364 complete(&se_cmd->cmd_wait_comp);
2365 return;
2367 list_del(&se_cmd->se_cmd_list);
2368 spin_unlock(&se_sess->sess_cmd_lock);
2370 se_cmd->se_tfo->release_cmd(se_cmd);
2373 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2374 * @se_sess: session to reference
2375 * @se_cmd: command descriptor to drop
2377 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
2379 return kref_put_spinlock_irqsave(&se_cmd->cmd_kref, target_release_cmd_kref,
2380 &se_sess->sess_cmd_lock);
2382 EXPORT_SYMBOL(target_put_sess_cmd);
2384 /* target_sess_cmd_list_set_waiting - Flag all commands in
2385 * sess_cmd_list to complete cmd_wait_comp. Set
2386 * sess_tearing_down so no more commands are queued.
2387 * @se_sess: session to flag
2389 void target_sess_cmd_list_set_waiting(struct se_session *se_sess)
2391 struct se_cmd *se_cmd;
2392 unsigned long flags;
2394 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2395 if (se_sess->sess_tearing_down) {
2396 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2397 return;
2399 se_sess->sess_tearing_down = 1;
2400 list_splice_init(&se_sess->sess_cmd_list, &se_sess->sess_wait_list);
2402 list_for_each_entry(se_cmd, &se_sess->sess_wait_list, se_cmd_list)
2403 se_cmd->cmd_wait_set = 1;
2405 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2407 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting);
2409 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
2410 * @se_sess: session to wait for active I/O
2412 void target_wait_for_sess_cmds(struct se_session *se_sess)
2414 struct se_cmd *se_cmd, *tmp_cmd;
2415 unsigned long flags;
2417 list_for_each_entry_safe(se_cmd, tmp_cmd,
2418 &se_sess->sess_wait_list, se_cmd_list) {
2419 list_del(&se_cmd->se_cmd_list);
2421 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2422 " %d\n", se_cmd, se_cmd->t_state,
2423 se_cmd->se_tfo->get_cmd_state(se_cmd));
2425 wait_for_completion(&se_cmd->cmd_wait_comp);
2426 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2427 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2428 se_cmd->se_tfo->get_cmd_state(se_cmd));
2430 se_cmd->se_tfo->release_cmd(se_cmd);
2433 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2434 WARN_ON(!list_empty(&se_sess->sess_cmd_list));
2435 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2438 EXPORT_SYMBOL(target_wait_for_sess_cmds);
2440 static int transport_clear_lun_ref_thread(void *p)
2442 struct se_lun *lun = p;
2444 percpu_ref_kill(&lun->lun_ref);
2446 wait_for_completion(&lun->lun_ref_comp);
2447 complete(&lun->lun_shutdown_comp);
2449 return 0;
2452 int transport_clear_lun_ref(struct se_lun *lun)
2454 struct task_struct *kt;
2456 kt = kthread_run(transport_clear_lun_ref_thread, lun,
2457 "tcm_cl_%u", lun->unpacked_lun);
2458 if (IS_ERR(kt)) {
2459 pr_err("Unable to start clear_lun thread\n");
2460 return PTR_ERR(kt);
2462 wait_for_completion(&lun->lun_shutdown_comp);
2464 return 0;
2468 * transport_wait_for_tasks - wait for completion to occur
2469 * @cmd: command to wait
2471 * Called from frontend fabric context to wait for storage engine
2472 * to pause and/or release frontend generated struct se_cmd.
2474 bool transport_wait_for_tasks(struct se_cmd *cmd)
2476 unsigned long flags;
2478 spin_lock_irqsave(&cmd->t_state_lock, flags);
2479 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2480 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
2481 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2482 return false;
2485 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2486 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
2487 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2488 return false;
2491 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
2492 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2493 return false;
2496 cmd->transport_state |= CMD_T_STOP;
2498 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
2499 " i_state: %d, t_state: %d, CMD_T_STOP\n",
2500 cmd, cmd->se_tfo->get_task_tag(cmd),
2501 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
2503 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2505 wait_for_completion(&cmd->t_transport_stop_comp);
2507 spin_lock_irqsave(&cmd->t_state_lock, flags);
2508 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
2510 pr_debug("wait_for_tasks: Stopped wait_for_completion("
2511 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
2512 cmd->se_tfo->get_task_tag(cmd));
2514 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2516 return true;
2518 EXPORT_SYMBOL(transport_wait_for_tasks);
2520 static int transport_get_sense_codes(
2521 struct se_cmd *cmd,
2522 u8 *asc,
2523 u8 *ascq)
2525 *asc = cmd->scsi_asc;
2526 *ascq = cmd->scsi_ascq;
2528 return 0;
2531 static
2532 void transport_err_sector_info(unsigned char *buffer, sector_t bad_sector)
2534 /* Place failed LBA in sense data information descriptor 0. */
2535 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 0xc;
2536 buffer[SPC_DESC_TYPE_OFFSET] = 0; /* Information */
2537 buffer[SPC_ADDITIONAL_DESC_LEN_OFFSET] = 0xa;
2538 buffer[SPC_VALIDITY_OFFSET] = 0x80;
2540 /* Descriptor Information: failing sector */
2541 put_unaligned_be64(bad_sector, &buffer[12]);
2545 transport_send_check_condition_and_sense(struct se_cmd *cmd,
2546 sense_reason_t reason, int from_transport)
2548 unsigned char *buffer = cmd->sense_buffer;
2549 unsigned long flags;
2550 u8 asc = 0, ascq = 0;
2552 spin_lock_irqsave(&cmd->t_state_lock, flags);
2553 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2554 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2555 return 0;
2557 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
2558 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2560 if (!reason && from_transport)
2561 goto after_reason;
2563 if (!from_transport)
2564 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
2567 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2568 * SENSE KEY values from include/scsi/scsi.h
2570 switch (reason) {
2571 case TCM_NO_SENSE:
2572 /* CURRENT ERROR */
2573 buffer[0] = 0x70;
2574 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2575 /* Not Ready */
2576 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
2577 /* NO ADDITIONAL SENSE INFORMATION */
2578 buffer[SPC_ASC_KEY_OFFSET] = 0;
2579 buffer[SPC_ASCQ_KEY_OFFSET] = 0;
2580 break;
2581 case TCM_NON_EXISTENT_LUN:
2582 /* CURRENT ERROR */
2583 buffer[0] = 0x70;
2584 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2585 /* ILLEGAL REQUEST */
2586 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2587 /* LOGICAL UNIT NOT SUPPORTED */
2588 buffer[SPC_ASC_KEY_OFFSET] = 0x25;
2589 break;
2590 case TCM_UNSUPPORTED_SCSI_OPCODE:
2591 case TCM_SECTOR_COUNT_TOO_MANY:
2592 /* CURRENT ERROR */
2593 buffer[0] = 0x70;
2594 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2595 /* ILLEGAL REQUEST */
2596 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2597 /* INVALID COMMAND OPERATION CODE */
2598 buffer[SPC_ASC_KEY_OFFSET] = 0x20;
2599 break;
2600 case TCM_UNKNOWN_MODE_PAGE:
2601 /* CURRENT ERROR */
2602 buffer[0] = 0x70;
2603 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2604 /* ILLEGAL REQUEST */
2605 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2606 /* INVALID FIELD IN CDB */
2607 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
2608 break;
2609 case TCM_CHECK_CONDITION_ABORT_CMD:
2610 /* CURRENT ERROR */
2611 buffer[0] = 0x70;
2612 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2613 /* ABORTED COMMAND */
2614 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2615 /* BUS DEVICE RESET FUNCTION OCCURRED */
2616 buffer[SPC_ASC_KEY_OFFSET] = 0x29;
2617 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03;
2618 break;
2619 case TCM_INCORRECT_AMOUNT_OF_DATA:
2620 /* CURRENT ERROR */
2621 buffer[0] = 0x70;
2622 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2623 /* ABORTED COMMAND */
2624 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2625 /* WRITE ERROR */
2626 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
2627 /* NOT ENOUGH UNSOLICITED DATA */
2628 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0d;
2629 break;
2630 case TCM_INVALID_CDB_FIELD:
2631 /* CURRENT ERROR */
2632 buffer[0] = 0x70;
2633 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2634 /* ILLEGAL REQUEST */
2635 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2636 /* INVALID FIELD IN CDB */
2637 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
2638 break;
2639 case TCM_INVALID_PARAMETER_LIST:
2640 /* CURRENT ERROR */
2641 buffer[0] = 0x70;
2642 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2643 /* ILLEGAL REQUEST */
2644 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2645 /* INVALID FIELD IN PARAMETER LIST */
2646 buffer[SPC_ASC_KEY_OFFSET] = 0x26;
2647 break;
2648 case TCM_PARAMETER_LIST_LENGTH_ERROR:
2649 /* CURRENT ERROR */
2650 buffer[0] = 0x70;
2651 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2652 /* ILLEGAL REQUEST */
2653 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2654 /* PARAMETER LIST LENGTH ERROR */
2655 buffer[SPC_ASC_KEY_OFFSET] = 0x1a;
2656 break;
2657 case TCM_UNEXPECTED_UNSOLICITED_DATA:
2658 /* CURRENT ERROR */
2659 buffer[0] = 0x70;
2660 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2661 /* ABORTED COMMAND */
2662 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2663 /* WRITE ERROR */
2664 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
2665 /* UNEXPECTED_UNSOLICITED_DATA */
2666 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0c;
2667 break;
2668 case TCM_SERVICE_CRC_ERROR:
2669 /* CURRENT ERROR */
2670 buffer[0] = 0x70;
2671 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2672 /* ABORTED COMMAND */
2673 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2674 /* PROTOCOL SERVICE CRC ERROR */
2675 buffer[SPC_ASC_KEY_OFFSET] = 0x47;
2676 /* N/A */
2677 buffer[SPC_ASCQ_KEY_OFFSET] = 0x05;
2678 break;
2679 case TCM_SNACK_REJECTED:
2680 /* CURRENT ERROR */
2681 buffer[0] = 0x70;
2682 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2683 /* ABORTED COMMAND */
2684 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2685 /* READ ERROR */
2686 buffer[SPC_ASC_KEY_OFFSET] = 0x11;
2687 /* FAILED RETRANSMISSION REQUEST */
2688 buffer[SPC_ASCQ_KEY_OFFSET] = 0x13;
2689 break;
2690 case TCM_WRITE_PROTECTED:
2691 /* CURRENT ERROR */
2692 buffer[0] = 0x70;
2693 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2694 /* DATA PROTECT */
2695 buffer[SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
2696 /* WRITE PROTECTED */
2697 buffer[SPC_ASC_KEY_OFFSET] = 0x27;
2698 break;
2699 case TCM_ADDRESS_OUT_OF_RANGE:
2700 /* CURRENT ERROR */
2701 buffer[0] = 0x70;
2702 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2703 /* ILLEGAL REQUEST */
2704 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2705 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2706 buffer[SPC_ASC_KEY_OFFSET] = 0x21;
2707 break;
2708 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
2709 /* CURRENT ERROR */
2710 buffer[0] = 0x70;
2711 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2712 /* UNIT ATTENTION */
2713 buffer[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
2714 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
2715 buffer[SPC_ASC_KEY_OFFSET] = asc;
2716 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
2717 break;
2718 case TCM_CHECK_CONDITION_NOT_READY:
2719 /* CURRENT ERROR */
2720 buffer[0] = 0x70;
2721 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2722 /* Not Ready */
2723 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
2724 transport_get_sense_codes(cmd, &asc, &ascq);
2725 buffer[SPC_ASC_KEY_OFFSET] = asc;
2726 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
2727 break;
2728 case TCM_MISCOMPARE_VERIFY:
2729 /* CURRENT ERROR */
2730 buffer[0] = 0x70;
2731 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2732 buffer[SPC_SENSE_KEY_OFFSET] = MISCOMPARE;
2733 /* MISCOMPARE DURING VERIFY OPERATION */
2734 buffer[SPC_ASC_KEY_OFFSET] = 0x1d;
2735 buffer[SPC_ASCQ_KEY_OFFSET] = 0x00;
2736 break;
2737 case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED:
2738 /* CURRENT ERROR */
2739 buffer[0] = 0x70;
2740 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2741 /* ILLEGAL REQUEST */
2742 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2743 /* LOGICAL BLOCK GUARD CHECK FAILED */
2744 buffer[SPC_ASC_KEY_OFFSET] = 0x10;
2745 buffer[SPC_ASCQ_KEY_OFFSET] = 0x01;
2746 transport_err_sector_info(buffer, cmd->bad_sector);
2747 break;
2748 case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED:
2749 /* CURRENT ERROR */
2750 buffer[0] = 0x70;
2751 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2752 /* ILLEGAL REQUEST */
2753 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2754 /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */
2755 buffer[SPC_ASC_KEY_OFFSET] = 0x10;
2756 buffer[SPC_ASCQ_KEY_OFFSET] = 0x02;
2757 transport_err_sector_info(buffer, cmd->bad_sector);
2758 break;
2759 case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED:
2760 /* CURRENT ERROR */
2761 buffer[0] = 0x70;
2762 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2763 /* ILLEGAL REQUEST */
2764 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2765 /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */
2766 buffer[SPC_ASC_KEY_OFFSET] = 0x10;
2767 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03;
2768 transport_err_sector_info(buffer, cmd->bad_sector);
2769 break;
2770 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
2771 default:
2772 /* CURRENT ERROR */
2773 buffer[0] = 0x70;
2774 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2776 * Returning ILLEGAL REQUEST would cause immediate IO errors on
2777 * Solaris initiators. Returning NOT READY instead means the
2778 * operations will be retried a finite number of times and we
2779 * can survive intermittent errors.
2781 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
2782 /* LOGICAL UNIT COMMUNICATION FAILURE */
2783 buffer[SPC_ASC_KEY_OFFSET] = 0x08;
2784 break;
2787 * This code uses linux/include/scsi/scsi.h SAM status codes!
2789 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
2791 * Automatically padded, this value is encoded in the fabric's
2792 * data_length response PDU containing the SCSI defined sense data.
2794 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
2796 after_reason:
2797 trace_target_cmd_complete(cmd);
2798 return cmd->se_tfo->queue_status(cmd);
2800 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
2802 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
2804 if (!(cmd->transport_state & CMD_T_ABORTED))
2805 return 0;
2808 * If cmd has been aborted but either no status is to be sent or it has
2809 * already been sent, just return
2811 if (!send_status || !(cmd->se_cmd_flags & SCF_SEND_DELAYED_TAS))
2812 return 1;
2814 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB: 0x%02x ITT: 0x%08x\n",
2815 cmd->t_task_cdb[0], cmd->se_tfo->get_task_tag(cmd));
2817 cmd->se_cmd_flags &= ~SCF_SEND_DELAYED_TAS;
2818 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
2819 trace_target_cmd_complete(cmd);
2820 cmd->se_tfo->queue_status(cmd);
2822 return 1;
2824 EXPORT_SYMBOL(transport_check_aborted_status);
2826 void transport_send_task_abort(struct se_cmd *cmd)
2828 unsigned long flags;
2830 spin_lock_irqsave(&cmd->t_state_lock, flags);
2831 if (cmd->se_cmd_flags & (SCF_SENT_CHECK_CONDITION)) {
2832 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2833 return;
2835 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2838 * If there are still expected incoming fabric WRITEs, we wait
2839 * until until they have completed before sending a TASK_ABORTED
2840 * response. This response with TASK_ABORTED status will be
2841 * queued back to fabric module by transport_check_aborted_status().
2843 if (cmd->data_direction == DMA_TO_DEVICE) {
2844 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
2845 cmd->transport_state |= CMD_T_ABORTED;
2846 cmd->se_cmd_flags |= SCF_SEND_DELAYED_TAS;
2847 smp_mb__after_atomic_inc();
2848 return;
2851 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
2853 transport_lun_remove_cmd(cmd);
2855 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
2856 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
2857 cmd->se_tfo->get_task_tag(cmd));
2859 trace_target_cmd_complete(cmd);
2860 cmd->se_tfo->queue_status(cmd);
2863 static void target_tmr_work(struct work_struct *work)
2865 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
2866 struct se_device *dev = cmd->se_dev;
2867 struct se_tmr_req *tmr = cmd->se_tmr_req;
2868 int ret;
2870 switch (tmr->function) {
2871 case TMR_ABORT_TASK:
2872 core_tmr_abort_task(dev, tmr, cmd->se_sess);
2873 break;
2874 case TMR_ABORT_TASK_SET:
2875 case TMR_CLEAR_ACA:
2876 case TMR_CLEAR_TASK_SET:
2877 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
2878 break;
2879 case TMR_LUN_RESET:
2880 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
2881 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
2882 TMR_FUNCTION_REJECTED;
2883 break;
2884 case TMR_TARGET_WARM_RESET:
2885 tmr->response = TMR_FUNCTION_REJECTED;
2886 break;
2887 case TMR_TARGET_COLD_RESET:
2888 tmr->response = TMR_FUNCTION_REJECTED;
2889 break;
2890 default:
2891 pr_err("Uknown TMR function: 0x%02x.\n",
2892 tmr->function);
2893 tmr->response = TMR_FUNCTION_REJECTED;
2894 break;
2897 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
2898 cmd->se_tfo->queue_tm_rsp(cmd);
2900 transport_cmd_check_stop_to_fabric(cmd);
2903 int transport_generic_handle_tmr(
2904 struct se_cmd *cmd)
2906 INIT_WORK(&cmd->work, target_tmr_work);
2907 queue_work(cmd->se_dev->tmr_wq, &cmd->work);
2908 return 0;
2910 EXPORT_SYMBOL(transport_generic_handle_tmr);