vserver 1.9.3
[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/delay.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)
46 #define HOST_RESET_SETTLE_TIME  (10)
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.
647  *
648  *    All drivers should request sense information internally these days,
649  *    so for now all I have to say is tough noogies if you end up in here.
650  *
651  *    XXX: Long term this code should go away, but that needs an audit of
652  *         all LLDDs first.
653  **/
654 static int scsi_eh_get_sense(struct list_head *work_q,
655                              struct list_head *done_q)
656 {
657         struct list_head *lh, *lh_sf;
658         struct scsi_cmnd *scmd;
659         int rtn;
660
661         list_for_each_safe(lh, lh_sf, work_q) {
662                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
663                 if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD) ||
664                     SCSI_SENSE_VALID(scmd))
665                         continue;
666
667                 SCSI_LOG_ERROR_RECOVERY(2, printk("%s: requesting sense"
668                                                   " for id: %d\n",
669                                                   current->comm,
670                                                   scmd->device->id));
671                 rtn = scsi_request_sense(scmd);
672                 if (rtn != SUCCESS)
673                         continue;
674
675                 SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
676                                                   " result %x\n", scmd,
677                                                   scmd->result));
678                 SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
679
680                 rtn = scsi_decide_disposition(scmd);
681
682                 /*
683                  * if the result was normal, then just pass it along to the
684                  * upper level.
685                  */
686                 if (rtn == SUCCESS)
687                         /* we don't want this command reissued, just
688                          * finished with the sense data, so set
689                          * retries to the max allowed to ensure it
690                          * won't get reissued */
691                         scmd->retries = scmd->allowed;
692                 else if (rtn != NEEDS_RETRY)
693                         continue;
694
695                 scsi_eh_finish_cmd(scmd, done_q);
696         }
697
698         return list_empty(work_q);
699 }
700
701 /**
702  * scsi_try_to_abort_cmd - Ask host to abort a running command.
703  * @scmd:       SCSI cmd to abort from Lower Level.
704  *
705  * Notes:
706  *    This function will not return until the user's completion function
707  *    has been called.  there is no timeout on this operation.  if the
708  *    author of the low-level driver wishes this operation to be timed,
709  *    they can provide this facility themselves.  helper functions in
710  *    scsi_error.c can be supplied to make this easier to do.
711  **/
712 static int scsi_try_to_abort_cmd(struct scsi_cmnd *scmd)
713 {
714         unsigned long flags;
715         int rtn = FAILED;
716
717         if (!scmd->device->host->hostt->eh_abort_handler)
718                 return rtn;
719
720         /*
721          * scsi_done was called just after the command timed out and before
722          * we had a chance to process it. (db)
723          */
724         if (scmd->serial_number == 0)
725                 return SUCCESS;
726
727         scmd->owner = SCSI_OWNER_LOWLEVEL;
728
729         spin_lock_irqsave(scmd->device->host->host_lock, flags);
730         rtn = scmd->device->host->hostt->eh_abort_handler(scmd);
731         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
732
733         return rtn;
734 }
735
736 /**
737  * scsi_eh_tur - Send TUR to device.
738  * @scmd:       Scsi cmd to send TUR
739  *
740  * Return value:
741  *    0 - Device is ready. 1 - Device NOT ready.
742  **/
743 static int scsi_eh_tur(struct scsi_cmnd *scmd)
744 {
745         static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
746         int retry_cnt = 1, rtn;
747
748 retry_tur:
749         memcpy(scmd->cmnd, tur_command, sizeof(tur_command));
750
751         /*
752          * zero the sense buffer.  the scsi spec mandates that any
753          * untransferred sense data should be interpreted as being zero.
754          */
755         memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
756
757         scmd->request_buffer = NULL;
758         scmd->request_bufflen = 0;
759         scmd->use_sg = 0;
760         scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
761         scmd->underflow = 0;
762         scmd->sc_data_direction = DMA_NONE;
763
764         rtn = scsi_send_eh_cmnd(scmd, SENSE_TIMEOUT);
765
766         /*
767          * when we eventually call scsi_finish, we really wish to complete
768          * the original request, so let's restore the original data. (db)
769          */
770         scsi_setup_cmd_retry(scmd);
771
772         /*
773          * hey, we are done.  let's look to see what happened.
774          */
775         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
776                 __FUNCTION__, scmd, rtn));
777         if (rtn == SUCCESS)
778                 return 0;
779         else if (rtn == NEEDS_RETRY)
780                 if (retry_cnt--)
781                         goto retry_tur;
782         return 1;
783 }
784
785 /**
786  * scsi_eh_abort_cmds - abort canceled commands.
787  * @shost:      scsi host being recovered.
788  * @eh_done_q:  list_head for processed commands.
789  *
790  * Decription:
791  *    Try and see whether or not it makes sense to try and abort the
792  *    running command.  this only works out to be the case if we have one
793  *    command that has timed out.  if the command simply failed, it makes
794  *    no sense to try and abort the command, since as far as the shost
795  *    adapter is concerned, it isn't running.
796  **/
797 static int scsi_eh_abort_cmds(struct list_head *work_q,
798                               struct list_head *done_q)
799 {
800         struct list_head *lh, *lh_sf;
801         struct scsi_cmnd *scmd;
802         int rtn;
803
804         list_for_each_safe(lh, lh_sf, work_q) {
805                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
806                 if (!scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD))
807                         continue;
808                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
809                                                   "0x%p\n", current->comm,
810                                                   scmd));
811                 rtn = scsi_try_to_abort_cmd(scmd);
812                 if (rtn == SUCCESS) {
813                         scsi_eh_eflags_clr(scmd,  SCSI_EH_CANCEL_CMD);
814                         if (!scsi_device_online(scmd->device) ||
815                             !scsi_eh_tur(scmd)) {
816                                 scsi_eh_finish_cmd(scmd, done_q);
817                         }
818                                 
819                 } else
820                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
821                                                           " cmd failed:"
822                                                           "0x%p\n",
823                                                           current->comm,
824                                                           scmd));
825         }
826
827         return list_empty(work_q);
828 }
829
830 /**
831  * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
832  * @scmd:       SCSI cmd used to send BDR       
833  *
834  * Notes:
835  *    There is no timeout for this operation.  if this operation is
836  *    unreliable for a given host, then the host itself needs to put a
837  *    timer on it, and set the host back to a consistent state prior to
838  *    returning.
839  **/
840 static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
841 {
842         unsigned long flags;
843         int rtn = FAILED;
844
845         if (!scmd->device->host->hostt->eh_device_reset_handler)
846                 return rtn;
847
848         scmd->owner = SCSI_OWNER_LOWLEVEL;
849
850         spin_lock_irqsave(scmd->device->host->host_lock, flags);
851         rtn = scmd->device->host->hostt->eh_device_reset_handler(scmd);
852         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
853
854         if (rtn == SUCCESS) {
855                 scmd->device->was_reset = 1;
856                 scmd->device->expecting_cc_ua = 1;
857         }
858
859         return rtn;
860 }
861
862 /**
863  * scsi_eh_try_stu - Send START_UNIT to device.
864  * @scmd:       Scsi cmd to send START_UNIT
865  *
866  * Return value:
867  *    0 - Device is ready. 1 - Device NOT ready.
868  **/
869 static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
870 {
871         static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
872         int rtn;
873
874         if (!scmd->device->allow_restart)
875                 return 1;
876
877         memcpy(scmd->cmnd, stu_command, sizeof(stu_command));
878
879         /*
880          * zero the sense buffer.  the scsi spec mandates that any
881          * untransferred sense data should be interpreted as being zero.
882          */
883         memset(scmd->sense_buffer, 0, sizeof(scmd->sense_buffer));
884
885         scmd->request_buffer = NULL;
886         scmd->request_bufflen = 0;
887         scmd->use_sg = 0;
888         scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
889         scmd->underflow = 0;
890         scmd->sc_data_direction = DMA_NONE;
891
892         rtn = scsi_send_eh_cmnd(scmd, START_UNIT_TIMEOUT);
893
894         /*
895          * when we eventually call scsi_finish, we really wish to complete
896          * the original request, so let's restore the original data. (db)
897          */
898         scsi_setup_cmd_retry(scmd);
899
900         /*
901          * hey, we are done.  let's look to see what happened.
902          */
903         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
904                 __FUNCTION__, scmd, rtn));
905         if (rtn == SUCCESS)
906                 return 0;
907         return 1;
908 }
909
910  /**
911  * scsi_eh_stu - send START_UNIT if needed
912  * @shost:      scsi host being recovered.
913  * @eh_done_q:  list_head for processed commands.
914  *
915  * Notes:
916  *    If commands are failing due to not ready, initializing command required,
917  *      try revalidating the device, which will end up sending a start unit. 
918  **/
919 static int scsi_eh_stu(struct Scsi_Host *shost,
920                               struct list_head *work_q,
921                               struct list_head *done_q)
922 {
923         struct list_head *lh, *lh_sf;
924         struct scsi_cmnd *scmd, *stu_scmd;
925         struct scsi_device *sdev;
926
927         shost_for_each_device(sdev, shost) {
928                 stu_scmd = NULL;
929                 list_for_each_entry(scmd, work_q, eh_entry)
930                         if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
931                             scsi_check_sense(scmd) == FAILED ) {
932                                 stu_scmd = scmd;
933                                 break;
934                         }
935
936                 if (!stu_scmd)
937                         continue;
938
939                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
940                                                   " 0x%p\n", current->comm, sdev));
941
942                 if (!scsi_eh_try_stu(stu_scmd)) {
943                         if (!scsi_device_online(sdev) ||
944                             !scsi_eh_tur(stu_scmd)) {
945                                 list_for_each_safe(lh, lh_sf, work_q) {
946                                         scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
947                                         if (scmd->device == sdev)
948                                                 scsi_eh_finish_cmd(scmd, done_q);
949                                 }
950                         }
951                 } else {
952                         SCSI_LOG_ERROR_RECOVERY(3,
953                                                 printk("%s: START_UNIT failed to sdev:"
954                                                        " 0x%p\n", current->comm, sdev));
955                 }
956         }
957
958         return list_empty(work_q);
959 }
960
961
962 /**
963  * scsi_eh_bus_device_reset - send bdr if needed
964  * @shost:      scsi host being recovered.
965  * @eh_done_q:  list_head for processed commands.
966  *
967  * Notes:
968  *    Try a bus device reset.  still, look to see whether we have multiple
969  *    devices that are jammed or not - if we have multiple devices, it
970  *    makes no sense to try bus_device_reset - we really would need to try
971  *    a bus_reset instead. 
972  **/
973 static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
974                                     struct list_head *work_q,
975                                     struct list_head *done_q)
976 {
977         struct list_head *lh, *lh_sf;
978         struct scsi_cmnd *scmd, *bdr_scmd;
979         struct scsi_device *sdev;
980         int rtn;
981
982         shost_for_each_device(sdev, shost) {
983                 bdr_scmd = NULL;
984                 list_for_each_entry(scmd, work_q, eh_entry)
985                         if (scmd->device == sdev) {
986                                 bdr_scmd = scmd;
987                                 break;
988                         }
989
990                 if (!bdr_scmd)
991                         continue;
992
993                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
994                                                   " 0x%p\n", current->comm,
995                                                   sdev));
996                 rtn = scsi_try_bus_device_reset(bdr_scmd);
997                 if (rtn == SUCCESS) {
998                         if (!scsi_device_online(sdev) ||
999                             !scsi_eh_tur(bdr_scmd)) {
1000                                 list_for_each_safe(lh, lh_sf,
1001                                                    work_q) {
1002                                         scmd = list_entry(lh, struct
1003                                                           scsi_cmnd,
1004                                                           eh_entry);
1005                                         if (scmd->device == sdev)
1006                                                 scsi_eh_finish_cmd(scmd,
1007                                                                    done_q);
1008                                 }
1009                         }
1010                 } else {
1011                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
1012                                                           " failed sdev:"
1013                                                           "0x%p\n",
1014                                                           current->comm,
1015                                                            sdev));
1016                 }
1017         }
1018
1019         return list_empty(work_q);
1020 }
1021
1022 /**
1023  * scsi_try_bus_reset - ask host to perform a bus reset
1024  * @scmd:       SCSI cmd to send bus reset.
1025  **/
1026 static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
1027 {
1028         unsigned long flags;
1029         int rtn;
1030
1031         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
1032                                           __FUNCTION__));
1033         scmd->owner = SCSI_OWNER_LOWLEVEL;
1034         scmd->serial_number_at_timeout = scmd->serial_number;
1035
1036         if (!scmd->device->host->hostt->eh_bus_reset_handler)
1037                 return FAILED;
1038
1039         spin_lock_irqsave(scmd->device->host->host_lock, flags);
1040         rtn = scmd->device->host->hostt->eh_bus_reset_handler(scmd);
1041         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1042
1043         if (rtn == SUCCESS) {
1044                 if (!scmd->device->host->hostt->skip_settle_delay)
1045                         ssleep(BUS_RESET_SETTLE_TIME);
1046                 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1047                 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1048                 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1049         }
1050
1051         return rtn;
1052 }
1053
1054 /**
1055  * scsi_try_host_reset - ask host adapter to reset itself
1056  * @scmd:       SCSI cmd to send hsot reset.
1057  **/
1058 static int scsi_try_host_reset(struct scsi_cmnd *scmd)
1059 {
1060         unsigned long flags;
1061         int rtn;
1062
1063         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
1064                                           __FUNCTION__));
1065         scmd->owner = SCSI_OWNER_LOWLEVEL;
1066         scmd->serial_number_at_timeout = scmd->serial_number;
1067
1068         if (!scmd->device->host->hostt->eh_host_reset_handler)
1069                 return FAILED;
1070
1071         spin_lock_irqsave(scmd->device->host->host_lock, flags);
1072         rtn = scmd->device->host->hostt->eh_host_reset_handler(scmd);
1073         spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1074
1075         if (rtn == SUCCESS) {
1076                 if (!scmd->device->host->hostt->skip_settle_delay)
1077                         ssleep(HOST_RESET_SETTLE_TIME);
1078                 spin_lock_irqsave(scmd->device->host->host_lock, flags);
1079                 scsi_report_bus_reset(scmd->device->host, scmd->device->channel);
1080                 spin_unlock_irqrestore(scmd->device->host->host_lock, flags);
1081         }
1082
1083         return rtn;
1084 }
1085
1086 /**
1087  * scsi_eh_bus_reset - send a bus reset 
1088  * @shost:      scsi host being recovered.
1089  * @eh_done_q:  list_head for processed commands.
1090  **/
1091 static int scsi_eh_bus_reset(struct Scsi_Host *shost,
1092                              struct list_head *work_q,
1093                              struct list_head *done_q)
1094 {
1095         struct list_head *lh, *lh_sf;
1096         struct scsi_cmnd *scmd;
1097         struct scsi_cmnd *chan_scmd;
1098         unsigned int channel;
1099         int rtn;
1100
1101         /*
1102          * we really want to loop over the various channels, and do this on
1103          * a channel by channel basis.  we should also check to see if any
1104          * of the failed commands are on soft_reset devices, and if so, skip
1105          * the reset.  
1106          */
1107
1108         for (channel = 0; channel <= shost->max_channel; channel++) {
1109                 chan_scmd = NULL;
1110                 list_for_each_entry(scmd, work_q, eh_entry) {
1111                         if (channel == scmd->device->channel) {
1112                                 chan_scmd = scmd;
1113                                 break;
1114                                 /*
1115                                  * FIXME add back in some support for
1116                                  * soft_reset devices.
1117                                  */
1118                         }
1119                 }
1120
1121                 if (!chan_scmd)
1122                         continue;
1123                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
1124                                                   " %d\n", current->comm,
1125                                                   channel));
1126                 rtn = scsi_try_bus_reset(chan_scmd);
1127                 if (rtn == SUCCESS) {
1128                         list_for_each_safe(lh, lh_sf, work_q) {
1129                                 scmd = list_entry(lh, struct scsi_cmnd,
1130                                                   eh_entry);
1131                                 if (channel == scmd->device->channel)
1132                                         if (!scsi_device_online(scmd->device) ||
1133                                             !scsi_eh_tur(scmd))
1134                                                 scsi_eh_finish_cmd(scmd,
1135                                                                    done_q);
1136                         }
1137                 } else {
1138                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
1139                                                           " failed chan: %d\n",
1140                                                           current->comm,
1141                                                           channel));
1142                 }
1143         }
1144         return list_empty(work_q);
1145 }
1146
1147 /**
1148  * scsi_eh_host_reset - send a host reset 
1149  * @work_q:     list_head for processed commands.
1150  * @done_q:     list_head for processed commands.
1151  **/
1152 static int scsi_eh_host_reset(struct list_head *work_q,
1153                               struct list_head *done_q)
1154 {
1155         int rtn;
1156         struct list_head *lh, *lh_sf;
1157         struct scsi_cmnd *scmd;
1158
1159         if (!list_empty(work_q)) {
1160                 scmd = list_entry(work_q->next,
1161                                   struct scsi_cmnd, eh_entry);
1162
1163                 SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
1164                                                   , current->comm));
1165
1166                 rtn = scsi_try_host_reset(scmd);
1167                 if (rtn == SUCCESS) {
1168                         list_for_each_safe(lh, lh_sf, work_q) {
1169                                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1170                                 if (!scsi_device_online(scmd->device) ||
1171                                     (!scsi_eh_try_stu(scmd) && !scsi_eh_tur(scmd)) ||
1172                                     !scsi_eh_tur(scmd))
1173                                         scsi_eh_finish_cmd(scmd, done_q);
1174                         }
1175                 } else {
1176                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
1177                                                           " failed\n",
1178                                                           current->comm));
1179                 }
1180         }
1181         return list_empty(work_q);
1182 }
1183
1184 /**
1185  * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
1186  * @work_q:     list_head for processed commands.
1187  * @done_q:     list_head for processed commands.
1188  *
1189  **/
1190 static void scsi_eh_offline_sdevs(struct list_head *work_q,
1191                                   struct list_head *done_q)
1192 {
1193         struct list_head *lh, *lh_sf;
1194         struct scsi_cmnd *scmd;
1195
1196         list_for_each_safe(lh, lh_sf, work_q) {
1197                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1198                 printk(KERN_INFO "scsi: Device offlined - not"
1199                                 " ready after error recovery: host"
1200                                 " %d channel %d id %d lun %d\n",
1201                                 scmd->device->host->host_no,
1202                                 scmd->device->channel,
1203                                 scmd->device->id,
1204                                 scmd->device->lun);
1205                 scsi_device_set_state(scmd->device, SDEV_OFFLINE);
1206                 if (scsi_eh_eflags_chk(scmd, SCSI_EH_CANCEL_CMD)) {
1207                         /*
1208                          * FIXME: Handle lost cmds.
1209                          */
1210                 }
1211                 scsi_eh_finish_cmd(scmd, done_q);
1212         }
1213         return;
1214 }
1215
1216 /**
1217  * scsi_decide_disposition - Disposition a cmd on return from LLD.
1218  * @scmd:       SCSI cmd to examine.
1219  *
1220  * Notes:
1221  *    This is *only* called when we are examining the status after sending
1222  *    out the actual data command.  any commands that are queued for error
1223  *    recovery (e.g. test_unit_ready) do *not* come through here.
1224  *
1225  *    When this routine returns failed, it means the error handler thread
1226  *    is woken.  In cases where the error code indicates an error that
1227  *    doesn't require the error handler read (i.e. we don't need to
1228  *    abort/reset), this function should return SUCCESS.
1229  **/
1230 int scsi_decide_disposition(struct scsi_cmnd *scmd)
1231 {
1232         int rtn;
1233
1234         /*
1235          * if the device is offline, then we clearly just pass the result back
1236          * up to the top level.
1237          */
1238         if (!scsi_device_online(scmd->device)) {
1239                 SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
1240                                                   " as SUCCESS\n",
1241                                                   __FUNCTION__));
1242                 return SUCCESS;
1243         }
1244
1245         /*
1246          * first check the host byte, to see if there is anything in there
1247          * that would indicate what we need to do.
1248          */
1249         switch (host_byte(scmd->result)) {
1250         case DID_PASSTHROUGH:
1251                 /*
1252                  * no matter what, pass this through to the upper layer.
1253                  * nuke this special code so that it looks like we are saying
1254                  * did_ok.
1255                  */
1256                 scmd->result &= 0xff00ffff;
1257                 return SUCCESS;
1258         case DID_OK:
1259                 /*
1260                  * looks good.  drop through, and check the next byte.
1261                  */
1262                 break;
1263         case DID_NO_CONNECT:
1264         case DID_BAD_TARGET:
1265         case DID_ABORT:
1266                 /*
1267                  * note - this means that we just report the status back
1268                  * to the top level driver, not that we actually think
1269                  * that it indicates SUCCESS.
1270                  */
1271                 return SUCCESS;
1272                 /*
1273                  * when the low level driver returns did_soft_error,
1274                  * it is responsible for keeping an internal retry counter 
1275                  * in order to avoid endless loops (db)
1276                  *
1277                  * actually this is a bug in this function here.  we should
1278                  * be mindful of the maximum number of retries specified
1279                  * and not get stuck in a loop.
1280                  */
1281         case DID_SOFT_ERROR:
1282                 goto maybe_retry;
1283         case DID_IMM_RETRY:
1284                 return NEEDS_RETRY;
1285
1286         case DID_ERROR:
1287                 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
1288                     status_byte(scmd->result) == RESERVATION_CONFLICT)
1289                         /*
1290                          * execute reservation conflict processing code
1291                          * lower down
1292                          */
1293                         break;
1294                 /* fallthrough */
1295
1296         case DID_BUS_BUSY:
1297         case DID_PARITY:
1298                 goto maybe_retry;
1299         case DID_TIME_OUT:
1300                 /*
1301                  * when we scan the bus, we get timeout messages for
1302                  * these commands if there is no device available.
1303                  * other hosts report did_no_connect for the same thing.
1304                  */
1305                 if ((scmd->cmnd[0] == TEST_UNIT_READY ||
1306                      scmd->cmnd[0] == INQUIRY)) {
1307                         return SUCCESS;
1308                 } else {
1309                         return FAILED;
1310                 }
1311         case DID_RESET:
1312                 return SUCCESS;
1313         default:
1314                 return FAILED;
1315         }
1316
1317         /*
1318          * next, check the message byte.
1319          */
1320         if (msg_byte(scmd->result) != COMMAND_COMPLETE)
1321                 return FAILED;
1322
1323         /*
1324          * check the status byte to see if this indicates anything special.
1325          */
1326         switch (status_byte(scmd->result)) {
1327         case QUEUE_FULL:
1328                 /*
1329                  * the case of trying to send too many commands to a
1330                  * tagged queueing device.
1331                  */
1332         case BUSY:
1333                 /*
1334                  * device can't talk to us at the moment.  Should only
1335                  * occur (SAM-3) when the task queue is empty, so will cause
1336                  * the empty queue handling to trigger a stall in the
1337                  * device.
1338                  */
1339                 return ADD_TO_MLQUEUE;
1340         case GOOD:
1341         case COMMAND_TERMINATED:
1342                 return SUCCESS;
1343         case CHECK_CONDITION:
1344                 rtn = scsi_check_sense(scmd);
1345                 if (rtn == NEEDS_RETRY)
1346                         goto maybe_retry;
1347                 /* if rtn == FAILED, we have no sense information;
1348                  * returning FAILED will wake the error handler thread
1349                  * to collect the sense and redo the decide
1350                  * disposition */
1351                 return rtn;
1352         case CONDITION_GOOD:
1353         case INTERMEDIATE_GOOD:
1354         case INTERMEDIATE_C_GOOD:
1355                 /*
1356                  * who knows?  FIXME(eric)
1357                  */
1358                 return SUCCESS;
1359
1360         case RESERVATION_CONFLICT:
1361                 printk("scsi%d (%d,%d,%d) : reservation conflict\n",
1362                        scmd->device->host->host_no, scmd->device->channel,
1363                        scmd->device->id, scmd->device->lun);
1364                 return SUCCESS; /* causes immediate i/o error */
1365         default:
1366                 return FAILED;
1367         }
1368         return FAILED;
1369
1370       maybe_retry:
1371
1372         /* we requeue for retry because the error was retryable, and
1373          * the request was not marked fast fail.  Note that above,
1374          * even if the request is marked fast fail, we still requeue
1375          * for queue congestion conditions (QUEUE_FULL or BUSY) */
1376         if ((++scmd->retries) < scmd->allowed 
1377             && !blk_noretry_request(scmd->request)) {
1378                 return NEEDS_RETRY;
1379         } else {
1380                 /*
1381                  * no more retries - report this one back to upper level.
1382                  */
1383                 return SUCCESS;
1384         }
1385 }
1386
1387 /**
1388  * scsi_eh_lock_done - done function for eh door lock request
1389  * @scmd:       SCSI command block for the door lock request
1390  *
1391  * Notes:
1392  *      We completed the asynchronous door lock request, and it has either
1393  *      locked the door or failed.  We must free the command structures
1394  *      associated with this request.
1395  **/
1396 static void scsi_eh_lock_done(struct scsi_cmnd *scmd)
1397 {
1398         struct scsi_request *sreq = scmd->sc_request;
1399
1400         scsi_release_request(sreq);
1401 }
1402
1403
1404 /**
1405  * scsi_eh_lock_door - Prevent medium removal for the specified device
1406  * @sdev:       SCSI device to prevent medium removal
1407  *
1408  * Locking:
1409  *      We must be called from process context; scsi_allocate_request()
1410  *      may sleep.
1411  *
1412  * Notes:
1413  *      We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
1414  *      head of the devices request queue, and continue.
1415  *
1416  * Bugs:
1417  *      scsi_allocate_request() may sleep waiting for existing requests to
1418  *      be processed.  However, since we haven't kicked off any request
1419  *      processing for this host, this may deadlock.
1420  *
1421  *      If scsi_allocate_request() fails for what ever reason, we
1422  *      completely forget to lock the door.
1423  **/
1424 static void scsi_eh_lock_door(struct scsi_device *sdev)
1425 {
1426         struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
1427
1428         if (unlikely(!sreq)) {
1429                 printk(KERN_ERR "%s: request allocate failed,"
1430                        "prevent media removal cmd not sent\n", __FUNCTION__);
1431                 return;
1432         }
1433
1434         sreq->sr_cmnd[0] = ALLOW_MEDIUM_REMOVAL;
1435         sreq->sr_cmnd[1] = 0;
1436         sreq->sr_cmnd[2] = 0;
1437         sreq->sr_cmnd[3] = 0;
1438         sreq->sr_cmnd[4] = SCSI_REMOVAL_PREVENT;
1439         sreq->sr_cmnd[5] = 0;
1440         sreq->sr_data_direction = DMA_NONE;
1441         sreq->sr_bufflen = 0;
1442         sreq->sr_buffer = NULL;
1443         sreq->sr_allowed = 5;
1444         sreq->sr_done = scsi_eh_lock_done;
1445         sreq->sr_timeout_per_command = 10 * HZ;
1446         sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
1447
1448         scsi_insert_special_req(sreq, 1);
1449 }
1450
1451
1452 /**
1453  * scsi_restart_operations - restart io operations to the specified host.
1454  * @shost:      Host we are restarting.
1455  *
1456  * Notes:
1457  *    When we entered the error handler, we blocked all further i/o to
1458  *    this device.  we need to 'reverse' this process.
1459  **/
1460 static void scsi_restart_operations(struct Scsi_Host *shost)
1461 {
1462         struct scsi_device *sdev;
1463
1464         /*
1465          * If the door was locked, we need to insert a door lock request
1466          * onto the head of the SCSI request queue for the device.  There
1467          * is no point trying to lock the door of an off-line device.
1468          */
1469         shost_for_each_device(sdev, shost) {
1470                 if (scsi_device_online(sdev) && sdev->locked)
1471                         scsi_eh_lock_door(sdev);
1472         }
1473
1474         /*
1475          * next free up anything directly waiting upon the host.  this
1476          * will be requests for character device operations, and also for
1477          * ioctls to queued block devices.
1478          */
1479         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
1480                                           __FUNCTION__));
1481
1482         clear_bit(SHOST_RECOVERY, &shost->shost_state);
1483
1484         wake_up(&shost->host_wait);
1485
1486         /*
1487          * finally we need to re-initiate requests that may be pending.  we will
1488          * have had everything blocked while error handling is taking place, and
1489          * now that error recovery is done, we will need to ensure that these
1490          * requests are started.
1491          */
1492         scsi_run_host_queues(shost);
1493 }
1494
1495 /**
1496  * scsi_eh_ready_devs - check device ready state and recover if not.
1497  * @shost:      host to be recovered.
1498  * @eh_done_q:  list_head for processed commands.
1499  *
1500  **/
1501 static void scsi_eh_ready_devs(struct Scsi_Host *shost,
1502                                struct list_head *work_q,
1503                                struct list_head *done_q)
1504 {
1505         if (!scsi_eh_stu(shost, work_q, done_q))
1506                 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1507                         if (!scsi_eh_bus_reset(shost, work_q, done_q))
1508                                 if (!scsi_eh_host_reset(work_q, done_q))
1509                                         scsi_eh_offline_sdevs(work_q, done_q);
1510 }
1511
1512 /**
1513  * scsi_eh_flush_done_q - finish processed commands or retry them.
1514  * @done_q:     list_head of processed commands.
1515  *
1516  **/
1517 static void scsi_eh_flush_done_q(struct list_head *done_q)
1518 {
1519         struct list_head *lh, *lh_sf;
1520         struct scsi_cmnd *scmd;
1521
1522         list_for_each_safe(lh, lh_sf, done_q) {
1523                 scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1524                 list_del_init(lh);
1525                 if (scsi_device_online(scmd->device) &&
1526                     !blk_noretry_request(scmd->request) &&
1527                     (++scmd->retries < scmd->allowed)) {
1528                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
1529                                                           " retry cmd: %p\n",
1530                                                           current->comm,
1531                                                           scmd));
1532                                 scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
1533                 } else {
1534                         if (!scmd->result)
1535                                 scmd->result |= (DRIVER_TIMEOUT << 24);
1536                         SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
1537                                                         " cmd: %p\n",
1538                                                         current->comm, scmd));
1539                         scsi_finish_command(scmd);
1540                 }
1541         }
1542 }
1543
1544 /**
1545  * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
1546  * @shost:      Host to unjam.
1547  *
1548  * Notes:
1549  *    When we come in here, we *know* that all commands on the bus have
1550  *    either completed, failed or timed out.  we also know that no further
1551  *    commands are being sent to the host, so things are relatively quiet
1552  *    and we have freedom to fiddle with things as we wish.
1553  *
1554  *    This is only the *default* implementation.  it is possible for
1555  *    individual drivers to supply their own version of this function, and
1556  *    if the maintainer wishes to do this, it is strongly suggested that
1557  *    this function be taken as a template and modified.  this function
1558  *    was designed to correctly handle problems for about 95% of the
1559  *    different cases out there, and it should always provide at least a
1560  *    reasonable amount of error recovery.
1561  *
1562  *    Any command marked 'failed' or 'timeout' must eventually have
1563  *    scsi_finish_cmd() called for it.  we do all of the retry stuff
1564  *    here, so when we restart the host after we return it should have an
1565  *    empty queue.
1566  **/
1567 static void scsi_unjam_host(struct Scsi_Host *shost)
1568 {
1569         unsigned long flags;
1570         LIST_HEAD(eh_work_q);
1571         LIST_HEAD(eh_done_q);
1572
1573         spin_lock_irqsave(shost->host_lock, flags);
1574         list_splice_init(&shost->eh_cmd_q, &eh_work_q);
1575         spin_unlock_irqrestore(shost->host_lock, flags);
1576
1577         SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
1578
1579         if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
1580                 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1581                         scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
1582
1583         scsi_eh_flush_done_q(&eh_done_q);
1584 }
1585
1586 /**
1587  * scsi_error_handler - Handle errors/timeouts of SCSI cmds.
1588  * @data:       Host for which we are running.
1589  *
1590  * Notes:
1591  *    This is always run in the context of a kernel thread.  The idea is
1592  *    that we start this thing up when the kernel starts up (one per host
1593  *    that we detect), and it immediately goes to sleep and waits for some
1594  *    event (i.e. failure).  When this takes place, we have the job of
1595  *    trying to unjam the bus and restarting things.
1596  **/
1597 int scsi_error_handler(void *data)
1598 {
1599         struct Scsi_Host *shost = (struct Scsi_Host *) data;
1600         int rtn;
1601         DECLARE_MUTEX_LOCKED(sem);
1602
1603         /*
1604          *    Flush resources
1605          */
1606
1607         daemonize("scsi_eh_%d", shost->host_no);
1608
1609         current->flags |= PF_NOFREEZE;
1610
1611         shost->eh_wait = &sem;
1612         shost->ehandler = current;
1613
1614         /*
1615          * Wake up the thread that created us.
1616          */
1617         SCSI_LOG_ERROR_RECOVERY(3, printk("Wake up parent of"
1618                                           " scsi_eh_%d\n",shost->host_no));
1619
1620         complete(shost->eh_notify);
1621
1622         while (1) {
1623                 /*
1624                  * If we get a signal, it means we are supposed to go
1625                  * away and die.  This typically happens if the user is
1626                  * trying to unload a module.
1627                  */
1628                 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1629                                                   " scsi_eh_%d"
1630                                                   " sleeping\n",shost->host_no));
1631
1632                 /*
1633                  * Note - we always use down_interruptible with the semaphore
1634                  * even if the module was loaded as part of the kernel.  The
1635                  * reason is that down() will cause this thread to be counted
1636                  * in the load average as a running process, and down
1637                  * interruptible doesn't.  Given that we need to allow this
1638                  * thread to die if the driver was loaded as a module, using
1639                  * semaphores isn't unreasonable.
1640                  */
1641                 down_interruptible(&sem);
1642                 if (shost->eh_kill)
1643                         break;
1644
1645                 SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler"
1646                                                   " scsi_eh_%d waking"
1647                                                   " up\n",shost->host_no));
1648
1649                 shost->eh_active = 1;
1650
1651                 /*
1652                  * We have a host that is failing for some reason.  Figure out
1653                  * what we need to do to get it up and online again (if we can).
1654                  * If we fail, we end up taking the thing offline.
1655                  */
1656                 if (shost->hostt->eh_strategy_handler) 
1657                         rtn = shost->hostt->eh_strategy_handler(shost);
1658                 else
1659                         scsi_unjam_host(shost);
1660
1661                 shost->eh_active = 0;
1662
1663                 /*
1664                  * Note - if the above fails completely, the action is to take
1665                  * individual devices offline and flush the queue of any
1666                  * outstanding requests that may have been pending.  When we
1667                  * restart, we restart any I/O to any other devices on the bus
1668                  * which are still online.
1669                  */
1670                 scsi_restart_operations(shost);
1671
1672         }
1673
1674         SCSI_LOG_ERROR_RECOVERY(1, printk("Error handler scsi_eh_%d"
1675                                           " exiting\n",shost->host_no));
1676
1677         /*
1678          * Make sure that nobody tries to wake us up again.
1679          */
1680         shost->eh_wait = NULL;
1681
1682         /*
1683          * Knock this down too.  From this point on, the host is flying
1684          * without a pilot.  If this is because the module is being unloaded,
1685          * that's fine.  If the user sent a signal to this thing, we are
1686          * potentially in real danger.
1687          */
1688         shost->eh_active = 0;
1689         shost->ehandler = NULL;
1690
1691         /*
1692          * If anyone is waiting for us to exit (i.e. someone trying to unload
1693          * a driver), then wake up that process to let them know we are on
1694          * the way out the door.
1695          */
1696         complete_and_exit(shost->eh_notify, 0);
1697         return 0;
1698 }
1699
1700 /*
1701  * Function:    scsi_report_bus_reset()
1702  *
1703  * Purpose:     Utility function used by low-level drivers to report that
1704  *              they have observed a bus reset on the bus being handled.
1705  *
1706  * Arguments:   shost       - Host in question
1707  *              channel     - channel on which reset was observed.
1708  *
1709  * Returns:     Nothing
1710  *
1711  * Lock status: Host lock must be held.
1712  *
1713  * Notes:       This only needs to be called if the reset is one which
1714  *              originates from an unknown location.  Resets originated
1715  *              by the mid-level itself don't need to call this, but there
1716  *              should be no harm.
1717  *
1718  *              The main purpose of this is to make sure that a CHECK_CONDITION
1719  *              is properly treated.
1720  */
1721 void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
1722 {
1723         struct scsi_device *sdev;
1724
1725         __shost_for_each_device(sdev, shost) {
1726                 if (channel == sdev->channel) {
1727                         sdev->was_reset = 1;
1728                         sdev->expecting_cc_ua = 1;
1729                 }
1730         }
1731 }
1732
1733 /*
1734  * Function:    scsi_report_device_reset()
1735  *
1736  * Purpose:     Utility function used by low-level drivers to report that
1737  *              they have observed a device reset on the device being handled.
1738  *
1739  * Arguments:   shost       - Host in question
1740  *              channel     - channel on which reset was observed
1741  *              target      - target on which reset was observed
1742  *
1743  * Returns:     Nothing
1744  *
1745  * Lock status: Host lock must be held
1746  *
1747  * Notes:       This only needs to be called if the reset is one which
1748  *              originates from an unknown location.  Resets originated
1749  *              by the mid-level itself don't need to call this, but there
1750  *              should be no harm.
1751  *
1752  *              The main purpose of this is to make sure that a CHECK_CONDITION
1753  *              is properly treated.
1754  */
1755 void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
1756 {
1757         struct scsi_device *sdev;
1758
1759         __shost_for_each_device(sdev, shost) {
1760                 if (channel == sdev->channel &&
1761                     target == sdev->id) {
1762                         sdev->was_reset = 1;
1763                         sdev->expecting_cc_ua = 1;
1764                 }
1765         }
1766 }
1767
1768 static void
1769 scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
1770 {
1771 }
1772
1773 /*
1774  * Function:    scsi_reset_provider
1775  *
1776  * Purpose:     Send requested reset to a bus or device at any phase.
1777  *
1778  * Arguments:   device  - device to send reset to
1779  *              flag - reset type (see scsi.h)
1780  *
1781  * Returns:     SUCCESS/FAILURE.
1782  *
1783  * Notes:       This is used by the SCSI Generic driver to provide
1784  *              Bus/Device reset capability.
1785  */
1786 int
1787 scsi_reset_provider(struct scsi_device *dev, int flag)
1788 {
1789         struct scsi_cmnd *scmd = scsi_get_command(dev, GFP_KERNEL);
1790         struct request req;
1791         int rtn;
1792
1793         scmd->request = &req;
1794         memset(&scmd->eh_timeout, 0, sizeof(scmd->eh_timeout));
1795         scmd->request->rq_status        = RQ_SCSI_BUSY;
1796         scmd->state                     = SCSI_STATE_INITIALIZING;
1797         scmd->owner                     = SCSI_OWNER_MIDLEVEL;
1798     
1799         memset(&scmd->cmnd, '\0', sizeof(scmd->cmnd));
1800     
1801         scmd->scsi_done         = scsi_reset_provider_done_command;
1802         scmd->done                      = NULL;
1803         scmd->buffer                    = NULL;
1804         scmd->bufflen                   = 0;
1805         scmd->request_buffer            = NULL;
1806         scmd->request_bufflen           = 0;
1807         scmd->internal_timeout          = NORMAL_TIMEOUT;
1808         scmd->abort_reason              = DID_ABORT;
1809
1810         scmd->cmd_len                   = 0;
1811
1812         scmd->sc_data_direction         = DMA_BIDIRECTIONAL;
1813         scmd->sc_request                = NULL;
1814         scmd->sc_magic                  = SCSI_CMND_MAGIC;
1815
1816         init_timer(&scmd->eh_timeout);
1817
1818         /*
1819          * Sometimes the command can get back into the timer chain,
1820          * so use the pid as an identifier.
1821          */
1822         scmd->pid                       = 0;
1823
1824         switch (flag) {
1825         case SCSI_TRY_RESET_DEVICE:
1826                 rtn = scsi_try_bus_device_reset(scmd);
1827                 if (rtn == SUCCESS)
1828                         break;
1829                 /* FALLTHROUGH */
1830         case SCSI_TRY_RESET_BUS:
1831                 rtn = scsi_try_bus_reset(scmd);
1832                 if (rtn == SUCCESS)
1833                         break;
1834                 /* FALLTHROUGH */
1835         case SCSI_TRY_RESET_HOST:
1836                 rtn = scsi_try_host_reset(scmd);
1837                 break;
1838         default:
1839                 rtn = FAILED;
1840         }
1841
1842         scsi_delete_timer(scmd);
1843         scsi_next_command(scmd);
1844         return rtn;
1845 }