root/drivers/scsi/scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. get_device_flags
  2. scsi_make_blocked_list
  3. scan_scsis_done
  4. scsi_luns_setup
  5. scan_scsis
  6. scan_scsis_single
  7. scsi_times_out
  8. request_queueable
  9. allocate_device
  10. internal_cmnd
  11. scsi_request_sense
  12. scsi_do_cmd
  13. check_sense
  14. scsi_done
  15. scsi_abort
  16. scsi_mark_device_reset
  17. scsi_mark_host_bus_reset
  18. scsi_reset
  19. scsi_main_timeout
  20. update_timeout
  21. scsi_malloc
  22. scsi_free
  23. scsi_init_malloc
  24. scsi_init_free
  25. scsi_build_commandblocks
  26. scsi_dev_init
  27. print_inquiry
  28. scsi_proc_info
  29. resize_dma_pool
  30. scsi_register_host
  31. scsi_unregister_host
  32. scsi_register_device_module
  33. scsi_unregister_device
  34. scsi_register_module
  35. scsi_unregister_module
  36. scsi_dump_status
  37. init_module
  38. cleanup_module

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

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