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             /*
1568               Allow TEST_UNIT_READY and INQUIRY commands to timeout early
1569               without causing resets.  All other commands should be retried.
1570             */
1571             if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
1572                 SCpnt->cmnd[0] != INQUIRY)
1573                     status = MAYREDO;
1574             exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1575         }
1576         else
1577         {
1578 #ifdef DEBUG
1579             printk ("Retrying.\n");
1580 #endif
1581             SCpnt->flags  |= WAS_TIMEDOUT;
1582             SCpnt->internal_timeout &= ~IN_ABORT;
1583             status = REDO;
1584         }
1585         break;
1586     case DID_BUS_BUSY:
1587     case DID_PARITY:
1588         status = REDO;
1589         break;
1590     case DID_NO_CONNECT:
1591 #ifdef DEBUG
1592         printk("Couldn't connect.\n");
1593 #endif
1594         exit  = (DRIVER_HARD | SUGGEST_ABORT);
1595         break;
1596     case DID_ERROR:
1597         status = MAYREDO;
1598         exit = (DRIVER_HARD | SUGGEST_ABORT);
1599         break;
1600     case DID_BAD_TARGET:
1601     case DID_ABORT:
1602         exit = (DRIVER_INVALID | SUGGEST_ABORT);
1603         break;
1604     case DID_RESET:
1605         if (SCpnt->flags & IS_RESETTING)
1606         {
1607             SCpnt->flags &= ~IS_RESETTING;
1608             status = REDO;
1609             break;
1610         }
1611         
1612         if(msg_byte(result) == GOOD &&
1613            status_byte(result) == CHECK_CONDITION) {
1614             switch (check_sense(SCpnt)) {
1615             case 0:
1616                 update_timeout(SCpnt, oldto);
1617                 status = REDO;
1618                 break;
1619             case SUGGEST_REMAP:
1620             case SUGGEST_RETRY:
1621                 status = MAYREDO;
1622                 exit = DRIVER_SENSE | SUGGEST_RETRY;
1623                 break;
1624             case SUGGEST_ABORT:
1625                 status = FINISHED;
1626                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
1627                 break;
1628             case SUGGEST_SENSE:
1629                 scsi_request_sense (SCpnt);
1630                 status = PENDING;
1631                 break;
1632             }
1633         } else {
1634             status=REDO;
1635             exit = SUGGEST_RETRY;
1636         }
1637         break;
1638     default :
1639         exit = (DRIVER_ERROR | SUGGEST_DIE);
1640     }
1641     
1642     switch (status)
1643     {
1644     case FINISHED:
1645     case PENDING:
1646         break;
1647     case MAYREDO:
1648 #ifdef DEBUG
1649         printk("In MAYREDO, allowing %d retries, have %d\n",
1650                SCpnt->allowed, SCpnt->retries);
1651 #endif
1652         if ((++SCpnt->retries) < SCpnt->allowed)
1653         {
1654             if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1655                 && !(jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
1656                 && !(SCpnt->flags & WAS_RESET))
1657             {
1658                 printk("scsi%d channel %d : resetting for second half of retries.\n",
1659                        SCpnt->host->host_no, SCpnt->channel);
1660                 scsi_reset(SCpnt, FALSE);
1661                 break;
1662             }
1663             
1664         }
1665         else
1666         {
1667             status = FINISHED;
1668             break;
1669         }
1670         /* fall through to REDO */
1671         
1672     case REDO:
1673         
1674         if (SCpnt->flags & WAS_SENSE)
1675             scsi_request_sense(SCpnt);
1676         else
1677         {
1678             memcpy ((void *) SCpnt->cmnd,
1679                     (void*) SCpnt->data_cmnd,
1680                     sizeof(SCpnt->data_cmnd));
1681             SCpnt->request_buffer = SCpnt->buffer;
1682             SCpnt->request_bufflen = SCpnt->bufflen;
1683             SCpnt->use_sg = SCpnt->old_use_sg;
1684             SCpnt->cmd_len = SCpnt->old_cmd_len;
1685             internal_cmnd (SCpnt);
1686         }
1687         break;
1688     default:
1689         INTERNAL_ERROR;
1690     }
1691     
1692     if (status == FINISHED) {
1693 #ifdef DEBUG
1694         printk("Calling done function - at address %p\n", SCpnt->done);
1695 #endif
1696         host->host_busy--; /* Indicate that we are free */
1697         
1698         if (host->block && host->host_busy == 0) {
1699             host_active = NULL;
1700             
1701             /* For block devices "wake_up" is done in end_scsi_request */
1702             if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
1703                 MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
1704                 struct Scsi_Host * next;
1705                 
1706                 for (next = host->block; next != host; next = next->block)
1707                     wake_up(&next->host_wait);
1708             }
1709             
1710         }
1711         
1712         wake_up(&host->host_wait);
1713         SCpnt->result = result | ((exit & 0xff) << 24);
1714         SCpnt->use_sg = SCpnt->old_use_sg;
1715         SCpnt->cmd_len = SCpnt->old_cmd_len;
1716         SCpnt->done (SCpnt);
1717     }
1718     
1719 #undef FINISHED
1720 #undef REDO
1721 #undef MAYREDO
1722 #undef PENDING
1723 }
1724 
1725 /*
1726  * The scsi_abort function interfaces with the abort() function of the host
1727  * we are aborting, and causes the current command to not complete.  The
1728  * caller should deal with any error messages or status returned on the
1729  * next call.
1730  * 
1731  * This will not be called reentrantly for a given host.
1732  */
1733 
1734 /*
1735  * Since we're nice guys and specified that abort() and reset()
1736  * can be non-reentrant.  The internal_timeout flags are used for
1737  * this.
1738  */
1739 
1740 
1741 int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
     /* [previous][next][first][last][top][bottom][index][help] */
