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 (2*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 (1*HZ)
 205     #define RESET_TIMEOUT (5*HZ)
 206     #define ABORT_TIMEOUT (5*HZ)
 207 #endif
 208 
 209 #define MIN_RESET_DELAY (3*HZ)
 210 
 211 /* Do not call reset on error if we just did a reset within 10 sec. */
 212 #define MIN_RESET_PERIOD (15*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_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 (host_byte(SCpnt->result) != DID_OK) {
 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 #define IN_RESET3 8
 823 
 824 /*
 825  * This is our time out function, called when the timer expires for a
 826  * given host adapter.  It will attempt to abort the currently executing
 827  * command, that failing perform a kernel panic.
 828  */
 829 
 830 static void scsi_times_out (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 831 {
 832     
 833     switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET | IN_RESET2 | IN_RESET3))
 834     {
 835     case NORMAL_TIMEOUT:
 836         {
 837 #ifdef DEBUG_TIMEOUT
 838             scsi_dump_status();
 839 #endif
 840         }
 841         
 842         if (!scsi_abort (SCpnt, DID_TIME_OUT))
 843             return;
 844     case IN_ABORT:
 845         printk("SCSI host %d abort (pid %ld) timed out - resetting\n",
 846                SCpnt->host->host_no, SCpnt->pid);
 847         if (!scsi_reset (SCpnt, SCSI_RESET_ASYNCHRONOUS))
 848             return;
 849     case IN_RESET:
 850     case (IN_ABORT | IN_RESET):
 851         /* This might be controversial, but if there is a bus hang,
 852          * you might conceivably want the machine up and running
 853          * esp if you have an ide disk. 
 854          */
 855         printk("SCSI host %d channel %d reset (pid %ld) timed out - "
 856                "trying harder\n",
 857                SCpnt->host->host_no, SCpnt->channel, SCpnt->pid);
 858         SCpnt->internal_timeout &= ~IN_RESET;
 859         SCpnt->internal_timeout |= IN_RESET2;
 860         scsi_reset (SCpnt,
 861                     SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_BUS_RESET);
 862         return;
 863         
 864     case (IN_ABORT | IN_RESET | IN_RESET2):
 865         /* Obviously the bus reset didn't work.
 866          * Let's try even harder and call for an HBA reset.
 867          * Maybe the HBA itself crashed and this will shake it loose.
 868          */
 869         printk("SCSI host %d reset (pid %ld) timed out - trying to shake it loose\n",
 870                SCpnt->host->host_no, SCpnt->pid);
 871         SCpnt->internal_timeout &= ~(IN_RESET | IN_RESET2);
 872         SCpnt->internal_timeout |= IN_RESET3;
 873         scsi_reset (SCpnt,
 874                     SCSI_RESET_ASYNCHRONOUS | SCSI_RESET_SUGGEST_HOST_RESET);
 875         return;
 876                 
 877     default:
 878         printk("SCSI host %d reset (pid %ld) timed out again -\n",
 879                SCpnt->host->host_no, SCpnt->pid);
 880         printk("probably an unrecoverable SCSI bus or device hang.\n");
 881         return;
 882 
 883     }
 884     
 885 }
 886 
 887 
 888 /* This function takes a quick look at a request, and decides if it
 889  * can be queued now, or if there would be a stall while waiting for
 890  * something else to finish.  This routine assumes that interrupts are
 891  * turned off when entering the routine.  It is the responsibility
 892  * of the calling code to ensure that this is the case. 
 893  */
 894 
 895 Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
     /* [previous][next][first][last][top][bottom][index][help] */
 896 {
 897     Scsi_Cmnd * SCpnt = NULL;
 898     int tablesize;
 899     Scsi_Cmnd * found = NULL;
 900     struct buffer_head * bh, *bhp;
 901     
 902     if (!device)
 903         panic ("No device passed to request_queueable().\n");
 904     
 905     if (req && req->rq_status == RQ_INACTIVE)
 906         panic("Inactive in request_queueable");
 907 
 908     /*
 909      * Look for a free command block.  If we have been instructed not to queue
 910      * multiple commands to multi-lun devices, then check to see what else is 
 911      * going for this device first.
 912      */
 913       
 914     if (!device->single_lun) {
 915         SCpnt = device->device_queue;
 916         while(SCpnt){
 917             if(SCpnt->request.rq_status == RQ_INACTIVE) break;
 918             SCpnt = SCpnt->device_next;
 919         }
 920     } else {
 921         SCpnt = device->host->host_queue;
 922         while(SCpnt){
 923             if(SCpnt->channel == device->channel 
 924                 && SCpnt->target == device->id) {
 925                 if (SCpnt->lun == device->lun) {
 926                     if(found == NULL 
 927                        && SCpnt->request.rq_status == RQ_INACTIVE) 
 928                     {
 929                         found=SCpnt;
 930                     }
 931                 } 
 932                 if(SCpnt->request.rq_status != RQ_INACTIVE) {
 933                     /*
 934                      * I think that we should really limit things to one
 935                      * outstanding command per device - this is what tends 
 936                      * to trip up buggy firmware.
 937                      */
 938                     return NULL;
 939                 }
 940             }
 941             SCpnt = SCpnt->next;
 942         }
 943         SCpnt = found;
 944     }
 945     
 946     if (!SCpnt) return NULL;
 947     
 948     if (SCSI_BLOCK(device->host)) return NULL;
 949     
 950     if (req) {
 951         memcpy(&SCpnt->request, req, sizeof(struct request));
 952         tablesize = device->host->sg_tablesize;
 953         bhp = bh = req->bh;
 954         if(!tablesize) bh = NULL;
 955         /* Take a quick look through the table to see how big it is.  
 956          * We already have our copy of req, so we can mess with that 
 957          * if we want to. 
 958          */
 959         while(req->nr_sectors && bh){
 960             bhp = bhp->b_reqnext;
 961             if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
 962             req->nr_sectors -= bh->b_size >> 9;
 963             req->sector += bh->b_size >> 9;
 964             if(!tablesize) break;
 965             bh = bhp;
 966         }
 967         if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
 968             SCpnt->request.bhtail = bh;
 969             req->bh = bh->b_reqnext; /* Divide request */
 970             bh->b_reqnext = NULL;
 971             bh = req->bh;
 972             
 973             /* Now reset things so that req looks OK */
 974             SCpnt->request.nr_sectors -= req->nr_sectors;
 975             req->current_nr_sectors = bh->b_size >> 9;
 976             req->buffer = bh->b_data;
 977             SCpnt->request.sem = NULL; /* Wait until whole thing done */
 978         } else {
 979             req->rq_status = RQ_INACTIVE;
 980             wake_up(&wait_for_request);
 981         }
 982     } else {
 983         SCpnt->request.rq_status = RQ_SCSI_BUSY;  /* Busy, but no request */
 984         SCpnt->request.sem = NULL;   /* And no one is waiting for the device 
 985                                       * either */
 986     }
 987     
 988     SCpnt->use_sg = 0;               /* Reset the scatter-gather flag */
 989     SCpnt->old_use_sg  = 0;
 990     SCpnt->transfersize = 0;
 991     SCpnt->underflow = 0;
 992     SCpnt->cmd_len = 0;
 993 
 994 /* Since not everyone seems to set the device info correctly
 995  * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
 996  */ 
 997     SCpnt->channel = device->channel;
 998     SCpnt->lun = device->lun;
 999     SCpnt->target = device->id;
1000 
1001     return SCpnt;
1002 }
1003 
1004 /* This function returns a structure pointer that will be valid for
1005  * the device.  The wait parameter tells us whether we should wait for
1006  * the unit to become free or not.  We are also able to tell this routine
1007  * not to return a descriptor if the host is unable to accept any more
1008  * commands for the time being.  We need to keep in mind that there is no
1009  * guarantee that the host remain not busy.  Keep in mind the
1010  * request_queueable function also knows the internal allocation scheme
1011  * of the packets for each device 
1012  */
1013 
1014 Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
     /* [previous][next][first][last][top][bottom][index][help] */
