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

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