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