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 
780f4a9431SNicholas Bellinger static void core_tmr_handle_tas_abort(struct se_cmd *cmd, int tas)
79c66ac9dbSNicholas Bellinger {
800f4a9431SNicholas Bellinger 	unsigned long flags;
810f4a9431SNicholas Bellinger 	bool remove = true, send_tas;
82c66ac9dbSNicholas Bellinger 	/*
83c66ac9dbSNicholas Bellinger 	 * TASK ABORTED status (TAS) bit support
84c66ac9dbSNicholas Bellinger 	 */
850f4a9431SNicholas Bellinger 	spin_lock_irqsave(&cmd->t_state_lock, flags);
860f4a9431SNicholas Bellinger 	send_tas = (cmd->transport_state & CMD_T_TAS);
870f4a9431SNicholas Bellinger 	spin_unlock_irqrestore(&cmd->t_state_lock, flags);
880f4a9431SNicholas Bellinger 
890f4a9431SNicholas Bellinger 	if (send_tas) {
9068259b5aSAlex Leung 		remove = false;
91c66ac9dbSNicholas Bellinger 		transport_send_task_abort(cmd);
9268259b5aSAlex Leung 	}
93c66ac9dbSNicholas Bellinger 
9468259b5aSAlex Leung 	transport_cmd_finish_abort(cmd, remove);
95c66ac9dbSNicholas Bellinger }
96c66ac9dbSNicholas Bellinger 
97feae8564SJörn Engel static int target_check_cdb_and_preempt(struct list_head *list,
98c165f69cSJörn Engel 		struct se_cmd *cmd)
99c165f69cSJörn Engel {
100feae8564SJörn Engel 	struct t10_pr_registration *reg;
101c165f69cSJörn Engel 
102feae8564SJörn Engel 	if (!list)
103feae8564SJörn Engel 		return 0;
104feae8564SJörn Engel 	list_for_each_entry(reg, list, pr_reg_abort_list) {
105feae8564SJörn Engel 		if (reg->pr_res_key == cmd->pr_res_key)
106c165f69cSJörn Engel 			return 0;
107c165f69cSJörn Engel 	}
108c165f69cSJörn Engel 
109c165f69cSJörn Engel 	return 1;
110c165f69cSJörn Engel }
111c165f69cSJörn Engel 
1120f4a9431SNicholas Bellinger static bool __target_check_io_state(struct se_cmd *se_cmd,
1130f4a9431SNicholas Bellinger 				    struct se_session *tmr_sess, int tas)
114febe562cSNicholas Bellinger {
115febe562cSNicholas Bellinger 	struct se_session *sess = se_cmd->se_sess;
116febe562cSNicholas Bellinger 
117febe562cSNicholas Bellinger 	assert_spin_locked(&sess->sess_cmd_lock);
118febe562cSNicholas Bellinger 	WARN_ON_ONCE(!irqs_disabled());
119febe562cSNicholas Bellinger 	/*
120febe562cSNicholas Bellinger 	 * If command already reached CMD_T_COMPLETE state within
1210f4a9431SNicholas Bellinger 	 * target_complete_cmd() or CMD_T_FABRIC_STOP due to shutdown,
1220f4a9431SNicholas Bellinger 	 * this se_cmd has been passed to fabric driver and will
1230f4a9431SNicholas Bellinger 	 * not be aborted.
124febe562cSNicholas Bellinger 	 *
125febe562cSNicholas Bellinger 	 * Otherwise, obtain a local se_cmd->cmd_kref now for TMR
126febe562cSNicholas Bellinger 	 * ABORT_TASK + LUN_RESET for CMD_T_ABORTED processing as
127febe562cSNicholas Bellinger 	 * long as se_cmd->cmd_kref is still active unless zero.
128febe562cSNicholas Bellinger 	 */
129febe562cSNicholas Bellinger 	spin_lock(&se_cmd->t_state_lock);
1300f4a9431SNicholas Bellinger 	if (se_cmd->transport_state & (CMD_T_COMPLETE | CMD_T_FABRIC_STOP)) {
1310f4a9431SNicholas Bellinger 		pr_debug("Attempted to abort io tag: %llu already complete or"
1320f4a9431SNicholas Bellinger 			" fabric stop, skipping\n", se_cmd->tag);
1330f4a9431SNicholas Bellinger 		spin_unlock(&se_cmd->t_state_lock);
1340f4a9431SNicholas Bellinger 		return false;
1350f4a9431SNicholas Bellinger 	}
1360f4a9431SNicholas Bellinger 	if (sess->sess_tearing_down || se_cmd->cmd_wait_set) {
1370f4a9431SNicholas Bellinger 		pr_debug("Attempted to abort io tag: %llu already shutdown,"
138febe562cSNicholas Bellinger 			" skipping\n", se_cmd->tag);
139febe562cSNicholas Bellinger 		spin_unlock(&se_cmd->t_state_lock);
140febe562cSNicholas Bellinger 		return false;
141febe562cSNicholas Bellinger 	}
142febe562cSNicholas Bellinger 	se_cmd->transport_state |= CMD_T_ABORTED;
1430f4a9431SNicholas Bellinger 
1440f4a9431SNicholas Bellinger 	if ((tmr_sess != se_cmd->se_sess) && tas)
1450f4a9431SNicholas Bellinger 		se_cmd->transport_state |= CMD_T_TAS;
1460f4a9431SNicholas Bellinger 
147febe562cSNicholas Bellinger 	spin_unlock(&se_cmd->t_state_lock);
148febe562cSNicholas Bellinger 
149febe562cSNicholas Bellinger 	return kref_get_unless_zero(&se_cmd->cmd_kref);
150febe562cSNicholas Bellinger }
151febe562cSNicholas Bellinger 
1523d28934aSNicholas Bellinger void core_tmr_abort_task(
1533d28934aSNicholas Bellinger 	struct se_device *dev,
1543d28934aSNicholas Bellinger 	struct se_tmr_req *tmr,
1553d28934aSNicholas Bellinger 	struct se_session *se_sess)
1563d28934aSNicholas Bellinger {
157f81ccb48SJoern Engel 	struct se_cmd *se_cmd;
1583d28934aSNicholas Bellinger 	unsigned long flags;
159649ee054SBart Van Assche 	u64 ref_tag;
1603d28934aSNicholas Bellinger 
1613d28934aSNicholas Bellinger 	spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
162f81ccb48SJoern Engel 	list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1633d28934aSNicholas Bellinger 
1643d28934aSNicholas Bellinger 		if (dev != se_cmd->se_dev)
1653d28934aSNicholas Bellinger 			continue;
16647b1584cSAlex Leung 
167dc0fafdaSBart Van Assche 		/* skip task management functions, including tmr->task_cmd */
168dc0fafdaSBart Van Assche 		if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
16947b1584cSAlex Leung 			continue;
17047b1584cSAlex Leung 
171649ee054SBart Van Assche 		ref_tag = se_cmd->tag;
1723d28934aSNicholas Bellinger 		if (tmr->ref_task_tag != ref_tag)
1733d28934aSNicholas Bellinger 			continue;
1743d28934aSNicholas Bellinger 
175649ee054SBart Van Assche 		printk("ABORT_TASK: Found referenced %s task_tag: %llu\n",
1763d28934aSNicholas Bellinger 			se_cmd->se_tfo->get_fabric_name(), ref_tag);
1773d28934aSNicholas Bellinger 
1780f4a9431SNicholas Bellinger 		if (!__target_check_io_state(se_cmd, se_sess, 0)) {
1793d28934aSNicholas Bellinger 			spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1809ff9d15eSBart Van Assche 			target_put_sess_cmd(se_cmd);
1813d28934aSNicholas Bellinger 			goto out;
1823d28934aSNicholas Bellinger 		}
1833d28934aSNicholas Bellinger 		list_del_init(&se_cmd->se_cmd_list);
1843d28934aSNicholas Bellinger 		spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1853d28934aSNicholas Bellinger 
1863d28934aSNicholas Bellinger 		cancel_work_sync(&se_cmd->work);
1873d28934aSNicholas Bellinger 		transport_wait_for_tasks(se_cmd);
1883d28934aSNicholas Bellinger 
18968259b5aSAlex Leung 		transport_cmd_finish_abort(se_cmd, true);
190febe562cSNicholas Bellinger 		target_put_sess_cmd(se_cmd);
1913d28934aSNicholas Bellinger 
1923d28934aSNicholas Bellinger 		printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for"
193649ee054SBart Van Assche 				" ref_tag: %llu\n", ref_tag);
1943d28934aSNicholas Bellinger 		tmr->response = TMR_FUNCTION_COMPLETE;
1953d28934aSNicholas Bellinger 		return;
1963d28934aSNicholas Bellinger 	}
1973d28934aSNicholas Bellinger 	spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1983d28934aSNicholas Bellinger 
1993d28934aSNicholas Bellinger out:
200649ee054SBart Van Assche 	printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %lld\n",
2013d28934aSNicholas Bellinger 			tmr->ref_task_tag);
2023d28934aSNicholas Bellinger 	tmr->response = TMR_TASK_DOES_NOT_EXIST;
2033d28934aSNicholas Bellinger }
2043d28934aSNicholas Bellinger 
205d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
206c66ac9dbSNicholas Bellinger 	struct se_device *dev,
207c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
208d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
209c66ac9dbSNicholas Bellinger {
210d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
211a6d9bb1cSNicholas Bellinger 	struct se_session *sess;
212c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
213d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
214c66ac9dbSNicholas Bellinger 	unsigned long flags;
215a6d9bb1cSNicholas Bellinger 	bool rc;
216c66ac9dbSNicholas Bellinger 	/*
217c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
218c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
219c66ac9dbSNicholas Bellinger 	 */
220d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
221c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
222c66ac9dbSNicholas Bellinger 		/*
223c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
224c66ac9dbSNicholas Bellinger 		 */
225abc1fd4fSJoern Engel 		if (tmr_p == tmr)
226c66ac9dbSNicholas Bellinger 			continue;
227c66ac9dbSNicholas Bellinger 
228c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
2296708bb27SAndy Grover 		if (!cmd) {
2306708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
231c66ac9dbSNicholas Bellinger 			continue;
232c66ac9dbSNicholas Bellinger 		}
233c66ac9dbSNicholas Bellinger 		/*
234c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
235c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
236ac75d8beSBart Van Assche 		 * skip non registration key matching TMRs.
237c66ac9dbSNicholas Bellinger 		 */
238feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
239c66ac9dbSNicholas Bellinger 			continue;
240c66ac9dbSNicholas Bellinger 
241a6d9bb1cSNicholas Bellinger 		sess = cmd->se_sess;
242a6d9bb1cSNicholas Bellinger 		if (WARN_ON_ONCE(!sess))
243a6d9bb1cSNicholas Bellinger 			continue;
244a6d9bb1cSNicholas Bellinger 
245a6d9bb1cSNicholas Bellinger 		spin_lock(&sess->sess_cmd_lock);
246d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
2470f4a9431SNicholas Bellinger 		if (!(cmd->transport_state & CMD_T_ACTIVE) ||
2480f4a9431SNicholas Bellinger 		     (cmd->transport_state & CMD_T_FABRIC_STOP)) {
249d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
250a6d9bb1cSNicholas Bellinger 			spin_unlock(&sess->sess_cmd_lock);
251c66ac9dbSNicholas Bellinger 			continue;
252c66ac9dbSNicholas Bellinger 		}
253c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
254d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
255a6d9bb1cSNicholas Bellinger 			spin_unlock(&sess->sess_cmd_lock);
256c66ac9dbSNicholas Bellinger 			continue;
257c66ac9dbSNicholas Bellinger 		}
2580f4a9431SNicholas Bellinger 		if (sess->sess_tearing_down || cmd->cmd_wait_set) {
2590f4a9431SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
2600f4a9431SNicholas Bellinger 			spin_unlock(&sess->sess_cmd_lock);
2610f4a9431SNicholas Bellinger 			continue;
2620f4a9431SNicholas Bellinger 		}
263a6d9bb1cSNicholas Bellinger 		cmd->transport_state |= CMD_T_ABORTED;
264d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
265d050ffb9SNicholas Bellinger 
266a6d9bb1cSNicholas Bellinger 		rc = kref_get_unless_zero(&cmd->cmd_kref);
267a6d9bb1cSNicholas Bellinger 		if (!rc) {
268a6d9bb1cSNicholas Bellinger 			printk("LUN_RESET TMR: non-zero kref_get_unless_zero\n");
2690f4a9431SNicholas Bellinger 			spin_unlock(&sess->sess_cmd_lock);
270a6d9bb1cSNicholas Bellinger 			continue;
271a6d9bb1cSNicholas Bellinger 		}
2720f4a9431SNicholas Bellinger 		spin_unlock(&sess->sess_cmd_lock);
2730f4a9431SNicholas Bellinger 
2746eb40b2aSJoern Engel 		list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
275d050ffb9SNicholas Bellinger 	}
276d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
277d050ffb9SNicholas Bellinger 
278abc1fd4fSJoern Engel 	list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
279b8a11d73SJoern Engel 		list_del_init(&tmr_p->tmr_list);
280abc1fd4fSJoern Engel 		cmd = tmr_p->task_cmd;
281d050ffb9SNicholas Bellinger 
2826708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
283c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
284abc1fd4fSJoern Engel 			(preempt_and_abort_list) ? "Preempt" : "", tmr_p,
285abc1fd4fSJoern Engel 			tmr_p->function, tmr_p->response, cmd->t_state);
286c66ac9dbSNicholas Bellinger 
287a6d9bb1cSNicholas Bellinger 		cancel_work_sync(&cmd->work);
288a6d9bb1cSNicholas Bellinger 		transport_wait_for_tasks(cmd);
289a6d9bb1cSNicholas Bellinger 
2908dc52b54SNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
291a6d9bb1cSNicholas Bellinger 		target_put_sess_cmd(cmd);
292c66ac9dbSNicholas Bellinger 	}
293d050ffb9SNicholas Bellinger }
294d050ffb9SNicholas Bellinger 
295cf572a96SChristoph Hellwig static void core_tmr_drain_state_list(
296d050ffb9SNicholas Bellinger 	struct se_device *dev,
297d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
298ebde1ca5SNicholas Bellinger 	struct se_session *tmr_sess,
299d050ffb9SNicholas Bellinger 	int tas,
300d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
301d050ffb9SNicholas Bellinger {
302d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
303febe562cSNicholas Bellinger 	struct se_session *sess;
304cf572a96SChristoph Hellwig 	struct se_cmd *cmd, *next;
305d050ffb9SNicholas Bellinger 	unsigned long flags;
306febe562cSNicholas Bellinger 	int rc;
307cf572a96SChristoph Hellwig 
308c66ac9dbSNicholas Bellinger 	/*
309cf572a96SChristoph Hellwig 	 * Complete outstanding commands with TASK_ABORTED SAM status.
310cf572a96SChristoph Hellwig 	 *
311c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
312c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
313c66ac9dbSNicholas Bellinger 	 *
314c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
315c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
316c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
317c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
318c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
319c66ac9dbSNicholas Bellinger 	 *
320c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
321c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
322c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
323c66ac9dbSNicholas Bellinger 	 *
324c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
325c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
326c66ac9dbSNicholas Bellinger 	 *
327c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
328c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
329c66ac9dbSNicholas Bellinger 	 */
330c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&dev->execute_task_lock, flags);
331cf572a96SChristoph Hellwig 	list_for_each_entry_safe(cmd, next, &dev->state_list, state_list) {
332c66ac9dbSNicholas Bellinger 		/*
333c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
334c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
335c66ac9dbSNicholas Bellinger 		 */
336feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
337c66ac9dbSNicholas Bellinger 			continue;
338cf572a96SChristoph Hellwig 
339c66ac9dbSNicholas Bellinger 		/*
340c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
341c66ac9dbSNicholas Bellinger 		 */
342c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
343c66ac9dbSNicholas Bellinger 			continue;
344c66ac9dbSNicholas Bellinger 
345febe562cSNicholas Bellinger 		sess = cmd->se_sess;
346febe562cSNicholas Bellinger 		if (WARN_ON_ONCE(!sess))
347febe562cSNicholas Bellinger 			continue;
348febe562cSNicholas Bellinger 
349febe562cSNicholas Bellinger 		spin_lock(&sess->sess_cmd_lock);
3500f4a9431SNicholas Bellinger 		rc = __target_check_io_state(cmd, tmr_sess, tas);
351febe562cSNicholas Bellinger 		spin_unlock(&sess->sess_cmd_lock);
352febe562cSNicholas Bellinger 		if (!rc)
353febe562cSNicholas Bellinger 			continue;
354febe562cSNicholas Bellinger 
355cf572a96SChristoph Hellwig 		list_move_tail(&cmd->state_list, &drain_task_list);
356cf572a96SChristoph Hellwig 		cmd->state_active = false;
357d050ffb9SNicholas Bellinger 	}
358c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
359c66ac9dbSNicholas Bellinger 
360d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
361cf572a96SChristoph Hellwig 		cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
362febe562cSNicholas Bellinger 		list_del_init(&cmd->state_list);
363d050ffb9SNicholas Bellinger 
364cf572a96SChristoph Hellwig 		pr_debug("LUN_RESET: %s cmd: %p"
365649ee054SBart Van Assche 			" ITT/CmdSN: 0x%08llx/0x%08x, i_state: %d, t_state: %d"
366f2da9dbdSChristoph Hellwig 			"cdb: 0x%02x\n",
367cf572a96SChristoph Hellwig 			(preempt_and_abort_list) ? "Preempt" : "", cmd,
368649ee054SBart Van Assche 			cmd->tag, 0,
369e3d6f909SAndy Grover 			cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
370f2da9dbdSChristoph Hellwig 			cmd->t_task_cdb[0]);
371649ee054SBart Van Assche 		pr_debug("LUN_RESET: ITT[0x%08llx] - pr_res_key: 0x%016Lx"
372785fdf70SChristoph Hellwig 			" -- CMD_T_ACTIVE: %d"
3737d680f3bSChristoph Hellwig 			" CMD_T_STOP: %d CMD_T_SENT: %d\n",
374649ee054SBart Van Assche 			cmd->tag, cmd->pr_res_key,
3757d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_ACTIVE) != 0,
3767d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_STOP) != 0,
3777d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_SENT) != 0);
378c66ac9dbSNicholas Bellinger 
37935e0e757SChristoph Hellwig 		/*
38035e0e757SChristoph Hellwig 		 * If the command may be queued onto a workqueue cancel it now.
38135e0e757SChristoph Hellwig 		 *
38235e0e757SChristoph Hellwig 		 * This is equivalent to removal from the execute queue in the
38335e0e757SChristoph Hellwig 		 * loop above, but we do it down here given that
38435e0e757SChristoph Hellwig 		 * cancel_work_sync may block.
38535e0e757SChristoph Hellwig 		 */
38635e0e757SChristoph Hellwig 		cancel_work_sync(&cmd->work);
387febe562cSNicholas Bellinger 		transport_wait_for_tasks(cmd);
388c66ac9dbSNicholas Bellinger 
3890f4a9431SNicholas Bellinger 		core_tmr_handle_tas_abort(cmd, tas);
390febe562cSNicholas Bellinger 		target_put_sess_cmd(cmd);
391c66ac9dbSNicholas Bellinger 	}
392d050ffb9SNicholas Bellinger }
393d050ffb9SNicholas Bellinger 
394d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
395d050ffb9SNicholas Bellinger         struct se_device *dev,
396d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
397d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
398d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
399d050ffb9SNicholas Bellinger {
400d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
401d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
402ebde1ca5SNicholas Bellinger 	struct se_session *tmr_sess = NULL;
403d050ffb9SNicholas Bellinger 	int tas;
404d050ffb9SNicholas Bellinger         /*
405d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
406d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
407d050ffb9SNicholas Bellinger 	 *
408d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
409d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
410d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
411d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
412d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
413d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
414d050ffb9SNicholas Bellinger 	 */
4150fd97ccfSChristoph Hellwig 	tas = dev->dev_attrib.emulate_tas;
416d050ffb9SNicholas Bellinger 	/*
417d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
418d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
419d050ffb9SNicholas Bellinger 	 */
420d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
421ebde1ca5SNicholas Bellinger 		tmr_sess = tmr->task_cmd->se_sess;
422ebde1ca5SNicholas Bellinger 		tmr_nacl = tmr_sess->se_node_acl;
423ebde1ca5SNicholas Bellinger 		tmr_tpg = tmr_sess->se_tpg;
424d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
425d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
426d050ffb9SNicholas Bellinger 				" initiator port %s\n",
427d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
428d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
429d050ffb9SNicholas Bellinger 		}
430d050ffb9SNicholas Bellinger 	}
431d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
432d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
433d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
434d050ffb9SNicholas Bellinger 
435d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
436ebde1ca5SNicholas Bellinger 	core_tmr_drain_state_list(dev, prout_cmd, tmr_sess, tas,
437d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
438af877292SChristoph Hellwig 
439c66ac9dbSNicholas Bellinger 	/*
440c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
441c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
442c66ac9dbSNicholas Bellinger 	 */
4436708bb27SAndy Grover 	if (!preempt_and_abort_list &&
4440fd97ccfSChristoph Hellwig 	     (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) {
445c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
446c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
4470fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
448c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
4496708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
450c66ac9dbSNicholas Bellinger 	}
451c66ac9dbSNicholas Bellinger 
452ee480683SNicholas Bellinger 	atomic_long_inc(&dev->num_resets);
453c66ac9dbSNicholas Bellinger 
4546708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
455c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
456e3d6f909SAndy Grover 			dev->transport->name);
457c66ac9dbSNicholas Bellinger 	return 0;
458c66ac9dbSNicholas Bellinger }
459d050ffb9SNicholas Bellinger 
460