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

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