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

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