root/drivers/scsi/scsi.c

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

DEFINITIONS

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

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

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