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

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