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

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