root/drivers/scsi/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_device_flags
  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. check_sense
  13. scsi_done
  14. scsi_abort
  15. scsi_mark_device_reset
  16. scsi_mark_host_bus_reset
  17. scsi_reset
  18. scsi_main_timeout
  19. update_timeout
  20. scsi_malloc
  21. scsi_free
  22. scsi_init_malloc
  23. scsi_init_free
  24. scsi_build_commandblocks
  25. scsi_dev_init
  26. print_inquiry
  27. scsi_proc_info
  28. resize_dma_pool
  29. scsi_register_host
  30. scsi_unregister_host
  31. scsi_register_device_module
  32. scsi_unregister_device
  33. scsi_register_module
  34. scsi_unregister_module
  35. scsi_dump_status
  36. init_module
  37. cleanup_module

   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 eric@aib.com to
  17  *  add scatter-gather, multiple outstanding request, and other
  18  *  enhancements.
  19  *
  20  *  Native multichannel and wide scsi support added 
  21  *  by Michael Neuffer neuffer@goofy.zdv.uni-mainz.de
  22  */
  23 
  24 /*
  25  * Don't import our own symbols, as this would severely mess up our
  26  * symbol tables.
  27  */
  28 #define _SCSI_SYMS_VER_
  29 #include <linux/module.h>
  30 
  31 #include <asm/system.h>
  32 #include <linux/sched.h>
  33 #include <linux/timer.h>
  34 #include <linux/string.h>
  35 #include <linux/malloc.h>
  36 #include <asm/irq.h>
  37 #include <asm/dma.h>
  38 #include <linux/ioport.h>
  39 #include <linux/kernel.h>
  40 #include<linux/stat.h>
  41 
  42 #include <linux/blk.h>
  43 #include "scsi.h"
  44 #include "hosts.h"
  45 #include "constants.h"
  46 
  47 #include <linux/config.h>
  48 
  49 #undef USE_STATIC_SCSI_MEMORY
  50 
  51 /*
  52 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 $";
  53 */
  54 
  55 
  56 /* Command groups 3 and 4 are reserved and should never be used.  */
  57 const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
  58 
  59 #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
  60 
  61 static void scsi_done (Scsi_Cmnd *SCpnt);
  62 static int update_timeout (Scsi_Cmnd *, int);
  63 static void print_inquiry(unsigned char *data);
  64 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid);
  65 
  66 static unsigned char * dma_malloc_freelist = NULL;
  67 static int scsi_need_isa_bounce_buffers;
  68 static unsigned int dma_sectors = 0;
  69 unsigned int dma_free_sectors = 0;
  70 unsigned int need_isa_buffer = 0;
  71 static unsigned char ** dma_malloc_pages = NULL;
  72 
  73 static int time_start;
  74 static int time_elapsed;
  75 static volatile struct Scsi_Host * host_active = NULL;
  76 #define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
  77                           || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))
  78 
  79 #define MAX_SCSI_DEVICE_CODE 10
  80 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
  81 {
  82     "Direct-Access    ",
  83     "Sequential-Access",
  84     "Printer          ",
  85     "Processor        ",
  86     "WORM             ",
  87     "CD-ROM           ",
  88     "Scanner          ",
  89     "Optical Device   ",
  90     "Medium Changer   ",
  91     "Communications   "
  92 };
  93 
  94 
  95 /*
  96  * global variables :
  97  * scsi_devices an array of these specifying the address for each
  98  * (host, id, LUN)
  99  */
 100 
 101 Scsi_Device * scsi_devices = NULL;
 102 
 103 /* Process ID of SCSI commands */
 104 unsigned long scsi_pid = 0;
 105 
 106 static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
 107 static void resize_dma_pool(void);
 108 
 109 /* This variable is merely a hook so that we can debug the kernel with gdb. */
 110 Scsi_Cmnd * last_cmnd = NULL;
 111 
 112 /* This is the pointer to the /proc/scsi code. 
 113  * It is only initialized to !=0 if the scsi code is present 
 114  */ 
 115 extern int (* dispatch_scsi_info_ptr)(int ino, char *buffer, char **start, 
 116                                       off_t offset, int length, int inout); 
 117 extern int dispatch_scsi_info(int ino, char *buffer, char **start, 
 118                               off_t offset, int length, int inout); 
 119 
 120 struct proc_dir_entry proc_scsi_scsi = {
 121     PROC_SCSI_SCSI, 4, "scsi",
 122     S_IFREG | S_IRUGO | S_IWUSR, 2, 0, 0, 0, 
 123     NULL,
 124     NULL, NULL,
 125     NULL, NULL, NULL
 126 };
 127 
 128 
 129 /*
 130  *  As the scsi do command functions are intelligent, and may need to
 131  *  redo a command, we need to keep track of the last command
 132  *  executed on each one.
 133  */
 134 
 135 #define WAS_RESET       0x01
 136 #define WAS_TIMEDOUT    0x02
 137 #define WAS_SENSE       0x04
 138 #define IS_RESETTING    0x08
 139 #define IS_ABORTING     0x10
 140 #define ASKED_FOR_SENSE 0x20
 141 
 142 /*
 143  *  This is the number  of clock ticks we should wait before we time out
 144  *  and abort the command.  This is for  where the scsi.c module generates
 145  *  the command, not where it originates from a higher level, in which
 146  *  case the timeout is specified there.
 147  *
 148  *  ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
 149  *  respectively.
 150  */
 151 
 152 #ifdef DEBUG_TIMEOUT
 153 static void scsi_dump_status(void);
 154 #endif
 155 
 156 
 157 #ifdef DEBUG
 158     #define SCSI_TIMEOUT (5*HZ)
 159 #else
 160     #define SCSI_TIMEOUT (1*HZ)
 161 #endif
 162 
 163 #ifdef DEBUG
 164     #define SENSE_TIMEOUT SCSI_TIMEOUT
 165     #define ABORT_TIMEOUT SCSI_TIMEOUT
 166     #define RESET_TIMEOUT SCSI_TIMEOUT
 167 #else
 168     #define SENSE_TIMEOUT (5*HZ/10)
 169     #define RESET_TIMEOUT (5*HZ/10)
 170     #define ABORT_TIMEOUT (5*HZ/10)
 171 #endif
 172 
 173 #define MIN_RESET_DELAY (1*HZ)
 174 
 175 /* Do not call reset on error if we just did a reset within 10 sec. */
 176 #define MIN_RESET_PERIOD (10*HZ)
 177 
 178 /* The following devices are known not to tolerate a lun != 0 scan for
 179  * one reason or another.  Some will respond to all luns, others will
 180  * lock up. 
 181  */
 182 
 183 #define BLIST_NOLUN     0x01
 184 #define BLIST_FORCELUN  0x02
 185 #define BLIST_BORKEN    0x04
 186 #define BLIST_KEY       0x08
 187 #define BLIST_SINGLELUN 0x10
 188 
 189 struct dev_info{
 190     const char * vendor;
 191     const char * model;
 192     const char * revision; /* Latest revision known to be bad.  Not used yet */
 193     unsigned flags;
 194 };
 195 
 196 /*
 197  * This is what was previously known as the blacklist.  The concept
 198  * has been expanded so that we can specify other types of things we
 199  * need to be aware of.
 200  */
 201 static struct dev_info device_list[] =
 202 {
 203 {"CHINON","CD-ROM CDS-431","H42", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
 204 {"CHINON","CD-ROM CDS-535","Q14", BLIST_NOLUN}, /* Locks up if polled for lun != 0 */
 205 {"DENON","DRD-25X","V", BLIST_NOLUN},           /* Locks up if probed for lun != 0 */
 206 {"HITACHI","DK312C","CM81", BLIST_NOLUN},       /* Responds to all lun - dtg */
 207 {"HITACHI","DK314C","CR21" , BLIST_NOLUN},      /* responds to all lun */
 208 {"IMS", "CDD521/10","2.06", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
 209 {"MAXTOR","XT-3280","PR02", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
 210 {"MAXTOR","XT-4380S","B3C", BLIST_NOLUN},       /* Locks-up when LUN>0 polled. */
 211 {"MAXTOR","MXT-1240S","I1.2", BLIST_NOLUN},     /* Locks up when LUN>0 polled */
 212 {"MAXTOR","XT-4170S","B5A", BLIST_NOLUN},       /* Locks-up sometimes when LUN>0 polled. */
 213 {"MAXTOR","XT-8760S","B7B", BLIST_NOLUN},       /* guess what? */
 214 {"NEC","CD-ROM DRIVE:841","1.0", BLIST_NOLUN},  /* Locks-up when LUN>0 polled. */
 215 {"RODIME","RO3000S","2.33", BLIST_NOLUN},       /* Locks up if polled for lun != 0 */
 216 {"SEAGATE", "ST157N", "\004|j", BLIST_NOLUN},   /* causes failed REQUEST SENSE on lun 1 
 217                                                  * for aha152x controller, which causes 
 218                                                  * SCSI code to reset bus.*/
 219 {"SEAGATE", "ST296","921", BLIST_NOLUN},        /* Responds to all lun */
 220 {"SONY","CD-ROM CDU-541","4.3d", BLIST_NOLUN},
 221 {"SONY","CD-ROM CDU-55S","1.0i", BLIST_NOLUN},
 222 {"SONY","CD-ROM CDU-561","1.7x", BLIST_NOLUN},
 223 {"TANDBERG","TDC 3600","U07", BLIST_NOLUN},     /* Locks up if polled for lun != 0 */
 224 {"TEAC","CD-ROM","1.06", BLIST_NOLUN},          /* causes failed REQUEST SENSE on lun 1 
 225                                                  * for seagate controller, which causes 
 226                                                  * SCSI code to reset bus.*/
 227 {"TEXEL","CD-ROM","1.06", BLIST_NOLUN},         /* causes failed REQUEST SENSE on lun 1 
 228                                                  * for seagate controller, which causes 
 229                                                  * SCSI code to reset bus.*/
 230 {"QUANTUM","LPS525S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
 231 {"QUANTUM","PD1225S","3110", BLIST_NOLUN},      /* Locks sometimes if polled for lun != 0 */
 232 {"MEDIAVIS","CDR-H93MV","1.31", BLIST_NOLUN},   /* Locks up if polled for lun != 0 */
 233 {"SANKYO", "CP525","6.64", BLIST_NOLUN},        /* causes failed REQ SENSE, extra reset */
 234 {"HP", "C1750A", "3226", BLIST_NOLUN},          /* scanjet iic */
 235 {"HP", "C1790A", "", BLIST_NOLUN},              /* scanjet iip */
 236 {"HP", "C2500A", "", BLIST_NOLUN},              /* scanjet iicx */
 237 
 238 /*
 239  * Other types of devices that have special flags.
 240  */
 241 {"SONY","CD-ROM CDU-8001","*", BLIST_BORKEN},
 242 {"TEXEL","CD-ROM","1.06", BLIST_BORKEN},
 243 {"INSITE","Floptical   F*8I","*", BLIST_KEY},
 244 {"INSITE","I325VM","*", BLIST_KEY},
 245 {"PIONEER","CD-ROMDRM-602X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
 246 {"PIONEER","CD-ROMDRM-604X","*", BLIST_FORCELUN | BLIST_SINGLELUN},
 247 /*
 248  * Must be at end of list...
 249  */
 250 {NULL, NULL, NULL}
 251 };
 252 
 253 static int get_device_flags(unsigned char * response_data){
     /* [previous][next][first][last][top][bottom][index][help] */
 254     int i = 0;
 255     unsigned char * pnt;
 256     for(i=0; 1; i++){
 257         if(device_list[i].vendor == NULL) return 0;
 258         pnt = &response_data[8];
 259         while(*pnt && *pnt == ' ') pnt++;
 260         if(memcmp(device_list[i].vendor, pnt,
 261                   strlen(device_list[i].vendor))) continue;
 262         pnt = &response_data[16];
 263         while(*pnt && *pnt == ' ') pnt++;
 264         if(memcmp(device_list[i].model, pnt,
 265                   strlen(device_list[i].model))) continue;
 266         return device_list[i].flags;
 267     }
 268     return 0;
 269 }
 270 
 271 /*
 272  *  As the actual SCSI command runs in the background, we must set up a
 273  *  flag that tells scan_scsis() when the result it has is valid.
 274  *  scan_scsis can set the_result to -1, and watch for it to become the
 275  *  actual return code for that call.  the scan_scsis_done function() is
 276  *  our user specified completion function that is passed on to the
 277  *  scsi_do_cmd() function.
 278  */
 279 
 280 volatile int in_scan_scsis = 0;
 281 static int the_result;
 282 
 283 void scsi_make_blocked_list(void)  {
     /* [previous][next][first][last][top][bottom][index][help] */
 284     int block_count = 0, index;
 285     unsigned int flags;
 286     struct Scsi_Host * sh[128], * shpnt;
 287     
 288     /*
 289      * Create a circular linked list from the scsi hosts which have
 290      * the "wish_block" field in the Scsi_Host structure set.
 291      * The blocked list should include all the scsi hosts using ISA DMA.
 292      * In some systems, using two dma channels simultaneously causes
 293      * unpredictable results.
 294      * Among the scsi hosts in the blocked list, only one host at a time
 295      * is allowed to have active commands queued. The transition from
 296      * one active host to the next one is allowed only when host_busy == 0
 297      * for the active host (which implies host_busy == 0 for all the hosts
 298      * in the list). Moreover for block devices the transition to a new
 299      * active host is allowed only when a request is completed, since a
 300      * block device request can be divided into multiple scsi commands
 301      * (when there are few sg lists or clustering is disabled).
 302      *
 303      * (DB, 4 Feb 1995)
 304      */
 305     
 306     save_flags(flags);
 307     cli();
 308     host_active = NULL;
 309     
 310     for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next) {
 311         
 312 #if 0
 313         /*
 314          * Is this is a candidate for the blocked list?
 315          * Useful to put into the blocked list all the hosts whose driver
 316          * does not know about the host->block feature.
 317          */
 318         if (shpnt->unchecked_isa_dma) shpnt->wish_block = 1;
 319 #endif
 320         
 321         if (shpnt->wish_block) sh[block_count++] = shpnt;
 322     }
 323     
 324     if (block_count == 1) sh[0]->block = NULL;
 325     
 326     else if (block_count > 1) {
 327         
 328         for(index = 0; index < block_count - 1; index++) {
 329             sh[index]->block = sh[index + 1];
 330             printk("scsi%d : added to blocked host list.\n",
 331                    sh[index]->host_no);
 332         }
 333         
 334         sh[block_count - 1]->block = sh[0];
 335         printk("scsi%d : added to blocked host list.\n",
 336                sh[index]->host_no);
 337     }
 338     
 339     restore_flags(flags);
 340 }
 341 
 342 static void scan_scsis_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 343 {
 344     
 345 #ifdef DEBUG
 346     printk ("scan_scsis_done(%p, %06x)\n", SCpnt->host, SCpnt->result);
 347 #endif
 348     SCpnt->request.rq_status = RQ_SCSI_DONE;
 349     
 350     if (SCpnt->request.sem != NULL)
 351         up(SCpnt->request.sem);
 352 }
 353 
 354 #ifdef CONFIG_SCSI_MULTI_LUN
 355 static int max_scsi_luns = 8;
 356 #else
 357 static int max_scsi_luns = 1;
 358 #endif
 359 
 360 void scsi_luns_setup(char *str, int *ints) {
     /* [previous][next][first][last][top][bottom][index][help] */
 361     if (ints[0] != 1)
 362         printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
 363     else
 364         max_scsi_luns = ints[1];
 365 }
 366 
 367 /*
 368  *  Detecting SCSI devices :
 369  *  We scan all present host adapter's busses,  from ID 0 to ID (max_id).
 370  *  We use the INQUIRY command, determine device type, and pass the ID /
 371  *  lun address of all sequential devices to the tape driver, all random
 372  *  devices to the disk driver.
 373  */
 374 static
 375 void scan_scsis (struct Scsi_Host * shpnt, unchar hardcoded, 
     /* [previous][next][first][last][top][bottom][index][help] */
 376                  unchar hchannel, unchar hid, unchar hlun)
 377 {
 378     int dev, lun, type, channel;
 379     unsigned char scsi_cmd [12];
 380     unsigned char scsi_result0 [256];
 381     unsigned char * scsi_result;
 382     Scsi_Device * SDpnt, *SDtail;
 383     struct Scsi_Device_Template * sdtpnt;
 384     int                 bflags;
 385     int                 max_dev_lun = 0;
 386     Scsi_Cmnd  *SCpnt;
 387     
 388     ++in_scan_scsis;
 389     lun = 0;
 390     type = -1;
 391     SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC|GFP_DMA);
 392     SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
 393     SDtail = scsi_devices;
 394     
 395     if(scsi_devices) while(SDtail->next) SDtail = SDtail->next;
 396     
 397     /* Make sure we have something that is valid for DMA purposes */
 398     scsi_result = ((!dma_malloc_freelist  || !shpnt->unchecked_isa_dma)
 399                    ?  &scsi_result0[0] : scsi_malloc(512));
 400     
 401     if(scsi_result == NULL) {
 402         printk("Unable to obtain scsi_result buffer\n");
 403         goto leave;
 404     }
 405     
 406     shpnt->host_queue = SCpnt; /* We need this so that commands can time out */
 407 
 408     if(hardcoded == 1) {
 409         channel = hchannel;
 410         dev = hid;
 411         lun = hlun;
 412         goto crude; /* Anyone remember good ol' BASIC ?  :-) */
 413     }
 414 
 415     for (channel = 0; channel <= shpnt->max_channel; channel++)
 416     {
 417         for (dev = 0; dev < shpnt->max_id; ++dev) {
 418             if (shpnt->this_id != dev) {
 419                 
 420                 /*
 421                  * We need the for so our continue, etc. work fine.
 422                  * We put this in a variable so that we can override
 423                  * it during the scan if we detect a device *KNOWN*
 424                  * to have multiple logical units.
 425                  */
 426                 max_dev_lun = (max_scsi_luns < shpnt->max_lun ? 
 427                                max_scsi_luns : shpnt->max_lun);
 428 
 429                 for (lun = 0; lun < max_dev_lun; ++lun)
 430                 {
 431                 crude:
 432                     memset(SDpnt, 0, sizeof(Scsi_Device));
 433                     SDpnt->host = shpnt;
 434                     SDpnt->id = dev;
 435                     SDpnt->lun = lun;
 436                     SDpnt->channel = channel;
 437 
 438                     /* Some low level driver could use device->type (DB) */
 439                     SDpnt->type = -1;
 440                     /*
 441                      * Assume that the device will have handshaking problems, 
 442                      * and then fix this field later if it turns out it doesn't
 443                      */
 444                     SDpnt->borken = 1;
 445                     SDpnt->was_reset = 0;
 446                     SDpnt->expecting_cc_ua = 0;
 447                     
 448                     scsi_cmd[0] = TEST_UNIT_READY;
 449                     scsi_cmd[1] = lun << 5;
 450                     scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = 0;
 451                     
 452                     memset(SCpnt, 0,  sizeof(Scsi_Cmnd));
 453                     SCpnt->host = SDpnt->host;
 454                     SCpnt->device = SDpnt;
 455                     SCpnt->target = SDpnt->id;
 456                     SCpnt->lun = SDpnt->lun;
 457                     SCpnt->channel = SDpnt->channel;
 458 
 459                     {
 460                         /*
 461                          * Do the actual command, and wait for it to finish
 462                          */
 463                         struct semaphore sem = MUTEX_LOCKED;
 464                         SCpnt->request.sem = &sem;
 465                         SCpnt->request.rq_status = RQ_SCSI_BUSY;
 466                         scsi_do_cmd (SCpnt, (void *)  scsi_cmd, 
 467                                      (void *) scsi_result,
 468                                      256,  scan_scsis_done, SCSI_TIMEOUT + 4 * HZ, 5);
 469                         down(&sem);
 470                     }
 471 
 472 #if defined(DEBUG) || defined(DEBUG_INIT)
 473                     printk("scsi: scan SCSIS id %d lun %d\n", dev, lun);
 474                     printk("scsi: return code %08x\n", SCpnt->result);
 475 #endif
 476 
 477                     if(SCpnt->result) {
 478                         if (((driver_byte(SCpnt->result) & DRIVER_SENSE) ||
 479                              (status_byte(SCpnt->result) & CHECK_CONDITION)) &&
 480                             ((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
 481                             if (SCpnt->sense_buffer[2] &0xe0)
 482                                 continue; /* No devices here... */
 483                             if(((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
 484                                ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION))
 485                                 continue;
 486                         }
 487                         else
 488                             break;
 489                     }
 490                     
 491 #if defined (DEBUG) || defined(DEBUG_INIT)
 492                     printk("scsi: performing INQUIRY\n");
 493 #endif
 494 
 495                     /*
 496                      * Build an INQUIRY command block.
 497                      */
 498                     scsi_cmd[0] = INQUIRY;
 499                     scsi_cmd[1] = (lun << 5) & 0xe0;
 500                     scsi_cmd[2] = 0;
 501                     scsi_cmd[3] = 0;
 502                     scsi_cmd[4] = 255;
 503                     scsi_cmd[5] = 0;
 504                     
 505                     SCpnt->cmd_len = 0;
 506 
 507                     {
 508                         struct semaphore sem = MUTEX_LOCKED;
 509                         SCpnt->request.sem = &sem;
 510                         SCpnt->request.rq_status = RQ_SCSI_BUSY;
 511                         scsi_do_cmd (SCpnt, (void *)  scsi_cmd, 
 512                                  (void *) scsi_result,
 513                                  256,  scan_scsis_done, SCSI_TIMEOUT, 3);
 514                         down(&sem);
 515                     }
 516                     
 517                     the_result = SCpnt->result;
 518                     
 519 #if defined(DEBUG) || defined(DEBUG_INIT)
 520                     if (!the_result)
 521                         printk("scsi: INQUIRY successful\n");
 522                     else
 523                         printk("scsi: INQUIRY failed with code %08x\n", the_result);
 524 #endif
 525                     
 526                     if(the_result) break;
 527                     
 528                     /* skip other luns on this device */
 529                     
 530                     if (!the_result)
 531                     {
 532                         /* It would seem some TOSHIBA CDROM 
 533                          * gets things wrong 
 534                          */
 535                         if (!strncmp(scsi_result+8,"TOSHIBA",7) &&
 536                             !strncmp(scsi_result+16,"CD-ROM",6) &&
 537                             scsi_result[0] == TYPE_DISK) {
 538                             scsi_result[0] = TYPE_ROM;
 539                             scsi_result[1] |= 0x80;  /* removable */
 540                         }
 541                         
 542                         if (!strncmp(scsi_result+8,"NEC",3)) {
 543                             if (!strncmp(scsi_result+16,"CD-ROM DRIVE:84 ",16) || 
 544                                 !strncmp(scsi_result+16,"CD-ROM DRIVE:25",15))
 545                                 SDpnt->manufacturer = SCSI_MAN_NEC_OLDCDR;
 546                             else
 547                                 SDpnt->manufacturer = SCSI_MAN_NEC;
 548                         } else if (!strncmp(scsi_result+8,"TOSHIBA",7))
 549                             SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
 550                         else if (!strncmp(scsi_result+8,"SONY",4))
 551                             SDpnt->manufacturer = SCSI_MAN_SONY;
 552                         else if (!strncmp(scsi_result+8, "PIONEER", 7))
 553                             SDpnt->manufacturer = SCSI_MAN_PIONEER;
 554                         else
 555                             SDpnt->manufacturer = SCSI_MAN_UNKNOWN;
 556                         
 557                         memcpy(SDpnt->vendor, scsi_result+8, 8);
 558                         memcpy(SDpnt->model, scsi_result+16, 16);
 559                         memcpy(SDpnt->rev, scsi_result+32, 4);
 560  
 561                         SDpnt->removable = (0x80 & scsi_result[1]) >> 7;
 562                         SDpnt->lockable = SDpnt->removable;
 563                         SDpnt->changed = 0;
 564                         SDpnt->access_count = 0;
 565                         SDpnt->busy = 0;
 566                         SDpnt->has_cmdblocks = 0;
 567                         /*
 568                          * Currently, all sequential devices are assumed to be
 569                          * tapes, all random devices disk, with the appropriate
 570                          * read only flags set for ROM / WORM treated as RO.
 571                          */
 572                         
 573                         switch (type = (scsi_result[0] & 0x1f))
 574                         {
 575                         case TYPE_TAPE :
 576                         case TYPE_DISK :
 577                         case TYPE_MOD :
 578                         case TYPE_PROCESSOR :
 579                         case TYPE_SCANNER :
 580                             SDpnt->writeable = 1;
 581                             break;
 582                         case TYPE_WORM :
 583                         case TYPE_ROM :
 584                             SDpnt->writeable = 0;
 585                             break;
 586                         default :
 587 #if 0
 588 #ifdef DEBUG
 589                             printk("scsi: unknown type %d\n", type);
 590                             print_inquiry(scsi_result);
 591 #endif
 592                             type = -1;
 593 #endif
 594                         }
 595                         
 596                         SDpnt->single_lun = 0;
 597                         SDpnt->soft_reset =
 598                             (scsi_result[7] & 1) && ((scsi_result[3] &7) == 2);
 599                         SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
 600                         SDpnt->type = (type & 0x1f);
 601                         
 602                         if (type != -1)
 603                         {
 604                             print_inquiry(scsi_result);
 605                             
 606                             for(sdtpnt = scsi_devicelist; sdtpnt; 
 607                                 sdtpnt = sdtpnt->next)
 608                                 if(sdtpnt->detect) SDpnt->attached +=
 609                                     (*sdtpnt->detect)(SDpnt);
 610                             
 611                             SDpnt->scsi_level = scsi_result[2] & 0x07;
 612                             if (SDpnt->scsi_level >= 2 ||
 613                                 (SDpnt->scsi_level == 1 &&
 614                                  (scsi_result[3] & 0x0f) == 1))
 615                                 SDpnt->scsi_level++;
 616                             /*
 617                              * Set the tagged_queue flag for SCSI-II devices 
 618                              * that purport to support
 619                              * tagged queuing in the INQUIRY data.
 620                              */
 621                             
 622                             SDpnt->tagged_queue = 0;
 623                             
 624                             if ((SDpnt->scsi_level >= SCSI_2) &&
 625                                 (scsi_result[7] & 2)) {
 626                                 SDpnt->tagged_supported = 1;
 627                                 SDpnt->current_tag = 0;
 628                             }
 629                             
 630                             /*
 631                              * Accommodate drivers that want to sleep when 
 632                              * they should be in a polling loop.
 633                              */
 634 
 635                             SDpnt->disconnect = 0;
 636 
 637                             /*
 638                              * Get any flags for this device.
 639                              */
 640                             bflags = get_device_flags(scsi_result);
 641 
 642                             
 643                             /*
 644                              * Some revisions of the Texel CD ROM drives have 
 645                              * handshaking problems when used with the Seagate
 646                              * controllers.  Before we know what type of device
 647                              * we're talking to, we assume it's borken and then
 648                              * change it here if it turns out that it isn't
 649                              * a TEXEL drive.
 650                              */
 651                             if( (bflags & BLIST_BORKEN) == 0 )
 652                             {
 653                                 SDpnt->borken = 0;
 654                             }
 655                             
 656                             
 657                             /* These devices need this "key" to unlock the
 658                              * devices so we can use it 
 659                              */
 660                             if( (bflags & BLIST_KEY) != 0 ) {
 661                                 printk("Unlocked floptical drive.\n");
 662                                 SDpnt->lockable = 0;
 663                                 scsi_cmd[0] = MODE_SENSE;
 664                                 scsi_cmd[1] = (lun << 5) & 0xe0;
 665                                 scsi_cmd[2] = 0x2e;
 666                                 scsi_cmd[3] = 0;
 667                                 scsi_cmd[4] = 0x2a;
 668                                 scsi_cmd[5] = 0;
 669                                 
 670                                 SCpnt->cmd_len = 0;
 671                                 {
 672                                     struct semaphore sem = MUTEX_LOCKED;
 673                                     SCpnt->request.rq_status = RQ_SCSI_BUSY;
 674                                     SCpnt->request.sem = &sem;
 675                                     scsi_do_cmd (SCpnt, (void *)  scsi_cmd,
 676                                              (void *) scsi_result, 0x2a,  
 677                                              scan_scsis_done, SCSI_TIMEOUT, 3);
 678                                     down(&sem);
 679                                 }
 680                             }
 681                             /* Add this device to the linked list at the end */
 682                             if(SDtail)
 683                                 SDtail->next = SDpnt;
 684                             else
 685                                 scsi_devices = SDpnt;
 686                             SDtail = SDpnt;
 687                             
 688                             SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
 689                             /* Some scsi devices cannot be polled for lun != 0
 690                              * due to firmware bugs 
 691                              */
 692                             if(bflags & BLIST_NOLUN) break;
 693 
 694                             /*
 695                              * If we want to only allow I/O to one of the luns
 696                              * attached to this device at a time, then we set 
 697                              * this flag.
 698                              */
 699                             if(bflags & BLIST_SINGLELUN)
 700                             {
 701                                 SDpnt->single_lun = 1;
 702                             }
 703 
 704                             /*
 705                              * If this device is known to support multiple 
 706                              * units, override the other settings, and scan 
 707                              * all of them.
 708                              */
 709                             if(bflags & BLIST_FORCELUN)
 710                             {
 711                                 /*
 712                                  * We probably want to make this a variable, 
 713                                  * but this will do for now.
 714                                  */
 715                                 max_dev_lun = 8;
 716                             }
 717  
 718 
 719                             /* Old drives like the MAXTOR XT-3280 say vers=0 */
 720                             if ((scsi_result[2] & 0x07) == 0)
 721                                 break;
 722                             /* Some scsi-1 peripherals do not handle lun != 0.
 723                              * I am assuming that scsi-2 peripherals do better 
 724                              */
 725                             if((scsi_result[2] & 0x07) == 1 &&
 726                                (scsi_result[3] & 0x0f) == 0) break;
 727                         }
 728                     }       /* if result == DID_OK ends */
 729 
 730                     /*
 731                      * This might screw us up with multi-lun devices, but the 
 732                      * user can scan for them too.
 733                      */
 734                     if(hardcoded == 1)
 735                         goto leave;
 736                 } /* for lun ends */
 737             } /* if this_id != id ends */
 738         } /* for dev ends */
 739     } /* for channel ends */
 740     
 741  leave:
 742     shpnt->host_queue = NULL;  /* No longer needed here */
 743     
 744     /* Last device block does not exist.  Free memory. */
 745     if(SDpnt != NULL)
 746         scsi_init_free((char *) SDpnt, sizeof(Scsi_Device));
 747     
 748     if(SCpnt != NULL)
 749         scsi_init_free((char *) SCpnt, sizeof(Scsi_Cmnd));
 750     
 751     /* If we allocated a buffer so we could do DMA, free it now */
 752     if (scsi_result != &scsi_result0[0] && scsi_result != NULL) 
 753         scsi_free(scsi_result, 512);
 754     
 755     in_scan_scsis = 0;
 756 }       /* scan_scsis  ends */
 757 
 758 /*
 759  *  Flag bits for the internal_timeout array
 760  */
 761 
 762 #define NORMAL_TIMEOUT 0
 763 #define IN_ABORT 1
 764 #define IN_RESET 2
 765 
 766 /*
 767  * This is our time out function, called when the timer expires for a
 768  * given host adapter.  It will attempt to abort the currently executing
 769  * command, that failing perform a kernel panic.
 770  */
 771 
 772 static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
 773 {
 774     
 775     switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
 776     {
 777     case NORMAL_TIMEOUT:
 778         if (!in_scan_scsis) {
 779 #ifdef DEBUG_TIMEOUT
 780             scsi_dump_status();
 781 #endif
 782         }
 783         
 784         if (!scsi_abort (SCpnt, DID_TIME_OUT, pid))
 785             return;
 786     case IN_ABORT:
 787         printk("SCSI host %d abort() timed out - resetting\n",
 788                SCpnt->host->host_no);
 789         if (!scsi_reset (SCpnt, FALSE))
 790             return;
 791     case IN_RESET:
 792     case (IN_ABORT | IN_RESET):
 793         /* This might be controversial, but if there is a bus hang,
 794          * you might conceivably want the machine up and running
 795          * esp if you have an ide disk. 
 796          */
 797         printk("Unable to reset scsi host %d - ", SCpnt->host->host_no);
 798         printk("probably a SCSI bus hang.\n");
 799         SCpnt->internal_timeout &= ~IN_RESET;
 800         scsi_reset (SCpnt, TRUE);
 801         return;
 802         
 803     default:
 804         INTERNAL_ERROR;
 805     }
 806     
 807 }
 808 
 809 
 810 /* This function takes a quick look at a request, and decides if it
 811  * can be queued now, or if there would be a stall while waiting for
 812  * something else to finish.  This routine assumes that interrupts are
 813  * turned off when entering the routine.  It is the responsibility
 814  * of the calling code to ensure that this is the case. 
 815  */
 816 
 817 Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 818 {
 819     Scsi_Cmnd * SCpnt = NULL;
 820     int tablesize;
 821     Scsi_Cmnd * found = NULL;
 822     struct buffer_head * bh, *bhp;
 823     
 824     if (!device)
 825         panic ("No device passed to request_queueable().\n");
 826     
 827     if (req && req->rq_status == RQ_INACTIVE)
 828         panic("Inactive in request_queueable");
 829     
 830     SCpnt =  device->host->host_queue;
 831 
 832     /*
 833      * Look for a free command block.  If we have been instructed not to queue
 834      * multiple commands to multi-lun devices, then check to see what else is 
 835      * going for this device first.
 836      */
 837       
 838     SCpnt = device->host->host_queue;
 839     if (!device->single_lun) {
 840         while(SCpnt){
 841             if(SCpnt->target == device->id &&
 842                SCpnt->lun == device->lun) {
 843                 if(SCpnt->request.rq_status == RQ_INACTIVE) break;
 844             }
 845             SCpnt = SCpnt->next;
 846         }
 847     } else {
 848         while(SCpnt){
 849             if(SCpnt->target == device->id) {
 850                 if (SCpnt->lun == device->lun) {
 851                     if(found == NULL 
 852                        && SCpnt->request.rq_status == RQ_INACTIVE) 
 853                     {
 854                         found=SCpnt;
 855                     }
 856                 } 
 857                 if(SCpnt->request.rq_status != RQ_INACTIVE) {
 858                     /*
 859                      * I think that we should really limit things to one
 860                      * outstanding command per device - this is what tends 
 861                      * to trip up buggy firmware.
 862                      */
 863                     return NULL;
 864                 }
 865             }
 866             SCpnt = SCpnt->next;
 867         }
 868         SCpnt = found;
 869     }
 870     
 871     if (!SCpnt) return NULL;
 872     
 873     if (SCSI_BLOCK(device->host)) return NULL;
 874     
 875     if (req) {
 876         memcpy(&SCpnt->request, req, sizeof(struct request));
 877         tablesize = device->host->sg_tablesize;
 878         bhp = bh = req->bh;
 879         if(!tablesize) bh = NULL;
 880         /* Take a quick look through the table to see how big it is.  
 881          * We already have our copy of req, so we can mess with that 
 882          * if we want to. 
 883          */
 884         while(req->nr_sectors && bh){
 885             bhp = bhp->b_reqnext;
 886             if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
 887             req->nr_sectors -= bh->b_size >> 9;
 888             req->sector += bh->b_size >> 9;
 889             if(!tablesize) break;
 890             bh = bhp;
 891         }
 892         if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
 893             SCpnt->request.bhtail = bh;
 894             req->bh = bh->b_reqnext; /* Divide request */
 895             bh->b_reqnext = NULL;
 896             bh = req->bh;
 897             
 898             /* Now reset things so that req looks OK */
 899             SCpnt->request.nr_sectors -= req->nr_sectors;
 900             req->current_nr_sectors = bh->b_size >> 9;
 901             req->buffer = bh->b_data;
 902             SCpnt->request.sem = NULL; /* Wait until whole thing done */
 903         } else {
 904             req->rq_status = RQ_INACTIVE;
 905             wake_up(&wait_for_request);
 906         }
 907     } else {
 908         SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Busy, but no request */
 909         SCpnt->request.sem = NULL;   /* And no one is waiting for the device 
 910                                       * either */
 911     }
 912     
 913     SCpnt->use_sg = 0;               /* Reset the scatter-gather flag */
 914     SCpnt->old_use_sg  = 0;
 915     SCpnt->transfersize = 0;
 916     SCpnt->underflow = 0;
 917     SCpnt->cmd_len = 0;
 918 
 919 /* Since not everyone seems to set the device info correctly
 920  * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
 921  */ 
 922     SCpnt->channel = device->channel;
 923     SCpnt->lun = device->lun;
 924     SCpnt->target = device->id;
 925 
 926     return SCpnt;
 927 }
 928 
 929 /* This function returns a structure pointer that will be valid for
 930  * the device.  The wait parameter tells us whether we should wait for
 931  * the unit to become free or not.  We are also able to tell this routine
 932  * not to return a descriptor if the host is unable to accept any more
 933  * commands for the time being.  We need to keep in mind that there is no
 934  * guarantee that the host remain not busy.  Keep in mind the
 935  * request_queueable function also knows the internal allocation scheme
 936  * of the packets for each device 
 937  */
 938 
 939 Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
     /* [previous][next][first][last][top][bottom][index][help] */
 940                              int wait)
 941 {
 942     kdev_t dev;
 943     struct request * req = NULL;
 944     int tablesize;
 945     unsigned int flags;
 946     struct buffer_head * bh, *bhp;
 947     struct Scsi_Host * host;
 948     Scsi_Cmnd * SCpnt = NULL;
 949     Scsi_Cmnd * SCwait = NULL;
 950     Scsi_Cmnd * found = NULL;
 951     
 952     if (!device)
 953         panic ("No device passed to allocate_device().\n");
 954     
 955     if (reqp) req = *reqp;
 956     
 957     /* See if this request has already been queued by an interrupt routine */
 958     if (req) {
 959         if(req->rq_status == RQ_INACTIVE) return NULL;
 960         dev = req->rq_dev;
 961     } else
 962         dev = 0;                /* unused */
 963     
 964     host = device->host;
 965     
 966     if (intr_count && SCSI_BLOCK(host)) return NULL;
 967     
 968     while (1==1){
 969         SCpnt = device->host->host_queue;
 970         if (!device->single_lun) {
 971             while(SCpnt){
 972                 if(SCpnt->target == device->id &&
 973                    SCpnt->lun == device->lun) {
 974                    SCwait = SCpnt;
 975                     if(SCpnt->request.rq_status == RQ_INACTIVE) break;
 976                 }
 977                 SCpnt = SCpnt->next;
 978             }
 979         } else {
 980             while(SCpnt){
 981                 if(SCpnt->target == device->id) {
 982                     if (SCpnt->lun == device->lun) {
 983                         SCwait = SCpnt;
 984                         if(found == NULL 
 985                            && SCpnt->request.rq_status == RQ_INACTIVE) 
 986                         {
 987                             found=SCpnt;
 988                         }
 989                     } 
 990                     if(SCpnt->request.rq_status != RQ_INACTIVE) {
 991                         /*
 992                          * I think that we should really limit things to one
 993                          * outstanding command per device - this is what tends
 994                          * to trip up buggy firmware.
 995                          */
 996                         found = NULL;
 997                         break;
 998                     }
 999                 }
1000                 SCpnt = SCpnt->next;
1001             }
1002             SCpnt = found;
1003         }
1004 
1005         save_flags(flags);
1006         cli();
1007         /* See if this request has already been queued by an interrupt routine
1008          */
1009         if (req && (req->rq_status == RQ_INACTIVE || req->rq_dev != dev)) {
1010             restore_flags(flags);
1011             return NULL;
1012         }
1013         if (!SCpnt || SCpnt->request.rq_status != RQ_INACTIVE)  /* Might have changed */
1014         {
1015             restore_flags(flags);
1016             if(!wait) return NULL;
1017             if (!SCwait) {
1018                 printk("Attempt to allocate device channel %d, target %d, "
1019                        "lun %d\n", device->channel, device->id, device->lun);
1020                 panic("No device found in allocate_device\n");
1021             }
1022             SCSI_SLEEP(&device->device_wait,
1023                        (SCwait->request.rq_status != RQ_INACTIVE));
1024         } else {
1025             if (req) {
1026                 memcpy(&SCpnt->request, req, sizeof(struct request));
1027                 tablesize = device->host->sg_tablesize;
1028                 bhp = bh = req->bh;
1029                 if(!tablesize) bh = NULL;
1030                 /* Take a quick look through the table to see how big it is.  
1031                  * We already have our copy of req, so we can mess with that 
1032                  * if we want to.  
1033                  */
1034                 while(req->nr_sectors && bh){
1035                     bhp = bhp->b_reqnext;
1036                     if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
1037                     req->nr_sectors -= bh->b_size >> 9;
1038                     req->sector += bh->b_size >> 9;
1039                     if(!tablesize) break;
1040                     bh = bhp;
1041                 }
1042                 if(req->nr_sectors && bh && bh->b_reqnext){/* Any leftovers? */
1043                     SCpnt->request.bhtail = bh;
1044                     req->bh = bh->b_reqnext; /* Divide request */
1045                     bh->b_reqnext = NULL;
1046                     bh = req->bh;
1047                     /* Now reset things so that req looks OK */
1048                     SCpnt->request.nr_sectors -= req->nr_sectors;
1049                     req->current_nr_sectors = bh->b_size >> 9;
1050                     req->buffer = bh->b_data;
1051                     SCpnt->request.sem = NULL; /* Wait until whole thing done*/
1052                 }
1053                 else
1054                 {
1055                     req->rq_status = RQ_INACTIVE;
1056                     *reqp = req->next;
1057                     wake_up(&wait_for_request);
1058                 }
1059             } else {
1060                 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1061                 SCpnt->request.sem = NULL;   /* And no one is waiting for this 
1062                                               * to complete */
1063             }
1064             restore_flags(flags);
1065             break;
1066         }
1067     }
1068     
1069     SCpnt->use_sg = 0;            /* Reset the scatter-gather flag */
1070     SCpnt->old_use_sg  = 0;
1071     SCpnt->transfersize = 0;      /* No default transfer size */
1072     SCpnt->cmd_len = 0;
1073 
1074     SCpnt->underflow = 0;         /* Do not flag underflow conditions */
1075 
1076     /* Since not everyone seems to set the device info correctly
1077      * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
1078      */ 
1079     SCpnt->channel = device->channel;
1080     SCpnt->lun = device->lun;
1081     SCpnt->target = device->id;
1082 
1083     return SCpnt;
1084 }
1085 
1086 /*
1087  * This is inline because we have stack problemes if we recurse to deeply.
1088  */
1089 
1090 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1091 {
1092     int temp;
1093     struct Scsi_Host * host;
1094     unsigned int flags;
1095 #ifdef DEBUG_DELAY
1096     int clock;
1097 #endif
1098     
1099     host = SCpnt->host;
1100     
1101     /*
1102      * We will wait MIN_RESET_DELAY clock ticks after the last reset so
1103      * we can avoid the drive not being ready.
1104      */
1105     save_flags(flags);
1106     sti();
1107     temp = host->last_reset + MIN_RESET_DELAY;
1108     while (jiffies < temp);
1109     restore_flags(flags);
1110     
1111     update_timeout(SCpnt, SCpnt->timeout_per_command);
1112     
1113     /*
1114      * We will use a queued command if possible, otherwise we will emulate the
1115      * queuing and calling of completion function ourselves.
1116      */
1117 #ifdef DEBUG
1118     printk("internal_cmnd (host = %d, channel = %d, target = %d, "
1119            "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n", 
1120            SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd, 
1121            SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
1122 #endif
1123     
1124     if (host->can_queue)
1125     {
1126 #ifdef DEBUG
1127         printk("queuecommand : routine at %p\n",
1128                host->hostt->queuecommand);
1129 #endif
1130         /* This locking tries to prevent all sorts of races between
1131          * queuecommand and the interrupt code.  In effect,
1132          * we are only allowed to be in queuecommand once at
1133          * any given time, and we can only be in the interrupt
1134          * handler and the queuecommand function at the same time
1135          * when queuecommand is called while servicing the
1136          * interrupt. 
1137          */
1138         
1139         if(!intr_count && SCpnt->host->irq)
1140             disable_irq(SCpnt->host->irq);
1141         
1142         host->hostt->queuecommand (SCpnt, scsi_done);
1143         
1144         if(!intr_count && SCpnt->host->irq)
1145             enable_irq(SCpnt->host->irq);
1146     }
1147     else
1148     {
1149         
1150 #ifdef DEBUG
1151         printk("command() :  routine at %p\n", host->hostt->command);
1152 #endif
1153         temp=host->hostt->command (SCpnt);
1154         SCpnt->result = temp;
1155 #ifdef DEBUG_DELAY
1156         clock = jiffies + 4 * HZ;
1157         while (jiffies < clock);
1158         printk("done(host = %d, result = %04x) : routine at %08x\n", 
1159                host->host_no, temp);
1160 #endif
1161         scsi_done(SCpnt);
1162     }
1163 #ifdef DEBUG
1164     printk("leaving internal_cmnd()\n");
1165 #endif
1166 }
1167 
1168 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1169 {
1170     unsigned int flags;
1171     
1172     save_flags(flags);
1173     cli();
1174     SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
1175     update_timeout(SCpnt, SENSE_TIMEOUT);
1176     restore_flags(flags);
1177     
1178     
1179     memcpy ((void *) SCpnt->cmnd , (void *) generic_sense, 
1180             sizeof(generic_sense));
1181     
1182     SCpnt->cmnd[1] = SCpnt->lun << 5;
1183     SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
1184     
1185     SCpnt->request_buffer = &SCpnt->sense_buffer;
1186     SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
1187     SCpnt->use_sg = 0;
1188     SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1189     internal_cmnd (SCpnt);
1190 }
1191 
1192 
1193 
1194 /*
1195  * scsi_do_cmd sends all the commands out to the low-level driver.  It
1196  * handles the specifics required for each low level driver - ie queued
1197  * or non queued.  It also prevents conflicts when different high level
1198  * drivers go for the same host at the same time.
1199  */
1200 
1201 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
     /* [previous][next][first][last][top][bottom][index][help] */
1202                   void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
1203                   int timeout, int retries)
1204 {
1205     unsigned long flags;
1206     struct Scsi_Host * host = SCpnt->host;
1207     
1208 #ifdef DEBUG
1209     {
1210         int i;
1211         int target = SCpnt->target;
1212         printk ("scsi_do_cmd (host = %d, channel = %d target = %d, "
1213                 "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
1214                 "retries = %d)\n"
1215                 "command : " , host->host_no, SCpnt->channel, target, buffer, 
1216                 bufflen, done, timeout, retries);
1217         for (i = 0; i < 10; ++i)
1218             printk ("%02x  ", ((unsigned char *) cmnd)[i]);
1219         printk("\n");
1220     }
1221 #endif
1222     
1223     if (!host)
1224     {
1225         panic ("Invalid or not present host.\n");
1226     }
1227     
1228     
1229     /*
1230      * We must prevent reentrancy to the lowlevel host driver.  This prevents
1231      * it - we enter a loop until the host we want to talk to is not busy.
1232      * Race conditions are prevented, as interrupts are disabled in between the
1233      * time we check for the host being not busy, and the time we mark it busy
1234      * ourselves.
1235      */
1236 
1237     save_flags(flags);
1238     cli();
1239     SCpnt->pid = scsi_pid++;
1240     
1241     while (SCSI_BLOCK(host)) {
1242         restore_flags(flags);
1243         SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
1244         cli();
1245     }
1246     
1247     if (host->block) host_active = host;
1248     
1249     host->host_busy++;
1250     restore_flags(flags);
1251     
1252     /*
1253      * Our own function scsi_done (which marks the host as not busy, disables
1254      * the timeout counter, etc) will be called by us or by the
1255      * scsi_hosts[host].queuecommand() function needs to also call
1256      * the completion function for the high level driver.
1257      */
1258     
1259     memcpy ((void *) SCpnt->data_cmnd , (const void *) cmnd, 12);
1260 #if 0
1261     SCpnt->host = host;
1262     SCpnt->channel = channel;
1263     SCpnt->target = target;
1264     SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
1265 #endif
1266     SCpnt->bufflen = bufflen;
1267     SCpnt->buffer = buffer;
1268     SCpnt->flags=0;
1269     SCpnt->retries=0;
1270     SCpnt->allowed=retries;
1271     SCpnt->done = done;
1272     SCpnt->timeout_per_command = timeout;
1273 
1274     memcpy ((void *) SCpnt->cmnd , (const void *) cmnd, 12);
1275     /* Zero the sense buffer.  Some host adapters automatically request
1276      * sense on error.  0 is not a valid sense code.  
1277      */
1278     memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1279     SCpnt->request_buffer = buffer;
1280     SCpnt->request_bufflen = bufflen;
1281     SCpnt->old_use_sg = SCpnt->use_sg;
1282     if (SCpnt->cmd_len == 0)
1283         SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1284     SCpnt->old_cmd_len = SCpnt->cmd_len;
1285 
1286     /* Start the timer ticking.  */
1287 
1288     SCpnt->internal_timeout = 0;
1289     SCpnt->abort_reason = 0;
1290     internal_cmnd (SCpnt);
1291 
1292 #ifdef DEBUG
1293     printk ("Leaving scsi_do_cmd()\n");
1294 #endif
1295 }
1296 
1297 static int check_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1298 {
1299     /* If there is no sense information, request it.  If we have already
1300      * requested it, there is no point in asking again - the firmware must
1301      * be confused. 
1302      */
1303     if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1304         if(!(SCpnt->flags & ASKED_FOR_SENSE))
1305             return SUGGEST_SENSE;
1306         else
1307             return SUGGEST_RETRY;
1308     }
1309     
1310     SCpnt->flags &= ~ASKED_FOR_SENSE;
1311     
1312 #ifdef DEBUG_INIT
1313     printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
1314     print_sense("", SCpnt);
1315     printk("\n");
1316 #endif
1317     if (SCpnt->sense_buffer[2] & 0xe0)
1318         return SUGGEST_ABORT;
1319     
1320     switch (SCpnt->sense_buffer[2] & 0xf)
1321     {
1322     case NO_SENSE:
1323         return 0;
1324     case RECOVERED_ERROR:
1325         return SUGGEST_IS_OK;
1326         
1327     case ABORTED_COMMAND:
1328         return SUGGEST_RETRY;
1329     case NOT_READY:
1330     case UNIT_ATTENTION:
1331         /*
1332          * If we are expecting a CC/UA because of a bus reset that we
1333          * performed, treat this just as a retry.  Otherwise this is
1334          * information that we should pass up to the upper-level driver
1335          * so that we can deal with it there.
1336          */
1337         if( SCpnt->device->expecting_cc_ua )
1338         {
1339             SCpnt->device->expecting_cc_ua = 0;
1340             return SUGGEST_RETRY;
1341         }
1342         return SUGGEST_ABORT;
1343         
1344     /* these three are not supported */
1345     case COPY_ABORTED:
1346     case VOLUME_OVERFLOW:
1347     case MISCOMPARE:
1348         
1349     case MEDIUM_ERROR:
1350         return SUGGEST_REMAP;
1351     case BLANK_CHECK:
1352     case DATA_PROTECT:
1353     case HARDWARE_ERROR:
1354     case ILLEGAL_REQUEST:
1355     default:
1356         return SUGGEST_ABORT;
1357     }
1358 }
1359 
1360 /* This function is the mid-level interrupt routine, which decides how
1361  *  to handle error conditions.  Each invocation of this function must
1362  *  do one and *only* one of the following:
1363  *
1364  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
1365  *      normal completion, and indicates that the handling for this
1366  *      request is complete.
1367  *  (2) Call internal_cmnd to requeue the command.  This will result in
1368  *      scsi_done being called again when the retry is complete.
1369  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
1370  *      more information about the error condition.  When the information
1371  *      is available, scsi_done will be called again.
1372  *  (4) Call reset().  This is sort of a last resort, and the idea is that
1373  *      this may kick things loose and get the drive working again.  reset()
1374  *      automatically calls scsi_request_sense, and thus scsi_done will be
1375  *      called again once the reset is complete.
1376  *
1377  *      If none of the above actions are taken, the drive in question
1378  *      will hang. If more than one of the above actions are taken by
1379  *      scsi_done, then unpredictable behavior will result.
1380  */
1381 static void scsi_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1382 {
1383     int status=0;
1384     int exit=0;
1385     int checked;
1386     int oldto;
1387     struct Scsi_Host * host = SCpnt->host;
1388     int result = SCpnt->result;
1389     oldto = update_timeout(SCpnt, 0);
1390     
1391 #ifdef DEBUG_TIMEOUT
1392     if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1393                       result, SCpnt->target, SCpnt->lun);
1394 #endif
1395     
1396     /* If we requested an abort, (and we got it) then fix up the return
1397      *  status to say why 
1398      */
1399     if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1400         SCpnt->result = result = (result & 0xff00ffff) |
1401             (SCpnt->abort_reason << 16);
1402 
1403 
1404 #define FINISHED 0
1405 #define MAYREDO  1
1406 #define REDO     3
1407 #define PENDING  4
1408 
1409 #ifdef DEBUG
1410     printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1411 #endif
1412 
1413     if(SCpnt->flags & WAS_SENSE)
1414     {
1415         SCpnt->use_sg = SCpnt->old_use_sg;
1416         SCpnt->cmd_len = SCpnt->old_cmd_len;
1417     }
1418 
1419     switch (host_byte(result))
1420     {
1421     case DID_OK:
1422         if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1423             /* Failed to obtain sense information */
1424         {
1425             SCpnt->flags &= ~WAS_SENSE;
1426             SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1427             
1428             if (!(SCpnt->flags & WAS_RESET))
1429             {
1430                 printk("scsi%d : channel %d target %d lun %d request sense"
1431                        " failed, performing reset.\n",
1432                        SCpnt->host->host_no, SCpnt->channel, SCpnt->target, 
1433                        SCpnt->lun);
1434                 scsi_reset(SCpnt, FALSE);
1435                 return;
1436             }
1437             else
1438             {
1439                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1440                 status = FINISHED;
1441             }
1442         }
1443         else switch(msg_byte(result))
1444         {
1445         case COMMAND_COMPLETE:
1446             switch (status_byte(result))
1447             {
1448             case GOOD:
1449                 if (SCpnt->flags & WAS_SENSE)
1450                 {
1451 #ifdef DEBUG
1452                     printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1453 #endif
1454                     SCpnt->flags &= ~WAS_SENSE;
1455                     SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1456                     
1457                     switch (checked = check_sense(SCpnt))
1458                     {
1459                     case SUGGEST_SENSE:
1460                     case 0:
1461 #ifdef DEBUG
1462                         printk("NO SENSE.  status = REDO\n");
1463 #endif
1464                         update_timeout(SCpnt, oldto);
1465                         status = REDO;
1466                         break;
1467                     case SUGGEST_IS_OK:
1468                         break;
1469                     case SUGGEST_REMAP:
1470                     case SUGGEST_RETRY:
1471 #ifdef DEBUG
1472                         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1473 #endif
1474                         status = MAYREDO;
1475                         exit = DRIVER_SENSE | SUGGEST_RETRY;
1476                         break;
1477                     case SUGGEST_ABORT:
1478 #ifdef DEBUG
1479                         printk("SENSE SUGGEST ABORT - status = FINISHED");
1480 #endif
1481                         status = FINISHED;
1482                         exit =  DRIVER_SENSE | SUGGEST_ABORT;
1483                         break;
1484                     default:
1485                         printk ("Internal error %s %d \n", __FILE__,
1486                                 __LINE__);
1487                     }
1488                 }
1489                 else
1490                 {
1491 #ifdef DEBUG
1492                     printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1493 #endif
1494                     exit =  DRIVER_OK;
1495                     status = FINISHED;
1496                 }
1497                 break;
1498                 
1499             case CHECK_CONDITION:
1500                 switch (check_sense(SCpnt))
1501                 {
1502                 case 0:
1503                     update_timeout(SCpnt, oldto);
1504                     status = REDO;
1505                     break;
1506                 case SUGGEST_REMAP:
1507                 case SUGGEST_RETRY:
1508                     status = MAYREDO;
1509                     exit = DRIVER_SENSE | SUGGEST_RETRY;
1510                     break;
1511                 case SUGGEST_ABORT:
1512                     status = FINISHED;
1513                     exit =  DRIVER_SENSE | SUGGEST_ABORT;
1514                     break;
1515                 case SUGGEST_SENSE:
1516                     scsi_request_sense (SCpnt);
1517                     status = PENDING;
1518                     break;
1519                 }
1520                 break;
1521                 
1522             case CONDITION_GOOD:
1523             case INTERMEDIATE_GOOD:
1524             case INTERMEDIATE_C_GOOD:
1525                 break;
1526                 
1527             case BUSY:
1528                 update_timeout(SCpnt, oldto);
1529                 status = REDO;
1530                 break;
1531                 
1532             case RESERVATION_CONFLICT:
1533                 printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
1534                        " reset.\n", SCpnt->host->host_no, SCpnt->channel);
1535                 scsi_reset(SCpnt, FALSE);
1536                 return;
1537 #if 0
1538                 exit = DRIVER_SOFT | SUGGEST_ABORT;
1539                 status = MAYREDO;
1540                 break;
1541 #endif
1542             default:
1543                 printk ("Internal error %s %d \n"
1544                         "status byte = %d \n", __FILE__,
1545                         __LINE__, status_byte(result));
1546                 
1547             }
1548             break;
1549         default:
1550             panic("scsi: unsupported message byte %d received\n", 
1551                   msg_byte(result));
1552         }
1553         break;
1554     case DID_TIME_OUT:
1555 #ifdef DEBUG
1556         printk("Host returned DID_TIME_OUT - ");
1557 #endif
1558         
1559         if (SCpnt->flags & WAS_TIMEDOUT)
1560         {
1561 #ifdef DEBUG
1562             printk("Aborting\n");
1563 #endif
1564             exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1565         }
1566         else
1567         {
1568 #ifdef DEBUG
1569             printk ("Retrying.\n");
1570 #endif
1571             SCpnt->flags  |= WAS_TIMEDOUT;
1572             SCpnt->internal_timeout &= ~IN_ABORT;
1573             status = REDO;
1574         }
1575         break;
1576     case DID_BUS_BUSY:
1577     case DID_PARITY:
1578         status = REDO;
1579         break;
1580     case DID_NO_CONNECT:
1581 #ifdef DEBUG
1582         printk("Couldn't connect.\n");
1583 #endif
1584         exit  = (DRIVER_HARD | SUGGEST_ABORT);
1585         break;
1586     case DID_ERROR:
1587         status = MAYREDO;
1588         exit = (DRIVER_HARD | SUGGEST_ABORT);
1589         break;
1590     case DID_BAD_TARGET:
1591     case DID_ABORT:
1592         exit = (DRIVER_INVALID | SUGGEST_ABORT);
1593         break;
1594     case DID_RESET:
1595         if (SCpnt->flags & IS_RESETTING)
1596         {
1597             SCpnt->flags &= ~IS_RESETTING;
1598             status = REDO;
1599             break;
1600         }
1601         
1602         if(msg_byte(result) == GOOD &&
1603            status_byte(result) == CHECK_CONDITION) {
1604             switch (check_sense(SCpnt)) {
1605             case 0:
1606                 update_timeout(SCpnt, oldto);
1607                 status = REDO;
1608                 break;
1609             case SUGGEST_REMAP:
1610             case SUGGEST_RETRY:
1611                 status = MAYREDO;
1612                 exit = DRIVER_SENSE | SUGGEST_RETRY;
1613                 break;
1614             case SUGGEST_ABORT:
1615                 status = FINISHED;
1616                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
1617                 break;
1618             case SUGGEST_SENSE:
1619                 scsi_request_sense (SCpnt);
1620                 status = PENDING;
1621                 break;
1622             }
1623         } else {
1624             status=REDO;
1625             exit = SUGGEST_RETRY;
1626         }
1627         break;
1628     default :
1629         exit = (DRIVER_ERROR | SUGGEST_DIE);
1630     }
1631     
1632     switch (status)
1633     {
1634     case FINISHED:
1635     case PENDING:
1636         break;
1637     case MAYREDO:
1638 #ifdef DEBUG
1639         printk("In MAYREDO, allowing %d retries, have %d\n",
1640                SCpnt->allowed, SCpnt->retries);
1641 #endif
1642         if ((++SCpnt->retries) < SCpnt->allowed)
1643         {
1644             if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1645                 && !(jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
1646                 && !(SCpnt->flags & WAS_RESET))
1647             {
1648                 printk("scsi%d channel %d : resetting for second half of retries.\n",
1649                        SCpnt->host->host_no, SCpnt->channel);
1650                 scsi_reset(SCpnt, FALSE);
1651                 break;
1652             }
1653             
1654         }
1655         else
1656         {
1657             status = FINISHED;
1658             break;
1659         }
1660         /* fall through to REDO */
1661         
1662     case REDO:
1663         
1664         if (SCpnt->flags & WAS_SENSE)
1665             scsi_request_sense(SCpnt);
1666         else
1667         {
1668             memcpy ((void *) SCpnt->cmnd,
1669                     (void*) SCpnt->data_cmnd,
1670                     sizeof(SCpnt->data_cmnd));
1671             SCpnt->request_buffer = SCpnt->buffer;
1672             SCpnt->request_bufflen = SCpnt->bufflen;
1673             SCpnt->use_sg = SCpnt->old_use_sg;
1674             SCpnt->cmd_len = SCpnt->old_cmd_len;
1675             internal_cmnd (SCpnt);
1676         }
1677         break;
1678     default:
1679         INTERNAL_ERROR;
1680     }
1681     
1682     if (status == FINISHED) {
1683 #ifdef DEBUG
1684         printk("Calling done function - at address %p\n", SCpnt->done);
1685 #endif
1686         host->host_busy--; /* Indicate that we are free */
1687         
1688         if (host->block && host->host_busy == 0) {
1689             host_active = NULL;
1690             
1691             /* For block devices "wake_up" is done in end_scsi_request */
1692             if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
1693                 MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
1694                 struct Scsi_Host * next;
1695                 
1696                 for (next = host->block; next != host; next = next->block)
1697                     wake_up(&next->host_wait);
1698             }
1699             
1700         }
1701         
1702         wake_up(&host->host_wait);
1703         SCpnt->result = result | ((exit & 0xff) << 24);
1704         SCpnt->use_sg = SCpnt->old_use_sg;
1705         SCpnt->cmd_len = SCpnt->old_cmd_len;
1706         SCpnt->done (SCpnt);
1707     }
1708     
1709 #undef FINISHED
1710 #undef REDO
1711 #undef MAYREDO
1712 #undef PENDING
1713 }
1714 
1715 /*
1716  * The scsi_abort function interfaces with the abort() function of the host
1717  * we are aborting, and causes the current command to not complete.  The
1718  * caller should deal with any error messages or status returned on the
1719  * next call.
1720  * 
1721  * This will not be called reentrantly for a given host.
1722  */
1723 
1724 /*
1725  * Since we're nice guys and specified that abort() and reset()
1726  * can be non-reentrant.  The internal_timeout flags are used for
1727  * this.
1728  */
1729 
1730 
1731 int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
1732 {
1733     int oldto;
1734     unsigned long flags;
1735     struct Scsi_Host * host = SCpnt->host;
1736     
1737     while(1)
1738     {
1739         save_flags(flags);
1740         cli();
1741         
1742         /*
1743          * Protect against races here.  If the command is done, or we are
1744          * on a different command forget it.
1745          */
1746         if (SCpnt->request.rq_status == RQ_INACTIVE || pid != SCpnt->pid) {
1747             restore_flags(flags);
1748             return 0;
1749         }
1750 
1751         if (SCpnt->internal_timeout & IN_ABORT)
1752         {
1753             restore_flags(flags);
1754             while (SCpnt->internal_timeout & IN_ABORT)
1755                 barrier();
1756         }
1757         else
1758         {
1759             SCpnt->internal_timeout |= IN_ABORT;
1760             oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1761             
1762             if ((SCpnt->flags & IS_RESETTING) &&
1763                 SCpnt->device->soft_reset) {
1764                 /* OK, this command must have died when we did the
1765                  *  reset.  The device itself must have lied. 
1766                  */
1767                 printk("Stale command on %d %d:%d appears to have died when"
1768                        " the bus was reset\n", 
1769                        SCpnt->channel, SCpnt->target, SCpnt->lun);
1770             }
1771             
1772             restore_flags(flags);
1773             if (!host->host_busy) {
1774                 SCpnt->internal_timeout &= ~IN_ABORT;
1775                 update_timeout(SCpnt, oldto);
1776                 return 0;
1777             }
1778             printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
1779                    " channel %d, id %d, lun %d ",
1780                    SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel, 
1781                    (int) SCpnt->target, (int) SCpnt->lun);
1782             print_command (SCpnt->cmnd);
1783             if (SCpnt->request.rq_status == RQ_INACTIVE || pid != SCpnt->pid)
1784                 return 0;
1785             SCpnt->abort_reason = why;
1786             switch(host->hostt->abort(SCpnt)) {
1787                 /* We do not know how to abort.  Try waiting another
1788                  * time increment and see if this helps. Set the
1789                  * WAS_TIMEDOUT flag set so we do not try this twice
1790                  */
1791             case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1792                                    * this is too severe 
1793                                    */
1794             case SCSI_ABORT_SNOOZE:
1795                 if(why == DID_TIME_OUT) {
1796                     save_flags(flags);
1797                     cli();
1798                     SCpnt->internal_timeout &= ~IN_ABORT;
1799                     if(SCpnt->flags & WAS_TIMEDOUT) {
1800                         restore_flags(flags);
1801                         return 1; /* Indicate we cannot handle this.
1802                                    * We drop down into the reset handler
1803                                    * and try again 
1804                                    */
1805                     } else {
1806                         SCpnt->flags |= WAS_TIMEDOUT;
1807                         oldto = SCpnt->timeout_per_command;
1808                         update_timeout(SCpnt, oldto);
1809                     }
1810                     restore_flags(flags);
1811                 }
1812                 return 0;
1813             case SCSI_ABORT_PENDING:
1814                 if(why != DID_TIME_OUT) {
1815                     save_flags(flags);
1816                     cli();
1817                     update_timeout(SCpnt, oldto);
1818                     restore_flags(flags);
1819                 }
1820                 return 0;
1821             case SCSI_ABORT_SUCCESS:
1822                 /* We should have already aborted this one.  No
1823                  * need to adjust timeout 
1824                  */
1825             case SCSI_ABORT_NOT_RUNNING:
1826                 SCpnt->internal_timeout &= ~IN_ABORT;
1827                 update_timeout(SCpnt, 0);
1828                 return 0;
1829             case SCSI_ABORT_ERROR:
1830             default:
1831                 SCpnt->internal_timeout &= ~IN_ABORT;
1832                 return 1;
1833             }
1834         }
1835     }
1836 }
1837 
1838 
1839 /* Mark a single SCSI Device as having been reset. */
1840 
1841 static inline void scsi_mark_device_reset(Scsi_Device *Device)
     /* [previous][next][first][last][top][bottom][index][help] */
