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

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