root/drivers/scsi/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. blacklisted
  2. scan_scsis_done
  3. scan_scsis
  4. scsi_times_out
  5. request_queueable
  6. allocate_device
  7. internal_cmnd
  8. scsi_request_sense
  9. scsi_do_cmd
  10. reset
  11. check_sense
  12. scsi_done
  13. scsi_abort
  14. scsi_reset
  15. scsi_main_timeout
  16. update_timeout
  17. scsi_malloc
  18. scsi_free
  19. scsi_dev_init
  20. print_inquiry

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

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