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

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