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

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