Line data Source code
1 : /*
2 : * scsi_error.c Copyright (C) 1997 Eric Youngdale
3 : *
4 : * SCSI error/timeout handling
5 : * Initial versions: Eric Youngdale. Based upon conversations with
6 : * Leonard Zubkoff and David Miller at Linux Expo,
7 : * ideas originating from all over the place.
8 : *
9 : * Restructured scsi_unjam_host and associated functions.
10 : * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
11 : *
12 : * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
13 : * minor cleanups.
14 : * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
15 : */
16 :
17 : #include <linux/module.h>
18 : #include <linux/sched.h>
19 : #include <linux/timer.h>
20 : #include <linux/string.h>
21 : #include <linux/kernel.h>
22 : #include <linux/freezer.h>
23 : #include <linux/kthread.h>
24 : #include <linux/interrupt.h>
25 : #include <linux/blkdev.h>
26 : #include <linux/delay.h>
27 :
28 : #include <scsi/scsi.h>
29 : #include <scsi/scsi_cmnd.h>
30 : #include <scsi/scsi_dbg.h>
31 : #include <scsi/scsi_device.h>
32 : #include <scsi/scsi_eh.h>
33 : #include <scsi/scsi_transport.h>
34 : #include <scsi/scsi_host.h>
35 : #include <scsi/scsi_ioctl.h>
36 :
37 : #include "scsi_priv.h"
38 : #include "scsi_logging.h"
39 : #include "scsi_transport_api.h"
40 :
41 : #define SENSE_TIMEOUT (10*HZ)
42 :
43 : /*
44 : * These should *probably* be handled by the host itself.
45 : * Since it is allowed to sleep, it probably should.
46 : */
47 : #define BUS_RESET_SETTLE_TIME (10)
48 : #define HOST_RESET_SETTLE_TIME (10)
49 :
50 : /* called with shost->host_lock held */
51 : void scsi_eh_wakeup(struct Scsi_Host *shost)
52 : {
53 0 : if (shost->host_busy == shost->host_failed) {
54 0 : wake_up_process(shost->ehandler);
55 0 : SCSI_LOG_ERROR_RECOVERY(5,
56 : printk("Waking error handler thread\n"));
57 : }
58 : }
59 :
60 : /**
61 : * scsi_schedule_eh - schedule EH for SCSI host
62 : * @shost: SCSI host to invoke error handling on.
63 : *
64 : * Schedule SCSI EH without scmd.
65 : */
66 : void scsi_schedule_eh(struct Scsi_Host *shost)
67 : {
68 0 : unsigned long flags;
69 0 :
70 0 : spin_lock_irqsave(shost->host_lock, flags);
71 0 :
72 0 : if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
73 : scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
74 0 : shost->host_eh_scheduled++;
75 0 : scsi_eh_wakeup(shost);
76 : }
77 :
78 0 : spin_unlock_irqrestore(shost->host_lock, flags);
79 0 : }
80 : EXPORT_SYMBOL_GPL(scsi_schedule_eh);
81 :
82 : /**
83 : * scsi_eh_scmd_add - add scsi cmd to error handling.
84 : * @scmd: scmd to run eh on.
85 : * @eh_flag: optional SCSI_EH flag.
86 : *
87 : * Return value:
88 : * 0 on failure.
89 : */
90 : int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
91 : {
92 0 : struct Scsi_Host *shost = scmd->device->host;
93 0 : unsigned long flags;
94 0 : int ret = 0;
95 0 :
96 0 : if (!shost->ehandler)
97 0 : return 0;
98 :
99 0 : spin_lock_irqsave(shost->host_lock, flags);
100 0 : if (scsi_host_set_state(shost, SHOST_RECOVERY))
101 0 : if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
102 0 : goto out_unlock;
103 :
104 0 : ret = 1;
105 0 : scmd->eh_eflags |= eh_flag;
106 0 : list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
107 0 : shost->host_failed++;
108 0 : scsi_eh_wakeup(shost);
109 : out_unlock:
110 0 : spin_unlock_irqrestore(shost->host_lock, flags);
111 0 : return ret;
112 : }
113 :
114 : /**
115 : * scsi_times_out - Timeout function for normal scsi commands.
116 : * @req: request that is timing out.
117 : *
118 : * Notes:
119 : * We do not need to lock this. There is the potential for a race
120 : * only in that the normal completion handling might run, but if the
121 : * normal completion function determines that the timer has already
122 : * fired, then it mustn't do anything.
123 : */
124 : enum blk_eh_timer_return scsi_times_out(struct request *req)
125 : {
126 0 : struct scsi_cmnd *scmd = req->special;
127 0 : enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
128 0 :
129 0 : scsi_log_completion(scmd, TIMEOUT_ERROR);
130 0 :
131 0 : if (scmd->device->host->transportt->eh_timed_out)
132 0 : rtn = scmd->device->host->transportt->eh_timed_out(scmd);
133 0 : else if (scmd->device->host->hostt->eh_timed_out)
134 0 : rtn = scmd->device->host->hostt->eh_timed_out(scmd);
135 :
136 0 : if (unlikely(rtn == BLK_EH_NOT_HANDLED &&
137 : !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
138 0 : scmd->result |= DID_TIME_OUT << 16;
139 0 : rtn = BLK_EH_HANDLED;
140 : }
141 :
142 0 : return rtn;
143 : }
144 :
145 : /**
146 : * scsi_block_when_processing_errors - Prevent cmds from being queued.
147 : * @sdev: Device on which we are performing recovery.
148 : *
149 : * Description:
150 : * We block until the host is out of error recovery, and then check to
151 : * see whether the host or the device is offline.
152 : *
153 : * Return value:
154 : * 0 when dev was taken offline by error recovery. 1 OK to proceed.
155 : */
156 : int scsi_block_when_processing_errors(struct scsi_device *sdev)
157 : {
158 0 : int online;
159 0 :
160 0 : wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
161 0 :
162 0 : online = scsi_device_online(sdev);
163 :
164 : SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__,
165 : online));
166 :
167 0 : return online;
168 : }
169 : EXPORT_SYMBOL(scsi_block_when_processing_errors);
170 :
171 : #ifdef CONFIG_SCSI_LOGGING
172 : /**
173 : * scsi_eh_prt_fail_stats - Log info on failures.
174 : * @shost: scsi host being recovered.
175 : * @work_q: Queue of scsi cmds to process.
176 : */
177 : static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
178 : struct list_head *work_q)
179 : {
180 : struct scsi_cmnd *scmd;
181 : struct scsi_device *sdev;
182 : int total_failures = 0;
183 : int cmd_failed = 0;
184 : int cmd_cancel = 0;
185 : int devices_failed = 0;
186 :
187 : shost_for_each_device(sdev, shost) {
188 : list_for_each_entry(scmd, work_q, eh_entry) {
189 : if (scmd->device == sdev) {
190 : ++total_failures;
191 : if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
192 : ++cmd_cancel;
193 : else
194 : ++cmd_failed;
195 : }
196 : }
197 :
198 : if (cmd_cancel || cmd_failed) {
199 : SCSI_LOG_ERROR_RECOVERY(3,
200 : sdev_printk(KERN_INFO, sdev,
201 : "%s: cmds failed: %d, cancel: %d\n",
202 : __func__, cmd_failed,
203 : cmd_cancel));
204 : cmd_cancel = 0;
205 : cmd_failed = 0;
206 : ++devices_failed;
207 : }
208 : }
209 :
210 : SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
211 : " devices require eh work\n",
212 : total_failures, devices_failed));
213 : }
214 : #endif
215 :
216 : /**
217 : * scsi_check_sense - Examine scsi cmd sense
218 : * @scmd: Cmd to have sense checked.
219 : *
220 : * Return value:
221 : * SUCCESS or FAILED or NEEDS_RETRY
222 : *
223 : * Notes:
224 : * When a deferred error is detected the current command has
225 : * not been executed and needs retrying.
226 : */
227 : static int scsi_check_sense(struct scsi_cmnd *scmd)
228 : {
229 0 : struct scsi_device *sdev = scmd->device;
230 0 : struct scsi_sense_hdr sshdr;
231 0 :
232 0 : if (! scsi_command_normalize_sense(scmd, &sshdr))
233 0 : return FAILED; /* no valid sense data */
234 :
235 0 : if (scsi_sense_is_deferred(&sshdr))
236 0 : return NEEDS_RETRY;
237 :
238 0 : if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh &&
239 : sdev->scsi_dh_data->scsi_dh->check_sense) {
240 : int rc;
241 :
242 0 : rc = sdev->scsi_dh_data->scsi_dh->check_sense(sdev, &sshdr);
243 0 : if (rc != SCSI_RETURN_NOT_HANDLED)
244 0 : return rc;
245 : /* handler does not care. Drop down to default handling */
246 : }
247 :
248 : /*
249 : * Previous logic looked for FILEMARK, EOM or ILI which are
250 : * mainly associated with tapes and returned SUCCESS.
251 : */
252 0 : if (sshdr.response_code == 0x70) {
253 : /* fixed format */
254 0 : if (scmd->sense_buffer[2] & 0xe0)
255 0 : return SUCCESS;
256 : } else {
257 : /*
258 : * descriptor format: look for "stream commands sense data
259 : * descriptor" (see SSC-3). Assume single sense data
260 : * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
261 : */
262 0 : if ((sshdr.additional_length > 3) &&
263 : (scmd->sense_buffer[8] == 0x4) &&
264 : (scmd->sense_buffer[11] & 0xe0))
265 0 : return SUCCESS;
266 : }
267 :
268 : switch (sshdr.sense_key) {
269 0 : case NO_SENSE:
270 0 : return SUCCESS;
271 0 : case RECOVERED_ERROR:
272 0 : return /* soft_error */ SUCCESS;
273 0 :
274 0 : case ABORTED_COMMAND:
275 0 : if (sshdr.asc == 0x10) /* DIF */
276 0 : return SUCCESS;
277 :
278 0 : return NEEDS_RETRY;
279 0 : case NOT_READY:
280 0 : case UNIT_ATTENTION:
281 : /*
282 : * if we are expecting a cc/ua because of a bus reset that we
283 : * performed, treat this just as a retry. otherwise this is
284 : * information that we should pass up to the upper-level driver
285 : * so that we can deal with it there.
286 : */
287 0 : if (scmd->device->expecting_cc_ua) {
288 0 : scmd->device->expecting_cc_ua = 0;
289 0 : return NEEDS_RETRY;
290 : }
291 : /*
292 : * if the device is in the process of becoming ready, we
293 : * should retry.
294 : */
295 0 : if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
296 0 : return NEEDS_RETRY;
297 : /*
298 : * if the device is not started, we need to wake
299 : * the error handler to start the motor
300 : */
301 0 : if (scmd->device->allow_restart &&
302 : (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
303 0 : return FAILED;
304 :
305 0 : if (blk_barrier_rq(scmd->request))
306 : /*
307 : * barrier requests should always retry on UA
308 : * otherwise block will get a spurious error
309 : */
310 0 : return NEEDS_RETRY;
311 : else
312 : /*
313 : * for normal (non barrier) commands, pass the
314 : * UA upwards for a determination in the
315 : * completion functions
316 : */
317 0 : return SUCCESS;
318 :
319 0 : /* these three are not supported */
320 0 : case COPY_ABORTED:
321 0 : case VOLUME_OVERFLOW:
322 0 : case MISCOMPARE:
323 0 : return SUCCESS;
324 0 :
325 0 : case MEDIUM_ERROR:
326 0 : if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
327 : sshdr.asc == 0x13 || /* AMNF DATA FIELD */
328 : sshdr.asc == 0x14) { /* RECORD NOT FOUND */
329 0 : return SUCCESS;
330 : }
331 0 : return NEEDS_RETRY;
332 0 :
333 0 : case HARDWARE_ERROR:
334 0 : if (scmd->device->retry_hwerror)
335 0 : return ADD_TO_MLQUEUE;
336 : else
337 0 : return SUCCESS;
338 :
339 0 : case ILLEGAL_REQUEST:
340 : case BLANK_CHECK:
341 : case DATA_PROTECT:
342 : default:
343 0 : return SUCCESS;
344 : }
345 : }
346 :
347 : static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
348 : {
349 0 : struct scsi_host_template *sht = sdev->host->hostt;
350 0 : struct scsi_device *tmp_sdev;
351 :
352 0 : if (!sht->change_queue_depth ||
353 : sdev->queue_depth >= sdev->max_queue_depth)
354 0 : return;
355 :
356 0 : if (time_before(jiffies,
357 : sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
358 0 : return;
359 :
360 0 : if (time_before(jiffies,
361 : sdev->last_queue_full_time + sdev->queue_ramp_up_period))
362 0 : return;
363 :
364 : /*
365 : * Walk all devices of a target and do
366 : * ramp up on them.
367 : */
368 0 : shost_for_each_device(tmp_sdev, sdev->host) {
369 0 : if (tmp_sdev->channel != sdev->channel ||
370 0 : tmp_sdev->id != sdev->id ||
371 : tmp_sdev->queue_depth == sdev->max_queue_depth)
372 0 : continue;
373 : /*
374 : * call back into LLD to increase queue_depth by one
375 0 : * with ramp up reason code.
376 : */
377 0 : sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1,
378 : SCSI_QDEPTH_RAMP_UP);
379 0 : sdev->last_queue_ramp_up = jiffies;
380 : }
381 0 : }
382 :
383 : static void scsi_handle_queue_full(struct scsi_device *sdev)
384 : {
385 0 : struct scsi_host_template *sht = sdev->host->hostt;
386 0 : struct scsi_device *tmp_sdev;
387 :
388 0 : if (!sht->change_queue_depth)
389 0 : return;
390 :
391 0 : shost_for_each_device(tmp_sdev, sdev->host) {
392 0 : if (tmp_sdev->channel != sdev->channel ||
393 0 : tmp_sdev->id != sdev->id)
394 0 : continue;
395 : /*
396 : * We do not know the number of commands that were at
397 : * the device when we got the queue full so we start
398 0 : * from the highest possible value and work our way down.
399 : */
400 0 : sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth - 1,
401 : SCSI_QDEPTH_QFULL);
402 0 : }
403 : }
404 :
405 : /**
406 : * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
407 : * @scmd: SCSI cmd to examine.
408 : *
409 : * Notes:
410 : * This is *only* called when we are examining the status of commands
411 : * queued during error recovery. the main difference here is that we
412 : * don't allow for the possibility of retries here, and we are a lot
413 : * more restrictive about what we consider acceptable.
414 : */
415 : static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
416 : {
417 0 : /*
418 0 : * first check the host byte, to see if there is anything in there
419 : * that would indicate what we need to do.
420 : */
421 0 : if (host_byte(scmd->result) == DID_RESET) {
422 : /*
423 : * rats. we are already in the error handler, so we now
424 : * get to try and figure out what to do next. if the sense
425 : * is valid, we have a pretty good idea of what to do.
426 : * if not, we mark it as FAILED.
427 : */
428 0 : return scsi_check_sense(scmd);
429 : }
430 0 : if (host_byte(scmd->result) != DID_OK)
431 0 : return FAILED;
432 :
433 : /*
434 : * next, check the message byte.
435 : */
436 0 : if (msg_byte(scmd->result) != COMMAND_COMPLETE)
437 0 : return FAILED;
438 :
439 : /*
440 : * now, check the status byte to see if this indicates
441 : * anything special.
442 : */
443 : switch (status_byte(scmd->result)) {
444 0 : case GOOD:
445 0 : scsi_handle_queue_ramp_up(scmd->device);
446 0 : case COMMAND_TERMINATED:
447 0 : return SUCCESS;
448 0 : case CHECK_CONDITION:
449 0 : return scsi_check_sense(scmd);
450 0 : case CONDITION_GOOD:
451 0 : case INTERMEDIATE_GOOD:
452 0 : case INTERMEDIATE_C_GOOD:
453 : /*
454 : * who knows? FIXME(eric)
455 : */
456 0 : return SUCCESS;
457 0 : case RESERVATION_CONFLICT:
458 : /*
459 : * let issuer deal with this, it could be just fine
460 : */
461 0 : return SUCCESS;
462 0 : case QUEUE_FULL:
463 0 : scsi_handle_queue_full(scmd->device);
464 : /* fall through */
465 0 : case BUSY:
466 0 : default:
467 0 : return FAILED;
468 : }
469 : return FAILED;
470 : }
471 :
472 : /**
473 : * scsi_eh_done - Completion function for error handling.
474 : * @scmd: Cmd that is done.
475 : */
476 : static void scsi_eh_done(struct scsi_cmnd *scmd)
477 : {
478 0 : struct completion *eh_action;
479 :
480 : SCSI_LOG_ERROR_RECOVERY(3,
481 : printk("%s scmd: %p result: %x\n",
482 : __func__, scmd, scmd->result));
483 :
484 0 : eh_action = scmd->device->host->eh_action;
485 0 : if (eh_action)
486 0 : complete(eh_action);
487 0 : }
488 :
489 : /**
490 : * scsi_try_host_reset - ask host adapter to reset itself
491 : * @scmd: SCSI cmd to send hsot reset.
492 : */
493 : static int scsi_try_host_reset(struct scsi_cmnd *scmd)
494 : {
495 0 : unsigned long flags;
496 0 : int rtn;
497 0 :
498 0 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
499 : __func__));
500 :
501 0 : if (!scmd->device->host->hostt->eh_host_reset_handler)
502 0 : return FAILED;
503 :
504 0 : rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
505 :
506 0 : if (rtn == SUCCESS) {
507 0 : if (!scmd->device->host->hostt->skip_settle_delay)
508 0 : ssleep(HOST_RESET_SETTLE_TIME);
509 0 : spin_lock_irqsave(scmd->device->host->host_lock, flags);
510 0 : scsi_report_bus_reset(scmd->device->host,
511 : scmd_channel(scmd));
512 0 : spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
513 : }
514 :
515 0 : return rtn;
516 : }
517 :
518 : /**
519 : * scsi_try_bus_reset - ask host to perform a bus reset
520 : * @scmd: SCSI cmd to send bus reset.
521 : */
522 : static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
523 : {
524 0 : unsigned long flags;
525 0 : int rtn;
526 0 :
527 0 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
528 : __func__));
529 :
530 0 : if (!scmd->device->host->hostt->eh_bus_reset_handler)
531 0 : return FAILED;
532 :
533 0 : rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
534 :
535 0 : if (rtn == SUCCESS) {
536 0 : if (!scmd->device->host->hostt->skip_settle_delay)
537 0 : ssleep(BUS_RESET_SETTLE_TIME);
538 0 : spin_lock_irqsave(scmd->device->host->host_lock, flags);
539 0 : scsi_report_bus_reset(scmd->device->host,
540 : scmd_channel(scmd));
541 0 : spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
542 : }
543 :
544 0 : return rtn;
545 : }
546 :
547 : static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
548 : {
549 0 : sdev->was_reset = 1;
550 0 : sdev->expecting_cc_ua = 1;
551 0 : }
552 :
553 : /**
554 : * scsi_try_target_reset - Ask host to perform a target reset
555 : * @scmd: SCSI cmd used to send a target reset
556 : *
557 : * Notes:
558 : * There is no timeout for this operation. if this operation is
559 : * unreliable for a given host, then the host itself needs to put a
560 : * timer on it, and set the host back to a consistent state prior to
561 : * returning.
562 : */
563 : static int scsi_try_target_reset(struct scsi_cmnd *scmd)
564 : {
565 0 : unsigned long flags;
566 0 : int rtn;
567 0 :
568 0 : if (!scmd->device->host->hostt->eh_target_reset_handler)
569 0 : return FAILED;
570 :
571 0 : rtn = scmd->device->host->hostt->eh_target_reset_handler(scmd);
572 0 : if (rtn == SUCCESS) {
573 0 : spin_lock_irqsave(scmd->device->host->host_lock, flags);
574 0 : __starget_for_each_device(scsi_target(scmd->device), NULL,
575 : __scsi_report_device_reset);
576 0 : spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
577 : }
578 :
579 0 : return rtn;
580 : }
581 :
582 : /**
583 : * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
584 : * @scmd: SCSI cmd used to send BDR
585 : *
586 : * Notes:
587 : * There is no timeout for this operation. if this operation is
588 : * unreliable for a given host, then the host itself needs to put a
589 : * timer on it, and set the host back to a consistent state prior to
590 : * returning.
591 : */
592 : static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
593 : {
594 0 : int rtn;
595 :
596 0 : if (!scmd->device->host->hostt->eh_device_reset_handler)
597 0 : return FAILED;
598 :
599 0 : rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
600 0 : if (rtn == SUCCESS)
601 0 : __scsi_report_device_reset(scmd->device, NULL);
602 0 : return rtn;
603 : }
604 :
605 : static int __scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
606 : {
607 0 : if (!scmd->device->host->hostt->eh_abort_handler)
608 0 : return FAILED;
609 :
610 0 : return scmd->device->host->hostt->eh_abort_handler(scmd);
611 : }
612 :
613 : /**
614 : * scsi_try_to_abort_cmd - Ask host to abort a running command.
615 : * @scmd: SCSI cmd to abort from Lower Level.
616 : *
617 : * Notes:
618 : * This function will not return until the user's completion function
619 : * has been called. there is no timeout on this operation. if the
620 : * author of the low-level driver wishes this operation to be timed,
621 : * they can provide this facility themselves. helper functions in
622 : * scsi_error.c can be supplied to make this easier to do.
623 : */
624 : static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
625 : {
626 0 : /*
627 : * scsi_done was called just after the command timed out and before
628 : * we had a chance to process it. (db)
629 : */
630 0 : if (scmd->serial_number == 0)
631 0 : return SUCCESS;
632 0 : return __scsi_try_to_abort_cmd(scmd);
633 : }
634 :
635 : static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
636 : {
637 0 : if (__scsi_try_to_abort_cmd(scmd) != SUCCESS)
638 0 : if (scsi_try_bus_device_reset(scmd) != SUCCESS)
639 0 : if (scsi_try_target_reset(scmd) != SUCCESS)
640 0 : if (scsi_try_bus_reset(scmd) != SUCCESS)
641 0 : scsi_try_host_reset(scmd);
642 0 : }
643 :
644 : /**
645 : * scsi_eh_prep_cmnd - Save a scsi command info as part of error recory
646 : * @scmd: SCSI command structure to hijack
647 : * @ses: structure to save restore information
648 : * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
649 : * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
650 : * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
651 : *
652 : * This function is used to save a scsi command information before re-execution
653 : * as part of the error recovery process. If @sense_bytes is 0 the command
654 : * sent must be one that does not transfer any data. If @sense_bytes != 0
655 : * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
656 : * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
657 : */
658 : void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
659 : unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
660 : {
661 0 : struct scsi_device *sdev = scmd->device;
662 0 :
663 0 : /*
664 0 : * We need saved copies of a number of fields - this is because
665 : * error handling may need to overwrite these with different values
666 : * to run different commands, and once error handling is complete,
667 : * we will need to restore these values prior to running the actual
668 : * command.
669 : */
670 0 : ses->cmd_len = scmd->cmd_len;
671 0 : ses->cmnd = scmd->cmnd;
672 0 : ses->data_direction = scmd->sc_data_direction;
673 0 : ses->sdb = scmd->sdb;
674 0 : ses->next_rq = scmd->request->next_rq;
675 0 : ses->result = scmd->result;
676 0 : ses->underflow = scmd->underflow;
677 0 : ses->prot_op = scmd->prot_op;
678 :
679 0 : scmd->prot_op = SCSI_PROT_NORMAL;
680 0 : scmd->cmnd = ses->eh_cmnd;
681 0 : memset(scmd->cmnd, 0, BLK_MAX_CDB);
682 0 : memset(&scmd->sdb, 0, sizeof(scmd->sdb));
683 0 : scmd->request->next_rq = NULL;
684 :
685 0 : if (sense_bytes) {
686 0 : scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
687 : sense_bytes);
688 0 : sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
689 : scmd->sdb.length);
690 0 : scmd->sdb.table.sgl = &ses->sense_sgl;
691 0 : scmd->sc_data_direction = DMA_FROM_DEVICE;
692 0 : scmd->sdb.table.nents = 1;
693 0 : scmd->cmnd[0] = REQUEST_SENSE;
694 0 : scmd->cmnd[4] = scmd->sdb.length;
695 0 : scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
696 : } else {
697 0 : scmd->sc_data_direction = DMA_NONE;
698 0 : if (cmnd) {
699 0 : BUG_ON(cmnd_size > BLK_MAX_CDB);
700 0 : memcpy(scmd->cmnd, cmnd, cmnd_size);
701 0 : scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
702 : }
703 : }
704 :
705 0 : scmd->underflow = 0;
706 :
707 0 : if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
708 0 : scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
709 : (sdev->lun << 5 & 0xe0);
710 :
711 : /*
712 : * Zero the sense buffer. The scsi spec mandates that any
713 : * untransferred sense data should be interpreted as being zero.
714 : */
715 0 : memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
716 0 : }
717 : EXPORT_SYMBOL(scsi_eh_prep_cmnd);
718 :
719 : /**
720 : * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recory
721 : * @scmd: SCSI command structure to restore
722 : * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
723 : *
724 : * Undo any damage done by above scsi_eh_prep_cmnd().
725 : */
726 : void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
727 : {
728 : /*
729 : * Restore original data
730 : */
731 0 : scmd->cmd_len = ses->cmd_len;
732 0 : scmd->cmnd = ses->cmnd;
733 0 : scmd->sc_data_direction = ses->data_direction;
734 0 : scmd->sdb = ses->sdb;
735 0 : scmd->request->next_rq = ses->next_rq;
736 0 : scmd->result = ses->result;
737 0 : scmd->underflow = ses->underflow;
738 0 : scmd->prot_op = ses->prot_op;
739 0 : }
740 : EXPORT_SYMBOL(scsi_eh_restore_cmnd);
741 :
742 : /**
743 : * scsi_send_eh_cmnd - submit a scsi command as part of error recory
744 : * @scmd: SCSI command structure to hijack
745 : * @cmnd: CDB to send
746 : * @cmnd_size: size in bytes of @cmnd
747 : * @timeout: timeout for this request
748 : * @sense_bytes: size of sense data to copy or 0
749 : *
750 : * This function is used to send a scsi command down to a target device
751 : * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
752 : *
753 : * Return value:
754 : * SUCCESS or FAILED or NEEDS_RETRY
755 : */
756 : static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
757 : int cmnd_size, int timeout, unsigned sense_bytes)
758 : {
759 0 : struct scsi_device *sdev = scmd->device;
760 0 : struct Scsi_Host *shost = sdev->host;
761 0 : DECLARE_COMPLETION_ONSTACK(done);
762 0 : unsigned long timeleft;
763 0 : unsigned long flags;
764 0 : struct scsi_eh_save ses;
765 0 : int rtn;
766 0 :
767 0 : scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
768 0 : shost->eh_action = &done;
769 :
770 0 : spin_lock_irqsave(shost->host_lock, flags);
771 0 : scsi_log_send(scmd);
772 0 : shost->hostt->queuecommand(scmd, scsi_eh_done);
773 0 : spin_unlock_irqrestore(shost->host_lock, flags);
774 :
775 0 : timeleft = wait_for_completion_timeout(&done, timeout);
776 :
777 0 : shost->eh_action = NULL;
778 :
779 0 : scsi_log_completion(scmd, SUCCESS);
780 :
781 : SCSI_LOG_ERROR_RECOVERY(3,
782 : printk("%s: scmd: %p, timeleft: %ld\n",
783 : __func__, scmd, timeleft));
784 :
785 : /*
786 : * If there is time left scsi_eh_done got called, and we will
787 : * examine the actual status codes to see whether the command
788 : * actually did complete normally, else tell the host to forget
789 : * about this command.
790 : */
791 0 : if (timeleft) {
792 0 : rtn = scsi_eh_completed_normally(scmd);
793 : SCSI_LOG_ERROR_RECOVERY(3,
794 : printk("%s: scsi_eh_completed_normally %x\n",
795 : __func__, rtn));
796 :
797 : switch (rtn) {
798 0 : case SUCCESS:
799 0 : case NEEDS_RETRY:
800 0 : case FAILED:
801 0 : break;
802 0 : case ADD_TO_MLQUEUE:
803 0 : rtn = NEEDS_RETRY;
804 0 : break;
805 0 : default:
806 0 : rtn = FAILED;
807 0 : break;
808 0 : }
809 : } else {
810 0 : scsi_abort_eh_cmnd(scmd);
811 0 : rtn = FAILED;
812 : }
813 :
814 0 : scsi_eh_restore_cmnd(scmd, &ses);
815 0 : return rtn;
816 : }
817 :
818 : /**
819 : * scsi_request_sense - Request sense data from a particular target.
820 : * @scmd: SCSI cmd for request sense.
821 : *
822 : * Notes:
823 : * Some hosts automatically obtain this information, others require
824 : * that we obtain it on our own. This function will *not* return until
825 : * the command either times out, or it completes.
826 : */
827 : static int scsi_request_sense(struct scsi_cmnd *scmd)
828 : {
829 0 : return scsi_send_eh_cmnd(scmd, NULL, 0, SENSE_TIMEOUT, ~0);
830 : }
831 :
832 : /**
833 : * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
834 : * @scmd: Original SCSI cmd that eh has finished.
835 : * @done_q: Queue for processed commands.
836 : *
837 : * Notes:
838 : * We don't want to use the normal command completion while we are are
839 : * still handling errors - it may cause other commands to be queued,
840 : * and that would disturb what we are doing. Thus we really want to
841 : * keep a list of pending commands for final completion, and once we
842 : * are ready to leave error handling we handle completion for real.
843 : */
844 : void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
845 : {
846 0 : scmd->device->host->host_failed--;
847 0 : scmd->eh_eflags = 0;
848 0 : list_move_tail(&scmd->eh_entry, done_q);
849 0 : }
850 : EXPORT_SYMBOL(scsi_eh_finish_cmd);
851 :
852 : /**
853 : * scsi_eh_get_sense - Get device sense data.
854 : * @work_q: Queue of commands to process.
855 : * @done_q: Queue of processed commands.
856 : *
857 : * Description:
858 : * See if we need to request sense information. if so, then get it
859 : * now, so we have a better idea of what to do.
860 : *
861 : * Notes:
862 : * This has the unfortunate side effect that if a shost adapter does
863 : * not automatically request sense information, we end up shutting
864 : * it down before we request it.
865 : *
866 : * All drivers should request sense information internally these days,
867 : * so for now all I have to say is tough noogies if you end up in here.
868 : *
869 : * XXX: Long term this code should go away, but that needs an audit of
870 : * all LLDDs first.
871 : */
872 : int scsi_eh_get_sense(struct list_head *work_q,
873 : struct list_head *done_q)
874 0 : {
875 0 : struct scsi_cmnd *scmd, *next;
876 0 : int rtn;
877 0 :
878 0 : list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
879 0 : if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
880 0 : SCSI_SENSE_VALID(scmd))
881 0 : continue;
882 :
883 : SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
884 : "%s: requesting sense\n",
885 : current->comm));
886 0 : rtn = scsi_request_sense(scmd);
887 0 : if (rtn != SUCCESS)
888 0 : continue;
889 :
890 : SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
891 : " result %x\n", scmd,
892 : scmd->result));
893 : SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
894 :
895 0 : rtn = scsi_decide_disposition(scmd);
896 :
897 : /*
898 : * if the result was normal, then just pass it along to the
899 : * upper level.
900 : */
901 0 : if (rtn == SUCCESS)
902 : /* we don't want this command reissued, just
903 : * finished with the sense data, so set
904 : * retries to the max allowed to ensure it
905 : * won't get reissued */
906 0 : scmd->retries = scmd->allowed;
907 0 : else if (rtn != NEEDS_RETRY)
908 0 : continue;
909 :
910 0 : scsi_eh_finish_cmd(scmd, done_q);
911 : }
912 0 :
913 0 : return list_empty(work_q);
914 : }
915 : EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
916 :
917 : /**
918 : * scsi_eh_tur - Send TUR to device.
919 : * @scmd: &scsi_cmnd to send TUR
920 : *
921 : * Return value:
922 : * 0 - Device is ready. 1 - Device NOT ready.
923 : */
924 : static int scsi_eh_tur(struct scsi_cmnd *scmd)
925 : {
926 0 : static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
927 0 : int retry_cnt = 1, rtn;
928 0 :
929 0 : retry_tur:
930 0 : rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
931 :
932 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
933 : __func__, scmd, rtn));
934 :
935 : switch (rtn) {
936 0 : case NEEDS_RETRY:
937 0 : if (retry_cnt--)
938 0 : goto retry_tur;
939 : /*FALLTHRU*/
940 0 : case SUCCESS:
941 0 : return 0;
942 0 : default:
943 0 : return 1;
944 : }
945 : }
946 :
947 : /**
948 : * scsi_eh_abort_cmds - abort pending commands.
949 : * @work_q: &list_head for pending commands.
950 : * @done_q: &list_head for processed commands.
951 : *
952 : * Decription:
953 : * Try and see whether or not it makes sense to try and abort the
954 : * running command. This only works out to be the case if we have one
955 : * command that has timed out. If the command simply failed, it makes
956 : * no sense to try and abort the command, since as far as the shost
957 : * adapter is concerned, it isn't running.
958 : */
959 : static int scsi_eh_abort_cmds(struct list_head *work_q,
960 : struct list_head *done_q)
961 0 : {
962 0 : struct scsi_cmnd *scmd, *next;
963 0 : int rtn;
964 0 :
965 0 : list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
966 0 : if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
967 0 : continue;
968 0 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
969 0 : "0x%p\n", current->comm,
970 : scmd));
971 0 : rtn = scsi_try_to_abort_cmd(scmd);
972 0 : if (rtn == SUCCESS) {
973 0 : scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
974 0 : if (!scsi_device_online(scmd->device) ||
975 : !scsi_eh_tur(scmd)) {
976 0 : scsi_eh_finish_cmd(scmd, done_q);
977 : }
978 :
979 : } else
980 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
981 : " cmd failed:"
982 : "0x%p\n",
983 : current->comm,
984 : scmd));
985 0 : }
986 :
987 0 : return list_empty(work_q);
988 : }
989 :
990 : /**
991 : * scsi_eh_try_stu - Send START_UNIT to device.
992 : * @scmd: &scsi_cmnd to send START_UNIT
993 : *
994 : * Return value:
995 : * 0 - Device is ready. 1 - Device NOT ready.
996 : */
997 : static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
998 : {
999 0 : static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1000 0 :
1001 0 : if (scmd->device->allow_restart) {
1002 0 : int i, rtn = NEEDS_RETRY;
1003 :
1004 0 : for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1005 0 : rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1006 0 :
1007 0 : if (rtn == SUCCESS)
1008 0 : return 0;
1009 : }
1010 :
1011 0 : return 1;
1012 : }
1013 :
1014 : /**
1015 : * scsi_eh_stu - send START_UNIT if needed
1016 : * @shost: &scsi host being recovered.
1017 : * @work_q: &list_head for pending commands.
1018 : * @done_q: &list_head for processed commands.
1019 : *
1020 : * Notes:
1021 : * If commands are failing due to not ready, initializing command required,
1022 : * try revalidating the device, which will end up sending a start unit.
1023 : */
1024 : static int scsi_eh_stu(struct Scsi_Host *shost,
1025 : struct list_head *work_q,
1026 0 : struct list_head *done_q)
1027 0 : {
1028 0 : struct scsi_cmnd *scmd, *stu_scmd, *next;
1029 0 : struct scsi_device *sdev;
1030 0 :
1031 0 : shost_for_each_device(sdev, shost) {
1032 0 : stu_scmd = NULL;
1033 0 : list_for_each_entry(scmd, work_q, eh_entry)
1034 0 : if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1035 0 : scsi_check_sense(scmd) == FAILED ) {
1036 0 : stu_scmd = scmd;
1037 0 : break;
1038 0 : }
1039 0 :
1040 0 : if (!stu_scmd)
1041 0 : continue;
1042 :
1043 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
1044 : " 0x%p\n", current->comm, sdev));
1045 :
1046 0 : if (!scsi_eh_try_stu(stu_scmd)) {
1047 0 : if (!scsi_device_online(sdev) ||
1048 0 : !scsi_eh_tur(stu_scmd)) {
1049 0 : list_for_each_entry_safe(scmd, next,
1050 0 : work_q, eh_entry) {
1051 0 : if (scmd->device == sdev)
1052 0 : scsi_eh_finish_cmd(scmd, done_q);
1053 : }
1054 : }
1055 : } else {
1056 : SCSI_LOG_ERROR_RECOVERY(3,
1057 : printk("%s: START_UNIT failed to sdev:"
1058 : " 0x%p\n", current->comm, sdev));
1059 : }
1060 : }
1061 :
1062 0 : return list_empty(work_q);
1063 0 : }
1064 :
1065 :
1066 : /**
1067 : * scsi_eh_bus_device_reset - send bdr if needed
1068 : * @shost: scsi host being recovered.
1069 : * @work_q: &list_head for pending commands.
1070 : * @done_q: &list_head for processed commands.
1071 : *
1072 : * Notes:
1073 : * Try a bus device reset. Still, look to see whether we have multiple
1074 : * devices that are jammed or not - if we have multiple devices, it
1075 : * makes no sense to try bus_device_reset - we really would need to try
1076 : * a bus_reset instead.
1077 : */
1078 : static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1079 : struct list_head *work_q,
1080 : struct list_head *done_q)
1081 0 : {
1082 0 : struct scsi_cmnd *scmd, *bdr_scmd, *next;
1083 0 : struct scsi_device *sdev;
1084 0 : int rtn;
1085 0 :
1086 0 : shost_for_each_device(sdev, shost) {
1087 0 : bdr_scmd = NULL;
1088 0 : list_for_each_entry(scmd, work_q, eh_entry)
1089 0 : if (scmd->device == sdev) {
1090 0 : bdr_scmd = scmd;
1091 0 : break;
1092 0 : }
1093 0 :
1094 0 : if (!bdr_scmd)
1095 0 : continue;
1096 :
1097 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1098 : " 0x%p\n", current->comm,
1099 : sdev));
1100 0 : rtn = scsi_try_bus_device_reset(bdr_scmd);
1101 0 : if (rtn == SUCCESS) {
1102 0 : if (!scsi_device_online(sdev) ||
1103 0 : !scsi_eh_tur(bdr_scmd)) {
1104 0 : list_for_each_entry_safe(scmd, next,
1105 0 : work_q, eh_entry) {
1106 0 : if (scmd->device == sdev)
1107 0 : scsi_eh_finish_cmd(scmd,
1108 : done_q);
1109 : }
1110 : }
1111 : } else {
1112 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1113 : " failed sdev:"
1114 : "0x%p\n",
1115 : current->comm,
1116 : sdev));
1117 : }
1118 0 : }
1119 :
1120 0 : return list_empty(work_q);
1121 : }
1122 :
1123 : /**
1124 : * scsi_eh_target_reset - send target reset if needed
1125 : * @shost: scsi host being recovered.
1126 : * @work_q: &list_head for pending commands.
1127 : * @done_q: &list_head for processed commands.
1128 : *
1129 : * Notes:
1130 : * Try a target reset.
1131 : */
1132 : static int scsi_eh_target_reset(struct Scsi_Host *shost,
1133 : struct list_head *work_q,
1134 : struct list_head *done_q)
1135 0 : {
1136 0 : struct scsi_cmnd *scmd, *tgtr_scmd, *next;
1137 0 : unsigned int id = 0;
1138 0 : int rtn;
1139 0 :
1140 0 : do {
1141 0 : tgtr_scmd = NULL;
1142 0 : list_for_each_entry(scmd, work_q, eh_entry) {
1143 0 : if (id == scmd_id(scmd)) {
1144 0 : tgtr_scmd = scmd;
1145 0 : break;
1146 0 : }
1147 0 : }
1148 0 : if (!tgtr_scmd) {
1149 0 : /* not one exactly equal; find the next highest */
1150 0 : list_for_each_entry(scmd, work_q, eh_entry) {
1151 0 : if (scmd_id(scmd) > id &&
1152 0 : (!tgtr_scmd ||
1153 0 : scmd_id(tgtr_scmd) > scmd_id(scmd)))
1154 0 : tgtr_scmd = scmd;
1155 : }
1156 : }
1157 0 : if (!tgtr_scmd)
1158 : /* no more commands, that's it */
1159 0 : break;
1160 :
1161 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
1162 : "to target %d\n",
1163 : current->comm, id));
1164 0 : rtn = scsi_try_target_reset(tgtr_scmd);
1165 0 : if (rtn == SUCCESS) {
1166 0 : list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1167 0 : if (id == scmd_id(scmd))
1168 0 : if (!scsi_device_online(scmd->device) ||
1169 : !scsi_eh_tur(tgtr_scmd))
1170 0 : scsi_eh_finish_cmd(scmd,
1171 : done_q);
1172 : }
1173 : } else
1174 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
1175 : " failed target: "
1176 : "%d\n",
1177 : current->comm, id));
1178 0 : id++;
1179 0 : } while(id != 0);
1180 :
1181 0 : return list_empty(work_q);
1182 : }
1183 :
1184 : /**
1185 0 : * scsi_eh_bus_reset - send a bus reset
1186 : * @shost: &scsi host being recovered.
1187 : * @work_q: &list_head for pending commands.
1188 : * @done_q: &list_head for processed commands.
1189 : */
1190 : static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1191 : struct list_head *work_q,
1192 : struct list_head *done_q)
1193 0 : {
1194 0 : struct scsi_cmnd *scmd, *chan_scmd, *next;
1195 0 : unsigned int channel;
1196 0 : int rtn;
1197 0 :
1198 0 : /*
1199 0 : * we really want to loop over the various channels, and do this on
1200 0 : * a channel by channel basis. we should also check to see if any
1201 0 : * of the failed commands are on soft_reset devices, and if so, skip
1202 0 : * the reset.
1203 0 : */
1204 0 :
1205 0 : for (channel = 0; channel <= shost->max_channel; channel++) {
1206 0 : chan_scmd = NULL;
1207 0 : list_for_each_entry(scmd, work_q, eh_entry) {
1208 0 : if (channel == scmd_channel(scmd)) {
1209 0 : chan_scmd = scmd;
1210 0 : break;
1211 : /*
1212 : * FIXME add back in some support for
1213 0 : * soft_reset devices.
1214 : */
1215 : }
1216 : }
1217 :
1218 0 : if (!chan_scmd)
1219 0 : continue;
1220 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1221 : " %d\n", current->comm,
1222 : channel));
1223 0 : rtn = scsi_try_bus_reset(chan_scmd);
1224 0 : if (rtn == SUCCESS) {
1225 0 : list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1226 0 : if (channel == scmd_channel(scmd))
1227 0 : if (!scsi_device_online(scmd->device) ||
1228 : !scsi_eh_tur(scmd))
1229 0 : scsi_eh_finish_cmd(scmd,
1230 : done_q);
1231 : }
1232 : } else {
1233 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1234 : " failed chan: %d\n",
1235 0 : current->comm,
1236 : channel));
1237 : }
1238 : }
1239 0 : return list_empty(work_q);
1240 : }
1241 :
1242 : /**
1243 : * scsi_eh_host_reset - send a host reset
1244 : * @work_q: list_head for processed commands.
1245 : * @done_q: list_head for processed commands.
1246 : */
1247 : static int scsi_eh_host_reset(struct list_head *work_q,
1248 : struct list_head *done_q)
1249 0 : {
1250 0 : struct scsi_cmnd *scmd, *next;
1251 0 : int rtn;
1252 0 :
1253 0 : if (!list_empty(work_q)) {
1254 0 : scmd = list_entry(work_q->next,
1255 0 : struct scsi_cmnd, eh_entry);
1256 0 :
1257 0 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1258 0 : , current->comm));
1259 0 :
1260 0 : rtn = scsi_try_host_reset(scmd);
1261 0 : if (rtn == SUCCESS) {
1262 0 : list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1263 0 : if (!scsi_device_online(scmd->device) ||
1264 0 : (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
1265 : !scsi_eh_tur(scmd))
1266 0 : scsi_eh_finish_cmd(scmd, done_q);
1267 : }
1268 : } else {
1269 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1270 : " failed\n",
1271 : current->comm));
1272 : }
1273 : }
1274 0 : return list_empty(work_q);
1275 : }
1276 :
1277 : /**
1278 : * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1279 : * @work_q: list_head for processed commands.
1280 : * @done_q: list_head for processed commands.
1281 : */
1282 : static void scsi_eh_offline_sdevs(struct list_head *work_q,
1283 : struct list_head *done_q)
1284 0 : {
1285 0 : struct scsi_cmnd *scmd, *next;
1286 0 :
1287 0 : list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1288 0 : sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1289 0 : "not ready after error recovery\n");
1290 0 : scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1291 : if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
1292 : /*
1293 : * FIXME: Handle lost cmds.
1294 : */
1295 : }
1296 0 : scsi_eh_finish_cmd(scmd, done_q);
1297 : }
1298 0 : return;
1299 : }
1300 :
1301 : /**
1302 : * scsi_noretry_cmd - determinte if command should be failed fast
1303 : * @scmd: SCSI cmd to examine.
1304 : */
1305 : int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1306 : {
1307 0 : switch (host_byte(scmd->result)) {
1308 0 : case DID_OK:
1309 0 : break;
1310 0 : case DID_BUS_BUSY:
1311 0 : return blk_failfast_transport(scmd->request);
1312 0 : case DID_PARITY:
1313 0 : return blk_failfast_dev(scmd->request);
1314 0 : case DID_ERROR:
1315 0 : if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1316 : status_byte(scmd->result) == RESERVATION_CONFLICT)
1317 0 : return 0;
1318 : /* fall through */
1319 0 : case DID_SOFT_ERROR:
1320 0 : return blk_failfast_driver(scmd->request);
1321 0 : }
1322 :
1323 0 : switch (status_byte(scmd->result)) {
1324 0 : case CHECK_CONDITION:
1325 : /*
1326 : * assume caller has checked sense and determinted
1327 : * the check condition was retryable.
1328 : */
1329 0 : return blk_failfast_dev(scmd->request);
1330 0 : }
1331 :
1332 0 : return 0;
1333 : }
1334 :
1335 : /**
1336 : * scsi_decide_disposition - Disposition a cmd on return from LLD.
1337 : * @scmd: SCSI cmd to examine.
1338 : *
1339 : * Notes:
1340 : * This is *only* called when we are examining the status after sending
1341 : * out the actual data command. any commands that are queued for error
1342 : * recovery (e.g. test_unit_ready) do *not* come through here.
1343 : *
1344 : * When this routine returns failed, it means the error handler thread
1345 : * is woken. In cases where the error code indicates an error that
1346 : * doesn't require the error handler read (i.e. we don't need to
1347 : * abort/reset), this function should return SUCCESS.
1348 : */
1349 : int scsi_decide_disposition(struct scsi_cmnd *scmd)
1350 : {
1351 0 : int rtn;
1352 0 :
1353 0 : /*
1354 0 : * if the device is offline, then we clearly just pass the result back
1355 0 : * up to the top level.
1356 : */
1357 0 : if (!scsi_device_online(scmd->device)) {
1358 : SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1359 : " as SUCCESS\n",
1360 : __func__));
1361 0 : return SUCCESS;
1362 : }
1363 :
1364 : /*
1365 : * first check the host byte, to see if there is anything in there
1366 : * that would indicate what we need to do.
1367 : */
1368 : switch (host_byte(scmd->result)) {
1369 0 : case DID_PASSTHROUGH:
1370 : /*
1371 : * no matter what, pass this through to the upper layer.
1372 : * nuke this special code so that it looks like we are saying
1373 : * did_ok.
1374 : */
1375 0 : scmd->result &= 0xff00ffff;
1376 0 : return SUCCESS;
1377 0 : case DID_OK:
1378 : /*
1379 : * looks good. drop through, and check the next byte.
1380 : */
1381 0 : break;
1382 0 : case DID_NO_CONNECT:
1383 0 : case DID_BAD_TARGET:
1384 0 : case DID_ABORT:
1385 : /*
1386 : * note - this means that we just report the status back
1387 : * to the top level driver, not that we actually think
1388 : * that it indicates SUCCESS.
1389 : */
1390 0 : return SUCCESS;
1391 0 : /*
1392 : * when the low level driver returns did_soft_error,
1393 : * it is responsible for keeping an internal retry counter
1394 : * in order to avoid endless loops (db)
1395 : *
1396 : * actually this is a bug in this function here. we should
1397 : * be mindful of the maximum number of retries specified
1398 : * and not get stuck in a loop.
1399 : */
1400 0 : case DID_SOFT_ERROR:
1401 0 : goto maybe_retry;
1402 0 : case DID_IMM_RETRY:
1403 0 : return NEEDS_RETRY;
1404 0 :
1405 0 : case DID_REQUEUE:
1406 0 : return ADD_TO_MLQUEUE;
1407 0 : case DID_TRANSPORT_DISRUPTED:
1408 : /*
1409 : * LLD/transport was disrupted during processing of the IO.
1410 : * The transport class is now blocked/blocking,
1411 : * and the transport will decide what to do with the IO
1412 : * based on its timers and recovery capablilities if
1413 : * there are enough retries.
1414 : */
1415 0 : goto maybe_retry;
1416 0 : case DID_TRANSPORT_FAILFAST:
1417 : /*
1418 : * The transport decided to failfast the IO (most likely
1419 : * the fast io fail tmo fired), so send IO directly upwards.
1420 : */
1421 0 : return SUCCESS;
1422 0 : case DID_ERROR:
1423 0 : if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1424 : status_byte(scmd->result) == RESERVATION_CONFLICT)
1425 : /*
1426 : * execute reservation conflict processing code
1427 : * lower down
1428 : */
1429 0 : break;
1430 : /* fallthrough */
1431 :
1432 0 : case DID_BUS_BUSY:
1433 0 : case DID_PARITY:
1434 0 : goto maybe_retry;
1435 0 : case DID_TIME_OUT:
1436 : /*
1437 : * when we scan the bus, we get timeout messages for
1438 : * these commands if there is no device available.
1439 : * other hosts report did_no_connect for the same thing.
1440 : */
1441 0 : if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1442 : scmd->cmnd[0] == INQUIRY)) {
1443 0 : return SUCCESS;
1444 : } else {
1445 0 : return FAILED;
1446 : }
1447 0 : case DID_RESET:
1448 0 : return SUCCESS;
1449 0 : default:
1450 0 : return FAILED;
1451 : }
1452 :
1453 : /*
1454 : * next, check the message byte.
1455 : */
1456 0 : if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1457 0 : return FAILED;
1458 :
1459 : /*
1460 : * check the status byte to see if this indicates anything special.
1461 : */
1462 : switch (status_byte(scmd->result)) {
1463 0 : case QUEUE_FULL:
1464 0 : scsi_handle_queue_full(scmd->device);
1465 : /*
1466 0 : * the case of trying to send too many commands to a
1467 : * tagged queueing device.
1468 : */
1469 0 : case BUSY:
1470 : /*
1471 : * device can't talk to us at the moment. Should only
1472 : * occur (SAM-3) when the task queue is empty, so will cause
1473 : * the empty queue handling to trigger a stall in the
1474 : * device.
1475 : */
1476 0 : return ADD_TO_MLQUEUE;
1477 0 : case GOOD:
1478 0 : scsi_handle_queue_ramp_up(scmd->device);
1479 0 : case COMMAND_TERMINATED:
1480 0 : return SUCCESS;
1481 0 : case TASK_ABORTED:
1482 0 : goto maybe_retry;
1483 0 : case CHECK_CONDITION:
1484 0 : rtn = scsi_check_sense(scmd);
1485 0 : if (rtn == NEEDS_RETRY)
1486 0 : goto maybe_retry;
1487 : /* if rtn == FAILED, we have no sense information;
1488 : * returning FAILED will wake the error handler thread
1489 : * to collect the sense and redo the decide
1490 : * disposition */
1491 0 : return rtn;
1492 0 : case CONDITION_GOOD:
1493 0 : case INTERMEDIATE_GOOD:
1494 0 : case INTERMEDIATE_C_GOOD:
1495 0 : case ACA_ACTIVE:
1496 : /*
1497 : * who knows? FIXME(eric)
1498 : */
1499 0 : return SUCCESS;
1500 0 :
1501 0 : case RESERVATION_CONFLICT:
1502 0 : sdev_printk(KERN_INFO, scmd->device,
1503 : "reservation conflict\n");
1504 0 : return SUCCESS; /* causes immediate i/o error */
1505 0 : default:
1506 0 : return FAILED;
1507 : }
1508 : return FAILED;
1509 0 :
1510 : maybe_retry:
1511 :
1512 : /* we requeue for retry because the error was retryable, and
1513 : * the request was not marked fast fail. Note that above,
1514 : * even if the request is marked fast fail, we still requeue
1515 : * for queue congestion conditions (QUEUE_FULL or BUSY) */
1516 0 : if ((++scmd->retries) <= scmd->allowed
1517 : && !scsi_noretry_cmd(scmd)) {
1518 0 : return NEEDS_RETRY;
1519 : } else {
1520 : /*
1521 : * no more retries - report this one back to upper level.
1522 : */
1523 0 : return SUCCESS;
1524 : }
1525 : }
1526 :
1527 : static void eh_lock_door_done(struct request *req, int uptodate)
1528 : {
1529 0 : __blk_put_request(req->q, req);
1530 0 : }
1531 :
1532 : /**
1533 : * scsi_eh_lock_door - Prevent medium removal for the specified device
1534 : * @sdev: SCSI device to prevent medium removal
1535 : *
1536 : * Locking:
1537 : * We must be called from process context.
1538 : *
1539 : * Notes:
1540 : * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1541 : * head of the devices request queue, and continue.
1542 : */
1543 : static void scsi_eh_lock_door(struct scsi_device *sdev)
1544 : {
1545 0 : struct request *req;
1546 :
1547 : /*
1548 : * blk_get_request with GFP_KERNEL (__GFP_WAIT) sleeps until a
1549 : * request becomes available
1550 : */
1551 0 : req = blk_get_request(sdev->request_queue, READ, GFP_KERNEL);
1552 :
1553 0 : req->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1554 0 : req->cmd[1] = 0;
1555 0 : req->cmd[2] = 0;
1556 0 : req->cmd[3] = 0;
1557 0 : req->cmd[4] = SCSI_REMOVAL_PREVENT;
1558 0 : req->cmd[5] = 0;
1559 :
1560 0 : req->cmd_len = COMMAND_SIZE(req->cmd[0]);
1561 :
1562 0 : req->cmd_type = REQ_TYPE_BLOCK_PC;
1563 0 : req->cmd_flags |= REQ_QUIET;
1564 0 : req->timeout = 10 * HZ;
1565 0 : req->retries = 5;
1566 :
1567 0 : blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
1568 0 : }
1569 :
1570 : /**
1571 : * scsi_restart_operations - restart io operations to the specified host.
1572 : * @shost: Host we are restarting.
1573 : *
1574 : * Notes:
1575 : * When we entered the error handler, we blocked all further i/o to
1576 : * this device. we need to 'reverse' this process.
1577 : */
1578 : static void scsi_restart_operations(struct Scsi_Host *shost)
1579 : {
1580 0 : struct scsi_device *sdev;
1581 0 : unsigned long flags;
1582 0 :
1583 0 : /*
1584 0 : * If the door was locked, we need to insert a door lock request
1585 0 : * onto the head of the SCSI request queue for the device. There
1586 0 : * is no point trying to lock the door of an off-line device.
1587 0 : */
1588 0 : shost_for_each_device(sdev, shost) {
1589 0 : if (scsi_device_online(sdev) && sdev->locked)
1590 0 : scsi_eh_lock_door(sdev);
1591 : }
1592 :
1593 : /*
1594 : * next free up anything directly waiting upon the host. this
1595 : * will be requests for character device operations, and also for
1596 : * ioctls to queued block devices.
1597 : */
1598 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1599 : __func__));
1600 :
1601 0 : spin_lock_irqsave(shost->host_lock, flags);
1602 0 : if (scsi_host_set_state(shost, SHOST_RUNNING))
1603 0 : if (scsi_host_set_state(shost, SHOST_CANCEL))
1604 0 : BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1605 0 : spin_unlock_irqrestore(shost->host_lock, flags);
1606 :
1607 0 : wake_up(&shost->host_wait);
1608 :
1609 : /*
1610 : * finally we need to re-initiate requests that may be pending. we will
1611 : * have had everything blocked while error handling is taking place, and
1612 : * now that error recovery is done, we will need to ensure that these
1613 : * requests are started.
1614 : */
1615 0 : scsi_run_host_queues(shost);
1616 0 : }
1617 :
1618 : /**
1619 : * scsi_eh_ready_devs - check device ready state and recover if not.
1620 : * @shost: host to be recovered.
1621 : * @work_q: &list_head for pending commands.
1622 : * @done_q: &list_head for processed commands.
1623 : */
1624 : void scsi_eh_ready_devs(struct Scsi_Host *shost,
1625 : struct list_head *work_q,
1626 0 : struct list_head *done_q)
1627 0 : {
1628 0 : if (!scsi_eh_stu(shost, work_q, done_q))
1629 0 : if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1630 0 : if (!scsi_eh_target_reset(shost, work_q, done_q))
1631 0 : if (!scsi_eh_bus_reset(shost, work_q, done_q))
1632 0 : if (!scsi_eh_host_reset(work_q, done_q))
1633 0 : scsi_eh_offline_sdevs(work_q,
1634 0 : done_q);
1635 : }
1636 : EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
1637 :
1638 : /**
1639 : * scsi_eh_flush_done_q - finish processed commands or retry them.
1640 : * @done_q: list_head of processed commands.
1641 : */
1642 : void scsi_eh_flush_done_q(struct list_head *done_q)
1643 : {
1644 0 : struct scsi_cmnd *scmd, *next;
1645 0 :
1646 0 : list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
1647 0 : list_del_init(&scmd->eh_entry);
1648 0 : if (scsi_device_online(scmd->device) &&
1649 0 : !scsi_noretry_cmd(scmd) &&
1650 0 : (++scmd->retries <= scmd->allowed)) {
1651 0 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1652 : " retry cmd: %p\n",
1653 0 : current->comm,
1654 : scmd));
1655 0 : scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
1656 : } else {
1657 : /*
1658 : * If just we got sense for the device (called
1659 : * scsi_eh_get_sense), scmd->result is already
1660 : * set, do not set DRIVER_TIMEOUT.
1661 : */
1662 0 : if (!scmd->result)
1663 0 : scmd->result |= (DRIVER_TIMEOUT << 24);
1664 : SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1665 : " cmd: %p\n",
1666 : current->comm, scmd));
1667 0 : scsi_finish_command(scmd);
1668 : }
1669 : }
1670 : }
1671 : EXPORT_SYMBOL(scsi_eh_flush_done_q);
1672 :
1673 : /**
1674 : * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1675 : * @shost: Host to unjam.
1676 : *
1677 : * Notes:
1678 : * When we come in here, we *know* that all commands on the bus have
1679 : * either completed, failed or timed out. we also know that no further
1680 : * commands are being sent to the host, so things are relatively quiet
1681 : * and we have freedom to fiddle with things as we wish.
1682 : *
1683 : * This is only the *default* implementation. it is possible for
1684 : * individual drivers to supply their own version of this function, and
1685 : * if the maintainer wishes to do this, it is strongly suggested that
1686 : * this function be taken as a template and modified. this function
1687 : * was designed to correctly handle problems for about 95% of the
1688 : * different cases out there, and it should always provide at least a
1689 : * reasonable amount of error recovery.
1690 : *
1691 : * Any command marked 'failed' or 'timeout' must eventually have
1692 : * scsi_finish_cmd() called for it. we do all of the retry stuff
1693 : * here, so when we restart the host after we return it should have an
1694 : * empty queue.
1695 : */
1696 : static void scsi_unjam_host(struct Scsi_Host *shost)
1697 : {
1698 0 : unsigned long flags;
1699 0 : LIST_HEAD(eh_work_q);
1700 0 : LIST_HEAD(eh_done_q);
1701 0 :
1702 0 : spin_lock_irqsave(shost->host_lock, flags);
1703 0 : list_splice_init(&shost->eh_cmd_q, &eh_work_q);
1704 0 : spin_unlock_irqrestore(shost->host_lock, flags);
1705 :
1706 : SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
1707 :
1708 0 : if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
1709 0 : if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1710 0 : scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1711 :
1712 0 : scsi_eh_flush_done_q(&eh_done_q);
1713 0 : }
1714 :
1715 : /**
1716 : * scsi_error_handler - SCSI error handler thread
1717 : * @data: Host for which we are running.
1718 : *
1719 : * Notes:
1720 : * This is the main error handling loop. This is run as a kernel thread
1721 : * for every SCSI host and handles all error handling activity.
1722 : */
1723 : int scsi_error_handler(void *data)
1724 : {
1725 0 : struct Scsi_Host *shost = data;
1726 0 :
1727 0 : /*
1728 0 : * We use TASK_INTERRUPTIBLE so that the thread is not
1729 0 : * counted against the load average as a running process.
1730 0 : * We never actually get interrupted because kthread_run
1731 0 : * disables signal delivery for the created thread.
1732 0 : */
1733 0 : set_current_state(TASK_INTERRUPTIBLE);
1734 0 : while (!kthread_should_stop()) {
1735 0 : if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
1736 0 : shost->host_failed != shost->host_busy) {
1737 0 : SCSI_LOG_ERROR_RECOVERY(1,
1738 0 : printk("Error handler scsi_eh_%d sleeping\n",
1739 0 : shost->host_no));
1740 0 : schedule();
1741 0 : set_current_state(TASK_INTERRUPTIBLE);
1742 0 : continue;
1743 0 : }
1744 :
1745 0 : __set_current_state(TASK_RUNNING);
1746 : SCSI_LOG_ERROR_RECOVERY(1,
1747 : printk("Error handler scsi_eh_%d waking up\n",
1748 : shost->host_no));
1749 :
1750 : /*
1751 : * We have a host that is failing for some reason. Figure out
1752 : * what we need to do to get it up and online again (if we can).
1753 : * If we fail, we end up taking the thing offline.
1754 : */
1755 0 : if (shost->transportt->eh_strategy_handler)
1756 0 : shost->transportt->eh_strategy_handler(shost);
1757 : else
1758 0 : scsi_unjam_host(shost);
1759 :
1760 : /*
1761 : * Note - if the above fails completely, the action is to take
1762 : * individual devices offline and flush the queue of any
1763 : * outstanding requests that may have been pending. When we
1764 : * restart, we restart any I/O to any other devices on the bus
1765 : * which are still online.
1766 : */
1767 0 : scsi_restart_operations(shost);
1768 0 : set_current_state(TASK_INTERRUPTIBLE);
1769 0 : }
1770 0 : __set_current_state(TASK_RUNNING);
1771 :
1772 : SCSI_LOG_ERROR_RECOVERY(1,
1773 0 : printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
1774 0 : shost->ehandler = NULL;
1775 0 : return 0;
1776 : }
1777 :
1778 : /*
1779 : * Function: scsi_report_bus_reset()
1780 : *
1781 : * Purpose: Utility function used by low-level drivers to report that
1782 : * they have observed a bus reset on the bus being handled.
1783 : *
1784 : * Arguments: shost - Host in question
1785 : * channel - channel on which reset was observed.
1786 : *
1787 : * Returns: Nothing
1788 : *
1789 : * Lock status: Host lock must be held.
1790 : *
1791 : * Notes: This only needs to be called if the reset is one which
1792 : * originates from an unknown location. Resets originated
1793 : * by the mid-level itself don't need to call this, but there
1794 : * should be no harm.
1795 : *
1796 : * The main purpose of this is to make sure that a CHECK_CONDITION
1797 : * is properly treated.
1798 : */
1799 : void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
1800 : {
1801 0 : struct scsi_device *sdev;
1802 0 :
1803 0 : __shost_for_each_device(sdev, shost) {
1804 0 : if (channel == sdev_channel(sdev))
1805 0 : __scsi_report_device_reset(sdev, NULL);
1806 : }
1807 : }
1808 : EXPORT_SYMBOL(scsi_report_bus_reset);
1809 :
1810 0 : /*
1811 : * Function: scsi_report_device_reset()
1812 : *
1813 : * Purpose: Utility function used by low-level drivers to report that
1814 : * they have observed a device reset on the device being handled.
1815 : *
1816 : * Arguments: shost - Host in question
1817 : * channel - channel on which reset was observed
1818 : * target - target on which reset was observed
1819 : *
1820 : * Returns: Nothing
1821 : *
1822 : * Lock status: Host lock must be held
1823 : *
1824 : * Notes: This only needs to be called if the reset is one which
1825 : * originates from an unknown location. Resets originated
1826 : * by the mid-level itself don't need to call this, but there
1827 : * should be no harm.
1828 : *
1829 : * The main purpose of this is to make sure that a CHECK_CONDITION
1830 : * is properly treated.
1831 : */
1832 : void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
1833 : {
1834 0 : struct scsi_device *sdev;
1835 0 :
1836 0 : __shost_for_each_device(sdev, shost) {
1837 0 : if (channel == sdev_channel(sdev) &&
1838 0 : target == sdev_id(sdev))
1839 0 : __scsi_report_device_reset(sdev, NULL);
1840 : }
1841 : }
1842 : EXPORT_SYMBOL(scsi_report_device_reset);
1843 0 :
1844 : static void
1845 : scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
1846 : {
1847 0 : }
1848 :
1849 : /*
1850 : * Function: scsi_reset_provider
1851 : *
1852 : * Purpose: Send requested reset to a bus or device at any phase.
1853 : *
1854 : * Arguments: device - device to send reset to
1855 : * flag - reset type (see scsi.h)
1856 : *
1857 : * Returns: SUCCESS/FAILURE.
1858 : *
1859 : * Notes: This is used by the SCSI Generic driver to provide
1860 : * Bus/Device reset capability.
1861 : */
1862 : int
1863 : scsi_reset_provider(struct scsi_device *dev, int flag)
1864 : {
1865 0 : struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
1866 0 : struct Scsi_Host *shost = dev->host;
1867 0 : struct request req;
1868 0 : unsigned long flags;
1869 0 : int rtn;
1870 0 :
1871 0 : blk_rq_init(NULL, &req);
1872 0 : scmd->request = &req;
1873 :
1874 0 : scmd->cmnd = req.cmd;
1875 :
1876 0 : scmd->scsi_done = scsi_reset_provider_done_command;
1877 0 : memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1878 :
1879 0 : scmd->cmd_len = 0;
1880 :
1881 0 : scmd->sc_data_direction = DMA_BIDIRECTIONAL;
1882 :
1883 0 : spin_lock_irqsave(shost->host_lock, flags);
1884 0 : shost->tmf_in_progress = 1;
1885 0 : spin_unlock_irqrestore(shost->host_lock, flags);
1886 :
1887 : switch (flag) {
1888 0 : case SCSI_TRY_RESET_DEVICE:
1889 0 : rtn = scsi_try_bus_device_reset(scmd);
1890 0 : if (rtn == SUCCESS)
1891 0 : break;
1892 : /* FALLTHROUGH */
1893 0 : case SCSI_TRY_RESET_TARGET:
1894 0 : rtn = scsi_try_target_reset(scmd);
1895 0 : if (rtn == SUCCESS)
1896 0 : break;
1897 : /* FALLTHROUGH */
1898 0 : case SCSI_TRY_RESET_BUS:
1899 0 : rtn = scsi_try_bus_reset(scmd);
1900 0 : if (rtn == SUCCESS)
1901 0 : break;
1902 : /* FALLTHROUGH */
1903 0 : case SCSI_TRY_RESET_HOST:
1904 0 : rtn = scsi_try_host_reset(scmd);
1905 0 : break;
1906 0 : default:
1907 0 : rtn = FAILED;
1908 0 : }
1909 :
1910 0 : spin_lock_irqsave(shost->host_lock, flags);
1911 0 : shost->tmf_in_progress = 0;
1912 0 : spin_unlock_irqrestore(shost->host_lock, flags);
1913 :
1914 : /*
1915 : * be sure to wake up anyone who was sleeping or had their queue
1916 : * suspended while we performed the TMF.
1917 : */
1918 : SCSI_LOG_ERROR_RECOVERY(3,
1919 : printk("%s: waking up host to restart after TMF\n",
1920 : __func__));
1921 :
1922 0 : wake_up(&shost->host_wait);
1923 :
1924 0 : scsi_run_host_queues(shost);
1925 :
1926 0 : scsi_next_command(scmd);
1927 0 : return rtn;
1928 : }
1929 : EXPORT_SYMBOL(scsi_reset_provider);
1930 :
1931 : /**
1932 : * scsi_normalize_sense - normalize main elements from either fixed or
1933 : * descriptor sense data format into a common format.
1934 : *
1935 : * @sense_buffer: byte array containing sense data returned by device
1936 : * @sb_len: number of valid bytes in sense_buffer
1937 : * @sshdr: pointer to instance of structure that common
1938 : * elements are written to.
1939 : *
1940 : * Notes:
1941 : * The "main elements" from sense data are: response_code, sense_key,
1942 : * asc, ascq and additional_length (only for descriptor format).
1943 : *
1944 : * Typically this function can be called after a device has
1945 : * responded to a SCSI command with the CHECK_CONDITION status.
1946 : *
1947 : * Return value:
1948 : * 1 if valid sense data information found, else 0;
1949 : */
1950 : int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
1951 : struct scsi_sense_hdr *sshdr)
1952 72 : {
1953 288 : if (!sense_buffer || !sb_len)
1954 72 : return 0;
1955 :
1956 72 : memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
1957 :
1958 72 : sshdr->response_code = (sense_buffer[0] & 0x7f);
1959 :
1960 288 : if (!scsi_sense_valid(sshdr))
1961 72 : return 0;
1962 :
1963 216 : if (sshdr->response_code >= 0x72) {
1964 : /*
1965 : * descriptor format
1966 : */
1967 144 : if (sb_len > 1)
1968 72 : sshdr->sense_key = (sense_buffer[1] & 0xf);
1969 144 : if (sb_len > 2)
1970 72 : sshdr->asc = sense_buffer[2];
1971 144 : if (sb_len > 3)
1972 72 : sshdr->ascq = sense_buffer[3];
1973 144 : if (sb_len > 7)
1974 72 : sshdr->additional_length = sense_buffer[7];
1975 : } else {
1976 : /*
1977 : * fixed format
1978 : */
1979 144 : if (sb_len > 2)
1980 72 : sshdr->sense_key = (sense_buffer[2] & 0xf);
1981 144 : if (sb_len > 7) {
1982 432 : sb_len = (sb_len < (sense_buffer[7] + 8)) ?
1983 : sb_len : (sense_buffer[7] + 8);
1984 144 : if (sb_len > 12)
1985 72 : sshdr->asc = sense_buffer[12];
1986 144 : if (sb_len > 13)
1987 72 : sshdr->ascq = sense_buffer[13];
1988 : }
1989 : }
1990 :
1991 72 : return 1;
1992 : }
1993 : EXPORT_SYMBOL(scsi_normalize_sense);
1994 :
1995 : int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
1996 : struct scsi_sense_hdr *sshdr)
1997 0 : {
1998 0 : return scsi_normalize_sense(cmd->sense_buffer,
1999 : SCSI_SENSE_BUFFERSIZE, sshdr);
2000 : }
2001 : EXPORT_SYMBOL(scsi_command_normalize_sense);
2002 :
2003 : /**
2004 : * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
2005 : * @sense_buffer: byte array of descriptor format sense data
2006 : * @sb_len: number of valid bytes in sense_buffer
2007 : * @desc_type: value of descriptor type to find
2008 : * (e.g. 0 -> information)
2009 : *
2010 : * Notes:
2011 : * only valid when sense data is in descriptor format
2012 : *
2013 : * Return value:
2014 : * pointer to start of (first) descriptor if found else NULL
2015 : */
2016 : const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
2017 : int desc_type)
2018 0 : {
2019 0 : int add_sen_len, add_len, desc_len, k;
2020 0 : const u8 * descp;
2021 0 :
2022 0 : if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
2023 0 : return NULL;
2024 0 : if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
2025 0 : return NULL;
2026 0 : add_sen_len = (add_sen_len < (sb_len - 8)) ?
2027 : add_sen_len : (sb_len - 8);
2028 0 : descp = &sense_buffer[8];
2029 0 : for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
2030 0 : descp += desc_len;
2031 0 : add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
2032 0 : desc_len = add_len + 2;
2033 0 : if (descp[0] == desc_type)
2034 0 : return descp;
2035 0 : if (add_len < 0) // short descriptor ??
2036 0 : break;
2037 : }
2038 0 : return NULL;
2039 : }
2040 : EXPORT_SYMBOL(scsi_sense_desc_find);
2041 :
2042 : /**
2043 : * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2044 : * @sense_buffer: byte array of sense data
2045 : * @sb_len: number of valid bytes in sense_buffer
2046 : * @info_out: pointer to 64 integer where 8 or 4 byte information
2047 : * field will be placed if found.
2048 : *
2049 : * Return value:
2050 : * 1 if information field found, 0 if not found.
2051 : */
2052 : int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
2053 : u64 * info_out)
2054 0 : {
2055 0 : int j;
2056 0 : const u8 * ucp;
2057 : u64 ull;
2058 :
2059 0 : if (sb_len < 7)
2060 0 : return 0;
2061 : switch (sense_buffer[0] & 0x7f) {
2062 0 : case 0x70:
2063 0 : case 0x71:
2064 0 : if (sense_buffer[0] & 0x80) {
2065 0 : *info_out = (sense_buffer[3] << 24) +
2066 : (sense_buffer[4] << 16) +
2067 : (sense_buffer[5] << 8) + sense_buffer[6];
2068 0 : return 1;
2069 : } else
2070 0 : return 0;
2071 0 : case 0x72:
2072 0 : case 0x73:
2073 0 : ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2074 : 0 /* info desc */);
2075 0 : if (ucp && (0xa == ucp[1])) {
2076 0 : ull = 0;
2077 0 : for (j = 0; j < 8; ++j) {
2078 0 : if (j > 0)
2079 0 : ull <<= 8;
2080 0 : ull |= ucp[4 + j];
2081 : }
2082 0 : *info_out = ull;
2083 0 : return 1;
2084 : } else
2085 0 : return 0;
2086 0 : default:
2087 0 : return 0;
2088 0 : }
2089 : }
2090 : EXPORT_SYMBOL(scsi_get_sense_info_fld);
2091 :
2092 : /**
2093 : * scsi_build_sense_buffer - build sense data in a buffer
2094 : * @desc: Sense format (non zero == descriptor format,
2095 : * 0 == fixed format)
2096 : * @buf: Where to build sense data
2097 : * @key: Sense key
2098 : * @asc: Additional sense code
2099 : * @ascq: Additional sense code qualifier
2100 : *
2101 : **/
2102 : void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
2103 : {
2104 0 : if (desc) {
2105 0 : buf[0] = 0x72; /* descriptor, current */
2106 0 : buf[1] = key;
2107 0 : buf[2] = asc;
2108 0 : buf[3] = ascq;
2109 0 : buf[7] = 0;
2110 : } else {
2111 0 : buf[0] = 0x70; /* fixed, current */
2112 0 : buf[2] = key;
2113 0 : buf[7] = 0xa;
2114 0 : buf[12] = asc;
2115 0 : buf[13] = ascq;
2116 : }
2117 0 : }
2118 : EXPORT_SYMBOL(scsi_build_sense_buffer);
|