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

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