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

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