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. reset
  13. check_sense
  14. scsi_done
  15. scsi_abort
  16. scsi_reset
  17. scsi_main_timeout
  18. update_timeout
  19. scsi_malloc
  20. scsi_free
  21. scsi_init_malloc
  22. scsi_init_free
  23. scsi_build_commandblocks
  24. scsi_dev_init
  25. print_inquiry
  26. scsi_proc_info
  27. scsi_register_host
  28. scsi_unregister_host
  29. scsi_register_device_module
  30. scsi_unregister_device
  31. scsi_register_module
  32. scsi_unregister_module
  33. scsi_dump_status
  34. init_module
  35. cleanup_module

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

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