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

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