root/drivers/scsi/scsi.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. blacklisted
  2. restart_all
  3. scan_scsis_done
  4. scsi_luns_setup
  5. scan_scsis
  6. scsi_times_out
  7. request_queueable
  8. allocate_device
  9. internal_cmnd
  10. scsi_request_sense
  11. scsi_do_cmd
  12. reset
  13. check_sense
  14. scsi_done
  15. scsi_abort
  16. scsi_reset
  17. scsi_main_timeout
  18. update_timeout
  19. scsi_malloc
  20. scsi_free
  21. scsi_init_malloc
  22. scsi_init_free
  23. scsi_dev_init
  24. print_inquiry
  25. scsi_register_host
  26. scsi_unregister_host
  27. scsi_register_module
  28. scsi_unregister_module
  29. scsi_dump_status

   1 /*
   2  *      scsi.c Copyright (C) 1992 Drew Eckhardt 
   3  *             Copyright (C) 1993, 1994, 1995 Eric Youngdale
   4  *
   5  *      generic mid-level SCSI driver
   6  *              Initial versions: Drew Eckhardt 
   7  *              Subsequent revisions: Eric Youngdale
   8  *
   9  *      <drew@colorado.edu>
  10  *
  11  *      Bug correction thanks go to : 
  12  *              Rik Faith <faith@cs.unc.edu>
  13  *              Tommy Thorn <tthorn>
  14  *              Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  15  * 
  16  *       Modified by Eric Youngdale ericy@cais.com to
  17  *       add scatter-gather, multiple outstanding request, and other
  18  *       enhancements.
  19  */
  20 
  21 #include <asm/system.h>
  22 #include <linux/sched.h>
  23 #include <linux/timer.h>
  24 #include <linux/string.h>
  25 #include <linux/malloc.h>
  26 #include <asm/irq.h>
  27 #include <asm/dma.h>
  28 #include <linux/ioport.h>
  29 
  30 #include "../block/blk.h"
  31 #include "scsi.h"
  32 #include "hosts.h"
  33 #include "constants.h"
  34 
  35 /*
  36 static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/scsi.c,v 1.5 1993/09/24 12:45:18 drew Exp drew $";
  37 */
  38 
  39 /* Command groups 3 and 4 are reserved and should never be used.  */
  40 const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
  41 
  42 #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
  43 
  44 static void scsi_done (Scsi_Cmnd *SCpnt);
  45 static int update_timeout (Scsi_Cmnd *, int);
  46 static void print_inquiry(unsigned char *data);
  47 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid);
  48 
  49 static int time_start;
  50 static int time_elapsed;
  51 static int scsi_maybe_deadlocked = 0;
  52 static int max_active = 0;
  53 static struct Scsi_Host * host_active = NULL;
  54 static struct Scsi_Host * host_next = NULL;
  55 
  56 #define MAX_SCSI_DEVICE_CODE 10
  57 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
  58 {
  59  "Direct-Access    ",
  60  "Sequential-Access",
  61  "Printer          ",
  62  "Processor        ",
  63  "WORM             ",
  64  "CD-ROM           ",
  65  "Scanner          ",
  66  "Optical Device   ",
  67  "Medium Changer   ",
  68  "Communications   "
  69 };
  70 
  71 
  72 /*
  73         global variables : 
  74         scsi_devices an array of these specifying the address for each 
  75         (host, id, LUN)
  76 */
  77 
  78 Scsi_Device * scsi_devices = NULL;
  79 
  80 /* Process ID of SCSI commands */
  81 unsigned long scsi_pid = 0;
  82 
  83 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
  84 
  85 /* This variable is merely a hook so that we can debug the kernel with gdb. */
  86 Scsi_Cmnd * last_cmnd = NULL;
  87 
  88 /*
  89  *      As the scsi do command functions are intelligent, and may need to 
  90  *      redo a command, we need to keep track of the last command 
  91  *      executed on each one.
  92  */
  93 
  94 #define WAS_RESET       0x01
  95 #define WAS_TIMEDOUT    0x02
  96 #define WAS_SENSE       0x04
  97 #define IS_RESETTING    0x08
  98 #define IS_ABORTING     0x10
  99 #define ASKED_FOR_SENSE 0x20
 100 
 101 /*
 102  *      This is the number  of clock ticks we should wait before we time out 
 103  *      and abort the command.  This is for  where the scsi.c module generates 
 104  *      the command, not where it originates from a higher level, in which
 105  *      case the timeout is specified there.
 106  *
 107  *      ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
 108  *      respectively.
 109  */
 110 
 111 #ifdef DEBUG_TIMEOUT
 112 static void scsi_dump_status(void);
 113 #endif
 114 
 115 
 116 #ifdef DEBUG
 117         #define SCSI_TIMEOUT 500
 118 #else
 119         #define SCSI_TIMEOUT 100
 120 #endif
 121 
 122 #ifdef DEBUG
 123         #define SENSE_TIMEOUT SCSI_TIMEOUT
 124         #define ABORT_TIMEOUT SCSI_TIMEOUT
 125         #define RESET_TIMEOUT SCSI_TIMEOUT
 126 #else
 127         #define SENSE_TIMEOUT 50
 128         #define RESET_TIMEOUT 50
 129         #define ABORT_TIMEOUT 50
 130         #define MIN_RESET_DELAY 100
 131 #endif
 132 
 133 /* The following devices are known not to tolerate a lun != 0 scan for
 134    one reason or another.  Some will respond to all luns, others will
 135    lock up. */
 136 
 137      struct blist{
 138        char * vendor;
 139        char * model;
 140        char * revision; /* Latest revision known to be bad.  Not used yet */
 141      };
 142 
 143 static struct blist blacklist[] = 
 144 {
 145    {"CHINON","CD-ROM CDS-431","H42"},  /* Locks up if polled for lun != 0 */
 146    {"CHINON","CD-ROM CDS-535","Q14"}, /* Lockup if polled for lun != 0 */
 147    {"DENON","DRD-25X","V"},   /* A cdrom that locks up when probed at lun != 0 */
 148    {"IMS", "CDD521/10","2.06"},   /* Locks-up when LUN>0 polled. */
 149    {"MAXTOR","XT-3280","PR02"},   /* Locks-up when LUN>0 polled. */
 150    {"MAXTOR","XT-4380S","B3C"},   /* Locks-up when LUN>0 polled. */
 151    {"MAXTOR","MXT-1240S","I1.2"}, /* Locks up when LUN > 0 polled */
 152    {"MAXTOR","XT-4170S","B5A"},   /* Locks-up sometimes when LUN>0 polled. */
 153    {"MAXTOR","XT-8760S","B7B"},   /* guess what? */
 154    {"NEC","CD-ROM DRIVE:841","1.0"},  /* Locks-up when LUN>0 polled. */
 155    {"RODIME","RO3000S","2.33"},  /* Locks up if polled for lun != 0 */
 156    {"SEAGATE", "ST157N", "\004|j"}, /* causes failed REQUEST SENSE on lun 1 for aha152x
 157                                      * controller, which causes SCSI code to reset bus.*/
 158    {"SEAGATE", "ST296","921"},   /* Responds to all lun */
 159    {"SONY","CD-ROM CDU-541","4.3d"},
 160    {"SONY","CD-ROM CDU-55S","1.0i"},
 161    {"TANDBERG","TDC 3600","U07"},  /* Locks up if polled for lun != 0 */
 162    {"TEAC","CD-ROM","1.06"},    /* causes failed REQUEST SENSE on lun 1 for seagate
 163                                  * controller, which causes SCSI code to reset bus.*/
 164    {"TEXEL","CD-ROM","1.06"},   /* causes failed REQUEST SENSE on lun 1 for seagate
 165                                  * controller, which causes SCSI code to reset bus.*/
 166    {"QUANTUM","LPS525S","3110"},/* Locks sometimes if polled for lun != 0 */
 167    {"QUANTUM","PD1225S","3110"},/* Locks sometimes if polled for lun != 0 */
 168    {"MEDIAVIS","CDR-H93MV","1.31"},  /* Locks up if polled for lun != 0 */
 169    {"SANKYO", "CP525","6.64"},  /* causes failed REQ SENSE, extra reset */
 170    {"HP", "C1750A", "3226"},    /* scanjet iic */
 171    {"HP", "C1790A", ""},        /* scanjet iip */
 172    {"HP", "C2500A", ""},        /* scanjet iicx */
 173    {NULL, NULL, NULL}}; 
 174 
 175 static int blacklisted(unsigned char * response_data){
     /* [previous][next][first][last][top][bottom][index][help] */
 176   int i = 0;
 177   unsigned char * pnt;
 178   for(i=0; 1; i++){
 179     if(blacklist[i].vendor == NULL) return 0;
 180     pnt = &response_data[8];
 181     while(*pnt && *pnt == ' ') pnt++;
 182     if(memcmp(blacklist[i].vendor, pnt,
 183                strlen(blacklist[i].vendor))) continue;
 184     pnt = &response_data[16];
 185     while(*pnt && *pnt == ' ') pnt++;
 186     if(memcmp(blacklist[i].model, pnt,
 187                strlen(blacklist[i].model))) continue;
 188     return 1;
 189   };    
 190 };
 191 
 192 /*
 193  *      As the actual SCSI command runs in the background, we must set up a 
 194  *      flag that tells scan_scsis() when the result it has is valid.  
 195  *      scan_scsis can set the_result to -1, and watch for it to become the 
 196  *      actual return code for that call.  the scan_scsis_done function() is 
 197  *      our user specified completion function that is passed on to the  
 198  *      scsi_do_cmd() function.
 199  */
 200 
 201 volatile int in_scan_scsis = 0;
 202 static int the_result;
 203 
 204 static void restart_all(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 205  /*
 206   * If we might have deadlocked, get things started again. Only
 207   * for block devices. Character devices should never deadlock.
 208   */
 209   struct Scsi_Device_Template * sdtpnt;
 210 
 211   for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
 212 
 213      if (sdtpnt->blk && blk_dev[sdtpnt->major].request_fn)
 214         (*blk_dev[sdtpnt->major].request_fn)();
 215 
 216   }
 217 
 218 static void scan_scsis_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 219         {
 220         
 221 #ifdef DEBUG
 222         printk ("scan_scsis_done(%d, %06x)\n", SCpnt->host, SCpnt->result);
 223 #endif  
 224         SCpnt->request.dev = 0xfffe;
 225 
 226         if (SCpnt->request.sem != NULL)
 227           up(SCpnt->request.sem);
 228         }
 229 
 230 #ifdef NO_MULTI_LUN
 231 static int max_scsi_luns = 1;
 232 #else
 233 static int max_scsi_luns = 8;
 234 #endif
 235 
 236 void scsi_luns_setup(char *str, int *ints) {
     /* [previous][next][first][last][top][bottom][index][help] */
 237     if (ints[0] != 1) 
 238         printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
 239     else
 240         max_scsi_luns = ints[1];
 241 }
 242 
 243 /*
 244  *      Detecting SCSI devices :        
 245  *      We scan all present host adapter's busses,  from ID 0 to ID 6.  
 246  *      We use the INQUIRY command, determine device type, and pass the ID / 
 247  *      lun address of all sequential devices to the tape driver, all random 
 248  *      devices to the disk driver.
 249  */
 250 
 251 void scan_scsis (struct Scsi_Host * shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 252 {
 253   int dev, lun, type;
 254   unsigned char scsi_cmd [12];
 255   unsigned char scsi_result0 [256];
 256   unsigned char * scsi_result;
 257   Scsi_Device * SDpnt, *SDtail;
 258   struct Scsi_Device_Template * sdtpnt;
 259   Scsi_Cmnd  SCmd;
 260   
 261   ++in_scan_scsis;
 262   lun = 0;
 263   type = -1;
 264   SCmd.next = NULL;
 265   SCmd.prev = NULL;
 266   SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
 267   SDtail = scsi_devices;
 268   if(scsi_devices) {
 269     while(SDtail->next) SDtail = SDtail->next;
 270   }
 271 
 272   /* Make sure we have something that is valid for DMA purposes */
 273   scsi_result = ((current == task[0]  || !shpnt->unchecked_isa_dma) 
 274                  ?  &scsi_result0[0] : scsi_malloc(512));
 275             
 276 
 277   shpnt->host_queue = &SCmd;  /* We need this so that
 278                                          commands can time out */
 279   for (dev = 0; dev < 8; ++dev)
 280     if (shpnt->this_id != dev)
 281 /*
 282  * We need the for so our continue, etc. work fine.
 283  */
 284 
 285       for (lun = 0; lun < max_scsi_luns; ++lun)
 286         {
 287           SDpnt->host = shpnt;
 288           SDpnt->id = dev;
 289           SDpnt->lun = lun;
 290           SDpnt->device_wait = NULL;
 291           SDpnt->next = NULL;
 292           SDpnt->attached = 0;
 293 /*
 294  * Assume that the device will have handshaking problems, and then 
 295  * fix this field later if it turns out it doesn't.
 296  */
 297           SDpnt->borken = 1;
 298           
 299           scsi_cmd[0] = TEST_UNIT_READY;
 300           scsi_cmd[1] = lun << 5;
 301           scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 302           scsi_cmd[4] = 0;
 303           
 304           SCmd.host = shpnt;
 305           SCmd.target = dev;
 306           SCmd.lun = lun;
 307 
 308           SCmd.request.sem = NULL;  /* Used for mutex if loading devices after boot */
 309           SCmd.request.dev = 0xffff; /* Mark not busy */
 310           SCmd.use_sg  = 0;
 311           SCmd.cmd_len = 0;
 312           SCmd.old_use_sg  = 0;
 313           SCmd.transfersize = 0;
 314           SCmd.underflow = 0;
 315           
 316           scsi_do_cmd (&SCmd,
 317                        (void *)  scsi_cmd, (void *) 
 318                        scsi_result, 256,  scan_scsis_done, 
 319                        SCSI_TIMEOUT + 400, 5);
 320           
 321           /* Wait for command to finish.  Use simple wait if we are booting, else
 322              do it right and use a mutex */
 323 
 324           if (current == task[0]){
 325             while (SCmd.request.dev != 0xfffe);
 326           } else {
 327             if (SCmd.request.dev != 0xfffe){
 328               struct semaphore sem = MUTEX_LOCKED;
 329               SCmd.request.sem = &sem;
 330               down(&sem);
 331               /* Hmm.. Have to ask about this one */
 332               while (SCmd.request.dev != 0xfffe) schedule();
 333             }
 334           }
 335 
 336 #if defined(DEBUG) || defined(DEBUG_INIT)
 337           printk("scsi: scan SCSIS id %d lun %d\n", dev, lun);
 338           printk("scsi: return code %08x\n", SCmd.result);
 339 #endif
 340           
 341           
 342           if(SCmd.result) {
 343             if ((driver_byte(SCmd.result)  & DRIVER_SENSE) &&
 344                 ((SCmd.sense_buffer[0] & 0x70) >> 4) == 7) {
 345               if (SCmd.sense_buffer[2] &0xe0)
 346                 continue; /* No devices here... */
 347               if(((SCmd.sense_buffer[2] & 0xf) != NOT_READY) &&
 348                  ((SCmd.sense_buffer[2] & 0xf) != UNIT_ATTENTION))
 349                 continue;
 350             }
 351             else
 352               break;
 353           };
 354           
 355 #if defined (DEBUG) || defined(DEBUG_INIT)
 356           printk("scsi: performing INQUIRY\n");
 357 #endif
 358           
 359           /*
 360            * Build an INQUIRY command block.  
 361            */
 362           
 363           scsi_cmd[0] = INQUIRY;
 364           scsi_cmd[1] = (lun << 5) & 0xe0;
 365           scsi_cmd[2] = 0;
 366           scsi_cmd[3] = 0;
 367           scsi_cmd[4] = 255;
 368           scsi_cmd[5] = 0;
 369           
 370           SCmd.request.dev = 0xffff; /* Mark not busy */
 371           SCmd.cmd_len = 0;
 372           
 373           scsi_do_cmd (&SCmd,
 374                        (void *)  scsi_cmd, (void *) 
 375                        scsi_result, 256,  scan_scsis_done, 
 376                        SCSI_TIMEOUT, 3);
 377           
 378           if (current == task[0]){
 379             while (SCmd.request.dev != 0xfffe);
 380           } else {
 381             if (SCmd.request.dev != 0xfffe){
 382               struct semaphore sem = MUTEX_LOCKED;
 383               SCmd.request.sem = &sem;
 384               down(&sem);
 385               /* Hmm.. Have to ask about this one */
 386               while (SCmd.request.dev != 0xfffe) schedule();
 387             }
 388           }
 389 
 390           the_result = SCmd.result;
 391           
 392 #if defined(DEBUG) || defined(DEBUG_INIT)
 393           if (!the_result)
 394             printk("scsi: INQUIRY successful\n");
 395           else
 396             printk("scsi: INQUIRY failed with code %08x\n", the_result);
 397 #endif
 398           
 399           if(the_result) break; 
 400           
 401           /* skip other luns on this device */
 402           
 403           if (!the_result)
 404             {
 405                 /* It would seem some TOSHIBA CD-ROM gets things wrong */
 406                 if (!strncmp(scsi_result+8,"TOSHIBA",7) &&
 407                     !strncmp(scsi_result+16,"CD-ROM",6) &&
 408                     scsi_result[0] == TYPE_DISK) {
 409                         scsi_result[0] = TYPE_ROM;
 410                         scsi_result[1] |= 0x80;  /* removable */
 411                 }
 412 
 413 /*
 414  *     Unfortunately the Toshiba CD-ROM XM-3401TA doesn't
 415  *     understand the vendor specific mode select/sense
 416  *     commands which are used by the photo cd routines in 
 417  *     sr.c.
 418  */
 419 
 420               SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
 421               if (!strncmp(scsi_result+8,"NEC",3))
 422                 SDpnt->manufacturer = SCSI_MAN_NEC;
 423               if (!strncmp(scsi_result+8,"TOSHIBA",7) &&
 424                   strncmp(scsi_result+16,"CD-ROM XM-3401TA",16) &&
 425                   strncmp(scsi_result+32,"3593",4))
 426                 SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
 427 
 428               SDpnt->removable = (0x80 & 
 429                                   scsi_result[1]) >> 7;
 430               SDpnt->lockable = SDpnt->removable;
 431               SDpnt->changed = 0;
 432               SDpnt->access_count = 0;
 433               SDpnt->busy = 0;
 434 /* 
 435  *      Currently, all sequential devices are assumed to be tapes,
 436  *      all random devices disk, with the appropriate read only 
 437  *      flags set for ROM / WORM treated as RO.
 438  */ 
 439 
 440               switch (type = (scsi_result[0] & 0x1f))
 441                 {
 442                 case TYPE_TAPE :
 443                 case TYPE_DISK :
 444                 case TYPE_MOD :
 445                 case TYPE_PROCESSOR :
 446                 case TYPE_SCANNER :
 447                   SDpnt->writeable = 1;
 448                   break;
 449                 case TYPE_WORM :
 450                 case TYPE_ROM :
 451                   SDpnt->writeable = 0;
 452                   break;
 453                 default :
 454 #if 0
 455 #ifdef DEBUG
 456                   printk("scsi: unknown type %d\n", type);
 457                   print_inquiry(scsi_result);
 458 #endif
 459                   type = -1;
 460 #endif
 461                 }
 462               
 463               SDpnt->soft_reset = 
 464                 (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
 465               SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
 466               SDpnt->type = (type & 0x1f);
 467               
 468               if (type != -1)
 469                 {
 470                   print_inquiry(scsi_result);
 471 
 472                   for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
 473                     if(sdtpnt->detect) SDpnt->attached +=
 474                       (*sdtpnt->detect)(SDpnt);
 475 
 476                   SDpnt->scsi_level = scsi_result[2] & 0x07;
 477                   if (SDpnt->scsi_level >= 2 ||
 478                       (SDpnt->scsi_level == 1 &&
 479                        (scsi_result[3] & 0x0f) == 1))
 480                     SDpnt->scsi_level++;
 481 /* 
 482  * Set the tagged_queue flag for SCSI-II devices that purport to support
 483  * tagged queuing in the INQUIRY data.
 484  */
 485                   
 486                   SDpnt->tagged_queue = 0;
 487                   
 488                   if ((SDpnt->scsi_level >= SCSI_2) &&
 489                       (scsi_result[7] & 2)) {
 490                     SDpnt->tagged_supported = 1;
 491                     SDpnt->current_tag = 0;
 492                   }
 493                   
 494 /*
 495  * Accommodate drivers that want to sleep when they should be in a polling
 496  * loop.
 497  */
 498 
 499                   SDpnt->disconnect = 0;
 500 
 501 /*
 502  * Some revisions of the Texel CD ROM drives have handshaking
 503  * problems when used with the Seagate controllers.  Before we
 504  * know what type of device we're talking to, we assume it's 
 505  * borken and then change it here if it turns out that it isn't
 506  * a TEXEL drive.
 507  */
 508 
 509                   if(strncmp("TEXEL", (char *) &scsi_result[8], 5) != 0 ||
 510                      strncmp("CD-ROM", (char *) &scsi_result[16], 6) != 0 
 511 /* 
 512  * XXX 1.06 has problems, some one should figure out the others too so
 513  * ALL TEXEL drives don't suffer in performance, especially when I finish
 514  * integrating my seagate patches which do multiple I_T_L nexuses.
 515  */
 516 
 517 #ifdef notyet
 518                      || (strncmp("1.06", (char *) &scsi_result[[, 4) != 0)))
 519 #endif
 520                                  )
 521                     SDpnt->borken = 0;
 522                   
 523                   
 524                   /* These devices need this "key" to unlock the device
 525                      so we can use it */
 526                   if(memcmp("INSITE", &scsi_result[8], 6) == 0 &&
 527                      (memcmp("Floptical   F*8I", &scsi_result[16], 16) == 0
 528                       || memcmp("I325VM", &scsi_result[16], 6) == 0)) {
 529                     printk("Unlocked floptical drive.\n");
 530                     SDpnt->lockable = 0;
 531                     scsi_cmd[0] = MODE_SENSE;
 532                     scsi_cmd[1] = (lun << 5) & 0xe0;
 533                     scsi_cmd[2] = 0x2e;
 534                     scsi_cmd[3] = 0;
 535                     scsi_cmd[4] = 0x2a;
 536                     scsi_cmd[5] = 0;
 537                     
 538                     SCmd.request.dev = 0xffff; /* Mark not busy */
 539                     SCmd.cmd_len = 0;
 540                     
 541                     scsi_do_cmd (&SCmd,
 542                                  (void *)  scsi_cmd, (void *) 
 543                                  scsi_result, 0x2a,  scan_scsis_done, 
 544                                  SCSI_TIMEOUT, 3);
 545                     
 546                     if (current == task[0]){
 547                       while (SCmd.request.dev != 0xfffe);
 548                     } else {
 549                       if (SCmd.request.dev != 0xfffe){
 550                         struct semaphore sem = MUTEX_LOCKED;
 551                         SCmd.request.sem = &sem;
 552                         down(&sem);
 553                         /* Hmm.. Have to ask about this one */
 554                         while (SCmd.request.dev != 0xfffe) schedule();
 555                       }
 556                     }
 557                   }
 558                   /* Add this device to the linked list at the end */
 559                   if(SDtail)
 560                     SDtail->next = SDpnt;
 561                   else
 562                     scsi_devices = SDpnt;
 563                   SDtail = SDpnt;
 564 
 565                   SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
 566                   /* Some scsi devices cannot be polled for lun != 0
 567                      due to firmware bugs */
 568                   if(blacklisted(scsi_result)) break;
 569                   /* Old drives like the MAXTOR XT-3280 say vers=0 */
 570                   if ((scsi_result[2] & 0x07) == 0)
 571                     break;
 572                   /* Some scsi-1 peripherals do not handle lun != 0.
 573                      I am assuming that scsi-2 peripherals do better */
 574                   if((scsi_result[2] & 0x07) == 1 && 
 575                      (scsi_result[3] & 0x0f) == 0) break;
 576                 }
 577             }       /* if result == DID_OK ends */
 578         }       /* for lun ends */
 579   shpnt->host_queue = NULL;  /* No longer needed here */
 580   
 581   /* Last device block does not exist.  Free memory. */
 582   scsi_init_free((char *) SDpnt, sizeof(Scsi_Device));
 583   
 584 
 585   /* If we allocated a buffer so we could do DMA, free it now */
 586   if (scsi_result != &scsi_result0[0]) scsi_free(scsi_result, 512);
 587 
 588   in_scan_scsis = 0;
 589 }       /* scan_scsis  ends */
 590 
 591 /*
 592  *      Flag bits for the internal_timeout array 
 593  */
 594 
 595 #define NORMAL_TIMEOUT 0
 596 #define IN_ABORT 1
 597 #define IN_RESET 2
 598 /*
 599         This is our time out function, called when the timer expires for a 
 600         given host adapter.  It will attempt to abort the currently executing 
 601         command, that failing perform a kernel panic.
 602 */ 
 603 
 604 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 605         {
 606         
 607         switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
 608                 {
 609                 case NORMAL_TIMEOUT:
 610                         if (!in_scan_scsis) {
 611 #ifdef DEBUG_TIMEOUT
 612                           scsi_dump_status();
 613 #endif
 614                         }
 615                         
 616                         if (!scsi_abort (SCpnt, DID_TIME_OUT, pid))
 617                                 return;                         
 618                 case IN_ABORT:
 619                         printk("SCSI host %d abort() timed out - resetting\n",
 620                                 SCpnt->host->host_no);
 621                         if (!scsi_reset (SCpnt)) 
 622                                 return;
 623                 case IN_RESET:
 624                 case (IN_ABORT | IN_RESET):
 625                   /* This might be controversial, but if there is a bus hang,
 626                      you might conceivably want the machine up and running
 627                      esp if you have an ide disk. */
 628                         printk("Unable to reset scsi host %d - ",SCpnt->host->host_no);
 629                         printk("probably a SCSI bus hang.\n");
 630                         return;
 631 
 632                 default:
 633                         INTERNAL_ERROR;
 634                 }
 635                                         
 636         }
 637 
 638 
 639 /* This function takes a quick look at a request, and decides if it
 640 can be queued now, or if there would be a stall while waiting for
 641 something else to finish.  This routine assumes that interrupts are
 642 turned off when entering the routine.  It is the responsibility
 643 of the calling code to ensure that this is the case. */
 644 
 645 Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 646 {
 647   Scsi_Cmnd * SCpnt = NULL;
 648   int tablesize;
 649   struct buffer_head * bh, *bhp;
 650 
 651   if (!device)
 652     panic ("No device passed to request_queueable().\n");
 653   
 654   if (req && req->dev <= 0)
 655     panic("Invalid device in request_queueable");
 656   
 657   SCpnt =  device->host->host_queue;
 658     while(SCpnt){
 659       if(SCpnt->target == device->id &&
 660          SCpnt->lun == device->lun)
 661         if(SCpnt->request.dev < 0) break;
 662       SCpnt = SCpnt->next;
 663     };
 664 
 665   if (!SCpnt) return NULL;
 666 
 667   if (device->host->can_queue &&
 668        ((device->host->block && host_active && device->host != host_active) || 
 669         (device->host->block && host_next && device->host != host_next) ||
 670         device->host->host_busy >= device->host->can_queue)) {
 671 
 672      if (device->host->block && !host_next && host_active 
 673                              && device->host != host_active) 
 674         host_next = device->host;
 675 
 676      return NULL;
 677      }
 678 
 679   if (req) {
 680     memcpy(&SCpnt->request, req, sizeof(struct request));
 681     tablesize = device->host->sg_tablesize;
 682     bhp = bh = req->bh;
 683     if(!tablesize) bh = NULL;
 684     /* Take a quick look through the table to see how big it is.  We already
 685        have our copy of req, so we can mess with that if we want to.  */
 686     while(req->nr_sectors && bh){
 687             bhp = bhp->b_reqnext;
 688             if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
 689             req->nr_sectors -= bh->b_size >> 9;
 690             req->sector += bh->b_size >> 9;
 691             if(!tablesize) break;
 692             bh = bhp;
 693     };
 694     if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
 695       SCpnt->request.bhtail = bh;
 696       req->bh = bh->b_reqnext; /* Divide request */
 697       bh->b_reqnext = NULL;
 698       bh = req->bh;
 699 
 700       /* Now reset things so that req looks OK */
 701       SCpnt->request.nr_sectors -= req->nr_sectors;
 702       req->current_nr_sectors = bh->b_size >> 9;
 703       req->buffer = bh->b_data;
 704       SCpnt->request.sem = NULL; /* Wait until whole thing done */
 705     } else {
 706       req->dev = -1;
 707       wake_up(&wait_for_request);
 708     };      
 709   } else {
 710     SCpnt->request.dev = 0xffff; /* Busy, but no request */
 711     SCpnt->request.sem = NULL;  /* And no one is waiting for the device either */
 712   };
 713 
 714   SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
 715   SCpnt->old_use_sg  = 0;
 716   SCpnt->transfersize = 0;
 717   SCpnt->underflow = 0;
 718   SCpnt->cmd_len = 0;
 719   return SCpnt;
 720 }
 721 
 722 /* This function returns a structure pointer that will be valid for
 723 the device.  The wait parameter tells us whether we should wait for
 724 the unit to become free or not.  We are also able to tell this routine
 725 not to return a descriptor if the host is unable to accept any more
 726 commands for the time being.  We need to keep in mind that there is no
 727 guarantee that the host remain not busy.  Keep in mind the
 728 request_queueable function also knows the internal allocation scheme
 729 of the packets for each device */
 730 
 731 Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
     /* [previous][next][first][last][top][bottom][index][help] */
 732                              int wait)
 733 {
 734   int dev = -1;
 735   struct request * req = NULL;
 736   int tablesize;
 737   unsigned int flags;
 738   struct buffer_head * bh, *bhp;
 739   struct Scsi_Host * host;
 740   Scsi_Cmnd * SCpnt = NULL;
 741   Scsi_Cmnd * SCwait = NULL;
 742 
 743   if (!device)
 744     panic ("No device passed to allocate_device().\n");
 745   
 746   if (reqp) req = *reqp;
 747 
 748     /* See if this request has already been queued by an interrupt routine */
 749   if (req && (dev = req->dev) <= 0) return NULL;
 750   
 751   host = device->host;
 752   
 753   if (intr_count && host->can_queue &&
 754                       ((host->block && host_active && host != host_active) ||
 755                        (host->block && host_next && host != host_next) ||
 756                        host->host_busy >= host->can_queue)) {
 757 
 758      scsi_maybe_deadlocked = 1;
 759 
 760      if (host->block && !host_next && host_active && host != host_active) 
 761         host_next = host;
 762 
 763      return NULL;
 764      }
 765 
 766   while (1==1){
 767     SCpnt = host->host_queue;
 768     while(SCpnt){
 769       if(SCpnt->target == device->id &&
 770          SCpnt->lun == device->lun) {
 771         SCwait = SCpnt;
 772         if(SCpnt->request.dev < 0) break;
 773       };
 774       SCpnt = SCpnt->next;
 775     };
 776     save_flags(flags);
 777     cli();
 778     /* See if this request has already been queued by an interrupt routine */
 779     if (req && ((req->dev < 0) || (req->dev != dev))) {
 780       restore_flags(flags);
 781       return NULL;
 782     };
 783     if (!SCpnt || SCpnt->request.dev >= 0)  /* Might have changed */
 784       {
 785         restore_flags(flags);
 786         if(!wait) return NULL;
 787         if (!SCwait) {
 788           printk("Attempt to allocate device target %d, lun %d\n",
 789                  device->id ,device->lun);
 790           panic("No device found in allocate_device\n");
 791         };
 792         SCSI_SLEEP(&device->device_wait, 
 793                    (SCwait->request.dev > 0));
 794       } else {
 795         if (req) {
 796           memcpy(&SCpnt->request, req, sizeof(struct request));
 797           tablesize = device->host->sg_tablesize;
 798           bhp = bh = req->bh;
 799           if(!tablesize) bh = NULL;
 800           /* Take a quick look through the table to see how big it is.  We already
 801              have our copy of req, so we can mess with that if we want to.  */
 802           while(req->nr_sectors && bh){
 803             bhp = bhp->b_reqnext;
 804             if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
 805             req->nr_sectors -= bh->b_size >> 9;
 806             req->sector += bh->b_size >> 9;
 807             if(!tablesize) break;
 808             bh = bhp;
 809           };
 810           if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
 811             SCpnt->request.bhtail = bh;
 812             req->bh = bh->b_reqnext; /* Divide request */
 813             bh->b_reqnext = NULL;
 814             bh = req->bh;
 815             /* Now reset things so that req looks OK */
 816             SCpnt->request.nr_sectors -= req->nr_sectors;
 817             req->current_nr_sectors = bh->b_size >> 9;
 818             req->buffer = bh->b_data;
 819             SCpnt->request.sem = NULL; /* Wait until whole thing done */
 820           }
 821           else 
 822             {
 823               req->dev = -1;
 824               *reqp = req->next;
 825               wake_up(&wait_for_request);
 826             };
 827         } else {
 828           SCpnt->request.dev = 0xffff; /* Busy */
 829           SCpnt->request.sem = NULL;  /* And no one is waiting for this to complete */
 830         };
 831         restore_flags(flags);
 832         break;
 833       };
 834   };
 835 
 836   SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
 837   SCpnt->old_use_sg  = 0;
 838   SCpnt->transfersize = 0;      /* No default transfer size */
 839   SCpnt->cmd_len = 0;
 840   SCpnt->underflow = 0;         /* Do not flag underflow conditions */
 841   return SCpnt;
 842 }
 843 
 844 /*
 845         This is inline because we have stack problemes if we recurse to deeply.
 846 */
 847                          
 848 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 849         {
 850         int temp;
 851         struct Scsi_Host * host;
 852 #ifdef DEBUG_DELAY      
 853         int clock;
 854 #endif
 855 
 856         if ((unsigned long) &SCpnt < current->kernel_stack_page)
 857           panic("Kernel stack overflow.");
 858 
 859         host = SCpnt->host;
 860 
 861 /*
 862         We will wait MIN_RESET_DELAY clock ticks after the last reset so 
 863         we can avoid the drive not being ready.
 864 */ 
 865 temp = host->last_reset;
 866 while (jiffies < temp);
 867 
 868 update_timeout(SCpnt, SCpnt->timeout_per_command);
 869 
 870 /*
 871         We will use a queued command if possible, otherwise we will emulate the
 872         queuing and calling of completion function ourselves. 
 873 */
 874 #ifdef DEBUG
 875         printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer =  %08x, \n"
 876                 "bufflen = %d, done = %08x)\n", SCpnt->host->host_no, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
 877 #endif
 878 
 879         if (host->can_queue)
 880                 {
 881 #ifdef DEBUG
 882         printk("queuecommand : routine at %08x\n", 
 883                 host->hostt->queuecommand);
 884 #endif
 885                   /* This locking tries to prevent all sorts of races between
 886                      queuecommand and the interrupt code.  In effect,
 887                      we are only allowed to be in queuecommand once at
 888                      any given time, and we can only be in the interrupt
 889                      handler and the queuecommand function at the same time
 890                      when queuecommand is called while servicing the
 891                      interrupt. */
 892 
 893                 if(!intr_count && SCpnt->host->irq)
 894                   disable_irq(SCpnt->host->irq);
 895 
 896                 host->hostt->queuecommand (SCpnt, scsi_done);
 897 
 898                 if(!intr_count && SCpnt->host->irq)
 899                   enable_irq(SCpnt->host->irq);
 900                 }
 901         else
 902                 {
 903 
 904 #ifdef DEBUG
 905         printk("command() :  routine at %08x\n", host->hostt->command);
 906 #endif
 907                 temp=host->hostt->command (SCpnt);
 908                 SCpnt->result = temp;
 909 #ifdef DEBUG_DELAY
 910         clock = jiffies + 400;
 911         while (jiffies < clock);
 912         printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp, done);
 913 #endif
 914                 scsi_done(SCpnt);
 915                 }       
 916 #ifdef DEBUG
 917         printk("leaving internal_cmnd()\n");
 918 #endif
 919         }       
 920 
 921 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 922         {
 923         unsigned int flags;
 924         
 925         save_flags(flags);
 926         cli();
 927         SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
 928         update_timeout(SCpnt, SENSE_TIMEOUT);
 929         restore_flags(flags);
 930         
 931 
 932         memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
 933                 sizeof(generic_sense));
 934 
 935         SCpnt->cmnd[1] = SCpnt->lun << 5;       
 936         SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
 937 
 938         SCpnt->request_buffer = &SCpnt->sense_buffer;
 939         SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
 940         SCpnt->use_sg = 0;
 941         SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
 942         internal_cmnd (SCpnt);
 943         }
 944 
 945 
 946 
 947 /*
 948         scsi_do_cmd sends all the commands out to the low-level driver.  It 
 949         handles the specifics required for each low level driver - ie queued 
 950         or non queued.  It also prevents conflicts when different high level 
 951         drivers go for the same host at the same time.
 952 */
 953 
 954 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd , 
     /* [previous][next][first][last][top][bottom][index][help] */
 955                   void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
 956                   int timeout, int retries 
 957                    )
 958         {
 959         unsigned long flags;
 960         struct Scsi_Host * host = SCpnt->host;
 961 
 962 #ifdef DEBUG
 963         {
 964         int i;  
 965         int target = SCpnt->target;
 966         printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
 967                 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
 968                 "command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
 969         for (i = 0; i < 10; ++i)
 970                 printk ("%02x  ", ((unsigned char *) cmnd)[i]); 
 971         printk("\n");
 972       };
 973 #endif
 974         
 975         if (!host)
 976                 {
 977                 panic ("Invalid or not present host.\n");
 978                 }
 979 
 980         
 981 /*
 982         We must prevent reentrancy to the lowlevel host driver.  This prevents 
 983         it - we enter a loop until the host we want to talk to is not busy.   
 984         Race conditions are prevented, as interrupts are disabled in between the
 985         time we check for the host being not busy, and the time we mark it busy
 986         ourselves.
 987 */
 988 
 989         SCpnt->pid = scsi_pid++; 
 990 
 991         for(;;) {
 992 
 993           save_flags(flags);
 994           cli();
 995 
 996           if (host->can_queue &&
 997                 ((host->block && host_active && host != host_active) ||
 998                  (host->block && host_next && host != host_next) ||
 999                  host->host_busy >= host->can_queue)) {
1000 
1001              if (intr_count) 
1002                 panic("scsi: queueing to inactive host while in interrupt.\n");
1003 
1004              if (host->block && !host_next && host_active 
1005                                            && host != host_active) 
1006                 host_next = host;
1007 
1008              restore_flags(flags);
1009              SCSI_SLEEP(&host->host_wait, 
1010                        ((host->block && host_active && host != host_active) ||
1011                         (host->block && host_next && host != host_next) ||
1012                         host->host_busy >= host->can_queue));
1013              } 
1014 
1015           else {
1016              host->host_busy++;
1017 
1018              if (host->block) {
1019 
1020                 if (!host_active) {
1021                    max_active = 0;
1022                    host_active = host;
1023                    host_next = NULL;
1024                    }
1025                 else if (max_active > 7 && !host_next) 
1026                    host_next = host->block;
1027 
1028                 max_active++;
1029                 }
1030 
1031              restore_flags(flags);
1032              break;
1033              }
1034           }
1035 /*
1036         Our own function scsi_done (which marks the host as not busy, disables 
1037         the timeout counter, etc) will be called by us or by the 
1038         scsi_hosts[host].queuecommand() function needs to also call
1039         the completion function for the high level driver.
1040 
1041 */
1042 
1043         memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
1044 #if 0
1045         SCpnt->host = host;
1046         SCpnt->target = target;
1047         SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
1048 #endif
1049         SCpnt->bufflen = bufflen;
1050         SCpnt->buffer = buffer;
1051         SCpnt->flags=0;
1052         SCpnt->retries=0;
1053         SCpnt->allowed=retries;
1054         SCpnt->done = done;
1055         SCpnt->timeout_per_command = timeout;
1056                                 
1057         memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
1058         /* Zero the sense buffer.  Some host adapters automatically request
1059            sense on error.  0 is not a valid sense code.  */
1060         memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1061         SCpnt->request_buffer = buffer;
1062         SCpnt->request_bufflen = bufflen;
1063         SCpnt->old_use_sg = SCpnt->use_sg;
1064         if (SCpnt->cmd_len == 0)
1065                 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1066         SCpnt->old_cmd_len = SCpnt->cmd_len;
1067 
1068         /* Start the timer ticking.  */
1069 
1070         SCpnt->internal_timeout = 0;
1071         SCpnt->abort_reason = 0;
1072         internal_cmnd (SCpnt);
1073 
1074 #ifdef DEBUG
1075         printk ("Leaving scsi_do_cmd()\n");
1076 #endif
1077         }
1078 
1079 
1080 
1081 /*
1082         The scsi_done() function disables the timeout timer for the scsi host, 
1083         marks the host as not busy, and calls the user specified completion 
1084         function for that host's current command.
1085 */
1086 
1087 static void reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1088 {
1089 #ifdef DEBUG
1090         printk("scsi: reset(%d)\n", SCpnt->host->host_no);
1091 #endif
1092         
1093         SCpnt->flags |= (WAS_RESET | IS_RESETTING);
1094         scsi_reset(SCpnt);
1095         
1096 #ifdef DEBUG
1097         printk("performing request sense\n");
1098 #endif
1099         
1100 #if 0  /* FIXME - remove this when done */
1101         if(SCpnt->flags & NEEDS_JUMPSTART) {
1102           SCpnt->flags &= ~NEEDS_JUMPSTART;
1103           scsi_request_sense (SCpnt);
1104         };
1105 #endif
1106 }
1107         
1108         
1109 
1110 static int check_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1111         {
1112   /* If there is no sense information, request it.  If we have already
1113      requested it, there is no point in asking again - the firmware must be
1114      confused. */
1115   if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1116     if(!(SCpnt->flags & ASKED_FOR_SENSE))
1117       return SUGGEST_SENSE;
1118     else
1119       return SUGGEST_RETRY;
1120       }
1121   
1122   SCpnt->flags &= ~ASKED_FOR_SENSE;
1123 
1124 #ifdef DEBUG_INIT
1125         printk("scsi%d : ", SCpnt->host->host_no);
1126         print_sense("", SCpnt);
1127         printk("\n");
1128 #endif
1129                 if (SCpnt->sense_buffer[2] &0xe0)
1130                   return SUGGEST_ABORT;
1131 
1132                 switch (SCpnt->sense_buffer[2] & 0xf)
1133                 {
1134                 case NO_SENSE:
1135                         return 0;
1136                 case RECOVERED_ERROR:
1137                         return SUGGEST_IS_OK;
1138 
1139                 case ABORTED_COMMAND:
1140                         return SUGGEST_RETRY;   
1141                 case NOT_READY:
1142                 case UNIT_ATTENTION:
1143                         return SUGGEST_ABORT;
1144 
1145                 /* these three are not supported */     
1146                 case COPY_ABORTED:
1147                 case VOLUME_OVERFLOW:
1148                 case MISCOMPARE:
1149         
1150                 case MEDIUM_ERROR:
1151                         return SUGGEST_REMAP;
1152                 case BLANK_CHECK:
1153                 case DATA_PROTECT:
1154                 case HARDWARE_ERROR:
1155                 case ILLEGAL_REQUEST:
1156                 default:
1157                         return SUGGEST_ABORT;
1158                 }
1159               }
1160 
1161 /* This function is the mid-level interrupt routine, which decides how
1162  *  to handle error conditions.  Each invocation of this function must
1163  *  do one and *only* one of the following:
1164  *
1165  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
1166  *      normal completion, and indicates that the handling for this
1167  *      request is complete.
1168  *  (2) Call internal_cmnd to requeue the command.  This will result in
1169  *      scsi_done being called again when the retry is complete.
1170  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
1171  *      more information about the error condition.  When the information
1172  *      is available, scsi_done will be called again.
1173  *  (4) Call reset().  This is sort of a last resort, and the idea is that
1174  *      this may kick things loose and get the drive working again.  reset()
1175  *      automatically calls scsi_request_sense, and thus scsi_done will be
1176  *      called again once the reset is complete.
1177  *
1178  *      If none of the above actions are taken, the drive in question
1179  * will hang. If more than one of the above actions are taken by
1180  * scsi_done, then unpredictable behavior will result.
1181  */
1182 static void scsi_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1183         {
1184         int status=0;
1185         int exit=0;
1186         int checked;
1187         int oldto;
1188         struct Scsi_Host * host = SCpnt->host;
1189         int result = SCpnt->result;
1190         oldto = update_timeout(SCpnt, 0);
1191 
1192 #ifdef DEBUG_TIMEOUT
1193         if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1194                           result, SCpnt->target, SCpnt->lun);
1195 #endif
1196 
1197         /* If we requested an abort, (and we got it) then fix up the return
1198            status to say why */
1199         if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1200           SCpnt->result = result = (result & 0xff00ffff) | 
1201             (SCpnt->abort_reason << 16);
1202 
1203 
1204 #define FINISHED 0
1205 #define MAYREDO  1
1206 #define REDO     3
1207 #define PENDING  4
1208 
1209 #ifdef DEBUG
1210         printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1211 #endif
1212 
1213         if(SCpnt->flags & WAS_SENSE)
1214         {
1215                 SCpnt->use_sg = SCpnt->old_use_sg;
1216                 SCpnt->cmd_len = SCpnt->old_cmd_len;
1217         }
1218 
1219         switch (host_byte(result))      
1220         {
1221         case DID_OK:
1222                 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1223                         /* Failed to obtain sense information */
1224                         {
1225                         SCpnt->flags &= ~WAS_SENSE;
1226                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1227 
1228                         if (!(SCpnt->flags & WAS_RESET))
1229                                 {
1230                                 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n", 
1231                                         SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
1232                                 reset(SCpnt);
1233                                 return;
1234                                 }
1235                         else
1236                                 {
1237                                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1238                                 status = FINISHED;
1239                                 }
1240                         }
1241                 else switch(msg_byte(result))
1242                         {
1243                         case COMMAND_COMPLETE:
1244                         switch (status_byte(result))
1245                         {
1246                         case GOOD:
1247                                 if (SCpnt->flags & WAS_SENSE)
1248                                         {
1249 #ifdef DEBUG
1250         printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1251 #endif
1252 
1253                                         SCpnt->flags &= ~WAS_SENSE;
1254                                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1255         
1256                                         switch (checked = check_sense(SCpnt))
1257                                         {
1258                                         case SUGGEST_SENSE:
1259                                         case 0: 
1260 #ifdef DEBUG
1261         printk("NO SENSE.  status = REDO\n");
1262 #endif
1263 
1264                                                 update_timeout(SCpnt, oldto);
1265                                                 status = REDO;
1266                                                 break;
1267                                         case SUGGEST_IS_OK:
1268                                                 break;
1269                                         case SUGGEST_REMAP:                     
1270                                         case SUGGEST_RETRY: 
1271 #ifdef DEBUG
1272         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1273 #endif
1274 
1275                                                 status = MAYREDO;
1276                                                 exit = DRIVER_SENSE | SUGGEST_RETRY;
1277                                                 break;
1278                                         case SUGGEST_ABORT:
1279 #ifdef DEBUG
1280         printk("SENSE SUGGEST ABORT - status = FINISHED");
1281 #endif
1282 
1283                                                 status = FINISHED;
1284                                                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
1285                                                 break;
1286                                         default:
1287                                                 printk ("Internal error %s %d \n", __FILE__, 
1288                                                         __LINE__);
1289                                         }                          
1290                                         }       
1291                                 else
1292                                         {
1293 #ifdef DEBUG
1294         printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1295 #endif
1296 
1297                                         exit =  DRIVER_OK;
1298                                         status = FINISHED;
1299                                         }
1300                                 break;  
1301 
1302                         case CHECK_CONDITION:
1303                                 switch (check_sense(SCpnt))
1304                                   {
1305                                   case 0: 
1306                                     update_timeout(SCpnt, oldto);
1307                                     status = REDO;
1308                                     break;
1309                                   case SUGGEST_REMAP:                   
1310                                   case SUGGEST_RETRY:
1311                                     status = MAYREDO;
1312                                     exit = DRIVER_SENSE | SUGGEST_RETRY;
1313                                     break;
1314                                   case SUGGEST_ABORT:
1315                                     status = FINISHED;
1316                                     exit =  DRIVER_SENSE | SUGGEST_ABORT;
1317                                     break;
1318                                   case SUGGEST_SENSE:
1319                                 scsi_request_sense (SCpnt);
1320                                 status = PENDING;
1321                                 break;          
1322                                   }
1323                                 break;          
1324                         
1325                         case CONDITION_GOOD:
1326                         case INTERMEDIATE_GOOD:
1327                         case INTERMEDIATE_C_GOOD:
1328                                 break;
1329                                 
1330                         case BUSY:
1331                                 update_timeout(SCpnt, oldto);
1332                                 status = REDO;
1333                                 break;
1334 
1335                         case RESERVATION_CONFLICT:
1336                                 printk("scsi%d : RESERVATION CONFLICT performing reset.\n", 
1337                                         SCpnt->host->host_no);
1338                                 reset(SCpnt);
1339                                 return;
1340 #if 0
1341                                 exit = DRIVER_SOFT | SUGGEST_ABORT;
1342                                 status = MAYREDO;
1343                                 break;
1344 #endif
1345                         default:
1346                                 printk ("Internal error %s %d \n"
1347                                         "status byte = %d \n", __FILE__, 
1348                                         __LINE__, status_byte(result));
1349                                 
1350                         }
1351                         break;
1352                         default:
1353                                 panic("scsi: unsupported message byte %d received\n", msg_byte(result)); 
1354                         }
1355                         break;
1356         case DID_TIME_OUT:      
1357 #ifdef DEBUG
1358         printk("Host returned DID_TIME_OUT - ");
1359 #endif
1360 
1361                 if (SCpnt->flags & WAS_TIMEDOUT)
1362                         {
1363 #ifdef DEBUG
1364         printk("Aborting\n");
1365 #endif  
1366                         exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1367                         }               
1368                 else 
1369                         {
1370 #ifdef DEBUG
1371                         printk ("Retrying.\n");
1372 #endif
1373                         SCpnt->flags  |= WAS_TIMEDOUT;
1374                         SCpnt->internal_timeout &= ~IN_ABORT;
1375                         status = REDO;
1376                         }
1377                 break;
1378         case DID_BUS_BUSY:
1379         case DID_PARITY:
1380                 status = REDO;
1381                 break;
1382         case DID_NO_CONNECT:
1383 #ifdef DEBUG
1384                 printk("Couldn't connect.\n");
1385 #endif
1386                 exit  = (DRIVER_HARD | SUGGEST_ABORT);
1387                 break;
1388         case DID_ERROR: 
1389                 status = MAYREDO;
1390                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1391                 break;
1392         case DID_BAD_TARGET:
1393         case DID_ABORT:
1394                 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1395                 break;  
1396         case DID_RESET:
1397                 if (SCpnt->flags & IS_RESETTING)
1398                         {
1399                         SCpnt->flags &= ~IS_RESETTING;
1400                         status = REDO;
1401                         break;
1402                         }
1403 
1404                 if(msg_byte(result) == GOOD &&
1405                       status_byte(result) == CHECK_CONDITION) {
1406                         switch (check_sense(SCpnt)) {
1407                         case 0: 
1408                             update_timeout(SCpnt, oldto);
1409                             status = REDO;
1410                             break;
1411                         case SUGGEST_REMAP:                     
1412                         case SUGGEST_RETRY:
1413                             status = MAYREDO;
1414                             exit = DRIVER_SENSE | SUGGEST_RETRY;
1415                             break;
1416                         case SUGGEST_ABORT:
1417                             status = FINISHED;
1418                             exit =  DRIVER_SENSE | SUGGEST_ABORT;
1419                             break;
1420                         case SUGGEST_SENSE:
1421                               scsi_request_sense (SCpnt);
1422                               status = PENDING;
1423                               break;
1424                         }
1425                 } else {
1426                 status=REDO;
1427                 exit = SUGGEST_RETRY;
1428                 }
1429                 break;
1430         default :               
1431                 exit = (DRIVER_ERROR | SUGGEST_DIE);
1432         }
1433 
1434         switch (status) 
1435                 {
1436                 case FINISHED:
1437                 case PENDING:
1438                         break;
1439                 case MAYREDO:
1440 
1441 #ifdef DEBUG
1442         printk("In MAYREDO, allowing %d retries, have %d\n",
1443                SCpnt->allowed, SCpnt->retries);
1444 #endif
1445 
1446                         if ((++SCpnt->retries) < SCpnt->allowed)
1447                         {
1448                         if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1449                             && !(SCpnt->flags & WAS_RESET))
1450                                 {
1451                                         printk("scsi%d : resetting for second half of retries.\n",
1452                                                 SCpnt->host->host_no);
1453                                         reset(SCpnt);
1454                                         break;
1455                                 }
1456 
1457                         }
1458                         else
1459                                 {
1460                                 status = FINISHED;
1461                                 break;
1462                                 }
1463                         /* fall through to REDO */
1464 
1465                 case REDO:
1466 
1467                         if (host->block) scsi_maybe_deadlocked = 1;
1468 
1469                         if (SCpnt->flags & WAS_SENSE)                   
1470                                 scsi_request_sense(SCpnt);      
1471                         else    
1472                           {
1473                             memcpy ((void *) SCpnt->cmnd,
1474                                     (void*) SCpnt->data_cmnd, 
1475                                     sizeof(SCpnt->data_cmnd));
1476                             SCpnt->request_buffer = SCpnt->buffer;
1477                             SCpnt->request_bufflen = SCpnt->bufflen;
1478                             SCpnt->use_sg = SCpnt->old_use_sg;
1479                             SCpnt->cmd_len = SCpnt->old_cmd_len;
1480                             internal_cmnd (SCpnt);
1481                           };
1482                         break;  
1483                 default: 
1484                         INTERNAL_ERROR;
1485                 }
1486 
1487 
1488         if (status == FINISHED) {
1489 #ifdef DEBUG
1490            printk("Calling done function - at address %08x\n", SCpnt->done);
1491 #endif
1492            host->host_busy--; /* Indicate that we are free */
1493 
1494            if (host->block && host->host_busy == 0) {
1495               struct Scsi_Host * block;
1496 
1497               block = host_next;
1498               host_active = NULL;
1499 
1500               if (block) wake_up(&block->host_wait);
1501 
1502               for (block = host->block; block != host; block = block->block)
1503                  wake_up(&block->host_wait);
1504 
1505               host_next = NULL;
1506 
1507               if (scsi_maybe_deadlocked) {
1508                  scsi_maybe_deadlocked = 0;
1509                  restart_all();
1510                  }
1511 
1512               }
1513 
1514            wake_up(&host->host_wait);
1515            SCpnt->result = result | ((exit & 0xff) << 24);
1516            SCpnt->use_sg = SCpnt->old_use_sg;
1517            SCpnt->cmd_len = SCpnt->old_cmd_len;
1518            SCpnt->done (SCpnt);
1519            }
1520 
1521 #undef FINISHED
1522 #undef REDO
1523 #undef MAYREDO
1524 #undef PENDING
1525         }
1526 
1527 /*
1528         The scsi_abort function interfaces with the abort() function of the host
1529         we are aborting, and causes the current command to not complete.  The 
1530         caller should deal with any error messages or status returned on the 
1531         next call.
1532         
1533         This will not be called reentrantly for a given host.
1534 */
1535         
1536 /*
1537         Since we're nice guys and specified that abort() and reset()
1538         can be non-reentrant.  The internal_timeout flags are used for
1539         this.
1540 */
1541 
1542 
1543 int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
1544         {
1545         int oldto;
1546         unsigned long flags;
1547         struct Scsi_Host * host = SCpnt->host;
1548         
1549         while(1)        
1550                 {
1551                 save_flags(flags);
1552                 cli();
1553 
1554                 /*
1555                  * Protect against races here.  If the command is done, or we are
1556                  * on a different command forget it.
1557                  */
1558                 if (SCpnt->request.dev == -1 || pid != SCpnt->pid) {
1559                   restore_flags(flags);
1560                   return 0;
1561                 }
1562 
1563                 if (SCpnt->internal_timeout & IN_ABORT) 
1564                         {
1565                         restore_flags(flags);
1566                         while (SCpnt->internal_timeout & IN_ABORT);
1567                         }
1568                 else
1569                         {       
1570                         SCpnt->internal_timeout |= IN_ABORT;
1571                         oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1572 
1573                         if ((SCpnt->flags & IS_RESETTING) && 
1574                             SCpnt->device->soft_reset) {
1575                           /* OK, this command must have died when we did the
1576                              reset.  The device itself must have lied. */
1577                           printk("Stale command on %d:%d appears to have died when"
1578                                  " the bus was reset\n", SCpnt->target, SCpnt->lun);
1579                         }
1580                         
1581                         restore_flags(flags);
1582                         if (!host->host_busy) {
1583                           SCpnt->internal_timeout &= ~IN_ABORT;
1584                           update_timeout(SCpnt, oldto);
1585                           return 0;
1586                         }
1587                         printk("scsi : aborting command due to timeout : pid %lu, scsi%d, id %d, lun %d ",
1588                                SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->target, (int) 
1589                                SCpnt->lun);
1590                         print_command (SCpnt->cmnd);
1591                         if (SCpnt->request.dev == -1 || pid != SCpnt->pid)
1592                           return 0;
1593                         SCpnt->abort_reason = why;
1594                         switch(host->hostt->abort(SCpnt)) {
1595                           /* We do not know how to abort.  Try waiting another
1596                              time increment and see if this helps. Set the
1597                              WAS_TIMEDOUT flag set so we do not try this twice
1598                              */
1599                         case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1600                                                  this is too severe */
1601                         case SCSI_ABORT_SNOOZE:
1602                           if(why == DID_TIME_OUT) {
1603                             save_flags(flags);
1604                             cli();
1605                             SCpnt->internal_timeout &= ~IN_ABORT;
1606                             if(SCpnt->flags & WAS_TIMEDOUT) {
1607                               restore_flags(flags);
1608                               return 1; /* Indicate we cannot handle this.
1609                                            We drop down into the reset handler
1610                                            and try again */
1611                             } else {
1612                               SCpnt->flags |= WAS_TIMEDOUT;
1613                               oldto = SCpnt->timeout_per_command;
1614                               update_timeout(SCpnt, oldto);
1615                             }
1616                             restore_flags(flags);
1617                           }
1618                           return 0;
1619                         case SCSI_ABORT_PENDING:
1620                           if(why != DID_TIME_OUT) {
1621                             save_flags(flags);
1622                             cli();
1623                             update_timeout(SCpnt, oldto);
1624                             restore_flags(flags);
1625                           }
1626                           return 0;
1627                         case SCSI_ABORT_SUCCESS:
1628                           /* We should have already aborted this one.  No
1629                              need to adjust timeout */
1630                         case SCSI_ABORT_NOT_RUNNING:
1631                           SCpnt->internal_timeout &= ~IN_ABORT;
1632                           update_timeout(SCpnt, 0);
1633                           return 0;
1634                         case SCSI_ABORT_ERROR:
1635                         default:
1636                           SCpnt->internal_timeout &= ~IN_ABORT;
1637                           return 1;
1638                         }
1639                       }
1640               } 
1641       }
1642      
1643 int scsi_reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1644         {
1645         int temp, oldto;
1646         unsigned long flags;
1647         Scsi_Cmnd * SCpnt1;
1648         struct Scsi_Host * host = SCpnt->host;
1649         
1650 #ifdef DEBUG
1651         printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
1652 #endif
1653         while (1) {
1654                 save_flags(flags);
1655                 cli();
1656                 if (SCpnt->internal_timeout & IN_RESET)
1657                         {
1658                         restore_flags(flags);
1659                         while (SCpnt->internal_timeout & IN_RESET);
1660                         }
1661                 else
1662                         {
1663                         SCpnt->internal_timeout |= IN_RESET;
1664                         oldto = update_timeout(SCpnt, RESET_TIMEOUT);   
1665                                         
1666                         if (host->host_busy)
1667                                 {       
1668                                 restore_flags(flags);
1669                                 SCpnt1 = host->host_queue;
1670                                 while(SCpnt1) {
1671                                   if (SCpnt1->request.dev > 0) {
1672 #if 0                             
1673                                     if (!(SCpnt1->flags & IS_RESETTING) && 
1674                                       !(SCpnt1->internal_timeout & IN_ABORT))
1675                                     scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
1676 #endif
1677                                     SCpnt1->flags |= IS_RESETTING;
1678                                   }
1679                                   SCpnt1 = SCpnt1->next;
1680                                 };
1681 
1682                                 temp = host->hostt->reset(SCpnt);       
1683                                 }                               
1684                         else
1685                                 {
1686                                 host->host_busy++;
1687 
1688                                 if (host->block) {
1689                                    host_active = host;
1690                                    host_next = NULL;
1691                                    }
1692         
1693                                 restore_flags(flags);
1694                                 temp = host->hostt->reset(SCpnt);
1695                                 host->last_reset = jiffies;
1696                                 host->host_busy--;
1697 
1698                                 }
1699 
1700                         if (host->block && host->host_busy == 0) {
1701                            host_active = NULL;
1702                            host_next = NULL;
1703                            restart_all();
1704                            }
1705 
1706 
1707 #ifdef DEBUG
1708                         printk("scsi reset function returned %d\n", temp);
1709 #endif
1710                         switch(temp) {
1711                         case SCSI_RESET_SUCCESS:
1712                           save_flags(flags);
1713                           cli();
1714                           SCpnt->internal_timeout &= ~IN_RESET;
1715                           update_timeout(SCpnt, oldto);
1716                           restore_flags(flags);
1717                           return 0;
1718                         case SCSI_RESET_PENDING:
1719                           return 0;
1720                         case SCSI_RESET_PUNT:
1721                         case SCSI_RESET_WAKEUP:
1722                           SCpnt->internal_timeout &= ~IN_RESET;
1723                           scsi_request_sense (SCpnt);
1724                           return 0;
1725                         case SCSI_RESET_SNOOZE:                   
1726                           /* In this case, we set the timeout field to 0
1727                              so that this command does not time out any more,
1728                              and we return 1 so that we get a message on the
1729                              screen. */
1730                           save_flags(flags);
1731                           cli();
1732                           SCpnt->internal_timeout &= ~IN_RESET;
1733                           update_timeout(SCpnt, 0);
1734                           restore_flags(flags);
1735                           /* If you snooze, you lose... */
1736                         case SCSI_RESET_ERROR:
1737                         default:
1738                           return 1;
1739                         }
1740         
1741                         return temp;    
1742                         }
1743                 }
1744         }
1745                          
1746 
1747 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1748         {
1749         /*
1750                 We must not enter update_timeout with a timeout condition still pending.
1751         */
1752 
1753         int timed_out, pid;
1754         unsigned long flags;
1755         struct Scsi_Host * host;
1756         Scsi_Cmnd * SCpnt = NULL;
1757 
1758         do      {       
1759                 save_flags(flags);
1760                 cli();
1761 
1762                 update_timeout(NULL, 0);
1763         /*
1764                 Find all timers such that they have 0 or negative (shouldn't happen)
1765                 time remaining on them.
1766         */
1767                         
1768                 timed_out = 0;
1769                 for(host = scsi_hostlist; host; host = host->next) {
1770                   for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1771                     if (SCpnt->timeout == -1)
1772                       {
1773                         SCpnt->timeout = 0;
1774                         pid = SCpnt->pid;
1775                         restore_flags(flags);
1776                         scsi_times_out(SCpnt, pid);
1777                         ++timed_out; 
1778                         save_flags(flags);
1779                         cli();
1780                       }
1781                 };
1782               } while (timed_out);      
1783         restore_flags(flags);
1784       }
1785 
1786 /*
1787         The strategy is to cause the timer code to call scsi_times_out()
1788         when the soonest timeout is pending.  
1789         The arguments are used when we are queueing a new command, because
1790         we do not want to subtract the time used from this time, but when we
1791         set the timer, we want to take this value into account.
1792 */
1793         
1794 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1795         {
1796         unsigned int least, used;
1797         unsigned int oldto;
1798         unsigned long flags;
1799         struct Scsi_Host * host;
1800         Scsi_Cmnd * SCpnt = NULL;
1801 
1802         save_flags(flags);
1803         cli();
1804 
1805 /* 
1806         Figure out how much time has passed since the last time the timeouts 
1807         were updated 
1808 */
1809         used = (time_start) ? (jiffies - time_start) : 0;
1810 
1811 /*
1812         Find out what is due to timeout soonest, and adjust all timeouts for
1813         the amount of time that has passed since the last time we called 
1814         update_timeout. 
1815 */
1816         
1817         oldto = 0;
1818 
1819         if(SCset){
1820           oldto = SCset->timeout - used;
1821           SCset->timeout = timeout + used;
1822         };
1823 
1824         least = 0xffffffff;
1825 
1826         for(host = scsi_hostlist; host; host = host->next)
1827           for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1828             if (SCpnt->timeout > 0) {
1829               SCpnt->timeout -= used;
1830               if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
1831               if(SCpnt->timeout > 0 && SCpnt->timeout < least)
1832                 least = SCpnt->timeout;
1833             };
1834 
1835 /*
1836         If something is due to timeout again, then we will set the next timeout 
1837         interrupt to occur.  Otherwise, timeouts are disabled.
1838 */
1839         
1840         if (least != 0xffffffff)
1841                 {
1842                 time_start = jiffies;   
1843                 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;     
1844                 timer_active |= 1 << SCSI_TIMER;
1845                 }
1846         else
1847                 {
1848                 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1849                 timer_active &= ~(1 << SCSI_TIMER);
1850                 }       
1851         restore_flags(flags);
1852         return oldto;
1853         }               
1854 
1855 
1856 static unsigned char * dma_malloc_freelist = NULL;
1857 static int scsi_need_isa_bounce_buffers;
1858 static unsigned int dma_sectors = 0;
1859 unsigned int dma_free_sectors = 0;
1860 unsigned int need_isa_buffer = 0;
1861 static unsigned char ** dma_malloc_pages = NULL;
1862 #define MALLOC_PAGEBITS 12
1863 
1864 static int scsi_register_host(Scsi_Host_Template *);
1865 static void scsi_unregister_host(Scsi_Host_Template *);
1866 
1867 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1868 {
1869   unsigned int nbits, mask;
1870   unsigned long flags;
1871   int i, j;
1872   if((len & 0x1ff) || len > (1<<MALLOC_PAGEBITS))
1873     return NULL;
1874   
1875   save_flags(flags);
1876   cli();
1877   nbits = len >> 9;
1878   mask = (1 << nbits) - 1;
1879   
1880   for(i=0;i < (dma_sectors >> (MALLOC_PAGEBITS - 9)); i++)
1881     for(j=0; j<=(sizeof(*dma_malloc_freelist) * 8) - nbits; j++){
1882       if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1883         dma_malloc_freelist[i] |= (mask << j);
1884         restore_flags(flags);
1885         dma_free_sectors -= nbits;
1886 #ifdef DEBUG
1887         printk("SMalloc: %d %x ",len, dma_malloc_pages[i] + (j << 9));
1888 #endif
1889         return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
1890       };
1891     };
1892   restore_flags(flags);
1893   return NULL;  /* Nope.  No more */
1894 }
1895 
1896 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1897 {
1898   int page, sector, nbits, mask;
1899   long offset;
1900   unsigned long flags;
1901 
1902 #ifdef DEBUG
1903   printk("Sfree %x %d\n",obj, len);
1904 #endif
1905 
1906    offset = -1;
1907   for (page = 0; page < (dma_sectors >> 3); page++)
1908         if ((unsigned long) obj >= (unsigned long) dma_malloc_pages[page] &&
1909                 (unsigned long) obj < (unsigned long) dma_malloc_pages[page] + (1 << MALLOC_PAGEBITS))
1910                 {
1911                         offset = ((unsigned long) obj) - ((unsigned long)dma_malloc_pages[page]);
1912                         break;
1913                 }
1914                  
1915   if (page == (dma_sectors >> 3)) panic("Bad offset");
1916   sector = offset >> 9;
1917   if(sector >= dma_sectors) panic ("Bad page");
1918 
1919   sector = (offset >> 9) & (sizeof(*dma_malloc_freelist) * 8 - 1);
1920   nbits = len >> 9;
1921   mask = (1 << nbits) - 1;
1922 
1923   if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1924 
1925   save_flags(flags);
1926   cli();
1927   if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1928     panic("Trying to free unused memory");
1929 
1930   dma_free_sectors += nbits;
1931   dma_malloc_freelist[page] &= ~(mask << sector);
1932   restore_flags(flags);
1933   return 0;
1934 }
1935 
1936 
1937 /* These are special functions that can be used to obtain memory at boot time.
1938    They act line a malloc function, but they simply take memory from the
1939    pool */
1940 
1941 static unsigned long scsi_init_memory_start = 0;
1942 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
1943 
1944 void * scsi_init_malloc(unsigned int size, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
1945 {
1946   unsigned long retval;
1947   if(scsi_loadable_module_flag) {
1948     retval = (unsigned long) kmalloc(size, priority);
1949   } else {
1950     /*
1951      * Keep all memory aligned on 16-byte boundaries. Some host adaptors
1952      * (e.g. BusLogic BT-445S) require DMA buffers to be aligned that way.
1953      */
1954     size = (size + 15) & ~15;
1955     retval = scsi_init_memory_start;
1956     scsi_init_memory_start += size;
1957   }
1958   return (void *) retval;
1959 }
1960 
1961 
1962 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1963 { /* FIXME - not right.  We need to compare addresses to see whether this was
1964      kmalloc'd or not */
1965   if((unsigned long) ptr > scsi_init_memory_start) {
1966     kfree(ptr);
1967   } else {
1968     size = (size + 15) & ~15; /* Use the same alignment as scsi_init_malloc(). */
1969     if(((unsigned long) ptr) + size == scsi_init_memory_start)
1970       scsi_init_memory_start = (unsigned long) ptr;
1971   }
1972 }
1973 
1974 /*
1975         scsi_dev_init() is our initialization routine, which in turn calls host 
1976         initialization, bus scanning, and sd/st initialization routines.  It 
1977         should be called from main().
1978 */
1979 
1980 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1981         {
1982         struct Scsi_Host * host = NULL;
1983         Scsi_Device * SDpnt;
1984         struct Scsi_Host * shpnt;
1985         struct Scsi_Device_Template * sdtpnt;
1986         Scsi_Cmnd * SCpnt;
1987         int i;
1988 #ifdef FOO_ON_YOU
1989         return;
1990 #endif  
1991 
1992         /* Init a few things so we can "malloc" memory. */
1993         scsi_loadable_module_flag = 0;
1994         /* Align everything on 16-byte boundaries. */
1995         scsi_init_memory_start = (memory_start + 15) & ~ 15;
1996 
1997         timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1998         timer_table[SCSI_TIMER].expires = 0;
1999 
2000         /* initialize all hosts */
2001         scsi_init(); 
2002                                 
2003         scsi_devices = (Scsi_Device *) NULL;
2004 
2005         for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2006           scan_scsis(shpnt);           /* scan for scsi devices */
2007 
2008         printk("scsi : detected ");
2009         for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2010           if (sdtpnt->dev_noticed && sdtpnt->name)
2011             printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2012             (sdtpnt->dev_noticed != 1) ? "s" : "");
2013         printk("total.\n");
2014   
2015         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2016           if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2017 
2018         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2019           int j;
2020           SDpnt->scsi_request_fn = NULL;
2021           for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2022               if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2023 
2024           if(SDpnt->attached){
2025             for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2026               SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2027               SCpnt->host = SDpnt->host;
2028               SCpnt->device = SDpnt;
2029               SCpnt->target = SDpnt->id;
2030               SCpnt->lun = SDpnt->lun;
2031               SCpnt->request.dev = -1; /* Mark not busy */
2032               SCpnt->use_sg = 0;
2033               SCpnt->old_use_sg = 0;
2034               SCpnt->old_cmd_len = 0;
2035               SCpnt->timeout = 0;
2036               SCpnt->underflow = 0;
2037               SCpnt->transfersize = 0;
2038               SCpnt->host_scribble = NULL;
2039               host = SDpnt->host;
2040               if(host->host_queue)
2041                 host->host_queue->prev = SCpnt;
2042               SCpnt->next = host->host_queue;
2043               SCpnt->prev = NULL;
2044               host->host_queue = SCpnt;
2045             };
2046           };
2047         };
2048 
2049         if (scsi_devicelist)
2050           dma_sectors = 16;  /* Base value we use */
2051 
2052         if (memory_end-1 > ISA_DMA_THRESHOLD)
2053           scsi_need_isa_bounce_buffers = 1;
2054         else
2055           scsi_need_isa_bounce_buffers = 0;
2056 
2057         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2058           host = SDpnt->host;
2059           
2060           if(SDpnt->type != TYPE_TAPE)
2061             dma_sectors += ((host->sg_tablesize * 
2062                              sizeof(struct scatterlist) + 511) >> 9) * 
2063                                host->cmd_per_lun;
2064           
2065           if(host->unchecked_isa_dma &&
2066              memory_end - 1 > ISA_DMA_THRESHOLD &&
2067              SDpnt->type != TYPE_TAPE) {
2068             dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2069               host->cmd_per_lun;
2070             need_isa_buffer++;
2071           };
2072         };
2073 
2074         dma_sectors = (dma_sectors + 15) & 0xfff0;
2075         dma_free_sectors = dma_sectors;  /* This must be a multiple of 16 */
2076 
2077         dma_malloc_freelist = (unsigned char *) 
2078           scsi_init_malloc(dma_sectors >> 3, GFP_ATOMIC);
2079         memset(dma_malloc_freelist, 0, dma_sectors >> 3);
2080 
2081         dma_malloc_pages = (unsigned char **) 
2082           scsi_init_malloc(dma_sectors >> 1, GFP_ATOMIC);
2083         memset(dma_malloc_pages, 0, dma_sectors >> 1);
2084  
2085         for(i=0; i< dma_sectors >> 3; i++)
2086           dma_malloc_pages[i] = (unsigned char *) 
2087             scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2088 
2089 
2090         /* OK, now we finish the initialization by doing spin-up, read
2091            capacity, etc, etc */
2092         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2093           if(sdtpnt->finish && sdtpnt->nr_dev)
2094             (*sdtpnt->finish)();
2095         
2096         scsi_loadable_module_flag = 1;
2097         return scsi_init_memory_start;
2098         }
2099 
2100 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
2101 {
2102         int i;
2103 
2104         printk("  Vendor: ");
2105         for (i = 8; i < 16; i++)
2106                 {
2107                 if (data[i] >= 0x20 && i < data[4] + 5)
2108                         printk("%c", data[i]);
2109                 else
2110                         printk(" ");
2111                 }
2112 
2113         printk("  Model: ");
2114         for (i = 16; i < 32; i++)
2115                 {
2116                 if (data[i] >= 0x20 && i < data[4] + 5)
2117                         printk("%c", data[i]);
2118                 else
2119                         printk(" ");
2120                 }
2121 
2122         printk("  Rev: ");
2123         for (i = 32; i < 36; i++)
2124                 {
2125                 if (data[i] >= 0x20 && i < data[4] + 5)
2126                         printk("%c", data[i]);
2127                 else
2128                         printk(" ");
2129                 }
2130 
2131         printk("\n");
2132 
2133         i = data[0] & 0x1f;
2134 
2135         printk("  Type:   %s ",
2136                i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2137         printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2138         if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2139           printk(" CCS\n");
2140         else
2141           printk("\n");
2142 }
2143 
2144 /*
2145  * This entry point should be called by a loadable module if it is trying
2146  * add a low level scsi driver to the system.
2147  */
2148 static int scsi_register_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2149 {
2150   int pcount;
2151   struct Scsi_Host * shpnt;
2152   struct Scsi_Host * host = NULL;
2153   unsigned long flags;
2154   Scsi_Device * SDpnt;
2155   Scsi_Cmnd * SCpnt;
2156   struct Scsi_Device_Template * sdtpnt;
2157   int j, i;
2158   const char * name;
2159 
2160   if (tpnt->next || !tpnt->detect) return 1;  /* Must be already loaded, or
2161                                                no detect routine available */
2162   pcount = next_scsi_host;
2163   if ((tpnt->present = tpnt->detect(tpnt)))
2164     {
2165       if(pcount == next_scsi_host) {
2166         if(tpnt->present > 1) {
2167           printk("Failure to register low-level scsi driver");
2168           scsi_unregister_host(tpnt);
2169           return 1;
2170         }
2171         /* The low-level driver failed to register a driver.  We
2172            can do this now. */
2173         scsi_register(tpnt,0);
2174       }
2175       tpnt->next = scsi_hosts; /* Add to the linked list */
2176       scsi_hosts = tpnt;
2177 
2178       for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2179         if(shpnt->hostt == tpnt)
2180           {
2181             if(tpnt->info)
2182               name = tpnt->info(shpnt);
2183             else
2184               name = tpnt->name;
2185             printk ("scsi%d : %s\n", /* And print a little message */
2186                     shpnt->host_no, name);
2187           }
2188       /* The next step is to call scan_scsis here.  This generates the
2189          Scsi_Devices entries */
2190 
2191       for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2192         if(shpnt->hostt == tpnt) scan_scsis(shpnt);
2193 
2194       for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2195         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2196 
2197       /* Next we create the Scsi_Cmnd structures for this host */
2198 
2199       for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2200         if(SDpnt->host->hostt == tpnt)
2201           {
2202             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2203               if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2204             if(SDpnt->attached){
2205               for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2206                 SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2207                 SCpnt->host = SDpnt->host;
2208                 SCpnt->device = SDpnt;
2209                 SCpnt->target = SDpnt->id;
2210                 SCpnt->lun = SDpnt->lun;
2211                 SCpnt->request.dev = -1; /* Mark not busy */
2212                 SCpnt->request.sem = NULL;
2213                 SCpnt->use_sg = 0;
2214                 SCpnt->old_use_sg = 0;
2215                 SCpnt->underflow = 0;
2216                 SCpnt->timeout = 0;
2217                 SCpnt->transfersize = 0;
2218                 SCpnt->host_scribble = NULL;
2219                 host = SDpnt->host;
2220                 SCpnt->next = host->host_queue;
2221                 SCpnt->prev = NULL;
2222                 host->host_queue = SCpnt;
2223                 if(host->host_queue)
2224                   host->host_queue->prev = SCpnt;
2225               };
2226             };
2227           }
2228         /* Next, check to see if we need to extend the DMA buffer pool */
2229       {
2230           unsigned char * new_dma_malloc_freelist = NULL;
2231           unsigned int new_dma_sectors = 0;
2232           unsigned int new_need_isa_buffer = 0;
2233           unsigned char ** new_dma_malloc_pages = NULL;
2234           
2235           if (scsi_devicelist)
2236             new_dma_sectors = 16;  /* Base value we use */
2237           
2238           for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2239             host = SDpnt->host;
2240             
2241             if(SDpnt->type != TYPE_TAPE)
2242               new_dma_sectors += ((host->sg_tablesize * 
2243                                    sizeof(struct scatterlist) + 511) >> 9) * 
2244                                      host->cmd_per_lun;
2245             
2246             if(host->unchecked_isa_dma &&
2247                scsi_need_isa_bounce_buffers &&
2248                SDpnt->type != TYPE_TAPE) {
2249               new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2250                 host->cmd_per_lun;
2251               new_need_isa_buffer++;
2252             };
2253           };
2254           
2255           new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2256 
2257           new_dma_malloc_freelist = (unsigned char *) 
2258             scsi_init_malloc(new_dma_sectors >> 3, GFP_ATOMIC);
2259           memset(new_dma_malloc_freelist, 0, new_dma_sectors >> 3);
2260           
2261           new_dma_malloc_pages = (unsigned char **) 
2262             scsi_init_malloc(new_dma_sectors >> 1, GFP_ATOMIC);
2263           memset(new_dma_malloc_pages, 0, new_dma_sectors >> 1);
2264           
2265           for(i=dma_sectors >> 3; i< new_dma_sectors >> 3; i++)
2266             new_dma_malloc_pages[i] = (unsigned char *) 
2267               scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2268           
2269 
2270           /* When we dick with the actual DMA list, we need to protect things */
2271 
2272           save_flags(flags);
2273           cli();
2274           memcpy(new_dma_malloc_freelist, dma_malloc_freelist, dma_sectors >> 3);
2275           scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2276           dma_malloc_freelist = new_dma_malloc_freelist;
2277           
2278           memcpy(new_dma_malloc_pages, dma_malloc_pages, dma_sectors >> 1);
2279           scsi_init_free((char *) dma_malloc_pages, dma_sectors>>1);
2280           
2281           dma_free_sectors += new_dma_sectors - dma_sectors;
2282           dma_malloc_pages = new_dma_malloc_pages;
2283           dma_sectors = new_dma_sectors;
2284           need_isa_buffer = new_need_isa_buffer;
2285           restore_flags(flags);
2286 
2287           
2288         }
2289       /* This does any final handling that is required. */
2290       for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2291         if(sdtpnt->finish) (*sdtpnt->finish)();
2292     }
2293   return 0;
2294 }
2295 
2296 /*
2297  * Similarly, this entry point should be called by a loadable module if it
2298  * is trying to remove a low level scsi driver from the system.
2299  */
2300 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2301 {  
2302   Scsi_Host_Template * SHT, *SHTp;
2303   Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
2304   Scsi_Cmnd * SCpnt;
2305   unsigned long flags;
2306   struct Scsi_Device_Template * sdtpnt;
2307   struct Scsi_Host * shpnt, *sh1;
2308   int pcount;
2309 
2310   /* First verify that this host adapter is completely free with no pending
2311      commands */
2312 
2313   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2314     if(sdpnt->host->hostt == tpnt && *sdpnt->host->hostt->usage_count) return;
2315 
2316   for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2317     {
2318       if (shpnt->hostt != tpnt) continue;
2319       for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2320         {
2321           save_flags(flags);
2322           cli();
2323           if(SCpnt->request.dev != -1) {
2324             restore_flags(flags);
2325             for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2326               if(SCpnt->request.dev == 0xffe0) SCpnt->request.dev = -1;
2327             printk("Device busy???\n");
2328             return;
2329           }
2330           SCpnt->request.dev = 0xffe0;  /* Mark as busy */
2331         }
2332     }
2333   /* Next we detach the high level drivers from the Scsi_Device structures */
2334 
2335   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2336     if(sdpnt->host->hostt == tpnt)
2337       {
2338         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2339           if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
2340         /* If something still attached, punt */
2341         if (sdpnt->attached) {
2342           printk("Attached usage count = %d\n", sdpnt->attached);
2343           return;
2344         }
2345       }
2346   
2347   /* Next we free up the Scsi_Cmnd structures for this host */
2348 
2349   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2350     if(sdpnt->host->hostt == tpnt)
2351       while (sdpnt->host->host_queue) {
2352         SCpnt = sdpnt->host->host_queue->next;
2353         scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
2354         sdpnt->host->host_queue = SCpnt;
2355         if (SCpnt) SCpnt->prev = NULL;
2356       }
2357   
2358   /* Next free up the Scsi_Device structures for this host */
2359 
2360   sdppnt = NULL;
2361   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
2362     {
2363       sdpnt1 = sdpnt->next;
2364       if (sdpnt->host->hostt == tpnt) {
2365         if (sdppnt)
2366           sdppnt->next = sdpnt->next;
2367         else
2368           scsi_devices = sdpnt->next;
2369         scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
2370       } else
2371         sdppnt = sdpnt;
2372     }
2373 
2374   /* Next we go through and remove the instances of the individual hosts
2375      that were detected */
2376 
2377   shpnt = scsi_hostlist;
2378   while(shpnt) { 
2379    sh1 = shpnt->next;
2380     if(shpnt->hostt == tpnt) {
2381       if(shpnt->loaded_as_module) {
2382         pcount = next_scsi_host;
2383         if(tpnt->release)
2384           (*tpnt->release)(shpnt);
2385         else {
2386           /* This is the default case for the release function.  It should do the right
2387              thing for most correctly written host adapters. */
2388           if (shpnt->irq) free_irq(shpnt->irq);
2389           if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
2390           if (shpnt->io_port && shpnt->n_io_port) 
2391             release_region(shpnt->io_port, shpnt->n_io_port);
2392         }
2393         if(pcount == next_scsi_host) scsi_unregister(shpnt);
2394         tpnt->present--;
2395       }
2396     }
2397     shpnt = sh1;
2398   }
2399 
2400   /* There were some hosts that were loaded at boot time, so we cannot
2401      do any more than this */
2402   if (tpnt->present) return;
2403 
2404   /* OK, this is the very last step.  Remove this host adapter from the
2405      linked list. */
2406   for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
2407     if(SHT == tpnt) {
2408       if(SHTp)
2409         SHTp->next = SHT->next;
2410       else
2411         scsi_hosts = SHT->next;
2412       SHT->next = NULL;
2413       break;
2414     }
2415 }
2416 
2417 int scsi_register_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2418 {
2419   switch(module_type){
2420   case MODULE_SCSI_HA:
2421     return scsi_register_host((Scsi_Host_Template *) ptr);
2422     /* The rest of these are not yet implemented */
2423 
2424     /* Load constants.o */
2425   case MODULE_SCSI_CONST:
2426 
2427     /* Load specialized ioctl handler for some device.  Intended for cdroms that
2428        have non-SCSI2 audio command sets. */
2429   case MODULE_SCSI_IOCTL:
2430 
2431     /* Load upper level device handler of some kind */
2432   case MODULE_SCSI_DEV:
2433   default:
2434     return 1;
2435   }
2436 }
2437 
2438 void scsi_unregister_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2439 {
2440   switch(module_type) {
2441   case MODULE_SCSI_HA:
2442     scsi_unregister_host((Scsi_Host_Template *) ptr);
2443     break;
2444     /* The rest of these are not yet implemented. */
2445   case MODULE_SCSI_CONST:
2446   case MODULE_SCSI_IOCTL:
2447   case MODULE_SCSI_DEV:
2448   default:
2449   }
2450   return;
2451 }
2452 
2453 #ifdef DEBUG_TIMEOUT
2454 static void 
2455 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2456 {
2457   int i;
2458   struct Scsi_Host * shpnt;
2459   Scsi_Cmnd * SCpnt;
2460   printk("Dump of scsi parameters:\n");
2461   i = 0;
2462   for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2463     for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2464       {
2465         /*  (0) 0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
2466         printk("(%d) %d:%d:%d (%4.4x %ld %ld %ld %ld) (%d %d %x) (%d %d %d) %x %x %x\n",
2467                i++, SCpnt->host->host_no,
2468                SCpnt->target,
2469                SCpnt->lun,
2470                SCpnt->request.dev,
2471                SCpnt->request.sector,
2472                SCpnt->request.nr_sectors,
2473                SCpnt->request.current_nr_sectors,
2474                SCpnt->use_sg,
2475                SCpnt->retries,
2476                SCpnt->allowed,
2477                SCpnt->flags,
2478                SCpnt->timeout_per_command,
2479                SCpnt->timeout,
2480                SCpnt->internal_timeout,
2481                SCpnt->cmnd[0],
2482                SCpnt->sense_buffer[2],
2483                SCpnt->result);
2484       };
2485   printk("wait_for_request = %p\n", wait_for_request);
2486   /* Now dump the request lists for each block device */
2487   printk("Dump of pending block device requests\n");
2488   for(i=0; i<MAX_BLKDEV; i++)
2489     if(blk_dev[i].current_request)
2490       {
2491         struct request * req;
2492         printk("%d: ", i);
2493         req = blk_dev[i].current_request;
2494         while(req) {
2495           printk("(%x %d %ld %ld %ld) ",
2496                  req->dev,
2497                  req->cmd,
2498                  req->sector,
2499                  req->nr_sectors,
2500                  req->current_nr_sectors);
2501           req = req->next;
2502         }
2503         printk("\n");
2504       }
2505 }
2506 #endif

/* [previous][next][first][last][top][bottom][index][help] */