1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/module.h>
18#include <linux/sched.h>
19#include <linux/gfp.h>
20#include <linux/timer.h>
21#include <linux/string.h>
22#include <linux/kernel.h>
23#include <linux/freezer.h>
24#include <linux/kthread.h>
25#include <linux/interrupt.h>
26#include <linux/blkdev.h>
27#include <linux/delay.h>
28#include <linux/jiffies.h>
29
30#include <scsi/scsi.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/scsi_dbg.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_driver.h>
35#include <scsi/scsi_eh.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi_ioctl.h>
39
40#include "scsi_priv.h"
41#include "scsi_logging.h"
42#include "scsi_transport_api.h"
43
44#include <trace/events/scsi.h>
45
46static void scsi_eh_done(struct scsi_cmnd *scmd);
47
48
49
50
51
52#define BUS_RESET_SETTLE_TIME (10)
53#define HOST_RESET_SETTLE_TIME (10)
54
55static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
56static int scsi_try_to_abort_cmd(struct scsi_host_template *,
57 struct scsi_cmnd *);
58
59
60void scsi_eh_wakeup(struct Scsi_Host *shost)
61{
62 if (shost->host_busy == shost->host_failed) {
63 trace_scsi_eh_wakeup(shost);
64 wake_up_process(shost->ehandler);
65 SCSI_LOG_ERROR_RECOVERY(5,
66 printk("Waking error handler thread\n"));
67 }
68}
69
70
71
72
73
74
75
76void scsi_schedule_eh(struct Scsi_Host *shost)
77{
78 unsigned long flags;
79
80 spin_lock_irqsave(shost->host_lock, flags);
81
82 if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
83 scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
84 shost->host_eh_scheduled++;
85 scsi_eh_wakeup(shost);
86 }
87
88 spin_unlock_irqrestore(shost->host_lock, flags);
89}
90EXPORT_SYMBOL_GPL(scsi_schedule_eh);
91
92static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
93{
94 if (!shost->last_reset || shost->eh_deadline == -1)
95 return 0;
96
97
98
99
100
101
102
103
104 if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
105 shost->eh_deadline > -1)
106 return 0;
107
108 return 1;
109}
110
111
112
113
114
115void
116scmd_eh_abort_handler(struct work_struct *work)
117{
118 struct scsi_cmnd *scmd =
119 container_of(work, struct scsi_cmnd, abort_work.work);
120 struct scsi_device *sdev = scmd->device;
121 int rtn;
122
123 if (scsi_host_eh_past_deadline(sdev->host)) {
124 SCSI_LOG_ERROR_RECOVERY(3,
125 scmd_printk(KERN_INFO, scmd,
126 "scmd %p eh timeout, not aborting\n",
127 scmd));
128 } else {
129 SCSI_LOG_ERROR_RECOVERY(3,
130 scmd_printk(KERN_INFO, scmd,
131 "aborting command %p\n", scmd));
132 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
133 if (rtn == SUCCESS) {
134 scmd->result |= DID_TIME_OUT << 16;
135 if (scsi_host_eh_past_deadline(sdev->host)) {
136 SCSI_LOG_ERROR_RECOVERY(3,
137 scmd_printk(KERN_INFO, scmd,
138 "scmd %p eh timeout, "
139 "not retrying aborted "
140 "command\n", scmd));
141 } else if (!scsi_noretry_cmd(scmd) &&
142 (++scmd->retries <= scmd->allowed)) {
143 SCSI_LOG_ERROR_RECOVERY(3,
144 scmd_printk(KERN_WARNING, scmd,
145 "scmd %p retry "
146 "aborted command\n", scmd));
147 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
148 return;
149 } else {
150 SCSI_LOG_ERROR_RECOVERY(3,
151 scmd_printk(KERN_WARNING, scmd,
152 "scmd %p finish "
153 "aborted command\n", scmd));
154 scsi_finish_command(scmd);
155 return;
156 }
157 } else {
158 SCSI_LOG_ERROR_RECOVERY(3,
159 scmd_printk(KERN_INFO, scmd,
160 "scmd %p abort failed, rtn %d\n",
161 scmd, rtn));
162 }
163 }
164
165 if (!scsi_eh_scmd_add(scmd, 0)) {
166 SCSI_LOG_ERROR_RECOVERY(3,
167 scmd_printk(KERN_WARNING, scmd,
168 "scmd %p terminate "
169 "aborted command\n", scmd));
170 scmd->result |= DID_TIME_OUT << 16;
171 scsi_finish_command(scmd);
172 }
173}
174
175
176
177
178
179
180
181static int
182scsi_abort_command(struct scsi_cmnd *scmd)
183{
184 struct scsi_device *sdev = scmd->device;
185 struct Scsi_Host *shost = sdev->host;
186 unsigned long flags;
187
188 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
189
190
191
192 SCSI_LOG_ERROR_RECOVERY(3,
193 scmd_printk(KERN_INFO, scmd,
194 "scmd %p previous abort failed\n", scmd));
195 cancel_delayed_work(&scmd->abort_work);
196 return FAILED;
197 }
198
199
200
201
202
203 spin_lock_irqsave(shost->host_lock, flags);
204 if (scsi_host_in_recovery(shost)) {
205 spin_unlock_irqrestore(shost->host_lock, flags);
206 SCSI_LOG_ERROR_RECOVERY(3,
207 scmd_printk(KERN_INFO, scmd,
208 "scmd %p not aborting, host in recovery\n",
209 scmd));
210 return FAILED;
211 }
212
213 if (shost->eh_deadline != -1 && !shost->last_reset)
214 shost->last_reset = jiffies;
215 spin_unlock_irqrestore(shost->host_lock, flags);
216
217 scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
218 SCSI_LOG_ERROR_RECOVERY(3,
219 scmd_printk(KERN_INFO, scmd,
220 "scmd %p abort scheduled\n", scmd));
221 queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
222 return SUCCESS;
223}
224
225
226
227
228
229
230
231
232
233int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
234{
235 struct Scsi_Host *shost = scmd->device->host;
236 unsigned long flags;
237 int ret = 0;
238
239 if (!shost->ehandler)
240 return 0;
241
242 spin_lock_irqsave(shost->host_lock, flags);
243 if (scsi_host_set_state(shost, SHOST_RECOVERY))
244 if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
245 goto out_unlock;
246
247 if (shost->eh_deadline != -1 && !shost->last_reset)
248 shost->last_reset = jiffies;
249
250 ret = 1;
251 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
252 eh_flag &= ~SCSI_EH_CANCEL_CMD;
253 scmd->eh_eflags |= eh_flag;
254 list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
255 shost->host_failed++;
256 scsi_eh_wakeup(shost);
257 out_unlock:
258 spin_unlock_irqrestore(shost->host_lock, flags);
259 return ret;
260}
261
262
263
264
265
266
267
268
269
270
271
272enum blk_eh_timer_return scsi_times_out(struct request *req)
273{
274 struct scsi_cmnd *scmd = req->special;
275 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
276 struct Scsi_Host *host = scmd->device->host;
277
278 trace_scsi_dispatch_cmd_timeout(scmd);
279 scsi_log_completion(scmd, TIMEOUT_ERROR);
280
281 if (host->eh_deadline != -1 && !host->last_reset)
282 host->last_reset = jiffies;
283
284 if (host->transportt->eh_timed_out)
285 rtn = host->transportt->eh_timed_out(scmd);
286 else if (host->hostt->eh_timed_out)
287 rtn = host->hostt->eh_timed_out(scmd);
288
289 if (rtn == BLK_EH_NOT_HANDLED && !host->hostt->no_async_abort)
290 if (scsi_abort_command(scmd) == SUCCESS)
291 return BLK_EH_NOT_HANDLED;
292
293 scmd->result |= DID_TIME_OUT << 16;
294
295 if (unlikely(rtn == BLK_EH_NOT_HANDLED &&
296 !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD)))
297 rtn = BLK_EH_HANDLED;
298
299 return rtn;
300}
301
302
303
304
305
306
307
308
309
310
311
312
313int scsi_block_when_processing_errors(struct scsi_device *sdev)
314{
315 int online;
316
317 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
318
319 online = scsi_device_online(sdev);
320
321 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__,
322 online));
323
324 return online;
325}
326EXPORT_SYMBOL(scsi_block_when_processing_errors);
327
328#ifdef CONFIG_SCSI_LOGGING
329
330
331
332
333
334static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
335 struct list_head *work_q)
336{
337 struct scsi_cmnd *scmd;
338 struct scsi_device *sdev;
339 int total_failures = 0;
340 int cmd_failed = 0;
341 int cmd_cancel = 0;
342 int devices_failed = 0;
343
344 shost_for_each_device(sdev, shost) {
345 list_for_each_entry(scmd, work_q, eh_entry) {
346 if (scmd->device == sdev) {
347 ++total_failures;
348 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
349 ++cmd_cancel;
350 else
351 ++cmd_failed;
352 }
353 }
354
355 if (cmd_cancel || cmd_failed) {
356 SCSI_LOG_ERROR_RECOVERY(3,
357 sdev_printk(KERN_INFO, sdev,
358 "%s: cmds failed: %d, cancel: %d\n",
359 __func__, cmd_failed,
360 cmd_cancel));
361 cmd_cancel = 0;
362 cmd_failed = 0;
363 ++devices_failed;
364 }
365 }
366
367 SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
368 " devices require eh work\n",
369 total_failures, devices_failed));
370}
371#endif
372
373
374
375
376
377
378static void scsi_report_lun_change(struct scsi_device *sdev)
379{
380 sdev->sdev_target->expecting_lun_change = 1;
381}
382
383
384
385
386
387
388
389static void scsi_report_sense(struct scsi_device *sdev,
390 struct scsi_sense_hdr *sshdr)
391{
392 enum scsi_device_event evt_type = SDEV_EVT_MAXBITS;
393
394 if (sshdr->sense_key == UNIT_ATTENTION) {
395 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
396 evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
397 sdev_printk(KERN_WARNING, sdev,
398 "Inquiry data has changed");
399 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
400 evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
401 scsi_report_lun_change(sdev);
402 sdev_printk(KERN_WARNING, sdev,
403 "Warning! Received an indication that the "
404 "LUN assignments on this target have "
405 "changed. The Linux SCSI layer does not "
406 "automatically remap LUN assignments.\n");
407 } else if (sshdr->asc == 0x3f)
408 sdev_printk(KERN_WARNING, sdev,
409 "Warning! Received an indication that the "
410 "operating parameters on this target have "
411 "changed. The Linux SCSI layer does not "
412 "automatically adjust these parameters.\n");
413
414 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
415 evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
416 sdev_printk(KERN_WARNING, sdev,
417 "Warning! Received an indication that the "
418 "LUN reached a thin provisioning soft "
419 "threshold.\n");
420 }
421
422 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
423 evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
424 sdev_printk(KERN_WARNING, sdev,
425 "Mode parameters changed");
426 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
427 evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
428 sdev_printk(KERN_WARNING, sdev,
429 "Capacity data has changed");
430 } else if (sshdr->asc == 0x2a)
431 sdev_printk(KERN_WARNING, sdev,
432 "Parameters changed");
433 }
434
435 if (evt_type != SDEV_EVT_MAXBITS) {
436 set_bit(evt_type, sdev->pending_events);
437 schedule_work(&sdev->event_work);
438 }
439}
440
441
442
443
444
445
446
447
448
449
450
451
452static int scsi_check_sense(struct scsi_cmnd *scmd)
453{
454 struct scsi_device *sdev = scmd->device;
455 struct scsi_sense_hdr sshdr;
456
457 if (! scsi_command_normalize_sense(scmd, &sshdr))
458 return FAILED;
459
460 if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
461
462
463
464
465
466 return SUCCESS;
467
468 scsi_report_sense(sdev, &sshdr);
469
470 if (scsi_sense_is_deferred(&sshdr))
471 return NEEDS_RETRY;
472
473 if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh &&
474 sdev->scsi_dh_data->scsi_dh->check_sense) {
475 int rc;
476
477 rc = sdev->scsi_dh_data->scsi_dh->check_sense(sdev, &sshdr);
478 if (rc != SCSI_RETURN_NOT_HANDLED)
479 return rc;
480
481 }
482
483
484
485
486
487 if (sshdr.response_code == 0x70) {
488
489 if (scmd->sense_buffer[2] & 0xe0)
490 return SUCCESS;
491 } else {
492
493
494
495
496
497 if ((sshdr.additional_length > 3) &&
498 (scmd->sense_buffer[8] == 0x4) &&
499 (scmd->sense_buffer[11] & 0xe0))
500 return SUCCESS;
501 }
502
503 switch (sshdr.sense_key) {
504 case NO_SENSE:
505 return SUCCESS;
506 case RECOVERED_ERROR:
507 return SUCCESS;
508
509 case ABORTED_COMMAND:
510 if (sshdr.asc == 0x10)
511 return SUCCESS;
512
513 return NEEDS_RETRY;
514 case NOT_READY:
515 case UNIT_ATTENTION:
516
517
518
519
520
521
522 if (scmd->device->expecting_cc_ua) {
523
524
525
526
527
528
529 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
530 scmd->device->expecting_cc_ua = 0;
531 return NEEDS_RETRY;
532 }
533 }
534
535
536
537
538
539 if (scmd->device->sdev_target->expecting_lun_change &&
540 sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
541 return NEEDS_RETRY;
542
543
544
545
546 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
547 return NEEDS_RETRY;
548
549
550
551
552 if (scmd->device->allow_restart &&
553 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
554 return FAILED;
555
556
557
558
559 return SUCCESS;
560
561
562 case DATA_PROTECT:
563 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
564
565 set_host_byte(scmd, DID_ALLOC_FAILURE);
566 return SUCCESS;
567 }
568 case COPY_ABORTED:
569 case VOLUME_OVERFLOW:
570 case MISCOMPARE:
571 case BLANK_CHECK:
572 set_host_byte(scmd, DID_TARGET_FAILURE);
573 return SUCCESS;
574
575 case MEDIUM_ERROR:
576 if (sshdr.asc == 0x11 ||
577 sshdr.asc == 0x13 ||
578 sshdr.asc == 0x14) {
579 set_host_byte(scmd, DID_MEDIUM_ERROR);
580 return SUCCESS;
581 }
582 return NEEDS_RETRY;
583
584 case HARDWARE_ERROR:
585 if (scmd->device->retry_hwerror)
586 return ADD_TO_MLQUEUE;
587 else
588 set_host_byte(scmd, DID_TARGET_FAILURE);
589
590 case ILLEGAL_REQUEST:
591 if (sshdr.asc == 0x20 ||
592 sshdr.asc == 0x21 ||
593 sshdr.asc == 0x24 ||
594 sshdr.asc == 0x26) {
595 set_host_byte(scmd, DID_TARGET_FAILURE);
596 }
597 return SUCCESS;
598
599 default:
600 return SUCCESS;
601 }
602}
603
604static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
605{
606 struct scsi_host_template *sht = sdev->host->hostt;
607 struct scsi_device *tmp_sdev;
608
609 if (!sht->change_queue_depth ||
610 sdev->queue_depth >= sdev->max_queue_depth)
611 return;
612
613 if (time_before(jiffies,
614 sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
615 return;
616
617 if (time_before(jiffies,
618 sdev->last_queue_full_time + sdev->queue_ramp_up_period))
619 return;
620
621
622
623
624
625 shost_for_each_device(tmp_sdev, sdev->host) {
626 if (tmp_sdev->channel != sdev->channel ||
627 tmp_sdev->id != sdev->id ||
628 tmp_sdev->queue_depth == sdev->max_queue_depth)
629 continue;
630
631
632
633
634 sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1,
635 SCSI_QDEPTH_RAMP_UP);
636 sdev->last_queue_ramp_up = jiffies;
637 }
638}
639
640static void scsi_handle_queue_full(struct scsi_device *sdev)
641{
642 struct scsi_host_template *sht = sdev->host->hostt;
643 struct scsi_device *tmp_sdev;
644
645 if (!sht->change_queue_depth)
646 return;
647
648 shost_for_each_device(tmp_sdev, sdev->host) {
649 if (tmp_sdev->channel != sdev->channel ||
650 tmp_sdev->id != sdev->id)
651 continue;
652
653
654
655
656
657 sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth - 1,
658 SCSI_QDEPTH_QFULL);
659 }
660}
661
662
663
664
665
666
667
668
669
670
671
672static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
673{
674
675
676
677
678 if (host_byte(scmd->result) == DID_RESET) {
679
680
681
682
683
684
685 return scsi_check_sense(scmd);
686 }
687 if (host_byte(scmd->result) != DID_OK)
688 return FAILED;
689
690
691
692
693 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
694 return FAILED;
695
696
697
698
699
700 switch (status_byte(scmd->result)) {
701 case GOOD:
702 scsi_handle_queue_ramp_up(scmd->device);
703 case COMMAND_TERMINATED:
704 return SUCCESS;
705 case CHECK_CONDITION:
706 return scsi_check_sense(scmd);
707 case CONDITION_GOOD:
708 case INTERMEDIATE_GOOD:
709 case INTERMEDIATE_C_GOOD:
710
711
712
713 return SUCCESS;
714 case RESERVATION_CONFLICT:
715 if (scmd->cmnd[0] == TEST_UNIT_READY)
716
717
718 return SUCCESS;
719
720 return FAILED;
721 case QUEUE_FULL:
722 scsi_handle_queue_full(scmd->device);
723
724 case BUSY:
725 return NEEDS_RETRY;
726 default:
727 return FAILED;
728 }
729 return FAILED;
730}
731
732
733
734
735
736static void scsi_eh_done(struct scsi_cmnd *scmd)
737{
738 struct completion *eh_action;
739
740 SCSI_LOG_ERROR_RECOVERY(3,
741 printk("%s scmd: %p result: %x\n",
742 __func__, scmd, scmd->result));
743
744 eh_action = scmd->device->host->eh_action;
745 if (eh_action)
746 complete(eh_action);
747}
748
749
750
751
752
753static int scsi_try_host_reset(struct scsi_cmnd *scmd)
754{
755 unsigned long flags;
756 int rtn;
757 struct Scsi_Host *host = scmd->device->host;
758 struct scsi_host_template *hostt = host->hostt;
759
760 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
761 __func__));
762
763 if (!hostt->eh_host_reset_handler)
764 return FAILED;
765
766 rtn = hostt->eh_host_reset_handler(scmd);
767
768 if (rtn == SUCCESS) {
769 if (!hostt->skip_settle_delay)
770 ssleep(HOST_RESET_SETTLE_TIME);
771 spin_lock_irqsave(host->host_lock, flags);
772 scsi_report_bus_reset(host, scmd_channel(scmd));
773 spin_unlock_irqrestore(host->host_lock, flags);
774 }
775
776 return rtn;
777}
778
779
780
781
782
783static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
784{
785 unsigned long flags;
786 int rtn;
787 struct Scsi_Host *host = scmd->device->host;
788 struct scsi_host_template *hostt = host->hostt;
789
790 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
791 __func__));
792
793 if (!hostt->eh_bus_reset_handler)
794 return FAILED;
795
796 rtn = hostt->eh_bus_reset_handler(scmd);
797
798 if (rtn == SUCCESS) {
799 if (!hostt->skip_settle_delay)
800 ssleep(BUS_RESET_SETTLE_TIME);
801 spin_lock_irqsave(host->host_lock, flags);
802 scsi_report_bus_reset(host, scmd_channel(scmd));
803 spin_unlock_irqrestore(host->host_lock, flags);
804 }
805
806 return rtn;
807}
808
809static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
810{
811 sdev->was_reset = 1;
812 sdev->expecting_cc_ua = 1;
813}
814
815
816
817
818
819
820
821
822
823
824
825static int scsi_try_target_reset(struct scsi_cmnd *scmd)
826{
827 unsigned long flags;
828 int rtn;
829 struct Scsi_Host *host = scmd->device->host;
830 struct scsi_host_template *hostt = host->hostt;
831
832 if (!hostt->eh_target_reset_handler)
833 return FAILED;
834
835 rtn = hostt->eh_target_reset_handler(scmd);
836 if (rtn == SUCCESS) {
837 spin_lock_irqsave(host->host_lock, flags);
838 __starget_for_each_device(scsi_target(scmd->device), NULL,
839 __scsi_report_device_reset);
840 spin_unlock_irqrestore(host->host_lock, flags);
841 }
842
843 return rtn;
844}
845
846
847
848
849
850
851
852
853
854
855
856static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
857{
858 int rtn;
859 struct scsi_host_template *hostt = scmd->device->host->hostt;
860
861 if (!hostt->eh_device_reset_handler)
862 return FAILED;
863
864 rtn = hostt->eh_device_reset_handler(scmd);
865 if (rtn == SUCCESS)
866 __scsi_report_device_reset(scmd->device, NULL);
867 return rtn;
868}
869
870static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
871{
872 if (!hostt->eh_abort_handler)
873 return FAILED;
874
875 return hostt->eh_abort_handler(scmd);
876}
877
878static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
879{
880 if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
881 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
882 if (scsi_try_target_reset(scmd) != SUCCESS)
883 if (scsi_try_bus_reset(scmd) != SUCCESS)
884 scsi_try_host_reset(scmd);
885}
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
902 unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
903{
904 struct scsi_device *sdev = scmd->device;
905
906
907
908
909
910
911
912
913 ses->cmd_len = scmd->cmd_len;
914 ses->cmnd = scmd->cmnd;
915 ses->data_direction = scmd->sc_data_direction;
916 ses->sdb = scmd->sdb;
917 ses->next_rq = scmd->request->next_rq;
918 ses->result = scmd->result;
919 ses->underflow = scmd->underflow;
920 ses->prot_op = scmd->prot_op;
921
922 scmd->prot_op = SCSI_PROT_NORMAL;
923 scmd->cmnd = ses->eh_cmnd;
924 memset(scmd->cmnd, 0, BLK_MAX_CDB);
925 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
926 scmd->request->next_rq = NULL;
927
928 if (sense_bytes) {
929 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
930 sense_bytes);
931 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
932 scmd->sdb.length);
933 scmd->sdb.table.sgl = &ses->sense_sgl;
934 scmd->sc_data_direction = DMA_FROM_DEVICE;
935 scmd->sdb.table.nents = 1;
936 scmd->cmnd[0] = REQUEST_SENSE;
937 scmd->cmnd[4] = scmd->sdb.length;
938 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
939 } else {
940 scmd->sc_data_direction = DMA_NONE;
941 if (cmnd) {
942 BUG_ON(cmnd_size > BLK_MAX_CDB);
943 memcpy(scmd->cmnd, cmnd, cmnd_size);
944 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
945 }
946 }
947
948 scmd->underflow = 0;
949
950 if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
951 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
952 (sdev->lun << 5 & 0xe0);
953
954
955
956
957
958 memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
959}
960EXPORT_SYMBOL(scsi_eh_prep_cmnd);
961
962
963
964
965
966
967
968
969void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
970{
971
972
973
974 scmd->cmd_len = ses->cmd_len;
975 scmd->cmnd = ses->cmnd;
976 scmd->sc_data_direction = ses->data_direction;
977 scmd->sdb = ses->sdb;
978 scmd->request->next_rq = ses->next_rq;
979 scmd->result = ses->result;
980 scmd->underflow = ses->underflow;
981 scmd->prot_op = ses->prot_op;
982}
983EXPORT_SYMBOL(scsi_eh_restore_cmnd);
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
1000 int cmnd_size, int timeout, unsigned sense_bytes)
1001{
1002 struct scsi_device *sdev = scmd->device;
1003 struct Scsi_Host *shost = sdev->host;
1004 DECLARE_COMPLETION_ONSTACK(done);
1005 unsigned long timeleft = timeout;
1006 struct scsi_eh_save ses;
1007 const unsigned long stall_for = msecs_to_jiffies(100);
1008 int rtn;
1009
1010retry:
1011 scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1012 shost->eh_action = &done;
1013
1014 scsi_log_send(scmd);
1015 scmd->scsi_done = scsi_eh_done;
1016 rtn = shost->hostt->queuecommand(shost, scmd);
1017 if (rtn) {
1018 if (timeleft > stall_for) {
1019 scsi_eh_restore_cmnd(scmd, &ses);
1020 timeleft -= stall_for;
1021 msleep(jiffies_to_msecs(stall_for));
1022 goto retry;
1023 }
1024
1025 timeleft = 0;
1026 rtn = NEEDS_RETRY;
1027 } else {
1028 timeleft = wait_for_completion_timeout(&done, timeout);
1029 }
1030
1031 shost->eh_action = NULL;
1032
1033 scsi_log_completion(scmd, rtn);
1034
1035 SCSI_LOG_ERROR_RECOVERY(3,
1036 printk("%s: scmd: %p, timeleft: %ld\n",
1037 __func__, scmd, timeleft));
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048 if (timeleft) {
1049 rtn = scsi_eh_completed_normally(scmd);
1050 SCSI_LOG_ERROR_RECOVERY(3,
1051 printk("%s: scsi_eh_completed_normally %x\n",
1052 __func__, rtn));
1053
1054 switch (rtn) {
1055 case SUCCESS:
1056 case NEEDS_RETRY:
1057 case FAILED:
1058 break;
1059 case ADD_TO_MLQUEUE:
1060 rtn = NEEDS_RETRY;
1061 break;
1062 default:
1063 rtn = FAILED;
1064 break;
1065 }
1066 } else if (!rtn) {
1067 scsi_abort_eh_cmnd(scmd);
1068 rtn = FAILED;
1069 }
1070
1071 scsi_eh_restore_cmnd(scmd, &ses);
1072
1073 return rtn;
1074}
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085static int scsi_request_sense(struct scsi_cmnd *scmd)
1086{
1087 return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1088}
1089
1090static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
1091{
1092 if (scmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
1093 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1094 if (sdrv->eh_action)
1095 rtn = sdrv->eh_action(scmd, rtn);
1096 }
1097 return rtn;
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1113{
1114 scmd->device->host->host_failed--;
1115 scmd->eh_eflags = 0;
1116 list_move_tail(&scmd->eh_entry, done_q);
1117}
1118EXPORT_SYMBOL(scsi_eh_finish_cmd);
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140int scsi_eh_get_sense(struct list_head *work_q,
1141 struct list_head *done_q)
1142{
1143 struct scsi_cmnd *scmd, *next;
1144 struct Scsi_Host *shost;
1145 int rtn;
1146
1147 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1148 if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
1149 SCSI_SENSE_VALID(scmd))
1150 continue;
1151
1152 shost = scmd->device->host;
1153 if (scsi_host_eh_past_deadline(shost)) {
1154 SCSI_LOG_ERROR_RECOVERY(3,
1155 shost_printk(KERN_INFO, shost,
1156 "skip %s, past eh deadline\n",
1157 __func__));
1158 break;
1159 }
1160 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1161 "%s: requesting sense\n",
1162 current->comm));
1163 rtn = scsi_request_sense(scmd);
1164 if (rtn != SUCCESS)
1165 continue;
1166
1167 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
1168 " result %x\n", scmd,
1169 scmd->result));
1170 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
1171
1172 rtn = scsi_decide_disposition(scmd);
1173
1174
1175
1176
1177
1178 if (rtn == SUCCESS)
1179
1180
1181
1182
1183 scmd->retries = scmd->allowed;
1184 else if (rtn != NEEDS_RETRY)
1185 continue;
1186
1187 scsi_eh_finish_cmd(scmd, done_q);
1188 }
1189
1190 return list_empty(work_q);
1191}
1192EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1193
1194
1195
1196
1197
1198
1199
1200
1201static int scsi_eh_tur(struct scsi_cmnd *scmd)
1202{
1203 static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1204 int retry_cnt = 1, rtn;
1205
1206retry_tur:
1207 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1208 scmd->device->eh_timeout, 0);
1209
1210 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
1211 __func__, scmd, rtn));
1212
1213 switch (rtn) {
1214 case NEEDS_RETRY:
1215 if (retry_cnt--)
1216 goto retry_tur;
1217
1218 case SUCCESS:
1219 return 0;
1220 default:
1221 return 1;
1222 }
1223}
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238static int scsi_eh_test_devices(struct list_head *cmd_list,
1239 struct list_head *work_q,
1240 struct list_head *done_q, int try_stu)
1241{
1242 struct scsi_cmnd *scmd, *next;
1243 struct scsi_device *sdev;
1244 int finish_cmds;
1245
1246 while (!list_empty(cmd_list)) {
1247 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1248 sdev = scmd->device;
1249
1250 if (!try_stu) {
1251 if (scsi_host_eh_past_deadline(sdev->host)) {
1252
1253 list_splice_init(cmd_list, work_q);
1254 SCSI_LOG_ERROR_RECOVERY(3,
1255 shost_printk(KERN_INFO, sdev->host,
1256 "skip %s, past eh deadline",
1257 __func__));
1258 break;
1259 }
1260 }
1261
1262 finish_cmds = !scsi_device_online(scmd->device) ||
1263 (try_stu && !scsi_eh_try_stu(scmd) &&
1264 !scsi_eh_tur(scmd)) ||
1265 !scsi_eh_tur(scmd);
1266
1267 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1268 if (scmd->device == sdev) {
1269 if (finish_cmds &&
1270 (try_stu ||
1271 scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1272 scsi_eh_finish_cmd(scmd, done_q);
1273 else
1274 list_move_tail(&scmd->eh_entry, work_q);
1275 }
1276 }
1277 return list_empty(work_q);
1278}
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293static int scsi_eh_abort_cmds(struct list_head *work_q,
1294 struct list_head *done_q)
1295{
1296 struct scsi_cmnd *scmd, *next;
1297 LIST_HEAD(check_list);
1298 int rtn;
1299 struct Scsi_Host *shost;
1300
1301 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1302 if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
1303 continue;
1304 shost = scmd->device->host;
1305 if (scsi_host_eh_past_deadline(shost)) {
1306 list_splice_init(&check_list, work_q);
1307 SCSI_LOG_ERROR_RECOVERY(3,
1308 shost_printk(KERN_INFO, shost,
1309 "skip %s, past eh deadline\n",
1310 __func__));
1311 return list_empty(work_q);
1312 }
1313 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
1314 "0x%p\n", current->comm,
1315 scmd));
1316 rtn = scsi_try_to_abort_cmd(shost->hostt, scmd);
1317 if (rtn == FAILED) {
1318 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
1319 " cmd failed:"
1320 "0x%p\n",
1321 current->comm,
1322 scmd));
1323 list_splice_init(&check_list, work_q);
1324 return list_empty(work_q);
1325 }
1326 scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
1327 if (rtn == FAST_IO_FAIL)
1328 scsi_eh_finish_cmd(scmd, done_q);
1329 else
1330 list_move_tail(&scmd->eh_entry, &check_list);
1331 }
1332
1333 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1334}
1335
1336
1337
1338
1339
1340
1341
1342
1343static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1344{
1345 static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1346
1347 if (scmd->device->allow_restart) {
1348 int i, rtn = NEEDS_RETRY;
1349
1350 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1351 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1352
1353 if (rtn == SUCCESS)
1354 return 0;
1355 }
1356
1357 return 1;
1358}
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370static int scsi_eh_stu(struct Scsi_Host *shost,
1371 struct list_head *work_q,
1372 struct list_head *done_q)
1373{
1374 struct scsi_cmnd *scmd, *stu_scmd, *next;
1375 struct scsi_device *sdev;
1376
1377 shost_for_each_device(sdev, shost) {
1378 if (scsi_host_eh_past_deadline(shost)) {
1379 SCSI_LOG_ERROR_RECOVERY(3,
1380 shost_printk(KERN_INFO, shost,
1381 "skip %s, past eh deadline\n",
1382 __func__));
1383 break;
1384 }
1385 stu_scmd = NULL;
1386 list_for_each_entry(scmd, work_q, eh_entry)
1387 if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1388 scsi_check_sense(scmd) == FAILED ) {
1389 stu_scmd = scmd;
1390 break;
1391 }
1392
1393 if (!stu_scmd)
1394 continue;
1395
1396 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
1397 " 0x%p\n", current->comm, sdev));
1398
1399 if (!scsi_eh_try_stu(stu_scmd)) {
1400 if (!scsi_device_online(sdev) ||
1401 !scsi_eh_tur(stu_scmd)) {
1402 list_for_each_entry_safe(scmd, next,
1403 work_q, eh_entry) {
1404 if (scmd->device == sdev &&
1405 scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1406 scsi_eh_finish_cmd(scmd, done_q);
1407 }
1408 }
1409 } else {
1410 SCSI_LOG_ERROR_RECOVERY(3,
1411 printk("%s: START_UNIT failed to sdev:"
1412 " 0x%p\n", current->comm, sdev));
1413 }
1414 }
1415
1416 return list_empty(work_q);
1417}
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1433 struct list_head *work_q,
1434 struct list_head *done_q)
1435{
1436 struct scsi_cmnd *scmd, *bdr_scmd, *next;
1437 struct scsi_device *sdev;
1438 int rtn;
1439
1440 shost_for_each_device(sdev, shost) {
1441 if (scsi_host_eh_past_deadline(shost)) {
1442 SCSI_LOG_ERROR_RECOVERY(3,
1443 shost_printk(KERN_INFO, shost,
1444 "skip %s, past eh deadline\n",
1445 __func__));
1446 break;
1447 }
1448 bdr_scmd = NULL;
1449 list_for_each_entry(scmd, work_q, eh_entry)
1450 if (scmd->device == sdev) {
1451 bdr_scmd = scmd;
1452 break;
1453 }
1454
1455 if (!bdr_scmd)
1456 continue;
1457
1458 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
1459 " 0x%p\n", current->comm,
1460 sdev));
1461 rtn = scsi_try_bus_device_reset(bdr_scmd);
1462 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1463 if (!scsi_device_online(sdev) ||
1464 rtn == FAST_IO_FAIL ||
1465 !scsi_eh_tur(bdr_scmd)) {
1466 list_for_each_entry_safe(scmd, next,
1467 work_q, eh_entry) {
1468 if (scmd->device == sdev &&
1469 scsi_eh_action(scmd, rtn) != FAILED)
1470 scsi_eh_finish_cmd(scmd,
1471 done_q);
1472 }
1473 }
1474 } else {
1475 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1476 " failed sdev:"
1477 "0x%p\n",
1478 current->comm,
1479 sdev));
1480 }
1481 }
1482
1483 return list_empty(work_q);
1484}
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495static int scsi_eh_target_reset(struct Scsi_Host *shost,
1496 struct list_head *work_q,
1497 struct list_head *done_q)
1498{
1499 LIST_HEAD(tmp_list);
1500 LIST_HEAD(check_list);
1501
1502 list_splice_init(work_q, &tmp_list);
1503
1504 while (!list_empty(&tmp_list)) {
1505 struct scsi_cmnd *next, *scmd;
1506 int rtn;
1507 unsigned int id;
1508
1509 if (scsi_host_eh_past_deadline(shost)) {
1510
1511 list_splice_init(&check_list, work_q);
1512 list_splice_init(&tmp_list, work_q);
1513 SCSI_LOG_ERROR_RECOVERY(3,
1514 shost_printk(KERN_INFO, shost,
1515 "skip %s, past eh deadline\n",
1516 __func__));
1517 return list_empty(work_q);
1518 }
1519
1520 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1521 id = scmd_id(scmd);
1522
1523 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
1524 "to target %d\n",
1525 current->comm, id));
1526 rtn = scsi_try_target_reset(scmd);
1527 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1528 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
1529 " failed target: "
1530 "%d\n",
1531 current->comm, id));
1532 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1533 if (scmd_id(scmd) != id)
1534 continue;
1535
1536 if (rtn == SUCCESS)
1537 list_move_tail(&scmd->eh_entry, &check_list);
1538 else if (rtn == FAST_IO_FAIL)
1539 scsi_eh_finish_cmd(scmd, done_q);
1540 else
1541
1542 list_move(&scmd->eh_entry, work_q);
1543 }
1544 }
1545
1546 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1547}
1548
1549
1550
1551
1552
1553
1554
1555static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1556 struct list_head *work_q,
1557 struct list_head *done_q)
1558{
1559 struct scsi_cmnd *scmd, *chan_scmd, *next;
1560 LIST_HEAD(check_list);
1561 unsigned int channel;
1562 int rtn;
1563
1564
1565
1566
1567
1568
1569
1570
1571 for (channel = 0; channel <= shost->max_channel; channel++) {
1572 if (scsi_host_eh_past_deadline(shost)) {
1573 list_splice_init(&check_list, work_q);
1574 SCSI_LOG_ERROR_RECOVERY(3,
1575 shost_printk(KERN_INFO, shost,
1576 "skip %s, past eh deadline\n",
1577 __func__));
1578 return list_empty(work_q);
1579 }
1580
1581 chan_scmd = NULL;
1582 list_for_each_entry(scmd, work_q, eh_entry) {
1583 if (channel == scmd_channel(scmd)) {
1584 chan_scmd = scmd;
1585 break;
1586
1587
1588
1589
1590 }
1591 }
1592
1593 if (!chan_scmd)
1594 continue;
1595 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1596 " %d\n", current->comm,
1597 channel));
1598 rtn = scsi_try_bus_reset(chan_scmd);
1599 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1600 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1601 if (channel == scmd_channel(scmd)) {
1602 if (rtn == FAST_IO_FAIL)
1603 scsi_eh_finish_cmd(scmd,
1604 done_q);
1605 else
1606 list_move_tail(&scmd->eh_entry,
1607 &check_list);
1608 }
1609 }
1610 } else {
1611 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1612 " failed chan: %d\n",
1613 current->comm,
1614 channel));
1615 }
1616 }
1617 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1618}
1619
1620
1621
1622
1623
1624
1625static int scsi_eh_host_reset(struct list_head *work_q,
1626 struct list_head *done_q)
1627{
1628 struct scsi_cmnd *scmd, *next;
1629 LIST_HEAD(check_list);
1630 int rtn;
1631
1632 if (!list_empty(work_q)) {
1633 scmd = list_entry(work_q->next,
1634 struct scsi_cmnd, eh_entry);
1635
1636 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1637 , current->comm));
1638
1639 rtn = scsi_try_host_reset(scmd);
1640 if (rtn == SUCCESS) {
1641 list_splice_init(work_q, &check_list);
1642 } else if (rtn == FAST_IO_FAIL) {
1643 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1644 scsi_eh_finish_cmd(scmd, done_q);
1645 }
1646 } else {
1647 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1648 " failed\n",
1649 current->comm));
1650 }
1651 }
1652 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1653}
1654
1655
1656
1657
1658
1659
1660static void scsi_eh_offline_sdevs(struct list_head *work_q,
1661 struct list_head *done_q)
1662{
1663 struct scsi_cmnd *scmd, *next;
1664
1665 list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1666 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1667 "not ready after error recovery\n");
1668 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1669 if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
1670
1671
1672
1673 }
1674 scsi_eh_finish_cmd(scmd, done_q);
1675 }
1676 return;
1677}
1678
1679
1680
1681
1682
1683int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1684{
1685 switch (host_byte(scmd->result)) {
1686 case DID_OK:
1687 break;
1688 case DID_TIME_OUT:
1689 goto check_type;
1690 case DID_BUS_BUSY:
1691 return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
1692 case DID_PARITY:
1693 return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
1694 case DID_ERROR:
1695 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1696 status_byte(scmd->result) == RESERVATION_CONFLICT)
1697 return 0;
1698
1699 case DID_SOFT_ERROR:
1700 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
1701 }
1702
1703 if (status_byte(scmd->result) != CHECK_CONDITION)
1704 return 0;
1705
1706check_type:
1707
1708
1709
1710
1711 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
1712 scmd->request->cmd_type == REQ_TYPE_BLOCK_PC)
1713 return 1;
1714 else
1715 return 0;
1716}
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732int scsi_decide_disposition(struct scsi_cmnd *scmd)
1733{
1734 int rtn;
1735
1736
1737
1738
1739
1740 if (!scsi_device_online(scmd->device)) {
1741 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1742 " as SUCCESS\n",
1743 __func__));
1744 return SUCCESS;
1745 }
1746
1747
1748
1749
1750
1751 switch (host_byte(scmd->result)) {
1752 case DID_PASSTHROUGH:
1753
1754
1755
1756
1757
1758 scmd->result &= 0xff00ffff;
1759 return SUCCESS;
1760 case DID_OK:
1761
1762
1763
1764 break;
1765 case DID_ABORT:
1766 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1767 scmd->result |= DID_TIME_OUT << 16;
1768 return SUCCESS;
1769 }
1770 case DID_NO_CONNECT:
1771 case DID_BAD_TARGET:
1772
1773
1774
1775
1776
1777 return SUCCESS;
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787 case DID_SOFT_ERROR:
1788 goto maybe_retry;
1789 case DID_IMM_RETRY:
1790 return NEEDS_RETRY;
1791
1792 case DID_REQUEUE:
1793 return ADD_TO_MLQUEUE;
1794 case DID_TRANSPORT_DISRUPTED:
1795
1796
1797
1798
1799
1800
1801
1802 goto maybe_retry;
1803 case DID_TRANSPORT_FAILFAST:
1804
1805
1806
1807
1808 return SUCCESS;
1809 case DID_ERROR:
1810 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1811 status_byte(scmd->result) == RESERVATION_CONFLICT)
1812
1813
1814
1815
1816 break;
1817
1818 case DID_BUS_BUSY:
1819 case DID_PARITY:
1820 goto maybe_retry;
1821 case DID_TIME_OUT:
1822
1823
1824
1825
1826
1827 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1828 scmd->cmnd[0] == INQUIRY)) {
1829 return SUCCESS;
1830 } else {
1831 return FAILED;
1832 }
1833 case DID_RESET:
1834 return SUCCESS;
1835 default:
1836 return FAILED;
1837 }
1838
1839
1840
1841
1842 if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1843 return FAILED;
1844
1845
1846
1847
1848 switch (status_byte(scmd->result)) {
1849 case QUEUE_FULL:
1850 scsi_handle_queue_full(scmd->device);
1851
1852
1853
1854
1855 case BUSY:
1856
1857
1858
1859
1860
1861
1862 return ADD_TO_MLQUEUE;
1863 case GOOD:
1864 if (scmd->cmnd[0] == REPORT_LUNS)
1865 scmd->device->sdev_target->expecting_lun_change = 0;
1866 scsi_handle_queue_ramp_up(scmd->device);
1867 case COMMAND_TERMINATED:
1868 return SUCCESS;
1869 case TASK_ABORTED:
1870 goto maybe_retry;
1871 case CHECK_CONDITION:
1872 rtn = scsi_check_sense(scmd);
1873 if (rtn == NEEDS_RETRY)
1874 goto maybe_retry;
1875
1876
1877
1878
1879 return rtn;
1880 case CONDITION_GOOD:
1881 case INTERMEDIATE_GOOD:
1882 case INTERMEDIATE_C_GOOD:
1883 case ACA_ACTIVE:
1884
1885
1886
1887 return SUCCESS;
1888
1889 case RESERVATION_CONFLICT:
1890 sdev_printk(KERN_INFO, scmd->device,
1891 "reservation conflict\n");
1892 set_host_byte(scmd, DID_NEXUS_FAILURE);
1893 return SUCCESS;
1894 default:
1895 return FAILED;
1896 }
1897 return FAILED;
1898
1899 maybe_retry:
1900
1901
1902
1903
1904
1905 if ((++scmd->retries) <= scmd->allowed
1906 && !scsi_noretry_cmd(scmd)) {
1907 return NEEDS_RETRY;
1908 } else {
1909
1910
1911
1912 return SUCCESS;
1913 }
1914}
1915
1916static void eh_lock_door_done(struct request *req, int uptodate)
1917{
1918 __blk_put_request(req->q, req);
1919}
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932static void scsi_eh_lock_door(struct scsi_device *sdev)
1933{
1934 struct request *req;
1935
1936
1937
1938
1939
1940 req = blk_get_request(sdev->request_queue, READ, GFP_KERNEL);
1941
1942 req->cmd[0] = ALLOW_MEDIUM_REMOVAL;
1943 req->cmd[1] = 0;
1944 req->cmd[2] = 0;
1945 req->cmd[3] = 0;
1946 req->cmd[4] = SCSI_REMOVAL_PREVENT;
1947 req->cmd[5] = 0;
1948
1949 req->cmd_len = COMMAND_SIZE(req->cmd[0]);
1950
1951 req->cmd_type = REQ_TYPE_BLOCK_PC;
1952 req->cmd_flags |= REQ_QUIET;
1953 req->timeout = 10 * HZ;
1954 req->retries = 5;
1955
1956 blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
1957}
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967static void scsi_restart_operations(struct Scsi_Host *shost)
1968{
1969 struct scsi_device *sdev;
1970 unsigned long flags;
1971
1972
1973
1974
1975
1976
1977 shost_for_each_device(sdev, shost) {
1978 if (scsi_device_online(sdev) && sdev->locked)
1979 scsi_eh_lock_door(sdev);
1980 }
1981
1982
1983
1984
1985
1986
1987 SCSI_LOG_ERROR_RECOVERY(3,
1988 printk("scsi_eh_%d waking up host to restart\n",
1989 shost->host_no));
1990
1991 spin_lock_irqsave(shost->host_lock, flags);
1992 if (scsi_host_set_state(shost, SHOST_RUNNING))
1993 if (scsi_host_set_state(shost, SHOST_CANCEL))
1994 BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
1995 spin_unlock_irqrestore(shost->host_lock, flags);
1996
1997 wake_up(&shost->host_wait);
1998
1999
2000
2001
2002
2003
2004
2005 scsi_run_host_queues(shost);
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015 spin_lock_irqsave(shost->host_lock, flags);
2016 if (shost->host_eh_scheduled)
2017 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2018 WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2019 spin_unlock_irqrestore(shost->host_lock, flags);
2020}
2021
2022
2023
2024
2025
2026
2027
2028void scsi_eh_ready_devs(struct Scsi_Host *shost,
2029 struct list_head *work_q,
2030 struct list_head *done_q)
2031{
2032 if (!scsi_eh_stu(shost, work_q, done_q))
2033 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2034 if (!scsi_eh_target_reset(shost, work_q, done_q))
2035 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2036 if (!scsi_eh_host_reset(work_q, done_q))
2037 scsi_eh_offline_sdevs(work_q,
2038 done_q);
2039}
2040EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2041
2042
2043
2044
2045
2046void scsi_eh_flush_done_q(struct list_head *done_q)
2047{
2048 struct scsi_cmnd *scmd, *next;
2049
2050 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2051 list_del_init(&scmd->eh_entry);
2052 if (scsi_device_online(scmd->device) &&
2053 !scsi_noretry_cmd(scmd) &&
2054 (++scmd->retries <= scmd->allowed)) {
2055 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
2056 " retry cmd: %p\n",
2057 current->comm,
2058 scmd));
2059 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2060 } else {
2061
2062
2063
2064
2065
2066 if (!scmd->result)
2067 scmd->result |= (DRIVER_TIMEOUT << 24);
2068 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
2069 " cmd: %p\n",
2070 current->comm, scmd));
2071 scsi_finish_command(scmd);
2072 }
2073 }
2074}
2075EXPORT_SYMBOL(scsi_eh_flush_done_q);
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100static void scsi_unjam_host(struct Scsi_Host *shost)
2101{
2102 unsigned long flags;
2103 LIST_HEAD(eh_work_q);
2104 LIST_HEAD(eh_done_q);
2105
2106 spin_lock_irqsave(shost->host_lock, flags);
2107 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2108 spin_unlock_irqrestore(shost->host_lock, flags);
2109
2110 SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2111
2112 if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2113 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
2114 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2115
2116 spin_lock_irqsave(shost->host_lock, flags);
2117 if (shost->eh_deadline != -1)
2118 shost->last_reset = 0;
2119 spin_unlock_irqrestore(shost->host_lock, flags);
2120 scsi_eh_flush_done_q(&eh_done_q);
2121}
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131int scsi_error_handler(void *data)
2132{
2133 struct Scsi_Host *shost = data;
2134
2135
2136
2137
2138
2139
2140
2141 while (!kthread_should_stop()) {
2142 set_current_state(TASK_INTERRUPTIBLE);
2143 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2144 shost->host_failed != shost->host_busy) {
2145 SCSI_LOG_ERROR_RECOVERY(1,
2146 printk("scsi_eh_%d: sleeping\n",
2147 shost->host_no));
2148 schedule();
2149 continue;
2150 }
2151
2152 __set_current_state(TASK_RUNNING);
2153 SCSI_LOG_ERROR_RECOVERY(1,
2154 printk("scsi_eh_%d: waking up %d/%d/%d\n",
2155 shost->host_no, shost->host_eh_scheduled,
2156 shost->host_failed, shost->host_busy));
2157
2158
2159
2160
2161
2162
2163 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2164 SCSI_LOG_ERROR_RECOVERY(1,
2165 printk(KERN_ERR "Error handler scsi_eh_%d "
2166 "unable to autoresume\n",
2167 shost->host_no));
2168 continue;
2169 }
2170
2171 if (shost->transportt->eh_strategy_handler)
2172 shost->transportt->eh_strategy_handler(shost);
2173 else
2174 scsi_unjam_host(shost);
2175
2176
2177
2178
2179
2180
2181
2182
2183 scsi_restart_operations(shost);
2184 if (!shost->eh_noresume)
2185 scsi_autopm_put_host(shost);
2186 }
2187 __set_current_state(TASK_RUNNING);
2188
2189 SCSI_LOG_ERROR_RECOVERY(1,
2190 printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
2191 shost->ehandler = NULL;
2192 return 0;
2193}
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2217{
2218 struct scsi_device *sdev;
2219
2220 __shost_for_each_device(sdev, shost) {
2221 if (channel == sdev_channel(sdev))
2222 __scsi_report_device_reset(sdev, NULL);
2223 }
2224}
2225EXPORT_SYMBOL(scsi_report_bus_reset);
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2250{
2251 struct scsi_device *sdev;
2252
2253 __shost_for_each_device(sdev, shost) {
2254 if (channel == sdev_channel(sdev) &&
2255 target == sdev_id(sdev))
2256 __scsi_report_device_reset(sdev, NULL);
2257 }
2258}
2259EXPORT_SYMBOL(scsi_report_device_reset);
2260
2261static void
2262scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2263{
2264}
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279int
2280scsi_reset_provider(struct scsi_device *dev, int flag)
2281{
2282 struct scsi_cmnd *scmd;
2283 struct Scsi_Host *shost = dev->host;
2284 struct request req;
2285 unsigned long flags;
2286 int rtn;
2287
2288 if (scsi_autopm_get_host(shost) < 0)
2289 return FAILED;
2290
2291 scmd = scsi_get_command(dev, GFP_KERNEL);
2292 blk_rq_init(NULL, &req);
2293 scmd->request = &req;
2294
2295 scmd->cmnd = req.cmd;
2296
2297 scmd->scsi_done = scsi_reset_provider_done_command;
2298 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2299
2300 scmd->cmd_len = 0;
2301
2302 scmd->sc_data_direction = DMA_BIDIRECTIONAL;
2303
2304 spin_lock_irqsave(shost->host_lock, flags);
2305 shost->tmf_in_progress = 1;
2306 spin_unlock_irqrestore(shost->host_lock, flags);
2307
2308 switch (flag) {
2309 case SCSI_TRY_RESET_DEVICE:
2310 rtn = scsi_try_bus_device_reset(scmd);
2311 if (rtn == SUCCESS)
2312 break;
2313
2314 case SCSI_TRY_RESET_TARGET:
2315 rtn = scsi_try_target_reset(scmd);
2316 if (rtn == SUCCESS)
2317 break;
2318
2319 case SCSI_TRY_RESET_BUS:
2320 rtn = scsi_try_bus_reset(scmd);
2321 if (rtn == SUCCESS)
2322 break;
2323
2324 case SCSI_TRY_RESET_HOST:
2325 rtn = scsi_try_host_reset(scmd);
2326 break;
2327 default:
2328 rtn = FAILED;
2329 }
2330
2331 spin_lock_irqsave(shost->host_lock, flags);
2332 shost->tmf_in_progress = 0;
2333 spin_unlock_irqrestore(shost->host_lock, flags);
2334
2335
2336
2337
2338
2339 SCSI_LOG_ERROR_RECOVERY(3,
2340 printk("%s: waking up host to restart after TMF\n",
2341 __func__));
2342
2343 wake_up(&shost->host_wait);
2344
2345 scsi_run_host_queues(shost);
2346
2347 scsi_next_command(scmd);
2348 scsi_autopm_put_host(shost);
2349 return rtn;
2350}
2351EXPORT_SYMBOL(scsi_reset_provider);
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
2373 struct scsi_sense_hdr *sshdr)
2374{
2375 if (!sense_buffer || !sb_len)
2376 return 0;
2377
2378 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
2379
2380 sshdr->response_code = (sense_buffer[0] & 0x7f);
2381
2382 if (!scsi_sense_valid(sshdr))
2383 return 0;
2384
2385 if (sshdr->response_code >= 0x72) {
2386
2387
2388
2389 if (sb_len > 1)
2390 sshdr->sense_key = (sense_buffer[1] & 0xf);
2391 if (sb_len > 2)
2392 sshdr->asc = sense_buffer[2];
2393 if (sb_len > 3)
2394 sshdr->ascq = sense_buffer[3];
2395 if (sb_len > 7)
2396 sshdr->additional_length = sense_buffer[7];
2397 } else {
2398
2399
2400
2401 if (sb_len > 2)
2402 sshdr->sense_key = (sense_buffer[2] & 0xf);
2403 if (sb_len > 7) {
2404 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
2405 sb_len : (sense_buffer[7] + 8);
2406 if (sb_len > 12)
2407 sshdr->asc = sense_buffer[12];
2408 if (sb_len > 13)
2409 sshdr->ascq = sense_buffer[13];
2410 }
2411 }
2412
2413 return 1;
2414}
2415EXPORT_SYMBOL(scsi_normalize_sense);
2416
2417int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
2418 struct scsi_sense_hdr *sshdr)
2419{
2420 return scsi_normalize_sense(cmd->sense_buffer,
2421 SCSI_SENSE_BUFFERSIZE, sshdr);
2422}
2423EXPORT_SYMBOL(scsi_command_normalize_sense);
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
2439 int desc_type)
2440{
2441 int add_sen_len, add_len, desc_len, k;
2442 const u8 * descp;
2443
2444 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
2445 return NULL;
2446 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
2447 return NULL;
2448 add_sen_len = (add_sen_len < (sb_len - 8)) ?
2449 add_sen_len : (sb_len - 8);
2450 descp = &sense_buffer[8];
2451 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
2452 descp += desc_len;
2453 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
2454 desc_len = add_len + 2;
2455 if (descp[0] == desc_type)
2456 return descp;
2457 if (add_len < 0)
2458 break;
2459 }
2460 return NULL;
2461}
2462EXPORT_SYMBOL(scsi_sense_desc_find);
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
2475 u64 * info_out)
2476{
2477 int j;
2478 const u8 * ucp;
2479 u64 ull;
2480
2481 if (sb_len < 7)
2482 return 0;
2483 switch (sense_buffer[0] & 0x7f) {
2484 case 0x70:
2485 case 0x71:
2486 if (sense_buffer[0] & 0x80) {
2487 *info_out = (sense_buffer[3] << 24) +
2488 (sense_buffer[4] << 16) +
2489 (sense_buffer[5] << 8) + sense_buffer[6];
2490 return 1;
2491 } else
2492 return 0;
2493 case 0x72:
2494 case 0x73:
2495 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2496 0 );
2497 if (ucp && (0xa == ucp[1])) {
2498 ull = 0;
2499 for (j = 0; j < 8; ++j) {
2500 if (j > 0)
2501 ull <<= 8;
2502 ull |= ucp[4 + j];
2503 }
2504 *info_out = ull;
2505 return 1;
2506 } else
2507 return 0;
2508 default:
2509 return 0;
2510 }
2511}
2512EXPORT_SYMBOL(scsi_get_sense_info_fld);
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
2525{
2526 if (desc) {
2527 buf[0] = 0x72;
2528 buf[1] = key;
2529 buf[2] = asc;
2530 buf[3] = ascq;
2531 buf[7] = 0;
2532 } else {
2533 buf[0] = 0x70;
2534 buf[2] = key;
2535 buf[7] = 0xa;
2536 buf[12] = asc;
2537 buf[13] = ascq;
2538 }
2539}
2540EXPORT_SYMBOL(scsi_build_sense_buffer);
2541