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;
120649ee054SBart Van Assche 	u64 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 
132649ee054SBart Van Assche 		ref_tag = se_cmd->tag;
1333d28934aSNicholas Bellinger 		if (tmr->ref_task_tag != ref_tag)
1343d28934aSNicholas Bellinger 			continue;
1353d28934aSNicholas Bellinger 
136649ee054SBart Van Assche 		printk("ABORT_TASK: Found referenced %s task_tag: %llu\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) {
141649ee054SBart Van Assche 			printk("ABORT_TASK: ref_tag: %llu already complete,"
142649ee054SBart Van Assche 			       " skipping\n", ref_tag);
143ab74b3d6SSteve Hodgson 			spin_unlock(&se_cmd->t_state_lock);
1443d28934aSNicholas Bellinger 			spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1453d28934aSNicholas Bellinger 			goto out;
1463d28934aSNicholas Bellinger 		}
1473d28934aSNicholas Bellinger 		se_cmd->transport_state |= CMD_T_ABORTED;
148ab74b3d6SSteve Hodgson 		spin_unlock(&se_cmd->t_state_lock);
1493d28934aSNicholas Bellinger 
1503d28934aSNicholas Bellinger 		list_del_init(&se_cmd->se_cmd_list);
1513d28934aSNicholas Bellinger 		kref_get(&se_cmd->cmd_kref);
1523d28934aSNicholas Bellinger 		spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1533d28934aSNicholas Bellinger 
1543d28934aSNicholas Bellinger 		cancel_work_sync(&se_cmd->work);
1553d28934aSNicholas Bellinger 		transport_wait_for_tasks(se_cmd);
1563d28934aSNicholas Bellinger 
157afc16604SBart Van Assche 		target_put_sess_cmd(se_cmd);
15868259b5aSAlex Leung 		transport_cmd_finish_abort(se_cmd, true);
1593d28934aSNicholas Bellinger 
1603d28934aSNicholas Bellinger 		printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for"
161649ee054SBart Van Assche 				" ref_tag: %llu\n", ref_tag);
1623d28934aSNicholas Bellinger 		tmr->response = TMR_FUNCTION_COMPLETE;
1633d28934aSNicholas Bellinger 		return;
1643d28934aSNicholas Bellinger 	}
1653d28934aSNicholas Bellinger 	spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
1663d28934aSNicholas Bellinger 
1673d28934aSNicholas Bellinger out:
168649ee054SBart Van Assche 	printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %lld\n",
1693d28934aSNicholas Bellinger 			tmr->ref_task_tag);
1703d28934aSNicholas Bellinger 	tmr->response = TMR_TASK_DOES_NOT_EXIST;
1713d28934aSNicholas Bellinger }
1723d28934aSNicholas Bellinger 
173d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
174c66ac9dbSNicholas Bellinger 	struct se_device *dev,
175c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
176d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
177c66ac9dbSNicholas Bellinger {
178d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
179c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
180d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
181c66ac9dbSNicholas Bellinger 	unsigned long flags;
182c66ac9dbSNicholas Bellinger 	/*
183c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
184c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
185c66ac9dbSNicholas Bellinger 	 */
186d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
187c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
188c66ac9dbSNicholas Bellinger 		/*
189c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
190c66ac9dbSNicholas Bellinger 		 */
191abc1fd4fSJoern Engel 		if (tmr_p == tmr)
192c66ac9dbSNicholas Bellinger 			continue;
193c66ac9dbSNicholas Bellinger 
194c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
1956708bb27SAndy Grover 		if (!cmd) {
1966708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
197c66ac9dbSNicholas Bellinger 			continue;
198c66ac9dbSNicholas Bellinger 		}
199c66ac9dbSNicholas Bellinger 		/*
200c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
201c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
202c66ac9dbSNicholas Bellinger 		 * skip non regisration key matching TMRs.
203c66ac9dbSNicholas Bellinger 		 */
204feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
205c66ac9dbSNicholas Bellinger 			continue;
206c66ac9dbSNicholas Bellinger 
207d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
2087d680f3bSChristoph Hellwig 		if (!(cmd->transport_state & CMD_T_ACTIVE)) {
209d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
210c66ac9dbSNicholas Bellinger 			continue;
211c66ac9dbSNicholas Bellinger 		}
212c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
213d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
214c66ac9dbSNicholas Bellinger 			continue;
215c66ac9dbSNicholas Bellinger 		}
216d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
217d050ffb9SNicholas Bellinger 
2186eb40b2aSJoern Engel 		list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
219d050ffb9SNicholas Bellinger 	}
220d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
221d050ffb9SNicholas Bellinger 
222abc1fd4fSJoern Engel 	list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
223b8a11d73SJoern Engel 		list_del_init(&tmr_p->tmr_list);
224abc1fd4fSJoern Engel 		cmd = tmr_p->task_cmd;
225d050ffb9SNicholas Bellinger 
2266708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
227c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
228abc1fd4fSJoern Engel 			(preempt_and_abort_list) ? "Preempt" : "", tmr_p,
229abc1fd4fSJoern Engel 			tmr_p->function, tmr_p->response, cmd->t_state);
230c66ac9dbSNicholas Bellinger 
2318dc52b54SNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
232c66ac9dbSNicholas Bellinger 	}
233d050ffb9SNicholas Bellinger }
234d050ffb9SNicholas Bellinger 
235cf572a96SChristoph Hellwig static void core_tmr_drain_state_list(
236d050ffb9SNicholas Bellinger 	struct se_device *dev,
237d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
238d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
239d050ffb9SNicholas Bellinger 	int tas,
240d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
241d050ffb9SNicholas Bellinger {
242d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
243cf572a96SChristoph Hellwig 	struct se_cmd *cmd, *next;
244d050ffb9SNicholas Bellinger 	unsigned long flags;
245cf572a96SChristoph Hellwig 
246c66ac9dbSNicholas Bellinger 	/*
247cf572a96SChristoph Hellwig 	 * Complete outstanding commands with TASK_ABORTED SAM status.
248cf572a96SChristoph Hellwig 	 *
249c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
250c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
251c66ac9dbSNicholas Bellinger 	 *
252c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
253c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
254c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
255c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
256c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
257c66ac9dbSNicholas Bellinger 	 *
258c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
259c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
260c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
261c66ac9dbSNicholas Bellinger 	 *
262c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
263c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
264c66ac9dbSNicholas Bellinger 	 *
265c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
266c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
267c66ac9dbSNicholas Bellinger 	 */
268c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&dev->execute_task_lock, flags);
269cf572a96SChristoph Hellwig 	list_for_each_entry_safe(cmd, next, &dev->state_list, state_list) {
270c66ac9dbSNicholas Bellinger 		/*
271c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
272c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
273c66ac9dbSNicholas Bellinger 		 */
274feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
275c66ac9dbSNicholas Bellinger 			continue;
276cf572a96SChristoph Hellwig 
277c66ac9dbSNicholas Bellinger 		/*
278c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
279c66ac9dbSNicholas Bellinger 		 */
280c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
281c66ac9dbSNicholas Bellinger 			continue;
282c66ac9dbSNicholas Bellinger 
283cf572a96SChristoph Hellwig 		list_move_tail(&cmd->state_list, &drain_task_list);
284cf572a96SChristoph Hellwig 		cmd->state_active = false;
285d050ffb9SNicholas Bellinger 	}
286c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
287c66ac9dbSNicholas Bellinger 
288d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
289cf572a96SChristoph Hellwig 		cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
290cf572a96SChristoph Hellwig 		list_del(&cmd->state_list);
291d050ffb9SNicholas Bellinger 
292cf572a96SChristoph Hellwig 		pr_debug("LUN_RESET: %s cmd: %p"
293649ee054SBart Van Assche 			" ITT/CmdSN: 0x%08llx/0x%08x, i_state: %d, t_state: %d"
294f2da9dbdSChristoph Hellwig 			"cdb: 0x%02x\n",
295cf572a96SChristoph Hellwig 			(preempt_and_abort_list) ? "Preempt" : "", cmd,
296649ee054SBart Van Assche 			cmd->tag, 0,
297e3d6f909SAndy Grover 			cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
298f2da9dbdSChristoph Hellwig 			cmd->t_task_cdb[0]);
299649ee054SBart Van Assche 		pr_debug("LUN_RESET: ITT[0x%08llx] - pr_res_key: 0x%016Lx"
300785fdf70SChristoph Hellwig 			" -- CMD_T_ACTIVE: %d"
3017d680f3bSChristoph Hellwig 			" CMD_T_STOP: %d CMD_T_SENT: %d\n",
302649ee054SBart Van Assche 			cmd->tag, cmd->pr_res_key,
3037d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_ACTIVE) != 0,
3047d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_STOP) != 0,
3057d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_SENT) != 0);
306c66ac9dbSNicholas Bellinger 
30735e0e757SChristoph Hellwig 		/*
30835e0e757SChristoph Hellwig 		 * If the command may be queued onto a workqueue cancel it now.
30935e0e757SChristoph Hellwig 		 *
31035e0e757SChristoph Hellwig 		 * This is equivalent to removal from the execute queue in the
31135e0e757SChristoph Hellwig 		 * loop above, but we do it down here given that
31235e0e757SChristoph Hellwig 		 * cancel_work_sync may block.
31335e0e757SChristoph Hellwig 		 */
31435e0e757SChristoph Hellwig 		if (cmd->t_state == TRANSPORT_COMPLETE)
31535e0e757SChristoph Hellwig 			cancel_work_sync(&cmd->work);
31635e0e757SChristoph Hellwig 
317a1d8b49aSAndy Grover 		spin_lock_irqsave(&cmd->t_state_lock, flags);
318cf572a96SChristoph Hellwig 		target_stop_cmd(cmd, &flags);
319c66ac9dbSNicholas Bellinger 
3207d680f3bSChristoph Hellwig 		cmd->transport_state |= CMD_T_ABORTED;
321a1d8b49aSAndy Grover 		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
322c66ac9dbSNicholas Bellinger 
323c1c35d52SNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas);
324c66ac9dbSNicholas Bellinger 	}
325d050ffb9SNicholas Bellinger }
326d050ffb9SNicholas Bellinger 
327d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
328d050ffb9SNicholas Bellinger         struct se_device *dev,
329d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
330d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
331d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
332d050ffb9SNicholas Bellinger {
333d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
334d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
335d050ffb9SNicholas Bellinger 	int tas;
336d050ffb9SNicholas Bellinger         /*
337d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
338d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
339d050ffb9SNicholas Bellinger 	 *
340d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
341d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
342d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
343d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
344d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
345d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
346d050ffb9SNicholas Bellinger 	 */
3470fd97ccfSChristoph Hellwig 	tas = dev->dev_attrib.emulate_tas;
348d050ffb9SNicholas Bellinger 	/*
349d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
350d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
351d050ffb9SNicholas Bellinger 	 */
352d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
353d050ffb9SNicholas Bellinger 		tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
354d050ffb9SNicholas Bellinger 		tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
355d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
356d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
357d050ffb9SNicholas Bellinger 				" initiator port %s\n",
358d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
359d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
360d050ffb9SNicholas Bellinger 		}
361d050ffb9SNicholas Bellinger 	}
362d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
363d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
364d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
365d050ffb9SNicholas Bellinger 
366d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
367cf572a96SChristoph Hellwig 	core_tmr_drain_state_list(dev, prout_cmd, tmr_nacl, tas,
368d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
369af877292SChristoph Hellwig 
370c66ac9dbSNicholas Bellinger 	/*
371c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
372c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
373c66ac9dbSNicholas Bellinger 	 */
3746708bb27SAndy Grover 	if (!preempt_and_abort_list &&
3750fd97ccfSChristoph Hellwig 	     (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) {
376c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
377c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
3780fd97ccfSChristoph Hellwig 		dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
379c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
3806708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
381c66ac9dbSNicholas Bellinger 	}
382c66ac9dbSNicholas Bellinger 
383ee480683SNicholas Bellinger 	atomic_long_inc(&dev->num_resets);
384c66ac9dbSNicholas Bellinger 
3856708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
386c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
387e3d6f909SAndy Grover 			dev->transport->name);
388c66ac9dbSNicholas Bellinger 	return 0;
389c66ac9dbSNicholas Bellinger }
390d050ffb9SNicholas Bellinger 
391