1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_tmr.c
3c66ac9dbSNicholas Bellinger  *
4c66ac9dbSNicholas Bellinger  * This file contains SPC-3 task management infrastructure
5c66ac9dbSNicholas Bellinger  *
6c66ac9dbSNicholas Bellinger  * Copyright (c) 2009,2010 Rising Tide Systems
7c66ac9dbSNicholas Bellinger  * Copyright (c) 2009,2010 Linux-iSCSI.org
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
12c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
13c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
14c66ac9dbSNicholas Bellinger  * (at your option) any later version.
15c66ac9dbSNicholas Bellinger  *
16c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
17c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
20c66ac9dbSNicholas Bellinger  *
21c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
22c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
23c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24c66ac9dbSNicholas Bellinger  *
25c66ac9dbSNicholas Bellinger  ******************************************************************************/
26c66ac9dbSNicholas Bellinger 
27c66ac9dbSNicholas Bellinger #include <linux/slab.h>
28c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
29c66ac9dbSNicholas Bellinger #include <linux/list.h>
30c53181afSPaul Gortmaker #include <linux/export.h>
31c66ac9dbSNicholas Bellinger #include <scsi/scsi.h>
32c66ac9dbSNicholas Bellinger #include <scsi/scsi_cmnd.h>
33c66ac9dbSNicholas Bellinger 
34c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
35c66ac9dbSNicholas Bellinger #include <target/target_core_device.h>
36c66ac9dbSNicholas Bellinger #include <target/target_core_tmr.h>
37c66ac9dbSNicholas Bellinger #include <target/target_core_transport.h>
38c66ac9dbSNicholas Bellinger #include <target/target_core_fabric_ops.h>
39c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h>
40c66ac9dbSNicholas Bellinger 
41e26d99aeSChristoph Hellwig #include "target_core_internal.h"
42c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
43c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
44c66ac9dbSNicholas Bellinger 
45c66ac9dbSNicholas Bellinger struct se_tmr_req *core_tmr_alloc_req(
46c66ac9dbSNicholas Bellinger 	struct se_cmd *se_cmd,
47c66ac9dbSNicholas Bellinger 	void *fabric_tmr_ptr,
48dd503a5fSRoland Dreier 	u8 function,
49dd503a5fSRoland Dreier 	gfp_t gfp_flags)
50c66ac9dbSNicholas Bellinger {
51c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr;
52c66ac9dbSNicholas Bellinger 
53dd503a5fSRoland Dreier 	tmr = kmem_cache_zalloc(se_tmr_req_cache, gfp_flags);
546708bb27SAndy Grover 	if (!tmr) {
556708bb27SAndy Grover 		pr_err("Unable to allocate struct se_tmr_req\n");
56c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
57c66ac9dbSNicholas Bellinger 	}
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 
63c66ac9dbSNicholas Bellinger 	return tmr;
64c66ac9dbSNicholas Bellinger }
65c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tmr_alloc_req);
66c66ac9dbSNicholas Bellinger 
67c66ac9dbSNicholas Bellinger void core_tmr_release_req(
68c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr)
69c66ac9dbSNicholas Bellinger {
70c66ac9dbSNicholas Bellinger 	struct se_device *dev = tmr->tmr_dev;
71d050ffb9SNicholas Bellinger 	unsigned long flags;
72c66ac9dbSNicholas Bellinger 
737fd29aa9SNicholas Bellinger 	if (!dev) {
747fd29aa9SNicholas Bellinger 		kmem_cache_free(se_tmr_req_cache, tmr);
757fd29aa9SNicholas Bellinger 		return;
767fd29aa9SNicholas Bellinger 	}
777fd29aa9SNicholas Bellinger 
78d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
79c66ac9dbSNicholas Bellinger 	list_del(&tmr->tmr_list);
80d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
817fd29aa9SNicholas Bellinger 
827fd29aa9SNicholas Bellinger 	kmem_cache_free(se_tmr_req_cache, tmr);
83c66ac9dbSNicholas Bellinger }
84c66ac9dbSNicholas Bellinger 
85c66ac9dbSNicholas Bellinger static void core_tmr_handle_tas_abort(
86c66ac9dbSNicholas Bellinger 	struct se_node_acl *tmr_nacl,
87c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
88c66ac9dbSNicholas Bellinger 	int tas,
89c66ac9dbSNicholas Bellinger 	int fe_count)
90c66ac9dbSNicholas Bellinger {
916708bb27SAndy Grover 	if (!fe_count) {
92c66ac9dbSNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
93c66ac9dbSNicholas Bellinger 		return;
94c66ac9dbSNicholas Bellinger 	}
95c66ac9dbSNicholas Bellinger 	/*
96c66ac9dbSNicholas Bellinger 	 * TASK ABORTED status (TAS) bit support
97c66ac9dbSNicholas Bellinger 	*/
986708bb27SAndy Grover 	if ((tmr_nacl &&
99c66ac9dbSNicholas Bellinger 	     (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
100c66ac9dbSNicholas Bellinger 		transport_send_task_abort(cmd);
101c66ac9dbSNicholas Bellinger 
102c66ac9dbSNicholas Bellinger 	transport_cmd_finish_abort(cmd, 0);
103c66ac9dbSNicholas Bellinger }
104c66ac9dbSNicholas Bellinger 
105d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
106c66ac9dbSNicholas Bellinger 	struct se_device *dev,
107c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
108d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
109c66ac9dbSNicholas Bellinger {
110d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
111c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
112d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
113c66ac9dbSNicholas Bellinger 	unsigned long flags;
114c66ac9dbSNicholas Bellinger 	/*
115c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
116c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
117c66ac9dbSNicholas Bellinger 	 */
118d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
119c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
120c66ac9dbSNicholas Bellinger 		/*
121c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
122c66ac9dbSNicholas Bellinger 		 */
123abc1fd4fSJoern Engel 		if (tmr_p == tmr)
124c66ac9dbSNicholas Bellinger 			continue;
125c66ac9dbSNicholas Bellinger 
126c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
1276708bb27SAndy Grover 		if (!cmd) {
1286708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
129c66ac9dbSNicholas Bellinger 			continue;
130c66ac9dbSNicholas Bellinger 		}
131c66ac9dbSNicholas Bellinger 		/*
132c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
133c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
134c66ac9dbSNicholas Bellinger 		 * skip non regisration key matching TMRs.
135c66ac9dbSNicholas Bellinger 		 */
1366708bb27SAndy Grover 		if (preempt_and_abort_list &&
137c66ac9dbSNicholas Bellinger 		    (core_scsi3_check_cdb_abort_and_preempt(
138c66ac9dbSNicholas Bellinger 					preempt_and_abort_list, cmd) != 0))
139c66ac9dbSNicholas Bellinger 			continue;
140c66ac9dbSNicholas Bellinger 
141d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
1426708bb27SAndy Grover 		if (!atomic_read(&cmd->t_transport_active)) {
143d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
144c66ac9dbSNicholas Bellinger 			continue;
145c66ac9dbSNicholas Bellinger 		}
146c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
147d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
148c66ac9dbSNicholas Bellinger 			continue;
149c66ac9dbSNicholas Bellinger 		}
150d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
151d050ffb9SNicholas Bellinger 
1526eb40b2aSJoern Engel 		list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
153d050ffb9SNicholas Bellinger 	}
154d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
155d050ffb9SNicholas Bellinger 
156abc1fd4fSJoern Engel 	list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
157b8a11d73SJoern Engel 		list_del_init(&tmr_p->tmr_list);
158abc1fd4fSJoern Engel 		cmd = tmr_p->task_cmd;
159d050ffb9SNicholas Bellinger 
1606708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
161c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
162abc1fd4fSJoern Engel 			(preempt_and_abort_list) ? "Preempt" : "", tmr_p,
163abc1fd4fSJoern Engel 			tmr_p->function, tmr_p->response, cmd->t_state);
164c66ac9dbSNicholas Bellinger 
1658dc52b54SNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
166c66ac9dbSNicholas Bellinger 	}
167d050ffb9SNicholas Bellinger }
168d050ffb9SNicholas Bellinger 
169d050ffb9SNicholas Bellinger static void core_tmr_drain_task_list(
170d050ffb9SNicholas Bellinger 	struct se_device *dev,
171d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
172d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
173d050ffb9SNicholas Bellinger 	int tas,
174d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
175d050ffb9SNicholas Bellinger {
176d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
177d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
178d050ffb9SNicholas Bellinger 	struct se_task *task, *task_tmp;
179d050ffb9SNicholas Bellinger 	unsigned long flags;
180d050ffb9SNicholas Bellinger 	int fe_count;
181c66ac9dbSNicholas Bellinger 	/*
182c66ac9dbSNicholas Bellinger 	 * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
183c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
184c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
185c66ac9dbSNicholas Bellinger 	 *
186c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
187c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
188c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
189c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
190c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
191c66ac9dbSNicholas Bellinger 	 *
192c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
193c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
194c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
195c66ac9dbSNicholas Bellinger 	 *
196c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
197c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
198c66ac9dbSNicholas Bellinger 	 *
199c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
200c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
201c66ac9dbSNicholas Bellinger 	 */
202c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&dev->execute_task_lock, flags);
203c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
204c66ac9dbSNicholas Bellinger 				t_state_list) {
205e3d6f909SAndy Grover 		if (!task->task_se_cmd) {
2066708bb27SAndy Grover 			pr_err("task->task_se_cmd is NULL!\n");
207c66ac9dbSNicholas Bellinger 			continue;
208c66ac9dbSNicholas Bellinger 		}
209e3d6f909SAndy Grover 		cmd = task->task_se_cmd;
210c66ac9dbSNicholas Bellinger 
211c66ac9dbSNicholas Bellinger 		/*
212c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
213c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
214c66ac9dbSNicholas Bellinger 		 */
2156708bb27SAndy Grover 		if (preempt_and_abort_list &&
216c66ac9dbSNicholas Bellinger 		    (core_scsi3_check_cdb_abort_and_preempt(
217c66ac9dbSNicholas Bellinger 					preempt_and_abort_list, cmd) != 0))
218c66ac9dbSNicholas Bellinger 			continue;
219c66ac9dbSNicholas Bellinger 		/*
220c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
221c66ac9dbSNicholas Bellinger 		 */
222c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
223c66ac9dbSNicholas Bellinger 			continue;
224c66ac9dbSNicholas Bellinger 
225d050ffb9SNicholas Bellinger 		list_move_tail(&task->t_state_list, &drain_task_list);
226c66ac9dbSNicholas Bellinger 		atomic_set(&task->task_state_active, 0);
227d050ffb9SNicholas Bellinger 		/*
228d050ffb9SNicholas Bellinger 		 * Remove from task execute list before processing drain_task_list
229d050ffb9SNicholas Bellinger 		 */
23004629b7bSChristoph Hellwig 		if (!list_empty(&task->t_execute_list))
23104629b7bSChristoph Hellwig 			__transport_remove_task_from_execute_queue(task, dev);
232d050ffb9SNicholas Bellinger 	}
233c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
234c66ac9dbSNicholas Bellinger 
235d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
236d050ffb9SNicholas Bellinger 		task = list_entry(drain_task_list.next, struct se_task, t_state_list);
237d050ffb9SNicholas Bellinger 		list_del(&task->t_state_list);
238d050ffb9SNicholas Bellinger 		cmd = task->task_se_cmd;
239d050ffb9SNicholas Bellinger 
2406708bb27SAndy Grover 		pr_debug("LUN_RESET: %s cmd: %p task: %p"
241f2da9dbdSChristoph Hellwig 			" ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state: %d"
242f2da9dbdSChristoph Hellwig 			"cdb: 0x%02x\n",
243c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "", cmd, task,
244e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), 0,
245e3d6f909SAndy Grover 			cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
246f2da9dbdSChristoph Hellwig 			cmd->t_task_cdb[0]);
2476708bb27SAndy Grover 		pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
248c66ac9dbSNicholas Bellinger 			" t_task_cdbs: %d t_task_cdbs_left: %d"
249c66ac9dbSNicholas Bellinger 			" t_task_cdbs_sent: %d -- t_transport_active: %d"
250c66ac9dbSNicholas Bellinger 			" t_transport_stop: %d t_transport_sent: %d\n",
251e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
252a1d8b49aSAndy Grover 			cmd->t_task_list_num,
253a1d8b49aSAndy Grover 			atomic_read(&cmd->t_task_cdbs_left),
254a1d8b49aSAndy Grover 			atomic_read(&cmd->t_task_cdbs_sent),
255a1d8b49aSAndy Grover 			atomic_read(&cmd->t_transport_active),
256a1d8b49aSAndy Grover 			atomic_read(&cmd->t_transport_stop),
257a1d8b49aSAndy Grover 			atomic_read(&cmd->t_transport_sent));
258c66ac9dbSNicholas Bellinger 
25935e0e757SChristoph Hellwig 		/*
26035e0e757SChristoph Hellwig 		 * If the command may be queued onto a workqueue cancel it now.
26135e0e757SChristoph Hellwig 		 *
26235e0e757SChristoph Hellwig 		 * This is equivalent to removal from the execute queue in the
26335e0e757SChristoph Hellwig 		 * loop above, but we do it down here given that
26435e0e757SChristoph Hellwig 		 * cancel_work_sync may block.
26535e0e757SChristoph Hellwig 		 */
26635e0e757SChristoph Hellwig 		if (cmd->t_state == TRANSPORT_COMPLETE)
26735e0e757SChristoph Hellwig 			cancel_work_sync(&cmd->work);
26835e0e757SChristoph Hellwig 
269a1d8b49aSAndy Grover 		spin_lock_irqsave(&cmd->t_state_lock, flags);
270cdbb70bbSChristoph Hellwig 		target_stop_task(task, &flags);
271c66ac9dbSNicholas Bellinger 
2726708bb27SAndy Grover 		if (!atomic_dec_and_test(&cmd->t_task_cdbs_ex_left)) {
273d050ffb9SNicholas Bellinger 			spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2746708bb27SAndy Grover 			pr_debug("LUN_RESET: Skipping task: %p, dev: %p for"
275c66ac9dbSNicholas Bellinger 				" t_task_cdbs_ex_left: %d\n", task, dev,
276a1d8b49aSAndy Grover 				atomic_read(&cmd->t_task_cdbs_ex_left));
277c66ac9dbSNicholas Bellinger 			continue;
278c66ac9dbSNicholas Bellinger 		}
279a1d8b49aSAndy Grover 		fe_count = atomic_read(&cmd->t_fe_count);
280c66ac9dbSNicholas Bellinger 
281a1d8b49aSAndy Grover 		if (atomic_read(&cmd->t_transport_active)) {
2826708bb27SAndy Grover 			pr_debug("LUN_RESET: got t_transport_active = 1 for"
283c66ac9dbSNicholas Bellinger 				" task: %p, t_fe_count: %d dev: %p\n", task,
284c66ac9dbSNicholas Bellinger 				fe_count, dev);
285a1d8b49aSAndy Grover 			atomic_set(&cmd->t_transport_aborted, 1);
286d050ffb9SNicholas Bellinger 			spin_unlock_irqrestore(&cmd->t_state_lock, flags);
287c66ac9dbSNicholas Bellinger 
288d050ffb9SNicholas Bellinger 			core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
289c66ac9dbSNicholas Bellinger 			continue;
290c66ac9dbSNicholas Bellinger 		}
2916708bb27SAndy Grover 		pr_debug("LUN_RESET: Got t_transport_active = 0 for task: %p,"
292c66ac9dbSNicholas Bellinger 			" t_fe_count: %d dev: %p\n", task, fe_count, dev);
293a1d8b49aSAndy Grover 		atomic_set(&cmd->t_transport_aborted, 1);
294a1d8b49aSAndy Grover 		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
295c66ac9dbSNicholas Bellinger 
296d050ffb9SNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
297c66ac9dbSNicholas Bellinger 	}
298d050ffb9SNicholas Bellinger }
299d050ffb9SNicholas Bellinger 
300d050ffb9SNicholas Bellinger static void core_tmr_drain_cmd_list(
301d050ffb9SNicholas Bellinger 	struct se_device *dev,
302d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
303d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
304d050ffb9SNicholas Bellinger 	int tas,
305d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
306d050ffb9SNicholas Bellinger {
307d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_cmd_list);
308d050ffb9SNicholas Bellinger 	struct se_queue_obj *qobj = &dev->dev_queue_obj;
309d050ffb9SNicholas Bellinger 	struct se_cmd *cmd, *tcmd;
310d050ffb9SNicholas Bellinger 	unsigned long flags;
311c66ac9dbSNicholas Bellinger 	/*
312c66ac9dbSNicholas Bellinger 	 * Release all commands remaining in the struct se_device cmd queue.
313c66ac9dbSNicholas Bellinger 	 *
314c66ac9dbSNicholas Bellinger 	 * This follows the same logic as above for the struct se_device
315c66ac9dbSNicholas Bellinger 	 * struct se_task state list, where commands are returned with
316c66ac9dbSNicholas Bellinger 	 * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
317c66ac9dbSNicholas Bellinger 	 * reference, otherwise the struct se_cmd is released.
318c66ac9dbSNicholas Bellinger 	 */
319c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
3205951146dSAndy Grover 	list_for_each_entry_safe(cmd, tcmd, &qobj->qobj_list, se_queue_node) {
321c66ac9dbSNicholas Bellinger 		/*
322c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
323c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
324c66ac9dbSNicholas Bellinger 		 */
3256708bb27SAndy Grover 		if (preempt_and_abort_list &&
326c66ac9dbSNicholas Bellinger 		    (core_scsi3_check_cdb_abort_and_preempt(
327c66ac9dbSNicholas Bellinger 					preempt_and_abort_list, cmd) != 0))
328c66ac9dbSNicholas Bellinger 			continue;
329c66ac9dbSNicholas Bellinger 		/*
330c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
331c66ac9dbSNicholas Bellinger 		 */
332c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
333c66ac9dbSNicholas Bellinger 			continue;
334c66ac9dbSNicholas Bellinger 
335d050ffb9SNicholas Bellinger 		atomic_set(&cmd->t_transport_queue_active, 0);
336c66ac9dbSNicholas Bellinger 		atomic_dec(&qobj->queue_cnt);
337d050ffb9SNicholas Bellinger 		list_move_tail(&cmd->se_queue_node, &drain_cmd_list);
338d050ffb9SNicholas Bellinger 	}
339c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
340c66ac9dbSNicholas Bellinger 
341d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_cmd_list)) {
342d050ffb9SNicholas Bellinger 		cmd = list_entry(drain_cmd_list.next, struct se_cmd, se_queue_node);
343d050ffb9SNicholas Bellinger 		list_del_init(&cmd->se_queue_node);
344d050ffb9SNicholas Bellinger 
3456708bb27SAndy Grover 		pr_debug("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
346c66ac9dbSNicholas Bellinger 			" %d t_fe_count: %d\n", (preempt_and_abort_list) ?
3475951146dSAndy Grover 			"Preempt" : "", cmd, cmd->t_state,
348a1d8b49aSAndy Grover 			atomic_read(&cmd->t_fe_count));
349c66ac9dbSNicholas Bellinger 
350c66ac9dbSNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
351a1d8b49aSAndy Grover 				atomic_read(&cmd->t_fe_count));
352c66ac9dbSNicholas Bellinger 	}
353d050ffb9SNicholas Bellinger }
354d050ffb9SNicholas Bellinger 
355d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
356d050ffb9SNicholas Bellinger         struct se_device *dev,
357d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
358d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
359d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
360d050ffb9SNicholas Bellinger {
361d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
362d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
363d050ffb9SNicholas Bellinger 	int tas;
364d050ffb9SNicholas Bellinger         /*
365d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
366d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
367d050ffb9SNicholas Bellinger 	 *
368d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
369d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
370d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
371d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
372d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
373d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
374d050ffb9SNicholas Bellinger 	 */
375d050ffb9SNicholas Bellinger 	tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
376d050ffb9SNicholas Bellinger 	/*
377d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
378d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
379d050ffb9SNicholas Bellinger 	 */
380d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
381d050ffb9SNicholas Bellinger 		tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
382d050ffb9SNicholas Bellinger 		tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
383d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
384d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
385d050ffb9SNicholas Bellinger 				" initiator port %s\n",
386d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
387d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
388d050ffb9SNicholas Bellinger 		}
389d050ffb9SNicholas Bellinger 	}
390d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
391d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
392d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
393d050ffb9SNicholas Bellinger 
394d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
395d050ffb9SNicholas Bellinger 	core_tmr_drain_task_list(dev, prout_cmd, tmr_nacl, tas,
396d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
397d050ffb9SNicholas Bellinger 	core_tmr_drain_cmd_list(dev, prout_cmd, tmr_nacl, tas,
398d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
399c66ac9dbSNicholas Bellinger 	/*
400c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
401c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
402c66ac9dbSNicholas Bellinger 	 */
4036708bb27SAndy Grover 	if (!preempt_and_abort_list &&
404c66ac9dbSNicholas Bellinger 	     (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
405c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
406c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
407c66ac9dbSNicholas Bellinger 		dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
408c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
4096708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
410c66ac9dbSNicholas Bellinger 	}
411c66ac9dbSNicholas Bellinger 
4121e7de68cSNicholas Bellinger 	spin_lock_irq(&dev->stats_lock);
413c66ac9dbSNicholas Bellinger 	dev->num_resets++;
4141e7de68cSNicholas Bellinger 	spin_unlock_irq(&dev->stats_lock);
415c66ac9dbSNicholas Bellinger 
4166708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
417c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
418e3d6f909SAndy Grover 			dev->transport->name);
419c66ac9dbSNicholas Bellinger 	return 0;
420c66ac9dbSNicholas Bellinger }
421d050ffb9SNicholas Bellinger 
422