]>
Commit | Line | Data |
---|---|---|
c66ac9db NB |
1 | /******************************************************************************* |
2 | * Filename: target_core_transport.c | |
3 | * | |
4 | * This file contains the Generic Target Engine Core. | |
5 | * | |
6 | * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc. | |
7 | * Copyright (c) 2005, 2006, 2007 SBE, Inc. | |
8 | * Copyright (c) 2007-2010 Rising Tide Systems | |
9 | * Copyright (c) 2008-2010 Linux-iSCSI.org | |
10 | * | |
11 | * Nicholas A. Bellinger <nab@kernel.org> | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2 of the License, or | |
16 | * (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software | |
25 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
26 | * | |
27 | ******************************************************************************/ | |
28 | ||
c66ac9db NB |
29 | #include <linux/net.h> |
30 | #include <linux/delay.h> | |
31 | #include <linux/string.h> | |
32 | #include <linux/timer.h> | |
33 | #include <linux/slab.h> | |
34 | #include <linux/blkdev.h> | |
35 | #include <linux/spinlock.h> | |
c66ac9db NB |
36 | #include <linux/kthread.h> |
37 | #include <linux/in.h> | |
38 | #include <linux/cdrom.h> | |
827509e3 | 39 | #include <linux/module.h> |
015487b8 | 40 | #include <linux/ratelimit.h> |
c66ac9db NB |
41 | #include <asm/unaligned.h> |
42 | #include <net/sock.h> | |
43 | #include <net/tcp.h> | |
44 | #include <scsi/scsi.h> | |
45 | #include <scsi/scsi_cmnd.h> | |
e66ecd50 | 46 | #include <scsi/scsi_tcq.h> |
c66ac9db NB |
47 | |
48 | #include <target/target_core_base.h> | |
c4795fb2 CH |
49 | #include <target/target_core_backend.h> |
50 | #include <target/target_core_fabric.h> | |
c66ac9db NB |
51 | #include <target/target_core_configfs.h> |
52 | ||
e26d99ae | 53 | #include "target_core_internal.h" |
c66ac9db | 54 | #include "target_core_alua.h" |
c66ac9db | 55 | #include "target_core_pr.h" |
c66ac9db NB |
56 | #include "target_core_ua.h" |
57 | ||
35e0e757 | 58 | static struct workqueue_struct *target_completion_wq; |
c66ac9db | 59 | static struct kmem_cache *se_sess_cache; |
c66ac9db | 60 | struct kmem_cache *se_ua_cache; |
c66ac9db NB |
61 | struct kmem_cache *t10_pr_reg_cache; |
62 | struct kmem_cache *t10_alua_lu_gp_cache; | |
63 | struct kmem_cache *t10_alua_lu_gp_mem_cache; | |
64 | struct kmem_cache *t10_alua_tg_pt_gp_cache; | |
65 | struct kmem_cache *t10_alua_tg_pt_gp_mem_cache; | |
66 | ||
c66ac9db | 67 | static void transport_complete_task_attr(struct se_cmd *cmd); |
07bde79a | 68 | static void transport_handle_queue_full(struct se_cmd *cmd, |
e057f533 | 69 | struct se_device *dev); |
05d1c7c0 | 70 | static int transport_generic_get_mem(struct se_cmd *cmd); |
bc187ea6 | 71 | static int target_get_sess_cmd(struct se_session *, struct se_cmd *, bool); |
39c05f32 | 72 | static void transport_put_cmd(struct se_cmd *cmd); |
35e0e757 | 73 | static void target_complete_ok_work(struct work_struct *work); |
c66ac9db | 74 | |
e3d6f909 | 75 | int init_se_kmem_caches(void) |
c66ac9db | 76 | { |
c66ac9db NB |
77 | se_sess_cache = kmem_cache_create("se_sess_cache", |
78 | sizeof(struct se_session), __alignof__(struct se_session), | |
79 | 0, NULL); | |
6708bb27 AG |
80 | if (!se_sess_cache) { |
81 | pr_err("kmem_cache_create() for struct se_session" | |
c66ac9db | 82 | " failed\n"); |
c8e31f26 | 83 | goto out; |
c66ac9db NB |
84 | } |
85 | se_ua_cache = kmem_cache_create("se_ua_cache", | |
86 | sizeof(struct se_ua), __alignof__(struct se_ua), | |
87 | 0, NULL); | |
6708bb27 AG |
88 | if (!se_ua_cache) { |
89 | pr_err("kmem_cache_create() for struct se_ua failed\n"); | |
35e0e757 | 90 | goto out_free_sess_cache; |
c66ac9db | 91 | } |
c66ac9db NB |
92 | t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache", |
93 | sizeof(struct t10_pr_registration), | |
94 | __alignof__(struct t10_pr_registration), 0, NULL); | |
6708bb27 AG |
95 | if (!t10_pr_reg_cache) { |
96 | pr_err("kmem_cache_create() for struct t10_pr_registration" | |
c66ac9db | 97 | " failed\n"); |
35e0e757 | 98 | goto out_free_ua_cache; |
c66ac9db NB |
99 | } |
100 | t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache", | |
101 | sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp), | |
102 | 0, NULL); | |
6708bb27 AG |
103 | if (!t10_alua_lu_gp_cache) { |
104 | pr_err("kmem_cache_create() for t10_alua_lu_gp_cache" | |
c66ac9db | 105 | " failed\n"); |
35e0e757 | 106 | goto out_free_pr_reg_cache; |
c66ac9db NB |
107 | } |
108 | t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache", | |
109 | sizeof(struct t10_alua_lu_gp_member), | |
110 | __alignof__(struct t10_alua_lu_gp_member), 0, NULL); | |
6708bb27 AG |
111 | if (!t10_alua_lu_gp_mem_cache) { |
112 | pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_" | |
c66ac9db | 113 | "cache failed\n"); |
35e0e757 | 114 | goto out_free_lu_gp_cache; |
c66ac9db NB |
115 | } |
116 | t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache", | |
117 | sizeof(struct t10_alua_tg_pt_gp), | |
118 | __alignof__(struct t10_alua_tg_pt_gp), 0, NULL); | |
6708bb27 AG |
119 | if (!t10_alua_tg_pt_gp_cache) { |
120 | pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" | |
c66ac9db | 121 | "cache failed\n"); |
35e0e757 | 122 | goto out_free_lu_gp_mem_cache; |
c66ac9db NB |
123 | } |
124 | t10_alua_tg_pt_gp_mem_cache = kmem_cache_create( | |
125 | "t10_alua_tg_pt_gp_mem_cache", | |
126 | sizeof(struct t10_alua_tg_pt_gp_member), | |
127 | __alignof__(struct t10_alua_tg_pt_gp_member), | |
128 | 0, NULL); | |
6708bb27 AG |
129 | if (!t10_alua_tg_pt_gp_mem_cache) { |
130 | pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_" | |
c66ac9db | 131 | "mem_t failed\n"); |
35e0e757 | 132 | goto out_free_tg_pt_gp_cache; |
c66ac9db NB |
133 | } |
134 | ||
35e0e757 CH |
135 | target_completion_wq = alloc_workqueue("target_completion", |
136 | WQ_MEM_RECLAIM, 0); | |
137 | if (!target_completion_wq) | |
138 | goto out_free_tg_pt_gp_mem_cache; | |
139 | ||
c66ac9db | 140 | return 0; |
35e0e757 CH |
141 | |
142 | out_free_tg_pt_gp_mem_cache: | |
143 | kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); | |
144 | out_free_tg_pt_gp_cache: | |
145 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); | |
146 | out_free_lu_gp_mem_cache: | |
147 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); | |
148 | out_free_lu_gp_cache: | |
149 | kmem_cache_destroy(t10_alua_lu_gp_cache); | |
150 | out_free_pr_reg_cache: | |
151 | kmem_cache_destroy(t10_pr_reg_cache); | |
152 | out_free_ua_cache: | |
153 | kmem_cache_destroy(se_ua_cache); | |
154 | out_free_sess_cache: | |
155 | kmem_cache_destroy(se_sess_cache); | |
c66ac9db | 156 | out: |
e3d6f909 | 157 | return -ENOMEM; |
c66ac9db NB |
158 | } |
159 | ||
e3d6f909 | 160 | void release_se_kmem_caches(void) |
c66ac9db | 161 | { |
35e0e757 | 162 | destroy_workqueue(target_completion_wq); |
c66ac9db NB |
163 | kmem_cache_destroy(se_sess_cache); |
164 | kmem_cache_destroy(se_ua_cache); | |
c66ac9db NB |
165 | kmem_cache_destroy(t10_pr_reg_cache); |
166 | kmem_cache_destroy(t10_alua_lu_gp_cache); | |
167 | kmem_cache_destroy(t10_alua_lu_gp_mem_cache); | |
168 | kmem_cache_destroy(t10_alua_tg_pt_gp_cache); | |
169 | kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache); | |
c66ac9db NB |
170 | } |
171 | ||
e3d6f909 AG |
172 | /* This code ensures unique mib indexes are handed out. */ |
173 | static DEFINE_SPINLOCK(scsi_mib_index_lock); | |
174 | static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX]; | |
e89d15ee NB |
175 | |
176 | /* | |
177 | * Allocate a new row index for the entry type specified | |
178 | */ | |
179 | u32 scsi_get_new_index(scsi_index_t type) | |
180 | { | |
181 | u32 new_index; | |
182 | ||
e3d6f909 | 183 | BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX)); |
e89d15ee | 184 | |
e3d6f909 AG |
185 | spin_lock(&scsi_mib_index_lock); |
186 | new_index = ++scsi_mib_index[type]; | |
187 | spin_unlock(&scsi_mib_index_lock); | |
e89d15ee NB |
188 | |
189 | return new_index; | |
190 | } | |
191 | ||
dbc5623e | 192 | void transport_subsystem_check_init(void) |
c66ac9db NB |
193 | { |
194 | int ret; | |
283669d2 | 195 | static int sub_api_initialized; |
c66ac9db | 196 | |
dbc5623e NB |
197 | if (sub_api_initialized) |
198 | return; | |
199 | ||
c66ac9db NB |
200 | ret = request_module("target_core_iblock"); |
201 | if (ret != 0) | |
6708bb27 | 202 | pr_err("Unable to load target_core_iblock\n"); |
c66ac9db NB |
203 | |
204 | ret = request_module("target_core_file"); | |
205 | if (ret != 0) | |
6708bb27 | 206 | pr_err("Unable to load target_core_file\n"); |
c66ac9db NB |
207 | |
208 | ret = request_module("target_core_pscsi"); | |
209 | if (ret != 0) | |
6708bb27 | 210 | pr_err("Unable to load target_core_pscsi\n"); |
c66ac9db | 211 | |
e3d6f909 | 212 | sub_api_initialized = 1; |
c66ac9db NB |
213 | } |
214 | ||
215 | struct se_session *transport_init_session(void) | |
216 | { | |
217 | struct se_session *se_sess; | |
218 | ||
219 | se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL); | |
6708bb27 AG |
220 | if (!se_sess) { |
221 | pr_err("Unable to allocate struct se_session from" | |
c66ac9db NB |
222 | " se_sess_cache\n"); |
223 | return ERR_PTR(-ENOMEM); | |
224 | } | |
225 | INIT_LIST_HEAD(&se_sess->sess_list); | |
226 | INIT_LIST_HEAD(&se_sess->sess_acl_list); | |
a17f091d | 227 | INIT_LIST_HEAD(&se_sess->sess_cmd_list); |
a17f091d | 228 | spin_lock_init(&se_sess->sess_cmd_lock); |
41ac82b6 | 229 | kref_init(&se_sess->sess_kref); |
c66ac9db NB |
230 | |
231 | return se_sess; | |
232 | } | |
233 | EXPORT_SYMBOL(transport_init_session); | |
234 | ||
235 | /* | |
140854cb | 236 | * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called. |
c66ac9db NB |
237 | */ |
238 | void __transport_register_session( | |
239 | struct se_portal_group *se_tpg, | |
240 | struct se_node_acl *se_nacl, | |
241 | struct se_session *se_sess, | |
242 | void *fabric_sess_ptr) | |
243 | { | |
244 | unsigned char buf[PR_REG_ISID_LEN]; | |
245 | ||
246 | se_sess->se_tpg = se_tpg; | |
247 | se_sess->fabric_sess_ptr = fabric_sess_ptr; | |
248 | /* | |
249 | * Used by struct se_node_acl's under ConfigFS to locate active se_session-t | |
250 | * | |
251 | * Only set for struct se_session's that will actually be moving I/O. | |
252 | * eg: *NOT* discovery sessions. | |
253 | */ | |
254 | if (se_nacl) { | |
255 | /* | |
256 | * If the fabric module supports an ISID based TransportID, | |
257 | * save this value in binary from the fabric I_T Nexus now. | |
258 | */ | |
e3d6f909 | 259 | if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) { |
c66ac9db | 260 | memset(&buf[0], 0, PR_REG_ISID_LEN); |
e3d6f909 | 261 | se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, |
c66ac9db NB |
262 | &buf[0], PR_REG_ISID_LEN); |
263 | se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]); | |
264 | } | |
afb999ff NB |
265 | kref_get(&se_nacl->acl_kref); |
266 | ||
c66ac9db NB |
267 | spin_lock_irq(&se_nacl->nacl_sess_lock); |
268 | /* | |
269 | * The se_nacl->nacl_sess pointer will be set to the | |
270 | * last active I_T Nexus for each struct se_node_acl. | |
271 | */ | |
272 | se_nacl->nacl_sess = se_sess; | |
273 | ||
274 | list_add_tail(&se_sess->sess_acl_list, | |
275 | &se_nacl->acl_sess_list); | |
276 | spin_unlock_irq(&se_nacl->nacl_sess_lock); | |
277 | } | |
278 | list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list); | |
279 | ||
6708bb27 | 280 | pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n", |
e3d6f909 | 281 | se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr); |
c66ac9db NB |
282 | } |
283 | EXPORT_SYMBOL(__transport_register_session); | |
284 | ||
285 | void transport_register_session( | |
286 | struct se_portal_group *se_tpg, | |
287 | struct se_node_acl *se_nacl, | |
288 | struct se_session *se_sess, | |
289 | void *fabric_sess_ptr) | |
290 | { | |
140854cb NB |
291 | unsigned long flags; |
292 | ||
293 | spin_lock_irqsave(&se_tpg->session_lock, flags); | |
c66ac9db | 294 | __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr); |
140854cb | 295 | spin_unlock_irqrestore(&se_tpg->session_lock, flags); |
c66ac9db NB |
296 | } |
297 | EXPORT_SYMBOL(transport_register_session); | |
298 | ||
ecf0dd66 | 299 | static void target_release_session(struct kref *kref) |
41ac82b6 NB |
300 | { |
301 | struct se_session *se_sess = container_of(kref, | |
302 | struct se_session, sess_kref); | |
303 | struct se_portal_group *se_tpg = se_sess->se_tpg; | |
304 | ||
305 | se_tpg->se_tpg_tfo->close_session(se_sess); | |
306 | } | |
307 | ||
308 | void target_get_session(struct se_session *se_sess) | |
309 | { | |
310 | kref_get(&se_sess->sess_kref); | |
311 | } | |
312 | EXPORT_SYMBOL(target_get_session); | |
313 | ||
33933a0e | 314 | void target_put_session(struct se_session *se_sess) |
41ac82b6 | 315 | { |
4149268e JE |
316 | struct se_portal_group *tpg = se_sess->se_tpg; |
317 | ||
318 | if (tpg->se_tpg_tfo->put_session != NULL) { | |
319 | tpg->se_tpg_tfo->put_session(se_sess); | |
320 | return; | |
321 | } | |
33933a0e | 322 | kref_put(&se_sess->sess_kref, target_release_session); |
41ac82b6 NB |
323 | } |
324 | EXPORT_SYMBOL(target_put_session); | |
325 | ||
afb999ff NB |
326 | static void target_complete_nacl(struct kref *kref) |
327 | { | |
328 | struct se_node_acl *nacl = container_of(kref, | |
329 | struct se_node_acl, acl_kref); | |
330 | ||
331 | complete(&nacl->acl_free_comp); | |
332 | } | |
333 | ||
334 | void target_put_nacl(struct se_node_acl *nacl) | |
335 | { | |
336 | kref_put(&nacl->acl_kref, target_complete_nacl); | |
337 | } | |
338 | ||
c66ac9db NB |
339 | void transport_deregister_session_configfs(struct se_session *se_sess) |
340 | { | |
341 | struct se_node_acl *se_nacl; | |
23388864 | 342 | unsigned long flags; |
c66ac9db NB |
343 | /* |
344 | * Used by struct se_node_acl's under ConfigFS to locate active struct se_session | |
345 | */ | |
346 | se_nacl = se_sess->se_node_acl; | |
6708bb27 | 347 | if (se_nacl) { |
23388864 | 348 | spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags); |
337c0607 NB |
349 | if (se_nacl->acl_stop == 0) |
350 | list_del(&se_sess->sess_acl_list); | |
c66ac9db NB |
351 | /* |
352 | * If the session list is empty, then clear the pointer. | |
353 | * Otherwise, set the struct se_session pointer from the tail | |
354 | * element of the per struct se_node_acl active session list. | |
355 | */ | |
356 | if (list_empty(&se_nacl->acl_sess_list)) | |
357 | se_nacl->nacl_sess = NULL; | |
358 | else { | |
359 | se_nacl->nacl_sess = container_of( | |
360 | se_nacl->acl_sess_list.prev, | |
361 | struct se_session, sess_acl_list); | |
362 | } | |
23388864 | 363 | spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags); |
c66ac9db NB |
364 | } |
365 | } | |
366 | EXPORT_SYMBOL(transport_deregister_session_configfs); | |
367 | ||
368 | void transport_free_session(struct se_session *se_sess) | |
369 | { | |
370 | kmem_cache_free(se_sess_cache, se_sess); | |
371 | } | |
372 | EXPORT_SYMBOL(transport_free_session); | |
373 | ||
374 | void transport_deregister_session(struct se_session *se_sess) | |
375 | { | |
376 | struct se_portal_group *se_tpg = se_sess->se_tpg; | |
01468346 | 377 | struct target_core_fabric_ops *se_tfo; |
c66ac9db | 378 | struct se_node_acl *se_nacl; |
e63a8e19 | 379 | unsigned long flags; |
01468346 | 380 | bool comp_nacl = true; |
c66ac9db | 381 | |
6708bb27 | 382 | if (!se_tpg) { |
c66ac9db NB |
383 | transport_free_session(se_sess); |
384 | return; | |
385 | } | |
01468346 | 386 | se_tfo = se_tpg->se_tpg_tfo; |
c66ac9db | 387 | |
e63a8e19 | 388 | spin_lock_irqsave(&se_tpg->session_lock, flags); |
c66ac9db NB |
389 | list_del(&se_sess->sess_list); |
390 | se_sess->se_tpg = NULL; | |
391 | se_sess->fabric_sess_ptr = NULL; | |
e63a8e19 | 392 | spin_unlock_irqrestore(&se_tpg->session_lock, flags); |
c66ac9db NB |
393 | |
394 | /* | |
395 | * Determine if we need to do extra work for this initiator node's | |
396 | * struct se_node_acl if it had been previously dynamically generated. | |
397 | */ | |
398 | se_nacl = se_sess->se_node_acl; | |
01468346 NB |
399 | |
400 | spin_lock_irqsave(&se_tpg->acl_node_lock, flags); | |
401 | if (se_nacl && se_nacl->dynamic_node_acl) { | |
402 | if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) { | |
403 | list_del(&se_nacl->acl_list); | |
404 | se_tpg->num_node_acls--; | |
405 | spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags); | |
406 | core_tpg_wait_for_nacl_pr_ref(se_nacl); | |
407 | core_free_device_list_for_node(se_nacl, se_tpg); | |
408 | se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl); | |
409 | ||
410 | comp_nacl = false; | |
411 | spin_lock_irqsave(&se_tpg->acl_node_lock, flags); | |
c66ac9db | 412 | } |
c66ac9db | 413 | } |
01468346 | 414 | spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags); |
c66ac9db | 415 | |
6708bb27 | 416 | pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n", |
e3d6f909 | 417 | se_tpg->se_tpg_tfo->get_fabric_name()); |
01468346 | 418 | /* |
afb999ff NB |
419 | * If last kref is dropping now for an explict NodeACL, awake sleeping |
420 | * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group | |
421 | * removal context. | |
01468346 NB |
422 | */ |
423 | if (se_nacl && comp_nacl == true) | |
afb999ff | 424 | target_put_nacl(se_nacl); |
01468346 | 425 | |
afb999ff | 426 | transport_free_session(se_sess); |
c66ac9db NB |
427 | } |
428 | EXPORT_SYMBOL(transport_deregister_session); | |
429 | ||
430 | /* | |
a1d8b49a | 431 | * Called with cmd->t_state_lock held. |
c66ac9db | 432 | */ |
cf572a96 | 433 | static void target_remove_from_state_list(struct se_cmd *cmd) |
c66ac9db | 434 | { |
42bf829e | 435 | struct se_device *dev = cmd->se_dev; |
c66ac9db NB |
436 | unsigned long flags; |
437 | ||
42bf829e CH |
438 | if (!dev) |
439 | return; | |
c66ac9db | 440 | |
cf572a96 CH |
441 | if (cmd->transport_state & CMD_T_BUSY) |
442 | return; | |
c66ac9db | 443 | |
cf572a96 CH |
444 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
445 | if (cmd->state_active) { | |
446 | list_del(&cmd->state_list); | |
cf572a96 | 447 | cmd->state_active = false; |
c66ac9db | 448 | } |
cf572a96 | 449 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
c66ac9db NB |
450 | } |
451 | ||
f7113a47 | 452 | static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists) |
c66ac9db NB |
453 | { |
454 | unsigned long flags; | |
455 | ||
a1d8b49a | 456 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
c66ac9db NB |
457 | /* |
458 | * Determine if IOCTL context caller in requesting the stopping of this | |
459 | * command for LUN shutdown purposes. | |
460 | */ | |
7d680f3b CH |
461 | if (cmd->transport_state & CMD_T_LUN_STOP) { |
462 | pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n", | |
463 | __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd)); | |
c66ac9db | 464 | |
7d680f3b | 465 | cmd->transport_state &= ~CMD_T_ACTIVE; |
f7113a47 | 466 | if (remove_from_lists) |
cf572a96 | 467 | target_remove_from_state_list(cmd); |
a1d8b49a | 468 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 469 | |
a1d8b49a | 470 | complete(&cmd->transport_lun_stop_comp); |
c66ac9db NB |
471 | return 1; |
472 | } | |
f7113a47 CH |
473 | |
474 | if (remove_from_lists) { | |
475 | target_remove_from_state_list(cmd); | |
476 | ||
477 | /* | |
478 | * Clear struct se_cmd->se_lun before the handoff to FE. | |
479 | */ | |
480 | cmd->se_lun = NULL; | |
481 | } | |
482 | ||
c66ac9db NB |
483 | /* |
484 | * Determine if frontend context caller is requesting the stopping of | |
e3d6f909 | 485 | * this command for frontend exceptions. |
c66ac9db | 486 | */ |
7d680f3b CH |
487 | if (cmd->transport_state & CMD_T_STOP) { |
488 | pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n", | |
489 | __func__, __LINE__, | |
e3d6f909 | 490 | cmd->se_tfo->get_task_tag(cmd)); |
c66ac9db | 491 | |
a1d8b49a | 492 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 493 | |
a1d8b49a | 494 | complete(&cmd->t_transport_stop_comp); |
c66ac9db NB |
495 | return 1; |
496 | } | |
f7113a47 CH |
497 | |
498 | cmd->transport_state &= ~CMD_T_ACTIVE; | |
499 | if (remove_from_lists) { | |
500 | /* | |
501 | * Some fabric modules like tcm_loop can release | |
502 | * their internally allocated I/O reference now and | |
503 | * struct se_cmd now. | |
504 | * | |
505 | * Fabric modules are expected to return '1' here if the | |
506 | * se_cmd being passed is released at this point, | |
507 | * or zero if not being released. | |
508 | */ | |
509 | if (cmd->se_tfo->check_stop_free != NULL) { | |
510 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); | |
511 | return cmd->se_tfo->check_stop_free(cmd); | |
c66ac9db | 512 | } |
f7113a47 | 513 | } |
c66ac9db | 514 | |
a1d8b49a | 515 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db NB |
516 | return 0; |
517 | } | |
518 | ||
519 | static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd) | |
520 | { | |
f7113a47 | 521 | return transport_cmd_check_stop(cmd, true); |
c66ac9db NB |
522 | } |
523 | ||
524 | static void transport_lun_remove_cmd(struct se_cmd *cmd) | |
525 | { | |
e3d6f909 | 526 | struct se_lun *lun = cmd->se_lun; |
c66ac9db NB |
527 | unsigned long flags; |
528 | ||
529 | if (!lun) | |
530 | return; | |
531 | ||
a1d8b49a | 532 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
7d680f3b CH |
533 | if (cmd->transport_state & CMD_T_DEV_ACTIVE) { |
534 | cmd->transport_state &= ~CMD_T_DEV_ACTIVE; | |
cf572a96 | 535 | target_remove_from_state_list(cmd); |
c66ac9db | 536 | } |
a1d8b49a | 537 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 538 | |
c66ac9db | 539 | spin_lock_irqsave(&lun->lun_cmd_lock, flags); |
3d26fea0 CH |
540 | if (!list_empty(&cmd->se_lun_node)) |
541 | list_del_init(&cmd->se_lun_node); | |
c66ac9db NB |
542 | spin_unlock_irqrestore(&lun->lun_cmd_lock, flags); |
543 | } | |
544 | ||
545 | void transport_cmd_finish_abort(struct se_cmd *cmd, int remove) | |
546 | { | |
c8e31f26 | 547 | if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) |
8dc52b54 | 548 | transport_lun_remove_cmd(cmd); |
c66ac9db NB |
549 | |
550 | if (transport_cmd_check_stop_to_fabric(cmd)) | |
551 | return; | |
af877292 | 552 | if (remove) |
e6a2573f | 553 | transport_put_cmd(cmd); |
c66ac9db NB |
554 | } |
555 | ||
35e0e757 CH |
556 | static void target_complete_failure_work(struct work_struct *work) |
557 | { | |
558 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); | |
559 | ||
03e98c9e | 560 | transport_generic_request_failure(cmd); |
35e0e757 CH |
561 | } |
562 | ||
6138ed2a | 563 | /* |
d5829eac PB |
564 | * Used when asking transport to copy Sense Data from the underlying |
565 | * Linux/SCSI struct scsi_cmnd | |
6138ed2a | 566 | */ |
d5829eac | 567 | static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd) |
6138ed2a | 568 | { |
6138ed2a | 569 | struct se_device *dev = cmd->se_dev; |
6138ed2a PB |
570 | |
571 | WARN_ON(!cmd->se_lun); | |
572 | ||
573 | if (!dev) | |
d5829eac | 574 | return NULL; |
6138ed2a | 575 | |
d5829eac PB |
576 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) |
577 | return NULL; | |
6138ed2a | 578 | |
9c58b7dd | 579 | cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; |
6138ed2a | 580 | |
d5829eac | 581 | pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n", |
6138ed2a | 582 | dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status); |
9c58b7dd | 583 | return cmd->sense_buffer; |
6138ed2a PB |
584 | } |
585 | ||
5787cacd | 586 | void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status) |
c66ac9db | 587 | { |
42bf829e | 588 | struct se_device *dev = cmd->se_dev; |
5787cacd | 589 | int success = scsi_status == GOOD; |
c66ac9db | 590 | unsigned long flags; |
c66ac9db | 591 | |
5787cacd CH |
592 | cmd->scsi_status = scsi_status; |
593 | ||
594 | ||
a1d8b49a | 595 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
cf572a96 | 596 | cmd->transport_state &= ~CMD_T_BUSY; |
c66ac9db | 597 | |
c66ac9db | 598 | if (dev && dev->transport->transport_complete) { |
d5829eac PB |
599 | dev->transport->transport_complete(cmd, |
600 | cmd->t_data_sg, | |
601 | transport_get_sense_buffer(cmd)); | |
602 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) | |
c66ac9db | 603 | success = 1; |
c66ac9db NB |
604 | } |
605 | ||
606 | /* | |
5787cacd | 607 | * See if we are waiting to complete for an exception condition. |
c66ac9db | 608 | */ |
cf572a96 | 609 | if (cmd->transport_state & CMD_T_REQUEST_STOP) { |
a1d8b49a | 610 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
cf572a96 | 611 | complete(&cmd->task_stop_comp); |
c66ac9db NB |
612 | return; |
613 | } | |
2235007c CH |
614 | |
615 | if (!success) | |
7d680f3b | 616 | cmd->transport_state |= CMD_T_FAILED; |
2235007c | 617 | |
3d28934a NB |
618 | /* |
619 | * Check for case where an explict ABORT_TASK has been received | |
620 | * and transport_wait_for_tasks() will be waiting for completion.. | |
621 | */ | |
622 | if (cmd->transport_state & CMD_T_ABORTED && | |
623 | cmd->transport_state & CMD_T_STOP) { | |
624 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); | |
625 | complete(&cmd->t_transport_stop_comp); | |
626 | return; | |
627 | } else if (cmd->transport_state & CMD_T_FAILED) { | |
41e16e98 | 628 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; |
35e0e757 | 629 | INIT_WORK(&cmd->work, target_complete_failure_work); |
c66ac9db | 630 | } else { |
35e0e757 | 631 | INIT_WORK(&cmd->work, target_complete_ok_work); |
c66ac9db | 632 | } |
35e0e757 | 633 | |
35e0e757 | 634 | cmd->t_state = TRANSPORT_COMPLETE; |
3d28934a | 635 | cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE); |
a1d8b49a | 636 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 637 | |
35e0e757 | 638 | queue_work(target_completion_wq, &cmd->work); |
c66ac9db | 639 | } |
6bb35e00 CH |
640 | EXPORT_SYMBOL(target_complete_cmd); |
641 | ||
cf572a96 | 642 | static void target_add_to_state_list(struct se_cmd *cmd) |
c66ac9db | 643 | { |
cf572a96 CH |
644 | struct se_device *dev = cmd->se_dev; |
645 | unsigned long flags; | |
c66ac9db | 646 | |
cf572a96 CH |
647 | spin_lock_irqsave(&dev->execute_task_lock, flags); |
648 | if (!cmd->state_active) { | |
649 | list_add_tail(&cmd->state_list, &dev->state_list); | |
650 | cmd->state_active = true; | |
c66ac9db | 651 | } |
cf572a96 | 652 | spin_unlock_irqrestore(&dev->execute_task_lock, flags); |
c66ac9db NB |
653 | } |
654 | ||
07bde79a | 655 | /* |
f147abb4 | 656 | * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status |
07bde79a | 657 | */ |
7a6f0a1e CH |
658 | static void transport_write_pending_qf(struct se_cmd *cmd); |
659 | static void transport_complete_qf(struct se_cmd *cmd); | |
07bde79a | 660 | |
0fd97ccf | 661 | void target_qf_do_work(struct work_struct *work) |
07bde79a NB |
662 | { |
663 | struct se_device *dev = container_of(work, struct se_device, | |
664 | qf_work_queue); | |
bcac364a | 665 | LIST_HEAD(qf_cmd_list); |
07bde79a NB |
666 | struct se_cmd *cmd, *cmd_tmp; |
667 | ||
668 | spin_lock_irq(&dev->qf_cmd_lock); | |
bcac364a RD |
669 | list_splice_init(&dev->qf_cmd_list, &qf_cmd_list); |
670 | spin_unlock_irq(&dev->qf_cmd_lock); | |
07bde79a | 671 | |
bcac364a | 672 | list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) { |
07bde79a NB |
673 | list_del(&cmd->se_qf_node); |
674 | atomic_dec(&dev->dev_qf_count); | |
675 | smp_mb__after_atomic_dec(); | |
07bde79a | 676 | |
6708bb27 | 677 | pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue" |
07bde79a | 678 | " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd, |
e057f533 | 679 | (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" : |
07bde79a NB |
680 | (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING" |
681 | : "UNKNOWN"); | |
f7a5cc0b | 682 | |
7a6f0a1e CH |
683 | if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) |
684 | transport_write_pending_qf(cmd); | |
685 | else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) | |
686 | transport_complete_qf(cmd); | |
07bde79a | 687 | } |
07bde79a NB |
688 | } |
689 | ||
c66ac9db NB |
690 | unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd) |
691 | { | |
692 | switch (cmd->data_direction) { | |
693 | case DMA_NONE: | |
694 | return "NONE"; | |
695 | case DMA_FROM_DEVICE: | |
696 | return "READ"; | |
697 | case DMA_TO_DEVICE: | |
698 | return "WRITE"; | |
699 | case DMA_BIDIRECTIONAL: | |
700 | return "BIDI"; | |
701 | default: | |
702 | break; | |
703 | } | |
704 | ||
705 | return "UNKNOWN"; | |
706 | } | |
707 | ||
708 | void transport_dump_dev_state( | |
709 | struct se_device *dev, | |
710 | char *b, | |
711 | int *bl) | |
712 | { | |
713 | *bl += sprintf(b + *bl, "Status: "); | |
0fd97ccf | 714 | if (dev->export_count) |
c66ac9db | 715 | *bl += sprintf(b + *bl, "ACTIVATED"); |
0fd97ccf | 716 | else |
c66ac9db | 717 | *bl += sprintf(b + *bl, "DEACTIVATED"); |
c66ac9db | 718 | |
5f41a31d | 719 | *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth); |
11e764bd | 720 | *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n", |
0fd97ccf CH |
721 | dev->dev_attrib.block_size, |
722 | dev->dev_attrib.hw_max_sectors); | |
c66ac9db NB |
723 | *bl += sprintf(b + *bl, " "); |
724 | } | |
725 | ||
c66ac9db NB |
726 | void transport_dump_vpd_proto_id( |
727 | struct t10_vpd *vpd, | |
728 | unsigned char *p_buf, | |
729 | int p_buf_len) | |
730 | { | |
731 | unsigned char buf[VPD_TMP_BUF_SIZE]; | |
732 | int len; | |
733 | ||
734 | memset(buf, 0, VPD_TMP_BUF_SIZE); | |
735 | len = sprintf(buf, "T10 VPD Protocol Identifier: "); | |
736 | ||
737 | switch (vpd->protocol_identifier) { | |
738 | case 0x00: | |
739 | sprintf(buf+len, "Fibre Channel\n"); | |
740 | break; | |
741 | case 0x10: | |
742 | sprintf(buf+len, "Parallel SCSI\n"); | |
743 | break; | |
744 | case 0x20: | |
745 | sprintf(buf+len, "SSA\n"); | |
746 | break; | |
747 | case 0x30: | |
748 | sprintf(buf+len, "IEEE 1394\n"); | |
749 | break; | |
750 | case 0x40: | |
751 | sprintf(buf+len, "SCSI Remote Direct Memory Access" | |
752 | " Protocol\n"); | |
753 | break; | |
754 | case 0x50: | |
755 | sprintf(buf+len, "Internet SCSI (iSCSI)\n"); | |
756 | break; | |
757 | case 0x60: | |
758 | sprintf(buf+len, "SAS Serial SCSI Protocol\n"); | |
759 | break; | |
760 | case 0x70: | |
761 | sprintf(buf+len, "Automation/Drive Interface Transport" | |
762 | " Protocol\n"); | |
763 | break; | |
764 | case 0x80: | |
765 | sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n"); | |
766 | break; | |
767 | default: | |
768 | sprintf(buf+len, "Unknown 0x%02x\n", | |
769 | vpd->protocol_identifier); | |
770 | break; | |
771 | } | |
772 | ||
773 | if (p_buf) | |
774 | strncpy(p_buf, buf, p_buf_len); | |
775 | else | |
6708bb27 | 776 | pr_debug("%s", buf); |
c66ac9db NB |
777 | } |
778 | ||
779 | void | |
780 | transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83) | |
781 | { | |
782 | /* | |
783 | * Check if the Protocol Identifier Valid (PIV) bit is set.. | |
784 | * | |
785 | * from spc3r23.pdf section 7.5.1 | |
786 | */ | |
787 | if (page_83[1] & 0x80) { | |
788 | vpd->protocol_identifier = (page_83[0] & 0xf0); | |
789 | vpd->protocol_identifier_set = 1; | |
790 | transport_dump_vpd_proto_id(vpd, NULL, 0); | |
791 | } | |
792 | } | |
793 | EXPORT_SYMBOL(transport_set_vpd_proto_id); | |
794 | ||
795 | int transport_dump_vpd_assoc( | |
796 | struct t10_vpd *vpd, | |
797 | unsigned char *p_buf, | |
798 | int p_buf_len) | |
799 | { | |
800 | unsigned char buf[VPD_TMP_BUF_SIZE]; | |
e3d6f909 AG |
801 | int ret = 0; |
802 | int len; | |
c66ac9db NB |
803 | |
804 | memset(buf, 0, VPD_TMP_BUF_SIZE); | |
805 | len = sprintf(buf, "T10 VPD Identifier Association: "); | |
806 | ||
807 | switch (vpd->association) { | |
808 | case 0x00: | |
809 | sprintf(buf+len, "addressed logical unit\n"); | |
810 | break; | |
811 | case 0x10: | |
812 | sprintf(buf+len, "target port\n"); | |
813 | break; | |
814 | case 0x20: | |
815 | sprintf(buf+len, "SCSI target device\n"); | |
816 | break; | |
817 | default: | |
818 | sprintf(buf+len, "Unknown 0x%02x\n", vpd->association); | |
e3d6f909 | 819 | ret = -EINVAL; |
c66ac9db NB |
820 | break; |
821 | } | |
822 | ||
823 | if (p_buf) | |
824 | strncpy(p_buf, buf, p_buf_len); | |
825 | else | |
6708bb27 | 826 | pr_debug("%s", buf); |
c66ac9db NB |
827 | |
828 | return ret; | |
829 | } | |
830 | ||
831 | int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83) | |
832 | { | |
833 | /* | |
834 | * The VPD identification association.. | |
835 | * | |
836 | * from spc3r23.pdf Section 7.6.3.1 Table 297 | |
837 | */ | |
838 | vpd->association = (page_83[1] & 0x30); | |
839 | return transport_dump_vpd_assoc(vpd, NULL, 0); | |
840 | } | |
841 | EXPORT_SYMBOL(transport_set_vpd_assoc); | |
842 | ||
843 | int transport_dump_vpd_ident_type( | |
844 | struct t10_vpd *vpd, | |
845 | unsigned char *p_buf, | |
846 | int p_buf_len) | |
847 | { | |
848 | unsigned char buf[VPD_TMP_BUF_SIZE]; | |
e3d6f909 AG |
849 | int ret = 0; |
850 | int len; | |
c66ac9db NB |
851 | |
852 | memset(buf, 0, VPD_TMP_BUF_SIZE); | |
853 | len = sprintf(buf, "T10 VPD Identifier Type: "); | |
854 | ||
855 | switch (vpd->device_identifier_type) { | |
856 | case 0x00: | |
857 | sprintf(buf+len, "Vendor specific\n"); | |
858 | break; | |
859 | case 0x01: | |
860 | sprintf(buf+len, "T10 Vendor ID based\n"); | |
861 | break; | |
862 | case 0x02: | |
863 | sprintf(buf+len, "EUI-64 based\n"); | |
864 | break; | |
865 | case 0x03: | |
866 | sprintf(buf+len, "NAA\n"); | |
867 | break; | |
868 | case 0x04: | |
869 | sprintf(buf+len, "Relative target port identifier\n"); | |
870 | break; | |
871 | case 0x08: | |
872 | sprintf(buf+len, "SCSI name string\n"); | |
873 | break; | |
874 | default: | |
875 | sprintf(buf+len, "Unsupported: 0x%02x\n", | |
876 | vpd->device_identifier_type); | |
e3d6f909 | 877 | ret = -EINVAL; |
c66ac9db NB |
878 | break; |
879 | } | |
880 | ||
e3d6f909 AG |
881 | if (p_buf) { |
882 | if (p_buf_len < strlen(buf)+1) | |
883 | return -EINVAL; | |
c66ac9db | 884 | strncpy(p_buf, buf, p_buf_len); |
e3d6f909 | 885 | } else { |
6708bb27 | 886 | pr_debug("%s", buf); |
e3d6f909 | 887 | } |
c66ac9db NB |
888 | |
889 | return ret; | |
890 | } | |
891 | ||
892 | int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83) | |
893 | { | |
894 | /* | |
895 | * The VPD identifier type.. | |
896 | * | |
897 | * from spc3r23.pdf Section 7.6.3.1 Table 298 | |
898 | */ | |
899 | vpd->device_identifier_type = (page_83[1] & 0x0f); | |
900 | return transport_dump_vpd_ident_type(vpd, NULL, 0); | |
901 | } | |
902 | EXPORT_SYMBOL(transport_set_vpd_ident_type); | |
903 | ||
904 | int transport_dump_vpd_ident( | |
905 | struct t10_vpd *vpd, | |
906 | unsigned char *p_buf, | |
907 | int p_buf_len) | |
908 | { | |
909 | unsigned char buf[VPD_TMP_BUF_SIZE]; | |
910 | int ret = 0; | |
911 | ||
912 | memset(buf, 0, VPD_TMP_BUF_SIZE); | |
913 | ||
914 | switch (vpd->device_identifier_code_set) { | |
915 | case 0x01: /* Binary */ | |
916 | sprintf(buf, "T10 VPD Binary Device Identifier: %s\n", | |
917 | &vpd->device_identifier[0]); | |
918 | break; | |
919 | case 0x02: /* ASCII */ | |
920 | sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n", | |
921 | &vpd->device_identifier[0]); | |
922 | break; | |
923 | case 0x03: /* UTF-8 */ | |
924 | sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n", | |
925 | &vpd->device_identifier[0]); | |
926 | break; | |
927 | default: | |
928 | sprintf(buf, "T10 VPD Device Identifier encoding unsupported:" | |
929 | " 0x%02x", vpd->device_identifier_code_set); | |
e3d6f909 | 930 | ret = -EINVAL; |
c66ac9db NB |
931 | break; |
932 | } | |
933 | ||
934 | if (p_buf) | |
935 | strncpy(p_buf, buf, p_buf_len); | |
936 | else | |
6708bb27 | 937 | pr_debug("%s", buf); |
c66ac9db NB |
938 | |
939 | return ret; | |
940 | } | |
941 | ||
942 | int | |
943 | transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83) | |
944 | { | |
945 | static const char hex_str[] = "0123456789abcdef"; | |
35d1efe8 | 946 | int j = 0, i = 4; /* offset to start of the identifier */ |
c66ac9db NB |
947 | |
948 | /* | |
949 | * The VPD Code Set (encoding) | |
950 | * | |
951 | * from spc3r23.pdf Section 7.6.3.1 Table 296 | |
952 | */ | |
953 | vpd->device_identifier_code_set = (page_83[0] & 0x0f); | |
954 | switch (vpd->device_identifier_code_set) { | |
955 | case 0x01: /* Binary */ | |
956 | vpd->device_identifier[j++] = | |
957 | hex_str[vpd->device_identifier_type]; | |
958 | while (i < (4 + page_83[3])) { | |
959 | vpd->device_identifier[j++] = | |
960 | hex_str[(page_83[i] & 0xf0) >> 4]; | |
961 | vpd->device_identifier[j++] = | |
962 | hex_str[page_83[i] & 0x0f]; | |
963 | i++; | |
964 | } | |
965 | break; | |
966 | case 0x02: /* ASCII */ | |
967 | case 0x03: /* UTF-8 */ | |
968 | while (i < (4 + page_83[3])) | |
969 | vpd->device_identifier[j++] = page_83[i++]; | |
970 | break; | |
971 | default: | |
972 | break; | |
973 | } | |
974 | ||
975 | return transport_dump_vpd_ident(vpd, NULL, 0); | |
976 | } | |
977 | EXPORT_SYMBOL(transport_set_vpd_ident); | |
978 | ||
1fd032ee | 979 | int target_cmd_size_check(struct se_cmd *cmd, unsigned int size) |
9b3b8041 CH |
980 | { |
981 | struct se_device *dev = cmd->se_dev; | |
982 | ||
983 | if (cmd->unknown_data_length) { | |
984 | cmd->data_length = size; | |
985 | } else if (size != cmd->data_length) { | |
986 | pr_warn("TARGET_CORE[%s]: Expected Transfer Length:" | |
987 | " %u does not match SCSI CDB Length: %u for SAM Opcode:" | |
988 | " 0x%02x\n", cmd->se_tfo->get_fabric_name(), | |
989 | cmd->data_length, size, cmd->t_task_cdb[0]); | |
990 | ||
9b3b8041 CH |
991 | if (cmd->data_direction == DMA_TO_DEVICE) { |
992 | pr_err("Rejecting underflow/overflow" | |
993 | " WRITE data\n"); | |
994 | goto out_invalid_cdb_field; | |
995 | } | |
996 | /* | |
997 | * Reject READ_* or WRITE_* with overflow/underflow for | |
998 | * type SCF_SCSI_DATA_CDB. | |
999 | */ | |
0fd97ccf | 1000 | if (dev->dev_attrib.block_size != 512) { |
9b3b8041 CH |
1001 | pr_err("Failing OVERFLOW/UNDERFLOW for LBA op" |
1002 | " CDB on non 512-byte sector setup subsystem" | |
1003 | " plugin: %s\n", dev->transport->name); | |
1004 | /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */ | |
1005 | goto out_invalid_cdb_field; | |
1006 | } | |
4c054ba6 NB |
1007 | /* |
1008 | * For the overflow case keep the existing fabric provided | |
1009 | * ->data_length. Otherwise for the underflow case, reset | |
1010 | * ->data_length to the smaller SCSI expected data transfer | |
1011 | * length. | |
1012 | */ | |
9b3b8041 CH |
1013 | if (size > cmd->data_length) { |
1014 | cmd->se_cmd_flags |= SCF_OVERFLOW_BIT; | |
1015 | cmd->residual_count = (size - cmd->data_length); | |
1016 | } else { | |
1017 | cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT; | |
1018 | cmd->residual_count = (cmd->data_length - size); | |
4c054ba6 | 1019 | cmd->data_length = size; |
9b3b8041 | 1020 | } |
9b3b8041 CH |
1021 | } |
1022 | ||
1023 | return 0; | |
1024 | ||
1025 | out_invalid_cdb_field: | |
1026 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | |
1027 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; | |
1028 | return -EINVAL; | |
1029 | } | |
1030 | ||
c66ac9db NB |
1031 | /* |
1032 | * Used by fabric modules containing a local struct se_cmd within their | |
1033 | * fabric dependent per I/O descriptor. | |
1034 | */ | |
1035 | void transport_init_se_cmd( | |
1036 | struct se_cmd *cmd, | |
1037 | struct target_core_fabric_ops *tfo, | |
1038 | struct se_session *se_sess, | |
1039 | u32 data_length, | |
1040 | int data_direction, | |
1041 | int task_attr, | |
1042 | unsigned char *sense_buffer) | |
1043 | { | |
5951146d AG |
1044 | INIT_LIST_HEAD(&cmd->se_lun_node); |
1045 | INIT_LIST_HEAD(&cmd->se_delayed_node); | |
07bde79a | 1046 | INIT_LIST_HEAD(&cmd->se_qf_node); |
a17f091d | 1047 | INIT_LIST_HEAD(&cmd->se_cmd_list); |
cf572a96 | 1048 | INIT_LIST_HEAD(&cmd->state_list); |
a1d8b49a AG |
1049 | init_completion(&cmd->transport_lun_fe_stop_comp); |
1050 | init_completion(&cmd->transport_lun_stop_comp); | |
1051 | init_completion(&cmd->t_transport_stop_comp); | |
a17f091d | 1052 | init_completion(&cmd->cmd_wait_comp); |
cf572a96 | 1053 | init_completion(&cmd->task_stop_comp); |
a1d8b49a | 1054 | spin_lock_init(&cmd->t_state_lock); |
7d680f3b | 1055 | cmd->transport_state = CMD_T_DEV_ACTIVE; |
c66ac9db NB |
1056 | |
1057 | cmd->se_tfo = tfo; | |
1058 | cmd->se_sess = se_sess; | |
1059 | cmd->data_length = data_length; | |
1060 | cmd->data_direction = data_direction; | |
1061 | cmd->sam_task_attr = task_attr; | |
1062 | cmd->sense_buffer = sense_buffer; | |
cf572a96 CH |
1063 | |
1064 | cmd->state_active = false; | |
c66ac9db NB |
1065 | } |
1066 | EXPORT_SYMBOL(transport_init_se_cmd); | |
1067 | ||
1068 | static int transport_check_alloc_task_attr(struct se_cmd *cmd) | |
1069 | { | |
019c4ca6 CH |
1070 | struct se_device *dev = cmd->se_dev; |
1071 | ||
c66ac9db NB |
1072 | /* |
1073 | * Check if SAM Task Attribute emulation is enabled for this | |
1074 | * struct se_device storage object | |
1075 | */ | |
019c4ca6 | 1076 | if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) |
c66ac9db NB |
1077 | return 0; |
1078 | ||
e66ecd50 | 1079 | if (cmd->sam_task_attr == MSG_ACA_TAG) { |
6708bb27 | 1080 | pr_debug("SAM Task Attribute ACA" |
c66ac9db | 1081 | " emulation is not supported\n"); |
e3d6f909 | 1082 | return -EINVAL; |
c66ac9db NB |
1083 | } |
1084 | /* | |
1085 | * Used to determine when ORDERED commands should go from | |
1086 | * Dormant to Active status. | |
1087 | */ | |
019c4ca6 | 1088 | cmd->se_ordered_id = atomic_inc_return(&dev->dev_ordered_id); |
c66ac9db | 1089 | smp_mb__after_atomic_inc(); |
6708bb27 | 1090 | pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n", |
c66ac9db | 1091 | cmd->se_ordered_id, cmd->sam_task_attr, |
019c4ca6 | 1092 | dev->transport->name); |
c66ac9db NB |
1093 | return 0; |
1094 | } | |
1095 | ||
a12f41f8 | 1096 | /* target_setup_cmd_from_cdb(): |
c66ac9db NB |
1097 | * |
1098 | * Called from fabric RX Thread. | |
1099 | */ | |
a12f41f8 | 1100 | int target_setup_cmd_from_cdb( |
c66ac9db NB |
1101 | struct se_cmd *cmd, |
1102 | unsigned char *cdb) | |
1103 | { | |
0fd97ccf | 1104 | struct se_device *dev = cmd->se_dev; |
cb4f4d3c | 1105 | unsigned long flags; |
c66ac9db NB |
1106 | int ret; |
1107 | ||
c66ac9db NB |
1108 | /* |
1109 | * Ensure that the received CDB is less than the max (252 + 8) bytes | |
1110 | * for VARIABLE_LENGTH_CMD | |
1111 | */ | |
1112 | if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) { | |
6708bb27 | 1113 | pr_err("Received SCSI CDB with command_size: %d that" |
c66ac9db NB |
1114 | " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n", |
1115 | scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE); | |
03e98c9e NB |
1116 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
1117 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; | |
e3d6f909 | 1118 | return -EINVAL; |
c66ac9db NB |
1119 | } |
1120 | /* | |
1121 | * If the received CDB is larger than TCM_MAX_COMMAND_SIZE, | |
1122 | * allocate the additional extended CDB buffer now.. Otherwise | |
1123 | * setup the pointer from __t_task_cdb to t_task_cdb. | |
1124 | */ | |
a1d8b49a AG |
1125 | if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) { |
1126 | cmd->t_task_cdb = kzalloc(scsi_command_size(cdb), | |
c66ac9db | 1127 | GFP_KERNEL); |
6708bb27 AG |
1128 | if (!cmd->t_task_cdb) { |
1129 | pr_err("Unable to allocate cmd->t_task_cdb" | |
a1d8b49a | 1130 | " %u > sizeof(cmd->__t_task_cdb): %lu ops\n", |
c66ac9db | 1131 | scsi_command_size(cdb), |
a1d8b49a | 1132 | (unsigned long)sizeof(cmd->__t_task_cdb)); |
03e98c9e NB |
1133 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
1134 | cmd->scsi_sense_reason = | |
1135 | TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
e3d6f909 | 1136 | return -ENOMEM; |
c66ac9db NB |
1137 | } |
1138 | } else | |
a1d8b49a | 1139 | cmd->t_task_cdb = &cmd->__t_task_cdb[0]; |
c66ac9db | 1140 | /* |
a1d8b49a | 1141 | * Copy the original CDB into cmd-> |
c66ac9db | 1142 | */ |
a1d8b49a | 1143 | memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb)); |
cb4f4d3c CH |
1144 | |
1145 | /* | |
1146 | * Check for an existing UNIT ATTENTION condition | |
1147 | */ | |
1148 | if (core_scsi3_ua_check(cmd, cdb) < 0) { | |
1149 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | |
1150 | cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION; | |
1151 | return -EINVAL; | |
1152 | } | |
1153 | ||
c87fbd56 CH |
1154 | ret = target_alua_state_check(cmd); |
1155 | if (ret) { | |
cb4f4d3c | 1156 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; |
c87fbd56 CH |
1157 | if (ret > 0) |
1158 | cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY; | |
1159 | else | |
1160 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; | |
cb4f4d3c CH |
1161 | return -EINVAL; |
1162 | } | |
1163 | ||
1164 | /* | |
1165 | * Check status for SPC-3 Persistent Reservations | |
1166 | */ | |
d977f437 CH |
1167 | ret = target_check_reservation(cmd); |
1168 | if (ret) { | |
1169 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | |
1170 | cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT; | |
1171 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; | |
1172 | cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT; | |
1173 | return ret; | |
cb4f4d3c CH |
1174 | } |
1175 | ||
0fd97ccf | 1176 | ret = dev->transport->parse_cdb(cmd); |
c66ac9db NB |
1177 | if (ret < 0) |
1178 | return ret; | |
cb4f4d3c CH |
1179 | |
1180 | spin_lock_irqsave(&cmd->t_state_lock, flags); | |
1181 | cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE; | |
1182 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); | |
1183 | ||
c66ac9db NB |
1184 | /* |
1185 | * Check for SAM Task Attribute Emulation | |
1186 | */ | |
1187 | if (transport_check_alloc_task_attr(cmd) < 0) { | |
1188 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | |
1189 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; | |
5951146d | 1190 | return -EINVAL; |
c66ac9db NB |
1191 | } |
1192 | spin_lock(&cmd->se_lun->lun_sep_lock); | |
1193 | if (cmd->se_lun->lun_sep) | |
1194 | cmd->se_lun->lun_sep->sep_stats.cmd_pdus++; | |
1195 | spin_unlock(&cmd->se_lun->lun_sep_lock); | |
1196 | return 0; | |
1197 | } | |
a12f41f8 | 1198 | EXPORT_SYMBOL(target_setup_cmd_from_cdb); |
c66ac9db | 1199 | |
695434e1 NB |
1200 | /* |
1201 | * Used by fabric module frontends to queue tasks directly. | |
1202 | * Many only be used from process context only | |
1203 | */ | |
1204 | int transport_handle_cdb_direct( | |
1205 | struct se_cmd *cmd) | |
1206 | { | |
dd8ae59d NB |
1207 | int ret; |
1208 | ||
695434e1 NB |
1209 | if (!cmd->se_lun) { |
1210 | dump_stack(); | |
6708bb27 | 1211 | pr_err("cmd->se_lun is NULL\n"); |
695434e1 NB |
1212 | return -EINVAL; |
1213 | } | |
1214 | if (in_interrupt()) { | |
1215 | dump_stack(); | |
6708bb27 | 1216 | pr_err("transport_generic_handle_cdb cannot be called" |
695434e1 NB |
1217 | " from interrupt context\n"); |
1218 | return -EINVAL; | |
1219 | } | |
dd8ae59d | 1220 | /* |
af877292 CH |
1221 | * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that |
1222 | * outstanding descriptors are handled correctly during shutdown via | |
1223 | * transport_wait_for_tasks() | |
dd8ae59d NB |
1224 | * |
1225 | * Also, we don't take cmd->t_state_lock here as we only expect | |
1226 | * this to be called for initial descriptor submission. | |
1227 | */ | |
1228 | cmd->t_state = TRANSPORT_NEW_CMD; | |
7d680f3b CH |
1229 | cmd->transport_state |= CMD_T_ACTIVE; |
1230 | ||
dd8ae59d NB |
1231 | /* |
1232 | * transport_generic_new_cmd() is already handling QUEUE_FULL, | |
1233 | * so follow TRANSPORT_NEW_CMD processing thread context usage | |
1234 | * and call transport_generic_request_failure() if necessary.. | |
1235 | */ | |
1236 | ret = transport_generic_new_cmd(cmd); | |
03e98c9e NB |
1237 | if (ret < 0) |
1238 | transport_generic_request_failure(cmd); | |
1239 | ||
dd8ae59d | 1240 | return 0; |
695434e1 NB |
1241 | } |
1242 | EXPORT_SYMBOL(transport_handle_cdb_direct); | |
1243 | ||
a026757f NB |
1244 | /* |
1245 | * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized | |
1246 | * se_cmd + use pre-allocated SGL memory. | |
a6360785 NB |
1247 | * |
1248 | * @se_cmd: command descriptor to submit | |
1249 | * @se_sess: associated se_sess for endpoint | |
1250 | * @cdb: pointer to SCSI CDB | |
1251 | * @sense: pointer to SCSI sense buffer | |
1252 | * @unpacked_lun: unpacked LUN to reference for struct se_lun | |
1253 | * @data_length: fabric expected data transfer length | |
1254 | * @task_addr: SAM task attribute | |
1255 | * @data_dir: DMA data direction | |
1256 | * @flags: flags for command submission from target_sc_flags_tables | |
a026757f NB |
1257 | * @sgl: struct scatterlist memory for unidirectional mapping |
1258 | * @sgl_count: scatterlist count for unidirectional mapping | |
1259 | * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping | |
1260 | * @sgl_bidi_count: scatterlist count for bidirectional READ mapping | |
a6360785 | 1261 | * |
d6dfc868 RD |
1262 | * Returns non zero to signal active I/O shutdown failure. All other |
1263 | * setup exceptions will be returned as a SCSI CHECK_CONDITION response, | |
1264 | * but still return zero here. | |
1265 | * | |
a6360785 NB |
1266 | * This may only be called from process context, and also currently |
1267 | * assumes internal allocation of fabric payload buffer by target-core. | |
a026757f NB |
1268 | */ |
1269 | int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess, | |
a6360785 | 1270 | unsigned char *cdb, unsigned char *sense, u32 unpacked_lun, |
a026757f NB |
1271 | u32 data_length, int task_attr, int data_dir, int flags, |
1272 | struct scatterlist *sgl, u32 sgl_count, | |
1273 | struct scatterlist *sgl_bidi, u32 sgl_bidi_count) | |
a6360785 NB |
1274 | { |
1275 | struct se_portal_group *se_tpg; | |
1276 | int rc; | |
1277 | ||
1278 | se_tpg = se_sess->se_tpg; | |
1279 | BUG_ON(!se_tpg); | |
1280 | BUG_ON(se_cmd->se_tfo || se_cmd->se_sess); | |
1281 | BUG_ON(in_interrupt()); | |
1282 | /* | |
1283 | * Initialize se_cmd for target operation. From this point | |
1284 | * exceptions are handled by sending exception status via | |
1285 | * target_core_fabric_ops->queue_status() callback | |
1286 | */ | |
1287 | transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, | |
1288 | data_length, data_dir, task_attr, sense); | |
b0d79946 SAS |
1289 | if (flags & TARGET_SCF_UNKNOWN_SIZE) |
1290 | se_cmd->unknown_data_length = 1; | |
a6360785 NB |
1291 | /* |
1292 | * Obtain struct se_cmd->cmd_kref reference and add new cmd to | |
1293 | * se_sess->sess_cmd_list. A second kref_get here is necessary | |
1294 | * for fabrics using TARGET_SCF_ACK_KREF that expect a second | |
1295 | * kref_put() to happen during fabric packet acknowledgement. | |
1296 | */ | |
bc187ea6 RD |
1297 | rc = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF)); |
1298 | if (rc) | |
d6dfc868 | 1299 | return rc; |
a6360785 NB |
1300 | /* |
1301 | * Signal bidirectional data payloads to target-core | |
1302 | */ | |
1303 | if (flags & TARGET_SCF_BIDI_OP) | |
1304 | se_cmd->se_cmd_flags |= SCF_BIDI; | |
1305 | /* | |
1306 | * Locate se_lun pointer and attach it to struct se_cmd | |
1307 | */ | |
735703ca NB |
1308 | if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) { |
1309 | transport_send_check_condition_and_sense(se_cmd, | |
1310 | se_cmd->scsi_sense_reason, 0); | |
1311 | target_put_sess_cmd(se_sess, se_cmd); | |
d6dfc868 | 1312 | return 0; |
735703ca | 1313 | } |
d6e0175c | 1314 | |
a12f41f8 | 1315 | rc = target_setup_cmd_from_cdb(se_cmd, cdb); |
735703ca NB |
1316 | if (rc != 0) { |
1317 | transport_generic_request_failure(se_cmd); | |
d6dfc868 | 1318 | return 0; |
735703ca | 1319 | } |
a026757f NB |
1320 | /* |
1321 | * When a non zero sgl_count has been passed perform SGL passthrough | |
1322 | * mapping for pre-allocated fabric memory instead of having target | |
1323 | * core perform an internal SGL allocation.. | |
1324 | */ | |
1325 | if (sgl_count != 0) { | |
1326 | BUG_ON(!sgl); | |
1327 | ||
944981c7 NB |
1328 | /* |
1329 | * A work-around for tcm_loop as some userspace code via | |
1330 | * scsi-generic do not memset their associated read buffers, | |
1331 | * so go ahead and do that here for type non-data CDBs. Also | |
1332 | * note that this is currently guaranteed to be a single SGL | |
1333 | * for this case by target core in target_setup_cmd_from_cdb() | |
1334 | * -> transport_generic_cmd_sequencer(). | |
1335 | */ | |
1336 | if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) && | |
1337 | se_cmd->data_direction == DMA_FROM_DEVICE) { | |
1338 | unsigned char *buf = NULL; | |
1339 | ||
1340 | if (sgl) | |
1341 | buf = kmap(sg_page(sgl)) + sgl->offset; | |
1342 | ||
1343 | if (buf) { | |
1344 | memset(buf, 0, sgl->length); | |
1345 | kunmap(sg_page(sgl)); | |
1346 | } | |
1347 | } | |
1348 | ||
a026757f NB |
1349 | rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count, |
1350 | sgl_bidi, sgl_bidi_count); | |
1351 | if (rc != 0) { | |
1352 | transport_generic_request_failure(se_cmd); | |
1353 | return 0; | |
1354 | } | |
1355 | } | |
11e319ed AG |
1356 | /* |
1357 | * Check if we need to delay processing because of ALUA | |
1358 | * Active/NonOptimized primary access state.. | |
1359 | */ | |
1360 | core_alua_check_nonop_delay(se_cmd); | |
1361 | ||
a6360785 | 1362 | transport_handle_cdb_direct(se_cmd); |
d6dfc868 | 1363 | return 0; |
a6360785 | 1364 | } |
a026757f NB |
1365 | EXPORT_SYMBOL(target_submit_cmd_map_sgls); |
1366 | ||
1367 | /* | |
1368 | * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd | |
1369 | * | |
1370 | * @se_cmd: command descriptor to submit | |
1371 | * @se_sess: associated se_sess for endpoint | |
1372 | * @cdb: pointer to SCSI CDB | |
1373 | * @sense: pointer to SCSI sense buffer | |
1374 | * @unpacked_lun: unpacked LUN to reference for struct se_lun | |
1375 | * @data_length: fabric expected data transfer length | |
1376 | * @task_addr: SAM task attribute | |
1377 | * @data_dir: DMA data direction | |
1378 | * @flags: flags for command submission from target_sc_flags_tables | |
1379 | * | |
1380 | * Returns non zero to signal active I/O shutdown failure. All other | |
1381 | * setup exceptions will be returned as a SCSI CHECK_CONDITION response, | |
1382 | * but still return zero here. | |
1383 | * | |
1384 | * This may only be called from process context, and also currently | |
1385 | * assumes internal allocation of fabric payload buffer by target-core. | |
1386 | * | |
1387 | * It also assumes interal target core SGL memory allocation. | |
1388 | */ | |
1389 | int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess, | |
1390 | unsigned char *cdb, unsigned char *sense, u32 unpacked_lun, | |
1391 | u32 data_length, int task_attr, int data_dir, int flags) | |
1392 | { | |
1393 | return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense, | |
1394 | unpacked_lun, data_length, task_attr, data_dir, | |
1395 | flags, NULL, 0, NULL, 0); | |
1396 | } | |
a6360785 NB |
1397 | EXPORT_SYMBOL(target_submit_cmd); |
1398 | ||
9f0d05c2 NB |
1399 | static void target_complete_tmr_failure(struct work_struct *work) |
1400 | { | |
1401 | struct se_cmd *se_cmd = container_of(work, struct se_cmd, work); | |
1402 | ||
1403 | se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST; | |
1404 | se_cmd->se_tfo->queue_tm_rsp(se_cmd); | |
9f0d05c2 NB |
1405 | } |
1406 | ||
ea98d7f9 AG |
1407 | /** |
1408 | * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd | |
1409 | * for TMR CDBs | |
1410 | * | |
1411 | * @se_cmd: command descriptor to submit | |
1412 | * @se_sess: associated se_sess for endpoint | |
1413 | * @sense: pointer to SCSI sense buffer | |
1414 | * @unpacked_lun: unpacked LUN to reference for struct se_lun | |
1415 | * @fabric_context: fabric context for TMR req | |
1416 | * @tm_type: Type of TM request | |
c0974f89 NB |
1417 | * @gfp: gfp type for caller |
1418 | * @tag: referenced task tag for TMR_ABORT_TASK | |
c7042cae | 1419 | * @flags: submit cmd flags |
ea98d7f9 AG |
1420 | * |
1421 | * Callable from all contexts. | |
1422 | **/ | |
1423 | ||
c7042cae | 1424 | int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess, |
ea98d7f9 | 1425 | unsigned char *sense, u32 unpacked_lun, |
c0974f89 NB |
1426 | void *fabric_tmr_ptr, unsigned char tm_type, |
1427 | gfp_t gfp, unsigned int tag, int flags) | |
ea98d7f9 AG |
1428 | { |
1429 | struct se_portal_group *se_tpg; | |
1430 | int ret; | |
1431 | ||
1432 | se_tpg = se_sess->se_tpg; | |
1433 | BUG_ON(!se_tpg); | |
1434 | ||
1435 | transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess, | |
1436 | 0, DMA_NONE, MSG_SIMPLE_TAG, sense); | |
c7042cae NB |
1437 | /* |
1438 | * FIXME: Currently expect caller to handle se_cmd->se_tmr_req | |
1439 | * allocation failure. | |
1440 | */ | |
c0974f89 | 1441 | ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp); |
c7042cae NB |
1442 | if (ret < 0) |
1443 | return -ENOMEM; | |
ea98d7f9 | 1444 | |
c0974f89 NB |
1445 | if (tm_type == TMR_ABORT_TASK) |
1446 | se_cmd->se_tmr_req->ref_task_tag = tag; | |
1447 | ||
ea98d7f9 | 1448 | /* See target_submit_cmd for commentary */ |
bc187ea6 RD |
1449 | ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF)); |
1450 | if (ret) { | |
1451 | core_tmr_release_req(se_cmd->se_tmr_req); | |
1452 | return ret; | |
1453 | } | |
ea98d7f9 | 1454 | |
ea98d7f9 AG |
1455 | ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun); |
1456 | if (ret) { | |
9f0d05c2 NB |
1457 | /* |
1458 | * For callback during failure handling, push this work off | |
1459 | * to process context with TMR_LUN_DOES_NOT_EXIST status. | |
1460 | */ | |
1461 | INIT_WORK(&se_cmd->work, target_complete_tmr_failure); | |
1462 | schedule_work(&se_cmd->work); | |
c7042cae | 1463 | return 0; |
ea98d7f9 AG |
1464 | } |
1465 | transport_generic_handle_tmr(se_cmd); | |
c7042cae | 1466 | return 0; |
ea98d7f9 AG |
1467 | } |
1468 | EXPORT_SYMBOL(target_submit_tmr); | |
1469 | ||
cdbb70bb | 1470 | /* |
cf572a96 | 1471 | * If the cmd is active, request it to be stopped and sleep until it |
cdbb70bb CH |
1472 | * has completed. |
1473 | */ | |
cf572a96 | 1474 | bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags) |
cdbb70bb | 1475 | { |
cdbb70bb CH |
1476 | bool was_active = false; |
1477 | ||
cf572a96 CH |
1478 | if (cmd->transport_state & CMD_T_BUSY) { |
1479 | cmd->transport_state |= CMD_T_REQUEST_STOP; | |
cdbb70bb CH |
1480 | spin_unlock_irqrestore(&cmd->t_state_lock, *flags); |
1481 | ||
cf572a96 CH |
1482 | pr_debug("cmd %p waiting to complete\n", cmd); |
1483 | wait_for_completion(&cmd->task_stop_comp); | |
1484 | pr_debug("cmd %p stopped successfully\n", cmd); | |
cdbb70bb CH |
1485 | |
1486 | spin_lock_irqsave(&cmd->t_state_lock, *flags); | |
cf572a96 CH |
1487 | cmd->transport_state &= ~CMD_T_REQUEST_STOP; |
1488 | cmd->transport_state &= ~CMD_T_BUSY; | |
cdbb70bb CH |
1489 | was_active = true; |
1490 | } | |
1491 | ||
cdbb70bb CH |
1492 | return was_active; |
1493 | } | |
1494 | ||
c66ac9db NB |
1495 | /* |
1496 | * Handle SAM-esque emulation for generic transport request failures. | |
1497 | */ | |
2fbff127 | 1498 | void transport_generic_request_failure(struct se_cmd *cmd) |
c66ac9db | 1499 | { |
07bde79a NB |
1500 | int ret = 0; |
1501 | ||
6708bb27 | 1502 | pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x" |
e3d6f909 | 1503 | " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd), |
a1d8b49a | 1504 | cmd->t_task_cdb[0]); |
03e98c9e | 1505 | pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n", |
e3d6f909 | 1506 | cmd->se_tfo->get_cmd_state(cmd), |
03e98c9e | 1507 | cmd->t_state, cmd->scsi_sense_reason); |
d43d6aea | 1508 | pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n", |
7d680f3b CH |
1509 | (cmd->transport_state & CMD_T_ACTIVE) != 0, |
1510 | (cmd->transport_state & CMD_T_STOP) != 0, | |
1511 | (cmd->transport_state & CMD_T_SENT) != 0); | |
c66ac9db | 1512 | |
c66ac9db NB |
1513 | /* |
1514 | * For SAM Task Attribute emulation for failed struct se_cmd | |
1515 | */ | |
019c4ca6 | 1516 | transport_complete_task_attr(cmd); |
c66ac9db | 1517 | |
03e98c9e NB |
1518 | switch (cmd->scsi_sense_reason) { |
1519 | case TCM_NON_EXISTENT_LUN: | |
1520 | case TCM_UNSUPPORTED_SCSI_OPCODE: | |
1521 | case TCM_INVALID_CDB_FIELD: | |
1522 | case TCM_INVALID_PARAMETER_LIST: | |
1523 | case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: | |
1524 | case TCM_UNKNOWN_MODE_PAGE: | |
1525 | case TCM_WRITE_PROTECTED: | |
e2397c70 | 1526 | case TCM_ADDRESS_OUT_OF_RANGE: |
03e98c9e NB |
1527 | case TCM_CHECK_CONDITION_ABORT_CMD: |
1528 | case TCM_CHECK_CONDITION_UNIT_ATTENTION: | |
1529 | case TCM_CHECK_CONDITION_NOT_READY: | |
c66ac9db | 1530 | break; |
03e98c9e | 1531 | case TCM_RESERVATION_CONFLICT: |
c66ac9db NB |
1532 | /* |
1533 | * No SENSE Data payload for this case, set SCSI Status | |
1534 | * and queue the response to $FABRIC_MOD. | |
1535 | * | |
1536 | * Uses linux/include/scsi/scsi.h SAM status codes defs | |
1537 | */ | |
1538 | cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT; | |
1539 | /* | |
1540 | * For UA Interlock Code 11b, a RESERVATION CONFLICT will | |
1541 | * establish a UNIT ATTENTION with PREVIOUS RESERVATION | |
1542 | * CONFLICT STATUS. | |
1543 | * | |
1544 | * See spc4r17, section 7.4.6 Control Mode Page, Table 349 | |
1545 | */ | |
e3d6f909 | 1546 | if (cmd->se_sess && |
0fd97ccf | 1547 | cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2) |
e3d6f909 | 1548 | core_scsi3_ua_allocate(cmd->se_sess->se_node_acl, |
c66ac9db NB |
1549 | cmd->orig_fe_lun, 0x2C, |
1550 | ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS); | |
1551 | ||
07bde79a | 1552 | ret = cmd->se_tfo->queue_status(cmd); |
f147abb4 | 1553 | if (ret == -EAGAIN || ret == -ENOMEM) |
07bde79a | 1554 | goto queue_full; |
c66ac9db | 1555 | goto check_stop; |
c66ac9db | 1556 | default: |
6708bb27 | 1557 | pr_err("Unknown transport error for CDB 0x%02x: %d\n", |
03e98c9e | 1558 | cmd->t_task_cdb[0], cmd->scsi_sense_reason); |
c66ac9db NB |
1559 | cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE; |
1560 | break; | |
1561 | } | |
f3146437 | 1562 | |
03e98c9e NB |
1563 | ret = transport_send_check_condition_and_sense(cmd, |
1564 | cmd->scsi_sense_reason, 0); | |
1565 | if (ret == -EAGAIN || ret == -ENOMEM) | |
1566 | goto queue_full; | |
07bde79a | 1567 | |
c66ac9db NB |
1568 | check_stop: |
1569 | transport_lun_remove_cmd(cmd); | |
6708bb27 | 1570 | if (!transport_cmd_check_stop_to_fabric(cmd)) |
c66ac9db | 1571 | ; |
07bde79a NB |
1572 | return; |
1573 | ||
1574 | queue_full: | |
e057f533 CH |
1575 | cmd->t_state = TRANSPORT_COMPLETE_QF_OK; |
1576 | transport_handle_queue_full(cmd, cmd->se_dev); | |
c66ac9db | 1577 | } |
2fbff127 | 1578 | EXPORT_SYMBOL(transport_generic_request_failure); |
c66ac9db | 1579 | |
5f41a31d | 1580 | static void __target_execute_cmd(struct se_cmd *cmd) |
c66ac9db | 1581 | { |
0c2ad7d1 | 1582 | int error = 0; |
5f41a31d CH |
1583 | |
1584 | spin_lock_irq(&cmd->t_state_lock); | |
1585 | cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT); | |
1586 | spin_unlock_irq(&cmd->t_state_lock); | |
1587 | ||
1588 | if (cmd->execute_cmd) | |
1589 | error = cmd->execute_cmd(cmd); | |
5f41a31d CH |
1590 | |
1591 | if (error) { | |
1592 | spin_lock_irq(&cmd->t_state_lock); | |
1593 | cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT); | |
1594 | spin_unlock_irq(&cmd->t_state_lock); | |
1595 | ||
1596 | transport_generic_request_failure(cmd); | |
1597 | } | |
1598 | } | |
1599 | ||
019c4ca6 | 1600 | static bool target_handle_task_attr(struct se_cmd *cmd) |
5f41a31d CH |
1601 | { |
1602 | struct se_device *dev = cmd->se_dev; | |
1603 | ||
019c4ca6 CH |
1604 | if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) |
1605 | return false; | |
5f41a31d | 1606 | |
c66ac9db | 1607 | /* |
25985edc | 1608 | * Check for the existence of HEAD_OF_QUEUE, and if true return 1 |
c66ac9db NB |
1609 | * to allow the passed struct se_cmd list of tasks to the front of the list. |
1610 | */ | |
5f41a31d CH |
1611 | switch (cmd->sam_task_attr) { |
1612 | case MSG_HEAD_TAG: | |
1613 | pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, " | |
1614 | "se_ordered_id: %u\n", | |
1615 | cmd->t_task_cdb[0], cmd->se_ordered_id); | |
019c4ca6 | 1616 | return false; |
5f41a31d CH |
1617 | case MSG_ORDERED_TAG: |
1618 | atomic_inc(&dev->dev_ordered_sync); | |
c66ac9db NB |
1619 | smp_mb__after_atomic_inc(); |
1620 | ||
5f41a31d CH |
1621 | pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, " |
1622 | " se_ordered_id: %u\n", | |
1623 | cmd->t_task_cdb[0], cmd->se_ordered_id); | |
1624 | ||
c66ac9db | 1625 | /* |
5f41a31d CH |
1626 | * Execute an ORDERED command if no other older commands |
1627 | * exist that need to be completed first. | |
c66ac9db | 1628 | */ |
5f41a31d | 1629 | if (!atomic_read(&dev->simple_cmds)) |
019c4ca6 | 1630 | return false; |
5f41a31d CH |
1631 | break; |
1632 | default: | |
c66ac9db NB |
1633 | /* |
1634 | * For SIMPLE and UNTAGGED Task Attribute commands | |
1635 | */ | |
5f41a31d | 1636 | atomic_inc(&dev->simple_cmds); |
c66ac9db | 1637 | smp_mb__after_atomic_inc(); |
5f41a31d | 1638 | break; |
c66ac9db | 1639 | } |
5f41a31d | 1640 | |
019c4ca6 CH |
1641 | if (atomic_read(&dev->dev_ordered_sync) == 0) |
1642 | return false; | |
c66ac9db | 1643 | |
019c4ca6 CH |
1644 | spin_lock(&dev->delayed_cmd_lock); |
1645 | list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list); | |
1646 | spin_unlock(&dev->delayed_cmd_lock); | |
1647 | ||
1648 | pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to" | |
1649 | " delayed CMD list, se_ordered_id: %u\n", | |
1650 | cmd->t_task_cdb[0], cmd->sam_task_attr, | |
1651 | cmd->se_ordered_id); | |
1652 | return true; | |
1653 | } | |
1654 | ||
1655 | void target_execute_cmd(struct se_cmd *cmd) | |
1656 | { | |
1657 | /* | |
1658 | * If the received CDB has aleady been aborted stop processing it here. | |
1659 | */ | |
1660 | if (transport_check_aborted_status(cmd, 1)) | |
5f41a31d | 1661 | return; |
c66ac9db | 1662 | |
c66ac9db | 1663 | /* |
019c4ca6 CH |
1664 | * Determine if IOCTL context caller in requesting the stopping of this |
1665 | * command for LUN shutdown purposes. | |
c66ac9db | 1666 | */ |
019c4ca6 CH |
1667 | spin_lock_irq(&cmd->t_state_lock); |
1668 | if (cmd->transport_state & CMD_T_LUN_STOP) { | |
1669 | pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n", | |
1670 | __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd)); | |
1671 | ||
1672 | cmd->transport_state &= ~CMD_T_ACTIVE; | |
1673 | spin_unlock_irq(&cmd->t_state_lock); | |
1674 | complete(&cmd->transport_lun_stop_comp); | |
1675 | return; | |
1676 | } | |
1677 | /* | |
1678 | * Determine if frontend context caller is requesting the stopping of | |
1679 | * this command for frontend exceptions. | |
1680 | */ | |
1681 | if (cmd->transport_state & CMD_T_STOP) { | |
1682 | pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n", | |
1683 | __func__, __LINE__, | |
1684 | cmd->se_tfo->get_task_tag(cmd)); | |
1685 | ||
1686 | spin_unlock_irq(&cmd->t_state_lock); | |
1687 | complete(&cmd->t_transport_stop_comp); | |
1688 | return; | |
1689 | } | |
1690 | ||
1691 | cmd->t_state = TRANSPORT_PROCESSING; | |
1692 | spin_unlock_irq(&cmd->t_state_lock); | |
1693 | ||
1694 | if (!target_handle_task_attr(cmd)) | |
1695 | __target_execute_cmd(cmd); | |
c66ac9db | 1696 | } |
70baf0ab | 1697 | EXPORT_SYMBOL(target_execute_cmd); |
c66ac9db | 1698 | |
5f41a31d CH |
1699 | /* |
1700 | * Process all commands up to the last received ORDERED task attribute which | |
1701 | * requires another blocking boundary | |
1702 | */ | |
1703 | static void target_restart_delayed_cmds(struct se_device *dev) | |
1704 | { | |
1705 | for (;;) { | |
1706 | struct se_cmd *cmd; | |
1707 | ||
1708 | spin_lock(&dev->delayed_cmd_lock); | |
1709 | if (list_empty(&dev->delayed_cmd_list)) { | |
1710 | spin_unlock(&dev->delayed_cmd_lock); | |
1711 | break; | |
1712 | } | |
1713 | ||
1714 | cmd = list_entry(dev->delayed_cmd_list.next, | |
1715 | struct se_cmd, se_delayed_node); | |
1716 | list_del(&cmd->se_delayed_node); | |
1717 | spin_unlock(&dev->delayed_cmd_lock); | |
1718 | ||
1719 | __target_execute_cmd(cmd); | |
1720 | ||
1721 | if (cmd->sam_task_attr == MSG_ORDERED_TAG) | |
1722 | break; | |
1723 | } | |
1724 | } | |
1725 | ||
c66ac9db | 1726 | /* |
35e0e757 | 1727 | * Called from I/O completion to determine which dormant/delayed |
c66ac9db NB |
1728 | * and ordered cmds need to have their tasks added to the execution queue. |
1729 | */ | |
1730 | static void transport_complete_task_attr(struct se_cmd *cmd) | |
1731 | { | |
5951146d | 1732 | struct se_device *dev = cmd->se_dev; |
c66ac9db | 1733 | |
019c4ca6 CH |
1734 | if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) |
1735 | return; | |
1736 | ||
e66ecd50 | 1737 | if (cmd->sam_task_attr == MSG_SIMPLE_TAG) { |
c66ac9db NB |
1738 | atomic_dec(&dev->simple_cmds); |
1739 | smp_mb__after_atomic_dec(); | |
1740 | dev->dev_cur_ordered_id++; | |
6708bb27 | 1741 | pr_debug("Incremented dev->dev_cur_ordered_id: %u for" |
c66ac9db NB |
1742 | " SIMPLE: %u\n", dev->dev_cur_ordered_id, |
1743 | cmd->se_ordered_id); | |
e66ecd50 | 1744 | } else if (cmd->sam_task_attr == MSG_HEAD_TAG) { |
c66ac9db | 1745 | dev->dev_cur_ordered_id++; |
6708bb27 | 1746 | pr_debug("Incremented dev_cur_ordered_id: %u for" |
c66ac9db NB |
1747 | " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id, |
1748 | cmd->se_ordered_id); | |
e66ecd50 | 1749 | } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) { |
c66ac9db NB |
1750 | atomic_dec(&dev->dev_ordered_sync); |
1751 | smp_mb__after_atomic_dec(); | |
c66ac9db NB |
1752 | |
1753 | dev->dev_cur_ordered_id++; | |
6708bb27 | 1754 | pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:" |
c66ac9db NB |
1755 | " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id); |
1756 | } | |
c66ac9db | 1757 | |
5f41a31d | 1758 | target_restart_delayed_cmds(dev); |
c66ac9db NB |
1759 | } |
1760 | ||
e057f533 | 1761 | static void transport_complete_qf(struct se_cmd *cmd) |
07bde79a NB |
1762 | { |
1763 | int ret = 0; | |
1764 | ||
019c4ca6 | 1765 | transport_complete_task_attr(cmd); |
e057f533 CH |
1766 | |
1767 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { | |
1768 | ret = cmd->se_tfo->queue_status(cmd); | |
1769 | if (ret) | |
1770 | goto out; | |
1771 | } | |
07bde79a NB |
1772 | |
1773 | switch (cmd->data_direction) { | |
1774 | case DMA_FROM_DEVICE: | |
1775 | ret = cmd->se_tfo->queue_data_in(cmd); | |
1776 | break; | |
1777 | case DMA_TO_DEVICE: | |
ec98f782 | 1778 | if (cmd->t_bidi_data_sg) { |
07bde79a NB |
1779 | ret = cmd->se_tfo->queue_data_in(cmd); |
1780 | if (ret < 0) | |
e057f533 | 1781 | break; |
07bde79a NB |
1782 | } |
1783 | /* Fall through for DMA_TO_DEVICE */ | |
1784 | case DMA_NONE: | |
1785 | ret = cmd->se_tfo->queue_status(cmd); | |
1786 | break; | |
1787 | default: | |
1788 | break; | |
1789 | } | |
1790 | ||
e057f533 CH |
1791 | out: |
1792 | if (ret < 0) { | |
1793 | transport_handle_queue_full(cmd, cmd->se_dev); | |
1794 | return; | |
1795 | } | |
1796 | transport_lun_remove_cmd(cmd); | |
1797 | transport_cmd_check_stop_to_fabric(cmd); | |
07bde79a NB |
1798 | } |
1799 | ||
1800 | static void transport_handle_queue_full( | |
1801 | struct se_cmd *cmd, | |
e057f533 | 1802 | struct se_device *dev) |
07bde79a NB |
1803 | { |
1804 | spin_lock_irq(&dev->qf_cmd_lock); | |
07bde79a NB |
1805 | list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list); |
1806 | atomic_inc(&dev->dev_qf_count); | |
1807 | smp_mb__after_atomic_inc(); | |
1808 | spin_unlock_irq(&cmd->se_dev->qf_cmd_lock); | |
1809 | ||
1810 | schedule_work(&cmd->se_dev->qf_work_queue); | |
1811 | } | |
1812 | ||
35e0e757 | 1813 | static void target_complete_ok_work(struct work_struct *work) |
c66ac9db | 1814 | { |
35e0e757 | 1815 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
27a27099 | 1816 | int ret; |
35e0e757 | 1817 | |
c66ac9db NB |
1818 | /* |
1819 | * Check if we need to move delayed/dormant tasks from cmds on the | |
1820 | * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task | |
1821 | * Attribute. | |
1822 | */ | |
019c4ca6 CH |
1823 | transport_complete_task_attr(cmd); |
1824 | ||
07bde79a NB |
1825 | /* |
1826 | * Check to schedule QUEUE_FULL work, or execute an existing | |
1827 | * cmd->transport_qf_callback() | |
1828 | */ | |
1829 | if (atomic_read(&cmd->se_dev->dev_qf_count) != 0) | |
1830 | schedule_work(&cmd->se_dev->qf_work_queue); | |
1831 | ||
c66ac9db | 1832 | /* |
d5829eac | 1833 | * Check if we need to send a sense buffer from |
c66ac9db NB |
1834 | * the struct se_cmd in question. |
1835 | */ | |
1836 | if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) { | |
27a27099 | 1837 | WARN_ON(!cmd->scsi_status); |
27a27099 PB |
1838 | ret = transport_send_check_condition_and_sense( |
1839 | cmd, 0, 1); | |
1840 | if (ret == -EAGAIN || ret == -ENOMEM) | |
1841 | goto queue_full; | |
1842 | ||
1843 | transport_lun_remove_cmd(cmd); | |
1844 | transport_cmd_check_stop_to_fabric(cmd); | |
1845 | return; | |
c66ac9db NB |
1846 | } |
1847 | /* | |
25985edc | 1848 | * Check for a callback, used by amongst other things |
c66ac9db NB |
1849 | * XDWRITE_READ_10 emulation. |
1850 | */ | |
1851 | if (cmd->transport_complete_callback) | |
1852 | cmd->transport_complete_callback(cmd); | |
1853 | ||
1854 | switch (cmd->data_direction) { | |
1855 | case DMA_FROM_DEVICE: | |
1856 | spin_lock(&cmd->se_lun->lun_sep_lock); | |
e3d6f909 AG |
1857 | if (cmd->se_lun->lun_sep) { |
1858 | cmd->se_lun->lun_sep->sep_stats.tx_data_octets += | |
c66ac9db NB |
1859 | cmd->data_length; |
1860 | } | |
1861 | spin_unlock(&cmd->se_lun->lun_sep_lock); | |
c66ac9db | 1862 | |
07bde79a | 1863 | ret = cmd->se_tfo->queue_data_in(cmd); |
f147abb4 | 1864 | if (ret == -EAGAIN || ret == -ENOMEM) |
07bde79a | 1865 | goto queue_full; |
c66ac9db NB |
1866 | break; |
1867 | case DMA_TO_DEVICE: | |
1868 | spin_lock(&cmd->se_lun->lun_sep_lock); | |
e3d6f909 AG |
1869 | if (cmd->se_lun->lun_sep) { |
1870 | cmd->se_lun->lun_sep->sep_stats.rx_data_octets += | |
c66ac9db NB |
1871 | cmd->data_length; |
1872 | } | |
1873 | spin_unlock(&cmd->se_lun->lun_sep_lock); | |
1874 | /* | |
1875 | * Check if we need to send READ payload for BIDI-COMMAND | |
1876 | */ | |
ec98f782 | 1877 | if (cmd->t_bidi_data_sg) { |
c66ac9db | 1878 | spin_lock(&cmd->se_lun->lun_sep_lock); |
e3d6f909 AG |
1879 | if (cmd->se_lun->lun_sep) { |
1880 | cmd->se_lun->lun_sep->sep_stats.tx_data_octets += | |
c66ac9db NB |
1881 | cmd->data_length; |
1882 | } | |
1883 | spin_unlock(&cmd->se_lun->lun_sep_lock); | |
07bde79a | 1884 | ret = cmd->se_tfo->queue_data_in(cmd); |
f147abb4 | 1885 | if (ret == -EAGAIN || ret == -ENOMEM) |
07bde79a | 1886 | goto queue_full; |
c66ac9db NB |
1887 | break; |
1888 | } | |
1889 | /* Fall through for DMA_TO_DEVICE */ | |
1890 | case DMA_NONE: | |
07bde79a | 1891 | ret = cmd->se_tfo->queue_status(cmd); |
f147abb4 | 1892 | if (ret == -EAGAIN || ret == -ENOMEM) |
07bde79a | 1893 | goto queue_full; |
c66ac9db NB |
1894 | break; |
1895 | default: | |
1896 | break; | |
1897 | } | |
1898 | ||
1899 | transport_lun_remove_cmd(cmd); | |
1900 | transport_cmd_check_stop_to_fabric(cmd); | |
07bde79a NB |
1901 | return; |
1902 | ||
1903 | queue_full: | |
6708bb27 | 1904 | pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p," |
07bde79a | 1905 | " data_direction: %d\n", cmd, cmd->data_direction); |
e057f533 CH |
1906 | cmd->t_state = TRANSPORT_COMPLETE_QF_OK; |
1907 | transport_handle_queue_full(cmd, cmd->se_dev); | |
c66ac9db NB |
1908 | } |
1909 | ||
6708bb27 | 1910 | static inline void transport_free_sgl(struct scatterlist *sgl, int nents) |
c66ac9db | 1911 | { |
ec98f782 | 1912 | struct scatterlist *sg; |
ec98f782 | 1913 | int count; |
c66ac9db | 1914 | |
6708bb27 AG |
1915 | for_each_sg(sgl, sg, nents, count) |
1916 | __free_page(sg_page(sg)); | |
c66ac9db | 1917 | |
6708bb27 AG |
1918 | kfree(sgl); |
1919 | } | |
c66ac9db | 1920 | |
6708bb27 AG |
1921 | static inline void transport_free_pages(struct se_cmd *cmd) |
1922 | { | |
1923 | if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) | |
1924 | return; | |
1925 | ||
1926 | transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents); | |
ec98f782 AG |
1927 | cmd->t_data_sg = NULL; |
1928 | cmd->t_data_nents = 0; | |
c66ac9db | 1929 | |
6708bb27 | 1930 | transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents); |
ec98f782 AG |
1931 | cmd->t_bidi_data_sg = NULL; |
1932 | cmd->t_bidi_data_nents = 0; | |
c66ac9db NB |
1933 | } |
1934 | ||
e26d99ae CH |
1935 | /** |
1936 | * transport_release_cmd - free a command | |
1937 | * @cmd: command to free | |
1938 | * | |
1939 | * This routine unconditionally frees a command, and reference counting | |
1940 | * or list removal must be done in the caller. | |
1941 | */ | |
1942 | static void transport_release_cmd(struct se_cmd *cmd) | |
1943 | { | |
1944 | BUG_ON(!cmd->se_tfo); | |
1945 | ||
c8e31f26 | 1946 | if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) |
e26d99ae CH |
1947 | core_tmr_release_req(cmd->se_tmr_req); |
1948 | if (cmd->t_task_cdb != cmd->__t_task_cdb) | |
1949 | kfree(cmd->t_task_cdb); | |
1950 | /* | |
7481deb4 NB |
1951 | * If this cmd has been setup with target_get_sess_cmd(), drop |
1952 | * the kref and call ->release_cmd() in kref callback. | |
e26d99ae | 1953 | */ |
7481deb4 NB |
1954 | if (cmd->check_release != 0) { |
1955 | target_put_sess_cmd(cmd->se_sess, cmd); | |
1956 | return; | |
1957 | } | |
e26d99ae CH |
1958 | cmd->se_tfo->release_cmd(cmd); |
1959 | } | |
1960 | ||
d3df7825 CH |
1961 | /** |
1962 | * transport_put_cmd - release a reference to a command | |
1963 | * @cmd: command to release | |
1964 | * | |
1965 | * This routine releases our reference to the command and frees it if possible. | |
1966 | */ | |
39c05f32 | 1967 | static void transport_put_cmd(struct se_cmd *cmd) |
c66ac9db NB |
1968 | { |
1969 | unsigned long flags; | |
c66ac9db | 1970 | |
a1d8b49a | 1971 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
4911e3cc CH |
1972 | if (atomic_read(&cmd->t_fe_count)) { |
1973 | if (!atomic_dec_and_test(&cmd->t_fe_count)) | |
1974 | goto out_busy; | |
1975 | } | |
1976 | ||
7d680f3b CH |
1977 | if (cmd->transport_state & CMD_T_DEV_ACTIVE) { |
1978 | cmd->transport_state &= ~CMD_T_DEV_ACTIVE; | |
cf572a96 | 1979 | target_remove_from_state_list(cmd); |
c66ac9db | 1980 | } |
a1d8b49a | 1981 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 1982 | |
c66ac9db | 1983 | transport_free_pages(cmd); |
31afc39c | 1984 | transport_release_cmd(cmd); |
39c05f32 | 1985 | return; |
4911e3cc CH |
1986 | out_busy: |
1987 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); | |
c66ac9db NB |
1988 | } |
1989 | ||
c66ac9db | 1990 | /* |
ec98f782 AG |
1991 | * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of |
1992 | * allocating in the core. | |
c66ac9db NB |
1993 | * @cmd: Associated se_cmd descriptor |
1994 | * @mem: SGL style memory for TCM WRITE / READ | |
1995 | * @sg_mem_num: Number of SGL elements | |
1996 | * @mem_bidi_in: SGL style memory for TCM BIDI READ | |
1997 | * @sg_mem_bidi_num: Number of BIDI READ SGL elements | |
1998 | * | |
1999 | * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage | |
2000 | * of parameters. | |
2001 | */ | |
2002 | int transport_generic_map_mem_to_cmd( | |
2003 | struct se_cmd *cmd, | |
5951146d AG |
2004 | struct scatterlist *sgl, |
2005 | u32 sgl_count, | |
2006 | struct scatterlist *sgl_bidi, | |
2007 | u32 sgl_bidi_count) | |
c66ac9db | 2008 | { |
5951146d | 2009 | if (!sgl || !sgl_count) |
c66ac9db | 2010 | return 0; |
c66ac9db | 2011 | |
64f1db38 CH |
2012 | /* |
2013 | * Reject SCSI data overflow with map_mem_to_cmd() as incoming | |
2014 | * scatterlists already have been set to follow what the fabric | |
2015 | * passes for the original expected data transfer length. | |
2016 | */ | |
2017 | if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) { | |
2018 | pr_warn("Rejecting SCSI DATA overflow for fabric using" | |
2019 | " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n"); | |
2020 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | |
2021 | cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD; | |
2022 | return -EINVAL; | |
2023 | } | |
c66ac9db | 2024 | |
64f1db38 CH |
2025 | cmd->t_data_sg = sgl; |
2026 | cmd->t_data_nents = sgl_count; | |
c66ac9db | 2027 | |
64f1db38 CH |
2028 | if (sgl_bidi && sgl_bidi_count) { |
2029 | cmd->t_bidi_data_sg = sgl_bidi; | |
2030 | cmd->t_bidi_data_nents = sgl_bidi_count; | |
c66ac9db | 2031 | } |
64f1db38 | 2032 | cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC; |
c66ac9db NB |
2033 | return 0; |
2034 | } | |
2035 | EXPORT_SYMBOL(transport_generic_map_mem_to_cmd); | |
2036 | ||
4949314c | 2037 | void *transport_kmap_data_sg(struct se_cmd *cmd) |
05d1c7c0 | 2038 | { |
ec98f782 | 2039 | struct scatterlist *sg = cmd->t_data_sg; |
4949314c AG |
2040 | struct page **pages; |
2041 | int i; | |
05d1c7c0 AG |
2042 | |
2043 | /* | |
ec98f782 AG |
2044 | * We need to take into account a possible offset here for fabrics like |
2045 | * tcm_loop who may be using a contig buffer from the SCSI midlayer for | |
2046 | * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd() | |
05d1c7c0 | 2047 | */ |
4949314c AG |
2048 | if (!cmd->t_data_nents) |
2049 | return NULL; | |
3717ef0c PB |
2050 | |
2051 | BUG_ON(!sg); | |
2052 | if (cmd->t_data_nents == 1) | |
4949314c AG |
2053 | return kmap(sg_page(sg)) + sg->offset; |
2054 | ||
2055 | /* >1 page. use vmap */ | |
2056 | pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL); | |
3717ef0c PB |
2057 | if (!pages) { |
2058 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
4949314c | 2059 | return NULL; |
3717ef0c | 2060 | } |
4949314c AG |
2061 | |
2062 | /* convert sg[] to pages[] */ | |
2063 | for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) { | |
2064 | pages[i] = sg_page(sg); | |
2065 | } | |
2066 | ||
2067 | cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL); | |
2068 | kfree(pages); | |
3717ef0c PB |
2069 | if (!cmd->t_data_vmap) { |
2070 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
4949314c | 2071 | return NULL; |
3717ef0c | 2072 | } |
4949314c AG |
2073 | |
2074 | return cmd->t_data_vmap + cmd->t_data_sg[0].offset; | |
05d1c7c0 | 2075 | } |
4949314c | 2076 | EXPORT_SYMBOL(transport_kmap_data_sg); |
05d1c7c0 | 2077 | |
4949314c | 2078 | void transport_kunmap_data_sg(struct se_cmd *cmd) |
05d1c7c0 | 2079 | { |
a1edf9cf | 2080 | if (!cmd->t_data_nents) { |
4949314c | 2081 | return; |
a1edf9cf | 2082 | } else if (cmd->t_data_nents == 1) { |
4949314c | 2083 | kunmap(sg_page(cmd->t_data_sg)); |
a1edf9cf AG |
2084 | return; |
2085 | } | |
4949314c AG |
2086 | |
2087 | vunmap(cmd->t_data_vmap); | |
2088 | cmd->t_data_vmap = NULL; | |
05d1c7c0 | 2089 | } |
4949314c | 2090 | EXPORT_SYMBOL(transport_kunmap_data_sg); |
05d1c7c0 | 2091 | |
c66ac9db | 2092 | static int |
05d1c7c0 | 2093 | transport_generic_get_mem(struct se_cmd *cmd) |
c66ac9db | 2094 | { |
ec98f782 AG |
2095 | u32 length = cmd->data_length; |
2096 | unsigned int nents; | |
2097 | struct page *page; | |
9db9da33 | 2098 | gfp_t zero_flag; |
ec98f782 | 2099 | int i = 0; |
c66ac9db | 2100 | |
ec98f782 AG |
2101 | nents = DIV_ROUND_UP(length, PAGE_SIZE); |
2102 | cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL); | |
2103 | if (!cmd->t_data_sg) | |
2104 | return -ENOMEM; | |
c66ac9db | 2105 | |
ec98f782 AG |
2106 | cmd->t_data_nents = nents; |
2107 | sg_init_table(cmd->t_data_sg, nents); | |
c66ac9db | 2108 | |
64f1db38 | 2109 | zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO; |
9db9da33 | 2110 | |
ec98f782 AG |
2111 | while (length) { |
2112 | u32 page_len = min_t(u32, length, PAGE_SIZE); | |
9db9da33 | 2113 | page = alloc_page(GFP_KERNEL | zero_flag); |
ec98f782 AG |
2114 | if (!page) |
2115 | goto out; | |
c66ac9db | 2116 | |
ec98f782 AG |
2117 | sg_set_page(&cmd->t_data_sg[i], page, page_len, 0); |
2118 | length -= page_len; | |
2119 | i++; | |
c66ac9db | 2120 | } |
c66ac9db | 2121 | return 0; |
c66ac9db | 2122 | |
ec98f782 | 2123 | out: |
d0e27c88 | 2124 | while (i > 0) { |
ec98f782 | 2125 | i--; |
d0e27c88 | 2126 | __free_page(sg_page(&cmd->t_data_sg[i])); |
c66ac9db | 2127 | } |
ec98f782 AG |
2128 | kfree(cmd->t_data_sg); |
2129 | cmd->t_data_sg = NULL; | |
2130 | return -ENOMEM; | |
c66ac9db NB |
2131 | } |
2132 | ||
da0f7619 | 2133 | /* |
b16a35b0 AG |
2134 | * Allocate any required resources to execute the command. For writes we |
2135 | * might not have the payload yet, so notify the fabric via a call to | |
2136 | * ->write_pending instead. Otherwise place it on the execution queue. | |
c66ac9db | 2137 | */ |
a1d8b49a | 2138 | int transport_generic_new_cmd(struct se_cmd *cmd) |
c66ac9db | 2139 | { |
c66ac9db NB |
2140 | int ret = 0; |
2141 | ||
2142 | /* | |
2143 | * Determine is the TCM fabric module has already allocated physical | |
2144 | * memory, and is directly calling transport_generic_map_mem_to_cmd() | |
ec98f782 | 2145 | * beforehand. |
c66ac9db | 2146 | */ |
ec98f782 AG |
2147 | if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) && |
2148 | cmd->data_length) { | |
05d1c7c0 | 2149 | ret = transport_generic_get_mem(cmd); |
c66ac9db | 2150 | if (ret < 0) |
03e98c9e | 2151 | goto out_fail; |
c66ac9db | 2152 | } |
da0f7619 | 2153 | |
4101f0a8 | 2154 | atomic_inc(&cmd->t_fe_count); |
4101f0a8 | 2155 | |
c66ac9db | 2156 | /* |
c3196f0c CH |
2157 | * If this command is not a write we can execute it right here, |
2158 | * for write buffers we need to notify the fabric driver first | |
2159 | * and let it call back once the write buffers are ready. | |
c66ac9db | 2160 | */ |
5f41a31d | 2161 | target_add_to_state_list(cmd); |
c3196f0c CH |
2162 | if (cmd->data_direction != DMA_TO_DEVICE) { |
2163 | target_execute_cmd(cmd); | |
2164 | return 0; | |
2165 | } | |
2166 | ||
2167 | spin_lock_irq(&cmd->t_state_lock); | |
2168 | cmd->t_state = TRANSPORT_WRITE_PENDING; | |
2169 | spin_unlock_irq(&cmd->t_state_lock); | |
2170 | ||
2171 | transport_cmd_check_stop(cmd, false); | |
2172 | ||
2173 | ret = cmd->se_tfo->write_pending(cmd); | |
2174 | if (ret == -EAGAIN || ret == -ENOMEM) | |
2175 | goto queue_full; | |
2176 | ||
2177 | if (ret < 0) | |
2178 | return ret; | |
2179 | return 1; | |
da0f7619 CH |
2180 | |
2181 | out_fail: | |
2182 | cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION; | |
2183 | cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE; | |
2184 | return -EINVAL; | |
c3196f0c CH |
2185 | queue_full: |
2186 | pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd); | |
2187 | cmd->t_state = TRANSPORT_COMPLETE_QF_WP; | |
2188 | transport_handle_queue_full(cmd, cmd->se_dev); | |
2189 | return 0; | |
c66ac9db | 2190 | } |
a1d8b49a | 2191 | EXPORT_SYMBOL(transport_generic_new_cmd); |
c66ac9db | 2192 | |
e057f533 | 2193 | static void transport_write_pending_qf(struct se_cmd *cmd) |
07bde79a | 2194 | { |
f147abb4 NB |
2195 | int ret; |
2196 | ||
2197 | ret = cmd->se_tfo->write_pending(cmd); | |
2198 | if (ret == -EAGAIN || ret == -ENOMEM) { | |
e057f533 CH |
2199 | pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", |
2200 | cmd); | |
2201 | transport_handle_queue_full(cmd, cmd->se_dev); | |
2202 | } | |
07bde79a NB |
2203 | } |
2204 | ||
39c05f32 | 2205 | void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks) |
c66ac9db | 2206 | { |
d14921d6 | 2207 | if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) { |
c8e31f26 | 2208 | if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) |
d14921d6 NB |
2209 | transport_wait_for_tasks(cmd); |
2210 | ||
35462975 | 2211 | transport_release_cmd(cmd); |
d14921d6 NB |
2212 | } else { |
2213 | if (wait_for_tasks) | |
2214 | transport_wait_for_tasks(cmd); | |
2215 | ||
c66ac9db NB |
2216 | core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd); |
2217 | ||
82f1c8a4 | 2218 | if (cmd->se_lun) |
c66ac9db | 2219 | transport_lun_remove_cmd(cmd); |
c66ac9db | 2220 | |
39c05f32 | 2221 | transport_put_cmd(cmd); |
c66ac9db NB |
2222 | } |
2223 | } | |
2224 | EXPORT_SYMBOL(transport_generic_free_cmd); | |
2225 | ||
a17f091d NB |
2226 | /* target_get_sess_cmd - Add command to active ->sess_cmd_list |
2227 | * @se_sess: session to reference | |
2228 | * @se_cmd: command descriptor to add | |
a6360785 | 2229 | * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd() |
a17f091d | 2230 | */ |
bc187ea6 RD |
2231 | static int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd, |
2232 | bool ack_kref) | |
a17f091d NB |
2233 | { |
2234 | unsigned long flags; | |
bc187ea6 | 2235 | int ret = 0; |
a17f091d | 2236 | |
7481deb4 | 2237 | kref_init(&se_cmd->cmd_kref); |
a6360785 NB |
2238 | /* |
2239 | * Add a second kref if the fabric caller is expecting to handle | |
2240 | * fabric acknowledgement that requires two target_put_sess_cmd() | |
2241 | * invocations before se_cmd descriptor release. | |
2242 | */ | |
86715569 | 2243 | if (ack_kref == true) { |
a6360785 | 2244 | kref_get(&se_cmd->cmd_kref); |
86715569 NB |
2245 | se_cmd->se_cmd_flags |= SCF_ACK_KREF; |
2246 | } | |
7481deb4 | 2247 | |
a17f091d | 2248 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
bc187ea6 RD |
2249 | if (se_sess->sess_tearing_down) { |
2250 | ret = -ESHUTDOWN; | |
2251 | goto out; | |
2252 | } | |
a17f091d NB |
2253 | list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list); |
2254 | se_cmd->check_release = 1; | |
bc187ea6 RD |
2255 | |
2256 | out: | |
a17f091d | 2257 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); |
bc187ea6 | 2258 | return ret; |
a17f091d | 2259 | } |
a17f091d | 2260 | |
7481deb4 | 2261 | static void target_release_cmd_kref(struct kref *kref) |
a17f091d | 2262 | { |
7481deb4 NB |
2263 | struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref); |
2264 | struct se_session *se_sess = se_cmd->se_sess; | |
a17f091d NB |
2265 | unsigned long flags; |
2266 | ||
2267 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); | |
2268 | if (list_empty(&se_cmd->se_cmd_list)) { | |
2269 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); | |
ffc32d52 | 2270 | se_cmd->se_tfo->release_cmd(se_cmd); |
7481deb4 | 2271 | return; |
a17f091d | 2272 | } |
a17f091d NB |
2273 | if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) { |
2274 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); | |
2275 | complete(&se_cmd->cmd_wait_comp); | |
7481deb4 | 2276 | return; |
a17f091d NB |
2277 | } |
2278 | list_del(&se_cmd->se_cmd_list); | |
2279 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); | |
2280 | ||
7481deb4 NB |
2281 | se_cmd->se_tfo->release_cmd(se_cmd); |
2282 | } | |
2283 | ||
2284 | /* target_put_sess_cmd - Check for active I/O shutdown via kref_put | |
2285 | * @se_sess: session to reference | |
2286 | * @se_cmd: command descriptor to drop | |
2287 | */ | |
2288 | int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd) | |
2289 | { | |
2290 | return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref); | |
a17f091d NB |
2291 | } |
2292 | EXPORT_SYMBOL(target_put_sess_cmd); | |
2293 | ||
1c7b13fe RD |
2294 | /* target_sess_cmd_list_set_waiting - Flag all commands in |
2295 | * sess_cmd_list to complete cmd_wait_comp. Set | |
2296 | * sess_tearing_down so no more commands are queued. | |
2297 | * @se_sess: session to flag | |
a17f091d | 2298 | */ |
1c7b13fe | 2299 | void target_sess_cmd_list_set_waiting(struct se_session *se_sess) |
a17f091d NB |
2300 | { |
2301 | struct se_cmd *se_cmd; | |
2302 | unsigned long flags; | |
2303 | ||
a17f091d | 2304 | spin_lock_irqsave(&se_sess->sess_cmd_lock, flags); |
a17f091d | 2305 | |
1c7b13fe RD |
2306 | WARN_ON(se_sess->sess_tearing_down); |
2307 | se_sess->sess_tearing_down = 1; | |
a17f091d | 2308 | |
1c7b13fe | 2309 | list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) |
a17f091d NB |
2310 | se_cmd->cmd_wait_set = 1; |
2311 | ||
2312 | spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags); | |
2313 | } | |
1c7b13fe | 2314 | EXPORT_SYMBOL(target_sess_cmd_list_set_waiting); |
a17f091d NB |
2315 | |
2316 | /* target_wait_for_sess_cmds - Wait for outstanding descriptors | |
2317 | * @se_sess: session to wait for active I/O | |
2318 | * @wait_for_tasks: Make extra transport_wait_for_tasks call | |
2319 | */ | |
2320 | void target_wait_for_sess_cmds( | |
2321 | struct se_session *se_sess, | |
2322 | int wait_for_tasks) | |
2323 | { | |
2324 | struct se_cmd *se_cmd, *tmp_cmd; | |
2325 | bool rc = false; | |
2326 | ||
2327 | list_for_each_entry_safe(se_cmd, tmp_cmd, | |
1c7b13fe | 2328 | &se_sess->sess_cmd_list, se_cmd_list) { |
a17f091d NB |
2329 | list_del(&se_cmd->se_cmd_list); |
2330 | ||
2331 | pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:" | |
2332 | " %d\n", se_cmd, se_cmd->t_state, | |
2333 | se_cmd->se_tfo->get_cmd_state(se_cmd)); | |
2334 | ||
2335 | if (wait_for_tasks) { | |
2336 | pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d," | |
2337 | " fabric state: %d\n", se_cmd, se_cmd->t_state, | |
2338 | se_cmd->se_tfo->get_cmd_state(se_cmd)); | |
2339 | ||
2340 | rc = transport_wait_for_tasks(se_cmd); | |
2341 | ||
2342 | pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d," | |
2343 | " fabric state: %d\n", se_cmd, se_cmd->t_state, | |
2344 | se_cmd->se_tfo->get_cmd_state(se_cmd)); | |
2345 | } | |
2346 | ||
2347 | if (!rc) { | |
2348 | wait_for_completion(&se_cmd->cmd_wait_comp); | |
2349 | pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d" | |
2350 | " fabric state: %d\n", se_cmd, se_cmd->t_state, | |
2351 | se_cmd->se_tfo->get_cmd_state(se_cmd)); | |
2352 | } | |
2353 | ||
2354 | se_cmd->se_tfo->release_cmd(se_cmd); | |
2355 | } | |
2356 | } | |
2357 | EXPORT_SYMBOL(target_wait_for_sess_cmds); | |
2358 | ||
c66ac9db NB |
2359 | /* transport_lun_wait_for_tasks(): |
2360 | * | |
2361 | * Called from ConfigFS context to stop the passed struct se_cmd to allow | |
2362 | * an struct se_lun to be successfully shutdown. | |
2363 | */ | |
2364 | static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun) | |
2365 | { | |
2366 | unsigned long flags; | |
cf572a96 CH |
2367 | int ret = 0; |
2368 | ||
c66ac9db NB |
2369 | /* |
2370 | * If the frontend has already requested this struct se_cmd to | |
2371 | * be stopped, we can safely ignore this struct se_cmd. | |
2372 | */ | |
a1d8b49a | 2373 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
7d680f3b CH |
2374 | if (cmd->transport_state & CMD_T_STOP) { |
2375 | cmd->transport_state &= ~CMD_T_LUN_STOP; | |
2376 | ||
2377 | pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n", | |
2378 | cmd->se_tfo->get_task_tag(cmd)); | |
a1d8b49a | 2379 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
f7113a47 | 2380 | transport_cmd_check_stop(cmd, false); |
e3d6f909 | 2381 | return -EPERM; |
c66ac9db | 2382 | } |
7d680f3b | 2383 | cmd->transport_state |= CMD_T_LUN_FE_STOP; |
a1d8b49a | 2384 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 2385 | |
cf572a96 CH |
2386 | // XXX: audit task_flags checks. |
2387 | spin_lock_irqsave(&cmd->t_state_lock, flags); | |
2388 | if ((cmd->transport_state & CMD_T_BUSY) && | |
2389 | (cmd->transport_state & CMD_T_SENT)) { | |
2390 | if (!target_stop_cmd(cmd, &flags)) | |
2391 | ret++; | |
cf572a96 | 2392 | } |
5f41a31d | 2393 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 2394 | |
785fdf70 CH |
2395 | pr_debug("ConfigFS: cmd: %p stop tasks ret:" |
2396 | " %d\n", cmd, ret); | |
c66ac9db | 2397 | if (!ret) { |
6708bb27 | 2398 | pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n", |
e3d6f909 | 2399 | cmd->se_tfo->get_task_tag(cmd)); |
a1d8b49a | 2400 | wait_for_completion(&cmd->transport_lun_stop_comp); |
6708bb27 | 2401 | pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n", |
e3d6f909 | 2402 | cmd->se_tfo->get_task_tag(cmd)); |
c66ac9db | 2403 | } |
c66ac9db NB |
2404 | |
2405 | return 0; | |
2406 | } | |
2407 | ||
c66ac9db NB |
2408 | static void __transport_clear_lun_from_sessions(struct se_lun *lun) |
2409 | { | |
2410 | struct se_cmd *cmd = NULL; | |
2411 | unsigned long lun_flags, cmd_flags; | |
2412 | /* | |
2413 | * Do exception processing and return CHECK_CONDITION status to the | |
2414 | * Initiator Port. | |
2415 | */ | |
2416 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); | |
5951146d AG |
2417 | while (!list_empty(&lun->lun_cmd_list)) { |
2418 | cmd = list_first_entry(&lun->lun_cmd_list, | |
2419 | struct se_cmd, se_lun_node); | |
3d26fea0 | 2420 | list_del_init(&cmd->se_lun_node); |
5951146d | 2421 | |
a1d8b49a | 2422 | spin_lock(&cmd->t_state_lock); |
6708bb27 | 2423 | pr_debug("SE_LUN[%d] - Setting cmd->transport" |
c66ac9db | 2424 | "_lun_stop for ITT: 0x%08x\n", |
e3d6f909 AG |
2425 | cmd->se_lun->unpacked_lun, |
2426 | cmd->se_tfo->get_task_tag(cmd)); | |
7d680f3b | 2427 | cmd->transport_state |= CMD_T_LUN_STOP; |
a1d8b49a | 2428 | spin_unlock(&cmd->t_state_lock); |
c66ac9db NB |
2429 | |
2430 | spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags); | |
2431 | ||
6708bb27 AG |
2432 | if (!cmd->se_lun) { |
2433 | pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n", | |
e3d6f909 AG |
2434 | cmd->se_tfo->get_task_tag(cmd), |
2435 | cmd->se_tfo->get_cmd_state(cmd), cmd->t_state); | |
c66ac9db NB |
2436 | BUG(); |
2437 | } | |
2438 | /* | |
2439 | * If the Storage engine still owns the iscsi_cmd_t, determine | |
2440 | * and/or stop its context. | |
2441 | */ | |
6708bb27 | 2442 | pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport" |
e3d6f909 AG |
2443 | "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun, |
2444 | cmd->se_tfo->get_task_tag(cmd)); | |
c66ac9db | 2445 | |
e3d6f909 | 2446 | if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) { |
c66ac9db NB |
2447 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
2448 | continue; | |
2449 | } | |
2450 | ||
6708bb27 | 2451 | pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun" |
c66ac9db | 2452 | "_wait_for_tasks(): SUCCESS\n", |
e3d6f909 AG |
2453 | cmd->se_lun->unpacked_lun, |
2454 | cmd->se_tfo->get_task_tag(cmd)); | |
c66ac9db | 2455 | |
a1d8b49a | 2456 | spin_lock_irqsave(&cmd->t_state_lock, cmd_flags); |
7d680f3b | 2457 | if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) { |
a1d8b49a | 2458 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
c66ac9db NB |
2459 | goto check_cond; |
2460 | } | |
7d680f3b | 2461 | cmd->transport_state &= ~CMD_T_DEV_ACTIVE; |
cf572a96 | 2462 | target_remove_from_state_list(cmd); |
a1d8b49a | 2463 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
c66ac9db | 2464 | |
c66ac9db NB |
2465 | /* |
2466 | * The Storage engine stopped this struct se_cmd before it was | |
2467 | * send to the fabric frontend for delivery back to the | |
2468 | * Initiator Node. Return this SCSI CDB back with an | |
2469 | * CHECK_CONDITION status. | |
2470 | */ | |
2471 | check_cond: | |
2472 | transport_send_check_condition_and_sense(cmd, | |
2473 | TCM_NON_EXISTENT_LUN, 0); | |
2474 | /* | |
2475 | * If the fabric frontend is waiting for this iscsi_cmd_t to | |
2476 | * be released, notify the waiting thread now that LU has | |
2477 | * finished accessing it. | |
2478 | */ | |
a1d8b49a | 2479 | spin_lock_irqsave(&cmd->t_state_lock, cmd_flags); |
7d680f3b | 2480 | if (cmd->transport_state & CMD_T_LUN_FE_STOP) { |
6708bb27 | 2481 | pr_debug("SE_LUN[%d] - Detected FE stop for" |
c66ac9db NB |
2482 | " struct se_cmd: %p ITT: 0x%08x\n", |
2483 | lun->unpacked_lun, | |
e3d6f909 | 2484 | cmd, cmd->se_tfo->get_task_tag(cmd)); |
c66ac9db | 2485 | |
a1d8b49a | 2486 | spin_unlock_irqrestore(&cmd->t_state_lock, |
c66ac9db | 2487 | cmd_flags); |
f7113a47 | 2488 | transport_cmd_check_stop(cmd, false); |
a1d8b49a | 2489 | complete(&cmd->transport_lun_fe_stop_comp); |
c66ac9db NB |
2490 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
2491 | continue; | |
2492 | } | |
6708bb27 | 2493 | pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n", |
e3d6f909 | 2494 | lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd)); |
c66ac9db | 2495 | |
a1d8b49a | 2496 | spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags); |
c66ac9db NB |
2497 | spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags); |
2498 | } | |
2499 | spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags); | |
2500 | } | |
2501 | ||
2502 | static int transport_clear_lun_thread(void *p) | |
2503 | { | |
8359cf43 | 2504 | struct se_lun *lun = p; |
c66ac9db NB |
2505 | |
2506 | __transport_clear_lun_from_sessions(lun); | |
2507 | complete(&lun->lun_shutdown_comp); | |
2508 | ||
2509 | return 0; | |
2510 | } | |
2511 | ||
2512 | int transport_clear_lun_from_sessions(struct se_lun *lun) | |
2513 | { | |
2514 | struct task_struct *kt; | |
2515 | ||
5951146d | 2516 | kt = kthread_run(transport_clear_lun_thread, lun, |
c66ac9db NB |
2517 | "tcm_cl_%u", lun->unpacked_lun); |
2518 | if (IS_ERR(kt)) { | |
6708bb27 | 2519 | pr_err("Unable to start clear_lun thread\n"); |
e3d6f909 | 2520 | return PTR_ERR(kt); |
c66ac9db NB |
2521 | } |
2522 | wait_for_completion(&lun->lun_shutdown_comp); | |
2523 | ||
2524 | return 0; | |
2525 | } | |
2526 | ||
d14921d6 NB |
2527 | /** |
2528 | * transport_wait_for_tasks - wait for completion to occur | |
2529 | * @cmd: command to wait | |
c66ac9db | 2530 | * |
d14921d6 NB |
2531 | * Called from frontend fabric context to wait for storage engine |
2532 | * to pause and/or release frontend generated struct se_cmd. | |
c66ac9db | 2533 | */ |
a17f091d | 2534 | bool transport_wait_for_tasks(struct se_cmd *cmd) |
c66ac9db NB |
2535 | { |
2536 | unsigned long flags; | |
2537 | ||
a1d8b49a | 2538 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
c8e31f26 AG |
2539 | if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && |
2540 | !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { | |
d14921d6 | 2541 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
a17f091d | 2542 | return false; |
d14921d6 | 2543 | } |
cb4f4d3c | 2544 | |
c8e31f26 AG |
2545 | if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && |
2546 | !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) { | |
d14921d6 | 2547 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
a17f091d | 2548 | return false; |
d14921d6 | 2549 | } |
c66ac9db NB |
2550 | /* |
2551 | * If we are already stopped due to an external event (ie: LUN shutdown) | |
2552 | * sleep until the connection can have the passed struct se_cmd back. | |
a1d8b49a | 2553 | * The cmd->transport_lun_stopped_sem will be upped by |
c66ac9db NB |
2554 | * transport_clear_lun_from_sessions() once the ConfigFS context caller |
2555 | * has completed its operation on the struct se_cmd. | |
2556 | */ | |
7d680f3b | 2557 | if (cmd->transport_state & CMD_T_LUN_STOP) { |
6708bb27 | 2558 | pr_debug("wait_for_tasks: Stopping" |
e3d6f909 | 2559 | " wait_for_completion(&cmd->t_tasktransport_lun_fe" |
c66ac9db | 2560 | "_stop_comp); for ITT: 0x%08x\n", |
e3d6f909 | 2561 | cmd->se_tfo->get_task_tag(cmd)); |
c66ac9db NB |
2562 | /* |
2563 | * There is a special case for WRITES where a FE exception + | |
2564 | * LUN shutdown means ConfigFS context is still sleeping on | |
2565 | * transport_lun_stop_comp in transport_lun_wait_for_tasks(). | |
2566 | * We go ahead and up transport_lun_stop_comp just to be sure | |
2567 | * here. | |
2568 | */ | |
a1d8b49a AG |
2569 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
2570 | complete(&cmd->transport_lun_stop_comp); | |
2571 | wait_for_completion(&cmd->transport_lun_fe_stop_comp); | |
2572 | spin_lock_irqsave(&cmd->t_state_lock, flags); | |
c66ac9db | 2573 | |
cf572a96 | 2574 | target_remove_from_state_list(cmd); |
c66ac9db NB |
2575 | /* |
2576 | * At this point, the frontend who was the originator of this | |
2577 | * struct se_cmd, now owns the structure and can be released through | |
2578 | * normal means below. | |
2579 | */ | |
6708bb27 | 2580 | pr_debug("wait_for_tasks: Stopped" |
e3d6f909 | 2581 | " wait_for_completion(&cmd->t_tasktransport_lun_fe_" |
c66ac9db | 2582 | "stop_comp); for ITT: 0x%08x\n", |
e3d6f909 | 2583 | cmd->se_tfo->get_task_tag(cmd)); |
c66ac9db | 2584 | |
7d680f3b | 2585 | cmd->transport_state &= ~CMD_T_LUN_STOP; |
c66ac9db | 2586 | } |
7d680f3b | 2587 | |
3d28934a | 2588 | if (!(cmd->transport_state & CMD_T_ACTIVE)) { |
d14921d6 | 2589 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
a17f091d | 2590 | return false; |
d14921d6 | 2591 | } |
c66ac9db | 2592 | |
7d680f3b | 2593 | cmd->transport_state |= CMD_T_STOP; |
c66ac9db | 2594 | |
6708bb27 | 2595 | pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x" |
7d680f3b | 2596 | " i_state: %d, t_state: %d, CMD_T_STOP\n", |
f2da9dbd CH |
2597 | cmd, cmd->se_tfo->get_task_tag(cmd), |
2598 | cmd->se_tfo->get_cmd_state(cmd), cmd->t_state); | |
c66ac9db | 2599 | |
a1d8b49a | 2600 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db | 2601 | |
a1d8b49a | 2602 | wait_for_completion(&cmd->t_transport_stop_comp); |
c66ac9db | 2603 | |
a1d8b49a | 2604 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
7d680f3b | 2605 | cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP); |
c66ac9db | 2606 | |
35d1efe8 | 2607 | pr_debug("wait_for_tasks: Stopped wait_for_completion(" |
a1d8b49a | 2608 | "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n", |
e3d6f909 | 2609 | cmd->se_tfo->get_task_tag(cmd)); |
c66ac9db | 2610 | |
d14921d6 | 2611 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
a17f091d NB |
2612 | |
2613 | return true; | |
c66ac9db | 2614 | } |
d14921d6 | 2615 | EXPORT_SYMBOL(transport_wait_for_tasks); |
c66ac9db NB |
2616 | |
2617 | static int transport_get_sense_codes( | |
2618 | struct se_cmd *cmd, | |
2619 | u8 *asc, | |
2620 | u8 *ascq) | |
2621 | { | |
2622 | *asc = cmd->scsi_asc; | |
2623 | *ascq = cmd->scsi_ascq; | |
2624 | ||
2625 | return 0; | |
2626 | } | |
2627 | ||
c66ac9db NB |
2628 | int transport_send_check_condition_and_sense( |
2629 | struct se_cmd *cmd, | |
2630 | u8 reason, | |
2631 | int from_transport) | |
2632 | { | |
2633 | unsigned char *buffer = cmd->sense_buffer; | |
2634 | unsigned long flags; | |
c66ac9db NB |
2635 | u8 asc = 0, ascq = 0; |
2636 | ||
a1d8b49a | 2637 | spin_lock_irqsave(&cmd->t_state_lock, flags); |
c66ac9db | 2638 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { |
a1d8b49a | 2639 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db NB |
2640 | return 0; |
2641 | } | |
2642 | cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION; | |
a1d8b49a | 2643 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); |
c66ac9db NB |
2644 | |
2645 | if (!reason && from_transport) | |
2646 | goto after_reason; | |
2647 | ||
2648 | if (!from_transport) | |
2649 | cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE; | |
9c58b7dd | 2650 | |
c66ac9db NB |
2651 | /* |
2652 | * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses | |
2653 | * SENSE KEY values from include/scsi/scsi.h | |
2654 | */ | |
2655 | switch (reason) { | |
2656 | case TCM_NON_EXISTENT_LUN: | |
eb39d340 | 2657 | /* CURRENT ERROR */ |
9c58b7dd RD |
2658 | buffer[0] = 0x70; |
2659 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
eb39d340 | 2660 | /* ILLEGAL REQUEST */ |
9c58b7dd | 2661 | buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
eb39d340 | 2662 | /* LOGICAL UNIT NOT SUPPORTED */ |
9c58b7dd | 2663 | buffer[SPC_ASC_KEY_OFFSET] = 0x25; |
eb39d340 | 2664 | break; |
c66ac9db NB |
2665 | case TCM_UNSUPPORTED_SCSI_OPCODE: |
2666 | case TCM_SECTOR_COUNT_TOO_MANY: | |
2667 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2668 | buffer[0] = 0x70; |
2669 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2670 | /* ILLEGAL REQUEST */ |
9c58b7dd | 2671 | buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
c66ac9db | 2672 | /* INVALID COMMAND OPERATION CODE */ |
9c58b7dd | 2673 | buffer[SPC_ASC_KEY_OFFSET] = 0x20; |
c66ac9db NB |
2674 | break; |
2675 | case TCM_UNKNOWN_MODE_PAGE: | |
2676 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2677 | buffer[0] = 0x70; |
2678 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2679 | /* ILLEGAL REQUEST */ |
9c58b7dd | 2680 | buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
c66ac9db | 2681 | /* INVALID FIELD IN CDB */ |
9c58b7dd | 2682 | buffer[SPC_ASC_KEY_OFFSET] = 0x24; |
c66ac9db NB |
2683 | break; |
2684 | case TCM_CHECK_CONDITION_ABORT_CMD: | |
2685 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2686 | buffer[0] = 0x70; |
2687 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2688 | /* ABORTED COMMAND */ |
9c58b7dd | 2689 | buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
c66ac9db | 2690 | /* BUS DEVICE RESET FUNCTION OCCURRED */ |
9c58b7dd RD |
2691 | buffer[SPC_ASC_KEY_OFFSET] = 0x29; |
2692 | buffer[SPC_ASCQ_KEY_OFFSET] = 0x03; | |
c66ac9db NB |
2693 | break; |
2694 | case TCM_INCORRECT_AMOUNT_OF_DATA: | |
2695 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2696 | buffer[0] = 0x70; |
2697 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2698 | /* ABORTED COMMAND */ |
9c58b7dd | 2699 | buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
c66ac9db | 2700 | /* WRITE ERROR */ |
9c58b7dd | 2701 | buffer[SPC_ASC_KEY_OFFSET] = 0x0c; |
c66ac9db | 2702 | /* NOT ENOUGH UNSOLICITED DATA */ |
9c58b7dd | 2703 | buffer[SPC_ASCQ_KEY_OFFSET] = 0x0d; |
c66ac9db NB |
2704 | break; |
2705 | case TCM_INVALID_CDB_FIELD: | |
2706 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2707 | buffer[0] = 0x70; |
2708 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
9fbc8909 | 2709 | /* ILLEGAL REQUEST */ |
9c58b7dd | 2710 | buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
c66ac9db | 2711 | /* INVALID FIELD IN CDB */ |
9c58b7dd | 2712 | buffer[SPC_ASC_KEY_OFFSET] = 0x24; |
c66ac9db NB |
2713 | break; |
2714 | case TCM_INVALID_PARAMETER_LIST: | |
2715 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2716 | buffer[0] = 0x70; |
2717 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
9fbc8909 | 2718 | /* ILLEGAL REQUEST */ |
9c58b7dd | 2719 | buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
c66ac9db | 2720 | /* INVALID FIELD IN PARAMETER LIST */ |
9c58b7dd | 2721 | buffer[SPC_ASC_KEY_OFFSET] = 0x26; |
c66ac9db NB |
2722 | break; |
2723 | case TCM_UNEXPECTED_UNSOLICITED_DATA: | |
2724 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2725 | buffer[0] = 0x70; |
2726 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2727 | /* ABORTED COMMAND */ |
9c58b7dd | 2728 | buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
c66ac9db | 2729 | /* WRITE ERROR */ |
9c58b7dd | 2730 | buffer[SPC_ASC_KEY_OFFSET] = 0x0c; |
c66ac9db | 2731 | /* UNEXPECTED_UNSOLICITED_DATA */ |
9c58b7dd | 2732 | buffer[SPC_ASCQ_KEY_OFFSET] = 0x0c; |
c66ac9db NB |
2733 | break; |
2734 | case TCM_SERVICE_CRC_ERROR: | |
2735 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2736 | buffer[0] = 0x70; |
2737 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2738 | /* ABORTED COMMAND */ |
9c58b7dd | 2739 | buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
c66ac9db | 2740 | /* PROTOCOL SERVICE CRC ERROR */ |
9c58b7dd | 2741 | buffer[SPC_ASC_KEY_OFFSET] = 0x47; |
c66ac9db | 2742 | /* N/A */ |
9c58b7dd | 2743 | buffer[SPC_ASCQ_KEY_OFFSET] = 0x05; |
c66ac9db NB |
2744 | break; |
2745 | case TCM_SNACK_REJECTED: | |
2746 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2747 | buffer[0] = 0x70; |
2748 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2749 | /* ABORTED COMMAND */ |
9c58b7dd | 2750 | buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND; |
c66ac9db | 2751 | /* READ ERROR */ |
9c58b7dd | 2752 | buffer[SPC_ASC_KEY_OFFSET] = 0x11; |
c66ac9db | 2753 | /* FAILED RETRANSMISSION REQUEST */ |
9c58b7dd | 2754 | buffer[SPC_ASCQ_KEY_OFFSET] = 0x13; |
c66ac9db NB |
2755 | break; |
2756 | case TCM_WRITE_PROTECTED: | |
2757 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2758 | buffer[0] = 0x70; |
2759 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2760 | /* DATA PROTECT */ |
9c58b7dd | 2761 | buffer[SPC_SENSE_KEY_OFFSET] = DATA_PROTECT; |
c66ac9db | 2762 | /* WRITE PROTECTED */ |
9c58b7dd | 2763 | buffer[SPC_ASC_KEY_OFFSET] = 0x27; |
c66ac9db | 2764 | break; |
e2397c70 RD |
2765 | case TCM_ADDRESS_OUT_OF_RANGE: |
2766 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2767 | buffer[0] = 0x70; |
2768 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
e2397c70 | 2769 | /* ILLEGAL REQUEST */ |
9c58b7dd | 2770 | buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
e2397c70 | 2771 | /* LOGICAL BLOCK ADDRESS OUT OF RANGE */ |
9c58b7dd | 2772 | buffer[SPC_ASC_KEY_OFFSET] = 0x21; |
e2397c70 | 2773 | break; |
c66ac9db NB |
2774 | case TCM_CHECK_CONDITION_UNIT_ATTENTION: |
2775 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2776 | buffer[0] = 0x70; |
2777 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2778 | /* UNIT ATTENTION */ |
9c58b7dd | 2779 | buffer[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION; |
c66ac9db | 2780 | core_scsi3_ua_for_check_condition(cmd, &asc, &ascq); |
9c58b7dd RD |
2781 | buffer[SPC_ASC_KEY_OFFSET] = asc; |
2782 | buffer[SPC_ASCQ_KEY_OFFSET] = ascq; | |
c66ac9db NB |
2783 | break; |
2784 | case TCM_CHECK_CONDITION_NOT_READY: | |
2785 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2786 | buffer[0] = 0x70; |
2787 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2788 | /* Not Ready */ |
9c58b7dd | 2789 | buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY; |
c66ac9db | 2790 | transport_get_sense_codes(cmd, &asc, &ascq); |
9c58b7dd RD |
2791 | buffer[SPC_ASC_KEY_OFFSET] = asc; |
2792 | buffer[SPC_ASCQ_KEY_OFFSET] = ascq; | |
c66ac9db NB |
2793 | break; |
2794 | case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE: | |
2795 | default: | |
2796 | /* CURRENT ERROR */ | |
9c58b7dd RD |
2797 | buffer[0] = 0x70; |
2798 | buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10; | |
c66ac9db | 2799 | /* ILLEGAL REQUEST */ |
9c58b7dd | 2800 | buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST; |
c66ac9db | 2801 | /* LOGICAL UNIT COMMUNICATION FAILURE */ |
9c58b7dd | 2802 | buffer[SPC_ASC_KEY_OFFSET] = 0x80; |
c66ac9db NB |
2803 | break; |
2804 | } | |
2805 | /* | |
2806 | * This code uses linux/include/scsi/scsi.h SAM status codes! | |
2807 | */ | |
2808 | cmd->scsi_status = SAM_STAT_CHECK_CONDITION; | |
2809 | /* | |
2810 | * Automatically padded, this value is encoded in the fabric's | |
2811 | * data_length response PDU containing the SCSI defined sense data. | |
2812 | */ | |
9c58b7dd | 2813 | cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER; |
c66ac9db NB |
2814 | |
2815 | after_reason: | |
07bde79a | 2816 | return cmd->se_tfo->queue_status(cmd); |
c66ac9db NB |
2817 | } |
2818 | EXPORT_SYMBOL(transport_send_check_condition_and_sense); | |
2819 | ||
2820 | int transport_check_aborted_status(struct se_cmd *cmd, int send_status) | |
2821 | { | |
2822 | int ret = 0; | |
2823 | ||
7d680f3b | 2824 | if (cmd->transport_state & CMD_T_ABORTED) { |
6708bb27 | 2825 | if (!send_status || |
c66ac9db NB |
2826 | (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS)) |
2827 | return 1; | |
8b1e1244 | 2828 | |
6708bb27 | 2829 | pr_debug("Sending delayed SAM_STAT_TASK_ABORTED" |
c66ac9db | 2830 | " status for CDB: 0x%02x ITT: 0x%08x\n", |
a1d8b49a | 2831 | cmd->t_task_cdb[0], |
e3d6f909 | 2832 | cmd->se_tfo->get_task_tag(cmd)); |
8b1e1244 | 2833 | |
c66ac9db | 2834 | cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS; |
e3d6f909 | 2835 | cmd->se_tfo->queue_status(cmd); |
c66ac9db NB |
2836 | ret = 1; |
2837 | } | |
2838 | return ret; | |
2839 | } | |
2840 | EXPORT_SYMBOL(transport_check_aborted_status); | |
2841 | ||
2842 | void transport_send_task_abort(struct se_cmd *cmd) | |
2843 | { | |
c252f003 NB |
2844 | unsigned long flags; |
2845 | ||
2846 | spin_lock_irqsave(&cmd->t_state_lock, flags); | |
2847 | if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) { | |
2848 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); | |
2849 | return; | |
2850 | } | |
2851 | spin_unlock_irqrestore(&cmd->t_state_lock, flags); | |
2852 | ||
c66ac9db NB |
2853 | /* |
2854 | * If there are still expected incoming fabric WRITEs, we wait | |
2855 | * until until they have completed before sending a TASK_ABORTED | |
2856 | * response. This response with TASK_ABORTED status will be | |
2857 | * queued back to fabric module by transport_check_aborted_status(). | |
2858 | */ | |
2859 | if (cmd->data_direction == DMA_TO_DEVICE) { | |
e3d6f909 | 2860 | if (cmd->se_tfo->write_pending_status(cmd) != 0) { |
7d680f3b | 2861 | cmd->transport_state |= CMD_T_ABORTED; |
c66ac9db | 2862 | smp_mb__after_atomic_inc(); |
c66ac9db NB |
2863 | } |
2864 | } | |
2865 | cmd->scsi_status = SAM_STAT_TASK_ABORTED; | |
8b1e1244 | 2866 | |
6708bb27 | 2867 | pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x," |
a1d8b49a | 2868 | " ITT: 0x%08x\n", cmd->t_task_cdb[0], |
e3d6f909 | 2869 | cmd->se_tfo->get_task_tag(cmd)); |
8b1e1244 | 2870 | |
e3d6f909 | 2871 | cmd->se_tfo->queue_status(cmd); |
c66ac9db NB |
2872 | } |
2873 | ||
af877292 | 2874 | static void target_tmr_work(struct work_struct *work) |
c66ac9db | 2875 | { |
af877292 | 2876 | struct se_cmd *cmd = container_of(work, struct se_cmd, work); |
5951146d | 2877 | struct se_device *dev = cmd->se_dev; |
c66ac9db NB |
2878 | struct se_tmr_req *tmr = cmd->se_tmr_req; |
2879 | int ret; | |
2880 | ||
2881 | switch (tmr->function) { | |
5c6cd613 | 2882 | case TMR_ABORT_TASK: |
3d28934a | 2883 | core_tmr_abort_task(dev, tmr, cmd->se_sess); |
c66ac9db | 2884 | break; |
5c6cd613 NB |
2885 | case TMR_ABORT_TASK_SET: |
2886 | case TMR_CLEAR_ACA: | |
2887 | case TMR_CLEAR_TASK_SET: | |
c66ac9db NB |
2888 | tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED; |
2889 | break; | |
5c6cd613 | 2890 | case TMR_LUN_RESET: |
c66ac9db NB |
2891 | ret = core_tmr_lun_reset(dev, tmr, NULL, NULL); |
2892 | tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE : | |
2893 | TMR_FUNCTION_REJECTED; | |
2894 | break; | |
5c6cd613 | 2895 | case TMR_TARGET_WARM_RESET: |
c66ac9db NB |
2896 | tmr->response = TMR_FUNCTION_REJECTED; |
2897 | break; | |
5c6cd613 | 2898 | case TMR_TARGET_COLD_RESET: |
c66ac9db NB |
2899 | tmr->response = TMR_FUNCTION_REJECTED; |
2900 | break; | |
c66ac9db | 2901 | default: |
6708bb27 | 2902 | pr_err("Uknown TMR function: 0x%02x.\n", |
c66ac9db NB |
2903 | tmr->function); |
2904 | tmr->response = TMR_FUNCTION_REJECTED; | |
2905 | break; | |
2906 | } | |
2907 | ||
2908 | cmd->t_state = TRANSPORT_ISTATE_PROCESSING; | |
e3d6f909 | 2909 | cmd->se_tfo->queue_tm_rsp(cmd); |
c66ac9db | 2910 | |
b7b8bef7 | 2911 | transport_cmd_check_stop_to_fabric(cmd); |
c66ac9db NB |
2912 | } |
2913 | ||
af877292 CH |
2914 | int transport_generic_handle_tmr( |
2915 | struct se_cmd *cmd) | |
c66ac9db | 2916 | { |
af877292 CH |
2917 | INIT_WORK(&cmd->work, target_tmr_work); |
2918 | queue_work(cmd->se_dev->tmr_wq, &cmd->work); | |
c66ac9db NB |
2919 | return 0; |
2920 | } | |
af877292 | 2921 | EXPORT_SYMBOL(transport_generic_handle_tmr); |