1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_tmr.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains SPC-3 task management infrastructure
5c66ac9dbSNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2009-2013 Datera, Inc.
7c66ac9dbSNicholas Bellinger  *
8c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
11c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
12c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
13c66ac9dbSNicholas Bellinger  * (at your option) any later version.
14c66ac9dbSNicholas Bellinger  *
15c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
16c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
19c66ac9dbSNicholas Bellinger  *
20c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
21c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
22c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23c66ac9dbSNicholas Bellinger  *
24c66ac9dbSNicholas Bellinger  ******************************************************************************/
25c66ac9dbSNicholas Bellinger 
26c66ac9dbSNicholas Bellinger #include <linux/slab.h>
27c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
28c66ac9dbSNicholas Bellinger #include <linux/list.h>
29c53181afSPaul Gortmaker #include <linux/export.h>
30c66ac9dbSNicholas Bellinger 
31c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
32c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
33c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
34c66ac9dbSNicholas Bellinger 
35e26d99aeSChristoph Hellwig #include "target_core_internal.h"
36c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
37c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
38c66ac9dbSNicholas Bellinger 
39c8e31f26SAndy Grover int core_tmr_alloc_req(
40c66ac9dbSNicholas Bellinger 	struct se_cmd *se_cmd,
41c66ac9dbSNicholas Bellinger 	void *fabric_tmr_ptr,
42dd503a5fSRoland Dreier 	u8 function,
43dd503a5fSRoland Dreier 	gfp_t gfp_flags)
44c66ac9dbSNicholas Bellinger {
45c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr;
46c66ac9dbSNicholas Bellinger 
47c8e31f26SAndy Grover 	tmr = kzalloc(sizeof(struct se_tmr_req), gfp_flags);
486708bb27SAndy Grover 	if (!tmr) {
496708bb27SAndy Grover 		pr_err("Unable to allocate struct se_tmr_req\n");
50c8e31f26SAndy Grover 		return -ENOMEM;
51c66ac9dbSNicholas Bellinger 	}
52c8e31f26SAndy Grover 
53c8e31f26SAndy Grover 	se_cmd->se_cmd_flags |= SCF_SCSI_TMR_CDB;
54c8e31f26SAndy Grover 	se_cmd->se_tmr_req = tmr;
55c66ac9dbSNicholas Bellinger 	tmr->task_cmd = se_cmd;
56c66ac9dbSNicholas Bellinger 	tmr->fabric_tmr_ptr = fabric_tmr_ptr;
57c66ac9dbSNicholas Bellinger 	tmr->function = function;
58c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tmr->tmr_list);
59c66ac9dbSNicholas Bellinger 
60c8e31f26SAndy Grover 	return 0;
61c66ac9dbSNicholas Bellinger }
62c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tmr_alloc_req);
63c66ac9dbSNicholas Bellinger 
648f832690SJoern Engel void core_tmr_release_req(struct se_tmr_req *tmr)
65c66ac9dbSNicholas Bellinger {
66c66ac9dbSNicholas Bellinger 	struct se_device *dev = tmr->tmr_dev;
67d050ffb9SNicholas Bellinger 	unsigned long flags;
68c66ac9dbSNicholas Bellinger 
698f832690SJoern Engel 	if (dev) {
70d050ffb9SNicholas Bellinger 		spin_lock_irqsave(&dev->se_tmr_lock, flags);
71a6d9bb1cSNicholas Bellinger 		list_del_init(&tmr->tmr_list);
72d050ffb9SNicholas Bellinger 		spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
738f832690SJoern Engel 	}
747fd29aa9SNicholas Bellinger 
75c8e31f26SAndy Grover 	kfree(tmr);
76c66ac9dbSNicholas Bellinger }
77c66ac9dbSNicholas Bellinger 
78c66ac9dbSNicholas Bellinger static void core_tmr_handle_tas_abort(
79c66ac9dbSNicholas Bellinger 	struct se_node_acl *tmr_nacl,
80c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
81c1c35d52SNicholas Bellinger 	int tas)
82c66ac9dbSNicholas Bellinger {
8368259b5aSAlex Leung 	bool remove = true;
84c66ac9dbSNicholas Bellinger 	/*
85c66ac9dbSNicholas Bellinger 	 * TASK ABORTED status (TAS) bit support
86c66ac9dbSNicholas Bellinger 	 */
878f832690SJoern Engel 	if ((tmr_nacl && (tmr_nacl != cmd->se_sess->se_node_acl)) && tas) {
8868259b5aSAlex Leung 		remove = false;
89c66ac9dbSNicholas Bellinger 		transport_send_task_abort(cmd);
9068259b5aSAlex Leung 	}
91c66ac9dbSNicholas Bellinger 
9268259b5aSAlex Leung 	transport_cmd_finish_abort(cmd, remove);
93c66ac9dbSNicholas Bellinger }
94c66ac9dbSNicholas Bellinger 
95feae8564SJörn Engel static int target_check_cdb_and_preempt(struct list_head *list,
96c165f69cSJörn Engel 		struct se_cmd *cmd)
97c165f69cSJörn Engel {
98feae8564SJörn Engel 	struct t10_pr_registration *reg;
99c165f69cSJörn Engel 
100feae8564SJörn Engel 	if (!list)
101feae8564SJörn Engel 		return 0;
102feae8564SJörn Engel 	list_for_each_entry(reg, list, pr_reg_abort_list) {
103feae8564SJörn Engel 		if (reg->pr_res_key == cmd->pr_res_key)
104c165f69cSJörn Engel 			return 0;
105c165f69cSJörn Engel 	}
106c165f69cSJörn Engel 
107c165f69cSJörn Engel 	return 1;
108c165f69cSJörn Engel }
109c165f69cSJörn Engel 
110febe562cSNicholas Bellinger static bool __target_check_io_state(struct se_cmd *se_cmd)
111febe562cSNicholas Bellinger {
112febe562cSNicholas Bellinger 	struct se_session *sess = se_cmd->se_sess;
113febe562cSNicholas Bellinger 
114febe562cSNicholas Bellinger 	assert_spin_locked(&sess->sess_cmd_lock);
115febe562cSNicholas Bellinger 	WARN_ON_ONCE(!irqs_disabled());
116febe562cSNicholas Bellinger 	/*
117febe562cSNicholas Bellinger 	 * If command already reached CMD_T_COMPLETE state within
118febe562cSNicholas Bellinger 	 * target_complete_cmd(), this se_cmd has been passed to
119febe562cSNicholas Bellinger 	 * fabric driver and will not be aborted.
120febe562cSNicholas Bellinger 	 *
121febe562cSNicholas Bellinger 	 * Otherwise, obtain a local se_cmd->cmd_kref now for TMR
122febe562cSNicholas Bellinger 	 * ABORT_TASK + LUN_RESET for CMD_T_ABORTED processing as
123febe562cSNicholas Bellinger 	 * long as se_cmd->cmd_kref is still active unless zero.
124febe562cSNicholas Bellinger 	 */
125febe562cSNicholas Bellinger 	spin_lock(&se_cmd->t_state_lock);
126febe562cSNicholas Bellinger 	if (se_cmd->transport_state & CMD_T_COMPLETE) {
127febe562cSNicholas Bellinger 		pr_debug("Attempted to abort io tag: %llu already complete,"
128febe562cSNicholas Bellinger 			" skipping\n", se_cmd->tag);
129febe562cSNicholas Bellinger 		spin_unlock(&se_cmd->t_state_lock);
130febe562cSNicholas Bellinger 		return false;
131febe562cSNicholas Bellinger 	}
132febe562cSNicholas Bellinger 	se_cmd->transport_state |= CMD_T_ABORTED;
133febe562cSNicholas Bellinger 	spin_unlock(&se_cmd->t_state_lock);
134febe562cSNicholas Bellinger 
135febe562cSNicholas Bellinger 	return kref_get_unless_zero(&se_cmd->cmd_kref);
136febe562cSNicholas Bellinger }
137febe562cSNicholas Bellinger 
1383d28934aSNicholas Bellinger void core_tmr_abort_task(
1393d28934aSNicholas Bellinger 	struct se_device *dev,
1403d28934aSNicholas Bellinger 	struct se_tmr_req *tmr,
1413d28934aSNicholas Bellinger 	struct se_session *se_sess)
1423d28934aSNicholas Bellinger {
143f81ccb48SJoern Engel 	struct se_cmd *se_cmd;
1443d28934aSNicholas Bellinger 	unsigned long flags;
145649ee054SBart Van Assche 	u64 ref_tag;
1463d28934aSNicholas Bellinger 
1473d28934aSNicholas Bellinger 	spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
148f81ccb48SJoern Engel 	list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1493d28934aSNicholas Bellinger 
1503d28934aSNicholas Bellinger 		if (dev != se_cmd->se_dev)
1513d28934aSNicholas Bellinger 			continue;
15247b1584cSAlex Leung 
153dc0fafdaSBart Van Assche 		/* skip task management functions, including tmr->task_cmd */
154dc0fafdaSBart Van Assche 		if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
15547b1584cSAlex Leung 			continue;
15647b1584cSAlex Leung 
157649ee054SBart Van Assche 		ref_tag = se_cmd->tag;
1583d28934aSNicholas Bellinger 		if (tmr->ref_task_tag != ref_tag)
1593d28934aSNicholas Bellinger 			continue;
1603d28934aSNicholas Bellinger 
161649ee054SBart Van Assche 		printk("ABORT_TASK: Found referenced %s task_tag: %llu\n",
1623d28934aSNicholas Bellinger 			se_cmd->se_tfo->get_fabric_name(), ref_tag);
1633d28934aSNicholas Bellinger 
164febe562cSNicholas Bellinger 		if (!__target_check_io_state(se_cmd)) {
1653d28934aSNicholas Bellinger 			spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1669ff9d15eSBart Van Assche 			target_put_sess_cmd(se_cmd);
1673d28934aSNicholas Bellinger 			goto out;
1683d28934aSNicholas Bellinger 		}
1693d28934aSNicholas Bellinger 		list_del_init(&se_cmd->se_cmd_list);
1703d28934aSNicholas Bellinger 		spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1713d28934aSNicholas Bellinger 
1723d28934aSNicholas Bellinger 		cancel_work_sync(&se_cmd->work);
1733d28934aSNicholas Bellinger 		transport_wait_for_tasks(se_cmd);
1743d28934aSNicholas Bellinger 
17568259b5aSAlex Leung 		transport_cmd_finish_abort(se_cmd, true);
176febe562cSNicholas Bellinger 		target_put_sess_cmd(se_cmd);
1773d28934aSNicholas Bellinger 
1783d28934aSNicholas Bellinger 		printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for"
179649ee054SBart Van Assche 				" ref_tag: %llu\n", ref_tag);
1803d28934aSNicholas Bellinger 		tmr->response = TMR_FUNCTION_COMPLETE;
1813d28934aSNicholas Bellinger 		return;
1823d28934aSNicholas Bellinger 	}
1833d28934aSNicholas Bellinger 	spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1843d28934aSNicholas Bellinger 
1853d28934aSNicholas Bellinger out:
186649ee054SBart Van Assche 	printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %lld\n",
1873d28934aSNicholas Bellinger 			tmr->ref_task_tag);
1883d28934aSNicholas Bellinger 	tmr->response = TMR_TASK_DOES_NOT_EXIST;
1893d28934aSNicholas Bellinger }
1903d28934aSNicholas Bellinger 
191d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
192c66ac9dbSNicholas Bellinger 	struct se_device *dev,
193c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
194d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
195c66ac9dbSNicholas Bellinger {
196d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
197a6d9bb1cSNicholas Bellinger 	struct se_session *sess;
198c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
199d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
200c66ac9dbSNicholas Bellinger 	unsigned long flags;
201a6d9bb1cSNicholas Bellinger 	bool rc;
202c66ac9dbSNicholas Bellinger 	/*
203c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
204c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
205c66ac9dbSNicholas Bellinger 	 */
206d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
207c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
208c66ac9dbSNicholas Bellinger 		/*
209c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
210c66ac9dbSNicholas Bellinger 		 */
211abc1fd4fSJoern Engel 		if (tmr_p == tmr)
212c66ac9dbSNicholas Bellinger 			continue;
213c66ac9dbSNicholas Bellinger 
214c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
2156708bb27SAndy Grover 		if (!cmd) {
2166708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
217c66ac9dbSNicholas Bellinger 			continue;
218c66ac9dbSNicholas Bellinger 		}
219c66ac9dbSNicholas Bellinger 		/*
220c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
221c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
222ac75d8beSBart Van Assche 		 * skip non registration key matching TMRs.
223c66ac9dbSNicholas Bellinger 		 */
224feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
225c66ac9dbSNicholas Bellinger 			continue;
226c66ac9dbSNicholas Bellinger 
227a6d9bb1cSNicholas Bellinger 		sess = cmd->se_sess;
228a6d9bb1cSNicholas Bellinger 		if (WARN_ON_ONCE(!sess))
229a6d9bb1cSNicholas Bellinger 			continue;
230a6d9bb1cSNicholas Bellinger 
231a6d9bb1cSNicholas Bellinger 		spin_lock(&sess->sess_cmd_lock);
232d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
2337d680f3bSChristoph Hellwig 		if (!(cmd->transport_state & CMD_T_ACTIVE)) {
234d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
235a6d9bb1cSNicholas Bellinger 			spin_unlock(&sess->sess_cmd_lock);
236c66ac9dbSNicholas Bellinger 			continue;
237c66ac9dbSNicholas Bellinger 		}
238c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
239d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
240a6d9bb1cSNicholas Bellinger 			spin_unlock(&sess->sess_cmd_lock);
241c66ac9dbSNicholas Bellinger 			continue;
242c66ac9dbSNicholas Bellinger 		}
243a6d9bb1cSNicholas Bellinger 		cmd->transport_state |= CMD_T_ABORTED;
244d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
245d050ffb9SNicholas Bellinger 
246a6d9bb1cSNicholas Bellinger 		rc = kref_get_unless_zero(&cmd->cmd_kref);
247a6d9bb1cSNicholas Bellinger 		spin_unlock(&sess->sess_cmd_lock);
248a6d9bb1cSNicholas Bellinger 		if (!rc) {
249a6d9bb1cSNicholas Bellinger 			printk("LUN_RESET TMR: non-zero kref_get_unless_zero\n");
250a6d9bb1cSNicholas Bellinger 			continue;
251a6d9bb1cSNicholas Bellinger 		}
2526eb40b2aSJoern Engel 		list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
253d050ffb9SNicholas Bellinger 	}
254d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
255d050ffb9SNicholas Bellinger 
256abc1fd4fSJoern Engel 	list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
257b8a11d73SJoern Engel 		list_del_init(&tmr_p->tmr_list);
258abc1fd4fSJoern Engel 		cmd = tmr_p->task_cmd;
259d050ffb9SNicholas Bellinger 
2606708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
261c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
262abc1fd4fSJoern Engel 			(preempt_and_abort_list) ? "Preempt" : "", tmr_p,
263abc1fd4fSJoern Engel 			tmr_p->function, tmr_p->response, cmd->t_state);
264c66ac9dbSNicholas Bellinger 
265a6d9bb1cSNicholas Bellinger 		cancel_work_sync(&cmd->work);
266a6d9bb1cSNicholas Bellinger 		transport_wait_for_tasks(cmd);
267a6d9bb1cSNicholas Bellinger 
2688dc52b54SNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
269a6d9bb1cSNicholas Bellinger 		target_put_sess_cmd(cmd);
270c66ac9dbSNicholas Bellinger 	}
271d050ffb9SNicholas Bellinger }
272d050ffb9SNicholas Bellinger 
273cf572a96SChristoph Hellwig static void core_tmr_drain_state_list(
274d050ffb9SNicholas Bellinger 	struct se_device *dev,
275d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
276d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
277d050ffb9SNicholas Bellinger 	int tas,
278d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
279d050ffb9SNicholas Bellinger {
280d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
281febe562cSNicholas Bellinger 	struct se_session *sess;
282cf572a96SChristoph Hellwig 	struct se_cmd *cmd, *next;
283d050ffb9SNicholas Bellinger 	unsigned long flags;
284febe562cSNicholas Bellinger 	int rc;
285cf572a96SChristoph Hellwig 
286c66ac9dbSNicholas Bellinger 	/*
287cf572a96SChristoph Hellwig 	 * Complete outstanding commands with TASK_ABORTED SAM status.
288cf572a96SChristoph Hellwig 	 *
289c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
290c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
291c66ac9dbSNicholas Bellinger 	 *
292c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
293c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
294c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
295c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
296c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
297c66ac9dbSNicholas Bellinger 	 *
298c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
299c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
300c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
301c66ac9dbSNicholas Bellinger 	 *
302c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
303c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
304c66ac9dbSNicholas Bellinger 	 *
305c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
306c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
307c66ac9dbSNicholas Bellinger 	 */
308c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&dev->execute_task_lock, flags);
309cf572a96SChristoph Hellwig 	list_for_each_entry_safe(cmd, next, &dev->state_list, state_list) {
310c66ac9dbSNicholas Bellinger 		/*
311c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
312c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
313c66ac9dbSNicholas Bellinger 		 */
314feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
315c66ac9dbSNicholas Bellinger 			continue;
316cf572a96SChristoph Hellwig 
317c66ac9dbSNicholas Bellinger 		/*
318c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
319c66ac9dbSNicholas Bellinger 		 */
320c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
321c66ac9dbSNicholas Bellinger 			continue;
322c66ac9dbSNicholas Bellinger 
323febe562cSNicholas Bellinger 		sess = cmd->se_sess;
324febe562cSNicholas Bellinger 		if (WARN_ON_ONCE(!sess))
325febe562cSNicholas Bellinger 			continue;
326febe562cSNicholas Bellinger 
327febe562cSNicholas Bellinger 		spin_lock(&sess->sess_cmd_lock);
328febe562cSNicholas Bellinger 		rc = __target_check_io_state(cmd);
329febe562cSNicholas Bellinger 		spin_unlock(&sess->sess_cmd_lock);
330febe562cSNicholas Bellinger 		if (!rc)
331febe562cSNicholas Bellinger 			continue;
332febe562cSNicholas Bellinger 
333cf572a96SChristoph Hellwig 		list_move_tail(&cmd->state_list, &drain_task_list);
334cf572a96SChristoph Hellwig 		cmd->state_active = false;
335d050ffb9SNicholas Bellinger 	}
336c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
337c66ac9dbSNicholas Bellinger 
338d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
339cf572a96SChristoph Hellwig 		cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
340febe562cSNicholas Bellinger 		list_del_init(&cmd->state_list);
341d050ffb9SNicholas Bellinger 
342cf572a96SChristoph Hellwig 		pr_debug("LUN_RESET: %s cmd: %p"
343649ee054SBart Van Assche 			" ITT/CmdSN: 0x%08llx/0x%08x, i_state: %d, t_state: %d"
344f2da9dbdSChristoph Hellwig 			"cdb: 0x%02x\n",
345cf572a96SChristoph Hellwig 			(preempt_and_abort_list) ? "Preempt" : "", cmd,
346649ee054SBart Van Assche 			cmd->tag, 0,
347e3d6f909SAndy Grover 			cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
348f2da9dbdSChristoph Hellwig 			cmd->t_task_cdb[0]);
349649ee054SBart Van Assche 		pr_debug("LUN_RESET: ITT[0x%08llx] - pr_res_key: 0x%016Lx"
350785fdf70SChristoph Hellwig 			" -- CMD_T_ACTIVE: %d"
3517d680f3bSChristoph Hellwig 			" CMD_T_STOP: %d CMD_T_SENT: %d\n",
352649ee054SBart Van Assche 			cmd->tag, cmd->pr_res_key,
3537d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_ACTIVE) != 0,
3547d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_STOP) != 0,
3557d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_SENT) != 0);
356c66ac9dbSNicholas Bellinger 
35735e0e757SChristoph Hellwig 		/*
35835e0e757SChristoph Hellwig 		 * If the command may be queued onto a workqueue cancel it now.
35935e0e757SChristoph Hellwig 		 *
36035e0e757SChristoph Hellwig 		 * This is equivalent to removal from the execute queue in the
36135e0e757SChristoph Hellwig 		 * loop above, but we do it down here given that
36235e0e757SChristoph Hellwig 		 * cancel_work_sync may block.
36335e0e757SChristoph Hellwig 		 */
36435e0e757SChristoph Hellwig 		cancel_work_sync(&cmd->work);
365febe562cSNicholas Bellinger 		transport_wait_for_tasks(cmd);
366c66ac9dbSNicholas Bellinger 
367c1c35d52SNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas);
368febe562cSNicholas Bellinger 		target_put_sess_cmd(cmd);
369c66ac9dbSNicholas Bellinger 	}
370d050ffb9SNicholas Bellinger }
371d050ffb9SNicholas Bellinger 
372d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
373d050ffb9SNicholas Bellinger         struct se_device *dev,
374d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
375d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
376d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
377d050ffb9SNicholas Bellinger {
378d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
379d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
380d050ffb9SNicholas Bellinger 	int tas;
381d050ffb9SNicholas Bellinger         /*
382d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
383d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
384d050ffb9SNicholas Bellinger 	 *
385d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
386d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
387d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
388d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
389d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
390d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
391d050ffb9SNicholas Bellinger 	 */
3920fd97ccfSChristoph Hellwig 	tas = dev->dev_attrib.emulate_tas;
393d050ffb9SNicholas Bellinger 	/*
394d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
395d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
396d050ffb9SNicholas Bellinger 	 */
397d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
398d050ffb9SNicholas Bellinger 		tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
399d050ffb9SNicholas Bellinger 		tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
400d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
401d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
402d050ffb9SNicholas Bellinger 				" initiator port %s\n",
403d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
404d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
405d050ffb9SNicholas Bellinger 		}
406d050ffb9SNicholas Bellinger 	}
407d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
408d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
409d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
410d050ffb9SNicholas Bellinger 
411d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
412cf572a96SChristoph Hellwig 	core_tmr_drain_state_list(dev, prout_cmd, tmr_nacl, tas,
413d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
414af877292SChristoph Hellwig 
415c66ac9dbSNicholas Bellinger 	/*
416c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
417c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
418c66ac9dbSNicholas Bellinger 	 */
4196708bb27SAndy Grover 	if (!preempt_and_abort_list &&
4200fd97ccfSChristoph Hellwig 	     (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) {
421c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
422c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
4230fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
424c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
4256708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
426c66ac9dbSNicholas Bellinger 	}
427c66ac9dbSNicholas Bellinger 
428ee480683SNicholas Bellinger 	atomic_long_inc(&dev->num_resets);
429c66ac9dbSNicholas Bellinger 
4306708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
431c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
432e3d6f909SAndy Grover 			dev->transport->name);
433c66ac9dbSNicholas Bellinger 	return 0;
434c66ac9dbSNicholas Bellinger }
435d050ffb9SNicholas Bellinger 
436