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

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