root/drivers/scsi/scsi.c

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

DEFINITIONS

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

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

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