11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger  * Filename:  target_core_tmr.c
4c66ac9dbSNicholas Bellinger  *
5c66ac9dbSNicholas Bellinger  * This file contains SPC-3 task management infrastructure
6c66ac9dbSNicholas Bellinger  *
74c76251eSNicholas Bellinger  * (c) Copyright 2009-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  ******************************************************************************/
12c66ac9dbSNicholas Bellinger 
13c66ac9dbSNicholas Bellinger #include <linux/slab.h>
14c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
15c66ac9dbSNicholas Bellinger #include <linux/list.h>
16c53181afSPaul Gortmaker #include <linux/export.h>
17c66ac9dbSNicholas Bellinger 
18c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
19c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
20c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
21c66ac9dbSNicholas Bellinger 
22e26d99aeSChristoph Hellwig #include "target_core_internal.h"
23c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
24c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
25c66ac9dbSNicholas Bellinger 
core_tmr_alloc_req(struct se_cmd * se_cmd,void * fabric_tmr_ptr,u8 function,gfp_t gfp_flags)26c8e31f26SAndy Grover int core_tmr_alloc_req(
27c66ac9dbSNicholas Bellinger 	struct se_cmd *se_cmd,
28c66ac9dbSNicholas Bellinger 	void *fabric_tmr_ptr,
29dd503a5fSRoland Dreier 	u8 function,
30dd503a5fSRoland Dreier 	gfp_t gfp_flags)
31c66ac9dbSNicholas Bellinger {
32c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr;
33c66ac9dbSNicholas Bellinger 
34c8e31f26SAndy Grover 	tmr = kzalloc(sizeof(struct se_tmr_req), gfp_flags);
356708bb27SAndy Grover 	if (!tmr) {
366708bb27SAndy Grover 		pr_err("Unable to allocate struct se_tmr_req\n");
37c8e31f26SAndy Grover 		return -ENOMEM;
38c66ac9dbSNicholas Bellinger 	}
39c8e31f26SAndy Grover 
40c8e31f26SAndy Grover 	se_cmd->se_cmd_flags |= SCF_SCSI_TMR_CDB;
41c8e31f26SAndy Grover 	se_cmd->se_tmr_req = tmr;
42c66ac9dbSNicholas Bellinger 	tmr->task_cmd = se_cmd;
43c66ac9dbSNicholas Bellinger 	tmr->fabric_tmr_ptr = fabric_tmr_ptr;
44c66ac9dbSNicholas Bellinger 	tmr->function = function;
45c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tmr->tmr_list);
46c66ac9dbSNicholas Bellinger 
47c8e31f26SAndy Grover 	return 0;
48c66ac9dbSNicholas Bellinger }
49c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tmr_alloc_req);
50c66ac9dbSNicholas Bellinger 
core_tmr_release_req(struct se_tmr_req * tmr)518f832690SJoern Engel void core_tmr_release_req(struct se_tmr_req *tmr)
52c66ac9dbSNicholas Bellinger {
53c8e31f26SAndy Grover 	kfree(tmr);
54c66ac9dbSNicholas Bellinger }
55c66ac9dbSNicholas Bellinger 
target_check_cdb_and_preempt(struct list_head * list,struct se_cmd * cmd)56feae8564SJörn Engel static int target_check_cdb_and_preempt(struct list_head *list,
57c165f69cSJörn Engel 		struct se_cmd *cmd)
58c165f69cSJörn Engel {
59feae8564SJörn Engel 	struct t10_pr_registration *reg;
60c165f69cSJörn Engel 
61feae8564SJörn Engel 	if (!list)
62feae8564SJörn Engel 		return 0;
63feae8564SJörn Engel 	list_for_each_entry(reg, list, pr_reg_abort_list) {
64feae8564SJörn Engel 		if (reg->pr_res_key == cmd->pr_res_key)
65c165f69cSJörn Engel 			return 0;
66c165f69cSJörn Engel 	}
67c165f69cSJörn Engel 
68c165f69cSJörn Engel 	return 1;
69c165f69cSJörn Engel }
70c165f69cSJörn Engel 
__target_check_io_state(struct se_cmd * se_cmd,struct se_session * tmr_sess,bool tas)710f4a9431SNicholas Bellinger static bool __target_check_io_state(struct se_cmd *se_cmd,
7287310c9fSDavid Disseldorp 				    struct se_session *tmr_sess, bool tas)
73febe562cSNicholas Bellinger {
74febe562cSNicholas Bellinger 	struct se_session *sess = se_cmd->se_sess;
75febe562cSNicholas Bellinger 
7684ed64b1SMaurizio Lombardi 	lockdep_assert_held(&sess->sess_cmd_lock);
7784ed64b1SMaurizio Lombardi 
78febe562cSNicholas Bellinger 	/*
79febe562cSNicholas Bellinger 	 * If command already reached CMD_T_COMPLETE state within
800f4a9431SNicholas Bellinger 	 * target_complete_cmd() or CMD_T_FABRIC_STOP due to shutdown,
810f4a9431SNicholas Bellinger 	 * this se_cmd has been passed to fabric driver and will
820f4a9431SNicholas Bellinger 	 * not be aborted.
83febe562cSNicholas Bellinger 	 *
84febe562cSNicholas Bellinger 	 * Otherwise, obtain a local se_cmd->cmd_kref now for TMR
85febe562cSNicholas Bellinger 	 * ABORT_TASK + LUN_RESET for CMD_T_ABORTED processing as
86febe562cSNicholas Bellinger 	 * long as se_cmd->cmd_kref is still active unless zero.
87febe562cSNicholas Bellinger 	 */
88febe562cSNicholas Bellinger 	spin_lock(&se_cmd->t_state_lock);
890f4a9431SNicholas Bellinger 	if (se_cmd->transport_state & (CMD_T_COMPLETE | CMD_T_FABRIC_STOP)) {
900f4a9431SNicholas Bellinger 		pr_debug("Attempted to abort io tag: %llu already complete or"
910f4a9431SNicholas Bellinger 			" fabric stop, skipping\n", se_cmd->tag);
920f4a9431SNicholas Bellinger 		spin_unlock(&se_cmd->t_state_lock);
930f4a9431SNicholas Bellinger 		return false;
940f4a9431SNicholas Bellinger 	}
95febe562cSNicholas Bellinger 	se_cmd->transport_state |= CMD_T_ABORTED;
960f4a9431SNicholas Bellinger 
970f4a9431SNicholas Bellinger 	if ((tmr_sess != se_cmd->se_sess) && tas)
980f4a9431SNicholas Bellinger 		se_cmd->transport_state |= CMD_T_TAS;
990f4a9431SNicholas Bellinger 
100febe562cSNicholas Bellinger 	spin_unlock(&se_cmd->t_state_lock);
101febe562cSNicholas Bellinger 
102febe562cSNicholas Bellinger 	return kref_get_unless_zero(&se_cmd->cmd_kref);
103febe562cSNicholas Bellinger }
104febe562cSNicholas Bellinger 
core_tmr_abort_task(struct se_device * dev,struct se_tmr_req * tmr,struct se_session * se_sess)1053d28934aSNicholas Bellinger void core_tmr_abort_task(
1063d28934aSNicholas Bellinger 	struct se_device *dev,
1073d28934aSNicholas Bellinger 	struct se_tmr_req *tmr,
1083d28934aSNicholas Bellinger 	struct se_session *se_sess)
1093d28934aSNicholas Bellinger {
1102e45a1a9SBodo Stroesser 	LIST_HEAD(aborted_list);
111f5e2714aSBodo Stroesser 	struct se_cmd *se_cmd, *next;
1123d28934aSNicholas Bellinger 	unsigned long flags;
113f5e2714aSBodo Stroesser 	bool rc;
114649ee054SBart Van Assche 	u64 ref_tag;
1151526d9f1SMike Christie 	int i;
1163d28934aSNicholas Bellinger 
1171526d9f1SMike Christie 	for (i = 0; i < dev->queue_cnt; i++) {
1183d75948bSMike Christie 		flush_work(&dev->queues[i].sq.work);
1193d75948bSMike Christie 
1201526d9f1SMike Christie 		spin_lock_irqsave(&dev->queues[i].lock, flags);
1211526d9f1SMike Christie 		list_for_each_entry_safe(se_cmd, next, &dev->queues[i].state_list,
1221526d9f1SMike Christie 					 state_list) {
123f5e2714aSBodo Stroesser 			if (se_sess != se_cmd->se_sess)
1243d28934aSNicholas Bellinger 				continue;
12547b1584cSAlex Leung 
1261526d9f1SMike Christie 			/*
1271526d9f1SMike Christie 			 * skip task management functions, including
1281526d9f1SMike Christie 			 * tmr->task_cmd
1291526d9f1SMike Christie 			 */
130dc0fafdaSBart Van Assche 			if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
13147b1584cSAlex Leung 				continue;
13247b1584cSAlex Leung 
133649ee054SBart Van Assche 			ref_tag = se_cmd->tag;
1343d28934aSNicholas Bellinger 			if (tmr->ref_task_tag != ref_tag)
1353d28934aSNicholas Bellinger 				continue;
1363d28934aSNicholas Bellinger 
1371526d9f1SMike Christie 			pr_err("ABORT_TASK: Found referenced %s task_tag: %llu\n",
13830c7ca93SDavid Disseldorp 			       se_cmd->se_tfo->fabric_name, ref_tag);
1393d28934aSNicholas Bellinger 
140f5e2714aSBodo Stroesser 			spin_lock(&se_sess->sess_cmd_lock);
141f5e2714aSBodo Stroesser 			rc = __target_check_io_state(se_cmd, se_sess, 0);
142f5e2714aSBodo Stroesser 			spin_unlock(&se_sess->sess_cmd_lock);
143f5e2714aSBodo Stroesser 			if (!rc)
1443a1e7ca6SBart Van Assche 				continue;
1453a1e7ca6SBart Van Assche 
1462e45a1a9SBodo Stroesser 			list_move_tail(&se_cmd->state_list, &aborted_list);
147f5e2714aSBodo Stroesser 			se_cmd->state_active = false;
1481526d9f1SMike Christie 			spin_unlock_irqrestore(&dev->queues[i].lock, flags);
1493d28934aSNicholas Bellinger 
1502e45a1a9SBodo Stroesser 			if (dev->transport->tmr_notify)
1512e45a1a9SBodo Stroesser 				dev->transport->tmr_notify(dev, TMR_ABORT_TASK,
1522e45a1a9SBodo Stroesser 							   &aborted_list);
1532e45a1a9SBodo Stroesser 
1542e45a1a9SBodo Stroesser 			list_del_init(&se_cmd->state_list);
1552c9fa49eSBart Van Assche 			target_put_cmd_and_wait(se_cmd);
1563d28934aSNicholas Bellinger 
1571526d9f1SMike Christie 			pr_err("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for ref_tag: %llu\n",
1581526d9f1SMike Christie 			       ref_tag);
1593d28934aSNicholas Bellinger 			tmr->response = TMR_FUNCTION_COMPLETE;
160c87ba9c4SNicholas Bellinger 			atomic_long_inc(&dev->aborts_complete);
1613d28934aSNicholas Bellinger 			return;
1623d28934aSNicholas Bellinger 		}
1631526d9f1SMike Christie 		spin_unlock_irqrestore(&dev->queues[i].lock, flags);
1641526d9f1SMike Christie 	}
1653d28934aSNicholas Bellinger 
1662e45a1a9SBodo Stroesser 	if (dev->transport->tmr_notify)
1672e45a1a9SBodo Stroesser 		dev->transport->tmr_notify(dev, TMR_ABORT_TASK, &aborted_list);
1682e45a1a9SBodo Stroesser 
169649ee054SBart Van Assche 	printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %lld\n",
1703d28934aSNicholas Bellinger 			tmr->ref_task_tag);
1713d28934aSNicholas Bellinger 	tmr->response = TMR_TASK_DOES_NOT_EXIST;
172c87ba9c4SNicholas Bellinger 	atomic_long_inc(&dev->aborts_no_task);
1733d28934aSNicholas Bellinger }
1743d28934aSNicholas Bellinger 
core_tmr_drain_tmr_list(struct se_device * dev,struct se_tmr_req * tmr,struct list_head * preempt_and_abort_list)175d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
176c66ac9dbSNicholas Bellinger 	struct se_device *dev,
177c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
178d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
179c66ac9dbSNicholas Bellinger {
180d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
181a6d9bb1cSNicholas Bellinger 	struct se_session *sess;
182c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
183d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
184c66ac9dbSNicholas Bellinger 	unsigned long flags;
185a6d9bb1cSNicholas Bellinger 	bool rc;
186c66ac9dbSNicholas Bellinger 	/*
187c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
188c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
189c66ac9dbSNicholas Bellinger 	 */
190d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
191c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
192*673db054SMike Christie 		if (tmr_p == tmr)
193*673db054SMike Christie 			continue;
194*673db054SMike Christie 
195c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
1966708bb27SAndy Grover 		if (!cmd) {
1976708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
198c66ac9dbSNicholas Bellinger 			continue;
199c66ac9dbSNicholas Bellinger 		}
200*673db054SMike Christie 
201*673db054SMike Christie 		/*
202*673db054SMike Christie 		 * We only execute one LUN_RESET at a time so we can't wait
203*673db054SMike Christie 		 * on them below.
204*673db054SMike Christie 		 */
205*673db054SMike Christie 		if (tmr_p->function == TMR_LUN_RESET)
206*673db054SMike Christie 			continue;
207*673db054SMike Christie 
208c66ac9dbSNicholas Bellinger 		/*
209c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
210c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
211ac75d8beSBart Van Assche 		 * skip non registration key matching TMRs.
212c66ac9dbSNicholas Bellinger 		 */
213feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
214c66ac9dbSNicholas Bellinger 			continue;
215c66ac9dbSNicholas Bellinger 
216a6d9bb1cSNicholas Bellinger 		sess = cmd->se_sess;
217a6d9bb1cSNicholas Bellinger 		if (WARN_ON_ONCE(!sess))
218a6d9bb1cSNicholas Bellinger 			continue;
219a6d9bb1cSNicholas Bellinger 
220a6d9bb1cSNicholas Bellinger 		spin_lock(&sess->sess_cmd_lock);
2212281c95fSBart Van Assche 		rc = __target_check_io_state(cmd, sess, 0);
222a6d9bb1cSNicholas Bellinger 		spin_unlock(&sess->sess_cmd_lock);
223d050ffb9SNicholas Bellinger 
224a6d9bb1cSNicholas Bellinger 		if (!rc) {
225a6d9bb1cSNicholas Bellinger 			printk("LUN_RESET TMR: non-zero kref_get_unless_zero\n");
226a6d9bb1cSNicholas Bellinger 			continue;
227a6d9bb1cSNicholas Bellinger 		}
2280f4a9431SNicholas Bellinger 
2296eb40b2aSJoern Engel 		list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
23012b6fcd0SDmitry Bogdanov 		tmr_p->tmr_dev = NULL;
231d050ffb9SNicholas Bellinger 	}
232d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
233d050ffb9SNicholas Bellinger 
234abc1fd4fSJoern Engel 	list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
235b8a11d73SJoern Engel 		list_del_init(&tmr_p->tmr_list);
236abc1fd4fSJoern Engel 		cmd = tmr_p->task_cmd;
237d050ffb9SNicholas Bellinger 
2386708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
239c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
240abc1fd4fSJoern Engel 			(preempt_and_abort_list) ? "Preempt" : "", tmr_p,
241abc1fd4fSJoern Engel 			tmr_p->function, tmr_p->response, cmd->t_state);
242c66ac9dbSNicholas Bellinger 
2432c9fa49eSBart Van Assche 		target_put_cmd_and_wait(cmd);
244c66ac9dbSNicholas Bellinger 	}
245d050ffb9SNicholas Bellinger }
246d050ffb9SNicholas Bellinger 
2472c9fa49eSBart Van Assche /**
2482c9fa49eSBart Van Assche  * core_tmr_drain_state_list() - abort SCSI commands associated with a device
2492c9fa49eSBart Van Assche  *
2502c9fa49eSBart Van Assche  * @dev:       Device for which to abort outstanding SCSI commands.
2512c9fa49eSBart Van Assche  * @prout_cmd: Pointer to the SCSI PREEMPT AND ABORT if this function is called
2522c9fa49eSBart Van Assche  *             to realize the PREEMPT AND ABORT functionality.
2532c9fa49eSBart Van Assche  * @tmr_sess:  Session through which the LUN RESET has been received.
2542c9fa49eSBart Van Assche  * @tas:       Task Aborted Status (TAS) bit from the SCSI control mode page.
2552c9fa49eSBart Van Assche  *             A quote from SPC-4, paragraph "7.5.10 Control mode page":
2562c9fa49eSBart Van Assche  *             "A task aborted status (TAS) bit set to zero specifies that
2572c9fa49eSBart Van Assche  *             aborted commands shall be terminated by the device server
2582c9fa49eSBart Van Assche  *             without any response to the application client. A TAS bit set
2592c9fa49eSBart Van Assche  *             to one specifies that commands aborted by the actions of an I_T
2602c9fa49eSBart Van Assche  *             nexus other than the I_T nexus on which the command was
2612c9fa49eSBart Van Assche  *             received shall be completed with TASK ABORTED status."
2622c9fa49eSBart Van Assche  * @preempt_and_abort_list: For the PREEMPT AND ABORT functionality, a list
2632c9fa49eSBart Van Assche  *             with registrations that will be preempted.
2642c9fa49eSBart Van Assche  */
core_tmr_drain_state_list(struct se_device * dev,struct se_cmd * prout_cmd,struct se_session * tmr_sess,bool tas,struct list_head * preempt_and_abort_list)265cf572a96SChristoph Hellwig static void core_tmr_drain_state_list(
266d050ffb9SNicholas Bellinger 	struct se_device *dev,
267d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
268ebde1ca5SNicholas Bellinger 	struct se_session *tmr_sess,
26987310c9fSDavid Disseldorp 	bool tas,
270d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
271d050ffb9SNicholas Bellinger {
272d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
273febe562cSNicholas Bellinger 	struct se_session *sess;
274cf572a96SChristoph Hellwig 	struct se_cmd *cmd, *next;
275d050ffb9SNicholas Bellinger 	unsigned long flags;
2761526d9f1SMike Christie 	int rc, i;
277cf572a96SChristoph Hellwig 
278c66ac9dbSNicholas Bellinger 	/*
279cf572a96SChristoph Hellwig 	 * Complete outstanding commands with TASK_ABORTED SAM status.
280cf572a96SChristoph Hellwig 	 *
281c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
282c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
283c66ac9dbSNicholas Bellinger 	 *
284c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
285c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
286c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
287c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
288c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
289c66ac9dbSNicholas Bellinger 	 *
290c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
291c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
292c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
293c66ac9dbSNicholas Bellinger 	 *
294c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
295c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
296c66ac9dbSNicholas Bellinger 	 *
297c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
298c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
299c66ac9dbSNicholas Bellinger 	 */
3001526d9f1SMike Christie 	for (i = 0; i < dev->queue_cnt; i++) {
3013d75948bSMike Christie 		flush_work(&dev->queues[i].sq.work);
3023d75948bSMike Christie 
3031526d9f1SMike Christie 		spin_lock_irqsave(&dev->queues[i].lock, flags);
3041526d9f1SMike Christie 		list_for_each_entry_safe(cmd, next, &dev->queues[i].state_list,
3051526d9f1SMike Christie 					 state_list) {
306c66ac9dbSNicholas Bellinger 			/*
307c66ac9dbSNicholas Bellinger 			 * For PREEMPT_AND_ABORT usage, only process commands
308c66ac9dbSNicholas Bellinger 			 * with a matching reservation key.
309c66ac9dbSNicholas Bellinger 			 */
3101526d9f1SMike Christie 			if (target_check_cdb_and_preempt(preempt_and_abort_list,
3111526d9f1SMike Christie 							 cmd))
312c66ac9dbSNicholas Bellinger 				continue;
313cf572a96SChristoph Hellwig 
314c66ac9dbSNicholas Bellinger 			/*
315c66ac9dbSNicholas Bellinger 			 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
316c66ac9dbSNicholas Bellinger 			 */
317c66ac9dbSNicholas Bellinger 			if (prout_cmd == cmd)
318c66ac9dbSNicholas Bellinger 				continue;
319c66ac9dbSNicholas Bellinger 
320febe562cSNicholas Bellinger 			sess = cmd->se_sess;
321febe562cSNicholas Bellinger 			if (WARN_ON_ONCE(!sess))
322febe562cSNicholas Bellinger 				continue;
323febe562cSNicholas Bellinger 
324febe562cSNicholas Bellinger 			spin_lock(&sess->sess_cmd_lock);
3250f4a9431SNicholas Bellinger 			rc = __target_check_io_state(cmd, tmr_sess, tas);
326febe562cSNicholas Bellinger 			spin_unlock(&sess->sess_cmd_lock);
327febe562cSNicholas Bellinger 			if (!rc)
328febe562cSNicholas Bellinger 				continue;
329febe562cSNicholas Bellinger 
330cf572a96SChristoph Hellwig 			list_move_tail(&cmd->state_list, &drain_task_list);
331cf572a96SChristoph Hellwig 			cmd->state_active = false;
332d050ffb9SNicholas Bellinger 		}
3331526d9f1SMike Christie 		spin_unlock_irqrestore(&dev->queues[i].lock, flags);
3341526d9f1SMike Christie 	}
335c66ac9dbSNicholas Bellinger 
3362e45a1a9SBodo Stroesser 	if (dev->transport->tmr_notify)
3372e45a1a9SBodo Stroesser 		dev->transport->tmr_notify(dev, preempt_and_abort_list ?
3382e45a1a9SBodo Stroesser 					   TMR_LUN_RESET_PRO : TMR_LUN_RESET,
3392e45a1a9SBodo Stroesser 					   &drain_task_list);
3402e45a1a9SBodo Stroesser 
341d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
342cf572a96SChristoph Hellwig 		cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
343febe562cSNicholas Bellinger 		list_del_init(&cmd->state_list);
344d050ffb9SNicholas Bellinger 
345c00e6220SBart Van Assche 		target_show_cmd("LUN_RESET: ", cmd);
346c00e6220SBart Van Assche 		pr_debug("LUN_RESET: ITT[0x%08llx] - %s pr_res_key: 0x%016Lx\n",
347c00e6220SBart Van Assche 			 cmd->tag, (preempt_and_abort_list) ? "preempt" : "",
348c00e6220SBart Van Assche 			 cmd->pr_res_key);
349c66ac9dbSNicholas Bellinger 
3502c9fa49eSBart Van Assche 		target_put_cmd_and_wait(cmd);
351c66ac9dbSNicholas Bellinger 	}
352d050ffb9SNicholas Bellinger }
353d050ffb9SNicholas Bellinger 
core_tmr_lun_reset(struct se_device * dev,struct se_tmr_req * tmr,struct list_head * preempt_and_abort_list,struct se_cmd * prout_cmd)354d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
355d050ffb9SNicholas Bellinger         struct se_device *dev,
356d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
357d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
358d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
359d050ffb9SNicholas Bellinger {
360d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
361d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
362ebde1ca5SNicholas Bellinger 	struct se_session *tmr_sess = NULL;
36387310c9fSDavid Disseldorp 	bool tas;
364d050ffb9SNicholas Bellinger         /*
365d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
366d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
367d050ffb9SNicholas Bellinger 	 *
368d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
369d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
370d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
371d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
372d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
373d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
374d050ffb9SNicholas Bellinger 	 */
3750fd97ccfSChristoph Hellwig 	tas = dev->dev_attrib.emulate_tas;
376d050ffb9SNicholas Bellinger 	/*
377d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
378d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
379d050ffb9SNicholas Bellinger 	 */
380d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
381ebde1ca5SNicholas Bellinger 		tmr_sess = tmr->task_cmd->se_sess;
382ebde1ca5SNicholas Bellinger 		tmr_nacl = tmr_sess->se_node_acl;
383ebde1ca5SNicholas Bellinger 		tmr_tpg = tmr_sess->se_tpg;
384d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
385d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
386d050ffb9SNicholas Bellinger 				" initiator port %s\n",
38730c7ca93SDavid Disseldorp 				tmr_tpg->se_tpg_tfo->fabric_name,
388d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
389d050ffb9SNicholas Bellinger 		}
390d050ffb9SNicholas Bellinger 	}
391*673db054SMike Christie 
392*673db054SMike Christie 
393*673db054SMike Christie 	/*
394*673db054SMike Christie 	 * We only allow one reset or preempt and abort to execute at a time
395*673db054SMike Christie 	 * to prevent one call from claiming all the cmds causing a second
396*673db054SMike Christie 	 * call from returning while cmds it should have waited on are still
397*673db054SMike Christie 	 * running.
398*673db054SMike Christie 	 */
399*673db054SMike Christie 	mutex_lock(&dev->lun_reset_mutex);
400*673db054SMike Christie 
401d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
402d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
403d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
404d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
405ebde1ca5SNicholas Bellinger 	core_tmr_drain_state_list(dev, prout_cmd, tmr_sess, tas,
406d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
407af877292SChristoph Hellwig 
408*673db054SMike Christie 	mutex_unlock(&dev->lun_reset_mutex);
409*673db054SMike Christie 
410c66ac9dbSNicholas Bellinger 	/*
411c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
412c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
413c66ac9dbSNicholas Bellinger 	 */
4146708bb27SAndy Grover 	if (!preempt_and_abort_list &&
4150fd97ccfSChristoph Hellwig 	     (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) {
416c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
417fae43461SBart Van Assche 		dev->reservation_holder = NULL;
4180fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
419c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
4206708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
421c66ac9dbSNicholas Bellinger 	}
422c66ac9dbSNicholas Bellinger 
423ee480683SNicholas Bellinger 	atomic_long_inc(&dev->num_resets);
424c66ac9dbSNicholas Bellinger 
4256708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
426c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
427e3d6f909SAndy Grover 			dev->transport->name);
428c66ac9dbSNicholas Bellinger 	return 0;
429c66ac9dbSNicholas Bellinger }
430d050ffb9SNicholas Bellinger 
431