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 
41c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
42c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
43c66ac9dbSNicholas Bellinger 
44c66ac9dbSNicholas Bellinger struct se_tmr_req *core_tmr_alloc_req(
45c66ac9dbSNicholas Bellinger 	struct se_cmd *se_cmd,
46c66ac9dbSNicholas Bellinger 	void *fabric_tmr_ptr,
47dd503a5fSRoland Dreier 	u8 function,
48dd503a5fSRoland Dreier 	gfp_t gfp_flags)
49c66ac9dbSNicholas Bellinger {
50c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr;
51c66ac9dbSNicholas Bellinger 
52dd503a5fSRoland Dreier 	tmr = kmem_cache_zalloc(se_tmr_req_cache, gfp_flags);
536708bb27SAndy Grover 	if (!tmr) {
546708bb27SAndy Grover 		pr_err("Unable to allocate struct se_tmr_req\n");
55c66ac9dbSNicholas Bellinger 		return ERR_PTR(-ENOMEM);
56c66ac9dbSNicholas Bellinger 	}
57c66ac9dbSNicholas Bellinger 	tmr->task_cmd = se_cmd;
58c66ac9dbSNicholas Bellinger 	tmr->fabric_tmr_ptr = fabric_tmr_ptr;
59c66ac9dbSNicholas Bellinger 	tmr->function = function;
60c66ac9dbSNicholas Bellinger 	INIT_LIST_HEAD(&tmr->tmr_list);
61c66ac9dbSNicholas Bellinger 
62c66ac9dbSNicholas Bellinger 	return tmr;
63c66ac9dbSNicholas Bellinger }
64c66ac9dbSNicholas Bellinger EXPORT_SYMBOL(core_tmr_alloc_req);
65c66ac9dbSNicholas Bellinger 
66c66ac9dbSNicholas Bellinger void core_tmr_release_req(
67c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr)
68c66ac9dbSNicholas Bellinger {
69c66ac9dbSNicholas Bellinger 	struct se_device *dev = tmr->tmr_dev;
70d050ffb9SNicholas Bellinger 	unsigned long flags;
71c66ac9dbSNicholas Bellinger 
727fd29aa9SNicholas Bellinger 	if (!dev) {
737fd29aa9SNicholas Bellinger 		kmem_cache_free(se_tmr_req_cache, tmr);
747fd29aa9SNicholas Bellinger 		return;
757fd29aa9SNicholas Bellinger 	}
767fd29aa9SNicholas Bellinger 
77d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
78c66ac9dbSNicholas Bellinger 	list_del(&tmr->tmr_list);
79d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
807fd29aa9SNicholas Bellinger 
817fd29aa9SNicholas Bellinger 	kmem_cache_free(se_tmr_req_cache, tmr);
82c66ac9dbSNicholas Bellinger }
83c66ac9dbSNicholas Bellinger 
84c66ac9dbSNicholas Bellinger static void core_tmr_handle_tas_abort(
85c66ac9dbSNicholas Bellinger 	struct se_node_acl *tmr_nacl,
86c66ac9dbSNicholas Bellinger 	struct se_cmd *cmd,
87c66ac9dbSNicholas Bellinger 	int tas,
88c66ac9dbSNicholas Bellinger 	int fe_count)
89c66ac9dbSNicholas Bellinger {
906708bb27SAndy Grover 	if (!fe_count) {
91c66ac9dbSNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
92c66ac9dbSNicholas Bellinger 		return;
93c66ac9dbSNicholas Bellinger 	}
94c66ac9dbSNicholas Bellinger 	/*
95c66ac9dbSNicholas Bellinger 	 * TASK ABORTED status (TAS) bit support
96c66ac9dbSNicholas Bellinger 	*/
976708bb27SAndy Grover 	if ((tmr_nacl &&
98c66ac9dbSNicholas Bellinger 	     (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
99c66ac9dbSNicholas Bellinger 		transport_send_task_abort(cmd);
100c66ac9dbSNicholas Bellinger 
101c66ac9dbSNicholas Bellinger 	transport_cmd_finish_abort(cmd, 0);
102c66ac9dbSNicholas Bellinger }
103c66ac9dbSNicholas Bellinger 
104d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
105c66ac9dbSNicholas Bellinger 	struct se_device *dev,
106c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
107d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
108c66ac9dbSNicholas Bellinger {
109d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
110c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
111d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
112c66ac9dbSNicholas Bellinger 	unsigned long flags;
113c66ac9dbSNicholas Bellinger 	/*
114c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
115c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
116c66ac9dbSNicholas Bellinger 	 */
117d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
118c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
119c66ac9dbSNicholas Bellinger 		/*
120c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
121c66ac9dbSNicholas Bellinger 		 */
122c66ac9dbSNicholas Bellinger 		if (tmr && (tmr_p == tmr))
123c66ac9dbSNicholas Bellinger 			continue;
124c66ac9dbSNicholas Bellinger 
125c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
1266708bb27SAndy Grover 		if (!cmd) {
1276708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
128c66ac9dbSNicholas Bellinger 			continue;
129c66ac9dbSNicholas Bellinger 		}
130c66ac9dbSNicholas Bellinger 		/*
131c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
132c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
133c66ac9dbSNicholas Bellinger 		 * skip non regisration key matching TMRs.
134c66ac9dbSNicholas Bellinger 		 */
1356708bb27SAndy Grover 		if (preempt_and_abort_list &&
136c66ac9dbSNicholas Bellinger 		    (core_scsi3_check_cdb_abort_and_preempt(
137c66ac9dbSNicholas Bellinger 					preempt_and_abort_list, cmd) != 0))
138c66ac9dbSNicholas Bellinger 			continue;
139c66ac9dbSNicholas Bellinger 
140d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
1416708bb27SAndy Grover 		if (!atomic_read(&cmd->t_transport_active)) {
142d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
143c66ac9dbSNicholas Bellinger 			continue;
144c66ac9dbSNicholas Bellinger 		}
145c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
146d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
147c66ac9dbSNicholas Bellinger 			continue;
148c66ac9dbSNicholas Bellinger 		}
149d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
150d050ffb9SNicholas Bellinger 
151d050ffb9SNicholas Bellinger 		list_move_tail(&tmr->tmr_list, &drain_tmr_list);
152d050ffb9SNicholas Bellinger 	}
153d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
154d050ffb9SNicholas Bellinger 
155d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_tmr_list)) {
156d050ffb9SNicholas Bellinger 		tmr = list_entry(drain_tmr_list.next, struct se_tmr_req, tmr_list);
157d050ffb9SNicholas Bellinger 		list_del(&tmr->tmr_list);
158d050ffb9SNicholas Bellinger 		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",
162d050ffb9SNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "", tmr,
163d050ffb9SNicholas Bellinger 			tmr->function, tmr->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;
334b0e062aeSNicholas Bellinger 		/*
335b0e062aeSNicholas Bellinger 		 * Skip direct processing of TRANSPORT_FREE_CMD_INTR for
336b0e062aeSNicholas Bellinger 		 * HW target mode fabrics.
337b0e062aeSNicholas Bellinger 		 */
338b0e062aeSNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
339b0e062aeSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_FREE_CMD_INTR) {
340b0e062aeSNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
341b0e062aeSNicholas Bellinger 			continue;
342b0e062aeSNicholas Bellinger 		}
343b0e062aeSNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
344c66ac9dbSNicholas Bellinger 
345d050ffb9SNicholas Bellinger 		atomic_set(&cmd->t_transport_queue_active, 0);
346c66ac9dbSNicholas Bellinger 		atomic_dec(&qobj->queue_cnt);
347d050ffb9SNicholas Bellinger 		list_move_tail(&cmd->se_queue_node, &drain_cmd_list);
348d050ffb9SNicholas Bellinger 	}
349c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
350c66ac9dbSNicholas Bellinger 
351d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_cmd_list)) {
352d050ffb9SNicholas Bellinger 		cmd = list_entry(drain_cmd_list.next, struct se_cmd, se_queue_node);
353d050ffb9SNicholas Bellinger 		list_del_init(&cmd->se_queue_node);
354d050ffb9SNicholas Bellinger 
3556708bb27SAndy Grover 		pr_debug("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
356c66ac9dbSNicholas Bellinger 			" %d t_fe_count: %d\n", (preempt_and_abort_list) ?
3575951146dSAndy Grover 			"Preempt" : "", cmd, cmd->t_state,
358a1d8b49aSAndy Grover 			atomic_read(&cmd->t_fe_count));
359c66ac9dbSNicholas Bellinger 		/*
360c66ac9dbSNicholas Bellinger 		 * Signal that the command has failed via cmd->se_cmd_flags,
361c66ac9dbSNicholas Bellinger 		 */
362c66ac9dbSNicholas Bellinger 		transport_new_cmd_failure(cmd);
363c66ac9dbSNicholas Bellinger 
364c66ac9dbSNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
365a1d8b49aSAndy Grover 				atomic_read(&cmd->t_fe_count));
366c66ac9dbSNicholas Bellinger 	}
367d050ffb9SNicholas Bellinger }
368d050ffb9SNicholas Bellinger 
369d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
370d050ffb9SNicholas Bellinger         struct se_device *dev,
371d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
372d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
373d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
374d050ffb9SNicholas Bellinger {
375d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
376d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
377d050ffb9SNicholas Bellinger 	int tas;
378d050ffb9SNicholas Bellinger         /*
379d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
380d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
381d050ffb9SNicholas Bellinger 	 *
382d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
383d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
384d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
385d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
386d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
387d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
388d050ffb9SNicholas Bellinger 	 */
389d050ffb9SNicholas Bellinger 	tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
390d050ffb9SNicholas Bellinger 	/*
391d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
392d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
393d050ffb9SNicholas Bellinger 	 */
394d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
395d050ffb9SNicholas Bellinger 		tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
396d050ffb9SNicholas Bellinger 		tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
397d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
398d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
399d050ffb9SNicholas Bellinger 				" initiator port %s\n",
400d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
401d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
402d050ffb9SNicholas Bellinger 		}
403d050ffb9SNicholas Bellinger 	}
404d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
405d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
406d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
407d050ffb9SNicholas Bellinger 
408d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
409d050ffb9SNicholas Bellinger 	core_tmr_drain_task_list(dev, prout_cmd, tmr_nacl, tas,
410d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
411d050ffb9SNicholas Bellinger 	core_tmr_drain_cmd_list(dev, prout_cmd, tmr_nacl, tas,
412d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
413c66ac9dbSNicholas Bellinger 	/*
414c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
415c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
416c66ac9dbSNicholas Bellinger 	 */
4176708bb27SAndy Grover 	if (!preempt_and_abort_list &&
418c66ac9dbSNicholas Bellinger 	     (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
419c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
420c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
421c66ac9dbSNicholas Bellinger 		dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
422c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
4236708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
424c66ac9dbSNicholas Bellinger 	}
425c66ac9dbSNicholas Bellinger 
4261e7de68cSNicholas Bellinger 	spin_lock_irq(&dev->stats_lock);
427c66ac9dbSNicholas Bellinger 	dev->num_resets++;
4281e7de68cSNicholas Bellinger 	spin_unlock_irq(&dev->stats_lock);
429c66ac9dbSNicholas Bellinger 
4306708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
431c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
432e3d6f909SAndy Grover 			dev->transport->name);
433c66ac9dbSNicholas Bellinger 	return 0;
434c66ac9dbSNicholas Bellinger }
435d050ffb9SNicholas Bellinger 
436