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

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