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