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, int pid);
  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, int pid)
     /* [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, pid))
 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         }
 841 
 842 
 843 
 844 /*
 845         scsi_do_cmd sends all the commands out to the low-level driver.  It 
 846         handles the specifics required for each low level driver - ie queued 
 847         or non queued.  It also prevents conflicts when different high level 
 848         drivers go for the same host at the same time.
 849 */
 850 
 851 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd , 
     /* [previous][next][first][last][top][bottom][index][help] */
 852                   void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
 853                   int timeout, int retries 
 854                    )
 855         {
 856         struct Scsi_Host * host = SCpnt->host;
 857 
 858 #ifdef DEBUG
 859         {
 860         int i;  
 861         int target = SCpnt->target;
 862         printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
 863                 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
 864                 "command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
 865         for (i = 0; i < 10; ++i)
 866                 printk ("%02x  ", ((unsigned char *) cmnd)[i]); 
 867         printk("\n");
 868       };
 869 #endif
 870         
 871         if (!host)
 872                 {
 873                 panic ("Invalid or not present host.\n");
 874                 }
 875 
 876         
 877 /*
 878         We must prevent reentrancy to the lowlevel host driver.  This prevents 
 879         it - we enter a loop until the host we want to talk to is not busy.   
 880         Race conditions are prevented, as interrupts are disabled in between the
 881         time we check for the host being not busy, and the time we mark it busy
 882         ourselves.
 883 */
 884 
 885         SCpnt->pid = scsi_pid++; 
 886 
 887         while (1==1){
 888           cli();
 889           if (host->can_queue
 890               && host->host_busy >= host->can_queue)
 891             {
 892               sti();
 893               SCSI_SLEEP(&host->host_wait, 
 894                          (host->host_busy >= host->can_queue));
 895             } else {
 896               host->host_busy++;
 897               if (host->block) {
 898                 struct Scsi_Host * block;
 899                 for(block = host->block; block != host; block = block->block)
 900                   block->host_busy |= SCSI_HOST_BLOCK;
 901               }
 902               sti();
 903               break;
 904             };
 905       };
 906 /*
 907         Our own function scsi_done (which marks the host as not busy, disables 
 908         the timeout counter, etc) will be called by us or by the 
 909         scsi_hosts[host].queuecommand() function needs to also call
 910         the completion function for the high level driver.
 911 
 912 */
 913 
 914         memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
 915 #if 0
 916         SCpnt->host = host;
 917         SCpnt->target = target;
 918         SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
 919 #endif
 920         SCpnt->bufflen = bufflen;
 921         SCpnt->buffer = buffer;
 922         SCpnt->flags=0;
 923         SCpnt->retries=0;
 924         SCpnt->allowed=retries;
 925         SCpnt->done = done;
 926         SCpnt->timeout_per_command = timeout;
 927                                 
 928         memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
 929         /* Zero the sense buffer.  Some host adapters automatically request
 930            sense on error.  0 is not a valid sense code.  */
 931         memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
 932         SCpnt->request_buffer = buffer;
 933         SCpnt->request_bufflen = bufflen;
 934         SCpnt->old_use_sg = SCpnt->use_sg;
 935         if (SCpnt->cmd_len == 0)
 936                 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
 937         SCpnt->old_cmd_len = SCpnt->cmd_len;
 938 
 939         /* Start the timer ticking.  */
 940 
 941         SCpnt->internal_timeout = 0;
 942         SCpnt->abort_reason = 0;
 943         internal_cmnd (SCpnt);
 944 
 945 #ifdef DEBUG
 946         printk ("Leaving scsi_do_cmd()\n");
 947 #endif
 948         }
 949 
 950 
 951 
 952 /*
 953         The scsi_done() function disables the timeout timer for the scsi host, 
 954         marks the host as not busy, and calls the user specified completion 
 955         function for that host's current command.
 956 */
 957 
 958 static void reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 959 {
 960 #ifdef DEBUG
 961         printk("scsi: reset(%d)\n", SCpnt->host->host_no);
 962 #endif
 963         
 964         SCpnt->flags |= (WAS_RESET | IS_RESETTING);
 965         scsi_reset(SCpnt);
 966         
 967 #ifdef DEBUG
 968         printk("performing request sense\n");
 969 #endif
 970         
 971 #if 0  /* FIXME - remove this when done */
 972         if(SCpnt->flags & NEEDS_JUMPSTART) {
 973           SCpnt->flags &= ~NEEDS_JUMPSTART;
 974           scsi_request_sense (SCpnt);
 975         };
 976 #endif
 977 }
 978         
 979         
 980 
 981 static int check_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 982         {
 983   /* If there is no sense information, request it.  If we have already
 984      requested it, there is no point in asking again - the firmware must be
 985      confused. */
 986   if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
 987     if(!(SCpnt->flags & ASKED_FOR_SENSE))
 988       return SUGGEST_SENSE;
 989     else
 990       return SUGGEST_RETRY;
 991       }
 992   
 993   SCpnt->flags &= ~ASKED_FOR_SENSE;
 994 
 995 #ifdef DEBUG_INIT
 996         printk("scsi%d : ", SCpnt->host->host_no);
 997         print_sense("", SCpnt);
 998         printk("\n");
 999 #endif
