blob: 2f73749b815173f1ab2b14d123220b1b009f6ff2 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
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
51struct 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
Nicholas Bellinger1e7de682011-05-19 20:19:10 -070058 tmr = kmem_cache_zalloc(se_tmr_req_cache, (in_interrupt()) ?
59 GFP_ATOMIC : GFP_KERNEL);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060 if (!(tmr)) {
61 printk(KERN_ERR "Unable to allocate struct se_tmr_req\n");
62 return ERR_PTR(-ENOMEM);
63 }
64 tmr->task_cmd = se_cmd;
65 tmr->fabric_tmr_ptr = fabric_tmr_ptr;
66 tmr->function = function;
67 INIT_LIST_HEAD(&tmr->tmr_list);
68
69 return tmr;
70}
71EXPORT_SYMBOL(core_tmr_alloc_req);
72
73void core_tmr_release_req(
74 struct se_tmr_req *tmr)
75{
76 struct se_device *dev = tmr->tmr_dev;
77
Nicholas Bellinger7fd29aa2011-06-23 23:48:32 +000078 if (!dev) {
79 kmem_cache_free(se_tmr_req_cache, tmr);
80 return;
81 }
82
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083 spin_lock(&dev->se_tmr_lock);
84 list_del(&tmr->tmr_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080085 spin_unlock(&dev->se_tmr_lock);
Nicholas Bellinger7fd29aa2011-06-23 23:48:32 +000086
87 kmem_cache_free(se_tmr_req_cache, tmr);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080088}
89
90static void core_tmr_handle_tas_abort(
91 struct se_node_acl *tmr_nacl,
92 struct se_cmd *cmd,
93 int tas,
94 int fe_count)
95{
96 if (!(fe_count)) {
97 transport_cmd_finish_abort(cmd, 1);
98 return;
99 }
100 /*
101 * TASK ABORTED status (TAS) bit support
102 */
103 if (((tmr_nacl != NULL) &&
104 (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
105 transport_send_task_abort(cmd);
106
107 transport_cmd_finish_abort(cmd, 0);
108}
109
110int core_tmr_lun_reset(
111 struct se_device *dev,
112 struct se_tmr_req *tmr,
113 struct list_head *preempt_and_abort_list,
114 struct se_cmd *prout_cmd)
115{
116 struct se_cmd *cmd;
117 struct se_queue_req *qr, *qr_tmp;
118 struct se_node_acl *tmr_nacl = NULL;
119 struct se_portal_group *tmr_tpg = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +0000120 struct se_queue_obj *qobj = &dev->dev_queue_obj;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800121 struct se_tmr_req *tmr_p, *tmr_pp;
122 struct se_task *task, *task_tmp;
123 unsigned long flags;
124 int fe_count, state, tas;
125 /*
126 * TASK_ABORTED status bit, this is configurable via ConfigFS
127 * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
128 *
129 * A task aborted status (TAS) bit set to zero specifies that aborted
130 * tasks shall be terminated by the device server without any response
131 * to the application client. A TAS bit set to one specifies that tasks
132 * aborted by the actions of an I_T nexus other than the I_T nexus on
133 * which the command was received shall be completed with TASK ABORTED
134 * status (see SAM-4).
135 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000136 tas = dev->se_sub_dev->se_dev_attrib.emulate_tas;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800137 /*
138 * Determine if this se_tmr is coming from a $FABRIC_MOD
139 * or struct se_device passthrough..
140 */
141 if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
142 tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
143 tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
144 if (tmr_nacl && tmr_tpg) {
145 DEBUG_LR("LUN_RESET: TMR caller fabric: %s"
146 " initiator port %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000147 tmr_tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800148 tmr_nacl->initiatorname);
149 }
150 }
151 DEBUG_LR("LUN_RESET: %s starting for [%s], tas: %d\n",
152 (preempt_and_abort_list) ? "Preempt" : "TMR",
Andy Grovere3d6f902011-07-19 08:55:10 +0000153 dev->transport->name, tas);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800154 /*
155 * Release all pending and outgoing TMRs aside from the received
156 * LUN_RESET tmr..
157 */
158 spin_lock(&dev->se_tmr_lock);
159 list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
160 /*
161 * Allow the received TMR to return with FUNCTION_COMPLETE.
162 */
163 if (tmr && (tmr_p == tmr))
164 continue;
165
166 cmd = tmr_p->task_cmd;
167 if (!(cmd)) {
168 printk(KERN_ERR "Unable to locate struct se_cmd for TMR\n");
169 continue;
170 }
171 /*
172 * If this function was called with a valid pr_res_key
173 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
174 * skip non regisration key matching TMRs.
175 */
176 if ((preempt_and_abort_list != NULL) &&
177 (core_scsi3_check_cdb_abort_and_preempt(
178 preempt_and_abort_list, cmd) != 0))
179 continue;
180 spin_unlock(&dev->se_tmr_lock);
181
Andy Grovere3d6f902011-07-19 08:55:10 +0000182 spin_lock_irqsave(&cmd->t_task->t_state_lock, flags);
183 if (!(atomic_read(&cmd->t_task->t_transport_active))) {
184 spin_unlock_irqrestore(&cmd->t_task->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800185 spin_lock(&dev->se_tmr_lock);
186 continue;
187 }
188 if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
Andy Grovere3d6f902011-07-19 08:55:10 +0000189 spin_unlock_irqrestore(&cmd->t_task->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190 spin_lock(&dev->se_tmr_lock);
191 continue;
192 }
193 DEBUG_LR("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
194 " Response: 0x%02x, t_state: %d\n",
195 (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
196 tmr_p->function, tmr_p->response, cmd->t_state);
Andy Grovere3d6f902011-07-19 08:55:10 +0000197 spin_unlock_irqrestore(&cmd->t_task->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800198
199 transport_cmd_finish_abort_tmr(cmd);
200 spin_lock(&dev->se_tmr_lock);
201 }
202 spin_unlock(&dev->se_tmr_lock);
203 /*
204 * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
205 * This is following sam4r17, section 5.6 Aborting commands, Table 38
206 * for TMR LUN_RESET:
207 *
208 * a) "Yes" indicates that each command that is aborted on an I_T nexus
209 * other than the one that caused the SCSI device condition is
210 * completed with TASK ABORTED status, if the TAS bit is set to one in
211 * the Control mode page (see SPC-4). "No" indicates that no status is
212 * returned for aborted commands.
213 *
214 * d) If the logical unit reset is caused by a particular I_T nexus
215 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
216 * (TASK_ABORTED status) applies.
217 *
218 * Otherwise (e.g., if triggered by a hard reset), "no"
219 * (no TASK_ABORTED SAM status) applies.
220 *
221 * Note that this seems to be independent of TAS (Task Aborted Status)
222 * in the Control Mode Page.
223 */
224 spin_lock_irqsave(&dev->execute_task_lock, flags);
225 list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
226 t_state_list) {
Andy Grovere3d6f902011-07-19 08:55:10 +0000227 if (!task->task_se_cmd) {
228 printk(KERN_ERR "task->task_se_cmd is NULL!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 continue;
230 }
Andy Grovere3d6f902011-07-19 08:55:10 +0000231 cmd = task->task_se_cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800232
Andy Grovere3d6f902011-07-19 08:55:10 +0000233 if (!cmd->t_task) {
234 printk(KERN_ERR "cmd->t_task is NULL for task: %p cmd:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800235 " %p ITT: 0x%08x\n", task, cmd,
Andy Grovere3d6f902011-07-19 08:55:10 +0000236 cmd->se_tfo->get_task_tag(cmd));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800237 continue;
238 }
239 /*
240 * For PREEMPT_AND_ABORT usage, only process commands
241 * with a matching reservation key.
242 */
243 if ((preempt_and_abort_list != NULL) &&
244 (core_scsi3_check_cdb_abort_and_preempt(
245 preempt_and_abort_list, cmd) != 0))
246 continue;
247 /*
248 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
249 */
250 if (prout_cmd == cmd)
251 continue;
252
253 list_del(&task->t_state_list);
254 atomic_set(&task->task_state_active, 0);
255 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
256
Andy Grovere3d6f902011-07-19 08:55:10 +0000257 spin_lock_irqsave(&cmd->t_task->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800258 DEBUG_LR("LUN_RESET: %s cmd: %p task: %p"
259 " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state/"
260 "def_t_state: %d/%d cdb: 0x%02x\n",
261 (preempt_and_abort_list) ? "Preempt" : "", cmd, task,
Andy Grovere3d6f902011-07-19 08:55:10 +0000262 cmd->se_tfo->get_task_tag(cmd), 0,
263 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
264 cmd->deferred_t_state, cmd->t_task->t_task_cdb[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800265 DEBUG_LR("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
266 " t_task_cdbs: %d t_task_cdbs_left: %d"
267 " t_task_cdbs_sent: %d -- t_transport_active: %d"
268 " t_transport_stop: %d t_transport_sent: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000269 cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
270 cmd->t_task->t_task_cdbs,
271 atomic_read(&cmd->t_task->t_task_cdbs_left),
272 atomic_read(&cmd->t_task->t_task_cdbs_sent),
273 atomic_read(&cmd->t_task->t_transport_active),
274 atomic_read(&cmd->t_task->t_transport_stop),
275 atomic_read(&cmd->t_task->t_transport_sent));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276
277 if (atomic_read(&task->task_active)) {
278 atomic_set(&task->task_stop, 1);
279 spin_unlock_irqrestore(
Andy Grovere3d6f902011-07-19 08:55:10 +0000280 &cmd->t_task->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281
282 DEBUG_LR("LUN_RESET: Waiting for task: %p to shutdown"
283 " for dev: %p\n", task, dev);
284 wait_for_completion(&task->task_stop_comp);
285 DEBUG_LR("LUN_RESET Completed task: %p shutdown for"
286 " dev: %p\n", task, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000287 spin_lock_irqsave(&cmd->t_task->t_state_lock, flags);
288 atomic_dec(&cmd->t_task->t_task_cdbs_left);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800289
290 atomic_set(&task->task_active, 0);
291 atomic_set(&task->task_stop, 0);
Nicholas Bellinger52208ae2011-02-24 16:58:20 -0800292 } else {
293 if (atomic_read(&task->task_execute_queue) != 0)
294 transport_remove_task_from_execute_queue(task, dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 }
296 __transport_stop_task_timer(task, &flags);
297
Andy Grovere3d6f902011-07-19 08:55:10 +0000298 if (!(atomic_dec_and_test(&cmd->t_task->t_task_cdbs_ex_left))) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299 spin_unlock_irqrestore(
Andy Grovere3d6f902011-07-19 08:55:10 +0000300 &cmd->t_task->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 DEBUG_LR("LUN_RESET: Skipping task: %p, dev: %p for"
302 " t_task_cdbs_ex_left: %d\n", task, dev,
Andy Grovere3d6f902011-07-19 08:55:10 +0000303 atomic_read(&cmd->t_task->t_task_cdbs_ex_left));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304
305 spin_lock_irqsave(&dev->execute_task_lock, flags);
306 continue;
307 }
Andy Grovere3d6f902011-07-19 08:55:10 +0000308 fe_count = atomic_read(&cmd->t_task->t_fe_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800309
Andy Grovere3d6f902011-07-19 08:55:10 +0000310 if (atomic_read(&cmd->t_task->t_transport_active)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311 DEBUG_LR("LUN_RESET: got t_transport_active = 1 for"
312 " task: %p, t_fe_count: %d dev: %p\n", task,
313 fe_count, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000314 atomic_set(&cmd->t_task->t_transport_aborted, 1);
315 spin_unlock_irqrestore(&cmd->t_task->t_state_lock,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800316 flags);
317 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
318
319 spin_lock_irqsave(&dev->execute_task_lock, flags);
320 continue;
321 }
322 DEBUG_LR("LUN_RESET: Got t_transport_active = 0 for task: %p,"
323 " t_fe_count: %d dev: %p\n", task, fe_count, dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000324 atomic_set(&cmd->t_task->t_transport_aborted, 1);
325 spin_unlock_irqrestore(&cmd->t_task->t_state_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800326 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
327
328 spin_lock_irqsave(&dev->execute_task_lock, flags);
329 }
330 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
331 /*
332 * Release all commands remaining in the struct se_device cmd queue.
333 *
334 * This follows the same logic as above for the struct se_device
335 * struct se_task state list, where commands are returned with
336 * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
337 * reference, otherwise the struct se_cmd is released.
338 */
339 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
340 list_for_each_entry_safe(qr, qr_tmp, &qobj->qobj_list, qr_list) {
341 cmd = (struct se_cmd *)qr->cmd;
342 if (!(cmd)) {
343 /*
344 * Skip these for non PREEMPT_AND_ABORT usage..
345 */
346 if (preempt_and_abort_list != NULL)
347 continue;
348
349 atomic_dec(&qobj->queue_cnt);
350 list_del(&qr->qr_list);
351 kfree(qr);
352 continue;
353 }
354 /*
355 * For PREEMPT_AND_ABORT usage, only process commands
356 * with a matching reservation key.
357 */
358 if ((preempt_and_abort_list != NULL) &&
359 (core_scsi3_check_cdb_abort_and_preempt(
360 preempt_and_abort_list, cmd) != 0))
361 continue;
362 /*
363 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
364 */
365 if (prout_cmd == cmd)
366 continue;
367
Andy Grovere3d6f902011-07-19 08:55:10 +0000368 atomic_dec(&cmd->t_task->t_transport_queue_active);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800369 atomic_dec(&qobj->queue_cnt);
370 list_del(&qr->qr_list);
371 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
372
373 state = qr->state;
374 kfree(qr);
375
376 DEBUG_LR("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
377 " %d t_fe_count: %d\n", (preempt_and_abort_list) ?
378 "Preempt" : "", cmd, state,
Andy Grovere3d6f902011-07-19 08:55:10 +0000379 atomic_read(&cmd->t_task->t_fe_count));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 /*
381 * Signal that the command has failed via cmd->se_cmd_flags,
382 * and call TFO->new_cmd_failure() to wakeup any fabric
383 * dependent code used to wait for unsolicited data out
384 * allocation to complete. The fabric module is expected
385 * to dump any remaining unsolicited data out for the aborted
386 * command at this point.
387 */
388 transport_new_cmd_failure(cmd);
389
390 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
Andy Grovere3d6f902011-07-19 08:55:10 +0000391 atomic_read(&cmd->t_task->t_fe_count));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
393 }
394 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
395 /*
396 * Clear any legacy SPC-2 reservation when called during
397 * LOGICAL UNIT RESET
398 */
399 if (!(preempt_and_abort_list) &&
400 (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
401 spin_lock(&dev->dev_reservation_lock);
402 dev->dev_reserved_node_acl = NULL;
403 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
404 spin_unlock(&dev->dev_reservation_lock);
405 printk(KERN_INFO "LUN_RESET: SCSI-2 Released reservation\n");
406 }
407
Nicholas Bellinger1e7de682011-05-19 20:19:10 -0700408 spin_lock_irq(&dev->stats_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 dev->num_resets++;
Nicholas Bellinger1e7de682011-05-19 20:19:10 -0700410 spin_unlock_irq(&dev->stats_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411
412 DEBUG_LR("LUN_RESET: %s for [%s] Complete\n",
413 (preempt_and_abort_list) ? "Preempt" : "TMR",
Andy Grovere3d6f902011-07-19 08:55:10 +0000414 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 return 0;
416}