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

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