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

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