1 /*******************************************************************************
2 * Filename: target_core_tmr.c
3 *
4 * This file contains SPC-3 task management infrastructure
5 *
6 * (c) Copyright 2009-2013 Datera, Inc.
7 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 ******************************************************************************/
25
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/list.h>
29 #include <linux/export.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32
33 #include <target/target_core_base.h>
34 #include <target/target_core_backend.h>
35 #include <target/target_core_fabric.h>
36 #include <target/target_core_configfs.h>
37
38 #include "target_core_internal.h"
39 #include "target_core_alua.h"
40 #include "target_core_pr.h"
41
core_tmr_alloc_req(struct se_cmd * se_cmd,void * fabric_tmr_ptr,u8 function,gfp_t gfp_flags)42 int core_tmr_alloc_req(
43 struct se_cmd *se_cmd,
44 void *fabric_tmr_ptr,
45 u8 function,
46 gfp_t gfp_flags)
47 {
48 struct se_tmr_req *tmr;
49
50 tmr = kzalloc(sizeof(struct se_tmr_req), gfp_flags);
51 if (!tmr) {
52 pr_err("Unable to allocate struct se_tmr_req\n");
53 return -ENOMEM;
54 }
55
56 se_cmd->se_cmd_flags |= SCF_SCSI_TMR_CDB;
57 se_cmd->se_tmr_req = tmr;
58 tmr->task_cmd = se_cmd;
59 tmr->fabric_tmr_ptr = fabric_tmr_ptr;
60 tmr->function = function;
61 INIT_LIST_HEAD(&tmr->tmr_list);
62
63 return 0;
64 }
65 EXPORT_SYMBOL(core_tmr_alloc_req);
66
core_tmr_release_req(struct se_tmr_req * tmr)67 void core_tmr_release_req(struct se_tmr_req *tmr)
68 {
69 struct se_device *dev = tmr->tmr_dev;
70 unsigned long flags;
71
72 if (dev) {
73 spin_lock_irqsave(&dev->se_tmr_lock, flags);
74 list_del_init(&tmr->tmr_list);
75 spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
76 }
77
78 kfree(tmr);
79 }
80
core_tmr_handle_tas_abort(struct se_cmd * cmd,int tas)81 static int core_tmr_handle_tas_abort(struct se_cmd *cmd, int tas)
82 {
83 unsigned long flags;
84 bool remove = true, send_tas;
85 /*
86 * TASK ABORTED status (TAS) bit support
87 */
88 spin_lock_irqsave(&cmd->t_state_lock, flags);
89 send_tas = (cmd->transport_state & CMD_T_TAS);
90 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
91
92 if (send_tas) {
93 remove = false;
94 transport_send_task_abort(cmd);
95 }
96
97 return transport_cmd_finish_abort(cmd, remove);
98 }
99
target_check_cdb_and_preempt(struct list_head * list,struct se_cmd * cmd)100 static int target_check_cdb_and_preempt(struct list_head *list,
101 struct se_cmd *cmd)
102 {
103 struct t10_pr_registration *reg;
104
105 if (!list)
106 return 0;
107 list_for_each_entry(reg, list, pr_reg_abort_list) {
108 if (reg->pr_res_key == cmd->pr_res_key)
109 return 0;
110 }
111
112 return 1;
113 }
114
__target_check_io_state(struct se_cmd * se_cmd,struct se_session * tmr_sess,int tas)115 static bool __target_check_io_state(struct se_cmd *se_cmd,
116 struct se_session *tmr_sess, int tas)
117 {
118 struct se_session *sess = se_cmd->se_sess;
119
120 assert_spin_locked(&sess->sess_cmd_lock);
121 WARN_ON_ONCE(!irqs_disabled());
122 /*
123 * If command already reached CMD_T_COMPLETE state within
124 * target_complete_cmd() or CMD_T_FABRIC_STOP due to shutdown,
125 * this se_cmd has been passed to fabric driver and will
126 * not be aborted.
127 *
128 * Otherwise, obtain a local se_cmd->cmd_kref now for TMR
129 * ABORT_TASK + LUN_RESET for CMD_T_ABORTED processing as
130 * long as se_cmd->cmd_kref is still active unless zero.
131 */
132 spin_lock(&se_cmd->t_state_lock);
133 if (se_cmd->transport_state & (CMD_T_COMPLETE | CMD_T_FABRIC_STOP)) {
134 pr_debug("Attempted to abort io tag: %u already complete or"
135 " fabric stop, skipping\n",
136 se_cmd->se_tfo->get_task_tag(se_cmd));
137 spin_unlock(&se_cmd->t_state_lock);
138 return false;
139 }
140 if (se_cmd->transport_state & CMD_T_PRE_EXECUTE) {
141 if (se_cmd->scsi_status) {
142 pr_debug("Attempted to abort io tag: %u early failure"
143 " status: 0x%02x\n", se_cmd->se_tfo->get_task_tag(se_cmd),
144 se_cmd->scsi_status);
145 spin_unlock(&se_cmd->t_state_lock);
146 return false;
147 }
148 }
149 if (sess->sess_tearing_down || se_cmd->cmd_wait_set) {
150 pr_debug("Attempted to abort io tag: %u already shutdown,"
151 " skipping\n", se_cmd->se_tfo->get_task_tag(se_cmd));
152 spin_unlock(&se_cmd->t_state_lock);
153 return false;
154 }
155 se_cmd->transport_state |= CMD_T_ABORTED;
156
157 if ((tmr_sess != se_cmd->se_sess) && tas)
158 se_cmd->transport_state |= CMD_T_TAS;
159
160 spin_unlock(&se_cmd->t_state_lock);
161
162 return kref_get_unless_zero(&se_cmd->cmd_kref);
163 }
164
core_tmr_abort_task(struct se_device * dev,struct se_tmr_req * tmr,struct se_session * se_sess)165 void core_tmr_abort_task(
166 struct se_device *dev,
167 struct se_tmr_req *tmr,
168 struct se_session *se_sess)
169 {
170 struct se_cmd *se_cmd;
171 unsigned long flags;
172 int ref_tag;
173
174 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
175 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list) {
176
177 if (dev != se_cmd->se_dev)
178 continue;
179
180 /* skip se_cmd associated with tmr */
181 if (tmr->task_cmd == se_cmd)
182 continue;
183
184 ref_tag = se_cmd->se_tfo->get_task_tag(se_cmd);
185 if (tmr->ref_task_tag != ref_tag)
186 continue;
187
188 printk("ABORT_TASK: Found referenced %s task_tag: %u\n",
189 se_cmd->se_tfo->get_fabric_name(), ref_tag);
190
191 if (!__target_check_io_state(se_cmd, se_sess, 0)) {
192 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
193 goto out;
194 }
195
196 list_del_init(&se_cmd->se_cmd_list);
197 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
198
199 cancel_work_sync(&se_cmd->work);
200 transport_wait_for_tasks(se_cmd);
201
202 if (!transport_cmd_finish_abort(se_cmd, true))
203 target_put_sess_cmd(se_cmd);
204
205 printk("ABORT_TASK: Sending TMR_FUNCTION_COMPLETE for"
206 " ref_tag: %d\n", ref_tag);
207 tmr->response = TMR_FUNCTION_COMPLETE;
208 return;
209 }
210 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
211
212 out:
213 printk("ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST for ref_tag: %d\n",
214 tmr->ref_task_tag);
215 tmr->response = TMR_TASK_DOES_NOT_EXIST;
216 }
217
core_tmr_drain_tmr_list(struct se_device * dev,struct se_tmr_req * tmr,struct list_head * preempt_and_abort_list)218 static void core_tmr_drain_tmr_list(
219 struct se_device *dev,
220 struct se_tmr_req *tmr,
221 struct list_head *preempt_and_abort_list)
222 {
223 LIST_HEAD(drain_tmr_list);
224 struct se_session *sess;
225 struct se_tmr_req *tmr_p, *tmr_pp;
226 struct se_cmd *cmd;
227 unsigned long flags;
228 bool rc;
229 /*
230 * Release all pending and outgoing TMRs aside from the received
231 * LUN_RESET tmr..
232 */
233 spin_lock_irqsave(&dev->se_tmr_lock, flags);
234 list_for_each_entry_safe(tmr_p, tmr_pp, &dev->dev_tmr_list, tmr_list) {
235 /*
236 * Allow the received TMR to return with FUNCTION_COMPLETE.
237 */
238 if (tmr_p == tmr)
239 continue;
240
241 cmd = tmr_p->task_cmd;
242 if (!cmd) {
243 pr_err("Unable to locate struct se_cmd for TMR\n");
244 continue;
245 }
246 /*
247 * If this function was called with a valid pr_res_key
248 * parameter (eg: for PROUT PREEMPT_AND_ABORT service action
249 * skip non regisration key matching TMRs.
250 */
251 if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
252 continue;
253
254 sess = cmd->se_sess;
255 if (WARN_ON_ONCE(!sess))
256 continue;
257
258 spin_lock(&sess->sess_cmd_lock);
259 spin_lock(&cmd->t_state_lock);
260 if (!(cmd->transport_state & CMD_T_ACTIVE) ||
261 (cmd->transport_state & CMD_T_FABRIC_STOP)) {
262 spin_unlock(&cmd->t_state_lock);
263 spin_unlock(&sess->sess_cmd_lock);
264 continue;
265 }
266 if (cmd->t_state == TRANSPORT_ISTATE_PROCESSING) {
267 spin_unlock(&cmd->t_state_lock);
268 spin_unlock(&sess->sess_cmd_lock);
269 continue;
270 }
271 if (sess->sess_tearing_down || cmd->cmd_wait_set) {
272 spin_unlock(&cmd->t_state_lock);
273 spin_unlock(&sess->sess_cmd_lock);
274 continue;
275 }
276 cmd->transport_state |= CMD_T_ABORTED;
277 spin_unlock(&cmd->t_state_lock);
278
279 rc = kref_get_unless_zero(&cmd->cmd_kref);
280 if (!rc) {
281 printk("LUN_RESET TMR: non-zero kref_get_unless_zero\n");
282 spin_unlock(&sess->sess_cmd_lock);
283 continue;
284 }
285 spin_unlock(&sess->sess_cmd_lock);
286
287 list_move_tail(&tmr_p->tmr_list, &drain_tmr_list);
288 }
289 spin_unlock_irqrestore(&dev->se_tmr_lock, flags);
290
291 list_for_each_entry_safe(tmr_p, tmr_pp, &drain_tmr_list, tmr_list) {
292 list_del_init(&tmr_p->tmr_list);
293 cmd = tmr_p->task_cmd;
294
295 pr_debug("LUN_RESET: %s releasing TMR %p Function: 0x%02x,"
296 " Response: 0x%02x, t_state: %d\n",
297 (preempt_and_abort_list) ? "Preempt" : "", tmr_p,
298 tmr_p->function, tmr_p->response, cmd->t_state);
299
300 cancel_work_sync(&cmd->work);
301 transport_wait_for_tasks(cmd);
302
303 if (!transport_cmd_finish_abort(cmd, 1))
304 target_put_sess_cmd(cmd);
305 }
306 }
307
core_tmr_drain_state_list(struct se_device * dev,struct se_cmd * prout_cmd,struct se_session * tmr_sess,int tas,struct list_head * preempt_and_abort_list)308 static void core_tmr_drain_state_list(
309 struct se_device *dev,
310 struct se_cmd *prout_cmd,
311 struct se_session *tmr_sess,
312 int tas,
313 struct list_head *preempt_and_abort_list)
314 {
315 LIST_HEAD(drain_task_list);
316 struct se_session *sess;
317 struct se_cmd *cmd, *next;
318 unsigned long flags;
319 int rc;
320
321 /*
322 * Complete outstanding commands with TASK_ABORTED SAM status.
323 *
324 * This is following sam4r17, section 5.6 Aborting commands, Table 38
325 * for TMR LUN_RESET:
326 *
327 * a) "Yes" indicates that each command that is aborted on an I_T nexus
328 * other than the one that caused the SCSI device condition is
329 * completed with TASK ABORTED status, if the TAS bit is set to one in
330 * the Control mode page (see SPC-4). "No" indicates that no status is
331 * returned for aborted commands.
332 *
333 * d) If the logical unit reset is caused by a particular I_T nexus
334 * (e.g., by a LOGICAL UNIT RESET task management function), then "yes"
335 * (TASK_ABORTED status) applies.
336 *
337 * Otherwise (e.g., if triggered by a hard reset), "no"
338 * (no TASK_ABORTED SAM status) applies.
339 *
340 * Note that this seems to be independent of TAS (Task Aborted Status)
341 * in the Control Mode Page.
342 */
343 spin_lock_irqsave(&dev->execute_task_lock, flags);
344 list_for_each_entry_safe(cmd, next, &dev->state_list, state_list) {
345 /*
346 * For PREEMPT_AND_ABORT usage, only process commands
347 * with a matching reservation key.
348 */
349 if (target_check_cdb_and_preempt(preempt_and_abort_list, cmd))
350 continue;
351
352 /*
353 * Not aborting PROUT PREEMPT_AND_ABORT CDB..
354 */
355 if (prout_cmd == cmd)
356 continue;
357
358 sess = cmd->se_sess;
359 if (WARN_ON_ONCE(!sess))
360 continue;
361
362 spin_lock(&sess->sess_cmd_lock);
363 rc = __target_check_io_state(cmd, tmr_sess, tas);
364 spin_unlock(&sess->sess_cmd_lock);
365 if (!rc)
366 continue;
367
368 list_move_tail(&cmd->state_list, &drain_task_list);
369 cmd->state_active = false;
370 }
371 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
372
373 while (!list_empty(&drain_task_list)) {
374 cmd = list_entry(drain_task_list.next, struct se_cmd, state_list);
375 list_del_init(&cmd->state_list);
376
377 pr_debug("LUN_RESET: %s cmd: %p"
378 " ITT/CmdSN: 0x%08x/0x%08x, i_state: %d, t_state: %d"
379 "cdb: 0x%02x\n",
380 (preempt_and_abort_list) ? "Preempt" : "", cmd,
381 cmd->se_tfo->get_task_tag(cmd), 0,
382 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
383 cmd->t_task_cdb[0]);
384 pr_debug("LUN_RESET: ITT[0x%08x] - pr_res_key: 0x%016Lx"
385 " -- CMD_T_ACTIVE: %d"
386 " CMD_T_STOP: %d CMD_T_SENT: %d\n",
387 cmd->se_tfo->get_task_tag(cmd), cmd->pr_res_key,
388 (cmd->transport_state & CMD_T_ACTIVE) != 0,
389 (cmd->transport_state & CMD_T_STOP) != 0,
390 (cmd->transport_state & CMD_T_SENT) != 0);
391
392 /*
393 * If the command may be queued onto a workqueue cancel it now.
394 *
395 * This is equivalent to removal from the execute queue in the
396 * loop above, but we do it down here given that
397 * cancel_work_sync may block.
398 */
399 cancel_work_sync(&cmd->work);
400 transport_wait_for_tasks(cmd);
401
402 if (!core_tmr_handle_tas_abort(cmd, tas))
403 target_put_sess_cmd(cmd);
404 }
405 }
406
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)407 int core_tmr_lun_reset(
408 struct se_device *dev,
409 struct se_tmr_req *tmr,
410 struct list_head *preempt_and_abort_list,
411 struct se_cmd *prout_cmd)
412 {
413 struct se_node_acl *tmr_nacl = NULL;
414 struct se_portal_group *tmr_tpg = NULL;
415 struct se_session *tmr_sess = NULL;
416 int tas;
417 /*
418 * TASK_ABORTED status bit, this is configurable via ConfigFS
419 * struct se_device attributes. spc4r17 section 7.4.6 Control mode page
420 *
421 * A task aborted status (TAS) bit set to zero specifies that aborted
422 * tasks shall be terminated by the device server without any response
423 * to the application client. A TAS bit set to one specifies that tasks
424 * aborted by the actions of an I_T nexus other than the I_T nexus on
425 * which the command was received shall be completed with TASK ABORTED
426 * status (see SAM-4).
427 */
428 tas = dev->dev_attrib.emulate_tas;
429 /*
430 * Determine if this se_tmr is coming from a $FABRIC_MOD
431 * or struct se_device passthrough..
432 */
433 if (tmr && tmr->task_cmd && tmr->task_cmd->se_sess) {
434 tmr_sess = tmr->task_cmd->se_sess;
435 tmr_nacl = tmr_sess->se_node_acl;
436 tmr_tpg = tmr_sess->se_tpg;
437 if (tmr_nacl && tmr_tpg) {
438 pr_debug("LUN_RESET: TMR caller fabric: %s"
439 " initiator port %s\n",
440 tmr_tpg->se_tpg_tfo->get_fabric_name(),
441 tmr_nacl->initiatorname);
442 }
443 }
444 pr_debug("LUN_RESET: %s starting for [%s], tas: %d\n",
445 (preempt_and_abort_list) ? "Preempt" : "TMR",
446 dev->transport->name, tas);
447
448 core_tmr_drain_tmr_list(dev, tmr, preempt_and_abort_list);
449 core_tmr_drain_state_list(dev, prout_cmd, tmr_sess, tas,
450 preempt_and_abort_list);
451
452 /*
453 * Clear any legacy SPC-2 reservation when called during
454 * LOGICAL UNIT RESET
455 */
456 if (!preempt_and_abort_list &&
457 (dev->dev_reservation_flags & DRF_SPC2_RESERVATIONS)) {
458 spin_lock(&dev->dev_reservation_lock);
459 dev->dev_reserved_node_acl = NULL;
460 dev->dev_reservation_flags &= ~DRF_SPC2_RESERVATIONS;
461 spin_unlock(&dev->dev_reservation_lock);
462 pr_debug("LUN_RESET: SCSI-2 Released reservation\n");
463 }
464
465 atomic_long_inc(&dev->num_resets);
466
467 pr_debug("LUN_RESET: %s for [%s] Complete\n",
468 (preempt_and_abort_list) ? "Preempt" : "TMR",
469 dev->transport->name);
470 return 0;
471 }
472
473