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

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