root/drivers/scsi/scsi.c

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

DEFINITIONS

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

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

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