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

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