1742 {
1743     int oldto;
1744     unsigned long flags;
1745     struct Scsi_Host * host = SCpnt->host;
1746     
1747     while(1)
1748     {
1749         save_flags(flags);
1750         cli();
1751         
1752         /*
1753          * Protect against races here.  If the command is done, or we are
1754          * on a different command forget it.
1755          */
1756         if (SCpnt->request.rq_status == RQ_INACTIVE || pid != SCpnt->pid) {
1757             restore_flags(flags);
1758             return 0;
1759         }
1760 
1761         if (SCpnt->internal_timeout & IN_ABORT)
1762         {
1763             restore_flags(flags);
1764             while (SCpnt->internal_timeout & IN_ABORT)
1765                 barrier();
1766         }
1767         else
1768         {
1769             SCpnt->internal_timeout |= IN_ABORT;
1770             oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1771             
1772             if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
1773                 /* OK, this command must have died when we did the
1774                  *  reset.  The device itself must have lied. 
1775                  */
1776                 printk("Stale command on %d %d:%d appears to have died when"
1777                        " the bus was reset\n", 
1778                        SCpnt->channel, SCpnt->target, SCpnt->lun);
1779             }
1780             
1781             restore_flags(flags);
1782             if (!host->host_busy) {
1783                 SCpnt->internal_timeout &= ~IN_ABORT;
1784                 update_timeout(SCpnt, oldto);
1785                 return 0;
1786             }
1787             printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
1788                    " channel %d, id %d, lun %d ",
1789                    SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel, 
1790                    (int) SCpnt->target, (int) SCpnt->lun);
1791             print_command (SCpnt->cmnd);
1792             if (SCpnt->request.rq_status == RQ_INACTIVE || pid != SCpnt->pid)
1793                 return 0;
1794             SCpnt->abort_reason = why;
1795             switch(host->hostt->abort(SCpnt)) {
1796                 /* We do not know how to abort.  Try waiting another
1797                  * time increment and see if this helps. Set the
1798                  * WAS_TIMEDOUT flag set so we do not try this twice
1799                  */
1800             case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1801                                    * this is too severe 
1802                                    */
1803             case SCSI_ABORT_SNOOZE:
1804                 if(why == DID_TIME_OUT) {
1805                     save_flags(flags);
1806                     cli();
1807                     SCpnt->internal_timeout &= ~IN_ABORT;
1808                     if(SCpnt->flags & WAS_TIMEDOUT) {
1809                         restore_flags(flags);
1810                         return 1; /* Indicate we cannot handle this.
1811                                    * We drop down into the reset handler
1812                                    * and try again 
1813                                    */
1814                     } else {
1815                         SCpnt->flags |= WAS_TIMEDOUT;
1816                         oldto = SCpnt->timeout_per_command;
1817                         update_timeout(SCpnt, oldto);
1818                     }
1819                     restore_flags(flags);
1820                 }
1821                 return 0;
1822             case SCSI_ABORT_PENDING:
1823                 if(why != DID_TIME_OUT) {
1824                     save_flags(flags);
1825                     cli();
1826                     update_timeout(SCpnt, oldto);
1827                     restore_flags(flags);
1828                 }
1829                 return 0;
1830             case SCSI_ABORT_SUCCESS:
1831                 /* We should have already aborted this one.  No
1832                  * need to adjust timeout 
1833                  */
1834                  SCpnt->internal_timeout &= ~IN_ABORT;
1835                  return 0;
1836             case SCSI_ABORT_NOT_RUNNING:
1837                 SCpnt->internal_timeout &= ~IN_ABORT;
1838                 update_timeout(SCpnt, 0);
1839                 return 0;
1840             case SCSI_ABORT_ERROR:
1841             default:
1842                 SCpnt->internal_timeout &= ~IN_ABORT;
1843                 return 1;
1844             }
1845         }
1846     }
1847 }
1848 
1849 
1850 /* Mark a single SCSI Device as having been reset. */
1851 
1852 static inline void scsi_mark_device_reset(Scsi_Device *Device)
     /* [previous][next][first][last][top][bottom][index][help] */
1853 {
1854   Device->was_reset = 1;
1855   Device->expecting_cc_ua = 1;
1856 }
1857 
1858 
1859 /* Mark all SCSI Devices on a specific Host as having been reset. */
1860 
1861 void scsi_mark_host_bus_reset(struct Scsi_Host *Host)
     /* [previous][next][first][last][top][bottom][index][help] */
