11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger * Filename: target_core_transport.c
4c66ac9dbSNicholas Bellinger *
5c66ac9dbSNicholas Bellinger * This file contains the Generic Target Engine Core.
6c66ac9dbSNicholas Bellinger *
74c76251eSNicholas Bellinger * (c) Copyright 2002-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger *
9c66ac9dbSNicholas Bellinger * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger *
11c66ac9dbSNicholas Bellinger ******************************************************************************/
12c66ac9dbSNicholas Bellinger
13c66ac9dbSNicholas Bellinger #include <linux/net.h>
14c66ac9dbSNicholas Bellinger #include <linux/delay.h>
15c66ac9dbSNicholas Bellinger #include <linux/string.h>
16c66ac9dbSNicholas Bellinger #include <linux/timer.h>
17c66ac9dbSNicholas Bellinger #include <linux/slab.h>
18c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
19c66ac9dbSNicholas Bellinger #include <linux/kthread.h>
20c66ac9dbSNicholas Bellinger #include <linux/in.h>
21c66ac9dbSNicholas Bellinger #include <linux/cdrom.h>
22827509e3SPaul Gortmaker #include <linux/module.h>
23015487b8SRoland Dreier #include <linux/ratelimit.h>
245538d294SDavid S. Miller #include <linux/vmalloc.h>
25c66ac9dbSNicholas Bellinger #include <asm/unaligned.h>
26c66ac9dbSNicholas Bellinger #include <net/sock.h>
27c66ac9dbSNicholas Bellinger #include <net/tcp.h>
28ba929992SBart Van Assche #include <scsi/scsi_proto.h>
299ec1e1ceSSagi Grimberg #include <scsi/scsi_common.h>
30c66ac9dbSNicholas Bellinger
31c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
32c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
33c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
34c66ac9dbSNicholas Bellinger
35e26d99aeSChristoph Hellwig #include "target_core_internal.h"
36c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
37c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
38c66ac9dbSNicholas Bellinger #include "target_core_ua.h"
39c66ac9dbSNicholas Bellinger
40e5c0d6adSRoland Dreier #define CREATE_TRACE_POINTS
41e5c0d6adSRoland Dreier #include <trace/events/target.h>
42e5c0d6adSRoland Dreier
4335e0e757SChristoph Hellwig static struct workqueue_struct *target_completion_wq;
44eb44ce8cSMike Christie static struct workqueue_struct *target_submission_wq;
45c66ac9dbSNicholas Bellinger static struct kmem_cache *se_sess_cache;
46c66ac9dbSNicholas Bellinger struct kmem_cache *se_ua_cache;
47c66ac9dbSNicholas Bellinger struct kmem_cache *t10_pr_reg_cache;
48c66ac9dbSNicholas Bellinger struct kmem_cache *t10_alua_lu_gp_cache;
49c66ac9dbSNicholas Bellinger struct kmem_cache *t10_alua_lu_gp_mem_cache;
50c66ac9dbSNicholas Bellinger struct kmem_cache *t10_alua_tg_pt_gp_cache;
51229d4f11SHannes Reinecke struct kmem_cache *t10_alua_lba_map_cache;
52229d4f11SHannes Reinecke struct kmem_cache *t10_alua_lba_map_mem_cache;
53c66ac9dbSNicholas Bellinger
54c66ac9dbSNicholas Bellinger static void transport_complete_task_attr(struct se_cmd *cmd);
5517e391ddSBart Van Assche static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason);
5607bde79aSNicholas Bellinger static void transport_handle_queue_full(struct se_cmd *cmd,
57fa7e25cfSNicholas Bellinger struct se_device *dev, int err, bool write_pending);
5835e0e757SChristoph Hellwig static void target_complete_ok_work(struct work_struct *work);
59c66ac9dbSNicholas Bellinger
init_se_kmem_caches(void)60e3d6f909SAndy Grover int init_se_kmem_caches(void)
61c66ac9dbSNicholas Bellinger {
62c66ac9dbSNicholas Bellinger se_sess_cache = kmem_cache_create("se_sess_cache",
63c66ac9dbSNicholas Bellinger sizeof(struct se_session), __alignof__(struct se_session),
64c66ac9dbSNicholas Bellinger 0, NULL);
656708bb27SAndy Grover if (!se_sess_cache) {
666708bb27SAndy Grover pr_err("kmem_cache_create() for struct se_session"
67c66ac9dbSNicholas Bellinger " failed\n");
68c8e31f26SAndy Grover goto out;
69c66ac9dbSNicholas Bellinger }
70c66ac9dbSNicholas Bellinger se_ua_cache = kmem_cache_create("se_ua_cache",
71c66ac9dbSNicholas Bellinger sizeof(struct se_ua), __alignof__(struct se_ua),
72c66ac9dbSNicholas Bellinger 0, NULL);
736708bb27SAndy Grover if (!se_ua_cache) {
746708bb27SAndy Grover pr_err("kmem_cache_create() for struct se_ua failed\n");
7535e0e757SChristoph Hellwig goto out_free_sess_cache;
76c66ac9dbSNicholas Bellinger }
77c66ac9dbSNicholas Bellinger t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
78c66ac9dbSNicholas Bellinger sizeof(struct t10_pr_registration),
79c66ac9dbSNicholas Bellinger __alignof__(struct t10_pr_registration), 0, NULL);
806708bb27SAndy Grover if (!t10_pr_reg_cache) {
816708bb27SAndy Grover pr_err("kmem_cache_create() for struct t10_pr_registration"
82c66ac9dbSNicholas Bellinger " failed\n");
8335e0e757SChristoph Hellwig goto out_free_ua_cache;
84c66ac9dbSNicholas Bellinger }
85c66ac9dbSNicholas Bellinger t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
86c66ac9dbSNicholas Bellinger sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
87c66ac9dbSNicholas Bellinger 0, NULL);
886708bb27SAndy Grover if (!t10_alua_lu_gp_cache) {
896708bb27SAndy Grover pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
90c66ac9dbSNicholas Bellinger " failed\n");
9135e0e757SChristoph Hellwig goto out_free_pr_reg_cache;
92c66ac9dbSNicholas Bellinger }
93c66ac9dbSNicholas Bellinger t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
94c66ac9dbSNicholas Bellinger sizeof(struct t10_alua_lu_gp_member),
95c66ac9dbSNicholas Bellinger __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
966708bb27SAndy Grover if (!t10_alua_lu_gp_mem_cache) {
976708bb27SAndy Grover pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
98c66ac9dbSNicholas Bellinger "cache failed\n");
9935e0e757SChristoph Hellwig goto out_free_lu_gp_cache;
100c66ac9dbSNicholas Bellinger }
101c66ac9dbSNicholas Bellinger t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
102c66ac9dbSNicholas Bellinger sizeof(struct t10_alua_tg_pt_gp),
103c66ac9dbSNicholas Bellinger __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
1046708bb27SAndy Grover if (!t10_alua_tg_pt_gp_cache) {
1056708bb27SAndy Grover pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
106c66ac9dbSNicholas Bellinger "cache failed\n");
10735e0e757SChristoph Hellwig goto out_free_lu_gp_mem_cache;
108c66ac9dbSNicholas Bellinger }
109229d4f11SHannes Reinecke t10_alua_lba_map_cache = kmem_cache_create(
110229d4f11SHannes Reinecke "t10_alua_lba_map_cache",
111229d4f11SHannes Reinecke sizeof(struct t10_alua_lba_map),
112229d4f11SHannes Reinecke __alignof__(struct t10_alua_lba_map), 0, NULL);
113229d4f11SHannes Reinecke if (!t10_alua_lba_map_cache) {
114229d4f11SHannes Reinecke pr_err("kmem_cache_create() for t10_alua_lba_map_"
115229d4f11SHannes Reinecke "cache failed\n");
116adf653f9SChristoph Hellwig goto out_free_tg_pt_gp_cache;
117229d4f11SHannes Reinecke }
118229d4f11SHannes Reinecke t10_alua_lba_map_mem_cache = kmem_cache_create(
119229d4f11SHannes Reinecke "t10_alua_lba_map_mem_cache",
120229d4f11SHannes Reinecke sizeof(struct t10_alua_lba_map_member),
121229d4f11SHannes Reinecke __alignof__(struct t10_alua_lba_map_member), 0, NULL);
122229d4f11SHannes Reinecke if (!t10_alua_lba_map_mem_cache) {
123229d4f11SHannes Reinecke pr_err("kmem_cache_create() for t10_alua_lba_map_mem_"
124229d4f11SHannes Reinecke "cache failed\n");
125229d4f11SHannes Reinecke goto out_free_lba_map_cache;
126229d4f11SHannes Reinecke }
127c66ac9dbSNicholas Bellinger
12835e0e757SChristoph Hellwig target_completion_wq = alloc_workqueue("target_completion",
12935e0e757SChristoph Hellwig WQ_MEM_RECLAIM, 0);
13035e0e757SChristoph Hellwig if (!target_completion_wq)
131229d4f11SHannes Reinecke goto out_free_lba_map_mem_cache;
13235e0e757SChristoph Hellwig
133eb44ce8cSMike Christie target_submission_wq = alloc_workqueue("target_submission",
134eb44ce8cSMike Christie WQ_MEM_RECLAIM, 0);
135eb44ce8cSMike Christie if (!target_submission_wq)
136eb44ce8cSMike Christie goto out_free_completion_wq;
137eb44ce8cSMike Christie
138c66ac9dbSNicholas Bellinger return 0;
13935e0e757SChristoph Hellwig
140eb44ce8cSMike Christie out_free_completion_wq:
141eb44ce8cSMike Christie destroy_workqueue(target_completion_wq);
142229d4f11SHannes Reinecke out_free_lba_map_mem_cache:
143229d4f11SHannes Reinecke kmem_cache_destroy(t10_alua_lba_map_mem_cache);
144229d4f11SHannes Reinecke out_free_lba_map_cache:
145229d4f11SHannes Reinecke kmem_cache_destroy(t10_alua_lba_map_cache);
14635e0e757SChristoph Hellwig out_free_tg_pt_gp_cache:
14735e0e757SChristoph Hellwig kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
14835e0e757SChristoph Hellwig out_free_lu_gp_mem_cache:
14935e0e757SChristoph Hellwig kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
15035e0e757SChristoph Hellwig out_free_lu_gp_cache:
15135e0e757SChristoph Hellwig kmem_cache_destroy(t10_alua_lu_gp_cache);
15235e0e757SChristoph Hellwig out_free_pr_reg_cache:
15335e0e757SChristoph Hellwig kmem_cache_destroy(t10_pr_reg_cache);
15435e0e757SChristoph Hellwig out_free_ua_cache:
15535e0e757SChristoph Hellwig kmem_cache_destroy(se_ua_cache);
15635e0e757SChristoph Hellwig out_free_sess_cache:
15735e0e757SChristoph Hellwig kmem_cache_destroy(se_sess_cache);
15835e0e757SChristoph Hellwig out:
159e3d6f909SAndy Grover return -ENOMEM;
160c66ac9dbSNicholas Bellinger }
161c66ac9dbSNicholas Bellinger
release_se_kmem_caches(void)162e3d6f909SAndy Grover void release_se_kmem_caches(void)
163c66ac9dbSNicholas Bellinger {
164eb44ce8cSMike Christie destroy_workqueue(target_submission_wq);
16535e0e757SChristoph Hellwig destroy_workqueue(target_completion_wq);
166c66ac9dbSNicholas Bellinger kmem_cache_destroy(se_sess_cache);
167c66ac9dbSNicholas Bellinger kmem_cache_destroy(se_ua_cache);
168c66ac9dbSNicholas Bellinger kmem_cache_destroy(t10_pr_reg_cache);
169c66ac9dbSNicholas Bellinger kmem_cache_destroy(t10_alua_lu_gp_cache);
170c66ac9dbSNicholas Bellinger kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
171c66ac9dbSNicholas Bellinger kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
172229d4f11SHannes Reinecke kmem_cache_destroy(t10_alua_lba_map_cache);
173229d4f11SHannes Reinecke kmem_cache_destroy(t10_alua_lba_map_mem_cache);
174c66ac9dbSNicholas Bellinger }
175c66ac9dbSNicholas Bellinger
176e3d6f909SAndy Grover /* This code ensures unique mib indexes are handed out. */
177e3d6f909SAndy Grover static DEFINE_SPINLOCK(scsi_mib_index_lock);
178e3d6f909SAndy Grover static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
179e89d15eeSNicholas Bellinger
180e89d15eeSNicholas Bellinger /*
181e89d15eeSNicholas Bellinger * Allocate a new row index for the entry type specified
182e89d15eeSNicholas Bellinger */
scsi_get_new_index(scsi_index_t type)183e89d15eeSNicholas Bellinger u32 scsi_get_new_index(scsi_index_t type)
184e89d15eeSNicholas Bellinger {
185e89d15eeSNicholas Bellinger u32 new_index;
186e89d15eeSNicholas Bellinger
187e3d6f909SAndy Grover BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
188e89d15eeSNicholas Bellinger
189e3d6f909SAndy Grover spin_lock(&scsi_mib_index_lock);
190e3d6f909SAndy Grover new_index = ++scsi_mib_index[type];
191e3d6f909SAndy Grover spin_unlock(&scsi_mib_index_lock);
192e89d15eeSNicholas Bellinger
193e89d15eeSNicholas Bellinger return new_index;
194e89d15eeSNicholas Bellinger }
195e89d15eeSNicholas Bellinger
transport_subsystem_check_init(void)196dbc5623eSNicholas Bellinger void transport_subsystem_check_init(void)
197c66ac9dbSNicholas Bellinger {
198c66ac9dbSNicholas Bellinger int ret;
199283669d2SAndy Grover static int sub_api_initialized;
200c66ac9dbSNicholas Bellinger
201dbc5623eSNicholas Bellinger if (sub_api_initialized)
202dbc5623eSNicholas Bellinger return;
203dbc5623eSNicholas Bellinger
2046110f37fSRoland Dreier ret = IS_ENABLED(CONFIG_TCM_IBLOCK) && request_module("target_core_iblock");
205c66ac9dbSNicholas Bellinger if (ret != 0)
2066708bb27SAndy Grover pr_err("Unable to load target_core_iblock\n");
207c66ac9dbSNicholas Bellinger
2086110f37fSRoland Dreier ret = IS_ENABLED(CONFIG_TCM_FILEIO) && request_module("target_core_file");
209c66ac9dbSNicholas Bellinger if (ret != 0)
2106708bb27SAndy Grover pr_err("Unable to load target_core_file\n");
211c66ac9dbSNicholas Bellinger
2126110f37fSRoland Dreier ret = IS_ENABLED(CONFIG_TCM_PSCSI) && request_module("target_core_pscsi");
213c66ac9dbSNicholas Bellinger if (ret != 0)
2146708bb27SAndy Grover pr_err("Unable to load target_core_pscsi\n");
215c66ac9dbSNicholas Bellinger
2166110f37fSRoland Dreier ret = IS_ENABLED(CONFIG_TCM_USER2) && request_module("target_core_user");
2177c9e7a6fSAndy Grover if (ret != 0)
2187c9e7a6fSAndy Grover pr_err("Unable to load target_core_user\n");
2197c9e7a6fSAndy Grover
220e3d6f909SAndy Grover sub_api_initialized = 1;
221c66ac9dbSNicholas Bellinger }
222c66ac9dbSNicholas Bellinger
target_release_cmd_refcnt(struct percpu_ref * ref)223becd9be6SMike Christie static void target_release_cmd_refcnt(struct percpu_ref *ref)
224ad669505SBart Van Assche {
225becd9be6SMike Christie struct target_cmd_counter *cmd_cnt = container_of(ref,
226becd9be6SMike Christie typeof(*cmd_cnt),
227becd9be6SMike Christie refcnt);
228becd9be6SMike Christie wake_up(&cmd_cnt->refcnt_wq);
229becd9be6SMike Christie }
230ad669505SBart Van Assche
target_alloc_cmd_counter(void)2314edba7e4SMike Christie struct target_cmd_counter *target_alloc_cmd_counter(void)
232becd9be6SMike Christie {
233becd9be6SMike Christie struct target_cmd_counter *cmd_cnt;
234becd9be6SMike Christie int rc;
235becd9be6SMike Christie
236becd9be6SMike Christie cmd_cnt = kzalloc(sizeof(*cmd_cnt), GFP_KERNEL);
237becd9be6SMike Christie if (!cmd_cnt)
238becd9be6SMike Christie return NULL;
239becd9be6SMike Christie
240becd9be6SMike Christie init_completion(&cmd_cnt->stop_done);
241becd9be6SMike Christie init_waitqueue_head(&cmd_cnt->refcnt_wq);
242becd9be6SMike Christie atomic_set(&cmd_cnt->stopped, 0);
243becd9be6SMike Christie
244becd9be6SMike Christie rc = percpu_ref_init(&cmd_cnt->refcnt, target_release_cmd_refcnt, 0,
245becd9be6SMike Christie GFP_KERNEL);
246becd9be6SMike Christie if (rc)
247becd9be6SMike Christie goto free_cmd_cnt;
248becd9be6SMike Christie
249becd9be6SMike Christie return cmd_cnt;
250becd9be6SMike Christie
251becd9be6SMike Christie free_cmd_cnt:
252becd9be6SMike Christie kfree(cmd_cnt);
253becd9be6SMike Christie return NULL;
254becd9be6SMike Christie }
2554edba7e4SMike Christie EXPORT_SYMBOL_GPL(target_alloc_cmd_counter);
256becd9be6SMike Christie
target_free_cmd_counter(struct target_cmd_counter * cmd_cnt)2576d256beeSMike Christie void target_free_cmd_counter(struct target_cmd_counter *cmd_cnt)
258becd9be6SMike Christie {
259becd9be6SMike Christie /*
260becd9be6SMike Christie * Drivers like loop do not call target_stop_session during session
261becd9be6SMike Christie * shutdown so we have to drop the ref taken at init time here.
262becd9be6SMike Christie */
263becd9be6SMike Christie if (!atomic_read(&cmd_cnt->stopped))
264becd9be6SMike Christie percpu_ref_put(&cmd_cnt->refcnt);
265becd9be6SMike Christie
266becd9be6SMike Christie percpu_ref_exit(&cmd_cnt->refcnt);
267d14e3e55SDavid Disseldorp kfree(cmd_cnt);
268ad669505SBart Van Assche }
2696d256beeSMike Christie EXPORT_SYMBOL_GPL(target_free_cmd_counter);
270ad669505SBart Van Assche
271317f8971SBart Van Assche /**
272d1bff07fSBart Van Assche * transport_init_session - initialize a session object
273d1bff07fSBart Van Assche * @se_sess: Session object pointer.
274d1bff07fSBart Van Assche *
275d1bff07fSBart Van Assche * The caller must have zero-initialized @se_sess before calling this function.
276d1bff07fSBart Van Assche */
transport_init_session(struct se_session * se_sess)2774edba7e4SMike Christie void transport_init_session(struct se_session *se_sess)
278d1bff07fSBart Van Assche {
279d1bff07fSBart Van Assche INIT_LIST_HEAD(&se_sess->sess_list);
280d1bff07fSBart Van Assche INIT_LIST_HEAD(&se_sess->sess_acl_list);
281d1bff07fSBart Van Assche spin_lock_init(&se_sess->sess_cmd_lock);
282d1bff07fSBart Van Assche }
283d1bff07fSBart Van Assche EXPORT_SYMBOL(transport_init_session);
284d1bff07fSBart Van Assche
285d1bff07fSBart Van Assche /**
286317f8971SBart Van Assche * transport_alloc_session - allocate a session object and initialize it
287317f8971SBart Van Assche * @sup_prot_ops: bitmask that defines which T10-PI modes are supported.
288317f8971SBart Van Assche */
transport_alloc_session(enum target_prot_op sup_prot_ops)289317f8971SBart Van Assche struct se_session *transport_alloc_session(enum target_prot_op sup_prot_ops)
290c66ac9dbSNicholas Bellinger {
291c66ac9dbSNicholas Bellinger struct se_session *se_sess;
292c66ac9dbSNicholas Bellinger
293c66ac9dbSNicholas Bellinger se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
2946708bb27SAndy Grover if (!se_sess) {
2956708bb27SAndy Grover pr_err("Unable to allocate struct se_session from"
296c66ac9dbSNicholas Bellinger " se_sess_cache\n");
297c66ac9dbSNicholas Bellinger return ERR_PTR(-ENOMEM);
298c66ac9dbSNicholas Bellinger }
2994edba7e4SMike Christie transport_init_session(se_sess);
300e70beee7SNicholas Bellinger se_sess->sup_prot_ops = sup_prot_ops;
301c66ac9dbSNicholas Bellinger
302c66ac9dbSNicholas Bellinger return se_sess;
303c66ac9dbSNicholas Bellinger }
304317f8971SBart Van Assche EXPORT_SYMBOL(transport_alloc_session);
305c66ac9dbSNicholas Bellinger
306317f8971SBart Van Assche /**
307317f8971SBart Van Assche * transport_alloc_session_tags - allocate target driver private data
308317f8971SBart Van Assche * @se_sess: Session pointer.
309317f8971SBart Van Assche * @tag_num: Maximum number of in-flight commands between initiator and target.
310317f8971SBart Van Assche * @tag_size: Size in bytes of the private data a target driver associates with
311317f8971SBart Van Assche * each command.
312317f8971SBart Van Assche */
transport_alloc_session_tags(struct se_session * se_sess,unsigned int tag_num,unsigned int tag_size)313c0add7fdSNicholas Bellinger int transport_alloc_session_tags(struct se_session *se_sess,
314c0add7fdSNicholas Bellinger unsigned int tag_num, unsigned int tag_size)
315c0add7fdSNicholas Bellinger {
316c0add7fdSNicholas Bellinger int rc;
317c0add7fdSNicholas Bellinger
3185d6cd9feSBart Van Assche se_sess->sess_cmd_map = kvcalloc(tag_size, tag_num,
3195d6cd9feSBart Van Assche GFP_KERNEL | __GFP_RETRY_MAYFAIL);
320c0add7fdSNicholas Bellinger if (!se_sess->sess_cmd_map) {
321c0add7fdSNicholas Bellinger pr_err("Unable to allocate se_sess->sess_cmd_map\n");
322c0add7fdSNicholas Bellinger return -ENOMEM;
323c0add7fdSNicholas Bellinger }
324c0add7fdSNicholas Bellinger
32510e9cbb6SMatthew Wilcox rc = sbitmap_queue_init_node(&se_sess->sess_tag_pool, tag_num, -1,
32610e9cbb6SMatthew Wilcox false, GFP_KERNEL, NUMA_NO_NODE);
327c0add7fdSNicholas Bellinger if (rc < 0) {
328c0add7fdSNicholas Bellinger pr_err("Unable to init se_sess->sess_tag_pool,"
329c0add7fdSNicholas Bellinger " tag_num: %u\n", tag_num);
330de64d3a6SPekka Enberg kvfree(se_sess->sess_cmd_map);
331c0add7fdSNicholas Bellinger se_sess->sess_cmd_map = NULL;
332c0add7fdSNicholas Bellinger return -ENOMEM;
333c0add7fdSNicholas Bellinger }
334c0add7fdSNicholas Bellinger
335c0add7fdSNicholas Bellinger return 0;
336c0add7fdSNicholas Bellinger }
337c0add7fdSNicholas Bellinger EXPORT_SYMBOL(transport_alloc_session_tags);
338c0add7fdSNicholas Bellinger
339317f8971SBart Van Assche /**
340317f8971SBart Van Assche * transport_init_session_tags - allocate a session and target driver private data
341317f8971SBart Van Assche * @tag_num: Maximum number of in-flight commands between initiator and target.
342317f8971SBart Van Assche * @tag_size: Size in bytes of the private data a target driver associates with
343317f8971SBart Van Assche * each command.
344317f8971SBart Van Assche * @sup_prot_ops: bitmask that defines which T10-PI modes are supported.
345317f8971SBart Van Assche */
3463cd14285SMike Christie static struct se_session *
transport_init_session_tags(unsigned int tag_num,unsigned int tag_size,enum target_prot_op sup_prot_ops)3473cd14285SMike Christie transport_init_session_tags(unsigned int tag_num, unsigned int tag_size,
348e70beee7SNicholas Bellinger enum target_prot_op sup_prot_ops)
349c0add7fdSNicholas Bellinger {
350c0add7fdSNicholas Bellinger struct se_session *se_sess;
351c0add7fdSNicholas Bellinger int rc;
352c0add7fdSNicholas Bellinger
3537861728dSNicholas Bellinger if (tag_num != 0 && !tag_size) {
3547861728dSNicholas Bellinger pr_err("init_session_tags called with percpu-ida tag_num:"
3557861728dSNicholas Bellinger " %u, but zero tag_size\n", tag_num);
3567861728dSNicholas Bellinger return ERR_PTR(-EINVAL);
3577861728dSNicholas Bellinger }
3587861728dSNicholas Bellinger if (!tag_num && tag_size) {
3597861728dSNicholas Bellinger pr_err("init_session_tags called with percpu-ida tag_size:"
3607861728dSNicholas Bellinger " %u, but zero tag_num\n", tag_size);
3617861728dSNicholas Bellinger return ERR_PTR(-EINVAL);
3627861728dSNicholas Bellinger }
3637861728dSNicholas Bellinger
364317f8971SBart Van Assche se_sess = transport_alloc_session(sup_prot_ops);
365c0add7fdSNicholas Bellinger if (IS_ERR(se_sess))
366c0add7fdSNicholas Bellinger return se_sess;
367c0add7fdSNicholas Bellinger
368c0add7fdSNicholas Bellinger rc = transport_alloc_session_tags(se_sess, tag_num, tag_size);
369c0add7fdSNicholas Bellinger if (rc < 0) {
370c0add7fdSNicholas Bellinger transport_free_session(se_sess);
371c0add7fdSNicholas Bellinger return ERR_PTR(-ENOMEM);
372c0add7fdSNicholas Bellinger }
373c0add7fdSNicholas Bellinger
374c0add7fdSNicholas Bellinger return se_sess;
375c0add7fdSNicholas Bellinger }
376c0add7fdSNicholas Bellinger
377c66ac9dbSNicholas Bellinger /*
378140854cbSNicholas Bellinger * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
379c66ac9dbSNicholas Bellinger */
__transport_register_session(struct se_portal_group * se_tpg,struct se_node_acl * se_nacl,struct se_session * se_sess,void * fabric_sess_ptr)380c66ac9dbSNicholas Bellinger void __transport_register_session(
381c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg,
382c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl,
383c66ac9dbSNicholas Bellinger struct se_session *se_sess,
384c66ac9dbSNicholas Bellinger void *fabric_sess_ptr)
385c66ac9dbSNicholas Bellinger {
3869ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo = se_tpg->se_tpg_tfo;
387c66ac9dbSNicholas Bellinger unsigned char buf[PR_REG_ISID_LEN];
3886a64f6e1SMike Christie unsigned long flags;
389c66ac9dbSNicholas Bellinger
390c66ac9dbSNicholas Bellinger se_sess->se_tpg = se_tpg;
391c66ac9dbSNicholas Bellinger se_sess->fabric_sess_ptr = fabric_sess_ptr;
392c66ac9dbSNicholas Bellinger /*
393c66ac9dbSNicholas Bellinger * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
394c66ac9dbSNicholas Bellinger *
395c66ac9dbSNicholas Bellinger * Only set for struct se_session's that will actually be moving I/O.
396c66ac9dbSNicholas Bellinger * eg: *NOT* discovery sessions.
397c66ac9dbSNicholas Bellinger */
398c66ac9dbSNicholas Bellinger if (se_nacl) {
399c66ac9dbSNicholas Bellinger /*
400bffb5128SNicholas Bellinger *
401bffb5128SNicholas Bellinger * Determine if fabric allows for T10-PI feature bits exposed to
402bffb5128SNicholas Bellinger * initiators for device backends with !dev->dev_attrib.pi_prot_type.
403bffb5128SNicholas Bellinger *
404bffb5128SNicholas Bellinger * If so, then always save prot_type on a per se_node_acl node
405bffb5128SNicholas Bellinger * basis and re-instate the previous sess_prot_type to avoid
406bffb5128SNicholas Bellinger * disabling PI from below any previously initiator side
407bffb5128SNicholas Bellinger * registered LUNs.
408bffb5128SNicholas Bellinger */
409bffb5128SNicholas Bellinger if (se_nacl->saved_prot_type)
410bffb5128SNicholas Bellinger se_sess->sess_prot_type = se_nacl->saved_prot_type;
411bffb5128SNicholas Bellinger else if (tfo->tpg_check_prot_fabric_only)
412bffb5128SNicholas Bellinger se_sess->sess_prot_type = se_nacl->saved_prot_type =
413bffb5128SNicholas Bellinger tfo->tpg_check_prot_fabric_only(se_tpg);
414bffb5128SNicholas Bellinger /*
415c66ac9dbSNicholas Bellinger * If the fabric module supports an ISID based TransportID,
416c66ac9dbSNicholas Bellinger * save this value in binary from the fabric I_T Nexus now.
417c66ac9dbSNicholas Bellinger */
418e3d6f909SAndy Grover if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
419c66ac9dbSNicholas Bellinger memset(&buf[0], 0, PR_REG_ISID_LEN);
420e3d6f909SAndy Grover se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
421c66ac9dbSNicholas Bellinger &buf[0], PR_REG_ISID_LEN);
422c66ac9dbSNicholas Bellinger se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
423c66ac9dbSNicholas Bellinger }
424afb999ffSNicholas Bellinger
4256a64f6e1SMike Christie spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
426c66ac9dbSNicholas Bellinger /*
427c66ac9dbSNicholas Bellinger * The se_nacl->nacl_sess pointer will be set to the
428c66ac9dbSNicholas Bellinger * last active I_T Nexus for each struct se_node_acl.
429c66ac9dbSNicholas Bellinger */
430c66ac9dbSNicholas Bellinger se_nacl->nacl_sess = se_sess;
431c66ac9dbSNicholas Bellinger
432c66ac9dbSNicholas Bellinger list_add_tail(&se_sess->sess_acl_list,
433c66ac9dbSNicholas Bellinger &se_nacl->acl_sess_list);
4346a64f6e1SMike Christie spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
435c66ac9dbSNicholas Bellinger }
436c66ac9dbSNicholas Bellinger list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
437c66ac9dbSNicholas Bellinger
4386708bb27SAndy Grover pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
43930c7ca93SDavid Disseldorp se_tpg->se_tpg_tfo->fabric_name, se_sess->fabric_sess_ptr);
440c66ac9dbSNicholas Bellinger }
441c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(__transport_register_session);
442c66ac9dbSNicholas Bellinger
transport_register_session(struct se_portal_group * se_tpg,struct se_node_acl * se_nacl,struct se_session * se_sess,void * fabric_sess_ptr)443c66ac9dbSNicholas Bellinger void transport_register_session(
444c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg,
445c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl,
446c66ac9dbSNicholas Bellinger struct se_session *se_sess,
447c66ac9dbSNicholas Bellinger void *fabric_sess_ptr)
448c66ac9dbSNicholas Bellinger {
449140854cbSNicholas Bellinger unsigned long flags;
450140854cbSNicholas Bellinger
451140854cbSNicholas Bellinger spin_lock_irqsave(&se_tpg->session_lock, flags);
452c66ac9dbSNicholas Bellinger __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
453140854cbSNicholas Bellinger spin_unlock_irqrestore(&se_tpg->session_lock, flags);
454c66ac9dbSNicholas Bellinger }
455c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_register_session);
456c66ac9dbSNicholas Bellinger
4577861728dSNicholas Bellinger struct se_session *
target_setup_session(struct se_portal_group * tpg,unsigned int tag_num,unsigned int tag_size,enum target_prot_op prot_op,const char * initiatorname,void * private,int (* callback)(struct se_portal_group *,struct se_session *,void *))458fa834287SMike Christie target_setup_session(struct se_portal_group *tpg,
4597861728dSNicholas Bellinger unsigned int tag_num, unsigned int tag_size,
4607861728dSNicholas Bellinger enum target_prot_op prot_op,
4617861728dSNicholas Bellinger const char *initiatorname, void *private,
4627861728dSNicholas Bellinger int (*callback)(struct se_portal_group *,
4637861728dSNicholas Bellinger struct se_session *, void *))
4647861728dSNicholas Bellinger {
4654edba7e4SMike Christie struct target_cmd_counter *cmd_cnt;
4667861728dSNicholas Bellinger struct se_session *sess;
4674edba7e4SMike Christie int rc;
4687861728dSNicholas Bellinger
4694edba7e4SMike Christie cmd_cnt = target_alloc_cmd_counter();
4704edba7e4SMike Christie if (!cmd_cnt)
4714edba7e4SMike Christie return ERR_PTR(-ENOMEM);
4727861728dSNicholas Bellinger /*
4737861728dSNicholas Bellinger * If the fabric driver is using percpu-ida based pre allocation
4747861728dSNicholas Bellinger * of I/O descriptor tags, go ahead and perform that setup now..
4757861728dSNicholas Bellinger */
4767861728dSNicholas Bellinger if (tag_num != 0)
4777861728dSNicholas Bellinger sess = transport_init_session_tags(tag_num, tag_size, prot_op);
4787861728dSNicholas Bellinger else
479317f8971SBart Van Assche sess = transport_alloc_session(prot_op);
4807861728dSNicholas Bellinger
4814edba7e4SMike Christie if (IS_ERR(sess)) {
4824edba7e4SMike Christie rc = PTR_ERR(sess);
4834edba7e4SMike Christie goto free_cnt;
4844edba7e4SMike Christie }
4854edba7e4SMike Christie sess->cmd_cnt = cmd_cnt;
4867861728dSNicholas Bellinger
4877861728dSNicholas Bellinger sess->se_node_acl = core_tpg_check_initiator_node_acl(tpg,
4887861728dSNicholas Bellinger (unsigned char *)initiatorname);
4897861728dSNicholas Bellinger if (!sess->se_node_acl) {
4904edba7e4SMike Christie rc = -EACCES;
4914edba7e4SMike Christie goto free_sess;
4927861728dSNicholas Bellinger }
4937861728dSNicholas Bellinger /*
4947861728dSNicholas Bellinger * Go ahead and perform any remaining fabric setup that is
4957861728dSNicholas Bellinger * required before transport_register_session().
4967861728dSNicholas Bellinger */
4977861728dSNicholas Bellinger if (callback != NULL) {
4984edba7e4SMike Christie rc = callback(tpg, sess, private);
4994edba7e4SMike Christie if (rc)
5004edba7e4SMike Christie goto free_sess;
5017861728dSNicholas Bellinger }
5027861728dSNicholas Bellinger
5037861728dSNicholas Bellinger transport_register_session(tpg, sess->se_node_acl, sess, private);
5047861728dSNicholas Bellinger return sess;
5054edba7e4SMike Christie
5064edba7e4SMike Christie free_sess:
5074edba7e4SMike Christie transport_free_session(sess);
50891271699SBob Pearson return ERR_PTR(rc);
50991271699SBob Pearson
5104edba7e4SMike Christie free_cnt:
5114edba7e4SMike Christie target_free_cmd_counter(cmd_cnt);
5124edba7e4SMike Christie return ERR_PTR(rc);
5137861728dSNicholas Bellinger }
514fa834287SMike Christie EXPORT_SYMBOL(target_setup_session);
5157861728dSNicholas Bellinger
target_show_dynamic_sessions(struct se_portal_group * se_tpg,char * page)516f8e471f9SNicholas Bellinger ssize_t target_show_dynamic_sessions(struct se_portal_group *se_tpg, char *page)
517f8e471f9SNicholas Bellinger {
518f8e471f9SNicholas Bellinger struct se_session *se_sess;
519f8e471f9SNicholas Bellinger ssize_t len = 0;
520f8e471f9SNicholas Bellinger
521f8e471f9SNicholas Bellinger spin_lock_bh(&se_tpg->session_lock);
522f8e471f9SNicholas Bellinger list_for_each_entry(se_sess, &se_tpg->tpg_sess_list, sess_list) {
523f8e471f9SNicholas Bellinger if (!se_sess->se_node_acl)
524f8e471f9SNicholas Bellinger continue;
525f8e471f9SNicholas Bellinger if (!se_sess->se_node_acl->dynamic_node_acl)
526f8e471f9SNicholas Bellinger continue;
527f8e471f9SNicholas Bellinger if (strlen(se_sess->se_node_acl->initiatorname) + 1 + len > PAGE_SIZE)
528f8e471f9SNicholas Bellinger break;
529f8e471f9SNicholas Bellinger
530f8e471f9SNicholas Bellinger len += snprintf(page + len, PAGE_SIZE - len, "%s\n",
531f8e471f9SNicholas Bellinger se_sess->se_node_acl->initiatorname);
532f8e471f9SNicholas Bellinger len += 1; /* Include NULL terminator */
533f8e471f9SNicholas Bellinger }
534f8e471f9SNicholas Bellinger spin_unlock_bh(&se_tpg->session_lock);
535f8e471f9SNicholas Bellinger
536f8e471f9SNicholas Bellinger return len;
537f8e471f9SNicholas Bellinger }
538f8e471f9SNicholas Bellinger EXPORT_SYMBOL(target_show_dynamic_sessions);
539f8e471f9SNicholas Bellinger
target_complete_nacl(struct kref * kref)540afb999ffSNicholas Bellinger static void target_complete_nacl(struct kref *kref)
541afb999ffSNicholas Bellinger {
542afb999ffSNicholas Bellinger struct se_node_acl *nacl = container_of(kref,
543afb999ffSNicholas Bellinger struct se_node_acl, acl_kref);
54401d4d673SNicholas Bellinger struct se_portal_group *se_tpg = nacl->se_tpg;
545afb999ffSNicholas Bellinger
54601d4d673SNicholas Bellinger if (!nacl->dynamic_stop) {
547afb999ffSNicholas Bellinger complete(&nacl->acl_free_comp);
54801d4d673SNicholas Bellinger return;
54901d4d673SNicholas Bellinger }
55001d4d673SNicholas Bellinger
55101d4d673SNicholas Bellinger mutex_lock(&se_tpg->acl_node_mutex);
5526f48655fSNicholas Bellinger list_del_init(&nacl->acl_list);
55301d4d673SNicholas Bellinger mutex_unlock(&se_tpg->acl_node_mutex);
55401d4d673SNicholas Bellinger
55501d4d673SNicholas Bellinger core_tpg_wait_for_nacl_pr_ref(nacl);
55601d4d673SNicholas Bellinger core_free_device_list_for_node(nacl, se_tpg);
55701d4d673SNicholas Bellinger kfree(nacl);
558afb999ffSNicholas Bellinger }
559afb999ffSNicholas Bellinger
target_put_nacl(struct se_node_acl * nacl)560afb999ffSNicholas Bellinger void target_put_nacl(struct se_node_acl *nacl)
561afb999ffSNicholas Bellinger {
562afb999ffSNicholas Bellinger kref_put(&nacl->acl_kref, target_complete_nacl);
563afb999ffSNicholas Bellinger }
56421aaa23bSNicholas Bellinger EXPORT_SYMBOL(target_put_nacl);
565afb999ffSNicholas Bellinger
transport_deregister_session_configfs(struct se_session * se_sess)566c66ac9dbSNicholas Bellinger void transport_deregister_session_configfs(struct se_session *se_sess)
567c66ac9dbSNicholas Bellinger {
568c66ac9dbSNicholas Bellinger struct se_node_acl *se_nacl;
56923388864SRoland Dreier unsigned long flags;
570c66ac9dbSNicholas Bellinger /*
571c66ac9dbSNicholas Bellinger * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
572c66ac9dbSNicholas Bellinger */
573c66ac9dbSNicholas Bellinger se_nacl = se_sess->se_node_acl;
5746708bb27SAndy Grover if (se_nacl) {
57523388864SRoland Dreier spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
576fba81f88SChristoph Hellwig if (!list_empty(&se_sess->sess_acl_list))
577fba81f88SChristoph Hellwig list_del_init(&se_sess->sess_acl_list);
578c66ac9dbSNicholas Bellinger /*
579c66ac9dbSNicholas Bellinger * If the session list is empty, then clear the pointer.
580c66ac9dbSNicholas Bellinger * Otherwise, set the struct se_session pointer from the tail
581c66ac9dbSNicholas Bellinger * element of the per struct se_node_acl active session list.
582c66ac9dbSNicholas Bellinger */
583c66ac9dbSNicholas Bellinger if (list_empty(&se_nacl->acl_sess_list))
584c66ac9dbSNicholas Bellinger se_nacl->nacl_sess = NULL;
585c66ac9dbSNicholas Bellinger else {
586c66ac9dbSNicholas Bellinger se_nacl->nacl_sess = container_of(
587c66ac9dbSNicholas Bellinger se_nacl->acl_sess_list.prev,
588c66ac9dbSNicholas Bellinger struct se_session, sess_acl_list);
589c66ac9dbSNicholas Bellinger }
59023388864SRoland Dreier spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
591c66ac9dbSNicholas Bellinger }
592c66ac9dbSNicholas Bellinger }
593c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_deregister_session_configfs);
594c66ac9dbSNicholas Bellinger
transport_free_session(struct se_session * se_sess)595c66ac9dbSNicholas Bellinger void transport_free_session(struct se_session *se_sess)
596c66ac9dbSNicholas Bellinger {
59721aaa23bSNicholas Bellinger struct se_node_acl *se_nacl = se_sess->se_node_acl;
59801d4d673SNicholas Bellinger
59921aaa23bSNicholas Bellinger /*
60021aaa23bSNicholas Bellinger * Drop the se_node_acl->nacl_kref obtained from within
60121aaa23bSNicholas Bellinger * core_tpg_get_initiator_node_acl().
60221aaa23bSNicholas Bellinger */
60321aaa23bSNicholas Bellinger if (se_nacl) {
60401d4d673SNicholas Bellinger struct se_portal_group *se_tpg = se_nacl->se_tpg;
60501d4d673SNicholas Bellinger const struct target_core_fabric_ops *se_tfo = se_tpg->se_tpg_tfo;
60601d4d673SNicholas Bellinger unsigned long flags;
60701d4d673SNicholas Bellinger
60821aaa23bSNicholas Bellinger se_sess->se_node_acl = NULL;
60901d4d673SNicholas Bellinger
61001d4d673SNicholas Bellinger /*
61101d4d673SNicholas Bellinger * Also determine if we need to drop the extra ->cmd_kref if
61201d4d673SNicholas Bellinger * it had been previously dynamically generated, and
61301d4d673SNicholas Bellinger * the endpoint is not caching dynamic ACLs.
61401d4d673SNicholas Bellinger */
61501d4d673SNicholas Bellinger mutex_lock(&se_tpg->acl_node_mutex);
61601d4d673SNicholas Bellinger if (se_nacl->dynamic_node_acl &&
61701d4d673SNicholas Bellinger !se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
61801d4d673SNicholas Bellinger spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
61901d4d673SNicholas Bellinger if (list_empty(&se_nacl->acl_sess_list))
62001d4d673SNicholas Bellinger se_nacl->dynamic_stop = true;
62101d4d673SNicholas Bellinger spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
62201d4d673SNicholas Bellinger
62301d4d673SNicholas Bellinger if (se_nacl->dynamic_stop)
6246f48655fSNicholas Bellinger list_del_init(&se_nacl->acl_list);
62501d4d673SNicholas Bellinger }
62601d4d673SNicholas Bellinger mutex_unlock(&se_tpg->acl_node_mutex);
62701d4d673SNicholas Bellinger
62801d4d673SNicholas Bellinger if (se_nacl->dynamic_stop)
62901d4d673SNicholas Bellinger target_put_nacl(se_nacl);
63001d4d673SNicholas Bellinger
63121aaa23bSNicholas Bellinger target_put_nacl(se_nacl);
63221aaa23bSNicholas Bellinger }
633c0add7fdSNicholas Bellinger if (se_sess->sess_cmd_map) {
63410e9cbb6SMatthew Wilcox sbitmap_queue_free(&se_sess->sess_tag_pool);
635de64d3a6SPekka Enberg kvfree(se_sess->sess_cmd_map);
636c0add7fdSNicholas Bellinger }
6374edba7e4SMike Christie if (se_sess->cmd_cnt)
6384edba7e4SMike Christie target_free_cmd_counter(se_sess->cmd_cnt);
639c66ac9dbSNicholas Bellinger kmem_cache_free(se_sess_cache, se_sess);
640c66ac9dbSNicholas Bellinger }
641c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_free_session);
642c66ac9dbSNicholas Bellinger
target_release_res(struct se_device * dev,void * data)64380647a89SBart Van Assche static int target_release_res(struct se_device *dev, void *data)
64480647a89SBart Van Assche {
64580647a89SBart Van Assche struct se_session *sess = data;
64680647a89SBart Van Assche
64780647a89SBart Van Assche if (dev->reservation_holder == sess)
64880647a89SBart Van Assche target_release_reservation(dev);
64980647a89SBart Van Assche return 0;
65080647a89SBart Van Assche }
65180647a89SBart Van Assche
transport_deregister_session(struct se_session * se_sess)652c66ac9dbSNicholas Bellinger void transport_deregister_session(struct se_session *se_sess)
653c66ac9dbSNicholas Bellinger {
654c66ac9dbSNicholas Bellinger struct se_portal_group *se_tpg = se_sess->se_tpg;
655e63a8e19SRoland Dreier unsigned long flags;
656c66ac9dbSNicholas Bellinger
6576708bb27SAndy Grover if (!se_tpg) {
658c66ac9dbSNicholas Bellinger transport_free_session(se_sess);
659c66ac9dbSNicholas Bellinger return;
660c66ac9dbSNicholas Bellinger }
661c66ac9dbSNicholas Bellinger
662e63a8e19SRoland Dreier spin_lock_irqsave(&se_tpg->session_lock, flags);
663c66ac9dbSNicholas Bellinger list_del(&se_sess->sess_list);
664c66ac9dbSNicholas Bellinger se_sess->se_tpg = NULL;
665c66ac9dbSNicholas Bellinger se_sess->fabric_sess_ptr = NULL;
666e63a8e19SRoland Dreier spin_unlock_irqrestore(&se_tpg->session_lock, flags);
667c66ac9dbSNicholas Bellinger
66880647a89SBart Van Assche /*
66980647a89SBart Van Assche * Since the session is being removed, release SPC-2
67080647a89SBart Van Assche * reservations held by the session that is disappearing.
67180647a89SBart Van Assche */
67280647a89SBart Van Assche target_for_each_device(target_release_res, se_sess);
67380647a89SBart Van Assche
6746708bb27SAndy Grover pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
67530c7ca93SDavid Disseldorp se_tpg->se_tpg_tfo->fabric_name);
67601468346SNicholas Bellinger /*
677125d0119SHannes Reinecke * If last kref is dropping now for an explicit NodeACL, awake sleeping
678afb999ffSNicholas Bellinger * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
67921aaa23bSNicholas Bellinger * removal context from within transport_free_session() code.
68001d4d673SNicholas Bellinger *
68101d4d673SNicholas Bellinger * For dynamic ACL, target_put_nacl() uses target_complete_nacl()
68201d4d673SNicholas Bellinger * to release all remaining generate_node_acl=1 created ACL resources.
68301468346SNicholas Bellinger */
68401468346SNicholas Bellinger
685afb999ffSNicholas Bellinger transport_free_session(se_sess);
686c66ac9dbSNicholas Bellinger }
687c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_deregister_session);
688c66ac9dbSNicholas Bellinger
target_remove_session(struct se_session * se_sess)689fb7c70f2SMike Christie void target_remove_session(struct se_session *se_sess)
690fb7c70f2SMike Christie {
691fb7c70f2SMike Christie transport_deregister_session_configfs(se_sess);
692fb7c70f2SMike Christie transport_deregister_session(se_sess);
693fb7c70f2SMike Christie }
694fb7c70f2SMike Christie EXPORT_SYMBOL(target_remove_session);
695fb7c70f2SMike Christie
target_remove_from_state_list(struct se_cmd * cmd)696cf572a96SChristoph Hellwig static void target_remove_from_state_list(struct se_cmd *cmd)
697c66ac9dbSNicholas Bellinger {
69842bf829eSChristoph Hellwig struct se_device *dev = cmd->se_dev;
699c66ac9dbSNicholas Bellinger unsigned long flags;
700c66ac9dbSNicholas Bellinger
7016708bb27SAndy Grover if (!dev)
70242bf829eSChristoph Hellwig return;
703c66ac9dbSNicholas Bellinger
7041526d9f1SMike Christie spin_lock_irqsave(&dev->queues[cmd->cpuid].lock, flags);
705cf572a96SChristoph Hellwig if (cmd->state_active) {
706cf572a96SChristoph Hellwig list_del(&cmd->state_list);
707cf572a96SChristoph Hellwig cmd->state_active = false;
708c66ac9dbSNicholas Bellinger }
7091526d9f1SMike Christie spin_unlock_irqrestore(&dev->queues[cmd->cpuid].lock, flags);
7101880807aSChristoph Hellwig }
7111880807aSChristoph Hellwig
target_remove_from_tmr_list(struct se_cmd * cmd)71212b6fcd0SDmitry Bogdanov static void target_remove_from_tmr_list(struct se_cmd *cmd)
71312b6fcd0SDmitry Bogdanov {
71412b6fcd0SDmitry Bogdanov struct se_device *dev = NULL;
71512b6fcd0SDmitry Bogdanov unsigned long flags;
71612b6fcd0SDmitry Bogdanov
71712b6fcd0SDmitry Bogdanov if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
71812b6fcd0SDmitry Bogdanov dev = cmd->se_tmr_req->tmr_dev;
71912b6fcd0SDmitry Bogdanov
72012b6fcd0SDmitry Bogdanov if (dev) {
72112b6fcd0SDmitry Bogdanov spin_lock_irqsave(&dev->se_tmr_lock, flags);
72212b6fcd0SDmitry Bogdanov if (cmd->se_tmr_req->tmr_dev)
72312b6fcd0SDmitry Bogdanov list_del_init(&cmd->se_tmr_req->tmr_list);
72412b6fcd0SDmitry Bogdanov spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
72512b6fcd0SDmitry Bogdanov }
72612b6fcd0SDmitry Bogdanov }
727953bcf7aSBart Van Assche /*
728953bcf7aSBart Van Assche * This function is called by the target core after the target core has
729953bcf7aSBart Van Assche * finished processing a SCSI command or SCSI TMF. Both the regular command
730953bcf7aSBart Van Assche * processing code and the code for aborting commands can call this
731953bcf7aSBart Van Assche * function. CMD_T_STOP is set if and only if another thread is waiting
732953bcf7aSBart Van Assche * inside transport_wait_for_tasks() for t_transport_stop_comp.
733953bcf7aSBart Van Assche */
transport_cmd_check_stop_to_fabric(struct se_cmd * cmd)734b1a2ecdaSBart Van Assche static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
735c66ac9dbSNicholas Bellinger {
736c66ac9dbSNicholas Bellinger unsigned long flags;
737c66ac9dbSNicholas Bellinger
738febe562cSNicholas Bellinger spin_lock_irqsave(&cmd->t_state_lock, flags);
739c66ac9dbSNicholas Bellinger /*
740c66ac9dbSNicholas Bellinger * Determine if frontend context caller is requesting the stopping of
741e3d6f909SAndy Grover * this command for frontend exceptions.
742c66ac9dbSNicholas Bellinger */
7437d680f3bSChristoph Hellwig if (cmd->transport_state & CMD_T_STOP) {
744649ee054SBart Van Assche pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
745649ee054SBart Van Assche __func__, __LINE__, cmd->tag);
746c66ac9dbSNicholas Bellinger
747a1d8b49aSAndy Grover spin_unlock_irqrestore(&cmd->t_state_lock, flags);
748c66ac9dbSNicholas Bellinger
749a95d6511SNicholas Bellinger complete_all(&cmd->t_transport_stop_comp);
750c66ac9dbSNicholas Bellinger return 1;
751c66ac9dbSNicholas Bellinger }
7527d680f3bSChristoph Hellwig cmd->transport_state &= ~CMD_T_ACTIVE;
753b1a2ecdaSBart Van Assche spin_unlock_irqrestore(&cmd->t_state_lock, flags);
754b1a2ecdaSBart Van Assche
755c66ac9dbSNicholas Bellinger /*
756b1a2ecdaSBart Van Assche * Some fabric modules like tcm_loop can release their internally
757b1a2ecdaSBart Van Assche * allocated I/O reference and struct se_cmd now.
75888dd9e26SNicholas Bellinger *
759b1a2ecdaSBart Van Assche * Fabric modules are expected to return '1' here if the se_cmd being
760b1a2ecdaSBart Van Assche * passed is released at this point, or zero if not being released.
761c66ac9dbSNicholas Bellinger */
7629c28ca4fSNicholas Bellinger return cmd->se_tfo->check_stop_free(cmd);
763c66ac9dbSNicholas Bellinger }
764c66ac9dbSNicholas Bellinger
transport_lun_remove_cmd(struct se_cmd * cmd)765c14335ebSBart Van Assche static void transport_lun_remove_cmd(struct se_cmd *cmd)
766c14335ebSBart Van Assche {
767c14335ebSBart Van Assche struct se_lun *lun = cmd->se_lun;
768c14335ebSBart Van Assche
769c14335ebSBart Van Assche if (!lun)
770c14335ebSBart Van Assche return;
771c14335ebSBart Van Assche
77212b6fcd0SDmitry Bogdanov target_remove_from_state_list(cmd);
77312b6fcd0SDmitry Bogdanov target_remove_from_tmr_list(cmd);
77412b6fcd0SDmitry Bogdanov
775c14335ebSBart Van Assche if (cmpxchg(&cmd->lun_ref_active, true, false))
776c14335ebSBart Van Assche percpu_ref_put(&lun->lun_ref);
77712b6fcd0SDmitry Bogdanov
77812b6fcd0SDmitry Bogdanov /*
77912b6fcd0SDmitry Bogdanov * Clear struct se_cmd->se_lun before the handoff to FE.
78012b6fcd0SDmitry Bogdanov */
78112b6fcd0SDmitry Bogdanov cmd->se_lun = NULL;
782c14335ebSBart Van Assche }
783c14335ebSBart Van Assche
target_complete_failure_work(struct work_struct * work)78435e0e757SChristoph Hellwig static void target_complete_failure_work(struct work_struct *work)
78535e0e757SChristoph Hellwig {
78635e0e757SChristoph Hellwig struct se_cmd *cmd = container_of(work, struct se_cmd, work);
78735e0e757SChristoph Hellwig
78844678553SSergey Samoylenko transport_generic_request_failure(cmd, cmd->sense_reason);
78935e0e757SChristoph Hellwig }
79035e0e757SChristoph Hellwig
7916138ed2aSPaolo Bonzini /*
792d5829eacSPaolo Bonzini * Used when asking transport to copy Sense Data from the underlying
793d5829eacSPaolo Bonzini * Linux/SCSI struct scsi_cmnd
7946138ed2aSPaolo Bonzini */
transport_get_sense_buffer(struct se_cmd * cmd)795d5829eacSPaolo Bonzini static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd)
7966138ed2aSPaolo Bonzini {
7976138ed2aSPaolo Bonzini struct se_device *dev = cmd->se_dev;
7986138ed2aSPaolo Bonzini
7996138ed2aSPaolo Bonzini WARN_ON(!cmd->se_lun);
8006138ed2aSPaolo Bonzini
8016138ed2aSPaolo Bonzini if (!dev)
802d5829eacSPaolo Bonzini return NULL;
8036138ed2aSPaolo Bonzini
804d5829eacSPaolo Bonzini if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION)
805d5829eacSPaolo Bonzini return NULL;
8066138ed2aSPaolo Bonzini
8079c58b7ddSRoland Dreier cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
8086138ed2aSPaolo Bonzini
809d5829eacSPaolo Bonzini pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
8106138ed2aSPaolo Bonzini dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
8119c58b7ddSRoland Dreier return cmd->sense_buffer;
8126138ed2aSPaolo Bonzini }
8136138ed2aSPaolo Bonzini
transport_copy_sense_to_cmd(struct se_cmd * cmd,unsigned char * sense)814c6d66abaSMike Christie void transport_copy_sense_to_cmd(struct se_cmd *cmd, unsigned char *sense)
815c6d66abaSMike Christie {
816c6d66abaSMike Christie unsigned char *cmd_sense_buf;
817c6d66abaSMike Christie unsigned long flags;
818c6d66abaSMike Christie
819c6d66abaSMike Christie spin_lock_irqsave(&cmd->t_state_lock, flags);
820c6d66abaSMike Christie cmd_sense_buf = transport_get_sense_buffer(cmd);
821c6d66abaSMike Christie if (!cmd_sense_buf) {
822c6d66abaSMike Christie spin_unlock_irqrestore(&cmd->t_state_lock, flags);
823c6d66abaSMike Christie return;
824c6d66abaSMike Christie }
825c6d66abaSMike Christie
826c6d66abaSMike Christie cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
827c6d66abaSMike Christie memcpy(cmd_sense_buf, sense, cmd->scsi_sense_length);
828c6d66abaSMike Christie spin_unlock_irqrestore(&cmd->t_state_lock, flags);
829c6d66abaSMike Christie }
830c6d66abaSMike Christie EXPORT_SYMBOL(transport_copy_sense_to_cmd);
831c6d66abaSMike Christie
target_handle_abort(struct se_cmd * cmd)8322c9fa49eSBart Van Assche static void target_handle_abort(struct se_cmd *cmd)
8332c9fa49eSBart Van Assche {
8342c9fa49eSBart Van Assche bool tas = cmd->transport_state & CMD_T_TAS;
8352c9fa49eSBart Van Assche bool ack_kref = cmd->se_cmd_flags & SCF_ACK_KREF;
8362c9fa49eSBart Van Assche int ret;
8372c9fa49eSBart Van Assche
8382c9fa49eSBart Van Assche pr_debug("tag %#llx: send_abort_response = %d\n", cmd->tag, tas);
8392c9fa49eSBart Van Assche
8402c9fa49eSBart Van Assche if (tas) {
8412c9fa49eSBart Van Assche if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
8422c9fa49eSBart Van Assche cmd->scsi_status = SAM_STAT_TASK_ABORTED;
8432c9fa49eSBart Van Assche pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x, ITT: 0x%08llx\n",
8442c9fa49eSBart Van Assche cmd->t_task_cdb[0], cmd->tag);
8452c9fa49eSBart Van Assche trace_target_cmd_complete(cmd);
8462c9fa49eSBart Van Assche ret = cmd->se_tfo->queue_status(cmd);
8472c9fa49eSBart Van Assche if (ret) {
8482c9fa49eSBart Van Assche transport_handle_queue_full(cmd, cmd->se_dev,
8492c9fa49eSBart Van Assche ret, false);
8502c9fa49eSBart Van Assche return;
8512c9fa49eSBart Van Assche }
8522c9fa49eSBart Van Assche } else {
8532c9fa49eSBart Van Assche cmd->se_tmr_req->response = TMR_FUNCTION_REJECTED;
8542c9fa49eSBart Van Assche cmd->se_tfo->queue_tm_rsp(cmd);
8552c9fa49eSBart Van Assche }
8562c9fa49eSBart Van Assche } else {
8572c9fa49eSBart Van Assche /*
8582c9fa49eSBart Van Assche * Allow the fabric driver to unmap any resources before
8592c9fa49eSBart Van Assche * releasing the descriptor via TFO->release_cmd().
8602c9fa49eSBart Van Assche */
8612c9fa49eSBart Van Assche cmd->se_tfo->aborted_task(cmd);
8622c9fa49eSBart Van Assche if (ack_kref)
8632c9fa49eSBart Van Assche WARN_ON_ONCE(target_put_sess_cmd(cmd) != 0);
8642c9fa49eSBart Van Assche /*
8652c9fa49eSBart Van Assche * To do: establish a unit attention condition on the I_T
8662c9fa49eSBart Van Assche * nexus associated with cmd. See also the paragraph "Aborting
8672c9fa49eSBart Van Assche * commands" in SAM.
8682c9fa49eSBart Van Assche */
8692c9fa49eSBart Van Assche }
8702c9fa49eSBart Van Assche
8712c9fa49eSBart Van Assche WARN_ON_ONCE(kref_read(&cmd->cmd_kref) == 0);
8722c9fa49eSBart Van Assche
873c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
874c14335ebSBart Van Assche
8752c9fa49eSBart Van Assche transport_cmd_check_stop_to_fabric(cmd);
8762c9fa49eSBart Van Assche }
8772c9fa49eSBart Van Assche
target_abort_work(struct work_struct * work)8782c9fa49eSBart Van Assche static void target_abort_work(struct work_struct *work)
8792c9fa49eSBart Van Assche {
8802c9fa49eSBart Van Assche struct se_cmd *cmd = container_of(work, struct se_cmd, work);
8812c9fa49eSBart Van Assche
8822c9fa49eSBart Van Assche target_handle_abort(cmd);
8832c9fa49eSBart Van Assche }
8842c9fa49eSBart Van Assche
target_cmd_interrupted(struct se_cmd * cmd)8852c9fa49eSBart Van Assche static bool target_cmd_interrupted(struct se_cmd *cmd)
8862c9fa49eSBart Van Assche {
8872c9fa49eSBart Van Assche int post_ret;
8882c9fa49eSBart Van Assche
8892c9fa49eSBart Van Assche if (cmd->transport_state & CMD_T_ABORTED) {
8902c9fa49eSBart Van Assche if (cmd->transport_complete_callback)
8912c9fa49eSBart Van Assche cmd->transport_complete_callback(cmd, false, &post_ret);
8922c9fa49eSBart Van Assche INIT_WORK(&cmd->work, target_abort_work);
8932c9fa49eSBart Van Assche queue_work(target_completion_wq, &cmd->work);
8942c9fa49eSBart Van Assche return true;
8952c9fa49eSBart Van Assche } else if (cmd->transport_state & CMD_T_STOP) {
8962c9fa49eSBart Van Assche if (cmd->transport_complete_callback)
8972c9fa49eSBart Van Assche cmd->transport_complete_callback(cmd, false, &post_ret);
8982c9fa49eSBart Van Assche complete_all(&cmd->t_transport_stop_comp);
8992c9fa49eSBart Van Assche return true;
9002c9fa49eSBart Van Assche }
9012c9fa49eSBart Van Assche
9022c9fa49eSBart Van Assche return false;
9032c9fa49eSBart Van Assche }
9042c9fa49eSBart Van Assche
9052c9fa49eSBart Van Assche /* May be called from interrupt context so must not sleep. */
target_complete_cmd_with_sense(struct se_cmd * cmd,u8 scsi_status,sense_reason_t sense_reason)90644678553SSergey Samoylenko void target_complete_cmd_with_sense(struct se_cmd *cmd, u8 scsi_status,
90744678553SSergey Samoylenko sense_reason_t sense_reason)
908c66ac9dbSNicholas Bellinger {
90939ae3eddSMike Christie struct se_wwn *wwn = cmd->se_sess->se_tpg->se_tpg_wwn;
91039ae3eddSMike Christie int success, cpu;
911c66ac9dbSNicholas Bellinger unsigned long flags;
912c66ac9dbSNicholas Bellinger
9132c9fa49eSBart Van Assche if (target_cmd_interrupted(cmd))
9142c9fa49eSBart Van Assche return;
9152c9fa49eSBart Van Assche
9165787cacdSChristoph Hellwig cmd->scsi_status = scsi_status;
91744678553SSergey Samoylenko cmd->sense_reason = sense_reason;
9185787cacdSChristoph Hellwig
919a1d8b49aSAndy Grover spin_lock_irqsave(&cmd->t_state_lock, flags);
920402242c9SMike Christie switch (cmd->scsi_status) {
921402242c9SMike Christie case SAM_STAT_CHECK_CONDITION:
922d5829eacSPaolo Bonzini if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
923c66ac9dbSNicholas Bellinger success = 1;
924402242c9SMike Christie else
925402242c9SMike Christie success = 0;
926402242c9SMike Christie break;
927402242c9SMike Christie default:
928402242c9SMike Christie success = 1;
929402242c9SMike Christie break;
930c66ac9dbSNicholas Bellinger }
931c66ac9dbSNicholas Bellinger
93235e0e757SChristoph Hellwig cmd->t_state = TRANSPORT_COMPLETE;
9333d28934aSNicholas Bellinger cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
934a1d8b49aSAndy Grover spin_unlock_irqrestore(&cmd->t_state_lock, flags);
935c66ac9dbSNicholas Bellinger
9363ad98002SBart Van Assche INIT_WORK(&cmd->work, success ? target_complete_ok_work :
9373ad98002SBart Van Assche target_complete_failure_work);
93839ae3eddSMike Christie
939a47fa413SDavid Disseldorp if (!wwn || wwn->cmd_compl_affinity == SE_COMPL_AFFINITY_CPUID)
94039ae3eddSMike Christie cpu = cmd->cpuid;
94139ae3eddSMike Christie else
94239ae3eddSMike Christie cpu = wwn->cmd_compl_affinity;
94339ae3eddSMike Christie
94439ae3eddSMike Christie queue_work_on(cpu, target_completion_wq, &cmd->work);
945c66ac9dbSNicholas Bellinger }
94644678553SSergey Samoylenko EXPORT_SYMBOL(target_complete_cmd_with_sense);
94744678553SSergey Samoylenko
target_complete_cmd(struct se_cmd * cmd,u8 scsi_status)94844678553SSergey Samoylenko void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
94944678553SSergey Samoylenko {
95044678553SSergey Samoylenko target_complete_cmd_with_sense(cmd, scsi_status, scsi_status ?
95144678553SSergey Samoylenko TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE :
95244678553SSergey Samoylenko TCM_NO_SENSE);
95344678553SSergey Samoylenko }
9546bb35e00SChristoph Hellwig EXPORT_SYMBOL(target_complete_cmd);
9556bb35e00SChristoph Hellwig
target_set_cmd_data_length(struct se_cmd * cmd,int length)9561c73e0c5SAleksandr Miloserdov void target_set_cmd_data_length(struct se_cmd *cmd, int length)
9572426bd45SRoland Dreier {
9581c73e0c5SAleksandr Miloserdov if (length < cmd->data_length) {
9592426bd45SRoland Dreier if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
9602426bd45SRoland Dreier cmd->residual_count += cmd->data_length - length;
9612426bd45SRoland Dreier } else {
9622426bd45SRoland Dreier cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
9632426bd45SRoland Dreier cmd->residual_count = cmd->data_length - length;
9642426bd45SRoland Dreier }
9652426bd45SRoland Dreier
9662426bd45SRoland Dreier cmd->data_length = length;
9672426bd45SRoland Dreier }
9681c73e0c5SAleksandr Miloserdov }
9691c73e0c5SAleksandr Miloserdov EXPORT_SYMBOL(target_set_cmd_data_length);
9701c73e0c5SAleksandr Miloserdov
target_complete_cmd_with_length(struct se_cmd * cmd,u8 scsi_status,int length)9711c73e0c5SAleksandr Miloserdov void target_complete_cmd_with_length(struct se_cmd *cmd, u8 scsi_status, int length)
9721c73e0c5SAleksandr Miloserdov {
9731c73e0c5SAleksandr Miloserdov if (scsi_status == SAM_STAT_GOOD ||
9741c73e0c5SAleksandr Miloserdov cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) {
9751c73e0c5SAleksandr Miloserdov target_set_cmd_data_length(cmd, length);
9761c73e0c5SAleksandr Miloserdov }
9772426bd45SRoland Dreier
9782426bd45SRoland Dreier target_complete_cmd(cmd, scsi_status);
9792426bd45SRoland Dreier }
9802426bd45SRoland Dreier EXPORT_SYMBOL(target_complete_cmd_with_length);
9812426bd45SRoland Dreier
target_add_to_state_list(struct se_cmd * cmd)982cf572a96SChristoph Hellwig static void target_add_to_state_list(struct se_cmd *cmd)
983c66ac9dbSNicholas Bellinger {
98442bf829eSChristoph Hellwig struct se_device *dev = cmd->se_dev;
985c66ac9dbSNicholas Bellinger unsigned long flags;
986c66ac9dbSNicholas Bellinger
9871526d9f1SMike Christie spin_lock_irqsave(&dev->queues[cmd->cpuid].lock, flags);
988cf572a96SChristoph Hellwig if (!cmd->state_active) {
9891526d9f1SMike Christie list_add_tail(&cmd->state_list,
9901526d9f1SMike Christie &dev->queues[cmd->cpuid].state_list);
991cf572a96SChristoph Hellwig cmd->state_active = true;
992cf572a96SChristoph Hellwig }
9931526d9f1SMike Christie spin_unlock_irqrestore(&dev->queues[cmd->cpuid].lock, flags);
994c66ac9dbSNicholas Bellinger }
995c66ac9dbSNicholas Bellinger
99607bde79aSNicholas Bellinger /*
997f147abb4SNicholas Bellinger * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
99807bde79aSNicholas Bellinger */
9997a6f0a1eSChristoph Hellwig static void transport_write_pending_qf(struct se_cmd *cmd);
10007a6f0a1eSChristoph Hellwig static void transport_complete_qf(struct se_cmd *cmd);
100107bde79aSNicholas Bellinger
target_qf_do_work(struct work_struct * work)10020fd97ccfSChristoph Hellwig void target_qf_do_work(struct work_struct *work)
100307bde79aSNicholas Bellinger {
100407bde79aSNicholas Bellinger struct se_device *dev = container_of(work, struct se_device,
100507bde79aSNicholas Bellinger qf_work_queue);
1006bcac364aSRoland Dreier LIST_HEAD(qf_cmd_list);
100707bde79aSNicholas Bellinger struct se_cmd *cmd, *cmd_tmp;
100807bde79aSNicholas Bellinger
100907bde79aSNicholas Bellinger spin_lock_irq(&dev->qf_cmd_lock);
1010bcac364aSRoland Dreier list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
1011bcac364aSRoland Dreier spin_unlock_irq(&dev->qf_cmd_lock);
101207bde79aSNicholas Bellinger
1013bcac364aSRoland Dreier list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
101407bde79aSNicholas Bellinger list_del(&cmd->se_qf_node);
101533940d09SJoern Engel atomic_dec_mb(&dev->dev_qf_count);
101607bde79aSNicholas Bellinger
10176708bb27SAndy Grover pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
101830c7ca93SDavid Disseldorp " context: %s\n", cmd->se_tfo->fabric_name, cmd,
1019e057f533SChristoph Hellwig (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
102007bde79aSNicholas Bellinger (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
102107bde79aSNicholas Bellinger : "UNKNOWN");
1022f7a5cc0bSChristoph Hellwig
10237a6f0a1eSChristoph Hellwig if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
10247a6f0a1eSChristoph Hellwig transport_write_pending_qf(cmd);
1025fa7e25cfSNicholas Bellinger else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK ||
1026fa7e25cfSNicholas Bellinger cmd->t_state == TRANSPORT_COMPLETE_QF_ERR)
10277a6f0a1eSChristoph Hellwig transport_complete_qf(cmd);
102807bde79aSNicholas Bellinger }
102907bde79aSNicholas Bellinger }
103007bde79aSNicholas Bellinger
transport_dump_cmd_direction(struct se_cmd * cmd)1031c66ac9dbSNicholas Bellinger unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
1032c66ac9dbSNicholas Bellinger {
1033c66ac9dbSNicholas Bellinger switch (cmd->data_direction) {
1034c66ac9dbSNicholas Bellinger case DMA_NONE:
1035c66ac9dbSNicholas Bellinger return "NONE";
1036c66ac9dbSNicholas Bellinger case DMA_FROM_DEVICE:
1037c66ac9dbSNicholas Bellinger return "READ";
1038c66ac9dbSNicholas Bellinger case DMA_TO_DEVICE:
1039c66ac9dbSNicholas Bellinger return "WRITE";
1040c66ac9dbSNicholas Bellinger case DMA_BIDIRECTIONAL:
1041c66ac9dbSNicholas Bellinger return "BIDI";
1042c66ac9dbSNicholas Bellinger default:
1043c66ac9dbSNicholas Bellinger break;
1044c66ac9dbSNicholas Bellinger }
1045c66ac9dbSNicholas Bellinger
1046c66ac9dbSNicholas Bellinger return "UNKNOWN";
1047c66ac9dbSNicholas Bellinger }
1048c66ac9dbSNicholas Bellinger
transport_dump_dev_state(struct se_device * dev,char * b,int * bl)1049c66ac9dbSNicholas Bellinger void transport_dump_dev_state(
1050c66ac9dbSNicholas Bellinger struct se_device *dev,
1051c66ac9dbSNicholas Bellinger char *b,
1052c66ac9dbSNicholas Bellinger int *bl)
1053c66ac9dbSNicholas Bellinger {
1054c66ac9dbSNicholas Bellinger *bl += sprintf(b + *bl, "Status: ");
10550fd97ccfSChristoph Hellwig if (dev->export_count)
1056c66ac9dbSNicholas Bellinger *bl += sprintf(b + *bl, "ACTIVATED");
10570fd97ccfSChristoph Hellwig else
1058c66ac9dbSNicholas Bellinger *bl += sprintf(b + *bl, "DEACTIVATED");
1059c66ac9dbSNicholas Bellinger
10605f41a31dSChristoph Hellwig *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
106111e764bdSNicholas Bellinger *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
10620fd97ccfSChristoph Hellwig dev->dev_attrib.block_size,
10630fd97ccfSChristoph Hellwig dev->dev_attrib.hw_max_sectors);
1064c66ac9dbSNicholas Bellinger *bl += sprintf(b + *bl, " ");
1065c66ac9dbSNicholas Bellinger }
1066c66ac9dbSNicholas Bellinger
transport_dump_vpd_proto_id(struct t10_vpd * vpd,unsigned char * p_buf,int p_buf_len)1067c66ac9dbSNicholas Bellinger void transport_dump_vpd_proto_id(
1068c66ac9dbSNicholas Bellinger struct t10_vpd *vpd,
1069c66ac9dbSNicholas Bellinger unsigned char *p_buf,
1070c66ac9dbSNicholas Bellinger int p_buf_len)
1071c66ac9dbSNicholas Bellinger {
1072c66ac9dbSNicholas Bellinger unsigned char buf[VPD_TMP_BUF_SIZE];
1073c66ac9dbSNicholas Bellinger int len;
1074c66ac9dbSNicholas Bellinger
1075c66ac9dbSNicholas Bellinger memset(buf, 0, VPD_TMP_BUF_SIZE);
1076c66ac9dbSNicholas Bellinger len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1077c66ac9dbSNicholas Bellinger
1078c66ac9dbSNicholas Bellinger switch (vpd->protocol_identifier) {
1079c66ac9dbSNicholas Bellinger case 0x00:
1080c66ac9dbSNicholas Bellinger sprintf(buf+len, "Fibre Channel\n");
1081c66ac9dbSNicholas Bellinger break;
1082c66ac9dbSNicholas Bellinger case 0x10:
1083c66ac9dbSNicholas Bellinger sprintf(buf+len, "Parallel SCSI\n");
1084c66ac9dbSNicholas Bellinger break;
1085c66ac9dbSNicholas Bellinger case 0x20:
1086c66ac9dbSNicholas Bellinger sprintf(buf+len, "SSA\n");
1087c66ac9dbSNicholas Bellinger break;
1088c66ac9dbSNicholas Bellinger case 0x30:
1089c66ac9dbSNicholas Bellinger sprintf(buf+len, "IEEE 1394\n");
1090c66ac9dbSNicholas Bellinger break;
1091c66ac9dbSNicholas Bellinger case 0x40:
1092c66ac9dbSNicholas Bellinger sprintf(buf+len, "SCSI Remote Direct Memory Access"
1093c66ac9dbSNicholas Bellinger " Protocol\n");
1094c66ac9dbSNicholas Bellinger break;
1095c66ac9dbSNicholas Bellinger case 0x50:
1096c66ac9dbSNicholas Bellinger sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1097c66ac9dbSNicholas Bellinger break;
1098c66ac9dbSNicholas Bellinger case 0x60:
1099c66ac9dbSNicholas Bellinger sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1100c66ac9dbSNicholas Bellinger break;
1101c66ac9dbSNicholas Bellinger case 0x70:
1102c66ac9dbSNicholas Bellinger sprintf(buf+len, "Automation/Drive Interface Transport"
1103c66ac9dbSNicholas Bellinger " Protocol\n");
1104c66ac9dbSNicholas Bellinger break;
1105c66ac9dbSNicholas Bellinger case 0x80:
1106c66ac9dbSNicholas Bellinger sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1107c66ac9dbSNicholas Bellinger break;
1108c66ac9dbSNicholas Bellinger default:
1109c66ac9dbSNicholas Bellinger sprintf(buf+len, "Unknown 0x%02x\n",
1110c66ac9dbSNicholas Bellinger vpd->protocol_identifier);
1111c66ac9dbSNicholas Bellinger break;
1112c66ac9dbSNicholas Bellinger }
1113c66ac9dbSNicholas Bellinger
1114c66ac9dbSNicholas Bellinger if (p_buf)
1115c66ac9dbSNicholas Bellinger strncpy(p_buf, buf, p_buf_len);
1116c66ac9dbSNicholas Bellinger else
11176708bb27SAndy Grover pr_debug("%s", buf);
1118c66ac9dbSNicholas Bellinger }
1119c66ac9dbSNicholas Bellinger
1120c66ac9dbSNicholas Bellinger void
transport_set_vpd_proto_id(struct t10_vpd * vpd,unsigned char * page_83)1121c66ac9dbSNicholas Bellinger transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1122c66ac9dbSNicholas Bellinger {
1123c66ac9dbSNicholas Bellinger /*
1124c66ac9dbSNicholas Bellinger * Check if the Protocol Identifier Valid (PIV) bit is set..
1125c66ac9dbSNicholas Bellinger *
1126c66ac9dbSNicholas Bellinger * from spc3r23.pdf section 7.5.1
1127c66ac9dbSNicholas Bellinger */
1128c66ac9dbSNicholas Bellinger if (page_83[1] & 0x80) {
1129c66ac9dbSNicholas Bellinger vpd->protocol_identifier = (page_83[0] & 0xf0);
1130c66ac9dbSNicholas Bellinger vpd->protocol_identifier_set = 1;
1131c66ac9dbSNicholas Bellinger transport_dump_vpd_proto_id(vpd, NULL, 0);
1132c66ac9dbSNicholas Bellinger }
1133c66ac9dbSNicholas Bellinger }
1134c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_set_vpd_proto_id);
1135c66ac9dbSNicholas Bellinger
transport_dump_vpd_assoc(struct t10_vpd * vpd,unsigned char * p_buf,int p_buf_len)1136c66ac9dbSNicholas Bellinger int transport_dump_vpd_assoc(
1137c66ac9dbSNicholas Bellinger struct t10_vpd *vpd,
1138c66ac9dbSNicholas Bellinger unsigned char *p_buf,
1139c66ac9dbSNicholas Bellinger int p_buf_len)
1140c66ac9dbSNicholas Bellinger {
1141c66ac9dbSNicholas Bellinger unsigned char buf[VPD_TMP_BUF_SIZE];
1142e3d6f909SAndy Grover int ret = 0;
1143e3d6f909SAndy Grover int len;
1144c66ac9dbSNicholas Bellinger
1145c66ac9dbSNicholas Bellinger memset(buf, 0, VPD_TMP_BUF_SIZE);
1146c66ac9dbSNicholas Bellinger len = sprintf(buf, "T10 VPD Identifier Association: ");
1147c66ac9dbSNicholas Bellinger
1148c66ac9dbSNicholas Bellinger switch (vpd->association) {
1149c66ac9dbSNicholas Bellinger case 0x00:
1150c66ac9dbSNicholas Bellinger sprintf(buf+len, "addressed logical unit\n");
1151c66ac9dbSNicholas Bellinger break;
1152c66ac9dbSNicholas Bellinger case 0x10:
1153c66ac9dbSNicholas Bellinger sprintf(buf+len, "target port\n");
1154c66ac9dbSNicholas Bellinger break;
1155c66ac9dbSNicholas Bellinger case 0x20:
1156c66ac9dbSNicholas Bellinger sprintf(buf+len, "SCSI target device\n");
1157c66ac9dbSNicholas Bellinger break;
1158c66ac9dbSNicholas Bellinger default:
1159c66ac9dbSNicholas Bellinger sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1160e3d6f909SAndy Grover ret = -EINVAL;
1161c66ac9dbSNicholas Bellinger break;
1162c66ac9dbSNicholas Bellinger }
1163c66ac9dbSNicholas Bellinger
1164c66ac9dbSNicholas Bellinger if (p_buf)
1165c66ac9dbSNicholas Bellinger strncpy(p_buf, buf, p_buf_len);
1166c66ac9dbSNicholas Bellinger else
11676708bb27SAndy Grover pr_debug("%s", buf);
1168c66ac9dbSNicholas Bellinger
1169c66ac9dbSNicholas Bellinger return ret;
1170c66ac9dbSNicholas Bellinger }
1171c66ac9dbSNicholas Bellinger
transport_set_vpd_assoc(struct t10_vpd * vpd,unsigned char * page_83)1172c66ac9dbSNicholas Bellinger int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1173c66ac9dbSNicholas Bellinger {
1174c66ac9dbSNicholas Bellinger /*
1175c66ac9dbSNicholas Bellinger * The VPD identification association..
1176c66ac9dbSNicholas Bellinger *
1177c66ac9dbSNicholas Bellinger * from spc3r23.pdf Section 7.6.3.1 Table 297
1178c66ac9dbSNicholas Bellinger */
1179c66ac9dbSNicholas Bellinger vpd->association = (page_83[1] & 0x30);
1180c66ac9dbSNicholas Bellinger return transport_dump_vpd_assoc(vpd, NULL, 0);
1181c66ac9dbSNicholas Bellinger }
1182c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_set_vpd_assoc);
1183c66ac9dbSNicholas Bellinger
transport_dump_vpd_ident_type(struct t10_vpd * vpd,unsigned char * p_buf,int p_buf_len)1184c66ac9dbSNicholas Bellinger int transport_dump_vpd_ident_type(
1185c66ac9dbSNicholas Bellinger struct t10_vpd *vpd,
1186c66ac9dbSNicholas Bellinger unsigned char *p_buf,
1187c66ac9dbSNicholas Bellinger int p_buf_len)
1188c66ac9dbSNicholas Bellinger {
1189c66ac9dbSNicholas Bellinger unsigned char buf[VPD_TMP_BUF_SIZE];
1190e3d6f909SAndy Grover int ret = 0;
1191e3d6f909SAndy Grover int len;
1192c66ac9dbSNicholas Bellinger
1193c66ac9dbSNicholas Bellinger memset(buf, 0, VPD_TMP_BUF_SIZE);
1194c66ac9dbSNicholas Bellinger len = sprintf(buf, "T10 VPD Identifier Type: ");
1195c66ac9dbSNicholas Bellinger
1196c66ac9dbSNicholas Bellinger switch (vpd->device_identifier_type) {
1197c66ac9dbSNicholas Bellinger case 0x00:
1198c66ac9dbSNicholas Bellinger sprintf(buf+len, "Vendor specific\n");
1199c66ac9dbSNicholas Bellinger break;
1200c66ac9dbSNicholas Bellinger case 0x01:
1201c66ac9dbSNicholas Bellinger sprintf(buf+len, "T10 Vendor ID based\n");
1202c66ac9dbSNicholas Bellinger break;
1203c66ac9dbSNicholas Bellinger case 0x02:
1204c66ac9dbSNicholas Bellinger sprintf(buf+len, "EUI-64 based\n");
1205c66ac9dbSNicholas Bellinger break;
1206c66ac9dbSNicholas Bellinger case 0x03:
1207c66ac9dbSNicholas Bellinger sprintf(buf+len, "NAA\n");
1208c66ac9dbSNicholas Bellinger break;
1209c66ac9dbSNicholas Bellinger case 0x04:
1210c66ac9dbSNicholas Bellinger sprintf(buf+len, "Relative target port identifier\n");
1211c66ac9dbSNicholas Bellinger break;
1212c66ac9dbSNicholas Bellinger case 0x08:
1213c66ac9dbSNicholas Bellinger sprintf(buf+len, "SCSI name string\n");
1214c66ac9dbSNicholas Bellinger break;
1215c66ac9dbSNicholas Bellinger default:
1216c66ac9dbSNicholas Bellinger sprintf(buf+len, "Unsupported: 0x%02x\n",
1217c66ac9dbSNicholas Bellinger vpd->device_identifier_type);
1218e3d6f909SAndy Grover ret = -EINVAL;
1219c66ac9dbSNicholas Bellinger break;
1220c66ac9dbSNicholas Bellinger }
1221c66ac9dbSNicholas Bellinger
1222e3d6f909SAndy Grover if (p_buf) {
1223e3d6f909SAndy Grover if (p_buf_len < strlen(buf)+1)
1224e3d6f909SAndy Grover return -EINVAL;
1225c66ac9dbSNicholas Bellinger strncpy(p_buf, buf, p_buf_len);
1226e3d6f909SAndy Grover } else {
12276708bb27SAndy Grover pr_debug("%s", buf);
1228e3d6f909SAndy Grover }
1229c66ac9dbSNicholas Bellinger
1230c66ac9dbSNicholas Bellinger return ret;
1231c66ac9dbSNicholas Bellinger }
1232c66ac9dbSNicholas Bellinger
transport_set_vpd_ident_type(struct t10_vpd * vpd,unsigned char * page_83)1233c66ac9dbSNicholas Bellinger int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1234c66ac9dbSNicholas Bellinger {
1235c66ac9dbSNicholas Bellinger /*
1236c66ac9dbSNicholas Bellinger * The VPD identifier type..
1237c66ac9dbSNicholas Bellinger *
1238c66ac9dbSNicholas Bellinger * from spc3r23.pdf Section 7.6.3.1 Table 298
1239c66ac9dbSNicholas Bellinger */
1240c66ac9dbSNicholas Bellinger vpd->device_identifier_type = (page_83[1] & 0x0f);
1241c66ac9dbSNicholas Bellinger return transport_dump_vpd_ident_type(vpd, NULL, 0);
1242c66ac9dbSNicholas Bellinger }
1243c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_set_vpd_ident_type);
1244c66ac9dbSNicholas Bellinger
transport_dump_vpd_ident(struct t10_vpd * vpd,unsigned char * p_buf,int p_buf_len)1245c66ac9dbSNicholas Bellinger int transport_dump_vpd_ident(
1246c66ac9dbSNicholas Bellinger struct t10_vpd *vpd,
1247c66ac9dbSNicholas Bellinger unsigned char *p_buf,
1248c66ac9dbSNicholas Bellinger int p_buf_len)
1249c66ac9dbSNicholas Bellinger {
1250c66ac9dbSNicholas Bellinger unsigned char buf[VPD_TMP_BUF_SIZE];
1251c66ac9dbSNicholas Bellinger int ret = 0;
1252c66ac9dbSNicholas Bellinger
1253c66ac9dbSNicholas Bellinger memset(buf, 0, VPD_TMP_BUF_SIZE);
1254c66ac9dbSNicholas Bellinger
1255c66ac9dbSNicholas Bellinger switch (vpd->device_identifier_code_set) {
1256c66ac9dbSNicholas Bellinger case 0x01: /* Binary */
1257703d641dSDan Carpenter snprintf(buf, sizeof(buf),
1258703d641dSDan Carpenter "T10 VPD Binary Device Identifier: %s\n",
1259c66ac9dbSNicholas Bellinger &vpd->device_identifier[0]);
1260c66ac9dbSNicholas Bellinger break;
1261c66ac9dbSNicholas Bellinger case 0x02: /* ASCII */
1262703d641dSDan Carpenter snprintf(buf, sizeof(buf),
1263703d641dSDan Carpenter "T10 VPD ASCII Device Identifier: %s\n",
1264c66ac9dbSNicholas Bellinger &vpd->device_identifier[0]);
1265c66ac9dbSNicholas Bellinger break;
1266c66ac9dbSNicholas Bellinger case 0x03: /* UTF-8 */
1267703d641dSDan Carpenter snprintf(buf, sizeof(buf),
1268703d641dSDan Carpenter "T10 VPD UTF-8 Device Identifier: %s\n",
1269c66ac9dbSNicholas Bellinger &vpd->device_identifier[0]);
1270c66ac9dbSNicholas Bellinger break;
1271c66ac9dbSNicholas Bellinger default:
1272c66ac9dbSNicholas Bellinger sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1273c66ac9dbSNicholas Bellinger " 0x%02x", vpd->device_identifier_code_set);
1274e3d6f909SAndy Grover ret = -EINVAL;
1275c66ac9dbSNicholas Bellinger break;
1276c66ac9dbSNicholas Bellinger }
1277c66ac9dbSNicholas Bellinger
1278c66ac9dbSNicholas Bellinger if (p_buf)
1279c66ac9dbSNicholas Bellinger strncpy(p_buf, buf, p_buf_len);
1280c66ac9dbSNicholas Bellinger else
12816708bb27SAndy Grover pr_debug("%s", buf);
1282c66ac9dbSNicholas Bellinger
1283c66ac9dbSNicholas Bellinger return ret;
1284c66ac9dbSNicholas Bellinger }
1285c66ac9dbSNicholas Bellinger
1286c66ac9dbSNicholas Bellinger int
transport_set_vpd_ident(struct t10_vpd * vpd,unsigned char * page_83)1287c66ac9dbSNicholas Bellinger transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1288c66ac9dbSNicholas Bellinger {
1289c66ac9dbSNicholas Bellinger static const char hex_str[] = "0123456789abcdef";
129035d1efe8SMasanari Iida int j = 0, i = 4; /* offset to start of the identifier */
1291c66ac9dbSNicholas Bellinger
1292c66ac9dbSNicholas Bellinger /*
1293c66ac9dbSNicholas Bellinger * The VPD Code Set (encoding)
1294c66ac9dbSNicholas Bellinger *
1295c66ac9dbSNicholas Bellinger * from spc3r23.pdf Section 7.6.3.1 Table 296
1296c66ac9dbSNicholas Bellinger */
1297c66ac9dbSNicholas Bellinger vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1298c66ac9dbSNicholas Bellinger switch (vpd->device_identifier_code_set) {
1299c66ac9dbSNicholas Bellinger case 0x01: /* Binary */
1300c66ac9dbSNicholas Bellinger vpd->device_identifier[j++] =
1301c66ac9dbSNicholas Bellinger hex_str[vpd->device_identifier_type];
1302c66ac9dbSNicholas Bellinger while (i < (4 + page_83[3])) {
1303c66ac9dbSNicholas Bellinger vpd->device_identifier[j++] =
1304c66ac9dbSNicholas Bellinger hex_str[(page_83[i] & 0xf0) >> 4];
1305c66ac9dbSNicholas Bellinger vpd->device_identifier[j++] =
1306c66ac9dbSNicholas Bellinger hex_str[page_83[i] & 0x0f];
1307c66ac9dbSNicholas Bellinger i++;
1308c66ac9dbSNicholas Bellinger }
1309c66ac9dbSNicholas Bellinger break;
1310c66ac9dbSNicholas Bellinger case 0x02: /* ASCII */
1311c66ac9dbSNicholas Bellinger case 0x03: /* UTF-8 */
1312c66ac9dbSNicholas Bellinger while (i < (4 + page_83[3]))
1313c66ac9dbSNicholas Bellinger vpd->device_identifier[j++] = page_83[i++];
1314c66ac9dbSNicholas Bellinger break;
1315c66ac9dbSNicholas Bellinger default:
1316c66ac9dbSNicholas Bellinger break;
1317c66ac9dbSNicholas Bellinger }
1318c66ac9dbSNicholas Bellinger
1319c66ac9dbSNicholas Bellinger return transport_dump_vpd_ident(vpd, NULL, 0);
1320c66ac9dbSNicholas Bellinger }
1321c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_set_vpd_ident);
1322c66ac9dbSNicholas Bellinger
13238f9b5654SNicholas Bellinger static sense_reason_t
target_check_max_data_sg_nents(struct se_cmd * cmd,struct se_device * dev,unsigned int size)13248f9b5654SNicholas Bellinger target_check_max_data_sg_nents(struct se_cmd *cmd, struct se_device *dev,
13258f9b5654SNicholas Bellinger unsigned int size)
13268f9b5654SNicholas Bellinger {
13278f9b5654SNicholas Bellinger u32 mtl;
13288f9b5654SNicholas Bellinger
13298f9b5654SNicholas Bellinger if (!cmd->se_tfo->max_data_sg_nents)
13308f9b5654SNicholas Bellinger return TCM_NO_SENSE;
13318f9b5654SNicholas Bellinger /*
13328f9b5654SNicholas Bellinger * Check if fabric enforced maximum SGL entries per I/O descriptor
13338f9b5654SNicholas Bellinger * exceeds se_cmd->data_length. If true, set SCF_UNDERFLOW_BIT +
13348f9b5654SNicholas Bellinger * residual_count and reduce original cmd->data_length to maximum
13358f9b5654SNicholas Bellinger * length based on single PAGE_SIZE entry scatter-lists.
13368f9b5654SNicholas Bellinger */
13378f9b5654SNicholas Bellinger mtl = (cmd->se_tfo->max_data_sg_nents * PAGE_SIZE);
13388f9b5654SNicholas Bellinger if (cmd->data_length > mtl) {
13398f9b5654SNicholas Bellinger /*
13408f9b5654SNicholas Bellinger * If an existing CDB overflow is present, calculate new residual
13418f9b5654SNicholas Bellinger * based on CDB size minus fabric maximum transfer length.
13428f9b5654SNicholas Bellinger *
13438f9b5654SNicholas Bellinger * If an existing CDB underflow is present, calculate new residual
13448f9b5654SNicholas Bellinger * based on original cmd->data_length minus fabric maximum transfer
13458f9b5654SNicholas Bellinger * length.
13468f9b5654SNicholas Bellinger *
13478f9b5654SNicholas Bellinger * Otherwise, set the underflow residual based on cmd->data_length
13488f9b5654SNicholas Bellinger * minus fabric maximum transfer length.
13498f9b5654SNicholas Bellinger */
13508f9b5654SNicholas Bellinger if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
13518f9b5654SNicholas Bellinger cmd->residual_count = (size - mtl);
13528f9b5654SNicholas Bellinger } else if (cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
13538f9b5654SNicholas Bellinger u32 orig_dl = size + cmd->residual_count;
13548f9b5654SNicholas Bellinger cmd->residual_count = (orig_dl - mtl);
13558f9b5654SNicholas Bellinger } else {
13568f9b5654SNicholas Bellinger cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
13578f9b5654SNicholas Bellinger cmd->residual_count = (cmd->data_length - mtl);
13588f9b5654SNicholas Bellinger }
13598f9b5654SNicholas Bellinger cmd->data_length = mtl;
13608f9b5654SNicholas Bellinger /*
13618f9b5654SNicholas Bellinger * Reset sbc_check_prot() calculated protection payload
13628f9b5654SNicholas Bellinger * length based upon the new smaller MTL.
13638f9b5654SNicholas Bellinger */
13648f9b5654SNicholas Bellinger if (cmd->prot_length) {
13658f9b5654SNicholas Bellinger u32 sectors = (mtl / dev->dev_attrib.block_size);
13668f9b5654SNicholas Bellinger cmd->prot_length = dev->prot_length * sectors;
13678f9b5654SNicholas Bellinger }
13688f9b5654SNicholas Bellinger }
13698f9b5654SNicholas Bellinger return TCM_NO_SENSE;
13708f9b5654SNicholas Bellinger }
13718f9b5654SNicholas Bellinger
137211bf1d14SBart Van Assche /**
137311bf1d14SBart Van Assche * target_cmd_size_check - Check whether there will be a residual.
137411bf1d14SBart Van Assche * @cmd: SCSI command.
137511bf1d14SBart Van Assche * @size: Data buffer size derived from CDB. The data buffer size provided by
137611bf1d14SBart Van Assche * the SCSI transport driver is available in @cmd->data_length.
137711bf1d14SBart Van Assche *
137811bf1d14SBart Van Assche * Compare the data buffer size from the CDB with the data buffer limit from the transport
137911bf1d14SBart Van Assche * header. Set @cmd->residual_count and SCF_OVERFLOW_BIT or SCF_UNDERFLOW_BIT if necessary.
138011bf1d14SBart Van Assche *
1381a78b7136SMike Christie * Note: target drivers set @cmd->data_length by calling __target_init_cmd().
138211bf1d14SBart Van Assche *
138311bf1d14SBart Van Assche * Return: TCM_NO_SENSE
138411bf1d14SBart Van Assche */
1385de103c93SChristoph Hellwig sense_reason_t
target_cmd_size_check(struct se_cmd * cmd,unsigned int size)1386de103c93SChristoph Hellwig target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
13879b3b8041SChristoph Hellwig {
13889b3b8041SChristoph Hellwig struct se_device *dev = cmd->se_dev;
13899b3b8041SChristoph Hellwig
13909b3b8041SChristoph Hellwig if (cmd->unknown_data_length) {
13919b3b8041SChristoph Hellwig cmd->data_length = size;
13929b3b8041SChristoph Hellwig } else if (size != cmd->data_length) {
13934ff83daaSNicholas Bellinger pr_warn_ratelimited("TARGET_CORE[%s]: Expected Transfer Length:"
13949b3b8041SChristoph Hellwig " %u does not match SCSI CDB Length: %u for SAM Opcode:"
139530c7ca93SDavid Disseldorp " 0x%02x\n", cmd->se_tfo->fabric_name,
13969b3b8041SChristoph Hellwig cmd->data_length, size, cmd->t_task_cdb[0]);
1397cc0b6ad7SAnastasia Kovaleva /*
1398cc0b6ad7SAnastasia Kovaleva * For READ command for the overflow case keep the existing
1399cc0b6ad7SAnastasia Kovaleva * fabric provided ->data_length. Otherwise for the underflow
1400cc0b6ad7SAnastasia Kovaleva * case, reset ->data_length to the smaller SCSI expected data
1401cc0b6ad7SAnastasia Kovaleva * transfer length.
1402cc0b6ad7SAnastasia Kovaleva */
1403cc0b6ad7SAnastasia Kovaleva if (size > cmd->data_length) {
1404cc0b6ad7SAnastasia Kovaleva cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1405cc0b6ad7SAnastasia Kovaleva cmd->residual_count = (size - cmd->data_length);
1406cc0b6ad7SAnastasia Kovaleva } else {
1407cc0b6ad7SAnastasia Kovaleva cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1408cc0b6ad7SAnastasia Kovaleva cmd->residual_count = (cmd->data_length - size);
1409cc0b6ad7SAnastasia Kovaleva /*
1410cc0b6ad7SAnastasia Kovaleva * Do not truncate ->data_length for WRITE command to
1411cc0b6ad7SAnastasia Kovaleva * dump all payload
1412cc0b6ad7SAnastasia Kovaleva */
1413cc0b6ad7SAnastasia Kovaleva if (cmd->data_direction == DMA_FROM_DEVICE) {
1414cc0b6ad7SAnastasia Kovaleva cmd->data_length = size;
1415cc0b6ad7SAnastasia Kovaleva }
1416cc0b6ad7SAnastasia Kovaleva }
14179b3b8041SChristoph Hellwig
14184ff83daaSNicholas Bellinger if (cmd->data_direction == DMA_TO_DEVICE) {
14194ff83daaSNicholas Bellinger if (cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) {
14204ff83daaSNicholas Bellinger pr_err_ratelimited("Rejecting underflow/overflow"
14214ff83daaSNicholas Bellinger " for WRITE data CDB\n");
1422ead0ffc9SAnastasia Kovaleva return TCM_INVALID_FIELD_IN_COMMAND_IU;
14239b3b8041SChristoph Hellwig }
14249b3b8041SChristoph Hellwig /*
14254ff83daaSNicholas Bellinger * Some fabric drivers like iscsi-target still expect to
14264ff83daaSNicholas Bellinger * always reject overflow writes. Reject this case until
14274ff83daaSNicholas Bellinger * full fabric driver level support for overflow writes
14284ff83daaSNicholas Bellinger * is introduced tree-wide.
14294ff83daaSNicholas Bellinger */
14304ff83daaSNicholas Bellinger if (size > cmd->data_length) {
14314ff83daaSNicholas Bellinger pr_err_ratelimited("Rejecting overflow for"
14324ff83daaSNicholas Bellinger " WRITE control CDB\n");
14334ff83daaSNicholas Bellinger return TCM_INVALID_CDB_FIELD;
14344ff83daaSNicholas Bellinger }
14354ff83daaSNicholas Bellinger }
14364c054ba6SNicholas Bellinger }
14379b3b8041SChristoph Hellwig
14388f9b5654SNicholas Bellinger return target_check_max_data_sg_nents(cmd, dev, size);
14399b3b8041SChristoph Hellwig
14409b3b8041SChristoph Hellwig }
14419b3b8041SChristoph Hellwig
1442c66ac9dbSNicholas Bellinger /*
1443c66ac9dbSNicholas Bellinger * Used by fabric modules containing a local struct se_cmd within their
1444c66ac9dbSNicholas Bellinger * fabric dependent per I/O descriptor.
1445649ee054SBart Van Assche *
1446649ee054SBart Van Assche * Preserves the value of @cmd->tag.
1447c66ac9dbSNicholas Bellinger */
__target_init_cmd(struct se_cmd * cmd,const struct target_core_fabric_ops * tfo,struct se_session * se_sess,u32 data_length,int data_direction,int task_attr,unsigned char * sense_buffer,u64 unpacked_lun,struct target_cmd_counter * cmd_cnt)14488e288be8SMike Christie void __target_init_cmd(struct se_cmd *cmd,
14499ac8928eSChristoph Hellwig const struct target_core_fabric_ops *tfo,
14508e288be8SMike Christie struct se_session *se_sess, u32 data_length,
14518e288be8SMike Christie int data_direction, int task_attr,
14528e288be8SMike Christie unsigned char *sense_buffer, u64 unpacked_lun,
14538e288be8SMike Christie struct target_cmd_counter *cmd_cnt)
1454c66ac9dbSNicholas Bellinger {
14555951146dSAndy Grover INIT_LIST_HEAD(&cmd->se_delayed_node);
145607bde79aSNicholas Bellinger INIT_LIST_HEAD(&cmd->se_qf_node);
1457cf572a96SChristoph Hellwig INIT_LIST_HEAD(&cmd->state_list);
1458a1d8b49aSAndy Grover init_completion(&cmd->t_transport_stop_comp);
1459a014c364SBart Van Assche cmd->free_compl = NULL;
1460a014c364SBart Van Assche cmd->abrt_compl = NULL;
1461a1d8b49aSAndy Grover spin_lock_init(&cmd->t_state_lock);
1462f2b72d6aSBart Van Assche INIT_WORK(&cmd->work, NULL);
14631e1110c4SMikulas Patocka kref_init(&cmd->cmd_kref);
1464c66ac9dbSNicholas Bellinger
1465a9294d86SMike Christie cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1466c66ac9dbSNicholas Bellinger cmd->se_tfo = tfo;
1467c66ac9dbSNicholas Bellinger cmd->se_sess = se_sess;
1468c66ac9dbSNicholas Bellinger cmd->data_length = data_length;
1469c66ac9dbSNicholas Bellinger cmd->data_direction = data_direction;
1470c66ac9dbSNicholas Bellinger cmd->sam_task_attr = task_attr;
1471c66ac9dbSNicholas Bellinger cmd->sense_buffer = sense_buffer;
1472a36840d8SSudhakar Panneerselvam cmd->orig_fe_lun = unpacked_lun;
14738e288be8SMike Christie cmd->cmd_cnt = cmd_cnt;
1474cf572a96SChristoph Hellwig
14751526d9f1SMike Christie if (!(cmd->se_cmd_flags & SCF_USE_CPUID))
147670ca3c57SShin'ichiro Kawasaki cmd->cpuid = raw_smp_processor_id();
14771526d9f1SMike Christie
1478cf572a96SChristoph Hellwig cmd->state_active = false;
1479c66ac9dbSNicholas Bellinger }
1480a78b7136SMike Christie EXPORT_SYMBOL(__target_init_cmd);
1481c66ac9dbSNicholas Bellinger
1482de103c93SChristoph Hellwig static sense_reason_t
transport_check_alloc_task_attr(struct se_cmd * cmd)1483de103c93SChristoph Hellwig transport_check_alloc_task_attr(struct se_cmd *cmd)
1484c66ac9dbSNicholas Bellinger {
1485019c4ca6SChristoph Hellwig struct se_device *dev = cmd->se_dev;
1486019c4ca6SChristoph Hellwig
1487c66ac9dbSNicholas Bellinger /*
1488c66ac9dbSNicholas Bellinger * Check if SAM Task Attribute emulation is enabled for this
1489c66ac9dbSNicholas Bellinger * struct se_device storage object
1490c66ac9dbSNicholas Bellinger */
149169088a04SBodo Stroesser if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
1492c66ac9dbSNicholas Bellinger return 0;
1493c66ac9dbSNicholas Bellinger
149468d81f40SChristoph Hellwig if (cmd->sam_task_attr == TCM_ACA_TAG) {
14956708bb27SAndy Grover pr_debug("SAM Task Attribute ACA"
1496c66ac9dbSNicholas Bellinger " emulation is not supported\n");
1497de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD;
1498c66ac9dbSNicholas Bellinger }
14999c31820bSRoland Dreier
1500c66ac9dbSNicholas Bellinger return 0;
1501c66ac9dbSNicholas Bellinger }
1502c66ac9dbSNicholas Bellinger
1503de103c93SChristoph Hellwig sense_reason_t
target_cmd_init_cdb(struct se_cmd * cmd,unsigned char * cdb,gfp_t gfp)150408694199SMike Christie target_cmd_init_cdb(struct se_cmd *cmd, unsigned char *cdb, gfp_t gfp)
1505c66ac9dbSNicholas Bellinger {
1506de103c93SChristoph Hellwig sense_reason_t ret;
1507c66ac9dbSNicholas Bellinger
1508c66ac9dbSNicholas Bellinger /*
1509c66ac9dbSNicholas Bellinger * Ensure that the received CDB is less than the max (252 + 8) bytes
1510c66ac9dbSNicholas Bellinger * for VARIABLE_LENGTH_CMD
1511c66ac9dbSNicholas Bellinger */
1512c66ac9dbSNicholas Bellinger if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
15136708bb27SAndy Grover pr_err("Received SCSI CDB with command_size: %d that"
1514c66ac9dbSNicholas Bellinger " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1515c66ac9dbSNicholas Bellinger scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
15169e95fb80SSudhakar Panneerselvam ret = TCM_INVALID_CDB_FIELD;
15179e95fb80SSudhakar Panneerselvam goto err;
1518c66ac9dbSNicholas Bellinger }
1519c66ac9dbSNicholas Bellinger /*
1520c66ac9dbSNicholas Bellinger * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1521c66ac9dbSNicholas Bellinger * allocate the additional extended CDB buffer now.. Otherwise
1522c66ac9dbSNicholas Bellinger * setup the pointer from __t_task_cdb to t_task_cdb.
1523c66ac9dbSNicholas Bellinger */
1524a1d8b49aSAndy Grover if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
152508694199SMike Christie cmd->t_task_cdb = kzalloc(scsi_command_size(cdb), gfp);
15266708bb27SAndy Grover if (!cmd->t_task_cdb) {
15276708bb27SAndy Grover pr_err("Unable to allocate cmd->t_task_cdb"
1528a1d8b49aSAndy Grover " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1529c66ac9dbSNicholas Bellinger scsi_command_size(cdb),
1530a1d8b49aSAndy Grover (unsigned long)sizeof(cmd->__t_task_cdb));
15319e95fb80SSudhakar Panneerselvam ret = TCM_OUT_OF_RESOURCES;
15329e95fb80SSudhakar Panneerselvam goto err;
1533c66ac9dbSNicholas Bellinger }
15349e95fb80SSudhakar Panneerselvam }
1535c66ac9dbSNicholas Bellinger /*
1536a1d8b49aSAndy Grover * Copy the original CDB into cmd->
1537c66ac9dbSNicholas Bellinger */
1538a1d8b49aSAndy Grover memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1539cb4f4d3cSChristoph Hellwig
1540e5c0d6adSRoland Dreier trace_target_sequencer_start(cmd);
1541f98c2ddfSSudhakar Panneerselvam return 0;
15429e95fb80SSudhakar Panneerselvam
15439e95fb80SSudhakar Panneerselvam err:
15449e95fb80SSudhakar Panneerselvam /*
15459e95fb80SSudhakar Panneerselvam * Copy the CDB here to allow trace_target_cmd_complete() to
15469e95fb80SSudhakar Panneerselvam * print the cdb to the trace buffers.
15479e95fb80SSudhakar Panneerselvam */
15489e95fb80SSudhakar Panneerselvam memcpy(cmd->t_task_cdb, cdb, min(scsi_command_size(cdb),
15499e95fb80SSudhakar Panneerselvam (unsigned int)TCM_MAX_COMMAND_SIZE));
15509e95fb80SSudhakar Panneerselvam return ret;
1551f98c2ddfSSudhakar Panneerselvam }
1552f98c2ddfSSudhakar Panneerselvam EXPORT_SYMBOL(target_cmd_init_cdb);
1553f98c2ddfSSudhakar Panneerselvam
1554f98c2ddfSSudhakar Panneerselvam sense_reason_t
target_cmd_parse_cdb(struct se_cmd * cmd)1555987db587SSudhakar Panneerselvam target_cmd_parse_cdb(struct se_cmd *cmd)
1556f98c2ddfSSudhakar Panneerselvam {
1557f98c2ddfSSudhakar Panneerselvam struct se_device *dev = cmd->se_dev;
1558f98c2ddfSSudhakar Panneerselvam sense_reason_t ret;
1559e5c0d6adSRoland Dreier
15600fd97ccfSChristoph Hellwig ret = dev->transport->parse_cdb(cmd);
1561568e1f65SJoern Engel if (ret == TCM_UNSUPPORTED_SCSI_OPCODE)
156205787e34SKonstantin Shelekhin pr_debug_ratelimited("%s/%s: Unsupported SCSI Opcode 0x%02x, sending CHECK_CONDITION.\n",
156330c7ca93SDavid Disseldorp cmd->se_tfo->fabric_name,
1564568e1f65SJoern Engel cmd->se_sess->se_node_acl->initiatorname,
1565568e1f65SJoern Engel cmd->t_task_cdb[0]);
1566de103c93SChristoph Hellwig if (ret)
1567de103c93SChristoph Hellwig return ret;
1568cb4f4d3cSChristoph Hellwig
1569de103c93SChristoph Hellwig ret = transport_check_alloc_task_attr(cmd);
1570de103c93SChristoph Hellwig if (ret)
1571c66ac9dbSNicholas Bellinger return ret;
1572cb4f4d3cSChristoph Hellwig
1573cb4f4d3cSChristoph Hellwig cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
15744cc987eaSNicholas Bellinger atomic_long_inc(&cmd->se_lun->lun_stats.cmd_pdus);
1575c66ac9dbSNicholas Bellinger return 0;
1576c66ac9dbSNicholas Bellinger }
1577987db587SSudhakar Panneerselvam EXPORT_SYMBOL(target_cmd_parse_cdb);
1578c66ac9dbSNicholas Bellinger
1579c66ac9dbSNicholas Bellinger /*
1580695434e1SNicholas Bellinger * Used by fabric module frontends to queue tasks directly.
1581ac75d8beSBart Van Assche * May only be used from process context.
1582695434e1SNicholas Bellinger */
transport_handle_cdb_direct(struct se_cmd * cmd)1583695434e1SNicholas Bellinger int transport_handle_cdb_direct(
1584695434e1SNicholas Bellinger struct se_cmd *cmd)
1585695434e1SNicholas Bellinger {
1586de103c93SChristoph Hellwig sense_reason_t ret;
1587dd8ae59dSNicholas Bellinger
1588bbb08767SAhmed S. Darwish might_sleep();
1589bbb08767SAhmed S. Darwish
1590695434e1SNicholas Bellinger if (!cmd->se_lun) {
1591695434e1SNicholas Bellinger dump_stack();
15926708bb27SAndy Grover pr_err("cmd->se_lun is NULL\n");
1593695434e1SNicholas Bellinger return -EINVAL;
1594695434e1SNicholas Bellinger }
1595bbb08767SAhmed S. Darwish
1596dd8ae59dSNicholas Bellinger /*
1597af877292SChristoph Hellwig * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1598af877292SChristoph Hellwig * outstanding descriptors are handled correctly during shutdown via
1599af877292SChristoph Hellwig * transport_wait_for_tasks()
1600dd8ae59dSNicholas Bellinger *
1601dd8ae59dSNicholas Bellinger * Also, we don't take cmd->t_state_lock here as we only expect
1602dd8ae59dSNicholas Bellinger * this to be called for initial descriptor submission.
1603dd8ae59dSNicholas Bellinger */
1604dd8ae59dSNicholas Bellinger cmd->t_state = TRANSPORT_NEW_CMD;
16057d680f3bSChristoph Hellwig cmd->transport_state |= CMD_T_ACTIVE;
16067d680f3bSChristoph Hellwig
1607dd8ae59dSNicholas Bellinger /*
1608dd8ae59dSNicholas Bellinger * transport_generic_new_cmd() is already handling QUEUE_FULL,
1609dd8ae59dSNicholas Bellinger * so follow TRANSPORT_NEW_CMD processing thread context usage
1610dd8ae59dSNicholas Bellinger * and call transport_generic_request_failure() if necessary..
1611dd8ae59dSNicholas Bellinger */
1612dd8ae59dSNicholas Bellinger ret = transport_generic_new_cmd(cmd);
1613de103c93SChristoph Hellwig if (ret)
1614de103c93SChristoph Hellwig transport_generic_request_failure(cmd, ret);
1615dd8ae59dSNicholas Bellinger return 0;
1616695434e1SNicholas Bellinger }
1617695434e1SNicholas Bellinger EXPORT_SYMBOL(transport_handle_cdb_direct);
1618695434e1SNicholas Bellinger
1619c5ff8d6bSNicholas Bellinger sense_reason_t
transport_generic_map_mem_to_cmd(struct se_cmd * cmd,struct scatterlist * sgl,u32 sgl_count,struct scatterlist * sgl_bidi,u32 sgl_bidi_count)1620de103c93SChristoph Hellwig transport_generic_map_mem_to_cmd(struct se_cmd *cmd, struct scatterlist *sgl,
1621de103c93SChristoph Hellwig u32 sgl_count, struct scatterlist *sgl_bidi, u32 sgl_bidi_count)
1622de103c93SChristoph Hellwig {
1623de103c93SChristoph Hellwig if (!sgl || !sgl_count)
1624de103c93SChristoph Hellwig return 0;
1625de103c93SChristoph Hellwig
1626de103c93SChristoph Hellwig /*
1627de103c93SChristoph Hellwig * Reject SCSI data overflow with map_mem_to_cmd() as incoming
1628de103c93SChristoph Hellwig * scatterlists already have been set to follow what the fabric
1629de103c93SChristoph Hellwig * passes for the original expected data transfer length.
1630de103c93SChristoph Hellwig */
1631de103c93SChristoph Hellwig if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
1632de103c93SChristoph Hellwig pr_warn("Rejecting SCSI DATA overflow for fabric using"
1633de103c93SChristoph Hellwig " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
1634de103c93SChristoph Hellwig return TCM_INVALID_CDB_FIELD;
1635de103c93SChristoph Hellwig }
1636de103c93SChristoph Hellwig
1637de103c93SChristoph Hellwig cmd->t_data_sg = sgl;
1638de103c93SChristoph Hellwig cmd->t_data_nents = sgl_count;
1639de103c93SChristoph Hellwig cmd->t_bidi_data_sg = sgl_bidi;
1640de103c93SChristoph Hellwig cmd->t_bidi_data_nents = sgl_bidi_count;
1641b32bd0a8SIlias Tsitsimpis
1642de103c93SChristoph Hellwig cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
1643de103c93SChristoph Hellwig return 0;
1644de103c93SChristoph Hellwig }
1645de103c93SChristoph Hellwig
16469ad97b8bSRandy Dunlap /**
1647750a1d93SMike Christie * target_init_cmd - initialize se_cmd
1648750a1d93SMike Christie * @se_cmd: command descriptor to init
1649750a1d93SMike Christie * @se_sess: associated se_sess for endpoint
1650750a1d93SMike Christie * @sense: pointer to SCSI sense buffer
1651750a1d93SMike Christie * @unpacked_lun: unpacked LUN to reference for struct se_lun
1652750a1d93SMike Christie * @data_length: fabric expected data transfer length
1653750a1d93SMike Christie * @task_attr: SAM task attribute
1654750a1d93SMike Christie * @data_dir: DMA data direction
1655750a1d93SMike Christie * @flags: flags for command submission from target_sc_flags_tables
1656750a1d93SMike Christie *
1657750a1d93SMike Christie * Task tags are supported if the caller has set @se_cmd->tag.
1658750a1d93SMike Christie *
1659750a1d93SMike Christie * Returns:
1660750a1d93SMike Christie * - less than zero to signal active I/O shutdown failure.
1661750a1d93SMike Christie * - zero on success.
1662750a1d93SMike Christie *
1663750a1d93SMike Christie * If the fabric driver calls target_stop_session, then it must check the
1664750a1d93SMike Christie * return code and handle failures. This will never fail for other drivers,
1665750a1d93SMike Christie * and the return code can be ignored.
1666750a1d93SMike Christie */
target_init_cmd(struct se_cmd * se_cmd,struct se_session * se_sess,unsigned char * sense,u64 unpacked_lun,u32 data_length,int task_attr,int data_dir,int flags)1667750a1d93SMike Christie int target_init_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1668750a1d93SMike Christie unsigned char *sense, u64 unpacked_lun,
1669750a1d93SMike Christie u32 data_length, int task_attr, int data_dir, int flags)
1670750a1d93SMike Christie {
1671750a1d93SMike Christie struct se_portal_group *se_tpg;
1672750a1d93SMike Christie
1673750a1d93SMike Christie se_tpg = se_sess->se_tpg;
1674750a1d93SMike Christie BUG_ON(!se_tpg);
1675750a1d93SMike Christie BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1676750a1d93SMike Christie
1677750a1d93SMike Christie if (flags & TARGET_SCF_USE_CPUID)
1678750a1d93SMike Christie se_cmd->se_cmd_flags |= SCF_USE_CPUID;
1679750a1d93SMike Christie /*
1680750a1d93SMike Christie * Signal bidirectional data payloads to target-core
1681750a1d93SMike Christie */
1682750a1d93SMike Christie if (flags & TARGET_SCF_BIDI_OP)
1683750a1d93SMike Christie se_cmd->se_cmd_flags |= SCF_BIDI;
1684750a1d93SMike Christie
1685750a1d93SMike Christie if (flags & TARGET_SCF_UNKNOWN_SIZE)
1686750a1d93SMike Christie se_cmd->unknown_data_length = 1;
1687750a1d93SMike Christie /*
1688750a1d93SMike Christie * Initialize se_cmd for target operation. From this point
1689750a1d93SMike Christie * exceptions are handled by sending exception status via
1690750a1d93SMike Christie * target_core_fabric_ops->queue_status() callback
1691750a1d93SMike Christie */
1692750a1d93SMike Christie __target_init_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, data_length,
16938e288be8SMike Christie data_dir, task_attr, sense, unpacked_lun,
16948e288be8SMike Christie se_sess->cmd_cnt);
1695750a1d93SMike Christie
1696750a1d93SMike Christie /*
1697750a1d93SMike Christie * Obtain struct se_cmd->cmd_kref reference. A second kref_get here is
1698750a1d93SMike Christie * necessary for fabrics using TARGET_SCF_ACK_KREF that expect a second
1699750a1d93SMike Christie * kref_put() to happen during fabric packet acknowledgement.
1700750a1d93SMike Christie */
1701750a1d93SMike Christie return target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF);
1702750a1d93SMike Christie }
1703750a1d93SMike Christie EXPORT_SYMBOL_GPL(target_init_cmd);
1704750a1d93SMike Christie
1705750a1d93SMike Christie /**
1706750a1d93SMike Christie * target_submit_prep - prepare cmd for submission
1707750a1d93SMike Christie * @se_cmd: command descriptor to prep
1708750a1d93SMike Christie * @cdb: pointer to SCSI CDB
1709750a1d93SMike Christie * @sgl: struct scatterlist memory for unidirectional mapping
1710750a1d93SMike Christie * @sgl_count: scatterlist count for unidirectional mapping
1711750a1d93SMike Christie * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1712750a1d93SMike Christie * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1713750a1d93SMike Christie * @sgl_prot: struct scatterlist memory protection information
1714750a1d93SMike Christie * @sgl_prot_count: scatterlist count for protection information
171508694199SMike Christie * @gfp: gfp allocation type
1716750a1d93SMike Christie *
1717750a1d93SMike Christie * Returns:
1718750a1d93SMike Christie * - less than zero to signal failure.
1719750a1d93SMike Christie * - zero on success.
1720b1ebd3b0SMike Christie *
1721750a1d93SMike Christie * If failure is returned, lio will the callers queue_status to complete
1722750a1d93SMike Christie * the cmd.
1723750a1d93SMike Christie */
target_submit_prep(struct se_cmd * se_cmd,unsigned char * cdb,struct scatterlist * sgl,u32 sgl_count,struct scatterlist * sgl_bidi,u32 sgl_bidi_count,struct scatterlist * sgl_prot,u32 sgl_prot_count,gfp_t gfp)1724750a1d93SMike Christie int target_submit_prep(struct se_cmd *se_cmd, unsigned char *cdb,
1725750a1d93SMike Christie struct scatterlist *sgl, u32 sgl_count,
1726750a1d93SMike Christie struct scatterlist *sgl_bidi, u32 sgl_bidi_count,
172708694199SMike Christie struct scatterlist *sgl_prot, u32 sgl_prot_count,
172808694199SMike Christie gfp_t gfp)
1729750a1d93SMike Christie {
1730750a1d93SMike Christie sense_reason_t rc;
1731750a1d93SMike Christie
173208694199SMike Christie rc = target_cmd_init_cdb(se_cmd, cdb, gfp);
1733750a1d93SMike Christie if (rc)
1734750a1d93SMike Christie goto send_cc_direct;
1735750a1d93SMike Christie
1736750a1d93SMike Christie /*
1737750a1d93SMike Christie * Locate se_lun pointer and attach it to struct se_cmd
1738750a1d93SMike Christie */
1739750a1d93SMike Christie rc = transport_lookup_cmd_lun(se_cmd);
1740750a1d93SMike Christie if (rc)
1741750a1d93SMike Christie goto send_cc_direct;
1742750a1d93SMike Christie
1743750a1d93SMike Christie rc = target_cmd_parse_cdb(se_cmd);
1744750a1d93SMike Christie if (rc != 0)
1745750a1d93SMike Christie goto generic_fail;
1746750a1d93SMike Christie
1747750a1d93SMike Christie /*
1748750a1d93SMike Christie * Save pointers for SGLs containing protection information,
1749750a1d93SMike Christie * if present.
1750750a1d93SMike Christie */
1751750a1d93SMike Christie if (sgl_prot_count) {
1752750a1d93SMike Christie se_cmd->t_prot_sg = sgl_prot;
1753750a1d93SMike Christie se_cmd->t_prot_nents = sgl_prot_count;
1754750a1d93SMike Christie se_cmd->se_cmd_flags |= SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC;
1755750a1d93SMike Christie }
1756750a1d93SMike Christie
1757750a1d93SMike Christie /*
1758750a1d93SMike Christie * When a non zero sgl_count has been passed perform SGL passthrough
1759750a1d93SMike Christie * mapping for pre-allocated fabric memory instead of having target
1760750a1d93SMike Christie * core perform an internal SGL allocation..
1761750a1d93SMike Christie */
1762750a1d93SMike Christie if (sgl_count != 0) {
1763750a1d93SMike Christie BUG_ON(!sgl);
1764750a1d93SMike Christie
1765750a1d93SMike Christie rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
1766750a1d93SMike Christie sgl_bidi, sgl_bidi_count);
1767750a1d93SMike Christie if (rc != 0)
1768750a1d93SMike Christie goto generic_fail;
1769750a1d93SMike Christie }
1770750a1d93SMike Christie
1771750a1d93SMike Christie return 0;
1772750a1d93SMike Christie
1773750a1d93SMike Christie send_cc_direct:
1774750a1d93SMike Christie transport_send_check_condition_and_sense(se_cmd, rc, 0);
1775750a1d93SMike Christie target_put_sess_cmd(se_cmd);
1776750a1d93SMike Christie return -EIO;
1777750a1d93SMike Christie
1778750a1d93SMike Christie generic_fail:
1779750a1d93SMike Christie transport_generic_request_failure(se_cmd, rc);
1780750a1d93SMike Christie return -EIO;
1781750a1d93SMike Christie }
1782750a1d93SMike Christie EXPORT_SYMBOL_GPL(target_submit_prep);
1783750a1d93SMike Christie
1784750a1d93SMike Christie /**
1785750a1d93SMike Christie * target_submit - perform final initialization and submit cmd to LIO core
1786750a1d93SMike Christie * @se_cmd: command descriptor to submit
1787750a1d93SMike Christie *
1788750a1d93SMike Christie * target_submit_prep must have been called on the cmd, and this must be
1789750a1d93SMike Christie * called from process context.
1790750a1d93SMike Christie */
target_submit(struct se_cmd * se_cmd)1791750a1d93SMike Christie void target_submit(struct se_cmd *se_cmd)
1792750a1d93SMike Christie {
1793750a1d93SMike Christie struct scatterlist *sgl = se_cmd->t_data_sg;
1794750a1d93SMike Christie unsigned char *buf = NULL;
1795750a1d93SMike Christie
1796750a1d93SMike Christie might_sleep();
1797750a1d93SMike Christie
1798750a1d93SMike Christie if (se_cmd->t_data_nents != 0) {
1799750a1d93SMike Christie BUG_ON(!sgl);
1800750a1d93SMike Christie /*
1801750a1d93SMike Christie * A work-around for tcm_loop as some userspace code via
1802750a1d93SMike Christie * scsi-generic do not memset their associated read buffers,
1803750a1d93SMike Christie * so go ahead and do that here for type non-data CDBs. Also
1804750a1d93SMike Christie * note that this is currently guaranteed to be a single SGL
1805750a1d93SMike Christie * for this case by target core in target_setup_cmd_from_cdb()
1806750a1d93SMike Christie * -> transport_generic_cmd_sequencer().
1807750a1d93SMike Christie */
1808750a1d93SMike Christie if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
1809750a1d93SMike Christie se_cmd->data_direction == DMA_FROM_DEVICE) {
1810750a1d93SMike Christie if (sgl)
1811750a1d93SMike Christie buf = kmap(sg_page(sgl)) + sgl->offset;
1812750a1d93SMike Christie
1813750a1d93SMike Christie if (buf) {
1814750a1d93SMike Christie memset(buf, 0, sgl->length);
1815750a1d93SMike Christie kunmap(sg_page(sgl));
1816750a1d93SMike Christie }
1817750a1d93SMike Christie }
1818750a1d93SMike Christie
1819750a1d93SMike Christie }
1820750a1d93SMike Christie
1821750a1d93SMike Christie /*
1822750a1d93SMike Christie * Check if we need to delay processing because of ALUA
1823750a1d93SMike Christie * Active/NonOptimized primary access state..
1824750a1d93SMike Christie */
1825750a1d93SMike Christie core_alua_check_nonop_delay(se_cmd);
1826750a1d93SMike Christie
1827750a1d93SMike Christie transport_handle_cdb_direct(se_cmd);
1828750a1d93SMike Christie }
1829750a1d93SMike Christie EXPORT_SYMBOL_GPL(target_submit);
1830750a1d93SMike Christie
1831750a1d93SMike Christie /**
1832a026757fSNicholas Bellinger * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1833a026757fSNicholas Bellinger *
1834a026757fSNicholas Bellinger * @se_cmd: command descriptor to submit
1835a026757fSNicholas Bellinger * @se_sess: associated se_sess for endpoint
1836a026757fSNicholas Bellinger * @cdb: pointer to SCSI CDB
1837a026757fSNicholas Bellinger * @sense: pointer to SCSI sense buffer
1838a026757fSNicholas Bellinger * @unpacked_lun: unpacked LUN to reference for struct se_lun
1839a026757fSNicholas Bellinger * @data_length: fabric expected data transfer length
18409ad97b8bSRandy Dunlap * @task_attr: SAM task attribute
1841a026757fSNicholas Bellinger * @data_dir: DMA data direction
1842a026757fSNicholas Bellinger * @flags: flags for command submission from target_sc_flags_tables
1843a026757fSNicholas Bellinger *
1844649ee054SBart Van Assche * Task tags are supported if the caller has set @se_cmd->tag.
1845649ee054SBart Van Assche *
1846a026757fSNicholas Bellinger * This may only be called from process context, and also currently
1847a026757fSNicholas Bellinger * assumes internal allocation of fabric payload buffer by target-core.
1848a026757fSNicholas Bellinger *
1849a026757fSNicholas Bellinger * It also assumes interal target core SGL memory allocation.
18500fa50a8bSMike Christie *
18510fa50a8bSMike Christie * This function must only be used by drivers that do their own
18520fa50a8bSMike Christie * sync during shutdown and does not use target_stop_session. If there
18530fa50a8bSMike Christie * is a failure this function will call into the fabric driver's
18540fa50a8bSMike Christie * queue_status with a CHECK_CONDITION.
1855a026757fSNicholas Bellinger */
target_submit_cmd(struct se_cmd * se_cmd,struct se_session * se_sess,unsigned char * cdb,unsigned char * sense,u64 unpacked_lun,u32 data_length,int task_attr,int data_dir,int flags)18560fa50a8bSMike Christie void target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1857f2d30680SHannes Reinecke unsigned char *cdb, unsigned char *sense, u64 unpacked_lun,
1858a026757fSNicholas Bellinger u32 data_length, int task_attr, int data_dir, int flags)
1859a026757fSNicholas Bellinger {
18600fa50a8bSMike Christie int rc;
18610fa50a8bSMike Christie
18620fa50a8bSMike Christie rc = target_init_cmd(se_cmd, se_sess, sense, unpacked_lun, data_length,
18630fa50a8bSMike Christie task_attr, data_dir, flags);
18640fa50a8bSMike Christie WARN(rc, "Invalid target_submit_cmd use. Driver must not use target_stop_session or call target_init_cmd directly.\n");
18650fa50a8bSMike Christie if (rc)
18660fa50a8bSMike Christie return;
18670fa50a8bSMike Christie
186808694199SMike Christie if (target_submit_prep(se_cmd, cdb, NULL, 0, NULL, 0, NULL, 0,
186908694199SMike Christie GFP_KERNEL))
18700fa50a8bSMike Christie return;
18710fa50a8bSMike Christie
18720fa50a8bSMike Christie target_submit(se_cmd);
1873a026757fSNicholas Bellinger }
1874a6360785SNicholas Bellinger EXPORT_SYMBOL(target_submit_cmd);
1875a6360785SNicholas Bellinger
1876302990acSMike Christie
target_plug_device(struct se_device * se_dev)1877302990acSMike Christie static struct se_dev_plug *target_plug_device(struct se_device *se_dev)
1878302990acSMike Christie {
1879302990acSMike Christie struct se_dev_plug *se_plug;
1880302990acSMike Christie
1881302990acSMike Christie if (!se_dev->transport->plug_device)
1882302990acSMike Christie return NULL;
1883302990acSMike Christie
1884302990acSMike Christie se_plug = se_dev->transport->plug_device(se_dev);
1885302990acSMike Christie if (!se_plug)
1886302990acSMike Christie return NULL;
1887302990acSMike Christie
1888302990acSMike Christie se_plug->se_dev = se_dev;
1889302990acSMike Christie /*
1890302990acSMike Christie * We have a ref to the lun at this point, but the cmds could
1891302990acSMike Christie * complete before we unplug, so grab a ref to the se_device so we
1892302990acSMike Christie * can call back into the backend.
1893302990acSMike Christie */
1894302990acSMike Christie config_group_get(&se_dev->dev_group);
1895302990acSMike Christie return se_plug;
1896302990acSMike Christie }
1897302990acSMike Christie
target_unplug_device(struct se_dev_plug * se_plug)1898302990acSMike Christie static void target_unplug_device(struct se_dev_plug *se_plug)
1899302990acSMike Christie {
1900302990acSMike Christie struct se_device *se_dev = se_plug->se_dev;
1901302990acSMike Christie
1902302990acSMike Christie se_dev->transport->unplug_device(se_plug);
1903302990acSMike Christie config_group_put(&se_dev->dev_group);
1904302990acSMike Christie }
1905302990acSMike Christie
target_queued_submit_work(struct work_struct * work)1906eb44ce8cSMike Christie void target_queued_submit_work(struct work_struct *work)
1907eb44ce8cSMike Christie {
1908eb44ce8cSMike Christie struct se_cmd_queue *sq = container_of(work, struct se_cmd_queue, work);
1909eb44ce8cSMike Christie struct se_cmd *se_cmd, *next_cmd;
1910302990acSMike Christie struct se_dev_plug *se_plug = NULL;
1911302990acSMike Christie struct se_device *se_dev = NULL;
1912eb44ce8cSMike Christie struct llist_node *cmd_list;
1913eb44ce8cSMike Christie
1914eb44ce8cSMike Christie cmd_list = llist_del_all(&sq->cmd_list);
1915eb44ce8cSMike Christie if (!cmd_list)
1916eb44ce8cSMike Christie /* Previous call took what we were queued to submit */
1917eb44ce8cSMike Christie return;
1918eb44ce8cSMike Christie
1919eb44ce8cSMike Christie cmd_list = llist_reverse_order(cmd_list);
1920302990acSMike Christie llist_for_each_entry_safe(se_cmd, next_cmd, cmd_list, se_cmd_list) {
1921302990acSMike Christie if (!se_dev) {
1922302990acSMike Christie se_dev = se_cmd->se_dev;
1923302990acSMike Christie se_plug = target_plug_device(se_dev);
1924302990acSMike Christie }
1925302990acSMike Christie
1926eb44ce8cSMike Christie target_submit(se_cmd);
1927eb44ce8cSMike Christie }
1928eb44ce8cSMike Christie
1929302990acSMike Christie if (se_plug)
1930302990acSMike Christie target_unplug_device(se_plug);
1931302990acSMike Christie }
1932302990acSMike Christie
1933eb44ce8cSMike Christie /**
1934eb44ce8cSMike Christie * target_queue_submission - queue the cmd to run on the LIO workqueue
1935eb44ce8cSMike Christie * @se_cmd: command descriptor to submit
1936eb44ce8cSMike Christie */
target_queue_submission(struct se_cmd * se_cmd)1937eb44ce8cSMike Christie void target_queue_submission(struct se_cmd *se_cmd)
1938eb44ce8cSMike Christie {
1939eb44ce8cSMike Christie struct se_device *se_dev = se_cmd->se_dev;
1940eb44ce8cSMike Christie int cpu = se_cmd->cpuid;
1941eb44ce8cSMike Christie struct se_cmd_queue *sq;
1942eb44ce8cSMike Christie
1943eb44ce8cSMike Christie sq = &se_dev->queues[cpu].sq;
1944eb44ce8cSMike Christie llist_add(&se_cmd->se_cmd_list, &sq->cmd_list);
1945eb44ce8cSMike Christie queue_work_on(cpu, target_submission_wq, &sq->work);
1946eb44ce8cSMike Christie }
1947eb44ce8cSMike Christie EXPORT_SYMBOL_GPL(target_queue_submission);
1948eb44ce8cSMike Christie
target_complete_tmr_failure(struct work_struct * work)19499f0d05c2SNicholas Bellinger static void target_complete_tmr_failure(struct work_struct *work)
19509f0d05c2SNicholas Bellinger {
19519f0d05c2SNicholas Bellinger struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
19529f0d05c2SNicholas Bellinger
19539f0d05c2SNicholas Bellinger se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
19549f0d05c2SNicholas Bellinger se_cmd->se_tfo->queue_tm_rsp(se_cmd);
19555a3b6fc0SRoland Dreier
1956c14335ebSBart Van Assche transport_lun_remove_cmd(se_cmd);
19575a3b6fc0SRoland Dreier transport_cmd_check_stop_to_fabric(se_cmd);
19589f0d05c2SNicholas Bellinger }
19599f0d05c2SNicholas Bellinger
1960ea98d7f9SAndy Grover /**
1961ea98d7f9SAndy Grover * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1962ea98d7f9SAndy Grover * for TMR CDBs
1963ea98d7f9SAndy Grover *
1964ea98d7f9SAndy Grover * @se_cmd: command descriptor to submit
1965ea98d7f9SAndy Grover * @se_sess: associated se_sess for endpoint
1966ea98d7f9SAndy Grover * @sense: pointer to SCSI sense buffer
1967ea98d7f9SAndy Grover * @unpacked_lun: unpacked LUN to reference for struct se_lun
19681e74aff1SRandy Dunlap * @fabric_tmr_ptr: fabric context for TMR req
1969ea98d7f9SAndy Grover * @tm_type: Type of TM request
1970c0974f89SNicholas Bellinger * @gfp: gfp type for caller
1971c0974f89SNicholas Bellinger * @tag: referenced task tag for TMR_ABORT_TASK
1972c7042caeSNicholas Bellinger * @flags: submit cmd flags
1973ea98d7f9SAndy Grover *
1974ea98d7f9SAndy Grover * Callable from all contexts.
1975ea98d7f9SAndy Grover **/
1976ea98d7f9SAndy Grover
target_submit_tmr(struct se_cmd * se_cmd,struct se_session * se_sess,unsigned char * sense,u64 unpacked_lun,void * fabric_tmr_ptr,unsigned char tm_type,gfp_t gfp,u64 tag,int flags)1977c7042caeSNicholas Bellinger int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1978f2d30680SHannes Reinecke unsigned char *sense, u64 unpacked_lun,
1979c0974f89SNicholas Bellinger void *fabric_tmr_ptr, unsigned char tm_type,
19805261d86cSBart Van Assche gfp_t gfp, u64 tag, int flags)
1981ea98d7f9SAndy Grover {
1982ea98d7f9SAndy Grover struct se_portal_group *se_tpg;
1983ea98d7f9SAndy Grover int ret;
1984ea98d7f9SAndy Grover
1985ea98d7f9SAndy Grover se_tpg = se_sess->se_tpg;
1986ea98d7f9SAndy Grover BUG_ON(!se_tpg);
1987ea98d7f9SAndy Grover
1988a78b7136SMike Christie __target_init_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
19898e288be8SMike Christie 0, DMA_NONE, TCM_SIMPLE_TAG, sense, unpacked_lun,
19908e288be8SMike Christie se_sess->cmd_cnt);
1991c7042caeSNicholas Bellinger /*
1992c7042caeSNicholas Bellinger * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1993c7042caeSNicholas Bellinger * allocation failure.
1994c7042caeSNicholas Bellinger */
1995c0974f89SNicholas Bellinger ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
1996c7042caeSNicholas Bellinger if (ret < 0)
1997c7042caeSNicholas Bellinger return -ENOMEM;
1998ea98d7f9SAndy Grover
1999c0974f89SNicholas Bellinger if (tm_type == TMR_ABORT_TASK)
2000c0974f89SNicholas Bellinger se_cmd->se_tmr_req->ref_task_tag = tag;
2001c0974f89SNicholas Bellinger
2002ea98d7f9SAndy Grover /* See target_submit_cmd for commentary */
2003afc16604SBart Van Assche ret = target_get_sess_cmd(se_cmd, flags & TARGET_SCF_ACK_KREF);
2004bc187ea6SRoland Dreier if (ret) {
2005bc187ea6SRoland Dreier core_tmr_release_req(se_cmd->se_tmr_req);
2006bc187ea6SRoland Dreier return ret;
2007bc187ea6SRoland Dreier }
2008ea98d7f9SAndy Grover
2009a36840d8SSudhakar Panneerselvam ret = transport_lookup_tmr_lun(se_cmd);
20105465e7d3SNicholas Bellinger if (ret)
20115465e7d3SNicholas Bellinger goto failure;
20125465e7d3SNicholas Bellinger
20135465e7d3SNicholas Bellinger transport_generic_handle_tmr(se_cmd);
20145465e7d3SNicholas Bellinger return 0;
20155465e7d3SNicholas Bellinger
20169f0d05c2SNicholas Bellinger /*
20179f0d05c2SNicholas Bellinger * For callback during failure handling, push this work off
20189f0d05c2SNicholas Bellinger * to process context with TMR_LUN_DOES_NOT_EXIST status.
20199f0d05c2SNicholas Bellinger */
20205465e7d3SNicholas Bellinger failure:
20219f0d05c2SNicholas Bellinger INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
20229f0d05c2SNicholas Bellinger schedule_work(&se_cmd->work);
2023c7042caeSNicholas Bellinger return 0;
2024ea98d7f9SAndy Grover }
2025ea98d7f9SAndy Grover EXPORT_SYMBOL(target_submit_tmr);
2026ea98d7f9SAndy Grover
2027cdbb70bbSChristoph Hellwig /*
2028c66ac9dbSNicholas Bellinger * Handle SAM-esque emulation for generic transport request failures.
2029c66ac9dbSNicholas Bellinger */
transport_generic_request_failure(struct se_cmd * cmd,sense_reason_t sense_reason)2030de103c93SChristoph Hellwig void transport_generic_request_failure(struct se_cmd *cmd,
2031de103c93SChristoph Hellwig sense_reason_t sense_reason)
2032c66ac9dbSNicholas Bellinger {
20335841734fSBart Van Assche int ret = 0, post_ret;
203407bde79aSNicholas Bellinger
2035c00e6220SBart Van Assche pr_debug("-----[ Storage Engine Exception; sense_reason %d\n",
2036c00e6220SBart Van Assche sense_reason);
2037c00e6220SBart Van Assche target_show_cmd("-----[ ", cmd);
2038c66ac9dbSNicholas Bellinger
2039c66ac9dbSNicholas Bellinger /*
2040c66ac9dbSNicholas Bellinger * For SAM Task Attribute emulation for failed struct se_cmd
2041c66ac9dbSNicholas Bellinger */
2042c66ac9dbSNicholas Bellinger transport_complete_task_attr(cmd);
2043fd2f928bSNicholas Bellinger
2044aa73237dSBart Van Assche if (cmd->transport_complete_callback)
20455841734fSBart Van Assche cmd->transport_complete_callback(cmd, false, &post_ret);
2046c66ac9dbSNicholas Bellinger
20472c9fa49eSBart Van Assche if (cmd->transport_state & CMD_T_ABORTED) {
20482c9fa49eSBart Van Assche INIT_WORK(&cmd->work, target_abort_work);
20492c9fa49eSBart Van Assche queue_work(target_completion_wq, &cmd->work);
2050fd2f928bSNicholas Bellinger return;
20512c9fa49eSBart Van Assche }
2052fd2f928bSNicholas Bellinger
2053de103c93SChristoph Hellwig switch (sense_reason) {
205403e98c9eSNicholas Bellinger case TCM_NON_EXISTENT_LUN:
205503e98c9eSNicholas Bellinger case TCM_UNSUPPORTED_SCSI_OPCODE:
205603e98c9eSNicholas Bellinger case TCM_INVALID_CDB_FIELD:
205703e98c9eSNicholas Bellinger case TCM_INVALID_PARAMETER_LIST:
2058bb992e72SRoland Dreier case TCM_PARAMETER_LIST_LENGTH_ERROR:
205903e98c9eSNicholas Bellinger case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
206003e98c9eSNicholas Bellinger case TCM_UNKNOWN_MODE_PAGE:
206103e98c9eSNicholas Bellinger case TCM_WRITE_PROTECTED:
2062e2397c70SRoland Dreier case TCM_ADDRESS_OUT_OF_RANGE:
206303e98c9eSNicholas Bellinger case TCM_CHECK_CONDITION_ABORT_CMD:
206403e98c9eSNicholas Bellinger case TCM_CHECK_CONDITION_UNIT_ATTENTION:
206594387aa7SNicholas Bellinger case TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED:
206694387aa7SNicholas Bellinger case TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED:
206794387aa7SNicholas Bellinger case TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED:
2068449a1378SNicholas Bellinger case TCM_COPY_TARGET_DEVICE_NOT_REACHABLE:
2069e8642120SDavid Disseldorp case TCM_TOO_MANY_TARGET_DESCS:
2070e8642120SDavid Disseldorp case TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE:
2071e8642120SDavid Disseldorp case TCM_TOO_MANY_SEGMENT_DESCS:
2072e8642120SDavid Disseldorp case TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE:
2073ead0ffc9SAnastasia Kovaleva case TCM_INVALID_FIELD_IN_COMMAND_IU:
207440fd8845SDavid Disseldorp case TCM_ALUA_TG_PT_STANDBY:
207540fd8845SDavid Disseldorp case TCM_ALUA_TG_PT_UNAVAILABLE:
207640fd8845SDavid Disseldorp case TCM_ALUA_STATE_TRANSITION:
207740fd8845SDavid Disseldorp case TCM_ALUA_OFFLINE:
2078c66ac9dbSNicholas Bellinger break;
2079de103c93SChristoph Hellwig case TCM_OUT_OF_RESOURCES:
2080a271eac4SMike Christie cmd->scsi_status = SAM_STAT_TASK_SET_FULL;
2081a271eac4SMike Christie goto queue_status;
2082d120c708SMike Christie case TCM_LUN_BUSY:
2083d120c708SMike Christie cmd->scsi_status = SAM_STAT_BUSY;
2084d120c708SMike Christie goto queue_status;
208503e98c9eSNicholas Bellinger case TCM_RESERVATION_CONFLICT:
2086c66ac9dbSNicholas Bellinger /*
2087c66ac9dbSNicholas Bellinger * No SENSE Data payload for this case, set SCSI Status
2088c66ac9dbSNicholas Bellinger * and queue the response to $FABRIC_MOD.
2089c66ac9dbSNicholas Bellinger *
2090c66ac9dbSNicholas Bellinger * Uses linux/include/scsi/scsi.h SAM status codes defs
2091c66ac9dbSNicholas Bellinger */
2092c66ac9dbSNicholas Bellinger cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2093c66ac9dbSNicholas Bellinger /*
2094c66ac9dbSNicholas Bellinger * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2095c66ac9dbSNicholas Bellinger * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2096c66ac9dbSNicholas Bellinger * CONFLICT STATUS.
2097c66ac9dbSNicholas Bellinger *
2098c66ac9dbSNicholas Bellinger * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2099c66ac9dbSNicholas Bellinger */
2100e3d6f909SAndy Grover if (cmd->se_sess &&
21011bf630fdSDavid Disseldorp cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl
21021bf630fdSDavid Disseldorp == TARGET_UA_INTLCK_CTRL_ESTABLISH_UA) {
2103c51c8e7bSHannes Reinecke target_ua_allocate_lun(cmd->se_sess->se_node_acl,
2104c66ac9dbSNicholas Bellinger cmd->orig_fe_lun, 0x2C,
2105c66ac9dbSNicholas Bellinger ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2106c51c8e7bSHannes Reinecke }
2107a271eac4SMike Christie
2108a271eac4SMike Christie goto queue_status;
2109c66ac9dbSNicholas Bellinger default:
21106708bb27SAndy Grover pr_err("Unknown transport error for CDB 0x%02x: %d\n",
2111de103c93SChristoph Hellwig cmd->t_task_cdb[0], sense_reason);
2112de103c93SChristoph Hellwig sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
2113c66ac9dbSNicholas Bellinger break;
2114c66ac9dbSNicholas Bellinger }
2115f3146437SChristoph Hellwig
2116de103c93SChristoph Hellwig ret = transport_send_check_condition_and_sense(cmd, sense_reason, 0);
2117fa7e25cfSNicholas Bellinger if (ret)
211807bde79aSNicholas Bellinger goto queue_full;
211907bde79aSNicholas Bellinger
2120c66ac9dbSNicholas Bellinger check_stop:
2121c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
212206b967e4SAndy Grover transport_cmd_check_stop_to_fabric(cmd);
212307bde79aSNicholas Bellinger return;
212407bde79aSNicholas Bellinger
2125a271eac4SMike Christie queue_status:
2126a271eac4SMike Christie trace_target_cmd_complete(cmd);
2127a271eac4SMike Christie ret = cmd->se_tfo->queue_status(cmd);
2128a271eac4SMike Christie if (!ret)
2129a271eac4SMike Christie goto check_stop;
213007bde79aSNicholas Bellinger queue_full:
2131fa7e25cfSNicholas Bellinger transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
2132c66ac9dbSNicholas Bellinger }
21332fbff127SNicholas Bellinger EXPORT_SYMBOL(transport_generic_request_failure);
2134c66ac9dbSNicholas Bellinger
__target_execute_cmd(struct se_cmd * cmd,bool do_checks)2135dff0ca9eSNicholas Bellinger void __target_execute_cmd(struct se_cmd *cmd, bool do_checks)
2136c66ac9dbSNicholas Bellinger {
2137de103c93SChristoph Hellwig sense_reason_t ret;
21385f41a31dSChristoph Hellwig
2139dff0ca9eSNicholas Bellinger if (!cmd->execute_cmd) {
2140dff0ca9eSNicholas Bellinger ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2141dff0ca9eSNicholas Bellinger goto err;
2142dff0ca9eSNicholas Bellinger }
2143dff0ca9eSNicholas Bellinger if (do_checks) {
2144dff0ca9eSNicholas Bellinger /*
2145dff0ca9eSNicholas Bellinger * Check for an existing UNIT ATTENTION condition after
2146dff0ca9eSNicholas Bellinger * target_handle_task_attr() has done SAM task attr
2147dff0ca9eSNicholas Bellinger * checking, and possibly have already defered execution
2148dff0ca9eSNicholas Bellinger * out to target_restart_delayed_cmds() context.
2149dff0ca9eSNicholas Bellinger */
2150dff0ca9eSNicholas Bellinger ret = target_scsi3_ua_check(cmd);
2151dff0ca9eSNicholas Bellinger if (ret)
2152dff0ca9eSNicholas Bellinger goto err;
2153dff0ca9eSNicholas Bellinger
2154dff0ca9eSNicholas Bellinger ret = target_alua_state_check(cmd);
2155dff0ca9eSNicholas Bellinger if (ret)
2156dff0ca9eSNicholas Bellinger goto err;
2157dff0ca9eSNicholas Bellinger
2158dff0ca9eSNicholas Bellinger ret = target_check_reservation(cmd);
2159de103c93SChristoph Hellwig if (ret) {
2160dff0ca9eSNicholas Bellinger cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2161dff0ca9eSNicholas Bellinger goto err;
2162dff0ca9eSNicholas Bellinger }
2163dff0ca9eSNicholas Bellinger }
2164dff0ca9eSNicholas Bellinger
2165dff0ca9eSNicholas Bellinger ret = cmd->execute_cmd(cmd);
2166dff0ca9eSNicholas Bellinger if (!ret)
2167dff0ca9eSNicholas Bellinger return;
2168dff0ca9eSNicholas Bellinger err:
21695f41a31dSChristoph Hellwig spin_lock_irq(&cmd->t_state_lock);
2170fd5e64deSBart Van Assche cmd->transport_state &= ~CMD_T_SENT;
21715f41a31dSChristoph Hellwig spin_unlock_irq(&cmd->t_state_lock);
21725f41a31dSChristoph Hellwig
2173de103c93SChristoph Hellwig transport_generic_request_failure(cmd, ret);
21745f41a31dSChristoph Hellwig }
21755f41a31dSChristoph Hellwig
target_write_prot_action(struct se_cmd * cmd)2176aa58b531SNicholas Bellinger static int target_write_prot_action(struct se_cmd *cmd)
2177aa58b531SNicholas Bellinger {
21785132d1e6SNicholas Bellinger u32 sectors;
2179aa58b531SNicholas Bellinger /*
2180aa58b531SNicholas Bellinger * Perform WRITE_INSERT of PI using software emulation when backend
2181aa58b531SNicholas Bellinger * device has PI enabled, if the transport has not already generated
2182aa58b531SNicholas Bellinger * PI using hardware WRITE_INSERT offload.
2183aa58b531SNicholas Bellinger */
2184aa58b531SNicholas Bellinger switch (cmd->prot_op) {
2185aa58b531SNicholas Bellinger case TARGET_PROT_DOUT_INSERT:
2186aa58b531SNicholas Bellinger if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_INSERT))
2187aa58b531SNicholas Bellinger sbc_dif_generate(cmd);
2188aa58b531SNicholas Bellinger break;
21895132d1e6SNicholas Bellinger case TARGET_PROT_DOUT_STRIP:
21905132d1e6SNicholas Bellinger if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DOUT_STRIP)
21915132d1e6SNicholas Bellinger break;
21925132d1e6SNicholas Bellinger
21935132d1e6SNicholas Bellinger sectors = cmd->data_length >> ilog2(cmd->se_dev->dev_attrib.block_size);
2194f75b6faeSSagi Grimberg cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba,
2195f75b6faeSSagi Grimberg sectors, 0, cmd->t_prot_sg, 0);
21965132d1e6SNicholas Bellinger if (unlikely(cmd->pi_err)) {
21975132d1e6SNicholas Bellinger spin_lock_irq(&cmd->t_state_lock);
2198fd5e64deSBart Van Assche cmd->transport_state &= ~CMD_T_SENT;
21995132d1e6SNicholas Bellinger spin_unlock_irq(&cmd->t_state_lock);
22005132d1e6SNicholas Bellinger transport_generic_request_failure(cmd, cmd->pi_err);
22015132d1e6SNicholas Bellinger return -1;
22025132d1e6SNicholas Bellinger }
22035132d1e6SNicholas Bellinger break;
2204aa58b531SNicholas Bellinger default:
2205aa58b531SNicholas Bellinger break;
2206aa58b531SNicholas Bellinger }
2207aa58b531SNicholas Bellinger
2208aa58b531SNicholas Bellinger return 0;
2209aa58b531SNicholas Bellinger }
2210aa58b531SNicholas Bellinger
target_handle_task_attr(struct se_cmd * cmd)2211019c4ca6SChristoph Hellwig static bool target_handle_task_attr(struct se_cmd *cmd)
22125f41a31dSChristoph Hellwig {
22135f41a31dSChristoph Hellwig struct se_device *dev = cmd->se_dev;
22145f41a31dSChristoph Hellwig
221569088a04SBodo Stroesser if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
2216019c4ca6SChristoph Hellwig return false;
2217019c4ca6SChristoph Hellwig
2218410c29dfSNicholas Bellinger cmd->se_cmd_flags |= SCF_TASK_ATTR_SET;
2219410c29dfSNicholas Bellinger
2220019c4ca6SChristoph Hellwig /*
2221019c4ca6SChristoph Hellwig * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2222019c4ca6SChristoph Hellwig * to allow the passed struct se_cmd list of tasks to the front of the list.
2223019c4ca6SChristoph Hellwig */
2224019c4ca6SChristoph Hellwig switch (cmd->sam_task_attr) {
222568d81f40SChristoph Hellwig case TCM_HEAD_TAG:
2226ed1227e0SMike Christie atomic_inc_mb(&dev->non_ordered);
22279c31820bSRoland Dreier pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x\n",
22289c31820bSRoland Dreier cmd->t_task_cdb[0]);
2229019c4ca6SChristoph Hellwig return false;
223068d81f40SChristoph Hellwig case TCM_ORDERED_TAG:
2231ed1227e0SMike Christie atomic_inc_mb(&dev->delayed_cmd_count);
2232019c4ca6SChristoph Hellwig
22339c31820bSRoland Dreier pr_debug("Added ORDERED for CDB: 0x%02x to ordered list\n",
22349c31820bSRoland Dreier cmd->t_task_cdb[0]);
2235019c4ca6SChristoph Hellwig break;
2236019c4ca6SChristoph Hellwig default:
2237019c4ca6SChristoph Hellwig /*
2238019c4ca6SChristoph Hellwig * For SIMPLE and UNTAGGED Task Attribute commands
2239019c4ca6SChristoph Hellwig */
2240ed1227e0SMike Christie atomic_inc_mb(&dev->non_ordered);
2241ed1227e0SMike Christie
2242ed1227e0SMike Christie if (atomic_read(&dev->delayed_cmd_count) == 0)
2243ed1227e0SMike Christie return false;
2244019c4ca6SChristoph Hellwig break;
2245019c4ca6SChristoph Hellwig }
2246019c4ca6SChristoph Hellwig
2247ed1227e0SMike Christie if (cmd->sam_task_attr != TCM_ORDERED_TAG) {
2248ed1227e0SMike Christie atomic_inc_mb(&dev->delayed_cmd_count);
2249ed1227e0SMike Christie /*
2250ed1227e0SMike Christie * We will account for this when we dequeue from the delayed
2251ed1227e0SMike Christie * list.
2252ed1227e0SMike Christie */
2253ed1227e0SMike Christie atomic_dec_mb(&dev->non_ordered);
2254ed1227e0SMike Christie }
2255019c4ca6SChristoph Hellwig
2256945a1607SMike Christie spin_lock_irq(&cmd->t_state_lock);
2257945a1607SMike Christie cmd->transport_state &= ~CMD_T_SENT;
2258945a1607SMike Christie spin_unlock_irq(&cmd->t_state_lock);
2259945a1607SMike Christie
2260019c4ca6SChristoph Hellwig spin_lock(&dev->delayed_cmd_lock);
2261019c4ca6SChristoph Hellwig list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
2262019c4ca6SChristoph Hellwig spin_unlock(&dev->delayed_cmd_lock);
2263019c4ca6SChristoph Hellwig
22649c31820bSRoland Dreier pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to delayed CMD listn",
22659c31820bSRoland Dreier cmd->t_task_cdb[0], cmd->sam_task_attr);
2266ed1227e0SMike Christie /*
2267ed1227e0SMike Christie * We may have no non ordered cmds when this function started or we
2268ed1227e0SMike Christie * could have raced with the last simple/head cmd completing, so kick
2269ed1227e0SMike Christie * the delayed handler here.
2270ed1227e0SMike Christie */
2271ed1227e0SMike Christie schedule_work(&dev->delayed_cmd_work);
2272019c4ca6SChristoph Hellwig return true;
2273019c4ca6SChristoph Hellwig }
22745f41a31dSChristoph Hellwig
target_execute_cmd(struct se_cmd * cmd)22755f41a31dSChristoph Hellwig void target_execute_cmd(struct se_cmd *cmd)
22765f41a31dSChristoph Hellwig {
2277f7113a47SChristoph Hellwig /*
2278f7113a47SChristoph Hellwig * Determine if frontend context caller is requesting the stopping of
2279f7113a47SChristoph Hellwig * this command for frontend exceptions.
2280310d3d31SNicholas Bellinger *
22814240d448SBart Van Assche * If the received CDB has already been aborted stop processing it here.
2282f7113a47SChristoph Hellwig */
22832c9fa49eSBart Van Assche if (target_cmd_interrupted(cmd))
22842c9fa49eSBart Van Assche return;
22852c9fa49eSBart Van Assche
22864a9a6c8dSNicholas Bellinger spin_lock_irq(&cmd->t_state_lock);
2287f7113a47SChristoph Hellwig cmd->t_state = TRANSPORT_PROCESSING;
2288fd5e64deSBart Van Assche cmd->transport_state |= CMD_T_ACTIVE | CMD_T_SENT;
2289f7113a47SChristoph Hellwig spin_unlock_irq(&cmd->t_state_lock);
2290aa58b531SNicholas Bellinger
2291aa58b531SNicholas Bellinger if (target_write_prot_action(cmd))
2292aa58b531SNicholas Bellinger return;
22935f41a31dSChristoph Hellwig
2294945a1607SMike Christie if (target_handle_task_attr(cmd))
22951a398b97SNicholas Bellinger return;
22961a398b97SNicholas Bellinger
2297dff0ca9eSNicholas Bellinger __target_execute_cmd(cmd, true);
2298c66ac9dbSNicholas Bellinger }
229970baf0abSChristoph Hellwig EXPORT_SYMBOL(target_execute_cmd);
2300c66ac9dbSNicholas Bellinger
2301c66ac9dbSNicholas Bellinger /*
23025f41a31dSChristoph Hellwig * Process all commands up to the last received ORDERED task attribute which
23035f41a31dSChristoph Hellwig * requires another blocking boundary
23045f41a31dSChristoph Hellwig */
target_do_delayed_work(struct work_struct * work)2305ed1227e0SMike Christie void target_do_delayed_work(struct work_struct *work)
23065f41a31dSChristoph Hellwig {
2307ed1227e0SMike Christie struct se_device *dev = container_of(work, struct se_device,
2308ed1227e0SMike Christie delayed_cmd_work);
23095f41a31dSChristoph Hellwig
23105f41a31dSChristoph Hellwig spin_lock(&dev->delayed_cmd_lock);
2311ed1227e0SMike Christie while (!dev->ordered_sync_in_progress) {
2312ed1227e0SMike Christie struct se_cmd *cmd;
2313ed1227e0SMike Christie
2314ed1227e0SMike Christie if (list_empty(&dev->delayed_cmd_list))
23155f41a31dSChristoph Hellwig break;
23165f41a31dSChristoph Hellwig
23175f41a31dSChristoph Hellwig cmd = list_entry(dev->delayed_cmd_list.next,
23185f41a31dSChristoph Hellwig struct se_cmd, se_delayed_node);
2319ed1227e0SMike Christie
2320ed1227e0SMike Christie if (cmd->sam_task_attr == TCM_ORDERED_TAG) {
2321ed1227e0SMike Christie /*
2322ed1227e0SMike Christie * Check if we started with:
2323ed1227e0SMike Christie * [ordered] [simple] [ordered]
2324ed1227e0SMike Christie * and we are now at the last ordered so we have to wait
2325ed1227e0SMike Christie * for the simple cmd.
2326ed1227e0SMike Christie */
2327ed1227e0SMike Christie if (atomic_read(&dev->non_ordered) > 0)
2328ed1227e0SMike Christie break;
2329ed1227e0SMike Christie
2330ed1227e0SMike Christie dev->ordered_sync_in_progress = true;
2331ed1227e0SMike Christie }
2332ed1227e0SMike Christie
23335f41a31dSChristoph Hellwig list_del(&cmd->se_delayed_node);
2334ed1227e0SMike Christie atomic_dec_mb(&dev->delayed_cmd_count);
23355f41a31dSChristoph Hellwig spin_unlock(&dev->delayed_cmd_lock);
23365f41a31dSChristoph Hellwig
2337ed1227e0SMike Christie if (cmd->sam_task_attr != TCM_ORDERED_TAG)
2338ed1227e0SMike Christie atomic_inc_mb(&dev->non_ordered);
2339ed1227e0SMike Christie
23401c79df1fSNicholas Bellinger cmd->transport_state |= CMD_T_SENT;
23411c79df1fSNicholas Bellinger
2342dff0ca9eSNicholas Bellinger __target_execute_cmd(cmd, true);
23435f41a31dSChristoph Hellwig
2344ed1227e0SMike Christie spin_lock(&dev->delayed_cmd_lock);
23455f41a31dSChristoph Hellwig }
2346ed1227e0SMike Christie spin_unlock(&dev->delayed_cmd_lock);
23475f41a31dSChristoph Hellwig }
23485f41a31dSChristoph Hellwig
23495f41a31dSChristoph Hellwig /*
235035e0e757SChristoph Hellwig * Called from I/O completion to determine which dormant/delayed
2351c66ac9dbSNicholas Bellinger * and ordered cmds need to have their tasks added to the execution queue.
2352c66ac9dbSNicholas Bellinger */
transport_complete_task_attr(struct se_cmd * cmd)2353c66ac9dbSNicholas Bellinger static void transport_complete_task_attr(struct se_cmd *cmd)
2354c66ac9dbSNicholas Bellinger {
23555951146dSAndy Grover struct se_device *dev = cmd->se_dev;
2356c66ac9dbSNicholas Bellinger
235769088a04SBodo Stroesser if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
2358019c4ca6SChristoph Hellwig return;
2359019c4ca6SChristoph Hellwig
2360410c29dfSNicholas Bellinger if (!(cmd->se_cmd_flags & SCF_TASK_ATTR_SET))
2361410c29dfSNicholas Bellinger goto restart;
2362410c29dfSNicholas Bellinger
236368d81f40SChristoph Hellwig if (cmd->sam_task_attr == TCM_SIMPLE_TAG) {
2364ed1227e0SMike Christie atomic_dec_mb(&dev->non_ordered);
2365c66ac9dbSNicholas Bellinger dev->dev_cur_ordered_id++;
236668d81f40SChristoph Hellwig } else if (cmd->sam_task_attr == TCM_HEAD_TAG) {
2367ed1227e0SMike Christie atomic_dec_mb(&dev->non_ordered);
2368c66ac9dbSNicholas Bellinger dev->dev_cur_ordered_id++;
23699c31820bSRoland Dreier pr_debug("Incremented dev_cur_ordered_id: %u for HEAD_OF_QUEUE\n",
23709c31820bSRoland Dreier dev->dev_cur_ordered_id);
237168d81f40SChristoph Hellwig } else if (cmd->sam_task_attr == TCM_ORDERED_TAG) {
2372ed1227e0SMike Christie spin_lock(&dev->delayed_cmd_lock);
2373ed1227e0SMike Christie dev->ordered_sync_in_progress = false;
2374ed1227e0SMike Christie spin_unlock(&dev->delayed_cmd_lock);
2375c66ac9dbSNicholas Bellinger
2376c66ac9dbSNicholas Bellinger dev->dev_cur_ordered_id++;
23779c31820bSRoland Dreier pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED\n",
23789c31820bSRoland Dreier dev->dev_cur_ordered_id);
2379c66ac9dbSNicholas Bellinger }
23801c79df1fSNicholas Bellinger cmd->se_cmd_flags &= ~SCF_TASK_ATTR_SET;
23811c79df1fSNicholas Bellinger
2382410c29dfSNicholas Bellinger restart:
2383ed1227e0SMike Christie if (atomic_read(&dev->delayed_cmd_count) > 0)
2384ed1227e0SMike Christie schedule_work(&dev->delayed_cmd_work);
2385c66ac9dbSNicholas Bellinger }
2386c66ac9dbSNicholas Bellinger
transport_complete_qf(struct se_cmd * cmd)2387e057f533SChristoph Hellwig static void transport_complete_qf(struct se_cmd *cmd)
238807bde79aSNicholas Bellinger {
238907bde79aSNicholas Bellinger int ret = 0;
239007bde79aSNicholas Bellinger
2391e057f533SChristoph Hellwig transport_complete_task_attr(cmd);
2392fa7e25cfSNicholas Bellinger /*
2393fa7e25cfSNicholas Bellinger * If a fabric driver ->write_pending() or ->queue_data_in() callback
2394fa7e25cfSNicholas Bellinger * has returned neither -ENOMEM or -EAGAIN, assume it's fatal and
2395fa7e25cfSNicholas Bellinger * the same callbacks should not be retried. Return CHECK_CONDITION
2396fa7e25cfSNicholas Bellinger * if a scsi_status is not already set.
2397fa7e25cfSNicholas Bellinger *
2398fa7e25cfSNicholas Bellinger * If a fabric driver ->queue_status() has returned non zero, always
2399fa7e25cfSNicholas Bellinger * keep retrying no matter what..
2400fa7e25cfSNicholas Bellinger */
2401fa7e25cfSNicholas Bellinger if (cmd->t_state == TRANSPORT_COMPLETE_QF_ERR) {
2402fa7e25cfSNicholas Bellinger if (cmd->scsi_status)
2403fa7e25cfSNicholas Bellinger goto queue_status;
2404e057f533SChristoph Hellwig
2405fa7e25cfSNicholas Bellinger translate_sense_reason(cmd, TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE);
2406fa7e25cfSNicholas Bellinger goto queue_status;
2407e057f533SChristoph Hellwig }
240807bde79aSNicholas Bellinger
2409bd813720SLee Duncan /*
2410bd813720SLee Duncan * Check if we need to send a sense buffer from
2411bd813720SLee Duncan * the struct se_cmd in question. We do NOT want
2412bd813720SLee Duncan * to take this path of the IO has been marked as
2413bd813720SLee Duncan * needing to be treated like a "normal read". This
2414bd813720SLee Duncan * is the case if it's a tape read, and either the
2415bd813720SLee Duncan * FM, EOM, or ILI bits are set, but there is no
2416bd813720SLee Duncan * sense data.
2417bd813720SLee Duncan */
2418bd813720SLee Duncan if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) &&
2419bd813720SLee Duncan cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
2420fa7e25cfSNicholas Bellinger goto queue_status;
2421fa7e25cfSNicholas Bellinger
242207bde79aSNicholas Bellinger switch (cmd->data_direction) {
242307bde79aSNicholas Bellinger case DMA_FROM_DEVICE:
2424bd813720SLee Duncan /* queue status if not treating this as a normal read */
2425bd813720SLee Duncan if (cmd->scsi_status &&
2426bd813720SLee Duncan !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL))
24274347ab5aSNicholas Bellinger goto queue_status;
24284347ab5aSNicholas Bellinger
2429e5c0d6adSRoland Dreier trace_target_cmd_complete(cmd);
243007bde79aSNicholas Bellinger ret = cmd->se_tfo->queue_data_in(cmd);
243107bde79aSNicholas Bellinger break;
243207bde79aSNicholas Bellinger case DMA_TO_DEVICE:
243364577407SNicholas Bellinger if (cmd->se_cmd_flags & SCF_BIDI) {
243407bde79aSNicholas Bellinger ret = cmd->se_tfo->queue_data_in(cmd);
2435e057f533SChristoph Hellwig break;
243607bde79aSNicholas Bellinger }
2437df561f66SGustavo A. R. Silva fallthrough;
243807bde79aSNicholas Bellinger case DMA_NONE:
24394347ab5aSNicholas Bellinger queue_status:
2440e5c0d6adSRoland Dreier trace_target_cmd_complete(cmd);
244107bde79aSNicholas Bellinger ret = cmd->se_tfo->queue_status(cmd);
244207bde79aSNicholas Bellinger break;
244307bde79aSNicholas Bellinger default:
244407bde79aSNicholas Bellinger break;
244507bde79aSNicholas Bellinger }
244607bde79aSNicholas Bellinger
2447e057f533SChristoph Hellwig if (ret < 0) {
2448fa7e25cfSNicholas Bellinger transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
2449e057f533SChristoph Hellwig return;
2450e057f533SChristoph Hellwig }
2451c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
2452e057f533SChristoph Hellwig transport_cmd_check_stop_to_fabric(cmd);
245307bde79aSNicholas Bellinger }
245407bde79aSNicholas Bellinger
transport_handle_queue_full(struct se_cmd * cmd,struct se_device * dev,int err,bool write_pending)2455fa7e25cfSNicholas Bellinger static void transport_handle_queue_full(struct se_cmd *cmd, struct se_device *dev,
2456fa7e25cfSNicholas Bellinger int err, bool write_pending)
245707bde79aSNicholas Bellinger {
2458fa7e25cfSNicholas Bellinger /*
2459fa7e25cfSNicholas Bellinger * -EAGAIN or -ENOMEM signals retry of ->write_pending() and/or
2460fa7e25cfSNicholas Bellinger * ->queue_data_in() callbacks from new process context.
2461fa7e25cfSNicholas Bellinger *
2462fa7e25cfSNicholas Bellinger * Otherwise for other errors, transport_complete_qf() will send
2463fa7e25cfSNicholas Bellinger * CHECK_CONDITION via ->queue_status() instead of attempting to
2464fa7e25cfSNicholas Bellinger * retry associated fabric driver data-transfer callbacks.
2465fa7e25cfSNicholas Bellinger */
2466fa7e25cfSNicholas Bellinger if (err == -EAGAIN || err == -ENOMEM) {
2467fa7e25cfSNicholas Bellinger cmd->t_state = (write_pending) ? TRANSPORT_COMPLETE_QF_WP :
2468fa7e25cfSNicholas Bellinger TRANSPORT_COMPLETE_QF_OK;
2469fa7e25cfSNicholas Bellinger } else {
2470fa7e25cfSNicholas Bellinger pr_warn_ratelimited("Got unknown fabric queue status: %d\n", err);
2471fa7e25cfSNicholas Bellinger cmd->t_state = TRANSPORT_COMPLETE_QF_ERR;
2472fa7e25cfSNicholas Bellinger }
2473fa7e25cfSNicholas Bellinger
247407bde79aSNicholas Bellinger spin_lock_irq(&dev->qf_cmd_lock);
247507bde79aSNicholas Bellinger list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
247633940d09SJoern Engel atomic_inc_mb(&dev->dev_qf_count);
247707bde79aSNicholas Bellinger spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
247807bde79aSNicholas Bellinger
247907bde79aSNicholas Bellinger schedule_work(&cmd->se_dev->qf_work_queue);
248007bde79aSNicholas Bellinger }
248107bde79aSNicholas Bellinger
target_read_prot_action(struct se_cmd * cmd)2482fdeab852SNicholas Bellinger static bool target_read_prot_action(struct se_cmd *cmd)
2483bc005869SNicholas Bellinger {
2484fdeab852SNicholas Bellinger switch (cmd->prot_op) {
2485fdeab852SNicholas Bellinger case TARGET_PROT_DIN_STRIP:
2486bc005869SNicholas Bellinger if (!(cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_STRIP)) {
2487f75b6faeSSagi Grimberg u32 sectors = cmd->data_length >>
2488f75b6faeSSagi Grimberg ilog2(cmd->se_dev->dev_attrib.block_size);
2489f75b6faeSSagi Grimberg
2490f75b6faeSSagi Grimberg cmd->pi_err = sbc_dif_verify(cmd, cmd->t_task_lba,
2491f75b6faeSSagi Grimberg sectors, 0, cmd->t_prot_sg,
2492f75b6faeSSagi Grimberg 0);
2493f75b6faeSSagi Grimberg if (cmd->pi_err)
2494bc005869SNicholas Bellinger return true;
2495bc005869SNicholas Bellinger }
2496fdeab852SNicholas Bellinger break;
249772c03850SNicholas Bellinger case TARGET_PROT_DIN_INSERT:
249872c03850SNicholas Bellinger if (cmd->se_sess->sup_prot_ops & TARGET_PROT_DIN_INSERT)
249972c03850SNicholas Bellinger break;
250072c03850SNicholas Bellinger
250172c03850SNicholas Bellinger sbc_dif_generate(cmd);
250272c03850SNicholas Bellinger break;
2503fdeab852SNicholas Bellinger default:
2504fdeab852SNicholas Bellinger break;
2505fdeab852SNicholas Bellinger }
2506bc005869SNicholas Bellinger
2507bc005869SNicholas Bellinger return false;
2508bc005869SNicholas Bellinger }
2509bc005869SNicholas Bellinger
target_complete_ok_work(struct work_struct * work)251035e0e757SChristoph Hellwig static void target_complete_ok_work(struct work_struct *work)
2511c66ac9dbSNicholas Bellinger {
251235e0e757SChristoph Hellwig struct se_cmd *cmd = container_of(work, struct se_cmd, work);
251327a27099SPaolo Bonzini int ret;
251435e0e757SChristoph Hellwig
2515c66ac9dbSNicholas Bellinger /*
2516c66ac9dbSNicholas Bellinger * Check if we need to move delayed/dormant tasks from cmds on the
2517c66ac9dbSNicholas Bellinger * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
2518c66ac9dbSNicholas Bellinger * Attribute.
2519c66ac9dbSNicholas Bellinger */
2520c66ac9dbSNicholas Bellinger transport_complete_task_attr(cmd);
2521019c4ca6SChristoph Hellwig
2522c66ac9dbSNicholas Bellinger /*
252307bde79aSNicholas Bellinger * Check to schedule QUEUE_FULL work, or execute an existing
252407bde79aSNicholas Bellinger * cmd->transport_qf_callback()
252507bde79aSNicholas Bellinger */
252607bde79aSNicholas Bellinger if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
252707bde79aSNicholas Bellinger schedule_work(&cmd->se_dev->qf_work_queue);
252807bde79aSNicholas Bellinger
252907bde79aSNicholas Bellinger /*
2530d5829eacSPaolo Bonzini * Check if we need to send a sense buffer from
2531bd813720SLee Duncan * the struct se_cmd in question. We do NOT want
2532bd813720SLee Duncan * to take this path of the IO has been marked as
2533bd813720SLee Duncan * needing to be treated like a "normal read". This
2534bd813720SLee Duncan * is the case if it's a tape read, and either the
2535bd813720SLee Duncan * FM, EOM, or ILI bits are set, but there is no
2536bd813720SLee Duncan * sense data.
2537c66ac9dbSNicholas Bellinger */
2538bd813720SLee Duncan if (!(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL) &&
2539bd813720SLee Duncan cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
254027a27099SPaolo Bonzini WARN_ON(!cmd->scsi_status);
254107bde79aSNicholas Bellinger ret = transport_send_check_condition_and_sense(
254227a27099SPaolo Bonzini cmd, 0, 1);
2543fa7e25cfSNicholas Bellinger if (ret)
254407bde79aSNicholas Bellinger goto queue_full;
254507bde79aSNicholas Bellinger
2546c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
2547c66ac9dbSNicholas Bellinger transport_cmd_check_stop_to_fabric(cmd);
2548c66ac9dbSNicholas Bellinger return;
2549c66ac9dbSNicholas Bellinger }
2550c66ac9dbSNicholas Bellinger /*
255125985edcSLucas De Marchi * Check for a callback, used by amongst other things
2552a6b0133cSNicholas Bellinger * XDWRITE_READ_10 and COMPARE_AND_WRITE emulation.
2553c66ac9dbSNicholas Bellinger */
2554a6b0133cSNicholas Bellinger if (cmd->transport_complete_callback) {
2555a6b0133cSNicholas Bellinger sense_reason_t rc;
2556057085e5SNicholas Bellinger bool caw = (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE);
2557057085e5SNicholas Bellinger bool zero_dl = !(cmd->data_length);
2558057085e5SNicholas Bellinger int post_ret = 0;
2559a6b0133cSNicholas Bellinger
2560057085e5SNicholas Bellinger rc = cmd->transport_complete_callback(cmd, true, &post_ret);
2561057085e5SNicholas Bellinger if (!rc && !post_ret) {
2562057085e5SNicholas Bellinger if (caw && zero_dl)
2563c8e63985SNicholas Bellinger goto queue_rsp;
2564c8e63985SNicholas Bellinger
2565a6b0133cSNicholas Bellinger return;
2566a2890087SNicholas Bellinger } else if (rc) {
2567a6b0133cSNicholas Bellinger ret = transport_send_check_condition_and_sense(cmd,
2568a6b0133cSNicholas Bellinger rc, 0);
2569fa7e25cfSNicholas Bellinger if (ret)
2570a6b0133cSNicholas Bellinger goto queue_full;
2571a6b0133cSNicholas Bellinger
2572c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
2573a6b0133cSNicholas Bellinger transport_cmd_check_stop_to_fabric(cmd);
2574a6b0133cSNicholas Bellinger return;
2575a6b0133cSNicholas Bellinger }
2576a2890087SNicholas Bellinger }
2577c66ac9dbSNicholas Bellinger
2578c8e63985SNicholas Bellinger queue_rsp:
2579c66ac9dbSNicholas Bellinger switch (cmd->data_direction) {
2580c66ac9dbSNicholas Bellinger case DMA_FROM_DEVICE:
2581bd813720SLee Duncan /*
2582bd813720SLee Duncan * if this is a READ-type IO, but SCSI status
2583bd813720SLee Duncan * is set, then skip returning data and just
2584bd813720SLee Duncan * return the status -- unless this IO is marked
2585bd813720SLee Duncan * as needing to be treated as a normal read,
2586bd813720SLee Duncan * in which case we want to go ahead and return
2587bd813720SLee Duncan * the data. This happens, for example, for tape
2588bd813720SLee Duncan * reads with the FM, EOM, or ILI bits set, with
2589bd813720SLee Duncan * no sense data.
2590bd813720SLee Duncan */
2591bd813720SLee Duncan if (cmd->scsi_status &&
2592bd813720SLee Duncan !(cmd->se_cmd_flags & SCF_TREAT_READ_AS_NORMAL))
25934347ab5aSNicholas Bellinger goto queue_status;
25944347ab5aSNicholas Bellinger
25954cc987eaSNicholas Bellinger atomic_long_add(cmd->data_length,
25964cc987eaSNicholas Bellinger &cmd->se_lun->lun_stats.tx_data_octets);
2597bc005869SNicholas Bellinger /*
2598bc005869SNicholas Bellinger * Perform READ_STRIP of PI using software emulation when
2599bc005869SNicholas Bellinger * backend had PI enabled, if the transport will not be
2600bc005869SNicholas Bellinger * performing hardware READ_STRIP offload.
2601bc005869SNicholas Bellinger */
2602fdeab852SNicholas Bellinger if (target_read_prot_action(cmd)) {
2603bc005869SNicholas Bellinger ret = transport_send_check_condition_and_sense(cmd,
2604bc005869SNicholas Bellinger cmd->pi_err, 0);
2605fa7e25cfSNicholas Bellinger if (ret)
2606bc005869SNicholas Bellinger goto queue_full;
2607bc005869SNicholas Bellinger
2608c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
2609bc005869SNicholas Bellinger transport_cmd_check_stop_to_fabric(cmd);
2610bc005869SNicholas Bellinger return;
2611bc005869SNicholas Bellinger }
2612c66ac9dbSNicholas Bellinger
2613e5c0d6adSRoland Dreier trace_target_cmd_complete(cmd);
261407bde79aSNicholas Bellinger ret = cmd->se_tfo->queue_data_in(cmd);
2615fa7e25cfSNicholas Bellinger if (ret)
261607bde79aSNicholas Bellinger goto queue_full;
2617c66ac9dbSNicholas Bellinger break;
2618c66ac9dbSNicholas Bellinger case DMA_TO_DEVICE:
26194cc987eaSNicholas Bellinger atomic_long_add(cmd->data_length,
26204cc987eaSNicholas Bellinger &cmd->se_lun->lun_stats.rx_data_octets);
2621c66ac9dbSNicholas Bellinger /*
2622c66ac9dbSNicholas Bellinger * Check if we need to send READ payload for BIDI-COMMAND
2623c66ac9dbSNicholas Bellinger */
262464577407SNicholas Bellinger if (cmd->se_cmd_flags & SCF_BIDI) {
26254cc987eaSNicholas Bellinger atomic_long_add(cmd->data_length,
26264cc987eaSNicholas Bellinger &cmd->se_lun->lun_stats.tx_data_octets);
262707bde79aSNicholas Bellinger ret = cmd->se_tfo->queue_data_in(cmd);
2628fa7e25cfSNicholas Bellinger if (ret)
262907bde79aSNicholas Bellinger goto queue_full;
2630c66ac9dbSNicholas Bellinger break;
2631c66ac9dbSNicholas Bellinger }
2632df561f66SGustavo A. R. Silva fallthrough;
2633c66ac9dbSNicholas Bellinger case DMA_NONE:
26344347ab5aSNicholas Bellinger queue_status:
2635e5c0d6adSRoland Dreier trace_target_cmd_complete(cmd);
263607bde79aSNicholas Bellinger ret = cmd->se_tfo->queue_status(cmd);
2637fa7e25cfSNicholas Bellinger if (ret)
263807bde79aSNicholas Bellinger goto queue_full;
2639c66ac9dbSNicholas Bellinger break;
2640c66ac9dbSNicholas Bellinger default:
2641c66ac9dbSNicholas Bellinger break;
2642c66ac9dbSNicholas Bellinger }
2643c66ac9dbSNicholas Bellinger
2644c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
2645c66ac9dbSNicholas Bellinger transport_cmd_check_stop_to_fabric(cmd);
264607bde79aSNicholas Bellinger return;
264707bde79aSNicholas Bellinger
264807bde79aSNicholas Bellinger queue_full:
26496708bb27SAndy Grover pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
265007bde79aSNicholas Bellinger " data_direction: %d\n", cmd, cmd->data_direction);
2651fa7e25cfSNicholas Bellinger
2652fa7e25cfSNicholas Bellinger transport_handle_queue_full(cmd, cmd->se_dev, ret, false);
2653c66ac9dbSNicholas Bellinger }
2654c66ac9dbSNicholas Bellinger
target_free_sgl(struct scatterlist * sgl,int nents)2655e64aa657SChristoph Hellwig void target_free_sgl(struct scatterlist *sgl, int nents)
2656c66ac9dbSNicholas Bellinger {
26578c7a8d1cSBart Van Assche sgl_free_n_order(sgl, nents, 0);
2658c66ac9dbSNicholas Bellinger }
2659e64aa657SChristoph Hellwig EXPORT_SYMBOL(target_free_sgl);
26606708bb27SAndy Grover
transport_reset_sgl_orig(struct se_cmd * cmd)266147e459e6SNicholas Bellinger static inline void transport_reset_sgl_orig(struct se_cmd *cmd)
266247e459e6SNicholas Bellinger {
266347e459e6SNicholas Bellinger /*
266447e459e6SNicholas Bellinger * Check for saved t_data_sg that may be used for COMPARE_AND_WRITE
266547e459e6SNicholas Bellinger * emulation, and free + reset pointers if necessary..
266647e459e6SNicholas Bellinger */
266747e459e6SNicholas Bellinger if (!cmd->t_data_sg_orig)
266847e459e6SNicholas Bellinger return;
266947e459e6SNicholas Bellinger
267047e459e6SNicholas Bellinger kfree(cmd->t_data_sg);
267147e459e6SNicholas Bellinger cmd->t_data_sg = cmd->t_data_sg_orig;
267247e459e6SNicholas Bellinger cmd->t_data_sg_orig = NULL;
267347e459e6SNicholas Bellinger cmd->t_data_nents = cmd->t_data_nents_orig;
267447e459e6SNicholas Bellinger cmd->t_data_nents_orig = 0;
267547e459e6SNicholas Bellinger }
267647e459e6SNicholas Bellinger
transport_free_pages(struct se_cmd * cmd)26776708bb27SAndy Grover static inline void transport_free_pages(struct se_cmd *cmd)
26786708bb27SAndy Grover {
26795835812fSAkinobu Mita if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
2680e64aa657SChristoph Hellwig target_free_sgl(cmd->t_prot_sg, cmd->t_prot_nents);
26815835812fSAkinobu Mita cmd->t_prot_sg = NULL;
26825835812fSAkinobu Mita cmd->t_prot_nents = 0;
26835835812fSAkinobu Mita }
26845835812fSAkinobu Mita
268547e459e6SNicholas Bellinger if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) {
2686c8e63985SNicholas Bellinger /*
2687c8e63985SNicholas Bellinger * Release special case READ buffer payload required for
2688c8e63985SNicholas Bellinger * SG_TO_MEM_NOALLOC to function with COMPARE_AND_WRITE
2689c8e63985SNicholas Bellinger */
2690c8e63985SNicholas Bellinger if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) {
2691e64aa657SChristoph Hellwig target_free_sgl(cmd->t_bidi_data_sg,
2692c8e63985SNicholas Bellinger cmd->t_bidi_data_nents);
2693c8e63985SNicholas Bellinger cmd->t_bidi_data_sg = NULL;
2694c8e63985SNicholas Bellinger cmd->t_bidi_data_nents = 0;
2695c8e63985SNicholas Bellinger }
269647e459e6SNicholas Bellinger transport_reset_sgl_orig(cmd);
26976708bb27SAndy Grover return;
269847e459e6SNicholas Bellinger }
269947e459e6SNicholas Bellinger transport_reset_sgl_orig(cmd);
27006708bb27SAndy Grover
2701e64aa657SChristoph Hellwig target_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
2702ec98f782SAndy Grover cmd->t_data_sg = NULL;
2703ec98f782SAndy Grover cmd->t_data_nents = 0;
2704c66ac9dbSNicholas Bellinger
2705e64aa657SChristoph Hellwig target_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
2706ec98f782SAndy Grover cmd->t_bidi_data_sg = NULL;
2707ec98f782SAndy Grover cmd->t_bidi_data_nents = 0;
2708c66ac9dbSNicholas Bellinger }
2709c66ac9dbSNicholas Bellinger
transport_kmap_data_sg(struct se_cmd * cmd)27104949314cSAndy Grover void *transport_kmap_data_sg(struct se_cmd *cmd)
2711c66ac9dbSNicholas Bellinger {
2712ec98f782SAndy Grover struct scatterlist *sg = cmd->t_data_sg;
27134949314cSAndy Grover struct page **pages;
27144949314cSAndy Grover int i;
2715c66ac9dbSNicholas Bellinger
271605d1c7c0SAndy Grover /*
2717ec98f782SAndy Grover * We need to take into account a possible offset here for fabrics like
2718ec98f782SAndy Grover * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2719ec98f782SAndy Grover * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
272005d1c7c0SAndy Grover */
27214949314cSAndy Grover if (!cmd->t_data_nents)
27224949314cSAndy Grover return NULL;
27233717ef0cSPaolo Bonzini
27243717ef0cSPaolo Bonzini BUG_ON(!sg);
27253717ef0cSPaolo Bonzini if (cmd->t_data_nents == 1)
2726ec98f782SAndy Grover return kmap(sg_page(sg)) + sg->offset;
272705d1c7c0SAndy Grover
27284949314cSAndy Grover /* >1 page. use vmap */
2729df6751f3SMarkus Elfring pages = kmalloc_array(cmd->t_data_nents, sizeof(*pages), GFP_KERNEL);
2730de103c93SChristoph Hellwig if (!pages)
27314949314cSAndy Grover return NULL;
27324949314cSAndy Grover
27334949314cSAndy Grover /* convert sg[] to pages[] */
27344949314cSAndy Grover for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
27354949314cSAndy Grover pages[i] = sg_page(sg);
273605d1c7c0SAndy Grover }
27374949314cSAndy Grover
27384949314cSAndy Grover cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
27394949314cSAndy Grover kfree(pages);
2740de103c93SChristoph Hellwig if (!cmd->t_data_vmap)
27414949314cSAndy Grover return NULL;
27424949314cSAndy Grover
27434949314cSAndy Grover return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
27444949314cSAndy Grover }
27454949314cSAndy Grover EXPORT_SYMBOL(transport_kmap_data_sg);
27464949314cSAndy Grover
transport_kunmap_data_sg(struct se_cmd * cmd)27474949314cSAndy Grover void transport_kunmap_data_sg(struct se_cmd *cmd)
27484949314cSAndy Grover {
2749a1edf9cfSAndy Grover if (!cmd->t_data_nents) {
27504949314cSAndy Grover return;
2751a1edf9cfSAndy Grover } else if (cmd->t_data_nents == 1) {
27524949314cSAndy Grover kunmap(sg_page(cmd->t_data_sg));
2753a1edf9cfSAndy Grover return;
2754a1edf9cfSAndy Grover }
27554949314cSAndy Grover
27564949314cSAndy Grover vunmap(cmd->t_data_vmap);
27574949314cSAndy Grover cmd->t_data_vmap = NULL;
27584949314cSAndy Grover }
27594949314cSAndy Grover EXPORT_SYMBOL(transport_kunmap_data_sg);
276005d1c7c0SAndy Grover
2761c5ff8d6bSNicholas Bellinger int
target_alloc_sgl(struct scatterlist ** sgl,unsigned int * nents,u32 length,bool zero_page,bool chainable)276220093994SNicholas Bellinger target_alloc_sgl(struct scatterlist **sgl, unsigned int *nents, u32 length,
2763e64aa657SChristoph Hellwig bool zero_page, bool chainable)
276405d1c7c0SAndy Grover {
276514db4917SBart Van Assche gfp_t gfp = GFP_KERNEL | (zero_page ? __GFP_ZERO : 0);
276605d1c7c0SAndy Grover
276714db4917SBart Van Assche *sgl = sgl_alloc_order(length, 0, chainable, gfp, nents);
276814db4917SBart Van Assche return *sgl ? 0 : -ENOMEM;
2769c66ac9dbSNicholas Bellinger }
2770e64aa657SChristoph Hellwig EXPORT_SYMBOL(target_alloc_sgl);
2771c66ac9dbSNicholas Bellinger
2772a1d8b49aSAndy Grover /*
2773b16a35b0SAndy Grover * Allocate any required resources to execute the command. For writes we
2774b16a35b0SAndy Grover * might not have the payload yet, so notify the fabric via a call to
2775b16a35b0SAndy Grover * ->write_pending instead. Otherwise place it on the execution queue.
2776c66ac9dbSNicholas Bellinger */
2777de103c93SChristoph Hellwig sense_reason_t
transport_generic_new_cmd(struct se_cmd * cmd)2778de103c93SChristoph Hellwig transport_generic_new_cmd(struct se_cmd *cmd)
2779c66ac9dbSNicholas Bellinger {
2780b1a2ecdaSBart Van Assche unsigned long flags;
2781c66ac9dbSNicholas Bellinger int ret = 0;
2782c8e63985SNicholas Bellinger bool zero_flag = !(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB);
2783c66ac9dbSNicholas Bellinger
27845835812fSAkinobu Mita if (cmd->prot_op != TARGET_PROT_NORMAL &&
27855835812fSAkinobu Mita !(cmd->se_cmd_flags & SCF_PASSTHROUGH_PROT_SG_TO_MEM_NOALLOC)) {
27865835812fSAkinobu Mita ret = target_alloc_sgl(&cmd->t_prot_sg, &cmd->t_prot_nents,
2787e64aa657SChristoph Hellwig cmd->prot_length, true, false);
27885835812fSAkinobu Mita if (ret < 0)
27895835812fSAkinobu Mita return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
27905835812fSAkinobu Mita }
27915835812fSAkinobu Mita
2792c66ac9dbSNicholas Bellinger /*
27934240d448SBart Van Assche * Determine if the TCM fabric module has already allocated physical
2794c66ac9dbSNicholas Bellinger * memory, and is directly calling transport_generic_map_mem_to_cmd()
2795ec98f782SAndy Grover * beforehand.
2796c66ac9dbSNicholas Bellinger */
2797ec98f782SAndy Grover if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2798ec98f782SAndy Grover cmd->data_length) {
279920093994SNicholas Bellinger
28008cefe07bSNicholas Bellinger if ((cmd->se_cmd_flags & SCF_BIDI) ||
28018cefe07bSNicholas Bellinger (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)) {
28028cefe07bSNicholas Bellinger u32 bidi_length;
28038cefe07bSNicholas Bellinger
28048cefe07bSNicholas Bellinger if (cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE)
28058cefe07bSNicholas Bellinger bidi_length = cmd->t_task_nolb *
28068cefe07bSNicholas Bellinger cmd->se_dev->dev_attrib.block_size;
28078cefe07bSNicholas Bellinger else
28088cefe07bSNicholas Bellinger bidi_length = cmd->data_length;
28098cefe07bSNicholas Bellinger
28108cefe07bSNicholas Bellinger ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
28118cefe07bSNicholas Bellinger &cmd->t_bidi_data_nents,
2812e64aa657SChristoph Hellwig bidi_length, zero_flag, false);
28138cefe07bSNicholas Bellinger if (ret < 0)
28148cefe07bSNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
28158cefe07bSNicholas Bellinger }
28168cefe07bSNicholas Bellinger
281720093994SNicholas Bellinger ret = target_alloc_sgl(&cmd->t_data_sg, &cmd->t_data_nents,
2818e64aa657SChristoph Hellwig cmd->data_length, zero_flag, false);
2819c66ac9dbSNicholas Bellinger if (ret < 0)
2820de103c93SChristoph Hellwig return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2821c8e63985SNicholas Bellinger } else if ((cmd->se_cmd_flags & SCF_COMPARE_AND_WRITE) &&
2822c8e63985SNicholas Bellinger cmd->data_length) {
2823c8e63985SNicholas Bellinger /*
2824c8e63985SNicholas Bellinger * Special case for COMPARE_AND_WRITE with fabrics
2825c8e63985SNicholas Bellinger * using SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC.
2826c8e63985SNicholas Bellinger */
2827c8e63985SNicholas Bellinger u32 caw_length = cmd->t_task_nolb *
2828c8e63985SNicholas Bellinger cmd->se_dev->dev_attrib.block_size;
2829c8e63985SNicholas Bellinger
2830c8e63985SNicholas Bellinger ret = target_alloc_sgl(&cmd->t_bidi_data_sg,
2831c8e63985SNicholas Bellinger &cmd->t_bidi_data_nents,
2832e64aa657SChristoph Hellwig caw_length, zero_flag, false);
2833c8e63985SNicholas Bellinger if (ret < 0)
2834c8e63985SNicholas Bellinger return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2835c66ac9dbSNicholas Bellinger }
2836c66ac9dbSNicholas Bellinger /*
2837c3196f0cSChristoph Hellwig * If this command is not a write we can execute it right here,
2838c3196f0cSChristoph Hellwig * for write buffers we need to notify the fabric driver first
2839c3196f0cSChristoph Hellwig * and let it call back once the write buffers are ready.
2840c66ac9dbSNicholas Bellinger */
28415f41a31dSChristoph Hellwig target_add_to_state_list(cmd);
2842885e7b0eSRoland Dreier if (cmd->data_direction != DMA_TO_DEVICE || cmd->data_length == 0) {
28435f41a31dSChristoph Hellwig target_execute_cmd(cmd);
2844c66ac9dbSNicholas Bellinger return 0;
2845c3196f0cSChristoph Hellwig }
2846b1a2ecdaSBart Van Assche
2847b1a2ecdaSBart Van Assche spin_lock_irqsave(&cmd->t_state_lock, flags);
2848b1a2ecdaSBart Van Assche cmd->t_state = TRANSPORT_WRITE_PENDING;
2849b1a2ecdaSBart Van Assche /*
2850b1a2ecdaSBart Van Assche * Determine if frontend context caller is requesting the stopping of
2851b1a2ecdaSBart Van Assche * this command for frontend exceptions.
2852b1a2ecdaSBart Van Assche */
2853fbbd4923SBart Van Assche if (cmd->transport_state & CMD_T_STOP &&
2854fbbd4923SBart Van Assche !cmd->se_tfo->write_pending_must_be_called) {
2855b1a2ecdaSBart Van Assche pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08llx\n",
2856b1a2ecdaSBart Van Assche __func__, __LINE__, cmd->tag);
2857b1a2ecdaSBart Van Assche
2858b1a2ecdaSBart Van Assche spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2859b1a2ecdaSBart Van Assche
2860b1a2ecdaSBart Van Assche complete_all(&cmd->t_transport_stop_comp);
2861a5eb307fSBart Van Assche return 0;
2862b1a2ecdaSBart Van Assche }
2863b1a2ecdaSBart Van Assche cmd->transport_state &= ~CMD_T_ACTIVE;
2864b1a2ecdaSBart Van Assche spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2865c3196f0cSChristoph Hellwig
2866c3196f0cSChristoph Hellwig ret = cmd->se_tfo->write_pending(cmd);
2867fa7e25cfSNicholas Bellinger if (ret)
2868c3196f0cSChristoph Hellwig goto queue_full;
2869c3196f0cSChristoph Hellwig
2870fa7e25cfSNicholas Bellinger return 0;
2871de103c93SChristoph Hellwig
2872c3196f0cSChristoph Hellwig queue_full:
2873c3196f0cSChristoph Hellwig pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
2874fa7e25cfSNicholas Bellinger transport_handle_queue_full(cmd, cmd->se_dev, ret, true);
2875c3196f0cSChristoph Hellwig return 0;
2876c66ac9dbSNicholas Bellinger }
2877a1d8b49aSAndy Grover EXPORT_SYMBOL(transport_generic_new_cmd);
2878c66ac9dbSNicholas Bellinger
transport_write_pending_qf(struct se_cmd * cmd)2879e057f533SChristoph Hellwig static void transport_write_pending_qf(struct se_cmd *cmd)
288007bde79aSNicholas Bellinger {
28819574a497SNicholas Bellinger unsigned long flags;
2882f147abb4SNicholas Bellinger int ret;
28839574a497SNicholas Bellinger bool stop;
28849574a497SNicholas Bellinger
28859574a497SNicholas Bellinger spin_lock_irqsave(&cmd->t_state_lock, flags);
28869574a497SNicholas Bellinger stop = (cmd->transport_state & (CMD_T_STOP | CMD_T_ABORTED));
28879574a497SNicholas Bellinger spin_unlock_irqrestore(&cmd->t_state_lock, flags);
28889574a497SNicholas Bellinger
28899574a497SNicholas Bellinger if (stop) {
28909574a497SNicholas Bellinger pr_debug("%s:%d CMD_T_STOP|CMD_T_ABORTED for ITT: 0x%08llx\n",
28919574a497SNicholas Bellinger __func__, __LINE__, cmd->tag);
28929574a497SNicholas Bellinger complete_all(&cmd->t_transport_stop_comp);
28939574a497SNicholas Bellinger return;
28949574a497SNicholas Bellinger }
2895f147abb4SNicholas Bellinger
2896f147abb4SNicholas Bellinger ret = cmd->se_tfo->write_pending(cmd);
2897fa7e25cfSNicholas Bellinger if (ret) {
2898e057f533SChristoph Hellwig pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2899e057f533SChristoph Hellwig cmd);
2900fa7e25cfSNicholas Bellinger transport_handle_queue_full(cmd, cmd->se_dev, ret, true);
2901e057f533SChristoph Hellwig }
290207bde79aSNicholas Bellinger }
290307bde79aSNicholas Bellinger
29040f4a9431SNicholas Bellinger static bool
29050f4a9431SNicholas Bellinger __transport_wait_for_tasks(struct se_cmd *, bool, bool *, bool *,
29060f4a9431SNicholas Bellinger unsigned long *flags);
29070f4a9431SNicholas Bellinger
target_wait_free_cmd(struct se_cmd * cmd,bool * aborted,bool * tas)29080f4a9431SNicholas Bellinger static void target_wait_free_cmd(struct se_cmd *cmd, bool *aborted, bool *tas)
29090f4a9431SNicholas Bellinger {
29100f4a9431SNicholas Bellinger unsigned long flags;
29110f4a9431SNicholas Bellinger
29120f4a9431SNicholas Bellinger spin_lock_irqsave(&cmd->t_state_lock, flags);
29130f4a9431SNicholas Bellinger __transport_wait_for_tasks(cmd, true, aborted, tas, &flags);
29140f4a9431SNicholas Bellinger spin_unlock_irqrestore(&cmd->t_state_lock, flags);
29150f4a9431SNicholas Bellinger }
29160f4a9431SNicholas Bellinger
2917953bcf7aSBart Van Assche /*
29182c9fa49eSBart Van Assche * Call target_put_sess_cmd() and wait until target_release_cmd_kref(@cmd) has
29192c9fa49eSBart Van Assche * finished.
29202c9fa49eSBart Van Assche */
target_put_cmd_and_wait(struct se_cmd * cmd)29212c9fa49eSBart Van Assche void target_put_cmd_and_wait(struct se_cmd *cmd)
29222c9fa49eSBart Van Assche {
29232c9fa49eSBart Van Assche DECLARE_COMPLETION_ONSTACK(compl);
29242c9fa49eSBart Van Assche
29252c9fa49eSBart Van Assche WARN_ON_ONCE(cmd->abrt_compl);
29262c9fa49eSBart Van Assche cmd->abrt_compl = &compl;
29272c9fa49eSBart Van Assche target_put_sess_cmd(cmd);
29282c9fa49eSBart Van Assche wait_for_completion(&compl);
29292c9fa49eSBart Van Assche }
29302c9fa49eSBart Van Assche
29312c9fa49eSBart Van Assche /*
2932953bcf7aSBart Van Assche * This function is called by frontend drivers after processing of a command
2933953bcf7aSBart Van Assche * has finished.
2934953bcf7aSBart Van Assche *
29352c9fa49eSBart Van Assche * The protocol for ensuring that either the regular frontend command
29362c9fa49eSBart Van Assche * processing flow or target_handle_abort() code drops one reference is as
29372c9fa49eSBart Van Assche * follows:
2938953bcf7aSBart Van Assche * - Calling .queue_data_in(), .queue_status() or queue_tm_rsp() will cause
29392c9fa49eSBart Van Assche * the frontend driver to call this function synchronously or asynchronously.
29402c9fa49eSBart Van Assche * That will cause one reference to be dropped.
2941953bcf7aSBart Van Assche * - During regular command processing the target core sets CMD_T_COMPLETE
2942953bcf7aSBart Van Assche * before invoking one of the .queue_*() functions.
2943953bcf7aSBart Van Assche * - The code that aborts commands skips commands and TMFs for which
2944953bcf7aSBart Van Assche * CMD_T_COMPLETE has been set.
2945953bcf7aSBart Van Assche * - CMD_T_ABORTED is set atomically after the CMD_T_COMPLETE check for
2946953bcf7aSBart Van Assche * commands that will be aborted.
2947953bcf7aSBart Van Assche * - If the CMD_T_ABORTED flag is set but CMD_T_TAS has not been set
2948953bcf7aSBart Van Assche * transport_generic_free_cmd() skips its call to target_put_sess_cmd().
2949953bcf7aSBart Van Assche * - For aborted commands for which CMD_T_TAS has been set .queue_status() will
2950953bcf7aSBart Van Assche * be called and will drop a reference.
2951953bcf7aSBart Van Assche * - For aborted commands for which CMD_T_TAS has not been set .aborted_task()
29522c9fa49eSBart Van Assche * will be called. target_handle_abort() will drop the final reference.
2953953bcf7aSBart Van Assche */
transport_generic_free_cmd(struct se_cmd * cmd,int wait_for_tasks)2954d5ddad41SNicholas Bellinger int transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
2955c66ac9dbSNicholas Bellinger {
29567b2cc7dcSBart Van Assche DECLARE_COMPLETION_ONSTACK(compl);
2957d5ddad41SNicholas Bellinger int ret = 0;
29580f4a9431SNicholas Bellinger bool aborted = false, tas = false;
2959d5ddad41SNicholas Bellinger
2960d14921d6SNicholas Bellinger if (wait_for_tasks)
29610f4a9431SNicholas Bellinger target_wait_free_cmd(cmd, &aborted, &tas);
2962edf46eeeSBart Van Assche
2963edf46eeeSBart Van Assche if (cmd->se_cmd_flags & SCF_SE_LUN_CMD) {
2964c130480bSNicholas Bellinger /*
2965c130480bSNicholas Bellinger * Handle WRITE failure case where transport_generic_new_cmd()
2966c130480bSNicholas Bellinger * has already added se_cmd to state_list, but fabric has
2967c130480bSNicholas Bellinger * failed command before I/O submission.
2968c130480bSNicholas Bellinger */
2969febe562cSNicholas Bellinger if (cmd->state_active)
2970c130480bSNicholas Bellinger target_remove_from_state_list(cmd);
2971c14335ebSBart Van Assche
2972c14335ebSBart Van Assche if (cmd->se_lun)
2973c14335ebSBart Van Assche transport_lun_remove_cmd(cmd);
2974a8864d86SBart Van Assche }
29757b2cc7dcSBart Van Assche if (aborted)
2976a014c364SBart Van Assche cmd->free_compl = &compl;
2977c0170698SBart Van Assche ret = target_put_sess_cmd(cmd);
29780f4a9431SNicholas Bellinger if (aborted) {
29790f4a9431SNicholas Bellinger pr_debug("Detected CMD_T_ABORTED for ITT: %llu\n", cmd->tag);
29807b2cc7dcSBart Van Assche wait_for_completion(&compl);
29810f4a9431SNicholas Bellinger ret = 1;
29820f4a9431SNicholas Bellinger }
2983d5ddad41SNicholas Bellinger return ret;
2984c66ac9dbSNicholas Bellinger }
2985c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_generic_free_cmd);
2986c66ac9dbSNicholas Bellinger
29879ad97b8bSRandy Dunlap /**
29886f55b06fSMike Christie * target_get_sess_cmd - Verify the session is accepting cmds and take ref
2989a17f091dSNicholas Bellinger * @se_cmd: command descriptor to add
2990a6360785SNicholas Bellinger * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2991a17f091dSNicholas Bellinger */
target_get_sess_cmd(struct se_cmd * se_cmd,bool ack_kref)2992afc16604SBart Van Assche int target_get_sess_cmd(struct se_cmd *se_cmd, bool ack_kref)
2993a17f091dSNicholas Bellinger {
2994bc187ea6SRoland Dreier int ret = 0;
2995a17f091dSNicholas Bellinger
2996a6360785SNicholas Bellinger /*
2997a6360785SNicholas Bellinger * Add a second kref if the fabric caller is expecting to handle
2998a6360785SNicholas Bellinger * fabric acknowledgement that requires two target_put_sess_cmd()
2999a6360785SNicholas Bellinger * invocations before se_cmd descriptor release.
3000a6360785SNicholas Bellinger */
3001527268dfSNicholas Bellinger if (ack_kref) {
3002cb222a01SMike Christie kref_get(&se_cmd->cmd_kref);
3003527268dfSNicholas Bellinger se_cmd->se_cmd_flags |= SCF_ACK_KREF;
3004527268dfSNicholas Bellinger }
30057481deb4SNicholas Bellinger
3006becd9be6SMike Christie /*
3007becd9be6SMike Christie * Users like xcopy do not use counters since they never do a stop
3008becd9be6SMike Christie * and wait.
3009becd9be6SMike Christie */
30108e288be8SMike Christie if (se_cmd->cmd_cnt) {
30118e288be8SMike Christie if (!percpu_ref_tryget_live(&se_cmd->cmd_cnt->refcnt))
3012bc187ea6SRoland Dreier ret = -ESHUTDOWN;
3013becd9be6SMike Christie }
30147544e597SBart Van Assche if (ret && ack_kref)
3015afc16604SBart Van Assche target_put_sess_cmd(se_cmd);
30167544e597SBart Van Assche
3017bc187ea6SRoland Dreier return ret;
3018a17f091dSNicholas Bellinger }
301920361e69SNicholas Bellinger EXPORT_SYMBOL(target_get_sess_cmd);
3020a17f091dSNicholas Bellinger
target_free_cmd_mem(struct se_cmd * cmd)3021febe562cSNicholas Bellinger static void target_free_cmd_mem(struct se_cmd *cmd)
3022febe562cSNicholas Bellinger {
3023febe562cSNicholas Bellinger transport_free_pages(cmd);
3024febe562cSNicholas Bellinger
3025febe562cSNicholas Bellinger if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
3026febe562cSNicholas Bellinger core_tmr_release_req(cmd->se_tmr_req);
3027febe562cSNicholas Bellinger if (cmd->t_task_cdb != cmd->__t_task_cdb)
3028febe562cSNicholas Bellinger kfree(cmd->t_task_cdb);
3029febe562cSNicholas Bellinger }
3030febe562cSNicholas Bellinger
target_release_cmd_kref(struct kref * kref)30317481deb4SNicholas Bellinger static void target_release_cmd_kref(struct kref *kref)
3032a17f091dSNicholas Bellinger {
30337481deb4SNicholas Bellinger struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
3034becd9be6SMike Christie struct target_cmd_counter *cmd_cnt = se_cmd->cmd_cnt;
3035a014c364SBart Van Assche struct completion *free_compl = se_cmd->free_compl;
3036a014c364SBart Van Assche struct completion *abrt_compl = se_cmd->abrt_compl;
3037a17f091dSNicholas Bellinger
3038febe562cSNicholas Bellinger target_free_cmd_mem(se_cmd);
30397481deb4SNicholas Bellinger se_cmd->se_tfo->release_cmd(se_cmd);
3040a014c364SBart Van Assche if (free_compl)
3041a014c364SBart Van Assche complete(free_compl);
3042a014c364SBart Van Assche if (abrt_compl)
3043a014c364SBart Van Assche complete(abrt_compl);
3044ad669505SBart Van Assche
3045becd9be6SMike Christie if (cmd_cnt)
3046becd9be6SMike Christie percpu_ref_put(&cmd_cnt->refcnt);
30477481deb4SNicholas Bellinger }
30487481deb4SNicholas Bellinger
30496b6427b6SBart Van Assche /**
30506b6427b6SBart Van Assche * target_put_sess_cmd - decrease the command reference count
30516b6427b6SBart Van Assche * @se_cmd: command to drop a reference from
30526b6427b6SBart Van Assche *
30536b6427b6SBart Van Assche * Returns 1 if and only if this target_put_sess_cmd() call caused the
30546b6427b6SBart Van Assche * refcount to drop to zero. Returns zero otherwise.
30557481deb4SNicholas Bellinger */
target_put_sess_cmd(struct se_cmd * se_cmd)3056afc16604SBart Van Assche int target_put_sess_cmd(struct se_cmd *se_cmd)
30577481deb4SNicholas Bellinger {
30589ff9d15eSBart Van Assche return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
3059a17f091dSNicholas Bellinger }
3060a17f091dSNicholas Bellinger EXPORT_SYMBOL(target_put_sess_cmd);
3061a17f091dSNicholas Bellinger
data_dir_name(enum dma_data_direction d)3062c00e6220SBart Van Assche static const char *data_dir_name(enum dma_data_direction d)
3063c00e6220SBart Van Assche {
3064c00e6220SBart Van Assche switch (d) {
3065c00e6220SBart Van Assche case DMA_BIDIRECTIONAL: return "BIDI";
3066c00e6220SBart Van Assche case DMA_TO_DEVICE: return "WRITE";
3067c00e6220SBart Van Assche case DMA_FROM_DEVICE: return "READ";
3068c00e6220SBart Van Assche case DMA_NONE: return "NONE";
3069c00e6220SBart Van Assche }
3070c00e6220SBart Van Assche
3071c00e6220SBart Van Assche return "(?)";
3072c00e6220SBart Van Assche }
3073c00e6220SBart Van Assche
cmd_state_name(enum transport_state_table t)3074c00e6220SBart Van Assche static const char *cmd_state_name(enum transport_state_table t)
3075c00e6220SBart Van Assche {
3076c00e6220SBart Van Assche switch (t) {
3077c00e6220SBart Van Assche case TRANSPORT_NO_STATE: return "NO_STATE";
3078c00e6220SBart Van Assche case TRANSPORT_NEW_CMD: return "NEW_CMD";
3079c00e6220SBart Van Assche case TRANSPORT_WRITE_PENDING: return "WRITE_PENDING";
3080c00e6220SBart Van Assche case TRANSPORT_PROCESSING: return "PROCESSING";
3081c00e6220SBart Van Assche case TRANSPORT_COMPLETE: return "COMPLETE";
3082c00e6220SBart Van Assche case TRANSPORT_ISTATE_PROCESSING:
3083c00e6220SBart Van Assche return "ISTATE_PROCESSING";
3084c00e6220SBart Van Assche case TRANSPORT_COMPLETE_QF_WP: return "COMPLETE_QF_WP";
3085c00e6220SBart Van Assche case TRANSPORT_COMPLETE_QF_OK: return "COMPLETE_QF_OK";
3086c00e6220SBart Van Assche case TRANSPORT_COMPLETE_QF_ERR: return "COMPLETE_QF_ERR";
3087c00e6220SBart Van Assche }
3088c00e6220SBart Van Assche
3089c00e6220SBart Van Assche return "(?)";
3090c00e6220SBart Van Assche }
3091c00e6220SBart Van Assche
target_append_str(char ** str,const char * txt)3092c00e6220SBart Van Assche static void target_append_str(char **str, const char *txt)
3093c00e6220SBart Van Assche {
3094c00e6220SBart Van Assche char *prev = *str;
3095c00e6220SBart Van Assche
3096c00e6220SBart Van Assche *str = *str ? kasprintf(GFP_ATOMIC, "%s,%s", *str, txt) :
3097c00e6220SBart Van Assche kstrdup(txt, GFP_ATOMIC);
3098c00e6220SBart Van Assche kfree(prev);
3099c00e6220SBart Van Assche }
3100c00e6220SBart Van Assche
3101c00e6220SBart Van Assche /*
3102c00e6220SBart Van Assche * Convert a transport state bitmask into a string. The caller is
3103c00e6220SBart Van Assche * responsible for freeing the returned pointer.
3104c00e6220SBart Van Assche */
target_ts_to_str(u32 ts)3105c00e6220SBart Van Assche static char *target_ts_to_str(u32 ts)
3106c00e6220SBart Van Assche {
3107c00e6220SBart Van Assche char *str = NULL;
3108c00e6220SBart Van Assche
3109c00e6220SBart Van Assche if (ts & CMD_T_ABORTED)
3110c00e6220SBart Van Assche target_append_str(&str, "aborted");
3111c00e6220SBart Van Assche if (ts & CMD_T_ACTIVE)
3112c00e6220SBart Van Assche target_append_str(&str, "active");
3113c00e6220SBart Van Assche if (ts & CMD_T_COMPLETE)
3114c00e6220SBart Van Assche target_append_str(&str, "complete");
3115c00e6220SBart Van Assche if (ts & CMD_T_SENT)
3116c00e6220SBart Van Assche target_append_str(&str, "sent");
3117c00e6220SBart Van Assche if (ts & CMD_T_STOP)
3118c00e6220SBart Van Assche target_append_str(&str, "stop");
3119c00e6220SBart Van Assche if (ts & CMD_T_FABRIC_STOP)
3120c00e6220SBart Van Assche target_append_str(&str, "fabric_stop");
3121c00e6220SBart Van Assche
3122c00e6220SBart Van Assche return str;
3123c00e6220SBart Van Assche }
3124c00e6220SBart Van Assche
target_tmf_name(enum tcm_tmreq_table tmf)3125c00e6220SBart Van Assche static const char *target_tmf_name(enum tcm_tmreq_table tmf)
3126c00e6220SBart Van Assche {
3127c00e6220SBart Van Assche switch (tmf) {
3128c00e6220SBart Van Assche case TMR_ABORT_TASK: return "ABORT_TASK";
3129c00e6220SBart Van Assche case TMR_ABORT_TASK_SET: return "ABORT_TASK_SET";
3130c00e6220SBart Van Assche case TMR_CLEAR_ACA: return "CLEAR_ACA";
3131c00e6220SBart Van Assche case TMR_CLEAR_TASK_SET: return "CLEAR_TASK_SET";
3132c00e6220SBart Van Assche case TMR_LUN_RESET: return "LUN_RESET";
3133c00e6220SBart Van Assche case TMR_TARGET_WARM_RESET: return "TARGET_WARM_RESET";
3134c00e6220SBart Van Assche case TMR_TARGET_COLD_RESET: return "TARGET_COLD_RESET";
31352e45a1a9SBodo Stroesser case TMR_LUN_RESET_PRO: return "LUN_RESET_PRO";
3136c00e6220SBart Van Assche case TMR_UNKNOWN: break;
3137c00e6220SBart Van Assche }
3138c00e6220SBart Van Assche return "(?)";
3139c00e6220SBart Van Assche }
3140c00e6220SBart Van Assche
target_show_cmd(const char * pfx,struct se_cmd * cmd)3141c00e6220SBart Van Assche void target_show_cmd(const char *pfx, struct se_cmd *cmd)
3142c00e6220SBart Van Assche {
3143c00e6220SBart Van Assche char *ts_str = target_ts_to_str(cmd->transport_state);
3144c00e6220SBart Van Assche const u8 *cdb = cmd->t_task_cdb;
3145c00e6220SBart Van Assche struct se_tmr_req *tmf = cmd->se_tmr_req;
3146c00e6220SBart Van Assche
3147c00e6220SBart Van Assche if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
3148c00e6220SBart Van Assche pr_debug("%scmd %#02x:%#02x with tag %#llx dir %s i_state %d t_state %s len %d refcnt %d transport_state %s\n",
3149c00e6220SBart Van Assche pfx, cdb[0], cdb[1], cmd->tag,
3150c00e6220SBart Van Assche data_dir_name(cmd->data_direction),
3151c00e6220SBart Van Assche cmd->se_tfo->get_cmd_state(cmd),
3152c00e6220SBart Van Assche cmd_state_name(cmd->t_state), cmd->data_length,
3153c00e6220SBart Van Assche kref_read(&cmd->cmd_kref), ts_str);
3154c00e6220SBart Van Assche } else {
3155c00e6220SBart Van Assche pr_debug("%stmf %s with tag %#llx ref_task_tag %#llx i_state %d t_state %s refcnt %d transport_state %s\n",
3156c00e6220SBart Van Assche pfx, target_tmf_name(tmf->function), cmd->tag,
3157c00e6220SBart Van Assche tmf->ref_task_tag, cmd->se_tfo->get_cmd_state(cmd),
3158c00e6220SBart Van Assche cmd_state_name(cmd->t_state),
3159c00e6220SBart Van Assche kref_read(&cmd->cmd_kref), ts_str);
3160c00e6220SBart Van Assche }
3161c00e6220SBart Van Assche kfree(ts_str);
3162c00e6220SBart Van Assche }
3163c00e6220SBart Van Assche EXPORT_SYMBOL(target_show_cmd);
3164c00e6220SBart Van Assche
target_stop_cmd_counter_confirm(struct percpu_ref * ref)3165becd9be6SMike Christie static void target_stop_cmd_counter_confirm(struct percpu_ref *ref)
3166a17f091dSNicholas Bellinger {
3167becd9be6SMike Christie struct target_cmd_counter *cmd_cnt = container_of(ref,
3168becd9be6SMike Christie struct target_cmd_counter,
3169becd9be6SMike Christie refcnt);
3170becd9be6SMike Christie complete_all(&cmd_cnt->stop_done);
3171becd9be6SMike Christie }
3172becd9be6SMike Christie
3173becd9be6SMike Christie /**
3174becd9be6SMike Christie * target_stop_cmd_counter - Stop new IO from being added to the counter.
3175becd9be6SMike Christie * @cmd_cnt: counter to stop
3176becd9be6SMike Christie */
target_stop_cmd_counter(struct target_cmd_counter * cmd_cnt)31776d256beeSMike Christie void target_stop_cmd_counter(struct target_cmd_counter *cmd_cnt)
3178becd9be6SMike Christie {
3179becd9be6SMike Christie pr_debug("Stopping command counter.\n");
3180becd9be6SMike Christie if (!atomic_cmpxchg(&cmd_cnt->stopped, 0, 1))
3181becd9be6SMike Christie percpu_ref_kill_and_confirm(&cmd_cnt->refcnt,
3182becd9be6SMike Christie target_stop_cmd_counter_confirm);
3183a17f091dSNicholas Bellinger }
31846d256beeSMike Christie EXPORT_SYMBOL_GPL(target_stop_cmd_counter);
31856f55b06fSMike Christie
31866f55b06fSMike Christie /**
31876f55b06fSMike Christie * target_stop_session - Stop new IO from being queued on the session.
31886f55b06fSMike Christie * @se_sess: session to stop
31896f55b06fSMike Christie */
target_stop_session(struct se_session * se_sess)31906f55b06fSMike Christie void target_stop_session(struct se_session *se_sess)
31916f55b06fSMike Christie {
3192becd9be6SMike Christie target_stop_cmd_counter(se_sess->cmd_cnt);
31936f55b06fSMike Christie }
31946f55b06fSMike Christie EXPORT_SYMBOL(target_stop_session);
3195a17f091dSNicholas Bellinger
31969ad97b8bSRandy Dunlap /**
3197becd9be6SMike Christie * target_wait_for_cmds - Wait for outstanding cmds.
3198becd9be6SMike Christie * @cmd_cnt: counter to wait for active I/O for.
3199becd9be6SMike Christie */
target_wait_for_cmds(struct target_cmd_counter * cmd_cnt)32006d256beeSMike Christie void target_wait_for_cmds(struct target_cmd_counter *cmd_cnt)
3201becd9be6SMike Christie {
3202becd9be6SMike Christie int ret;
3203becd9be6SMike Christie
3204becd9be6SMike Christie WARN_ON_ONCE(!atomic_read(&cmd_cnt->stopped));
3205becd9be6SMike Christie
3206becd9be6SMike Christie do {
3207becd9be6SMike Christie pr_debug("Waiting for running cmds to complete.\n");
3208becd9be6SMike Christie ret = wait_event_timeout(cmd_cnt->refcnt_wq,
3209becd9be6SMike Christie percpu_ref_is_zero(&cmd_cnt->refcnt),
3210becd9be6SMike Christie 180 * HZ);
3211becd9be6SMike Christie } while (ret <= 0);
3212becd9be6SMike Christie
3213becd9be6SMike Christie wait_for_completion(&cmd_cnt->stop_done);
3214becd9be6SMike Christie pr_debug("Waiting for cmds done.\n");
3215becd9be6SMike Christie }
32166d256beeSMike Christie EXPORT_SYMBOL_GPL(target_wait_for_cmds);
3217becd9be6SMike Christie
3218becd9be6SMike Christie /**
321900d909a1SBart Van Assche * target_wait_for_sess_cmds - Wait for outstanding commands
3220a17f091dSNicholas Bellinger * @se_sess: session to wait for active I/O
3221a17f091dSNicholas Bellinger */
target_wait_for_sess_cmds(struct se_session * se_sess)3222be646c2dSJoern Engel void target_wait_for_sess_cmds(struct se_session *se_sess)
3223a17f091dSNicholas Bellinger {
3224becd9be6SMike Christie target_wait_for_cmds(se_sess->cmd_cnt);
3225a17f091dSNicholas Bellinger }
3226a17f091dSNicholas Bellinger EXPORT_SYMBOL(target_wait_for_sess_cmds);
3227a17f091dSNicholas Bellinger
3228a95be384SBart Van Assche /*
3229a95be384SBart Van Assche * Prevent that new percpu_ref_tryget_live() calls succeed and wait until
3230a95be384SBart Van Assche * all references to the LUN have been released. Called during LUN shutdown.
3231a95be384SBart Van Assche */
transport_clear_lun_ref(struct se_lun * lun)3232b3eeea66SBart Van Assche void transport_clear_lun_ref(struct se_lun *lun)
3233c66ac9dbSNicholas Bellinger {
3234a95be384SBart Van Assche percpu_ref_kill(&lun->lun_ref);
3235bd4e2d29SNicholas Bellinger wait_for_completion(&lun->lun_shutdown_comp);
3236c66ac9dbSNicholas Bellinger }
3237c66ac9dbSNicholas Bellinger
32380f4a9431SNicholas Bellinger static bool
__transport_wait_for_tasks(struct se_cmd * cmd,bool fabric_stop,bool * aborted,bool * tas,unsigned long * flags)32390f4a9431SNicholas Bellinger __transport_wait_for_tasks(struct se_cmd *cmd, bool fabric_stop,
32400f4a9431SNicholas Bellinger bool *aborted, bool *tas, unsigned long *flags)
32410f4a9431SNicholas Bellinger __releases(&cmd->t_state_lock)
32420f4a9431SNicholas Bellinger __acquires(&cmd->t_state_lock)
32430f4a9431SNicholas Bellinger {
3244515da6f4SMaurizio Lombardi lockdep_assert_held(&cmd->t_state_lock);
32450f4a9431SNicholas Bellinger
32460f4a9431SNicholas Bellinger if (fabric_stop)
32470f4a9431SNicholas Bellinger cmd->transport_state |= CMD_T_FABRIC_STOP;
32480f4a9431SNicholas Bellinger
32490f4a9431SNicholas Bellinger if (cmd->transport_state & CMD_T_ABORTED)
32500f4a9431SNicholas Bellinger *aborted = true;
32510f4a9431SNicholas Bellinger
32520f4a9431SNicholas Bellinger if (cmd->transport_state & CMD_T_TAS)
32530f4a9431SNicholas Bellinger *tas = true;
32540f4a9431SNicholas Bellinger
32550f4a9431SNicholas Bellinger if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
32560f4a9431SNicholas Bellinger !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
32570f4a9431SNicholas Bellinger return false;
32580f4a9431SNicholas Bellinger
32590f4a9431SNicholas Bellinger if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
32600f4a9431SNicholas Bellinger !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
32610f4a9431SNicholas Bellinger return false;
32620f4a9431SNicholas Bellinger
32630f4a9431SNicholas Bellinger if (!(cmd->transport_state & CMD_T_ACTIVE))
32640f4a9431SNicholas Bellinger return false;
32650f4a9431SNicholas Bellinger
32660f4a9431SNicholas Bellinger if (fabric_stop && *aborted)
32670f4a9431SNicholas Bellinger return false;
32680f4a9431SNicholas Bellinger
32690f4a9431SNicholas Bellinger cmd->transport_state |= CMD_T_STOP;
32700f4a9431SNicholas Bellinger
3271c00e6220SBart Van Assche target_show_cmd("wait_for_tasks: Stopping ", cmd);
32720f4a9431SNicholas Bellinger
32730f4a9431SNicholas Bellinger spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
32740f4a9431SNicholas Bellinger
3275c00e6220SBart Van Assche while (!wait_for_completion_timeout(&cmd->t_transport_stop_comp,
3276c00e6220SBart Van Assche 180 * HZ))
3277c00e6220SBart Van Assche target_show_cmd("wait for tasks: ", cmd);
32780f4a9431SNicholas Bellinger
32790f4a9431SNicholas Bellinger spin_lock_irqsave(&cmd->t_state_lock, *flags);
32800f4a9431SNicholas Bellinger cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
32810f4a9431SNicholas Bellinger
32820f4a9431SNicholas Bellinger pr_debug("wait_for_tasks: Stopped wait_for_completion(&cmd->"
32830f4a9431SNicholas Bellinger "t_transport_stop_comp) for ITT: 0x%08llx\n", cmd->tag);
32840f4a9431SNicholas Bellinger
32850f4a9431SNicholas Bellinger return true;
32860f4a9431SNicholas Bellinger }
32870f4a9431SNicholas Bellinger
3288d14921d6SNicholas Bellinger /**
32892d4760eeSBart Van Assche * transport_wait_for_tasks - set CMD_T_STOP and wait for t_transport_stop_comp
32902d4760eeSBart Van Assche * @cmd: command to wait on
3291c66ac9dbSNicholas Bellinger */
transport_wait_for_tasks(struct se_cmd * cmd)3292a17f091dSNicholas Bellinger bool transport_wait_for_tasks(struct se_cmd *cmd)
3293c66ac9dbSNicholas Bellinger {
3294c66ac9dbSNicholas Bellinger unsigned long flags;
32950f4a9431SNicholas Bellinger bool ret, aborted = false, tas = false;
3296c66ac9dbSNicholas Bellinger
3297a1d8b49aSAndy Grover spin_lock_irqsave(&cmd->t_state_lock, flags);
32980f4a9431SNicholas Bellinger ret = __transport_wait_for_tasks(cmd, false, &aborted, &tas, &flags);
3299a1d8b49aSAndy Grover spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3300c66ac9dbSNicholas Bellinger
33010f4a9431SNicholas Bellinger return ret;
3302c66ac9dbSNicholas Bellinger }
3303d14921d6SNicholas Bellinger EXPORT_SYMBOL(transport_wait_for_tasks);
3304c66ac9dbSNicholas Bellinger
3305b455233dSDavid Disseldorp struct sense_detail {
3306ab78fef4SBart Van Assche u8 key;
3307ab78fef4SBart Van Assche u8 asc;
3308ab78fef4SBart Van Assche u8 ascq;
33098dd992fbSDavid Disseldorp bool add_sense_info;
3310ab78fef4SBart Van Assche };
3311ab78fef4SBart Van Assche
3312b455233dSDavid Disseldorp static const struct sense_detail sense_detail_table[] = {
3313ab78fef4SBart Van Assche [TCM_NO_SENSE] = {
3314ab78fef4SBart Van Assche .key = NOT_READY
3315ab78fef4SBart Van Assche },
3316ab78fef4SBart Van Assche [TCM_NON_EXISTENT_LUN] = {
3317ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3318ab78fef4SBart Van Assche .asc = 0x25 /* LOGICAL UNIT NOT SUPPORTED */
3319ab78fef4SBart Van Assche },
3320ab78fef4SBart Van Assche [TCM_UNSUPPORTED_SCSI_OPCODE] = {
3321ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3322ab78fef4SBart Van Assche .asc = 0x20, /* INVALID COMMAND OPERATION CODE */
3323ab78fef4SBart Van Assche },
3324ab78fef4SBart Van Assche [TCM_SECTOR_COUNT_TOO_MANY] = {
3325ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3326ab78fef4SBart Van Assche .asc = 0x20, /* INVALID COMMAND OPERATION CODE */
3327ab78fef4SBart Van Assche },
3328ab78fef4SBart Van Assche [TCM_UNKNOWN_MODE_PAGE] = {
3329ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3330ab78fef4SBart Van Assche .asc = 0x24, /* INVALID FIELD IN CDB */
3331ab78fef4SBart Van Assche },
3332ab78fef4SBart Van Assche [TCM_CHECK_CONDITION_ABORT_CMD] = {
3333ab78fef4SBart Van Assche .key = ABORTED_COMMAND,
3334ab78fef4SBart Van Assche .asc = 0x29, /* BUS DEVICE RESET FUNCTION OCCURRED */
3335ab78fef4SBart Van Assche .ascq = 0x03,
3336ab78fef4SBart Van Assche },
3337ab78fef4SBart Van Assche [TCM_INCORRECT_AMOUNT_OF_DATA] = {
3338ab78fef4SBart Van Assche .key = ABORTED_COMMAND,
3339ab78fef4SBart Van Assche .asc = 0x0c, /* WRITE ERROR */
3340ab78fef4SBart Van Assche .ascq = 0x0d, /* NOT ENOUGH UNSOLICITED DATA */
3341ab78fef4SBart Van Assche },
3342ab78fef4SBart Van Assche [TCM_INVALID_CDB_FIELD] = {
3343ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3344ab78fef4SBart Van Assche .asc = 0x24, /* INVALID FIELD IN CDB */
3345ab78fef4SBart Van Assche },
3346ab78fef4SBart Van Assche [TCM_INVALID_PARAMETER_LIST] = {
3347ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3348ab78fef4SBart Van Assche .asc = 0x26, /* INVALID FIELD IN PARAMETER LIST */
3349ab78fef4SBart Van Assche },
3350e8642120SDavid Disseldorp [TCM_TOO_MANY_TARGET_DESCS] = {
3351e8642120SDavid Disseldorp .key = ILLEGAL_REQUEST,
3352e8642120SDavid Disseldorp .asc = 0x26,
3353e8642120SDavid Disseldorp .ascq = 0x06, /* TOO MANY TARGET DESCRIPTORS */
3354e8642120SDavid Disseldorp },
3355e8642120SDavid Disseldorp [TCM_UNSUPPORTED_TARGET_DESC_TYPE_CODE] = {
3356e8642120SDavid Disseldorp .key = ILLEGAL_REQUEST,
3357e8642120SDavid Disseldorp .asc = 0x26,
3358e8642120SDavid Disseldorp .ascq = 0x07, /* UNSUPPORTED TARGET DESCRIPTOR TYPE CODE */
3359e8642120SDavid Disseldorp },
3360e8642120SDavid Disseldorp [TCM_TOO_MANY_SEGMENT_DESCS] = {
3361e8642120SDavid Disseldorp .key = ILLEGAL_REQUEST,
3362e8642120SDavid Disseldorp .asc = 0x26,
3363e8642120SDavid Disseldorp .ascq = 0x08, /* TOO MANY SEGMENT DESCRIPTORS */
3364e8642120SDavid Disseldorp },
3365e8642120SDavid Disseldorp [TCM_UNSUPPORTED_SEGMENT_DESC_TYPE_CODE] = {
3366e8642120SDavid Disseldorp .key = ILLEGAL_REQUEST,
3367e8642120SDavid Disseldorp .asc = 0x26,
3368e8642120SDavid Disseldorp .ascq = 0x09, /* UNSUPPORTED SEGMENT DESCRIPTOR TYPE CODE */
3369e8642120SDavid Disseldorp },
3370ab78fef4SBart Van Assche [TCM_PARAMETER_LIST_LENGTH_ERROR] = {
3371ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3372ab78fef4SBart Van Assche .asc = 0x1a, /* PARAMETER LIST LENGTH ERROR */
3373ab78fef4SBart Van Assche },
3374ab78fef4SBart Van Assche [TCM_UNEXPECTED_UNSOLICITED_DATA] = {
3375ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3376ab78fef4SBart Van Assche .asc = 0x0c, /* WRITE ERROR */
3377ab78fef4SBart Van Assche .ascq = 0x0c, /* UNEXPECTED_UNSOLICITED_DATA */
3378ab78fef4SBart Van Assche },
3379ab78fef4SBart Van Assche [TCM_SERVICE_CRC_ERROR] = {
3380ab78fef4SBart Van Assche .key = ABORTED_COMMAND,
3381ab78fef4SBart Van Assche .asc = 0x47, /* PROTOCOL SERVICE CRC ERROR */
3382ab78fef4SBart Van Assche .ascq = 0x05, /* N/A */
3383ab78fef4SBart Van Assche },
3384ab78fef4SBart Van Assche [TCM_SNACK_REJECTED] = {
3385ab78fef4SBart Van Assche .key = ABORTED_COMMAND,
3386ab78fef4SBart Van Assche .asc = 0x11, /* READ ERROR */
3387ab78fef4SBart Van Assche .ascq = 0x13, /* FAILED RETRANSMISSION REQUEST */
3388ab78fef4SBart Van Assche },
3389ab78fef4SBart Van Assche [TCM_WRITE_PROTECTED] = {
3390ab78fef4SBart Van Assche .key = DATA_PROTECT,
3391ab78fef4SBart Van Assche .asc = 0x27, /* WRITE PROTECTED */
3392ab78fef4SBart Van Assche },
3393ab78fef4SBart Van Assche [TCM_ADDRESS_OUT_OF_RANGE] = {
3394ab78fef4SBart Van Assche .key = ILLEGAL_REQUEST,
3395ab78fef4SBart Van Assche .asc = 0x21, /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
3396ab78fef4SBart Van Assche },
3397ab78fef4SBart Van Assche [TCM_CHECK_CONDITION_UNIT_ATTENTION] = {
3398ab78fef4SBart Van Assche .key = UNIT_ATTENTION,
3399ab78fef4SBart Van Assche },
3400ab78fef4SBart Van Assche [TCM_MISCOMPARE_VERIFY] = {
3401ab78fef4SBart Van Assche .key = MISCOMPARE,
3402ab78fef4SBart Van Assche .asc = 0x1d, /* MISCOMPARE DURING VERIFY OPERATION */
3403ab78fef4SBart Van Assche .ascq = 0x00,
3404749c226cSDavid Disseldorp .add_sense_info = true,
3405ab78fef4SBart Van Assche },
3406ab78fef4SBart Van Assche [TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED] = {
3407734ca5c4SSagi Grimberg .key = ABORTED_COMMAND,
3408ab78fef4SBart Van Assche .asc = 0x10,
3409ab78fef4SBart Van Assche .ascq = 0x01, /* LOGICAL BLOCK GUARD CHECK FAILED */
34108dd992fbSDavid Disseldorp .add_sense_info = true,
3411ab78fef4SBart Van Assche },
3412ab78fef4SBart Van Assche [TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED] = {
3413734ca5c4SSagi Grimberg .key = ABORTED_COMMAND,
3414ab78fef4SBart Van Assche .asc = 0x10,
3415ab78fef4SBart Van Assche .ascq = 0x02, /* LOGICAL BLOCK APPLICATION TAG CHECK FAILED */
34168dd992fbSDavid Disseldorp .add_sense_info = true,
3417ab78fef4SBart Van Assche },
3418ab78fef4SBart Van Assche [TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED] = {
3419734ca5c4SSagi Grimberg .key = ABORTED_COMMAND,
3420ab78fef4SBart Van Assche .asc = 0x10,
3421ab78fef4SBart Van Assche .ascq = 0x03, /* LOGICAL BLOCK REFERENCE TAG CHECK FAILED */
34228dd992fbSDavid Disseldorp .add_sense_info = true,
3423ab78fef4SBart Van Assche },
3424449a1378SNicholas Bellinger [TCM_COPY_TARGET_DEVICE_NOT_REACHABLE] = {
3425449a1378SNicholas Bellinger .key = COPY_ABORTED,
3426449a1378SNicholas Bellinger .asc = 0x0d,
3427449a1378SNicholas Bellinger .ascq = 0x02, /* COPY TARGET DEVICE NOT REACHABLE */
3428449a1378SNicholas Bellinger
3429449a1378SNicholas Bellinger },
3430ab78fef4SBart Van Assche [TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE] = {
3431ab78fef4SBart Van Assche /*
3432ab78fef4SBart Van Assche * Returning ILLEGAL REQUEST would cause immediate IO errors on
3433ab78fef4SBart Van Assche * Solaris initiators. Returning NOT READY instead means the
3434ab78fef4SBart Van Assche * operations will be retried a finite number of times and we
3435ab78fef4SBart Van Assche * can survive intermittent errors.
3436ab78fef4SBart Van Assche */
3437ab78fef4SBart Van Assche .key = NOT_READY,
3438ab78fef4SBart Van Assche .asc = 0x08, /* LOGICAL UNIT COMMUNICATION FAILURE */
3439ab78fef4SBart Van Assche },
344079dd6f2fStangwenji [TCM_INSUFFICIENT_REGISTRATION_RESOURCES] = {
344179dd6f2fStangwenji /*
344279dd6f2fStangwenji * From spc4r22 section5.7.7,5.7.8
344379dd6f2fStangwenji * If a PERSISTENT RESERVE OUT command with a REGISTER service action
344479dd6f2fStangwenji * or a REGISTER AND IGNORE EXISTING KEY service action or
344579dd6f2fStangwenji * REGISTER AND MOVE service actionis attempted,
344679dd6f2fStangwenji * but there are insufficient device server resources to complete the
344779dd6f2fStangwenji * operation, then the command shall be terminated with CHECK CONDITION
344879dd6f2fStangwenji * status, with the sense key set to ILLEGAL REQUEST,and the additonal
344979dd6f2fStangwenji * sense code set to INSUFFICIENT REGISTRATION RESOURCES.
345079dd6f2fStangwenji */
345179dd6f2fStangwenji .key = ILLEGAL_REQUEST,
345279dd6f2fStangwenji .asc = 0x55,
345379dd6f2fStangwenji .ascq = 0x04, /* INSUFFICIENT REGISTRATION RESOURCES */
345479dd6f2fStangwenji },
3455ead0ffc9SAnastasia Kovaleva [TCM_INVALID_FIELD_IN_COMMAND_IU] = {
3456ead0ffc9SAnastasia Kovaleva .key = ILLEGAL_REQUEST,
3457ead0ffc9SAnastasia Kovaleva .asc = 0x0e,
3458ead0ffc9SAnastasia Kovaleva .ascq = 0x03, /* INVALID FIELD IN COMMAND INFORMATION UNIT */
3459ead0ffc9SAnastasia Kovaleva },
346040fd8845SDavid Disseldorp [TCM_ALUA_TG_PT_STANDBY] = {
346140fd8845SDavid Disseldorp .key = NOT_READY,
346240fd8845SDavid Disseldorp .asc = 0x04,
346340fd8845SDavid Disseldorp .ascq = ASCQ_04H_ALUA_TG_PT_STANDBY,
346440fd8845SDavid Disseldorp },
346540fd8845SDavid Disseldorp [TCM_ALUA_TG_PT_UNAVAILABLE] = {
346640fd8845SDavid Disseldorp .key = NOT_READY,
346740fd8845SDavid Disseldorp .asc = 0x04,
346840fd8845SDavid Disseldorp .ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE,
346940fd8845SDavid Disseldorp },
347040fd8845SDavid Disseldorp [TCM_ALUA_STATE_TRANSITION] = {
347140fd8845SDavid Disseldorp .key = NOT_READY,
347240fd8845SDavid Disseldorp .asc = 0x04,
347340fd8845SDavid Disseldorp .ascq = ASCQ_04H_ALUA_STATE_TRANSITION,
347440fd8845SDavid Disseldorp },
347540fd8845SDavid Disseldorp [TCM_ALUA_OFFLINE] = {
347640fd8845SDavid Disseldorp .key = NOT_READY,
347740fd8845SDavid Disseldorp .asc = 0x04,
347840fd8845SDavid Disseldorp .ascq = ASCQ_04H_ALUA_OFFLINE,
347940fd8845SDavid Disseldorp },
3480ab78fef4SBart Van Assche };
3481ab78fef4SBart Van Assche
3482325c1e8bSBart Van Assche /**
3483325c1e8bSBart Van Assche * translate_sense_reason - translate a sense reason into T10 key, asc and ascq
3484325c1e8bSBart Van Assche * @cmd: SCSI command in which the resulting sense buffer or SCSI status will
3485325c1e8bSBart Van Assche * be stored.
3486325c1e8bSBart Van Assche * @reason: LIO sense reason code. If this argument has the value
3487325c1e8bSBart Van Assche * TCM_CHECK_CONDITION_UNIT_ATTENTION, try to dequeue a unit attention. If
3488325c1e8bSBart Van Assche * dequeuing a unit attention fails due to multiple commands being processed
3489325c1e8bSBart Van Assche * concurrently, set the command status to BUSY.
3490325c1e8bSBart Van Assche *
3491325c1e8bSBart Van Assche * Return: 0 upon success or -EINVAL if the sense buffer is too small.
3492325c1e8bSBart Van Assche */
translate_sense_reason(struct se_cmd * cmd,sense_reason_t reason)349317e391ddSBart Van Assche static void translate_sense_reason(struct se_cmd *cmd, sense_reason_t reason)
3494ab78fef4SBart Van Assche {
3495b455233dSDavid Disseldorp const struct sense_detail *sd;
3496ab78fef4SBart Van Assche u8 *buffer = cmd->sense_buffer;
3497ab78fef4SBart Van Assche int r = (__force int)reason;
3498325c1e8bSBart Van Assche u8 key, asc, ascq;
34994e4937e8SSagi Grimberg bool desc_format = target_sense_desc_format(cmd->se_dev);
3500ab78fef4SBart Van Assche
3501b455233dSDavid Disseldorp if (r < ARRAY_SIZE(sense_detail_table) && sense_detail_table[r].key)
3502b455233dSDavid Disseldorp sd = &sense_detail_table[r];
3503ab78fef4SBart Van Assche else
3504b455233dSDavid Disseldorp sd = &sense_detail_table[(__force int)
3505ab78fef4SBart Van Assche TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE];
3506ab78fef4SBart Van Assche
3507b455233dSDavid Disseldorp key = sd->key;
3508ab78fef4SBart Van Assche if (reason == TCM_CHECK_CONDITION_UNIT_ATTENTION) {
3509325c1e8bSBart Van Assche if (!core_scsi3_ua_for_check_condition(cmd, &key, &asc,
3510325c1e8bSBart Van Assche &ascq)) {
3511325c1e8bSBart Van Assche cmd->scsi_status = SAM_STAT_BUSY;
3512325c1e8bSBart Van Assche return;
3513325c1e8bSBart Van Assche }
3514ab78fef4SBart Van Assche } else {
351540fd8845SDavid Disseldorp WARN_ON_ONCE(sd->asc == 0);
3516b455233dSDavid Disseldorp asc = sd->asc;
3517b455233dSDavid Disseldorp ascq = sd->ascq;
3518ab78fef4SBart Van Assche }
35199ec1e1ceSSagi Grimberg
352089a104edSBart Van Assche cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
352189a104edSBart Van Assche cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
352289a104edSBart Van Assche cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
3523325c1e8bSBart Van Assche scsi_build_sense_buffer(desc_format, buffer, key, asc, ascq);
35248dd992fbSDavid Disseldorp if (sd->add_sense_info)
352517e391ddSBart Van Assche WARN_ON_ONCE(scsi_set_sense_information(buffer,
3526f5a8b3a7SSagi Grimberg cmd->scsi_sense_length,
35278dd992fbSDavid Disseldorp cmd->sense_info) < 0);
3528ab78fef4SBart Van Assche }
3529ab78fef4SBart Van Assche
3530de103c93SChristoph Hellwig int
transport_send_check_condition_and_sense(struct se_cmd * cmd,sense_reason_t reason,int from_transport)3531de103c93SChristoph Hellwig transport_send_check_condition_and_sense(struct se_cmd *cmd,
3532de103c93SChristoph Hellwig sense_reason_t reason, int from_transport)
3533c66ac9dbSNicholas Bellinger {
3534c66ac9dbSNicholas Bellinger unsigned long flags;
3535c66ac9dbSNicholas Bellinger
35362c9fa49eSBart Van Assche WARN_ON_ONCE(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB);
35372c9fa49eSBart Van Assche
3538a1d8b49aSAndy Grover spin_lock_irqsave(&cmd->t_state_lock, flags);
3539c66ac9dbSNicholas Bellinger if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
3540a1d8b49aSAndy Grover spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3541c66ac9dbSNicholas Bellinger return 0;
3542c66ac9dbSNicholas Bellinger }
3543c66ac9dbSNicholas Bellinger cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
3544a1d8b49aSAndy Grover spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3545c66ac9dbSNicholas Bellinger
354617e391ddSBart Van Assche if (!from_transport)
354717e391ddSBart Van Assche translate_sense_reason(cmd, reason);
3548c66ac9dbSNicholas Bellinger
3549e5c0d6adSRoland Dreier trace_target_cmd_complete(cmd);
355007bde79aSNicholas Bellinger return cmd->se_tfo->queue_status(cmd);
3551c66ac9dbSNicholas Bellinger }
3552c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(transport_send_check_condition_and_sense);
3553c66ac9dbSNicholas Bellinger
355494ebb471SBart Van Assche /**
355594ebb471SBart Van Assche * target_send_busy - Send SCSI BUSY status back to the initiator
355694ebb471SBart Van Assche * @cmd: SCSI command for which to send a BUSY reply.
355794ebb471SBart Van Assche *
355894ebb471SBart Van Assche * Note: Only call this function if target_submit_cmd*() failed.
355994ebb471SBart Van Assche */
target_send_busy(struct se_cmd * cmd)356094ebb471SBart Van Assche int target_send_busy(struct se_cmd *cmd)
356194ebb471SBart Van Assche {
356294ebb471SBart Van Assche WARN_ON_ONCE(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB);
356394ebb471SBart Van Assche
356494ebb471SBart Van Assche cmd->scsi_status = SAM_STAT_BUSY;
356594ebb471SBart Van Assche trace_target_cmd_complete(cmd);
356694ebb471SBart Van Assche return cmd->se_tfo->queue_status(cmd);
356794ebb471SBart Van Assche }
356894ebb471SBart Van Assche EXPORT_SYMBOL(target_send_busy);
356994ebb471SBart Van Assche
target_tmr_work(struct work_struct * work)3570af877292SChristoph Hellwig static void target_tmr_work(struct work_struct *work)
3571c66ac9dbSNicholas Bellinger {
3572af877292SChristoph Hellwig struct se_cmd *cmd = container_of(work, struct se_cmd, work);
35735951146dSAndy Grover struct se_device *dev = cmd->se_dev;
3574c66ac9dbSNicholas Bellinger struct se_tmr_req *tmr = cmd->se_tmr_req;
3575c66ac9dbSNicholas Bellinger int ret;
3576c66ac9dbSNicholas Bellinger
35772c9fa49eSBart Van Assche if (cmd->transport_state & CMD_T_ABORTED)
35782c9fa49eSBart Van Assche goto aborted;
3579a6d9bb1cSNicholas Bellinger
3580c66ac9dbSNicholas Bellinger switch (tmr->function) {
35815c6cd613SNicholas Bellinger case TMR_ABORT_TASK:
35823d28934aSNicholas Bellinger core_tmr_abort_task(dev, tmr, cmd->se_sess);
3583c66ac9dbSNicholas Bellinger break;
35845c6cd613SNicholas Bellinger case TMR_ABORT_TASK_SET:
35855c6cd613SNicholas Bellinger case TMR_CLEAR_ACA:
35865c6cd613SNicholas Bellinger case TMR_CLEAR_TASK_SET:
3587c66ac9dbSNicholas Bellinger tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
3588c66ac9dbSNicholas Bellinger break;
35895c6cd613SNicholas Bellinger case TMR_LUN_RESET:
3590c66ac9dbSNicholas Bellinger ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
3591c66ac9dbSNicholas Bellinger tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
3592c66ac9dbSNicholas Bellinger TMR_FUNCTION_REJECTED;
3593b5aafb16SHannes Reinecke if (tmr->response == TMR_FUNCTION_COMPLETE) {
35946290e23fSDmitry Bogdanov target_dev_ua_allocate(dev, 0x29,
3595b5aafb16SHannes Reinecke ASCQ_29H_BUS_DEVICE_RESET_FUNCTION_OCCURRED);
3596b5aafb16SHannes Reinecke }
3597c66ac9dbSNicholas Bellinger break;
35985c6cd613SNicholas Bellinger case TMR_TARGET_WARM_RESET:
3599c66ac9dbSNicholas Bellinger tmr->response = TMR_FUNCTION_REJECTED;
3600c66ac9dbSNicholas Bellinger break;
36015c6cd613SNicholas Bellinger case TMR_TARGET_COLD_RESET:
3602c66ac9dbSNicholas Bellinger tmr->response = TMR_FUNCTION_REJECTED;
3603c66ac9dbSNicholas Bellinger break;
3604c66ac9dbSNicholas Bellinger default:
3605c1678280SColin Ian King pr_err("Unknown TMR function: 0x%02x.\n",
3606c66ac9dbSNicholas Bellinger tmr->function);
3607c66ac9dbSNicholas Bellinger tmr->response = TMR_FUNCTION_REJECTED;
3608c66ac9dbSNicholas Bellinger break;
3609c66ac9dbSNicholas Bellinger }
3610c66ac9dbSNicholas Bellinger
36112c9fa49eSBart Van Assche if (cmd->transport_state & CMD_T_ABORTED)
36122c9fa49eSBart Van Assche goto aborted;
3613a6d9bb1cSNicholas Bellinger
3614e3d6f909SAndy Grover cmd->se_tfo->queue_tm_rsp(cmd);
3615c66ac9dbSNicholas Bellinger
3616f2e6b75fSBodo Stroesser transport_lun_remove_cmd(cmd);
3617b7b8bef7SChristoph Hellwig transport_cmd_check_stop_to_fabric(cmd);
36182c9fa49eSBart Van Assche return;
36192c9fa49eSBart Van Assche
36202c9fa49eSBart Van Assche aborted:
36212c9fa49eSBart Van Assche target_handle_abort(cmd);
3622c66ac9dbSNicholas Bellinger }
3623c66ac9dbSNicholas Bellinger
transport_generic_handle_tmr(struct se_cmd * cmd)3624af877292SChristoph Hellwig int transport_generic_handle_tmr(
3625af877292SChristoph Hellwig struct se_cmd *cmd)
3626c66ac9dbSNicholas Bellinger {
3627f15e9cd9SNicholas Bellinger unsigned long flags;
3628c54eeffbSNicholas Bellinger bool aborted = false;
3629f15e9cd9SNicholas Bellinger
3630*36bc5040SDmitry Bogdanov spin_lock_irqsave(&cmd->se_dev->se_tmr_lock, flags);
3631*36bc5040SDmitry Bogdanov list_add_tail(&cmd->se_tmr_req->tmr_list, &cmd->se_dev->dev_tmr_list);
3632*36bc5040SDmitry Bogdanov spin_unlock_irqrestore(&cmd->se_dev->se_tmr_lock, flags);
3633*36bc5040SDmitry Bogdanov
3634f15e9cd9SNicholas Bellinger spin_lock_irqsave(&cmd->t_state_lock, flags);
3635c54eeffbSNicholas Bellinger if (cmd->transport_state & CMD_T_ABORTED) {
3636c54eeffbSNicholas Bellinger aborted = true;
3637c54eeffbSNicholas Bellinger } else {
3638c54eeffbSNicholas Bellinger cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
3639f15e9cd9SNicholas Bellinger cmd->transport_state |= CMD_T_ACTIVE;
3640c54eeffbSNicholas Bellinger }
3641f15e9cd9SNicholas Bellinger spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3642f15e9cd9SNicholas Bellinger
3643c54eeffbSNicholas Bellinger if (aborted) {
36442c9fa49eSBart Van Assche pr_warn_ratelimited("handle_tmr caught CMD_T_ABORTED TMR %d ref_tag: %llu tag: %llu\n",
36452c9fa49eSBart Van Assche cmd->se_tmr_req->function,
3646c54eeffbSNicholas Bellinger cmd->se_tmr_req->ref_task_tag, cmd->tag);
36472c9fa49eSBart Van Assche target_handle_abort(cmd);
3648c54eeffbSNicholas Bellinger return 0;
3649c54eeffbSNicholas Bellinger }
3650c54eeffbSNicholas Bellinger
3651af877292SChristoph Hellwig INIT_WORK(&cmd->work, target_tmr_work);
3652db5b21a2SBart Van Assche schedule_work(&cmd->work);
3653c66ac9dbSNicholas Bellinger return 0;
3654c66ac9dbSNicholas Bellinger }
3655af877292SChristoph Hellwig EXPORT_SYMBOL(transport_generic_handle_tmr);
3656814e5b45SChristoph Hellwig
3657814e5b45SChristoph Hellwig bool
target_check_wce(struct se_device * dev)3658814e5b45SChristoph Hellwig target_check_wce(struct se_device *dev)
3659814e5b45SChristoph Hellwig {
3660814e5b45SChristoph Hellwig bool wce = false;
3661814e5b45SChristoph Hellwig
3662814e5b45SChristoph Hellwig if (dev->transport->get_write_cache)
3663814e5b45SChristoph Hellwig wce = dev->transport->get_write_cache(dev);
3664814e5b45SChristoph Hellwig else if (dev->dev_attrib.emulate_write_cache > 0)
3665814e5b45SChristoph Hellwig wce = true;
3666814e5b45SChristoph Hellwig
3667814e5b45SChristoph Hellwig return wce;
3668814e5b45SChristoph Hellwig }
3669814e5b45SChristoph Hellwig
3670814e5b45SChristoph Hellwig bool
target_check_fua(struct se_device * dev)3671814e5b45SChristoph Hellwig target_check_fua(struct se_device *dev)
3672814e5b45SChristoph Hellwig {
3673814e5b45SChristoph Hellwig return target_check_wce(dev) && dev->dev_attrib.emulate_fua_write > 0;
3674814e5b45SChristoph Hellwig }
3675