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