VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.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/timer.h>
20 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/kernel.h>
23 #include <linux/interrupt.h>
24 #include <linux/blkdev.h>
25 #include <linux/smp_lock.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_dbg.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_eh.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_ioctl.h>
33 #include <scsi/scsi_request.h>
34
35 #include "scsi_priv.h"
36 #include "scsi_logging.h"
37
38 #define SENSE_TIMEOUT           (10*HZ)
39 #define START_UNIT_TIMEOUT      (30*HZ)
40
41 /*
42  * These should *probably* be handled by the host itself.
43  * Since it is allowed to sleep, it probably should.
44  */
45 #define BUS_RESET_SETTLE_TIME   (10*HZ)
46 #define HOST_RESET_SETTLE_TIME  (10*HZ)
47
48 /* called with shost->host_lock held */
49 void scsi_eh_wakeup(struct Scsi_Host *shost)
50 {
51         if (shost->host_busy == shost->host_failed) {
52                 up(shost->eh_wait);
53                 SCSI_LOG_ERROR_RECOVERY(5,
54                                 printk("Waking error handler thread\n"));
55         }
56 }
57
58 /**
59  * scsi_eh_scmd_add - add scsi cmd to error handling.
60  * @scmd:       scmd to run eh on.
61  * @eh_flag:    optional SCSI_EH flag.
62  *
63  * Return value:
64  *      0 on failure.
65  **/
66 int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
67 {
68         struct Scsi_Host *shost = scmd->device->host;
69         unsigned long flags;
70
71         if (shost->eh_wait == NULL)
72                 return 0;
73
74         spin_lock_irqsave(shost->host_lock, flags);
75
76         scsi_eh_eflags_set(scmd, eh_flag);
77         /*
78          * FIXME: Can we stop setting owner and state.
79          */
80         scmd->owner = SCSI_OWNER_ERROR_HANDLER;
81         scmd->state = SCSI_STATE_FAILED;
82         /*
83          * Set the serial_number_at_timeout to the current
84          * serial_number
85          */
86         scmd->serial_number_at_timeout = scmd->serial_number;
87         list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
88         set_bit(SHOST_RECOVERY, &shost->shost_state);
89         shost->host_failed++;
90         scsi_eh_wakeup(shost);
91         spin_unlock_irqrestore(shost->host_lock, flags);
92         return 1;
93 }
94
95 /**
96  * scsi_add_timer - Start timeout timer for a single scsi command.
97  * @scmd:       scsi command that is about to start running.
98  * @timeout:    amount of time to allow this command to run.
99  * @complete:   timeout function to call if timer isn't canceled.
100  *
101  * Notes:
102  *    This should be turned into an inline function.  Each scsi command
103  *    has its own timer, and as it is added to the queue, we set up the
104  *    timer.  When the command completes, we cancel the timer.
105  **/
106 void scsi_add_timer(struct scsi_cmnd *scmd, int timeout,
107                     void (*complete)(struct scsi_cmnd *))
108 {
109
110         /*
111          * If the clock was already running for this command, then
112          * first delete the timer.  The timer handling code gets rather
113          * confused if we don't do this.
114          */
115         if (scmd->eh_timeout.function)
116                 del_timer(&scmd->eh_timeout);
117
118         scmd->eh_timeout.data = (unsigned long)scmd;
119         scmd->eh_timeout.expires = jiffies + timeout;
120         scmd->eh_timeout.function = (void (*)(unsigned long)) complete;
121
122         SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p, time:"
123                                           " %d, (%p)\n", __FUNCTION__,
124                                           scmd, timeout, complete));
125
126         add_timer(&scmd->eh_timeout);
127 }
128
129 /**
130  * scsi_delete_timer - Delete/cancel timer for a given function.
131  * @scmd:       Cmd that we are canceling timer for
132  *
133  * Notes:
134  *     This should be turned into an inline function.
135  *
136  * Return value:
137  *     1 if we were able to detach the timer.  0 if we blew it, and the
138  *     timer function has already started to run.
139  **/
140 int scsi_delete_timer(struct scsi_cmnd *scmd)
141 {
142         int rtn;
143
144         rtn = del_timer(&scmd->eh_timeout);
145
146         SCSI_LOG_ERROR_RECOVERY(5, printk("%s: scmd: %p,"
147                                          " rtn: %d\n", __FUNCTION__,
148                                          scmd, rtn));
149
150         scmd->eh_timeout.data = (unsigned long)NULL;
151         scmd->eh_timeout.function = NULL;
152
153         return rtn;
154 }
155
156 /**
157  * scsi_times_out - Timeout function for normal scsi commands.
158  * @scmd:       Cmd that is timing out.
159  *
160  * Notes:
161  *     We do not need to lock this.  There is the potential for a race
162  *     only in that the normal completion handling might run, but if the
163  *     normal completion function determines that the timer has already
164  *     fired, then it mustn't do anything.
165  **/
166 void scsi_times_out(struct scsi_cmnd *scmd)
167 {
168         scsi_log_completion(scmd, TIMEOUT_ERROR);
169
170         if (scmd->device->host->hostt->eh_timed_out)
171                 switch (scmd->device->host->hostt->eh_timed_out(scmd)) {
172                 case EH_HANDLED:
173                         __scsi_done(scmd);
174                         return;
175                 case EH_RESET_TIMER:
176                         /* This allows a single retry even of a command
177                          * with allowed == 0 */
178                         if (scmd->retries++ > scmd->allowed)
179                                 break;
180                         scsi_add_timer(scmd, scmd->timeout_per_command,
181                                        scsi_times_out);
182                         return;
183                 case EH_NOT_HANDLED:
184                         break;
185                 }
186
187         if (unlikely(!scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
188                 panic("Error handler thread not present at %p %p %s %d",
189                       scmd, scmd->device->host, __FILE__, __LINE__);
190         }
191 }
192
193 /**
194  * scsi_block_when_processing_errors - Prevent cmds from being queued.
195  * @sdev:       Device on which we are performing recovery.
196  *
197  * Description:
198  *     We block until the host is out of error recovery, and then check to
199  *     see whether the host or the device is offline.
200  *
201  * Return value:
202  *     0 when dev was taken offline by error recovery. 1 OK to proceed.
203  **/
204 int scsi_block_when_processing_errors(struct scsi_device *sdev)
205 {
206         int online;
207
208         wait_event(sdev->host->host_wait, (!test_bit(SHOST_RECOVERY, &sdev->host->shost_state)));
209
210         online = scsi_device_online(sdev);
211
212         SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __FUNCTION__,
213                                           online));
214
215         return online;
216 }
217
218 #ifdef CONFIG_SCSI_LOGGING
219 /**
220  * scsi_eh_prt_fail_stats - Log info on failures.
221  * @shost:      scsi host being recovered.
222  * @work_q:     Queue of scsi cmds to process.
223  **/
224 static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
225                                           struct list_head *work_q)
226 {
227         struct scsi_cmnd *scmd;
228         struct scsi_device *sdev;
229         int total_failures = 0;
230         int cmd_failed = 0;
231         int cmd_cancel = 0;
232         int devices_failed = 0;
233
234         shost_for_each_device(sdev, shost) {
235                 list_for_each_entry(scmd, work_q, eh_entry) {
236                         if (scmd->device == sdev) {
237                                 ++total_failures;
238                                 if (scsi_eh_eflags_chk(scmd,
239                                                        SCSI_EH_CANCEL_CMD))
240                                         ++cmd_cancel;
241                                 else 
242                                         ++cmd_failed;
243                         }
244                 }
245
246                 if (cmd_cancel || cmd_failed) {
247                         SCSI_LOG_ERROR_RECOVERY(3,
248                                 printk("%s: %d:%d:%d:%d cmds failed: %d,"
249                                        " cancel: %d\n",
250                                        __FUNCTION__, shost->host_no,
251                                        sdev->channel, sdev->id, sdev->lun,
252                                        cmd_failed, cmd_cancel));
253                         cmd_cancel = 0;
254                         cmd_failed = 0;
255                         ++devices_failed;
256                 }
257         }
258
259         SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
260                                           " devices require eh work\n",
261                                   total_failures, devices_failed));
262 }
263 #endif
264
265 /**
266  * scsi_check_sense - Examine scsi cmd sense
267  * @scmd:       Cmd to have sense checked.
268  *
269  * Return value:
270  *      SUCCESS or FAILED or NEEDS_RETRY
271  **/
272 static int scsi_check_sense(struct scsi_cmnd *scmd)
273 {
274         if (!SCSI_SENSE_VALID(scmd))
275                 return FAILED;
276
277         if (scmd->sense_buffer[2] & 0xe0)
278                 return SUCCESS;
279
280         switch (scmd->sense_buffer[2] & 0xf) {
281         case NO_SENSE:
282                 return SUCCESS;
283         case RECOVERED_ERROR:
284                 return /* soft_error */ SUCCESS;
285
286         case ABORTED_COMMAND:
287                 return NEEDS_RETRY;
288         case NOT_READY:
289         case UNIT_ATTENTION:
290                 /*
291                  * if we are expecting a cc/ua because of a bus reset that we
292                  * performed, treat this just as a retry.  otherwise this is
293                  * information that we should pass up to the upper-level driver
294                  * so that we can deal with it there.
295                  */
296                 if (scmd->device->expecting_cc_ua) {
297                         scmd->device->expecting_cc_ua = 0;
298                         return NEEDS_RETRY;
299                 }
300                 /*
301                  * if the device is in the process of becoming ready, we 
302                  * should retry.
303                  */
304                 if ((scmd->sense_buffer[12] == 0x04) &&
305                         (scmd->sense_buffer[13] == 0x01)) {
306                         return NEEDS_RETRY;
307                 }
308                 /*
309                  * if the device is not started, we need to wake
310                  * the error handler to start the motor
311                  */
312                 if (scmd->device->allow_restart &&
313                     (scmd->sense_buffer[12] == 0x04) &&
314                     (scmd->sense_buffer[13] == 0x02)) {
315                         return FAILED;
316                 }
317                 return SUCCESS;
318
319                 /* these three are not supported */
320         case COPY_ABORTED:
321         case VOLUME_OVERFLOW:
322         case MISCOMPARE:
323                 return SUCCESS;
324
325         case MEDIUM_ERROR:
326                 return NEEDS_RETRY;
327
328         case ILLEGAL_REQUEST:
329         case BLANK_CHECK:
330         case DATA_PROTECT:
331         case HARDWARE_ERROR:
332         default:
333                 return SUCCESS;
334         }
335 }
336
337 /**
338  * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
339  * @scmd:       SCSI cmd to examine.
340  *
341  * Notes:
342  *    This is *only* called when we are examining the status of commands
343  *    queued during error recovery.  the main difference here is that we
344  *    don't allow for the possibility of retries here, and we are a lot
345  *    more restrictive about what we consider acceptable.
346  **/
347 static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
348 {
349         /*
350          * first check the host byte, to see if there is anything in there
351          * that would indicate what we need to do.
352          */
353         if (host_byte(scmd->result) == DID_RESET) {
354                 /*
355                  * rats.  we are already in the error handler, so we now
356                  * get to try and figure out what to do next.  if the sense
357                  * is valid, we have a pretty good idea of what to do.
358                  * if not, we mark it as FAILED.
359                  */
360                 return scsi_check_sense(scmd);
361         }
362         if (host_byte(scmd->result) != DID_OK)
363                 return FAILED;
364
365         /*
366          * next, check the message byte.
367          */
368         if (msg_byte(scmd->result) != COMMAND_COMPLETE)
369                 return FAILED;
370
371         /*
372          * now, check the status byte to see if this indicates
373          * anything special.
374          */
375         switch (status_byte(scmd->result)) {
376         case GOOD:
377         case COMMAND_TERMINATED:
378                 return SUCCESS;
379         case CHECK_CONDITION:
380                 return scsi_check_sense(scmd);
381         case CONDITION_GOOD:
382         case INTERMEDIATE_GOOD:
383         case INTERMEDIATE_C_GOOD:
384                 /*
385                  * who knows?  FIXME(eric)
386                  */
387                 return SUCCESS;
388         case BUSY:
389         case QUEUE_FULL:
390         case RESERVATION_CONFLICT:
391         default:
392                 return FAILED;
393         }
394         return FAILED;
395 }
396
397 /**
398  * scsi_eh_times_out - timeout function for error handling.
399  * @scmd:       Cmd that is timing out.
400  *
401  * Notes:
402  *    During error handling, the kernel thread will be sleeping waiting
403  *    for some action to complete on the device.  our only job is to
404  *    record that it timed out, and to wake up the thread.
405  **/
406 static void scsi_eh_times_out(struct scsi_cmnd *scmd)
407 {
408         scsi_eh_eflags_set(scmd, SCSI_EH_REC_TIMEOUT);
409         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd:%p\n", __FUNCTION__,
410                                           scmd));
411
412         if (scmd->device->host->eh_action)
413                 up(scmd->device->host->eh_action);
414 }
415
416 /**
417  * scsi_eh_done - Completion function for error handling.
418  * @scmd:       Cmd that is done.
419  **/
420 static void scsi_eh_done(struct scsi_cmnd *scmd)
421 {
422         /*
423          * if the timeout handler is already running, then just set the
424          * flag which says we finished late, and return.  we have no
425          * way of stopping the timeout handler from running, so we must
426          * always defer to it.
427          */
428         if (del_timer(&scmd->eh_timeout)) {
429                 scmd->request->rq_status = RQ_SCSI_DONE;
430                 scmd->owner = SCSI_OWNER_ERROR_HANDLER;
431
432                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s scmd: %p result: %x\n",
433                                            __FUNCTION__, scmd, scmd->result));
434
435                 if (scmd->device->host->eh_action)
436                         up(scmd->device->host->eh_action);
437         }
438 }
439
440 /**
441  * scsi_send_eh_cmnd  - send a cmd to a device as part of error recovery.
442  * @scmd:       SCSI Cmd to send.
443  * @timeout:    Timeout for cmd.
444  *
445  * Notes:
446  *    The initialization of the structures is quite a bit different in
447  *    this case, and furthermore, there is a different completion handler
448  *    vs scsi_dispatch_cmd.
449  * Return value:
450  *    SUCCESS or FAILED or NEEDS_RETRY
451  **/
452 static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, int timeout)
453 {
454         struct Scsi_Host *host = scmd->device->host;
455         DECLARE_MUTEX_LOCKED(sem);
456         unsigned long flags;
457         int rtn = SUCCESS;
458
459         /*
460          * we will use a queued command if possible, otherwise we will
461          * emulate the queuing and calling of completion function ourselves.
462          */
463         scmd->owner = SCSI_OWNER_LOWLEVEL;
464
465         if (scmd->device->scsi_level <= SCSI_2)
466                 scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
467                         (scmd->device->lun << 5 & 0xe0);
468
469         scsi_add_timer(scmd, timeout, scsi_eh_times_out);
470
471         /*
472          * set up the semaphore so we wait for the command to complete.
473          */
474         scmd->device->host->eh_action = &sem;
475         scmd->request->rq_status = RQ_SCSI_BUSY;
476
477         spin_lock_irqsave(scmd->device->host->host_lock, flags);
478         scsi_log_send(scmd);
479         host->hostt->queuecommand(scmd, scsi_eh_done);
480         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
481
482         down(&sem);
483         scsi_log_completion(scmd, SUCCESS);
484
485         scmd->device->host->eh_action = NULL;
486
487         /*
488          * see if timeout.  if so, tell the host to forget about it.
489          * in other words, we don't want a callback any more.
490          */
491         if (scsi_eh_eflags_chk(scmd, SCSI_EH_REC_TIMEOUT)) {
492                 scsi_eh_eflags_clr(scmd,  SCSI_EH_REC_TIMEOUT);
493                 scmd->owner = SCSI_OWNER_LOWLEVEL;
494
495                 /*
496                  * as far as the low level driver is
497                  * concerned, this command is still active, so
498                  * we must give the low level driver a chance
499                  * to abort it. (db) 
500                  *
501                  * FIXME(eric) - we are not tracking whether we could
502                  * abort a timed out command or not.  not sure how
503                  * we should treat them differently anyways.
504                  */
505                 spin_lock_irqsave(scmd->device->host->host_lock, flags);
506                 if (scmd->device->host->hostt->eh_abort_handler)
507                         scmd->device->host->hostt->eh_abort_handler(scmd);
508                 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
509                         
510                 scmd->request->rq_status = RQ_SCSI_DONE;
511                 scmd->owner = SCSI_OWNER_ERROR_HANDLER;
512                         
513                 rtn = FAILED;
514         }
515
516         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd: %p, rtn:%x\n",
517                                           __FUNCTION__, scmd, rtn));
518
519         /*
520          * now examine the actual status codes to see whether the command
521          * actually did complete normally.
522          */
523         if (rtn == SUCCESS) {
524                 rtn = scsi_eh_completed_normally(scmd);
525                 SCSI_LOG_ERROR_RECOVERY(3,
526                         printk("%s: scsi_eh_completed_normally %x\n",
527                                __FUNCTION__, rtn));
528                 switch (rtn) {
529                 case SUCCESS:
530                 case NEEDS_RETRY:
531                 case FAILED:
532                         break;
533                 default:
534                         rtn = FAILED;
535                         break;
536                 }
537         }
538
539         return rtn;
540 }
541
542 /**
543  * scsi_request_sense - Request sense data from a particular target.
544  * @scmd:       SCSI cmd for request sense.
545  *
546  * Notes:
547  *    Some hosts automatically obtain this information, others require
548  *    that we obtain it on our own. This function will *not* return until
549  *    the command either times out, or it completes.
550  **/
551 static int scsi_request_sense(struct scsi_cmnd *scmd)
552 {
553         static unsigned char generic_sense[6] =
554         {REQUEST_SENSE, 0, 0, 0, 252, 0};
555         unsigned char *scsi_result;
556         int saved_result;
557         int rtn;
558
559         memcpy(scmd->cmnd, generic_sense, sizeof(generic_sense));
560
561         scsi_result = kmalloc(252, GFP_ATOMIC | (scmd->device->host->hostt->unchecked_isa_dma) ? __GFP_DMA : 0);
562
563
564         if (unlikely(!scsi_result)) {
565                 printk(KERN_ERR "%s: cannot allocate scsi_result.\n",
566                        __FUNCTION__);
567                 return FAILED;
568         }
569
570         /*
571          * zero the sense buffer.  some host adapters automatically always
572          * request sense, so it is not a good idea that
573          * scmd->request_buffer and scmd->sense_buffer point to the same
574          * address (db).  0 is not a valid sense code. 
575          */
576         memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
577         memset(scsi_result, 0, 252);
578
579         saved_result = scmd->result;
580         scmd->request_buffer = scsi_result;
581         scmd->request_bufflen = 252;
582         scmd->use_sg = 0;
583         scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
584         scmd->sc_data_direction = DMA_FROM_DEVICE;
585         scmd->underflow = 0;
586
587         rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
588
589         /* last chance to have valid sense data */
590         if(!SCSI_SENSE_VALID(scmd)) {
591                 memcpy(scmd->sense_buffer, scmd->request_buffer,
592                        sizeof(scmd->sense_buffer));
593         }
594
595         kfree(scsi_result);
596
597         /*
598          * when we eventually call scsi_finish, we really wish to complete
599          * the original request, so let's restore the original data. (db)
600          */
601         scsi_setup_cmd_retry(scmd);
602         scmd->result = saved_result;
603         return rtn;
604 }
605
606 /**
607  * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
608  * @scmd:       Original SCSI cmd that eh has finished.
609  * @done_q:     Queue for processed commands.
610  *
611  * Notes:
612  *    We don't want to use the normal command completion while we are are
613  *    still handling errors - it may cause other commands to be queued,
614  *    and that would disturb what we are doing.  thus we really want to
615  *    keep a list of pending commands for final completion, and once we
616  *    are ready to leave error handling we handle completion for real.
617  **/
618 static void scsi_eh_finish_cmd(struct scsi_cmnd *scmd,
619                                struct list_head *done_q)
620 {
621         scmd->device->host->host_failed--;
622         scmd->state = SCSI_STATE_BHQUEUE;
623
624         scsi_eh_eflags_clr_all(scmd);
625
626         /*
627          * set this back so that the upper level can correctly free up
628          * things.
629          */
630         scsi_setup_cmd_retry(scmd);
631         list_move_tail(&scmd->eh_entry, done_q);
632 }
633
634 /**
635  * scsi_eh_get_sense - Get device sense data.
636  * @work_q:     Queue of commands to process.
637  * @done_q:     Queue of proccessed commands..
638  *
639  * Description:
640  *    See if we need to request sense information.  if so, then get it
641  *    now, so we have a better idea of what to do.  
642  *
643  * Notes:
644  *    This has the unfortunate side effect that if a shost adapter does
645  *    not automatically request sense information, that we end up shutting
646  *    it down before we request it.  All shosts should be doing this
647  *    anyways, so for now all I have to say is tough noogies if you end up
648  *    in here.  On second thought, this is probably a good idea.  We
649  *    *really* want to give authors an incentive to automatically request
650  *    this.
651  *
652  *    In 2.5 this capability will be going away.
653  *
654  *    Really?  --hch
655  **/
656 static int scsi_eh_get_sense(struct list_head *work_q,
657                              struct list_head *done_q)
658 {
659         struct list_head *lh, *lh_sf;
660         struct scsi_cmnd *scmd;
661         int rtn;
662
663         list_for_each_safe(lh, lh_sf, work_q) {
664                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
665                 if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD) ||
666                     SCSI_SENSE_VALID(scmd))
667                         continue;
668
669                 SCSI_LOG_ERROR_RECOVERY(2, printk("%s: requesting sense"
670                                                   " for id: %d\n",
671                                                   current->comm,
672                                                   scmd->device->id));
673                 rtn = scsi_request_sense(scmd);
674                 if (rtn != SUCCESS)
675                         continue;
676
677                 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
678                                                   " result %x\n", scmd,
679                                                   scmd->result));
680                 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
681
682                 rtn = scsi_decide_disposition(scmd);
683
684                 /*
685                  * if the result was normal, then just pass it along to the
686                  * upper level.
687                  */
688                 if (rtn == SUCCESS)
689                         /* we don't want this command reissued, just
690                          * finished with the sense data, so set
691                          * retries to the max allowed to ensure it
692                          * won't get reissued */
693                         scmd->retries = scmd->allowed;
694                 else if (rtn != NEEDS_RETRY)
695                         continue;
696
697                 scsi_eh_finish_cmd(scmd, done_q);
698         }
699
700         return list_empty(work_q);
701 }
702
703 /**
704  * scsi_try_to_abort_cmd - Ask host to abort a running command.
705  * @scmd:       SCSI cmd to abort from Lower Level.
706  *
707  * Notes:
708  *    This function will not return until the user's completion function
709  *    has been called.  there is no timeout on this operation.  if the
710  *    author of the low-level driver wishes this operation to be timed,
711  *    they can provide this facility themselves.  helper functions in
712  *    scsi_error.c can be supplied to make this easier to do.
713  **/
714 static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
715 {
716         unsigned long flags;
717         int rtn = FAILED;
718
719         if (!scmd->device->host->hostt->eh_abort_handler)
720                 return rtn;
721
722         /*
723          * scsi_done was called just after the command timed out and before
724          * we had a chance to process it. (db)
725          */
726         if (scmd->serial_number == 0)
727                 return SUCCESS;
728
729         scmd->owner = SCSI_OWNER_LOWLEVEL;
730
731         spin_lock_irqsave(scmd->device->host->host_lock, flags);
732         rtn = scmd->device->host->hostt->eh_abort_handler(scmd);
733         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
734
735         return rtn;
736 }
737
738 /**
739  * scsi_eh_tur - Send TUR to device.
740  * @scmd:       Scsi cmd to send TUR
741  *
742  * Return value:
743  *    0 - Device is ready. 1 - Device NOT ready.
744  **/
745 static int scsi_eh_tur(struct scsi_cmnd *scmd)
746 {
747         static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
748         int retry_cnt = 1, rtn;
749
750 retry_tur:
751         memcpy(scmd->cmnd, tur_command, sizeof(tur_command));
752
753         /*
754          * zero the sense buffer.  the scsi spec mandates that any
755          * untransferred sense data should be interpreted as being zero.
756          */
757         memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
758
759         scmd->request_buffer = NULL;
760         scmd->request_bufflen = 0;
761         scmd->use_sg = 0;
762         scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
763         scmd->underflow = 0;
764         scmd->sc_data_direction = DMA_NONE;
765
766         rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
767
768         /*
769          * when we eventually call scsi_finish, we really wish to complete
770          * the original request, so let's restore the original data. (db)
771          */
772         scsi_setup_cmd_retry(scmd);
773
774         /*
775          * hey, we are done.  let's look to see what happened.
776          */
777         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
778                 __FUNCTION__, scmd, rtn));
779         if (rtn == SUCCESS)
780                 return 0;
781         else if (rtn == NEEDS_RETRY)
782                 if (retry_cnt--)
783                         goto retry_tur;
784         return 1;
785 }
786
787 /**
788  * scsi_eh_abort_cmds - abort canceled commands.
789  * @shost:      scsi host being recovered.
790  * @eh_done_q:  list_head for processed commands.
791  *
792  * Decription:
793  *    Try and see whether or not it makes sense to try and abort the
794  *    running command.  this only works out to be the case if we have one
795  *    command that has timed out.  if the command simply failed, it makes
796  *    no sense to try and abort the command, since as far as the shost
797  *    adapter is concerned, it isn't running.
798  **/
799 static int scsi_eh_abort_cmds(struct list_head *work_q,
800                               struct list_head *done_q)
801 {
802         struct list_head *lh, *lh_sf;
803         struct scsi_cmnd *scmd;
804         int rtn;
805
806         list_for_each_safe(lh, lh_sf, work_q) {
807                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
808                 if (!scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD))
809                         continue;
810                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
811                                                   "0x%p\n", current->comm,
812                                                   scmd));
813                 rtn = scsi_try_to_abort_cmd(scmd);
814                 if (rtn == SUCCESS) {
815                         scsi_eh_eflags_clr(scmd,  SCSI_EH_CANCEL_CMD);
816                         if (!scsi_device_online(scmd->device) ||
817                             !scsi_eh_tur(scmd)) {
818                                 scsi_eh_finish_cmd(scmd, done_q);
819                         }
820                                 
821                 } else
822                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
823                                                           " cmd failed:"
824                                                           "0x%p\n",
825                                                           current->comm,
826                                                           scmd));
827         }
828
829         return list_empty(work_q);
830 }
831
832 /**
833  * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
834  * @scmd:       SCSI cmd used to send BDR       
835  *
836  * Notes:
837  *    There is no timeout for this operation.  if this operation is
838  *    unreliable for a given host, then the host itself needs to put a
839  *    timer on it, and set the host back to a consistent state prior to
840  *    returning.
841  **/
842 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
843 {
844         unsigned long flags;
845         int rtn = FAILED;
846
847         if (!scmd->device->host->hostt->eh_device_reset_handler)
848                 return rtn;
849
850         scmd->owner = SCSI_OWNER_LOWLEVEL;
851
852         spin_lock_irqsave(scmd->device->host->host_lock, flags);
853         rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
854         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
855
856         if (rtn == SUCCESS) {
857                 scmd->device->was_reset = 1;
858                 scmd->device->expecting_cc_ua = 1;
859         }
860
861         return rtn;
862 }
863
864 /**
865  * scsi_eh_try_stu - Send START_UNIT to device.
866  * @scmd:       Scsi cmd to send START_UNIT
867  *
868  * Return value:
869  *    0 - Device is ready. 1 - Device NOT ready.
870  **/
871 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
872 {
873         static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
874         int rtn;
875
876         if (!scmd->device->allow_restart)
877                 return 1;
878
879         memcpy(scmd->cmnd, stu_command, sizeof(stu_command));
880
881         /*
882          * zero the sense buffer.  the scsi spec mandates that any
883          * untransferred sense data should be interpreted as being zero.
884          */
885         memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
886
887         scmd->request_buffer = NULL;
888         scmd->request_bufflen = 0;
889         scmd->use_sg = 0;
890         scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
891         scmd->underflow = 0;
892         scmd->sc_data_direction = DMA_NONE;
893
894         rtn = scsi_send_eh_cmnd(scmd, START_UNIT_TIMEOUT);
895
896         /*
897          * when we eventually call scsi_finish, we really wish to complete
898          * the original request, so let's restore the original data. (db)
899          */
900         scsi_setup_cmd_retry(scmd);
901
902         /*
903          * hey, we are done.  let's look to see what happened.
904          */
905         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
906                 __FUNCTION__, scmd, rtn));
907         if (rtn == SUCCESS)
908                 return 0;
909         return 1;
910 }
911
912  /**
913  * scsi_eh_stu - send START_UNIT if needed
914  * @shost:      scsi host being recovered.
915  * @eh_done_q:  list_head for processed commands.
916  *
917  * Notes:
918  *    If commands are failing due to not ready, initializing command required,
919  *      try revalidating the device, which will end up sending a start unit. 
920  **/
921 static int scsi_eh_stu(struct Scsi_Host *shost,
922                               struct list_head *work_q,
923                               struct list_head *done_q)
924 {
925         struct list_head *lh, *lh_sf;
926         struct scsi_cmnd *scmd, *stu_scmd;
927         struct scsi_device *sdev;
928
929         shost_for_each_device(sdev, shost) {
930                 stu_scmd = NULL;
931                 list_for_each_entry(scmd, work_q, eh_entry)
932                         if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
933                             scsi_check_sense(scmd) == FAILED ) {
934                                 stu_scmd = scmd;
935                                 break;
936                         }
937
938                 if (!stu_scmd)
939                         continue;
940
941                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
942                                                   " 0x%p\n", current->comm, sdev));
943
944                 if (!scsi_eh_try_stu(stu_scmd)) {
945                         if (!scsi_device_online(sdev) ||
946                             !scsi_eh_tur(stu_scmd)) {
947                                 list_for_each_safe(lh, lh_sf, work_q) {
948                                         scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
949                                         if (scmd->device == sdev)
950                                                 scsi_eh_finish_cmd(scmd, done_q);
951                                 }
952                         }
953                 } else {
954                         SCSI_LOG_ERROR_RECOVERY(3,
955                                                 printk("%s: START_UNIT failed to sdev:"
956                                                        " 0x%p\n", current->comm, sdev));
957                 }
958         }
959
960         return list_empty(work_q);
961 }
962
963
964 /**
965  * scsi_eh_bus_device_reset - send bdr if needed
966  * @shost:      scsi host being recovered.
967  * @eh_done_q:  list_head for processed commands.
968  *
969  * Notes:
970  *    Try a bus device reset.  still, look to see whether we have multiple
971  *    devices that are jammed or not - if we have multiple devices, it
972  *    makes no sense to try bus_device_reset - we really would need to try
973  *    a bus_reset instead. 
974  **/
975 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
976                                     struct list_head *work_q,
977                                     struct list_head *done_q)
978 {
979         struct list_head *lh, *lh_sf;
980         struct scsi_cmnd *scmd, *bdr_scmd;
981         struct scsi_device *sdev;
982         int rtn;
983
984         shost_for_each_device(sdev, shost) {
985                 bdr_scmd = NULL;
986                 list_for_each_entry(scmd, work_q, eh_entry)
987                         if (scmd->device == sdev) {
988                                 bdr_scmd = scmd;
989                                 break;
990                         }
991
992                 if (!bdr_scmd)
993                         continue;
994
995                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
996                                                   " 0x%p\n", current->comm,
997                                                   sdev));
998                 rtn = scsi_try_bus_device_reset(bdr_scmd);
999                 if (rtn == SUCCESS) {
1000                         if (!scsi_device_online(sdev) ||
1001                             !scsi_eh_tur(bdr_scmd)) {
1002                                 list_for_each_safe(lh, lh_sf,
1003                                                    work_q) {
1004                                         scmd = list_entry(lh, struct
1005                                                           scsi_cmnd,
1006                                                           eh_entry);
1007                                         if (scmd->device == sdev)
1008                                                 scsi_eh_finish_cmd(scmd,
1009                                                                    done_q);
1010                                 }
1011                         }
1012                 } else {
1013                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1014                                                           " failed sdev:"
1015                                                           "0x%p\n",
1016                                                           current->comm,
1017                                                            sdev));
1018                 }
1019         }
1020
1021         return list_empty(work_q);
1022 }
1023
1024 /**
1025  * scsi_try_bus_reset - ask host to perform a bus reset
1026  * @scmd:       SCSI cmd to send bus reset.
1027  **/
1028 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
1029 {
1030         unsigned long flags;
1031         int rtn;
1032
1033         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
1034                                           __FUNCTION__));
1035         scmd->owner = SCSI_OWNER_LOWLEVEL;
1036         scmd->serial_number_at_timeout = scmd->serial_number;
1037
1038         if (!scmd->device->host->hostt->eh_bus_reset_handler)
1039                 return FAILED;
1040
1041         spin_lock_irqsave(scmd->device->host->host_lock, flags);
1042         rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
1043         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1044
1045         if (rtn == SUCCESS) {
1046                 if (!scmd->device->host->hostt->skip_settle_delay)
1047                         scsi_sleep(BUS_RESET_SETTLE_TIME);
1048                 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1049                 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1050                 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1051         }
1052
1053         return rtn;
1054 }
1055
1056 /**
1057  * scsi_try_host_reset - ask host adapter to reset itself
1058  * @scmd:       SCSI cmd to send hsot reset.
1059  **/
1060 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
1061 {
1062         unsigned long flags;
1063         int rtn;
1064
1065         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
1066                                           __FUNCTION__));
1067         scmd->owner = SCSI_OWNER_LOWLEVEL;
1068         scmd->serial_number_at_timeout = scmd->serial_number;
1069
1070         if (!scmd->device->host->hostt->eh_host_reset_handler)
1071                 return FAILED;
1072
1073         spin_lock_irqsave(scmd->device->host->host_lock, flags);
1074         rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
1075         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1076
1077         if (rtn == SUCCESS) {
1078                 if (!scmd->device->host->hostt->skip_settle_delay)
1079                         scsi_sleep(HOST_RESET_SETTLE_TIME);
1080                 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1081                 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1082                 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1083         }
1084
1085         return rtn;
1086 }
1087
1088 /**
1089  * scsi_eh_bus_reset - send a bus reset 
1090  * @shost:      scsi host being recovered.
1091  * @eh_done_q:  list_head for processed commands.
1092  **/
1093 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1094                              struct list_head *work_q,
1095                              struct list_head *done_q)
1096 {
1097         struct list_head *lh, *lh_sf;
1098         struct scsi_cmnd *scmd;
1099         struct scsi_cmnd *chan_scmd;
1100         unsigned int channel;
1101         int rtn;
1102
1103         /*
1104          * we really want to loop over the various channels, and do this on
1105          * a channel by channel basis.  we should also check to see if any
1106          * of the failed commands are on soft_reset devices, and if so, skip
1107          * the reset.  
1108          */
1109
1110         for (channel = 0; channel <= shost->max_channel; channel++) {
1111                 chan_scmd = NULL;
1112                 list_for_each_entry(scmd, work_q, eh_entry) {
1113                         if (channel == scmd->device->channel) {
1114                                 chan_scmd = scmd;
1115                                 break;
1116                                 /*
1117                                  * FIXME add back in some support for
1118                                  * soft_reset devices.
1119                                  */
1120                         }
1121                 }
1122
1123                 if (!chan_scmd)
1124                         continue;
1125                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1126                                                   " %d\n", current->comm,
1127                                                   channel));
1128                 rtn = scsi_try_bus_reset(chan_scmd);
1129                 if (rtn == SUCCESS) {
1130                         list_for_each_safe(lh, lh_sf, work_q) {
1131                                 scmd = list_entry(lh, struct scsi_cmnd,
1132                                                   eh_entry);
1133                                 if (channel == scmd->device->channel)
1134                                         if (!scsi_device_online(scmd->device) ||
1135                                             !scsi_eh_tur(scmd))
1136                                                 scsi_eh_finish_cmd(scmd,
1137                                                                    done_q);
1138                         }
1139                 } else {
1140                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1141                                                           " failed chan: %d\n",
1142                                                           current->comm,
1143                                                           channel));
1144                 }
1145         }
1146         return list_empty(work_q);
1147 }
1148
1149 /**
1150  * scsi_eh_host_reset - send a host reset 
1151  * @work_q:     list_head for processed commands.
1152  * @done_q:     list_head for processed commands.
1153  **/
1154 static int scsi_eh_host_reset(struct list_head *work_q,
1155                               struct list_head *done_q)
1156 {
1157         int rtn;
1158         struct list_head *lh, *lh_sf;
1159         struct scsi_cmnd *scmd;
1160
1161         if (!list_empty(work_q)) {
1162                 scmd = list_entry(work_q->next,
1163                                   struct scsi_cmnd, eh_entry);
1164
1165                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1166                                                   , current->comm));
1167
1168                 rtn = scsi_try_host_reset(scmd);
1169                 if (rtn == SUCCESS) {
1170                         list_for_each_safe(lh, lh_sf, work_q) {
1171                                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1172                                 if (!scsi_device_online(scmd->device) ||
1173                                     (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
1174                                     !scsi_eh_tur(scmd))
1175                                         scsi_eh_finish_cmd(scmd, done_q);
1176                         }
1177                 } else {
1178                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1179                                                           " failed\n",
1180                                                           current->comm));
1181                 }
1182         }
1183         return list_empty(work_q);
1184 }
1185
1186 /**
1187  * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1188  * @work_q:     list_head for processed commands.
1189  * @done_q:     list_head for processed commands.
1190  *
1191  **/
1192 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1193                                   struct list_head *done_q)
1194 {
1195         struct list_head *lh, *lh_sf;
1196         struct scsi_cmnd *scmd;
1197
1198         list_for_each_safe(lh, lh_sf, work_q) {
1199                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1200                 printk(KERN_INFO "scsi: Device offlined - not"
1201                                 " ready after error recovery: host"
1202                                 " %d channel %d id %d lun %d\n",
1203                                 scmd->device->host->host_no,
1204                                 scmd->device->channel,
1205                                 scmd->device->id,
1206                                 scmd->device->lun);
1207                 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1208                 if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD)) {
1209                         /*
1210                          * FIXME: Handle lost cmds.
1211                          */
1212                 }
1213                 scsi_eh_finish_cmd(scmd, done_q);
1214         }
1215         return;
1216 }
1217
1218 /**
1219  * scsi_sleep_done - timer function for scsi_sleep
1220  * @sem:        semphore to signal
1221  *
1222  **/
1223 static void scsi_sleep_done(unsigned long data)
1224 {
1225         struct semaphore *sem = (struct semaphore *)data;
1226
1227         if (sem)
1228                 up(sem);
1229 }
1230
1231 /**
1232  * scsi_sleep - sleep for specified timeout
1233  * @timeout:    timeout value
1234  *
1235  **/
1236 void scsi_sleep(int timeout)
1237 {
1238         DECLARE_MUTEX_LOCKED(sem);
1239         struct timer_list timer;
1240
1241         init_timer(&timer);
1242         timer.data = (unsigned long)&sem;
1243         timer.expires = jiffies + timeout;
1244         timer.function = (void (*)(unsigned long))scsi_sleep_done;
1245
1246         SCSI_LOG_ERROR_RECOVERY(5, printk("sleeping for timer tics %d\n",
1247                                           timeout));
1248
1249         add_timer(&timer);
1250
1251         down(&sem);
1252         del_timer(&timer);
1253 }
1254
1255 /**
1256  * scsi_decide_disposition - Disposition a cmd on return from LLD.
1257  * @scmd:       SCSI cmd to examine.
1258  *
1259  * Notes:
1260  *    This is *only* called when we are examining the status after sending
1261  *    out the actual data command.  any commands that are queued for error
1262  *    recovery (e.g. test_unit_ready) do *not* come through here.
1263  *
1264  *    When this routine returns failed, it means the error handler thread
1265  *    is woken.  In cases where the error code indicates an error that
1266  *    doesn't require the error handler read (i.e. we don't need to
1267  *    abort/reset), this function should return SUCCESS.
1268  **/
1269 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1270 {
1271         int rtn;
1272
1273         /*
1274          * if the device is offline, then we clearly just pass the result back
1275          * up to the top level.
1276          */
1277         if (!scsi_device_online(scmd->device)) {
1278                 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1279                                                   " as SUCCESS\n",
1280                                                   __FUNCTION__));
1281                 return SUCCESS;
1282         }
1283
1284         /*
1285          * first check the host byte, to see if there is anything in there
1286          * that would indicate what we need to do.
1287          */
1288         switch (host_byte(scmd->result)) {
1289         case DID_PASSTHROUGH:
1290                 /*
1291                  * no matter what, pass this through to the upper layer.
1292                  * nuke this special code so that it looks like we are saying
1293                  * did_ok.
1294                  */
1295                 scmd->result &= 0xff00ffff;
1296                 return SUCCESS;
1297         case DID_OK:
1298                 /*
1299                  * looks good.  drop through, and check the next byte.
1300                  */
1301                 break;
1302         case DID_NO_CONNECT:
1303         case DID_BAD_TARGET:
1304         case DID_ABORT:
1305                 /*
1306                  * note - this means that we just report the status back
1307                  * to the top level driver, not that we actually think
1308                  * that it indicates SUCCESS.
1309                  */
1310                 return SUCCESS;
1311                 /*
1312                  * when the low level driver returns did_soft_error,
1313                  * it is responsible for keeping an internal retry counter 
1314                  * in order to avoid endless loops (db)
1315                  *
1316                  * actually this is a bug in this function here.  we should
1317                  * be mindful of the maximum number of retries specified
1318                  * and not get stuck in a loop.
1319                  */
1320         case DID_SOFT_ERROR:
1321                 goto maybe_retry;
1322         case DID_IMM_RETRY:
1323                 return NEEDS_RETRY;
1324
1325         case DID_ERROR:
1326                 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1327                     status_byte(scmd->result) == RESERVATION_CONFLICT)
1328                         /*
1329                          * execute reservation conflict processing code
1330                          * lower down
1331                          */
1332                         break;
1333                 /* fallthrough */
1334
1335         case DID_BUS_BUSY:
1336         case DID_PARITY:
1337                 goto maybe_retry;
1338         case DID_TIME_OUT:
1339                 /*
1340                  * when we scan the bus, we get timeout messages for
1341                  * these commands if there is no device available.
1342                  * other hosts report did_no_connect for the same thing.
1343                  */
1344                 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1345                      scmd->cmnd[0] == INQUIRY)) {
1346                         return SUCCESS;
1347                 } else {
1348                         return FAILED;
1349                 }
1350         case DID_RESET:
1351                 return SUCCESS;
1352         default:
1353                 return FAILED;
1354         }
1355
1356         /*
1357          * next, check the message byte.
1358          */
1359         if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1360                 return FAILED;
1361
1362         /*
1363          * check the status byte to see if this indicates anything special.
1364          */
1365         switch (status_byte(scmd->result)) {
1366         case QUEUE_FULL:
1367                 /*
1368                  * the case of trying to send too many commands to a
1369                  * tagged queueing device.
1370                  */
1371         case BUSY:
1372                 /*
1373                  * device can't talk to us at the moment.  Should only
1374                  * occur (SAM-3) when the task queue is empty, so will cause
1375                  * the empty queue handling to trigger a stall in the
1376                  * device.
1377                  */
1378                 return ADD_TO_MLQUEUE;
1379         case GOOD:
1380         case COMMAND_TERMINATED:
1381                 return SUCCESS;
1382         case CHECK_CONDITION:
1383                 rtn = scsi_check_sense(scmd);
1384                 if (rtn == NEEDS_RETRY)
1385                         goto maybe_retry;
1386                 /* if rtn == FAILED, we have no sense information;
1387                  * returning FAILED will wake the error handler thread
1388                  * to collect the sense and redo the decide
1389                  * disposition */
1390                 return rtn;
1391         case CONDITION_GOOD:
1392         case INTERMEDIATE_GOOD:
1393         case INTERMEDIATE_C_GOOD:
1394                 /*
1395                  * who knows?  FIXME(eric)
1396                  */
1397                 return SUCCESS;
1398
1399         case RESERVATION_CONFLICT:
1400                 printk("scsi%d (%d,%d,%d) : reservation conflict\n",
1401                        scmd->device->host->host_no, scmd->device->channel,
1402                        scmd->device->id, scmd->device->lun);
1403                 return SUCCESS; /* causes immediate i/o error */
1404         default:
1405                 return FAILED;
1406         }
1407         return FAILED;
1408
1409       maybe_retry:
1410
1411         /* we requeue for retry because the error was retryable, and
1412          * the request was not marked fast fail.  Note that above,
1413          * even if the request is marked fast fail, we still requeue
1414          * for queue congestion conditions (QUEUE_FULL or BUSY) */
1415         if ((++scmd->retries) < scmd->allowed 
1416             && !blk_noretry_request(scmd->request)) {
1417                 return NEEDS_RETRY;
1418         } else {
1419                 /*
1420                  * no more retries - report this one back to upper level.
1421                  */
1422                 return SUCCESS;
1423         }
1424 }
1425
1426 /**
1427  * scsi_eh_lock_done - done function for eh door lock request
1428  * @scmd:       SCSI command block for the door lock request
1429  *
1430  * Notes:
1431  *      We completed the asynchronous door lock request, and it has either
1432  *      locked the door or failed.  We must free the command structures
1433  *      associated with this request.
1434  **/
1435 static void scsi_eh_lock_done(struct scsi_cmnd *scmd)
1436 {
1437         struct scsi_request *sreq = scmd->sc_request;
1438
1439         scsi_release_request(sreq);
1440 }
1441
1442
1443 /**
1444  * scsi_eh_lock_door - Prevent medium removal for the specified device
1445  * @sdev:       SCSI device to prevent medium removal
1446  *
1447  * Locking:
1448  *      We must be called from process context; scsi_allocate_request()
1449  *      may sleep.
1450  *
1451  * Notes:
1452  *      We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1453  *      head of the devices request queue, and continue.
1454  *
1455  * Bugs:
1456  *      scsi_allocate_request() may sleep waiting for existing requests to
1457  *      be processed.  However, since we haven't kicked off any request
1458  *      processing for this host, this may deadlock.
1459  *
1460  *      If scsi_allocate_request() fails for what ever reason, we
1461  *      completely forget to lock the door.
1462  **/
1463 static void scsi_eh_lock_door(struct scsi_device *sdev)
1464 {
1465         struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1466
1467         if (unlikely(!sreq)) {
1468                 printk(KERN_ERR "%s: request allocate failed,"
1469                        "prevent media removal cmd not sent\n", __FUNCTION__);
1470                 return;
1471         }
1472
1473         sreq->sr_cmnd[0] = ALLOW_MEDIUM_REMOVAL;
1474         sreq->sr_cmnd[1] = 0;
1475         sreq->sr_cmnd[2] = 0;
1476         sreq->sr_cmnd[3] = 0;
1477         sreq->sr_cmnd[4] = SCSI_REMOVAL_PREVENT;
1478         sreq->sr_cmnd[5] = 0;
1479         sreq->sr_data_direction = DMA_NONE;
1480         sreq->sr_bufflen = 0;
1481         sreq->sr_buffer = NULL;
1482         sreq->sr_allowed = 5;
1483         sreq->sr_done = scsi_eh_lock_done;
1484         sreq->sr_timeout_per_command = 10 * HZ;
1485         sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
1486
1487         scsi_insert_special_req(sreq, 1);
1488 }
1489
1490
1491 /**
1492  * scsi_restart_operations - restart io operations to the specified host.
1493  * @shost:      Host we are restarting.
1494  *
1495  * Notes:
1496  *    When we entered the error handler, we blocked all further i/o to
1497  *    this device.  we need to 'reverse' this process.
1498  **/
1499 static void scsi_restart_operations(struct Scsi_Host *shost)
1500 {
1501         struct scsi_device *sdev;
1502
1503         /*
1504          * If the door was locked, we need to insert a door lock request
1505          * onto the head of the SCSI request queue for the device.  There
1506          * is no point trying to lock the door of an off-line device.
1507          */
1508         shost_for_each_device(sdev, shost) {
1509                 if (scsi_device_online(sdev) && sdev->locked)
1510                         scsi_eh_lock_door(sdev);
1511         }
1512
1513         /*
1514          * next free up anything directly waiting upon the host.  this
1515          * will be requests for character device operations, and also for
1516          * ioctls to queued block devices.
1517          */
1518         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1519                                           __FUNCTION__));
1520
1521         clear_bit(SHOST_RECOVERY, &shost->shost_state);
1522
1523         wake_up(&shost->host_wait);
1524
1525         /*
1526          * finally we need to re-initiate requests that may be pending.  we will
1527          * have had everything blocked while error handling is taking place, and
1528          * now that error recovery is done, we will need to ensure that these
1529          * requests are started.
1530          */
1531         scsi_run_host_queues(shost);
1532 }
1533
1534 /**
1535  * scsi_eh_ready_devs - check device ready state and recover if not.
1536  * @shost:      host to be recovered.
1537  * @eh_done_q:  list_head for processed commands.
1538  *
1539  **/
1540 static void scsi_eh_ready_devs(struct Scsi_Host *shost,
1541                                struct list_head *work_q,
1542                                struct list_head *done_q)
1543 {
1544         if (!scsi_eh_stu(shost, work_q, done_q))
1545                 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1546                         if (!scsi_eh_bus_reset(shost, work_q, done_q))
1547                                 if (!scsi_eh_host_reset(work_q, done_q))
1548                                         scsi_eh_offline_sdevs(work_q, done_q);
1549 }
1550
1551 /**
1552  * scsi_eh_flush_done_q - finish processed commands or retry them.
1553  * @done_q:     list_head of processed commands.
1554  *
1555  **/
1556 static void scsi_eh_flush_done_q(struct list_head *done_q)
1557 {
1558         struct list_head *lh, *lh_sf;
1559         struct scsi_cmnd *scmd;
1560
1561         list_for_each_safe(lh, lh_sf, done_q) {
1562                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1563                 list_del_init(lh);
1564                 if (scsi_device_online(scmd->device) &&
1565                     !blk_noretry_request(scmd->request) &&
1566                     (++scmd->retries < scmd->allowed)) {
1567                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1568                                                           " retry cmd: %p\n",
1569                                                           current->comm,
1570                                                           scmd));
1571                                 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
1572                 } else {
1573                         if (!scmd->result)
1574                                 scmd->result |= (DRIVER_TIMEOUT << 24);
1575                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1576                                                         " cmd: %p\n",
1577                                                         current->comm, scmd));
1578                         scsi_finish_command(scmd);
1579                 }
1580         }
1581 }
1582
1583 /**
1584  * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1585  * @shost:      Host to unjam.
1586  *
1587  * Notes:
1588  *    When we come in here, we *know* that all commands on the bus have
1589  *    either completed, failed or timed out.  we also know that no further
1590  *    commands are being sent to the host, so things are relatively quiet
1591  *    and we have freedom to fiddle with things as we wish.
1592  *
1593  *    This is only the *default* implementation.  it is possible for
1594  *    individual drivers to supply their own version of this function, and
1595  *    if the maintainer wishes to do this, it is strongly suggested that
1596  *    this function be taken as a template and modified.  this function
1597  *    was designed to correctly handle problems for about 95% of the
1598  *    different cases out there, and it should always provide at least a
1599  *    reasonable amount of error recovery.
1600  *
1601  *    Any command marked 'failed' or 'timeout' must eventually have
1602  *    scsi_finish_cmd() called for it.  we do all of the retry stuff
1603  *    here, so when we restart the host after we return it should have an
1604  *    empty queue.
1605  **/
1606 static void scsi_unjam_host(struct Scsi_Host *shost)
1607 {
1608         unsigned long flags;
1609         LIST_HEAD(eh_work_q);
1610         LIST_HEAD(eh_done_q);
1611
1612         spin_lock_irqsave(shost->host_lock, flags);
1613         list_splice_init(&shost->eh_cmd_q, &eh_work_q);
1614         spin_unlock_irqrestore(shost->host_lock, flags);
1615
1616         SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
1617
1618         if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
1619                 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1620                         scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1621
1622         scsi_eh_flush_done_q(&eh_done_q);
1623 }
1624
1625 /**
1626  * scsi_error_handler - Handle errors/timeouts of SCSI cmds.
1627  * @data:       Host for which we are running.
1628  *
1629  * Notes:
1630  *    This is always run in the context of a kernel thread.  The idea is
1631  *    that we start this thing up when the kernel starts up (one per host
1632  *    that we detect), and it immediately goes to sleep and waits for some
1633  *    event (i.e. failure).  When this takes place, we have the job of
1634  *    trying to unjam the bus and restarting things.
1635  **/
1636 int scsi_error_handler(void *data)
1637 {
1638         struct Scsi_Host *shost = (struct Scsi_Host *) data;
1639         int rtn;
1640         DECLARE_MUTEX_LOCKED(sem);
1641
1642         lock_kernel();
1643
1644         /*
1645          *    Flush resources
1646          */
1647
1648         daemonize("scsi_eh_%d", shost->host_no);
1649
1650         current->flags |= PF_NOFREEZE;
1651
1652         shost->eh_wait = &sem;
1653         shost->ehandler = current;
1654
1655         unlock_kernel();
1656
1657         /*
1658          * Wake up the thread that created us.
1659          */
1660         SCSI_LOG_ERROR_RECOVERY(3, printk("Wake up parent of"
1661                                           " scsi_eh_%d\n",shost->host_no));
1662
1663         complete(shost->eh_notify);
1664
1665         while (1) {
1666                 /*
1667                  * If we get a signal, it means we are supposed to go
1668                  * away and die.  This typically happens if the user is
1669                  * trying to unload a module.
1670                  */
1671                 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1672                                                   " scsi_eh_%d"
1673                                                   " sleeping\n",shost->host_no));
1674
1675                 /*
1676                  * Note - we always use down_interruptible with the semaphore
1677                  * even if the module was loaded as part of the kernel.  The
1678                  * reason is that down() will cause this thread to be counted
1679                  * in the load average as a running process, and down
1680                  * interruptible doesn't.  Given that we need to allow this
1681                  * thread to die if the driver was loaded as a module, using
1682                  * semaphores isn't unreasonable.
1683                  */
1684                 down_interruptible(&sem);
1685                 if (shost->eh_kill)
1686                         break;
1687
1688                 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1689                                                   " scsi_eh_%d waking"
1690                                                   " up\n",shost->host_no));
1691
1692                 shost->eh_active = 1;
1693
1694                 /*
1695                  * We have a host that is failing for some reason.  Figure out
1696                  * what we need to do to get it up and online again (if we can).
1697                  * If we fail, we end up taking the thing offline.
1698                  */
1699                 if (shost->hostt->eh_strategy_handler) 
1700                         rtn = shost->hostt->eh_strategy_handler(shost);
1701                 else
1702                         scsi_unjam_host(shost);
1703
1704                 shost->eh_active = 0;
1705
1706                 /*
1707                  * Note - if the above fails completely, the action is to take
1708                  * individual devices offline and flush the queue of any
1709                  * outstanding requests that may have been pending.  When we
1710                  * restart, we restart any I/O to any other devices on the bus
1711                  * which are still online.
1712                  */
1713                 scsi_restart_operations(shost);
1714
1715         }
1716
1717         SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler scsi_eh_%d"
1718                                           " exiting\n",shost->host_no));
1719
1720         /*
1721          * Make sure that nobody tries to wake us up again.
1722          */
1723         shost->eh_wait = NULL;
1724
1725         /*
1726          * Knock this down too.  From this point on, the host is flying
1727          * without a pilot.  If this is because the module is being unloaded,
1728          * that's fine.  If the user sent a signal to this thing, we are
1729          * potentially in real danger.
1730          */
1731         shost->eh_active = 0;
1732         shost->ehandler = NULL;
1733
1734         /*
1735          * If anyone is waiting for us to exit (i.e. someone trying to unload
1736          * a driver), then wake up that process to let them know we are on
1737          * the way out the door.
1738          */
1739         complete_and_exit(shost->eh_notify, 0);
1740         return 0;
1741 }
1742
1743 /*
1744  * Function:    scsi_report_bus_reset()
1745  *
1746  * Purpose:     Utility function used by low-level drivers to report that
1747  *              they have observed a bus reset on the bus being handled.
1748  *
1749  * Arguments:   shost       - Host in question
1750  *              channel     - channel on which reset was observed.
1751  *
1752  * Returns:     Nothing
1753  *
1754  * Lock status: Host lock must be held.
1755  *
1756  * Notes:       This only needs to be called if the reset is one which
1757  *              originates from an unknown location.  Resets originated
1758  *              by the mid-level itself don't need to call this, but there
1759  *              should be no harm.
1760  *
1761  *              The main purpose of this is to make sure that a CHECK_CONDITION
1762  *              is properly treated.
1763  */
1764 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
1765 {
1766         struct scsi_device *sdev;
1767
1768         __shost_for_each_device(sdev, shost) {
1769                 if (channel == sdev->channel) {
1770                         sdev->was_reset = 1;
1771                         sdev->expecting_cc_ua = 1;
1772                 }
1773         }
1774 }
1775
1776 /*
1777  * Function:    scsi_report_device_reset()
1778  *
1779  * Purpose:     Utility function used by low-level drivers to report that
1780  *              they have observed a device reset on the device being handled.
1781  *
1782  * Arguments:   shost       - Host in question
1783  *              channel     - channel on which reset was observed
1784  *              target      - target on which reset was observed
1785  *
1786  * Returns:     Nothing
1787  *
1788  * Lock status: Host lock must be held
1789  *
1790  * Notes:       This only needs to be called if the reset is one which
1791  *              originates from an unknown location.  Resets originated
1792  *              by the mid-level itself don't need to call this, but there
1793  *              should be no harm.
1794  *
1795  *              The main purpose of this is to make sure that a CHECK_CONDITION
1796  *              is properly treated.
1797  */
1798 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
1799 {
1800         struct scsi_device *sdev;
1801
1802         __shost_for_each_device(sdev, shost) {
1803                 if (channel == sdev->channel &&
1804                     target == sdev->id) {
1805                         sdev->was_reset = 1;
1806                         sdev->expecting_cc_ua = 1;
1807                 }
1808         }
1809 }
1810
1811 static void
1812 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
1813 {
1814 }
1815
1816 /*
1817  * Function:    scsi_reset_provider
1818  *
1819  * Purpose:     Send requested reset to a bus or device at any phase.
1820  *
1821  * Arguments:   device  - device to send reset to
1822  *              flag - reset type (see scsi.h)
1823  *
1824  * Returns:     SUCCESS/FAILURE.
1825  *
1826  * Notes:       This is used by the SCSI Generic driver to provide
1827  *              Bus/Device reset capability.
1828  */
1829 int
1830 scsi_reset_provider(struct scsi_device *dev, int flag)
1831 {
1832         struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
1833         struct request req;
1834         int rtn;
1835
1836         scmd->request = &req;
1837         memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
1838         scmd->request->rq_status        = RQ_SCSI_BUSY;
1839         scmd->state                     = SCSI_STATE_INITIALIZING;
1840         scmd->owner                     = SCSI_OWNER_MIDLEVEL;
1841     
1842         memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
1843     
1844         scmd->scsi_done         = scsi_reset_provider_done_command;
1845         scmd->done                      = NULL;
1846         scmd->buffer                    = NULL;
1847         scmd->bufflen                   = 0;
1848         scmd->request_buffer            = NULL;
1849         scmd->request_bufflen           = 0;
1850         scmd->internal_timeout          = NORMAL_TIMEOUT;
1851         scmd->abort_reason              = DID_ABORT;
1852
1853         scmd->cmd_len                   = 0;
1854
1855         scmd->sc_data_direction         = DMA_BIDIRECTIONAL;
1856         scmd->sc_request                = NULL;
1857         scmd->sc_magic                  = SCSI_CMND_MAGIC;
1858
1859         init_timer(&scmd->eh_timeout);
1860
1861         /*
1862          * Sometimes the command can get back into the timer chain,
1863          * so use the pid as an identifier.
1864          */
1865         scmd->pid                       = 0;
1866
1867         switch (flag) {
1868         case SCSI_TRY_RESET_DEVICE:
1869                 rtn = scsi_try_bus_device_reset(scmd);
1870                 if (rtn == SUCCESS)
1871                         break;
1872                 /* FALLTHROUGH */
1873         case SCSI_TRY_RESET_BUS:
1874                 rtn = scsi_try_bus_reset(scmd);
1875                 if (rtn == SUCCESS)
1876                         break;
1877                 /* FALLTHROUGH */
1878         case SCSI_TRY_RESET_HOST:
1879                 rtn = scsi_try_host_reset(scmd);
1880                 break;
1881         default:
1882                 rtn = FAILED;
1883         }
1884
1885         scsi_delete_timer(scmd);
1886         scsi_next_command(scmd);
1887         return rtn;
1888 }