root/drivers/scsi/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. blacklisted
  2. restart_all
  3. scan_scsis_done
  4. scsi_luns_setup
  5. scan_scsis
  6. scsi_times_out
  7. request_queueable
  8. allocate_device
  9. internal_cmnd
  10. scsi_request_sense
  11. scsi_do_cmd
  12. reset
  13. check_sense
  14. scsi_done
  15. scsi_abort
  16. scsi_reset
  17. scsi_main_timeout
  18. update_timeout
  19. scsi_malloc
  20. scsi_free
  21. scsi_init_malloc
  22. scsi_init_free
  23. scsi_dev_init
  24. print_inquiry
  25. scsi_register_host
  26. scsi_unregister_host
  27. scsi_register_module
  28. scsi_unregister_module
  29. scsi_dump_status

   1 /*
   2  *      scsi.c Copyright (C) 1992 Drew Eckhardt 
   3  *             Copyright (C) 1993, 1994 Eric Youngdale
   4  *
   5  *      generic mid-level SCSI driver by
   6  *              Drew Eckhardt 
   7  *
   8  *      <drew@colorado.edu>
   9  *
  10  *      Bug correction thanks go to : 
  11  *              Rik Faith <faith@cs.unc.edu>
  12  *              Tommy Thorn <tthorn>
  13  *              Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  14  * 
  15  *       Modified by Eric Youngdale ericy@cais.com to
  16  *       add scatter-gather, multiple outstanding request, and other
  17  *       enhancements.
  18  */
  19 
  20 #include <asm/system.h>
  21 #include <linux/sched.h>
  22 #include <linux/timer.h>
  23 #include <linux/string.h>
  24 #include <linux/malloc.h>
  25 #include <asm/irq.h>
  26 
  27 #include "../block/blk.h"
  28 #include "scsi.h"
  29 #include "hosts.h"
  30 #include "constants.h"
  31 
  32 /*
  33 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 $";
  34 */
  35 
  36 /* Command groups 3 and 4 are reserved and should never be used.  */
  37 const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
  38 
  39 #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
  40 
  41 static void scsi_done (Scsi_Cmnd *SCpnt);
  42 static int update_timeout (Scsi_Cmnd *, int);
  43 static void print_inquiry(unsigned char *data);
  44 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid);
  45 
  46 static int time_start;
  47 static int time_elapsed;
  48 static int scsi_maybe_deadlocked = 0;
  49 static int max_active = 0;
  50 static struct Scsi_Host * host_active = NULL;
  51 static struct Scsi_Host * host_next = NULL;
  52 
  53 #define MAX_SCSI_DEVICE_CODE 10
  54 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
  55 {
  56  "Direct-Access    ",
  57  "Sequential-Access",
  58  "Printer          ",
  59  "Processor        ",
  60  "WORM             ",
  61  "CD-ROM           ",
  62  "Scanner          ",
  63  "Optical Device   ",
  64  "Medium Changer   ",
  65  "Communications   "
  66 };
  67 
  68 
  69 /*
  70         global variables : 
  71         scsi_devices an array of these specifying the address for each 
  72         (host, id, LUN)
  73 */
  74 
  75 Scsi_Device * scsi_devices = NULL;
  76 
  77 /* Process ID of SCSI commands */
  78 unsigned long scsi_pid = 0;
  79 
  80 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
  81 
  82 /* This variable is merely a hook so that we can debug the kernel with gdb. */
  83 Scsi_Cmnd * last_cmnd = NULL;
  84 
  85 /*
  86  *      As the scsi do command functions are intelligent, and may need to 
  87  *      redo a command, we need to keep track of the last command 
  88  *      executed on each one.
  89  */
  90 
  91 #define WAS_RESET       0x01
  92 #define WAS_TIMEDOUT    0x02
  93 #define WAS_SENSE       0x04
  94 #define IS_RESETTING    0x08
  95 #define IS_ABORTING     0x10
  96 #define ASKED_FOR_SENSE 0x20
  97 
  98 /*
  99  *      This is the number  of clock ticks we should wait before we time out 
 100  *      and abort the command.  This is for  where the scsi.c module generates 
 101  *      the command, not where it originates from a higher level, in which
 102  *      case the timeout is specified there.
 103  *
 104  *      ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
 105  *      respectively.
 106  */
 107 
 108 #ifdef DEBUG_TIMEOUT
 109 static void scsi_dump_status(void);
 110 #endif
 111 
 112 
 113 #ifdef DEBUG
 114         #define SCSI_TIMEOUT 500
 115 #else
 116         #define SCSI_TIMEOUT 100
 117 #endif
 118 
 119 #ifdef DEBUG
 120         #define SENSE_TIMEOUT SCSI_TIMEOUT
 121         #define ABORT_TIMEOUT SCSI_TIMEOUT
 122         #define RESET_TIMEOUT SCSI_TIMEOUT
 123 #else
 124         #define SENSE_TIMEOUT 50
 125         #define RESET_TIMEOUT 50
 126         #define ABORT_TIMEOUT 50
 127         #define MIN_RESET_DELAY 100
 128 #endif
 129 
 130 /* The following devices are known not to tolerate a lun != 0 scan for
 131    one reason or another.  Some will respond to all luns, others will
 132    lock up. */
 133 
 134      struct blist{
 135        char * vendor;
 136        char * model;
 137        char * revision; /* Latest revision known to be bad.  Not used yet */
 138      };
 139 
 140 static struct blist blacklist[] = 
 141 {
 142    {"CHINON","CD-ROM CDS-431","H42"},  /* Locks up if polled for lun != 0 */
 143    {"CHINON","CD-ROM CDS-535","Q14"}, /* Lockup if polled for lun != 0 */
 144    {"DENON","DRD-25X","V"},   /* A cdrom that locks up when probed at lun != 0 */
 145    {"IMS", "CDD521/10","2.06"},   /* Locks-up when LUN>0 polled. */
 146    {"MAXTOR","XT-3280","PR02"},   /* Locks-up when LUN>0 polled. */
 147    {"MAXTOR","XT-4380S","B3C"},   /* Locks-up when LUN>0 polled. */
 148    {"MAXTOR","MXT-1240S","I1.2"}, /* Locks up when LUN > 0 polled */
 149    {"MAXTOR","XT-4170S","B5A"},   /* Locks-up sometimes when LUN>0 polled. */
 150    {"MAXTOR","XT-8760S","B7B"},   /* guess what? */
 151    {"NEC","CD-ROM DRIVE:841","1.0"},  /* Locks-up when LUN>0 polled. */
 152    {"RODIME","RO3000S","2.33"},  /* Locks up if polled for lun != 0 */
 153    {"SEAGATE", "ST157N", "\004|j"}, /* causes failed REQUEST SENSE on lun 1 for aha152x
 154                                      * controller, which causes SCSI code to reset bus.*/
 155    {"SEAGATE", "ST296","921"},   /* Responds to all lun */
 156    {"SONY","CD-ROM CDU-541","4.3d"},
 157    {"SONY","CD-ROM CDU-55S","1.0i"},
 158    {"TANDBERG","TDC 3600","U07"},  /* Locks up if polled for lun != 0 */
 159    {"TEAC","CD-ROM","1.06"},    /* causes failed REQUEST SENSE on lun 1 for seagate
 160                                  * controller, which causes SCSI code to reset bus.*/
 161    {"TEXEL","CD-ROM","1.06"},   /* causes failed REQUEST SENSE on lun 1 for seagate
 162                                  * controller, which causes SCSI code to reset bus.*/
 163    {"QUANTUM","LPS525S","3110"},/* Locks sometimes if polled for lun != 0 */
 164    {"QUANTUM","PD1225S","3110"},/* Locks sometimes if polled for lun != 0 */
 165    {"MEDIAVIS","CDR-H93MV","1.31"},  /* Locks up if polled for lun != 0 */
 166    {"SANKYO", "CP525","6.64"},  /* causes failed REQ SENSE, extra reset */
 167    {"HP", "C1750A", "3226"},    /* scanjet iic */
 168    {"HP", "C1790A", ""},        /* scanjet iip */
 169    {"HP", "C2500A", ""},        /* scanjet iicx */
 170    {NULL, NULL, NULL}}; 
 171 
 172 static int blacklisted(unsigned char * response_data){
     /* [previous][next][first][last][top][bottom][index][help] */
 173   int i = 0;
 174   unsigned char * pnt;
 175   for(i=0; 1; i++){
 176     if(blacklist[i].vendor == NULL) return 0;
 177     pnt = &response_data[8];
 178     while(*pnt && *pnt == ' ') pnt++;
 179     if(memcmp(blacklist[i].vendor, pnt,
 180                strlen(blacklist[i].vendor))) continue;
 181     pnt = &response_data[16];
 182     while(*pnt && *pnt == ' ') pnt++;
 183     if(memcmp(blacklist[i].model, pnt,
 184                strlen(blacklist[i].model))) continue;
 185     return 1;
 186   };    
 187 };
 188 
 189 /*
 190  *      As the actual SCSI command runs in the background, we must set up a 
 191  *      flag that tells scan_scsis() when the result it has is valid.  
 192  *      scan_scsis can set the_result to -1, and watch for it to become the 
 193  *      actual return code for that call.  the scan_scsis_done function() is 
 194  *      our user specified completion function that is passed on to the  
 195  *      scsi_do_cmd() function.
 196  */
 197 
 198 volatile int in_scan_scsis = 0;
 199 static int the_result;
 200 
 201 static void restart_all(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 202  /*
 203   * If we might have deadlocked, get things started again. Only
 204   * for block devices. Character devices should never deadlock.
 205   */
 206   struct Scsi_Device_Template * sdtpnt;
 207 
 208   for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
 209 
 210      if (sdtpnt->blk && blk_dev[sdtpnt->major].request_fn)
 211         (*blk_dev[sdtpnt->major].request_fn)();
 212 
 213   }
 214 
 215 static void scan_scsis_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 216         {
 217         
 218 #ifdef DEBUG
 219         printk ("scan_scsis_done(%d, %06x)\n", SCpnt->host, SCpnt->result);
 220 #endif  
 221         SCpnt->request.dev = 0xfffe;
 222         }
 223 
 224 #ifdef NO_MULTI_LUN
 225 static int max_scsi_luns = 1;
 226 #else
 227 static int max_scsi_luns = 8;
 228 #endif
 229 
 230 void scsi_luns_setup(char *str, int *ints) {
     /* [previous][next][first][last][top][bottom][index][help] */
 231     if (ints[0] != 1) 
 232         printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
 233     else
 234         max_scsi_luns = ints[1];
 235 }
 236 
 237 /*
 238  *      Detecting SCSI devices :        
 239  *      We scan all present host adapter's busses,  from ID 0 to ID 6.  
 240  *      We use the INQUIRY command, determine device type, and pass the ID / 
 241  *      lun address of all sequential devices to the tape driver, all random 
 242  *      devices to the disk driver.
 243  */
 244 
 245 static void scan_scsis (struct Scsi_Host * shpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 246 {
 247   int dev, lun, type;
 248   unsigned char scsi_cmd [12];
 249   unsigned char scsi_result [256];
 250   Scsi_Device * SDpnt, *SDtail;
 251   struct Scsi_Device_Template * sdtpnt;
 252   Scsi_Cmnd  SCmd;
 253   
 254   ++in_scan_scsis;
 255   lun = 0;
 256   type = -1;
 257   SCmd.next = NULL;
 258   SCmd.prev = NULL;
 259   SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device));
 260   SDtail = scsi_devices;
 261   if(scsi_devices) {
 262     while(SDtail->next) SDtail = SDtail->next;
 263   }
 264 
 265   shpnt->host_queue = &SCmd;  /* We need this so that
 266                                          commands can time out */
 267   for (dev = 0; dev < 8; ++dev)
 268     if (shpnt->this_id != dev)
 269 /*
 270  * We need the for so our continue, etc. work fine.
 271  */
 272 
 273       for (lun = 0; lun < max_scsi_luns; ++lun)
 274         {
 275           SDpnt->host = shpnt;
 276           SDpnt->id = dev;
 277           SDpnt->lun = lun;
 278           SDpnt->device_wait = NULL;
 279           SDpnt->next = NULL;
 280           SDpnt->attached = 0;
 281 /*
 282  * Assume that the device will have handshaking problems, and then 
 283  * fix this field later if it turns out it doesn't.
 284  */
 285           SDpnt->borken = 1;
 286           
 287           scsi_cmd[0] = TEST_UNIT_READY;
 288           scsi_cmd[1] = lun << 5;
 289           scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
 290           scsi_cmd[4] = 0;
 291           
 292           SCmd.host = shpnt;
 293           SCmd.target = dev;
 294           SCmd.lun = lun;
 295 
 296           SCmd.request.sem = NULL;  /* Used for mutex if loading devices after boot */
 297           SCmd.request.dev = 0xffff; /* Mark not busy */
 298           SCmd.use_sg  = 0;
 299           SCmd.cmd_len = 0;
 300           SCmd.old_use_sg  = 0;
 301           SCmd.transfersize = 0;
 302           SCmd.underflow = 0;
 303           
 304           scsi_do_cmd (&SCmd,
 305                        (void *)  scsi_cmd, (void *) 
 306                        scsi_result, 256,  scan_scsis_done, 
 307                        SCSI_TIMEOUT + 400, 5);
 308           
 309           while (SCmd.request.dev != 0xfffe);
 310 #if defined(DEBUG) || defined(DEBUG_INIT)
 311           printk("scsi: scan SCSIS id %d lun %d\n", dev, lun);
 312           printk("scsi: return code %08x\n", SCmd.result);
 313 #endif
 314           
 315           
 316           if(SCmd.result) {
 317             if ((driver_byte(SCmd.result)  & DRIVER_SENSE) &&
 318                 ((SCmd.sense_buffer[0] & 0x70) >> 4) == 7) {
 319               if (SCmd.sense_buffer[2] &0xe0)
 320                 continue; /* No devices here... */
 321               if(((SCmd.sense_buffer[2] & 0xf) != NOT_READY) &&
 322                  ((SCmd.sense_buffer[2] & 0xf) != UNIT_ATTENTION))
 323                 continue;
 324             }
 325             else
 326               break;
 327           };
 328           
 329 #if defined (DEBUG) || defined(DEBUG_INIT)
 330           printk("scsi: performing INQUIRY\n");
 331 #endif
 332           
 333           /*
 334            * Build an INQUIRY command block.  
 335            */
 336           
 337           scsi_cmd[0] = INQUIRY;
 338           scsi_cmd[1] = (lun << 5) & 0xe0;
 339           scsi_cmd[2] = 0;
 340           scsi_cmd[3] = 0;
 341           scsi_cmd[4] = 255;
 342           scsi_cmd[5] = 0;
 343           
 344           SCmd.request.dev = 0xffff; /* Mark not busy */
 345           SCmd.cmd_len = 0;
 346           
 347           scsi_do_cmd (&SCmd,
 348                        (void *)  scsi_cmd, (void *) 
 349                        scsi_result, 256,  scan_scsis_done, 
 350                        SCSI_TIMEOUT, 3);
 351           
 352           while (SCmd.request.dev != 0xfffe);
 353           
 354           the_result = SCmd.result;
 355           
 356 #if defined(DEBUG) || defined(DEBUG_INIT)
 357           if (!the_result)
 358             printk("scsi: INQUIRY successful\n");
 359           else
 360             printk("scsi: INQUIRY failed with code %08x\n", the_result);
 361 #endif
 362           
 363           if(the_result) break; 
 364           
 365           /* skip other luns on this device */
 366           
 367           if (!the_result)
 368             {
 369                 /* It would seem some TOSHIBA CD-ROM gets things wrong */
 370                 if (!strncmp(scsi_result+8,"TOSHIBA",7) &&
 371                     !strncmp(scsi_result+16,"CD-ROM",6) &&
 372                     scsi_result[0] == TYPE_DISK) {
 373                         scsi_result[0] = TYPE_ROM;
 374                         scsi_result[1] |= 0x80;  /* removable */
 375                 }
 376 
 377               SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
 378               if (!strncmp(scsi_result+8,"NEC",3))
 379                 SDpnt->manufacturer = SCSI_MAN_NEC;
 380               if (!strncmp(scsi_result+8,"TOSHIBA",7))
 381                 SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
 382 
 383               SDpnt->removable = (0x80 & 
 384                                   scsi_result[1]) >> 7;
 385               SDpnt->lockable = SDpnt->removable;
 386               SDpnt->changed = 0;
 387               SDpnt->access_count = 0;
 388               SDpnt->busy = 0;
 389 /* 
 390  *      Currently, all sequential devices are assumed to be tapes,
 391  *      all random devices disk, with the appropriate read only 
 392  *      flags set for ROM / WORM treated as RO.
 393  */ 
 394 
 395               switch (type = (scsi_result[0] & 0x1f))
 396                 {
 397                 case TYPE_TAPE :
 398                 case TYPE_DISK :
 399                 case TYPE_MOD :
 400                 case TYPE_PROCESSOR :
 401                 case TYPE_SCANNER :
 402                   SDpnt->writeable = 1;
 403                   break;
 404                 case TYPE_WORM :
 405                 case TYPE_ROM :
 406                   SDpnt->writeable = 0;
 407                   break;
 408                 default :
 409 #if 0
 410 #ifdef DEBUG
 411                   printk("scsi: unknown type %d\n", type);
 412                   print_inquiry(scsi_result);
 413 #endif
 414                   type = -1;
 415 #endif
 416                 }
 417               
 418               SDpnt->soft_reset = 
 419                 (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
 420               SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
 421               SDpnt->type = (type & 0x1f);
 422               
 423               if (type != -1)
 424                 {
 425                   print_inquiry(scsi_result);
 426 
 427                   for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
 428                     if(sdtpnt->detect) SDpnt->attached +=
 429                       (*sdtpnt->detect)(SDpnt);
 430 
 431                   SDpnt->scsi_level = scsi_result[2] & 0x07;
 432                   if (SDpnt->scsi_level >= 2 ||
 433                       (SDpnt->scsi_level == 1 &&
 434                        (scsi_result[3] & 0x0f) == 1))
 435                     SDpnt->scsi_level++;
 436 /* 
 437  * Set the tagged_queue flag for SCSI-II devices that purport to support
 438  * tagged queuing in the INQUIRY data.
 439  */
 440                   
 441                   SDpnt->tagged_queue = 0;
 442                   
 443                   if ((SDpnt->scsi_level >= SCSI_2) &&
 444                       (scsi_result[7] & 2)) {
 445                     SDpnt->tagged_supported = 1;
 446                     SDpnt->current_tag = 0;
 447                   }
 448                   
 449 /*
 450  * Accommodate drivers that want to sleep when they should be in a polling
 451  * loop.
 452  */
 453 
 454                   SDpnt->disconnect = 0;
 455 
 456 /*
 457  * Some revisions of the Texel CD ROM drives have handshaking
 458  * problems when used with the Seagate controllers.  Before we
 459  * know what type of device we're talking to, we assume it's 
 460  * borken and then change it here if it turns out that it isn't
 461  * a TEXEL drive.
 462  */
 463 
 464                   if(strncmp("TEXEL", (char *) &scsi_result[8], 5) != 0 ||
 465                      strncmp("CD-ROM", (char *) &scsi_result[16], 6) != 0 
 466 /* 
 467  * XXX 1.06 has problems, some one should figure out the others too so
 468  * ALL TEXEL drives don't suffer in performance, especially when I finish
 469  * integrating my seagate patches which do multiple I_T_L nexuses.
 470  */
 471 
 472 #ifdef notyet
 473                      || (strncmp("1.06", (char *) &scsi_result[[, 4) != 0)))
 474 #endif
 475                                  )
 476                     SDpnt->borken = 0;
 477                   
 478                   
 479                   /* These devices need this "key" to unlock the device
 480                      so we can use it */
 481                   if(memcmp("INSITE", &scsi_result[8], 6) == 0 &&
 482                      (memcmp("Floptical   F*8I", &scsi_result[16], 16) == 0
 483                       || memcmp("I325VM", &scsi_result[16], 6) == 0)) {
 484                     printk("Unlocked floptical drive.\n");
 485                     SDpnt->lockable = 0;
 486                     scsi_cmd[0] = MODE_SENSE;
 487                     scsi_cmd[1] = (lun << 5) & 0xe0;
 488                     scsi_cmd[2] = 0x2e;
 489                     scsi_cmd[3] = 0;
 490                     scsi_cmd[4] = 0x2a;
 491                     scsi_cmd[5] = 0;
 492                     
 493                     SCmd.request.dev = 0xffff; /* Mark not busy */
 494                     SCmd.cmd_len = 0;
 495                     
 496                     scsi_do_cmd (&SCmd,
 497                                  (void *)  scsi_cmd, (void *) 
 498                                  scsi_result, 0x2a,  scan_scsis_done, 
 499                                  SCSI_TIMEOUT, 3);
 500                     
 501                     while (SCmd.request.dev != 0xfffe);
 502                   };
 503                   /* Add this device to the linked list at the end */
 504                   if(SDtail)
 505                     SDtail->next = SDpnt;
 506                   else
 507                     scsi_devices = SDpnt;
 508                   SDtail = SDpnt;
 509 
 510                   SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device));
 511                   /* Some scsi devices cannot be polled for lun != 0
 512                      due to firmware bugs */
 513                   if(blacklisted(scsi_result)) break;
 514                   /* Old drives like the MAXTOR XT-3280 say vers=0 */
 515                   if ((scsi_result[2] & 0x07) == 0)
 516                     break;
 517                   /* Some scsi-1 peripherals do not handle lun != 0.
 518                      I am assuming that scsi-2 peripherals do better */
 519                   if((scsi_result[2] & 0x07) == 1 && 
 520                      (scsi_result[3] & 0x0f) == 0) break;
 521                 }
 522             }       /* if result == DID_OK ends */
 523         }       /* for lun ends */
 524   shpnt->host_queue = NULL;  /* No longer needed here */
 525   
 526   printk("scsi : detected ");
 527   for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
 528     if(sdtpnt->dev_noticed && sdtpnt->name)
 529       printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
 530              (sdtpnt->dev_noticed != 1) ? "s" : "");
 531 
 532   printk("total.\n");
 533   
 534   /* Last device block does not exist.  Free memory. */
 535   scsi_init_free((char *) SDpnt, sizeof(Scsi_Device));
 536   
 537   in_scan_scsis = 0;
 538 }       /* scan_scsis  ends */
 539 
 540 /*
 541  *      Flag bits for the internal_timeout array 
 542  */
 543 
 544 #define NORMAL_TIMEOUT 0
 545 #define IN_ABORT 1
 546 #define IN_RESET 2
 547 /*
 548         This is our time out function, called when the timer expires for a 
 549         given host adapter.  It will attempt to abort the currently executing 
 550         command, that failing perform a kernel panic.
 551 */ 
 552 
 553 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 554         {
 555         
 556         switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
 557                 {
 558                 case NORMAL_TIMEOUT:
 559                         if (!in_scan_scsis) {
 560 #ifdef DEBUG_TIMEOUT
 561                           scsi_dump_status();
 562 #endif
 563                         }
 564                         
 565                         if (!scsi_abort (SCpnt, DID_TIME_OUT, pid))
 566                                 return;                         
 567                 case IN_ABORT:
 568                         printk("SCSI host %d abort() timed out - resetting\n",
 569                                 SCpnt->host->host_no);
 570                         if (!scsi_reset (SCpnt)) 
 571                                 return;
 572                 case IN_RESET:
 573                 case (IN_ABORT | IN_RESET):
 574                   /* This might be controversial, but if there is a bus hang,
 575                      you might conceivably want the machine up and running
 576                      esp if you have an ide disk. */
 577                         printk("Unable to reset scsi host %d - ",SCpnt->host->host_no);
 578                         printk("probably a SCSI bus hang.\n");
 579                         return;
 580 
 581                 default:
 582                         INTERNAL_ERROR;
 583                 }
 584                                         
 585         }
 586 
 587 
 588 /* This function takes a quick look at a request, and decides if it
 589 can be queued now, or if there would be a stall while waiting for
 590 something else to finish.  This routine assumes that interrupts are
 591 turned off when entering the routine.  It is the responsibility
 592 of the calling code to ensure that this is the case. */
 593 
 594 Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 595 {
 596   Scsi_Cmnd * SCpnt = NULL;
 597   int tablesize;
 598   struct buffer_head * bh, *bhp;
 599 
 600   if (!device)
 601     panic ("No device passed to request_queueable().\n");
 602   
 603   if (req && req->dev <= 0)
 604     panic("Invalid device in request_queueable");
 605   
 606   SCpnt =  device->host->host_queue;
 607     while(SCpnt){
 608       if(SCpnt->target == device->id &&
 609          SCpnt->lun == device->lun)
 610         if(SCpnt->request.dev < 0) break;
 611       SCpnt = SCpnt->next;
 612     };
 613 
 614   if (!SCpnt) return NULL;
 615 
 616   if (device->host->can_queue &&
 617        ((device->host->block && host_active && device->host != host_active) || 
 618         (device->host->block && host_next && device->host != host_next) ||
 619         device->host->host_busy >= device->host->can_queue)) {
 620 
 621      if (device->host->block && !host_next && host_active 
 622                              && device->host != host_active) 
 623         host_next = device->host;
 624 
 625      return NULL;
 626      }
 627 
 628   if (req) {
 629     memcpy(&SCpnt->request, req, sizeof(struct request));
 630     tablesize = device->host->sg_tablesize;
 631     bhp = bh = req->bh;
 632     if(!tablesize) bh = NULL;
 633     /* Take a quick look through the table to see how big it is.  We already
 634        have our copy of req, so we can mess with that if we want to.  */
 635     while(req->nr_sectors && bh){
 636             bhp = bhp->b_reqnext;
 637             if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
 638             req->nr_sectors -= bh->b_size >> 9;
 639             req->sector += bh->b_size >> 9;
 640             if(!tablesize) break;
 641             bh = bhp;
 642     };
 643     if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
 644       SCpnt->request.bhtail = bh;
 645       req->bh = bh->b_reqnext; /* Divide request */
 646       bh->b_reqnext = NULL;
 647       bh = req->bh;
 648 
 649       /* Now reset things so that req looks OK */
 650       SCpnt->request.nr_sectors -= req->nr_sectors;
 651       req->current_nr_sectors = bh->b_size >> 9;
 652       req->buffer = bh->b_data;
 653       SCpnt->request.sem = NULL; /* Wait until whole thing done */
 654     } else {
 655       req->dev = -1;
 656       wake_up(&wait_for_request);
 657     };      
 658   } else {
 659     SCpnt->request.dev = 0xffff; /* Busy, but no request */
 660     SCpnt->request.sem = NULL;  /* And no one is waiting for the device either */
 661   };
 662 
 663   SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
 664   SCpnt->old_use_sg  = 0;
 665   SCpnt->transfersize = 0;
 666   SCpnt->underflow = 0;
 667   SCpnt->cmd_len = 0;
 668   return SCpnt;
 669 }
 670 
 671 /* This function returns a structure pointer that will be valid for
 672 the device.  The wait parameter tells us whether we should wait for
 673 the unit to become free or not.  We are also able to tell this routine
 674 not to return a descriptor if the host is unable to accept any more
 675 commands for the time being.  We need to keep in mind that there is no
 676 guarantee that the host remain not busy.  Keep in mind the
 677 request_queueable function also knows the internal allocation scheme
 678 of the packets for each device */
 679 
 680 Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
     /* [previous][next][first][last][top][bottom][index][help] */
 681                              int wait)
 682 {
 683   int dev = -1;
 684   struct request * req = NULL;
 685   int tablesize;
 686   struct buffer_head * bh, *bhp;
 687   struct Scsi_Host * host;
 688   Scsi_Cmnd * SCpnt = NULL;
 689   Scsi_Cmnd * SCwait = NULL;
 690 
 691   if (!device)
 692     panic ("No device passed to allocate_device().\n");
 693   
 694   if (reqp) req = *reqp;
 695 
 696     /* See if this request has already been queued by an interrupt routine */
 697   if (req && (dev = req->dev) <= 0) return NULL;
 698   
 699   host = device->host;
 700   
 701   if (intr_count && host->can_queue &&
 702                       ((host->block && host_active && host != host_active) ||
 703                        (host->block && host_next && host != host_next) ||
 704                        host->host_busy >= host->can_queue)) {
 705 
 706      scsi_maybe_deadlocked = 1;
 707 
 708      if (host->block && !host_next && host_active && host != host_active) 
 709         host_next = host;
 710 
 711      return NULL;
 712      }
 713 
 714   while (1==1){
 715     SCpnt = host->host_queue;
 716     while(SCpnt){
 717       if(SCpnt->target == device->id &&
 718          SCpnt->lun == device->lun) {
 719         SCwait = SCpnt;
 720         if(SCpnt->request.dev < 0) break;
 721       };
 722       SCpnt = SCpnt->next;
 723     };
 724     cli();
 725     /* See if this request has already been queued by an interrupt routine */
 726     if (req && ((req->dev < 0) || (req->dev != dev))) {
 727       sti();
 728       return NULL;
 729     };
 730     if (!SCpnt || SCpnt->request.dev >= 0)  /* Might have changed */
 731       {
 732         sti();
 733         if(!wait) return NULL;
 734         if (!SCwait) {
 735           printk("Attempt to allocate device target %d, lun %d\n",
 736                  device->id ,device->lun);
 737           panic("No device found in allocate_device\n");
 738         };
 739         SCSI_SLEEP(&device->device_wait, 
 740                    (SCwait->request.dev > 0));
 741       } else {
 742         if (req) {
 743           memcpy(&SCpnt->request, req, sizeof(struct request));
 744           tablesize = device->host->sg_tablesize;
 745           bhp = bh = req->bh;
 746           if(!tablesize) bh = NULL;
 747           /* Take a quick look through the table to see how big it is.  We already
 748              have our copy of req, so we can mess with that if we want to.  */
 749           while(req->nr_sectors && bh){
 750             bhp = bhp->b_reqnext;
 751             if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
 752             req->nr_sectors -= bh->b_size >> 9;
 753             req->sector += bh->b_size >> 9;
 754             if(!tablesize) break;
 755             bh = bhp;
 756           };
 757           if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
 758             SCpnt->request.bhtail = bh;
 759             req->bh = bh->b_reqnext; /* Divide request */
 760             bh->b_reqnext = NULL;
 761             bh = req->bh;
 762             /* Now reset things so that req looks OK */
 763             SCpnt->request.nr_sectors -= req->nr_sectors;
 764             req->current_nr_sectors = bh->b_size >> 9;
 765             req->buffer = bh->b_data;
 766             SCpnt->request.sem = NULL; /* Wait until whole thing done */
 767           }
 768           else 
 769             {
 770               req->dev = -1;
 771               *reqp = req->next;
 772               wake_up(&wait_for_request);
 773             };
 774         } else {
 775           SCpnt->request.dev = 0xffff; /* Busy */
 776           SCpnt->request.sem = NULL;  /* And no one is waiting for this to complete */
 777         };
 778         sti();
 779         break;
 780       };
 781   };
 782 
 783   SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
 784   SCpnt->old_use_sg  = 0;
 785   SCpnt->transfersize = 0;      /* No default transfer size */
 786   SCpnt->cmd_len = 0;
 787   SCpnt->underflow = 0;         /* Do not flag underflow conditions */
 788   return SCpnt;
 789 }
 790 
 791 /*
 792         This is inline because we have stack problemes if we recurse to deeply.
 793 */
 794                          
 795 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 796         {
 797         int temp;
 798         struct Scsi_Host * host;
 799 #ifdef DEBUG_DELAY      
 800         int clock;
 801 #endif
 802 
 803         if ((unsigned long) &SCpnt < current->kernel_stack_page)
 804           panic("Kernel stack overflow.");
 805 
 806         host = SCpnt->host;
 807 
 808 /*
 809         We will wait MIN_RESET_DELAY clock ticks after the last reset so 
 810         we can avoid the drive not being ready.
 811 */ 
 812 temp = host->last_reset;
 813 while (jiffies < temp);
 814 
 815 update_timeout(SCpnt, SCpnt->timeout_per_command);
 816 
 817 /*
 818         We will use a queued command if possible, otherwise we will emulate the
 819         queuing and calling of completion function ourselves. 
 820 */
 821 #ifdef DEBUG
 822         printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer =  %08x, \n"
 823                 "bufflen = %d, done = %08x)\n", SCpnt->host->host_no, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
 824 #endif
 825 
 826         if (host->can_queue)
 827                 {
 828 #ifdef DEBUG
 829         printk("queuecommand : routine at %08x\n", 
 830                 host->hostt->queuecommand);
 831 #endif
 832                   /* This locking tries to prevent all sorts of races between
 833                      queuecommand and the interrupt code.  In effect,
 834                      we are only allowed to be in queuecommand once at
 835                      any given time, and we can only be in the interrupt
 836                      handler and the queuecommand function at the same time
 837                      when queuecommand is called while servicing the
 838                      interrupt. */
 839 
 840                 if(!intr_count && SCpnt->host->irq)
 841                   disable_irq(SCpnt->host->irq);
 842 
 843                 host->hostt->queuecommand (SCpnt, scsi_done);
 844 
 845                 if(!intr_count && SCpnt->host->irq)
 846                   enable_irq(SCpnt->host->irq);
 847                 }
 848         else
 849                 {
 850 
 851 #ifdef DEBUG
 852         printk("command() :  routine at %08x\n", host->hostt->command);
 853 #endif
 854                 temp=host->hostt->command (SCpnt);
 855                 SCpnt->result = temp;
 856 #ifdef DEBUG_DELAY
 857         clock = jiffies + 400;
 858         while (jiffies < clock);
 859         printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp, done);
 860 #endif
 861                 scsi_done(SCpnt);
 862                 }       
 863 #ifdef DEBUG
 864         printk("leaving internal_cmnd()\n");
 865 #endif
 866         }       
 867 
 868 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 869         {
 870         cli();
 871         SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
 872         update_timeout(SCpnt, SENSE_TIMEOUT);
 873         sti();
 874         
 875 
 876         memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
 877                 sizeof(generic_sense));
 878 
 879         SCpnt->cmnd[1] = SCpnt->lun << 5;       
 880         SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
 881 
 882         SCpnt->request_buffer = &SCpnt->sense_buffer;
 883         SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
 884         SCpnt->use_sg = 0;
 885         SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
 886         internal_cmnd (SCpnt);
 887         }
 888 
 889 
 890 
 891 /*
 892         scsi_do_cmd sends all the commands out to the low-level driver.  It 
 893         handles the specifics required for each low level driver - ie queued 
 894         or non queued.  It also prevents conflicts when different high level 
 895         drivers go for the same host at the same time.
 896 */
 897 
 898 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd , 
     /* [previous][next][first][last][top][bottom][index][help] */
 899                   void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
 900                   int timeout, int retries 
 901                    )
 902         {
 903         struct Scsi_Host * host = SCpnt->host;
 904 
 905 #ifdef DEBUG
 906         {
 907         int i;  
 908         int target = SCpnt->target;
 909         printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
 910                 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
 911                 "command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
 912         for (i = 0; i < 10; ++i)
 913                 printk ("%02x  ", ((unsigned char *) cmnd)[i]); 
 914         printk("\n");
 915       };
 916 #endif
 917         
 918         if (!host)
 919                 {
 920                 panic ("Invalid or not present host.\n");
 921                 }
 922 
 923         
 924 /*
 925         We must prevent reentrancy to the lowlevel host driver.  This prevents 
 926         it - we enter a loop until the host we want to talk to is not busy.   
 927         Race conditions are prevented, as interrupts are disabled in between the
 928         time we check for the host being not busy, and the time we mark it busy
 929         ourselves.
 930 */
 931 
 932         SCpnt->pid = scsi_pid++; 
 933 
 934         for(;;) {
 935 
 936           cli();
 937 
 938           if (host->can_queue &&
 939                 ((host->block && host_active && host != host_active) ||
 940                  (host->block && host_next && host != host_next) ||
 941                  host->host_busy >= host->can_queue)) {
 942 
 943              if (intr_count) 
 944                 panic("scsi: queueing to inactive host while in interrupt.\n");
 945 
 946              if (host->block && !host_next && host_active 
 947                                            && host != host_active) 
 948                 host_next = host;
 949 
 950              sti();
 951              SCSI_SLEEP(&host->host_wait, 
 952                        ((host->block && host_active && host != host_active) ||
 953                         (host->block && host_next && host != host_next) ||
 954                         host->host_busy >= host->can_queue));
 955              } 
 956 
 957           else {
 958              host->host_busy++;
 959 
 960              if (host->block) {
 961 
 962                 if (!host_active) {
 963                    max_active = 0;
 964                    host_active = host;
 965                    host_next = NULL;
 966                    }
 967                 else if (max_active > 7 && !host_next) 
 968                    host_next = host->block;
 969 
 970                 max_active++;
 971                 }
 972 
 973              sti();
 974              break;
 975              }
 976           }
 977 /*
 978         Our own function scsi_done (which marks the host as not busy, disables 
 979         the timeout counter, etc) will be called by us or by the 
 980         scsi_hosts[host].queuecommand() function needs to also call
 981         the completion function for the high level driver.
 982 
 983 */
 984 
 985         memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
 986 #if 0
 987         SCpnt->host = host;
 988         SCpnt->target = target;
 989         SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
 990 #endif
 991         SCpnt->bufflen = bufflen;
 992         SCpnt->buffer = buffer;
 993         SCpnt->flags=0;
 994         SCpnt->retries=0;
 995         SCpnt->allowed=retries;
 996         SCpnt->done = done;
 997         SCpnt->timeout_per_command = timeout;
 998                                 
 999         memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
1000         /* Zero the sense buffer.  Some host adapters automatically request
1001            sense on error.  0 is not a valid sense code.  */
1002         memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1003         SCpnt->request_buffer = buffer;
1004         SCpnt->request_bufflen = bufflen;
1005         SCpnt->old_use_sg = SCpnt->use_sg;
1006         if (SCpnt->cmd_len == 0)
1007                 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1008         SCpnt->old_cmd_len = SCpnt->cmd_len;
1009 
1010         /* Start the timer ticking.  */
1011 
1012         SCpnt->internal_timeout = 0;
1013         SCpnt->abort_reason = 0;
1014         internal_cmnd (SCpnt);
1015 
1016 #ifdef DEBUG
1017         printk ("Leaving scsi_do_cmd()\n");
1018 #endif
1019         }
1020 
1021 
1022 
1023 /*
1024         The scsi_done() function disables the timeout timer for the scsi host, 
1025         marks the host as not busy, and calls the user specified completion 
1026         function for that host's current command.
1027 */
1028 
1029 static void reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1030 {
1031 #ifdef DEBUG
1032         printk("scsi: reset(%d)\n", SCpnt->host->host_no);
1033 #endif
1034         
1035         SCpnt->flags |= (WAS_RESET | IS_RESETTING);
1036         scsi_reset(SCpnt);
1037         
1038 #ifdef DEBUG
1039         printk("performing request sense\n");
1040 #endif
1041         
1042 #if 0  /* FIXME - remove this when done */
1043         if(SCpnt->flags & NEEDS_JUMPSTART) {
1044           SCpnt->flags &= ~NEEDS_JUMPSTART;
1045           scsi_request_sense (SCpnt);
1046         };
1047 #endif
1048 }
1049         
1050         
1051 
1052 static int check_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1053         {
1054   /* If there is no sense information, request it.  If we have already
1055      requested it, there is no point in asking again - the firmware must be
1056      confused. */
1057   if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1058     if(!(SCpnt->flags & ASKED_FOR_SENSE))
1059       return SUGGEST_SENSE;
1060     else
1061       return SUGGEST_RETRY;
1062       }
1063   
1064   SCpnt->flags &= ~ASKED_FOR_SENSE;
1065 
1066 #ifdef DEBUG_INIT
1067         printk("scsi%d : ", SCpnt->host->host_no);
1068         print_sense("", SCpnt);
1069         printk("\n");
1070 #endif
1071                 if (SCpnt->sense_buffer[2] &0xe0)
1072                   return SUGGEST_ABORT;
1073 
1074                 switch (SCpnt->sense_buffer[2] & 0xf)
1075                 {
1076                 case NO_SENSE:
1077                         return 0;
1078                 case RECOVERED_ERROR:
1079                         if (SCpnt->device->type == TYPE_TAPE)
1080                           return SUGGEST_IS_OK;
1081                         else
1082                           return 0;
1083 
1084                 case ABORTED_COMMAND:
1085                         return SUGGEST_RETRY;   
1086                 case NOT_READY:
1087                 case UNIT_ATTENTION:
1088                         return SUGGEST_ABORT;
1089 
1090                 /* these three are not supported */     
1091                 case COPY_ABORTED:
1092                 case VOLUME_OVERFLOW:
1093                 case MISCOMPARE:
1094         
1095                 case MEDIUM_ERROR:
1096                         return SUGGEST_REMAP;
1097                 case BLANK_CHECK:
1098                 case DATA_PROTECT:
1099                 case HARDWARE_ERROR:
1100                 case ILLEGAL_REQUEST:
1101                 default:
1102                         return SUGGEST_ABORT;
1103                 }
1104               }
1105 
1106 /* This function is the mid-level interrupt routine, which decides how
1107  *  to handle error conditions.  Each invocation of this function must
1108  *  do one and *only* one of the following:
1109  *
1110  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
1111  *      normal completion, and indicates that the handling for this
1112  *      request is complete.
1113  *  (2) Call internal_cmnd to requeue the command.  This will result in
1114  *      scsi_done being called again when the retry is complete.
1115  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
1116  *      more information about the error condition.  When the information
1117  *      is available, scsi_done will be called again.
1118  *  (4) Call reset().  This is sort of a last resort, and the idea is that
1119  *      this may kick things loose and get the drive working again.  reset()
1120  *      automatically calls scsi_request_sense, and thus scsi_done will be
1121  *      called again once the reset is complete.
1122  *
1123  *      If none of the above actions are taken, the drive in question
1124  * will hang. If more than one of the above actions are taken by
1125  * scsi_done, then unpredictable behavior will result.
1126  */
1127 static void scsi_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1128         {
1129         int status=0;
1130         int exit=0;
1131         int checked;
1132         int oldto;
1133         struct Scsi_Host * host = SCpnt->host;
1134         int result = SCpnt->result;
1135         oldto = update_timeout(SCpnt, 0);
1136 
1137 #ifdef DEBUG_TIMEOUT
1138         if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1139                           result, SCpnt->target, SCpnt->lun);
1140 #endif
1141 
1142         /* If we requested an abort, (and we got it) then fix up the return
1143            status to say why */
1144         if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1145           SCpnt->result = result = (result & 0xff00ffff) | 
1146             (SCpnt->abort_reason << 16);
1147 
1148 
1149 #define FINISHED 0
1150 #define MAYREDO  1
1151 #define REDO     3
1152 #define PENDING  4
1153 
1154 #ifdef DEBUG
1155         printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1156 #endif
1157 
1158         if(SCpnt->flags & WAS_SENSE)
1159         {
1160                 SCpnt->use_sg = SCpnt->old_use_sg;
1161                 SCpnt->cmd_len = SCpnt->old_cmd_len;
1162         }
1163 
1164         switch (host_byte(result))      
1165         {
1166         case DID_OK:
1167                 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1168                         /* Failed to obtain sense information */
1169                         {
1170                         SCpnt->flags &= ~WAS_SENSE;
1171                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1172 
1173                         if (!(SCpnt->flags & WAS_RESET))
1174                                 {
1175                                 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n", 
1176                                         SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
1177                                 reset(SCpnt);
1178                                 return;
1179                                 }
1180                         else
1181                                 {
1182                                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1183                                 status = FINISHED;
1184                                 }
1185                         }
1186                 else switch(msg_byte(result))
1187                         {
1188                         case COMMAND_COMPLETE:
1189                         switch (status_byte(result))
1190                         {
1191                         case GOOD:
1192                                 if (SCpnt->flags & WAS_SENSE)
1193                                         {
1194 #ifdef DEBUG
1195         printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1196 #endif
1197 
1198                                         SCpnt->flags &= ~WAS_SENSE;
1199                                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1200         
1201                                         switch (checked = check_sense(SCpnt))
1202                                         {
1203                                         case SUGGEST_SENSE:
1204                                         case 0: 
1205 #ifdef DEBUG
1206         printk("NO SENSE.  status = REDO\n");
1207 #endif
1208 
1209                                                 update_timeout(SCpnt, oldto);
1210                                                 status = REDO;
1211                                                 break;
1212                                         case SUGGEST_IS_OK:
1213                                                 break;
1214                                         case SUGGEST_REMAP:                     
1215                                         case SUGGEST_RETRY: 
1216 #ifdef DEBUG
1217         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1218 #endif
1219 
1220                                                 status = MAYREDO;
1221                                                 exit = DRIVER_SENSE | SUGGEST_RETRY;
1222                                                 break;
1223                                         case SUGGEST_ABORT:
1224 #ifdef DEBUG
1225         printk("SENSE SUGGEST ABORT - status = FINISHED");
1226 #endif
1227 
1228                                                 status = FINISHED;
1229                                                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
1230                                                 break;
1231                                         default:
1232                                                 printk ("Internal error %s %d \n", __FILE__, 
1233                                                         __LINE__);
1234                                         }                          
1235                                         }       
1236                                 else
1237                                         {
1238 #ifdef DEBUG
1239         printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1240 #endif
1241 
1242                                         exit =  DRIVER_OK;
1243                                         status = FINISHED;
1244                                         }
1245                                 break;  
1246 
1247                         case CHECK_CONDITION:
1248                                 switch (check_sense(SCpnt))
1249                                   {
1250                                   case 0: 
1251                                     update_timeout(SCpnt, oldto);
1252                                     status = REDO;
1253                                     break;
1254                                   case SUGGEST_REMAP:                   
1255                                   case SUGGEST_RETRY:
1256                                     status = MAYREDO;
1257                                     exit = DRIVER_SENSE | SUGGEST_RETRY;
1258                                     break;
1259                                   case SUGGEST_ABORT:
1260                                     status = FINISHED;
1261                                     exit =  DRIVER_SENSE | SUGGEST_ABORT;
1262                                     break;
1263                                   case SUGGEST_SENSE:
1264                                 scsi_request_sense (SCpnt);
1265                                 status = PENDING;
1266                                 break;          
1267                                   }
1268                                 break;          
1269                         
1270                         case CONDITION_GOOD:
1271                         case INTERMEDIATE_GOOD:
1272                         case INTERMEDIATE_C_GOOD:
1273                                 break;
1274                                 
1275                         case BUSY:
1276                                 update_timeout(SCpnt, oldto);
1277                                 status = REDO;
1278                                 break;
1279 
1280                         case RESERVATION_CONFLICT:
1281                                 printk("scsi%d : RESERVATION CONFLICT performing reset.\n", 
1282                                         SCpnt->host->host_no);
1283                                 reset(SCpnt);
1284                                 return;
1285 #if 0
1286                                 exit = DRIVER_SOFT | SUGGEST_ABORT;
1287                                 status = MAYREDO;
1288                                 break;
1289 #endif
1290                         default:
1291                                 printk ("Internal error %s %d \n"
1292                                         "status byte = %d \n", __FILE__, 
1293                                         __LINE__, status_byte(result));
1294                                 
1295                         }
1296                         break;
1297                         default:
1298                                 panic("scsi: unsupported message byte %d received\n", msg_byte(result)); 
1299                         }
1300                         break;
1301         case DID_TIME_OUT:      
1302 #ifdef DEBUG
1303         printk("Host returned DID_TIME_OUT - ");
1304 #endif
1305 
1306                 if (SCpnt->flags & WAS_TIMEDOUT)
1307                         {
1308 #ifdef DEBUG
1309         printk("Aborting\n");
1310 #endif  
1311                         exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1312                         }               
1313                 else 
1314                         {
1315 #ifdef DEBUG
1316                         printk ("Retrying.\n");
1317 #endif
1318                         SCpnt->flags  |= WAS_TIMEDOUT;
1319                         SCpnt->internal_timeout &= ~IN_ABORT;
1320                         status = REDO;
1321                         }
1322                 break;
1323         case DID_BUS_BUSY:
1324         case DID_PARITY:
1325                 status = REDO;
1326                 break;
1327         case DID_NO_CONNECT:
1328 #ifdef DEBUG
1329                 printk("Couldn't connect.\n");
1330 #endif
1331                 exit  = (DRIVER_HARD | SUGGEST_ABORT);
1332                 break;
1333         case DID_ERROR: 
1334                 status = MAYREDO;
1335                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1336                 break;
1337         case DID_BAD_TARGET:
1338         case DID_ABORT:
1339                 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1340                 break;  
1341         case DID_RESET:
1342                 if (SCpnt->flags & IS_RESETTING)
1343                         {
1344                         SCpnt->flags &= ~IS_RESETTING;
1345                         status = REDO;
1346                         break;
1347                         }
1348 
1349                 if(msg_byte(result) == GOOD &&
1350                       status_byte(result) == CHECK_CONDITION) {
1351                         switch (check_sense(SCpnt)) {
1352                         case 0: 
1353                             update_timeout(SCpnt, oldto);
1354                             status = REDO;
1355                             break;
1356                         case SUGGEST_REMAP:                     
1357                         case SUGGEST_RETRY:
1358                             status = MAYREDO;
1359                             exit = DRIVER_SENSE | SUGGEST_RETRY;
1360                             break;
1361                         case SUGGEST_ABORT:
1362                             status = FINISHED;
1363                             exit =  DRIVER_SENSE | SUGGEST_ABORT;
1364                             break;
1365                         case SUGGEST_SENSE:
1366                               scsi_request_sense (SCpnt);
1367                               status = PENDING;
1368                               break;
1369                         }
1370                 } else {
1371                 status=REDO;
1372                 exit = SUGGEST_RETRY;
1373                 }
1374                 break;
1375         default :               
1376                 exit = (DRIVER_ERROR | SUGGEST_DIE);
1377         }
1378 
1379         switch (status) 
1380                 {
1381                 case FINISHED:
1382                 case PENDING:
1383                         break;
1384                 case MAYREDO:
1385 
1386 #ifdef DEBUG
1387         printk("In MAYREDO, allowing %d retries, have %d\n",
1388                SCpnt->allowed, SCpnt->retries);
1389 #endif
1390 
1391                         if ((++SCpnt->retries) < SCpnt->allowed)
1392                         {
1393                         if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1394                             && !(SCpnt->flags & WAS_RESET))
1395                                 {
1396                                         printk("scsi%d : resetting for second half of retries.\n",
1397                                                 SCpnt->host->host_no);
1398                                         reset(SCpnt);
1399                                         break;
1400                                 }
1401 
1402                         }
1403                         else
1404                                 {
1405                                 status = FINISHED;
1406                                 break;
1407                                 }
1408                         /* fall through to REDO */
1409 
1410                 case REDO:
1411 
1412                         if (host->block) scsi_maybe_deadlocked = 1;
1413 
1414                         if (SCpnt->flags & WAS_SENSE)                   
1415                                 scsi_request_sense(SCpnt);      
1416                         else    
1417                           {
1418                             memcpy ((void *) SCpnt->cmnd,
1419                                     (void*) SCpnt->data_cmnd, 
1420                                     sizeof(SCpnt->data_cmnd));
1421                             SCpnt->request_buffer = SCpnt->buffer;
1422                             SCpnt->request_bufflen = SCpnt->bufflen;
1423                             SCpnt->use_sg = SCpnt->old_use_sg;
1424                             SCpnt->cmd_len = SCpnt->old_cmd_len;
1425                             internal_cmnd (SCpnt);
1426                           };
1427                         break;  
1428                 default: 
1429                         INTERNAL_ERROR;
1430                 }
1431 
1432 
1433         if (status == FINISHED) {
1434 #ifdef DEBUG
1435            printk("Calling done function - at address %08x\n", SCpnt->done);
1436 #endif
1437            host->host_busy--; /* Indicate that we are free */
1438 
1439            if (host->block && host->host_busy == 0) {
1440               struct Scsi_Host * block;
1441 
1442               block = host_next;
1443               host_active = NULL;
1444 
1445               if (block) wake_up(&block->host_wait);
1446 
1447               for (block = host->block; block != host; block = block->block)
1448                  wake_up(&block->host_wait);
1449 
1450               host_next = NULL;
1451 
1452               if (scsi_maybe_deadlocked) {
1453                  scsi_maybe_deadlocked = 0;
1454                  restart_all();
1455                  }
1456 
1457               }
1458 
1459            wake_up(&host->host_wait);
1460            SCpnt->result = result | ((exit & 0xff) << 24);
1461            SCpnt->use_sg = SCpnt->old_use_sg;
1462            SCpnt->cmd_len = SCpnt->old_cmd_len;
1463            SCpnt->done (SCpnt);
1464            }
1465 
1466 #undef FINISHED
1467 #undef REDO
1468 #undef MAYREDO
1469 #undef PENDING
1470         }
1471 
1472 /*
1473         The scsi_abort function interfaces with the abort() function of the host
1474         we are aborting, and causes the current command to not complete.  The 
1475         caller should deal with any error messages or status returned on the 
1476         next call.
1477         
1478         This will not be called reentrantly for a given host.
1479 */
1480         
1481 /*
1482         Since we're nice guys and specified that abort() and reset()
1483         can be non-reentrant.  The internal_timeout flags are used for
1484         this.
1485 */
1486 
1487 
1488 int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
1489         {
1490         int oldto;
1491         struct Scsi_Host * host = SCpnt->host;
1492         
1493         while(1)        
1494                 {
1495                 cli();
1496 
1497                 /*
1498                  * Protect against races here.  If the command is done, or we are
1499                  * on a different command forget it.
1500                  */
1501                 if (SCpnt->request.dev == -1 || pid != SCpnt->pid) {
1502                   sti();
1503                   return 0;
1504                 }
1505 
1506                 if (SCpnt->internal_timeout & IN_ABORT) 
1507                         {
1508                         sti();
1509                         while (SCpnt->internal_timeout & IN_ABORT);
1510                         }
1511                 else
1512                         {       
1513                         SCpnt->internal_timeout |= IN_ABORT;
1514                         oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1515 
1516                         if ((SCpnt->flags & IS_RESETTING) && 
1517                             SCpnt->device->soft_reset) {
1518                           /* OK, this command must have died when we did the
1519                              reset.  The device itself must have lied. */
1520                           printk("Stale command on %d:%d appears to have died when"
1521                                  " the bus was reset\n", SCpnt->target, SCpnt->lun);
1522                         }
1523                         
1524                         sti();
1525                         if (!host->host_busy) {
1526                           SCpnt->internal_timeout &= ~IN_ABORT;
1527                           update_timeout(SCpnt, oldto);
1528                           return 0;
1529                         }
1530                         printk("scsi : aborting command due to timeout : pid %lu, scsi%d, id %d, lun %d ",
1531                                SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->target, (int) 
1532                                SCpnt->lun);
1533                         print_command (SCpnt->cmnd);
1534                         if (SCpnt->request.dev == -1 || pid != SCpnt->pid)
1535                           return 0;
1536                         SCpnt->abort_reason = why;
1537                         switch(host->hostt->abort(SCpnt)) {
1538                           /* We do not know how to abort.  Try waiting another
1539                              time increment and see if this helps. Set the
1540                              WAS_TIMEDOUT flag set so we do not try this twice
1541                              */
1542                         case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1543                                                  this is too severe */
1544                         case SCSI_ABORT_SNOOZE:
1545                           if(why == DID_TIME_OUT) {
1546                             cli();
1547                             SCpnt->internal_timeout &= ~IN_ABORT;
1548                             if(SCpnt->flags & WAS_TIMEDOUT) {
1549                               sti();
1550                               return 1; /* Indicate we cannot handle this.
1551                                            We drop down into the reset handler
1552                                            and try again */
1553                             } else {
1554                               SCpnt->flags |= WAS_TIMEDOUT;
1555                               oldto = SCpnt->timeout_per_command;
1556                               update_timeout(SCpnt, oldto);
1557                             }
1558                             sti();
1559                           }
1560                           return 0;
1561                         case SCSI_ABORT_PENDING:
1562                           if(why != DID_TIME_OUT) {
1563                             cli();
1564                             update_timeout(SCpnt, oldto);
1565                             sti();
1566                           }
1567                           return 0;
1568                         case SCSI_ABORT_SUCCESS:
1569                           /* We should have already aborted this one.  No
1570                              need to adjust timeout */
1571                         case SCSI_ABORT_NOT_RUNNING:
1572                           SCpnt->internal_timeout &= ~IN_ABORT;
1573                           update_timeout(SCpnt, 0);
1574                           return 0;
1575                         case SCSI_ABORT_ERROR:
1576                         default:
1577                           SCpnt->internal_timeout &= ~IN_ABORT;
1578                           return 1;
1579                         }
1580                       }
1581               } 
1582       }
1583      
1584 int scsi_reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1585         {
1586         int temp, oldto;
1587         Scsi_Cmnd * SCpnt1;
1588         struct Scsi_Host * host = SCpnt->host;
1589         
1590 #ifdef DEBUG
1591         printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
1592 #endif
1593         while (1) {
1594                 cli();  
1595                 if (SCpnt->internal_timeout & IN_RESET)
1596                         {
1597                         sti();
1598                         while (SCpnt->internal_timeout & IN_RESET);
1599                         }
1600                 else
1601                         {
1602                         SCpnt->internal_timeout |= IN_RESET;
1603                         oldto = update_timeout(SCpnt, RESET_TIMEOUT);   
1604                                         
1605                         if (host->host_busy)
1606                                 {       
1607                                 sti();
1608                                 SCpnt1 = host->host_queue;
1609                                 while(SCpnt1) {
1610                                   if (SCpnt1->request.dev > 0) {
1611 #if 0                             
1612                                     if (!(SCpnt1->flags & IS_RESETTING) && 
1613                                       !(SCpnt1->internal_timeout & IN_ABORT))
1614                                     scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
1615 #endif
1616                                     SCpnt1->flags |= IS_RESETTING;
1617                                   }
1618                                   SCpnt1 = SCpnt1->next;
1619                                 };
1620 
1621                                 temp = host->hostt->reset(SCpnt);       
1622                                 }                               
1623                         else
1624                                 {
1625                                 host->host_busy++;
1626 
1627                                 if (host->block) {
1628                                    host_active = host;
1629                                    host_next = NULL;
1630                                    }
1631         
1632                                 sti();
1633                                 temp = host->hostt->reset(SCpnt);
1634                                 host->last_reset = jiffies;
1635                                 host->host_busy--;
1636 
1637                                 }
1638 
1639                         if (host->block && host->host_busy == 0) {
1640                            host_active = NULL;
1641                            host_next = NULL;
1642                            restart_all();
1643                            }
1644 
1645 
1646 #ifdef DEBUG
1647                         printk("scsi reset function returned %d\n", temp);
1648 #endif
1649                         switch(temp) {
1650                         case SCSI_RESET_SUCCESS:
1651                           cli();
1652                           SCpnt->internal_timeout &= ~IN_RESET;
1653                           update_timeout(SCpnt, oldto);
1654                           sti();
1655                           return 0;
1656                         case SCSI_RESET_PENDING:
1657                           return 0;
1658                         case SCSI_RESET_PUNT:
1659                         case SCSI_RESET_WAKEUP:
1660                           SCpnt->internal_timeout &= ~IN_RESET;
1661                           scsi_request_sense (SCpnt);
1662                           return 0;
1663                         case SCSI_RESET_SNOOZE:                   
1664                           /* In this case, we set the timeout field to 0
1665                              so that this command does not time out any more,
1666                              and we return 1 so that we get a message on the
1667                              screen. */
1668                           cli();
1669                           SCpnt->internal_timeout &= ~IN_RESET;
1670                           update_timeout(SCpnt, 0);
1671                           sti();
1672                           /* If you snooze, you lose... */
1673                         case SCSI_RESET_ERROR:
1674                         default:
1675                           return 1;
1676                         }
1677         
1678                         return temp;    
1679                         }
1680                 }
1681         }
1682                          
1683 
1684 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1685         {
1686         /*
1687                 We must not enter update_timeout with a timeout condition still pending.
1688         */
1689 
1690         int timed_out, pid;
1691         struct Scsi_Host * host;
1692         Scsi_Cmnd * SCpnt = NULL;
1693 
1694         do      {       
1695                 cli();
1696 
1697                 update_timeout(NULL, 0);
1698         /*
1699                 Find all timers such that they have 0 or negative (shouldn't happen)
1700                 time remaining on them.
1701         */
1702                         
1703                 timed_out = 0;
1704                 for(host = scsi_hostlist; host; host = host->next) {
1705                   for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1706                     if (SCpnt->timeout == -1)
1707                       {
1708                         SCpnt->timeout = 0;
1709                         pid = SCpnt->pid;
1710                         sti();
1711                         scsi_times_out(SCpnt, pid);
1712                         ++timed_out; 
1713                         cli();
1714                       }
1715                 };
1716               } while (timed_out);      
1717         sti();
1718       }
1719 
1720 /*
1721         The strategy is to cause the timer code to call scsi_times_out()
1722         when the soonest timeout is pending.  
1723         The arguments are used when we are queueing a new command, because
1724         we do not want to subtract the time used from this time, but when we
1725         set the timer, we want to take this value into account.
1726 */
1727         
1728 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1729         {
1730         unsigned int least, used;
1731         unsigned int oldto;
1732         struct Scsi_Host * host;
1733         Scsi_Cmnd * SCpnt = NULL;
1734 
1735         cli();
1736 
1737 /* 
1738         Figure out how much time has passed since the last time the timeouts 
1739         were updated 
1740 */
1741         used = (time_start) ? (jiffies - time_start) : 0;
1742 
1743 /*
1744         Find out what is due to timeout soonest, and adjust all timeouts for
1745         the amount of time that has passed since the last time we called 
1746         update_timeout. 
1747 */
1748         
1749         oldto = 0;
1750 
1751         if(SCset){
1752           oldto = SCset->timeout - used;
1753           SCset->timeout = timeout + used;
1754         };
1755 
1756         least = 0xffffffff;
1757 
1758         for(host = scsi_hostlist; host; host = host->next)
1759           for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1760             if (SCpnt->timeout > 0) {
1761               SCpnt->timeout -= used;
1762               if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
1763               if(SCpnt->timeout > 0 && SCpnt->timeout < least)
1764                 least = SCpnt->timeout;
1765             };
1766 
1767 /*
1768         If something is due to timeout again, then we will set the next timeout 
1769         interrupt to occur.  Otherwise, timeouts are disabled.
1770 */
1771         
1772         if (least != 0xffffffff)
1773                 {
1774                 time_start = jiffies;   
1775                 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;     
1776                 timer_active |= 1 << SCSI_TIMER;
1777                 }
1778         else
1779                 {
1780                 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1781                 timer_active &= ~(1 << SCSI_TIMER);
1782                 }       
1783         sti();
1784         return oldto;
1785         }               
1786 
1787 
1788 static unsigned short * dma_malloc_freelist = NULL;
1789 static unsigned int dma_sectors = 0;
1790 unsigned int dma_free_sectors = 0;
1791 unsigned int need_isa_buffer = 0;
1792 static unsigned char * dma_malloc_buffer = NULL;
1793 
1794 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1795 {
1796   unsigned int nbits, mask;
1797   unsigned long flags;
1798   int i, j;
1799   if((len & 0x1ff) || len > 8192)
1800     return NULL;
1801   
1802   save_flags(flags);
1803   cli();
1804   nbits = len >> 9;
1805   mask = (1 << nbits) - 1;
1806   
1807   for(i=0;i < (dma_sectors >> 4); i++)
1808     for(j=0; j<17-nbits; j++){
1809       if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1810         dma_malloc_freelist[i] |= (mask << j);
1811         restore_flags(flags);
1812         dma_free_sectors -= nbits;
1813 #ifdef DEBUG
1814         printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1815 #endif
1816         return (void *) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1817       };
1818     };
1819   restore_flags(flags);
1820   return NULL;  /* Nope.  No more */
1821 }
1822 
1823 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1824 {
1825   int offset;
1826   int page, sector, nbits, mask;
1827   unsigned long flags;
1828 
1829 #ifdef DEBUG
1830   printk("Sfree %x %d\n",obj, len);
1831 #endif
1832 
1833   offset = ((int) obj) - ((int) dma_malloc_buffer);
1834 
1835   if (offset < 0) panic("Bad offset");
1836   page = offset >> 13;
1837   sector = offset >> 9;
1838   if(sector >= dma_sectors) panic ("Bad page");
1839 
1840   sector = (offset >> 9) & 15;
1841   nbits = len >> 9;
1842   mask = (1 << nbits) - 1;
1843 
1844   if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1845 
1846   save_flags(flags);
1847   cli();
1848   if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1849     panic("Trying to free unused memory");
1850 
1851   dma_free_sectors += nbits;
1852   dma_malloc_freelist[page] &= ~(mask << sector);
1853   restore_flags(flags);
1854   return 0;
1855 }
1856 
1857 
1858 /* These are special functions that can be used to obtain memory at boot time.
1859    They act line a malloc function, but they simply take memory from the
1860    pool */
1861 
1862 static unsigned int scsi_init_memory_start = 0;
1863 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
1864 
1865 void * scsi_init_malloc(unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1866 {
1867   unsigned int retval;
1868   if(scsi_loadable_module_flag) {
1869     retval = (unsigned int) kmalloc(size, GFP_ATOMIC);
1870   } else {
1871     retval = scsi_init_memory_start;
1872     scsi_init_memory_start += size;
1873   }
1874   return (void *) retval;
1875 }
1876 
1877 
1878 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1879 { /* FIXME - not right.  We need to compare addresses to see whether this was
1880      kmalloc'd or not */
1881   if((unsigned int) ptr < scsi_loadable_module_flag) {
1882     kfree(ptr);
1883   } else {
1884     if(((unsigned int) ptr) + size == scsi_init_memory_start)
1885       scsi_init_memory_start = (unsigned int) ptr;
1886   }
1887 }
1888 
1889 /*
1890         scsi_dev_init() is our initialization routine, which in turn calls host 
1891         initialization, bus scanning, and sd/st initialization routines.  It 
1892         should be called from main().
1893 */
1894 
1895 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1896         {
1897         struct Scsi_Host * host = NULL;
1898         Scsi_Device * SDpnt;
1899         struct Scsi_Host * shpnt;
1900         struct Scsi_Device_Template * sdtpnt;
1901         Scsi_Cmnd * SCpnt;
1902 #ifdef FOO_ON_YOU
1903         return;
1904 #endif  
1905 
1906         /* Init a few things so we can "malloc" memory. */
1907         scsi_loadable_module_flag = 0;
1908         scsi_init_memory_start = memory_start;
1909 
1910         timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1911         timer_table[SCSI_TIMER].expires = 0;
1912 
1913         /* initialize all hosts */
1914         scsi_init(); 
1915                                 
1916         scsi_devices = (Scsi_Device *) NULL;
1917 
1918         for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
1919           scan_scsis(shpnt);           /* scan for scsi devices */
1920 
1921         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1922           if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
1923 
1924         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1925           int j;
1926           SDpnt->scsi_request_fn = NULL;
1927           for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1928             if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
1929           if(SDpnt->type != -1){
1930             for(j=0;j<SDpnt->host->cmd_per_lun;j++){
1931               SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd));
1932               SCpnt->host = SDpnt->host;
1933               SCpnt->device = SDpnt;
1934               SCpnt->target = SDpnt->id;
1935               SCpnt->lun = SDpnt->lun;
1936               SCpnt->request.dev = -1; /* Mark not busy */
1937               SCpnt->use_sg = 0;
1938               SCpnt->old_use_sg = 0;
1939               SCpnt->old_cmd_len = 0;
1940               SCpnt->timeout = 0;
1941               SCpnt->underflow = 0;
1942               SCpnt->transfersize = 0;
1943               SCpnt->host_scribble = NULL;
1944               host = SDpnt->host;
1945               if(host->host_queue)
1946                 host->host_queue->prev = SCpnt;
1947               SCpnt->next = host->host_queue;
1948               SCpnt->prev = NULL;
1949               host->host_queue = SCpnt;
1950             };
1951           };
1952         };
1953 
1954         if (scsi_devicelist)
1955           dma_sectors = 16;  /* Base value we use */
1956 
1957         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1958           host = SDpnt->host;
1959           
1960           if(SDpnt->type != TYPE_TAPE)
1961             dma_sectors += ((host->sg_tablesize * 
1962                              sizeof(struct scatterlist) + 511) >> 9) * 
1963                                host->cmd_per_lun;
1964           
1965           if(host->unchecked_isa_dma &&
1966              memory_end - 1 > ISA_DMA_THRESHOLD &&
1967              SDpnt->type != TYPE_TAPE) {
1968             dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
1969               host->cmd_per_lun;
1970             need_isa_buffer++;
1971           };
1972         };
1973 
1974         dma_sectors = (dma_sectors + 15) & 0xfff0;
1975         dma_free_sectors = dma_sectors;  /* This must be a multiple of 16 */
1976 
1977         scsi_init_memory_start = (scsi_init_memory_start + 3) & 0xfffffffc;
1978         dma_malloc_freelist = (unsigned short *) 
1979           scsi_init_malloc(dma_sectors >> 3);
1980         memset(dma_malloc_freelist, 0, dma_sectors >> 3);
1981 
1982         /* Some host adapters require buffers to be word aligned */
1983         if(scsi_init_memory_start & 1) scsi_init_memory_start++;
1984 
1985         dma_malloc_buffer = (unsigned char *) 
1986           scsi_init_malloc(dma_sectors << 9);
1987         
1988         /* OK, now we finish the initialization by doing spin-up, read
1989            capacity, etc, etc */
1990         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1991           if(sdtpnt->finish && sdtpnt->nr_dev)
1992             (*sdtpnt->finish)();
1993         
1994         scsi_loadable_module_flag = 1;
1995         return scsi_init_memory_start;
1996         }
1997 
1998 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
1999 {
2000         int i;
2001 
2002         printk("  Vendor: ");
2003         for (i = 8; i < 16; i++)
2004                 {
2005                 if (data[i] >= 0x20 && i < data[4] + 5)
2006                         printk("%c", data[i]);
2007                 else
2008                         printk(" ");
2009                 }
2010 
2011         printk("  Model: ");
2012         for (i = 16; i < 32; i++)
2013                 {
2014                 if (data[i] >= 0x20 && i < data[4] + 5)
2015                         printk("%c", data[i]);
2016                 else
2017                         printk(" ");
2018                 }
2019 
2020         printk("  Rev: ");
2021         for (i = 32; i < 36; i++)
2022                 {
2023                 if (data[i] >= 0x20 && i < data[4] + 5)
2024                         printk("%c", data[i]);
2025                 else
2026                         printk(" ");
2027                 }
2028 
2029         printk("\n");
2030 
2031         i = data[0] & 0x1f;
2032 
2033         printk("  Type:   %s ",
2034                i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2035         printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2036         if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2037           printk(" CCS\n");
2038         else
2039           printk("\n");
2040 }
2041 
2042 #ifdef NOT_YET
2043 /*
2044  * This entry point should be called by a loadable module if it is trying
2045  * add a low level scsi driver to the system.
2046  */
2047 static int scsi_register_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2048 {
2049   int pcount;
2050   struct Scsi_Host * shpnt;
2051   struct Scsi_Host * host = NULL;
2052   Scsi_Device * SDpnt;
2053   Scsi_Cmnd * SCpnt;
2054   struct Scsi_Device_Template * sdtpnt;
2055   int j, i;
2056   char * name;
2057 
2058   if (tpnt->next || !tpnt->detect) return 1;  /* Must be already loaded, or
2059                                                no detect routine available */
2060   pcount = next_scsi_host;
2061   if ((tpnt->present = tpnt->detect(tpnt)))
2062     {
2063       if(pcount == next_scsi_host) {
2064         if(tpnt->present > 1) {
2065           printk("Failure to register low-level scsi driver");
2066           scsi_unregister_host(tpnt);
2067           return 1;
2068         }
2069         /* The low-level driver failed to register a driver.  We
2070            can do this now. */
2071         scsi_register(tpnt,0);
2072       }
2073       tpnt->next = scsi_hosts; /* Add to the linked list */
2074       scsi_hosts = tpnt;
2075 
2076       for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2077         if(shpnt->hostt == tpnt)
2078           {
2079             if(tpnt->info)
2080               name = tpnt->info(shpnt);
2081             else
2082               name = tpnt->name;
2083             printk ("scsi%d : %s\n", /* And print a little message */
2084                     shpnt->host_no, name);
2085           }
2086       /* The next step is to call scan_scsis here.  This generates the
2087          Scsi_Devices entries */
2088 
2089       for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2090         if(shpnt->hostt == tpnt) scan_scsis(shpnt);
2091 
2092       for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2093         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2094 
2095       /* Next we create the Scsi_Cmnd structures for this host */
2096 
2097       for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2098         if(SDpnt->host->hostt == tpnt)
2099           {
2100             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2101               if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2102             if(SDpnt->attached){
2103               for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2104                 SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2105                 SCpnt->host = SDpnt->host;
2106                 SCpnt->device = SDpnt;
2107                 SCpnt->target = SDpnt->id;
2108                 SCpnt->lun = SDpnt->lun;
2109                 SCpnt->request.dev = -1; /* Mark not busy */
2110                 SCpnt->request.sem = NULL;
2111                 SCpnt->use_sg = 0;
2112                 SCpnt->old_use_sg = 0;
2113                 SCpnt->underflow = 0;
2114                 SCpnt->timeout = 0;
2115                 SCpnt->transfersize = 0;
2116                 SCpnt->host_scribble = NULL;
2117                 host = SDpnt->host;
2118                 SCpnt->next = host->host_queue;
2119                 SCpnt->prev = NULL;
2120                 host->host_queue = SCpnt;
2121                 if(host->host_queue)
2122                   host->host_queue->prev = SCpnt;
2123               };
2124             };
2125           }
2126         /* Next, check to see if we need to extend the DMA buffer pool */
2127       {
2128           unsigned char * new_dma_malloc_freelist = NULL;
2129           unsigned int new_dma_sectors = 0;
2130           unsigned int new_need_isa_buffer = 0;
2131           unsigned char ** new_dma_malloc_pages = NULL;
2132           
2133           if (scsi_devicelist)
2134             new_dma_sectors = 16;  /* Base value we use */
2135           
2136           for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2137             host = SDpnt->host;
2138             
2139             if(SDpnt->type != TYPE_TAPE)
2140               new_dma_sectors += ((host->sg_tablesize * 
2141                                    sizeof(struct scatterlist) + 511) >> 9) * 
2142                                      host->cmd_per_lun;
2143             
2144             if(host->unchecked_isa_dma &&
2145                scsi_need_isa_bounce_buffers &&
2146                SDpnt->type != TYPE_TAPE) {
2147               new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2148                 host->cmd_per_lun;
2149               new_need_isa_buffer++;
2150             };
2151           };
2152           
2153           new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2154 
2155           new_dma_malloc_freelist = (unsigned char *) 
2156             scsi_init_malloc(new_dma_sectors >> 3, GFP_ATOMIC);
2157           memset(new_dma_malloc_freelist, 0, new_dma_sectors >> 3);
2158           
2159           new_dma_malloc_pages = (unsigned char **) 
2160             scsi_init_malloc(new_dma_sectors >> 1, GFP_ATOMIC);
2161           memset(new_dma_malloc_pages, 0, new_dma_sectors >> 1);
2162           
2163           for(i=dma_sectors >> 3; i< new_dma_sectors >> 3; i++)
2164             new_dma_malloc_pages[i] = (unsigned char *) 
2165               scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2166           
2167 
2168           /* When we dick with the actual DMA list, we need to protect things */
2169           cli();
2170           memcpy(new_dma_malloc_freelist, dma_malloc_freelist, dma_sectors >> 3);
2171           scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2172           dma_malloc_freelist = new_dma_malloc_freelist;
2173           
2174           memcpy(new_dma_malloc_pages, dma_malloc_pages, dma_sectors >> 1);
2175           scsi_init_free((char *) dma_malloc_pages, dma_sectors>>1);
2176           
2177           dma_free_sectors += new_dma_sectors - dma_sectors;
2178           dma_malloc_pages = new_dma_malloc_pages;
2179           dma_sectors = new_dma_sectors;
2180           need_isa_buffer = new_need_isa_buffer;
2181           sti();
2182 
2183           
2184         }
2185       /* This does any final handling that is required. */
2186       for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2187         if(sdtpnt->finish) (*sdtpnt->finish)();
2188     }
2189   return 0;
2190 }
2191 
2192 /*
2193  * Similarly, this entry point should be called by a loadable module if it
2194  * is trying to remove a low level scsi driver from the system.
2195  */
2196 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2197 {  
2198   Scsi_Host_Template * SHT, *SHTp;
2199   Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
2200   Scsi_Cmnd * SCpnt;
2201   struct Scsi_Device_Template * sdtpnt;
2202   struct Scsi_Host * shpnt, *sh1;
2203   int pcount;
2204 
2205   /* First verify that this host adapter is completely free with no pending
2206      commands */
2207 
2208   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2209     if(sdpnt->host->hostt == tpnt && *sdpnt->host->hostt->usage_count) return;
2210 
2211   for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2212     {
2213       if (shpnt->hostt != tpnt) continue;
2214       for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2215         {
2216           cli();
2217           if(SCpnt->request.dev != -1) {
2218             sti();
2219             for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2220               if(SCpnt->request.dev == 0xffe0) SCpnt->request.dev = -1;
2221             printk("Device busy???\n");
2222             return;
2223           }
2224           SCpnt->request.dev = 0xffe0;  /* Mark as busy */
2225         }
2226     }
2227   /* Next we detach the high level drivers from the Scsi_Device structures */
2228 
2229   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2230     if(sdpnt->host->hostt == tpnt)
2231       {
2232         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2233           if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
2234         /* If something still attached, punt */
2235         if (sdpnt->attached) {
2236           printk("Attached usage count = %d\n", sdpnt->attached);
2237           return;
2238         }
2239       }
2240   
2241   /* Next we free up the Scsi_Cmnd structures for this host */
2242 
2243   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2244     if(sdpnt->host->hostt == tpnt)
2245       while (sdpnt->host->host_queue) {
2246         SCpnt = sdpnt->host->host_queue->next;
2247         scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
2248         sdpnt->host->host_queue = SCpnt;
2249         if (SCpnt) SCpnt->prev = NULL;
2250       }
2251   
2252   /* Next free up the Scsi_Device structures for this host */
2253 
2254   sdppnt = NULL;
2255   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
2256     {
2257       sdpnt1 = sdpnt->next;
2258       if (sdpnt->host->hostt == tpnt) {
2259         if (sdppnt)
2260           sdppnt->next = sdpnt->next;
2261         else
2262           scsi_devices = sdpnt->next;
2263         scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
2264       } else
2265         sdppnt = sdpnt;
2266     }
2267 
2268   /* Next we go through and remove the instances of the individual hosts
2269      that were detected */
2270 
2271   shpnt = scsi_hostlist;
2272   while(shpnt) { 
2273    sh1 = shpnt->next;
2274     if(shpnt->hostt == tpnt) {
2275       if(shpnt->loaded_as_module) {
2276         pcount = next_scsi_host;
2277         if(tpnt->release)
2278           (*tpnt->release)(shpnt);
2279         else {
2280           /* This is the default case for the release function.  It should do the right
2281              thing for most correctly written host adapters. */
2282           if (shpnt->irq) free_irq(shpnt->irq);
2283           if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
2284           if (shpnt->io_port && shpnt->n_io_port) 
2285             release_region(shpnt->io_port, shpnt->n_io_port);
2286         }
2287         if(pcount == next_scsi_host) scsi_unregister(shpnt);
2288         tpnt->present--;
2289       }
2290     }
2291     shpnt = sh1;
2292   }
2293 
2294   /* There were some hosts that were loaded at boot time, so we cannot
2295      do any more than this */
2296   if (tpnt->present) return;
2297 
2298   /* OK, this is the very last step.  Remove this host adapter from the
2299      linked list. */
2300   for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
2301     if(SHT == tpnt) {
2302       if(SHTp)
2303         SHTp->next = SHT->next;
2304       else
2305         scsi_hosts = SHT->next;
2306       SHT->next = NULL;
2307       break;
2308     }
2309 }
2310 
2311 int scsi_register_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2312 {
2313   switch(module_type){
2314   case MODULE_SCSI_HA:
2315     return scsi_register_host((Scsi_Host_Template *) ptr);
2316     /* The rest of these are not yet implemented */
2317 
2318     /* Load constants.o */
2319   case MODULE_SCSI_CONST:
2320 
2321     /* Load specialized ioctl handler for some device.  Intended for cdroms that
2322        have non-SCSI2 audio command sets. */
2323   case MODULE_SCSI_IOCTL:
2324 
2325     /* Load upper level device handler of some kind */
2326   case MODULE_SCSI_DEV:
2327   default:
2328     return 1;
2329   }
2330 }
2331 
2332 void scsi_unregister_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2333 {
2334   switch(module_type) {
2335   case MODULE_SCSI_HA:
2336     scsi_unregister_host((Scsi_Host_Template *) ptr);
2337     break;
2338     /* The rest of these are not yet implemented. */
2339   case MODULE_SCSI_CONST:
2340   case MODULE_SCSI_IOCTL:
2341   case MODULE_SCSI_DEV:
2342   default:
2343   }
2344   return;
2345 }
2346 #endif
2347 
2348 #ifdef DEBUG_TIMEOUT
2349 static void 
2350 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2351 {
2352   int i;
2353   struct Scsi_Host * shpnt;
2354   Scsi_Cmnd * SCpnt;
2355   printk("Dump of scsi parameters:\n");
2356   i = 0;
2357   for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2358     for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2359       {
2360         /*  (0) 0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
2361         printk("(%d) %d:%d:%d (%4.4x %ld %ld %ld %ld) (%d %d %x) (%d %d %d) %x %x %x\n",
2362                i++, SCpnt->host->host_no,
2363                SCpnt->target,
2364                SCpnt->lun,
2365                SCpnt->request.dev,
2366                SCpnt->request.sector,
2367                SCpnt->request.nr_sectors,
2368                SCpnt->request.current_nr_sectors,
2369                SCpnt->use_sg,
2370                SCpnt->retries,
2371                SCpnt->allowed,
2372                SCpnt->flags,
2373                SCpnt->timeout_per_command,
2374                SCpnt->timeout,
2375                SCpnt->internal_timeout,
2376                SCpnt->cmnd[0],
2377                SCpnt->sense_buffer[2],
2378                SCpnt->result);
2379       };
2380   printk("wait_for_request = %p\n", wait_for_request);
2381   /* Now dump the request lists for each block device */
2382   printk("Dump of pending block device requests\n");
2383   for(i=0; i<MAX_BLKDEV; i++)
2384     if(blk_dev[i].current_request)
2385       {
2386         struct request * req;
2387         printk("%d: ", i);
2388         req = blk_dev[i].current_request;
2389         while(req) {
2390           printk("(%x %d %ld %ld %ld) ",
2391                  req->dev,
2392                  req->cmd,
2393                  req->sector,
2394                  req->nr_sectors,
2395                  req->current_nr_sectors);
2396           req = req->next;
2397         }
2398         printk("\n");
2399       }
2400 }
2401 #endif
2402 
2403 

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