1015                              int wait)
1016 {
1017     kdev_t dev;
1018     struct request * req = NULL;
1019     int tablesize;
1020     unsigned int flags;
1021     struct buffer_head * bh, *bhp;
1022     struct Scsi_Host * host;
1023     Scsi_Cmnd * SCpnt = NULL;
1024     Scsi_Cmnd * SCwait = NULL;
1025     Scsi_Cmnd * found = NULL;
1026     
1027     if (!device)
1028         panic ("No device passed to allocate_device().\n");
1029     
1030     if (reqp) req = *reqp;
1031     
1032     /* See if this request has already been queued by an interrupt routine */
1033     if (req) {
1034         if(req->rq_status == RQ_INACTIVE) return NULL;
1035         dev = req->rq_dev;
1036     } else
1037         dev = 0;                /* unused */
1038     
1039     host = device->host;
1040     
1041     if (intr_count && SCSI_BLOCK(host)) return NULL;
1042     
1043     while (1==1){
1044         if (!device->single_lun) {
1045             SCpnt = device->device_queue;
1046             while(SCpnt){
1047                 SCwait = SCpnt;
1048                 if(SCpnt->request.rq_status == RQ_INACTIVE) break;
1049                 SCpnt = SCpnt->device_next;
1050             }
1051         } else {
1052             SCpnt = device->host->host_queue;
1053             while(SCpnt){
1054                 if(SCpnt->channel == device->channel 
1055                    && SCpnt->target == device->id) {
1056                     if (SCpnt->lun == device->lun) {
1057                         SCwait = SCpnt;
1058                         if(found == NULL 
1059                            && SCpnt->request.rq_status == RQ_INACTIVE) 
1060                         {
1061                             found=SCpnt;
1062                         }
1063                     } 
1064                     if(SCpnt->request.rq_status != RQ_INACTIVE) {
1065                         /*
1066                          * I think that we should really limit things to one
1067                          * outstanding command per device - this is what tends
1068                          * to trip up buggy firmware.
1069                          */
1070                         found = NULL;
1071                         break;
1072                     }
1073                 }
1074                 SCpnt = SCpnt->next;
1075             }
1076             SCpnt = found;
1077         }
1078 
1079         save_flags(flags);
1080         cli();
1081         /* See if this request has already been queued by an interrupt routine
1082          */
1083         if (req && (req->rq_status == RQ_INACTIVE || req->rq_dev != dev)) {
1084             restore_flags(flags);
1085             return NULL;
1086         }
1087         if (!SCpnt || SCpnt->request.rq_status != RQ_INACTIVE)  /* Might have changed */
1088         {
1089 #if 1   /* NEW CODE */
1090                 if (wait && SCwait && SCwait->request.rq_status != RQ_INACTIVE){
1091                         sleep_on(&device->device_wait);
1092                         restore_flags(flags);
1093                 } else {
1094                         restore_flags(flags);
1095                         if (!wait) return NULL;
1096                         if (!SCwait) {
1097                                 printk("Attempt to allocate device channel %d,"
1098                                        " target %d, lun %d\n", device->channel,
1099                                        device->id, device->lun);
1100                                 panic("No device found in allocate_device\n");
1101                         }
1102                 }
1103 #else   /* ORIGINAL CODE */
1104                     restore_flags(flags);
1105                     if(!wait) return NULL;
1106                     if (!SCwait) {
1107                         printk("Attempt to allocate device channel %d, target"
1108                                " %d, lun %d\n", device->channel, device->id, 
1109                                device->lun);
1110                         panic("No device found in allocate_device\n");
1111                     }
1112                     SCSI_SLEEP(&device->device_wait,
1113                                (SCwait->request.rq_status != RQ_INACTIVE));
1114 #endif
1115         } else {
1116             if (req) {
1117                 memcpy(&SCpnt->request, req, sizeof(struct request));
1118                 tablesize = device->host->sg_tablesize;
1119                 bhp = bh = req->bh;
1120                 if(!tablesize) bh = NULL;
1121                 /* Take a quick look through the table to see how big it is.  
1122                  * We already have our copy of req, so we can mess with that 
1123                  * if we want to.  
1124                  */
1125                 while(req->nr_sectors && bh){
1126                     bhp = bhp->b_reqnext;
1127                     if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
1128                     req->nr_sectors -= bh->b_size >> 9;
1129                     req->sector += bh->b_size >> 9;
1130                     if(!tablesize) break;
1131                     bh = bhp;
1132                 }
1133                 if(req->nr_sectors && bh && bh->b_reqnext){/* Any leftovers? */
1134                     SCpnt->request.bhtail = bh;
1135                     req->bh = bh->b_reqnext; /* Divide request */
1136                     bh->b_reqnext = NULL;
1137                     bh = req->bh;
1138                     /* Now reset things so that req looks OK */
1139                     SCpnt->request.nr_sectors -= req->nr_sectors;
1140                     req->current_nr_sectors = bh->b_size >> 9;
1141                     req->buffer = bh->b_data;
1142                     SCpnt->request.sem = NULL; /* Wait until whole thing done*/
1143                 }
1144                 else
1145                 {
1146                     req->rq_status = RQ_INACTIVE;
1147                     *reqp = req->next;
1148                     wake_up(&wait_for_request);
1149                 }
1150             } else {
1151                 SCpnt->request.rq_status = RQ_SCSI_BUSY;
1152                 SCpnt->request.sem = NULL;   /* And no one is waiting for this 
1153                                               * to complete */
1154             }
1155             restore_flags(flags);
1156             break;
1157         }
1158     }
1159     
1160     SCpnt->use_sg = 0;            /* Reset the scatter-gather flag */
1161     SCpnt->old_use_sg  = 0;
1162     SCpnt->transfersize = 0;      /* No default transfer size */
1163     SCpnt->cmd_len = 0;
1164 
1165     SCpnt->underflow = 0;         /* Do not flag underflow conditions */
1166 
1167     /* Since not everyone seems to set the device info correctly
1168      * before Scsi_Cmnd gets send out to scsi_do_command, we do it here.
1169      */ 
1170     SCpnt->channel = device->channel;
1171     SCpnt->lun = device->lun;
1172     SCpnt->target = device->id;
1173 
1174     return SCpnt;
1175 }
1176 
1177 /*
1178  * This is inline because we have stack problemes if we recurse to deeply.
1179  */
1180 
1181 inline void internal_cmnd (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1182 {
1183     int temp;
1184     struct Scsi_Host * host;
1185     unsigned int flags;
1186 #ifdef DEBUG_DELAY
1187     int clock;
1188 #endif
1189     
1190     host = SCpnt->host;
1191     
1192     /*
1193      * We will wait MIN_RESET_DELAY clock ticks after the last reset so
1194      * we can avoid the drive not being ready.
1195      */
1196     save_flags(flags);
1197     cli();
1198     /* Assign a unique nonzero serial_number. */
1199     if (++serial_number == 0) serial_number = 1;
1200     SCpnt->serial_number = serial_number;
1201     sti();
1202     temp = host->last_reset + MIN_RESET_DELAY;
1203     while (jiffies < temp);
1204     restore_flags(flags);
1205     
1206     update_timeout(SCpnt, SCpnt->timeout_per_command);
1207     
1208     /*
1209      * We will use a queued command if possible, otherwise we will emulate the
1210      * queuing and calling of completion function ourselves.
1211      */
1212 #ifdef DEBUG
1213     printk("internal_cmnd (host = %d, channel = %d, target = %d, "
1214            "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n", 
1215            SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd, 
1216            SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
1217 #endif
1218     
1219     if (host->can_queue)
1220     {
1221 #ifdef DEBUG
1222         printk("queuecommand : routine at %p\n",
1223                host->hostt->queuecommand);
1224 #endif
1225         /* This locking tries to prevent all sorts of races between
1226          * queuecommand and the interrupt code.  In effect,
1227          * we are only allowed to be in queuecommand once at
1228          * any given time, and we can only be in the interrupt
1229          * handler and the queuecommand function at the same time
1230          * when queuecommand is called while servicing the
1231          * interrupt. 
1232          */
1233         
1234         if(!intr_count && SCpnt->host->irq)
1235             disable_irq(SCpnt->host->irq);
1236         
1237         host->hostt->queuecommand (SCpnt, scsi_done);
1238         
1239         if(!intr_count && SCpnt->host->irq)
1240             enable_irq(SCpnt->host->irq);
1241     }
1242     else
1243     {
1244         
1245 #ifdef DEBUG
1246         printk("command() :  routine at %p\n", host->hostt->command);
1247 #endif
1248         temp=host->hostt->command (SCpnt);
1249         SCpnt->result = temp;
1250 #ifdef DEBUG_DELAY
1251         clock = jiffies + 4 * HZ;
1252         while (jiffies < clock);
1253         printk("done(host = %d, result = %04x) : routine at %p\n", 
1254                host->host_no, temp, host->hostt->command);
1255 #endif
1256         scsi_done(SCpnt);
1257     }
1258 #ifdef DEBUG
1259     printk("leaving internal_cmnd()\n");
1260 #endif
1261 }
1262 
1263 static void scsi_request_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1264 {
1265     unsigned int flags;
1266     
1267     save_flags(flags);
1268     cli();
1269     SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
1270     update_timeout(SCpnt, SENSE_TIMEOUT);
1271     restore_flags(flags);
1272     
1273     
1274     memcpy ((void *) SCpnt->cmnd , (void *) generic_sense, 
1275             sizeof(generic_sense));
1276     
1277     SCpnt->cmnd[1] = SCpnt->lun << 5;
1278     SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
1279     
1280     SCpnt->request_buffer = &SCpnt->sense_buffer;
1281     SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
1282     SCpnt->use_sg = 0;
1283     SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1284     internal_cmnd (SCpnt);
1285 }
1286 
1287 
1288 
1289 /*
1290  * scsi_do_cmd sends all the commands out to the low-level driver.  It
1291  * handles the specifics required for each low level driver - ie queued
1292  * or non queued.  It also prevents conflicts when different high level
1293  * drivers go for the same host at the same time.
1294  */
1295 
1296 void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
     /* [previous][next][first][last][top][bottom][index][help] */
1297                   void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
1298                   int timeout, int retries)
1299 {
1300     unsigned long flags;
1301     struct Scsi_Host * host = SCpnt->host;
1302     
1303 #ifdef DEBUG
1304     {
1305         int i;
1306         int target = SCpnt->target;
1307         printk ("scsi_do_cmd (host = %d, channel = %d target = %d, "
1308                 "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
1309                 "retries = %d)\n"
1310                 "command : " , host->host_no, SCpnt->channel, target, buffer, 
1311                 bufflen, done, timeout, retries);
1312         for (i = 0; i < 10; ++i)
1313             printk ("%02x  ", ((unsigned char *) cmnd)[i]);
1314         printk("\n");
1315     }
1316 #endif
1317     
1318     if (!host)
1319     {
1320         panic ("Invalid or not present host.\n");
1321     }
1322     
1323     
1324     /*
1325      * We must prevent reentrancy to the lowlevel host driver.  This prevents
1326      * it - we enter a loop until the host we want to talk to is not busy.
1327      * Race conditions are prevented, as interrupts are disabled in between the
1328      * time we check for the host being not busy, and the time we mark it busy
1329      * ourselves.
1330      */
1331 
1332     save_flags(flags);
1333     cli();
1334     SCpnt->pid = scsi_pid++;
1335     
1336     while (SCSI_BLOCK(host)) {
1337         restore_flags(flags);
1338         SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
1339         cli();
1340     }
1341     
1342     if (host->block) host_active = host;
1343     
1344     host->host_busy++;
1345     restore_flags(flags);
1346     
1347     /*
1348      * Our own function scsi_done (which marks the host as not busy, disables
1349      * the timeout counter, etc) will be called by us or by the
1350      * scsi_hosts[host].queuecommand() function needs to also call
1351      * the completion function for the high level driver.
1352      */
1353     
1354     memcpy ((void *) SCpnt->data_cmnd , (const void *) cmnd, 12);
1355 #if 0
1356     SCpnt->host = host;
1357     SCpnt->channel = channel;
1358     SCpnt->target = target;
1359     SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
1360 #endif
1361     SCpnt->reset_chain = NULL;
1362     SCpnt->serial_number = 0;
1363     SCpnt->bufflen = bufflen;
1364     SCpnt->buffer = buffer;
1365     SCpnt->flags=0;
1366     SCpnt->retries=0;
1367     SCpnt->allowed=retries;
1368     SCpnt->done = done;
1369     SCpnt->timeout_per_command = timeout;
1370 
1371     memcpy ((void *) SCpnt->cmnd , (const void *) cmnd, 12);
1372     /* Zero the sense buffer.  Some host adapters automatically request
1373      * sense on error.  0 is not a valid sense code.  
1374      */
1375     memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1376     SCpnt->request_buffer = buffer;
1377     SCpnt->request_bufflen = bufflen;
1378     SCpnt->old_use_sg = SCpnt->use_sg;
1379     if (SCpnt->cmd_len == 0)
1380         SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1381     SCpnt->old_cmd_len = SCpnt->cmd_len;
1382 
1383     /* Start the timer ticking.  */
1384 
1385     SCpnt->internal_timeout = 0;
1386     SCpnt->abort_reason = 0;
1387     internal_cmnd (SCpnt);
1388 
1389 #ifdef DEBUG
1390     printk ("Leaving scsi_do_cmd()\n");
1391 #endif
1392 }
1393 
1394 static int check_sense (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1395 {
1396     /* If there is no sense information, request it.  If we have already
1397      * requested it, there is no point in asking again - the firmware must
1398      * be confused. 
1399      */
1400     if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
1401         if(!(SCpnt->flags & ASKED_FOR_SENSE))
1402             return SUGGEST_SENSE;
1403         else
1404             return SUGGEST_RETRY;
1405     }
1406     
1407     SCpnt->flags &= ~ASKED_FOR_SENSE;
1408     
1409 #ifdef DEBUG_INIT
1410     printk("scsi%d, channel%d : ", SCpnt->host->host_no, SCpnt->channel);
1411     print_sense("", SCpnt);
1412     printk("\n");
1413 #endif
1414     if (SCpnt->sense_buffer[2] & 0xe0)
1415         return SUGGEST_ABORT;
1416     
1417     switch (SCpnt->sense_buffer[2] & 0xf)
1418     {
1419     case NO_SENSE:
1420         return 0;
1421     case RECOVERED_ERROR:
1422         return SUGGEST_IS_OK;
1423         
1424     case ABORTED_COMMAND:
1425         return SUGGEST_RETRY;
1426     case NOT_READY:
1427     case UNIT_ATTENTION:
1428         /*
1429          * If we are expecting a CC/UA because of a bus reset that we
1430          * performed, treat this just as a retry.  Otherwise this is
1431          * information that we should pass up to the upper-level driver
1432          * so that we can deal with it there.
1433          */
1434         if( SCpnt->device->expecting_cc_ua )
1435         {
1436             SCpnt->device->expecting_cc_ua = 0;
1437             return SUGGEST_RETRY;
1438         }
1439         return SUGGEST_ABORT;
1440         
1441     /* these three are not supported */
1442     case COPY_ABORTED:
1443     case VOLUME_OVERFLOW:
1444     case MISCOMPARE:
1445         
1446     case MEDIUM_ERROR:
1447         return SUGGEST_REMAP;
1448     case BLANK_CHECK:
1449     case DATA_PROTECT:
1450     case HARDWARE_ERROR:
1451     case ILLEGAL_REQUEST:
1452     default:
1453         return SUGGEST_ABORT;
1454     }
1455 }
1456 
1457 /* This function is the mid-level interrupt routine, which decides how
1458  *  to handle error conditions.  Each invocation of this function must
1459  *  do one and *only* one of the following:
1460  *
1461  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
1462  *      normal completion, and indicates that the handling for this
1463  *      request is complete.
1464  *  (2) Call internal_cmnd to requeue the command.  This will result in
1465  *      scsi_done being called again when the retry is complete.
1466  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
1467  *      more information about the error condition.  When the information
1468  *      is available, scsi_done will be called again.
1469  *  (4) Call reset().  This is sort of a last resort, and the idea is that
1470  *      this may kick things loose and get the drive working again.  reset()
1471  *      automatically calls scsi_request_sense, and thus scsi_done will be
1472  *      called again once the reset is complete.
1473  *
1474  *      If none of the above actions are taken, the drive in question
1475  *      will hang. If more than one of the above actions are taken by
1476  *      scsi_done, then unpredictable behavior will result.
1477  */
1478 static void scsi_done (Scsi_Cmnd * SCpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
1479 {
1480     int status=0;
1481     int exit=0;
1482     int checked;
1483     int oldto;
1484     struct Scsi_Host * host = SCpnt->host;
1485     int result = SCpnt->result;
1486     SCpnt->serial_number = 0;
1487     oldto = update_timeout(SCpnt, 0);
1488     
1489 #ifdef DEBUG_TIMEOUT
1490     if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
1491                       result, SCpnt->target, SCpnt->lun);
1492 #endif
1493     
1494     /* If we requested an abort, (and we got it) then fix up the return
1495      *  status to say why 
1496      */
1497     if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
1498         SCpnt->result = result = (result & 0xff00ffff) |
1499             (SCpnt->abort_reason << 16);
1500 
1501 
1502 #define FINISHED 0
1503 #define MAYREDO  1
1504 #define REDO     3
1505 #define PENDING  4
1506 
1507 #ifdef DEBUG
1508     printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
1509 #endif
1510 
1511     if(SCpnt->flags & WAS_SENSE)
1512     {
1513         SCpnt->use_sg = SCpnt->old_use_sg;
1514         SCpnt->cmd_len = SCpnt->old_cmd_len;
1515     }
1516 
1517     switch (host_byte(result))
1518     {
1519     case DID_OK:
1520         if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
1521             /* Failed to obtain sense information */
1522         {
1523             SCpnt->flags &= ~WAS_SENSE;
1524 #if 0   /* This cannot possibly be correct. */
1525             SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1526 #endif
1527             
1528             if (!(SCpnt->flags & WAS_RESET))
1529             {
1530                 printk("scsi%d : channel %d target %d lun %d request sense"
1531                        " failed, performing reset.\n",
1532                        SCpnt->host->host_no, SCpnt->channel, SCpnt->target, 
1533                        SCpnt->lun);
1534                 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1535                 return;
1536             }
1537             else
1538             {
1539                 exit = (DRIVER_HARD | SUGGEST_ABORT);
1540                 status = FINISHED;
1541             }
1542         }
1543         else switch(msg_byte(result))
1544         {
1545         case COMMAND_COMPLETE:
1546             switch (status_byte(result))
1547             {
1548             case GOOD:
1549                 if (SCpnt->flags & WAS_SENSE)
1550                 {
1551 #ifdef DEBUG
1552                     printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
1553 #endif
1554                     SCpnt->flags &= ~WAS_SENSE;
1555 #if 0   /* This cannot possibly be correct. */
1556                     SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
1557 #endif
1558                     
1559                     switch (checked = check_sense(SCpnt))
1560                     {
1561                     case SUGGEST_SENSE:
1562                     case 0:
1563 #ifdef DEBUG
1564                         printk("NO SENSE.  status = REDO\n");
1565 #endif
1566                         update_timeout(SCpnt, oldto);
1567                         status = REDO;
1568                         break;
1569                     case SUGGEST_IS_OK:
1570                         break;
1571                     case SUGGEST_REMAP:
1572                     case SUGGEST_RETRY:
1573 #ifdef DEBUG
1574                         printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
1575 #endif
1576                         status = MAYREDO;
1577                         exit = DRIVER_SENSE | SUGGEST_RETRY;
1578                         break;
1579                     case SUGGEST_ABORT:
1580 #ifdef DEBUG
1581                         printk("SENSE SUGGEST ABORT - status = FINISHED");
1582 #endif
1583                         status = FINISHED;
1584                         exit =  DRIVER_SENSE | SUGGEST_ABORT;
1585                         break;
1586                     default:
1587                         printk ("Internal error %s %d \n", __FILE__,
1588                                 __LINE__);
1589                     }
1590                 } /* end WAS_SENSE */
1591                 else
1592                 {
1593 #ifdef DEBUG
1594                     printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
1595 #endif
1596                     exit =  DRIVER_OK;
1597                     status = FINISHED;
1598                 }
1599                 break;
1600                 
1601             case CHECK_CONDITION:
1602                 switch (check_sense(SCpnt))
1603                 {
1604                 case 0:
1605                     update_timeout(SCpnt, oldto);
1606                     status = REDO;
1607                     break;
1608                 case SUGGEST_REMAP:
1609                 case SUGGEST_RETRY:
1610                     status = MAYREDO;
1611                     exit = DRIVER_SENSE | SUGGEST_RETRY;
1612                     break;
1613                 case SUGGEST_ABORT:
1614                     status = FINISHED;
1615                     exit =  DRIVER_SENSE | SUGGEST_ABORT;
1616                     break;
1617                 case SUGGEST_SENSE:
1618                     scsi_request_sense (SCpnt);
1619                     status = PENDING;
1620                     break;
1621                 }
1622                 break;
1623                 
1624             case CONDITION_GOOD:
1625             case INTERMEDIATE_GOOD:
1626             case INTERMEDIATE_C_GOOD:
1627                 break;
1628                 
1629             case BUSY:
1630                 update_timeout(SCpnt, oldto);
1631                 status = REDO;
1632                 break;
1633                 
1634             case RESERVATION_CONFLICT:
1635                 printk("scsi%d, channel %d : RESERVATION CONFLICT performing"
1636                        " reset.\n", SCpnt->host->host_no, SCpnt->channel);
1637                 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1638                 return;
1639 #if 0
1640                 exit = DRIVER_SOFT | SUGGEST_ABORT;
1641                 status = MAYREDO;
1642                 break;
1643 #endif
1644             default:
1645                 printk ("Internal error %s %d \n"
1646                         "status byte = %d \n", __FILE__,
1647                         __LINE__, status_byte(result));
1648                 
1649             }
1650             break;
1651         default:
1652             panic("scsi: unsupported message byte %d received\n", 
1653                   msg_byte(result));
1654         }
1655         break;
1656     case DID_TIME_OUT:
1657 #ifdef DEBUG
1658         printk("Host returned DID_TIME_OUT - ");
1659 #endif
1660         
1661         if (SCpnt->flags & WAS_TIMEDOUT)
1662         {
1663 #ifdef DEBUG
1664             printk("Aborting\n");
1665 #endif
1666             /*
1667               Allow TEST_UNIT_READY and INQUIRY commands to timeout early
1668               without causing resets.  All other commands should be retried.
1669             */
1670             if (SCpnt->cmnd[0] != TEST_UNIT_READY &&
1671                 SCpnt->cmnd[0] != INQUIRY)
1672                     status = MAYREDO;
1673             exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
1674         }
1675         else
1676         {
1677 #ifdef DEBUG
1678             printk ("Retrying.\n");
1679 #endif
1680             SCpnt->flags  |= WAS_TIMEDOUT;
1681             SCpnt->internal_timeout &= ~IN_ABORT;
1682             status = REDO;
1683         }
1684         break;
1685     case DID_BUS_BUSY:
1686     case DID_PARITY:
1687         status = REDO;
1688         break;
1689     case DID_NO_CONNECT:
1690 #ifdef DEBUG
1691         printk("Couldn't connect.\n");
1692 #endif
1693         exit  = (DRIVER_HARD | SUGGEST_ABORT);
1694         break;
1695     case DID_ERROR:
1696         status = MAYREDO;
1697         exit = (DRIVER_HARD | SUGGEST_ABORT);
1698         break;
1699     case DID_BAD_TARGET:
1700     case DID_ABORT:
1701         exit = (DRIVER_INVALID | SUGGEST_ABORT);
1702         break;
1703     case DID_RESET:
1704         if (SCpnt->flags & IS_RESETTING)
1705         {
1706             SCpnt->flags &= ~IS_RESETTING;
1707             status = REDO;
1708             break;
1709         }
1710         
1711         if(msg_byte(result) == GOOD &&
1712            status_byte(result) == CHECK_CONDITION) {
1713             switch (check_sense(SCpnt)) {
1714             case 0:
1715                 update_timeout(SCpnt, oldto);
1716                 status = REDO;
1717                 break;
1718             case SUGGEST_REMAP:
1719             case SUGGEST_RETRY:
1720                 status = MAYREDO;
1721                 exit = DRIVER_SENSE | SUGGEST_RETRY;
1722                 break;
1723             case SUGGEST_ABORT:
1724                 status = FINISHED;
1725                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
1726                 break;
1727             case SUGGEST_SENSE:
1728                 scsi_request_sense (SCpnt);
1729                 status = PENDING;
1730                 break;
1731             }
1732         } else {
1733             status=REDO;
1734             exit = SUGGEST_RETRY;
1735         }
1736         break;
1737     default :
1738         exit = (DRIVER_ERROR | SUGGEST_DIE);
1739     }
1740     
1741     switch (status)
1742     {
1743     case FINISHED:
1744     case PENDING:
1745         break;
1746     case MAYREDO:
1747 #ifdef DEBUG
1748         printk("In MAYREDO, allowing %d retries, have %d\n",
1749                SCpnt->allowed, SCpnt->retries);
1750 #endif
1751         if ((++SCpnt->retries) < SCpnt->allowed)
1752         {
1753             if ((SCpnt->retries >= (SCpnt->allowed >> 1))
1754                 && !(jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
1755                 && !(SCpnt->flags & WAS_RESET))
1756             {
1757                 printk("scsi%d channel %d : resetting for second half of retries.\n",
1758                        SCpnt->host->host_no, SCpnt->channel);
1759                 scsi_reset(SCpnt, SCSI_RESET_SYNCHRONOUS);
1760                 break;
1761             }
1762             
1763         }
1764         else
1765         {
1766             status = FINISHED;
1767             break;
1768         }
1769         /* fall through to REDO */
1770         
1771     case REDO:
1772         
1773         if (SCpnt->flags & WAS_SENSE)
1774             scsi_request_sense(SCpnt);
1775         else
1776         {
1777             memcpy ((void *) SCpnt->cmnd,
1778                     (void*) SCpnt->data_cmnd,
1779                     sizeof(SCpnt->data_cmnd));
1780             SCpnt->request_buffer = SCpnt->buffer;
1781             SCpnt->request_bufflen = SCpnt->bufflen;
1782             SCpnt->use_sg = SCpnt->old_use_sg;
1783             SCpnt->cmd_len = SCpnt->old_cmd_len;
1784             internal_cmnd (SCpnt);
1785         }
1786         break;
1787     default:
1788         INTERNAL_ERROR;
1789     }
1790     
1791     if (status == FINISHED) {
1792 #ifdef DEBUG
1793         printk("Calling done function - at address %p\n", SCpnt->done);
1794 #endif
1795         host->host_busy--; /* Indicate that we are free */
1796         
1797         if (host->block && host->host_busy == 0) {
1798             host_active = NULL;
1799             
1800             /* For block devices "wake_up" is done in end_scsi_request */
1801             if (MAJOR(SCpnt->request.rq_dev) != SCSI_DISK_MAJOR &&
1802                 MAJOR(SCpnt->request.rq_dev) != SCSI_CDROM_MAJOR) {
1803                 struct Scsi_Host * next;
1804                 
1805                 for (next = host->block; next != host; next = next->block)
1806                     wake_up(&next->host_wait);
1807             }
1808             
1809         }
1810         
1811         wake_up(&host->host_wait);
1812         SCpnt->result = result | ((exit & 0xff) << 24);
1813         SCpnt->use_sg = SCpnt->old_use_sg;
1814         SCpnt->cmd_len = SCpnt->old_cmd_len;
1815         SCpnt->done (SCpnt);
1816     }
1817     
1818 #undef FINISHED
1819 #undef REDO
1820 #undef MAYREDO
1821 #undef PENDING
1822 }
1823 
1824 /*
1825  * The scsi_abort function interfaces with the abort() function of the host
1826  * we are aborting, and causes the current command to not complete.  The
1827  * caller should deal with any error messages or status returned on the
1828  * next call.
1829  * 
1830  * This will not be called reentrantly for a given host.
1831  */
1832 
1833 /*
1834  * Since we're nice guys and specified that abort() and reset()
1835  * can be non-reentrant.  The internal_timeout flags are used for
1836  * this.
1837  */
1838 
1839 
1840 int scsi_abort (Scsi_Cmnd * SCpnt, int why)
     /* [previous][next][first][last][top][bottom][index][help] */
1841 {
1842     int oldto;
1843     unsigned long flags;
1844     struct Scsi_Host * host = SCpnt->host;
1845     
1846     while(1)
1847     {
1848         save_flags(flags);
1849         cli();
1850         
1851         /*
1852          * Protect against races here.  If the command is done, or we are
1853          * on a different command forget it.
1854          */
1855         if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
1856             restore_flags(flags);
1857             return 0;
1858         }
1859 
1860         if (SCpnt->internal_timeout & IN_ABORT)
1861         {
1862             restore_flags(flags);
1863             while (SCpnt->internal_timeout & IN_ABORT)
1864                 barrier();
1865         }
1866         else
1867         {
1868             SCpnt->internal_timeout |= IN_ABORT;
1869             oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
1870             
1871             if ((SCpnt->flags & IS_RESETTING) && SCpnt->device->soft_reset) {
1872                 /* OK, this command must have died when we did the
1873                  *  reset.  The device itself must have lied. 
1874                  */
1875                 printk("Stale command on %d %d:%d appears to have died when"
1876                        " the bus was reset\n", 
1877                        SCpnt->channel, SCpnt->target, SCpnt->lun);
1878             }
1879             
1880             restore_flags(flags);
1881             if (!host->host_busy) {
1882                 SCpnt->internal_timeout &= ~IN_ABORT;
1883                 update_timeout(SCpnt, oldto);
1884                 return 0;
1885             }
1886             printk("scsi : aborting command due to timeout : pid %lu, scsi%d,"
1887                    " channel %d, id %d, lun %d ",
1888                    SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->channel, 
1889                    (int) SCpnt->target, (int) SCpnt->lun);
1890             print_command (SCpnt->cmnd);
1891             if (SCpnt->serial_number != SCpnt->serial_number_at_timeout)
1892                 return 0;
1893             SCpnt->abort_reason = why;
1894             switch(host->hostt->abort(SCpnt)) {
1895                 /* We do not know how to abort.  Try waiting another
1896                  * time increment and see if this helps. Set the
1897                  * WAS_TIMEDOUT flag set so we do not try this twice
1898                  */
1899             case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
1900                                    * this is too severe 
1901                                    */
1902             case SCSI_ABORT_SNOOZE:
1903                 if(why == DID_TIME_OUT) {
1904                     save_flags(flags);
1905                     cli();
1906                     SCpnt->internal_timeout &= ~IN_ABORT;
1907                     if(SCpnt->flags & WAS_TIMEDOUT) {
1908                         restore_flags(flags);
1909                         return 1; /* Indicate we cannot handle this.
1910                                    * We drop down into the reset handler
1911                                    * and try again 
1912                                    */
1913                     } else {
1914                         SCpnt->flags |= WAS_TIMEDOUT;
1915                         oldto = SCpnt->timeout_per_command;
1916                         update_timeout(SCpnt, oldto);
1917                     }
1918                     restore_flags(flags);
1919                 }
1920                 return 0;
1921             case SCSI_ABORT_PENDING:
1922                 if(why != DID_TIME_OUT) {
1923                     save_flags(flags);
1924                     cli();
1925                     update_timeout(SCpnt, oldto);
1926                     restore_flags(flags);
1927                 }
1928                 return 0;
1929             case SCSI_ABORT_SUCCESS:
1930                 /* We should have already aborted this one.  No
1931                  * need to adjust timeout 
1932                  */
1933                  SCpnt->internal_timeout &= ~IN_ABORT;
1934                  return 0;
1935             case SCSI_ABORT_NOT_RUNNING:
1936                 SCpnt->internal_timeout &= ~IN_ABORT;
1937                 update_timeout(SCpnt, 0);
1938                 return 0;
1939             case SCSI_ABORT_ERROR:
1940             default:
1941                 SCpnt->internal_timeout &= ~IN_ABORT;
1942                 return 1;
1943             }
1944         }
1945     }
1946 }
1947 
1948 
1949 /* Mark a single SCSI Device as having been reset. */
1950 
1951 static inline void scsi_mark_device_reset(Scsi_Device *Device)
     /* [previous][next][first][last][top][bottom][index][help] */
1952 {
1953   Device->was_reset = 1;
1954   Device->expecting_cc_ua = 1;
1955 }
1956 
1957 
1958 /* Mark all SCSI Devices on a specific Host as having been reset. */
1959 
1960 void scsi_mark_host_reset(struct Scsi_Host *Host)
     /* [previous][next][first][last][top][bottom][index][help] */
1961 {
1962   Scsi_Cmnd *SCpnt;
1963   for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
1964       scsi_mark_device_reset(SCpnt->device);
1965 }
1966 
1967 
1968 /* Mark all SCSI Devices on a specific Host Bus as having been reset. */
1969 
1970 void scsi_mark_bus_reset(struct Scsi_Host *Host, int channel)
     /* [previous][next][first][last][top][bottom][index][help] */
1971 {
1972   Scsi_Cmnd *SCpnt;
1973   for (SCpnt = Host->host_queue; SCpnt; SCpnt = SCpnt->next)
1974       if (SCpnt->channel == channel)
1975           scsi_mark_device_reset(SCpnt->device);
1976 }
1977 
1978 
1979 int scsi_reset (Scsi_Cmnd * SCpnt, unsigned int reset_flags)
     /* [previous][next][first][last][top][bottom][index][help] */
1980 {
1981     int temp;
1982     unsigned long flags;
1983     Scsi_Cmnd * SCpnt1;
1984     struct Scsi_Host * host = SCpnt->host;
1985 
1986     printk("SCSI bus is being reset for host %d channel %d.\n",
1987            host->host_no, SCpnt->channel);
1988  
1989 #if 0
1990     /*
1991      * First of all, we need to make a recommendation to the low-level
1992      * driver as to whether a BUS_DEVICE_RESET should be performed,
1993      * or whether we should do a full BUS_RESET.  There is no simple
1994      * algorithm here - we basically use a series of heuristics
1995      * to determine what we should do.
1996      */
1997     SCpnt->host->suggest_bus_reset = FALSE;
1998     
1999     /*
2000      * First see if all of the active devices on the bus have
2001      * been jammed up so that we are attempting resets.  If so,
2002      * then suggest a bus reset.  Forcing a bus reset could
2003      * result in some race conditions, but no more than
2004      * you would usually get with timeouts.  We will cross
2005      * that bridge when we come to it.
2006      *
2007      * This is actually a pretty bad idea, since a sequence of
2008      * commands will often timeout together and this will cause a
2009      * Bus Device Reset followed immediately by a SCSI Bus Reset.
2010      * If all of the active devices really are jammed up, the
2011      * Bus Device Reset will quickly timeout and scsi_times_out
2012      * will follow up with a SCSI Bus Reset anyway.
2013      */
2014     SCpnt1 = host->host_queue;
2015     while(SCpnt1) {
2016         if( SCpnt1->request.rq_status != RQ_INACTIVE
2017             && (SCpnt1->flags & (WAS_RESET | IS_RESETTING)) == 0 )
2018                 break;
2019         SCpnt1 = SCpnt1->next;
2020         }
2021     if( SCpnt1 == NULL ) {
2022         reset_flags |= SCSI_RESET_SUGGEST_BUS_RESET;
2023     }
2024     
2025     /*
2026      * If the code that called us is suggesting a hard reset, then
2027      * definitely request it.  This usually occurs because a
2028      * BUS_DEVICE_RESET times out.
2029      *
2030      * Passing reset_flags along takes care of this automatically.
2031      */
2032     if( reset_flags & SCSI_RESET_SUGGEST_BUS_RESET ) {
2033         SCpnt->host->suggest_bus_reset = TRUE;
2034     }
2035 #endif
2036     
2037     while (1) {
2038         save_flags(flags);
2039         cli();
2040 
2041         /*
2042          * Protect against races here.  If the command is done, or we are
2043          * on a different command forget it.
2044          */
2045         if (SCpnt->serial_number != SCpnt->serial_number_at_timeout) {
2046             restore_flags(flags);
2047             return 0;
2048         }
2049 
2050         if (SCpnt->internal_timeout & IN_RESET)
2051         {
2052             restore_flags(flags);
2053             while (SCpnt->internal_timeout & IN_RESET)
2054                 barrier();
2055         }
2056         else
2057         {
2058             SCpnt->internal_timeout |= IN_RESET;
2059             update_timeout(SCpnt, RESET_TIMEOUT);
2060             
2061             if (host->host_busy)
2062             {
2063                 restore_flags(flags);
2064                 SCpnt1 = host->host_queue;
2065                 while(SCpnt1) {
2066                     if (SCpnt1->request.rq_status != RQ_INACTIVE) {
2067 #if 0
2068                         if (!(SCpnt1->flags & IS_RESETTING) &&
2069                             !(SCpnt1->internal_timeout & IN_ABORT))
2070                             scsi_abort(SCpnt1, DID_RESET);
2071 #endif
2072                         SCpnt1->flags |= (WAS_RESET | IS_RESETTING);
2073                     }
2074                     SCpnt1 = SCpnt1->next;
2075                 }
2076                 
2077                 host->last_reset = jiffies;
2078                 temp = host->hostt->reset(SCpnt, reset_flags);
2079                 host->last_reset = jiffies;
2080             }
2081             else
2082             {
2083                 if (!host->block) host->host_busy++;
2084                 restore_flags(flags);
2085                 host->last_reset = jiffies;
2086                 SCpnt->flags |= (WAS_RESET | IS_RESETTING);
2087                 temp = host->hostt->reset(SCpnt, reset_flags);
2088                 host->last_reset = jiffies;
2089                 if (!host->block) host->host_busy--;
2090             }
2091             
2092 #ifdef DEBUG
2093             printk("scsi reset function returned %d\n", temp);
2094 #endif
2095             
2096             /*
2097              * Now figure out what we need to do, based upon
2098              * what the low level driver said that it did.
2099              * If the result is SCSI_RESET_SUCCESS, SCSI_RESET_PENDING,
2100              * or SCSI_RESET_WAKEUP, then the low level driver did a
2101              * bus device reset or bus reset, so we should go through
2102              * and mark one or all of the devices on that bus
2103              * as having been reset.
2104              */
2105             switch(temp & SCSI_RESET_ACTION) {
2106             case SCSI_RESET_SUCCESS:
2107                 if (temp & SCSI_RESET_HOST_RESET)
2108                   scsi_mark_host_reset(host);
2109                 else if (temp & SCSI_RESET_BUS_RESET)
2110                   scsi_mark_bus_reset(host, SCpnt->channel);
2111                 else scsi_mark_device_reset(SCpnt->device);
2112                 save_flags(flags);
2113                 cli();
2114                 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2115                 restore_flags(flags);
2116                 return 0;
2117             case SCSI_RESET_PENDING:
2118                 if (temp & SCSI_RESET_HOST_RESET)
2119                   scsi_mark_host_reset(host);
2120                 else if (temp & SCSI_RESET_BUS_RESET)
2121                   scsi_mark_bus_reset(host, SCpnt->channel);
2122                 else scsi_mark_device_reset(SCpnt->device);
2123             case SCSI_RESET_NOT_RUNNING:
2124                 return 0;
2125             case SCSI_RESET_PUNT:
2126                 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2127                 scsi_request_sense (SCpnt);
2128                 return 0;
2129             case SCSI_RESET_WAKEUP:
2130                 if (temp & SCSI_RESET_HOST_RESET)
2131                   scsi_mark_host_reset(host);
2132                 else if (temp & SCSI_RESET_BUS_RESET)
2133                   scsi_mark_bus_reset(host, SCpnt->channel);
2134                 else scsi_mark_device_reset(SCpnt->device);
2135                 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2136                 scsi_request_sense (SCpnt);
2137                 /*
2138                  * If a bus reset was performed, we
2139                  * need to wake up each and every command
2140                  * that was active on the bus or if it was a HBA
2141                  * reset all active commands on all channels
2142                  */
2143                 if( temp & SCSI_RESET_HOST_RESET )
2144                 {
2145                     SCpnt1 = host->host_queue;
2146                     while(SCpnt1) {
2147                         if (SCpnt1->request.rq_status != RQ_INACTIVE
2148                             && SCpnt1 != SCpnt)
2149                             scsi_request_sense (SCpnt1);
2150                         SCpnt1 = SCpnt1->next;
2151                     }
2152                 } else if( temp & SCSI_RESET_BUS_RESET ) {
2153                     SCpnt1 = host->host_queue;
2154                     while(SCpnt1) {
2155                         if(SCpnt1->request.rq_status != RQ_INACTIVE
2156                            && SCpnt1 != SCpnt 
2157                            && SCpnt1->channel == SCpnt->channel)
2158                             scsi_request_sense (SCpnt);
2159                         SCpnt1 = SCpnt1->next;
2160                     }
2161                 }
2162                 return 0;
2163             case SCSI_RESET_SNOOZE:
2164                 /* In this case, we set the timeout field to 0
2165                  * so that this command does not time out any more,
2166                  * and we return 1 so that we get a message on the
2167                  * screen. 
2168                  */
2169                 save_flags(flags);
2170                 cli();
2171                 SCpnt->internal_timeout &= ~(IN_RESET|IN_RESET2|IN_RESET3);
2172                 update_timeout(SCpnt, 0);
2173                 restore_flags(flags);
2174                 /* If you snooze, you lose... */
2175             case SCSI_RESET_ERROR:
2176             default:
2177                 return 1;
2178             }
2179             
2180             return temp;
2181         }
2182     }
2183 }
2184 
2185 
2186 static void scsi_main_timeout(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2187 {
2188     /*
2189      * We must not enter update_timeout with a timeout condition still pending.
2190      */
2191     
2192     int timed_out;
2193     unsigned long flags;
2194     struct Scsi_Host * host;
2195     Scsi_Cmnd * SCpnt = NULL;
2196     
2197     save_flags(flags);
2198     cli();
2199 
2200     update_timeout(NULL, 0);
2201 
2202     /*
2203      * Find all timers such that they have 0 or negative (shouldn't happen)
2204      * time remaining on them.
2205      */
2206     timed_out = 0;
2207     for (host = scsi_hostlist; host; host = host->next) {
2208         for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2209             if (SCpnt->timeout == -1)
2210               {
2211                 SCpnt->timeout = 0;
2212                 SCpnt->serial_number_at_timeout = SCpnt->serial_number;
2213                 ++timed_out;
2214               }
2215     }
2216     if (timed_out > 0) {
2217         for (host = scsi_hostlist; host; host = host->next) {
2218             for (SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2219                 if (SCpnt->serial_number_at_timeout > 0 &&
2220                     SCpnt->serial_number_at_timeout == SCpnt->serial_number)
2221                   {
2222                     restore_flags(flags);
2223                     scsi_times_out(SCpnt);
2224                     SCpnt->serial_number_at_timeout = 0;
2225                     cli();
2226                   }
2227           }
2228     }
2229     restore_flags(flags);
2230 }
2231 
2232 /*
2233  * The strategy is to cause the timer code to call scsi_times_out()
2234  * when the soonest timeout is pending.
2235  * The arguments are used when we are queueing a new command, because
2236  * we do not want to subtract the time used from this time, but when we
2237  * set the timer, we want to take this value into account.
2238  */
2239 
2240 static int update_timeout(Scsi_Cmnd * SCset, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
2241 {
2242     unsigned int least, used;
2243     unsigned int oldto;
2244     unsigned long flags;
2245     struct Scsi_Host * host;
2246     Scsi_Cmnd * SCpnt = NULL;
2247 
2248     save_flags(flags);
2249     cli();
2250 
2251     oldto = 0;
2252 
2253     /*
2254      * This routine can be a performance bottleneck under high loads, since
2255      * it is called twice per SCSI operation: once when internal_cmnd is
2256      * called, and again when scsi_done completes the command.  To limit
2257      * the load this routine can cause, we shortcut processing if no clock
2258      * ticks have occurred since the last time it was called.  This may
2259      * cause the computation of least below to be inaccurate, but it will
2260      * be corrected after the next clock tick.
2261      */
2262 
2263     if (jiffies == time_start && timer_table[SCSI_TIMER].expires > 0) {
2264         if(SCset){
2265             oldto = SCset->timeout;
2266             SCset->timeout = timeout;
2267         }
2268         restore_flags(flags);
2269         return oldto;
2270     }
2271 
2272     /*
2273      * Figure out how much time has passed since the last time the timeouts
2274      * were updated
2275      */
2276     used = (time_start) ? (jiffies - time_start) : 0;
2277 
2278     /*
2279      * Find out what is due to timeout soonest, and adjust all timeouts for
2280      * the amount of time that has passed since the last time we called
2281      * update_timeout.
2282      */
2283 
2284     oldto = 0;
2285     
2286     if(SCset){
2287         oldto = SCset->timeout - used;
2288         SCset->timeout = timeout;
2289     }
2290 
2291     least = 0xffffffff;
2292     
2293     for(host = scsi_hostlist; host; host = host->next)
2294         for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
2295             if (SCpnt->timeout > 0) {
2296                 if (SCpnt != SCset)
2297                     SCpnt->timeout -= used;
2298                 if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
2299                 if(SCpnt->timeout > 0 && SCpnt->timeout < least)
2300                     least = SCpnt->timeout;
2301             }
2302     
2303     /*
2304      * If something is due to timeout again, then we will set the next timeout
2305      * interrupt to occur.  Otherwise, timeouts are disabled.
2306      */
2307     
2308     if (least != 0xffffffff)
2309     {
2310         time_start = jiffies;
2311         timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
2312         timer_active |= 1 << SCSI_TIMER;
2313     }
2314     else
2315     {
2316         timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
2317         timer_active &= ~(1 << SCSI_TIMER);
2318     }
2319     restore_flags(flags);
2320     return oldto;
2321 }
2322 
2323 #ifdef CONFIG_MODULES
2324 static int scsi_register_host(Scsi_Host_Template *);
2325 static void scsi_unregister_host(Scsi_Host_Template *);
2326 #endif
2327 
2328 void *scsi_malloc(unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2329 {
2330     unsigned int nbits, mask;
2331     unsigned long flags;
2332     int i, j;
2333     if(len % SECTOR_SIZE != 0 || len > PAGE_SIZE)
2334         return NULL;
2335     
2336     save_flags(flags);
2337     cli();
2338     nbits = len >> 9;
2339     mask = (1 << nbits) - 1;
2340     
2341     for(i=0;i < dma_sectors / SECTORS_PER_PAGE; i++)
2342         for(j=0; j<=SECTORS_PER_PAGE - nbits; j++){
2343             if ((dma_malloc_freelist[i] & (mask << j)) == 0){
2344                 dma_malloc_freelist[i] |= (mask << j);
2345                 restore_flags(flags);
2346                 dma_free_sectors -= nbits;
2347 #ifdef DEBUG
2348                 printk("SMalloc: %d %p\n",len, dma_malloc_pages[i] + (j << 9));
2349 #endif
2350                 return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
2351             }
2352         }
2353     restore_flags(flags);
2354     return NULL;  /* Nope.  No more */
2355 }
2356 
2357 int scsi_free(void *obj, unsigned int len)
     /* [previous][next][first][last][top][bottom][index][help] */
2358 {
2359     unsigned int page, sector, nbits, mask;
2360     unsigned long flags;
2361     
2362 #ifdef DEBUG
2363     printk("scsi_free %p %d\n",obj, len);
2364 #endif
2365     
2366     for (page = 0; page < dma_sectors / SECTORS_PER_PAGE; page++) {
2367         unsigned long page_addr = (unsigned long) dma_malloc_pages[page];
2368         if ((unsigned long) obj >= page_addr &&
2369             (unsigned long) obj <  page_addr + PAGE_SIZE)
2370         {
2371             sector = (((unsigned long) obj) - page_addr) >> 9;
2372 
2373             nbits = len >> 9;
2374             mask = (1 << nbits) - 1;
2375 
2376             if ((mask << sector) >= (1 << SECTORS_PER_PAGE))
2377                 panic ("scsi_free:Bad memory alignment");
2378 
2379             save_flags(flags);
2380             cli();
2381             if((dma_malloc_freelist[page] & (mask << sector)) != (mask<<sector))
2382                 panic("scsi_free:Trying to free unused memory");
2383 
2384             dma_free_sectors += nbits;
2385             dma_malloc_freelist[page] &= ~(mask << sector);
2386             restore_flags(flags);
2387             return 0;
2388         }
2389     }
2390     panic("scsi_free:Bad offset");
2391 }
2392 
2393 
2394 int scsi_loadable_module_flag; /* Set after we scan builtin drivers */
2395 
2396 void * scsi_init_malloc(unsigned int size, int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
2397 {
2398     void * retval;
2399     
2400     /*
2401      * For buffers used by the DMA pool, we assume page aligned 
2402      * structures.
2403      */
2404     if ((size % PAGE_SIZE) == 0) {
2405         int order, a_size;
2406         for (order = 0, a_size = PAGE_SIZE;
2407              a_size < size; order++, a_size <<= 1)
2408             ;
2409         retval = (void *) __get_dma_pages(priority & GFP_LEVEL_MASK,
2410                                                     order);
2411     } else
2412         retval = kmalloc(size, priority);
2413 
2414     if (retval)
2415         memset(retval, 0, size);
2416     return retval;
2417 }
2418 
2419 
2420 void scsi_init_free(char * ptr, unsigned int size)
     /* [previous][next][first][last][top][bottom][index][help] */
2421 { 
2422     /*
2423      * We need this special code here because the DMA pool assumes
2424      * page aligned data.  Besides, it is wasteful to allocate
2425      * page sized chunks with kmalloc.
2426      */
2427     if ((size % PAGE_SIZE) == 0) {
2428         int order, a_size;
2429 
2430         for (order = 0, a_size = PAGE_SIZE;
2431              a_size < size; order++, a_size <<= 1)
2432             ;
2433         free_pages((unsigned long)ptr, order);
2434     } else
2435         kfree(ptr);
2436 }
2437 
2438 void scsi_build_commandblocks(Scsi_Device * SDpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2439 {
2440     struct Scsi_Host *host = SDpnt->host;
2441     int j;
2442     Scsi_Cmnd * SCpnt;                 
2443 
2444     if (SDpnt->queue_depth == 0)
2445         SDpnt->queue_depth = host->cmd_per_lun;
2446     SDpnt->device_queue = NULL;
2447  
2448     for(j=0;j<SDpnt->queue_depth;j++){
2449       SCpnt = (Scsi_Cmnd *)
2450               scsi_init_malloc(sizeof(Scsi_Cmnd),
2451                                GFP_ATOMIC |
2452                                (host->unchecked_isa_dma ? GFP_DMA : 0));
2453         SCpnt->host = host;
2454         SCpnt->device = SDpnt;
2455         SCpnt->target = SDpnt->id;
2456         SCpnt->lun = SDpnt->lun;
2457         SCpnt->channel = SDpnt->channel;
2458         SCpnt->request.rq_status = RQ_INACTIVE;
2459         SCpnt->use_sg = 0;
2460         SCpnt->old_use_sg = 0;
2461         SCpnt->old_cmd_len = 0;
2462         SCpnt->timeout = 0;
2463         SCpnt->underflow = 0;
2464         SCpnt->transfersize = 0;
2465         SCpnt->serial_number = 0;
2466         SCpnt->serial_number_at_timeout = 0;
2467         SCpnt->host_scribble = NULL;
2468         if(host->host_queue)
2469             host->host_queue->prev = SCpnt;
2470         SCpnt->next = host->host_queue;
2471         SCpnt->prev = NULL;
2472         host->host_queue = SCpnt;
2473         SCpnt->device_next = SDpnt->device_queue;
2474         SDpnt->device_queue = SCpnt;
2475     }
2476     SDpnt->has_cmdblocks = 1;
2477 }
2478 
2479 /*
2480  * scsi_dev_init() is our initialization routine, which in turn calls host
2481  * initialization, bus scanning, and sd/st initialization routines. 
2482  */
2483 
2484 int scsi_dev_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2485 {
2486     Scsi_Device * SDpnt;
2487     struct Scsi_Host * shpnt;
2488     struct Scsi_Device_Template * sdtpnt;
2489 #ifdef FOO_ON_YOU
2490     return;
2491 #endif
2492 
2493     /* Yes we're here... */
2494     dispatch_scsi_info_ptr = dispatch_scsi_info;
2495 
2496     /* Init a few things so we can "malloc" memory. */
2497     scsi_loadable_module_flag = 0;
2498     
2499     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
2500     timer_table[SCSI_TIMER].expires = 0;
2501 
2502 #ifdef CONFIG_MODULES
2503     register_symtab(&scsi_symbol_table);
2504 #endif    
2505 
2506     /* Register the /proc/scsi/scsi entry */
2507 #if CONFIG_PROC_FS 
2508     proc_scsi_register(0, &proc_scsi_scsi);    
2509 #endif
2510 
2511     /* initialize all hosts */
2512     scsi_init();
2513 
2514     scsi_devices = (Scsi_Device *) NULL;
2515 
2516     for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2517         scan_scsis(shpnt,0,0,0,0);           /* scan for scsi devices */
2518         if (shpnt->select_queue_depths != NULL)
2519             (shpnt->select_queue_depths)(shpnt, scsi_devices);
2520     }
2521 
2522     printk("scsi : detected ");
2523     for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2524         if (sdtpnt->dev_noticed && sdtpnt->name)
2525             printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
2526                    (sdtpnt->dev_noticed != 1) ? "s" : "");
2527     printk("total.\n");
2528     
2529     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2530         if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2531 
2532     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2533         SDpnt->scsi_request_fn = NULL;
2534         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2535             if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2536         if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2537     }
2538     
2539 
2540     /*
2541      * This should build the DMA pool.
2542      */
2543     resize_dma_pool();
2544 
2545     /*
2546      * OK, now we finish the initialization by doing spin-up, read
2547      * capacity, etc, etc 
2548      */
2549     for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2550         if(sdtpnt->finish && sdtpnt->nr_dev)
2551             (*sdtpnt->finish)();
2552 
2553     scsi_loadable_module_flag = 1;
2554 
2555     return 0;
2556 }
2557 
2558 static void print_inquiry(unsigned char *data)
     /* [previous][next][first][last][top][bottom][index][help] */
2559 {
2560     int i;
2561     
2562     printk("  Vendor: ");
2563     for (i = 8; i < 16; i++)
2564     {
2565         if (data[i] >= 0x20 && i < data[4] + 5)
2566             printk("%c", data[i]);
2567         else
2568             printk(" ");
2569     }
2570     
2571     printk("  Model: ");
2572     for (i = 16; i < 32; i++)
2573     {
2574         if (data[i] >= 0x20 && i < data[4] + 5)
2575             printk("%c", data[i]);
2576         else
2577             printk(" ");
2578     }
2579     
2580     printk("  Rev: ");
2581     for (i = 32; i < 36; i++)
2582     {
2583         if (data[i] >= 0x20 && i < data[4] + 5)
2584             printk("%c", data[i]);
2585         else
2586             printk(" ");
2587     }
2588     
2589     printk("\n");
2590     
2591     i = data[0] & 0x1f;
2592     
2593     printk("  Type:   %s ",
2594            i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
2595     printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
2596     if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
2597         printk(" CCS\n");
2598     else
2599         printk("\n");
2600 }
2601 
2602 
2603 #ifdef CONFIG_PROC_FS
2604 int scsi_proc_info(char *buffer, char **start, off_t offset, int length, 
     /* [previous][next][first][last][top][bottom][index][help] */
2605                     int hostno, int inout)
2606 {
2607     Scsi_Cmnd *SCpnt;
2608     struct Scsi_Device_Template *SDTpnt;
2609     Scsi_Device *scd, *scd_h = NULL;
2610     struct Scsi_Host *HBA_ptr;
2611     char *p;
2612     int   host, channel, id, lun;
2613     int   size, len = 0;
2614     off_t begin = 0;
2615     off_t pos = 0;
2616 
2617     scd = scsi_devices;
2618     HBA_ptr = scsi_hostlist;
2619 
2620     if(inout == 0) { 
2621         size = sprintf(buffer+len,"Attached devices: %s\n", (scd)?"":"none");
2622         len += size; 
2623         pos = begin + len;
2624         while (HBA_ptr) {
2625 #if 0
2626             size += sprintf(buffer+len,"scsi%2d: %s\n", (int) HBA_ptr->host_no, 
2627                             HBA_ptr->hostt->procname);
2628             len += size; 
2629             pos = begin + len;
2630 #endif
2631             scd = scsi_devices;
2632             while (scd) {
2633                 if (scd->host == HBA_ptr) {
2634                     proc_print_scsidevice(scd, buffer, &size, len);
2635                     len += size; 
2636                     pos = begin + len;
2637                     
2638                     if (pos < offset) {
2639                         len = 0;
2640                         begin = pos;
2641                     }
2642                     if (pos > offset + length)
2643                         goto stop_output;
2644                 }
2645                 scd = scd->next;
2646             }
2647             HBA_ptr = HBA_ptr->next;
2648         }
2649         
2650     stop_output:
2651         *start=buffer+(offset-begin);   /* Start of wanted data */
2652         len-=(offset-begin);            /* Start slop */
2653         if(len>length)
2654             len = length;               /* Ending slop */
2655         return (len);     
2656     }
2657 
2658     if(!buffer || length < 25 || strncmp("scsi", buffer, 4))
2659         return(-EINVAL);
2660 
2661     /*
2662      * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
2663      * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
2664      * Consider this feature BETA.
2665      *     CAUTION: This is not for hotplugging your peripherals. As
2666      *     SCSI was not designed for this you could damage your
2667      *     hardware !  
2668      * However perhaps it is legal to switch on an
2669      * already connected device. It is perhaps not 
2670      * guaranteed this device doesn't corrupt an ongoing data transfer.
2671      */
2672     if(!strncmp("add-single-device", buffer + 5, 17)) {
2673         p = buffer + 23;
2674 
2675         host    = simple_strtoul(p, &p, 0);
2676         channel = simple_strtoul(p+1, &p, 0);
2677         id      = simple_strtoul(p+1, &p, 0);
2678         lun     = simple_strtoul(p+1, &p, 0);
2679 
2680         printk("scsi singledevice %d %d %d %d\n", host, channel,
2681                         id, lun);
2682 
2683         while(scd && (scd->host->host_no != host 
2684               || scd->channel != channel 
2685               || scd->id != id 
2686               || scd->lun != lun)) {
2687             scd = scd->next;
2688         }
2689         if(scd)
2690             return(-ENOSYS);  /* We do not yet support unplugging */
2691         while(HBA_ptr && HBA_ptr->host_no != host)
2692             HBA_ptr = HBA_ptr->next;
2693 
2694         if(!HBA_ptr)
2695             return(-ENXIO);
2696 
2697         scan_scsis (HBA_ptr, 1, channel, id, lun);
2698         return(length);
2699         
2700     } 
2701 
2702     /*
2703      * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
2704      * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
2705      *
2706      * Consider this feature pre-BETA.
2707      *
2708      *     CAUTION: This is not for hotplugging your peripherals. As
2709      *     SCSI was not designed for this you could damage your
2710      *     hardware and thoroughly confuse the SCSI subsystem.  
2711      *
2712      */
2713     else if(!strncmp("remove-single-device", buffer + 5, 20)) {
2714         p = buffer + 26;
2715         
2716         host    = simple_strtoul(p, &p, 0);
2717         channel = simple_strtoul(p+1, &p, 0);
2718         id      = simple_strtoul(p+1, &p, 0);
2719         lun     = simple_strtoul(p+1, &p, 0);
2720         
2721         while(scd != NULL) {
2722             if(scd->host->host_no == host  
2723                && scd->channel == channel 
2724                && scd->id == id 
2725                && scd->lun == lun){
2726                 break;                
2727             }
2728             scd_h = scd;
2729             scd = scd->next;
2730         }
2731         
2732         if(scd == NULL)
2733             return(-ENODEV);  /* there is no such device attached */
2734         
2735         if(scd->access_count)
2736             return(-EBUSY);
2737         
2738         SDTpnt = scsi_devicelist;
2739         while(SDTpnt != NULL) {
2740             if(SDTpnt->detach) (*SDTpnt->detach)(scd);
2741             SDTpnt = SDTpnt->next;
2742         }
2743         
2744         if(scd->attached == 0) {
2745             /*
2746              * Nobody is using this device any more.
2747              * Free all of the command structures.
2748              */
2749             for(SCpnt=scd->host->host_queue; SCpnt; SCpnt = SCpnt->next){
2750                 if(SCpnt->device == scd) {
2751                     if(SCpnt->prev != NULL)
2752                         SCpnt->prev->next = SCpnt->next;
2753                     if(SCpnt->next != NULL)
2754                         SCpnt->next->prev = SCpnt->prev;
2755                     if(SCpnt == scd->host->host_queue)
2756                         scd->host->host_queue = SCpnt->next;
2757                     scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
2758                 }
2759             }
2760             /* Now we can remove the device structure */
2761             if(scd_h != NULL) {
2762                 scd_h->next = scd->next;
2763             } else if (scsi_devices == scd) {
2764                 /* We had a hit on the first entry of the device list */
2765                 scsi_devices = scd->next;
2766             } 
2767             scsi_init_free((char *) scd, sizeof(Scsi_Device));
2768         } else {
2769             return(-EBUSY);
2770         }
2771         return(0);
2772     }
2773     return(-EINVAL);
2774 }
2775 #endif
2776 
2777 /*
2778  * Go through the device list and recompute the most appropriate size
2779  * for the dma pool.  Then grab more memory (as required).
2780  */
2781 static void resize_dma_pool(void)
     /* [previous][next][first][last][top][bottom][index][help] */
2782 {
2783     int i;
2784     unsigned long size;
2785     struct Scsi_Host * shpnt;
2786     struct Scsi_Host * host = NULL;
2787     Scsi_Device * SDpnt;
2788     unsigned long flags;
2789     FreeSectorBitmap * new_dma_malloc_freelist = NULL;
2790     unsigned int new_dma_sectors = 0;
2791     unsigned int new_need_isa_buffer = 0;
2792     unsigned char ** new_dma_malloc_pages = NULL;
2793 
2794     if( !scsi_devices )
2795     {
2796         /*
2797          * Free up the DMA pool.
2798          */
2799         if( dma_free_sectors != dma_sectors )
2800             panic("SCSI DMA pool memory leak %d %d\n",dma_free_sectors,dma_sectors);
2801 
2802         for(i=0; i < dma_sectors / SECTORS_PER_PAGE; i++)
2803             scsi_init_free(dma_malloc_pages[i], PAGE_SIZE);
2804         if (dma_malloc_pages)
2805             scsi_init_free((char *) dma_malloc_pages,
2806                            (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages));
2807         dma_malloc_pages = NULL;
2808         if (dma_malloc_freelist)
2809             scsi_init_free((char *) dma_malloc_freelist,
2810                            (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_freelist));
2811         dma_malloc_freelist = NULL;
2812         dma_sectors = 0;
2813         dma_free_sectors = 0;
2814         return;
2815     }
2816     /* Next, check to see if we need to extend the DMA buffer pool */
2817         
2818     new_dma_sectors = 2*SECTORS_PER_PAGE;               /* Base value we use */
2819 
2820     if (high_memory-1 > ISA_DMA_THRESHOLD)
2821         scsi_need_isa_bounce_buffers = 1;
2822     else
2823         scsi_need_isa_bounce_buffers = 0;
2824     
2825     if (scsi_devicelist)
2826         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2827             new_dma_sectors += SECTORS_PER_PAGE;        /* Increment for each host */
2828     
2829     for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
2830         host = SDpnt->host;
2831 
2832         if(SDpnt->type != TYPE_TAPE)
2833             new_dma_sectors += ((host->sg_tablesize *
2834                                  sizeof(struct scatterlist) + 511) >> 9) *
2835                                SDpnt->queue_depth;
2836         
2837         if(host->unchecked_isa_dma &&
2838            scsi_need_isa_bounce_buffers &&
2839            SDpnt->type != TYPE_TAPE) {
2840             new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
2841                                                   SDpnt->queue_depth;
2842             new_need_isa_buffer++;
2843         }
2844     }
2845     
2846     /* limit DMA memory to 32MB: */
2847     new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;
2848     
2849     /*
2850      * We never shrink the buffers - this leads to
2851      * race conditions that I would rather not even think
2852      * about right now.
2853      */
2854     if( new_dma_sectors < dma_sectors )
2855         new_dma_sectors = dma_sectors;
2856     
2857     if (new_dma_sectors)
2858     {
2859         size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
2860         new_dma_malloc_freelist = (FreeSectorBitmap *) scsi_init_malloc(size, GFP_ATOMIC);
2861         memset(new_dma_malloc_freelist, 0, size);
2862 
2863         size = (new_dma_sectors / SECTORS_PER_PAGE)*sizeof(*new_dma_malloc_pages);
2864         new_dma_malloc_pages = (unsigned char **) scsi_init_malloc(size, GFP_ATOMIC);
2865         memset(new_dma_malloc_pages, 0, size);
2866     }
2867     
2868     /*
2869      * If we need more buffers, expand the list.
2870      */
2871     if( new_dma_sectors > dma_sectors ) { 
2872         for(i=dma_sectors / SECTORS_PER_PAGE; i< new_dma_sectors / SECTORS_PER_PAGE; i++)
2873             new_dma_malloc_pages[i] = (unsigned char *)
2874                 scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
2875     }
2876     
2877     /* When we dick with the actual DMA list, we need to 
2878      * protect things 
2879      */
2880     save_flags(flags);
2881     cli();
2882     if (dma_malloc_freelist)
2883     {
2884         size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
2885         memcpy(new_dma_malloc_freelist, dma_malloc_freelist, size);
2886         scsi_init_free((char *) dma_malloc_freelist, size);
2887     }
2888     dma_malloc_freelist = new_dma_malloc_freelist;
2889     
2890     if (dma_malloc_pages)
2891     {
2892         size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages);
2893         memcpy(new_dma_malloc_pages, dma_malloc_pages, size);
2894         scsi_init_free((char *) dma_malloc_pages, size);
2895     }
2896     
2897     dma_free_sectors += new_dma_sectors - dma_sectors;
2898     dma_malloc_pages = new_dma_malloc_pages;
2899     dma_sectors = new_dma_sectors;
2900     need_isa_buffer = new_need_isa_buffer;
2901     restore_flags(flags);
2902 }
2903 
2904 #ifdef CONFIG_MODULES           /* a big #ifdef block... */
2905 
2906 /*
2907  * This entry point should be called by a loadable module if it is trying
2908  * add a low level scsi driver to the system.
2909  */
2910 static int scsi_register_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
2911 {
2912     int pcount;
2913     struct Scsi_Host * shpnt;
2914     Scsi_Device * SDpnt;
2915     struct Scsi_Device_Template * sdtpnt;
2916     const char * name;
2917     
2918     if (tpnt->next || !tpnt->detect) return 1;/* Must be already loaded, or
2919                                                * no detect routine available 
2920                                                */
2921     pcount = next_scsi_host;
2922     if ((tpnt->present = tpnt->detect(tpnt)))
2923     {
2924         if(pcount == next_scsi_host) {
2925             if(tpnt->present > 1) {
2926                 printk("Failure to register low-level scsi driver");
2927                 scsi_unregister_host(tpnt);
2928                 return 1;
2929             }
2930             /* The low-level driver failed to register a driver.  We
2931              *  can do this now. 
2932              */
2933             scsi_register(tpnt,0);
2934         }
2935         tpnt->next = scsi_hosts; /* Add to the linked list */
2936         scsi_hosts = tpnt;
2937         
2938         /* Add the new driver to /proc/scsi */
2939 #if CONFIG_PROC_FS 
2940         build_proc_dir_entries(tpnt);
2941 #endif
2942         
2943         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2944             if(shpnt->hostt == tpnt)
2945             {
2946                 if(tpnt->info)
2947                     name = tpnt->info(shpnt);
2948                 else
2949                     name = tpnt->name;
2950                 printk ("scsi%d : %s\n", /* And print a little message */
2951                         shpnt->host_no, name);
2952             }
2953         
2954         printk ("scsi : %d host%s.\n", next_scsi_host,
2955                 (next_scsi_host == 1) ? "" : "s");
2956         
2957         scsi_make_blocked_list();
2958         
2959         /* The next step is to call scan_scsis here.  This generates the
2960          * Scsi_Devices entries 
2961          */
2962         
2963         for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
2964             if(shpnt->hostt == tpnt) scan_scsis(shpnt,0,0,0,0);
2965         
2966         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2967             if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();
2968         
2969         /* Next we create the Scsi_Cmnd structures for this host */
2970         
2971         for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
2972             if(SDpnt->host->hostt == tpnt)
2973             {
2974                 for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2975                     if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
2976                 if(SDpnt->attached) scsi_build_commandblocks(SDpnt);
2977             }
2978         
2979         /*
2980          * Now that we have all of the devices, resize the DMA pool,
2981          * as required.  */
2982         resize_dma_pool();
2983 
2984 
2985         /* This does any final handling that is required. */
2986         for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2987             if(sdtpnt->finish && sdtpnt->nr_dev)
2988                 (*sdtpnt->finish)();
2989     }
2990     
2991 #if defined(USE_STATIC_SCSI_MEMORY)
2992     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2993             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2994             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2995             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2996 #endif
2997         
2998     MOD_INC_USE_COUNT;
2999     return 0;
3000 }
3001 
3002 /*
3003  * Similarly, this entry point should be called by a loadable module if it
3004  * is trying to remove a low level scsi driver from the system.
3005  */
3006 static void scsi_unregister_host(Scsi_Host_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
3007 {
3008     Scsi_Host_Template * SHT, *SHTp;
3009     Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
3010     Scsi_Cmnd * SCpnt;
3011     unsigned long flags;
3012     struct Scsi_Device_Template * sdtpnt;
3013     struct Scsi_Host * shpnt, *sh1;
3014     int pcount;
3015     
3016     /* First verify that this host adapter is completely free with no pending
3017      * commands */
3018     
3019     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3020         if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
3021            && *sdpnt->host->hostt->usage_count) return;
3022     
3023     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3024     {
3025         if (shpnt->hostt != tpnt) continue;
3026         for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3027         {
3028             save_flags(flags);
3029             cli();
3030             if(SCpnt->request.rq_status != RQ_INACTIVE) {
3031                 restore_flags(flags);
3032                 for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3033                     if(SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
3034                         SCpnt->request.rq_status = RQ_INACTIVE;
3035                 printk("Device busy???\n");
3036                 return;
3037             }
3038             SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;  /* Mark as busy */
3039             restore_flags(flags);
3040         }
3041     }
3042     /* Next we detach the high level drivers from the Scsi_Device structures */
3043     
3044     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3045         if(sdpnt->host->hostt == tpnt)
3046         {
3047             for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
3048                 if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
3049             /* If something still attached, punt */
3050             if (sdpnt->attached) {
3051                 printk("Attached usage count = %d\n", sdpnt->attached);
3052                 return;
3053             }
3054         }
3055     
3056     /* Next we free up the Scsi_Cmnd structures for this host */
3057     
3058     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
3059         if(sdpnt->host->hostt == tpnt)
3060             while (sdpnt->host->host_queue) {
3061                 SCpnt = sdpnt->host->host_queue->next;
3062                 scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
3063                 sdpnt->host->host_queue = SCpnt;
3064                 if (SCpnt) SCpnt->prev = NULL;
3065                 sdpnt->has_cmdblocks = 0;
3066             }
3067     
3068     /* Next free up the Scsi_Device structures for this host */
3069     
3070     sdppnt = NULL;
3071     for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
3072     {
3073         sdpnt1 = sdpnt->next;
3074         if (sdpnt->host->hostt == tpnt) {
3075             if (sdppnt)
3076                 sdppnt->next = sdpnt->next;
3077             else
3078                 scsi_devices = sdpnt->next;
3079             scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
3080         } else
3081             sdppnt = sdpnt;
3082     }
3083     
3084     /* Next we go through and remove the instances of the individual hosts
3085      * that were detected */
3086     
3087     shpnt = scsi_hostlist;
3088     while(shpnt) {
3089         sh1 = shpnt->next;
3090         if(shpnt->hostt == tpnt) {
3091             if(shpnt->loaded_as_module) {
3092                 pcount = next_scsi_host;
3093                 /* Remove the /proc/scsi directory entry */
3094 #if CONFIG_PROC_FS 
3095                 proc_scsi_unregister(tpnt->proc_dir, 
3096                                      shpnt->host_no + PROC_SCSI_FILE);
3097 #endif   
3098                 if(tpnt->release)
3099                     (*tpnt->release)(shpnt);
3100                 else {
3101                     /* This is the default case for the release function.  
3102                      * It should do the right thing for most correctly 
3103                      * written host adapters. 
3104                      */
3105                     if (shpnt->irq) free_irq(shpnt->irq, NULL);
3106                     if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
3107                     if (shpnt->io_port && shpnt->n_io_port)
3108                         release_region(shpnt->io_port, shpnt->n_io_port);
3109                 }
3110                 if(pcount == next_scsi_host) scsi_unregister(shpnt);
3111                 tpnt->present--;
3112             }
3113         }
3114         shpnt = sh1;
3115     }
3116     
3117     /*
3118      * If there are absolutely no more hosts left, it is safe
3119      * to completely nuke the DMA pool.  The resize operation will
3120      * do the right thing and free everything.
3121      */
3122     if( !scsi_devices )
3123         resize_dma_pool();
3124 
3125     printk ("scsi : %d host%s.\n", next_scsi_host,
3126             (next_scsi_host == 1) ? "" : "s");
3127     
3128 #if defined(USE_STATIC_SCSI_MEMORY)
3129     printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
3130             (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
3131             (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
3132             (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
3133 #endif
3134     
3135     scsi_make_blocked_list();
3136     
3137     /* There were some hosts that were loaded at boot time, so we cannot
3138        do any more than this */
3139     if (tpnt->present) return;
3140     
3141     /* OK, this is the very last step.  Remove this host adapter from the
3142        linked list. */
3143     for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
3144         if(SHT == tpnt) {
3145             if(SHTp)
3146                 SHTp->next = SHT->next;
3147             else
3148                 scsi_hosts = SHT->next;
3149             SHT->next = NULL;
3150             break;
3151         }
3152     
3153     /* Rebuild the /proc/scsi directory entries */
3154 #if CONFIG_PROC_FS 
3155     proc_scsi_unregister(tpnt->proc_dir, tpnt->proc_dir->low_ino);
3156 #endif
3157     MOD_DEC_USE_COUNT;
3158 }
3159 
3160 /*
3161  * This entry point should be called by a loadable module if it is trying
3162  * add a high level scsi driver to the system.
3163  */
3164 static int scsi_register_device_module(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
3165 {
3166     Scsi_Device * SDpnt;
3167     
3168     if (tpnt->next) return 1;
3169     
3170     scsi_register_device(tpnt);
3171     /*
3172      * First scan the devices that we know about, and see if we notice them.
3173      */
3174     
3175     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3176         if(tpnt->detect) SDpnt->attached += (*tpnt->detect)(SDpnt);
3177     
3178     /*
3179      * If any of the devices would match this driver, then perform the
3180      * init function.
3181      */
3182     if(tpnt->init && tpnt->dev_noticed)
3183         if ((*tpnt->init)()) return 1;
3184     
3185     /*
3186      * Now actually connect the devices to the new driver.
3187      */
3188     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3189     {
3190         if(tpnt->attach)  (*tpnt->attach)(SDpnt);
3191         /*
3192          * If this driver attached to the device, and we no longer
3193          * have anything attached, release the scsi command blocks.
3194          */
3195         if(SDpnt->attached && SDpnt->has_cmdblocks == 0)
3196             scsi_build_commandblocks(SDpnt);
3197     }
3198     
3199     /*
3200      * This does any final handling that is required. 
3201      */
3202     if(tpnt->finish && tpnt->nr_dev)  (*tpnt->finish)();
3203     MOD_INC_USE_COUNT;
3204     return 0;
3205 }
3206 
3207 static int scsi_unregister_device(struct Scsi_Device_Template * tpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
3208 {
3209     Scsi_Device * SDpnt;
3210     Scsi_Cmnd * SCpnt;
3211     struct Scsi_Device_Template * spnt;
3212     struct Scsi_Device_Template * prev_spnt;
3213     
3214     /*
3215      * If we are busy, this is not going to fly.
3216      */
3217     if( *tpnt->usage_count != 0) return 0;
3218     /*
3219      * Next, detach the devices from the driver.
3220      */
3221     
3222     for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
3223     {
3224         if(tpnt->detach) (*tpnt->detach)(SDpnt);
3225         if(SDpnt->attached == 0)
3226         {
3227             /*
3228              * Nobody is using this device any more.  Free all of the
3229              * command structures.
3230              */
3231             for(SCpnt = SDpnt->host->host_queue; SCpnt; SCpnt = SCpnt->next)
3232             {
3233                 if(SCpnt->device == SDpnt)
3234                 {
3235                     if(SCpnt->prev != NULL)
3236                         SCpnt->prev->next = SCpnt->next;
3237                     if(SCpnt->next != NULL)
3238                         SCpnt->next->prev = SCpnt->prev;
3239                     if(SCpnt == SDpnt->host->host_queue)
3240                         SDpnt->host->host_queue = SCpnt->next;
3241                     scsi_init_free((char *) SCpnt, sizeof(*SCpnt));
3242                 }
3243             }
3244             SDpnt->has_cmdblocks = 0;
3245         }
3246     }
3247     /*
3248      * Extract the template from the linked list.
3249      */
3250     spnt = scsi_devicelist;
3251     prev_spnt = NULL;
3252     while(spnt != tpnt)
3253     {
3254         prev_spnt = spnt;
3255         spnt = spnt->next;
3256     }
3257     if(prev_spnt == NULL)
3258         scsi_devicelist = tpnt->next;
3259     else
3260         prev_spnt->next = spnt->next;
3261     
3262     MOD_DEC_USE_COUNT;
3263     /*
3264      * Final cleanup for the driver is done in the driver sources in the 
3265      * cleanup function.
3266      */
3267     return 0;
3268 }
3269 
3270 
3271 int scsi_register_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
3272 {
3273     switch(module_type){
3274     case MODULE_SCSI_HA:
3275         return scsi_register_host((Scsi_Host_Template *) ptr);
3276         
3277         /* Load upper level device handler of some kind */
3278     case MODULE_SCSI_DEV:
3279 #ifdef CONFIG_KERNELD
3280         if (scsi_hosts == NULL)
3281                 request_module("scsi_hostadapter");
3282 #endif
3283         return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
3284         /* The rest of these are not yet implemented */
3285         
3286         /* Load constants.o */
3287     case MODULE_SCSI_CONST:
3288         
3289         /* Load specialized ioctl handler for some device.  Intended for 
3290          * cdroms that have non-SCSI2 audio command sets. */
3291     case MODULE_SCSI_IOCTL:
3292         
3293     default:
3294         return 1;
3295     }
3296 }
3297 
3298 void scsi_unregister_module(int module_type, void * ptr)
     /* [previous][next][first][last][top][bottom][index][help] */
3299 {
3300     switch(module_type) {
3301     case MODULE_SCSI_HA:
3302         scsi_unregister_host((Scsi_Host_Template *) ptr);
3303         break;
3304     case MODULE_SCSI_DEV:
3305         scsi_unregister_device((struct Scsi_Device_Template *) ptr);
3306         break;
3307         /* The rest of these are not yet implemented. */
3308     case MODULE_SCSI_CONST:
3309     case MODULE_SCSI_IOCTL:
3310         break;
3311     default:
3312     }
3313     return;
3314 }
3315 
3316 #endif          /* CONFIG_MODULES */
3317 
3318 #ifdef DEBUG_TIMEOUT
3319 static void
3320 scsi_dump_status(void)
     /* [previous][next][first][last][top][bottom][index][help] */
3321 {
3322     int i;
3323     struct Scsi_Host * shpnt;
3324     Scsi_Cmnd * SCpnt;
3325     printk("Dump of scsi parameters:\n");
3326     i = 0;
3327     for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
3328         for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
3329         {
3330             /*  (0) 0:0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
3331             printk("(%d) %d:%d:%d:%d (%s %ld %ld %ld %d) (%d %d %x) (%d %d %d) %x %x %x\n",
3332                    i++, SCpnt->host->host_no,
3333                    SCpnt->channel,
3334                    SCpnt->target,
3335                    SCpnt->lun,
3336                    kdevname(SCpnt->request.rq_dev),
3337                    SCpnt->request.sector,
3338                    SCpnt->request.nr_sectors,
3339                    SCpnt->request.current_nr_sectors,
3340                    SCpnt->use_sg,
3341                    SCpnt->retries,
3342                    SCpnt->allowed,
3343                    SCpnt->flags,
3344                    SCpnt->timeout_per_command,
3345                    SCpnt->timeout,
3346                    SCpnt->internal_timeout,
3347                    SCpnt->cmnd[0],
3348                    SCpnt->sense_buffer[2],
3349                    SCpnt->result);
3350         }
3351     printk("wait_for_request = %p\n", wait_for_request);
3352     /* Now dump the request lists for each block device */
3353     printk("Dump of pending block device requests\n");
3354     for(i=0; i<MAX_BLKDEV; i++)
3355         if(blk_dev[i].current_request)
3356         {
3357             struct request * req;
3358             printk("%d: ", i);
3359             req = blk_dev[i].current_request;
3360             while(req) {
3361                 printk("(%s %d %ld %ld %ld) ",
3362                        kdevname(req->rq_dev),
3363                        req->cmd,
3364                        req->sector,
3365                        req->nr_sectors,
3366                        req->current_nr_sectors);
3367                 req = req->next;
3368             }
3369             printk("\n");
3370         }
3371 }
3372 #endif
3373 
3374 #ifdef MODULE
3375 
3376 int init_module(void) {
     /* [previous][next][first][last][top][bottom][index][help] */
3377     unsigned long size;
3378 
3379     /*
3380      * This makes /proc/scsi visible.
3381      */
3382     dispatch_scsi_info_ptr = dispatch_scsi_info;
3383 
3384     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
3385     timer_table[SCSI_TIMER].expires = 0;
3386     register_symtab(&scsi_symbol_table);
3387     scsi_loadable_module_flag = 1;
3388 
3389     /* Register the /proc/scsi/scsi entry */
3390 #if CONFIG_PROC_FS
3391     proc_scsi_register(0, &proc_scsi_scsi);
3392 #endif
3393 
3394     
3395     dma_sectors = PAGE_SIZE / SECTOR_SIZE;
3396     dma_free_sectors= dma_sectors;
3397     /*
3398      * Set up a minimal DMA buffer list - this will be used during scan_scsis
3399      * in some cases.
3400      */
3401     
3402     /* One bit per sector to indicate free/busy */
3403     size = (dma_sectors / SECTORS_PER_PAGE)*sizeof(FreeSectorBitmap);
3404     dma_malloc_freelist = (unsigned char *) scsi_init_malloc(size, GFP_ATOMIC);
3405     memset(dma_malloc_freelist, 0, size);
3406 
3407     /* One pointer per page for the page list */
3408     dma_malloc_pages = (unsigned char **)
3409         scsi_init_malloc((dma_sectors / SECTORS_PER_PAGE)*sizeof(*dma_malloc_pages), GFP_ATOMIC);
3410     dma_malloc_pages[0] = (unsigned char *)
3411         scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);
3412     return 0;
3413 }
3414 
3415 void cleanup_module( void) 
     /* [previous][next][first][last][top][bottom][index][help] */
3416 {
3417 #if CONFIG_PROC_FS
3418     proc_scsi_unregister(0, PROC_SCSI_SCSI);
3419 #endif
3420 
3421     /* No, we're not here anymore. Don't show the /proc/scsi files. */
3422     dispatch_scsi_info_ptr = 0L;
3423 
3424     /*
3425      * Free up the DMA pool.
3426      */
3427     resize_dma_pool();
3428 
3429     timer_table[SCSI_TIMER].fn = NULL;
3430     timer_table[SCSI_TIMER].expires = 0;
3431 }
3432 #endif /* MODULE */
3433 
3434 /*
3435  * Overrides for Emacs so that we follow Linus's tabbing style.
3436  * Emacs will notice this stuff at the end of the file and automatically
3437  * adjust the settings for this buffer only.  This must remain at the end
3438  * of the file.
3439  * ---------------------------------------------------------------------------
3440  * Local variables:
3441  * c-indent-level: 4
3442  * c-brace-imaginary-offset: 0
3443  * c-brace-offset: -4
3444  * c-argdecl-indent: 4
3445  * c-label-offset: -4
3446  * c-continued-statement-offset: 4
3447  * c-continued-brace-offset: 0
3448  * indent-tabs-mode: nil
3449  * tab-width: 8
3450  * End:
3451  */

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