root/drivers/scsi/scsi.c

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

DEFINITIONS

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

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