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