root/drivers/scsi/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. blacklisted
  2. scsi_make_blocked_list
  3. scan_scsis_done
  4. scsi_luns_setup
  5. scan_scsis
  6. scsi_times_out
  7. request_queueable
  8. allocate_device
  9. internal_cmnd
  10. scsi_request_sense
  11. scsi_do_cmd
  12. reset
  13. check_sense
  14. scsi_done
  15. scsi_abort
  16. scsi_reset
  17. scsi_main_timeout
  18. update_timeout
  19. scsi_malloc
  20. scsi_free
  21. scsi_init_malloc
  22. scsi_init_free
  23. scsi_dev_init
  24. print_inquiry
  25. scsi_register_host
  26. scsi_unregister_host
  27. scsi_register_module
  28. scsi_unregister_module
  29. scsi_dump_status

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

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