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

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