Merge tag 'v3.0-rc4' of /pub/scm/linux/kernel/git/torvalds/linux-2.6 into for-linus
[linux-2.6.git] / drivers / target / target_core_tmr.c
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
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, (in_interrupt()) ?
59                                         GFP_ATOMIC : GFP_KERNEL);
60         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 }
71 EXPORT_SYMBOL(core_tmr_alloc_req);
72
73 void core_tmr_release_req(
74         struct se_tmr_req *tmr)
75 {
76         struct se_device *dev = tmr->tmr_dev;
77
78         spin_lock(&dev->se_tmr_lock);
79         list_del(&tmr->tmr_list);
80         kmem_cache_free(se_tmr_req_cache, tmr);
81         spin_unlock(&dev->se_tmr_lock);
82 }
83
84 static void core_tmr_handle_tas_abort(
85         struct se_node_acl *tmr_nacl,
86         struct se_cmd *cmd,
87         int tas,
88         int fe_count)
89 {
90         if (!(fe_count)) {
91                 transport_cmd_finish_abort(cmd, 1);
92                 return;
93         }
94         /*
95          * TASK ABORTED status (TAS) bit support
96         */
97         if (((tmr_nacl != NULL) &&
98              (tmr_nacl == cmd->se_sess->se_node_acl)) || tas)
99                 transport_send_task_abort(cmd);
100
101         transport_cmd_finish_abort(cmd, 0);
102 }
103
104 int core_tmr_lun_reset(
105         struct se_device *dev,
106         struct se_tmr_req *tmr,
107         struct list_head *preempt_and_abort_list,
108         struct se_cmd *prout_cmd)
109 {
110         struct se_cmd *cmd;
111         struct se_queue_req *qr, *qr_tmp;
112         struct se_node_acl *tmr_nacl = NULL;
113         struct se_portal_group *tmr_tpg = NULL;
114         struct se_queue_obj *qobj = dev->dev_queue_obj;
115         struct se_tmr_req *tmr_p, *tmr_pp;
116         struct se_task *task, *task_tmp;
117         unsigned long flags;
118         int fe_count, state, tas;
119         /*
120          * TASK_ABORTED status bit, this is configurable via ConfigFS
121          * struct se_device attributes.  spc4r17 section 7.4.6 Control mode page
122          *
123          * A task aborted status (TAS) bit set to zero specifies that aborted
124          * tasks shall be terminated by the device server without any response
125          * to the application client. A TAS bit set to one specifies that tasks
126          * aborted by the actions of an I_T nexus other than the I_T nexus on
127          * which the command was received shall be completed with TASK ABORTED
128          * status (see SAM-4).
129          */
130         tas = DEV_ATTRIB(dev)->emulate_tas;
131         /*
132          * Determine if this se_tmr is coming from a $FABRIC_MOD
133          * or struct se_device passthrough..
134          */
135         if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
136                 tmr_nacl = tmr->task_cmd->se_sess->se_node_acl;
137                 tmr_tpg = tmr->task_cmd->se_sess->se_tpg;
138                 if (tmr_nacl && tmr_tpg) {
139                         DEBUG_LR("LUN_RESET: TMR caller fabric: %s"
140                                 " initiator port %s\n",
141                                 TPG_TFO(tmr_tpg)->get_fabric_name(),
142                                 tmr_nacl->initiatorname);
143                 }
144         }
145         DEBUG_LR("LUN_RESET: %s starting for [%s], tas: %d\n",
146                 (preempt_and_abort_list) ? "Preempt" : "TMR",
147                 TRANSPORT(dev)->name, tas);
148         /*
149          * Release all pending and outgoing TMRs aside from the received
150          * LUN_RESET tmr..
151          */
152         spin_lock(&dev->se_tmr_lock);
153         list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
154                 /*
155                  * Allow the received TMR to return with FUNCTION_COMPLETE.
156                  */
157                 if (tmr && (tmr_p == tmr))
158                         continue;
159
160                 cmd = tmr_p->task_cmd;
161                 if (!(cmd)) {
162                         printk(KERN_ERR "Unable to locate struct se_cmd for TMR\n");
163                         continue;
164                 }
165                 /*
166                  * If this function was called with a valid pr_res_key
167                  * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
168                  * skip non regisration key matching TMRs.
169                  */
170                 if ((preempt_and_abort_list != NULL) &&
171                     (core_scsi3_check_cdb_abort_and_preempt(
172                                         preempt_and_abort_list, cmd) != 0))
173                         continue;
174                 spin_unlock(&dev->se_tmr_lock);
175
176                 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
177                 if (!(atomic_read(&T_TASK(cmd)->t_transport_active))) {
178                         spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
179                         spin_lock(&dev->se_tmr_lock);
180                         continue;
181                 }
182                 if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
183                         spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
184                         spin_lock(&dev->se_tmr_lock);
185                         continue;
186                 }
187                 DEBUG_LR("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
188                         " Response: 0x%02x, t_state: %d\n",
189                         (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
190                         tmr_p->function, tmr_p->response, cmd->t_state);
191                 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
192
193                 transport_cmd_finish_abort_tmr(cmd);
194                 spin_lock(&dev->se_tmr_lock);
195         }
196         spin_unlock(&dev->se_tmr_lock);
197         /*
198          * Complete outstanding struct se_task CDBs with TASK_ABORTED SAM status.
199          * This is following sam4r17, section 5.6 Aborting commands, Table 38
200          * for TMR LUN_RESET:
201          *
202          * a) "Yes" indicates that each command that is aborted on an I_T nexus
203          * other than the one that caused the SCSI device condition is
204          * completed with TASK ABORTED status, if the TAS bit is set to one in
205          * the Control mode page (see SPC-4). "No" indicates that no status is
206          * returned for aborted commands.
207          *
208          * d) If the logical unit reset is caused by a particular I_T nexus
209          * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
210          * (TASK_ABORTED status) applies.
211          *
212          * Otherwise (e.g., if triggered by a hard reset), "no"
213          * (no TASK_ABORTED SAM status) applies.
214          *
215          * Note that this seems to be independent of TAS (Task Aborted Status)
216          * in the Control Mode Page.
217          */
218         spin_lock_irqsave(&dev->execute_task_lock, flags);
219         list_for_each_entry_safe(task, task_tmp, &dev->state_task_list,
220                                 t_state_list) {
221                 if (!(TASK_CMD(task))) {
222                         printk(KERN_ERR "TASK_CMD(task) is NULL!\n");
223                         continue;
224                 }
225                 cmd = TASK_CMD(task);
226
227                 if (!T_TASK(cmd)) {
228                         printk(KERN_ERR "T_TASK(cmd) is NULL for task: %p cmd:"
229                                 " %p ITT: 0x%08x\n", task, cmd,
230                                 CMD_TFO(cmd)->get_task_tag(cmd));
231                         continue;
232                 }
233                 /*
234                  * For PREEMPT_AND_ABORT usage, only process commands
235                  * with a matching reservation key.
236                  */
237                 if ((preempt_and_abort_list != NULL) &&
238                     (core_scsi3_check_cdb_abort_and_preempt(
239                                         preempt_and_abort_list, cmd) != 0))
240                         continue;
241                 /*
242                  * Not aborting PROUT PREEMPT_AND_ABORT CDB..
243                  */
244                 if (prout_cmd == cmd)
245                         continue;
246
247                 list_del(&task->t_state_list);
248                 atomic_set(&task->task_state_active, 0);
249                 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
250
251                 spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
252                 DEBUG_LR("LUN_RESET: %s cmd: %p task: %p"
253                         " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state/"
254                         "def_t_state: %d/%d cdb: 0x%02x\n",
255                         (preempt_and_abort_list) ? "Preempt" : "", cmd, task,
256                         CMD_TFO(cmd)->get_task_tag(cmd), 0,
257                         CMD_TFO(cmd)->get_cmd_state(cmd), cmd->t_state,
258                         cmd->deferred_t_state, T_TASK(cmd)->t_task_cdb[0]);
259                 DEBUG_LR("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
260                         " t_task_cdbs: %d t_task_cdbs_left: %d"
261                         " t_task_cdbs_sent: %d -- t_transport_active: %d"
262                         " t_transport_stop: %d t_transport_sent: %d\n",
263                         CMD_TFO(cmd)->get_task_tag(cmd), cmd->pr_res_key,
264                         T_TASK(cmd)->t_task_cdbs,
265                         atomic_read(&T_TASK(cmd)->t_task_cdbs_left),
266                         atomic_read(&T_TASK(cmd)->t_task_cdbs_sent),
267                         atomic_read(&T_TASK(cmd)->t_transport_active),
268                         atomic_read(&T_TASK(cmd)->t_transport_stop),
269                         atomic_read(&T_TASK(cmd)->t_transport_sent));
270
271                 if (atomic_read(&task->task_active)) {
272                         atomic_set(&task->task_stop, 1);
273                         spin_unlock_irqrestore(
274                                 &T_TASK(cmd)->t_state_lock, flags);
275
276                         DEBUG_LR("LUN_RESET: Waiting for task: %p to shutdown"
277                                 " for dev: %p\n", task, dev);
278                         wait_for_completion(&task->task_stop_comp);
279                         DEBUG_LR("LUN_RESET Completed task: %p shutdown for"
280                                 " dev: %p\n", task, dev);
281                         spin_lock_irqsave(&T_TASK(cmd)->t_state_lock, flags);
282                         atomic_dec(&T_TASK(cmd)->t_task_cdbs_left);
283
284                         atomic_set(&task->task_active, 0);
285                         atomic_set(&task->task_stop, 0);
286                 } else {
287                         if (atomic_read(&task->task_execute_queue) != 0)
288                                 transport_remove_task_from_execute_queue(task, dev);
289                 }
290                 __transport_stop_task_timer(task, &flags);
291
292                 if (!(atomic_dec_and_test(&T_TASK(cmd)->t_task_cdbs_ex_left))) {
293                         spin_unlock_irqrestore(
294                                         &T_TASK(cmd)->t_state_lock, flags);
295                         DEBUG_LR("LUN_RESET: Skipping task: %p, dev: %p for"
296                                 " t_task_cdbs_ex_left: %d\n", task, dev,
297                                 atomic_read(&T_TASK(cmd)->t_task_cdbs_ex_left));
298
299                         spin_lock_irqsave(&dev->execute_task_lock, flags);
300                         continue;
301                 }
302                 fe_count = atomic_read(&T_TASK(cmd)->t_fe_count);
303
304                 if (atomic_read(&T_TASK(cmd)->t_transport_active)) {
305                         DEBUG_LR("LUN_RESET: got t_transport_active = 1 for"
306                                 " task: %p, t_fe_count: %d dev: %p\n", task,
307                                 fe_count, dev);
308                         atomic_set(&T_TASK(cmd)->t_transport_aborted, 1);
309                         spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock,
310                                                 flags);
311                         core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
312
313                         spin_lock_irqsave(&dev->execute_task_lock, flags);
314                         continue;
315                 }
316                 DEBUG_LR("LUN_RESET: Got t_transport_active = 0 for task: %p,"
317                         " t_fe_count: %d dev: %p\n", task, fe_count, dev);
318                 atomic_set(&T_TASK(cmd)->t_transport_aborted, 1);
319                 spin_unlock_irqrestore(&T_TASK(cmd)->t_state_lock, flags);
320                 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas, fe_count);
321
322                 spin_lock_irqsave(&dev->execute_task_lock, flags);
323         }
324         spin_unlock_irqrestore(&dev->execute_task_lock, flags);
325         /*
326          * Release all commands remaining in the struct se_device cmd queue.
327          *
328          * This follows the same logic as above for the struct se_device
329          * struct se_task state list, where commands are returned with
330          * TASK_ABORTED status, if there is an outstanding $FABRIC_MOD
331          * reference, otherwise the struct se_cmd is released.
332          */
333         spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
334         list_for_each_entry_safe(qr, qr_tmp, &qobj->qobj_list, qr_list) {
335                 cmd = (struct se_cmd *)qr->cmd;
336                 if (!(cmd)) {
337                         /*
338                          * Skip these for non PREEMPT_AND_ABORT usage..
339                          */
340                         if (preempt_and_abort_list != NULL)
341                                 continue;
342
343                         atomic_dec(&qobj->queue_cnt);
344                         list_del(&qr->qr_list);
345                         kfree(qr);
346                         continue;
347                 }
348                 /*
349                  * For PREEMPT_AND_ABORT usage, only process commands
350                  * with a matching reservation key.
351                  */
352                 if ((preempt_and_abort_list != NULL) &&
353                     (core_scsi3_check_cdb_abort_and_preempt(
354                                         preempt_and_abort_list, cmd) != 0))
355                         continue;
356                 /*
357                  * Not aborting PROUT PREEMPT_AND_ABORT CDB..
358                  */
359                 if (prout_cmd == cmd)
360                         continue;
361
362                 atomic_dec(&T_TASK(cmd)->t_transport_queue_active);
363                 atomic_dec(&qobj->queue_cnt);
364                 list_del(&qr->qr_list);
365                 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
366
367                 state = qr->state;
368                 kfree(qr);
369
370                 DEBUG_LR("LUN_RESET: %s from Device Queue: cmd: %p t_state:"
371                         " %d t_fe_count: %d\n", (preempt_and_abort_list) ?
372                         "Preempt" : "", cmd, state,
373                         atomic_read(&T_TASK(cmd)->t_fe_count));
374                 /*
375                  * Signal that the command has failed via cmd->se_cmd_flags,
376                  * and call TFO->new_cmd_failure() to wakeup any fabric
377                  * dependent code used to wait for unsolicited data out
378                  * allocation to complete.  The fabric module is expected
379                  * to dump any remaining unsolicited data out for the aborted
380                  * command at this point.
381                  */
382                 transport_new_cmd_failure(cmd);
383
384                 core_tmr_handle_tas_abort(tmr_nacl, cmd, tas,
385                                 atomic_read(&T_TASK(cmd)->t_fe_count));
386                 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
387         }
388         spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
389         /*
390          * Clear any legacy SPC-2 reservation when called during
391          * LOGICAL UNIT RESET
392          */
393         if (!(preempt_and_abort_list) &&
394              (dev->dev_flags & DF_SPC2_RESERVATIONS)) {
395                 spin_lock(&dev->dev_reservation_lock);
396                 dev->dev_reserved_node_acl = NULL;
397                 dev->dev_flags &= ~DF_SPC2_RESERVATIONS;
398                 spin_unlock(&dev->dev_reservation_lock);
399                 printk(KERN_INFO "LUN_RESET: SCSI-2 Released reservation\n");
400         }
401
402         spin_lock_irq(&dev->stats_lock);
403         dev->num_resets++;
404         spin_unlock_irq(&dev->stats_lock);
405
406         DEBUG_LR("LUN_RESET: %s for [%s] Complete\n",
407                         (preempt_and_abort_list) ? "Preempt" : "TMR",
408                         TRANSPORT(dev)->name);
409         return 0;
410 }