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