1 /*******************************************************************************
2 * Filename: target_core_transport.c
4 * This file contains the Generic Target Engine Core.
6 * (c) Copyright 2002-2012 RisingTide Systems LLC.
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/blkdev.h>
32 #include <linux/spinlock.h>
33 #include <linux/kthread.h>
35 #include <linux/cdrom.h>
36 #include <linux/module.h>
37 #include <linux/ratelimit.h>
38 #include <asm/unaligned.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_tcq.h>
45 #include <target/target_core_base.h>
46 #include <target/target_core_backend.h>
47 #include <target/target_core_fabric.h>
48 #include <target/target_core_configfs.h>
50 #include "target_core_internal.h"
51 #include "target_core_alua.h"
52 #include "target_core_pr.h"
53 #include "target_core_ua.h"
55 static struct workqueue_struct
*target_completion_wq
;
56 static struct kmem_cache
*se_sess_cache
;
57 struct kmem_cache
*se_ua_cache
;
58 struct kmem_cache
*t10_pr_reg_cache
;
59 struct kmem_cache
*t10_alua_lu_gp_cache
;
60 struct kmem_cache
*t10_alua_lu_gp_mem_cache
;
61 struct kmem_cache
*t10_alua_tg_pt_gp_cache
;
62 struct kmem_cache
*t10_alua_tg_pt_gp_mem_cache
;
64 static void transport_complete_task_attr(struct se_cmd
*cmd
);
65 static void transport_handle_queue_full(struct se_cmd
*cmd
,
66 struct se_device
*dev
);
67 static int transport_generic_get_mem(struct se_cmd
*cmd
);
68 static int target_get_sess_cmd(struct se_session
*, struct se_cmd
*, bool);
69 static void transport_put_cmd(struct se_cmd
*cmd
);
70 static void target_complete_ok_work(struct work_struct
*work
);
72 int init_se_kmem_caches(void)
74 se_sess_cache
= kmem_cache_create("se_sess_cache",
75 sizeof(struct se_session
), __alignof__(struct se_session
),
78 pr_err("kmem_cache_create() for struct se_session"
82 se_ua_cache
= kmem_cache_create("se_ua_cache",
83 sizeof(struct se_ua
), __alignof__(struct se_ua
),
86 pr_err("kmem_cache_create() for struct se_ua failed\n");
87 goto out_free_sess_cache
;
89 t10_pr_reg_cache
= kmem_cache_create("t10_pr_reg_cache",
90 sizeof(struct t10_pr_registration
),
91 __alignof__(struct t10_pr_registration
), 0, NULL
);
92 if (!t10_pr_reg_cache
) {
93 pr_err("kmem_cache_create() for struct t10_pr_registration"
95 goto out_free_ua_cache
;
97 t10_alua_lu_gp_cache
= kmem_cache_create("t10_alua_lu_gp_cache",
98 sizeof(struct t10_alua_lu_gp
), __alignof__(struct t10_alua_lu_gp
),
100 if (!t10_alua_lu_gp_cache
) {
101 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
103 goto out_free_pr_reg_cache
;
105 t10_alua_lu_gp_mem_cache
= kmem_cache_create("t10_alua_lu_gp_mem_cache",
106 sizeof(struct t10_alua_lu_gp_member
),
107 __alignof__(struct t10_alua_lu_gp_member
), 0, NULL
);
108 if (!t10_alua_lu_gp_mem_cache
) {
109 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
111 goto out_free_lu_gp_cache
;
113 t10_alua_tg_pt_gp_cache
= kmem_cache_create("t10_alua_tg_pt_gp_cache",
114 sizeof(struct t10_alua_tg_pt_gp
),
115 __alignof__(struct t10_alua_tg_pt_gp
), 0, NULL
);
116 if (!t10_alua_tg_pt_gp_cache
) {
117 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
119 goto out_free_lu_gp_mem_cache
;
121 t10_alua_tg_pt_gp_mem_cache
= kmem_cache_create(
122 "t10_alua_tg_pt_gp_mem_cache",
123 sizeof(struct t10_alua_tg_pt_gp_member
),
124 __alignof__(struct t10_alua_tg_pt_gp_member
),
126 if (!t10_alua_tg_pt_gp_mem_cache
) {
127 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
129 goto out_free_tg_pt_gp_cache
;
132 target_completion_wq
= alloc_workqueue("target_completion",
134 if (!target_completion_wq
)
135 goto out_free_tg_pt_gp_mem_cache
;
139 out_free_tg_pt_gp_mem_cache
:
140 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
141 out_free_tg_pt_gp_cache
:
142 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
143 out_free_lu_gp_mem_cache
:
144 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
145 out_free_lu_gp_cache
:
146 kmem_cache_destroy(t10_alua_lu_gp_cache
);
147 out_free_pr_reg_cache
:
148 kmem_cache_destroy(t10_pr_reg_cache
);
150 kmem_cache_destroy(se_ua_cache
);
152 kmem_cache_destroy(se_sess_cache
);
157 void release_se_kmem_caches(void)
159 destroy_workqueue(target_completion_wq
);
160 kmem_cache_destroy(se_sess_cache
);
161 kmem_cache_destroy(se_ua_cache
);
162 kmem_cache_destroy(t10_pr_reg_cache
);
163 kmem_cache_destroy(t10_alua_lu_gp_cache
);
164 kmem_cache_destroy(t10_alua_lu_gp_mem_cache
);
165 kmem_cache_destroy(t10_alua_tg_pt_gp_cache
);
166 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache
);
169 /* This code ensures unique mib indexes are handed out. */
170 static DEFINE_SPINLOCK(scsi_mib_index_lock
);
171 static u32 scsi_mib_index
[SCSI_INDEX_TYPE_MAX
];
174 * Allocate a new row index for the entry type specified
176 u32
scsi_get_new_index(scsi_index_t type
)
180 BUG_ON((type
< 0) || (type
>= SCSI_INDEX_TYPE_MAX
));
182 spin_lock(&scsi_mib_index_lock
);
183 new_index
= ++scsi_mib_index
[type
];
184 spin_unlock(&scsi_mib_index_lock
);
189 void transport_subsystem_check_init(void)
192 static int sub_api_initialized
;
194 if (sub_api_initialized
)
197 ret
= request_module("target_core_iblock");
199 pr_err("Unable to load target_core_iblock\n");
201 ret
= request_module("target_core_file");
203 pr_err("Unable to load target_core_file\n");
205 ret
= request_module("target_core_pscsi");
207 pr_err("Unable to load target_core_pscsi\n");
209 sub_api_initialized
= 1;
212 struct se_session
*transport_init_session(void)
214 struct se_session
*se_sess
;
216 se_sess
= kmem_cache_zalloc(se_sess_cache
, GFP_KERNEL
);
218 pr_err("Unable to allocate struct se_session from"
220 return ERR_PTR(-ENOMEM
);
222 INIT_LIST_HEAD(&se_sess
->sess_list
);
223 INIT_LIST_HEAD(&se_sess
->sess_acl_list
);
224 INIT_LIST_HEAD(&se_sess
->sess_cmd_list
);
225 spin_lock_init(&se_sess
->sess_cmd_lock
);
226 kref_init(&se_sess
->sess_kref
);
230 EXPORT_SYMBOL(transport_init_session
);
233 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
235 void __transport_register_session(
236 struct se_portal_group
*se_tpg
,
237 struct se_node_acl
*se_nacl
,
238 struct se_session
*se_sess
,
239 void *fabric_sess_ptr
)
241 unsigned char buf
[PR_REG_ISID_LEN
];
243 se_sess
->se_tpg
= se_tpg
;
244 se_sess
->fabric_sess_ptr
= fabric_sess_ptr
;
246 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
248 * Only set for struct se_session's that will actually be moving I/O.
249 * eg: *NOT* discovery sessions.
253 * If the fabric module supports an ISID based TransportID,
254 * save this value in binary from the fabric I_T Nexus now.
256 if (se_tpg
->se_tpg_tfo
->sess_get_initiator_sid
!= NULL
) {
257 memset(&buf
[0], 0, PR_REG_ISID_LEN
);
258 se_tpg
->se_tpg_tfo
->sess_get_initiator_sid(se_sess
,
259 &buf
[0], PR_REG_ISID_LEN
);
260 se_sess
->sess_bin_isid
= get_unaligned_be64(&buf
[0]);
262 kref_get(&se_nacl
->acl_kref
);
264 spin_lock_irq(&se_nacl
->nacl_sess_lock
);
266 * The se_nacl->nacl_sess pointer will be set to the
267 * last active I_T Nexus for each struct se_node_acl.
269 se_nacl
->nacl_sess
= se_sess
;
271 list_add_tail(&se_sess
->sess_acl_list
,
272 &se_nacl
->acl_sess_list
);
273 spin_unlock_irq(&se_nacl
->nacl_sess_lock
);
275 list_add_tail(&se_sess
->sess_list
, &se_tpg
->tpg_sess_list
);
277 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
278 se_tpg
->se_tpg_tfo
->get_fabric_name(), se_sess
->fabric_sess_ptr
);
280 EXPORT_SYMBOL(__transport_register_session
);
282 void transport_register_session(
283 struct se_portal_group
*se_tpg
,
284 struct se_node_acl
*se_nacl
,
285 struct se_session
*se_sess
,
286 void *fabric_sess_ptr
)
290 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
291 __transport_register_session(se_tpg
, se_nacl
, se_sess
, fabric_sess_ptr
);
292 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
294 EXPORT_SYMBOL(transport_register_session
);
296 static void target_release_session(struct kref
*kref
)
298 struct se_session
*se_sess
= container_of(kref
,
299 struct se_session
, sess_kref
);
300 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
302 se_tpg
->se_tpg_tfo
->close_session(se_sess
);
305 void target_get_session(struct se_session
*se_sess
)
307 kref_get(&se_sess
->sess_kref
);
309 EXPORT_SYMBOL(target_get_session
);
311 void target_put_session(struct se_session
*se_sess
)
313 struct se_portal_group
*tpg
= se_sess
->se_tpg
;
315 if (tpg
->se_tpg_tfo
->put_session
!= NULL
) {
316 tpg
->se_tpg_tfo
->put_session(se_sess
);
319 kref_put(&se_sess
->sess_kref
, target_release_session
);
321 EXPORT_SYMBOL(target_put_session
);
323 static void target_complete_nacl(struct kref
*kref
)
325 struct se_node_acl
*nacl
= container_of(kref
,
326 struct se_node_acl
, acl_kref
);
328 complete(&nacl
->acl_free_comp
);
331 void target_put_nacl(struct se_node_acl
*nacl
)
333 kref_put(&nacl
->acl_kref
, target_complete_nacl
);
336 void transport_deregister_session_configfs(struct se_session
*se_sess
)
338 struct se_node_acl
*se_nacl
;
341 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
343 se_nacl
= se_sess
->se_node_acl
;
345 spin_lock_irqsave(&se_nacl
->nacl_sess_lock
, flags
);
346 if (se_nacl
->acl_stop
== 0)
347 list_del(&se_sess
->sess_acl_list
);
349 * If the session list is empty, then clear the pointer.
350 * Otherwise, set the struct se_session pointer from the tail
351 * element of the per struct se_node_acl active session list.
353 if (list_empty(&se_nacl
->acl_sess_list
))
354 se_nacl
->nacl_sess
= NULL
;
356 se_nacl
->nacl_sess
= container_of(
357 se_nacl
->acl_sess_list
.prev
,
358 struct se_session
, sess_acl_list
);
360 spin_unlock_irqrestore(&se_nacl
->nacl_sess_lock
, flags
);
363 EXPORT_SYMBOL(transport_deregister_session_configfs
);
365 void transport_free_session(struct se_session
*se_sess
)
367 kmem_cache_free(se_sess_cache
, se_sess
);
369 EXPORT_SYMBOL(transport_free_session
);
371 void transport_deregister_session(struct se_session
*se_sess
)
373 struct se_portal_group
*se_tpg
= se_sess
->se_tpg
;
374 struct target_core_fabric_ops
*se_tfo
;
375 struct se_node_acl
*se_nacl
;
377 bool comp_nacl
= true;
380 transport_free_session(se_sess
);
383 se_tfo
= se_tpg
->se_tpg_tfo
;
385 spin_lock_irqsave(&se_tpg
->session_lock
, flags
);
386 list_del(&se_sess
->sess_list
);
387 se_sess
->se_tpg
= NULL
;
388 se_sess
->fabric_sess_ptr
= NULL
;
389 spin_unlock_irqrestore(&se_tpg
->session_lock
, flags
);
392 * Determine if we need to do extra work for this initiator node's
393 * struct se_node_acl if it had been previously dynamically generated.
395 se_nacl
= se_sess
->se_node_acl
;
397 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
398 if (se_nacl
&& se_nacl
->dynamic_node_acl
) {
399 if (!se_tfo
->tpg_check_demo_mode_cache(se_tpg
)) {
400 list_del(&se_nacl
->acl_list
);
401 se_tpg
->num_node_acls
--;
402 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
403 core_tpg_wait_for_nacl_pr_ref(se_nacl
);
404 core_free_device_list_for_node(se_nacl
, se_tpg
);
405 se_tfo
->tpg_release_fabric_acl(se_tpg
, se_nacl
);
408 spin_lock_irqsave(&se_tpg
->acl_node_lock
, flags
);
411 spin_unlock_irqrestore(&se_tpg
->acl_node_lock
, flags
);
413 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
414 se_tpg
->se_tpg_tfo
->get_fabric_name());
416 * If last kref is dropping now for an explict NodeACL, awake sleeping
417 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
420 if (se_nacl
&& comp_nacl
== true)
421 target_put_nacl(se_nacl
);
423 transport_free_session(se_sess
);
425 EXPORT_SYMBOL(transport_deregister_session
);
428 * Called with cmd->t_state_lock held.
430 static void target_remove_from_state_list(struct se_cmd
*cmd
)
432 struct se_device
*dev
= cmd
->se_dev
;
438 if (cmd
->transport_state
& CMD_T_BUSY
)
441 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
442 if (cmd
->state_active
) {
443 list_del(&cmd
->state_list
);
444 cmd
->state_active
= false;
446 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
449 static int transport_cmd_check_stop(struct se_cmd
*cmd
, bool remove_from_lists
)
453 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
455 * Determine if IOCTL context caller in requesting the stopping of this
456 * command for LUN shutdown purposes.
458 if (cmd
->transport_state
& CMD_T_LUN_STOP
) {
459 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
460 __func__
, __LINE__
, cmd
->se_tfo
->get_task_tag(cmd
));
462 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
463 if (remove_from_lists
)
464 target_remove_from_state_list(cmd
);
465 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
467 complete(&cmd
->transport_lun_stop_comp
);
471 if (remove_from_lists
) {
472 target_remove_from_state_list(cmd
);
475 * Clear struct se_cmd->se_lun before the handoff to FE.
481 * Determine if frontend context caller is requesting the stopping of
482 * this command for frontend exceptions.
484 if (cmd
->transport_state
& CMD_T_STOP
) {
485 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
487 cmd
->se_tfo
->get_task_tag(cmd
));
489 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
491 complete(&cmd
->t_transport_stop_comp
);
495 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
496 if (remove_from_lists
) {
498 * Some fabric modules like tcm_loop can release
499 * their internally allocated I/O reference now and
502 * Fabric modules are expected to return '1' here if the
503 * se_cmd being passed is released at this point,
504 * or zero if not being released.
506 if (cmd
->se_tfo
->check_stop_free
!= NULL
) {
507 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
508 return cmd
->se_tfo
->check_stop_free(cmd
);
512 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
516 static int transport_cmd_check_stop_to_fabric(struct se_cmd
*cmd
)
518 return transport_cmd_check_stop(cmd
, true);
521 static void transport_lun_remove_cmd(struct se_cmd
*cmd
)
523 struct se_lun
*lun
= cmd
->se_lun
;
529 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
530 if (cmd
->transport_state
& CMD_T_DEV_ACTIVE
) {
531 cmd
->transport_state
&= ~CMD_T_DEV_ACTIVE
;
532 target_remove_from_state_list(cmd
);
534 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
536 spin_lock_irqsave(&lun
->lun_cmd_lock
, flags
);
537 if (!list_empty(&cmd
->se_lun_node
))
538 list_del_init(&cmd
->se_lun_node
);
539 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, flags
);
542 void transport_cmd_finish_abort(struct se_cmd
*cmd
, int remove
)
544 if (transport_cmd_check_stop_to_fabric(cmd
))
547 transport_put_cmd(cmd
);
550 static void target_complete_failure_work(struct work_struct
*work
)
552 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
554 transport_generic_request_failure(cmd
,
555 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
);
559 * Used when asking transport to copy Sense Data from the underlying
560 * Linux/SCSI struct scsi_cmnd
562 static unsigned char *transport_get_sense_buffer(struct se_cmd
*cmd
)
564 struct se_device
*dev
= cmd
->se_dev
;
566 WARN_ON(!cmd
->se_lun
);
571 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
)
574 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
576 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
577 dev
->se_hba
->hba_id
, dev
->transport
->name
, cmd
->scsi_status
);
578 return cmd
->sense_buffer
;
581 void target_complete_cmd(struct se_cmd
*cmd
, u8 scsi_status
)
583 struct se_device
*dev
= cmd
->se_dev
;
584 int success
= scsi_status
== GOOD
;
587 cmd
->scsi_status
= scsi_status
;
590 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
591 cmd
->transport_state
&= ~CMD_T_BUSY
;
593 if (dev
&& dev
->transport
->transport_complete
) {
594 dev
->transport
->transport_complete(cmd
,
596 transport_get_sense_buffer(cmd
));
597 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
)
602 * See if we are waiting to complete for an exception condition.
604 if (cmd
->transport_state
& CMD_T_REQUEST_STOP
) {
605 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
606 complete(&cmd
->task_stop_comp
);
611 cmd
->transport_state
|= CMD_T_FAILED
;
614 * Check for case where an explict ABORT_TASK has been received
615 * and transport_wait_for_tasks() will be waiting for completion..
617 if (cmd
->transport_state
& CMD_T_ABORTED
&&
618 cmd
->transport_state
& CMD_T_STOP
) {
619 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
620 complete(&cmd
->t_transport_stop_comp
);
622 } else if (cmd
->transport_state
& CMD_T_FAILED
) {
623 INIT_WORK(&cmd
->work
, target_complete_failure_work
);
625 INIT_WORK(&cmd
->work
, target_complete_ok_work
);
628 cmd
->t_state
= TRANSPORT_COMPLETE
;
629 cmd
->transport_state
|= (CMD_T_COMPLETE
| CMD_T_ACTIVE
);
630 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
632 queue_work(target_completion_wq
, &cmd
->work
);
634 EXPORT_SYMBOL(target_complete_cmd
);
636 static void target_add_to_state_list(struct se_cmd
*cmd
)
638 struct se_device
*dev
= cmd
->se_dev
;
641 spin_lock_irqsave(&dev
->execute_task_lock
, flags
);
642 if (!cmd
->state_active
) {
643 list_add_tail(&cmd
->state_list
, &dev
->state_list
);
644 cmd
->state_active
= true;
646 spin_unlock_irqrestore(&dev
->execute_task_lock
, flags
);
650 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
652 static void transport_write_pending_qf(struct se_cmd
*cmd
);
653 static void transport_complete_qf(struct se_cmd
*cmd
);
655 void target_qf_do_work(struct work_struct
*work
)
657 struct se_device
*dev
= container_of(work
, struct se_device
,
659 LIST_HEAD(qf_cmd_list
);
660 struct se_cmd
*cmd
, *cmd_tmp
;
662 spin_lock_irq(&dev
->qf_cmd_lock
);
663 list_splice_init(&dev
->qf_cmd_list
, &qf_cmd_list
);
664 spin_unlock_irq(&dev
->qf_cmd_lock
);
666 list_for_each_entry_safe(cmd
, cmd_tmp
, &qf_cmd_list
, se_qf_node
) {
667 list_del(&cmd
->se_qf_node
);
668 atomic_dec(&dev
->dev_qf_count
);
669 smp_mb__after_atomic_dec();
671 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
672 " context: %s\n", cmd
->se_tfo
->get_fabric_name(), cmd
,
673 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
) ? "COMPLETE_OK" :
674 (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
) ? "WRITE_PENDING"
677 if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_WP
)
678 transport_write_pending_qf(cmd
);
679 else if (cmd
->t_state
== TRANSPORT_COMPLETE_QF_OK
)
680 transport_complete_qf(cmd
);
684 unsigned char *transport_dump_cmd_direction(struct se_cmd
*cmd
)
686 switch (cmd
->data_direction
) {
689 case DMA_FROM_DEVICE
:
693 case DMA_BIDIRECTIONAL
:
702 void transport_dump_dev_state(
703 struct se_device
*dev
,
707 *bl
+= sprintf(b
+ *bl
, "Status: ");
708 if (dev
->export_count
)
709 *bl
+= sprintf(b
+ *bl
, "ACTIVATED");
711 *bl
+= sprintf(b
+ *bl
, "DEACTIVATED");
713 *bl
+= sprintf(b
+ *bl
, " Max Queue Depth: %d", dev
->queue_depth
);
714 *bl
+= sprintf(b
+ *bl
, " SectorSize: %u HwMaxSectors: %u\n",
715 dev
->dev_attrib
.block_size
,
716 dev
->dev_attrib
.hw_max_sectors
);
717 *bl
+= sprintf(b
+ *bl
, " ");
720 void transport_dump_vpd_proto_id(
722 unsigned char *p_buf
,
725 unsigned char buf
[VPD_TMP_BUF_SIZE
];
728 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
729 len
= sprintf(buf
, "T10 VPD Protocol Identifier: ");
731 switch (vpd
->protocol_identifier
) {
733 sprintf(buf
+len
, "Fibre Channel\n");
736 sprintf(buf
+len
, "Parallel SCSI\n");
739 sprintf(buf
+len
, "SSA\n");
742 sprintf(buf
+len
, "IEEE 1394\n");
745 sprintf(buf
+len
, "SCSI Remote Direct Memory Access"
749 sprintf(buf
+len
, "Internet SCSI (iSCSI)\n");
752 sprintf(buf
+len
, "SAS Serial SCSI Protocol\n");
755 sprintf(buf
+len
, "Automation/Drive Interface Transport"
759 sprintf(buf
+len
, "AT Attachment Interface ATA/ATAPI\n");
762 sprintf(buf
+len
, "Unknown 0x%02x\n",
763 vpd
->protocol_identifier
);
768 strncpy(p_buf
, buf
, p_buf_len
);
774 transport_set_vpd_proto_id(struct t10_vpd
*vpd
, unsigned char *page_83
)
777 * Check if the Protocol Identifier Valid (PIV) bit is set..
779 * from spc3r23.pdf section 7.5.1
781 if (page_83
[1] & 0x80) {
782 vpd
->protocol_identifier
= (page_83
[0] & 0xf0);
783 vpd
->protocol_identifier_set
= 1;
784 transport_dump_vpd_proto_id(vpd
, NULL
, 0);
787 EXPORT_SYMBOL(transport_set_vpd_proto_id
);
789 int transport_dump_vpd_assoc(
791 unsigned char *p_buf
,
794 unsigned char buf
[VPD_TMP_BUF_SIZE
];
798 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
799 len
= sprintf(buf
, "T10 VPD Identifier Association: ");
801 switch (vpd
->association
) {
803 sprintf(buf
+len
, "addressed logical unit\n");
806 sprintf(buf
+len
, "target port\n");
809 sprintf(buf
+len
, "SCSI target device\n");
812 sprintf(buf
+len
, "Unknown 0x%02x\n", vpd
->association
);
818 strncpy(p_buf
, buf
, p_buf_len
);
825 int transport_set_vpd_assoc(struct t10_vpd
*vpd
, unsigned char *page_83
)
828 * The VPD identification association..
830 * from spc3r23.pdf Section 7.6.3.1 Table 297
832 vpd
->association
= (page_83
[1] & 0x30);
833 return transport_dump_vpd_assoc(vpd
, NULL
, 0);
835 EXPORT_SYMBOL(transport_set_vpd_assoc
);
837 int transport_dump_vpd_ident_type(
839 unsigned char *p_buf
,
842 unsigned char buf
[VPD_TMP_BUF_SIZE
];
846 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
847 len
= sprintf(buf
, "T10 VPD Identifier Type: ");
849 switch (vpd
->device_identifier_type
) {
851 sprintf(buf
+len
, "Vendor specific\n");
854 sprintf(buf
+len
, "T10 Vendor ID based\n");
857 sprintf(buf
+len
, "EUI-64 based\n");
860 sprintf(buf
+len
, "NAA\n");
863 sprintf(buf
+len
, "Relative target port identifier\n");
866 sprintf(buf
+len
, "SCSI name string\n");
869 sprintf(buf
+len
, "Unsupported: 0x%02x\n",
870 vpd
->device_identifier_type
);
876 if (p_buf_len
< strlen(buf
)+1)
878 strncpy(p_buf
, buf
, p_buf_len
);
886 int transport_set_vpd_ident_type(struct t10_vpd
*vpd
, unsigned char *page_83
)
889 * The VPD identifier type..
891 * from spc3r23.pdf Section 7.6.3.1 Table 298
893 vpd
->device_identifier_type
= (page_83
[1] & 0x0f);
894 return transport_dump_vpd_ident_type(vpd
, NULL
, 0);
896 EXPORT_SYMBOL(transport_set_vpd_ident_type
);
898 int transport_dump_vpd_ident(
900 unsigned char *p_buf
,
903 unsigned char buf
[VPD_TMP_BUF_SIZE
];
906 memset(buf
, 0, VPD_TMP_BUF_SIZE
);
908 switch (vpd
->device_identifier_code_set
) {
909 case 0x01: /* Binary */
910 snprintf(buf
, sizeof(buf
),
911 "T10 VPD Binary Device Identifier: %s\n",
912 &vpd
->device_identifier
[0]);
914 case 0x02: /* ASCII */
915 snprintf(buf
, sizeof(buf
),
916 "T10 VPD ASCII Device Identifier: %s\n",
917 &vpd
->device_identifier
[0]);
919 case 0x03: /* UTF-8 */
920 snprintf(buf
, sizeof(buf
),
921 "T10 VPD UTF-8 Device Identifier: %s\n",
922 &vpd
->device_identifier
[0]);
925 sprintf(buf
, "T10 VPD Device Identifier encoding unsupported:"
926 " 0x%02x", vpd
->device_identifier_code_set
);
932 strncpy(p_buf
, buf
, p_buf_len
);
940 transport_set_vpd_ident(struct t10_vpd
*vpd
, unsigned char *page_83
)
942 static const char hex_str
[] = "0123456789abcdef";
943 int j
= 0, i
= 4; /* offset to start of the identifier */
946 * The VPD Code Set (encoding)
948 * from spc3r23.pdf Section 7.6.3.1 Table 296
950 vpd
->device_identifier_code_set
= (page_83
[0] & 0x0f);
951 switch (vpd
->device_identifier_code_set
) {
952 case 0x01: /* Binary */
953 vpd
->device_identifier
[j
++] =
954 hex_str
[vpd
->device_identifier_type
];
955 while (i
< (4 + page_83
[3])) {
956 vpd
->device_identifier
[j
++] =
957 hex_str
[(page_83
[i
] & 0xf0) >> 4];
958 vpd
->device_identifier
[j
++] =
959 hex_str
[page_83
[i
] & 0x0f];
963 case 0x02: /* ASCII */
964 case 0x03: /* UTF-8 */
965 while (i
< (4 + page_83
[3]))
966 vpd
->device_identifier
[j
++] = page_83
[i
++];
972 return transport_dump_vpd_ident(vpd
, NULL
, 0);
974 EXPORT_SYMBOL(transport_set_vpd_ident
);
977 target_cmd_size_check(struct se_cmd
*cmd
, unsigned int size
)
979 struct se_device
*dev
= cmd
->se_dev
;
981 if (cmd
->unknown_data_length
) {
982 cmd
->data_length
= size
;
983 } else if (size
!= cmd
->data_length
) {
984 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
985 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
986 " 0x%02x\n", cmd
->se_tfo
->get_fabric_name(),
987 cmd
->data_length
, size
, cmd
->t_task_cdb
[0]);
989 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
990 pr_err("Rejecting underflow/overflow"
992 return TCM_INVALID_CDB_FIELD
;
995 * Reject READ_* or WRITE_* with overflow/underflow for
996 * type SCF_SCSI_DATA_CDB.
998 if (dev
->dev_attrib
.block_size
!= 512) {
999 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1000 " CDB on non 512-byte sector setup subsystem"
1001 " plugin: %s\n", dev
->transport
->name
);
1002 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1003 return TCM_INVALID_CDB_FIELD
;
1006 * For the overflow case keep the existing fabric provided
1007 * ->data_length. Otherwise for the underflow case, reset
1008 * ->data_length to the smaller SCSI expected data transfer
1011 if (size
> cmd
->data_length
) {
1012 cmd
->se_cmd_flags
|= SCF_OVERFLOW_BIT
;
1013 cmd
->residual_count
= (size
- cmd
->data_length
);
1015 cmd
->se_cmd_flags
|= SCF_UNDERFLOW_BIT
;
1016 cmd
->residual_count
= (cmd
->data_length
- size
);
1017 cmd
->data_length
= size
;
1026 * Used by fabric modules containing a local struct se_cmd within their
1027 * fabric dependent per I/O descriptor.
1029 void transport_init_se_cmd(
1031 struct target_core_fabric_ops
*tfo
,
1032 struct se_session
*se_sess
,
1036 unsigned char *sense_buffer
)
1038 INIT_LIST_HEAD(&cmd
->se_lun_node
);
1039 INIT_LIST_HEAD(&cmd
->se_delayed_node
);
1040 INIT_LIST_HEAD(&cmd
->se_qf_node
);
1041 INIT_LIST_HEAD(&cmd
->se_cmd_list
);
1042 INIT_LIST_HEAD(&cmd
->state_list
);
1043 init_completion(&cmd
->transport_lun_fe_stop_comp
);
1044 init_completion(&cmd
->transport_lun_stop_comp
);
1045 init_completion(&cmd
->t_transport_stop_comp
);
1046 init_completion(&cmd
->cmd_wait_comp
);
1047 init_completion(&cmd
->task_stop_comp
);
1048 spin_lock_init(&cmd
->t_state_lock
);
1049 cmd
->transport_state
= CMD_T_DEV_ACTIVE
;
1052 cmd
->se_sess
= se_sess
;
1053 cmd
->data_length
= data_length
;
1054 cmd
->data_direction
= data_direction
;
1055 cmd
->sam_task_attr
= task_attr
;
1056 cmd
->sense_buffer
= sense_buffer
;
1058 cmd
->state_active
= false;
1060 EXPORT_SYMBOL(transport_init_se_cmd
);
1062 static sense_reason_t
1063 transport_check_alloc_task_attr(struct se_cmd
*cmd
)
1065 struct se_device
*dev
= cmd
->se_dev
;
1068 * Check if SAM Task Attribute emulation is enabled for this
1069 * struct se_device storage object
1071 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1074 if (cmd
->sam_task_attr
== MSG_ACA_TAG
) {
1075 pr_debug("SAM Task Attribute ACA"
1076 " emulation is not supported\n");
1077 return TCM_INVALID_CDB_FIELD
;
1080 * Used to determine when ORDERED commands should go from
1081 * Dormant to Active status.
1083 cmd
->se_ordered_id
= atomic_inc_return(&dev
->dev_ordered_id
);
1084 smp_mb__after_atomic_inc();
1085 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1086 cmd
->se_ordered_id
, cmd
->sam_task_attr
,
1087 dev
->transport
->name
);
1092 target_setup_cmd_from_cdb(struct se_cmd
*cmd
, unsigned char *cdb
)
1094 struct se_device
*dev
= cmd
->se_dev
;
1095 unsigned long flags
;
1099 * Ensure that the received CDB is less than the max (252 + 8) bytes
1100 * for VARIABLE_LENGTH_CMD
1102 if (scsi_command_size(cdb
) > SCSI_MAX_VARLEN_CDB_SIZE
) {
1103 pr_err("Received SCSI CDB with command_size: %d that"
1104 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1105 scsi_command_size(cdb
), SCSI_MAX_VARLEN_CDB_SIZE
);
1106 return TCM_INVALID_CDB_FIELD
;
1109 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1110 * allocate the additional extended CDB buffer now.. Otherwise
1111 * setup the pointer from __t_task_cdb to t_task_cdb.
1113 if (scsi_command_size(cdb
) > sizeof(cmd
->__t_task_cdb
)) {
1114 cmd
->t_task_cdb
= kzalloc(scsi_command_size(cdb
),
1116 if (!cmd
->t_task_cdb
) {
1117 pr_err("Unable to allocate cmd->t_task_cdb"
1118 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1119 scsi_command_size(cdb
),
1120 (unsigned long)sizeof(cmd
->__t_task_cdb
));
1121 return TCM_OUT_OF_RESOURCES
;
1124 cmd
->t_task_cdb
= &cmd
->__t_task_cdb
[0];
1126 * Copy the original CDB into cmd->
1128 memcpy(cmd
->t_task_cdb
, cdb
, scsi_command_size(cdb
));
1131 * Check for an existing UNIT ATTENTION condition
1133 ret
= target_scsi3_ua_check(cmd
);
1137 ret
= target_alua_state_check(cmd
);
1141 ret
= target_check_reservation(cmd
);
1145 ret
= dev
->transport
->parse_cdb(cmd
);
1149 ret
= transport_check_alloc_task_attr(cmd
);
1153 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
1154 cmd
->se_cmd_flags
|= SCF_SUPPORTED_SAM_OPCODE
;
1155 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
1157 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1158 if (cmd
->se_lun
->lun_sep
)
1159 cmd
->se_lun
->lun_sep
->sep_stats
.cmd_pdus
++;
1160 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1163 EXPORT_SYMBOL(target_setup_cmd_from_cdb
);
1166 * Used by fabric module frontends to queue tasks directly.
1167 * Many only be used from process context only
1169 int transport_handle_cdb_direct(
1176 pr_err("cmd->se_lun is NULL\n");
1179 if (in_interrupt()) {
1181 pr_err("transport_generic_handle_cdb cannot be called"
1182 " from interrupt context\n");
1186 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1187 * outstanding descriptors are handled correctly during shutdown via
1188 * transport_wait_for_tasks()
1190 * Also, we don't take cmd->t_state_lock here as we only expect
1191 * this to be called for initial descriptor submission.
1193 cmd
->t_state
= TRANSPORT_NEW_CMD
;
1194 cmd
->transport_state
|= CMD_T_ACTIVE
;
1197 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1198 * so follow TRANSPORT_NEW_CMD processing thread context usage
1199 * and call transport_generic_request_failure() if necessary..
1201 ret
= transport_generic_new_cmd(cmd
);
1203 transport_generic_request_failure(cmd
, ret
);
1206 EXPORT_SYMBOL(transport_handle_cdb_direct
);
1208 static sense_reason_t
1209 transport_generic_map_mem_to_cmd(struct se_cmd
*cmd
, struct scatterlist
*sgl
,
1210 u32 sgl_count
, struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
)
1212 if (!sgl
|| !sgl_count
)
1216 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
1217 * scatterlists already have been set to follow what the fabric
1218 * passes for the original expected data transfer length.
1220 if (cmd
->se_cmd_flags
& SCF_OVERFLOW_BIT
) {
1221 pr_warn("Rejecting SCSI DATA overflow for fabric using"
1222 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
1223 return TCM_INVALID_CDB_FIELD
;
1226 cmd
->t_data_sg
= sgl
;
1227 cmd
->t_data_nents
= sgl_count
;
1229 if (sgl_bidi
&& sgl_bidi_count
) {
1230 cmd
->t_bidi_data_sg
= sgl_bidi
;
1231 cmd
->t_bidi_data_nents
= sgl_bidi_count
;
1233 cmd
->se_cmd_flags
|= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
;
1238 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1239 * se_cmd + use pre-allocated SGL memory.
1241 * @se_cmd: command descriptor to submit
1242 * @se_sess: associated se_sess for endpoint
1243 * @cdb: pointer to SCSI CDB
1244 * @sense: pointer to SCSI sense buffer
1245 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1246 * @data_length: fabric expected data transfer length
1247 * @task_addr: SAM task attribute
1248 * @data_dir: DMA data direction
1249 * @flags: flags for command submission from target_sc_flags_tables
1250 * @sgl: struct scatterlist memory for unidirectional mapping
1251 * @sgl_count: scatterlist count for unidirectional mapping
1252 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1253 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1255 * Returns non zero to signal active I/O shutdown failure. All other
1256 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1257 * but still return zero here.
1259 * This may only be called from process context, and also currently
1260 * assumes internal allocation of fabric payload buffer by target-core.
1262 int target_submit_cmd_map_sgls(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1263 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1264 u32 data_length
, int task_attr
, int data_dir
, int flags
,
1265 struct scatterlist
*sgl
, u32 sgl_count
,
1266 struct scatterlist
*sgl_bidi
, u32 sgl_bidi_count
)
1268 struct se_portal_group
*se_tpg
;
1272 se_tpg
= se_sess
->se_tpg
;
1274 BUG_ON(se_cmd
->se_tfo
|| se_cmd
->se_sess
);
1275 BUG_ON(in_interrupt());
1277 * Initialize se_cmd for target operation. From this point
1278 * exceptions are handled by sending exception status via
1279 * target_core_fabric_ops->queue_status() callback
1281 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1282 data_length
, data_dir
, task_attr
, sense
);
1283 if (flags
& TARGET_SCF_UNKNOWN_SIZE
)
1284 se_cmd
->unknown_data_length
= 1;
1286 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1287 * se_sess->sess_cmd_list. A second kref_get here is necessary
1288 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1289 * kref_put() to happen during fabric packet acknowledgement.
1291 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1295 * Signal bidirectional data payloads to target-core
1297 if (flags
& TARGET_SCF_BIDI_OP
)
1298 se_cmd
->se_cmd_flags
|= SCF_BIDI
;
1300 * Locate se_lun pointer and attach it to struct se_cmd
1302 rc
= transport_lookup_cmd_lun(se_cmd
, unpacked_lun
);
1304 transport_send_check_condition_and_sense(se_cmd
, rc
, 0);
1305 target_put_sess_cmd(se_sess
, se_cmd
);
1309 rc
= target_setup_cmd_from_cdb(se_cmd
, cdb
);
1311 transport_generic_request_failure(se_cmd
, rc
);
1315 * When a non zero sgl_count has been passed perform SGL passthrough
1316 * mapping for pre-allocated fabric memory instead of having target
1317 * core perform an internal SGL allocation..
1319 if (sgl_count
!= 0) {
1323 * A work-around for tcm_loop as some userspace code via
1324 * scsi-generic do not memset their associated read buffers,
1325 * so go ahead and do that here for type non-data CDBs. Also
1326 * note that this is currently guaranteed to be a single SGL
1327 * for this case by target core in target_setup_cmd_from_cdb()
1328 * -> transport_generic_cmd_sequencer().
1330 if (!(se_cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
) &&
1331 se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
1332 unsigned char *buf
= NULL
;
1335 buf
= kmap(sg_page(sgl
)) + sgl
->offset
;
1338 memset(buf
, 0, sgl
->length
);
1339 kunmap(sg_page(sgl
));
1343 rc
= transport_generic_map_mem_to_cmd(se_cmd
, sgl
, sgl_count
,
1344 sgl_bidi
, sgl_bidi_count
);
1346 transport_generic_request_failure(se_cmd
, rc
);
1351 * Check if we need to delay processing because of ALUA
1352 * Active/NonOptimized primary access state..
1354 core_alua_check_nonop_delay(se_cmd
);
1356 transport_handle_cdb_direct(se_cmd
);
1359 EXPORT_SYMBOL(target_submit_cmd_map_sgls
);
1362 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1364 * @se_cmd: command descriptor to submit
1365 * @se_sess: associated se_sess for endpoint
1366 * @cdb: pointer to SCSI CDB
1367 * @sense: pointer to SCSI sense buffer
1368 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1369 * @data_length: fabric expected data transfer length
1370 * @task_addr: SAM task attribute
1371 * @data_dir: DMA data direction
1372 * @flags: flags for command submission from target_sc_flags_tables
1374 * Returns non zero to signal active I/O shutdown failure. All other
1375 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1376 * but still return zero here.
1378 * This may only be called from process context, and also currently
1379 * assumes internal allocation of fabric payload buffer by target-core.
1381 * It also assumes interal target core SGL memory allocation.
1383 int target_submit_cmd(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1384 unsigned char *cdb
, unsigned char *sense
, u32 unpacked_lun
,
1385 u32 data_length
, int task_attr
, int data_dir
, int flags
)
1387 return target_submit_cmd_map_sgls(se_cmd
, se_sess
, cdb
, sense
,
1388 unpacked_lun
, data_length
, task_attr
, data_dir
,
1389 flags
, NULL
, 0, NULL
, 0);
1391 EXPORT_SYMBOL(target_submit_cmd
);
1393 static void target_complete_tmr_failure(struct work_struct
*work
)
1395 struct se_cmd
*se_cmd
= container_of(work
, struct se_cmd
, work
);
1397 se_cmd
->se_tmr_req
->response
= TMR_LUN_DOES_NOT_EXIST
;
1398 se_cmd
->se_tfo
->queue_tm_rsp(se_cmd
);
1400 transport_cmd_check_stop_to_fabric(se_cmd
);
1404 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1407 * @se_cmd: command descriptor to submit
1408 * @se_sess: associated se_sess for endpoint
1409 * @sense: pointer to SCSI sense buffer
1410 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1411 * @fabric_context: fabric context for TMR req
1412 * @tm_type: Type of TM request
1413 * @gfp: gfp type for caller
1414 * @tag: referenced task tag for TMR_ABORT_TASK
1415 * @flags: submit cmd flags
1417 * Callable from all contexts.
1420 int target_submit_tmr(struct se_cmd
*se_cmd
, struct se_session
*se_sess
,
1421 unsigned char *sense
, u32 unpacked_lun
,
1422 void *fabric_tmr_ptr
, unsigned char tm_type
,
1423 gfp_t gfp
, unsigned int tag
, int flags
)
1425 struct se_portal_group
*se_tpg
;
1428 se_tpg
= se_sess
->se_tpg
;
1431 transport_init_se_cmd(se_cmd
, se_tpg
->se_tpg_tfo
, se_sess
,
1432 0, DMA_NONE
, MSG_SIMPLE_TAG
, sense
);
1434 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1435 * allocation failure.
1437 ret
= core_tmr_alloc_req(se_cmd
, fabric_tmr_ptr
, tm_type
, gfp
);
1441 if (tm_type
== TMR_ABORT_TASK
)
1442 se_cmd
->se_tmr_req
->ref_task_tag
= tag
;
1444 /* See target_submit_cmd for commentary */
1445 ret
= target_get_sess_cmd(se_sess
, se_cmd
, (flags
& TARGET_SCF_ACK_KREF
));
1447 core_tmr_release_req(se_cmd
->se_tmr_req
);
1451 ret
= transport_lookup_tmr_lun(se_cmd
, unpacked_lun
);
1454 * For callback during failure handling, push this work off
1455 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1457 INIT_WORK(&se_cmd
->work
, target_complete_tmr_failure
);
1458 schedule_work(&se_cmd
->work
);
1461 transport_generic_handle_tmr(se_cmd
);
1464 EXPORT_SYMBOL(target_submit_tmr
);
1467 * If the cmd is active, request it to be stopped and sleep until it
1470 bool target_stop_cmd(struct se_cmd
*cmd
, unsigned long *flags
)
1472 bool was_active
= false;
1474 if (cmd
->transport_state
& CMD_T_BUSY
) {
1475 cmd
->transport_state
|= CMD_T_REQUEST_STOP
;
1476 spin_unlock_irqrestore(&cmd
->t_state_lock
, *flags
);
1478 pr_debug("cmd %p waiting to complete\n", cmd
);
1479 wait_for_completion(&cmd
->task_stop_comp
);
1480 pr_debug("cmd %p stopped successfully\n", cmd
);
1482 spin_lock_irqsave(&cmd
->t_state_lock
, *flags
);
1483 cmd
->transport_state
&= ~CMD_T_REQUEST_STOP
;
1484 cmd
->transport_state
&= ~CMD_T_BUSY
;
1492 * Handle SAM-esque emulation for generic transport request failures.
1494 void transport_generic_request_failure(struct se_cmd
*cmd
,
1495 sense_reason_t sense_reason
)
1499 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1500 " CDB: 0x%02x\n", cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
1501 cmd
->t_task_cdb
[0]);
1502 pr_debug("-----[ i_state: %d t_state: %d sense_reason: %d\n",
1503 cmd
->se_tfo
->get_cmd_state(cmd
),
1504 cmd
->t_state
, sense_reason
);
1505 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1506 (cmd
->transport_state
& CMD_T_ACTIVE
) != 0,
1507 (cmd
->transport_state
& CMD_T_STOP
) != 0,
1508 (cmd
->transport_state
& CMD_T_SENT
) != 0);
1511 * For SAM Task Attribute emulation for failed struct se_cmd
1513 transport_complete_task_attr(cmd
);
1515 switch (sense_reason
) {
1516 case TCM_NON_EXISTENT_LUN
:
1517 case TCM_UNSUPPORTED_SCSI_OPCODE
:
1518 case TCM_INVALID_CDB_FIELD
:
1519 case TCM_INVALID_PARAMETER_LIST
:
1520 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
1521 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
1522 case TCM_UNKNOWN_MODE_PAGE
:
1523 case TCM_WRITE_PROTECTED
:
1524 case TCM_ADDRESS_OUT_OF_RANGE
:
1525 case TCM_CHECK_CONDITION_ABORT_CMD
:
1526 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
1527 case TCM_CHECK_CONDITION_NOT_READY
:
1529 case TCM_OUT_OF_RESOURCES
:
1530 sense_reason
= TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
1532 case TCM_RESERVATION_CONFLICT
:
1534 * No SENSE Data payload for this case, set SCSI Status
1535 * and queue the response to $FABRIC_MOD.
1537 * Uses linux/include/scsi/scsi.h SAM status codes defs
1539 cmd
->scsi_status
= SAM_STAT_RESERVATION_CONFLICT
;
1541 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1542 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1545 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1548 cmd
->se_dev
->dev_attrib
.emulate_ua_intlck_ctrl
== 2)
1549 core_scsi3_ua_allocate(cmd
->se_sess
->se_node_acl
,
1550 cmd
->orig_fe_lun
, 0x2C,
1551 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS
);
1553 ret
= cmd
->se_tfo
->queue_status(cmd
);
1554 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1558 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1559 cmd
->t_task_cdb
[0], sense_reason
);
1560 sense_reason
= TCM_UNSUPPORTED_SCSI_OPCODE
;
1564 ret
= transport_send_check_condition_and_sense(cmd
, sense_reason
, 0);
1565 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1569 transport_lun_remove_cmd(cmd
);
1570 if (!transport_cmd_check_stop_to_fabric(cmd
))
1575 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
1576 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1578 EXPORT_SYMBOL(transport_generic_request_failure
);
1580 static void __target_execute_cmd(struct se_cmd
*cmd
)
1584 spin_lock_irq(&cmd
->t_state_lock
);
1585 cmd
->transport_state
|= (CMD_T_BUSY
|CMD_T_SENT
);
1586 spin_unlock_irq(&cmd
->t_state_lock
);
1588 if (cmd
->execute_cmd
) {
1589 ret
= cmd
->execute_cmd(cmd
);
1591 spin_lock_irq(&cmd
->t_state_lock
);
1592 cmd
->transport_state
&= ~(CMD_T_BUSY
|CMD_T_SENT
);
1593 spin_unlock_irq(&cmd
->t_state_lock
);
1595 transport_generic_request_failure(cmd
, ret
);
1600 static bool target_handle_task_attr(struct se_cmd
*cmd
)
1602 struct se_device
*dev
= cmd
->se_dev
;
1604 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1608 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1609 * to allow the passed struct se_cmd list of tasks to the front of the list.
1611 switch (cmd
->sam_task_attr
) {
1613 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1614 "se_ordered_id: %u\n",
1615 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1617 case MSG_ORDERED_TAG
:
1618 atomic_inc(&dev
->dev_ordered_sync
);
1619 smp_mb__after_atomic_inc();
1621 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1622 " se_ordered_id: %u\n",
1623 cmd
->t_task_cdb
[0], cmd
->se_ordered_id
);
1626 * Execute an ORDERED command if no other older commands
1627 * exist that need to be completed first.
1629 if (!atomic_read(&dev
->simple_cmds
))
1634 * For SIMPLE and UNTAGGED Task Attribute commands
1636 atomic_inc(&dev
->simple_cmds
);
1637 smp_mb__after_atomic_inc();
1641 if (atomic_read(&dev
->dev_ordered_sync
) == 0)
1644 spin_lock(&dev
->delayed_cmd_lock
);
1645 list_add_tail(&cmd
->se_delayed_node
, &dev
->delayed_cmd_list
);
1646 spin_unlock(&dev
->delayed_cmd_lock
);
1648 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1649 " delayed CMD list, se_ordered_id: %u\n",
1650 cmd
->t_task_cdb
[0], cmd
->sam_task_attr
,
1651 cmd
->se_ordered_id
);
1655 void target_execute_cmd(struct se_cmd
*cmd
)
1658 * If the received CDB has aleady been aborted stop processing it here.
1660 if (transport_check_aborted_status(cmd
, 1)) {
1661 complete(&cmd
->transport_lun_stop_comp
);
1666 * Determine if IOCTL context caller in requesting the stopping of this
1667 * command for LUN shutdown purposes.
1669 spin_lock_irq(&cmd
->t_state_lock
);
1670 if (cmd
->transport_state
& CMD_T_LUN_STOP
) {
1671 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
1672 __func__
, __LINE__
, cmd
->se_tfo
->get_task_tag(cmd
));
1674 cmd
->transport_state
&= ~CMD_T_ACTIVE
;
1675 spin_unlock_irq(&cmd
->t_state_lock
);
1676 complete(&cmd
->transport_lun_stop_comp
);
1680 * Determine if frontend context caller is requesting the stopping of
1681 * this command for frontend exceptions.
1683 if (cmd
->transport_state
& CMD_T_STOP
) {
1684 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1686 cmd
->se_tfo
->get_task_tag(cmd
));
1688 spin_unlock_irq(&cmd
->t_state_lock
);
1689 complete(&cmd
->t_transport_stop_comp
);
1693 cmd
->t_state
= TRANSPORT_PROCESSING
;
1694 cmd
->transport_state
|= CMD_T_ACTIVE
;
1695 spin_unlock_irq(&cmd
->t_state_lock
);
1697 if (!target_handle_task_attr(cmd
))
1698 __target_execute_cmd(cmd
);
1700 EXPORT_SYMBOL(target_execute_cmd
);
1703 * Process all commands up to the last received ORDERED task attribute which
1704 * requires another blocking boundary
1706 static void target_restart_delayed_cmds(struct se_device
*dev
)
1711 spin_lock(&dev
->delayed_cmd_lock
);
1712 if (list_empty(&dev
->delayed_cmd_list
)) {
1713 spin_unlock(&dev
->delayed_cmd_lock
);
1717 cmd
= list_entry(dev
->delayed_cmd_list
.next
,
1718 struct se_cmd
, se_delayed_node
);
1719 list_del(&cmd
->se_delayed_node
);
1720 spin_unlock(&dev
->delayed_cmd_lock
);
1722 __target_execute_cmd(cmd
);
1724 if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
)
1730 * Called from I/O completion to determine which dormant/delayed
1731 * and ordered cmds need to have their tasks added to the execution queue.
1733 static void transport_complete_task_attr(struct se_cmd
*cmd
)
1735 struct se_device
*dev
= cmd
->se_dev
;
1737 if (dev
->transport
->transport_type
== TRANSPORT_PLUGIN_PHBA_PDEV
)
1740 if (cmd
->sam_task_attr
== MSG_SIMPLE_TAG
) {
1741 atomic_dec(&dev
->simple_cmds
);
1742 smp_mb__after_atomic_dec();
1743 dev
->dev_cur_ordered_id
++;
1744 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
1745 " SIMPLE: %u\n", dev
->dev_cur_ordered_id
,
1746 cmd
->se_ordered_id
);
1747 } else if (cmd
->sam_task_attr
== MSG_HEAD_TAG
) {
1748 dev
->dev_cur_ordered_id
++;
1749 pr_debug("Incremented dev_cur_ordered_id: %u for"
1750 " HEAD_OF_QUEUE: %u\n", dev
->dev_cur_ordered_id
,
1751 cmd
->se_ordered_id
);
1752 } else if (cmd
->sam_task_attr
== MSG_ORDERED_TAG
) {
1753 atomic_dec(&dev
->dev_ordered_sync
);
1754 smp_mb__after_atomic_dec();
1756 dev
->dev_cur_ordered_id
++;
1757 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
1758 " %u\n", dev
->dev_cur_ordered_id
, cmd
->se_ordered_id
);
1761 target_restart_delayed_cmds(dev
);
1764 static void transport_complete_qf(struct se_cmd
*cmd
)
1768 transport_complete_task_attr(cmd
);
1770 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
1771 ret
= cmd
->se_tfo
->queue_status(cmd
);
1776 switch (cmd
->data_direction
) {
1777 case DMA_FROM_DEVICE
:
1778 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1781 if (cmd
->t_bidi_data_sg
) {
1782 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1786 /* Fall through for DMA_TO_DEVICE */
1788 ret
= cmd
->se_tfo
->queue_status(cmd
);
1796 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1799 transport_lun_remove_cmd(cmd
);
1800 transport_cmd_check_stop_to_fabric(cmd
);
1803 static void transport_handle_queue_full(
1805 struct se_device
*dev
)
1807 spin_lock_irq(&dev
->qf_cmd_lock
);
1808 list_add_tail(&cmd
->se_qf_node
, &cmd
->se_dev
->qf_cmd_list
);
1809 atomic_inc(&dev
->dev_qf_count
);
1810 smp_mb__after_atomic_inc();
1811 spin_unlock_irq(&cmd
->se_dev
->qf_cmd_lock
);
1813 schedule_work(&cmd
->se_dev
->qf_work_queue
);
1816 static void target_complete_ok_work(struct work_struct
*work
)
1818 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
1822 * Check if we need to move delayed/dormant tasks from cmds on the
1823 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1826 transport_complete_task_attr(cmd
);
1829 * Check to schedule QUEUE_FULL work, or execute an existing
1830 * cmd->transport_qf_callback()
1832 if (atomic_read(&cmd
->se_dev
->dev_qf_count
) != 0)
1833 schedule_work(&cmd
->se_dev
->qf_work_queue
);
1836 * Check if we need to send a sense buffer from
1837 * the struct se_cmd in question.
1839 if (cmd
->se_cmd_flags
& SCF_TRANSPORT_TASK_SENSE
) {
1840 WARN_ON(!cmd
->scsi_status
);
1841 ret
= transport_send_check_condition_and_sense(
1843 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1846 transport_lun_remove_cmd(cmd
);
1847 transport_cmd_check_stop_to_fabric(cmd
);
1851 * Check for a callback, used by amongst other things
1852 * XDWRITE_READ_10 emulation.
1854 if (cmd
->transport_complete_callback
)
1855 cmd
->transport_complete_callback(cmd
);
1857 switch (cmd
->data_direction
) {
1858 case DMA_FROM_DEVICE
:
1859 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1860 if (cmd
->se_lun
->lun_sep
) {
1861 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
1864 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1866 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1867 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1871 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1872 if (cmd
->se_lun
->lun_sep
) {
1873 cmd
->se_lun
->lun_sep
->sep_stats
.rx_data_octets
+=
1876 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1878 * Check if we need to send READ payload for BIDI-COMMAND
1880 if (cmd
->t_bidi_data_sg
) {
1881 spin_lock(&cmd
->se_lun
->lun_sep_lock
);
1882 if (cmd
->se_lun
->lun_sep
) {
1883 cmd
->se_lun
->lun_sep
->sep_stats
.tx_data_octets
+=
1886 spin_unlock(&cmd
->se_lun
->lun_sep_lock
);
1887 ret
= cmd
->se_tfo
->queue_data_in(cmd
);
1888 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1892 /* Fall through for DMA_TO_DEVICE */
1894 ret
= cmd
->se_tfo
->queue_status(cmd
);
1895 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
1902 transport_lun_remove_cmd(cmd
);
1903 transport_cmd_check_stop_to_fabric(cmd
);
1907 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
1908 " data_direction: %d\n", cmd
, cmd
->data_direction
);
1909 cmd
->t_state
= TRANSPORT_COMPLETE_QF_OK
;
1910 transport_handle_queue_full(cmd
, cmd
->se_dev
);
1913 static inline void transport_free_sgl(struct scatterlist
*sgl
, int nents
)
1915 struct scatterlist
*sg
;
1918 for_each_sg(sgl
, sg
, nents
, count
)
1919 __free_page(sg_page(sg
));
1924 static inline void transport_free_pages(struct se_cmd
*cmd
)
1926 if (cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
)
1929 transport_free_sgl(cmd
->t_data_sg
, cmd
->t_data_nents
);
1930 cmd
->t_data_sg
= NULL
;
1931 cmd
->t_data_nents
= 0;
1933 transport_free_sgl(cmd
->t_bidi_data_sg
, cmd
->t_bidi_data_nents
);
1934 cmd
->t_bidi_data_sg
= NULL
;
1935 cmd
->t_bidi_data_nents
= 0;
1939 * transport_release_cmd - free a command
1940 * @cmd: command to free
1942 * This routine unconditionally frees a command, and reference counting
1943 * or list removal must be done in the caller.
1945 static void transport_release_cmd(struct se_cmd
*cmd
)
1947 BUG_ON(!cmd
->se_tfo
);
1949 if (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)
1950 core_tmr_release_req(cmd
->se_tmr_req
);
1951 if (cmd
->t_task_cdb
!= cmd
->__t_task_cdb
)
1952 kfree(cmd
->t_task_cdb
);
1954 * If this cmd has been setup with target_get_sess_cmd(), drop
1955 * the kref and call ->release_cmd() in kref callback.
1957 if (cmd
->check_release
!= 0) {
1958 target_put_sess_cmd(cmd
->se_sess
, cmd
);
1961 cmd
->se_tfo
->release_cmd(cmd
);
1965 * transport_put_cmd - release a reference to a command
1966 * @cmd: command to release
1968 * This routine releases our reference to the command and frees it if possible.
1970 static void transport_put_cmd(struct se_cmd
*cmd
)
1972 unsigned long flags
;
1974 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
1975 if (atomic_read(&cmd
->t_fe_count
) &&
1976 !atomic_dec_and_test(&cmd
->t_fe_count
)) {
1977 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
1981 if (cmd
->transport_state
& CMD_T_DEV_ACTIVE
) {
1982 cmd
->transport_state
&= ~CMD_T_DEV_ACTIVE
;
1983 target_remove_from_state_list(cmd
);
1985 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
1987 transport_free_pages(cmd
);
1988 transport_release_cmd(cmd
);
1992 void *transport_kmap_data_sg(struct se_cmd
*cmd
)
1994 struct scatterlist
*sg
= cmd
->t_data_sg
;
1995 struct page
**pages
;
1999 * We need to take into account a possible offset here for fabrics like
2000 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2001 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
2003 if (!cmd
->t_data_nents
)
2007 if (cmd
->t_data_nents
== 1)
2008 return kmap(sg_page(sg
)) + sg
->offset
;
2010 /* >1 page. use vmap */
2011 pages
= kmalloc(sizeof(*pages
) * cmd
->t_data_nents
, GFP_KERNEL
);
2015 /* convert sg[] to pages[] */
2016 for_each_sg(cmd
->t_data_sg
, sg
, cmd
->t_data_nents
, i
) {
2017 pages
[i
] = sg_page(sg
);
2020 cmd
->t_data_vmap
= vmap(pages
, cmd
->t_data_nents
, VM_MAP
, PAGE_KERNEL
);
2022 if (!cmd
->t_data_vmap
)
2025 return cmd
->t_data_vmap
+ cmd
->t_data_sg
[0].offset
;
2027 EXPORT_SYMBOL(transport_kmap_data_sg
);
2029 void transport_kunmap_data_sg(struct se_cmd
*cmd
)
2031 if (!cmd
->t_data_nents
) {
2033 } else if (cmd
->t_data_nents
== 1) {
2034 kunmap(sg_page(cmd
->t_data_sg
));
2038 vunmap(cmd
->t_data_vmap
);
2039 cmd
->t_data_vmap
= NULL
;
2041 EXPORT_SYMBOL(transport_kunmap_data_sg
);
2044 transport_generic_get_mem(struct se_cmd
*cmd
)
2046 u32 length
= cmd
->data_length
;
2052 nents
= DIV_ROUND_UP(length
, PAGE_SIZE
);
2053 cmd
->t_data_sg
= kmalloc(sizeof(struct scatterlist
) * nents
, GFP_KERNEL
);
2054 if (!cmd
->t_data_sg
)
2057 cmd
->t_data_nents
= nents
;
2058 sg_init_table(cmd
->t_data_sg
, nents
);
2060 zero_flag
= cmd
->se_cmd_flags
& SCF_SCSI_DATA_CDB
? 0 : __GFP_ZERO
;
2063 u32 page_len
= min_t(u32
, length
, PAGE_SIZE
);
2064 page
= alloc_page(GFP_KERNEL
| zero_flag
);
2068 sg_set_page(&cmd
->t_data_sg
[i
], page
, page_len
, 0);
2077 __free_page(sg_page(&cmd
->t_data_sg
[i
]));
2079 kfree(cmd
->t_data_sg
);
2080 cmd
->t_data_sg
= NULL
;
2085 * Allocate any required resources to execute the command. For writes we
2086 * might not have the payload yet, so notify the fabric via a call to
2087 * ->write_pending instead. Otherwise place it on the execution queue.
2090 transport_generic_new_cmd(struct se_cmd
*cmd
)
2095 * Determine is the TCM fabric module has already allocated physical
2096 * memory, and is directly calling transport_generic_map_mem_to_cmd()
2099 if (!(cmd
->se_cmd_flags
& SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC
) &&
2101 ret
= transport_generic_get_mem(cmd
);
2103 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2106 atomic_inc(&cmd
->t_fe_count
);
2109 * If this command is not a write we can execute it right here,
2110 * for write buffers we need to notify the fabric driver first
2111 * and let it call back once the write buffers are ready.
2113 target_add_to_state_list(cmd
);
2114 if (cmd
->data_direction
!= DMA_TO_DEVICE
) {
2115 target_execute_cmd(cmd
);
2119 spin_lock_irq(&cmd
->t_state_lock
);
2120 cmd
->t_state
= TRANSPORT_WRITE_PENDING
;
2121 spin_unlock_irq(&cmd
->t_state_lock
);
2123 transport_cmd_check_stop(cmd
, false);
2125 ret
= cmd
->se_tfo
->write_pending(cmd
);
2126 if (ret
== -EAGAIN
|| ret
== -ENOMEM
)
2129 /* fabric drivers should only return -EAGAIN or -ENOMEM as error */
2132 return (!ret
) ? 0 : TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
;
2135 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd
);
2136 cmd
->t_state
= TRANSPORT_COMPLETE_QF_WP
;
2137 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2140 EXPORT_SYMBOL(transport_generic_new_cmd
);
2142 static void transport_write_pending_qf(struct se_cmd
*cmd
)
2146 ret
= cmd
->se_tfo
->write_pending(cmd
);
2147 if (ret
== -EAGAIN
|| ret
== -ENOMEM
) {
2148 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2150 transport_handle_queue_full(cmd
, cmd
->se_dev
);
2154 void transport_generic_free_cmd(struct se_cmd
*cmd
, int wait_for_tasks
)
2156 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
)) {
2157 if (wait_for_tasks
&& (cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
))
2158 transport_wait_for_tasks(cmd
);
2160 transport_release_cmd(cmd
);
2163 transport_wait_for_tasks(cmd
);
2165 core_dec_lacl_count(cmd
->se_sess
->se_node_acl
, cmd
);
2168 transport_lun_remove_cmd(cmd
);
2170 transport_put_cmd(cmd
);
2173 EXPORT_SYMBOL(transport_generic_free_cmd
);
2175 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
2176 * @se_sess: session to reference
2177 * @se_cmd: command descriptor to add
2178 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2180 static int target_get_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
,
2183 unsigned long flags
;
2186 kref_init(&se_cmd
->cmd_kref
);
2188 * Add a second kref if the fabric caller is expecting to handle
2189 * fabric acknowledgement that requires two target_put_sess_cmd()
2190 * invocations before se_cmd descriptor release.
2192 if (ack_kref
== true) {
2193 kref_get(&se_cmd
->cmd_kref
);
2194 se_cmd
->se_cmd_flags
|= SCF_ACK_KREF
;
2197 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2198 if (se_sess
->sess_tearing_down
) {
2202 list_add_tail(&se_cmd
->se_cmd_list
, &se_sess
->sess_cmd_list
);
2203 se_cmd
->check_release
= 1;
2206 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2210 static void target_release_cmd_kref(struct kref
*kref
)
2212 struct se_cmd
*se_cmd
= container_of(kref
, struct se_cmd
, cmd_kref
);
2213 struct se_session
*se_sess
= se_cmd
->se_sess
;
2214 unsigned long flags
;
2216 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2217 if (list_empty(&se_cmd
->se_cmd_list
)) {
2218 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2219 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2222 if (se_sess
->sess_tearing_down
&& se_cmd
->cmd_wait_set
) {
2223 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2224 complete(&se_cmd
->cmd_wait_comp
);
2227 list_del(&se_cmd
->se_cmd_list
);
2228 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2230 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2233 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2234 * @se_sess: session to reference
2235 * @se_cmd: command descriptor to drop
2237 int target_put_sess_cmd(struct se_session
*se_sess
, struct se_cmd
*se_cmd
)
2239 return kref_put(&se_cmd
->cmd_kref
, target_release_cmd_kref
);
2241 EXPORT_SYMBOL(target_put_sess_cmd
);
2243 /* target_sess_cmd_list_set_waiting - Flag all commands in
2244 * sess_cmd_list to complete cmd_wait_comp. Set
2245 * sess_tearing_down so no more commands are queued.
2246 * @se_sess: session to flag
2248 void target_sess_cmd_list_set_waiting(struct se_session
*se_sess
)
2250 struct se_cmd
*se_cmd
;
2251 unsigned long flags
;
2253 spin_lock_irqsave(&se_sess
->sess_cmd_lock
, flags
);
2255 WARN_ON(se_sess
->sess_tearing_down
);
2256 se_sess
->sess_tearing_down
= 1;
2258 list_for_each_entry(se_cmd
, &se_sess
->sess_cmd_list
, se_cmd_list
)
2259 se_cmd
->cmd_wait_set
= 1;
2261 spin_unlock_irqrestore(&se_sess
->sess_cmd_lock
, flags
);
2263 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting
);
2265 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
2266 * @se_sess: session to wait for active I/O
2267 * @wait_for_tasks: Make extra transport_wait_for_tasks call
2269 void target_wait_for_sess_cmds(
2270 struct se_session
*se_sess
,
2273 struct se_cmd
*se_cmd
, *tmp_cmd
;
2276 list_for_each_entry_safe(se_cmd
, tmp_cmd
,
2277 &se_sess
->sess_cmd_list
, se_cmd_list
) {
2278 list_del(&se_cmd
->se_cmd_list
);
2280 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2281 " %d\n", se_cmd
, se_cmd
->t_state
,
2282 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2284 if (wait_for_tasks
) {
2285 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
2286 " fabric state: %d\n", se_cmd
, se_cmd
->t_state
,
2287 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2289 rc
= transport_wait_for_tasks(se_cmd
);
2291 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
2292 " fabric state: %d\n", se_cmd
, se_cmd
->t_state
,
2293 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2297 wait_for_completion(&se_cmd
->cmd_wait_comp
);
2298 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2299 " fabric state: %d\n", se_cmd
, se_cmd
->t_state
,
2300 se_cmd
->se_tfo
->get_cmd_state(se_cmd
));
2303 se_cmd
->se_tfo
->release_cmd(se_cmd
);
2306 EXPORT_SYMBOL(target_wait_for_sess_cmds
);
2308 /* transport_lun_wait_for_tasks():
2310 * Called from ConfigFS context to stop the passed struct se_cmd to allow
2311 * an struct se_lun to be successfully shutdown.
2313 static int transport_lun_wait_for_tasks(struct se_cmd
*cmd
, struct se_lun
*lun
)
2315 unsigned long flags
;
2319 * If the frontend has already requested this struct se_cmd to
2320 * be stopped, we can safely ignore this struct se_cmd.
2322 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2323 if (cmd
->transport_state
& CMD_T_STOP
) {
2324 cmd
->transport_state
&= ~CMD_T_LUN_STOP
;
2326 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
2327 cmd
->se_tfo
->get_task_tag(cmd
));
2328 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2329 transport_cmd_check_stop(cmd
, false);
2332 cmd
->transport_state
|= CMD_T_LUN_FE_STOP
;
2333 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2335 // XXX: audit task_flags checks.
2336 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2337 if ((cmd
->transport_state
& CMD_T_BUSY
) &&
2338 (cmd
->transport_state
& CMD_T_SENT
)) {
2339 if (!target_stop_cmd(cmd
, &flags
))
2342 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2344 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
2347 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
2348 cmd
->se_tfo
->get_task_tag(cmd
));
2349 wait_for_completion(&cmd
->transport_lun_stop_comp
);
2350 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
2351 cmd
->se_tfo
->get_task_tag(cmd
));
2357 static void __transport_clear_lun_from_sessions(struct se_lun
*lun
)
2359 struct se_cmd
*cmd
= NULL
;
2360 unsigned long lun_flags
, cmd_flags
;
2362 * Do exception processing and return CHECK_CONDITION status to the
2365 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2366 while (!list_empty(&lun
->lun_cmd_list
)) {
2367 cmd
= list_first_entry(&lun
->lun_cmd_list
,
2368 struct se_cmd
, se_lun_node
);
2369 list_del_init(&cmd
->se_lun_node
);
2371 spin_lock(&cmd
->t_state_lock
);
2372 pr_debug("SE_LUN[%d] - Setting cmd->transport"
2373 "_lun_stop for ITT: 0x%08x\n",
2374 cmd
->se_lun
->unpacked_lun
,
2375 cmd
->se_tfo
->get_task_tag(cmd
));
2376 cmd
->transport_state
|= CMD_T_LUN_STOP
;
2377 spin_unlock(&cmd
->t_state_lock
);
2379 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
2382 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
2383 cmd
->se_tfo
->get_task_tag(cmd
),
2384 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
2388 * If the Storage engine still owns the iscsi_cmd_t, determine
2389 * and/or stop its context.
2391 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
2392 "_lun_wait_for_tasks()\n", cmd
->se_lun
->unpacked_lun
,
2393 cmd
->se_tfo
->get_task_tag(cmd
));
2395 if (transport_lun_wait_for_tasks(cmd
, cmd
->se_lun
) < 0) {
2396 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2400 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
2401 "_wait_for_tasks(): SUCCESS\n",
2402 cmd
->se_lun
->unpacked_lun
,
2403 cmd
->se_tfo
->get_task_tag(cmd
));
2405 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
2406 if (!(cmd
->transport_state
& CMD_T_DEV_ACTIVE
)) {
2407 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
2410 cmd
->transport_state
&= ~CMD_T_DEV_ACTIVE
;
2411 target_remove_from_state_list(cmd
);
2412 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
2415 * The Storage engine stopped this struct se_cmd before it was
2416 * send to the fabric frontend for delivery back to the
2417 * Initiator Node. Return this SCSI CDB back with an
2418 * CHECK_CONDITION status.
2421 transport_send_check_condition_and_sense(cmd
,
2422 TCM_NON_EXISTENT_LUN
, 0);
2424 * If the fabric frontend is waiting for this iscsi_cmd_t to
2425 * be released, notify the waiting thread now that LU has
2426 * finished accessing it.
2428 spin_lock_irqsave(&cmd
->t_state_lock
, cmd_flags
);
2429 if (cmd
->transport_state
& CMD_T_LUN_FE_STOP
) {
2430 pr_debug("SE_LUN[%d] - Detected FE stop for"
2431 " struct se_cmd: %p ITT: 0x%08x\n",
2433 cmd
, cmd
->se_tfo
->get_task_tag(cmd
));
2435 spin_unlock_irqrestore(&cmd
->t_state_lock
,
2437 transport_cmd_check_stop(cmd
, false);
2438 complete(&cmd
->transport_lun_fe_stop_comp
);
2439 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2442 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
2443 lun
->unpacked_lun
, cmd
->se_tfo
->get_task_tag(cmd
));
2445 spin_unlock_irqrestore(&cmd
->t_state_lock
, cmd_flags
);
2446 spin_lock_irqsave(&lun
->lun_cmd_lock
, lun_flags
);
2448 spin_unlock_irqrestore(&lun
->lun_cmd_lock
, lun_flags
);
2451 static int transport_clear_lun_thread(void *p
)
2453 struct se_lun
*lun
= p
;
2455 __transport_clear_lun_from_sessions(lun
);
2456 complete(&lun
->lun_shutdown_comp
);
2461 int transport_clear_lun_from_sessions(struct se_lun
*lun
)
2463 struct task_struct
*kt
;
2465 kt
= kthread_run(transport_clear_lun_thread
, lun
,
2466 "tcm_cl_%u", lun
->unpacked_lun
);
2468 pr_err("Unable to start clear_lun thread\n");
2471 wait_for_completion(&lun
->lun_shutdown_comp
);
2477 * transport_wait_for_tasks - wait for completion to occur
2478 * @cmd: command to wait
2480 * Called from frontend fabric context to wait for storage engine
2481 * to pause and/or release frontend generated struct se_cmd.
2483 bool transport_wait_for_tasks(struct se_cmd
*cmd
)
2485 unsigned long flags
;
2487 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2488 if (!(cmd
->se_cmd_flags
& SCF_SE_LUN_CMD
) &&
2489 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2490 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2494 if (!(cmd
->se_cmd_flags
& SCF_SUPPORTED_SAM_OPCODE
) &&
2495 !(cmd
->se_cmd_flags
& SCF_SCSI_TMR_CDB
)) {
2496 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2500 * If we are already stopped due to an external event (ie: LUN shutdown)
2501 * sleep until the connection can have the passed struct se_cmd back.
2502 * The cmd->transport_lun_stopped_sem will be upped by
2503 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2504 * has completed its operation on the struct se_cmd.
2506 if (cmd
->transport_state
& CMD_T_LUN_STOP
) {
2507 pr_debug("wait_for_tasks: Stopping"
2508 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
2509 "_stop_comp); for ITT: 0x%08x\n",
2510 cmd
->se_tfo
->get_task_tag(cmd
));
2512 * There is a special case for WRITES where a FE exception +
2513 * LUN shutdown means ConfigFS context is still sleeping on
2514 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2515 * We go ahead and up transport_lun_stop_comp just to be sure
2518 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2519 complete(&cmd
->transport_lun_stop_comp
);
2520 wait_for_completion(&cmd
->transport_lun_fe_stop_comp
);
2521 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2523 target_remove_from_state_list(cmd
);
2525 * At this point, the frontend who was the originator of this
2526 * struct se_cmd, now owns the structure and can be released through
2527 * normal means below.
2529 pr_debug("wait_for_tasks: Stopped"
2530 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
2531 "stop_comp); for ITT: 0x%08x\n",
2532 cmd
->se_tfo
->get_task_tag(cmd
));
2534 cmd
->transport_state
&= ~CMD_T_LUN_STOP
;
2537 if (!(cmd
->transport_state
& CMD_T_ACTIVE
)) {
2538 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2542 cmd
->transport_state
|= CMD_T_STOP
;
2544 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
2545 " i_state: %d, t_state: %d, CMD_T_STOP\n",
2546 cmd
, cmd
->se_tfo
->get_task_tag(cmd
),
2547 cmd
->se_tfo
->get_cmd_state(cmd
), cmd
->t_state
);
2549 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2551 wait_for_completion(&cmd
->t_transport_stop_comp
);
2553 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2554 cmd
->transport_state
&= ~(CMD_T_ACTIVE
| CMD_T_STOP
);
2556 pr_debug("wait_for_tasks: Stopped wait_for_completion("
2557 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
2558 cmd
->se_tfo
->get_task_tag(cmd
));
2560 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2564 EXPORT_SYMBOL(transport_wait_for_tasks
);
2566 static int transport_get_sense_codes(
2571 *asc
= cmd
->scsi_asc
;
2572 *ascq
= cmd
->scsi_ascq
;
2578 transport_send_check_condition_and_sense(struct se_cmd
*cmd
,
2579 sense_reason_t reason
, int from_transport
)
2581 unsigned char *buffer
= cmd
->sense_buffer
;
2582 unsigned long flags
;
2583 u8 asc
= 0, ascq
= 0;
2585 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2586 if (cmd
->se_cmd_flags
& SCF_SENT_CHECK_CONDITION
) {
2587 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2590 cmd
->se_cmd_flags
|= SCF_SENT_CHECK_CONDITION
;
2591 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2593 if (!reason
&& from_transport
)
2596 if (!from_transport
)
2597 cmd
->se_cmd_flags
|= SCF_EMULATED_TASK_SENSE
;
2600 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2601 * SENSE KEY values from include/scsi/scsi.h
2607 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2609 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2610 /* NO ADDITIONAL SENSE INFORMATION */
2611 buffer
[SPC_ASC_KEY_OFFSET
] = 0;
2612 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0;
2614 case TCM_NON_EXISTENT_LUN
:
2617 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2618 /* ILLEGAL REQUEST */
2619 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2620 /* LOGICAL UNIT NOT SUPPORTED */
2621 buffer
[SPC_ASC_KEY_OFFSET
] = 0x25;
2623 case TCM_UNSUPPORTED_SCSI_OPCODE
:
2624 case TCM_SECTOR_COUNT_TOO_MANY
:
2627 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2628 /* ILLEGAL REQUEST */
2629 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2630 /* INVALID COMMAND OPERATION CODE */
2631 buffer
[SPC_ASC_KEY_OFFSET
] = 0x20;
2633 case TCM_UNKNOWN_MODE_PAGE
:
2636 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2637 /* ILLEGAL REQUEST */
2638 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2639 /* INVALID FIELD IN CDB */
2640 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2642 case TCM_CHECK_CONDITION_ABORT_CMD
:
2645 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2646 /* ABORTED COMMAND */
2647 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2648 /* BUS DEVICE RESET FUNCTION OCCURRED */
2649 buffer
[SPC_ASC_KEY_OFFSET
] = 0x29;
2650 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x03;
2652 case TCM_INCORRECT_AMOUNT_OF_DATA
:
2655 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2656 /* ABORTED COMMAND */
2657 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2659 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2660 /* NOT ENOUGH UNSOLICITED DATA */
2661 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0d;
2663 case TCM_INVALID_CDB_FIELD
:
2666 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2667 /* ILLEGAL REQUEST */
2668 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2669 /* INVALID FIELD IN CDB */
2670 buffer
[SPC_ASC_KEY_OFFSET
] = 0x24;
2672 case TCM_INVALID_PARAMETER_LIST
:
2675 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2676 /* ILLEGAL REQUEST */
2677 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2678 /* INVALID FIELD IN PARAMETER LIST */
2679 buffer
[SPC_ASC_KEY_OFFSET
] = 0x26;
2681 case TCM_PARAMETER_LIST_LENGTH_ERROR
:
2684 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2685 /* ILLEGAL REQUEST */
2686 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2687 /* PARAMETER LIST LENGTH ERROR */
2688 buffer
[SPC_ASC_KEY_OFFSET
] = 0x1a;
2690 case TCM_UNEXPECTED_UNSOLICITED_DATA
:
2693 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2694 /* ABORTED COMMAND */
2695 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2697 buffer
[SPC_ASC_KEY_OFFSET
] = 0x0c;
2698 /* UNEXPECTED_UNSOLICITED_DATA */
2699 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x0c;
2701 case TCM_SERVICE_CRC_ERROR
:
2704 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2705 /* ABORTED COMMAND */
2706 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2707 /* PROTOCOL SERVICE CRC ERROR */
2708 buffer
[SPC_ASC_KEY_OFFSET
] = 0x47;
2710 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x05;
2712 case TCM_SNACK_REJECTED
:
2715 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2716 /* ABORTED COMMAND */
2717 buffer
[SPC_SENSE_KEY_OFFSET
] = ABORTED_COMMAND
;
2719 buffer
[SPC_ASC_KEY_OFFSET
] = 0x11;
2720 /* FAILED RETRANSMISSION REQUEST */
2721 buffer
[SPC_ASCQ_KEY_OFFSET
] = 0x13;
2723 case TCM_WRITE_PROTECTED
:
2726 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2728 buffer
[SPC_SENSE_KEY_OFFSET
] = DATA_PROTECT
;
2729 /* WRITE PROTECTED */
2730 buffer
[SPC_ASC_KEY_OFFSET
] = 0x27;
2732 case TCM_ADDRESS_OUT_OF_RANGE
:
2735 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2736 /* ILLEGAL REQUEST */
2737 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2738 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2739 buffer
[SPC_ASC_KEY_OFFSET
] = 0x21;
2741 case TCM_CHECK_CONDITION_UNIT_ATTENTION
:
2744 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2745 /* UNIT ATTENTION */
2746 buffer
[SPC_SENSE_KEY_OFFSET
] = UNIT_ATTENTION
;
2747 core_scsi3_ua_for_check_condition(cmd
, &asc
, &ascq
);
2748 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2749 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2751 case TCM_CHECK_CONDITION_NOT_READY
:
2754 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2756 buffer
[SPC_SENSE_KEY_OFFSET
] = NOT_READY
;
2757 transport_get_sense_codes(cmd
, &asc
, &ascq
);
2758 buffer
[SPC_ASC_KEY_OFFSET
] = asc
;
2759 buffer
[SPC_ASCQ_KEY_OFFSET
] = ascq
;
2761 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE
:
2765 buffer
[SPC_ADD_SENSE_LEN_OFFSET
] = 10;
2766 /* ILLEGAL REQUEST */
2767 buffer
[SPC_SENSE_KEY_OFFSET
] = ILLEGAL_REQUEST
;
2768 /* LOGICAL UNIT COMMUNICATION FAILURE */
2769 buffer
[SPC_ASC_KEY_OFFSET
] = 0x08;
2773 * This code uses linux/include/scsi/scsi.h SAM status codes!
2775 cmd
->scsi_status
= SAM_STAT_CHECK_CONDITION
;
2777 * Automatically padded, this value is encoded in the fabric's
2778 * data_length response PDU containing the SCSI defined sense data.
2780 cmd
->scsi_sense_length
= TRANSPORT_SENSE_BUFFER
;
2783 return cmd
->se_tfo
->queue_status(cmd
);
2785 EXPORT_SYMBOL(transport_send_check_condition_and_sense
);
2787 int transport_check_aborted_status(struct se_cmd
*cmd
, int send_status
)
2789 if (!(cmd
->transport_state
& CMD_T_ABORTED
))
2792 if (!send_status
|| (cmd
->se_cmd_flags
& SCF_SENT_DELAYED_TAS
))
2795 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED status for CDB: 0x%02x ITT: 0x%08x\n",
2796 cmd
->t_task_cdb
[0], cmd
->se_tfo
->get_task_tag(cmd
));
2798 cmd
->se_cmd_flags
|= SCF_SENT_DELAYED_TAS
;
2799 cmd
->se_tfo
->queue_status(cmd
);
2803 EXPORT_SYMBOL(transport_check_aborted_status
);
2805 void transport_send_task_abort(struct se_cmd
*cmd
)
2807 unsigned long flags
;
2809 spin_lock_irqsave(&cmd
->t_state_lock
, flags
);
2810 if (cmd
->se_cmd_flags
& (SCF_SENT_CHECK_CONDITION
| SCF_SENT_DELAYED_TAS
)) {
2811 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2814 spin_unlock_irqrestore(&cmd
->t_state_lock
, flags
);
2817 * If there are still expected incoming fabric WRITEs, we wait
2818 * until until they have completed before sending a TASK_ABORTED
2819 * response. This response with TASK_ABORTED status will be
2820 * queued back to fabric module by transport_check_aborted_status().
2822 if (cmd
->data_direction
== DMA_TO_DEVICE
) {
2823 if (cmd
->se_tfo
->write_pending_status(cmd
) != 0) {
2824 cmd
->transport_state
|= CMD_T_ABORTED
;
2825 smp_mb__after_atomic_inc();
2828 cmd
->scsi_status
= SAM_STAT_TASK_ABORTED
;
2830 transport_lun_remove_cmd(cmd
);
2832 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
2833 " ITT: 0x%08x\n", cmd
->t_task_cdb
[0],
2834 cmd
->se_tfo
->get_task_tag(cmd
));
2836 cmd
->se_tfo
->queue_status(cmd
);
2839 static void target_tmr_work(struct work_struct
*work
)
2841 struct se_cmd
*cmd
= container_of(work
, struct se_cmd
, work
);
2842 struct se_device
*dev
= cmd
->se_dev
;
2843 struct se_tmr_req
*tmr
= cmd
->se_tmr_req
;
2846 switch (tmr
->function
) {
2847 case TMR_ABORT_TASK
:
2848 core_tmr_abort_task(dev
, tmr
, cmd
->se_sess
);
2850 case TMR_ABORT_TASK_SET
:
2852 case TMR_CLEAR_TASK_SET
:
2853 tmr
->response
= TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
2856 ret
= core_tmr_lun_reset(dev
, tmr
, NULL
, NULL
);
2857 tmr
->response
= (!ret
) ? TMR_FUNCTION_COMPLETE
:
2858 TMR_FUNCTION_REJECTED
;
2860 case TMR_TARGET_WARM_RESET
:
2861 tmr
->response
= TMR_FUNCTION_REJECTED
;
2863 case TMR_TARGET_COLD_RESET
:
2864 tmr
->response
= TMR_FUNCTION_REJECTED
;
2867 pr_err("Uknown TMR function: 0x%02x.\n",
2869 tmr
->response
= TMR_FUNCTION_REJECTED
;
2873 cmd
->t_state
= TRANSPORT_ISTATE_PROCESSING
;
2874 cmd
->se_tfo
->queue_tm_rsp(cmd
);
2876 transport_cmd_check_stop_to_fabric(cmd
);
2879 int transport_generic_handle_tmr(
2882 INIT_WORK(&cmd
->work
, target_tmr_work
);
2883 queue_work(cmd
->se_dev
->tmr_wq
, &cmd
->work
);
2886 EXPORT_SYMBOL(transport_generic_handle_tmr
);