root/drivers/scsi/scsi.c

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

DEFINITIONS

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

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