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

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