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

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