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

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