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 #include <scsi/scsi.h>
31c66ac9dbSNicholas Bellinger #include <scsi/scsi_cmnd.h>
32c66ac9dbSNicholas Bellinger 
33c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
34c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
35c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
36c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h>
37c66ac9dbSNicholas Bellinger 
38e26d99aeSChristoph Hellwig #include "target_core_internal.h"
39c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
40c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
41c66ac9dbSNicholas Bellinger 
42c8e31f26SAndy Grover int core_tmr_alloc_req(
43c66ac9dbSNicholas Bellinger 	struct se_cmd *se_cmd,
44c66ac9dbSNicholas Bellinger 	void *fabric_tmr_ptr,
45dd503a5fSRoland Dreier 	u8 function,
46dd503a5fSRoland Dreier 	gfp_t gfp_flags)
47c66ac9dbSNicholas Bellinger {
48c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr;
49c66ac9dbSNicholas Bellinger 
50c8e31f26SAndy Grover 	tmr = kzalloc(sizeof(struct se_tmr_req), gfp_flags);
516708bb27SAndy Grover 	if (!tmr) {
526708bb27SAndy Grover 		pr_err("Unable to allocate struct se_tmr_req\n");
53c8e31f26SAndy Grover 		return -ENOMEM;
54c66ac9dbSNicholas Bellinger 	}
55c8e31f26SAndy Grover 
56c8e31f26SAndy Grover 	se_cmd->se_cmd_flags |= SCF_SCSI_TMR_CDB;
57c8e31f26SAndy Grover 	se_cmd->se_tmr_req = tmr;
58c66ac9dbSNicholas Bellinger 	tmr->task_cmd = se_cmd;
59c66ac9dbSNicholas Bellinger 	tmr->fabric_tmr_ptr = fabric_tmr_ptr;
60c66ac9dbSNicholas Bellinger 	tmr->function = function;
61c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tmr->tmr_list);
62c66ac9dbSNicholas Bellinger 
63c8e31f26SAndy Grover 	return 0;
64c66ac9dbSNicholas Bellinger }
65c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tmr_alloc_req);
66c66ac9dbSNicholas Bellinger 
678f832690SJoern Engel void core_tmr_release_req(struct se_tmr_req *tmr)
68c66ac9dbSNicholas Bellinger {
69c66ac9dbSNicholas Bellinger 	struct se_device *dev = tmr->tmr_dev;
70d050ffb9SNicholas Bellinger 	unsigned long flags;
71c66ac9dbSNicholas Bellinger 
728f832690SJoern Engel 	if (dev) {
73d050ffb9SNicholas Bellinger 		spin_lock_irqsave(&dev->se_tmr_lock, flags);
74c66ac9dbSNicholas Bellinger 		list_del(&tmr->tmr_list);
75d050ffb9SNicholas Bellinger 		spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
768f832690SJoern Engel 	}
777fd29aa9SNicholas Bellinger 
78c8e31f26SAndy Grover 	kfree(tmr);
79c66ac9dbSNicholas Bellinger }
80c66ac9dbSNicholas Bellinger 
81c66ac9dbSNicholas Bellinger static void core_tmr_handle_tas_abort(
82c66ac9dbSNicholas Bellinger 	struct se_node_acl *tmr_nacl,
83c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
84c1c35d52SNicholas Bellinger 	int tas)
85c66ac9dbSNicholas Bellinger {
8668259b5aSAlex Leung 	bool remove = true;
87c66ac9dbSNicholas Bellinger 	/*
88c66ac9dbSNicholas Bellinger 	 * TASK ABORTED status (TAS) bit support
89c66ac9dbSNicholas Bellinger 	 */
908f832690SJoern Engel 	if ((tmr_nacl && (tmr_nacl != cmd->se_sess->se_node_acl)) && tas) {
9168259b5aSAlex Leung 		remove = false;
92c66ac9dbSNicholas Bellinger 		transport_send_task_abort(cmd);
9368259b5aSAlex Leung 	}
94c66ac9dbSNicholas Bellinger 
9568259b5aSAlex Leung 	transport_cmd_finish_abort(cmd, remove);
96c66ac9dbSNicholas Bellinger }
97c66ac9dbSNicholas Bellinger 
98feae8564SJörn Engel static int target_check_cdb_and_preempt(struct list_head *list,
99c165f69cSJörn Engel 		struct se_cmd *cmd)
100c165f69cSJörn Engel {
101feae8564SJörn Engel 	struct t10_pr_registration *reg;
102c165f69cSJörn Engel 
103feae8564SJörn Engel 	if (!list)
104feae8564SJörn Engel 		return 0;
105feae8564SJörn Engel 	list_for_each_entry(reg, list, pr_reg_abort_list) {
106feae8564SJörn Engel 		if (reg->pr_res_key == cmd->pr_res_key)
107c165f69cSJörn Engel 			return 0;
108c165f69cSJörn Engel 	}
109c165f69cSJörn Engel 
110c165f69cSJörn Engel 	return 1;
111c165f69cSJörn Engel }
112c165f69cSJörn Engel 
1133d28934aSNicholas Bellinger void core_tmr_abort_task(
1143d28934aSNicholas Bellinger 	struct se_device *dev,
1153d28934aSNicholas Bellinger 	struct se_tmr_req *tmr,
1163d28934aSNicholas Bellinger 	struct se_session *se_sess)
1173d28934aSNicholas Bellinger {
118f81ccb48SJoern Engel 	struct se_cmd *se_cmd;
1193d28934aSNicholas Bellinger 	unsigned long flags;
1203d28934aSNicholas Bellinger 	int ref_tag;
1213d28934aSNicholas Bellinger 
1223d28934aSNicholas Bellinger 	spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
123f81ccb48SJoern Engel 	list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
1243d28934aSNicholas Bellinger 
1253d28934aSNicholas Bellinger 		if (dev != se_cmd->se_dev)
1263d28934aSNicholas Bellinger 			continue;
12747b1584cSAlex Leung 
128dc0fafdaSBart Van Assche 		/* skip task management functions, including tmr->task_cmd */
129dc0fafdaSBart Van Assche 		if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
13047b1584cSAlex Leung 			continue;
13147b1584cSAlex Leung 
1323d28934aSNicholas Bellinger 		ref_tag = se_cmd->se_tfo->get_task_tag(se_cmd);
1333d28934aSNicholas Bellinger 		if (tmr->ref_task_tag != ref_tag)
1343d28934aSNicholas Bellinger 			continue;
1353d28934aSNicholas Bellinger 
1363d28934aSNicholas Bellinger 		printk("ABORT_TASK: Found referenced %s task_tag: %u\n",
1373d28934aSNicholas Bellinger 			se_cmd->se_tfo->get_fabric_name(), ref_tag);
1383d28934aSNicholas Bellinger 
139ab74b3d6SSteve Hodgson 		spin_lock(&se_cmd->t_state_lock);
1403d28934aSNicholas Bellinger 		if (se_cmd->transport_state & CMD_T_COMPLETE) {
1413d28934aSNicholas Bellinger 			printk("ABORT_TASK: ref_tag: %u already complete, skipping\n", ref_tag);
142ab74b3d6SSteve Hodgson 			spin_unlock(&se_cmd->t_state_lock);
1433d28934aSNicholas Bellinger 			spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1443d28934aSNicholas Bellinger 			goto out;
1453d28934aSNicholas Bellinger 		}
1463d28934aSNicholas Bellinger 		se_cmd->transport_state |= CMD_T_ABORTED;
147ab74b3d6SSteve Hodgson 		spin_unlock(&se_cmd->t_state_lock);
1483d28934aSNicholas Bellinger 
1493d28934aSNicholas Bellinger 		list_del_init(&se_cmd->se_cmd_list);
1503d28934aSNicholas Bellinger 		kref_get(&se_cmd->cmd_kref);
1513d28934aSNicholas Bellinger 		spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1523d28934aSNicholas Bellinger 
1533d28934aSNicholas Bellinger 		cancel_work_sync(&se_cmd->work);
1543d28934aSNicholas Bellinger 		transport_wait_for_tasks(se_cmd);
1553d28934aSNicholas Bellinger 
1563d28934aSNicholas Bellinger 		target_put_sess_cmd(se_sess, se_cmd);
15768259b5aSAlex Leung 		transport_cmd_finish_abort(se_cmd, true);
1583d28934aSNicholas Bellinger 
1593d28934aSNicholas Bellinger 		printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for"
1603d28934aSNicholas Bellinger 				" ref_tag: %d\n", ref_tag);
1613d28934aSNicholas Bellinger 		tmr->response = TMR_FUNCTION_COMPLETE;
1623d28934aSNicholas Bellinger 		return;
1633d28934aSNicholas Bellinger 	}
1643d28934aSNicholas Bellinger 	spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1653d28934aSNicholas Bellinger 
1663d28934aSNicholas Bellinger out:
1673d28934aSNicholas Bellinger 	printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %d\n",
1683d28934aSNicholas Bellinger 			tmr->ref_task_tag);
1693d28934aSNicholas Bellinger 	tmr->response = TMR_TASK_DOES_NOT_EXIST;
1703d28934aSNicholas Bellinger }
1713d28934aSNicholas Bellinger 
172d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
173c66ac9dbSNicholas Bellinger 	struct se_device *dev,
174c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
175d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
176c66ac9dbSNicholas Bellinger {
177d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
178c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
179d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
180c66ac9dbSNicholas Bellinger 	unsigned long flags;
181c66ac9dbSNicholas Bellinger 	/*
182c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
183c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
184c66ac9dbSNicholas Bellinger 	 */
185d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
186c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
187c66ac9dbSNicholas Bellinger 		/*
188c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
189c66ac9dbSNicholas Bellinger 		 */
190abc1fd4fSJoern Engel 		if (tmr_p == tmr)
191c66ac9dbSNicholas Bellinger 			continue;
192c66ac9dbSNicholas Bellinger 
193c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
1946708bb27SAndy Grover 		if (!cmd) {
1956708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
196c66ac9dbSNicholas Bellinger 			continue;
197c66ac9dbSNicholas Bellinger 		}
198c66ac9dbSNicholas Bellinger 		/*
199c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
200c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
201c66ac9dbSNicholas Bellinger 		 * skip non regisration key matching TMRs.
202c66ac9dbSNicholas Bellinger 		 */
203feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
204c66ac9dbSNicholas Bellinger 			continue;
205c66ac9dbSNicholas Bellinger 
206d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
2077d680f3bSChristoph Hellwig 		if (!(cmd->transport_state & CMD_T_ACTIVE)) {
208d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
209c66ac9dbSNicholas Bellinger 			continue;
210c66ac9dbSNicholas Bellinger 		}
211c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
212d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
213c66ac9dbSNicholas Bellinger 			continue;
214c66ac9dbSNicholas Bellinger 		}
215d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
216d050ffb9SNicholas Bellinger 
2176eb40b2aSJoern Engel 		list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
218d050ffb9SNicholas Bellinger 	}
219d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
220d050ffb9SNicholas Bellinger 
221abc1fd4fSJoern Engel 	list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
222b8a11d73SJoern Engel 		list_del_init(&tmr_p->tmr_list);
223abc1fd4fSJoern Engel 		cmd = tmr_p->task_cmd;
224d050ffb9SNicholas Bellinger 
2256708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
226c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
227abc1fd4fSJoern Engel 			(preempt_and_abort_list) ? "Preempt" : "", tmr_p,
228abc1fd4fSJoern Engel 			tmr_p->function, tmr_p->response, cmd->t_state);
229c66ac9dbSNicholas Bellinger 
2308dc52b54SNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
231c66ac9dbSNicholas Bellinger 	}
232d050ffb9SNicholas Bellinger }
233d050ffb9SNicholas Bellinger 
234cf572a96SChristoph Hellwig static void core_tmr_drain_state_list(
235d050ffb9SNicholas Bellinger 	struct se_device *dev,
236d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
237d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
238d050ffb9SNicholas Bellinger 	int tas,
239d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
240d050ffb9SNicholas Bellinger {
241d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
242cf572a96SChristoph Hellwig 	struct se_cmd *cmd, *next;
243d050ffb9SNicholas Bellinger 	unsigned long flags;
244cf572a96SChristoph Hellwig 
245c66ac9dbSNicholas Bellinger 	/*
246cf572a96SChristoph Hellwig 	 * Complete outstanding commands with TASK_ABORTED SAM status.
247cf572a96SChristoph Hellwig 	 *
248c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
249c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
250c66ac9dbSNicholas Bellinger 	 *
251c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
252c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
253c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
254c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
255c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
256c66ac9dbSNicholas Bellinger 	 *
257c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
258c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
259c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
260c66ac9dbSNicholas Bellinger 	 *
261c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
262c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
263c66ac9dbSNicholas Bellinger 	 *
264c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
265c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
266c66ac9dbSNicholas Bellinger 	 */
267c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&dev->execute_task_lock, flags);
268cf572a96SChristoph Hellwig 	list_for_each_entry_safe(cmd, next, &dev->state_list, state_list) {
269c66ac9dbSNicholas Bellinger 		/*
270c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
271c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
272c66ac9dbSNicholas Bellinger 		 */
273feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
274c66ac9dbSNicholas Bellinger 			continue;
275cf572a96SChristoph Hellwig 
276c66ac9dbSNicholas Bellinger 		/*
277c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
278c66ac9dbSNicholas Bellinger 		 */
279c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
280c66ac9dbSNicholas Bellinger 			continue;
281c66ac9dbSNicholas Bellinger 
282cf572a96SChristoph Hellwig 		list_move_tail(&cmd->state_list, &drain_task_list);
283cf572a96SChristoph Hellwig 		cmd->state_active = false;
284d050ffb9SNicholas Bellinger 	}
285c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
286c66ac9dbSNicholas Bellinger 
287d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
288cf572a96SChristoph Hellwig 		cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
289cf572a96SChristoph Hellwig 		list_del(&cmd->state_list);
290d050ffb9SNicholas Bellinger 
291cf572a96SChristoph Hellwig 		pr_debug("LUN_RESET: %s cmd: %p"
292f2da9dbdSChristoph Hellwig 			" ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state: %d"
293f2da9dbdSChristoph Hellwig 			"cdb: 0x%02x\n",
294cf572a96SChristoph Hellwig 			(preempt_and_abort_list) ? "Preempt" : "", cmd,
295e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), 0,
296e3d6f909SAndy Grover 			cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
297f2da9dbdSChristoph Hellwig 			cmd->t_task_cdb[0]);
2986708bb27SAndy Grover 		pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
299785fdf70SChristoph Hellwig 			" -- CMD_T_ACTIVE: %d"
3007d680f3bSChristoph Hellwig 			" CMD_T_STOP: %d CMD_T_SENT: %d\n",
301e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
3027d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_ACTIVE) != 0,
3037d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_STOP) != 0,
3047d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_SENT) != 0);
305c66ac9dbSNicholas Bellinger 
30635e0e757SChristoph Hellwig 		/*
30735e0e757SChristoph Hellwig 		 * If the command may be queued onto a workqueue cancel it now.
30835e0e757SChristoph Hellwig 		 *
30935e0e757SChristoph Hellwig 		 * This is equivalent to removal from the execute queue in the
31035e0e757SChristoph Hellwig 		 * loop above, but we do it down here given that
31135e0e757SChristoph Hellwig 		 * cancel_work_sync may block.
31235e0e757SChristoph Hellwig 		 */
31335e0e757SChristoph Hellwig 		if (cmd->t_state == TRANSPORT_COMPLETE)
31435e0e757SChristoph Hellwig 			cancel_work_sync(&cmd->work);
31535e0e757SChristoph Hellwig 
316a1d8b49aSAndy Grover 		spin_lock_irqsave(&cmd->t_state_lock, flags);
317cf572a96SChristoph Hellwig 		target_stop_cmd(cmd, &flags);
318c66ac9dbSNicholas Bellinger 
3197d680f3bSChristoph Hellwig 		cmd->transport_state |= CMD_T_ABORTED;
320a1d8b49aSAndy Grover 		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
321c66ac9dbSNicholas Bellinger 
322c1c35d52SNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas);
323c66ac9dbSNicholas Bellinger 	}
324d050ffb9SNicholas Bellinger }
325d050ffb9SNicholas Bellinger 
326d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
327d050ffb9SNicholas Bellinger         struct se_device *dev,
328d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
329d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
330d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
331d050ffb9SNicholas Bellinger {
332d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
333d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
334d050ffb9SNicholas Bellinger 	int tas;
335d050ffb9SNicholas Bellinger         /*
336d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
337d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
338d050ffb9SNicholas Bellinger 	 *
339d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
340d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
341d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
342d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
343d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
344d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
345d050ffb9SNicholas Bellinger 	 */
3460fd97ccfSChristoph Hellwig 	tas = dev->dev_attrib.emulate_tas;
347d050ffb9SNicholas Bellinger 	/*
348d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
349d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
350d050ffb9SNicholas Bellinger 	 */
351d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
352d050ffb9SNicholas Bellinger 		tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
353d050ffb9SNicholas Bellinger 		tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
354d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
355d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
356d050ffb9SNicholas Bellinger 				" initiator port %s\n",
357d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
358d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
359d050ffb9SNicholas Bellinger 		}
360d050ffb9SNicholas Bellinger 	}
361d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
362d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
363d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
364d050ffb9SNicholas Bellinger 
365d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
366cf572a96SChristoph Hellwig 	core_tmr_drain_state_list(dev, prout_cmd, tmr_nacl, tas,
367d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
368af877292SChristoph Hellwig 
369c66ac9dbSNicholas Bellinger 	/*
370c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
371c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
372c66ac9dbSNicholas Bellinger 	 */
3736708bb27SAndy Grover 	if (!preempt_and_abort_list &&
3740fd97ccfSChristoph Hellwig 	     (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) {
375c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
376c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
3770fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
378c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3796708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
380c66ac9dbSNicholas Bellinger 	}
381c66ac9dbSNicholas Bellinger 
382ee480683SNicholas Bellinger 	atomic_long_inc(&dev->num_resets);
383c66ac9dbSNicholas Bellinger 
3846708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
385c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
386e3d6f909SAndy Grover 			dev->transport->name);
387c66ac9dbSNicholas Bellinger 	return 0;
388c66ac9dbSNicholas Bellinger }
389d050ffb9SNicholas Bellinger 
390