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. scan_scsis
  4. scsi_times_out
  5. request_queueable
  6. allocate_device
  7. internal_cmnd
  8. scsi_request_sense
  9. scsi_do_cmd
  10. reset
  11. check_sense
  12. scsi_done
  13. scsi_abort
  14. scsi_reset
  15. scsi_main_timeout
  16. update_timeout
  17. scsi_malloc
  18. scsi_free
  19. scsi_dev_init
  20. print_inquiry

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

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