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>
30c66ac9dbSNicholas Bellinger #include <scsi/scsi.h>
31c66ac9dbSNicholas Bellinger #include <scsi/scsi_cmnd.h>
32c66ac9dbSNicholas Bellinger 
33c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
34c66ac9dbSNicholas Bellinger #include <target/target_core_device.h>
35c66ac9dbSNicholas Bellinger #include <target/target_core_tmr.h>
36c66ac9dbSNicholas Bellinger #include <target/target_core_transport.h>
37c66ac9dbSNicholas Bellinger #include <target/target_core_fabric_ops.h>
38c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h>
39c66ac9dbSNicholas Bellinger 
40c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
41c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
42c66ac9dbSNicholas Bellinger 
43c66ac9dbSNicholas Bellinger struct se_tmr_req *core_tmr_alloc_req(
44c66ac9dbSNicholas Bellinger 	struct se_cmd *se_cmd,
45c66ac9dbSNicholas Bellinger 	void *fabric_tmr_ptr,
46c66ac9dbSNicholas Bellinger 	u8 function)
47c66ac9dbSNicholas Bellinger {
48c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr;
49c66ac9dbSNicholas Bellinger 
501e7de68cSNicholas Bellinger 	tmr = kmem_cache_zalloc(se_tmr_req_cache, (in_interrupt()) ?
511e7de68cSNicholas Bellinger 					GFP_ATOMIC : GFP_KERNEL);
526708bb27SAndy Grover 	if (!tmr) {
536708bb27SAndy Grover 		pr_err("Unable to allocate struct se_tmr_req\n");
54c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
55c66ac9dbSNicholas Bellinger 	}
56c66ac9dbSNicholas Bellinger 	tmr->task_cmd = se_cmd;
57c66ac9dbSNicholas Bellinger 	tmr->fabric_tmr_ptr = fabric_tmr_ptr;
58c66ac9dbSNicholas Bellinger 	tmr->function = function;
59c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tmr->tmr_list);
60c66ac9dbSNicholas Bellinger 
61c66ac9dbSNicholas Bellinger 	return tmr;
62c66ac9dbSNicholas Bellinger }
63c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tmr_alloc_req);
64c66ac9dbSNicholas Bellinger 
65c66ac9dbSNicholas Bellinger void core_tmr_release_req(
66c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr)
67c66ac9dbSNicholas Bellinger {
68c66ac9dbSNicholas Bellinger 	struct se_device *dev = tmr->tmr_dev;
69d050ffb9SNicholas Bellinger 	unsigned long flags;
70c66ac9dbSNicholas Bellinger 
717fd29aa9SNicholas Bellinger 	if (!dev) {
727fd29aa9SNicholas Bellinger 		kmem_cache_free(se_tmr_req_cache, tmr);
737fd29aa9SNicholas Bellinger 		return;
747fd29aa9SNicholas Bellinger 	}
757fd29aa9SNicholas Bellinger 
76d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
77c66ac9dbSNicholas Bellinger 	list_del(&tmr->tmr_list);
78d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
797fd29aa9SNicholas Bellinger 
807fd29aa9SNicholas Bellinger 	kmem_cache_free(se_tmr_req_cache, tmr);
81c66ac9dbSNicholas Bellinger }
82c66ac9dbSNicholas Bellinger 
83c66ac9dbSNicholas Bellinger static void core_tmr_handle_tas_abort(
84c66ac9dbSNicholas Bellinger 	struct se_node_acl *tmr_nacl,
85c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
86c66ac9dbSNicholas Bellinger 	int tas,
87c66ac9dbSNicholas Bellinger 	int fe_count)
88c66ac9dbSNicholas Bellinger {
896708bb27SAndy Grover 	if (!fe_count) {
90c66ac9dbSNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
91c66ac9dbSNicholas Bellinger 		return;
92c66ac9dbSNicholas Bellinger 	}
93c66ac9dbSNicholas Bellinger 	/*
94c66ac9dbSNicholas Bellinger 	 * TASK ABORTED status (TAS) bit support
95c66ac9dbSNicholas Bellinger 	*/
966708bb27SAndy Grover 	if ((tmr_nacl &&
97c66ac9dbSNicholas Bellinger 	     (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
98c66ac9dbSNicholas Bellinger 		transport_send_task_abort(cmd);
99c66ac9dbSNicholas Bellinger 
100c66ac9dbSNicholas Bellinger 	transport_cmd_finish_abort(cmd, 0);
101c66ac9dbSNicholas Bellinger }
102c66ac9dbSNicholas Bellinger 
103d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
104c66ac9dbSNicholas Bellinger 	struct se_device *dev,
105c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
106d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
107c66ac9dbSNicholas Bellinger {
108d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
109c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
110d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
111c66ac9dbSNicholas Bellinger 	unsigned long flags;
112c66ac9dbSNicholas Bellinger 	/*
113c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
114c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
115c66ac9dbSNicholas Bellinger 	 */
116d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
117c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
118c66ac9dbSNicholas Bellinger 		/*
119c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
120c66ac9dbSNicholas Bellinger 		 */
121c66ac9dbSNicholas Bellinger 		if (tmr && (tmr_p == tmr))
122c66ac9dbSNicholas Bellinger 			continue;
123c66ac9dbSNicholas Bellinger 
124c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
1256708bb27SAndy Grover 		if (!cmd) {
1266708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
127c66ac9dbSNicholas Bellinger 			continue;
128c66ac9dbSNicholas Bellinger 		}
129c66ac9dbSNicholas Bellinger 		/*
130c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
131c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
132c66ac9dbSNicholas Bellinger 		 * skip non regisration key matching TMRs.
133c66ac9dbSNicholas Bellinger 		 */
1346708bb27SAndy Grover 		if (preempt_and_abort_list &&
135c66ac9dbSNicholas Bellinger 		    (core_scsi3_check_cdb_abort_and_preempt(
136c66ac9dbSNicholas Bellinger 					preempt_and_abort_list, cmd) != 0))
137c66ac9dbSNicholas Bellinger 			continue;
138c66ac9dbSNicholas Bellinger 
139d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
1406708bb27SAndy Grover 		if (!atomic_read(&cmd->t_transport_active)) {
141d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
142c66ac9dbSNicholas Bellinger 			continue;
143c66ac9dbSNicholas Bellinger 		}
144c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
145d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
146c66ac9dbSNicholas Bellinger 			continue;
147c66ac9dbSNicholas Bellinger 		}
148d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
149d050ffb9SNicholas Bellinger 
150d050ffb9SNicholas Bellinger 		list_move_tail(&tmr->tmr_list, &drain_tmr_list);
151d050ffb9SNicholas Bellinger 	}
152d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
153d050ffb9SNicholas Bellinger 
154d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_tmr_list)) {
155d050ffb9SNicholas Bellinger 		tmr = list_entry(drain_tmr_list.next, struct se_tmr_req, tmr_list);
156d050ffb9SNicholas Bellinger 		list_del(&tmr->tmr_list);
157d050ffb9SNicholas Bellinger 		cmd = tmr_p->task_cmd;
158d050ffb9SNicholas Bellinger 
1596708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
160c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
161d050ffb9SNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "", tmr,
162d050ffb9SNicholas Bellinger 			tmr->function, tmr->response, cmd->t_state);
163c66ac9dbSNicholas Bellinger 
164c66ac9dbSNicholas Bellinger 		transport_cmd_finish_abort_tmr(cmd);
165c66ac9dbSNicholas Bellinger 	}
166d050ffb9SNicholas Bellinger }
167d050ffb9SNicholas Bellinger 
168d050ffb9SNicholas Bellinger static void core_tmr_drain_task_list(
169d050ffb9SNicholas Bellinger 	struct se_device *dev,
170d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
171d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
172d050ffb9SNicholas Bellinger 	int tas,
173d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
174d050ffb9SNicholas Bellinger {
175d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
176d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
177d050ffb9SNicholas Bellinger 	struct se_task *task, *task_tmp;
178d050ffb9SNicholas Bellinger 	unsigned long flags;
179d050ffb9SNicholas Bellinger 	int fe_count;
180c66ac9dbSNicholas Bellinger 	/*
181c66ac9dbSNicholas Bellinger 	 * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
182c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
183c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
184c66ac9dbSNicholas Bellinger 	 *
185c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
186c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
187c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
188c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
189c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
190c66ac9dbSNicholas Bellinger 	 *
191c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
192c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
193c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
194c66ac9dbSNicholas Bellinger 	 *
195c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
196c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
197c66ac9dbSNicholas Bellinger 	 *
198c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
199c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
200c66ac9dbSNicholas Bellinger 	 */
201c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&dev->execute_task_lock, flags);
202c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
203c66ac9dbSNicholas Bellinger 				t_state_list) {
204e3d6f909SAndy Grover 		if (!task->task_se_cmd) {
2056708bb27SAndy Grover 			pr_err("task->task_se_cmd is NULL!\n");
206c66ac9dbSNicholas Bellinger 			continue;
207c66ac9dbSNicholas Bellinger 		}
208e3d6f909SAndy Grover 		cmd = task->task_se_cmd;
209c66ac9dbSNicholas Bellinger 
210c66ac9dbSNicholas Bellinger 		/*
211c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
212c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
213c66ac9dbSNicholas Bellinger 		 */
2146708bb27SAndy Grover 		if (preempt_and_abort_list &&
215c66ac9dbSNicholas Bellinger 		    (core_scsi3_check_cdb_abort_and_preempt(
216c66ac9dbSNicholas Bellinger 					preempt_and_abort_list, cmd) != 0))
217c66ac9dbSNicholas Bellinger 			continue;
218c66ac9dbSNicholas Bellinger 		/*
219c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
220c66ac9dbSNicholas Bellinger 		 */
221c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
222c66ac9dbSNicholas Bellinger 			continue;
223c66ac9dbSNicholas Bellinger 
224d050ffb9SNicholas Bellinger 		list_move_tail(&task->t_state_list, &drain_task_list);
225c66ac9dbSNicholas Bellinger 		atomic_set(&task->task_state_active, 0);
226d050ffb9SNicholas Bellinger 		/*
227d050ffb9SNicholas Bellinger 		 * Remove from task execute list before processing drain_task_list
228d050ffb9SNicholas Bellinger 		 */
229d050ffb9SNicholas Bellinger 		if (atomic_read(&task->task_execute_queue) != 0) {
230d050ffb9SNicholas Bellinger 			list_del(&task->t_execute_list);
231d050ffb9SNicholas Bellinger 			atomic_set(&task->task_execute_queue, 0);
232d050ffb9SNicholas Bellinger 			atomic_dec(&dev->execute_tasks);
233d050ffb9SNicholas Bellinger 		}
234d050ffb9SNicholas Bellinger 	}
235c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
236c66ac9dbSNicholas Bellinger 
237d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
238d050ffb9SNicholas Bellinger 		task = list_entry(drain_task_list.next, struct se_task, t_state_list);
239d050ffb9SNicholas Bellinger 		list_del(&task->t_state_list);
240d050ffb9SNicholas Bellinger 		cmd = task->task_se_cmd;
241d050ffb9SNicholas Bellinger 
242a1d8b49aSAndy Grover 		spin_lock_irqsave(&cmd->t_state_lock, flags);
2436708bb27SAndy Grover 		pr_debug("LUN_RESET: %s cmd: %p task: %p"
244c66ac9dbSNicholas Bellinger 			" ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state/"
245c66ac9dbSNicholas Bellinger 			"def_t_state: %d/%d cdb: 0x%02x\n",
246c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "", cmd, task,
247e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), 0,
248e3d6f909SAndy Grover 			cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
249a1d8b49aSAndy Grover 			cmd->deferred_t_state, cmd->t_task_cdb[0]);
2506708bb27SAndy Grover 		pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
251c66ac9dbSNicholas Bellinger 			" t_task_cdbs: %d t_task_cdbs_left: %d"
252c66ac9dbSNicholas Bellinger 			" t_task_cdbs_sent: %d -- t_transport_active: %d"
253c66ac9dbSNicholas Bellinger 			" t_transport_stop: %d t_transport_sent: %d\n",
254e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
255a1d8b49aSAndy Grover 			cmd->t_task_list_num,
256a1d8b49aSAndy Grover 			atomic_read(&cmd->t_task_cdbs_left),
257a1d8b49aSAndy Grover 			atomic_read(&cmd->t_task_cdbs_sent),
258a1d8b49aSAndy Grover 			atomic_read(&cmd->t_transport_active),
259a1d8b49aSAndy Grover 			atomic_read(&cmd->t_transport_stop),
260a1d8b49aSAndy Grover 			atomic_read(&cmd->t_transport_sent));
261c66ac9dbSNicholas Bellinger 
262c66ac9dbSNicholas Bellinger 		if (atomic_read(&task->task_active)) {
263c66ac9dbSNicholas Bellinger 			atomic_set(&task->task_stop, 1);
264c66ac9dbSNicholas Bellinger 			spin_unlock_irqrestore(
265a1d8b49aSAndy Grover 				&cmd->t_state_lock, flags);
266c66ac9dbSNicholas Bellinger 
2676708bb27SAndy Grover 			pr_debug("LUN_RESET: Waiting for task: %p to shutdown"
268c66ac9dbSNicholas Bellinger 				" for dev: %p\n", task, dev);
269c66ac9dbSNicholas Bellinger 			wait_for_completion(&task->task_stop_comp);
2706708bb27SAndy Grover 			pr_debug("LUN_RESET Completed task: %p shutdown for"
271c66ac9dbSNicholas Bellinger 				" dev: %p\n", task, dev);
272a1d8b49aSAndy Grover 			spin_lock_irqsave(&cmd->t_state_lock, flags);
273a1d8b49aSAndy Grover 			atomic_dec(&cmd->t_task_cdbs_left);
274c66ac9dbSNicholas Bellinger 
275c66ac9dbSNicholas Bellinger 			atomic_set(&task->task_active, 0);
276c66ac9dbSNicholas Bellinger 			atomic_set(&task->task_stop, 0);
277c66ac9dbSNicholas Bellinger 		}
278c66ac9dbSNicholas Bellinger 		__transport_stop_task_timer(task, &flags);
279c66ac9dbSNicholas Bellinger 
2806708bb27SAndy Grover 		if (!atomic_dec_and_test(&cmd->t_task_cdbs_ex_left)) {
281d050ffb9SNicholas Bellinger 			spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2826708bb27SAndy Grover 			pr_debug("LUN_RESET: Skipping task: %p, dev: %p for"
283c66ac9dbSNicholas Bellinger 				" t_task_cdbs_ex_left: %d\n", task, dev,
284a1d8b49aSAndy Grover 				atomic_read(&cmd->t_task_cdbs_ex_left));
285c66ac9dbSNicholas Bellinger 			continue;
286c66ac9dbSNicholas Bellinger 		}
287a1d8b49aSAndy Grover 		fe_count = atomic_read(&cmd->t_fe_count);
288c66ac9dbSNicholas Bellinger 
289a1d8b49aSAndy Grover 		if (atomic_read(&cmd->t_transport_active)) {
2906708bb27SAndy Grover 			pr_debug("LUN_RESET: got t_transport_active = 1 for"
291c66ac9dbSNicholas Bellinger 				" task: %p, t_fe_count: %d dev: %p\n", task,
292c66ac9dbSNicholas Bellinger 				fe_count, dev);
293a1d8b49aSAndy Grover 			atomic_set(&cmd->t_transport_aborted, 1);
294d050ffb9SNicholas Bellinger 			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 			continue;
298c66ac9dbSNicholas Bellinger 		}
2996708bb27SAndy Grover 		pr_debug("LUN_RESET: Got t_transport_active = 0 for task: %p,"
300c66ac9dbSNicholas Bellinger 			" t_fe_count: %d dev: %p\n", task, fe_count, dev);
301a1d8b49aSAndy Grover 		atomic_set(&cmd->t_transport_aborted, 1);
302a1d8b49aSAndy Grover 		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
303c66ac9dbSNicholas Bellinger 
304d050ffb9SNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
305c66ac9dbSNicholas Bellinger 	}
306d050ffb9SNicholas Bellinger }
307d050ffb9SNicholas Bellinger 
308d050ffb9SNicholas Bellinger static void core_tmr_drain_cmd_list(
309d050ffb9SNicholas Bellinger 	struct se_device *dev,
310d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
311d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
312d050ffb9SNicholas Bellinger 	int tas,
313d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
314d050ffb9SNicholas Bellinger {
315d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_cmd_list);
316d050ffb9SNicholas Bellinger 	struct se_queue_obj *qobj = &dev->dev_queue_obj;
317d050ffb9SNicholas Bellinger 	struct se_cmd *cmd, *tcmd;
318d050ffb9SNicholas Bellinger 	unsigned long flags;
319c66ac9dbSNicholas Bellinger 	/*
320c66ac9dbSNicholas Bellinger 	 * Release all commands remaining in the struct se_device cmd queue.
321c66ac9dbSNicholas Bellinger 	 *
322c66ac9dbSNicholas Bellinger 	 * This follows the same logic as above for the struct se_device
323c66ac9dbSNicholas Bellinger 	 * struct se_task state list, where commands are returned with
324c66ac9dbSNicholas Bellinger 	 * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
325c66ac9dbSNicholas Bellinger 	 * reference, otherwise the struct se_cmd is released.
326c66ac9dbSNicholas Bellinger 	 */
327c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
3285951146dSAndy Grover 	list_for_each_entry_safe(cmd, tcmd, &qobj->qobj_list, se_queue_node) {
329c66ac9dbSNicholas Bellinger 		/*
330c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
331c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
332c66ac9dbSNicholas Bellinger 		 */
3336708bb27SAndy Grover 		if (preempt_and_abort_list &&
334c66ac9dbSNicholas Bellinger 		    (core_scsi3_check_cdb_abort_and_preempt(
335c66ac9dbSNicholas Bellinger 					preempt_and_abort_list, cmd) != 0))
336c66ac9dbSNicholas Bellinger 			continue;
337c66ac9dbSNicholas Bellinger 		/*
338c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
339c66ac9dbSNicholas Bellinger 		 */
340c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
341c66ac9dbSNicholas Bellinger 			continue;
342c66ac9dbSNicholas Bellinger 
343d050ffb9SNicholas Bellinger 		atomic_set(&cmd->t_transport_queue_active, 0);
344c66ac9dbSNicholas Bellinger 		atomic_dec(&qobj->queue_cnt);
345d050ffb9SNicholas Bellinger 		list_move_tail(&cmd->se_queue_node, &drain_cmd_list);
346d050ffb9SNicholas Bellinger 	}
347c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
348c66ac9dbSNicholas Bellinger 
349d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_cmd_list)) {
350d050ffb9SNicholas Bellinger 		cmd = list_entry(drain_cmd_list.next, struct se_cmd, se_queue_node);
351d050ffb9SNicholas Bellinger 		list_del_init(&cmd->se_queue_node);
352d050ffb9SNicholas Bellinger 
3536708bb27SAndy Grover 		pr_debug("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
354c66ac9dbSNicholas Bellinger 			" %d t_fe_count: %d\n", (preempt_and_abort_list) ?
3555951146dSAndy Grover 			"Preempt" : "", cmd, cmd->t_state,
356a1d8b49aSAndy Grover 			atomic_read(&cmd->t_fe_count));
357c66ac9dbSNicholas Bellinger 		/*
358c66ac9dbSNicholas Bellinger 		 * Signal that the command has failed via cmd->se_cmd_flags,
359c66ac9dbSNicholas Bellinger 		 */
360c66ac9dbSNicholas Bellinger 		transport_new_cmd_failure(cmd);
361c66ac9dbSNicholas Bellinger 
362c66ac9dbSNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
363a1d8b49aSAndy Grover 				atomic_read(&cmd->t_fe_count));
364c66ac9dbSNicholas Bellinger 	}
365d050ffb9SNicholas Bellinger }
366d050ffb9SNicholas Bellinger 
367d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
368d050ffb9SNicholas Bellinger         struct se_device *dev,
369d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
370d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
371d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
372d050ffb9SNicholas Bellinger {
373d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
374d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
375d050ffb9SNicholas Bellinger 	int tas;
376d050ffb9SNicholas Bellinger         /*
377d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
378d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
379d050ffb9SNicholas Bellinger 	 *
380d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
381d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
382d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
383d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
384d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
385d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
386d050ffb9SNicholas Bellinger 	 */
387d050ffb9SNicholas Bellinger 	tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
388d050ffb9SNicholas Bellinger 	/*
389d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
390d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
391d050ffb9SNicholas Bellinger 	 */
392d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
393d050ffb9SNicholas Bellinger 		tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
394d050ffb9SNicholas Bellinger 		tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
395d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
396d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
397d050ffb9SNicholas Bellinger 				" initiator port %s\n",
398d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
399d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
400d050ffb9SNicholas Bellinger 		}
401d050ffb9SNicholas Bellinger 	}
402d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
403d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
404d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
405d050ffb9SNicholas Bellinger 
406d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
407d050ffb9SNicholas Bellinger 	core_tmr_drain_task_list(dev, prout_cmd, tmr_nacl, tas,
408d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
409d050ffb9SNicholas Bellinger 	core_tmr_drain_cmd_list(dev, prout_cmd, tmr_nacl, tas,
410d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
411c66ac9dbSNicholas Bellinger 	/*
412c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
413c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
414c66ac9dbSNicholas Bellinger 	 */
4156708bb27SAndy Grover 	if (!preempt_and_abort_list &&
416c66ac9dbSNicholas Bellinger 	     (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
417c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
418c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
419c66ac9dbSNicholas Bellinger 		dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
420c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
4216708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
422c66ac9dbSNicholas Bellinger 	}
423c66ac9dbSNicholas Bellinger 
4241e7de68cSNicholas Bellinger 	spin_lock_irq(&dev->stats_lock);
425c66ac9dbSNicholas Bellinger 	dev->num_resets++;
4261e7de68cSNicholas Bellinger 	spin_unlock_irq(&dev->stats_lock);
427c66ac9dbSNicholas Bellinger 
4286708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
429c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
430e3d6f909SAndy Grover 			dev->transport->name);
431c66ac9dbSNicholas Bellinger 	return 0;
432c66ac9dbSNicholas Bellinger }
433d050ffb9SNicholas Bellinger 
434