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

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