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

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