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>
35c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
36c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
37c66ac9dbSNicholas Bellinger #include <target/target_core_configfs.h>
38c66ac9dbSNicholas Bellinger 
39e26d99aeSChristoph Hellwig #include "target_core_internal.h"
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,
46dd503a5fSRoland Dreier 	u8 function,
47dd503a5fSRoland Dreier 	gfp_t gfp_flags)
48c66ac9dbSNicholas Bellinger {
49c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr;
50c66ac9dbSNicholas Bellinger 
51dd503a5fSRoland Dreier 	tmr = kmem_cache_zalloc(se_tmr_req_cache, gfp_flags);
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 
103feae8564SJörn Engel static int target_check_cdb_and_preempt(struct list_head *list,
104c165f69cSJörn Engel 		struct se_cmd *cmd)
105c165f69cSJörn Engel {
106feae8564SJörn Engel 	struct t10_pr_registration *reg;
107c165f69cSJörn Engel 
108feae8564SJörn Engel 	if (!list)
109feae8564SJörn Engel 		return 0;
110feae8564SJörn Engel 	list_for_each_entry(reg, list, pr_reg_abort_list) {
111feae8564SJörn Engel 		if (reg->pr_res_key == cmd->pr_res_key)
112c165f69cSJörn Engel 			return 0;
113c165f69cSJörn Engel 	}
114c165f69cSJörn Engel 
115c165f69cSJörn Engel 	return 1;
116c165f69cSJörn Engel }
117c165f69cSJörn Engel 
118d050ffb9SNicholas Bellinger static void core_tmr_drain_tmr_list(
119c66ac9dbSNicholas Bellinger 	struct se_device *dev,
120c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr,
121d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
122c66ac9dbSNicholas Bellinger {
123d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_tmr_list);
124c66ac9dbSNicholas Bellinger 	struct se_tmr_req *tmr_p, *tmr_pp;
125d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
126c66ac9dbSNicholas Bellinger 	unsigned long flags;
127c66ac9dbSNicholas Bellinger 	/*
128c66ac9dbSNicholas Bellinger 	 * Release all pending and outgoing TMRs aside from the received
129c66ac9dbSNicholas Bellinger 	 * LUN_RESET tmr..
130c66ac9dbSNicholas Bellinger 	 */
131d050ffb9SNicholas Bellinger 	spin_lock_irqsave(&dev->se_tmr_lock, flags);
132c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
133c66ac9dbSNicholas Bellinger 		/*
134c66ac9dbSNicholas Bellinger 		 * Allow the received TMR to return with FUNCTION_COMPLETE.
135c66ac9dbSNicholas Bellinger 		 */
136abc1fd4fSJoern Engel 		if (tmr_p == tmr)
137c66ac9dbSNicholas Bellinger 			continue;
138c66ac9dbSNicholas Bellinger 
139c66ac9dbSNicholas Bellinger 		cmd = tmr_p->task_cmd;
1406708bb27SAndy Grover 		if (!cmd) {
1416708bb27SAndy Grover 			pr_err("Unable to locate struct se_cmd for TMR\n");
142c66ac9dbSNicholas Bellinger 			continue;
143c66ac9dbSNicholas Bellinger 		}
144c66ac9dbSNicholas Bellinger 		/*
145c66ac9dbSNicholas Bellinger 		 * If this function was called with a valid pr_res_key
146c66ac9dbSNicholas Bellinger 		 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
147c66ac9dbSNicholas Bellinger 		 * skip non regisration key matching TMRs.
148c66ac9dbSNicholas Bellinger 		 */
149feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
150c66ac9dbSNicholas Bellinger 			continue;
151c66ac9dbSNicholas Bellinger 
152d050ffb9SNicholas Bellinger 		spin_lock(&cmd->t_state_lock);
1537d680f3bSChristoph Hellwig 		if (!(cmd->transport_state & CMD_T_ACTIVE)) {
154d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
155c66ac9dbSNicholas Bellinger 			continue;
156c66ac9dbSNicholas Bellinger 		}
157c66ac9dbSNicholas Bellinger 		if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
158d050ffb9SNicholas Bellinger 			spin_unlock(&cmd->t_state_lock);
159c66ac9dbSNicholas Bellinger 			continue;
160c66ac9dbSNicholas Bellinger 		}
161d050ffb9SNicholas Bellinger 		spin_unlock(&cmd->t_state_lock);
162d050ffb9SNicholas Bellinger 
1636eb40b2aSJoern Engel 		list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
164d050ffb9SNicholas Bellinger 	}
165d050ffb9SNicholas Bellinger 	spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
166d050ffb9SNicholas Bellinger 
167abc1fd4fSJoern Engel 	list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
168b8a11d73SJoern Engel 		list_del_init(&tmr_p->tmr_list);
169abc1fd4fSJoern Engel 		cmd = tmr_p->task_cmd;
170d050ffb9SNicholas Bellinger 
1716708bb27SAndy Grover 		pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
172c66ac9dbSNicholas Bellinger 			" Response: 0x%02x, t_state: %d\n",
173abc1fd4fSJoern Engel 			(preempt_and_abort_list) ? "Preempt" : "", tmr_p,
174abc1fd4fSJoern Engel 			tmr_p->function, tmr_p->response, cmd->t_state);
175c66ac9dbSNicholas Bellinger 
1768dc52b54SNicholas Bellinger 		transport_cmd_finish_abort(cmd, 1);
177c66ac9dbSNicholas Bellinger 	}
178d050ffb9SNicholas Bellinger }
179d050ffb9SNicholas Bellinger 
180d050ffb9SNicholas Bellinger static void core_tmr_drain_task_list(
181d050ffb9SNicholas Bellinger 	struct se_device *dev,
182d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
183d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
184d050ffb9SNicholas Bellinger 	int tas,
185d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
186d050ffb9SNicholas Bellinger {
187d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_task_list);
188d050ffb9SNicholas Bellinger 	struct se_cmd *cmd;
189d050ffb9SNicholas Bellinger 	struct se_task *task, *task_tmp;
190d050ffb9SNicholas Bellinger 	unsigned long flags;
191d050ffb9SNicholas Bellinger 	int fe_count;
192c66ac9dbSNicholas Bellinger 	/*
193c66ac9dbSNicholas Bellinger 	 * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
194c66ac9dbSNicholas Bellinger 	 * This is following sam4r17, section 5.6 Aborting commands, Table 38
195c66ac9dbSNicholas Bellinger 	 * for TMR LUN_RESET:
196c66ac9dbSNicholas Bellinger 	 *
197c66ac9dbSNicholas Bellinger 	 * a) "Yes" indicates that each command that is aborted on an I_T nexus
198c66ac9dbSNicholas Bellinger 	 * other than the one that caused the SCSI device condition is
199c66ac9dbSNicholas Bellinger 	 * completed with TASK ABORTED status, if the TAS bit is set to one in
200c66ac9dbSNicholas Bellinger 	 * the Control mode page (see SPC-4). "No" indicates that no status is
201c66ac9dbSNicholas Bellinger 	 * returned for aborted commands.
202c66ac9dbSNicholas Bellinger 	 *
203c66ac9dbSNicholas Bellinger 	 * d) If the logical unit reset is caused by a particular I_T nexus
204c66ac9dbSNicholas Bellinger 	 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
205c66ac9dbSNicholas Bellinger 	 * (TASK_ABORTED status) applies.
206c66ac9dbSNicholas Bellinger 	 *
207c66ac9dbSNicholas Bellinger 	 * Otherwise (e.g., if triggered by a hard reset), "no"
208c66ac9dbSNicholas Bellinger 	 * (no TASK_ABORTED SAM status) applies.
209c66ac9dbSNicholas Bellinger 	 *
210c66ac9dbSNicholas Bellinger 	 * Note that this seems to be independent of TAS (Task Aborted Status)
211c66ac9dbSNicholas Bellinger 	 * in the Control Mode Page.
212c66ac9dbSNicholas Bellinger 	 */
213c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&dev->execute_task_lock, flags);
214c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
215c66ac9dbSNicholas Bellinger 				t_state_list) {
216e3d6f909SAndy Grover 		if (!task->task_se_cmd) {
2176708bb27SAndy Grover 			pr_err("task->task_se_cmd is NULL!\n");
218c66ac9dbSNicholas Bellinger 			continue;
219c66ac9dbSNicholas Bellinger 		}
220e3d6f909SAndy Grover 		cmd = task->task_se_cmd;
221c66ac9dbSNicholas Bellinger 
222c66ac9dbSNicholas Bellinger 		/*
223c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
224c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
225c66ac9dbSNicholas Bellinger 		 */
226feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
227c66ac9dbSNicholas Bellinger 			continue;
228c66ac9dbSNicholas Bellinger 		/*
229c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
230c66ac9dbSNicholas Bellinger 		 */
231c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
232c66ac9dbSNicholas Bellinger 			continue;
233c66ac9dbSNicholas Bellinger 
234d050ffb9SNicholas Bellinger 		list_move_tail(&task->t_state_list, &drain_task_list);
2351880807aSChristoph Hellwig 		task->t_state_active = false;
236d050ffb9SNicholas Bellinger 		/*
237d050ffb9SNicholas Bellinger 		 * Remove from task execute list before processing drain_task_list
238d050ffb9SNicholas Bellinger 		 */
23904629b7bSChristoph Hellwig 		if (!list_empty(&task->t_execute_list))
24004629b7bSChristoph Hellwig 			__transport_remove_task_from_execute_queue(task, dev);
241d050ffb9SNicholas Bellinger 	}
242c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&dev->execute_task_lock, flags);
243c66ac9dbSNicholas Bellinger 
244d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_task_list)) {
245d050ffb9SNicholas Bellinger 		task = list_entry(drain_task_list.next, struct se_task, t_state_list);
246d050ffb9SNicholas Bellinger 		list_del(&task->t_state_list);
247d050ffb9SNicholas Bellinger 		cmd = task->task_se_cmd;
248d050ffb9SNicholas Bellinger 
2496708bb27SAndy Grover 		pr_debug("LUN_RESET: %s cmd: %p task: %p"
250f2da9dbdSChristoph Hellwig 			" ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state: %d"
251f2da9dbdSChristoph Hellwig 			"cdb: 0x%02x\n",
252c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "", cmd, task,
253e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), 0,
254e3d6f909SAndy Grover 			cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
255f2da9dbdSChristoph Hellwig 			cmd->t_task_cdb[0]);
2566708bb27SAndy Grover 		pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
257c66ac9dbSNicholas Bellinger 			" t_task_cdbs: %d t_task_cdbs_left: %d"
2587d680f3bSChristoph Hellwig 			" t_task_cdbs_sent: %d -- CMD_T_ACTIVE: %d"
2597d680f3bSChristoph Hellwig 			" CMD_T_STOP: %d CMD_T_SENT: %d\n",
260e3d6f909SAndy Grover 			cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
261a1d8b49aSAndy Grover 			cmd->t_task_list_num,
262a1d8b49aSAndy Grover 			atomic_read(&cmd->t_task_cdbs_left),
263a1d8b49aSAndy Grover 			atomic_read(&cmd->t_task_cdbs_sent),
2647d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_ACTIVE) != 0,
2657d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_STOP) != 0,
2667d680f3bSChristoph Hellwig 			(cmd->transport_state & CMD_T_SENT) != 0);
267c66ac9dbSNicholas Bellinger 
26835e0e757SChristoph Hellwig 		/*
26935e0e757SChristoph Hellwig 		 * If the command may be queued onto a workqueue cancel it now.
27035e0e757SChristoph Hellwig 		 *
27135e0e757SChristoph Hellwig 		 * This is equivalent to removal from the execute queue in the
27235e0e757SChristoph Hellwig 		 * loop above, but we do it down here given that
27335e0e757SChristoph Hellwig 		 * cancel_work_sync may block.
27435e0e757SChristoph Hellwig 		 */
27535e0e757SChristoph Hellwig 		if (cmd->t_state == TRANSPORT_COMPLETE)
27635e0e757SChristoph Hellwig 			cancel_work_sync(&cmd->work);
27735e0e757SChristoph Hellwig 
278a1d8b49aSAndy Grover 		spin_lock_irqsave(&cmd->t_state_lock, flags);
279cdbb70bbSChristoph Hellwig 		target_stop_task(task, &flags);
280c66ac9dbSNicholas Bellinger 
2816708bb27SAndy Grover 		if (!atomic_dec_and_test(&cmd->t_task_cdbs_ex_left)) {
282d050ffb9SNicholas Bellinger 			spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2836708bb27SAndy Grover 			pr_debug("LUN_RESET: Skipping task: %p, dev: %p for"
284c66ac9dbSNicholas Bellinger 				" t_task_cdbs_ex_left: %d\n", task, dev,
285a1d8b49aSAndy Grover 				atomic_read(&cmd->t_task_cdbs_ex_left));
286c66ac9dbSNicholas Bellinger 			continue;
287c66ac9dbSNicholas Bellinger 		}
288a1d8b49aSAndy Grover 		fe_count = atomic_read(&cmd->t_fe_count);
289c66ac9dbSNicholas Bellinger 
2907d680f3bSChristoph Hellwig 		if (!(cmd->transport_state & CMD_T_ACTIVE)) {
2917d680f3bSChristoph Hellwig 			pr_debug("LUN_RESET: got CMD_T_ACTIVE for"
292c66ac9dbSNicholas Bellinger 				" task: %p, t_fe_count: %d dev: %p\n", task,
293c66ac9dbSNicholas Bellinger 				fe_count, dev);
2947d680f3bSChristoph Hellwig 			cmd->transport_state |= CMD_T_ABORTED;
295d050ffb9SNicholas Bellinger 			spin_unlock_irqrestore(&cmd->t_state_lock, flags);
296c66ac9dbSNicholas Bellinger 
297d050ffb9SNicholas Bellinger 			core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
298c66ac9dbSNicholas Bellinger 			continue;
299c66ac9dbSNicholas Bellinger 		}
3007d680f3bSChristoph Hellwig 		pr_debug("LUN_RESET: Got !CMD_T_ACTIVE for task: %p,"
301c66ac9dbSNicholas Bellinger 			" t_fe_count: %d dev: %p\n", task, fe_count, dev);
3027d680f3bSChristoph Hellwig 		cmd->transport_state |= CMD_T_ABORTED;
303a1d8b49aSAndy Grover 		spin_unlock_irqrestore(&cmd->t_state_lock, flags);
304c66ac9dbSNicholas Bellinger 
305d050ffb9SNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
306c66ac9dbSNicholas Bellinger 	}
307d050ffb9SNicholas Bellinger }
308d050ffb9SNicholas Bellinger 
309d050ffb9SNicholas Bellinger static void core_tmr_drain_cmd_list(
310d050ffb9SNicholas Bellinger 	struct se_device *dev,
311d050ffb9SNicholas Bellinger 	struct se_cmd *prout_cmd,
312d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl,
313d050ffb9SNicholas Bellinger 	int tas,
314d050ffb9SNicholas Bellinger 	struct list_head *preempt_and_abort_list)
315d050ffb9SNicholas Bellinger {
316d050ffb9SNicholas Bellinger 	LIST_HEAD(drain_cmd_list);
317d050ffb9SNicholas Bellinger 	struct se_queue_obj *qobj = &dev->dev_queue_obj;
318d050ffb9SNicholas Bellinger 	struct se_cmd *cmd, *tcmd;
319d050ffb9SNicholas Bellinger 	unsigned long flags;
320c66ac9dbSNicholas Bellinger 	/*
321c66ac9dbSNicholas Bellinger 	 * Release all commands remaining in the struct se_device cmd queue.
322c66ac9dbSNicholas Bellinger 	 *
323c66ac9dbSNicholas Bellinger 	 * This follows the same logic as above for the struct se_device
324c66ac9dbSNicholas Bellinger 	 * struct se_task state list, where commands are returned with
325c66ac9dbSNicholas Bellinger 	 * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
326c66ac9dbSNicholas Bellinger 	 * reference, otherwise the struct se_cmd is released.
327c66ac9dbSNicholas Bellinger 	 */
328c66ac9dbSNicholas Bellinger 	spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
3295951146dSAndy Grover 	list_for_each_entry_safe(cmd, tcmd, &qobj->qobj_list, se_queue_node) {
330c66ac9dbSNicholas Bellinger 		/*
331c66ac9dbSNicholas Bellinger 		 * For PREEMPT_AND_ABORT usage, only process commands
332c66ac9dbSNicholas Bellinger 		 * with a matching reservation key.
333c66ac9dbSNicholas Bellinger 		 */
334feae8564SJörn Engel 		if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
335c66ac9dbSNicholas Bellinger 			continue;
336c66ac9dbSNicholas Bellinger 		/*
337c66ac9dbSNicholas Bellinger 		 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
338c66ac9dbSNicholas Bellinger 		 */
339c66ac9dbSNicholas Bellinger 		if (prout_cmd == cmd)
340c66ac9dbSNicholas Bellinger 			continue;
341c66ac9dbSNicholas Bellinger 
3427d680f3bSChristoph Hellwig 		cmd->transport_state &= ~CMD_T_QUEUED;
343c66ac9dbSNicholas Bellinger 		atomic_dec(&qobj->queue_cnt);
344d050ffb9SNicholas Bellinger 		list_move_tail(&cmd->se_queue_node, &drain_cmd_list);
345d050ffb9SNicholas Bellinger 	}
346c66ac9dbSNicholas Bellinger 	spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
347c66ac9dbSNicholas Bellinger 
348d050ffb9SNicholas Bellinger 	while (!list_empty(&drain_cmd_list)) {
349d050ffb9SNicholas Bellinger 		cmd = list_entry(drain_cmd_list.next, struct se_cmd, se_queue_node);
350d050ffb9SNicholas Bellinger 		list_del_init(&cmd->se_queue_node);
351d050ffb9SNicholas Bellinger 
3526708bb27SAndy Grover 		pr_debug("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
353c66ac9dbSNicholas Bellinger 			" %d t_fe_count: %d\n", (preempt_and_abort_list) ?
3545951146dSAndy Grover 			"Preempt" : "", cmd, cmd->t_state,
355a1d8b49aSAndy Grover 			atomic_read(&cmd->t_fe_count));
356c66ac9dbSNicholas Bellinger 
357c66ac9dbSNicholas Bellinger 		core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
358a1d8b49aSAndy Grover 				atomic_read(&cmd->t_fe_count));
359c66ac9dbSNicholas Bellinger 	}
360d050ffb9SNicholas Bellinger }
361d050ffb9SNicholas Bellinger 
362d050ffb9SNicholas Bellinger int core_tmr_lun_reset(
363d050ffb9SNicholas Bellinger         struct se_device *dev,
364d050ffb9SNicholas Bellinger         struct se_tmr_req *tmr,
365d050ffb9SNicholas Bellinger         struct list_head *preempt_and_abort_list,
366d050ffb9SNicholas Bellinger         struct se_cmd *prout_cmd)
367d050ffb9SNicholas Bellinger {
368d050ffb9SNicholas Bellinger 	struct se_node_acl *tmr_nacl = NULL;
369d050ffb9SNicholas Bellinger 	struct se_portal_group *tmr_tpg = NULL;
370d050ffb9SNicholas Bellinger 	int tas;
371d050ffb9SNicholas Bellinger         /*
372d050ffb9SNicholas Bellinger 	 * TASK_ABORTED status bit, this is configurable via ConfigFS
373d050ffb9SNicholas Bellinger 	 * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
374d050ffb9SNicholas Bellinger 	 *
375d050ffb9SNicholas Bellinger 	 * A task aborted status (TAS) bit set to zero specifies that aborted
376d050ffb9SNicholas Bellinger 	 * tasks shall be terminated by the device server without any response
377d050ffb9SNicholas Bellinger 	 * to the application client. A TAS bit set to one specifies that tasks
378d050ffb9SNicholas Bellinger 	 * aborted by the actions of an I_T nexus other than the I_T nexus on
379d050ffb9SNicholas Bellinger 	 * which the command was received shall be completed with TASK ABORTED
380d050ffb9SNicholas Bellinger 	 * status (see SAM-4).
381d050ffb9SNicholas Bellinger 	 */
382d050ffb9SNicholas Bellinger 	tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
383d050ffb9SNicholas Bellinger 	/*
384d050ffb9SNicholas Bellinger 	 * Determine if this se_tmr is coming from a $FABRIC_MOD
385d050ffb9SNicholas Bellinger 	 * or struct se_device passthrough..
386d050ffb9SNicholas Bellinger 	 */
387d050ffb9SNicholas Bellinger 	if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
388d050ffb9SNicholas Bellinger 		tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
389d050ffb9SNicholas Bellinger 		tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
390d050ffb9SNicholas Bellinger 		if (tmr_nacl && tmr_tpg) {
391d050ffb9SNicholas Bellinger 			pr_debug("LUN_RESET: TMR caller fabric: %s"
392d050ffb9SNicholas Bellinger 				" initiator port %s\n",
393d050ffb9SNicholas Bellinger 				tmr_tpg->se_tpg_tfo->get_fabric_name(),
394d050ffb9SNicholas Bellinger 				tmr_nacl->initiatorname);
395d050ffb9SNicholas Bellinger 		}
396d050ffb9SNicholas Bellinger 	}
397d050ffb9SNicholas Bellinger 	pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
398d050ffb9SNicholas Bellinger 		(preempt_and_abort_list) ? "Preempt" : "TMR",
399d050ffb9SNicholas Bellinger 		dev->transport->name, tas);
400d050ffb9SNicholas Bellinger 
401d050ffb9SNicholas Bellinger 	core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
402d050ffb9SNicholas Bellinger 	core_tmr_drain_task_list(dev, prout_cmd, tmr_nacl, tas,
403d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
404d050ffb9SNicholas Bellinger 	core_tmr_drain_cmd_list(dev, prout_cmd, tmr_nacl, tas,
405d050ffb9SNicholas Bellinger 				preempt_and_abort_list);
406c66ac9dbSNicholas Bellinger 	/*
407c66ac9dbSNicholas Bellinger 	 * Clear any legacy SPC-2 reservation when called during
408c66ac9dbSNicholas Bellinger 	 * LOGICAL UNIT RESET
409c66ac9dbSNicholas Bellinger 	 */
4106708bb27SAndy Grover 	if (!preempt_and_abort_list &&
411c66ac9dbSNicholas Bellinger 	     (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
412c66ac9dbSNicholas Bellinger 		spin_lock(&dev->dev_reservation_lock);
413c66ac9dbSNicholas Bellinger 		dev->dev_reserved_node_acl = NULL;
414c66ac9dbSNicholas Bellinger 		dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
415c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->dev_reservation_lock);
4166708bb27SAndy Grover 		pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
417c66ac9dbSNicholas Bellinger 	}
418c66ac9dbSNicholas Bellinger 
4191e7de68cSNicholas Bellinger 	spin_lock_irq(&dev->stats_lock);
420c66ac9dbSNicholas Bellinger 	dev->num_resets++;
4211e7de68cSNicholas Bellinger 	spin_unlock_irq(&dev->stats_lock);
422c66ac9dbSNicholas Bellinger 
4236708bb27SAndy Grover 	pr_debug("LUN_RESET: %s for [%s] Complete\n",
424c66ac9dbSNicholas Bellinger 			(preempt_and_abort_list) ? "Preempt" : "TMR",
425e3d6f909SAndy Grover 			dev->transport->name);
426c66ac9dbSNicholas Bellinger 	return 0;
427c66ac9dbSNicholas Bellinger }
428d050ffb9SNicholas Bellinger 
429