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

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