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

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