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

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