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   unsigned int flags;
 687   struct buffer_head * bh, *bhp;
 688   struct Scsi_Host * host;
 689   Scsi_Cmnd * SCpnt = NULL;
 690   Scsi_Cmnd * SCwait = NULL;
 691 
 692   if (!device)
 693     panic ("No device passed to allocate_device().\n");
 694   
 695   if (reqp) req = *reqp;
 696 
 697     /* See if this request has already been queued by an interrupt routine */
 698   if (req && (dev = req->dev) <= 0) return NULL;
 699   
 700   host = device->host;
 701   
 702   if (intr_count && host->can_queue &&
 703                       ((host->block && host_active && host != host_active) ||
 704                        (host->block && host_next && host != host_next) ||
 705                        host->host_busy >= host->can_queue)) {
 706 
 707      scsi_maybe_deadlocked = 1;
 708 
 709      if (host->block && !host_next && host_active && host != host_active) 
 710         host_next = host;
 711 
 712      return NULL;
 713      }
 714 
 715   while (1==1){
 716     SCpnt = host->host_queue;
 717     while(SCpnt){
 718       if(SCpnt->target == device->id &&
 719          SCpnt->lun == device->lun) {
 720         SCwait = SCpnt;
 721         if(SCpnt->request.dev < 0) break;
 722       };
 723       SCpnt = SCpnt->next;
 724     };
 725     save_flags(flags);
 726     cli();
 727     /* See if this request has already been queued by an interrupt routine */
 728     if (req && ((req->dev < 0) || (req->dev != dev))) {
 729       restore_flags(flags);
 730       return NULL;
 731     };
 732     if (!SCpnt || SCpnt->request.dev >= 0)  /* Might have changed */
 733       {
 734         restore_flags(flags);
 735         if(!wait) return NULL;
 736         if (!SCwait) {
 737           printk("Attempt to allocate device target %d, lun %d\n",
 738                  device->id ,device->lun);
 739           panic("No device found in allocate_device\n");
 740         };
 741         SCSI_SLEEP(&device->device_wait, 
 742                    (SCwait->request.dev > 0));
 743       } else {
 744         if (req) {
 745           memcpy(&SCpnt->request, req, sizeof(struct request));
 746           tablesize = device->host->sg_tablesize;
 747           bhp = bh = req->bh;
 748           if(!tablesize) bh = NULL;
 749           /* Take a quick look through the table to see how big it is.  We already
 750              have our copy of req, so we can mess with that if we want to.  */
 751           while(req->nr_sectors && bh){
 752             bhp = bhp->b_reqnext;
 753             if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
 754             req->nr_sectors -= bh->b_size >> 9;
 755             req->sector += bh->b_size >> 9;
 756             if(!tablesize) break;
 757             bh = bhp;
 758           };
 759           if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
 760             SCpnt->request.bhtail = bh;
 761             req->bh = bh->b_reqnext; /* Divide request */
 762             bh->b_reqnext = NULL;
 763             bh = req->bh;
 764             /* Now reset things so that req looks OK */
 765             SCpnt->request.nr_sectors -= req->nr_sectors;
 766             req->current_nr_sectors = bh->b_size >> 9;
 767             req->buffer = bh->b_data;
 768             SCpnt->request.sem = NULL; /* Wait until whole thing done */
 769           }
 770           else 
 771             {
 772               req->dev = -1;
 773               *reqp = req->next;
 774               wake_up(&wait_for_request);
 775             };
 776         } else {
 777           SCpnt->request.dev = 0xffff; /* Busy */
 778           SCpnt->request.sem = NULL;  /* And no one is waiting for this to complete */
 779         };
 780         restore_flags(flags);
 781         break;
 782       };
 783   };
 784 
 785   SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
 786   SCpnt->old_use_sg  = 0;
 787   SCpnt->transfersize = 0;      /* No default transfer size */
 788   SCpnt->cmd_len = 0;
 789   SCpnt->underflow = 0;         /* Do not flag underflow conditions */
 790   return SCpnt;
 791 }
 792 
 793 /*
 794         This is inline because we have stack problemes if we recurse to deeply.
 795 */
 796                          
 797 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 798         {
 799         int temp;
 800         struct Scsi_Host * host;
 801 #ifdef DEBUG_DELAY      
 802         int clock;
 803 #endif
 804 
 805         if ((unsigned long) &SCpnt < current->kernel_stack_page)
 806           panic("Kernel stack overflow.");
 807 
 808         host = SCpnt->host;
 809 
 810 /*
 811         We will wait MIN_RESET_DELAY clock ticks after the last reset so 
 812         we can avoid the drive not being ready.
 813 */ 
 814 temp = host->last_reset;
 815 while (jiffies < temp);
 816 
 817 update_timeout(SCpnt, SCpnt->timeout_per_command);
 818 
 819 /*
 820         We will use a queued command if possible, otherwise we will emulate the
 821         queuing and calling of completion function ourselves. 
 822 */
 823 #ifdef DEBUG
 824         printk("internal_cmnd (host = %d, target = %d, command = %08x, buffer =  %08x, \n"
 825                 "bufflen = %d, done = %08x)\n", SCpnt->host->host_no, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
 826 #endif
 827 
 828         if (host->can_queue)
 829                 {
 830 #ifdef DEBUG
 831         printk("queuecommand : routine at %08x\n", 
 832                 host->hostt->queuecommand);
 833 #endif
 834                   /* This locking tries to prevent all sorts of races between
 835                      queuecommand and the interrupt code.  In effect,
 836                      we are only allowed to be in queuecommand once at
 837                      any given time, and we can only be in the interrupt
 838                      handler and the queuecommand function at the same time
 839                      when queuecommand is called while servicing the
 840                      interrupt. */
 841 
 842                 if(!intr_count && SCpnt->host->irq)
 843                   disable_irq(SCpnt->host->irq);
 844 
 845                 host->hostt->queuecommand (SCpnt, scsi_done);
 846 
 847                 if(!intr_count && SCpnt->host->irq)
 848                   enable_irq(SCpnt->host->irq);
 849                 }
 850         else
 851                 {
 852 
 853 #ifdef DEBUG
 854         printk("command() :  routine at %08x\n", host->hostt->command);
 855 #endif
 856                 temp=host->hostt->command (SCpnt);
 857                 SCpnt->result = temp;
 858 #ifdef DEBUG_DELAY
 859         clock = jiffies + 400;
 860         while (jiffies < clock);
 861         printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp, done);
 862 #endif
 863                 scsi_done(SCpnt);
 864                 }       
 865 #ifdef DEBUG
 866         printk("leaving internal_cmnd()\n");
 867 #endif
 868         }       
 869 
 870 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 871         {
 872         unsigned int flags;
 873         
 874         save_flags(flags);
 875         cli();
 876         SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
 877         update_timeout(SCpnt, SENSE_TIMEOUT);
 878         restore_flags(flags);
 879         
 880 
 881         memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
 882                 sizeof(generic_sense));
 883 
 884         SCpnt->cmnd[1] = SCpnt->lun << 5;       
 885         SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
 886 
 887         SCpnt->request_buffer = &SCpnt->sense_buffer;
 888         SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
 889         SCpnt->use_sg = 0;
 890         SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
 891         internal_cmnd (SCpnt);
 892         }
 893 
 894 
 895 
 896 /*
 897         scsi_do_cmd sends all the commands out to the low-level driver.  It 
 898         handles the specifics required for each low level driver - ie queued 
 899         or non queued.  It also prevents conflicts when different high level 
 900         drivers go for the same host at the same time.
 901 */
 902 
 903 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd , 
     /* [previous][next][first][last][top][bottom][index][help] */
 904                   void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
 905                   int timeout, int retries 
 906                    )
 907         {
 908         unsigned long flags;
 909         struct Scsi_Host * host = SCpnt->host;
 910 
 911 #ifdef DEBUG
 912         {
 913         int i;  
 914         int target = SCpnt->target;
 915         printk ("scsi_do_cmd (host = %d, target = %d, buffer =%08x, "
 916                 "bufflen = %d, done = %08x, timeout = %d, retries = %d)\n"
 917                 "command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
 918         for (i = 0; i < 10; ++i)
 919                 printk ("%02x  ", ((unsigned char *) cmnd)[i]); 
 920         printk("\n");
 921       };
 922 #endif
 923         
 924         if (!host)
 925                 {
 926                 panic ("Invalid or not present host.\n");
 927                 }
 928 
 929         
 930 /*
 931         We must prevent reentrancy to the lowlevel host driver.  This prevents 
 932         it - we enter a loop until the host we want to talk to is not busy.   
 933         Race conditions are prevented, as interrupts are disabled in between the
 934         time we check for the host being not busy, and the time we mark it busy
 935         ourselves.
 936 */
 937 
 938         SCpnt->pid = scsi_pid++; 
 939 
 940         for(;;) {
 941 
 942           save_flags(flags);
 943           cli();
 944 
 945           if (host->can_queue &&
 946                 ((host->block && host_active && host != host_active) ||
 947                  (host->block && host_next && host != host_next) ||
 948                  host->host_busy >= host->can_queue)) {
 949 
 950              if (intr_count) 
 951                 panic("scsi: queueing to inactive host while in interrupt.\n");
 952 
 953              if (host->block && !host_next && host_active 
 954                                            && host != host_active) 
 955                 host_next = host;
 956 
 957              restore_flags(flags);
 958              SCSI_SLEEP(&host->host_wait, 
 959                        ((host->block && host_active && host != host_active) ||
 960                         (host->block && host_next && host != host_next) ||
 961                         host->host_busy >= host->can_queue));
 962              } 
 963 
 964           else {
 965              host->host_busy++;
 966 
 967              if (host->block) {
 968 
 969                 if (!host_active) {
 970                    max_active = 0;
 971                    host_active = host;
 972                    host_next = NULL;
 973                    }
 974                 else if (max_active > 7 && !host_next) 
 975                    host_next = host->block;
 976 
 977                 max_active++;
 978                 }
 979 
 980              restore_flags(flags);
 981              break;
 982              }
 983           }
 984 /*
 985         Our own function scsi_done (which marks the host as not busy, disables 
 986         the timeout counter, etc) will be called by us or by the 
 987         scsi_hosts[host].queuecommand() function needs to also call
 988         the completion function for the high level driver.
 989 
 990 */
 991 
 992         memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
 993 #if 0
 994         SCpnt->host = host;
 995         SCpnt->target = target;
 996         SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
 997 #endif
 998         SCpnt->bufflen = bufflen;
 999         SCpnt->buffer = buffer;
1000         SCpnt->flags=0;
1001         SCpnt->retries=0;
1002         SCpnt->allowed=retries;
1003         SCpnt->done = done;
1004         SCpnt->timeout_per_command = timeout;
1005                                 
1006         memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
1007         /* Zero the sense buffer.  Some host adapters automatically request
1008            sense on error.  0 is not a valid sense code.  */
1009         memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1010         SCpnt->request_buffer = buffer;
1011         SCpnt->request_bufflen = bufflen;
1012         SCpnt->old_use_sg = SCpnt->use_sg;
1013         if (SCpnt->cmd_len == 0)
1014                 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1015         SCpnt->old_cmd_len = SCpnt->cmd_len;
1016 
1017         /* Start the timer ticking.  */
1018 
1019         SCpnt->internal_timeout = 0;
1020         SCpnt->abort_reason = 0;
1021         internal_cmnd (SCpnt);
1022 
1023 #ifdef DEBUG
1024         printk ("Leaving scsi_do_cmd()\n");
1025 #endif
1026         }
1027 
1028 
1029 
1030 /*
1031         The scsi_done() function disables the timeout timer for the scsi host, 
1032         marks the host as not busy, and calls the user specified completion 
1033         function for that host's current command.
1034 */
1035 
1036 static void reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1037 {
1038 #ifdef DEBUG
1039         printk("scsi: reset(%d)\n", SCpnt->host->host_no);
1040 #endif
1041         
1042         SCpnt->flags |= (WAS_RESET | IS_RESETTING);
1043         scsi_reset(SCpnt);
1044         
1045 #ifdef DEBUG
1046         printk("performing request sense\n");
1047 #endif
1048         
1049 #if 0  /* FIXME - remove this when done */
1050         if(SCpnt->flags & NEEDS_JUMPSTART) {
1051           SCpnt->flags &= ~NEEDS_JUMPSTART;
1052           scsi_request_sense (SCpnt);
1053         };
1054 #endif
1055 }
1056         
1057         
1058 
1059 static int check_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1060         {
1061   /* If there is no sense information, request it.  If we have already
1062      requested it, there is no point in asking again - the firmware must be
1063      confused. */
1064   if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1065     if(!(SCpnt->flags & ASKED_FOR_SENSE))
1066       return SUGGEST_SENSE;
1067     else
1068       return SUGGEST_RETRY;
1069       }
1070   
1071   SCpnt->flags &= ~ASKED_FOR_SENSE;
1072 
1073 #ifdef DEBUG_INIT
1074         printk("scsi%d : ", SCpnt->host->host_no);
1075         print_sense("", SCpnt);
1076         printk("\n");
1077 #endif
1078                 if (SCpnt->sense_buffer[2] &0xe0)
1079                   return SUGGEST_ABORT;
1080 
1081                 switch (SCpnt->sense_buffer[2] & 0xf)
1082                 {
1083                 case NO_SENSE:
1084                         return 0;
1085                 case RECOVERED_ERROR:
1086                         if (SCpnt->device->type == TYPE_TAPE)
1087                           return SUGGEST_IS_OK;
1088                         else
1089                           return 0;
1090 
1091                 case ABORTED_COMMAND:
1092                         return SUGGEST_RETRY;   
1093                 case NOT_READY:
1094                 case UNIT_ATTENTION:
1095                         return SUGGEST_ABORT;
1096 
1097                 /* these three are not supported */     
1098                 case COPY_ABORTED:
1099                 case VOLUME_OVERFLOW:
1100                 case MISCOMPARE:
1101         
1102                 case MEDIUM_ERROR:
1103                         return SUGGEST_REMAP;
1104                 case BLANK_CHECK:
1105                 case DATA_PROTECT:
1106                 case HARDWARE_ERROR:
1107                 case ILLEGAL_REQUEST:
1108                 default:
1109                         return SUGGEST_ABORT;
1110                 }
1111               }
1112 
1113 /* This function is the mid-level interrupt routine, which decides how
1114  *  to handle error conditions.  Each invocation of this function must
1115  *  do one and *only* one of the following:
1116  *
1117  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
1118  *      normal completion, and indicates that the handling for this
1119  *      request is complete.
1120  *  (2) Call internal_cmnd to requeue the command.  This will result in
1121  *      scsi_done being called again when the retry is complete.
1122  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
1123  *      more information about the error condition.  When the information
1124  *      is available, scsi_done will be called again.
1125  *  (4) Call reset().  This is sort of a last resort, and the idea is that
1126  *      this may kick things loose and get the drive working again.  reset()
1127  *      automatically calls scsi_request_sense, and thus scsi_done will be
1128  *      called again once the reset is complete.
1129  *
1130  *      If none of the above actions are taken, the drive in question
1131  * will hang. If more than one of the above actions are taken by
1132  * scsi_done, then unpredictable behavior will result.
1133  */
1134 static void scsi_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1135         {
1136         int status=0;
1137         int exit=0;
1138         int checked;
1139         int oldto;
1140         struct Scsi_Host * host = SCpnt->host;
1141         int result = SCpnt->result;
1142         oldto = update_timeout(SCpnt, 0);
1143 
1144 #ifdef DEBUG_TIMEOUT
1145         if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1146                           result, SCpnt->target, SCpnt->lun);
1147 #endif
1148 
1149         /* If we requested an abort, (and we got it) then fix up the return
1150            status to say why */
1151         if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1152           SCpnt->result = result = (result & 0xff00ffff) | 
1153             (SCpnt->abort_reason << 16);
1154 
1155 
1156 #define FINISHED 0
1157 #define MAYREDO  1
1158 #define REDO     3
1159 #define PENDING  4
1160 
1161 #ifdef DEBUG
1162         printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1163 #endif
1164 
1165         if(SCpnt->flags & WAS_SENSE)
1166         {
1167                 SCpnt->use_sg = SCpnt->old_use_sg;
1168                 SCpnt->cmd_len = SCpnt->old_cmd_len;
1169         }
1170 
1171         switch (host_byte(result))      
1172         {
1173         case DID_OK:
1174                 if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1175                         /* Failed to obtain sense information */
1176                         {
1177                         SCpnt->flags &= ~WAS_SENSE;
1178                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1179 
1180                         if (!(SCpnt->flags & WAS_RESET))
1181                                 {
1182                                 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n", 
1183                                         SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
1184                                 reset(SCpnt);
1185                                 return;
1186                                 }
1187                         else
1188                                 {
1189                                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1190                                 status = FINISHED;
1191                                 }
1192                         }
1193                 else switch(msg_byte(result))
1194                         {
1195                         case COMMAND_COMPLETE:
1196                         switch (status_byte(result))
1197                         {
1198                         case GOOD:
1199                                 if (SCpnt->flags & WAS_SENSE)
1200                                         {
1201 #ifdef DEBUG
1202         printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1203 #endif
1204 
1205                                         SCpnt->flags &= ~WAS_SENSE;
1206                                         SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1207         
1208                                         switch (checked = check_sense(SCpnt))
1209                                         {
1210                                         case SUGGEST_SENSE:
1211                                         case 0: 
1212 #ifdef DEBUG
1213         printk("NO SENSE.  status = REDO\n");
1214 #endif
1215 
1216                                                 update_timeout(SCpnt, oldto);
1217                                                 status = REDO;
1218                                                 break;
1219                                         case SUGGEST_IS_OK:
1220                                                 break;
1221                                         case SUGGEST_REMAP:                     
1222                                         case SUGGEST_RETRY: 
1223 #ifdef DEBUG
1224         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1225 #endif
1226 
1227                                                 status = MAYREDO;
1228                                                 exit = DRIVER_SENSE | SUGGEST_RETRY;
1229                                                 break;
1230                                         case SUGGEST_ABORT:
1231 #ifdef DEBUG
1232         printk("SENSE SUGGEST ABORT - status = FINISHED");
1233 #endif
1234 
1235                                                 status = FINISHED;
1236                                                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
1237                                                 break;
1238                                         default:
1239                                                 printk ("Internal error %s %d \n", __FILE__, 
1240                                                         __LINE__);
1241                                         }                          
1242                                         }       
1243                                 else
1244                                         {
1245 #ifdef DEBUG
1246         printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1247 #endif
1248 
1249                                         exit =  DRIVER_OK;
1250                                         status = FINISHED;
1251                                         }
1252                                 break;  
1253 
1254                         case CHECK_CONDITION:
1255                                 switch (check_sense(SCpnt))
1256                                   {
1257                                   case 0: 
1258                                     update_timeout(SCpnt, oldto);
1259                                     status = REDO;
1260                                     break;
1261                                   case SUGGEST_REMAP:                   
1262                                   case SUGGEST_RETRY:
1263                                     status = MAYREDO;
1264                                     exit = DRIVER_SENSE | SUGGEST_RETRY;
1265                                     break;
1266                                   case SUGGEST_ABORT:
1267                                     status = FINISHED;
1268                                     exit =  DRIVER_SENSE | SUGGEST_ABORT;
1269                                     break;
1270                                   case SUGGEST_SENSE:
1271                                 scsi_request_sense (SCpnt);
1272                                 status = PENDING;
1273                                 break;          
1274                                   }
1275                                 break;          
1276                         
1277                         case CONDITION_GOOD:
1278                         case INTERMEDIATE_GOOD:
1279                         case INTERMEDIATE_C_GOOD:
1280                                 break;
1281                                 
1282                         case BUSY:
1283                                 update_timeout(SCpnt, oldto);
1284                                 status = REDO;
1285                                 break;
1286 
1287                         case RESERVATION_CONFLICT:
1288                                 printk("scsi%d : RESERVATION CONFLICT performing reset.\n", 
1289                                         SCpnt->host->host_no);
1290                                 reset(SCpnt);
1291                                 return;
1292 #if 0
1293                                 exit = DRIVER_SOFT | SUGGEST_ABORT;
1294                                 status = MAYREDO;
1295                                 break;
1296 #endif
1297                         default:
1298                                 printk ("Internal error %s %d \n"
1299                                         "status byte = %d \n", __FILE__, 
1300                                         __LINE__, status_byte(result));
1301                                 
1302                         }
1303                         break;
1304                         default:
1305                                 panic("scsi: unsupported message byte %d received\n", msg_byte(result)); 
1306                         }
1307                         break;
1308         case DID_TIME_OUT:      
1309 #ifdef DEBUG
1310         printk("Host returned DID_TIME_OUT - ");
1311 #endif
1312 
1313                 if (SCpnt->flags & WAS_TIMEDOUT)
1314                         {
1315 #ifdef DEBUG
1316         printk("Aborting\n");
1317 #endif  
1318                         exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1319                         }               
1320                 else 
1321                         {
1322 #ifdef DEBUG
1323                         printk ("Retrying.\n");
1324 #endif
1325                         SCpnt->flags  |= WAS_TIMEDOUT;
1326                         SCpnt->internal_timeout &= ~IN_ABORT;
1327                         status = REDO;
1328                         }
1329                 break;
1330         case DID_BUS_BUSY:
1331         case DID_PARITY:
1332                 status = REDO;
1333                 break;
1334         case DID_NO_CONNECT:
1335 #ifdef DEBUG
1336                 printk("Couldn't connect.\n");
1337 #endif
1338                 exit  = (DRIVER_HARD | SUGGEST_ABORT);
1339                 break;
1340         case DID_ERROR: 
1341                 status = MAYREDO;
1342                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1343                 break;
1344         case DID_BAD_TARGET:
1345         case DID_ABORT:
1346                 exit = (DRIVER_INVALID | SUGGEST_ABORT);
1347                 break;  
1348         case DID_RESET:
1349                 if (SCpnt->flags & IS_RESETTING)
1350                         {
1351                         SCpnt->flags &= ~IS_RESETTING;
1352                         status = REDO;
1353                         break;
1354                         }
1355 
1356                 if(msg_byte(result) == GOOD &&
1357                       status_byte(result) == CHECK_CONDITION) {
1358                         switch (check_sense(SCpnt)) {
1359                         case 0: 
1360                             update_timeout(SCpnt, oldto);
1361                             status = REDO;
1362                             break;
1363                         case SUGGEST_REMAP:                     
1364                         case SUGGEST_RETRY:
1365                             status = MAYREDO;
1366                             exit = DRIVER_SENSE | SUGGEST_RETRY;
1367                             break;
1368                         case SUGGEST_ABORT:
1369                             status = FINISHED;
1370                             exit =  DRIVER_SENSE | SUGGEST_ABORT;
1371                             break;
1372                         case SUGGEST_SENSE:
1373                               scsi_request_sense (SCpnt);
1374                               status = PENDING;
1375                               break;
1376                         }
1377                 } else {
1378                 status=REDO;
1379                 exit = SUGGEST_RETRY;
1380                 }
1381                 break;
1382         default :               
1383                 exit = (DRIVER_ERROR | SUGGEST_DIE);
1384         }
1385 
1386         switch (status) 
1387                 {
1388                 case FINISHED:
1389                 case PENDING:
1390                         break;
1391                 case MAYREDO:
1392 
1393 #ifdef DEBUG
1394         printk("In MAYREDO, allowing %d retries, have %d\n",
1395                SCpnt->allowed, SCpnt->retries);
1396 #endif
1397 
1398                         if ((++SCpnt->retries) < SCpnt->allowed)
1399                         {
1400                         if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1401                             && !(SCpnt->flags & WAS_RESET))
1402                                 {
1403                                         printk("scsi%d : resetting for second half of retries.\n",
1404                                                 SCpnt->host->host_no);
1405                                         reset(SCpnt);
1406                                         break;
1407                                 }
1408 
1409                         }
1410                         else
1411                                 {
1412                                 status = FINISHED;
1413                                 break;
1414                                 }
1415                         /* fall through to REDO */
1416 
1417                 case REDO:
1418 
1419                         if (host->block) scsi_maybe_deadlocked = 1;
1420 
1421                         if (SCpnt->flags & WAS_SENSE)                   
1422                                 scsi_request_sense(SCpnt);      
1423                         else    
1424                           {
1425                             memcpy ((void *) SCpnt->cmnd,
1426                                     (void*) SCpnt->data_cmnd, 
1427                                     sizeof(SCpnt->data_cmnd));
1428                             SCpnt->request_buffer = SCpnt->buffer;
1429                             SCpnt->request_bufflen = SCpnt->bufflen;
1430                             SCpnt->use_sg = SCpnt->old_use_sg;
1431                             SCpnt->cmd_len = SCpnt->old_cmd_len;
1432                             internal_cmnd (SCpnt);
1433                           };
1434                         break;  
1435                 default: 
1436                         INTERNAL_ERROR;
1437                 }
1438 
1439 
1440         if (status == FINISHED) {
1441 #ifdef DEBUG
1442            printk("Calling done function - at address %08x\n", SCpnt->done);
1443 #endif
1444            host->host_busy--; /* Indicate that we are free */
1445 
1446            if (host->block && host->host_busy == 0) {
1447               struct Scsi_Host * block;
1448 
1449               block = host_next;
1450               host_active = NULL;
1451 
1452               if (block) wake_up(&block->host_wait);
1453 
1454               for (block = host->block; block != host; block = block->block)
1455                  wake_up(&block->host_wait);
1456 
1457               host_next = NULL;
1458 
1459               if (scsi_maybe_deadlocked) {
1460                  scsi_maybe_deadlocked = 0;
1461                  restart_all();
1462                  }
1463 
1464               }
1465 
1466            wake_up(&host->host_wait);
1467            SCpnt->result = result | ((exit & 0xff) << 24);
1468            SCpnt->use_sg = SCpnt->old_use_sg;
1469            SCpnt->cmd_len = SCpnt->old_cmd_len;
1470            SCpnt->done (SCpnt);
1471            }
1472 
1473 #undef FINISHED
1474 #undef REDO
1475 #undef MAYREDO
1476 #undef PENDING
1477         }
1478 
1479 /*
1480         The scsi_abort function interfaces with the abort() function of the host
1481         we are aborting, and causes the current command to not complete.  The 
1482         caller should deal with any error messages or status returned on the 
1483         next call.
1484         
1485         This will not be called reentrantly for a given host.
1486 */
1487         
1488 /*
1489         Since we're nice guys and specified that abort() and reset()
1490         can be non-reentrant.  The internal_timeout flags are used for
1491         this.
1492 */
1493 
1494 
1495 int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
1496         {
1497         int oldto;
1498         unsigned long flags;
1499         struct Scsi_Host * host = SCpnt->host;
1500         
1501         while(1)        
1502                 {
1503                 save_flags(flags);
1504                 cli();
1505 
1506                 /*
1507                  * Protect against races here.  If the command is done, or we are
1508                  * on a different command forget it.
1509                  */
1510                 if (SCpnt->request.dev == -1 || pid != SCpnt->pid) {
1511                   restore_flags(flags);
1512                   return 0;
1513                 }
1514 
1515                 if (SCpnt->internal_timeout & IN_ABORT) 
1516                         {
1517                         restore_flags(flags);
1518                         while (SCpnt->internal_timeout & IN_ABORT);
1519                         }
1520                 else
1521                         {       
1522                         SCpnt->internal_timeout |= IN_ABORT;
1523                         oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1524 
1525                         if ((SCpnt->flags & IS_RESETTING) && 
1526                             SCpnt->device->soft_reset) {
1527                           /* OK, this command must have died when we did the
1528                              reset.  The device itself must have lied. */
1529                           printk("Stale command on %d:%d appears to have died when"
1530                                  " the bus was reset\n", SCpnt->target, SCpnt->lun);
1531                         }
1532                         
1533                         restore_flags(flags);
1534                         if (!host->host_busy) {
1535                           SCpnt->internal_timeout &= ~IN_ABORT;
1536                           update_timeout(SCpnt, oldto);
1537                           return 0;
1538                         }
1539                         printk("scsi : aborting command due to timeout : pid %lu, scsi%d, id %d, lun %d ",
1540                                SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->target, (int) 
1541                                SCpnt->lun);
1542                         print_command (SCpnt->cmnd);
1543                         if (SCpnt->request.dev == -1 || pid != SCpnt->pid)
1544                           return 0;
1545                         SCpnt->abort_reason = why;
1546                         switch(host->hostt->abort(SCpnt)) {
1547                           /* We do not know how to abort.  Try waiting another
1548                              time increment and see if this helps. Set the
1549                              WAS_TIMEDOUT flag set so we do not try this twice
1550                              */
1551                         case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1552                                                  this is too severe */
1553                         case SCSI_ABORT_SNOOZE:
1554                           if(why == DID_TIME_OUT) {
1555                             save_flags(flags);
1556                             cli();
1557                             SCpnt->internal_timeout &= ~IN_ABORT;
1558                             if(SCpnt->flags & WAS_TIMEDOUT) {
1559                               restore_flags(flags);
1560                               return 1; /* Indicate we cannot handle this.
1561                                            We drop down into the reset handler
1562                                            and try again */
1563                             } else {
1564                               SCpnt->flags |= WAS_TIMEDOUT;
1565                               oldto = SCpnt->timeout_per_command;
1566                               update_timeout(SCpnt, oldto);
1567                             }
1568                             restore_flags(flags);
1569                           }
1570                           return 0;
1571                         case SCSI_ABORT_PENDING:
1572                           if(why != DID_TIME_OUT) {
1573                             save_flags(flags);
1574                             cli();
1575                             update_timeout(SCpnt, oldto);
1576                             restore_flags(flags);
1577                           }
1578                           return 0;
1579                         case SCSI_ABORT_SUCCESS:
1580                           /* We should have already aborted this one.  No
1581                              need to adjust timeout */
1582                         case SCSI_ABORT_NOT_RUNNING:
1583                           SCpnt->internal_timeout &= ~IN_ABORT;
1584                           update_timeout(SCpnt, 0);
1585                           return 0;
1586                         case SCSI_ABORT_ERROR:
1587                         default:
1588                           SCpnt->internal_timeout &= ~IN_ABORT;
1589                           return 1;
1590                         }
1591                       }
1592               } 
1593       }
1594      
1595 int scsi_reset (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1596         {
1597         int temp, oldto;
1598         unsigned long flags;
1599         Scsi_Cmnd * SCpnt1;
1600         struct Scsi_Host * host = SCpnt->host;
1601         
1602 #ifdef DEBUG
1603         printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
1604 #endif
1605         while (1) {
1606                 save_flags(flags);
1607                 cli();
1608                 if (SCpnt->internal_timeout & IN_RESET)
1609                         {
1610                         restore_flags(flags);
1611                         while (SCpnt->internal_timeout & IN_RESET);
1612                         }
1613                 else
1614                         {
1615                         SCpnt->internal_timeout |= IN_RESET;
1616                         oldto = update_timeout(SCpnt, RESET_TIMEOUT);   
1617                                         
1618                         if (host->host_busy)
1619                                 {       
1620                                 restore_flags(flags);
1621                                 SCpnt1 = host->host_queue;
1622                                 while(SCpnt1) {
1623                                   if (SCpnt1->request.dev > 0) {
1624 #if 0                             
1625                                     if (!(SCpnt1->flags & IS_RESETTING) && 
1626                                       !(SCpnt1->internal_timeout & IN_ABORT))
1627                                     scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
1628 #endif
1629                                     SCpnt1->flags |= IS_RESETTING;
1630                                   }
1631                                   SCpnt1 = SCpnt1->next;
1632                                 };
1633 
1634                                 temp = host->hostt->reset(SCpnt);       
1635                                 }                               
1636                         else
1637                                 {
1638                                 host->host_busy++;
1639 
1640                                 if (host->block) {
1641                                    host_active = host;
1642                                    host_next = NULL;
1643                                    }
1644         
1645                                 restore_flags(flags);
1646                                 temp = host->hostt->reset(SCpnt);
1647                                 host->last_reset = jiffies;
1648                                 host->host_busy--;
1649 
1650                                 }
1651 
1652                         if (host->block && host->host_busy == 0) {
1653                            host_active = NULL;
1654                            host_next = NULL;
1655                            restart_all();
1656                            }
1657 
1658 
1659 #ifdef DEBUG
1660                         printk("scsi reset function returned %d\n", temp);
1661 #endif
1662                         switch(temp) {
1663                         case SCSI_RESET_SUCCESS:
1664                           save_flags(flags);
1665                           cli();
1666                           SCpnt->internal_timeout &= ~IN_RESET;
1667                           update_timeout(SCpnt, oldto);
1668                           restore_flags(flags);
1669                           return 0;
1670                         case SCSI_RESET_PENDING:
1671                           return 0;
1672                         case SCSI_RESET_PUNT:
1673                         case SCSI_RESET_WAKEUP:
1674                           SCpnt->internal_timeout &= ~IN_RESET;
1675                           scsi_request_sense (SCpnt);
1676                           return 0;
1677                         case SCSI_RESET_SNOOZE:                   
1678                           /* In this case, we set the timeout field to 0
1679                              so that this command does not time out any more,
1680                              and we return 1 so that we get a message on the
1681                              screen. */
1682                           save_flags(flags);
1683                           cli();
1684                           SCpnt->internal_timeout &= ~IN_RESET;
1685                           update_timeout(SCpnt, 0);
1686                           restore_flags(flags);
1687                           /* If you snooze, you lose... */
1688                         case SCSI_RESET_ERROR:
1689                         default:
1690                           return 1;
1691                         }
1692         
1693                         return temp;    
1694                         }
1695                 }
1696         }
1697                          
1698 
1699 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1700         {
1701         /*
1702                 We must not enter update_timeout with a timeout condition still pending.
1703         */
1704 
1705         int timed_out, pid;
1706         unsigned long flags;
1707         struct Scsi_Host * host;
1708         Scsi_Cmnd * SCpnt = NULL;
1709 
1710         do      {       
1711                 save_flags(flags);
1712                 cli();
1713 
1714                 update_timeout(NULL, 0);
1715         /*
1716                 Find all timers such that they have 0 or negative (shouldn't happen)
1717                 time remaining on them.
1718         */
1719                         
1720                 timed_out = 0;
1721                 for(host = scsi_hostlist; host; host = host->next) {
1722                   for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1723                     if (SCpnt->timeout == -1)
1724                       {
1725                         SCpnt->timeout = 0;
1726                         pid = SCpnt->pid;
1727                         restore_flags(flags);
1728                         scsi_times_out(SCpnt, pid);
1729                         ++timed_out; 
1730                         save_flags(flags);
1731                         cli();
1732                       }
1733                 };
1734               } while (timed_out);      
1735         restore_flags(flags);
1736       }
1737 
1738 /*
1739         The strategy is to cause the timer code to call scsi_times_out()
1740         when the soonest timeout is pending.  
1741         The arguments are used when we are queueing a new command, because
1742         we do not want to subtract the time used from this time, but when we
1743         set the timer, we want to take this value into account.
1744 */
1745         
1746 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
1747         {
1748         unsigned int least, used;
1749         unsigned int oldto;
1750         unsigned long flags;
1751         struct Scsi_Host * host;
1752         Scsi_Cmnd * SCpnt = NULL;
1753 
1754         save_flags(flags);
1755         cli();
1756 
1757 /* 
1758         Figure out how much time has passed since the last time the timeouts 
1759         were updated 
1760 */
1761         used = (time_start) ? (jiffies - time_start) : 0;
1762 
1763 /*
1764         Find out what is due to timeout soonest, and adjust all timeouts for
1765         the amount of time that has passed since the last time we called 
1766         update_timeout. 
1767 */
1768         
1769         oldto = 0;
1770 
1771         if(SCset){
1772           oldto = SCset->timeout - used;
1773           SCset->timeout = timeout + used;
1774         };
1775 
1776         least = 0xffffffff;
1777 
1778         for(host = scsi_hostlist; host; host = host->next)
1779           for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
1780             if (SCpnt->timeout > 0) {
1781               SCpnt->timeout -= used;
1782               if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
1783               if(SCpnt->timeout > 0 && SCpnt->timeout < least)
1784                 least = SCpnt->timeout;
1785             };
1786 
1787 /*
1788         If something is due to timeout again, then we will set the next timeout 
1789         interrupt to occur.  Otherwise, timeouts are disabled.
1790 */
1791         
1792         if (least != 0xffffffff)
1793                 {
1794                 time_start = jiffies;   
1795                 timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;     
1796                 timer_active |= 1 << SCSI_TIMER;
1797                 }
1798         else
1799                 {
1800                 timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
1801                 timer_active &= ~(1 << SCSI_TIMER);
1802                 }       
1803         restore_flags(flags);
1804         return oldto;
1805         }               
1806 
1807 
1808 static unsigned short * dma_malloc_freelist = NULL;
1809 static unsigned int dma_sectors = 0;
1810 unsigned int dma_free_sectors = 0;
1811 unsigned int need_isa_buffer = 0;
1812 static unsigned char * dma_malloc_buffer = NULL;
1813 
1814 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1815 {
1816   unsigned int nbits, mask;
1817   unsigned long flags;
1818   int i, j;
1819   if((len & 0x1ff) || len > 8192)
1820     return NULL;
1821   
1822   save_flags(flags);
1823   cli();
1824   nbits = len >> 9;
1825   mask = (1 << nbits) - 1;
1826   
1827   for(i=0;i < (dma_sectors >> 4); i++)
1828     for(j=0; j<17-nbits; j++){
1829       if ((dma_malloc_freelist[i] & (mask << j)) == 0){
1830         dma_malloc_freelist[i] |= (mask << j);
1831         restore_flags(flags);
1832         dma_free_sectors -= nbits;
1833 #ifdef DEBUG
1834         printk("SMalloc: %d %x ",len, dma_malloc_buffer + (i << 13) + (j << 9));
1835 #endif
1836         return (void *) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
1837       };
1838     };
1839   restore_flags(flags);
1840   return NULL;  /* Nope.  No more */
1841 }
1842 
1843 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
1844 {
1845   int page, sector, nbits, mask;
1846   long offset;
1847   unsigned long flags;
1848 
1849 #ifdef DEBUG
1850   printk("Sfree %x %d\n",obj, len);
1851 #endif
1852 
1853   offset = ((unsigned long) obj) - ((unsigned long) dma_malloc_buffer);
1854 
1855   if (offset < 0) panic("Bad offset");
1856   page = offset >> 13;
1857   sector = offset >> 9;
1858   if(sector >= dma_sectors) panic ("Bad page");
1859 
1860   sector = (offset >> 9) & 15;
1861   nbits = len >> 9;
1862   mask = (1 << nbits) - 1;
1863 
1864   if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
1865 
1866   save_flags(flags);
1867   cli();
1868   if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
1869     panic("Trying to free unused memory");
1870 
1871   dma_free_sectors += nbits;
1872   dma_malloc_freelist[page] &= ~(mask << sector);
1873   restore_flags(flags);
1874   return 0;
1875 }
1876 
1877 
1878 /* These are special functions that can be used to obtain memory at boot time.
1879    They act line a malloc function, but they simply take memory from the
1880    pool */
1881 
1882 static unsigned long scsi_init_memory_start = 0;
1883 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
1884 
1885 void * scsi_init_malloc(unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1886 {
1887   unsigned long retval;
1888   if(scsi_loadable_module_flag) {
1889     retval = (unsigned long) kmalloc(size, GFP_ATOMIC);
1890   } else {
1891     retval = scsi_init_memory_start;
1892     scsi_init_memory_start += size;
1893   }
1894   return (void *) retval;
1895 }
1896 
1897 
1898 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
1899 { /* FIXME - not right.  We need to compare addresses to see whether this was
1900      kmalloc'd or not */
1901   if((unsigned long) ptr < scsi_loadable_module_flag) {
1902     kfree(ptr);
1903   } else {
1904     if(((unsigned long) ptr) + size == scsi_init_memory_start)
1905       scsi_init_memory_start = (unsigned long) ptr;
1906   }
1907 }
1908 
1909 /*
1910         scsi_dev_init() is our initialization routine, which in turn calls host 
1911         initialization, bus scanning, and sd/st initialization routines.  It 
1912         should be called from main().
1913 */
1914 
1915 unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
     /* [previous][next][first][last][top][bottom][index][help] */
1916         {
1917         struct Scsi_Host * host = NULL;
1918         Scsi_Device * SDpnt;
1919         struct Scsi_Host * shpnt;
1920         struct Scsi_Device_Template * sdtpnt;
1921         Scsi_Cmnd * SCpnt;
1922 #ifdef FOO_ON_YOU
1923         return;
1924 #endif  
1925 
1926         /* Init a few things so we can "malloc" memory. */
1927         scsi_loadable_module_flag = 0;
1928         scsi_init_memory_start = memory_start;
1929 
1930         timer_table[SCSI_TIMER].fn = scsi_main_timeout;
1931         timer_table[SCSI_TIMER].expires = 0;
1932 
1933         /* initialize all hosts */
1934         scsi_init(); 
1935                                 
1936         scsi_devices = (Scsi_Device *) NULL;
1937 
1938         for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
1939           scan_scsis(shpnt);           /* scan for scsi devices */
1940 
1941         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1942           if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
1943 
1944         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1945           int j;
1946           SDpnt->scsi_request_fn = NULL;
1947           for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1948             if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
1949           if(SDpnt->type != -1){
1950             for(j=0;j<SDpnt->host->cmd_per_lun;j++){
1951               SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd));
1952               SCpnt->host = SDpnt->host;
1953               SCpnt->device = SDpnt;
1954               SCpnt->target = SDpnt->id;
1955               SCpnt->lun = SDpnt->lun;
1956               SCpnt->request.dev = -1; /* Mark not busy */
1957               SCpnt->use_sg = 0;
1958               SCpnt->old_use_sg = 0;
1959               SCpnt->old_cmd_len = 0;
1960               SCpnt->timeout = 0;
1961               SCpnt->underflow = 0;
1962               SCpnt->transfersize = 0;
1963               SCpnt->host_scribble = NULL;
1964               host = SDpnt->host;
1965               if(host->host_queue)
1966                 host->host_queue->prev = SCpnt;
1967               SCpnt->next = host->host_queue;
1968               SCpnt->prev = NULL;
1969               host->host_queue = SCpnt;
1970             };
1971           };
1972         };
1973 
1974         if (scsi_devicelist)
1975           dma_sectors = 16;  /* Base value we use */
1976 
1977         for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
1978           host = SDpnt->host;
1979           
1980           if(SDpnt->type != TYPE_TAPE)
1981             dma_sectors += ((host->sg_tablesize * 
1982                              sizeof(struct scatterlist) + 511) >> 9) * 
1983                                host->cmd_per_lun;
1984           
1985           if(host->unchecked_isa_dma &&
1986              memory_end - 1 > ISA_DMA_THRESHOLD &&
1987              SDpnt->type != TYPE_TAPE) {
1988             dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
1989               host->cmd_per_lun;
1990             need_isa_buffer++;
1991           };
1992         };
1993 
1994         dma_sectors = (dma_sectors + 15) & 0xfff0;
1995         dma_free_sectors = dma_sectors;  /* This must be a multiple of 16 */
1996 
1997         scsi_init_memory_start = (scsi_init_memory_start + 3) & 0xfffffffc;
1998         dma_malloc_freelist = (unsigned short *) 
1999           scsi_init_malloc(dma_sectors >> 3);
2000         memset(dma_malloc_freelist, 0, dma_sectors >> 3);
2001 
2002         /* Some host adapters require buffers to be word aligned */
2003         if(scsi_init_memory_start & 1) scsi_init_memory_start++;
2004 
2005         dma_malloc_buffer = (unsigned char *) 
2006           scsi_init_malloc(dma_sectors << 9);
2007         
2008         /* OK, now we finish the initialization by doing spin-up, read
2009            capacity, etc, etc */
2010         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2011           if(sdtpnt->finish && sdtpnt->nr_dev)
2012             (*sdtpnt->finish)();
2013         
2014         scsi_loadable_module_flag = 1;
2015         return scsi_init_memory_start;
2016         }
2017 
2018 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
2019 {
2020         int i;
2021 
2022         printk("  Vendor: ");
2023         for (i = 8; i < 16; i++)
2024                 {
2025                 if (data[i] >= 0x20 && i < data[4] + 5)
2026                         printk("%c", data[i]);
2027                 else
2028                         printk(" ");
2029                 }
2030 
2031         printk("  Model: ");
2032         for (i = 16; i < 32; i++)
2033                 {
2034                 if (data[i] >= 0x20 && i < data[4] + 5)
2035                         printk("%c", data[i]);
2036                 else
2037                         printk(" ");
2038                 }
2039 
2040         printk("  Rev: ");
2041         for (i = 32; i < 36; i++)
2042                 {
2043                 if (data[i] >= 0x20 && i < data[4] + 5)
2044                         printk("%c", data[i]);
2045                 else
2046                         printk(" ");
2047                 }
2048 
2049         printk("\n");
2050 
2051         i = data[0] & 0x1f;
2052 
2053         printk("  Type:   %s ",
2054                i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2055         printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2056         if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2057           printk(" CCS\n");
2058         else
2059           printk("\n");
2060 }
2061 
2062 #ifdef NOT_YET
2063 /*
2064  * This entry point should be called by a loadable module if it is trying
2065  * add a low level scsi driver to the system.
2066  */
2067 static int scsi_register_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2068 {
2069   int pcount;
2070   struct Scsi_Host * shpnt;
2071   struct Scsi_Host * host = NULL;
2072   unsigned long flags;
2073   Scsi_Device * SDpnt;
2074   Scsi_Cmnd * SCpnt;
2075   struct Scsi_Device_Template * sdtpnt;
2076   int j, i;
2077   char * name;
2078 
2079   if (tpnt->next || !tpnt->detect) return 1;  /* Must be already loaded, or
2080                                                no detect routine available */
2081   pcount = next_scsi_host;
2082   if ((tpnt->present = tpnt->detect(tpnt)))
2083     {
2084       if(pcount == next_scsi_host) {
2085         if(tpnt->present > 1) {
2086           printk("Failure to register low-level scsi driver");
2087           scsi_unregister_host(tpnt);
2088           return 1;
2089         }
2090         /* The low-level driver failed to register a driver.  We
2091            can do this now. */
2092         scsi_register(tpnt,0);
2093       }
2094       tpnt->next = scsi_hosts; /* Add to the linked list */
2095       scsi_hosts = tpnt;
2096 
2097       for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2098         if(shpnt->hostt == tpnt)
2099           {
2100             if(tpnt->info)
2101               name = tpnt->info(shpnt);
2102             else
2103               name = tpnt->name;
2104             printk ("scsi%d : %s\n", /* And print a little message */
2105                     shpnt->host_no, name);
2106           }
2107       /* The next step is to call scan_scsis here.  This generates the
2108          Scsi_Devices entries */
2109 
2110       for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2111         if(shpnt->hostt == tpnt) scan_scsis(shpnt);
2112 
2113       for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2114         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2115 
2116       /* Next we create the Scsi_Cmnd structures for this host */
2117 
2118       for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2119         if(SDpnt->host->hostt == tpnt)
2120           {
2121             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2122               if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2123             if(SDpnt->attached){
2124               for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2125                 SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2126                 SCpnt->host = SDpnt->host;
2127                 SCpnt->device = SDpnt;
2128                 SCpnt->target = SDpnt->id;
2129                 SCpnt->lun = SDpnt->lun;
2130                 SCpnt->request.dev = -1; /* Mark not busy */
2131                 SCpnt->request.sem = NULL;
2132                 SCpnt->use_sg = 0;
2133                 SCpnt->old_use_sg = 0;
2134                 SCpnt->underflow = 0;
2135                 SCpnt->timeout = 0;
2136                 SCpnt->transfersize = 0;
2137                 SCpnt->host_scribble = NULL;
2138                 host = SDpnt->host;
2139                 SCpnt->next = host->host_queue;
2140                 SCpnt->prev = NULL;
2141                 host->host_queue = SCpnt;
2142                 if(host->host_queue)
2143                   host->host_queue->prev = SCpnt;
2144               };
2145             };
2146           }
2147         /* Next, check to see if we need to extend the DMA buffer pool */
2148       {
2149           unsigned char * new_dma_malloc_freelist = NULL;
2150           unsigned int new_dma_sectors = 0;
2151           unsigned int new_need_isa_buffer = 0;
2152           unsigned char ** new_dma_malloc_pages = NULL;
2153           
2154           if (scsi_devicelist)
2155             new_dma_sectors = 16;  /* Base value we use */
2156           
2157           for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2158             host = SDpnt->host;
2159             
2160             if(SDpnt->type != TYPE_TAPE)
2161               new_dma_sectors += ((host->sg_tablesize * 
2162                                    sizeof(struct scatterlist) + 511) >> 9) * 
2163                                      host->cmd_per_lun;
2164             
2165             if(host->unchecked_isa_dma &&
2166                scsi_need_isa_bounce_buffers &&
2167                SDpnt->type != TYPE_TAPE) {
2168               new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2169                 host->cmd_per_lun;
2170               new_need_isa_buffer++;
2171             };
2172           };
2173           
2174           new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2175 
2176           new_dma_malloc_freelist = (unsigned char *) 
2177             scsi_init_malloc(new_dma_sectors >> 3, GFP_ATOMIC);
2178           memset(new_dma_malloc_freelist, 0, new_dma_sectors >> 3);
2179           
2180           new_dma_malloc_pages = (unsigned char **) 
2181             scsi_init_malloc(new_dma_sectors >> 1, GFP_ATOMIC);
2182           memset(new_dma_malloc_pages, 0, new_dma_sectors >> 1);
2183           
2184           for(i=dma_sectors >> 3; i< new_dma_sectors >> 3; i++)
2185             new_dma_malloc_pages[i] = (unsigned char *) 
2186               scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2187           
2188 
2189           /* When we dick with the actual DMA list, we need to protect things */
2190 
2191           save_flags(flags);
2192           cli();
2193           memcpy(new_dma_malloc_freelist, dma_malloc_freelist, dma_sectors >> 3);
2194           scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2195           dma_malloc_freelist = new_dma_malloc_freelist;
2196           
2197           memcpy(new_dma_malloc_pages, dma_malloc_pages, dma_sectors >> 1);
2198           scsi_init_free((char *) dma_malloc_pages, dma_sectors>>1);
2199           
2200           dma_free_sectors += new_dma_sectors - dma_sectors;
2201           dma_malloc_pages = new_dma_malloc_pages;
2202           dma_sectors = new_dma_sectors;
2203           need_isa_buffer = new_need_isa_buffer;
2204           restore_flags(flags);
2205 
2206           
2207         }
2208       /* This does any final handling that is required. */
2209       for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2210         if(sdtpnt->finish) (*sdtpnt->finish)();
2211     }
2212   return 0;
2213 }
2214 
2215 /*
2216  * Similarly, this entry point should be called by a loadable module if it
2217  * is trying to remove a low level scsi driver from the system.
2218  */
2219 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2220 {  
2221   Scsi_Host_Template * SHT, *SHTp;
2222   Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
2223   Scsi_Cmnd * SCpnt;
2224   unsigned long flags;
2225   struct Scsi_Device_Template * sdtpnt;
2226   struct Scsi_Host * shpnt, *sh1;
2227   int pcount;
2228 
2229   /* First verify that this host adapter is completely free with no pending
2230      commands */
2231 
2232   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2233     if(sdpnt->host->hostt == tpnt && *sdpnt->host->hostt->usage_count) return;
2234 
2235   for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2236     {
2237       if (shpnt->hostt != tpnt) continue;
2238       for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2239         {
2240           save_flags(flags);
2241           cli();
2242           if(SCpnt->request.dev != -1) {
2243             restore_flags(flags);
2244             for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2245               if(SCpnt->request.dev == 0xffe0) SCpnt->request.dev = -1;
2246             printk("Device busy???\n");
2247             return;
2248           }
2249           SCpnt->request.dev = 0xffe0;  /* Mark as busy */
2250         }
2251     }
2252   /* Next we detach the high level drivers from the Scsi_Device structures */
2253 
2254   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2255     if(sdpnt->host->hostt == tpnt)
2256       {
2257         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2258           if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
2259         /* If something still attached, punt */
2260         if (sdpnt->attached) {
2261           printk("Attached usage count = %d\n", sdpnt->attached);
2262           return;
2263         }
2264       }
2265   
2266   /* Next we free up the Scsi_Cmnd structures for this host */
2267 
2268   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2269     if(sdpnt->host->hostt == tpnt)
2270       while (sdpnt->host->host_queue) {
2271         SCpnt = sdpnt->host->host_queue->next;
2272         scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
2273         sdpnt->host->host_queue = SCpnt;
2274         if (SCpnt) SCpnt->prev = NULL;
2275       }
2276   
2277   /* Next free up the Scsi_Device structures for this host */
2278 
2279   sdppnt = NULL;
2280   for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
2281     {
2282       sdpnt1 = sdpnt->next;
2283       if (sdpnt->host->hostt == tpnt) {
2284         if (sdppnt)
2285           sdppnt->next = sdpnt->next;
2286         else
2287           scsi_devices = sdpnt->next;
2288         scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
2289       } else
2290         sdppnt = sdpnt;
2291     }
2292 
2293   /* Next we go through and remove the instances of the individual hosts
2294      that were detected */
2295 
2296   shpnt = scsi_hostlist;
2297   while(shpnt) { 
2298    sh1 = shpnt->next;
2299     if(shpnt->hostt == tpnt) {
2300       if(shpnt->loaded_as_module) {
2301         pcount = next_scsi_host;
2302         if(tpnt->release)
2303           (*tpnt->release)(shpnt);
2304         else {
2305           /* This is the default case for the release function.  It should do the right
2306              thing for most correctly written host adapters. */
2307           if (shpnt->irq) free_irq(shpnt->irq);
2308           if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
2309           if (shpnt->io_port && shpnt->n_io_port) 
2310             release_region(shpnt->io_port, shpnt->n_io_port);
2311         }
2312         if(pcount == next_scsi_host) scsi_unregister(shpnt);
2313         tpnt->present--;
2314       }
2315     }
2316     shpnt = sh1;
2317   }
2318 
2319   /* There were some hosts that were loaded at boot time, so we cannot
2320      do any more than this */
2321   if (tpnt->present) return;
2322 
2323   /* OK, this is the very last step.  Remove this host adapter from the
2324      linked list. */
2325   for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
2326     if(SHT == tpnt) {
2327       if(SHTp)
2328         SHTp->next = SHT->next;
2329       else
2330         scsi_hosts = SHT->next;
2331       SHT->next = NULL;
2332       break;
2333     }
2334 }
2335 
2336 int scsi_register_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2337 {
2338   switch(module_type){
2339   case MODULE_SCSI_HA:
2340     return scsi_register_host((Scsi_Host_Template *) ptr);
2341     /* The rest of these are not yet implemented */
2342 
2343     /* Load constants.o */
2344   case MODULE_SCSI_CONST:
2345 
2346     /* Load specialized ioctl handler for some device.  Intended for cdroms that
2347        have non-SCSI2 audio command sets. */
2348   case MODULE_SCSI_IOCTL:
2349 
2350     /* Load upper level device handler of some kind */
2351   case MODULE_SCSI_DEV:
2352   default:
2353     return 1;
2354   }
2355 }
2356 
2357 void scsi_unregister_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2358 {
2359   switch(module_type) {
2360   case MODULE_SCSI_HA:
2361     scsi_unregister_host((Scsi_Host_Template *) ptr);
2362     break;
2363     /* The rest of these are not yet implemented. */
2364   case MODULE_SCSI_CONST:
2365   case MODULE_SCSI_IOCTL:
2366   case MODULE_SCSI_DEV:
2367   default:
2368   }
2369   return;
2370 }
2371 #endif
2372 
2373 #ifdef DEBUG_TIMEOUT
2374 static void 
2375 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2376 {
2377   int i;
2378   struct Scsi_Host * shpnt;
2379   Scsi_Cmnd * SCpnt;
2380   printk("Dump of scsi parameters:\n");
2381   i = 0;
2382   for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2383     for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2384       {
2385         /*  (0) 0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
2386         printk("(%d) %d:%d:%d (%4.4x %ld %ld %ld %ld) (%d %d %x) (%d %d %d) %x %x %x\n",
2387                i++, SCpnt->host->host_no,
2388                SCpnt->target,
2389                SCpnt->lun,
2390                SCpnt->request.dev,
2391                SCpnt->request.sector,
2392                SCpnt->request.nr_sectors,
2393                SCpnt->request.current_nr_sectors,
2394                SCpnt->use_sg,
2395                SCpnt->retries,
2396                SCpnt->allowed,
2397                SCpnt->flags,
2398                SCpnt->timeout_per_command,
2399                SCpnt->timeout,
2400                SCpnt->internal_timeout,
2401                SCpnt->cmnd[0],
2402                SCpnt->sense_buffer[2],
2403                SCpnt->result);
2404       };
2405   printk("wait_for_request = %p\n", wait_for_request);
2406   /* Now dump the request lists for each block device */
2407   printk("Dump of pending block device requests\n");
2408   for(i=0; i<MAX_BLKDEV; i++)
2409     if(blk_dev[i].current_request)
2410       {
2411         struct request * req;
2412         printk("%d: ", i);
2413         req = blk_dev[i].current_request;
2414         while(req) {
2415           printk("(%x %d %ld %ld %ld) ",
2416                  req->dev,
2417                  req->cmd,
2418                  req->sector,
2419                  req->nr_sectors,
2420                  req->current_nr_sectors);
2421           req = req->next;
2422         }
2423         printk("\n");
2424       }
2425 }
2426 #endif
2427 
2428 

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