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

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