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

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