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