home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / drivers / scsi / scsi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  43.8 KB  |  1,748 lines

  1. /*
  2.  *    scsi.c Copyright (C) 1992 Drew Eckhardt
  3.  *    generic mid-level SCSI driver by
  4.  *        Drew Eckhardt
  5.  *
  6.  *    <drew@colorado.edu>
  7.  *
  8.  *    Bug correction thanks go to :
  9.  *        Rik Faith <faith@cs.unc.edu>
  10.  *        Tommy Thorn <tthorn>
  11.  *        Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  12.  *
  13.  *     Modified by Eric Youngdale eric@tantalus.nrl.navy.mil to
  14.  *     add scatter-gather, multiple outstanding request, and other
  15.  *     enhancements.
  16.  */
  17.  
  18. #include <asm/system.h>
  19. #include <linux/sched.h>
  20. #include <linux/timer.h>
  21. #include <linux/string.h>
  22.  
  23. #include "../block/blk.h"
  24. #include "scsi.h"
  25. #include "hosts.h"
  26. #include "constants.h"
  27.  
  28. /*
  29. 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 $";
  30. */
  31.  
  32. /* Command groups 3 and 4 are reserved and should never be used.  */
  33. const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };
  34.  
  35. #define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))
  36.  
  37. static void scsi_done (Scsi_Cmnd *SCpnt);
  38. static int update_timeout (Scsi_Cmnd *, int);
  39. static void print_inquiry(unsigned char *data);
  40. static void scsi_times_out (Scsi_Cmnd * SCpnt);
  41.  
  42. static int time_start;
  43. static int time_elapsed;
  44.  
  45. #define MAX_SCSI_DEVICE_CODE 10
  46. const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
  47. {
  48.  "Direct-Access    ",
  49.  "Sequential-Access",
  50.  "Printer          ",
  51.  "Processor        ",
  52.  "WORM             ",
  53.  "CD-ROM           ",
  54.  "Scanner          ",
  55.  "Optical Device   ",
  56.  "Medium Changer   ",
  57.  "Communications   "
  58. };
  59.  
  60.  
  61. /*
  62.     global variables :
  63.     NR_SCSI_DEVICES is the number of SCSI devices we have detected,
  64.     scsi_devices an array of these specifing the address for each
  65.     (host, id, LUN)
  66. */
  67.  
  68. int NR_SCSI_DEVICES=0;
  69.  
  70. Scsi_Device * scsi_devices = NULL;
  71.  
  72. static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};
  73.  
  74. /* We make this not static so that we can read the array with gdb. */
  75. /* static */ Scsi_Cmnd * last_cmnd = NULL;
  76.  
  77. /*
  78.  *    As the scsi do command functions are inteligent, and may need to
  79.  *    redo a command, we need to keep track of the last command
  80.  *    executed on each one.
  81.  */
  82.  
  83. #define WAS_RESET    0x01
  84. #define WAS_TIMEDOUT    0x02
  85. #define WAS_SENSE    0x04
  86. #define IS_RESETTING    0x08
  87. #define ASKED_FOR_SENSE 0x10
  88. /* #define NEEDS_JUMPSTART 0x20  defined in hosts.h */
  89.  
  90. /*
  91.  *    This is the number  of clock ticks we should wait before we time out
  92.  *    and abort the command.    This is for  where the scsi.c module generates
  93.  *    the command, not where it originates from a higher level, in which
  94.  *    case the timeout is specified there.
  95.  *
  96.  *    ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
  97.  *    respectively.
  98.  */
  99.  
  100. #ifdef DEBUG
  101.     #define SCSI_TIMEOUT 500
  102. #else
  103.     #define SCSI_TIMEOUT 100
  104. #endif
  105.  
  106. #ifdef DEBUG
  107.     #define SENSE_TIMEOUT SCSI_TIMEOUT
  108.     #define ABORT_TIMEOUT SCSI_TIMEOUT
  109.     #define RESET_TIMEOUT SCSI_TIMEOUT
  110. #else
  111.     #define SENSE_TIMEOUT 50
  112.     #define RESET_TIMEOUT 50
  113.     #define ABORT_TIMEOUT 50
  114.     #define MIN_RESET_DELAY 100
  115. #endif
  116.  
  117. /* The following devices are known not to tolerate a lun != 0 scan for
  118.    one reason or another.  Some will respond to all luns, others will
  119.    lock up. */
  120.  
  121.      struct blist{
  122.        char * vendor;
  123.        char * model;
  124.        char * revision; /* Latest revision known to be bad.  Not used yet */
  125.      };
  126.  
  127. static struct blist blacklist[] =
  128. {
  129.    {"DENON","DRD-25X","V"},   /* A cdrom that locks up when probed at lun != 0 */
  130.    {"MAXTOR","XT-4380S","B3C"},  /* Locks-up when LUN>0 polled. */
  131.    {"MAXTOR","MXT-1240S","I1.2"}, /* Locks up when LUN > 0 polled */
  132.    {"MAXTOR","XT-4170S","B5A"},  /* Locks-up sometimes when LUN>0 polled. */
  133.    {"NEC","CD-ROM DRIVE:841","1.0"},  /* Locks-up when LUN>0 polled. */
  134.    {"SEAGATE", "ST157N", "\004|j"}, /* causes failed REQUEST SENSE on lun 1 for aha152x
  135.                      * controller, which causes SCSI code to reset bus.*/
  136.    {"SEAGATE", "ST296","921"},   /* Responds to all lun */
  137.    {"SONY","CD-ROM CDU-541","4.3d"},
  138.    {"TANDBERG","TDC 3600","U07"},  /* Locks up if polled for lun != 0 */
  139.    {"TEXEL","CD-ROM","1.06"},   /* causes failed REQUEST SENSE on lun 1 for seagate
  140.                  * controller, which causes SCSI code to reset bus.*/
  141.    {NULL, NULL, NULL}};
  142.  
  143. static int blacklisted(unsigned char * response_data){
  144.   int i = 0;
  145.   unsigned char * pnt;
  146.   for(i=0; 1; i++){
  147.     if(blacklist[i].vendor == NULL) return 0;
  148.     pnt = &response_data[8];
  149.     while(*pnt && *pnt == ' ') pnt++;
  150.     if(memcmp(blacklist[i].vendor, pnt,
  151.            strlen(blacklist[i].vendor))) continue;
  152.     pnt = &response_data[16];
  153.     while(*pnt && *pnt == ' ') pnt++;
  154.     if(memcmp(blacklist[i].model, pnt,
  155.            strlen(blacklist[i].model))) continue;
  156.     return 1;
  157.   };
  158. };
  159.  
  160. /*
  161.  *    As the actual SCSI command runs in the background, we must set up a
  162.  *    flag that tells scan_scsis() when the result it has is valid.
  163.  *    scan_scsis can set the_result to -1, and watch for it to become the
  164.  *    actual return code for that call.  the scan_scsis_done function() is
  165.  *    our user specified completion function that is passed on to the
  166.  *    scsi_do_cmd() function.
  167.  */
  168.  
  169. static volatile int in_scan = 0;
  170. static int the_result;
  171. static void scan_scsis_done (Scsi_Cmnd * SCpnt)
  172.     {
  173.  
  174. #ifdef DEBUG
  175.     printk ("scan_scsis_done(%d, %06x)\n", SCpnt->host->host_no, SCpnt->result);
  176. #endif
  177.     SCpnt->request.dev = 0xfffe;
  178.     }
  179. /*
  180.  *    Detecting SCSI devices :
  181.  *    We scan all present host adapter's busses,  from ID 0 to ID 6.
  182.  *    We use the INQUIRY command, determine device type, and pass the ID /
  183.  *    lun address of all sequential devices to the tape driver, all random
  184.  *    devices to the disk driver.
  185.  */
  186.  
  187. static void scan_scsis (void)
  188. {
  189.   int dev, lun, type;
  190.   unsigned char scsi_cmd [12];
  191.   unsigned char scsi_result [256];
  192.   struct Scsi_Host * shpnt;
  193.   Scsi_Cmnd  SCmd;
  194.  
  195.   ++in_scan;
  196.   lun = 0;
  197.  
  198.   SCmd.next = NULL;
  199.   SCmd.prev = NULL;
  200.   for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
  201.       {
  202.     shpnt->host_queue = &SCmd;  /* We need this so that
  203.                      commands can time out */
  204.     for (dev = 0; dev < 8; ++dev)
  205.       if (shpnt->this_id != dev)
  206. /*
  207.  * We need the for so our continue, etc. work fine.
  208.  */
  209.  
  210. #ifdef NO_MULTI_LUN
  211.         for (lun = 0; lun < 1; ++lun)
  212. #else
  213.         for (lun = 0; lun < 8; ++lun)
  214. #endif
  215.           {
  216. #ifdef WDEBUG
  217.         printk ("about to scan target %d, lun %d\n", dev, lun);
  218.         printk ("press left mouse button to continue\n");
  219.         waitbut();
  220. #endif
  221.  
  222.         scsi_devices[NR_SCSI_DEVICES].host = shpnt;
  223.         scsi_devices[NR_SCSI_DEVICES].id = dev;
  224.         scsi_devices[NR_SCSI_DEVICES].lun = lun;
  225.         scsi_devices[NR_SCSI_DEVICES].index = NR_SCSI_DEVICES;
  226.         scsi_devices[NR_SCSI_DEVICES].device_wait = NULL;
  227.  
  228. /*
  229.  * Assume that the device will have handshaking problems, and then
  230.  * fix this field later if it turns out it doesn't.
  231.  */
  232.         scsi_devices[NR_SCSI_DEVICES].borken = 1;
  233.  
  234.         scsi_cmd[0] = TEST_UNIT_READY;
  235.         scsi_cmd[1] = lun << 5;
  236.         scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[5] = 0;
  237.         scsi_cmd[4] = 0;
  238.  
  239.         SCmd.host = shpnt;
  240.         SCmd.target = dev;
  241.         SCmd.lun = lun;
  242.  
  243.         SCmd.request.dev = 0xffff; /* Mark not busy */
  244.         SCmd.use_sg  = 0;
  245.         SCmd.old_use_sg  = 0;
  246.         SCmd.transfersize = 0;
  247.         SCmd.underflow = 0;
  248.         SCmd.index = NR_SCSI_DEVICES;
  249.  
  250.         scsi_do_cmd (&SCmd,
  251.                  (void *)  scsi_cmd, (void *)
  252.                  scsi_result, 0,    scan_scsis_done,
  253.                  SCSI_TIMEOUT + 400, 5);
  254.  
  255.         while (SCmd.request.dev != 0xfffe);
  256. #if defined(DEBUG) || defined(DEBUG_INIT)
  257.         printk("scsi: scan SCSIS id %d lun %d\n", dev, lun);
  258.         printk("scsi: return code %08x\n", SCmd.result);
  259. #endif
  260.  
  261.  
  262.         if(SCmd.result) {
  263.           if ((driver_byte(SCmd.result)  & DRIVER_SENSE) &&
  264.               ((SCmd.sense_buffer[0] & 0x70) >> 4) == 7) {
  265.             if (SCmd.sense_buffer[2] &0xe0)
  266.               continue; /* No devices here... */
  267.             if(((SCmd.sense_buffer[2] & 0xf) != NOT_READY) &&
  268.                ((SCmd.sense_buffer[2] & 0xf) != UNIT_ATTENTION))
  269.               continue;
  270.           }
  271.           else
  272.             break;
  273.         };
  274.  
  275. #ifdef WDEBUG
  276.         printk ("Press left mouse button to continue\n");
  277.         waitbut();
  278. #endif
  279.  
  280. #if defined (DEBUG) || defined(DEBUG_INIT)
  281.         printk("scsi: performing INQUIRY\n");
  282. #endif
  283.  
  284.         /*
  285.          * Build an INQUIRY command block.
  286.          */
  287.  
  288.         scsi_cmd[0] = INQUIRY;
  289.         scsi_cmd[1] = (lun << 5) & 0xe0;
  290.         scsi_cmd[2] = 0;
  291.         scsi_cmd[3] = 0;
  292.         scsi_cmd[4] = 255;
  293.         scsi_cmd[5] = 0;
  294.  
  295.         SCmd.request.dev = 0xffff; /* Mark not busy */
  296.  
  297.         scsi_do_cmd (&SCmd,
  298.                  (void *)  scsi_cmd, (void *)
  299.                  scsi_result, 255,    scan_scsis_done,
  300.                  SCSI_TIMEOUT, 3);
  301.  
  302.         while (SCmd.request.dev != 0xfffe);
  303.  
  304.         the_result = SCmd.result;
  305.  
  306. #if defined(DEBUG) || defined(DEBUG_INIT)
  307.         if (!the_result)
  308.             printk("scsi: INQUIRY successful\n");
  309.         else
  310.             printk("scsi: INQUIRY failed with code %08x\n", the_result);
  311. #endif
  312.  
  313.         if(the_result) break;
  314.  
  315.         /* skip other luns on this device */
  316.  
  317.         if (!the_result)
  318.           {
  319.             scsi_devices[NR_SCSI_DEVICES].
  320.               removable = (0x80 &
  321.                    scsi_result[1]) >> 7;
  322.             scsi_devices[NR_SCSI_DEVICES].lockable =
  323.               scsi_devices[NR_SCSI_DEVICES].removable;
  324.             scsi_devices[NR_SCSI_DEVICES].
  325.               changed = 0;
  326.             scsi_devices[NR_SCSI_DEVICES].
  327.               access_count = 0;
  328.             scsi_devices[NR_SCSI_DEVICES].
  329.               busy = 0;
  330. /*
  331.  *    Currently, all sequential devices are assumed to be tapes,
  332.  *    all random devices disk, with the appropriate read only
  333.  *    flags set for ROM / WORM treated as RO.
  334.  */
  335.  
  336.             switch (type = scsi_result[0])
  337.               {
  338.               case TYPE_TAPE :
  339.               case TYPE_DISK :
  340.               case TYPE_MOD :
  341.             scsi_devices[NR_SCSI_DEVICES].writeable = 1;
  342.             break;
  343.               case TYPE_WORM :
  344.               case TYPE_ROM :
  345.             scsi_devices[NR_SCSI_DEVICES].writeable = 0;
  346.             break;
  347.             default :
  348. #if 0
  349. #ifdef DEBUG
  350.             printk("scsi: unknown type %d\n", type);
  351.             print_inquiry(scsi_result);
  352. #endif
  353. #endif
  354.               type = -1;
  355.               }
  356.  
  357.             scsi_devices[NR_SCSI_DEVICES].random =
  358.               (type == TYPE_TAPE) ? 0 : 1;
  359.             scsi_devices[NR_SCSI_DEVICES].type = type;
  360.  
  361.             if (type != -1)
  362.               {
  363.             print_inquiry(scsi_result);
  364.             switch(type){
  365.             case TYPE_TAPE:
  366.               printk("Detected scsi tape st%d at scsi%d, id %d, lun %d\n", MAX_ST,
  367.                  shpnt->host_no , dev, lun);
  368.               if(NR_ST != -1) ++MAX_ST;
  369.               break;
  370.             case TYPE_ROM:
  371.               printk("Detected scsi CD-ROM sr%d at scsi%d, id %d, lun %d\n", MAX_SR,
  372.                  shpnt->host_no , dev, lun);
  373.               if(NR_SR != -1) ++MAX_SR;
  374.               break;
  375.             case TYPE_DISK:
  376.             case TYPE_MOD:
  377.               printk("Detected scsi disk sd%d at scsi%d, id %d, lun %d\n", MAX_SD,
  378.                  shpnt->host_no , dev, lun);
  379.               if(NR_SD != -1) ++MAX_SD;
  380.               break;
  381.             default:
  382.               break;
  383.             };
  384.  
  385.             if(NR_SG != -1) ++MAX_SG;
  386.  
  387.             scsi_devices[NR_SCSI_DEVICES].scsi_level =
  388.               scsi_result[2] & 0x07;
  389.             if (scsi_devices[NR_SCSI_DEVICES].scsi_level >= 2 ||
  390.                 (scsi_devices[NR_SCSI_DEVICES].scsi_level == 1 &&
  391.                  (scsi_result[3] & 0x0f) == 1))
  392.               scsi_devices[NR_SCSI_DEVICES].scsi_level++;
  393. /* 
  394.  * Set the tagged_queue flag for SCSI-II devices that purport to support
  395.  * tagged queuing in the INQUIRY data.
  396.  */
  397.  
  398.             scsi_devices[NR_SCSI_DEVICES].tagged_queue = 0;
  399.  
  400.             if ((scsi_devices[NR_SCSI_DEVICES].scsi_level == SCSI_2) &&
  401.                 (scsi_result[7] & 2)) {
  402.                 scsi_devices[NR_SCSI_DEVICES].tagged_supported = 1;
  403.                 scsi_devices[NR_SCSI_DEVICES].current_tag = 0;
  404.             }
  405.  
  406. /*
  407.  * Accomodate drivers that want to sleep when they should be in a polling
  408.  * loop.
  409.  */
  410.  
  411.             scsi_devices[NR_SCSI_DEVICES].disconnect = 0;
  412.  
  413. /*
  414.  * Some revisions of the Texel CD ROM drives have handshaking
  415.  * problems when used with the Seagate controllers.  Before we
  416.  * know what type of device we're talking to, we assume it's
  417.  * borken and then change it here if it turns out that it isn't
  418.  * a TEXEL drive.
  419.  */
  420.  
  421.             if(strncmp("TEXEL", (char *) &scsi_result[8], 5) != 0 ||
  422.               strncmp("CD-ROM", (char *) &scsi_result[16], 6) != 0 
  423. /*
  424.  * XXX 1.06 has problems, some one should figure out the others too so
  425.  * ALL TEXEL drives don't suffer in performance, especially when I finish
  426.  * integrating my seagate patches which do multiple I_T_L nexuses.
  427.  */
  428.  
  429. #ifdef notyet
  430.                || (strncmp("1.06", (char *) &scsi_result[[, 4) != 0)
  431. #endif
  432.                )
  433.                scsi_devices[NR_SCSI_DEVICES].borken = 0;
  434.  
  435.  
  436.             /* These devices need this "key" to unlock the device
  437.                so we can use it */
  438.             if(memcmp("INSITE", &scsi_result[8], 6) == 0 &&
  439.                (memcmp("Floptical   F*8I", &scsi_result[16], 16) == 0
  440.                 || memcmp("I325VM", &scsi_result[16], 6) == 0)) {
  441.               printk("Unlocked floptical drive.\n");
  442.               scsi_devices[NR_SCSI_DEVICES].lockable = 0;
  443.               scsi_cmd[0] = MODE_SENSE;
  444.               scsi_cmd[1] = (lun << 5) & 0xe0;
  445.               scsi_cmd[2] = 0x2e;
  446.               scsi_cmd[3] = 0;
  447.               scsi_cmd[4] = 0x2a;
  448.               scsi_cmd[5] = 0;
  449.  
  450.               SCmd.request.dev = 0xffff; /* Mark not busy */
  451.  
  452.               scsi_do_cmd (&SCmd,
  453.                        (void *)  scsi_cmd, (void *)
  454.                        scsi_result, 0x2a,  scan_scsis_done,
  455.                        SCSI_TIMEOUT, 3);
  456.  
  457.               while (SCmd.request.dev != 0xfffe);
  458.             };
  459.  
  460.             ++NR_SCSI_DEVICES;
  461.             /* Some scsi devices cannot be polled for lun != 0
  462.                due to firmware bugs */
  463.             if(blacklisted(scsi_result)) break;
  464.             /* Some scsi-1 peripherals do not handle lun != 0.
  465.                I am assuming that scsi-2 peripherals do better */
  466.             if((scsi_result[2] & 0x07) == 1 &&
  467.                (scsi_result[3] & 0x0f) == 0) break;
  468.             }
  469.           }      /* if result == DID_OK ends */
  470.  
  471. #ifdef WDEBUG
  472.           printk ("Press left mouse button to continue\n");
  473.           waitbut();
  474. #endif
  475.  
  476.           }       /* for lun ends */
  477.     shpnt->host_queue = NULL;  /* No longer needed here */
  478.       }     /* if present */
  479.  
  480.   printk("scsi : detected ");
  481.   if(NR_SD != -1)
  482.     printk("%d SCSI disk%s ", MAX_SD, (MAX_SD != 1) ? "s" : "");
  483.  
  484.   if(NR_ST != -1)
  485.     printk("%d tape%s ", MAX_ST, (MAX_ST != 1) ? "s" : "");
  486.  
  487.   if(NR_SR != -1)
  488.     printk("%d CD-ROM drive%s ", MAX_SR, (MAX_SR != 1) ? "s" : "");
  489.  
  490.   printk("total.\n");
  491.   in_scan = 0;
  492. }    /* scan_scsis  ends */
  493.  
  494. /*
  495.  *    Flag bits for the internal_timeout array
  496.  */
  497.  
  498. #define NORMAL_TIMEOUT 0
  499. #define IN_ABORT 1
  500. #define IN_RESET 2
  501. /*
  502.     This is our time out function, called when the timer expires for a
  503.     given host adapter.  It will attempt to abort the currently executing
  504.     command, that failing perform a kernel panic.
  505. */
  506.  
  507. static void scsi_times_out (Scsi_Cmnd * SCpnt)
  508.     {
  509.  
  510.     switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
  511.         {
  512.         case NORMAL_TIMEOUT:
  513.             if (!in_scan)
  514.                   printk("SCSI host %d timed out - aborting command\n",
  515.                 SCpnt->host->host_no);
  516.  
  517.             if (!scsi_abort (SCpnt, DID_TIME_OUT))
  518.                 return;
  519.         case IN_ABORT:
  520.             printk("SCSI host %d abort() timed out - reseting\n",
  521.                 SCpnt->host->host_no);
  522.             if (!scsi_reset (SCpnt))
  523.                 return;
  524.         case IN_RESET:
  525.         case (IN_ABORT | IN_RESET):
  526.             panic("Unable to reset scsi host %d\n",SCpnt->host->host_no);
  527.         default:
  528.             INTERNAL_ERROR;
  529.         }
  530.  
  531.     }
  532.  
  533.  
  534. /* This function takes a quick look at a request, and decides if it
  535. can be queued now, or if there would be a stall while waiting for
  536. something else to finish.  This routine assumes that interrupts are
  537. turned off when entering the routine.  It is the responsibility
  538. of the calling code to ensure that this is the case. */
  539.  
  540. Scsi_Cmnd * request_queueable (struct request * req, int index)
  541. {
  542.   Scsi_Cmnd * SCpnt = NULL;
  543.   int tablesize;
  544.   struct buffer_head * bh;
  545.  
  546.   if ((index < 0) ||  (index > NR_SCSI_DEVICES))
  547.     panic ("Index number in allocate_device() is out of range.\n");
  548.  
  549.   if (req && req->dev <= 0)
  550.     panic("Invalid device in allocate_device");
  551.  
  552.   SCpnt =  scsi_devices[index].host->host_queue;
  553.     while(SCpnt){
  554.       if(SCpnt->target == scsi_devices[index].id &&
  555.      SCpnt->lun == scsi_devices[index].lun)
  556.     if(SCpnt->request.dev < 0) break;
  557.       SCpnt = SCpnt->next;
  558.     };
  559.  
  560.   if (!SCpnt) return NULL;
  561.  
  562.   if (scsi_devices[index].host->hostt->can_queue
  563.       && scsi_devices[index].host->host_busy >= scsi_devices[index].host->hostt->can_queue) return NULL;
  564.  
  565.   if (req) {
  566.     memcpy(&SCpnt->request, req, sizeof(struct request));
  567.     tablesize = scsi_devices[index].host->sg_tablesize;
  568.     bh = req->bh;
  569.     if(!tablesize) bh = NULL;
  570.     /* Take a quick look through the table to see how big it is.  We already
  571.        have our copy of req, so we can mess with that if we want to.  */
  572.     while(req->nr_sectors && bh){
  573.         tablesize--;
  574.         req->nr_sectors -= bh->b_size >> 9;
  575.         req->sector += bh->b_size >> 9;
  576.         if(!tablesize) break;
  577.         bh = bh->b_reqnext;
  578.     };
  579.     if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
  580.       SCpnt->request.bhtail = bh;
  581.       req->bh = bh->b_reqnext; /* Divide request */
  582.       bh->b_reqnext = NULL;
  583.       bh = req->bh;
  584.  
  585.       /* Now reset things so that req looks OK */
  586.       SCpnt->request.nr_sectors -= req->nr_sectors;
  587.       req->current_nr_sectors = bh->b_size >> 9;
  588.       req->buffer = bh->b_data;
  589.       SCpnt->request.waiting = NULL; /* Wait until whole thing done */
  590.     } else
  591.       req->dev = -1;
  592.       
  593.   } else {
  594.     SCpnt->request.dev = 0xffff; /* Busy, but no request */
  595.     SCpnt->request.waiting = NULL;  /* And no one is waiting for the device either */
  596.   };
  597.  
  598.   SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
  599.   SCpnt->old_use_sg  = 0;
  600.   SCpnt->transfersize = 0;
  601.   SCpnt->underflow = 0;
  602.   return SCpnt;
  603. }
  604.  
  605. /* This function returns a structure pointer that will be valid for
  606. the device.  The wait parameter tells us whether we should wait for
  607. the unit to become free or not.  We are also able to tell this routine
  608. not to return a descriptor if the host is unable to accept any more
  609. commands for the time being.  We need to keep in mind that there is no
  610. guarantee that the host remain not busy.  Keep in mind the
  611. request_queueable function also knows the internal allocation scheme
  612. of the packets for each device */
  613.  
  614. Scsi_Cmnd * allocate_device (struct request ** reqp, int index, int wait)
  615. {
  616.   int dev = -1;
  617.   struct request * req = NULL;
  618.   int tablesize;
  619.   struct buffer_head * bh;
  620.   struct Scsi_Host * host;
  621.   Scsi_Cmnd * SCpnt = NULL;
  622.   Scsi_Cmnd * SCwait = NULL;
  623.   unsigned long flags;
  624.  
  625.   if ((index < 0) ||  (index > NR_SCSI_DEVICES))
  626.     panic ("Index number in allocate_device() is out of range.\n");
  627.  
  628.   if (reqp) req = *reqp;
  629.  
  630.     /* See if this request has already been queued by an interrupt routine */
  631.   if (req && (dev = req->dev) <= 0) return NULL;
  632.  
  633.   host = scsi_devices[index].host;
  634.  
  635.   while (1==1){
  636.     SCpnt = host->host_queue;
  637.     while(SCpnt){
  638.       if(SCpnt->target == scsi_devices[index].id &&
  639.      SCpnt->lun == scsi_devices[index].lun) {
  640.     SCwait = SCpnt;
  641.     if(SCpnt->request.dev < 0) break;
  642.       };
  643.       SCpnt = SCpnt->next;
  644.     };
  645.     save_flags (flags);
  646.     cli();
  647.     /* See if this request has already been queued by an interrupt routine */
  648.     if (req && ((req->dev < 0) || (req->dev != dev))) {
  649.       restore_flags (flags);
  650.       return NULL;
  651.     };
  652.     if (!SCpnt || SCpnt->request.dev >= 0)  /* Might have changed */
  653.       {
  654.     restore_flags (flags);
  655.     if(!wait) return NULL;
  656.     if (!SCwait) {
  657.       printk("Attempt to allocate device index %d, target %d, lun %d\n",
  658.          index, scsi_devices[index].id ,scsi_devices[index].lun);
  659.       panic("No device found in allocate_device\n");
  660.     };
  661.     SCSI_SLEEP(&scsi_devices[SCwait->index].device_wait,
  662.            (SCwait->request.dev > 0));
  663.       } else {
  664.     if (req) {
  665.       memcpy(&SCpnt->request, req, sizeof(struct request));
  666.       tablesize = scsi_devices[index].host->sg_tablesize;
  667.       bh = req->bh;
  668.       if(!tablesize) bh = NULL;
  669.       /* Take a quick look through the table to see how big it is.  We already
  670.          have our copy of req, so we can mess with that if we want to.  */
  671.       while(req->nr_sectors && bh){
  672.         tablesize--;
  673.         req->nr_sectors -= bh->b_size >> 9;
  674.         req->sector += bh->b_size >> 9;
  675.         if(!tablesize) break;
  676.         bh = bh->b_reqnext;
  677.       };
  678.       if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
  679.         SCpnt->request.bhtail = bh;
  680.         req->bh = bh->b_reqnext; /* Divide request */
  681.         bh->b_reqnext = NULL;
  682.         bh = req->bh;
  683.         /* Now reset things so that req looks OK */
  684.         SCpnt->request.nr_sectors -= req->nr_sectors;
  685.         req->current_nr_sectors = bh->b_size >> 9;
  686.         req->buffer = bh->b_data;
  687.         SCpnt->request.waiting = NULL; /* Wait until whole thing done */
  688.       }
  689.       else 
  690.         {
  691.           req->dev = -1;
  692.           *reqp = req->next;
  693.         };
  694.     } else {
  695.       SCpnt->request.dev = 0xffff; /* Busy */
  696.       SCpnt->request.waiting = NULL;  /* And no one is waiting for this to complete */
  697.     };
  698.     restore_flags (flags);
  699.     break;
  700.       };
  701.   };
  702.  
  703.   SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
  704.   SCpnt->old_use_sg  = 0;
  705.   SCpnt->transfersize = 0;    /* No default transfer size */
  706.   SCpnt->underflow = 0;     /* Do not flag underflow conditions */
  707.   return SCpnt;
  708. }
  709.  
  710. /*
  711.     This is inline because we have stack problemes if we recurse to deeply.
  712. */
  713.  
  714. inline void internal_cmnd (Scsi_Cmnd * SCpnt)
  715.     {
  716.     int temp;
  717.     struct Scsi_Host * host;
  718. #ifdef DEBUG_DELAY
  719.     int clock;
  720. #endif
  721.  
  722. #ifdef __i386__
  723.     if ((unsigned long) &SCpnt < current->kernel_stack_page)
  724.       panic("Kernel stack overflow.");
  725. #endif
  726.  
  727.     host = SCpnt->host;
  728.  
  729. /*
  730.     We will wait MIN_RESET_DELAY clock ticks after the last reset so
  731.     we can avoid the drive not being ready.
  732. */
  733. temp = host->last_reset;
  734. while (jiffies < temp);
  735.  
  736. update_timeout(SCpnt, SCpnt->timeout_per_command);
  737.  
  738. /*
  739.     We will use a queued command if possible, otherwise we will emulate the
  740.     queing and calling of completion function ourselves.
  741. */
  742. #ifdef DEBUG
  743.     printk("internal_cmnd (host = %d, target = %d, command = %p, buffer =  %p, \n"
  744.         "bufflen = %d, done = %p)\n", SCpnt->host->host_no, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
  745. #endif
  746.  
  747.     if (host->hostt->can_queue)
  748.         {
  749. #ifdef DEBUG
  750.     printk("queuecommand : routine at %p\n",
  751.         host->hostt->queuecommand);
  752. #endif
  753.         host->hostt->queuecommand (SCpnt, scsi_done);
  754.         }
  755.     else
  756.         {
  757.  
  758. #ifdef DEBUG
  759.     printk("command() :  routine at %p\n", host->hostt->command);
  760. #endif
  761.         temp=host->hostt->command (SCpnt);
  762.         SCpnt->result = temp;
  763. #ifdef DEBUG_DELAY
  764.     clock = jiffies + 400;
  765.     while (jiffies < clock);
  766.     printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp, done);
  767. #endif
  768.         scsi_done(SCpnt);
  769.         }
  770. #ifdef DEBUG
  771.     printk("leaving internal_cmnd()\n");
  772. #endif
  773.     }
  774.  
  775. static void scsi_request_sense (Scsi_Cmnd * SCpnt)
  776.     {
  777.     unsigned long flags;
  778.  
  779.     save_flags (flags);
  780.     cli();
  781.     SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
  782.     update_timeout(SCpnt, SENSE_TIMEOUT);
  783.     restore_flags (flags);
  784.  
  785.  
  786.     memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
  787.         sizeof(generic_sense));
  788.  
  789.     SCpnt->cmnd[1] = SCpnt->lun << 5;
  790.     SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
  791.  
  792.     SCpnt->request_buffer = &SCpnt->sense_buffer;
  793.     SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
  794.     SCpnt->use_sg = 0;
  795.     internal_cmnd (SCpnt);
  796.     SCpnt->use_sg = SCpnt->old_use_sg;
  797.     }
  798.  
  799.  
  800.  
  801. /*
  802.     scsi_do_cmd sends all the commands out to the low-level driver.  It
  803.     handles the specifics required for each low level driver - ie queued
  804.     or non queud.  It also prevents conflicts when different high level
  805.     drivers go for the same host at the same time.
  806. */
  807.  
  808. void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
  809.           void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
  810.           int timeout, int retries
  811.            )
  812.     {
  813.     struct Scsi_Host * host = SCpnt->host;
  814.     unsigned long flags;
  815.  
  816. #ifdef DEBUG
  817.     {
  818.     int i;
  819.     int target = SCpnt->target;
  820.     printk ("scsi_do_cmd (host = %d, target = %d, buffer =%p, "
  821.         "bufflen = %d, done = %p, timeout = %d, retries = %d)\n"
  822.         "command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
  823.     for (i = 0; i < 10; ++i)
  824.         printk ("%02x  ", ((unsigned char *) cmnd)[i]);
  825.     printk("\n");
  826.       };
  827. #endif
  828.  
  829.     if (!host)
  830.         {
  831.         panic ("Invalid or not present host number. %d\n", host->host_no);
  832.         }
  833.  
  834.  
  835. /*
  836.     We must prevent reentrancy to the lowlevel host driver.  This prevents
  837.     it - we enter a loop until the host we want to talk to is not busy.
  838.     Race conditions are prevented, as interrupts are disabled inbetween the
  839.     time we check for the host being not busy, and the time we mark it busy
  840.     ourselves.
  841. */
  842.  
  843.     while (1==1){
  844.       save_flags (flags);
  845.       cli();
  846.       if (host->hostt->can_queue
  847.           && host->host_busy >= host->hostt->can_queue)
  848.         {
  849.           restore_flags (flags);
  850.           SCSI_SLEEP(&host->host_wait,
  851.              (host->host_busy >= host->hostt->can_queue));
  852.         } else {
  853.           host->host_busy++;
  854.           restore_flags (flags);
  855.           break;
  856.         };
  857.       };
  858. /*
  859.     Our own function scsi_done (which marks the host as not busy, disables
  860.     the timeout counter, etc) will be called by us or by the
  861.     scsi_hosts[host].queuecommand() function needs to also call
  862.     the completion function for the high level driver.
  863.  
  864. */
  865.  
  866.     memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
  867. #if 0
  868.     SCpnt->host = host;
  869.     SCpnt->target = target;
  870.     SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
  871. #endif
  872.     SCpnt->bufflen = bufflen;
  873.     SCpnt->buffer = buffer;
  874.     SCpnt->flags=0;
  875.     SCpnt->retries=0;
  876.     SCpnt->allowed=retries;
  877.     SCpnt->done = done;
  878.     SCpnt->timeout_per_command = timeout;
  879.  
  880.     memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
  881.     /* Zero the sense buffer.  Some host adapters automatically request
  882.        sense on error.  0 is not a valid sense code.  */
  883.     memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
  884.     SCpnt->request_buffer = buffer;
  885.     SCpnt->request_bufflen = bufflen;
  886.     SCpnt->old_use_sg = SCpnt->use_sg;
  887.  
  888.     /* Start the timer ticking.  */
  889.  
  890.     SCpnt->internal_timeout = 0;
  891.     internal_cmnd (SCpnt);
  892.  
  893. #ifdef DEBUG
  894.     printk ("Leaving scsi_do_cmd()\n");
  895. #endif
  896.     }
  897.  
  898.  
  899.  
  900. /*
  901.     The scsi_done() function disables the timeout timer for the scsi host,
  902.     marks the host as not busy, and calls the user specified completion
  903.     function for that host's current command.
  904. */
  905.  
  906. static void reset (Scsi_Cmnd * SCpnt)
  907. {
  908. #ifdef DEBUG
  909.     printk("scsi: reset(%d)\n", SCpnt->host->host_no);
  910. #endif
  911.     
  912.     SCpnt->flags |= (WAS_RESET | IS_RESETTING);
  913.     scsi_reset(SCpnt);
  914.     
  915. #ifdef DEBUG
  916.     printk("performing request sense\n");
  917. #endif
  918.     
  919.     if(SCpnt->flags & NEEDS_JUMPSTART) {
  920.       SCpnt->flags &= ~NEEDS_JUMPSTART;
  921.       scsi_request_sense (SCpnt);
  922.     };
  923. }
  924.  
  925.  
  926. static int check_sense (Scsi_Cmnd * SCpnt)
  927.     {
  928.   /* If there is no sense information, request it.  If we have already
  929.      requested it, there is no point in asking again - the firmware must be
  930.      confused. */
  931.   if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
  932.     if(!(SCpnt->flags & ASKED_FOR_SENSE))
  933.       return SUGGEST_SENSE;
  934.     else
  935.       return SUGGEST_RETRY;
  936.       }
  937.  
  938.   SCpnt->flags &= ~ASKED_FOR_SENSE;
  939.  
  940. #ifdef DEBUG_INIT
  941.     printk("scsi%d : ", SCpnt->host->host_no);
  942.     print_sense("", SCpnt);
  943.     printk("\n");
  944. #endif
  945.         if (SCpnt->sense_buffer[2] &0xe0)
  946.           return SUGGEST_ABORT;
  947.  
  948.         switch (SCpnt->sense_buffer[2] & 0xf)
  949.         {
  950.         case NO_SENSE:
  951.         case RECOVERED_ERROR:
  952.             return 0;
  953.  
  954.         case ABORTED_COMMAND:
  955.             return SUGGEST_RETRY;
  956.         case NOT_READY:
  957.         case UNIT_ATTENTION:
  958.             return SUGGEST_ABORT;
  959.  
  960.         /* these three are not supported */
  961.         case COPY_ABORTED:
  962.         case VOLUME_OVERFLOW:
  963.         case MISCOMPARE:
  964.  
  965.         case MEDIUM_ERROR:
  966.             return SUGGEST_REMAP;
  967.         case BLANK_CHECK:
  968.         case DATA_PROTECT:
  969.         case HARDWARE_ERROR:
  970.         case ILLEGAL_REQUEST:
  971.         default:
  972.             return SUGGEST_ABORT;
  973.         }
  974.           }
  975.  
  976. /* This function is the mid-level interrupt routine, which decides how
  977.  *  to handle error conditions.  Each invocation of this function must
  978.  *  do one and *only* one of the following:
  979.  *
  980.  *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
  981.  *    normal completion, and indicates that the handling for this
  982.  *    request is complete.
  983.  *  (2) Call internal_cmnd to requeue the command.  This will result in
  984.  *    scsi_done being called again when the retry is complete.
  985.  *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
  986.  *    more information about the error condition.  When the information
  987.  *    is available, scsi_done will be called again.
  988.  *  (4) Call reset().  This is sort of a last resort, and the idea is that
  989.  *    this may kick things loose and get the drive working again.  reset()
  990.  *    automatically calls scsi_request_sense, and thus scsi_done will be
  991.  *    called again once the reset is complete.
  992.  *
  993.  *    If none of the above actions are taken, the drive in question
  994.  * will hang. If more than one of the above actions are taken by
  995.  * scsi_done, then unpredictable behavior will result.
  996.  */
  997. static void scsi_done (Scsi_Cmnd * SCpnt)
  998.     {
  999.     int status=0;
  1000.     int exit=0;
  1001.     int checked;
  1002.     int oldto;
  1003.     struct Scsi_Host * host = SCpnt->host;
  1004.     int result = SCpnt->result;
  1005.     oldto = update_timeout(SCpnt, 0);
  1006.  
  1007. #define FINISHED 0
  1008. #define MAYREDO  1
  1009. #define REDO     3
  1010. #define PENDING  4
  1011.  
  1012. #ifdef DEBUG
  1013.     printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
  1014. #endif
  1015.     switch (host_byte(result))
  1016.     {
  1017.     case DID_OK:
  1018.         if (SCpnt->flags & IS_RESETTING)
  1019.             {
  1020.             SCpnt->flags &= ~IS_RESETTING;
  1021.             status = REDO;
  1022.             break;
  1023.             }
  1024.  
  1025.         if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
  1026.             /* Failed to obtain sense information */
  1027.             {
  1028.             SCpnt->flags &= ~WAS_SENSE;
  1029.             SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
  1030.  
  1031.             if (!(SCpnt->flags & WAS_RESET))
  1032.                 {
  1033.                 printk("scsi%d : target %d lun %d request sense failed, performing reset.\n",
  1034.                     SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
  1035.                 reset(SCpnt);
  1036.                 return;
  1037.                 }
  1038.             else
  1039.                 {
  1040.                 exit = (DRIVER_HARD | SUGGEST_ABORT);
  1041.                 status = FINISHED;
  1042.                 }
  1043.             }
  1044.         else switch(msg_byte(result))
  1045.             {
  1046.             case COMMAND_COMPLETE:
  1047.             switch (status_byte(result))
  1048.             {
  1049.             case GOOD:
  1050.                 if (SCpnt->flags & WAS_SENSE)
  1051.                     {
  1052. #ifdef DEBUG
  1053.     printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
  1054. #endif
  1055.  
  1056.                     SCpnt->flags &= ~WAS_SENSE;
  1057.                     SCpnt->internal_timeout &= ~SENSE_TIMEOUT;
  1058.  
  1059.                     switch (checked = check_sense(SCpnt))
  1060.                     {
  1061.                     case SUGGEST_SENSE:
  1062.                     case 0:
  1063. #ifdef DEBUG
  1064.     printk("NO SENSE.  status = REDO\n");
  1065. #endif
  1066.  
  1067.                         update_timeout(SCpnt, oldto);
  1068.                         status = REDO;
  1069.                         break;
  1070.                     case SUGGEST_REMAP:
  1071.                     case SUGGEST_RETRY:
  1072. #ifdef DEBUG
  1073.     printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
  1074. #endif
  1075.  
  1076.                         status = MAYREDO;
  1077.                         exit = DRIVER_SENSE | SUGGEST_RETRY;
  1078.                         break;
  1079.                     case SUGGEST_ABORT:
  1080. #ifdef DEBUG
  1081.     printk("SENSE SUGGEST ABORT - status = FINISHED");
  1082. #endif
  1083.  
  1084.                         status = FINISHED;
  1085.                         exit =    DRIVER_SENSE | SUGGEST_ABORT;
  1086.                         break;
  1087.                     default:
  1088.                         printk ("Internal error %s %d \n", __FILE__,
  1089.                             __LINE__);
  1090.                     }
  1091.                     }
  1092.                 else
  1093.                     {
  1094. #ifdef DEBUG
  1095.     printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
  1096. #endif
  1097.  
  1098.                     exit =    DRIVER_OK;
  1099.                     status = FINISHED;
  1100.                     }
  1101.                 break;
  1102.  
  1103.             case CHECK_CONDITION:
  1104.                 switch (check_sense(SCpnt))
  1105.                   {
  1106.                   case 0:
  1107.                     update_timeout(SCpnt, oldto);
  1108.                     status = REDO;
  1109.                     break;
  1110.                   case SUGGEST_REMAP:
  1111.                   case SUGGEST_RETRY:
  1112.                     status = MAYREDO;
  1113.                     exit = DRIVER_SENSE | SUGGEST_RETRY;
  1114.                     break;
  1115.                   case SUGGEST_ABORT:
  1116.                     status = FINISHED;
  1117.                     exit =  DRIVER_SENSE | SUGGEST_ABORT;
  1118.                     break;
  1119.                   case SUGGEST_SENSE:
  1120.                 scsi_request_sense (SCpnt);
  1121.                 status = PENDING;
  1122.                 break;
  1123.                   }
  1124.                 break;
  1125.  
  1126.             case CONDITION_GOOD:
  1127.             case INTERMEDIATE_GOOD:
  1128.             case INTERMEDIATE_C_GOOD:
  1129.                 break;
  1130.  
  1131.             case BUSY:
  1132.                 update_timeout(SCpnt, oldto);
  1133.                 status = REDO;
  1134.                 break;
  1135.  
  1136.             case RESERVATION_CONFLICT:
  1137.                 printk("scsi%d : RESERVATION CONFLICT performing reset.\n",
  1138.                     SCpnt->host->host_no);
  1139.                 reset(SCpnt);
  1140.                 return;
  1141. #if 0
  1142.                 exit = DRIVER_SOFT | SUGGEST_ABORT;
  1143.                 status = MAYREDO;
  1144.                 break;
  1145. #endif
  1146.             default:
  1147.                 printk ("Internal error %s %d \n"
  1148.                     "status byte = %d \n", __FILE__,
  1149.                     __LINE__, status_byte(result));
  1150.  
  1151.             }
  1152.             break;
  1153.             default:
  1154.                 panic("scsi: unsupported message byte %d recieved\n", msg_byte(result));
  1155.             }
  1156.             break;
  1157.     case DID_TIME_OUT:
  1158. #ifdef DEBUG
  1159.     printk("Host returned DID_TIME_OUT - ");
  1160. #endif
  1161.  
  1162.         if (SCpnt->flags & WAS_TIMEDOUT)
  1163.             {
  1164. #ifdef DEBUG
  1165.     printk("Aborting\n");
  1166. #endif
  1167.             exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
  1168.             }
  1169.         else
  1170.             {
  1171. #ifdef DEBUG
  1172.             printk ("Retrying.\n");
  1173. #endif
  1174.             SCpnt->flags  |= WAS_TIMEDOUT;
  1175.             status = REDO;
  1176.             }
  1177.         break;
  1178.     case DID_BUS_BUSY:
  1179.     case DID_PARITY:
  1180.         status = REDO;
  1181.         break;
  1182.     case DID_NO_CONNECT:
  1183. #ifdef DEBUG
  1184.         printk("Couldn't connect.\n");
  1185. #endif
  1186.         exit  = (DRIVER_HARD | SUGGEST_ABORT);
  1187.         break;
  1188.     case DID_ERROR:
  1189.         status = MAYREDO;
  1190.         exit = (DRIVER_HARD | SUGGEST_ABORT);
  1191.         break;
  1192.     case DID_BAD_TARGET:
  1193.     case DID_ABORT:
  1194.         exit = (DRIVER_INVALID | SUGGEST_ABORT);
  1195.         break;
  1196.     case DID_RESET:
  1197.         if(msg_byte(result) == GOOD &&
  1198.               status_byte(result) == CHECK_CONDITION) {
  1199.             switch (check_sense(SCpnt)) {
  1200.             case 0:
  1201.                 update_timeout(SCpnt, oldto);
  1202.                 status = REDO;
  1203.                 break;
  1204.             case SUGGEST_REMAP:
  1205.             case SUGGEST_RETRY:
  1206.                 status = MAYREDO;
  1207.                 exit = DRIVER_SENSE | SUGGEST_RETRY;
  1208.                 break;
  1209.             case SUGGEST_ABORT:
  1210.                 status = FINISHED;
  1211.                 exit =  DRIVER_SENSE | SUGGEST_ABORT;
  1212.                 break;
  1213.             case SUGGEST_SENSE:
  1214.                   scsi_request_sense (SCpnt);
  1215.                   status = PENDING;
  1216.                   break;
  1217.             }
  1218.         } else {
  1219.             status=REDO;
  1220.             exit = SUGGEST_RETRY;
  1221.         }
  1222.         break;
  1223.     default :
  1224.         exit = (DRIVER_ERROR | SUGGEST_DIE);
  1225.     }
  1226.  
  1227.     switch (status)
  1228.         {
  1229.         case FINISHED:
  1230.         case PENDING:
  1231.             break;
  1232.         case MAYREDO:
  1233.  
  1234. #ifdef DEBUG
  1235.     printk("In MAYREDO, allowing %d retries, have %d\n",
  1236.            SCpnt->allowed, SCpnt->retries);
  1237. #endif
  1238.  
  1239.             if ((++SCpnt->retries) < SCpnt->allowed)
  1240.             {
  1241.             if ((SCpnt->retries >= (SCpnt->allowed >> 1))
  1242.                 && !(SCpnt->flags & WAS_RESET))
  1243.                 {
  1244.                     printk("scsi%d : reseting for second half of retries.\n",
  1245.                         SCpnt->host->host_no);
  1246.                     reset(SCpnt);
  1247.                     break;
  1248.                 }
  1249.  
  1250.             }
  1251.             else
  1252.                 {
  1253.                 status = FINISHED;
  1254.                 break;
  1255.                 }
  1256.             /* fall through to REDO */
  1257.  
  1258.         case REDO:
  1259.             if (SCpnt->flags & WAS_SENSE)
  1260.                 scsi_request_sense(SCpnt);
  1261.             else
  1262.               {
  1263.                 memcpy ((void *) SCpnt->cmnd,
  1264.                     (void*) SCpnt->data_cmnd,
  1265.                     sizeof(SCpnt->data_cmnd));
  1266.                 SCpnt->request_buffer = SCpnt->buffer;
  1267.                 SCpnt->request_bufflen = SCpnt->bufflen;
  1268.                 SCpnt->use_sg = SCpnt->old_use_sg;
  1269.                 internal_cmnd (SCpnt);
  1270.               };
  1271.             break;
  1272.         default:
  1273.             INTERNAL_ERROR;
  1274.         }
  1275.  
  1276.     if (status == FINISHED)
  1277.         {
  1278.         #ifdef DEBUG
  1279.             printk("Calling done function - at address %p\n", SCpnt->done);
  1280.         #endif
  1281.         host->host_busy--; /* Indicate that we are free */
  1282.         wake_up(&host->host_wait);
  1283.         SCpnt->result = result | ((exit & 0xff) << 24);
  1284.         SCpnt->use_sg = SCpnt->old_use_sg;
  1285.         SCpnt->done (SCpnt);
  1286.         }
  1287.  
  1288.  
  1289. #undef FINISHED
  1290. #undef REDO
  1291. #undef MAYREDO
  1292. #undef PENDING
  1293.     }
  1294.  
  1295. /*
  1296.     The scsi_abort function interfaces with the abort() function of the host
  1297.     we are aborting, and causes the current command to not complete.  The
  1298.     caller should deal with any error messages or status returned on the
  1299.     next call.
  1300.  
  1301.     This will not be called rentrantly for a given host.
  1302. */
  1303.  
  1304. /*
  1305.     Since we're nice guys and specified that abort() and reset()
  1306.     can be non-reentrant.  The internal_timeout flags are used for
  1307.     this.
  1308. */
  1309.  
  1310.  
  1311. int scsi_abort (Scsi_Cmnd * SCpnt, int why)
  1312.     {
  1313.     int temp, oldto;
  1314.     struct Scsi_Host * host = SCpnt->host;
  1315.     unsigned long flags;
  1316.  
  1317.     while(1)
  1318.         {
  1319.         save_flags (flags);
  1320.         cli();
  1321.         if (SCpnt->internal_timeout & IN_ABORT)
  1322.             {
  1323.             restore_flags (flags);
  1324.             while (SCpnt->internal_timeout & IN_ABORT);
  1325.             }
  1326.         else
  1327.             {
  1328.             SCpnt->internal_timeout |= IN_ABORT;
  1329.             oldto = update_timeout(SCpnt, ABORT_TIMEOUT);
  1330.  
  1331.  
  1332.             restore_flags (flags);
  1333.             if (!host->host_busy || !host->hostt->abort(SCpnt, why))
  1334.                 temp =    0;
  1335.             else
  1336.                 temp = 1;
  1337.  
  1338.             cli();
  1339.             SCpnt->internal_timeout &= ~IN_ABORT;
  1340.             update_timeout(SCpnt, oldto);
  1341.             restore_flags (flags);
  1342.             return temp;
  1343.             }
  1344.         }
  1345.     }
  1346.  
  1347. int scsi_reset (Scsi_Cmnd * SCpnt)
  1348.     {
  1349.     int temp, oldto;
  1350.     Scsi_Cmnd * SCpnt1;
  1351.     struct Scsi_Host * host = SCpnt->host;
  1352.     unsigned long flags;
  1353.  
  1354. #ifdef DEBUG
  1355.     printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
  1356. #endif
  1357.     while (1) {
  1358.         save_flags (flags);
  1359.         cli();
  1360.         if (SCpnt->internal_timeout & IN_RESET)
  1361.             {
  1362.             restore_flags (flags);
  1363.             while (SCpnt->internal_timeout & IN_RESET);
  1364.             }
  1365.         else
  1366.             {
  1367.             SCpnt->internal_timeout |= IN_RESET;
  1368.             oldto = update_timeout(SCpnt, RESET_TIMEOUT);
  1369.  
  1370.             if (host->host_busy)
  1371.                 {
  1372.                 restore_flags (flags);
  1373.                 SCpnt1 = host->host_queue;
  1374.                 while(SCpnt1) {
  1375.                   if ((SCpnt1->request.dev > 0) &&
  1376.                       !(SCpnt1->flags & IS_RESETTING) &&
  1377.                       !(SCpnt1->internal_timeout & IN_ABORT))
  1378.                     scsi_abort(SCpnt1, DID_RESET);
  1379.                   SCpnt1 = SCpnt1->next;
  1380.                 };
  1381.  
  1382.                 temp = host->hostt->reset(SCpnt);
  1383.                 }
  1384.             else
  1385.                 {
  1386.                 host->host_busy++;
  1387.  
  1388.                 restore_flags(flags);
  1389.                 temp = host->hostt->reset(SCpnt);
  1390.                 host->last_reset = jiffies;
  1391.                 host->host_busy--;
  1392.                 }
  1393.  
  1394.             cli();
  1395.             SCpnt->internal_timeout &= ~IN_RESET;
  1396.             update_timeout(SCpnt, oldto);
  1397.             restore_flags(flags);
  1398.             return temp;
  1399.             }
  1400.         }
  1401.     }
  1402.  
  1403.  
  1404. static void scsi_main_timeout(void)
  1405.     {
  1406.     /*
  1407.         We must not enter update_timeout with a timeout condition still pending.
  1408.     */
  1409.  
  1410.     int timed_out;
  1411.     struct Scsi_Host * host;
  1412.     Scsi_Cmnd * SCpnt = NULL;
  1413.     unsigned long flags;
  1414.  
  1415.     do    {
  1416.         save_flags (flags);
  1417.         cli();
  1418.  
  1419.     /*
  1420.         Find all timers such that they have 0 or negative (shouldn't happen)
  1421.         time remaining on them.
  1422.     */
  1423.  
  1424.         timed_out = 0;
  1425.         for(host = scsi_hostlist; host; host = host->next) {
  1426.           SCpnt = host->host_queue;
  1427.           while (SCpnt){
  1428.             if (SCpnt->timeout > 0 && SCpnt->timeout <= time_elapsed)
  1429.               {
  1430.             restore_flags(flags);
  1431.             SCpnt->timeout = 0;
  1432.             scsi_times_out(SCpnt);
  1433.             ++timed_out;
  1434.             cli();
  1435.               }
  1436.           SCpnt =  SCpnt->next;
  1437.           };
  1438.         };
  1439.         update_timeout(NULL, 0);
  1440.           } while (timed_out);
  1441.     restore_flags(flags);
  1442.       }
  1443.  
  1444. /*
  1445.     The strategy is to cause the timer code to call scsi_times_out()
  1446.     when the soonest timeout is pending.
  1447.     The arguments are used when we are queueing a new command, because
  1448.     we do not want to subtract the time used from this time, but when we
  1449.     set the timer, we want to take this value into account.
  1450. */
  1451.  
  1452. static int update_timeout(Scsi_Cmnd * SCset, int timeout)
  1453.     {
  1454.     unsigned int least, used;
  1455.     unsigned int oldto;
  1456.     struct Scsi_Host * host;
  1457.     unsigned long flags;
  1458.     Scsi_Cmnd * SCpnt = NULL;
  1459.  
  1460.     save_flags (flags);
  1461.     cli();
  1462.  
  1463. /*
  1464.     Figure out how much time has passed since the last time the timeouts
  1465.     were updated
  1466. */
  1467.     used = (time_start) ? (jiffies - time_start) : 0;
  1468.  
  1469. /*
  1470.     Find out what is due to timeout soonest, and adjust all timeouts for
  1471.     the amount of time that has passed since the last time we called
  1472.     update_timeout.
  1473. */
  1474.  
  1475.     oldto = 0;
  1476.  
  1477.     if(SCset){
  1478.       oldto = SCset->timeout - used;
  1479.       SCset->timeout = timeout + used;
  1480.     };
  1481.  
  1482.     least = 0xffffffff;
  1483.  
  1484.     for(host = scsi_hostlist; host; host = host->next) {
  1485.       SCpnt = host->host_queue;
  1486.       while (SCpnt){
  1487.         if (SCpnt->timeout > 0 && (SCpnt->timeout -= used) < least)
  1488.           least = SCpnt->timeout;
  1489.         SCpnt =  SCpnt->next;
  1490.       };
  1491.     };
  1492.  
  1493. /*
  1494.     If something is due to timeout again, then we will set the next timeout
  1495.     interrupt to occur.  Otherwise, timeouts are disabled.
  1496. */
  1497.  
  1498.     if (least != 0xffffffff)
  1499.         {
  1500.         time_start = jiffies;
  1501.         timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
  1502.         timer_active |= 1 << SCSI_TIMER;
  1503.         }
  1504.     else
  1505.         {
  1506.         timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
  1507.         timer_active &= ~(1 << SCSI_TIMER);
  1508.         }
  1509.     restore_flags (flags);
  1510.     return oldto;
  1511.     }
  1512.  
  1513.  
  1514. static unsigned short * dma_malloc_freelist = NULL;
  1515. static unsigned int dma_sectors = 0;
  1516. unsigned int dma_free_sectors = 0;
  1517. unsigned int need_isa_buffer = 0;
  1518. static unsigned char * dma_malloc_buffer = NULL;
  1519.  
  1520. void *scsi_malloc(unsigned int len)
  1521. {
  1522.   unsigned int nbits, mask;
  1523.   int i, j;
  1524.   unsigned long flags;
  1525.  
  1526.   if((len & 0x1ff) || len > 4096)
  1527.     panic("Inappropriate buffer size requested");
  1528.  
  1529.   save_flags (flags);
  1530.   cli();
  1531.   nbits = len >> 9;
  1532.   mask = (1 << nbits) - 1;
  1533.  
  1534.   for(i=0;i < (dma_sectors >> 4); i++)
  1535.     for(j=0; j<17-nbits; j++){
  1536.       if ((dma_malloc_freelist[i] & (mask << j)) == 0){
  1537.     dma_malloc_freelist[i] |= (mask << j);
  1538.     restore_flags (flags);
  1539.     dma_free_sectors -= nbits;
  1540. #ifdef DEBUG
  1541.     printk("SMalloc: %d %p ",len, dma_malloc_buffer + (i << 13) + (j << 9));
  1542. #endif
  1543.     return (void *) ((unsigned long) dma_malloc_buffer + (i << 13) + (j << 9));
  1544.       };
  1545.     };
  1546.   restore_flags (flags);
  1547.   return NULL;    /* Nope.  No more */
  1548. }
  1549.  
  1550. int scsi_free(void *obj, unsigned int len)
  1551. {
  1552.   int offset;
  1553.   int page, sector, nbits, mask;
  1554.   unsigned long flags;
  1555.  
  1556. #ifdef DEBUG
  1557.   printk("Sfree %p %d\n",obj, len);
  1558. #endif
  1559.  
  1560.   offset = ((int) obj) - ((int) dma_malloc_buffer);
  1561.  
  1562.   if (offset < 0) panic("Bad offset");
  1563.   page = offset >> 13;
  1564.   sector = offset >> 9;
  1565.   if(sector >= dma_sectors) panic ("Bad page");
  1566.  
  1567.   sector = (offset >> 9) & 15;
  1568.   nbits = len >> 9;
  1569.   mask = (1 << nbits) - 1;
  1570.  
  1571.   if ((mask << sector) > 0xffff) panic ("Bad memory alignment");
  1572.  
  1573.   save_flags (flags);
  1574.   cli();
  1575.   if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
  1576.     panic("Trying to free unused memory");
  1577.  
  1578.   dma_free_sectors += nbits;
  1579.   dma_malloc_freelist[page] &= ~(mask << sector);
  1580.   restore_flags (flags);
  1581.   return 0;
  1582. }
  1583.  
  1584. /*
  1585.     scsi_dev_init() is our initialization routine, which inturn calls host
  1586.     initialization, bus scanning, and sd/st initialization routines.  It
  1587.     should be called from main().
  1588. */
  1589.  
  1590. unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
  1591.     {
  1592.     int i;
  1593.     struct Scsi_Host * host;
  1594.     Scsi_Cmnd * SCpnt;
  1595. #ifdef FOO_ON_YOU
  1596.     return;
  1597. #endif
  1598.     timer_table[SCSI_TIMER].fn = scsi_main_timeout;
  1599.     timer_table[SCSI_TIMER].expires = 0;
  1600.  
  1601.     /* initialize all hosts */
  1602.     memory_start = scsi_init(memory_start, memory_end); 
  1603.  
  1604. #ifdef WDEBUG
  1605.     printk ("&host = %p\n", &host);
  1606.     printk ("press left mouse button to continue\n");
  1607.     waitbut();
  1608. #endif
  1609.  
  1610.     scsi_devices = (Scsi_Device *) memory_start;
  1611.     scan_scsis();           /* scan for scsi devices */
  1612.     memory_start += NR_SCSI_DEVICES * sizeof(Scsi_Device);
  1613.  
  1614.     memory_start = sd_init1(memory_start, memory_end);
  1615.     memory_start = st_init1(memory_start, memory_end);
  1616.     memory_start = sr_init1(memory_start, memory_end);
  1617.     memory_start = sg_init1(memory_start, memory_end);
  1618.  
  1619.     last_cmnd = (Scsi_Cmnd *) memory_start;
  1620.  
  1621.     SCpnt = last_cmnd;
  1622.  
  1623.     for (i=0; i< NR_SCSI_DEVICES; i++) {
  1624.       int j;
  1625.       switch (scsi_devices[i].type)
  1626.         {
  1627.         case TYPE_TAPE :
  1628.           st_attach(&scsi_devices[i]);
  1629.           break;
  1630.         case TYPE_ROM:
  1631.           sr_attach(&scsi_devices[i]);
  1632.           break;
  1633.         case TYPE_DISK:
  1634.         case TYPE_MOD:
  1635.           sd_attach(&scsi_devices[i]);
  1636.         default:
  1637.           break;
  1638.         };
  1639.       sg_attach(&scsi_devices[i]);
  1640.       if(scsi_devices[i].type != -1){
  1641.         for(j=0;j<scsi_devices[i].host->hostt->cmd_per_lun;j++){
  1642.           SCpnt->host = scsi_devices[i].host;
  1643.           SCpnt->target = scsi_devices[i].id;
  1644.           SCpnt->lun = scsi_devices[i].lun;
  1645.           SCpnt->index = i;
  1646.           SCpnt->request.dev = -1; /* Mark not busy */
  1647.           SCpnt->use_sg = 0;
  1648.           SCpnt->old_use_sg = 0;
  1649.           SCpnt->underflow = 0;
  1650.           SCpnt->transfersize = 0;
  1651.           SCpnt->host_scribble = NULL;
  1652.           host = scsi_devices[i].host;
  1653.           if(host->host_queue)
  1654.         host->host_queue->prev = SCpnt;
  1655.           SCpnt->next = host->host_queue;
  1656.           SCpnt->prev = NULL;
  1657.           host->host_queue = SCpnt;
  1658.           SCpnt++;
  1659.         };
  1660.       };
  1661.     };
  1662.  
  1663.     memory_start = (int) SCpnt;
  1664.  
  1665.     if (NR_SD > 0 || NR_SR > 0 || NR_ST > 0)
  1666.       dma_sectors = 16;  /* Base value we use */
  1667.  
  1668.     for (i = 0; i < NR_SCSI_DEVICES; ++i) {
  1669.       struct Scsi_Host * host;
  1670.       host = scsi_devices[i].host;
  1671.  
  1672.       if(scsi_devices[i].type != TYPE_TAPE)
  1673.         dma_sectors += ((host->sg_tablesize * 
  1674.                  sizeof(struct scatterlist) + 511) >> 9) *
  1675.                    host->hostt->cmd_per_lun;
  1676.  
  1677.       if(host->unchecked_isa_dma &&
  1678.          memory_end > ISA_DMA_THRESHOLD &&
  1679.          scsi_devices[i].type != TYPE_TAPE) {
  1680.         dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
  1681.           host->hostt->cmd_per_lun;
  1682.         need_isa_buffer++;
  1683.       }; 
  1684.     };
  1685.  
  1686.     dma_sectors = (dma_sectors + 15) & 0xfff0;
  1687.     dma_free_sectors = dma_sectors;  /* This must be a multiple of 16 */
  1688.  
  1689.     memory_start = (memory_start + 3) & 0xfffffffc;
  1690.     dma_malloc_freelist = (unsigned short *) memory_start;
  1691.     memory_start += dma_sectors >> 3;
  1692.     memset(dma_malloc_freelist, 0, dma_sectors >> 3);
  1693.   
  1694.     if(memory_start & 1) memory_start++; /* Some host adapters require
  1695.                         buffers to be word aligned */
  1696.     dma_malloc_buffer = (unsigned char *) memory_start;
  1697.     memory_start += dma_sectors << 9;
  1698.  
  1699.     memory_start = sd_init(memory_start, memory_end); /* init scsi disks */
  1700.     memory_start = st_init(memory_start, memory_end); /* init scsi tapes */
  1701.     memory_start = sr_init(memory_start, memory_end); /* init scsi CDROMs */
  1702.     return memory_start;
  1703.     }
  1704.  
  1705. static void print_inquiry(unsigned char *data)
  1706. {
  1707.     int i;
  1708.  
  1709.     printk("  Vendor: ");
  1710.     for (i = 8; i < 16; i++)
  1711.         {
  1712.             if (data[i] >= 0x20 && i < data[4] + 5)
  1713.             printk("%c", data[i]);
  1714.         else
  1715.             printk(" ");
  1716.         }
  1717.  
  1718.     printk("  Model: ");
  1719.     for (i = 16; i < 32; i++)
  1720.         {
  1721.             if (data[i] >= 0x20 && i < data[4] + 5)
  1722.             printk("%c", data[i]);
  1723.         else
  1724.             printk(" ");
  1725.         }
  1726.  
  1727.     printk("  Rev: ");
  1728.     for (i = 32; i < 36; i++)
  1729.         {
  1730.             if (data[i] >= 0x20 && i < data[4] + 5)
  1731.             printk("%c", data[i]);
  1732.         else
  1733.             printk(" ");
  1734.         }
  1735.  
  1736.     printk("\n");
  1737.  
  1738.     i = data[0] & 0x1f;
  1739.  
  1740.     printk("  Type:   %s ",
  1741.            i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
  1742.     printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
  1743.     if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
  1744.       printk(" CCS\n");
  1745.     else
  1746.       printk("\n");
  1747. }
  1748.