1 /*******************************************************************************
2 * Filename: target_core_tmr.c
3 *
4 * This file contains SPC-3 task management infrastructure
5 *
6 * Copyright (c) 2009,2010 Rising Tide Systems
7 * Copyright (c) 2009,2010 Linux-iSCSI.org
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 ******************************************************************************/
26
27 #include <linux/version.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/list.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33
34 #include <target/target_core_base.h>
35 #include <target/target_core_device.h>
36 #include <target/target_core_tmr.h>
37 #include <target/target_core_transport.h>
38 #include <target/target_core_fabric_ops.h>
39 #include <target/target_core_configfs.h>
40
41 #include "target_core_alua.h"
42 #include "target_core_pr.h"
43
44 #define DEBUG_LUN_RESET
45 #ifdef DEBUG_LUN_RESET
46 #define DEBUG_LR(x...) printk(KERN_INFO x)
47 #else
48 #define DEBUG_LR(x...)
49 #endif
50
core_tmr_alloc_req(struct se_cmd * se_cmd,void * fabric_tmr_ptr,u8 function)51 struct se_tmr_req *core_tmr_alloc_req(
52 struct se_cmd *se_cmd,
53 void *fabric_tmr_ptr,
54 u8 function)
55 {
56 struct se_tmr_req *tmr;
57
58 tmr = kmem_cache_zalloc(se_tmr_req_cache, GFP_KERNEL);
59 if (!(tmr)) {
60 printk(KERN_ERR "Unable to allocate struct se_tmr_req\n");
61 return ERR_PTR(-ENOMEM);
62 }
63 tmr->task_cmd = se_cmd;
64 tmr->fabric_tmr_ptr = fabric_tmr_ptr;
65 tmr->function = function;
66 INIT_LIST_HEAD(&tmr->tmr_list);
67
68 return tmr;
69 }
70 EXPORT_SYMBOL(core_tmr_alloc_req);
71
core_tmr_release_req(struct se_tmr_req * tmr)72 void core_tmr_release_req(
73 struct se_tmr_req *tmr)
74 {
75 struct se_device *dev = tmr->tmr_dev;
76
77 spin_lock(&dev->se_tmr_lock);
78 list_del(&tmr->tmr_list);
79 kmem_cache_free(se_tmr_req_cache, tmr);
80 spin_unlock(&dev->se_tmr_lock);
81 }
82
core_tmr_handle_tas_abort(struct se_node_acl * tmr_nacl,struct se_cmd * cmd,int tas,int fe_count)83 static void core_tmr_handle_tas_abort(
84 struct se_node_acl *tmr_nacl,
85 struct se_cmd *cmd,
86 int tas,
87 int fe_count)
88 {
89 if (!(fe_count)) {
90 transport_cmd_finish_abort(cmd, 1);
91 return;
92 }
93 /*
94 * TASK ABORTED status (TAS) bit support
95 */
96 if (((tmr_nacl != NULL) &&
97 (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
98 transport_send_task_abort(cmd);
99
100 transport_cmd_finish_abort(cmd, 0);
101 }
102
core_tmr_lun_reset(struct se_device * dev,struct se_tmr_req * tmr,struct list_head * preempt_and_abort_list,struct se_cmd * prout_cmd)103 int core_tmr_lun_reset(
104 struct se_device *dev,
105 struct se_tmr_req *tmr,
106 struct list_head *preempt_and_abort_list,
107 struct se_cmd *prout_cmd)
108 {
109 struct se_cmd *cmd;
110 struct se_queue_req *qr, *qr_tmp;
111 struct se_node_acl *tmr_nacl = NULL;
112 struct se_portal_group *tmr_tpg = NULL;
113 struct se_queue_obj *qobj = dev->dev_queue_obj;
114 struct se_tmr_req *tmr_p, *tmr_pp;
115 struct se_task *task, *task_tmp;
116 unsigned long flags;
117 int fe_count, state, tas;
118 /*
119 * TASK_ABORTED status bit, this is configurable via ConfigFS
120 * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
121 *
122 * A task aborted status (TAS) bit set to zero specifies that aborted
123 * tasks shall be terminated by the device server without any response
124 * to the application client. A TAS bit set to one specifies that tasks
125 * aborted by the actions of an I_T nexus other than the I_T nexus on
126 * which the command was received shall be completed with TASK ABORTED
127 * status (see SAM-4).
128 */
129 tas = DEV_ATTRIB(dev)->emulate_tas;
130 /*
131 * Determine if this se_tmr is coming from a $FABRIC_MOD
132 * or struct se_device passthrough..
133 */
134 if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
135 tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
136 tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
137 if (tmr_nacl && tmr_tpg) {
138 DEBUG_LR("LUN_RESET: TMR caller fabric: %s"
139 " initiator port %s\n",
140 TPG_TFO(tmr_tpg)->get_fabric_name(),
141 tmr_nacl->initiatorname);
142 }
143 }
144 DEBUG_LR("LUN_RESET: %s starting for [%s], tas: %d\n",
145 (preempt_and_abort_list) ? "Preempt" : "TMR",
146 TRANSPORT(dev)->name, tas);
147 /*
148 * Release all pending and outgoing TMRs aside from the received
149 * LUN_RESET tmr..
150 */
151 spin_lock(&dev->se_tmr_lock);
152 list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
153 /*
154 * Allow the received TMR to return with FUNCTION_COMPLETE.
155 */
156 if (tmr && (tmr_p == tmr))
157 continue;
158
159 cmd = tmr_p->task_cmd;
160 if (!(cmd)) {
161 printk(KERN_ERR "Unable to locate struct se_cmd for TMR\n");
162 continue;
163 }
164 /*
165 * If this function was called with a valid pr_res_key
166 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
167 * skip non regisration key matching TMRs.
168 */
169 if ((preempt_and_abort_list != NULL) &&
170 (core_scsi3_check_cdb_abort_and_preempt(
171 preempt_and_abort_list, cmd) != 0))
172 continue;
173 spin_unlock(&dev->se_tmr_lock);
174
175 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
176 if (!(atomic_read(&T_TASK(cmd)->t_transport_active))) {
177 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
178 spin_lock(&dev->se_tmr_lock);
179 continue;
180 }
181 if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
182 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
183 spin_lock(&dev->se_tmr_lock);
184 continue;
185 }
186 DEBUG_LR("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
187 " Response: 0x%02x, t_state: %d\n",
188 (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
189 tmr_p->function, tmr_p->response, cmd->t_state);
190 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
191
192 transport_cmd_finish_abort_tmr(cmd);
193 spin_lock(&dev->se_tmr_lock);
194 }
195 spin_unlock(&dev->se_tmr_lock);
196 /*
197 * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
198 * This is following sam4r17, section 5.6 Aborting commands, Table 38
199 * for TMR LUN_RESET:
200 *
201 * a) "Yes" indicates that each command that is aborted on an I_T nexus
202 * other than the one that caused the SCSI device condition is
203 * completed with TASK ABORTED status, if the TAS bit is set to one in
204 * the Control mode page (see SPC-4). "No" indicates that no status is
205 * returned for aborted commands.
206 *
207 * d) If the logical unit reset is caused by a particular I_T nexus
208 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
209 * (TASK_ABORTED status) applies.
210 *
211 * Otherwise (e.g., if triggered by a hard reset), "no"
212 * (no TASK_ABORTED SAM status) applies.
213 *
214 * Note that this seems to be independent of TAS (Task Aborted Status)
215 * in the Control Mode Page.
216 */
217 spin_lock_irqsave(&dev->execute_task_lock, flags);
218 list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
219 t_state_list) {
220 if (!(TASK_CMD(task))) {
221 printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
222 continue;
223 }
224 cmd = TASK_CMD(task);
225
226 if (!T_TASK(cmd)) {
227 printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
228 " %p ITT: 0x%08x\n", task, cmd,
229 CMD_TFO(cmd)->get_task_tag(cmd));
230 continue;
231 }
232 /*
233 * For PREEMPT_AND_ABORT usage, only process commands
234 * with a matching reservation key.
235 */
236 if ((preempt_and_abort_list != NULL) &&
237 (core_scsi3_check_cdb_abort_and_preempt(
238 preempt_and_abort_list, cmd) != 0))
239 continue;
240 /*
241 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
242 */
243 if (prout_cmd == cmd)
244 continue;
245
246 list_del(&task->t_state_list);
247 atomic_set(&task->task_state_active, 0);
248 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
249
250 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
251 DEBUG_LR("LUN_RESET: %s cmd: %p task: %p"
252 " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state/"
253 "def_t_state: %d/%d cdb: 0x%02x\n",
254 (preempt_and_abort_list) ? "Preempt" : "", cmd, task,
255 CMD_TFO(cmd)->get_task_tag(cmd), 0,
256 CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
257 cmd->deferred_t_state, T_TASK(cmd)->t_task_cdb[0]);
258 DEBUG_LR("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
259 " t_task_cdbs: %d t_task_cdbs_left: %d"
260 " t_task_cdbs_sent: %d -- t_transport_active: %d"
261 " t_transport_stop: %d t_transport_sent: %d\n",
262 CMD_TFO(cmd)->get_task_tag(cmd), cmd->pr_res_key,
263 T_TASK(cmd)->t_task_cdbs,
264 atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
265 atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
266 atomic_read(&T_TASK(cmd)->t_transport_active),
267 atomic_read(&T_TASK(cmd)->t_transport_stop),
268 atomic_read(&T_TASK(cmd)->t_transport_sent));
269
270 if (atomic_read(&task->task_active)) {
271 atomic_set(&task->task_stop, 1);
272 spin_unlock_irqrestore(
273 &T_TASK(cmd)->t_state_lock, flags);
274
275 DEBUG_LR("LUN_RESET: Waiting for task: %p to shutdown"
276 " for dev: %p\n", task, dev);
277 wait_for_completion(&task->task_stop_comp);
278 DEBUG_LR("LUN_RESET Completed task: %p shutdown for"
279 " dev: %p\n", task, dev);
280 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
281 atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
282
283 atomic_set(&task->task_active, 0);
284 atomic_set(&task->task_stop, 0);
285 } else {
286 if (atomic_read(&task->task_execute_queue) != 0)
287 transport_remove_task_from_execute_queue(task, dev);
288 }
289 __transport_stop_task_timer(task, &flags);
290
291 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
292 spin_unlock_irqrestore(
293 &T_TASK(cmd)->t_state_lock, flags);
294 DEBUG_LR("LUN_RESET: Skipping task: %p, dev: %p for"
295 " t_task_cdbs_ex_left: %d\n", task, dev,
296 atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
297
298 spin_lock_irqsave(&dev->execute_task_lock, flags);
299 continue;
300 }
301 fe_count = atomic_read(&T_TASK(cmd)->t_fe_count);
302
303 if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
304 DEBUG_LR("LUN_RESET: got t_transport_active = 1 for"
305 " task: %p, t_fe_count: %d dev: %p\n", task,
306 fe_count, dev);
307 atomic_set(&T_TASK(cmd)->t_transport_aborted, 1);
308 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
309 flags);
310 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
311
312 spin_lock_irqsave(&dev->execute_task_lock, flags);
313 continue;
314 }
315 DEBUG_LR("LUN_RESET: Got t_transport_active = 0 for task: %p,"
316 " t_fe_count: %d dev: %p\n", task, fe_count, dev);
317 atomic_set(&T_TASK(cmd)->t_transport_aborted, 1);
318 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
319 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
320
321 spin_lock_irqsave(&dev->execute_task_lock, flags);
322 }
323 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
324 /*
325 * Release all commands remaining in the struct se_device cmd queue.
326 *
327 * This follows the same logic as above for the struct se_device
328 * struct se_task state list, where commands are returned with
329 * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
330 * reference, otherwise the struct se_cmd is released.
331 */
332 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
333 list_for_each_entry_safe(qr, qr_tmp, &qobj->qobj_list, qr_list) {
334 cmd = (struct se_cmd *)qr->cmd;
335 if (!(cmd)) {
336 /*
337 * Skip these for non PREEMPT_AND_ABORT usage..
338 */
339 if (preempt_and_abort_list != NULL)
340 continue;
341
342 atomic_dec(&qobj->queue_cnt);
343 list_del(&qr->qr_list);
344 kfree(qr);
345 continue;
346 }
347 /*
348 * For PREEMPT_AND_ABORT usage, only process commands
349 * with a matching reservation key.
350 */
351 if ((preempt_and_abort_list != NULL) &&
352 (core_scsi3_check_cdb_abort_and_preempt(
353 preempt_and_abort_list, cmd) != 0))
354 continue;
355 /*
356 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
357 */
358 if (prout_cmd == cmd)
359 continue;
360
361 atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
362 atomic_dec(&qobj->queue_cnt);
363 list_del(&qr->qr_list);
364 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
365
366 state = qr->state;
367 kfree(qr);
368
369 DEBUG_LR("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
370 " %d t_fe_count: %d\n", (preempt_and_abort_list) ?
371 "Preempt" : "", cmd, state,
372 atomic_read(&T_TASK(cmd)->t_fe_count));
373 /*
374 * Signal that the command has failed via cmd->se_cmd_flags,
375 * and call TFO->new_cmd_failure() to wakeup any fabric
376 * dependent code used to wait for unsolicited data out
377 * allocation to complete. The fabric module is expected
378 * to dump any remaining unsolicited data out for the aborted
379 * command at this point.
380 */
381 transport_new_cmd_failure(cmd);
382
383 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
384 atomic_read(&T_TASK(cmd)->t_fe_count));
385 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
386 }
387 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
388 /*
389 * Clear any legacy SPC-2 reservation when called during
390 * LOGICAL UNIT RESET
391 */
392 if (!(preempt_and_abort_list) &&
393 (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
394 spin_lock(&dev->dev_reservation_lock);
395 dev->dev_reserved_node_acl = NULL;
396 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
397 spin_unlock(&dev->dev_reservation_lock);
398 printk(KERN_INFO "LUN_RESET: SCSI-2 Released reservation\n");
399 }
400
401 spin_lock(&dev->stats_lock);
402 dev->num_resets++;
403 spin_unlock(&dev->stats_lock);
404
405 DEBUG_LR("LUN_RESET: %s for [%s] Complete\n",
406 (preempt_and_abort_list) ? "Preempt" : "TMR",
407 TRANSPORT(dev)->name);
408 return 0;
409 }
410