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         int old_use_sg;
 594 
 595         cli();
 596         SCpnt->flags |= WAS_SENSE;
 597         update_timeout(SCpnt, SENSE_TIMEOUT);
 598         sti();
 599         
 600 
 601         memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
 602                 sizeof(generic_sense));
 603 
 604         SCpnt->cmnd[1] = SCpnt->lun << 5;       
 605         SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
 606 
 607         SCpnt->request_buffer = &SCpnt->sense_buffer;
 608         SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
 609         old_use_sg = SCpnt->use_sg;
 610         SCpnt->use_sg = 0;
 611         internal_cmnd (SCpnt);
 612         SCpnt->use_sg = 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 
 699         /* Start the timer ticking.  */
 700 
 701         SCpnt->internal_timeout = 0;
 702         internal_cmnd (SCpnt);
 703 
 704 #ifdef DEBUG
 705         printk ("Leaving scsi_do_cmd()\n");
 706 #endif
 707         }
 708 
 709 
 710 
 711 /*
 712         The scsi_done() function disables the timeout timer for the scsi host, 
 713         marks the host as not busy, and calls the user specified completion 
 714         function for that host's current command.
 715 */
 716 
 717 static void reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 718         {
 719         #ifdef DEBUG
 720                 printk("reset(%d)\n", SCpnt->host);
 721         #endif
 722 
 723         SCpnt->flags |= (WAS_RESET | IS_RESETTING);
 724         scsi_reset(SCpnt);
 725 
 726         #ifdef DEBUG
 727                 printk("performing request sense\n");
 728         #endif
 729 
 730         scsi_request_sense (SCpnt);
 731         }
 732         
 733         
 734 
 735 static int check_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 736         {
 737         if (((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
 738                 if (SCpnt->sense_buffer[2] &0xe0)
 739                   return SUGGEST_ABORT;
 740                 switch (SCpnt->sense_buffer[2] & 0xf)
 741                 {
 742                 case NO_SENSE:
 743                 case RECOVERED_ERROR:
 744                         return 0;
 745 
 746                 case ABORTED_COMMAND:
 747                         return SUGGEST_RETRY;   
 748                 case NOT_READY:
 749                 case UNIT_ATTENTION:
 750                         return SUGGEST_ABORT;
 751 
 752                 /* these three are not supported */     
 753                 case COPY_ABORTED:
 754                 case VOLUME_OVERFLOW:
 755                 case MISCOMPARE:
 756         
 757                 case MEDIUM_ERROR:
 758                         return SUGGEST_REMAP;
 759                 case BLANK_CHECK:
 760                 case DATA_PROTECT:
 761                 case HARDWARE_ERROR:
 762                 case ILLEGAL_REQUEST:
 763                 default:
 764                         return SUGGEST_ABORT;
 765                 }
 766               }
 767         else
 768                 return SUGGEST_RETRY;   
 769         }       
 770 
 771 /* This function is the mid-level interrupt routine, which decides how
 772  *  to handle error conditions.  Each invocation of this function must
 773  *  do one and *only* one of the following:
 774  *
 775  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
 776  *      normal completion, and indicates that the handling for this
 777  *      request is complete.
 778  *  (2) Call internal_cmnd to requeue the command.  This will result in
 779  *      scsi_done being called again when the retry is complete.
 780  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
 781  *      more information about the error condition.  When the information
 782  *      is available, scsi_done will be called again.
 783  *  (4) Call reset().  This is sort of a last resort, and the idea is that
 784  *      this may kick things loose and get the drive working again.  reset()
 785  *      automatically calls scsi_request_sense, and thus scsi_done will be
 786  *      called again once the reset is complete.
 787  *
 788  *      If none of the above actions are taken, the drive in question
 789  * will hang. If more than one of the above actions are taken by
 790  * scsi_done, then unpredictable behavior will result.
 791  */
 792 static void scsi_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 793         {
 794         int status=0;
 795         int exit=0;
 796         int checked;
 797         int oldto;
 798         int host = SCpnt->host;
 799         int result = SCpnt->result;
 800         oldto = update_timeout(SCpnt, 0);
 801 
 802 #define FINISHED 0
 803 #define MAYREDO  1
 804 #define REDO     3
 805 #define PENDING  4
 806 
 807 #ifdef DEBUG
 808         printk("In scsi_done(host = %d, result = %06x)\n", host, result);
 809 #endif
 810         if (host > max_scsi_hosts || host  < 0) 
 811                 {
 812                 update_timeout(SCpnt, 0);
 813                 panic("scsi_done() called with invalid host number.\n");
 814                 }
 815 
 816         switch (host_byte(result))      
 817         {
 818         case DID_OK:
 819                 if (SCpnt->flags & IS_RESETTING)
 820                         {
 821                         SCpnt->flags &= ~IS_RESETTING;
 822                         status = REDO;
 823                         break;
 824                         }
 825 
 826                 if (status_byte(result) && (SCpnt->flags & 
 827                     WAS_SENSE)) /* Failed to obtain sense information */
 828                         {
 829                         SCpnt->flags &= ~WAS_SENSE;
 830                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
 831 
 832                         if (!(SCpnt->flags & WAS_RESET))
 833                                 {
 834                                 reset(SCpnt);
 835                                 return;
 836                                 }
 837                         else
 838                                 {
 839                                 exit = (DRIVER_HARD | SUGGEST_ABORT);
 840                                 status = FINISHED;
 841                                 }
 842                         }
 843                 else switch(msg_byte(result))
 844                         {
 845                         case COMMAND_COMPLETE:
 846                         switch (status_byte(result))
 847                         {
 848                         case GOOD:
 849                                 if (SCpnt->flags & WAS_SENSE)
 850                                         {
 851 #ifdef DEBUG
 852         printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
 853 #endif
 854 
 855                                         SCpnt->flags &= ~WAS_SENSE;
 856                                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
 857         
 858                                         switch (checked = check_sense(SCpnt))
 859                                         {
 860                                         case 0: 
 861 #ifdef DEBUG
 862         printk("NO SENSE.  status = REDO\n");
 863 #endif
 864 
 865                                                 update_timeout(SCpnt, oldto);
 866                                                 status = REDO;
 867                                                 break;
 868                                         case SUGGEST_REMAP:                     
 869                                         case SUGGEST_RETRY: 
 870 #ifdef DEBUG
 871         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
 872 #endif
 873 
 874                                                 status = MAYREDO;
 875                                                 exit = DRIVER_SENSE | SUGGEST_RETRY;
 876                                                 break;
 877                                         case SUGGEST_ABORT:
 878 #ifdef DEBUG
 879         printk("SENSE SUGGEST ABORT - status = FINISHED");
 880 #endif
 881 
 882                                                 status = FINISHED;
 883                                                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
 884                                                 break;
 885                                         default:
 886                                                 printk ("Internal error %s %s \n", __FILE__, 
 887                                                         __LINE__);
 888                                         }                          
 889                                         }       
 890                                 else
 891                                         {
 892 #ifdef DEBUG
 893         printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
 894 #endif
 895 
 896                                         exit =  DRIVER_OK;
 897                                         status = FINISHED;
 898                                         }
 899                                 break;  
 900 
 901                         case CHECK_CONDITION:
 902 
 903 #ifdef DEBUG
 904         printk("CHECK CONDITION message returned, performing request sense.\n");
 905 #endif
 906 
 907                                 scsi_request_sense (SCpnt);
 908                                 status = PENDING;
 909                                 break;          
 910                         
 911                         case CONDITION_GOOD:
 912                         case INTERMEDIATE_GOOD:
 913                         case INTERMEDIATE_C_GOOD:
 914 #ifdef DEBUG
 915         printk("CONDITION GOOD, INTERMEDIATE GOOD, or INTERMEDIATE CONDITION GOOD recieved and ignored. \n");
 916 #endif
 917                                 break;
 918                                 
 919                         case BUSY:
 920 #ifdef DEBUG
 921         printk("BUSY message returned, performing REDO");
 922 #endif
 923                                 update_timeout(SCpnt, oldto);
 924                                 status = REDO;
 925                                 break;
 926 
 927                         case RESERVATION_CONFLICT:
 928                                 reset(SCpnt);
 929                                 return;
 930 #if 0
 931                                 exit = DRIVER_SOFT | SUGGEST_ABORT;
 932                                 status = MAYREDO;
 933                                 break;
 934 #endif
 935                         default:
 936                                 printk ("Internal error %s %s \n"
 937                                         "status byte = %d \n", __FILE__, 
 938                                         __LINE__, status_byte(result));
 939                                 
 940                         }
 941                         break;
 942                         default:
 943                                 printk("scsi: unsupported message byte %d recieved\n", msg_byte(result)); 
 944                                 panic ("");
 945                         }
 946                         break;
 947         case DID_TIME_OUT:      
 948 #ifdef DEBUG
 949         printk("Host returned DID_TIME_OUT - ");
 950 #endif
 951 
 952                 if (SCpnt->flags & WAS_TIMEDOUT)        
 953                         {
 954 #ifdef DEBUG
 955         printk("Aborting\n");
 956 #endif  
 957                         exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
 958                         }               
 959                 else 
 960                         {
 961 #ifdef DEBUG
 962                         printk ("Retrying.\n");
 963 #endif
 964                         SCpnt->flags  |= WAS_TIMEDOUT;
 965                         status = REDO;
 966                         }
 967                 break;
 968         case DID_BUS_BUSY:
 969         case DID_PARITY:
 970                 status = REDO;
 971                 break;
 972         case DID_NO_CONNECT:
 973 #ifdef DEBUG
 974                 printk("Couldn't connect.\n");
 975 #endif
 976                 exit  = (DRIVER_HARD | SUGGEST_ABORT);
 977                 break;
 978         case DID_ERROR: 
 979                 status = MAYREDO;
 980                 exit = (DRIVER_HARD | SUGGEST_ABORT);
 981                 break;
 982         case DID_BAD_TARGET:
 983         case DID_ABORT:
 984                 exit = (DRIVER_INVALID | SUGGEST_ABORT);
 985                 break;  
 986         case DID_RESET:
 987                 if(msg_byte(result) == GOOD &&
 988                       status_byte(result) == CHECK_CONDITION) {
 989                               scsi_request_sense (SCpnt);
 990                               status = PENDING;
 991                               break;
 992                               };
 993                 status=REDO;
 994                 exit = SUGGEST_RETRY;
 995                 break;
 996         default :               
 997                 exit = (DRIVER_ERROR | SUGGEST_DIE);
 998         }
 999 
1000         switch (status) 
1001                 {
1002                 case FINISHED:
1003                 case PENDING:
1004                         break;
1005                 case MAYREDO:
1006 
1007 #ifdef DEBUG
1008         printk("In MAYREDO, allowing %d retries, have %d\n",
1009                SCpnt->allowed, SCpnt->retries);
1010 #endif
1011 
1012                         if ((++SCpnt->retries) < SCpnt->allowed)
1013                         {
1014                         if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1015                             && !(SCpnt->flags & WAS_RESET))
1016                                 {
1017                                         reset(SCpnt);
1018                                         break;
1019                                 }
1020 
1021                         }
1022                         else
1023                                 {
1024                                 status = FINISHED;
1025                                 break;
1026                                 }
1027                         /* fall through to REDO */
1028 
1029                 case REDO:
1030                         if (SCpnt->flags & WAS_SENSE)                   
1031                                 scsi_request_sense(SCpnt);      
1032                         else    
1033                           {
1034                             memcpy ((void *) SCpnt->cmnd,
1035                                     (void*) SCpnt->data_cmnd, 
1036                                     sizeof(SCpnt->data_cmnd));
1037                             SCpnt->request_buffer = SCpnt->buffer;
1038                             SCpnt->request_bufflen = SCpnt->bufflen;
1039                             internal_cmnd (SCpnt);
1040                           };
1041                         break;  
1042                 default: 
1043                         INTERNAL_ERROR;
1044                 }
1045 
1046         if (status == FINISHED) 
1047                 {
1048                 #ifdef DEBUG
1049                         printk("Calling done function - at address %08x\n", SCpnt->done);
1050                 #endif
1051                 host_busy[host]--; /* Indicate that we are free */
1052                 wake_up(&host_wait[host]);
1053                 SCpnt->result = result | ((exit & 0xff) << 24);
1054                 SCpnt->done (SCpnt);
1055                 }
1056 
1057 
1058 #undef FINISHED
1059 #undef REDO
1060 #undef MAYREDO
1061 #undef PENDING
1062         }
1063 
1064 /*
1065         The scsi_abort function interfaces with the abort() function of the host
1066         we are aborting, and causes the current command to not complete.  The 
1067         caller should deal with any error messages or status returned on the 
1068         next call.
1069         
1070         This will not be called rentrantly for a given host.
1071 */
1072         
1073 /*
1074         Since we're nice guys and specified that abort() and reset()
1075         can be non-reentrant.  The internal_timeout flags are used for
1076         this.
1077 */
1078 
1079 
1080 int scsi_abort (Scsi_Cmnd * SCpnt, int why)
     /* [previous][next][first][last][top][bottom][index][help] */
1081         {
1082         int temp, oldto;
1083         int host = SCpnt->host;
1084         
1085         while(1)        
1086                 {
1087                 cli();
1088                 if (SCpnt->internal_timeout & IN_ABORT) 
1089                         {
1090                         sti();
1091                         while (SCpnt->internal_timeout & IN_ABORT);
1092                         }
1093                 else
1094                         {       
1095                         SCpnt->internal_timeout |= IN_ABORT;
1096                         oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1097 
1098                         
1099                         sti();
1100                         if (!host_busy[host] || !scsi_hosts[host].abort(SCpnt, why))
1101                                 temp =  0;
1102                         else
1103                                 temp = 1;
1104                         
1105                         cli();
1106                         SCpnt->internal_timeout &= ~IN_ABORT;
1107                         update_timeout(SCpnt, oldto);
1108                         sti();
1109                         return temp;
1110                         }
1111                 }       
1112         }
1113 
1114 int scsi_reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1115         {
1116         int temp, oldto;
1117         Scsi_Cmnd * SCpnt1;
1118         int host = SCpnt->host;
1119         
1120 #ifdef DEBUG
1121         printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host);
1122 #endif
1123         while (1) {
1124                 cli();  
1125                 if (SCpnt->internal_timeout & IN_RESET)
1126                         {
1127                         sti();
1128                         while (SCpnt->internal_timeout & IN_RESET);
1129                         }
1130                 else
1131                         {
1132                         SCpnt->internal_timeout |= IN_RESET;
1133                         oldto = update_timeout(SCpnt, RESET_TIMEOUT);   
1134                                         
1135                         if (host_busy[host])
1136                                 {       
1137                                 sti();
1138                                 SCpnt1 = host_queue[host];
1139                                 while(SCpnt1) {
1140                                   if ((SCpnt1->request.dev > 0) &&
1141                                       !(SCpnt1->flags & IS_RESETTING) && 
1142                                       !(SCpnt1->internal_timeout & IN_ABORT))
1143                                     scsi_abort(SCpnt1, DID_RESET);
1144                                   SCpnt1 = SCpnt1->next;
1145                                 };
1146 
1147                                 temp = scsi_hosts[host].reset();                        
1148                                 }                               
1149                         else
1150                                 {
1151                                 host_busy[host]++;
1152         
1153                                 sti();
1154                                 temp = scsi_hosts[host].reset();
1155                                 last_reset[host] = jiffies;
1156                                 host_busy[host]--;
1157                                 }
1158         
1159                         cli();
1160                         SCpnt->internal_timeout &= ~IN_RESET;
1161                         update_timeout(SCpnt, oldto);
1162                         sti();
1163                         return temp;    
1164                         }
1165                 }
1166         }
1167                          
1168 
1169 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1170         {
1171         /*
1172                 We must not enter update_timeout with a timeout condition still pending.
1173         */
1174 
1175         int timed_out, host;
1176         Scsi_Cmnd * SCpnt = NULL;
1177 
1178         do      {       
1179                 cli();
1180 
1181         /*
1182                 Find all timers such that they have 0 or negative (shouldn't happen)
1183                 time remaining on them.
1184         */
1185                         
1186                 timed_out = 0;
1187                 for(host = 0; host < max_scsi_hosts; host++) {
1188                   SCpnt = host_queue[host];
1189                   while (SCpnt){
1190                     if (SCpnt->timeout > 0 && SCpnt->timeout <= time_elapsed)
1191                       {
1192                         sti();
1193                         SCpnt->timeout = 0;
1194                         scsi_times_out(SCpnt);
1195                         ++timed_out; 
1196                         cli();
1197                       }
1198                   SCpnt =  SCpnt->next;
1199                   };
1200                 };
1201                 update_timeout(NULL, 0);
1202               } while (timed_out);      
1203         sti();
1204       }
1205 
1206 /*
1207         These are used to keep track of things. 
1208 */
1209 
1210 static int time_start, time_elapsed;
1211 
1212 /*
1213         The strategy is to cause the timer code to call scsi_times_out()
1214         when the soonest timeout is pending.  
1215         The arguments are used when we are queueing a new command, because
1216         we do not want to subtract the time used from this time, but when we
1217         set the timer, we want to take this value into account.
1218 */
1219         
1220 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1221         {
1222         unsigned int least, used, host;
1223         unsigned int oldto;
1224         Scsi_Cmnd * SCpnt = NULL;
1225 
1226         cli();
1227 
1228 /* 
1229         Figure out how much time has passed since the last time the timeouts 
1230         were updated 
1231 */
1232         used = (time_start) ? (jiffies - time_start) : 0;
1233 
1234 /*
1235         Find out what is due to timeout soonest, and adjust all timeouts for
1236         the amount of time that has passed since the last time we called 
1237         update_timeout. 
1238 */
1239         
1240         oldto = 0;
1241 
1242         if(SCset){
1243           oldto = SCset->timeout - used;
1244           SCset->timeout = timeout + used;
1245         };
1246 
1247         least = 0xffffffff;
1248 
1249         for(host = 0; host < max_scsi_hosts; host++) {
1250           SCpnt = host_queue[host];
1251           while (SCpnt){
1252             if (SCpnt->timeout > 0 && (SCpnt->timeout -= used) < least)
1253               least = SCpnt->timeout;
1254             SCpnt =  SCpnt->next;
1255           };
1256         };
1257 
1258 /*
1259         If something is due to timeout again, then we will set the next timeout 
1260         interrupt to occur.  Otherwise, timeouts are disabled.
1261 */
1262         
1263         if (least != 0xffffffff)
1264                 {
1265                 time_start = jiffies;   
1266                 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;     
1267                 timer_active |= 1 << SCSI_TIMER;
1268                 }
1269         else
1270                 {
1271                 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1272                 timer_active &= ~(1 << SCSI_TIMER);
1273                 }       
1274         sti();
1275         return oldto;
1276         }               
1277 
1278 
1279 static unsigned short * dma_malloc_freelist = NULL;
1280 static unsigned int dma_sectors = 0;
1281 unsigned int dma_free_sectors = 0;
1282 unsigned int need_isa_buffer = 0;
1283 static unsigned char * dma_malloc_buffer = NULL;
1284 
1285 char *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1286 {
1287   unsigned int nbits, mask;
1288   int i, j;
1289   if((len & 0x1ff) || len > 4096)
1290     panic("Inappropriate buffer size requested");
1291   
1292   cli();
1293   nbits = len >> 9;
1294   mask = (1 << nbits) - 1;
1295   
1296   for(i=0;i < (dma_sectors >> 4); i++)
1297     for(j=0; j<17-nbits; j++){
1298       if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1299         dma_malloc_freelist[i] |= (mask << j);
1300         sti();
1301         dma_free_sectors -= nbits;
1302 #ifdef DEBUG
1303         printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1304 #endif
1305         return (void*) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1306       };
1307     };
1308   sti();
1309   return NULL;  /* Nope.  No more */
1310 }
1311 
1312 int scsi_free(char *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1313 {
1314   int offset;
1315   int page, sector, nbits, mask;
1316 
1317 #ifdef DEBUG
1318   printk("Sfree %x %d\n",obj, len);
1319 #endif
1320 
1321   offset = ((int) obj) - ((int) dma_malloc_buffer);
1322 
1323   if (offset < 0) panic("Bad offset");
1324   page = offset >> 13;
1325   sector = offset >> 9;
1326   if(sector >= dma_sectors) panic ("Bad page");
1327 
1328   sector = (offset >> 9) & 15;
1329   nbits = len >> 9;
1330   mask = (1 << nbits) - 1;
1331 
1332   if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1333 
1334   cli();
1335   if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1336     panic("Trying to free unused memory");
1337 
1338   dma_free_sectors += nbits;
1339   dma_malloc_freelist[page] &= ~(mask << sector);
1340   sti();
1341   return 0;
1342 }
1343 
1344 /*
1345         scsi_dev_init() is our initialization routine, which inturn calls host 
1346         initialization, bus scanning, and sd/st initialization routines.  It 
1347         should be called from main().
1348 */
1349 
1350 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1351         {
1352         int i;
1353         int host;
1354         Scsi_Cmnd * SCpnt;
1355 #ifdef FOO_ON_YOU
1356         return;
1357 #endif  
1358         timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1359         timer_table[SCSI_TIMER].expires = 0;
1360 
1361         scsi_init();            /* initialize all hosts */
1362 
1363         for (i = 0; i < max_scsi_hosts; ++i)
1364                 last_reset[i] = 0;
1365                                 
1366         scsi_devices = (Scsi_Device *) memory_start;
1367         scan_scsis();           /* scan for scsi devices */
1368         memory_start += NR_SCSI_DEVICES * sizeof(Scsi_Device);
1369 
1370         memory_start = sd_init1(memory_start, memory_end);
1371         memory_start = st_init1(memory_start, memory_end);
1372         memory_start = sr_init1(memory_start, memory_end);
1373 
1374         last_cmnd = (Scsi_Cmnd *) memory_start;
1375 
1376         SCpnt = last_cmnd;
1377 
1378         for (i=0; i< NR_SCSI_DEVICES; i++) {
1379           int j;
1380           switch (scsi_devices[i].type)
1381             {
1382             case TYPE_TAPE :
1383               st_attach(&scsi_devices[i]);
1384               break;
1385             case TYPE_ROM:
1386               sr_attach(&scsi_devices[i]);
1387               break;
1388             case TYPE_DISK:
1389             case TYPE_MOD:
1390               sd_attach(&scsi_devices[i]);
1391             default:
1392               break;
1393             };
1394           if(scsi_devices[i].type != -1){
1395             for(j=0;j<scsi_hosts[scsi_devices[i].host_no].cmd_per_lun;j++){
1396               SCpnt->host = scsi_devices[i].host_no;
1397               SCpnt->target = scsi_devices[i].id;
1398               SCpnt->lun = scsi_devices[i].lun;
1399               SCpnt->index = i;
1400               SCpnt->request.dev = -1; /* Mark not busy */
1401               SCpnt->use_sg = 0;
1402               SCpnt->underflow = 0;
1403               SCpnt->transfersize = 0;
1404               host = scsi_devices[i].host_no;
1405               if(host_queue[host])
1406                 host_queue[host]->prev = SCpnt;
1407               SCpnt->next = host_queue[host];
1408               SCpnt->prev = NULL;
1409               host_queue[host] = SCpnt;
1410               SCpnt++;
1411             };
1412           };
1413         };
1414 
1415         memory_start = (int) SCpnt;
1416 
1417         if (NR_SD > 0 || NR_SR > 0 || NR_ST > 0)
1418           dma_sectors = 16;  /* Base value we use */
1419 
1420         for (i = 0; i < NR_SCSI_DEVICES; ++i) {
1421           int host;
1422           host = scsi_devices[i].host_no;
1423           
1424           if(scsi_devices[i].type != TYPE_TAPE)
1425             dma_sectors += ((scsi_hosts[host].sg_tablesize * 
1426                              sizeof(struct scatterlist) + 511) >> 9) * 
1427                                scsi_hosts[host].cmd_per_lun;
1428           
1429           if(scsi_hosts[host].unchecked_isa_dma &&
1430              memory_end > ISA_DMA_THRESHOLD &&
1431              scsi_devices[i].type != TYPE_TAPE) {
1432             dma_sectors += (BLOCK_SIZE >> 9) * scsi_hosts[host].sg_tablesize * 
1433               scsi_hosts[host].cmd_per_lun;
1434             need_isa_buffer++;
1435           };
1436         };
1437 
1438         dma_sectors = (dma_sectors + 15) & 0xfff0;
1439         dma_free_sectors = dma_sectors;  /* This must be a multiple of 16 */
1440 
1441         memory_start = (memory_start + 3) & 0xfffffffc;
1442         dma_malloc_freelist = (unsigned short *) memory_start;
1443         memory_start += dma_sectors >> 3;
1444         memset(dma_malloc_freelist, 0, dma_sectors >> 3);
1445 
1446         if(memory_start & 1) memory_start++; /* Some host adapters require
1447                                                 buffers to be word aligned */
1448         dma_malloc_buffer = (char *) memory_start;
1449         memory_start += dma_sectors << 9;
1450 
1451         memory_start = sd_init(memory_start, memory_end); /* init scsi disks */
1452         memory_start = st_init(memory_start, memory_end); /* init scsi tapes */
1453         memory_start = sr_init(memory_start, memory_end);
1454         return memory_start;
1455         }
1456 
1457 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
1458 {
1459         int i;
1460 
1461         printk("  Vendor: ");
1462         for (i = 8; i < 16; i++)
1463                 {
1464                 if (data[i] >= 20 && i < data[4] + 5)
1465                         printk("%c", data[i]);
1466                 else
1467                         printk(" ");
1468                 }
1469 
1470         printk("  Model: ");
1471         for (i = 16; i < 32; i++)
1472                 {
1473                 if (data[i] >= 20 && i < data[4] + 5)
1474                         printk("%c", data[i]);
1475                 else
1476                         printk(" ");
1477                 }
1478 
1479         printk("  Rev: ");
1480         for (i = 32; i < 36; i++)
1481                 {
1482                 if (data[i] >= 20 && i < data[4] + 5)
1483                         printk("%c", data[i]);
1484                 else
1485                         printk(" ");
1486                 }
1487 
1488         printk("\n");
1489 
1490         i = data[0] & 0x1f;
1491 
1492         printk("  Type:   %s ", i == 0x00 ? "Direct-Access    " :
1493                                 i == 0x01 ? "Sequential-Access" :
1494                                 i == 0x02 ? "Printer          " :
1495                                 i == 0x03 ? "Processor        " :
1496                                 i == 0x04 ? "WORM             " :
1497                                 i == 0x05 ? "CD-ROM           " :
1498                                 i == 0x06 ? "Scanner          " :
1499                                 i == 0x07 ? "Optical Device   " :
1500                                 i == 0x08 ? "Medium Changer   " :
1501                                 i == 0x09 ? "Communications   " :
1502                                             "Unknown          " );
1503         printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
1504         if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1505           printk(" CCS\n");
1506         else
1507           printk("\n");
1508 }

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