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

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