1000                 if (SCpnt->sense_buffer[2] &0xe0)
1001                   return SUGGEST_ABORT;
1002 
1003                 switch (SCpnt->sense_buffer[2] & 0xf)
1004                 {
1005                 case NO_SENSE:
1006                         return 0;
1007                 case RECOVERED_ERROR:
1008                         if (SCpnt->device->type == TYPE_TAPE)
1009                           return SUGGEST_IS_OK;
1010                         else
1011                           return 0;
1012 
1013                 case ABORTED_COMMAND:
1014                         return SUGGEST_RETRY;   
1015                 case NOT_READY:
1016                 case UNIT_ATTENTION:
1017                         return SUGGEST_ABORT;
1018 
1019                 /* these three are not supported */     
1020                 case COPY_ABORTED:
1021                 case VOLUME_OVERFLOW:
1022                 case MISCOMPARE:
1023         
1024                 case MEDIUM_ERROR:
1025                         return SUGGEST_REMAP;
1026                 case BLANK_CHECK:
1027                 case DATA_PROTECT:
1028                 case HARDWARE_ERROR:
1029                 case ILLEGAL_REQUEST:
1030                 default:
1031                         return SUGGEST_ABORT;
1032                 }
1033               }
1034 
1035 /* This function is the mid-level interrupt routine, which decides how
1036  *  to handle error conditions.  Each invocation of this function must
1037  *  do one and *only* one of the following:
1038  *
1039  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
1040  *      normal completion, and indicates that the handling for this
1041  *      request is complete.
1042  *  (2) Call internal_cmnd to requeue the command.  This will result in
1043  *      scsi_done being called again when the retry is complete.
1044  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
1045  *      more information about the error condition.  When the information
1046  *      is available, scsi_done will be called again.
1047  *  (4) Call reset().  This is sort of a last resort, and the idea is that
1048  *      this may kick things loose and get the drive working again.  reset()
1049  *      automatically calls scsi_request_sense, and thus scsi_done will be
1050  *      called again once the reset is complete.
1051  *
1052  *      If none of the above actions are taken, the drive in question
1053  * will hang. If more than one of the above actions are taken by
1054  * scsi_done, then unpredictable behavior will result.
1055  */
1056 static void scsi_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1057         {
1058         int status=0;
1059         int exit=0;
1060         int checked;
1061         int oldto;
1062         struct Scsi_Host * host = SCpnt->host;
1063         int result = SCpnt->result;
1064         oldto = update_timeout(SCpnt, 0);
1065 
1066 #ifdef DEBUG_TIMEOUT
1067         if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1068                           result, SCpnt->target, SCpnt->lun);
1069 #endif
1070 
1071         /* If we requested an abort, (and we got it) then fix up the return
1072            status to say why */
1073         if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1074           SCpnt->result = result = (result & 0xff00ffff) | 
1075             (SCpnt->abort_reason << 16);
1076 
1077 
1078 #define FINISHED 0
1079 #define MAYREDO  1
1080 #define REDO     3
1081 #define PENDING  4
1082 
1083 #ifdef DEBUG
1084         printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1085 #endif
1086 
1087         if(SCpnt->flags & WAS_SENSE)
1088         {
1089                 SCpnt->use_sg = SCpnt->old_use_sg;
1090                 SCpnt->cmd_len = SCpnt->old_cmd_len;
1091         }
1092 
1093         switch (host_byte(result))      
1094         {
1095         case DID_OK:
1096                 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1097                         /* Failed to obtain sense information */
1098                         {
1099                         SCpnt->flags &= ~WAS_SENSE;
1100                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1101 
1102                         if (!(SCpnt->flags & WAS_RESET))
1103                                 {
1104                                 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n", 
1105                                         SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
1106                                 reset(SCpnt);
1107                                 return;
1108                                 }
1109                         else
1110                                 {
1111                                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1112                                 status = FINISHED;
1113                                 }
1114                         }
1115                 else switch(msg_byte(result))
1116                         {
1117                         case COMMAND_COMPLETE:
1118                         switch (status_byte(result))
1119                         {
1120                         case GOOD:
1121                                 if (SCpnt->flags & WAS_SENSE)
1122                                         {
1123 #ifdef DEBUG
1124         printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1125 #endif
1126 
1127                                         SCpnt->flags &= ~WAS_SENSE;
1128                                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1129         
1130                                         switch (checked = check_sense(SCpnt))
1131                                         {
1132                                         case SUGGEST_SENSE:
1133                                         case 0: 
1134 #ifdef DEBUG
1135         printk("NO SENSE.  status = REDO\n");
1136 #endif
1137 
1138                                                 update_timeout(SCpnt, oldto);
1139                                                 status = REDO;
1140                                                 break;
1141                                         case SUGGEST_IS_OK:
1142                                                 break;
1143                                         case SUGGEST_REMAP:                     
1144                                         case SUGGEST_RETRY: 
1145 #ifdef DEBUG
1146         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1147 #endif
1148 
1149                                                 status = MAYREDO;
1150                                                 exit = DRIVER_SENSE | SUGGEST_RETRY;
1151                                                 break;
1152                                         case SUGGEST_ABORT:
1153 #ifdef DEBUG
1154         printk("SENSE SUGGEST ABORT - status = FINISHED");
1155 #endif
1156 
1157                                                 status = FINISHED;
1158                                                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
1159                                                 break;
1160                                         default:
1161                                                 printk ("Internal error %s %d \n", __FILE__, 
1162                                                         __LINE__);
1163                                         }                          
1164                                         }       
1165                                 else
1166                                         {
1167 #ifdef DEBUG
1168         printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1169 #endif
1170 
1171                                         exit =  DRIVER_OK;
1172                                         status = FINISHED;
1173                                         }
1174                                 break;  
1175 
1176                         case CHECK_CONDITION:
1177                                 switch (check_sense(SCpnt))
1178                                   {
1179                                   case 0: 
1180                                     update_timeout(SCpnt, oldto);
1181                                     status = REDO;
1182                                     break;
1183                                   case SUGGEST_REMAP:                   
1184                                   case SUGGEST_RETRY:
1185                                     status = MAYREDO;
1186                                     exit = DRIVER_SENSE | SUGGEST_RETRY;
1187                                     break;
1188                                   case SUGGEST_ABORT:
1189                                     status = FINISHED;
1190                                     exit =  DRIVER_SENSE | SUGGEST_ABORT;
1191                                     break;
1192                                   case SUGGEST_SENSE:
1193                                 scsi_request_sense (SCpnt);
1194                                 status = PENDING;
1195                                 break;          
1196                                   }
1197                                 break;          
1198                         
1199                         case CONDITION_GOOD:
1200                         case INTERMEDIATE_GOOD:
1201                         case INTERMEDIATE_C_GOOD:
1202                                 break;
1203                                 
1204                         case BUSY:
1205                                 update_timeout(SCpnt, oldto);
1206                                 status = REDO;
1207                                 break;
1208 
1209                         case RESERVATION_CONFLICT:
1210                                 printk("scsi%d : RESERVATION CONFLICT performing reset.\n", 
1211                                         SCpnt->host->host_no);
1212                                 reset(SCpnt);
1213                                 return;
1214 #if 0
1215                                 exit = DRIVER_SOFT | SUGGEST_ABORT;
1216                                 status = MAYREDO;
1217                                 break;
1218 #endif
1219                         default:
1220                                 printk ("Internal error %s %d \n"
1221                                         "status byte = %d \n", __FILE__, 
1222                                         __LINE__, status_byte(result));
1223                                 
1224                         }
1225                         break;
1226                         default:
1227                                 panic("scsi: unsupported message byte %d received\n", msg_byte(result)); 
1228                         }
1229                         break;
1230         case DID_TIME_OUT:      
1231 #ifdef DEBUG
1232         printk("Host returned DID_TIME_OUT - ");
1233 #endif
1234 
1235                 if (SCpnt->flags & WAS_TIMEDOUT)
1236                         {
1237 #ifdef DEBUG
1238         printk("Aborting\n");
1239 #endif  
1240                         exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1241                         }               
1242                 else 
1243                         {
1244 #ifdef DEBUG
1245                         printk ("Retrying.\n");
1246 #endif
1247                         SCpnt->flags  |= WAS_TIMEDOUT;
1248                         SCpnt->internal_timeout &= ~IN_ABORT;
1249                         status = REDO;
1250                         }
1251                 break;
1252         case DID_BUS_BUSY:
1253         case DID_PARITY:
1254                 status = REDO;
1255                 break;
1256         case DID_NO_CONNECT:
1257 #ifdef DEBUG
1258                 printk("Couldn't connect.\n");
1259 #endif
1260                 exit  = (DRIVER_HARD | SUGGEST_ABORT);
1261                 break;
1262         case DID_ERROR: 
1263                 status = MAYREDO;
1264                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1265                 break;
1266         case DID_BAD_TARGET:
1267         case DID_ABORT:
1268                 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1269                 break;  
1270         case DID_RESET:
1271                 if (SCpnt->flags & IS_RESETTING)
1272                         {
1273                         SCpnt->flags &= ~IS_RESETTING;
1274                         status = REDO;
1275                         break;
1276                         }
1277 
1278                 if(msg_byte(result) == GOOD &&
1279                       status_byte(result) == CHECK_CONDITION) {
1280                         switch (check_sense(SCpnt)) {
1281                         case 0: 
1282                             update_timeout(SCpnt, oldto);
1283                             status = REDO;
1284                             break;
1285                         case SUGGEST_REMAP:                     
1286                         case SUGGEST_RETRY:
1287                             status = MAYREDO;
1288                             exit = DRIVER_SENSE | SUGGEST_RETRY;
1289                             break;
1290                         case SUGGEST_ABORT:
1291                             status = FINISHED;
1292                             exit =  DRIVER_SENSE | SUGGEST_ABORT;
1293                             break;
1294                         case SUGGEST_SENSE:
1295                               scsi_request_sense (SCpnt);
1296                               status = PENDING;
1297                               break;
1298                         }
1299                 } else {
1300                 status=REDO;
1301                 exit = SUGGEST_RETRY;
1302                 }
1303                 break;
1304         default :               
1305                 exit = (DRIVER_ERROR | SUGGEST_DIE);
1306         }
1307 
1308         switch (status) 
1309                 {
1310                 case FINISHED:
1311                 case PENDING:
1312                         break;
1313                 case MAYREDO:
1314 
1315 #ifdef DEBUG
1316         printk("In MAYREDO, allowing %d retries, have %d\n",
1317                SCpnt->allowed, SCpnt->retries);
1318 #endif
1319 
1320                         if ((++SCpnt->retries) < SCpnt->allowed)
1321                         {
1322                         if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1323                             && !(SCpnt->flags & WAS_RESET))
1324                                 {
1325                                         printk("scsi%d : resetting for second half of retries.\n",
1326                                                 SCpnt->host->host_no);
1327                                         reset(SCpnt);
1328                                         break;
1329                                 }
1330 
1331                         }
1332                         else
1333                                 {
1334                                 status = FINISHED;
1335                                 break;
1336                                 }
1337                         /* fall through to REDO */
1338 
1339                 case REDO:
1340                         if (SCpnt->flags & WAS_SENSE)                   
1341                                 scsi_request_sense(SCpnt);      
1342                         else    
1343                           {
1344                             memcpy ((void *) SCpnt->cmnd,
1345                                     (void*) SCpnt->data_cmnd, 
1346                                     sizeof(SCpnt->data_cmnd));
1347                             SCpnt->request_buffer = SCpnt->buffer;
1348                             SCpnt->request_bufflen = SCpnt->bufflen;
1349                             SCpnt->use_sg = SCpnt->old_use_sg;
1350                             SCpnt->cmd_len = SCpnt->old_cmd_len;
1351                             internal_cmnd (SCpnt);
1352                           };
1353                         break;  
1354                 default: 
1355                         INTERNAL_ERROR;
1356                 }
1357 
1358         if (status == FINISHED) 
1359           {
1360 #ifdef DEBUG
1361                 printk("Calling done function - at address %08x\n", SCpnt->done);
1362 #endif
1363                 host->host_busy--; /* Indicate that we are free */
1364                 if (host->host_busy == 0 && host->block) {
1365                   struct Scsi_Host * block;
1366                   /*
1367                    * Now remove the locks for all of the related hosts.
1368                    */
1369                   for(block = host->block; block != host; block = block->block)
1370                       block->host_busy &= ~SCSI_HOST_BLOCK;
1371                   /*
1372                    * Now wake them up.  We do this in two separate stages to prevent
1373                    * race conditions.
1374                    */
1375                   for(block = host->block; block != host; block = block->block)
1376                       wake_up(&block->host_wait);
1377                 }
1378                 wake_up(&host->host_wait);
1379                 SCpnt->result = result | ((exit & 0xff) << 24);
1380                 SCpnt->use_sg = SCpnt->old_use_sg;
1381                 SCpnt->cmd_len = SCpnt->old_cmd_len;
1382                 SCpnt->done (SCpnt);
1383                 }
1384 
1385 
1386 #undef FINISHED
1387 #undef REDO
1388 #undef MAYREDO
1389 #undef PENDING
1390         }
1391 
1392 /*
1393         The scsi_abort function interfaces with the abort() function of the host
1394         we are aborting, and causes the current command to not complete.  The 
1395         caller should deal with any error messages or status returned on the 
1396         next call.
1397         
1398         This will not be called reentrantly for a given host.
1399 */
1400         
1401 /*
1402         Since we're nice guys and specified that abort() and reset()
1403         can be non-reentrant.  The internal_timeout flags are used for
1404         this.
1405 */
1406 
1407 
1408 int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
1409         {
1410         int oldto;
1411         struct Scsi_Host * host = SCpnt->host;
1412         
1413         while(1)        
1414                 {
1415                 cli();
1416 
1417                 /*
1418                  * Protect against races here.  If the command is done, or we are
1419                  * on a different command forget it.
1420                  */
1421                 if (SCpnt->request.dev == -1 || pid != SCpnt->pid) {
1422                   sti();
1423                   return 0;
1424                 }
1425 
1426                 if (SCpnt->internal_timeout & IN_ABORT) 
1427                         {
1428                         sti();
1429                         while (SCpnt->internal_timeout & IN_ABORT);
1430                         }
1431                 else
1432                         {       
1433                         SCpnt->internal_timeout |= IN_ABORT;
1434                         oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1435 
1436                         if ((SCpnt->flags & IS_RESETTING) && 
1437                             SCpnt->device->soft_reset) {
1438                           /* OK, this command must have died when we did the
1439                              reset.  The device itself must have lied. */
1440                           printk("Stale command on %d:%d appears to have died when"
1441                                  " the bus was reset\n", SCpnt->target, SCpnt->lun);
1442                         }
1443                         
1444                         sti();
1445                         if (!host->host_busy) {
1446                           SCpnt->internal_timeout &= ~IN_ABORT;
1447                           update_timeout(SCpnt, oldto);
1448                           return 0;
1449                         }
1450                         printk("scsi : aborting command due to timeout : pid %lu, scsi%d, id %d, lun %d ",
1451                                SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->target, (int) 
1452                                SCpnt->lun);
1453                         print_command (SCpnt->cmnd);
1454                         if (SCpnt->request.dev == -1 || pid != SCpnt->pid)
1455                           return 0;
1456                         SCpnt->abort_reason = why;
1457                         switch(host->hostt->abort(SCpnt)) {
1458                           /* We do not know how to abort.  Try waiting another
1459                              time increment and see if this helps. Set the
1460                              WAS_TIMEDOUT flag set so we do not try this twice
1461                              */
1462                         case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1463                                                  this is too severe */
1464                         case SCSI_ABORT_SNOOZE:
1465                           if(why == DID_TIME_OUT) {
1466                             cli();
1467                             SCpnt->internal_timeout &= ~IN_ABORT;
1468                             if(SCpnt->flags & WAS_TIMEDOUT) {
1469                               sti();
1470                               return 1; /* Indicate we cannot handle this.
1471                                            We drop down into the reset handler
1472                                            and try again */
1473                             } else {
1474                               SCpnt->flags |= WAS_TIMEDOUT;
1475                               oldto = SCpnt->timeout_per_command;
1476                               update_timeout(SCpnt, oldto);
1477                             }
1478                             sti();
1479                           }
1480                           return 0;
1481                         case SCSI_ABORT_PENDING:
1482                           if(why != DID_TIME_OUT) {
1483                             cli();
1484                             update_timeout(SCpnt, oldto);
1485                             sti();
1486                           }
1487                           return 0;
1488                         case SCSI_ABORT_SUCCESS:
1489                           /* We should have already aborted this one.  No
1490                              need to adjust timeout */
1491                         case SCSI_ABORT_NOT_RUNNING:
1492                           SCpnt->internal_timeout &= ~IN_ABORT;
1493                           update_timeout(SCpnt, 0);
1494                           return 0;
1495                         case SCSI_ABORT_ERROR:
1496                         default:
1497                           SCpnt->internal_timeout &= ~IN_ABORT;
1498                           return 1;
1499                         }
1500                       }
1501               } 
1502       }
1503      
1504 int scsi_reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1505         {
1506         int temp, oldto;
1507         Scsi_Cmnd * SCpnt1;
1508         struct Scsi_Host * host = SCpnt->host;
1509         
1510 #ifdef DEBUG
1511         printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
1512 #endif
1513         while (1) {
1514                 cli();  
1515                 if (SCpnt->internal_timeout & IN_RESET)
1516                         {
1517                         sti();
1518                         while (SCpnt->internal_timeout & IN_RESET);
1519                         }
1520                 else
1521                         {
1522                         SCpnt->internal_timeout |= IN_RESET;
1523                         oldto = update_timeout(SCpnt, RESET_TIMEOUT);   
1524                                         
1525                         if (host->host_busy)
1526                                 {       
1527                                 sti();
1528                                 SCpnt1 = host->host_queue;
1529                                 while(SCpnt1) {
1530                                   if (SCpnt1->request.dev > 0) {
1531 #if 0                             
1532                                     if (!(SCpnt1->flags & IS_RESETTING) && 
1533                                       !(SCpnt1->internal_timeout & IN_ABORT))
1534                                     scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
1535 #endif
1536                                     SCpnt1->flags |= IS_RESETTING;
1537                                   }
1538                                   SCpnt1 = SCpnt1->next;
1539                                 };
1540 
1541                                 temp = host->hostt->reset(SCpnt);       
1542                                 }                               
1543                         else
1544                                 {
1545                                 host->host_busy++;
1546         
1547                                 sti();
1548                                 temp = host->hostt->reset(SCpnt);
1549                                 host->last_reset = jiffies;
1550                                 host->host_busy--;
1551                                 }
1552 
1553 #ifdef DEBUG
1554                         printk("scsi reset function returned %d\n", temp);
1555 #endif
1556                         switch(temp) {
1557                         case SCSI_RESET_SUCCESS:
1558                           cli();
1559                           SCpnt->internal_timeout &= ~IN_RESET;
1560                           update_timeout(SCpnt, oldto);
1561                           sti();
1562                           return 0;
1563                         case SCSI_RESET_PENDING:
1564                           return 0;
1565                         case SCSI_RESET_PUNT:
1566                         case SCSI_RESET_WAKEUP:
1567                           SCpnt->internal_timeout &= ~IN_RESET;
1568                           scsi_request_sense (SCpnt);
1569                           return 0;
1570                         case SCSI_RESET_SNOOZE:                   
1571                           /* In this case, we set the timeout field to 0
1572                              so that this command does not time out any more,
1573                              and we return 1 so that we get a message on the
1574                              screen. */
1575                           cli();
1576                           SCpnt->internal_timeout &= ~IN_RESET;
1577                           update_timeout(SCpnt, 0);
1578                           sti();
1579                           /* If you snooze, you lose... */
1580                         case SCSI_RESET_ERROR:
1581                         default:
1582                           return 1;
1583                         }
1584         
1585                         return temp;    
1586                         }
1587                 }
1588         }
1589                          
1590 
1591 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1592         {
1593         /*
1594                 We must not enter update_timeout with a timeout condition still pending.
1595         */
1596 
1597         int timed_out, pid;
1598         struct Scsi_Host * host;
1599         Scsi_Cmnd * SCpnt = NULL;
1600 
1601         do      {       
1602                 cli();
1603 
1604                 update_timeout(NULL, 0);
1605         /*
1606                 Find all timers such that they have 0 or negative (shouldn't happen)
1607                 time remaining on them.
1608         */
1609                         
1610                 timed_out = 0;
1611                 for(host = scsi_hostlist; host; host = host->next) {
1612                   for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1613                     if (SCpnt->timeout == -1)
1614                       {
1615                         SCpnt->timeout = 0;
1616                         pid = SCpnt->pid;
1617                         sti();
1618                         scsi_times_out(SCpnt, SCpnt);
1619                         ++timed_out; 
1620                         cli();
1621                       }
1622                 };
1623               } while (timed_out);      
1624         sti();
1625       }
1626 
1627 /*
1628         The strategy is to cause the timer code to call scsi_times_out()
1629         when the soonest timeout is pending.  
1630         The arguments are used when we are queueing a new command, because
1631         we do not want to subtract the time used from this time, but when we
1632         set the timer, we want to take this value into account.
1633 */
1634         
1635 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1636         {
1637         unsigned int least, used;
1638         unsigned int oldto;
1639         struct Scsi_Host * host;
1640         Scsi_Cmnd * SCpnt = NULL;
1641 
1642         cli();
1643 
1644 /* 
1645         Figure out how much time has passed since the last time the timeouts 
1646         were updated 
1647 */
1648         used = (time_start) ? (jiffies - time_start) : 0;
1649 
1650 /*
1651         Find out what is due to timeout soonest, and adjust all timeouts for
1652         the amount of time that has passed since the last time we called 
1653         update_timeout. 
1654 */
1655         
1656         oldto = 0;
1657 
1658         if(SCset){
1659           oldto = SCset->timeout - used;
1660           SCset->timeout = timeout + used;
1661         };
1662 
1663         least = 0xffffffff;
1664 
1665         for(host = scsi_hostlist; host; host = host->next)
1666           for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1667             if (SCpnt->timeout > 0) {
1668               SCpnt->timeout -= used;
1669               if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
1670               if(SCpnt->timeout > 0 && SCpnt->timeout < least)
1671                 least = SCpnt->timeout;
1672             };
1673 
1674 /*
1675         If something is due to timeout again, then we will set the next timeout 
1676         interrupt to occur.  Otherwise, timeouts are disabled.
1677 */
1678         
1679         if (least != 0xffffffff)
1680                 {
1681                 time_start = jiffies;   
1682                 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;     
1683                 timer_active |= 1 << SCSI_TIMER;
1684                 }
1685         else
1686                 {
1687                 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1688                 timer_active &= ~(1 << SCSI_TIMER);
1689                 }       
1690         sti();
1691         return oldto;
1692         }               
1693 
1694 
1695 static unsigned short * dma_malloc_freelist = NULL;
1696 static unsigned int dma_sectors = 0;
1697 unsigned int dma_free_sectors = 0;
1698 unsigned int need_isa_buffer = 0;
1699 static unsigned char * dma_malloc_buffer = NULL;
1700 
1701 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1702 {
1703   unsigned int nbits, mask;
1704   int i, j;
1705   if((len & 0x1ff) || len > 8192)
1706     return NULL;
1707   
1708   cli();
1709   nbits = len >> 9;
1710   mask = (1 << nbits) - 1;
1711   
1712   for(i=0;i < (dma_sectors >> 4); i++)
1713     for(j=0; j<17-nbits; j++){
1714       if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1715         dma_malloc_freelist[i] |= (mask << j);
1716         sti();
1717         dma_free_sectors -= nbits;
1718 #ifdef DEBUG
1719         printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1720 #endif
1721         return (void *) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1722       };
1723     };
1724   sti();
1725   return NULL;  /* Nope.  No more */
1726 }
1727 
1728 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1729 {
1730   int offset;
1731   int page, sector, nbits, mask;
1732 
1733 #ifdef DEBUG
1734   printk("Sfree %x %d\n",obj, len);
1735 #endif
1736 
1737   offset = ((int) obj) - ((int) dma_malloc_buffer);
1738 
1739   if (offset < 0) panic("Bad offset");
1740   page = offset >> 13;
1741   sector = offset >> 9;
1742   if(sector >= dma_sectors) panic ("Bad page");
1743 
1744   sector = (offset >> 9) & 15;
1745   nbits = len >> 9;
1746   mask = (1 << nbits) - 1;
1747 
1748   if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1749 
1750   cli();
1751   if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1752     panic("Trying to free unused memory");
1753 
1754   dma_free_sectors += nbits;
1755   dma_malloc_freelist[page] &= ~(mask << sector);
1756   sti();
1757   return 0;
1758 }
1759 
1760 
1761 /* These are special functions that can be used to obtain memory at boot time.
1762    They act line a malloc function, but they simply take memory from the
1763    pool */
1764 
1765 static unsigned int scsi_init_memory_start = 0;
1766 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
1767 
1768 void * scsi_init_malloc(unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1769 {
1770   unsigned int retval;
1771   if(scsi_loadable_module_flag) {
1772     retval = (unsigned int) kmalloc(size, GFP_ATOMIC);
1773   } else {
1774     retval = scsi_init_memory_start;
1775     scsi_init_memory_start += size;
1776   }
1777   return (void *) retval;
1778 }
1779 
1780 
1781 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1782 { /* FIXME - not right.  We need to compare addresses to see whether this was
1783      kmalloc'd or not */
1784   if((unsigned int) ptr < scsi_loadable_module_flag) {
1785     kfree(ptr);
1786   } else {
1787     if(((unsigned int) ptr) + size == scsi_init_memory_start)
1788       scsi_init_memory_start = (unsigned int) ptr;
1789   }
1790 }
1791 
1792 /*
1793         scsi_dev_init() is our initialization routine, which in turn calls host 
1794         initialization, bus scanning, and sd/st initialization routines.  It 
1795         should be called from main().
1796 */
1797 
1798 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1799         {
1800         struct Scsi_Host * host = NULL;
1801         Scsi_Device * SDpnt;
1802         struct Scsi_Host * shpnt;
1803         struct Scsi_Device_Template * sdtpnt;
1804         Scsi_Cmnd * SCpnt;
1805 #ifdef FOO_ON_YOU
1806         return;
1807 #endif  
1808 
1809         /* Init a few things so we can "malloc" memory. */
1810         scsi_loadable_module_flag = 0;
1811         scsi_init_memory_start = memory_start;
1812 
1813         timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1814         timer_table[SCSI_TIMER].expires = 0;
1815 
1816         /* initialize all hosts */
1817         scsi_init(); 
1818                                 
1819         scsi_devices = (Scsi_Device *) NULL;
1820 
1821         for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
1822           scan_scsis(shpnt);           /* scan for scsi devices */
1823 
1824         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1825           if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
1826 
1827         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1828           int j;
1829           SDpnt->scsi_request_fn = NULL;
1830           for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1831             if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
1832           if(SDpnt->type != -1){
1833             for(j=0;j<SDpnt->host->cmd_per_lun;j++){
1834               SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd));
1835               SCpnt->host = SDpnt->host;
1836               SCpnt->device = SDpnt;
1837               SCpnt->target = SDpnt->id;
1838               SCpnt->lun = SDpnt->lun;
1839               SCpnt->request.dev = -1; /* Mark not busy */
1840               SCpnt->use_sg = 0;
1841               SCpnt->old_use_sg = 0;
1842               SCpnt->old_cmd_len = 0;
1843               SCpnt->timeout = 0;
1844               SCpnt->underflow = 0;
1845               SCpnt->transfersize = 0;
1846               SCpnt->host_scribble = NULL;
1847               host = SDpnt->host;
1848               if(host->host_queue)
1849                 host->host_queue->prev = SCpnt;
1850               SCpnt->next = host->host_queue;
1851               SCpnt->prev = NULL;
1852               host->host_queue = SCpnt;
1853             };
1854           };
1855         };
1856 
1857         if (scsi_devicelist)
1858           dma_sectors = 16;  /* Base value we use */
1859 
1860         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1861           host = SDpnt->host;
1862           
1863           if(SDpnt->type != TYPE_TAPE)
1864             dma_sectors += ((host->sg_tablesize * 
1865                              sizeof(struct scatterlist) + 511) >> 9) * 
1866                                host->cmd_per_lun;
1867           
1868           if(host->unchecked_isa_dma &&
1869              memory_end - 1 > ISA_DMA_THRESHOLD &&
1870              SDpnt->type != TYPE_TAPE) {
1871             dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
1872               host->cmd_per_lun;
1873             need_isa_buffer++;
1874           };
1875         };
1876 
1877         dma_sectors = (dma_sectors + 15) & 0xfff0;
1878         dma_free_sectors = dma_sectors;  /* This must be a multiple of 16 */
1879 
1880         scsi_init_memory_start = (scsi_init_memory_start + 3) & 0xfffffffc;
1881         dma_malloc_freelist = (unsigned short *) 
1882           scsi_init_malloc(dma_sectors >> 3);
1883         memset(dma_malloc_freelist, 0, dma_sectors >> 3);
1884 
1885         /* Some host adapters require buffers to be word aligned */
1886         if(scsi_init_memory_start & 1) scsi_init_memory_start++;
1887 
1888         dma_malloc_buffer = (unsigned char *) 
1889           scsi_init_malloc(dma_sectors << 9);
1890         
1891         /* OK, now we finish the initialization by doing spin-up, read
1892            capacity, etc, etc */
1893         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1894           if(sdtpnt->finish && sdtpnt->nr_dev)
1895             (*sdtpnt->finish)();
1896         
1897         scsi_loadable_module_flag = 1;
1898         return scsi_init_memory_start;
1899         }
1900 
1901 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
1902 {
1903         int i;
1904 
1905         printk("  Vendor: ");
1906         for (i = 8; i < 16; i++)
1907                 {
1908                 if (data[i] >= 0x20 && i < data[4] + 5)
1909                         printk("%c", data[i]);
1910                 else
1911                         printk(" ");
1912                 }
1913 
1914         printk("  Model: ");
1915         for (i = 16; i < 32; i++)
1916                 {
1917                 if (data[i] >= 0x20 && i < data[4] + 5)
1918                         printk("%c", data[i]);
1919                 else
1920                         printk(" ");
1921                 }
1922 
1923         printk("  Rev: ");
1924         for (i = 32; i < 36; i++)
1925                 {
1926                 if (data[i] >= 0x20 && i < data[4] + 5)
1927                         printk("%c", data[i]);
1928                 else
1929                         printk(" ");
1930                 }
1931 
1932         printk("\n");
1933 
1934         i = data[0] & 0x1f;
1935 
1936         printk("  Type:   %s ",
1937                i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
1938         printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
1939         if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1940           printk(" CCS\n");
1941         else
1942           printk("\n");
1943 }
1944 
1945 #ifdef DEBUG_TIMEOUT
1946 static void 
1947 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1948 {
1949   int i, i1;
1950   Scsi_Host * shpnt;
1951   Scsi_Cmnd * SCpnt;
1952   printk("Dump of scsi parameters:\n");
1953   for(shpnt = scsi_hosts; shpnt; shpnt = shpnt->next)
1954     for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
1955       {
1956         /*  (0) 0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
1957         printk("(%d) %d:%d:%d (%4.4x %d %d %d %d) (%d %d %x) (%d %d %d) %x %x %x\n",
1958                i, SCpnt->host->host_no,
1959                SCpnt->target,
1960                SCpnt->lun,
1961                SCpnt->request.dev,
1962                SCpnt->request.sector,
1963                SCpnt->request.nr_sectors,
1964                SCpnt->request.current_nr_sectors,
1965                SCpnt->use_sg,
1966                SCpnt->retries,
1967                SCpnt->allowed,
1968                SCpnt->flags,
1969                SCpnt->timeout_per_command,
1970                SCpnt->timeout,
1971                SCpnt->internal_timeout,
1972                SCpnt->cmnd[0],
1973                SCpnt->sense_buffer[2],
1974                SCpnt->result);
1975       };
1976   printk("wait_for_request = %x\n", wait_for_request);
1977   /* Now dump the request lists for each block device */
1978   printk("Dump of pending block device requests\n");
1979   for(i=0; i<MAX_BLKDEV; i++)
1980     if(blk_dev[i].current_request)
1981       {
1982         struct request * req;
1983         printk("%d: ", i);
1984         req = blk_dev[i].current_request;
1985         while(req) {
1986           printk("(%x %d %d %d %d) ",
1987                  req->dev,
1988                  req->cmd,
1989                  req->sector,
1990                  req->nr_sectors,
1991                  req->current_nr_sectors);
1992           req = req->next;
1993         }
1994         printk("\n");
1995       }
1996 }
1997 #endif
1998 
1999 

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