home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.2 / LINUX-1.2 / LINUX-1 / linux / drivers / scsi / scsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-20  |  68.4 KB  |  2,557 lines

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