scsi: ufs: Stop using the clock scaling lock in the error handler
[platform/kernel/linux-rpi.git] / drivers / scsi / scsi_error.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  scsi_error.c Copyright (C) 1997 Eric Youngdale
4  *
5  *  SCSI error/timeout handling
6  *      Initial versions: Eric Youngdale.  Based upon conversations with
7  *                        Leonard Zubkoff and David Miller at Linux Expo,
8  *                        ideas originating from all over the place.
9  *
10  *      Restructured scsi_unjam_host and associated functions.
11  *      September 04, 2002 Mike Anderson (andmike@us.ibm.com)
12  *
13  *      Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
14  *      minor cleanups.
15  *      September 30, 2002 Mike Anderson (andmike@us.ibm.com)
16  */
17
18 #include <linux/module.h>
19 #include <linux/sched.h>
20 #include <linux/gfp.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/freezer.h>
25 #include <linux/kthread.h>
26 #include <linux/interrupt.h>
27 #include <linux/blkdev.h>
28 #include <linux/delay.h>
29 #include <linux/jiffies.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_common.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_host.h>
40 #include <scsi/scsi_ioctl.h>
41 #include <scsi/scsi_dh.h>
42 #include <scsi/scsi_devinfo.h>
43 #include <scsi/sg.h>
44
45 #include "scsi_priv.h"
46 #include "scsi_logging.h"
47 #include "scsi_transport_api.h"
48
49 #include <trace/events/scsi.h>
50
51 #include <asm/unaligned.h>
52
53 static void scsi_eh_done(struct scsi_cmnd *scmd);
54
55 /*
56  * These should *probably* be handled by the host itself.
57  * Since it is allowed to sleep, it probably should.
58  */
59 #define BUS_RESET_SETTLE_TIME   (10)
60 #define HOST_RESET_SETTLE_TIME  (10)
61
62 static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
63 static enum scsi_disposition scsi_try_to_abort_cmd(struct scsi_host_template *,
64                                                    struct scsi_cmnd *);
65
66 void scsi_eh_wakeup(struct Scsi_Host *shost)
67 {
68         lockdep_assert_held(shost->host_lock);
69
70         if (scsi_host_busy(shost) == shost->host_failed) {
71                 trace_scsi_eh_wakeup(shost);
72                 wake_up_process(shost->ehandler);
73                 SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
74                         "Waking error handler thread\n"));
75         }
76 }
77
78 /**
79  * scsi_schedule_eh - schedule EH for SCSI host
80  * @shost:      SCSI host to invoke error handling on.
81  *
82  * Schedule SCSI EH without scmd.
83  */
84 void scsi_schedule_eh(struct Scsi_Host *shost)
85 {
86         unsigned long flags;
87
88         spin_lock_irqsave(shost->host_lock, flags);
89
90         if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
91             scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
92                 shost->host_eh_scheduled++;
93                 scsi_eh_wakeup(shost);
94         }
95
96         spin_unlock_irqrestore(shost->host_lock, flags);
97 }
98 EXPORT_SYMBOL_GPL(scsi_schedule_eh);
99
100 static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
101 {
102         if (!shost->last_reset || shost->eh_deadline == -1)
103                 return 0;
104
105         /*
106          * 32bit accesses are guaranteed to be atomic
107          * (on all supported architectures), so instead
108          * of using a spinlock we can as well double check
109          * if eh_deadline has been set to 'off' during the
110          * time_before call.
111          */
112         if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
113             shost->eh_deadline > -1)
114                 return 0;
115
116         return 1;
117 }
118
119 static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd)
120 {
121         if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
122                 return true;
123
124         return ++cmd->retries <= cmd->allowed;
125 }
126
127 static bool scsi_eh_should_retry_cmd(struct scsi_cmnd *cmd)
128 {
129         struct scsi_device *sdev = cmd->device;
130         struct Scsi_Host *host = sdev->host;
131
132         if (host->hostt->eh_should_retry_cmd)
133                 return  host->hostt->eh_should_retry_cmd(cmd);
134
135         return true;
136 }
137
138 static void scsi_eh_complete_abort(struct scsi_cmnd *scmd, struct Scsi_Host *shost)
139 {
140         unsigned long flags;
141
142         spin_lock_irqsave(shost->host_lock, flags);
143         list_del_init(&scmd->eh_entry);
144         /*
145          * If the abort succeeds, and there is no further
146          * EH action, clear the ->last_reset time.
147          */
148         if (list_empty(&shost->eh_abort_list) &&
149             list_empty(&shost->eh_cmd_q))
150                 if (shost->eh_deadline != -1)
151                         shost->last_reset = 0;
152         spin_unlock_irqrestore(shost->host_lock, flags);
153 }
154
155 /**
156  * scmd_eh_abort_handler - Handle command aborts
157  * @work:       command to be aborted.
158  *
159  * Note: this function must be called only for a command that has timed out.
160  * Because the block layer marks a request as complete before it calls
161  * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
162  * timed out do not have any effect. Hence it is safe to call
163  * scsi_finish_command() from this function.
164  */
165 void
166 scmd_eh_abort_handler(struct work_struct *work)
167 {
168         struct scsi_cmnd *scmd =
169                 container_of(work, struct scsi_cmnd, abort_work.work);
170         struct scsi_device *sdev = scmd->device;
171         enum scsi_disposition rtn;
172         unsigned long flags;
173
174         if (scsi_host_eh_past_deadline(sdev->host)) {
175                 SCSI_LOG_ERROR_RECOVERY(3,
176                         scmd_printk(KERN_INFO, scmd,
177                                     "eh timeout, not aborting\n"));
178         } else {
179                 SCSI_LOG_ERROR_RECOVERY(3,
180                         scmd_printk(KERN_INFO, scmd,
181                                     "aborting command\n"));
182                 rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
183                 if (rtn == SUCCESS) {
184                         set_host_byte(scmd, DID_TIME_OUT);
185                         if (scsi_host_eh_past_deadline(sdev->host)) {
186                                 SCSI_LOG_ERROR_RECOVERY(3,
187                                         scmd_printk(KERN_INFO, scmd,
188                                                     "eh timeout, not retrying "
189                                                     "aborted command\n"));
190                         } else if (!scsi_noretry_cmd(scmd) &&
191                                    scsi_cmd_retry_allowed(scmd) &&
192                                 scsi_eh_should_retry_cmd(scmd)) {
193                                 SCSI_LOG_ERROR_RECOVERY(3,
194                                         scmd_printk(KERN_WARNING, scmd,
195                                                     "retry aborted command\n"));
196                                 scsi_eh_complete_abort(scmd, sdev->host);
197                                 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
198                                 return;
199                         } else {
200                                 SCSI_LOG_ERROR_RECOVERY(3,
201                                         scmd_printk(KERN_WARNING, scmd,
202                                                     "finish aborted command\n"));
203                                 scsi_eh_complete_abort(scmd, sdev->host);
204                                 scsi_finish_command(scmd);
205                                 return;
206                         }
207                 } else {
208                         SCSI_LOG_ERROR_RECOVERY(3,
209                                 scmd_printk(KERN_INFO, scmd,
210                                             "cmd abort %s\n",
211                                             (rtn == FAST_IO_FAIL) ?
212                                             "not send" : "failed"));
213                 }
214         }
215
216         spin_lock_irqsave(sdev->host->host_lock, flags);
217         list_del_init(&scmd->eh_entry);
218         spin_unlock_irqrestore(sdev->host->host_lock, flags);
219         scsi_eh_scmd_add(scmd);
220 }
221
222 /**
223  * scsi_abort_command - schedule a command abort
224  * @scmd:       scmd to abort.
225  *
226  * We only need to abort commands after a command timeout
227  */
228 static int
229 scsi_abort_command(struct scsi_cmnd *scmd)
230 {
231         struct scsi_device *sdev = scmd->device;
232         struct Scsi_Host *shost = sdev->host;
233         unsigned long flags;
234
235         if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
236                 /*
237                  * Retry after abort failed, escalate to next level.
238                  */
239                 SCSI_LOG_ERROR_RECOVERY(3,
240                         scmd_printk(KERN_INFO, scmd,
241                                     "previous abort failed\n"));
242                 BUG_ON(delayed_work_pending(&scmd->abort_work));
243                 return FAILED;
244         }
245
246         spin_lock_irqsave(shost->host_lock, flags);
247         if (shost->eh_deadline != -1 && !shost->last_reset)
248                 shost->last_reset = jiffies;
249         BUG_ON(!list_empty(&scmd->eh_entry));
250         list_add_tail(&scmd->eh_entry, &shost->eh_abort_list);
251         spin_unlock_irqrestore(shost->host_lock, flags);
252
253         scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
254         SCSI_LOG_ERROR_RECOVERY(3,
255                 scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
256         queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
257         return SUCCESS;
258 }
259
260 /**
261  * scsi_eh_reset - call into ->eh_action to reset internal counters
262  * @scmd:       scmd to run eh on.
263  *
264  * The scsi driver might be carrying internal state about the
265  * devices, so we need to call into the driver to reset the
266  * internal state once the error handler is started.
267  */
268 static void scsi_eh_reset(struct scsi_cmnd *scmd)
269 {
270         if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
271                 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
272                 if (sdrv->eh_reset)
273                         sdrv->eh_reset(scmd);
274         }
275 }
276
277 static void scsi_eh_inc_host_failed(struct rcu_head *head)
278 {
279         struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
280         struct Scsi_Host *shost = scmd->device->host;
281         unsigned long flags;
282
283         spin_lock_irqsave(shost->host_lock, flags);
284         shost->host_failed++;
285         scsi_eh_wakeup(shost);
286         spin_unlock_irqrestore(shost->host_lock, flags);
287 }
288
289 /**
290  * scsi_eh_scmd_add - add scsi cmd to error handling.
291  * @scmd:       scmd to run eh on.
292  */
293 void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
294 {
295         struct Scsi_Host *shost = scmd->device->host;
296         unsigned long flags;
297         int ret;
298
299         WARN_ON_ONCE(!shost->ehandler);
300
301         spin_lock_irqsave(shost->host_lock, flags);
302         if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
303                 ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
304                 WARN_ON_ONCE(ret);
305         }
306         if (shost->eh_deadline != -1 && !shost->last_reset)
307                 shost->last_reset = jiffies;
308
309         scsi_eh_reset(scmd);
310         list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
311         spin_unlock_irqrestore(shost->host_lock, flags);
312         /*
313          * Ensure that all tasks observe the host state change before the
314          * host_failed change.
315          */
316         call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
317 }
318
319 /**
320  * scsi_times_out - Timeout function for normal scsi commands.
321  * @req:        request that is timing out.
322  *
323  * Notes:
324  *     We do not need to lock this.  There is the potential for a race
325  *     only in that the normal completion handling might run, but if the
326  *     normal completion function determines that the timer has already
327  *     fired, then it mustn't do anything.
328  */
329 enum blk_eh_timer_return scsi_times_out(struct request *req)
330 {
331         struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
332         enum blk_eh_timer_return rtn = BLK_EH_DONE;
333         struct Scsi_Host *host = scmd->device->host;
334
335         trace_scsi_dispatch_cmd_timeout(scmd);
336         scsi_log_completion(scmd, TIMEOUT_ERROR);
337
338         if (host->eh_deadline != -1 && !host->last_reset)
339                 host->last_reset = jiffies;
340
341         if (host->hostt->eh_timed_out)
342                 rtn = host->hostt->eh_timed_out(scmd);
343
344         if (rtn == BLK_EH_DONE) {
345                 /*
346                  * If scsi_done() has already set SCMD_STATE_COMPLETE, do not
347                  * modify *scmd.
348                  */
349                 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
350                         return BLK_EH_DONE;
351                 if (scsi_abort_command(scmd) != SUCCESS) {
352                         set_host_byte(scmd, DID_TIME_OUT);
353                         scsi_eh_scmd_add(scmd);
354                 }
355         }
356
357         return rtn;
358 }
359
360 /**
361  * scsi_block_when_processing_errors - Prevent cmds from being queued.
362  * @sdev:       Device on which we are performing recovery.
363  *
364  * Description:
365  *     We block until the host is out of error recovery, and then check to
366  *     see whether the host or the device is offline.
367  *
368  * Return value:
369  *     0 when dev was taken offline by error recovery. 1 OK to proceed.
370  */
371 int scsi_block_when_processing_errors(struct scsi_device *sdev)
372 {
373         int online;
374
375         wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
376
377         online = scsi_device_online(sdev);
378
379         return online;
380 }
381 EXPORT_SYMBOL(scsi_block_when_processing_errors);
382
383 #ifdef CONFIG_SCSI_LOGGING
384 /**
385  * scsi_eh_prt_fail_stats - Log info on failures.
386  * @shost:      scsi host being recovered.
387  * @work_q:     Queue of scsi cmds to process.
388  */
389 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
390                                           struct list_head *work_q)
391 {
392         struct scsi_cmnd *scmd;
393         struct scsi_device *sdev;
394         int total_failures = 0;
395         int cmd_failed = 0;
396         int cmd_cancel = 0;
397         int devices_failed = 0;
398
399         shost_for_each_device(sdev, shost) {
400                 list_for_each_entry(scmd, work_q, eh_entry) {
401                         if (scmd->device == sdev) {
402                                 ++total_failures;
403                                 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
404                                         ++cmd_cancel;
405                                 else
406                                         ++cmd_failed;
407                         }
408                 }
409
410                 if (cmd_cancel || cmd_failed) {
411                         SCSI_LOG_ERROR_RECOVERY(3,
412                                 shost_printk(KERN_INFO, shost,
413                                             "%s: cmds failed: %d, cancel: %d\n",
414                                             __func__, cmd_failed,
415                                             cmd_cancel));
416                         cmd_cancel = 0;
417                         cmd_failed = 0;
418                         ++devices_failed;
419                 }
420         }
421
422         SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
423                                    "Total of %d commands on %d"
424                                    " devices require eh work\n",
425                                    total_failures, devices_failed));
426 }
427 #endif
428
429  /**
430  * scsi_report_lun_change - Set flag on all *other* devices on the same target
431  *                          to indicate that a UNIT ATTENTION is expected.
432  * @sdev:       Device reporting the UNIT ATTENTION
433  */
434 static void scsi_report_lun_change(struct scsi_device *sdev)
435 {
436         sdev->sdev_target->expecting_lun_change = 1;
437 }
438
439 /**
440  * scsi_report_sense - Examine scsi sense information and log messages for
441  *                     certain conditions, also issue uevents for some of them.
442  * @sdev:       Device reporting the sense code
443  * @sshdr:      sshdr to be examined
444  */
445 static void scsi_report_sense(struct scsi_device *sdev,
446                               struct scsi_sense_hdr *sshdr)
447 {
448         enum scsi_device_event evt_type = SDEV_EVT_MAXBITS;     /* i.e. none */
449
450         if (sshdr->sense_key == UNIT_ATTENTION) {
451                 if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
452                         evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
453                         sdev_printk(KERN_WARNING, sdev,
454                                     "Inquiry data has changed");
455                 } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
456                         evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
457                         scsi_report_lun_change(sdev);
458                         sdev_printk(KERN_WARNING, sdev,
459                                     "Warning! Received an indication that the "
460                                     "LUN assignments on this target have "
461                                     "changed. The Linux SCSI layer does not "
462                                     "automatically remap LUN assignments.\n");
463                 } else if (sshdr->asc == 0x3f)
464                         sdev_printk(KERN_WARNING, sdev,
465                                     "Warning! Received an indication that the "
466                                     "operating parameters on this target have "
467                                     "changed. The Linux SCSI layer does not "
468                                     "automatically adjust these parameters.\n");
469
470                 if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
471                         evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
472                         sdev_printk(KERN_WARNING, sdev,
473                                     "Warning! Received an indication that the "
474                                     "LUN reached a thin provisioning soft "
475                                     "threshold.\n");
476                 }
477
478                 if (sshdr->asc == 0x29) {
479                         evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
480                         /*
481                          * Do not print message if it is an expected side-effect
482                          * of runtime PM.
483                          */
484                         if (!sdev->silence_suspend)
485                                 sdev_printk(KERN_WARNING, sdev,
486                                             "Power-on or device reset occurred\n");
487                 }
488
489                 if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
490                         evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
491                         sdev_printk(KERN_WARNING, sdev,
492                                     "Mode parameters changed");
493                 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
494                         evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
495                         sdev_printk(KERN_WARNING, sdev,
496                                     "Asymmetric access state changed");
497                 } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
498                         evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
499                         sdev_printk(KERN_WARNING, sdev,
500                                     "Capacity data has changed");
501                 } else if (sshdr->asc == 0x2a)
502                         sdev_printk(KERN_WARNING, sdev,
503                                     "Parameters changed");
504         }
505
506         if (evt_type != SDEV_EVT_MAXBITS) {
507                 set_bit(evt_type, sdev->pending_events);
508                 schedule_work(&sdev->event_work);
509         }
510 }
511
512 /**
513  * scsi_check_sense - Examine scsi cmd sense
514  * @scmd:       Cmd to have sense checked.
515  *
516  * Return value:
517  *      SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
518  *
519  * Notes:
520  *      When a deferred error is detected the current command has
521  *      not been executed and needs retrying.
522  */
523 enum scsi_disposition scsi_check_sense(struct scsi_cmnd *scmd)
524 {
525         struct scsi_device *sdev = scmd->device;
526         struct scsi_sense_hdr sshdr;
527
528         if (! scsi_command_normalize_sense(scmd, &sshdr))
529                 return FAILED;  /* no valid sense data */
530
531         scsi_report_sense(sdev, &sshdr);
532
533         if (scsi_sense_is_deferred(&sshdr))
534                 return NEEDS_RETRY;
535
536         if (sdev->handler && sdev->handler->check_sense) {
537                 enum scsi_disposition rc;
538
539                 rc = sdev->handler->check_sense(sdev, &sshdr);
540                 if (rc != SCSI_RETURN_NOT_HANDLED)
541                         return rc;
542                 /* handler does not care. Drop down to default handling */
543         }
544
545         if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
546                 /*
547                  * nasty: for mid-layer issued TURs, we need to return the
548                  * actual sense data without any recovery attempt.  For eh
549                  * issued ones, we need to try to recover and interpret
550                  */
551                 return SUCCESS;
552
553         /*
554          * Previous logic looked for FILEMARK, EOM or ILI which are
555          * mainly associated with tapes and returned SUCCESS.
556          */
557         if (sshdr.response_code == 0x70) {
558                 /* fixed format */
559                 if (scmd->sense_buffer[2] & 0xe0)
560                         return SUCCESS;
561         } else {
562                 /*
563                  * descriptor format: look for "stream commands sense data
564                  * descriptor" (see SSC-3). Assume single sense data
565                  * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
566                  */
567                 if ((sshdr.additional_length > 3) &&
568                     (scmd->sense_buffer[8] == 0x4) &&
569                     (scmd->sense_buffer[11] & 0xe0))
570                         return SUCCESS;
571         }
572
573         switch (sshdr.sense_key) {
574         case NO_SENSE:
575                 return SUCCESS;
576         case RECOVERED_ERROR:
577                 return /* soft_error */ SUCCESS;
578
579         case ABORTED_COMMAND:
580                 if (sshdr.asc == 0x10) /* DIF */
581                         return SUCCESS;
582
583                 if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
584                         return ADD_TO_MLQUEUE;
585                 if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
586                     sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
587                         return ADD_TO_MLQUEUE;
588
589                 return NEEDS_RETRY;
590         case NOT_READY:
591         case UNIT_ATTENTION:
592                 /*
593                  * if we are expecting a cc/ua because of a bus reset that we
594                  * performed, treat this just as a retry.  otherwise this is
595                  * information that we should pass up to the upper-level driver
596                  * so that we can deal with it there.
597                  */
598                 if (scmd->device->expecting_cc_ua) {
599                         /*
600                          * Because some device does not queue unit
601                          * attentions correctly, we carefully check
602                          * additional sense code and qualifier so as
603                          * not to squash media change unit attention.
604                          */
605                         if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
606                                 scmd->device->expecting_cc_ua = 0;
607                                 return NEEDS_RETRY;
608                         }
609                 }
610                 /*
611                  * we might also expect a cc/ua if another LUN on the target
612                  * reported a UA with an ASC/ASCQ of 3F 0E -
613                  * REPORTED LUNS DATA HAS CHANGED.
614                  */
615                 if (scmd->device->sdev_target->expecting_lun_change &&
616                     sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
617                         return NEEDS_RETRY;
618                 /*
619                  * if the device is in the process of becoming ready, we
620                  * should retry.
621                  */
622                 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
623                         return NEEDS_RETRY;
624                 /*
625                  * if the device is not started, we need to wake
626                  * the error handler to start the motor
627                  */
628                 if (scmd->device->allow_restart &&
629                     (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
630                         return FAILED;
631                 /*
632                  * Pass the UA upwards for a determination in the completion
633                  * functions.
634                  */
635                 return SUCCESS;
636
637                 /* these are not supported */
638         case DATA_PROTECT:
639                 if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
640                         /* Thin provisioning hard threshold reached */
641                         set_host_byte(scmd, DID_ALLOC_FAILURE);
642                         return SUCCESS;
643                 }
644                 fallthrough;
645         case COPY_ABORTED:
646         case VOLUME_OVERFLOW:
647         case MISCOMPARE:
648         case BLANK_CHECK:
649                 set_host_byte(scmd, DID_TARGET_FAILURE);
650                 return SUCCESS;
651
652         case MEDIUM_ERROR:
653                 if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
654                     sshdr.asc == 0x13 || /* AMNF DATA FIELD */
655                     sshdr.asc == 0x14) { /* RECORD NOT FOUND */
656                         set_host_byte(scmd, DID_MEDIUM_ERROR);
657                         return SUCCESS;
658                 }
659                 return NEEDS_RETRY;
660
661         case HARDWARE_ERROR:
662                 if (scmd->device->retry_hwerror)
663                         return ADD_TO_MLQUEUE;
664                 else
665                         set_host_byte(scmd, DID_TARGET_FAILURE);
666                 fallthrough;
667
668         case ILLEGAL_REQUEST:
669                 if (sshdr.asc == 0x20 || /* Invalid command operation code */
670                     sshdr.asc == 0x21 || /* Logical block address out of range */
671                     sshdr.asc == 0x22 || /* Invalid function */
672                     sshdr.asc == 0x24 || /* Invalid field in cdb */
673                     sshdr.asc == 0x26 || /* Parameter value invalid */
674                     sshdr.asc == 0x27) { /* Write protected */
675                         set_host_byte(scmd, DID_TARGET_FAILURE);
676                 }
677                 return SUCCESS;
678
679         default:
680                 return SUCCESS;
681         }
682 }
683 EXPORT_SYMBOL_GPL(scsi_check_sense);
684
685 static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
686 {
687         struct scsi_host_template *sht = sdev->host->hostt;
688         struct scsi_device *tmp_sdev;
689
690         if (!sht->track_queue_depth ||
691             sdev->queue_depth >= sdev->max_queue_depth)
692                 return;
693
694         if (time_before(jiffies,
695             sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
696                 return;
697
698         if (time_before(jiffies,
699             sdev->last_queue_full_time + sdev->queue_ramp_up_period))
700                 return;
701
702         /*
703          * Walk all devices of a target and do
704          * ramp up on them.
705          */
706         shost_for_each_device(tmp_sdev, sdev->host) {
707                 if (tmp_sdev->channel != sdev->channel ||
708                     tmp_sdev->id != sdev->id ||
709                     tmp_sdev->queue_depth == sdev->max_queue_depth)
710                         continue;
711
712                 scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
713                 sdev->last_queue_ramp_up = jiffies;
714         }
715 }
716
717 static void scsi_handle_queue_full(struct scsi_device *sdev)
718 {
719         struct scsi_host_template *sht = sdev->host->hostt;
720         struct scsi_device *tmp_sdev;
721
722         if (!sht->track_queue_depth)
723                 return;
724
725         shost_for_each_device(tmp_sdev, sdev->host) {
726                 if (tmp_sdev->channel != sdev->channel ||
727                     tmp_sdev->id != sdev->id)
728                         continue;
729                 /*
730                  * We do not know the number of commands that were at
731                  * the device when we got the queue full so we start
732                  * from the highest possible value and work our way down.
733                  */
734                 scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
735         }
736 }
737
738 /**
739  * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
740  * @scmd:       SCSI cmd to examine.
741  *
742  * Notes:
743  *    This is *only* called when we are examining the status of commands
744  *    queued during error recovery.  the main difference here is that we
745  *    don't allow for the possibility of retries here, and we are a lot
746  *    more restrictive about what we consider acceptable.
747  */
748 static enum scsi_disposition scsi_eh_completed_normally(struct scsi_cmnd *scmd)
749 {
750         /*
751          * first check the host byte, to see if there is anything in there
752          * that would indicate what we need to do.
753          */
754         if (host_byte(scmd->result) == DID_RESET) {
755                 /*
756                  * rats.  we are already in the error handler, so we now
757                  * get to try and figure out what to do next.  if the sense
758                  * is valid, we have a pretty good idea of what to do.
759                  * if not, we mark it as FAILED.
760                  */
761                 return scsi_check_sense(scmd);
762         }
763         if (host_byte(scmd->result) != DID_OK)
764                 return FAILED;
765
766         /*
767          * now, check the status byte to see if this indicates
768          * anything special.
769          */
770         switch (get_status_byte(scmd)) {
771         case SAM_STAT_GOOD:
772                 scsi_handle_queue_ramp_up(scmd->device);
773                 fallthrough;
774         case SAM_STAT_COMMAND_TERMINATED:
775                 return SUCCESS;
776         case SAM_STAT_CHECK_CONDITION:
777                 return scsi_check_sense(scmd);
778         case SAM_STAT_CONDITION_MET:
779         case SAM_STAT_INTERMEDIATE:
780         case SAM_STAT_INTERMEDIATE_CONDITION_MET:
781                 /*
782                  * who knows?  FIXME(eric)
783                  */
784                 return SUCCESS;
785         case SAM_STAT_RESERVATION_CONFLICT:
786                 if (scmd->cmnd[0] == TEST_UNIT_READY)
787                         /* it is a success, we probed the device and
788                          * found it */
789                         return SUCCESS;
790                 /* otherwise, we failed to send the command */
791                 return FAILED;
792         case SAM_STAT_TASK_SET_FULL:
793                 scsi_handle_queue_full(scmd->device);
794                 fallthrough;
795         case SAM_STAT_BUSY:
796                 return NEEDS_RETRY;
797         default:
798                 return FAILED;
799         }
800         return FAILED;
801 }
802
803 /**
804  * scsi_eh_done - Completion function for error handling.
805  * @scmd:       Cmd that is done.
806  */
807 static void scsi_eh_done(struct scsi_cmnd *scmd)
808 {
809         struct completion *eh_action;
810
811         SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
812                         "%s result: %x\n", __func__, scmd->result));
813
814         eh_action = scmd->device->host->eh_action;
815         if (eh_action)
816                 complete(eh_action);
817 }
818
819 /**
820  * scsi_try_host_reset - ask host adapter to reset itself
821  * @scmd:       SCSI cmd to send host reset.
822  */
823 static enum scsi_disposition scsi_try_host_reset(struct scsi_cmnd *scmd)
824 {
825         unsigned long flags;
826         enum scsi_disposition rtn;
827         struct Scsi_Host *host = scmd->device->host;
828         struct scsi_host_template *hostt = host->hostt;
829
830         SCSI_LOG_ERROR_RECOVERY(3,
831                 shost_printk(KERN_INFO, host, "Snd Host RST\n"));
832
833         if (!hostt->eh_host_reset_handler)
834                 return FAILED;
835
836         rtn = hostt->eh_host_reset_handler(scmd);
837
838         if (rtn == SUCCESS) {
839                 if (!hostt->skip_settle_delay)
840                         ssleep(HOST_RESET_SETTLE_TIME);
841                 spin_lock_irqsave(host->host_lock, flags);
842                 scsi_report_bus_reset(host, scmd_channel(scmd));
843                 spin_unlock_irqrestore(host->host_lock, flags);
844         }
845
846         return rtn;
847 }
848
849 /**
850  * scsi_try_bus_reset - ask host to perform a bus reset
851  * @scmd:       SCSI cmd to send bus reset.
852  */
853 static enum scsi_disposition scsi_try_bus_reset(struct scsi_cmnd *scmd)
854 {
855         unsigned long flags;
856         enum scsi_disposition rtn;
857         struct Scsi_Host *host = scmd->device->host;
858         struct scsi_host_template *hostt = host->hostt;
859
860         SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
861                 "%s: Snd Bus RST\n", __func__));
862
863         if (!hostt->eh_bus_reset_handler)
864                 return FAILED;
865
866         rtn = hostt->eh_bus_reset_handler(scmd);
867
868         if (rtn == SUCCESS) {
869                 if (!hostt->skip_settle_delay)
870                         ssleep(BUS_RESET_SETTLE_TIME);
871                 spin_lock_irqsave(host->host_lock, flags);
872                 scsi_report_bus_reset(host, scmd_channel(scmd));
873                 spin_unlock_irqrestore(host->host_lock, flags);
874         }
875
876         return rtn;
877 }
878
879 static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
880 {
881         sdev->was_reset = 1;
882         sdev->expecting_cc_ua = 1;
883 }
884
885 /**
886  * scsi_try_target_reset - Ask host to perform a target reset
887  * @scmd:       SCSI cmd used to send a target reset
888  *
889  * Notes:
890  *    There is no timeout for this operation.  if this operation is
891  *    unreliable for a given host, then the host itself needs to put a
892  *    timer on it, and set the host back to a consistent state prior to
893  *    returning.
894  */
895 static enum scsi_disposition scsi_try_target_reset(struct scsi_cmnd *scmd)
896 {
897         unsigned long flags;
898         enum scsi_disposition rtn;
899         struct Scsi_Host *host = scmd->device->host;
900         struct scsi_host_template *hostt = host->hostt;
901
902         if (!hostt->eh_target_reset_handler)
903                 return FAILED;
904
905         rtn = hostt->eh_target_reset_handler(scmd);
906         if (rtn == SUCCESS) {
907                 spin_lock_irqsave(host->host_lock, flags);
908                 __starget_for_each_device(scsi_target(scmd->device), NULL,
909                                           __scsi_report_device_reset);
910                 spin_unlock_irqrestore(host->host_lock, flags);
911         }
912
913         return rtn;
914 }
915
916 /**
917  * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
918  * @scmd:       SCSI cmd used to send BDR
919  *
920  * Notes:
921  *    There is no timeout for this operation.  if this operation is
922  *    unreliable for a given host, then the host itself needs to put a
923  *    timer on it, and set the host back to a consistent state prior to
924  *    returning.
925  */
926 static enum scsi_disposition scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
927 {
928         enum scsi_disposition rtn;
929         struct scsi_host_template *hostt = scmd->device->host->hostt;
930
931         if (!hostt->eh_device_reset_handler)
932                 return FAILED;
933
934         rtn = hostt->eh_device_reset_handler(scmd);
935         if (rtn == SUCCESS)
936                 __scsi_report_device_reset(scmd->device, NULL);
937         return rtn;
938 }
939
940 /**
941  * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
942  * @hostt:      SCSI driver host template
943  * @scmd:       SCSI cmd used to send a target reset
944  *
945  * Return value:
946  *      SUCCESS, FAILED, or FAST_IO_FAIL
947  *
948  * Notes:
949  *    SUCCESS does not necessarily indicate that the command
950  *    has been aborted; it only indicates that the LLDDs
951  *    has cleared all references to that command.
952  *    LLDDs should return FAILED only if an abort was required
953  *    but could not be executed. LLDDs should return FAST_IO_FAIL
954  *    if the device is temporarily unavailable (eg due to a
955  *    link down on FibreChannel)
956  */
957 static enum scsi_disposition
958 scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
959 {
960         if (!hostt->eh_abort_handler)
961                 return FAILED;
962
963         return hostt->eh_abort_handler(scmd);
964 }
965
966 static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
967 {
968         if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
969                 if (scsi_try_bus_device_reset(scmd) != SUCCESS)
970                         if (scsi_try_target_reset(scmd) != SUCCESS)
971                                 if (scsi_try_bus_reset(scmd) != SUCCESS)
972                                         scsi_try_host_reset(scmd);
973 }
974
975 /**
976  * scsi_eh_prep_cmnd  - Save a scsi command info as part of error recovery
977  * @scmd:       SCSI command structure to hijack
978  * @ses:        structure to save restore information
979  * @cmnd:       CDB to send. Can be NULL if no new cmnd is needed
980  * @cmnd_size:  size in bytes of @cmnd (must be <= BLK_MAX_CDB)
981  * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
982  *
983  * This function is used to save a scsi command information before re-execution
984  * as part of the error recovery process.  If @sense_bytes is 0 the command
985  * sent must be one that does not transfer any data.  If @sense_bytes != 0
986  * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
987  * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
988  */
989 void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
990                         unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
991 {
992         struct scsi_device *sdev = scmd->device;
993
994         /*
995          * We need saved copies of a number of fields - this is because
996          * error handling may need to overwrite these with different values
997          * to run different commands, and once error handling is complete,
998          * we will need to restore these values prior to running the actual
999          * command.
1000          */
1001         ses->cmd_len = scmd->cmd_len;
1002         ses->cmnd = scmd->cmnd;
1003         ses->data_direction = scmd->sc_data_direction;
1004         ses->sdb = scmd->sdb;
1005         ses->result = scmd->result;
1006         ses->resid_len = scmd->req.resid_len;
1007         ses->underflow = scmd->underflow;
1008         ses->prot_op = scmd->prot_op;
1009         ses->eh_eflags = scmd->eh_eflags;
1010
1011         scmd->prot_op = SCSI_PROT_NORMAL;
1012         scmd->eh_eflags = 0;
1013         scmd->cmnd = ses->eh_cmnd;
1014         memset(scmd->cmnd, 0, BLK_MAX_CDB);
1015         memset(&scmd->sdb, 0, sizeof(scmd->sdb));
1016         scmd->result = 0;
1017         scmd->req.resid_len = 0;
1018
1019         if (sense_bytes) {
1020                 scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
1021                                          sense_bytes);
1022                 sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
1023                             scmd->sdb.length);
1024                 scmd->sdb.table.sgl = &ses->sense_sgl;
1025                 scmd->sc_data_direction = DMA_FROM_DEVICE;
1026                 scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
1027                 scmd->cmnd[0] = REQUEST_SENSE;
1028                 scmd->cmnd[4] = scmd->sdb.length;
1029                 scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1030         } else {
1031                 scmd->sc_data_direction = DMA_NONE;
1032                 if (cmnd) {
1033                         BUG_ON(cmnd_size > BLK_MAX_CDB);
1034                         memcpy(scmd->cmnd, cmnd, cmnd_size);
1035                         scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
1036                 }
1037         }
1038
1039         scmd->underflow = 0;
1040
1041         if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
1042                 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
1043                         (sdev->lun << 5 & 0xe0);
1044
1045         /*
1046          * Zero the sense buffer.  The scsi spec mandates that any
1047          * untransferred sense data should be interpreted as being zero.
1048          */
1049         memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1050 }
1051 EXPORT_SYMBOL(scsi_eh_prep_cmnd);
1052
1053 /**
1054  * scsi_eh_restore_cmnd  - Restore a scsi command info as part of error recovery
1055  * @scmd:       SCSI command structure to restore
1056  * @ses:        saved information from a coresponding call to scsi_eh_prep_cmnd
1057  *
1058  * Undo any damage done by above scsi_eh_prep_cmnd().
1059  */
1060 void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
1061 {
1062         /*
1063          * Restore original data
1064          */
1065         scmd->cmd_len = ses->cmd_len;
1066         scmd->cmnd = ses->cmnd;
1067         scmd->sc_data_direction = ses->data_direction;
1068         scmd->sdb = ses->sdb;
1069         scmd->result = ses->result;
1070         scmd->req.resid_len = ses->resid_len;
1071         scmd->underflow = ses->underflow;
1072         scmd->prot_op = ses->prot_op;
1073         scmd->eh_eflags = ses->eh_eflags;
1074 }
1075 EXPORT_SYMBOL(scsi_eh_restore_cmnd);
1076
1077 /**
1078  * scsi_send_eh_cmnd  - submit a scsi command as part of error recovery
1079  * @scmd:       SCSI command structure to hijack
1080  * @cmnd:       CDB to send
1081  * @cmnd_size:  size in bytes of @cmnd
1082  * @timeout:    timeout for this request
1083  * @sense_bytes: size of sense data to copy or 0
1084  *
1085  * This function is used to send a scsi command down to a target device
1086  * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
1087  *
1088  * Return value:
1089  *    SUCCESS or FAILED or NEEDS_RETRY
1090  */
1091 static enum scsi_disposition scsi_send_eh_cmnd(struct scsi_cmnd *scmd,
1092         unsigned char *cmnd, int cmnd_size, int timeout, unsigned sense_bytes)
1093 {
1094         struct scsi_device *sdev = scmd->device;
1095         struct Scsi_Host *shost = sdev->host;
1096         DECLARE_COMPLETION_ONSTACK(done);
1097         unsigned long timeleft = timeout, delay;
1098         struct scsi_eh_save ses;
1099         const unsigned long stall_for = msecs_to_jiffies(100);
1100         int rtn;
1101
1102 retry:
1103         scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
1104         shost->eh_action = &done;
1105
1106         scsi_log_send(scmd);
1107         scmd->scsi_done = scsi_eh_done;
1108
1109         /*
1110          * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
1111          * change the SCSI device state after we have examined it and before
1112          * .queuecommand() is called.
1113          */
1114         mutex_lock(&sdev->state_mutex);
1115         while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
1116                 mutex_unlock(&sdev->state_mutex);
1117                 SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
1118                         "%s: state %d <> %d\n", __func__, sdev->sdev_state,
1119                         SDEV_BLOCK));
1120                 delay = min(timeleft, stall_for);
1121                 timeleft -= delay;
1122                 msleep(jiffies_to_msecs(delay));
1123                 mutex_lock(&sdev->state_mutex);
1124         }
1125         if (sdev->sdev_state != SDEV_BLOCK)
1126                 rtn = shost->hostt->queuecommand(shost, scmd);
1127         else
1128                 rtn = FAILED;
1129         mutex_unlock(&sdev->state_mutex);
1130
1131         if (rtn) {
1132                 if (timeleft > stall_for) {
1133                         scsi_eh_restore_cmnd(scmd, &ses);
1134                         timeleft -= stall_for;
1135                         msleep(jiffies_to_msecs(stall_for));
1136                         goto retry;
1137                 }
1138                 /* signal not to enter either branch of the if () below */
1139                 timeleft = 0;
1140                 rtn = FAILED;
1141         } else {
1142                 timeleft = wait_for_completion_timeout(&done, timeout);
1143                 rtn = SUCCESS;
1144         }
1145
1146         shost->eh_action = NULL;
1147
1148         scsi_log_completion(scmd, rtn);
1149
1150         SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1151                         "%s timeleft: %ld\n",
1152                         __func__, timeleft));
1153
1154         /*
1155          * If there is time left scsi_eh_done got called, and we will examine
1156          * the actual status codes to see whether the command actually did
1157          * complete normally, else if we have a zero return and no time left,
1158          * the command must still be pending, so abort it and return FAILED.
1159          * If we never actually managed to issue the command, because
1160          * ->queuecommand() kept returning non zero, use the rtn = FAILED
1161          * value above (so don't execute either branch of the if)
1162          */
1163         if (timeleft) {
1164                 rtn = scsi_eh_completed_normally(scmd);
1165                 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1166                         "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
1167
1168                 switch (rtn) {
1169                 case SUCCESS:
1170                 case NEEDS_RETRY:
1171                 case FAILED:
1172                         break;
1173                 case ADD_TO_MLQUEUE:
1174                         rtn = NEEDS_RETRY;
1175                         break;
1176                 default:
1177                         rtn = FAILED;
1178                         break;
1179                 }
1180         } else if (rtn != FAILED) {
1181                 scsi_abort_eh_cmnd(scmd);
1182                 rtn = FAILED;
1183         }
1184
1185         scsi_eh_restore_cmnd(scmd, &ses);
1186
1187         return rtn;
1188 }
1189
1190 /**
1191  * scsi_request_sense - Request sense data from a particular target.
1192  * @scmd:       SCSI cmd for request sense.
1193  *
1194  * Notes:
1195  *    Some hosts automatically obtain this information, others require
1196  *    that we obtain it on our own. This function will *not* return until
1197  *    the command either times out, or it completes.
1198  */
1199 static enum scsi_disposition scsi_request_sense(struct scsi_cmnd *scmd)
1200 {
1201         return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
1202 }
1203
1204 static enum scsi_disposition
1205 scsi_eh_action(struct scsi_cmnd *scmd, enum scsi_disposition rtn)
1206 {
1207         if (!blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))) {
1208                 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
1209                 if (sdrv->eh_action)
1210                         rtn = sdrv->eh_action(scmd, rtn);
1211         }
1212         return rtn;
1213 }
1214
1215 /**
1216  * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
1217  * @scmd:       Original SCSI cmd that eh has finished.
1218  * @done_q:     Queue for processed commands.
1219  *
1220  * Notes:
1221  *    We don't want to use the normal command completion while we are are
1222  *    still handling errors - it may cause other commands to be queued,
1223  *    and that would disturb what we are doing.  Thus we really want to
1224  *    keep a list of pending commands for final completion, and once we
1225  *    are ready to leave error handling we handle completion for real.
1226  */
1227 void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
1228 {
1229         list_move_tail(&scmd->eh_entry, done_q);
1230 }
1231 EXPORT_SYMBOL(scsi_eh_finish_cmd);
1232
1233 /**
1234  * scsi_eh_get_sense - Get device sense data.
1235  * @work_q:     Queue of commands to process.
1236  * @done_q:     Queue of processed commands.
1237  *
1238  * Description:
1239  *    See if we need to request sense information.  if so, then get it
1240  *    now, so we have a better idea of what to do.
1241  *
1242  * Notes:
1243  *    This has the unfortunate side effect that if a shost adapter does
1244  *    not automatically request sense information, we end up shutting
1245  *    it down before we request it.
1246  *
1247  *    All drivers should request sense information internally these days,
1248  *    so for now all I have to say is tough noogies if you end up in here.
1249  *
1250  *    XXX: Long term this code should go away, but that needs an audit of
1251  *         all LLDDs first.
1252  */
1253 int scsi_eh_get_sense(struct list_head *work_q,
1254                       struct list_head *done_q)
1255 {
1256         struct scsi_cmnd *scmd, *next;
1257         struct Scsi_Host *shost;
1258         enum scsi_disposition rtn;
1259
1260         /*
1261          * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
1262          * should not get sense.
1263          */
1264         list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1265                 if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
1266                     SCSI_SENSE_VALID(scmd))
1267                         continue;
1268
1269                 shost = scmd->device->host;
1270                 if (scsi_host_eh_past_deadline(shost)) {
1271                         SCSI_LOG_ERROR_RECOVERY(3,
1272                                 scmd_printk(KERN_INFO, scmd,
1273                                             "%s: skip request sense, past eh deadline\n",
1274                                              current->comm));
1275                         break;
1276                 }
1277                 if (!scsi_status_is_check_condition(scmd->result))
1278                         /*
1279                          * don't request sense if there's no check condition
1280                          * status because the error we're processing isn't one
1281                          * that has a sense code (and some devices get
1282                          * confused by sense requests out of the blue)
1283                          */
1284                         continue;
1285
1286                 SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
1287                                                   "%s: requesting sense\n",
1288                                                   current->comm));
1289                 rtn = scsi_request_sense(scmd);
1290                 if (rtn != SUCCESS)
1291                         continue;
1292
1293                 SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1294                         "sense requested, result %x\n", scmd->result));
1295                 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
1296
1297                 rtn = scsi_decide_disposition(scmd);
1298
1299                 /*
1300                  * if the result was normal, then just pass it along to the
1301                  * upper level.
1302                  */
1303                 if (rtn == SUCCESS)
1304                         /*
1305                          * We don't want this command reissued, just finished
1306                          * with the sense data, so set retries to the max
1307                          * allowed to ensure it won't get reissued. If the user
1308                          * has requested infinite retries, we also want to
1309                          * finish this command, so force completion by setting
1310                          * retries and allowed to the same value.
1311                          */
1312                         if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
1313                                 scmd->retries = scmd->allowed = 1;
1314                         else
1315                                 scmd->retries = scmd->allowed;
1316                 else if (rtn != NEEDS_RETRY)
1317                         continue;
1318
1319                 scsi_eh_finish_cmd(scmd, done_q);
1320         }
1321
1322         return list_empty(work_q);
1323 }
1324 EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
1325
1326 /**
1327  * scsi_eh_tur - Send TUR to device.
1328  * @scmd:       &scsi_cmnd to send TUR
1329  *
1330  * Return value:
1331  *    0 - Device is ready. 1 - Device NOT ready.
1332  */
1333 static int scsi_eh_tur(struct scsi_cmnd *scmd)
1334 {
1335         static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
1336         int retry_cnt = 1;
1337         enum scsi_disposition rtn;
1338
1339 retry_tur:
1340         rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
1341                                 scmd->device->eh_timeout, 0);
1342
1343         SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
1344                 "%s return: %x\n", __func__, rtn));
1345
1346         switch (rtn) {
1347         case NEEDS_RETRY:
1348                 if (retry_cnt--)
1349                         goto retry_tur;
1350                 fallthrough;
1351         case SUCCESS:
1352                 return 0;
1353         default:
1354                 return 1;
1355         }
1356 }
1357
1358 /**
1359  * scsi_eh_test_devices - check if devices are responding from error recovery.
1360  * @cmd_list:   scsi commands in error recovery.
1361  * @work_q:     queue for commands which still need more error recovery
1362  * @done_q:     queue for commands which are finished
1363  * @try_stu:    boolean on if a STU command should be tried in addition to TUR.
1364  *
1365  * Decription:
1366  *    Tests if devices are in a working state.  Commands to devices now in
1367  *    a working state are sent to the done_q while commands to devices which
1368  *    are still failing to respond are returned to the work_q for more
1369  *    processing.
1370  **/
1371 static int scsi_eh_test_devices(struct list_head *cmd_list,
1372                                 struct list_head *work_q,
1373                                 struct list_head *done_q, int try_stu)
1374 {
1375         struct scsi_cmnd *scmd, *next;
1376         struct scsi_device *sdev;
1377         int finish_cmds;
1378
1379         while (!list_empty(cmd_list)) {
1380                 scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
1381                 sdev = scmd->device;
1382
1383                 if (!try_stu) {
1384                         if (scsi_host_eh_past_deadline(sdev->host)) {
1385                                 /* Push items back onto work_q */
1386                                 list_splice_init(cmd_list, work_q);
1387                                 SCSI_LOG_ERROR_RECOVERY(3,
1388                                         sdev_printk(KERN_INFO, sdev,
1389                                                     "%s: skip test device, past eh deadline",
1390                                                     current->comm));
1391                                 break;
1392                         }
1393                 }
1394
1395                 finish_cmds = !scsi_device_online(scmd->device) ||
1396                         (try_stu && !scsi_eh_try_stu(scmd) &&
1397                          !scsi_eh_tur(scmd)) ||
1398                         !scsi_eh_tur(scmd);
1399
1400                 list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
1401                         if (scmd->device == sdev) {
1402                                 if (finish_cmds &&
1403                                     (try_stu ||
1404                                      scsi_eh_action(scmd, SUCCESS) == SUCCESS))
1405                                         scsi_eh_finish_cmd(scmd, done_q);
1406                                 else
1407                                         list_move_tail(&scmd->eh_entry, work_q);
1408                         }
1409         }
1410         return list_empty(work_q);
1411 }
1412
1413 /**
1414  * scsi_eh_try_stu - Send START_UNIT to device.
1415  * @scmd:       &scsi_cmnd to send START_UNIT
1416  *
1417  * Return value:
1418  *    0 - Device is ready. 1 - Device NOT ready.
1419  */
1420 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
1421 {
1422         static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
1423
1424         if (scmd->device->allow_restart) {
1425                 int i;
1426                 enum scsi_disposition rtn = NEEDS_RETRY;
1427
1428                 for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
1429                         rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
1430
1431                 if (rtn == SUCCESS)
1432                         return 0;
1433         }
1434
1435         return 1;
1436 }
1437
1438  /**
1439  * scsi_eh_stu - send START_UNIT if needed
1440  * @shost:      &scsi host being recovered.
1441  * @work_q:     &list_head for pending commands.
1442  * @done_q:     &list_head for processed commands.
1443  *
1444  * Notes:
1445  *    If commands are failing due to not ready, initializing command required,
1446  *      try revalidating the device, which will end up sending a start unit.
1447  */
1448 static int scsi_eh_stu(struct Scsi_Host *shost,
1449                               struct list_head *work_q,
1450                               struct list_head *done_q)
1451 {
1452         struct scsi_cmnd *scmd, *stu_scmd, *next;
1453         struct scsi_device *sdev;
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 START_UNIT, past eh deadline\n",
1460                                             current->comm));
1461                         scsi_device_put(sdev);
1462                         break;
1463                 }
1464                 stu_scmd = NULL;
1465                 list_for_each_entry(scmd, work_q, eh_entry)
1466                         if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
1467                             scsi_check_sense(scmd) == FAILED ) {
1468                                 stu_scmd = scmd;
1469                                 break;
1470                         }
1471
1472                 if (!stu_scmd)
1473                         continue;
1474
1475                 SCSI_LOG_ERROR_RECOVERY(3,
1476                         sdev_printk(KERN_INFO, sdev,
1477                                      "%s: Sending START_UNIT\n",
1478                                     current->comm));
1479
1480                 if (!scsi_eh_try_stu(stu_scmd)) {
1481                         if (!scsi_device_online(sdev) ||
1482                             !scsi_eh_tur(stu_scmd)) {
1483                                 list_for_each_entry_safe(scmd, next,
1484                                                           work_q, eh_entry) {
1485                                         if (scmd->device == sdev &&
1486                                             scsi_eh_action(scmd, SUCCESS) == SUCCESS)
1487                                                 scsi_eh_finish_cmd(scmd, done_q);
1488                                 }
1489                         }
1490                 } else {
1491                         SCSI_LOG_ERROR_RECOVERY(3,
1492                                 sdev_printk(KERN_INFO, sdev,
1493                                             "%s: START_UNIT failed\n",
1494                                             current->comm));
1495                 }
1496         }
1497
1498         return list_empty(work_q);
1499 }
1500
1501
1502 /**
1503  * scsi_eh_bus_device_reset - send bdr if needed
1504  * @shost:      scsi host being recovered.
1505  * @work_q:     &list_head for pending commands.
1506  * @done_q:     &list_head for processed commands.
1507  *
1508  * Notes:
1509  *    Try a bus device reset.  Still, look to see whether we have multiple
1510  *    devices that are jammed or not - if we have multiple devices, it
1511  *    makes no sense to try bus_device_reset - we really would need to try
1512  *    a bus_reset instead.
1513  */
1514 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
1515                                     struct list_head *work_q,
1516                                     struct list_head *done_q)
1517 {
1518         struct scsi_cmnd *scmd, *bdr_scmd, *next;
1519         struct scsi_device *sdev;
1520         enum scsi_disposition rtn;
1521
1522         shost_for_each_device(sdev, shost) {
1523                 if (scsi_host_eh_past_deadline(shost)) {
1524                         SCSI_LOG_ERROR_RECOVERY(3,
1525                                 sdev_printk(KERN_INFO, sdev,
1526                                             "%s: skip BDR, past eh deadline\n",
1527                                              current->comm));
1528                         scsi_device_put(sdev);
1529                         break;
1530                 }
1531                 bdr_scmd = NULL;
1532                 list_for_each_entry(scmd, work_q, eh_entry)
1533                         if (scmd->device == sdev) {
1534                                 bdr_scmd = scmd;
1535                                 break;
1536                         }
1537
1538                 if (!bdr_scmd)
1539                         continue;
1540
1541                 SCSI_LOG_ERROR_RECOVERY(3,
1542                         sdev_printk(KERN_INFO, sdev,
1543                                      "%s: Sending BDR\n", current->comm));
1544                 rtn = scsi_try_bus_device_reset(bdr_scmd);
1545                 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1546                         if (!scsi_device_online(sdev) ||
1547                             rtn == FAST_IO_FAIL ||
1548                             !scsi_eh_tur(bdr_scmd)) {
1549                                 list_for_each_entry_safe(scmd, next,
1550                                                          work_q, eh_entry) {
1551                                         if (scmd->device == sdev &&
1552                                             scsi_eh_action(scmd, rtn) != FAILED)
1553                                                 scsi_eh_finish_cmd(scmd,
1554                                                                    done_q);
1555                                 }
1556                         }
1557                 } else {
1558                         SCSI_LOG_ERROR_RECOVERY(3,
1559                                 sdev_printk(KERN_INFO, sdev,
1560                                             "%s: BDR failed\n", current->comm));
1561                 }
1562         }
1563
1564         return list_empty(work_q);
1565 }
1566
1567 /**
1568  * scsi_eh_target_reset - send target reset if needed
1569  * @shost:      scsi host being recovered.
1570  * @work_q:     &list_head for pending commands.
1571  * @done_q:     &list_head for processed commands.
1572  *
1573  * Notes:
1574  *    Try a target reset.
1575  */
1576 static int scsi_eh_target_reset(struct Scsi_Host *shost,
1577                                 struct list_head *work_q,
1578                                 struct list_head *done_q)
1579 {
1580         LIST_HEAD(tmp_list);
1581         LIST_HEAD(check_list);
1582
1583         list_splice_init(work_q, &tmp_list);
1584
1585         while (!list_empty(&tmp_list)) {
1586                 struct scsi_cmnd *next, *scmd;
1587                 enum scsi_disposition rtn;
1588                 unsigned int id;
1589
1590                 if (scsi_host_eh_past_deadline(shost)) {
1591                         /* push back on work queue for further processing */
1592                         list_splice_init(&check_list, work_q);
1593                         list_splice_init(&tmp_list, work_q);
1594                         SCSI_LOG_ERROR_RECOVERY(3,
1595                                 shost_printk(KERN_INFO, shost,
1596                                             "%s: Skip target reset, past eh deadline\n",
1597                                              current->comm));
1598                         return list_empty(work_q);
1599                 }
1600
1601                 scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
1602                 id = scmd_id(scmd);
1603
1604                 SCSI_LOG_ERROR_RECOVERY(3,
1605                         shost_printk(KERN_INFO, shost,
1606                                      "%s: Sending target reset to target %d\n",
1607                                      current->comm, id));
1608                 rtn = scsi_try_target_reset(scmd);
1609                 if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
1610                         SCSI_LOG_ERROR_RECOVERY(3,
1611                                 shost_printk(KERN_INFO, shost,
1612                                              "%s: Target reset failed"
1613                                              " target: %d\n",
1614                                              current->comm, id));
1615                 list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
1616                         if (scmd_id(scmd) != id)
1617                                 continue;
1618
1619                         if (rtn == SUCCESS)
1620                                 list_move_tail(&scmd->eh_entry, &check_list);
1621                         else if (rtn == FAST_IO_FAIL)
1622                                 scsi_eh_finish_cmd(scmd, done_q);
1623                         else
1624                                 /* push back on work queue for further processing */
1625                                 list_move(&scmd->eh_entry, work_q);
1626                 }
1627         }
1628
1629         return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1630 }
1631
1632 /**
1633  * scsi_eh_bus_reset - send a bus reset
1634  * @shost:      &scsi host being recovered.
1635  * @work_q:     &list_head for pending commands.
1636  * @done_q:     &list_head for processed commands.
1637  */
1638 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1639                              struct list_head *work_q,
1640                              struct list_head *done_q)
1641 {
1642         struct scsi_cmnd *scmd, *chan_scmd, *next;
1643         LIST_HEAD(check_list);
1644         unsigned int channel;
1645         enum scsi_disposition rtn;
1646
1647         /*
1648          * we really want to loop over the various channels, and do this on
1649          * a channel by channel basis.  we should also check to see if any
1650          * of the failed commands are on soft_reset devices, and if so, skip
1651          * the reset.
1652          */
1653
1654         for (channel = 0; channel <= shost->max_channel; channel++) {
1655                 if (scsi_host_eh_past_deadline(shost)) {
1656                         list_splice_init(&check_list, work_q);
1657                         SCSI_LOG_ERROR_RECOVERY(3,
1658                                 shost_printk(KERN_INFO, shost,
1659                                             "%s: skip BRST, past eh deadline\n",
1660                                              current->comm));
1661                         return list_empty(work_q);
1662                 }
1663
1664                 chan_scmd = NULL;
1665                 list_for_each_entry(scmd, work_q, eh_entry) {
1666                         if (channel == scmd_channel(scmd)) {
1667                                 chan_scmd = scmd;
1668                                 break;
1669                                 /*
1670                                  * FIXME add back in some support for
1671                                  * soft_reset devices.
1672                                  */
1673                         }
1674                 }
1675
1676                 if (!chan_scmd)
1677                         continue;
1678                 SCSI_LOG_ERROR_RECOVERY(3,
1679                         shost_printk(KERN_INFO, shost,
1680                                      "%s: Sending BRST chan: %d\n",
1681                                      current->comm, channel));
1682                 rtn = scsi_try_bus_reset(chan_scmd);
1683                 if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
1684                         list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1685                                 if (channel == scmd_channel(scmd)) {
1686                                         if (rtn == FAST_IO_FAIL)
1687                                                 scsi_eh_finish_cmd(scmd,
1688                                                                    done_q);
1689                                         else
1690                                                 list_move_tail(&scmd->eh_entry,
1691                                                                &check_list);
1692                                 }
1693                         }
1694                 } else {
1695                         SCSI_LOG_ERROR_RECOVERY(3,
1696                                 shost_printk(KERN_INFO, shost,
1697                                              "%s: BRST failed chan: %d\n",
1698                                              current->comm, channel));
1699                 }
1700         }
1701         return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1702 }
1703
1704 /**
1705  * scsi_eh_host_reset - send a host reset
1706  * @shost:      host to be reset.
1707  * @work_q:     &list_head for pending commands.
1708  * @done_q:     &list_head for processed commands.
1709  */
1710 static int scsi_eh_host_reset(struct Scsi_Host *shost,
1711                               struct list_head *work_q,
1712                               struct list_head *done_q)
1713 {
1714         struct scsi_cmnd *scmd, *next;
1715         LIST_HEAD(check_list);
1716         enum scsi_disposition rtn;
1717
1718         if (!list_empty(work_q)) {
1719                 scmd = list_entry(work_q->next,
1720                                   struct scsi_cmnd, eh_entry);
1721
1722                 SCSI_LOG_ERROR_RECOVERY(3,
1723                         shost_printk(KERN_INFO, shost,
1724                                      "%s: Sending HRST\n",
1725                                      current->comm));
1726
1727                 rtn = scsi_try_host_reset(scmd);
1728                 if (rtn == SUCCESS) {
1729                         list_splice_init(work_q, &check_list);
1730                 } else if (rtn == FAST_IO_FAIL) {
1731                         list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1732                                         scsi_eh_finish_cmd(scmd, done_q);
1733                         }
1734                 } else {
1735                         SCSI_LOG_ERROR_RECOVERY(3,
1736                                 shost_printk(KERN_INFO, shost,
1737                                              "%s: HRST failed\n",
1738                                              current->comm));
1739                 }
1740         }
1741         return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1742 }
1743
1744 /**
1745  * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1746  * @work_q:     &list_head for pending commands.
1747  * @done_q:     &list_head for processed commands.
1748  */
1749 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1750                                   struct list_head *done_q)
1751 {
1752         struct scsi_cmnd *scmd, *next;
1753         struct scsi_device *sdev;
1754
1755         list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
1756                 sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
1757                             "not ready after error recovery\n");
1758                 sdev = scmd->device;
1759
1760                 mutex_lock(&sdev->state_mutex);
1761                 scsi_device_set_state(sdev, SDEV_OFFLINE);
1762                 mutex_unlock(&sdev->state_mutex);
1763
1764                 scsi_eh_finish_cmd(scmd, done_q);
1765         }
1766         return;
1767 }
1768
1769 /**
1770  * scsi_noretry_cmd - determine if command should be failed fast
1771  * @scmd:       SCSI cmd to examine.
1772  */
1773 int scsi_noretry_cmd(struct scsi_cmnd *scmd)
1774 {
1775         struct request *req = scsi_cmd_to_rq(scmd);
1776
1777         switch (host_byte(scmd->result)) {
1778         case DID_OK:
1779                 break;
1780         case DID_TIME_OUT:
1781                 goto check_type;
1782         case DID_BUS_BUSY:
1783                 return req->cmd_flags & REQ_FAILFAST_TRANSPORT;
1784         case DID_PARITY:
1785                 return req->cmd_flags & REQ_FAILFAST_DEV;
1786         case DID_ERROR:
1787                 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1788                         return 0;
1789                 fallthrough;
1790         case DID_SOFT_ERROR:
1791                 return req->cmd_flags & REQ_FAILFAST_DRIVER;
1792         }
1793
1794         if (!scsi_status_is_check_condition(scmd->result))
1795                 return 0;
1796
1797 check_type:
1798         /*
1799          * assume caller has checked sense and determined
1800          * the check condition was retryable.
1801          */
1802         if (req->cmd_flags & REQ_FAILFAST_DEV || blk_rq_is_passthrough(req))
1803                 return 1;
1804
1805         return 0;
1806 }
1807
1808 /**
1809  * scsi_decide_disposition - Disposition a cmd on return from LLD.
1810  * @scmd:       SCSI cmd to examine.
1811  *
1812  * Notes:
1813  *    This is *only* called when we are examining the status after sending
1814  *    out the actual data command.  any commands that are queued for error
1815  *    recovery (e.g. test_unit_ready) do *not* come through here.
1816  *
1817  *    When this routine returns failed, it means the error handler thread
1818  *    is woken.  In cases where the error code indicates an error that
1819  *    doesn't require the error handler read (i.e. we don't need to
1820  *    abort/reset), this function should return SUCCESS.
1821  */
1822 enum scsi_disposition scsi_decide_disposition(struct scsi_cmnd *scmd)
1823 {
1824         enum scsi_disposition rtn;
1825
1826         /*
1827          * if the device is offline, then we clearly just pass the result back
1828          * up to the top level.
1829          */
1830         if (!scsi_device_online(scmd->device)) {
1831                 SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
1832                         "%s: device offline - report as SUCCESS\n", __func__));
1833                 return SUCCESS;
1834         }
1835
1836         /*
1837          * first check the host byte, to see if there is anything in there
1838          * that would indicate what we need to do.
1839          */
1840         switch (host_byte(scmd->result)) {
1841         case DID_PASSTHROUGH:
1842                 /*
1843                  * no matter what, pass this through to the upper layer.
1844                  * nuke this special code so that it looks like we are saying
1845                  * did_ok.
1846                  */
1847                 scmd->result &= 0xff00ffff;
1848                 return SUCCESS;
1849         case DID_OK:
1850                 /*
1851                  * looks good.  drop through, and check the next byte.
1852                  */
1853                 break;
1854         case DID_ABORT:
1855                 if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
1856                         set_host_byte(scmd, DID_TIME_OUT);
1857                         return SUCCESS;
1858                 }
1859                 fallthrough;
1860         case DID_NO_CONNECT:
1861         case DID_BAD_TARGET:
1862                 /*
1863                  * note - this means that we just report the status back
1864                  * to the top level driver, not that we actually think
1865                  * that it indicates SUCCESS.
1866                  */
1867                 return SUCCESS;
1868         case DID_SOFT_ERROR:
1869                 /*
1870                  * when the low level driver returns did_soft_error,
1871                  * it is responsible for keeping an internal retry counter
1872                  * in order to avoid endless loops (db)
1873                  */
1874                 goto maybe_retry;
1875         case DID_IMM_RETRY:
1876                 return NEEDS_RETRY;
1877
1878         case DID_REQUEUE:
1879                 return ADD_TO_MLQUEUE;
1880         case DID_TRANSPORT_DISRUPTED:
1881                 /*
1882                  * LLD/transport was disrupted during processing of the IO.
1883                  * The transport class is now blocked/blocking,
1884                  * and the transport will decide what to do with the IO
1885                  * based on its timers and recovery capablilities if
1886                  * there are enough retries.
1887                  */
1888                 goto maybe_retry;
1889         case DID_TRANSPORT_FAILFAST:
1890                 /*
1891                  * The transport decided to failfast the IO (most likely
1892                  * the fast io fail tmo fired), so send IO directly upwards.
1893                  */
1894                 return SUCCESS;
1895         case DID_TRANSPORT_MARGINAL:
1896                 /*
1897                  * caller has decided not to do retries on
1898                  * abort success, so send IO directly upwards
1899                  */
1900                 return SUCCESS;
1901         case DID_ERROR:
1902                 if (get_status_byte(scmd) == SAM_STAT_RESERVATION_CONFLICT)
1903                         /*
1904                          * execute reservation conflict processing code
1905                          * lower down
1906                          */
1907                         break;
1908                 fallthrough;
1909         case DID_BUS_BUSY:
1910         case DID_PARITY:
1911                 goto maybe_retry;
1912         case DID_TIME_OUT:
1913                 /*
1914                  * when we scan the bus, we get timeout messages for
1915                  * these commands if there is no device available.
1916                  * other hosts report did_no_connect for the same thing.
1917                  */
1918                 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1919                      scmd->cmnd[0] == INQUIRY)) {
1920                         return SUCCESS;
1921                 } else {
1922                         return FAILED;
1923                 }
1924         case DID_RESET:
1925                 return SUCCESS;
1926         default:
1927                 return FAILED;
1928         }
1929
1930         /*
1931          * check the status byte to see if this indicates anything special.
1932          */
1933         switch (get_status_byte(scmd)) {
1934         case SAM_STAT_TASK_SET_FULL:
1935                 scsi_handle_queue_full(scmd->device);
1936                 /*
1937                  * the case of trying to send too many commands to a
1938                  * tagged queueing device.
1939                  */
1940                 fallthrough;
1941         case SAM_STAT_BUSY:
1942                 /*
1943                  * device can't talk to us at the moment.  Should only
1944                  * occur (SAM-3) when the task queue is empty, so will cause
1945                  * the empty queue handling to trigger a stall in the
1946                  * device.
1947                  */
1948                 return ADD_TO_MLQUEUE;
1949         case SAM_STAT_GOOD:
1950                 if (scmd->cmnd[0] == REPORT_LUNS)
1951                         scmd->device->sdev_target->expecting_lun_change = 0;
1952                 scsi_handle_queue_ramp_up(scmd->device);
1953                 fallthrough;
1954         case SAM_STAT_COMMAND_TERMINATED:
1955                 return SUCCESS;
1956         case SAM_STAT_TASK_ABORTED:
1957                 goto maybe_retry;
1958         case SAM_STAT_CHECK_CONDITION:
1959                 rtn = scsi_check_sense(scmd);
1960                 if (rtn == NEEDS_RETRY)
1961                         goto maybe_retry;
1962                 /* if rtn == FAILED, we have no sense information;
1963                  * returning FAILED will wake the error handler thread
1964                  * to collect the sense and redo the decide
1965                  * disposition */
1966                 return rtn;
1967         case SAM_STAT_CONDITION_MET:
1968         case SAM_STAT_INTERMEDIATE:
1969         case SAM_STAT_INTERMEDIATE_CONDITION_MET:
1970         case SAM_STAT_ACA_ACTIVE:
1971                 /*
1972                  * who knows?  FIXME(eric)
1973                  */
1974                 return SUCCESS;
1975
1976         case SAM_STAT_RESERVATION_CONFLICT:
1977                 sdev_printk(KERN_INFO, scmd->device,
1978                             "reservation conflict\n");
1979                 set_host_byte(scmd, DID_NEXUS_FAILURE);
1980                 return SUCCESS; /* causes immediate i/o error */
1981         default:
1982                 return FAILED;
1983         }
1984         return FAILED;
1985
1986 maybe_retry:
1987
1988         /* we requeue for retry because the error was retryable, and
1989          * the request was not marked fast fail.  Note that above,
1990          * even if the request is marked fast fail, we still requeue
1991          * for queue congestion conditions (QUEUE_FULL or BUSY) */
1992         if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
1993                 return NEEDS_RETRY;
1994         } else {
1995                 /*
1996                  * no more retries - report this one back to upper level.
1997                  */
1998                 return SUCCESS;
1999         }
2000 }
2001
2002 static void eh_lock_door_done(struct request *req, blk_status_t status)
2003 {
2004         blk_put_request(req);
2005 }
2006
2007 /**
2008  * scsi_eh_lock_door - Prevent medium removal for the specified device
2009  * @sdev:       SCSI device to prevent medium removal
2010  *
2011  * Locking:
2012  *      We must be called from process context.
2013  *
2014  * Notes:
2015  *      We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
2016  *      head of the devices request queue, and continue.
2017  */
2018 static void scsi_eh_lock_door(struct scsi_device *sdev)
2019 {
2020         struct request *req;
2021         struct scsi_request *rq;
2022
2023         req = blk_get_request(sdev->request_queue, REQ_OP_DRV_IN, 0);
2024         if (IS_ERR(req))
2025                 return;
2026         rq = scsi_req(req);
2027
2028         rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
2029         rq->cmd[1] = 0;
2030         rq->cmd[2] = 0;
2031         rq->cmd[3] = 0;
2032         rq->cmd[4] = SCSI_REMOVAL_PREVENT;
2033         rq->cmd[5] = 0;
2034         rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
2035
2036         req->rq_flags |= RQF_QUIET;
2037         req->timeout = 10 * HZ;
2038         rq->retries = 5;
2039
2040         blk_execute_rq_nowait(NULL, req, 1, eh_lock_door_done);
2041 }
2042
2043 /**
2044  * scsi_restart_operations - restart io operations to the specified host.
2045  * @shost:      Host we are restarting.
2046  *
2047  * Notes:
2048  *    When we entered the error handler, we blocked all further i/o to
2049  *    this device.  we need to 'reverse' this process.
2050  */
2051 static void scsi_restart_operations(struct Scsi_Host *shost)
2052 {
2053         struct scsi_device *sdev;
2054         unsigned long flags;
2055
2056         /*
2057          * If the door was locked, we need to insert a door lock request
2058          * onto the head of the SCSI request queue for the device.  There
2059          * is no point trying to lock the door of an off-line device.
2060          */
2061         shost_for_each_device(sdev, shost) {
2062                 if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
2063                         scsi_eh_lock_door(sdev);
2064                         sdev->was_reset = 0;
2065                 }
2066         }
2067
2068         /*
2069          * next free up anything directly waiting upon the host.  this
2070          * will be requests for character device operations, and also for
2071          * ioctls to queued block devices.
2072          */
2073         SCSI_LOG_ERROR_RECOVERY(3,
2074                 shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
2075
2076         spin_lock_irqsave(shost->host_lock, flags);
2077         if (scsi_host_set_state(shost, SHOST_RUNNING))
2078                 if (scsi_host_set_state(shost, SHOST_CANCEL))
2079                         BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
2080         spin_unlock_irqrestore(shost->host_lock, flags);
2081
2082         wake_up(&shost->host_wait);
2083
2084         /*
2085          * finally we need to re-initiate requests that may be pending.  we will
2086          * have had everything blocked while error handling is taking place, and
2087          * now that error recovery is done, we will need to ensure that these
2088          * requests are started.
2089          */
2090         scsi_run_host_queues(shost);
2091
2092         /*
2093          * if eh is active and host_eh_scheduled is pending we need to re-run
2094          * recovery.  we do this check after scsi_run_host_queues() to allow
2095          * everything pent up since the last eh run a chance to make forward
2096          * progress before we sync again.  Either we'll immediately re-run
2097          * recovery or scsi_device_unbusy() will wake us again when these
2098          * pending commands complete.
2099          */
2100         spin_lock_irqsave(shost->host_lock, flags);
2101         if (shost->host_eh_scheduled)
2102                 if (scsi_host_set_state(shost, SHOST_RECOVERY))
2103                         WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
2104         spin_unlock_irqrestore(shost->host_lock, flags);
2105 }
2106
2107 /**
2108  * scsi_eh_ready_devs - check device ready state and recover if not.
2109  * @shost:      host to be recovered.
2110  * @work_q:     &list_head for pending commands.
2111  * @done_q:     &list_head for processed commands.
2112  */
2113 void scsi_eh_ready_devs(struct Scsi_Host *shost,
2114                         struct list_head *work_q,
2115                         struct list_head *done_q)
2116 {
2117         if (!scsi_eh_stu(shost, work_q, done_q))
2118                 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
2119                         if (!scsi_eh_target_reset(shost, work_q, done_q))
2120                                 if (!scsi_eh_bus_reset(shost, work_q, done_q))
2121                                         if (!scsi_eh_host_reset(shost, work_q, done_q))
2122                                                 scsi_eh_offline_sdevs(work_q,
2123                                                                       done_q);
2124 }
2125 EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
2126
2127 /**
2128  * scsi_eh_flush_done_q - finish processed commands or retry them.
2129  * @done_q:     list_head of processed commands.
2130  */
2131 void scsi_eh_flush_done_q(struct list_head *done_q)
2132 {
2133         struct scsi_cmnd *scmd, *next;
2134
2135         list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
2136                 list_del_init(&scmd->eh_entry);
2137                 if (scsi_device_online(scmd->device) &&
2138                     !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd) &&
2139                         scsi_eh_should_retry_cmd(scmd)) {
2140                         SCSI_LOG_ERROR_RECOVERY(3,
2141                                 scmd_printk(KERN_INFO, scmd,
2142                                              "%s: flush retry cmd\n",
2143                                              current->comm));
2144                                 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
2145                 } else {
2146                         /*
2147                          * If just we got sense for the device (called
2148                          * scsi_eh_get_sense), scmd->result is already
2149                          * set, do not set DID_TIME_OUT.
2150                          */
2151                         if (!scmd->result)
2152                                 scmd->result |= (DID_TIME_OUT << 16);
2153                         SCSI_LOG_ERROR_RECOVERY(3,
2154                                 scmd_printk(KERN_INFO, scmd,
2155                                              "%s: flush finish cmd\n",
2156                                              current->comm));
2157                         scsi_finish_command(scmd);
2158                 }
2159         }
2160 }
2161 EXPORT_SYMBOL(scsi_eh_flush_done_q);
2162
2163 /**
2164  * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
2165  * @shost:      Host to unjam.
2166  *
2167  * Notes:
2168  *    When we come in here, we *know* that all commands on the bus have
2169  *    either completed, failed or timed out.  we also know that no further
2170  *    commands are being sent to the host, so things are relatively quiet
2171  *    and we have freedom to fiddle with things as we wish.
2172  *
2173  *    This is only the *default* implementation.  it is possible for
2174  *    individual drivers to supply their own version of this function, and
2175  *    if the maintainer wishes to do this, it is strongly suggested that
2176  *    this function be taken as a template and modified.  this function
2177  *    was designed to correctly handle problems for about 95% of the
2178  *    different cases out there, and it should always provide at least a
2179  *    reasonable amount of error recovery.
2180  *
2181  *    Any command marked 'failed' or 'timeout' must eventually have
2182  *    scsi_finish_cmd() called for it.  we do all of the retry stuff
2183  *    here, so when we restart the host after we return it should have an
2184  *    empty queue.
2185  */
2186 static void scsi_unjam_host(struct Scsi_Host *shost)
2187 {
2188         unsigned long flags;
2189         LIST_HEAD(eh_work_q);
2190         LIST_HEAD(eh_done_q);
2191
2192         spin_lock_irqsave(shost->host_lock, flags);
2193         list_splice_init(&shost->eh_cmd_q, &eh_work_q);
2194         spin_unlock_irqrestore(shost->host_lock, flags);
2195
2196         SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
2197
2198         if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
2199                 scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
2200
2201         spin_lock_irqsave(shost->host_lock, flags);
2202         if (shost->eh_deadline != -1)
2203                 shost->last_reset = 0;
2204         spin_unlock_irqrestore(shost->host_lock, flags);
2205         scsi_eh_flush_done_q(&eh_done_q);
2206 }
2207
2208 /**
2209  * scsi_error_handler - SCSI error handler thread
2210  * @data:       Host for which we are running.
2211  *
2212  * Notes:
2213  *    This is the main error handling loop.  This is run as a kernel thread
2214  *    for every SCSI host and handles all error handling activity.
2215  */
2216 int scsi_error_handler(void *data)
2217 {
2218         struct Scsi_Host *shost = data;
2219
2220         /*
2221          * We use TASK_INTERRUPTIBLE so that the thread is not
2222          * counted against the load average as a running process.
2223          * We never actually get interrupted because kthread_run
2224          * disables signal delivery for the created thread.
2225          */
2226         while (true) {
2227                 /*
2228                  * The sequence in kthread_stop() sets the stop flag first
2229                  * then wakes the process.  To avoid missed wakeups, the task
2230                  * should always be in a non running state before the stop
2231                  * flag is checked
2232                  */
2233                 set_current_state(TASK_INTERRUPTIBLE);
2234                 if (kthread_should_stop())
2235                         break;
2236
2237                 if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
2238                     shost->host_failed != scsi_host_busy(shost)) {
2239                         SCSI_LOG_ERROR_RECOVERY(1,
2240                                 shost_printk(KERN_INFO, shost,
2241                                              "scsi_eh_%d: sleeping\n",
2242                                              shost->host_no));
2243                         schedule();
2244                         continue;
2245                 }
2246
2247                 __set_current_state(TASK_RUNNING);
2248                 SCSI_LOG_ERROR_RECOVERY(1,
2249                         shost_printk(KERN_INFO, shost,
2250                                      "scsi_eh_%d: waking up %d/%d/%d\n",
2251                                      shost->host_no, shost->host_eh_scheduled,
2252                                      shost->host_failed,
2253                                      scsi_host_busy(shost)));
2254
2255                 /*
2256                  * We have a host that is failing for some reason.  Figure out
2257                  * what we need to do to get it up and online again (if we can).
2258                  * If we fail, we end up taking the thing offline.
2259                  */
2260                 if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
2261                         SCSI_LOG_ERROR_RECOVERY(1,
2262                                 shost_printk(KERN_ERR, shost,
2263                                              "scsi_eh_%d: unable to autoresume\n",
2264                                              shost->host_no));
2265                         continue;
2266                 }
2267
2268                 if (shost->transportt->eh_strategy_handler)
2269                         shost->transportt->eh_strategy_handler(shost);
2270                 else
2271                         scsi_unjam_host(shost);
2272
2273                 /* All scmds have been handled */
2274                 shost->host_failed = 0;
2275
2276                 /*
2277                  * Note - if the above fails completely, the action is to take
2278                  * individual devices offline and flush the queue of any
2279                  * outstanding requests that may have been pending.  When we
2280                  * restart, we restart any I/O to any other devices on the bus
2281                  * which are still online.
2282                  */
2283                 scsi_restart_operations(shost);
2284                 if (!shost->eh_noresume)
2285                         scsi_autopm_put_host(shost);
2286         }
2287         __set_current_state(TASK_RUNNING);
2288
2289         SCSI_LOG_ERROR_RECOVERY(1,
2290                 shost_printk(KERN_INFO, shost,
2291                              "Error handler scsi_eh_%d exiting\n",
2292                              shost->host_no));
2293         shost->ehandler = NULL;
2294         return 0;
2295 }
2296
2297 /*
2298  * Function:    scsi_report_bus_reset()
2299  *
2300  * Purpose:     Utility function used by low-level drivers to report that
2301  *              they have observed a bus reset on the bus being handled.
2302  *
2303  * Arguments:   shost       - Host in question
2304  *              channel     - channel on which reset was observed.
2305  *
2306  * Returns:     Nothing
2307  *
2308  * Lock status: Host lock must be held.
2309  *
2310  * Notes:       This only needs to be called if the reset is one which
2311  *              originates from an unknown location.  Resets originated
2312  *              by the mid-level itself don't need to call this, but there
2313  *              should be no harm.
2314  *
2315  *              The main purpose of this is to make sure that a CHECK_CONDITION
2316  *              is properly treated.
2317  */
2318 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
2319 {
2320         struct scsi_device *sdev;
2321
2322         __shost_for_each_device(sdev, shost) {
2323                 if (channel == sdev_channel(sdev))
2324                         __scsi_report_device_reset(sdev, NULL);
2325         }
2326 }
2327 EXPORT_SYMBOL(scsi_report_bus_reset);
2328
2329 /*
2330  * Function:    scsi_report_device_reset()
2331  *
2332  * Purpose:     Utility function used by low-level drivers to report that
2333  *              they have observed a device reset on the device being handled.
2334  *
2335  * Arguments:   shost       - Host in question
2336  *              channel     - channel on which reset was observed
2337  *              target      - target on which reset was observed
2338  *
2339  * Returns:     Nothing
2340  *
2341  * Lock status: Host lock must be held
2342  *
2343  * Notes:       This only needs to be called if the reset is one which
2344  *              originates from an unknown location.  Resets originated
2345  *              by the mid-level itself don't need to call this, but there
2346  *              should be no harm.
2347  *
2348  *              The main purpose of this is to make sure that a CHECK_CONDITION
2349  *              is properly treated.
2350  */
2351 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
2352 {
2353         struct scsi_device *sdev;
2354
2355         __shost_for_each_device(sdev, shost) {
2356                 if (channel == sdev_channel(sdev) &&
2357                     target == sdev_id(sdev))
2358                         __scsi_report_device_reset(sdev, NULL);
2359         }
2360 }
2361 EXPORT_SYMBOL(scsi_report_device_reset);
2362
2363 static void
2364 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
2365 {
2366 }
2367
2368 /**
2369  * scsi_ioctl_reset: explicitly reset a host/bus/target/device
2370  * @dev:        scsi_device to operate on
2371  * @arg:        reset type (see sg.h)
2372  */
2373 int
2374 scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
2375 {
2376         struct scsi_cmnd *scmd;
2377         struct Scsi_Host *shost = dev->host;
2378         struct request *rq;
2379         unsigned long flags;
2380         int error = 0, val;
2381         enum scsi_disposition rtn;
2382
2383         if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
2384                 return -EACCES;
2385
2386         error = get_user(val, arg);
2387         if (error)
2388                 return error;
2389
2390         if (scsi_autopm_get_host(shost) < 0)
2391                 return -EIO;
2392
2393         error = -EIO;
2394         rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
2395                         shost->hostt->cmd_size, GFP_KERNEL);
2396         if (!rq)
2397                 goto out_put_autopm_host;
2398         blk_rq_init(NULL, rq);
2399
2400         scmd = (struct scsi_cmnd *)(rq + 1);
2401         scsi_init_command(dev, scmd);
2402         scmd->cmnd = scsi_req(rq)->cmd;
2403
2404         scmd->scsi_done         = scsi_reset_provider_done_command;
2405         memset(&scmd->sdb, 0, sizeof(scmd->sdb));
2406
2407         scmd->cmd_len                   = 0;
2408
2409         scmd->sc_data_direction         = DMA_BIDIRECTIONAL;
2410
2411         spin_lock_irqsave(shost->host_lock, flags);
2412         shost->tmf_in_progress = 1;
2413         spin_unlock_irqrestore(shost->host_lock, flags);
2414
2415         switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
2416         case SG_SCSI_RESET_NOTHING:
2417                 rtn = SUCCESS;
2418                 break;
2419         case SG_SCSI_RESET_DEVICE:
2420                 rtn = scsi_try_bus_device_reset(scmd);
2421                 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2422                         break;
2423                 fallthrough;
2424         case SG_SCSI_RESET_TARGET:
2425                 rtn = scsi_try_target_reset(scmd);
2426                 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2427                         break;
2428                 fallthrough;
2429         case SG_SCSI_RESET_BUS:
2430                 rtn = scsi_try_bus_reset(scmd);
2431                 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
2432                         break;
2433                 fallthrough;
2434         case SG_SCSI_RESET_HOST:
2435                 rtn = scsi_try_host_reset(scmd);
2436                 if (rtn == SUCCESS)
2437                         break;
2438                 fallthrough;
2439         default:
2440                 rtn = FAILED;
2441                 break;
2442         }
2443
2444         error = (rtn == SUCCESS) ? 0 : -EIO;
2445
2446         spin_lock_irqsave(shost->host_lock, flags);
2447         shost->tmf_in_progress = 0;
2448         spin_unlock_irqrestore(shost->host_lock, flags);
2449
2450         /*
2451          * be sure to wake up anyone who was sleeping or had their queue
2452          * suspended while we performed the TMF.
2453          */
2454         SCSI_LOG_ERROR_RECOVERY(3,
2455                 shost_printk(KERN_INFO, shost,
2456                              "waking up host to restart after TMF\n"));
2457
2458         wake_up(&shost->host_wait);
2459         scsi_run_host_queues(shost);
2460
2461         kfree(rq);
2462
2463 out_put_autopm_host:
2464         scsi_autopm_put_host(shost);
2465         return error;
2466 }
2467
2468 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
2469                                   struct scsi_sense_hdr *sshdr)
2470 {
2471         return scsi_normalize_sense(cmd->sense_buffer,
2472                         SCSI_SENSE_BUFFERSIZE, sshdr);
2473 }
2474 EXPORT_SYMBOL(scsi_command_normalize_sense);
2475
2476 /**
2477  * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
2478  * @sense_buffer:       byte array of sense data
2479  * @sb_len:             number of valid bytes in sense_buffer
2480  * @info_out:           pointer to 64 integer where 8 or 4 byte information
2481  *                      field will be placed if found.
2482  *
2483  * Return value:
2484  *      true if information field found, false if not found.
2485  */
2486 bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
2487                              u64 *info_out)
2488 {
2489         const u8 * ucp;
2490
2491         if (sb_len < 7)
2492                 return false;
2493         switch (sense_buffer[0] & 0x7f) {
2494         case 0x70:
2495         case 0x71:
2496                 if (sense_buffer[0] & 0x80) {
2497                         *info_out = get_unaligned_be32(&sense_buffer[3]);
2498                         return true;
2499                 }
2500                 return false;
2501         case 0x72:
2502         case 0x73:
2503                 ucp = scsi_sense_desc_find(sense_buffer, sb_len,
2504                                            0 /* info desc */);
2505                 if (ucp && (0xa == ucp[1])) {
2506                         *info_out = get_unaligned_be64(&ucp[4]);
2507                         return true;
2508                 }
2509                 return false;
2510         default:
2511                 return false;
2512         }
2513 }
2514 EXPORT_SYMBOL(scsi_get_sense_info_fld);