root/drivers/scsi/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_device_flags
  2. scsi_make_blocked_list
  3. scan_scsis_done
  4. scsi_luns_setup
  5. scan_scsis
  6. scsi_times_out
  7. request_queueable
  8. allocate_device
  9. internal_cmnd
  10. scsi_request_sense
  11. scsi_do_cmd
  12. check_sense
  13. scsi_done
  14. scsi_abort
  15. scsi_mark_device_reset
  16. scsi_mark_host_bus_reset
  17. scsi_reset
  18. scsi_main_timeout
  19. update_timeout
  20. scsi_malloc
  21. scsi_free
  22. scsi_init_malloc
  23. scsi_init_free
  24. scsi_build_commandblocks
  25. scsi_dev_init
  26. print_inquiry
  27. scsi_proc_info
  28. resize_dma_pool
  29. scsi_register_host
  30. scsi_unregister_host
  31. scsi_register_device_module
  32. scsi_unregister_device
  33. scsi_register_module
  34. scsi_unregister_module
  35. scsi_dump_status
  36. init_module
  37. cleanup_module

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

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