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

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