1842 {
1843   Device->was_reset = 1;
1844   Device->expecting_cc_ua = 1;
1845 }
1846 
1847 
1848 /* Mark all SCSI Devices on a specific Host as having been reset. */
1849 
1850 void scsi_mark_host_bus_reset(struct Scsi_Host *Host)
     /* [previous][next][first][last][top][bottom][index][help] */
1851 {
1852   Scsi_Cmnd *SCpnt;
1853   for(SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
1854     scsi_mark_device_reset(SCpnt->device);
1855 }
1856 
1857 
1858 int scsi_reset (Scsi_Cmnd * SCpnt, int bus_reset_flag)
     /* [previous][next][first][last][top][bottom][index][help] */
1859 {
1860     int temp, oldto;
1861     unsigned long flags;
1862     Scsi_Cmnd * SCpnt1;
1863     struct Scsi_Host * host = SCpnt->host;
1864 
1865 #ifdef DEBUG
1866     printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",
1867            host->host_no);
1868 #endif
1869  
1870     /*
1871      * First of all, we need to make a recommendation to the low-level
1872      * driver as to whether a BUS_DEVICE_RESET should be performed,
1873      * or whether we should do a full BUS_RESET.  There is no simple
1874      * algorithm here - we basically use a series of heuristics
1875      * to determine what we should do.
1876      */
1877     SCpnt->host->suggest_bus_reset = FALSE;
1878     
1879     /*
1880      * First see if all of the active devices on the bus have
1881      * been jammed up so that we are attempting resets.  If so,
1882      * then suggest a bus reset.  Forcing a bus reset could
1883      * result in some race conditions, but no more than
1884      * you would usually get with timeouts.  We will cross
1885      * that bridge when we come to it.
1886      */
1887     SCpnt1 = host->host_queue;
1888     while(SCpnt1) {
1889         if( SCpnt1->request.rq_status != RQ_INACTIVE
1890            && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
1891         {
1892             break;
1893         }
1894         SCpnt1 = SCpnt1->next;
1895         }
1896     if( SCpnt1 == NULL ) {
1897         SCpnt->host->suggest_bus_reset = TRUE;
1898     }
1899     
1900     
1901     /*
1902      * If the code that called us is suggesting a hard reset, then
1903      * definitely request it.  This usually occurs because a
1904      * BUS_DEVICE_RESET times out.
1905      */
1906     if( bus_reset_flag ) {
1907         SCpnt->host->suggest_bus_reset = TRUE;
1908     }
1909     
1910     while (1) {
1911         save_flags(flags);
1912         cli();
1913         if (SCpnt->internal_timeout & IN_RESET)
1914         {
1915             restore_flags(flags);
1916             while (SCpnt->internal_timeout & IN_RESET)
1917                 barrier();
1918         }
1919         else
1920         {
1921             SCpnt->internal_timeout |= IN_RESET;
1922             oldto = update_timeout(SCpnt, RESET_TIMEOUT);
1923             
1924             if (host->host_busy)
1925             {
1926                 restore_flags(flags);
1927                 SCpnt1 = host->host_queue;
1928                 while(SCpnt1) {
1929                     if (SCpnt1->request.rq_status != RQ_INACTIVE) {
1930 #if 0
1931                         if (!(SCpnt1->flags & IS_RESETTING) &&
1932                             !(SCpnt1->internal_timeout & IN_ABORT))
1933                             scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
1934 #endif
1935                         SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
1936                     }
1937                     SCpnt1 = SCpnt1->next;
1938                 }
1939                 
1940                 host->last_reset = jiffies;
1941                 temp = host->hostt->reset(SCpnt);
1942                 host->last_reset = jiffies;
1943             }
1944             else
1945             {
1946                 if (!host->block) host->host_busy++;
1947                 restore_flags(flags);
1948                 host->last_reset = jiffies;
1949                 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
1950                 temp = host->hostt->reset(SCpnt);
1951                 host->last_reset = jiffies;
1952                 if (!host->block) host->host_busy--;
1953             }
1954             
1955 #ifdef DEBUG
1956             printk("scsi reset function returned %d\n", temp);
1957 #endif
1958             
1959             /*
1960              * Now figure out what we need to do, based upon
1961              * what the low level driver said that it did.
1962              * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
1963              * or SCSI_RESET_WAKEUP, then the low level driver did a
1964              * bus device reset or bus reset, so we should go through
1965              * and mark one or all of the devices on that bus
1966              * as having been reset.
1967              */
1968             switch(temp & SCSI_RESET_ACTION) {
1969             case SCSI_RESET_SUCCESS:
1970                 if (temp & SCSI_RESET_BUS_RESET)
1971                   scsi_mark_host_bus_reset(host);
1972                 else scsi_mark_device_reset(SCpnt->device);
1973                 save_flags(flags);
1974                 cli();
1975                 SCpnt->internal_timeout &= ~IN_RESET;
1976                 update_timeout(SCpnt, oldto);
1977                 restore_flags(flags);
1978                 return 0;
1979             case SCSI_RESET_PENDING:
1980                 if (temp & SCSI_RESET_BUS_RESET)
1981                   scsi_mark_host_bus_reset(host);
1982                 else scsi_mark_device_reset(SCpnt->device);
1983                 return 0;
1984             case SCSI_RESET_PUNT:
1985                 SCpnt->internal_timeout &= ~IN_RESET;
1986                 scsi_request_sense (SCpnt);
1987                 return 0;
1988             case SCSI_RESET_WAKEUP:
1989                 if (temp & SCSI_RESET_BUS_RESET)
1990                   scsi_mark_host_bus_reset(host);
1991                 else scsi_mark_device_reset(SCpnt->device);
1992                 SCpnt->internal_timeout &= ~IN_RESET;
1993                 scsi_request_sense (SCpnt);
1994                 /*
1995                  * Since a bus reset was performed, we
1996                  * need to wake up each and every command
1997                  * that was active on the bus.
1998                  */
1999                 if( temp & SCSI_RESET_BUS_RESET )
2000                 {
2001                     SCpnt1 = host->host_queue;
2002                     while(SCpnt1) {
2003                         if( SCpnt->request.rq_status != RQ_INACTIVE
2004                            && SCpnt1 != SCpnt)
2005                             scsi_request_sense (SCpnt);
2006                         SCpnt1 = SCpnt1->next;
2007                     }
2008                 }
2009                 return 0;
2010             case SCSI_RESET_SNOOZE:
2011                 /* In this case, we set the timeout field to 0
2012                  * so that this command does not time out any more,
2013                  * and we return 1 so that we get a message on the
2014                  * screen. 
2015                  */
2016                 save_flags(flags);
2017                 cli();
2018                 SCpnt->internal_timeout &= ~IN_RESET;
2019                 update_timeout(SCpnt, 0);
2020                 restore_flags(flags);
2021                 /* If you snooze, you lose... */
2022             case SCSI_RESET_ERROR:
2023             default:
2024                 return 1;
2025             }
2026             
2027             return temp;
2028         }
2029     }
2030 }
2031 
2032 
2033 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2034 {
2035     /*
2036      * We must not enter update_timeout with a timeout condition still pending.
2037      */
2038     
2039     int timed_out, pid;
2040     unsigned long flags;
2041     struct Scsi_Host * host;
2042     Scsi_Cmnd * SCpnt = NULL;
2043     
2044     do {
2045         save_flags(flags);
2046         cli();
2047         
2048         update_timeout(NULL, 0);
2049         /*
2050          * Find all timers such that they have 0 or negative (shouldn't happen)
2051          * time remaining on them.
2052          */
2053         
2054         timed_out = 0;
2055         for(host = scsi_hostlist; host; host = host->next) {
2056             for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2057                 if (SCpnt->timeout == -1)
2058                 {
2059                     SCpnt->timeout = 0;
2060                     pid = SCpnt->pid;
2061                     restore_flags(flags);
2062                     scsi_times_out(SCpnt, pid);
2063                     ++timed_out;
2064                     save_flags(flags);
2065                     cli();
2066                 }
2067         }
2068     } while (timed_out);
2069     restore_flags(flags);
2070 }
2071 
2072 /*
2073  * The strategy is to cause the timer code to call scsi_times_out()
2074  * when the soonest timeout is pending.
2075  * The arguments are used when we are queueing a new command, because
2076  * we do not want to subtract the time used from this time, but when we
2077  * set the timer, we want to take this value into account.
2078  */
2079 
2080 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
2081 {
2082     unsigned int least, used;
2083     unsigned int oldto;
2084     unsigned long flags;
2085     struct Scsi_Host * host;
2086     Scsi_Cmnd * SCpnt = NULL;
2087 
2088     save_flags(flags);
2089     cli();
2090 
2091     /*
2092      * Figure out how much time has passed since the last time the timeouts
2093      * were updated
2094      */
2095     used = (time_start) ? (jiffies - time_start) : 0;
2096 
2097     /*
2098      * Find out what is due to timeout soonest, and adjust all timeouts for
2099      * the amount of time that has passed since the last time we called
2100      * update_timeout.
2101      */
2102 
2103     oldto = 0;
2104     
2105     if(SCset){
2106         oldto = SCset->timeout - used;
2107         SCset->timeout = timeout + used;
2108     }
2109 
2110     least = 0xffffffff;
2111     
2112     for(host = scsi_hostlist; host; host = host->next)
2113         for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2114             if (SCpnt->timeout > 0) {
2115                 SCpnt->timeout -= used;
2116                 if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
2117                 if(SCpnt->timeout > 0 && SCpnt->timeout < least)
2118                     least = SCpnt->timeout;
2119             }
2120     
2121     /*
2122      * If something is due to timeout again, then we will set the next timeout
2123      * interrupt to occur.  Otherwise, timeouts are disabled.
2124      */
2125     
2126     if (least != 0xffffffff)
2127     {
2128         time_start = jiffies;
2129         timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
2130         timer_active |= 1 << SCSI_TIMER;
2131     }
2132     else
2133     {
2134         timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
2135         timer_active &= ~(1 << SCSI_TIMER);
2136     }
2137     restore_flags(flags);
2138     return oldto;
2139 }
2140 
2141 #define MALLOC_PAGEBITS 12
2142 
2143 static int scsi_register_host(Scsi_Host_Template *);
2144 static void scsi_unregister_host(Scsi_Host_Template *);
2145 
2146 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2147 {
2148     unsigned int nbits, mask;
2149     unsigned long flags;
2150     int i, j;
2151     if((len & 0x1ff) || len > (1<<MALLOC_PAGEBITS))
2152         return NULL;
2153     
2154     save_flags(flags);
2155     cli();
2156     nbits = len >> 9;
2157     mask = (1 << nbits) - 1;
2158     
2159     for(i=0;i < (dma_sectors >> (MALLOC_PAGEBITS - 9)); i++)
2160         for(j=0; j<=(sizeof(*dma_malloc_freelist) * 8) - nbits; j++){
2161             if ((dma_malloc_freelist[i] & (mask << j)) == 0){
2162                 dma_malloc_freelist[i] |= (mask << j);
2163                 restore_flags(flags);
2164                 dma_free_sectors -= nbits;
2165 #ifdef DEBUG
2166                 printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
2167 #endif
2168                 return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
2169             }
2170         }
2171     restore_flags(flags);
2172     return NULL;  /* Nope.  No more */
2173 }
2174 
2175 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2176 {
2177     int page, sector, nbits, mask;
2178     long offset;
2179     unsigned long flags;
2180     
2181 #ifdef DEBUG
2182     printk("Sfree %p %d\n",obj, len);
2183 #endif
2184     
2185     offset = -1;
2186     for (page = 0; page < (dma_sectors >> 3); page++)
2187         if ((unsigned long) obj >= (unsigned long) dma_malloc_pages[page] &&
2188             (unsigned long) obj < (unsigned long) dma_malloc_pages[page] 
2189             + (1 << MALLOC_PAGEBITS))
2190         {
2191             offset = ((unsigned long) obj) - ((unsigned long)dma_malloc_pages[page]);
2192             break;
2193         }
2194     
2195     if (page == (dma_sectors >> 3)) panic("Bad offset");
2196     sector = offset >> 9;
2197     if(sector >= dma_sectors) panic ("Bad page");
2198     
2199     sector = (offset >> 9) & (sizeof(*dma_malloc_freelist) * 8 - 1);
2200     nbits = len >> 9;
2201     mask = (1 << nbits) - 1;
2202     
2203     if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
2204     
2205     save_flags(flags);
2206     cli();
2207     if((dma_malloc_freelist[page] & (mask << sector)) != (mask<<sector))
2208         panic("Trying to free unused memory");
2209     
2210     dma_free_sectors += nbits;
2211     dma_malloc_freelist[page] &= ~(mask << sector);
2212     restore_flags(flags);
2213     return 0;
2214 }
2215 
2216 
2217 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
2218 
2219 void * scsi_init_malloc(unsigned int size, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
2220 {
2221     void * retval;
2222     
2223     /*
2224      * For buffers used by the DMA pool, we assume page aligned 
2225      * structures.
2226      */
2227     if ((size % PAGE_SIZE) == 0) {
2228         int order, a_size;
2229         for (order = 0, a_size = PAGE_SIZE;
2230              a_size < size; order++, a_size <<= 1)
2231             ;
2232         retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
2233                                                     order);
2234     } else
2235         retval = kmalloc(size, priority);
2236 
2237     if (retval)
2238         memset(retval, 0, size);
2239     return retval;
2240 }
2241 
2242 
2243 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
2244 { 
2245     /*
2246      * We need this special code here because the DMA pool assumes
2247      * page aligned data.  Besides, it is wasteful to allocate
2248      * page sized chunks with kmalloc.
2249      */
2250     if ((size % PAGE_SIZE) == 0) {
2251         int order, a_size;
2252 
2253         for (order = 0, a_size = PAGE_SIZE;
2254              a_size < size; order++, a_size <<= 1)
2255             ;
2256         free_pages((unsigned long)ptr, order);
2257     } else
2258         kfree(ptr);
2259 }
2260 
2261 void scsi_build_commandblocks(Scsi_Device * SDpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2262 {
2263     int j;
2264     Scsi_Cmnd * SCpnt;
2265     struct Scsi_Host * host = NULL;
2266     
2267     for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2268         SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2269         SCpnt->host = SDpnt->host;
2270         SCpnt->device = SDpnt;
2271         SCpnt->target = SDpnt->id;
2272         SCpnt->lun = SDpnt->lun;
2273         SCpnt->channel = SDpnt->channel;
2274         SCpnt->request.rq_status = RQ_INACTIVE;
2275         SCpnt->use_sg = 0;
2276         SCpnt->old_use_sg = 0;
2277         SCpnt->old_cmd_len = 0;
2278         SCpnt->timeout = 0;
2279         SCpnt->underflow = 0;
2280         SCpnt->transfersize = 0;
2281         SCpnt->host_scribble = NULL;
2282         host = SDpnt->host;
2283         if(host->host_queue)
2284             host->host_queue->prev = SCpnt;
2285         SCpnt->next = host->host_queue;
2286         SCpnt->prev = NULL;
2287         host->host_queue = SCpnt;
2288     }
2289     SDpnt->has_cmdblocks = 1;
2290 }
2291 
2292 /*
2293  * scsi_dev_init() is our initialization routine, which in turn calls host
2294  * initialization, bus scanning, and sd/st initialization routines. 
2295  */
2296 
2297 int scsi_dev_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2298 {
2299     Scsi_Device * SDpnt;
2300     struct Scsi_Host * shpnt;
2301     struct Scsi_Device_Template * sdtpnt;
2302 #ifdef FOO_ON_YOU
2303     return;
2304 #endif
2305 
2306     /* Yes we're here... */
2307     dispatch_scsi_info_ptr = dispatch_scsi_info;
2308 
2309     /* Init a few things so we can "malloc" memory. */
2310     scsi_loadable_module_flag = 0;
2311     
2312     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
2313     timer_table[SCSI_TIMER].expires = 0;
2314 
2315 
2316     /* Register the core /proc/scsi entry */
2317 #if CONFIG_PROC_FS 
2318     proc_scsi_register(0, &proc_scsi_scsi);    
2319 #endif
2320 
2321     /* initialize all hosts */
2322     scsi_init();
2323 
2324     scsi_devices = (Scsi_Device *) NULL;
2325 
2326     for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2327         scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
2328 
2329     printk("scsi : detected ");
2330     for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2331         if (sdtpnt->dev_noticed && sdtpnt->name)
2332             printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2333                    (sdtpnt->dev_noticed != 1) ? "s" : "");
2334     printk("total.\n");
2335     
2336     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2337         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2338 
2339     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2340         SDpnt->scsi_request_fn = NULL;
2341         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2342             if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2343         if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2344     }
2345     
2346 
2347     /*
2348      * This should build the DMA pool.
2349      */
2350     resize_dma_pool();
2351 
2352     /*
2353      * OK, now we finish the initialization by doing spin-up, read
2354      * capacity, etc, etc 
2355      */
2356     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2357         if(sdtpnt->finish && sdtpnt->nr_dev)
2358             (*sdtpnt->finish)();
2359 
2360     scsi_loadable_module_flag = 1;
2361 
2362     return 0;
2363 }
2364 
2365 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
2366 {
2367     int i;
2368     
2369     printk("  Vendor: ");
2370     for (i = 8; i < 16; i++)
2371     {
2372         if (data[i] >= 0x20 && i < data[4] + 5)
2373             printk("%c", data[i]);
2374         else
2375             printk(" ");
2376     }
2377     
2378     printk("  Model: ");
2379     for (i = 16; i < 32; i++)
2380     {
2381         if (data[i] >= 0x20 && i < data[4] + 5)
2382             printk("%c", data[i]);
2383         else
2384             printk(" ");
2385     }
2386     
2387     printk("  Rev: ");
2388     for (i = 32; i < 36; i++)
2389     {
2390         if (data[i] >= 0x20 && i < data[4] + 5)
2391             printk("%c", data[i]);
2392         else
2393             printk(" ");
2394     }
2395     
2396     printk("\n");
2397     
2398     i = data[0] & 0x1f;
2399     
2400     printk("  Type:   %s ",
2401            i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2402     printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2403     if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2404         printk(" CCS\n");
2405     else
2406         printk("\n");
2407 }
2408 
2409 
2410 #ifdef CONFIG_PROC_FS
2411 int scsi_proc_info(char *buffer, char **start, off_t offset, int length, 
     /* [previous][next][first][last][top][bottom][index][help] */
2412                     int hostno, int inout)
2413 {
2414     Scsi_Device *scd;
2415     struct Scsi_Host *HBA_ptr;
2416     int  parameter[4];
2417     char *p;
2418     int   size, len = 0;
2419     off_t begin = 0;
2420     off_t pos = 0;
2421 
2422     scd = scsi_devices;
2423     HBA_ptr = scsi_hostlist;
2424 
2425     if(inout == 0) { 
2426         size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
2427         len += size; 
2428         pos = begin + len;
2429         while (HBA_ptr) {
2430 #if 0
2431             size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no, 
2432                             HBA_ptr->hostt->procname);
2433             len += size; 
2434             pos = begin + len;
2435 #endif
2436             scd = scsi_devices;
2437             while (scd) {
2438                 if (scd->host == HBA_ptr) {
2439                     proc_print_scsidevice(scd, buffer, &size, len);
2440                     len += size; 
2441                     pos = begin + len;
2442                     
2443                     if (pos < offset) {
2444                         len = 0;
2445                         begin = pos;
2446                     }
2447                     if (pos > offset + length)
2448                         goto stop_output;
2449                 }
2450                 scd = scd->next;
2451             }
2452             HBA_ptr = HBA_ptr->next;
2453         }
2454         
2455     stop_output:
2456         *start=buffer+(offset-begin);   /* Start of wanted data */
2457         len-=(offset-begin);            /* Start slop */
2458         if(len>length)
2459             len = length;               /* Ending slop */
2460         return (len);     
2461     }
2462 
2463     if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
2464         return(-EINVAL);
2465 
2466     if(!strncmp("singledevice", buffer + 5, 12)) {
2467         p = buffer + 17;
2468 
2469         parameter[0] = simple_strtoul(p , &p, 0);
2470         parameter[1] = simple_strtoul(p , &p, 0);
2471         parameter[2] = simple_strtoul(p , &p, 0);
2472         parameter[3] = simple_strtoul(p , &p, 0);
2473 
2474         while(scd && scd->host->host_no != parameter[0] 
2475               && scd->channel != parameter[1] 
2476               && scd->id != parameter[2] 
2477               && scd->lun != parameter[3]) {
2478             scd = scd->next;
2479         }
2480         if(scd)
2481             return(-ENOSYS);  /* We do not yet support unplugging */
2482         while(HBA_ptr && HBA_ptr->host_no != parameter[0])
2483             HBA_ptr = HBA_ptr->next;
2484 
2485         if(!HBA_ptr)
2486             return(-ENXIO);
2487 
2488         scan_scsis (HBA_ptr, 1, parameter[1], parameter[2], parameter[3]);
2489         return(0);
2490     }
2491     return(-EINVAL);
2492 }
2493 #endif
2494 
2495 /*
2496  * Go through the device list and recompute the most appropriate size
2497  * for the dma pool.  Then grab more memory (as required).
2498  */
2499 static void resize_dma_pool(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2500 {
2501     int i;
2502     struct Scsi_Host * shpnt;
2503     struct Scsi_Host * host = NULL;
2504     Scsi_Device * SDpnt;
2505     unsigned long flags;
2506     unsigned char * new_dma_malloc_freelist = NULL;
2507     unsigned int new_dma_sectors = 0;
2508     unsigned int new_need_isa_buffer = 0;
2509     unsigned char ** new_dma_malloc_pages = NULL;
2510 
2511     if( !scsi_devices )
2512     {
2513         /*
2514          * Free up the DMA pool.
2515          */
2516         if( dma_free_sectors != dma_sectors )
2517             panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
2518 
2519         for(i=0; i < dma_sectors >> 3; i++)
2520             scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
2521         if (dma_malloc_pages)
2522             scsi_init_free((char *) dma_malloc_pages,
2523                            (dma_sectors>>3)*sizeof(*dma_malloc_pages));
2524         dma_malloc_pages = NULL;
2525         if (dma_malloc_freelist)
2526             scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2527         dma_malloc_freelist = NULL;
2528         dma_sectors = 0;
2529         dma_free_sectors = 0;
2530         return;
2531     }
2532     /* Next, check to see if we need to extend the DMA buffer pool */
2533         
2534     new_dma_sectors = 16;  /* Base value we use */
2535 
2536     if (high_memory-1 > ISA_DMA_THRESHOLD)
2537         scsi_need_isa_bounce_buffers = 1;
2538     else
2539         scsi_need_isa_bounce_buffers = 0;
2540     
2541     if (scsi_devicelist)
2542         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2543             new_dma_sectors += 8;  /* Increment for each host */
2544     
2545     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2546         host = SDpnt->host;
2547         
2548         if(SDpnt->type != TYPE_TAPE)
2549             new_dma_sectors += ((host->sg_tablesize *
2550                                  sizeof(struct scatterlist) + 511) >> 9) *
2551                                      host->cmd_per_lun;
2552         
2553         if(host->unchecked_isa_dma &&
2554            scsi_need_isa_bounce_buffers &&
2555            SDpnt->type != TYPE_TAPE) {
2556             new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2557                 host->cmd_per_lun;
2558             new_need_isa_buffer++;
2559         }
2560     }
2561     
2562     new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2563     
2564     /*
2565      * We never shrink the buffers - this leads to
2566      * race conditions that I would rather not even think
2567      * about right now.
2568      */
2569     if( new_dma_sectors < dma_sectors )
2570         new_dma_sectors = dma_sectors;
2571     
2572     if (new_dma_sectors)
2573     {
2574         new_dma_malloc_freelist = (unsigned char *)
2575             scsi_init_malloc(new_dma_sectors >> 3, GFP_ATOMIC);
2576         memset(new_dma_malloc_freelist, 0, new_dma_sectors >> 3);
2577         
2578         new_dma_malloc_pages = (unsigned char **)
2579             scsi_init_malloc((new_dma_sectors>>3)*sizeof(*new_dma_malloc_pages),
2580                              GFP_ATOMIC);
2581         memset(new_dma_malloc_pages, 0,
2582                (new_dma_sectors>>3)*sizeof(*new_dma_malloc_pages));
2583     }
2584     
2585     /*
2586      * If we need more buffers, expand the list.
2587      */
2588     if( new_dma_sectors > dma_sectors ) { 
2589         for(i=dma_sectors >> 3; i< new_dma_sectors >> 3; i++)
2590             new_dma_malloc_pages[i] = (unsigned char *)
2591                 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2592     }
2593     
2594     /* When we dick with the actual DMA list, we need to 
2595      * protect things 
2596      */
2597     save_flags(flags);
2598     cli();
2599     if (dma_malloc_freelist)
2600     {
2601         memcpy(new_dma_malloc_freelist, dma_malloc_freelist, dma_sectors >> 3);
2602         scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2603     }
2604     dma_malloc_freelist = new_dma_malloc_freelist;
2605     
2606     if (dma_malloc_pages)
2607     {
2608         memcpy(new_dma_malloc_pages, dma_malloc_pages,
2609                (dma_sectors>>3)*sizeof(*dma_malloc_pages));
2610         scsi_init_free((char *) dma_malloc_pages,
2611                        (dma_sectors>>3)*sizeof(*dma_malloc_pages));
2612     }
2613     
2614     dma_free_sectors += new_dma_sectors - dma_sectors;
2615     dma_malloc_pages = new_dma_malloc_pages;
2616     dma_sectors = new_dma_sectors;
2617     need_isa_buffer = new_need_isa_buffer;
2618     restore_flags(flags);
2619 }
2620 
2621 /*
2622  * This entry point should be called by a loadable module if it is trying
2623  * add a low level scsi driver to the system.
2624  */
2625 static int scsi_register_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2626 {
2627     int pcount;
2628     struct Scsi_Host * shpnt;
2629     Scsi_Device * SDpnt;
2630     struct Scsi_Device_Template * sdtpnt;
2631     const char * name;
2632     
2633     if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
2634                                                * no detect routine available 
2635                                                */
2636     pcount = next_scsi_host;
2637     if ((tpnt->present = tpnt->detect(tpnt)))
2638     {
2639         if(pcount == next_scsi_host) {
2640             if(tpnt->present > 1) {
2641                 printk("Failure to register low-level scsi driver");
2642                 scsi_unregister_host(tpnt);
2643                 return 1;
2644             }
2645             /* The low-level driver failed to register a driver.  We
2646              *  can do this now. 
2647              */
2648             scsi_register(tpnt,0);
2649         }
2650         tpnt->next = scsi_hosts; /* Add to the linked list */
2651         scsi_hosts = tpnt;
2652         
2653         /* Add the new driver to /proc/scsi */
2654 #if CONFIG_PROC_FS 
2655         build_proc_dir_entries(tpnt);
2656 #endif
2657         
2658         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2659             if(shpnt->hostt == tpnt)
2660             {
2661                 if(tpnt->info)
2662                     name = tpnt->info(shpnt);
2663                 else
2664                     name = tpnt->name;
2665                 printk ("scsi%d : %s\n", /* And print a little message */
2666                         shpnt->host_no, name);
2667             }
2668         
2669         printk ("scsi : %d host%s.\n", next_scsi_host,
2670                 (next_scsi_host == 1) ? "" : "s");
2671         
2672         scsi_make_blocked_list();
2673         
2674         /* The next step is to call scan_scsis here.  This generates the
2675          * Scsi_Devices entries 
2676          */
2677         
2678         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2679             if(shpnt->hostt == tpnt) scan_scsis(shpnt,0,0,0,0);
2680         
2681         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2682             if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2683         
2684         /* Next we create the Scsi_Cmnd structures for this host */
2685         
2686         for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2687             if(SDpnt->host->hostt == tpnt)
2688             {
2689                 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2690                     if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2691                 if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2692             }
2693         
2694         /*
2695          * Now that we have all of the devices, resize the DMA pool,
2696          * as required.  */
2697         resize_dma_pool();
2698 
2699 
2700         /* This does any final handling that is required. */
2701         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2702             if(sdtpnt->finish && sdtpnt->nr_dev)
2703                 (*sdtpnt->finish)();
2704     }
2705     
2706 #if defined(USE_STATIC_SCSI_MEMORY)
2707     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2708             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2709             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2710             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2711 #endif
2712         
2713     MOD_INC_USE_COUNT;
2714     return 0;
2715 }
2716 
2717 /*
2718  * Similarly, this entry point should be called by a loadable module if it
2719  * is trying to remove a low level scsi driver from the system.
2720  */
2721 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2722 {
2723     Scsi_Host_Template * SHT, *SHTp;
2724     Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
2725     Scsi_Cmnd * SCpnt;
2726     unsigned long flags;
2727     struct Scsi_Device_Template * sdtpnt;
2728     struct Scsi_Host * shpnt, *sh1;
2729     int pcount;
2730     
2731     /* First verify that this host adapter is completely free with no pending
2732      * commands */
2733     
2734     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2735         if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
2736            && *sdpnt->host->hostt->usage_count) return;
2737     
2738     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2739     {
2740         if (shpnt->hostt != tpnt) continue;
2741         for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2742         {
2743             save_flags(flags);
2744             cli();
2745             if(SCpnt->request.rq_status != RQ_INACTIVE) {
2746                 restore_flags(flags);
2747                 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2748                     if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
2749                         SCpnt->request.rq_status = RQ_INACTIVE;
2750                 printk("Device busy???\n");
2751                 return;
2752             }
2753             SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
2754             restore_flags(flags);
2755         }
2756     }
2757     /* Next we detach the high level drivers from the Scsi_Device structures */
2758     
2759     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2760         if(sdpnt->host->hostt == tpnt)
2761         {
2762             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2763                 if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
2764             /* If something still attached, punt */
2765             if (sdpnt->attached) {
2766                 printk("Attached usage count = %d\n", sdpnt->attached);
2767                 return;
2768             }
2769         }
2770     
2771     /* Next we free up the Scsi_Cmnd structures for this host */
2772     
2773     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2774         if(sdpnt->host->hostt == tpnt)
2775             while (sdpnt->host->host_queue) {
2776                 SCpnt = sdpnt->host->host_queue->next;
2777                 scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
2778                 sdpnt->host->host_queue = SCpnt;
2779                 if (SCpnt) SCpnt->prev = NULL;
2780                 sdpnt->has_cmdblocks = 0;
2781             }
2782     
2783     /* Next free up the Scsi_Device structures for this host */
2784     
2785     sdppnt = NULL;
2786     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
2787     {
2788         sdpnt1 = sdpnt->next;
2789         if (sdpnt->host->hostt == tpnt) {
2790             if (sdppnt)
2791                 sdppnt->next = sdpnt->next;
2792             else
2793                 scsi_devices = sdpnt->next;
2794             scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
2795         } else
2796             sdppnt = sdpnt;
2797     }
2798     
2799     /* Next we go through and remove the instances of the individual hosts
2800      * that were detected */
2801     
2802     shpnt = scsi_hostlist;
2803     while(shpnt) {
2804         sh1 = shpnt->next;
2805         if(shpnt->hostt == tpnt) {
2806             if(shpnt->loaded_as_module) {
2807                 pcount = next_scsi_host;
2808                 /* Remove the /proc/scsi directory entry */
2809 #if CONFIG_PROC_FS 
2810                 proc_scsi_unregister(tpnt->proc_dir, 
2811                                      shpnt->host_no + PROC_SCSI_FILE);
2812 #endif   
2813                 if(tpnt->release)
2814                     (*tpnt->release)(shpnt);
2815                 else {
2816                     /* This is the default case for the release function.  
2817                      * It should do the right thing for most correctly 
2818                      * written host adapters. 
2819                      */
2820                     if (shpnt->irq) free_irq(shpnt->irq);
2821                     if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
2822                     if (shpnt->io_port && shpnt->n_io_port)
2823                         release_region(shpnt->io_port, shpnt->n_io_port);
2824                 }
2825                 if(pcount == next_scsi_host) scsi_unregister(shpnt);
2826                 tpnt->present--;
2827             }
2828         }
2829         shpnt = sh1;
2830     }
2831     
2832     /*
2833      * If there are absolutely no more hosts left, it is safe
2834      * to completely nuke the DMA pool.  The resize operation will
2835      * do the right thing and free everything.
2836      */
2837     if( !scsi_devices )
2838         resize_dma_pool();
2839 
2840     printk ("scsi : %d host%s.\n", next_scsi_host,
2841             (next_scsi_host == 1) ? "" : "s");
2842     
2843 #if defined(USE_STATIC_SCSI_MEMORY)
2844     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2845             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2846             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2847             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2848 #endif
2849     
2850     scsi_make_blocked_list();
2851     
2852     /* There were some hosts that were loaded at boot time, so we cannot
2853        do any more than this */
2854     if (tpnt->present) return;
2855     
2856     /* OK, this is the very last step.  Remove this host adapter from the
2857        linked list. */
2858     for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
2859         if(SHT == tpnt) {
2860             if(SHTp)
2861                 SHTp->next = SHT->next;
2862             else
2863                 scsi_hosts = SHT->next;
2864             SHT->next = NULL;
2865             break;
2866         }
2867     
2868     /* Rebuild the /proc/scsi directory entries */
2869 #if CONFIG_PROC_FS 
2870     proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
2871 #endif
2872     MOD_DEC_USE_COUNT;
2873 }
2874 
2875 /*
2876  * This entry point should be called by a loadable module if it is trying
2877  * add a high level scsi driver to the system.
2878  */
2879 static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2880 {
2881     Scsi_Device * SDpnt;
2882     
2883     if (tpnt->next) return 1;
2884     
2885     scsi_register_device(tpnt);
2886     /*
2887      * First scan the devices that we know about, and see if we notice them.
2888      */
2889     
2890     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2891         if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
2892     
2893     /*
2894      * If any of the devices would match this driver, then perform the
2895      * init function.
2896      */
2897     if(tpnt->init && tpnt->dev_noticed)
2898         if ((*tpnt->init)()) return 1;
2899     
2900     /*
2901      * Now actually connect the devices to the new driver.
2902      */
2903     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2904     {
2905         if(tpnt->attach)  (*tpnt->attach)(SDpnt);
2906         /*
2907          * If this driver attached to the device, and we no longer
2908          * have anything attached, release the scso command blocks.
2909          */
2910         if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
2911             scsi_build_commandblocks(SDpnt);
2912     }
2913     
2914     /*
2915      * This does any final handling that is required. 
2916      */
2917     if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
2918     MOD_INC_USE_COUNT;
2919     return 0;
2920 }
2921 
2922 static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2923 {
2924     Scsi_Device * SDpnt;
2925     Scsi_Cmnd * SCpnt;
2926     struct Scsi_Device_Template * spnt;
2927     struct Scsi_Device_Template * prev_spnt;
2928     
2929     /*
2930      * If we are busy, this is not going to fly.
2931      */
2932     if( *tpnt->usage_count != 0) return 0;
2933     /*
2934      * Next, detach the devices from the driver.
2935      */
2936     
2937     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2938     {
2939         if(tpnt->detach) (*tpnt->detach)(SDpnt);
2940         if(SDpnt->attached == 0)
2941         {
2942             /*
2943              * Nobody is using this device any more.  Free all of the
2944              * command structures.
2945              */
2946             for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
2947             {
2948                 if(SCpnt->device == SDpnt)
2949                 {
2950                     if(SCpnt->prev != NULL)
2951                         SCpnt->prev->next = SCpnt->next;
2952                     if(SCpnt->next != NULL)
2953                         SCpnt->next->prev = SCpnt->prev;
2954                     if(SCpnt == SDpnt->host->host_queue)
2955                         SDpnt->host->host_queue = SCpnt->next;
2956                     scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
2957                 }
2958             }
2959             SDpnt->has_cmdblocks = 0;
2960         }
2961     }
2962     /*
2963      * Extract the template from the linked list.
2964      */
2965     spnt = scsi_devicelist;
2966     prev_spnt = NULL;
2967     while(spnt != tpnt)
2968     {
2969         prev_spnt = spnt;
2970         spnt = spnt->next;
2971     }
2972     if(prev_spnt == NULL)
2973         scsi_devicelist = tpnt->next;
2974     else
2975         prev_spnt->next = spnt->next;
2976     
2977     MOD_DEC_USE_COUNT;
2978     /*
2979      * Final cleanup for the driver is done in the driver sources in the 
2980      * cleanup function.
2981      */
2982     return 0;
2983 }
2984 
2985 
2986 int scsi_register_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
2987 {
2988     switch(module_type){
2989     case MODULE_SCSI_HA:
2990         return scsi_register_host((Scsi_Host_Template *) ptr);
2991         
2992         /* Load upper level device handler of some kind */
2993     case MODULE_SCSI_DEV:
2994         return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
2995         /* The rest of these are not yet implemented */
2996         
2997         /* Load constants.o */
2998     case MODULE_SCSI_CONST:
2999         
3000         /* Load specialized ioctl handler for some device.  Intended for 
3001          * cdroms that have non-SCSI2 audio command sets. */
3002     case MODULE_SCSI_IOCTL:
3003         
3004     default:
3005         return 1;
3006     }
3007 }
3008 
3009 void scsi_unregister_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
3010 {
3011     switch(module_type) {
3012     case MODULE_SCSI_HA:
3013         scsi_unregister_host((Scsi_Host_Template *) ptr);
3014         break;
3015     case MODULE_SCSI_DEV:
3016         scsi_unregister_device((struct Scsi_Device_Template *) ptr);
3017         break;
3018         /* The rest of these are not yet implemented. */
3019     case MODULE_SCSI_CONST:
3020     case MODULE_SCSI_IOCTL:
3021         break;
3022     default:
3023     }
3024     return;
3025 }
3026 
3027 #ifdef DEBUG_TIMEOUT
3028 static void
3029 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3030 {
3031     int i;
3032     struct Scsi_Host * shpnt;
3033     Scsi_Cmnd * SCpnt;
3034     printk("Dump of scsi parameters:\n");
3035     i = 0;
3036     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3037         for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3038         {
3039             /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
3040             printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %ld) (%d %d %x) (%d %d %d) %x %x %x\n",
3041                    i++, SCpnt->host->host_no,
3042                    SCpnt->channel,
3043                    SCpnt->target,
3044                    SCpnt->lun,
3045                    kdevname(SCpnt->request.rq_dev),
3046                    SCpnt->request.sector,
3047                    SCpnt->request.nr_sectors,
3048                    SCpnt->request.current_nr_sectors,
3049                    SCpnt->use_sg,
3050                    SCpnt->retries,
3051                    SCpnt->allowed,
3052                    SCpnt->flags,
3053                    SCpnt->timeout_per_command,
3054                    SCpnt->timeout,
3055                    SCpnt->internal_timeout,
3056                    SCpnt->cmnd[0],
3057                    SCpnt->sense_buffer[2],
3058                    SCpnt->result);
3059         }
3060     printk("wait_for_request = %p\n", wait_for_request);
3061     /* Now dump the request lists for each block device */
3062     printk("Dump of pending block device requests\n");
3063     for(i=0; i<MAX_BLKDEV; i++)
3064         if(blk_dev[i].current_request)
3065         {
3066             struct request * req;
3067             printk("%d: ", i);
3068             req = blk_dev[i].current_request;
3069             while(req) {
3070                 printk("(%s %d %ld %ld %ld) ",
3071                        kdevname(req->rq_dev),
3072                        req->cmd,
3073                        req->sector,
3074                        req->nr_sectors,
3075                        req->current_nr_sectors);
3076                 req = req->next;
3077             }
3078             printk("\n");
3079         }
3080 }
3081 #endif
3082 
3083 #ifdef MODULE
3084 
3085 extern struct symbol_table scsi_symbol_table;
3086 
3087 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
3088     /*
3089      * This makes /proc/scsi visible.
3090      */
3091     dispatch_scsi_info_ptr = dispatch_scsi_info;
3092 
3093     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
3094     timer_table[SCSI_TIMER].expires = 0;
3095     register_symtab(&scsi_symbol_table);
3096     scsi_loadable_module_flag = 1;
3097     
3098     dma_sectors = PAGE_SIZE / 512;
3099     dma_free_sectors= dma_sectors;
3100     /*
3101      * Set up a minimal DMA buffer list - this will be used during scan_scsis
3102      * in some cases.
3103      */
3104     
3105     /* One bit per sector to indicate free/busy */
3106     dma_malloc_freelist = (unsigned char *)
3107         scsi_init_malloc(dma_sectors >> 3, GFP_ATOMIC);
3108     memset(dma_malloc_freelist, 0, dma_sectors >> 3);
3109     
3110     /* One pointer per page for the page list */
3111     dma_malloc_pages = (unsigned char **)
3112         scsi_init_malloc((dma_sectors >> 3)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
3113     dma_malloc_pages[0] = (unsigned char *)
3114         scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
3115     return 0;
3116 }
3117 
3118 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
3119 {
3120     if (MOD_IN_USE) {
3121         printk(KERN_INFO __FILE__ ": module is in use, remove rejected\n");
3122         return;
3123     }
3124     
3125     /* No, we're not here anymore. Don't show the /proc/scsi files. */
3126     dispatch_scsi_info_ptr = 0L;
3127 
3128     /*
3129      * Free up the DMA pool.
3130      */
3131     resize_dma_pool();
3132 
3133     timer_table[SCSI_TIMER].fn = NULL;
3134     timer_table[SCSI_TIMER].expires = 0;
3135 }
3136 #endif /* MODULE */
3137 
3138 /*
3139  * Overrides for Emacs so that we follow Linus's tabbing style.
3140  * Emacs will notice this stuff at the end of the file and automatically
3141  * adjust the settings for this buffer only.  This must remain at the end
3142  * of the file.
3143  * ---------------------------------------------------------------------------
3144  * Local variables:
3145  * c-indent-level: 4
3146  * c-brace-imaginary-offset: 0
3147  * c-brace-offset: -4
3148  * c-argdecl-indent: 4
3149  * c-label-offset: -4
3150  * c-continued-statement-offset: 4
3151  * c-continued-brace-offset: 0
3152  * indent-tabs-mode: nil
3153  * tab-width: 8
3154  * End:
3155  */

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