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