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

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