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

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