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