root/drivers/scsi/scsi.c

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

DEFINITIONS

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

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