1862 {
1863   Scsi_Cmnd *SCpnt;
1864   for(SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
1865     scsi_mark_device_reset(SCpnt->device);
1866 }
1867 
1868 
1869 int scsi_reset (Scsi_Cmnd * SCpnt, int bus_reset_flag)
     /* [previous][next][first][last][top][bottom][index][help] */
1870 {
1871     int temp, oldto;
1872     unsigned long flags;
1873     Scsi_Cmnd * SCpnt1;
1874     struct Scsi_Host * host = SCpnt->host;
1875 
1876     printk("SCSI bus is being reset for host %d.\n",
1877            host->host_no);
1878  
1879     /*
1880      * First of all, we need to make a recommendation to the low-level
1881      * driver as to whether a BUS_DEVICE_RESET should be performed,
1882      * or whether we should do a full BUS_RESET.  There is no simple
1883      * algorithm here - we basically use a series of heuristics
1884      * to determine what we should do.
1885      */
1886     SCpnt->host->suggest_bus_reset = FALSE;
1887     
1888     /*
1889      * First see if all of the active devices on the bus have
1890      * been jammed up so that we are attempting resets.  If so,
1891      * then suggest a bus reset.  Forcing a bus reset could
1892      * result in some race conditions, but no more than
1893      * you would usually get with timeouts.  We will cross
1894      * that bridge when we come to it.
1895      */
1896     SCpnt1 = host->host_queue;
1897     while(SCpnt1) {
1898         if( SCpnt1->request.rq_status != RQ_INACTIVE
1899             && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
1900                 break;
1901         SCpnt1 = SCpnt1->next;
1902         }
1903     if( SCpnt1 == NULL ) {
1904         SCpnt->host->suggest_bus_reset = TRUE;
1905     }
1906     
1907     
1908     /*
1909      * If the code that called us is suggesting a hard reset, then
1910      * definitely request it.  This usually occurs because a
1911      * BUS_DEVICE_RESET times out.
1912      */
1913     if( bus_reset_flag ) {
1914         SCpnt->host->suggest_bus_reset = TRUE;
1915     }
1916     
1917     while (1) {
1918         save_flags(flags);
1919         cli();
1920         if (SCpnt->internal_timeout & IN_RESET)
1921         {
1922             restore_flags(flags);
1923             while (SCpnt->internal_timeout & IN_RESET)
1924                 barrier();
1925         }
1926         else
1927         {
1928             SCpnt->internal_timeout |= IN_RESET;
1929             oldto = update_timeout(SCpnt, RESET_TIMEOUT);
1930             
1931             if (host->host_busy)
1932             {
1933                 restore_flags(flags);
1934                 SCpnt1 = host->host_queue;
1935                 while(SCpnt1) {
1936                     if (SCpnt1->request.rq_status != RQ_INACTIVE) {
1937 #if 0
1938                         if (!(SCpnt1->flags & IS_RESETTING) &&
1939                             !(SCpnt1->internal_timeout & IN_ABORT))
1940                             scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
1941 #endif
1942                         SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
1943                     }
1944                     SCpnt1 = SCpnt1->next;
1945                 }
1946                 
1947                 host->last_reset = jiffies;
1948                 temp = host->hostt->reset(SCpnt);
1949                 host->last_reset = jiffies;
1950             }
1951             else
1952             {
1953                 if (!host->block) host->host_busy++;
1954                 restore_flags(flags);
1955                 host->last_reset = jiffies;
1956                 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
1957                 temp = host->hostt->reset(SCpnt);
1958                 host->last_reset = jiffies;
1959                 if (!host->block) host->host_busy--;
1960             }
1961             
1962 #ifdef DEBUG
1963             printk("scsi reset function returned %d\n", temp);
1964 #endif
1965             
1966             /*
1967              * Now figure out what we need to do, based upon
1968              * what the low level driver said that it did.
1969              * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
1970              * or SCSI_RESET_WAKEUP, then the low level driver did a
1971              * bus device reset or bus reset, so we should go through
1972              * and mark one or all of the devices on that bus
1973              * as having been reset.
1974              */
1975             switch(temp & SCSI_RESET_ACTION) {
1976             case SCSI_RESET_SUCCESS:
1977                 if (temp & SCSI_RESET_BUS_RESET)
1978                   scsi_mark_host_bus_reset(host);
1979                 else scsi_mark_device_reset(SCpnt->device);
1980                 save_flags(flags);
1981                 cli();
1982                 SCpnt->internal_timeout &= ~IN_RESET;
1983                 update_timeout(SCpnt, oldto);
1984                 restore_flags(flags);
1985                 return 0;
1986             case SCSI_RESET_PENDING:
1987                 if (temp & SCSI_RESET_BUS_RESET)
1988                   scsi_mark_host_bus_reset(host);
1989                 else scsi_mark_device_reset(SCpnt->device);
1990                 return 0;
1991             case SCSI_RESET_PUNT:
1992                 SCpnt->internal_timeout &= ~IN_RESET;
1993                 scsi_request_sense (SCpnt);
1994                 return 0;
1995             case SCSI_RESET_WAKEUP:
1996                 if (temp & SCSI_RESET_BUS_RESET)
1997                   scsi_mark_host_bus_reset(host);
1998                 else scsi_mark_device_reset(SCpnt->device);
1999                 SCpnt->internal_timeout &= ~IN_RESET;
2000                 scsi_request_sense (SCpnt);
2001                 /*
2002                  * Since a bus reset was performed, we
2003                  * need to wake up each and every command
2004                  * that was active on the bus.
2005                  */
2006                 if( temp & SCSI_RESET_BUS_RESET )
2007                 {
2008                     SCpnt1 = host->host_queue;
2009                     while(SCpnt1) {
2010                         if( SCpnt->request.rq_status != RQ_INACTIVE
2011                            && SCpnt1 != SCpnt)
2012                             scsi_request_sense (SCpnt);
2013                         SCpnt1 = SCpnt1->next;
2014                     }
2015                 }
2016                 return 0;
2017             case SCSI_RESET_SNOOZE:
2018                 /* In this case, we set the timeout field to 0
2019                  * so that this command does not time out any more,
2020                  * and we return 1 so that we get a message on the
2021                  * screen. 
2022                  */
2023                 save_flags(flags);
2024                 cli();
2025                 SCpnt->internal_timeout &= ~IN_RESET;
2026                 update_timeout(SCpnt, 0);
2027                 restore_flags(flags);
2028                 /* If you snooze, you lose... */
2029             case SCSI_RESET_ERROR:
2030             default:
2031                 return 1;
2032             }
2033             
2034             return temp;
2035         }
2036     }
2037 }
2038 
2039 
2040 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2041 {
2042     /*
2043      * We must not enter update_timeout with a timeout condition still pending.
2044      */
2045     
2046     int timed_out, pid;
2047     unsigned long flags;
2048     struct Scsi_Host * host;
2049     Scsi_Cmnd * SCpnt = NULL;
2050     
2051     do {
2052         save_flags(flags);
2053         cli();
2054         
2055         update_timeout(NULL, 0);
2056         /*
2057          * Find all timers such that they have 0 or negative (shouldn't happen)
2058          * time remaining on them.
2059          */
2060         
2061         timed_out = 0;
2062         for(host = scsi_hostlist; host; host = host->next) {
2063             for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2064                 if (SCpnt->timeout == -1)
2065                 {
2066                     SCpnt->timeout = 0;
2067                     pid = SCpnt->pid;
2068                     restore_flags(flags);
2069                     scsi_times_out(SCpnt, pid);
2070                     ++timed_out;
2071                     save_flags(flags);
2072                     cli();
2073                 }
2074         }
2075     } while (timed_out);
2076     restore_flags(flags);
2077 }
2078 
2079 /*
2080  * The strategy is to cause the timer code to call scsi_times_out()
2081  * when the soonest timeout is pending.
2082  * The arguments are used when we are queueing a new command, because
2083  * we do not want to subtract the time used from this time, but when we
2084  * set the timer, we want to take this value into account.
2085  */
2086 
2087 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
2088 {
2089     unsigned int least, used;
2090     unsigned int oldto;
2091     unsigned long flags;
2092     struct Scsi_Host * host;
2093     Scsi_Cmnd * SCpnt = NULL;
2094 
2095     save_flags(flags);
2096     cli();
2097 
2098     /*
2099      * Figure out how much time has passed since the last time the timeouts
2100      * were updated
2101      */
2102     used = (time_start) ? (jiffies - time_start) : 0;
2103 
2104     /*
2105      * Find out what is due to timeout soonest, and adjust all timeouts for
2106      * the amount of time that has passed since the last time we called
2107      * update_timeout.
2108      */
2109 
2110     oldto = 0;
2111     
2112     if(SCset){
2113         oldto = SCset->timeout - used;
2114         SCset->timeout = timeout + used;
2115     }
2116 
2117     least = 0xffffffff;
2118     
2119     for(host = scsi_hostlist; host; host = host->next)
2120         for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2121             if (SCpnt->timeout > 0) {
2122                 SCpnt->timeout -= used;
2123                 if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
2124                 if(SCpnt->timeout > 0 && SCpnt->timeout < least)
2125                     least = SCpnt->timeout;
2126             }
2127     
2128     /*
2129      * If something is due to timeout again, then we will set the next timeout
2130      * interrupt to occur.  Otherwise, timeouts are disabled.
2131      */
2132     
2133     if (least != 0xffffffff)
2134     {
2135         time_start = jiffies;
2136         timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
2137         timer_active |= 1 << SCSI_TIMER;
2138     }
2139     else
2140     {
2141         timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
2142         timer_active &= ~(1 << SCSI_TIMER);
2143     }
2144     restore_flags(flags);
2145     return oldto;
2146 }
2147 
2148 #define MALLOC_PAGEBITS 12
2149 
2150 static int scsi_register_host(Scsi_Host_Template *);
2151 static void scsi_unregister_host(Scsi_Host_Template *);
2152 
2153 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2154 {
2155     unsigned int nbits, mask;
2156     unsigned long flags;
2157     int i, j;
2158     if((len & 0x1ff) || len > (1<<MALLOC_PAGEBITS))
2159         return NULL;
2160     
2161     save_flags(flags);
2162     cli();
2163     nbits = len >> 9;
2164     mask = (1 << nbits) - 1;
2165     
2166     for(i=0;i < (dma_sectors >> (MALLOC_PAGEBITS - 9)); i++)
2167         for(j=0; j<=(sizeof(*dma_malloc_freelist) * 8) - nbits; j++){
2168             if ((dma_malloc_freelist[i] & (mask << j)) == 0){
2169                 dma_malloc_freelist[i] |= (mask << j);
2170                 restore_flags(flags);
2171                 dma_free_sectors -= nbits;
2172 #ifdef DEBUG
2173                 printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
2174 #endif
2175                 return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
2176             }
2177         }
2178     restore_flags(flags);
2179     return NULL;  /* Nope.  No more */
2180 }
2181 
2182 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2183 {
2184     int page, sector, nbits, mask;
2185     long offset;
2186     unsigned long flags;
2187     
2188 #ifdef DEBUG
2189     printk("scsi_free %p %d\n",obj, len);
2190 #endif
2191     
2192     offset = -1;
2193     for (page = 0; page < (dma_sectors >> 3); page++)
2194         if ((unsigned long) obj >= (unsigned long) dma_malloc_pages[page] &&
2195             (unsigned long) obj < (unsigned long) dma_malloc_pages[page] 
2196             + (1 << MALLOC_PAGEBITS))
2197         {
2198             offset = ((unsigned long) obj) - ((unsigned long)dma_malloc_pages[page]);
2199             break;
2200         }
2201     
2202     if (page == (dma_sectors >> 3)) panic("scsi_free:Bad offset");
2203     sector = offset >> 9;
2204     if(sector >= dma_sectors) panic ("scsi_free:Bad page");
2205     
2206     sector = (offset >> 9) & (sizeof(*dma_malloc_freelist) * 8 - 1);
2207     nbits = len >> 9;
2208     mask = (1 << nbits) - 1;
2209     
2210     if ((mask << sector) > 0xffff) panic ("scsi_free:Bad memory alignment");
2211     
2212     save_flags(flags);
2213     cli();
2214     if((dma_malloc_freelist[page] & (mask << sector)) != (mask<<sector))
2215         panic("scsi_free:Trying to free unused memory");
2216     
2217     dma_free_sectors += nbits;
2218     dma_malloc_freelist[page] &= ~(mask << sector);
2219     restore_flags(flags);
2220     return 0;
2221 }
2222 
2223 
2224 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
2225 
2226 void * scsi_init_malloc(unsigned int size, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
2227 {
2228     void * retval;
2229     
2230     /*
2231      * For buffers used by the DMA pool, we assume page aligned 
2232      * structures.
2233      */
2234     if ((size % PAGE_SIZE) == 0) {
2235         int order, a_size;
2236         for (order = 0, a_size = PAGE_SIZE;
2237              a_size < size; order++, a_size <<= 1)
2238             ;
2239         retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
2240                                                     order);
2241     } else
2242         retval = kmalloc(size, priority);
2243 
2244     if (retval)
2245         memset(retval, 0, size);
2246     return retval;
2247 }
2248 
2249 
2250 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
2251 { 
2252     /*
2253      * We need this special code here because the DMA pool assumes
2254      * page aligned data.  Besides, it is wasteful to allocate
2255      * page sized chunks with kmalloc.
2256      */
2257     if ((size % PAGE_SIZE) == 0) {
2258         int order, a_size;
2259 
2260         for (order = 0, a_size = PAGE_SIZE;
2261              a_size < size; order++, a_size <<= 1)
2262             ;
2263         free_pages((unsigned long)ptr, order);
2264     } else
2265         kfree(ptr);
2266 }
2267 
2268 void scsi_build_commandblocks(Scsi_Device * SDpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2269 {
2270     int j;
2271     Scsi_Cmnd * SCpnt;
2272     struct Scsi_Host * host = NULL;
2273     
2274     for(j=0;j<SDpnt->host->cmd_per_lun;j++){
2275         SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
2276         SCpnt->host = SDpnt->host;
2277         SCpnt->device = SDpnt;
2278         SCpnt->target = SDpnt->id;
2279         SCpnt->lun = SDpnt->lun;
2280         SCpnt->channel = SDpnt->channel;
2281         SCpnt->request.rq_status = RQ_INACTIVE;
2282         SCpnt->use_sg = 0;
2283         SCpnt->old_use_sg = 0;
2284         SCpnt->old_cmd_len = 0;
2285         SCpnt->timeout = 0;
2286         SCpnt->underflow = 0;
2287         SCpnt->transfersize = 0;
2288         SCpnt->host_scribble = NULL;
2289         host = SDpnt->host;
2290         if(host->host_queue)
2291             host->host_queue->prev = SCpnt;
2292         SCpnt->next = host->host_queue;
2293         SCpnt->prev = NULL;
2294         host->host_queue = SCpnt;
2295     }
2296     SDpnt->has_cmdblocks = 1;
2297 }
2298 
2299 /*
2300  * scsi_dev_init() is our initialization routine, which in turn calls host
2301  * initialization, bus scanning, and sd/st initialization routines. 
2302  */
2303 
2304 int scsi_dev_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2305 {
2306     Scsi_Device * SDpnt;
2307     struct Scsi_Host * shpnt;
2308     struct Scsi_Device_Template * sdtpnt;
2309 #ifdef FOO_ON_YOU
2310     return;
2311 #endif
2312 
2313     /* Yes we're here... */
2314     dispatch_scsi_info_ptr = dispatch_scsi_info;
2315 
2316     /* Init a few things so we can "malloc" memory. */
2317     scsi_loadable_module_flag = 0;
2318     
2319     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
2320     timer_table[SCSI_TIMER].expires = 0;
2321 
2322 
2323     /* Register the /proc/scsi/scsi entry */
2324 #if CONFIG_PROC_FS 
2325     proc_scsi_register(0, &proc_scsi_scsi);    
2326 #endif
2327 
2328     /* initialize all hosts */
2329     scsi_init();
2330 
2331     scsi_devices = (Scsi_Device *) NULL;
2332 
2333     for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2334         scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
2335 
2336     printk("scsi : detected ");
2337     for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2338         if (sdtpnt->dev_noticed && sdtpnt->name)
2339             printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2340                    (sdtpnt->dev_noticed != 1) ? "s" : "");
2341     printk("total.\n");
2342     
2343     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2344         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2345 
2346     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2347         SDpnt->scsi_request_fn = NULL;
2348         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2349             if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2350         if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2351     }
2352     
2353 
2354     /*
2355      * This should build the DMA pool.
2356      */
2357     resize_dma_pool();
2358 
2359     /*
2360      * OK, now we finish the initialization by doing spin-up, read
2361      * capacity, etc, etc 
2362      */
2363     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2364         if(sdtpnt->finish && sdtpnt->nr_dev)
2365             (*sdtpnt->finish)();
2366 
2367     scsi_loadable_module_flag = 1;
2368 
2369     return 0;
2370 }
2371 
2372 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
2373 {
2374     int i;
2375     
2376     printk("  Vendor: ");
2377     for (i = 8; i < 16; i++)
2378     {
2379         if (data[i] >= 0x20 && i < data[4] + 5)
2380             printk("%c", data[i]);
2381         else
2382             printk(" ");
2383     }
2384     
2385     printk("  Model: ");
2386     for (i = 16; i < 32; i++)
2387     {
2388         if (data[i] >= 0x20 && i < data[4] + 5)
2389             printk("%c", data[i]);
2390         else
2391             printk(" ");
2392     }
2393     
2394     printk("  Rev: ");
2395     for (i = 32; i < 36; i++)
2396     {
2397         if (data[i] >= 0x20 && i < data[4] + 5)
2398             printk("%c", data[i]);
2399         else
2400             printk(" ");
2401     }
2402     
2403     printk("\n");
2404     
2405     i = data[0] & 0x1f;
2406     
2407     printk("  Type:   %s ",
2408            i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2409     printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2410     if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2411         printk(" CCS\n");
2412     else
2413         printk("\n");
2414 }
2415 
2416 
2417 #ifdef CONFIG_PROC_FS
2418 int scsi_proc_info(char *buffer, char **start, off_t offset, int length, 
     /* [previous][next][first][last][top][bottom][index][help] */
2419                     int hostno, int inout)
2420 {
2421     Scsi_Device *scd;
2422     struct Scsi_Host *HBA_ptr;
2423     int  parameter[4];
2424     char *p;
2425     int   i,size, len = 0;
2426     off_t begin = 0;
2427     off_t pos = 0;
2428 
2429     scd = scsi_devices;
2430     HBA_ptr = scsi_hostlist;
2431 
2432     if(inout == 0) { 
2433         size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
2434         len += size; 
2435         pos = begin + len;
2436         while (HBA_ptr) {
2437 #if 0
2438             size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no, 
2439                             HBA_ptr->hostt->procname);
2440             len += size; 
2441             pos = begin + len;
2442 #endif
2443             scd = scsi_devices;
2444             while (scd) {
2445                 if (scd->host == HBA_ptr) {
2446                     proc_print_scsidevice(scd, buffer, &size, len);
2447                     len += size; 
2448                     pos = begin + len;
2449                     
2450                     if (pos < offset) {
2451                         len = 0;
2452                         begin = pos;
2453                     }
2454                     if (pos > offset + length)
2455                         goto stop_output;
2456                 }
2457                 scd = scd->next;
2458             }
2459             HBA_ptr = HBA_ptr->next;
2460         }
2461         
2462     stop_output:
2463         *start=buffer+(offset-begin);   /* Start of wanted data */
2464         len-=(offset-begin);            /* Start slop */
2465         if(len>length)
2466             len = length;               /* Ending slop */
2467         return (len);     
2468     }
2469 
2470     /*
2471      * Usage: echo "scsi singledevice 0 1 2 3" >/proc/scsi/scsi
2472      * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
2473      * Consider this feature BETA.
2474      *     CAUTION: This is not for hotplugging your peripherals. As
2475      *     SCSI was not designed for this you could damage your
2476      *     hardware !  
2477      * However perhaps it is legal to switch on an
2478      * already connected device. It is perhaps not 
2479      * guaranteed this device doesn't corrupt an ongoing data transfer.
2480      */
2481     if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
2482         return(-EINVAL);
2483 
2484     if(!strncmp("singledevice", buffer + 5, 12)) {
2485         p = buffer + 17;
2486 
2487         for(i=0; i<4; i++)      {
2488             p++;
2489             parameter[i] = simple_strtoul(p, &p, 0);
2490         }
2491         printk("scsi singledevice %d %d %d %d\n", parameter[0], parameter[1],
2492                         parameter[2], parameter[3]);
2493 
2494         while(scd && (scd->host->host_no != parameter[0] 
2495               || scd->channel != parameter[1] 
2496               || scd->id != parameter[2] 
2497               || scd->lun != parameter[3])) {
2498             scd = scd->next;
2499         }
2500         if(scd)
2501             return(-ENOSYS);  /* We do not yet support unplugging */
2502         while(HBA_ptr && HBA_ptr->host_no != parameter[0])
2503             HBA_ptr = HBA_ptr->next;
2504 
2505         if(!HBA_ptr)
2506             return(-ENXIO);
2507 
2508         scan_scsis (HBA_ptr, 1, parameter[1], parameter[2], parameter[3]);
2509         return(length);
2510     }
2511     return(-EINVAL);
2512 }
2513 #endif
2514 
2515 /*
2516  * Go through the device list and recompute the most appropriate size
2517  * for the dma pool.  Then grab more memory (as required).
2518  */
2519 static void resize_dma_pool(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2520 {
2521     int i;
2522     struct Scsi_Host * shpnt;
2523     struct Scsi_Host * host = NULL;
2524     Scsi_Device * SDpnt;
2525     unsigned long flags;
2526     unsigned char * new_dma_malloc_freelist = NULL;
2527     unsigned int new_dma_sectors = 0;
2528     unsigned int new_need_isa_buffer = 0;
2529     unsigned char ** new_dma_malloc_pages = NULL;
2530 
2531     if( !scsi_devices )
2532     {
2533         /*
2534          * Free up the DMA pool.
2535          */
2536         if( dma_free_sectors != dma_sectors )
2537             panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
2538 
2539         for(i=0; i < dma_sectors >> 3; i++)
2540             scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
2541         if (dma_malloc_pages)
2542             scsi_init_free((char *) dma_malloc_pages,
2543                            (dma_sectors>>3)*sizeof(*dma_malloc_pages));
2544         dma_malloc_pages = NULL;
2545         if (dma_malloc_freelist)
2546             scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2547         dma_malloc_freelist = NULL;
2548         dma_sectors = 0;
2549         dma_free_sectors = 0;
2550         return;
2551     }
2552     /* Next, check to see if we need to extend the DMA buffer pool */
2553         
2554     new_dma_sectors = 16;  /* Base value we use */
2555 
2556     if (high_memory-1 > ISA_DMA_THRESHOLD)
2557         scsi_need_isa_bounce_buffers = 1;
2558     else
2559         scsi_need_isa_bounce_buffers = 0;
2560     
2561     if (scsi_devicelist)
2562         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2563             new_dma_sectors += 8;  /* Increment for each host */
2564     
2565     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2566         host = SDpnt->host;
2567         
2568         if(SDpnt->type != TYPE_TAPE)
2569             new_dma_sectors += ((host->sg_tablesize *
2570                                  sizeof(struct scatterlist) + 511) >> 9) *
2571                                      host->cmd_per_lun;
2572         
2573         if(host->unchecked_isa_dma &&
2574            scsi_need_isa_bounce_buffers &&
2575            SDpnt->type != TYPE_TAPE) {
2576             new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2577                 host->cmd_per_lun;
2578             new_need_isa_buffer++;
2579         }
2580     }
2581     
2582     new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2583     
2584     /*
2585      * We never shrink the buffers - this leads to
2586      * race conditions that I would rather not even think
2587      * about right now.
2588      */
2589     if( new_dma_sectors < dma_sectors )
2590         new_dma_sectors = dma_sectors;
2591     
2592     if (new_dma_sectors)
2593     {
2594         new_dma_malloc_freelist = (unsigned char *)
2595             scsi_init_malloc(new_dma_sectors >> 3, GFP_ATOMIC);
2596         memset(new_dma_malloc_freelist, 0, new_dma_sectors >> 3);
2597         
2598         new_dma_malloc_pages = (unsigned char **)
2599             scsi_init_malloc((new_dma_sectors>>3)*sizeof(*new_dma_malloc_pages),
2600                              GFP_ATOMIC);
2601         memset(new_dma_malloc_pages, 0,
2602                (new_dma_sectors>>3)*sizeof(*new_dma_malloc_pages));
2603     }
2604     
2605     /*
2606      * If we need more buffers, expand the list.
2607      */
2608     if( new_dma_sectors > dma_sectors ) { 
2609         for(i=dma_sectors >> 3; i< new_dma_sectors >> 3; i++)
2610             new_dma_malloc_pages[i] = (unsigned char *)
2611                 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2612     }
2613     
2614     /* When we dick with the actual DMA list, we need to 
2615      * protect things 
2616      */
2617     save_flags(flags);
2618     cli();
2619     if (dma_malloc_freelist)
2620     {
2621         memcpy(new_dma_malloc_freelist, dma_malloc_freelist, dma_sectors >> 3);
2622         scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
2623     }
2624     dma_malloc_freelist = new_dma_malloc_freelist;
2625     
2626     if (dma_malloc_pages)
2627     {
2628         memcpy(new_dma_malloc_pages, dma_malloc_pages,
2629                (dma_sectors>>3)*sizeof(*dma_malloc_pages));
2630         scsi_init_free((char *) dma_malloc_pages,
2631                        (dma_sectors>>3)*sizeof(*dma_malloc_pages));
2632     }
2633     
2634     dma_free_sectors += new_dma_sectors - dma_sectors;
2635     dma_malloc_pages = new_dma_malloc_pages;
2636     dma_sectors = new_dma_sectors;
2637     need_isa_buffer = new_need_isa_buffer;
2638     restore_flags(flags);
2639 }
2640 
2641 /*
2642  * This entry point should be called by a loadable module if it is trying
2643  * add a low level scsi driver to the system.
2644  */
2645 static int scsi_register_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2646 {
2647     int pcount;
2648     struct Scsi_Host * shpnt;
2649     Scsi_Device * SDpnt;
2650     struct Scsi_Device_Template * sdtpnt;
2651     const char * name;
2652     
2653     if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
2654                                                * no detect routine available 
2655                                                */
2656     pcount = next_scsi_host;
2657     if ((tpnt->present = tpnt->detect(tpnt)))
2658     {
2659         if(pcount == next_scsi_host) {
2660             if(tpnt->present > 1) {
2661                 printk("Failure to register low-level scsi driver");
2662                 scsi_unregister_host(tpnt);
2663                 return 1;
2664             }
2665             /* The low-level driver failed to register a driver.  We
2666              *  can do this now. 
2667              */
2668             scsi_register(tpnt,0);
2669         }
2670         tpnt->next = scsi_hosts; /* Add to the linked list */
2671         scsi_hosts = tpnt;
2672         
2673         /* Add the new driver to /proc/scsi */
2674 #if CONFIG_PROC_FS 
2675         build_proc_dir_entries(tpnt);
2676 #endif
2677         
2678         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2679             if(shpnt->hostt == tpnt)
2680             {
2681                 if(tpnt->info)
2682                     name = tpnt->info(shpnt);
2683                 else
2684                     name = tpnt->name;
2685                 printk ("scsi%d : %s\n", /* And print a little message */
2686                         shpnt->host_no, name);
2687             }
2688         
2689         printk ("scsi : %d host%s.\n", next_scsi_host,
2690                 (next_scsi_host == 1) ? "" : "s");
2691         
2692         scsi_make_blocked_list();
2693         
2694         /* The next step is to call scan_scsis here.  This generates the
2695          * Scsi_Devices entries 
2696          */
2697         
2698         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2699             if(shpnt->hostt == tpnt) scan_scsis(shpnt,0,0,0,0);
2700         
2701         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2702             if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2703         
2704         /* Next we create the Scsi_Cmnd structures for this host */
2705         
2706         for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2707             if(SDpnt->host->hostt == tpnt)
2708             {
2709                 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2710                     if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2711                 if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2712             }
2713         
2714         /*
2715          * Now that we have all of the devices, resize the DMA pool,
2716          * as required.  */
2717         resize_dma_pool();
2718 
2719 
2720         /* This does any final handling that is required. */
2721         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2722             if(sdtpnt->finish && sdtpnt->nr_dev)
2723                 (*sdtpnt->finish)();
2724     }
2725     
2726 #if defined(USE_STATIC_SCSI_MEMORY)
2727     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2728             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2729             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2730             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2731 #endif
2732         
2733     MOD_INC_USE_COUNT;
2734     return 0;
2735 }
2736 
2737 /*
2738  * Similarly, this entry point should be called by a loadable module if it
2739  * is trying to remove a low level scsi driver from the system.
2740  */
2741 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2742 {
2743     Scsi_Host_Template * SHT, *SHTp;
2744     Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
2745     Scsi_Cmnd * SCpnt;
2746     unsigned long flags;
2747     struct Scsi_Device_Template * sdtpnt;
2748     struct Scsi_Host * shpnt, *sh1;
2749     int pcount;
2750     
2751     /* First verify that this host adapter is completely free with no pending
2752      * commands */
2753     
2754     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2755         if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
2756            && *sdpnt->host->hostt->usage_count) return;
2757     
2758     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
2759     {
2760         if (shpnt->hostt != tpnt) continue;
2761         for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2762         {
2763             save_flags(flags);
2764             cli();
2765             if(SCpnt->request.rq_status != RQ_INACTIVE) {
2766                 restore_flags(flags);
2767                 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
2768                     if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
2769                         SCpnt->request.rq_status = RQ_INACTIVE;
2770                 printk("Device busy???\n");
2771                 return;
2772             }
2773             SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
2774             restore_flags(flags);
2775         }
2776     }
2777     /* Next we detach the high level drivers from the Scsi_Device structures */
2778     
2779     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2780         if(sdpnt->host->hostt == tpnt)
2781         {
2782             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2783                 if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
2784             /* If something still attached, punt */
2785             if (sdpnt->attached) {
2786                 printk("Attached usage count = %d\n", sdpnt->attached);
2787                 return;
2788             }
2789         }
2790     
2791     /* Next we free up the Scsi_Cmnd structures for this host */
2792     
2793     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
2794         if(sdpnt->host->hostt == tpnt)
2795             while (sdpnt->host->host_queue) {
2796                 SCpnt = sdpnt->host->host_queue->next;
2797                 scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
2798                 sdpnt->host->host_queue = SCpnt;
2799                 if (SCpnt) SCpnt->prev = NULL;
2800                 sdpnt->has_cmdblocks = 0;
2801             }
2802     
2803     /* Next free up the Scsi_Device structures for this host */
2804     
2805     sdppnt = NULL;
2806     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
2807     {
2808         sdpnt1 = sdpnt->next;
2809         if (sdpnt->host->hostt == tpnt) {
2810             if (sdppnt)
2811                 sdppnt->next = sdpnt->next;
2812             else
2813                 scsi_devices = sdpnt->next;
2814             scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
2815         } else
2816             sdppnt = sdpnt;
2817     }
2818     
2819     /* Next we go through and remove the instances of the individual hosts
2820      * that were detected */
2821     
2822     shpnt = scsi_hostlist;
2823     while(shpnt) {
2824         sh1 = shpnt->next;
2825         if(shpnt->hostt == tpnt) {
2826             if(shpnt->loaded_as_module) {
2827                 pcount = next_scsi_host;
2828                 /* Remove the /proc/scsi directory entry */
2829 #if CONFIG_PROC_FS 
2830                 proc_scsi_unregister(tpnt->proc_dir, 
2831                                      shpnt->host_no + PROC_SCSI_FILE);
2832 #endif   
2833                 if(tpnt->release)
2834                     (*tpnt->release)(shpnt);
2835                 else {
2836                     /* This is the default case for the release function.  
2837                      * It should do the right thing for most correctly 
2838                      * written host adapters. 
2839                      */
2840                     if (shpnt->irq) free_irq(shpnt->irq);
2841                     if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
2842                     if (shpnt->io_port && shpnt->n_io_port)
2843                         release_region(shpnt->io_port, shpnt->n_io_port);
2844                 }
2845                 if(pcount == next_scsi_host) scsi_unregister(shpnt);
2846                 tpnt->present--;
2847             }
2848         }
2849         shpnt = sh1;
2850     }
2851     
2852     /*
2853      * If there are absolutely no more hosts left, it is safe
2854      * to completely nuke the DMA pool.  The resize operation will
2855      * do the right thing and free everything.
2856      */
2857     if( !scsi_devices )
2858         resize_dma_pool();
2859 
2860     printk ("scsi : %d host%s.\n", next_scsi_host,
2861             (next_scsi_host == 1) ? "" : "s");
2862     
2863 #if defined(USE_STATIC_SCSI_MEMORY)
2864     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2865             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2866             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2867             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2868 #endif
2869     
2870     scsi_make_blocked_list();
2871     
2872     /* There were some hosts that were loaded at boot time, so we cannot
2873        do any more than this */
2874     if (tpnt->present) return;
2875     
2876     /* OK, this is the very last step.  Remove this host adapter from the
2877        linked list. */
2878     for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
2879         if(SHT == tpnt) {
2880             if(SHTp)
2881                 SHTp->next = SHT->next;
2882             else
2883                 scsi_hosts = SHT->next;
2884             SHT->next = NULL;
2885             break;
2886         }
2887     
2888     /* Rebuild the /proc/scsi directory entries */
2889 #if CONFIG_PROC_FS 
2890     proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
2891 #endif
2892     MOD_DEC_USE_COUNT;
2893 }
2894 
2895 /*
2896  * This entry point should be called by a loadable module if it is trying
2897  * add a high level scsi driver to the system.
2898  */
2899 static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2900 {
2901     Scsi_Device * SDpnt;
2902     
2903     if (tpnt->next) return 1;
2904     
2905     scsi_register_device(tpnt);
2906     /*
2907      * First scan the devices that we know about, and see if we notice them.
2908      */
2909     
2910     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2911         if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
2912     
2913     /*
2914      * If any of the devices would match this driver, then perform the
2915      * init function.
2916      */
2917     if(tpnt->init && tpnt->dev_noticed)
2918         if ((*tpnt->init)()) return 1;
2919     
2920     /*
2921      * Now actually connect the devices to the new driver.
2922      */
2923     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2924     {
2925         if(tpnt->attach)  (*tpnt->attach)(SDpnt);
2926         /*
2927          * If this driver attached to the device, and we no longer
2928          * have anything attached, release the scso command blocks.
2929          */
2930         if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
2931             scsi_build_commandblocks(SDpnt);
2932     }
2933     
2934     /*
2935      * This does any final handling that is required. 
2936      */
2937     if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
2938     MOD_INC_USE_COUNT;
2939     return 0;
2940 }
2941 
2942 static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2943 {
2944     Scsi_Device * SDpnt;
2945     Scsi_Cmnd * SCpnt;
2946     struct Scsi_Device_Template * spnt;
2947     struct Scsi_Device_Template * prev_spnt;
2948     
2949     /*
2950      * If we are busy, this is not going to fly.
2951      */
2952     if( *tpnt->usage_count != 0) return 0;
2953     /*
2954      * Next, detach the devices from the driver.
2955      */
2956     
2957     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2958     {
2959         if(tpnt->detach) (*tpnt->detach)(SDpnt);
2960         if(SDpnt->attached == 0)
2961         {
2962             /*
2963              * Nobody is using this device any more.  Free all of the
2964              * command structures.
2965              */
2966             for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
2967             {
2968                 if(SCpnt->device == SDpnt)
2969                 {
2970                     if(SCpnt->prev != NULL)
2971                         SCpnt->prev->next = SCpnt->next;
2972                     if(SCpnt->next != NULL)
2973                         SCpnt->next->prev = SCpnt->prev;
2974                     if(SCpnt == SDpnt->host->host_queue)
2975                         SDpnt->host->host_queue = SCpnt->next;
2976                     scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
2977                 }
2978             }
2979             SDpnt->has_cmdblocks = 0;
2980         }
2981     }
2982     /*
2983      * Extract the template from the linked list.
2984      */
2985     spnt = scsi_devicelist;
2986     prev_spnt = NULL;
2987     while(spnt != tpnt)
2988     {
2989         prev_spnt = spnt;
2990         spnt = spnt->next;
2991     }
2992     if(prev_spnt == NULL)
2993         scsi_devicelist = tpnt->next;
2994     else
2995         prev_spnt->next = spnt->next;
2996     
2997     MOD_DEC_USE_COUNT;
2998     /*
2999      * Final cleanup for the driver is done in the driver sources in the 
3000      * cleanup function.
3001      */
3002     return 0;
3003 }
3004 
3005 
3006 int scsi_register_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
3007 {
3008     switch(module_type){
3009     case MODULE_SCSI_HA:
3010         return scsi_register_host((Scsi_Host_Template *) ptr);
3011         
3012         /* Load upper level device handler of some kind */
3013     case MODULE_SCSI_DEV:
3014         return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
3015         /* The rest of these are not yet implemented */
3016         
3017         /* Load constants.o */
3018     case MODULE_SCSI_CONST:
3019         
3020         /* Load specialized ioctl handler for some device.  Intended for 
3021          * cdroms that have non-SCSI2 audio command sets. */
3022     case MODULE_SCSI_IOCTL:
3023         
3024     default:
3025         return 1;
3026     }
3027 }
3028 
3029 void scsi_unregister_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
3030 {
3031     switch(module_type) {
3032     case MODULE_SCSI_HA:
3033         scsi_unregister_host((Scsi_Host_Template *) ptr);
3034         break;
3035     case MODULE_SCSI_DEV:
3036         scsi_unregister_device((struct Scsi_Device_Template *) ptr);
3037         break;
3038         /* The rest of these are not yet implemented. */
3039     case MODULE_SCSI_CONST:
3040     case MODULE_SCSI_IOCTL:
3041         break;
3042     default:
3043     }
3044     return;
3045 }
3046 
3047 #ifdef DEBUG_TIMEOUT
3048 static void
3049 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3050 {
3051     int i;
3052     struct Scsi_Host * shpnt;
3053     Scsi_Cmnd * SCpnt;
3054     printk("Dump of scsi parameters:\n");
3055     i = 0;
3056     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3057         for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3058         {
3059             /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
3060             printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
3061                    i++, SCpnt->host->host_no,
3062                    SCpnt->channel,
3063                    SCpnt->target,
3064                    SCpnt->lun,
3065                    kdevname(SCpnt->request.rq_dev),
3066                    SCpnt->request.sector,
3067                    SCpnt->request.nr_sectors,
3068                    SCpnt->request.current_nr_sectors,
3069                    SCpnt->use_sg,
3070                    SCpnt->retries,
3071                    SCpnt->allowed,
3072                    SCpnt->flags,
3073                    SCpnt->timeout_per_command,
3074                    SCpnt->timeout,
3075                    SCpnt->internal_timeout,
3076                    SCpnt->cmnd[0],
3077                    SCpnt->sense_buffer[2],
3078                    SCpnt->result);
3079         }
3080     printk("wait_for_request = %p\n", wait_for_request);
3081     /* Now dump the request lists for each block device */
3082     printk("Dump of pending block device requests\n");
3083     for(i=0; i<MAX_BLKDEV; i++)
3084         if(blk_dev[i].current_request)
3085         {
3086             struct request * req;
3087             printk("%d: ", i);
3088             req = blk_dev[i].current_request;
3089             while(req) {
3090                 printk("(%s %d %ld %ld %ld) ",
3091                        kdevname(req->rq_dev),
3092                        req->cmd,
3093                        req->sector,
3094                        req->nr_sectors,
3095                        req->current_nr_sectors);
3096                 req = req->next;
3097             }
3098             printk("\n");
3099         }
3100 }
3101 #endif
3102 
3103 #ifdef MODULE
3104 
3105 extern struct symbol_table scsi_symbol_table;
3106 
3107 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
3108     /*
3109      * This makes /proc/scsi visible.
3110      */
3111     dispatch_scsi_info_ptr = dispatch_scsi_info;
3112 
3113     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
3114     timer_table[SCSI_TIMER].expires = 0;
3115     register_symtab(&scsi_symbol_table);
3116     scsi_loadable_module_flag = 1;
3117 
3118     /* Register the /proc/scsi/scsi entry */
3119 #if CONFIG_PROC_FS
3120     proc_scsi_register(0, &proc_scsi_scsi);
3121 #endif
3122 
3123     
3124     dma_sectors = PAGE_SIZE / 512;
3125     dma_free_sectors= dma_sectors;
3126     /*
3127      * Set up a minimal DMA buffer list - this will be used during scan_scsis
3128      * in some cases.
3129      */
3130     
3131     /* One bit per sector to indicate free/busy */
3132     dma_malloc_freelist = (unsigned char *)
3133         scsi_init_malloc(dma_sectors >> 3, GFP_ATOMIC);
3134     memset(dma_malloc_freelist, 0, dma_sectors >> 3);
3135     
3136     /* One pointer per page for the page list */
3137     dma_malloc_pages = (unsigned char **)
3138         scsi_init_malloc((dma_sectors >> 3)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
3139     dma_malloc_pages[0] = (unsigned char *)
3140         scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
3141     return 0;
3142 }
3143 
3144 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
3145 {
3146 #if CONFIG_PROC_FS
3147     proc_scsi_unregister(0, PROC_SCSI_SCSI);
3148 #endif
3149 
3150     /* No, we're not here anymore. Don't show the /proc/scsi files. */
3151     dispatch_scsi_info_ptr = 0L;
3152 
3153     /*
3154      * Free up the DMA pool.
3155      */
3156     resize_dma_pool();
3157 
3158     timer_table[SCSI_TIMER].fn = NULL;
3159     timer_table[SCSI_TIMER].expires = 0;
3160 }
3161 #endif /* MODULE */
3162 
3163 /*
3164  * Overrides for Emacs so that we follow Linus's tabbing style.
3165  * Emacs will notice this stuff at the end of the file and automatically
3166  * adjust the settings for this buffer only.  This must remain at the end
3167  * of the file.
3168  * ---------------------------------------------------------------------------
3169  * Local variables:
3170  * c-indent-level: 4
3171  * c-brace-imaginary-offset: 0
3172  * c-brace-offset: -4
3173  * c-argdecl-indent: 4
3174  * c-label-offset: -4
3175  * c-continued-statement-offset: 4
3176  * c-continued-brace-offset: 0
3177  * indent-tabs-mode: nil
3178  * tab-width: 8
3179  * End:
3180  */

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