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

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