home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / kernel-s / v1.1 / scsi / 274x / aha274x / aha274x.c < prev    next >
C/C++ Source or Header  |  1995-10-10  |  39KB  |  1,429 lines

  1. /*
  2.  *  @(#)aha274x.c 1.25 94/09/06 jda
  3.  *
  4.  *  Adaptec 274x device driver for Linux.
  5.  *  Copyright (c) 1994 The University of Calgary Department of Computer Science.
  6.  *  
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *  
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *  
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  *  Sources include the Adaptec 1740 driver (aha1740.c), the
  22.  *  Ultrastor 24F driver (ultrastor.c), various Linux kernel
  23.  *  source, the Adaptec EISA config file (!adp7771.cfg), the
  24.  *  Adaptec AHA-2740A Series User's Guide, the Linux Kernel
  25.  *  Hacker's Guide, Writing a SCSI Device Driver for Linux,
  26.  *  the Adaptec 1542 driver (aha1542.c), the Adaptec EISA
  27.  *  overlay file (adp7770.ovl), the Adaptec AHA-2740 Series
  28.  *  Technical Reference Manual, the Adaptec AIC-7770 Data
  29.  *  Book, the ANSI SCSI specification, the ANSI SCSI-2
  30.  *  specification (draft 10c), ...
  31.  *
  32.  *  On a twin-bus adapter card, channel B is ignored.  Rationale:
  33.  *  it would greatly complicate the sequencer and host driver code,
  34.  *  and both busses are multiplexed on to the EISA bus anyway.  So
  35.  *  I don't really see any technical advantage to supporting both.
  36.  *
  37.  *  As well, multiple adapter card using the same IRQ level are
  38.  *  not supported.  It doesn't make sense to configure the cards
  39.  *  this way from a performance standpoint.  Not to mention that
  40.  *  the kernel would have to support two devices per registered IRQ.
  41.  */
  42.  
  43. #include <stdarg.h>
  44. #include <asm/io.h>
  45. #include <linux/string.h>
  46. #include <linux/kernel.h>
  47. #include <linux/ioport.h>
  48. #include <linux/delay.h>
  49.  
  50. #include "../block/blk.h"
  51. #include "sd.h"
  52. #include "scsi.h"
  53. #include "hosts.h"
  54. #include "aha274x.h"
  55.  
  56. /*
  57.  *  There should be a specific return value for this in scsi.h, but
  58.  *  it seems that most drivers ignore it.
  59.  */
  60. #define DID_UNDERFLOW    DID_ERROR
  61.  
  62. /* EISA stuff */
  63.  
  64. #define MINEISA        1
  65. #define MAXEISA        15
  66. #define SLOTBASE(x)    ((x) << 12)
  67.  
  68. #define MAXIRQ        15
  69.  
  70. /* AIC-7770 offset definitions */
  71.  
  72. #define O_MINREG(x)    ((x) + 0xc00)        /* i/o range to reserve */
  73. #define O_MAXREG(x)    ((x) + 0xcbf)
  74.  
  75. #define O_SCSISEQ(x)    ((x) + 0xc00)        /* scsi sequence control */
  76. #define O_SCSISIGI(x)    ((x) + 0xc03)        /* scsi control signal read */
  77. #define O_SCSISIGO(x)    ((x) + 0xc03)        /* scsi control signal write */
  78. #define O_SCSIID(x)    ((x) + 0xc05)        /* scsi id */
  79. #define O_SSTAT0(x)    ((x) + 0xc0b)        /* scsi status register 0 */
  80. #define O_CLRSINT1(x)    ((x) + 0xc0c)        /* clear scsi interrupt 1 */
  81. #define O_SSTAT1(x)    ((x) + 0xc0c)        /* scsi status register 1 */
  82. #define O_SELID(x)    ((x) + 0xc19)        /* [re]selection id */
  83. #define O_SBLKCTL(x)    ((x) + 0xc1f)        /* scsi block control */
  84. #define O_SEQCTL(x)    ((x) + 0xc60)        /* sequencer control */
  85. #define O_SEQRAM(x)    ((x) + 0xc61)        /* sequencer ram data */
  86. #define O_SEQADDR(x)    ((x) + 0xc62)        /* sequencer address (W) */
  87. #define O_BIDx(x)    ((x) + 0xc80)        /* board id */
  88. #define O_BCTL(x)    ((x) + 0xc84)        /* board control */
  89. #define O_HCNTRL(x)    ((x) + 0xc87)        /* host control */
  90. #define O_SCBPTR(x)    ((x) + 0xc90)        /* scb pointer */
  91. #define O_INTSTAT(x)    ((x) + 0xc91)        /* interrupt status */
  92. #define O_ERROR(x)    ((x) + 0xc92)        /* hard error */
  93. #define O_CLRINT(x)    ((x) + 0xc92)        /* clear interrupt status */
  94. #define O_SCBCNT(x)    ((x) + 0xc9a)        /* scb auto increment */
  95. #define O_QINFIFO(x)    ((x) + 0xc9b)        /* queue in fifo */
  96. #define O_QINCNT(x)    ((x) + 0xc9c)        /* queue in count */
  97. #define O_QOUTFIFO(x)    ((x) + 0xc9d)        /* queue out fifo */
  98. #define O_QOUTCNT(x)    ((x) + 0xc9e)        /* queue out count */
  99. #define O_SCBARRAY(x)    ((x) + 0xca0)        /* scb array start */
  100.  
  101. /* host adapter offset definitions */
  102.  
  103. #define HA_REJBYTE(x)    ((x) + 0xc31)        /* 1st message in byte */
  104. #define HA_MSG_FLAGS(x)    ((x) + 0xc35)        /* outgoing message flag */
  105. #define HA_MSG_LEN(x)    ((x) + 0xc36)        /* outgoing message length */
  106. #define HA_MSG_START(x)    ((x) + 0xc37)        /* outgoing message body */
  107. #define HA_ARG_1(x)    ((x) + 0xc4c)        /* sdtr <-> rate parameters */
  108. #define HA_ARG_2(x)    ((x) + 0xc4d)
  109. #define HA_RETURN_1(x)    ((x) + 0xc4c)
  110. #define HA_RETURN_2(x)    ((x) + 0xc4d)
  111. #define HA_SIGSTATE(x)    ((x) + 0xc4e)        /* value in SCSISIGO */
  112. #define HA_NEEDSDTR(x)    ((x) + 0xc4f)        /* synchronous negotiation? */
  113.  
  114. #define HA_SCSICONF(x)    ((x) + 0xc5a)        /* SCSI config register */
  115. #define HA_INTDEF(x)    ((x) + 0xc5c)        /* interrupt def'n register */
  116. #define HA_HOSTCONF(x)    ((x) + 0xc5d)        /* host config def'n register */
  117.  
  118. /* debugging code */
  119.  
  120. #define AHA274X_DEBUG
  121.  
  122. /*
  123.  *  If a parity error occurs during a data transfer phase, run the
  124.  *  command to completion - it's easier that way - making a note
  125.  *  of the error condition in this location.  This then will modify
  126.  *  a DID_OK status into a DID_PARITY one for the higher-level SCSI
  127.  *  code.
  128.  */
  129. #define aha274x_parity(cmd)    ((cmd)->SCp.Status)
  130.  
  131. /*
  132.  *  Since the sequencer code DMAs the scatter-gather structures
  133.  *  directly from memory, we use this macro to assert that the
  134.  *  kernel structure hasn't changed.
  135.  */
  136. #define SG_STRUCT_CHECK(sg) \
  137.     ((char *)&(sg).address - (char *)&(sg) != 0 ||    \
  138.      (char *)&(sg).length  - (char *)&(sg) != 8 ||    \
  139.      sizeof((sg).address) != 4 ||            \
  140.      sizeof((sg).length)  != 4 ||            \
  141.      sizeof(sg)          != 12)
  142.  
  143. /*
  144.  *  "Static" structures.  Note that these are NOT initialized
  145.  *  to zero inside the kernel - we have to initialize them all
  146.  *  explicitly.
  147.  *
  148.  *  We support a maximum of one adapter card per IRQ level (see the
  149.  *  rationale for this above).  On an interrupt, use the IRQ as an
  150.  *  index into aha274x_boards[] to locate the card information.
  151.  */
  152. static struct Scsi_Host *aha274x_boards[MAXIRQ + 1];
  153.  
  154. struct aha274x_host {
  155.     int base;                    /* card base address */
  156.     int unpause;                    /* value for HCNTRL */
  157.     volatile Scsi_Cmnd *SCB_array[AHA274X_MAXSCB];    /* active commands */
  158. };
  159.  
  160. struct aha274x_scb {
  161.     unsigned char control;
  162.     unsigned char target_channel_lun;        /* 4/1/3 bits */
  163.     unsigned char SG_segment_count;
  164.     unsigned char SG_list_pointer[4];
  165.     unsigned char SCSI_cmd_pointer[4];
  166.     unsigned char SCSI_cmd_length;
  167.     unsigned char RESERVED[2];            /* must be zero */
  168.     unsigned char target_status;
  169.     unsigned char residual_data_count[3];
  170.     unsigned char residual_SG_segment_count;
  171.     unsigned char data_pointer[4];
  172.     unsigned char data_count[3];
  173. #if 0
  174.     /*
  175.      *  No real point in transferring this to the
  176.      *  SCB registers.
  177.      */
  178.     unsigned char RESERVED[6];
  179. #endif
  180. };
  181.  
  182. /*
  183.  *  NB.  This table MUST be ordered shortest period first.
  184.  */
  185. static struct {
  186.     short period;
  187.     short rate;
  188.     char *english;
  189. } aha274x_synctab[] = {
  190.     100,    0,    "10.0",
  191.     125,    1,    "8.0",
  192.     150,    2,    "6.67",
  193.     175,    3,    "5.7",
  194.     200,    4,    "5.0",
  195.     225,    5,    "4.4",
  196.     250,    6,    "4.0",
  197.     275,    7,    "3.6"
  198. };
  199.  
  200. static int aha274x_synctab_max =
  201.     sizeof(aha274x_synctab) / sizeof(aha274x_synctab[0]);
  202.  
  203. enum aha_type {
  204.     T_NONE,
  205.     T_274X,
  206.     T_284X,
  207.     T_MAX
  208. };
  209.  
  210. #ifdef AHA274X_DEBUG
  211.  
  212.     extern int vsprintf(char *, const char *, va_list);
  213.  
  214.     static
  215.     void debug(const char *fmt, ...)
  216.     {
  217.         va_list ap;
  218.         char buf[256];
  219.  
  220.         va_start(ap, fmt);
  221.           vsprintf(buf, fmt, ap);
  222.           printk(buf);
  223.         va_end(ap);
  224.     }
  225.  
  226.     static
  227.     void debug_config(enum aha_type type, int base)
  228.     {
  229.         int ioport2, ioport3, ioport4;
  230.  
  231.         static char *BRT[T_MAX][16] = {
  232.             { },                    /* T_NONE */
  233.             {
  234.                 "2",   "???", "???", "12",    /* T_274X */
  235.                 "???", "???", "???", "28",
  236.                 "???", "???", "???", "44",
  237.                 "???", "???", "???", "60"
  238.             },
  239.             {
  240.                 "2",  "4",  "8",  "12",        /* T_284X */
  241.                 "16", "20", "24", "28",
  242.                 "32", "36", "40", "44",
  243.                 "48", "52", "56", "60"
  244.             }
  245.         };
  246.         static int DFT[4] = {
  247.             0, 50, 75, 100
  248.         };
  249.         static int SST[4] = {
  250.             256, 128, 64, 32
  251.         };
  252.  
  253.         ioport2 = inb(HA_HOSTCONF(base));
  254.         ioport3 = inb(HA_SCSICONF(base));
  255.         ioport4 = inb(HA_INTDEF(base));
  256.  
  257.         if (type == T_284X)
  258.             printk("AHA284X AT SLOT %d:\n", base >> 12);
  259.         else
  260.             printk("AHA274X AT EISA SLOT %d:\n", base >> 12);
  261.  
  262.         printk("    irq %d\n"
  263.                "    bus release time %s bclks\n"
  264.                "    data fifo threshold %d%%\n",
  265.                ioport4 & 0xf,
  266.                BRT[type][(ioport2 >> 2) & 0xf],
  267.                DFT[(ioport2 >> 6) & 0x3]);
  268.  
  269.         printk("    SCSI CHANNEL A:\n"
  270.                "        scsi id %d\n"
  271.                "        scsi bus parity check %sabled\n"
  272.                "        scsi selection timeout %d ms\n"
  273.                "        scsi bus reset at power-on %sabled\n",
  274.                ioport3 & 0x7,
  275.                (ioport3 & 0x20) ? "en" : "dis",
  276.                SST[(ioport3 >> 3) & 0x3],
  277.                (ioport3 & 0x40) ? "en" : "dis");
  278.  
  279.         if (type == T_274X) {
  280.             printk("        scsi bus termination %sabled\n",
  281.                    (ioport3 & 0x80) ? "en" : "dis");
  282.         }
  283.     }
  284.  
  285.     static
  286.     void debug_rate(int base, int rate)
  287.     {
  288.         int target = inb(O_SCSIID(base)) >> 4;
  289.  
  290.         if (rate) {
  291.             printk("aha274x: target %d now synchronous at %sMb/s\n",
  292.                    target,
  293.                    aha274x_synctab[(rate >> 4) & 0x7].english);
  294.         } else {
  295.             printk("aha274x: target %d using asynchronous mode\n",
  296.                    target);
  297.         }
  298.     }
  299.  
  300. #else
  301.  
  302. #    define debug(fmt, args...)
  303. #    define debug_config(x)
  304. #    define debug_rate(x,y)
  305.  
  306. #endif AHA274X_DEBUG
  307.  
  308. static
  309. void aha274x_getscb(int base, struct aha274x_scb *scb)
  310. {
  311.     /*
  312.      *  This is almost identical to aha274x_putscb().
  313.      */
  314.     outb(0x80, O_SCBCNT(base));    /* SCBAUTO */
  315.  
  316.     asm volatile("cld\n\t"
  317.              "rep\n\t"
  318.              "insb"
  319.              : /* no output */
  320.              :"D" (scb), "c" (sizeof(*scb)), "d" (O_SCBARRAY(base))
  321.              :"di", "cx", "dx");
  322.  
  323.     outb(0, O_SCBCNT(base));
  324. }
  325.  
  326. /*
  327.  *  How much data should be transferred for this SCSI command?  Stop
  328.  *  at segment sg_last if it's a scatter-gather command so we can
  329.  *  compute underflow easily.
  330.  */
  331. static
  332. unsigned aha274x_length(Scsi_Cmnd *cmd, int sg_last)
  333. {
  334.     int i, segments;
  335.     unsigned length;
  336.     struct scatterlist *sg;
  337.  
  338.     segments = cmd->use_sg - sg_last;
  339.     sg = (struct scatterlist *)cmd->buffer;
  340.  
  341.     if (cmd->use_sg) {
  342.         for (i = length = 0;
  343.              i < cmd->use_sg && i < segments;
  344.              i++)
  345.         {
  346.             length += sg[i].length;
  347.         }
  348.     } else
  349.         length = cmd->request_bufflen;
  350.  
  351.     return(length);
  352. }
  353.  
  354. static
  355. void aha274x_sg_check(Scsi_Cmnd *cmd)
  356. {
  357.     int i;
  358.     struct scatterlist *sg = (struct scatterlist *)cmd->buffer;
  359.  
  360.     if (cmd->use_sg) {
  361.         for (i = 0; i < cmd->use_sg; i++)
  362.             if ((unsigned)sg[i].length > 0xffff)
  363.                 panic("aha274x_sg_check: s/g segment > 64k\n");
  364.     }
  365. }
  366.  
  367. static
  368. void aha274x_to_scsirate(unsigned char *rate,
  369.              unsigned char transfer,
  370.              unsigned char offset)
  371. {
  372.     int i;
  373.  
  374.     transfer *= 4;
  375.  
  376.     for (i = 0; i < aha274x_synctab_max-1; i++) {
  377.  
  378.         if (transfer == aha274x_synctab[i].period) {
  379.             *rate = (aha274x_synctab[i].rate << 4) | (offset & 0xf);
  380.             return;
  381.         }
  382.  
  383.         if (transfer > aha274x_synctab[i].period &&
  384.             transfer < aha274x_synctab[i+1].period)
  385.         {
  386.             *rate = (aha274x_synctab[i+1].rate << 4) |
  387.                 (offset & 0xf);
  388.             return;
  389.         }
  390.     }
  391.     *rate = 0;
  392. }
  393.  
  394. /*
  395.  *  Pause the sequencer and wait for it to actually stop - this
  396.  *  is important since the sequencer can disable pausing for critical
  397.  *  sections.
  398.  */
  399. #define PAUSE_SEQUENCER(p)    \
  400.     do {                                \
  401.         outb(0xe, O_HCNTRL(p->base));    /* IRQMS|PAUSE|INTEN */    \
  402.                                     \
  403.         while ((inb(O_HCNTRL(p->base)) & 0x4) == 0)        \
  404.             ;                        \
  405.     } while (0)
  406.  
  407. /*
  408.  *  Unpause the sequencer.  Unremarkable, yet done often enough to
  409.  *  warrant an easy way to do it.
  410.  */
  411. #define UNPAUSE_SEQUENCER(p)    \
  412.     outb(p->unpause, O_HCNTRL(p->base))    /* IRQMS|INTEN */
  413.  
  414. /*
  415.  *  See comments in aha274x_loadram() wrt this.
  416.  */
  417. #define RESTART_SEQUENCER(p)    \
  418.     do {                        \
  419.         do {                    \
  420.             outb(0x2, O_SEQCTL(p->base));    \
  421.         } while (inw(O_SEQADDR(p->base)) != 0);    \
  422.                             \
  423.         UNPAUSE_SEQUENCER(p);            \
  424.     } while (0)
  425.  
  426. /*
  427.  *  Since we declared this using SA_INTERRUPT, interrupts should
  428.  *  be disabled all through this function unless we say otherwise.
  429.  */
  430. static
  431. void aha274x_isr(int irq)
  432. {
  433.     int base, intstat;
  434.     struct aha274x_host *p;
  435.     
  436.     p = (struct aha274x_host *)aha274x_boards[irq]->hostdata;
  437.     base = p->base;
  438.  
  439.     /*
  440.      *  Handle all the interrupt sources - especially for SCSI
  441.      *  interrupts, we won't get a second chance at them.
  442.      */
  443.     intstat = inb(O_INTSTAT(base));
  444.  
  445.     if (intstat & 0x8) {                /* BRKADRINT */
  446.  
  447.         panic("aha274x_isr: brkadrint, error = 0x%x, seqaddr = 0x%x\n",
  448.               inb(O_ERROR(base)), inw(O_SEQADDR(base)));
  449.     }
  450.  
  451.     if (intstat & 0x4) {                /* SCSIINT */
  452.  
  453.         int scbptr = inb(O_SCBPTR(base));
  454.         int status = inb(O_SSTAT1(base));
  455.         Scsi_Cmnd *cmd;
  456.  
  457.         cmd = (Scsi_Cmnd *)p->SCB_array[scbptr];
  458.         if (!cmd) {
  459.             printk("aha274x_isr: no command for scb (scsiint)\n");
  460.             /*
  461.              *  Turn off the interrupt and set status
  462.              *  to zero, so that it falls through the
  463.              *  reset of the SCSIINT code.
  464.              */
  465.             outb(status, O_CLRSINT1(base));
  466.             UNPAUSE_SEQUENCER(p);
  467.             outb(0x4, O_CLRINT(base));    /* undocumented */
  468.             status = 0;
  469.         }
  470.         p->SCB_array[scbptr] = NULL;
  471.  
  472.         /*
  473.          *  Only the SCSI Status 1 register has information
  474.          *  about exceptional conditions that we'd have a
  475.          *  SCSIINT about; anything in SSTAT0 will be handled
  476.          *  by the sequencer.  Note that there can be multiple
  477.          *  bits set.
  478.          */
  479.         if (status & 0x80) {            /* SELTO */
  480.             /*
  481.              *  Hardware selection timer has expired.  Turn
  482.              *  off SCSI selection sequence.
  483.              */
  484.             outb(0, O_SCSISEQ(base));
  485.             cmd->result = DID_TIME_OUT << 16;
  486.  
  487.             /*
  488.              *  If there's an active message, it belongs to the
  489.              *  command that is getting punted - remove it.
  490.              */
  491.             outb(0, HA_MSG_FLAGS(base));
  492.  
  493.             /*
  494.              *  Shut off the offending interrupt sources, reset
  495.              *  the sequencer address to zero and unpause it,
  496.              *  then call the high-level SCSI completion routine.
  497.              *
  498.              *  WARNING!  This is a magic sequence!  After many
  499.              *  hours of guesswork, turning off the SCSI interrupts
  500.              *  in CLRSINT? does NOT clear the SCSIINT bit in
  501.              *  INTSTAT.  By writing to the (undocumented, unused
  502.              *  according to the AIC-7770 manual) third bit of
  503.              *  CLRINT, you can clear INTSTAT.  But, if you do it
  504.              *  while the sequencer is paused, you get a BRKADRINT
  505.              *  with an Illegal Host Address status, so the
  506.              *  sequencer has to be restarted first.
  507.              */
  508.             outb(0x80, O_CLRSINT1(base));    /* CLRSELTIMO */
  509.             RESTART_SEQUENCER(p);
  510.  
  511.             outb(0x4, O_CLRINT(base));    /* undocumented */
  512.             cmd->scsi_done(cmd);
  513.         }
  514.  
  515.         if (status & 0x4) {            /* SCSIPERR */
  516.             /*
  517.              *  A parity error has occurred during a data
  518.              *  transfer phase.  Flag it and continue.
  519.              */
  520.             printk("aha274x: parity error on target %d, lun %d\n",
  521.                    cmd->target,
  522.                    cmd->lun);
  523.             aha274x_parity(cmd) = DID_PARITY;
  524.  
  525.             /*
  526.              *  Clear interrupt and resume as above.
  527.              */
  528.             outb(0x4, O_CLRSINT1(base));    /* CLRSCSIPERR */
  529.             UNPAUSE_SEQUENCER(p);
  530.  
  531.             outb(0x4, O_CLRINT(base));    /* undocumented */
  532.         }
  533.  
  534.         if ((status & (0x8|0x4)) == 0 && status) {
  535.             /*
  536.              *  We don't know what's going on.  Turn off the
  537.              *  interrupt source and try to continue.
  538.              */
  539.             printk("aha274x_isr: sstat1 = 0x%x\n", status);
  540.             outb(status, O_CLRSINT1(base));
  541.             UNPAUSE_SEQUENCER(p);
  542.             outb(0x4, O_CLRINT(base));    /* undocumented */
  543.         }
  544.     }
  545.  
  546.     if (intstat & 0x2) {                /* CMDCMPLT */
  547.  
  548.         int complete, old_scbptr;
  549.         struct aha274x_scb scb;
  550.         unsigned actual;
  551.         Scsi_Cmnd *cmd;
  552.  
  553.         /*
  554.          *  The sequencer will continue running when it
  555.          *  issues this interrupt.  There may be >1 commands
  556.          *  finished, so loop until we've processed them all.
  557.          */
  558.         do {
  559.             complete = inb(O_QOUTFIFO(base));
  560.  
  561.             cmd = (Scsi_Cmnd *)p->SCB_array[complete];
  562.             if (!cmd) {
  563.                 printk("aha274x warning: "
  564.                        "no command for scb (cmdcmplt)\n");
  565.                 continue;
  566.             }
  567.             p->SCB_array[complete] = NULL;
  568.             
  569.             PAUSE_SEQUENCER(p);
  570.  
  571.             /*
  572.              *  After pausing the sequencer (and waiting
  573.              *  for it to stop), save its SCB pointer, then
  574.              *  write in our completed one and read the SCB
  575.              *  registers.  Afterwards, restore the saved
  576.              *  pointer, unpause the sequencer and call the
  577.              *  higher-level completion function - unpause
  578.              *  first since we have no idea how long done()
  579.              *  will take.
  580.              */
  581.             old_scbptr = inb(O_SCBPTR(base));
  582.             outb(complete, O_SCBPTR(base));
  583.  
  584.             aha274x_getscb(base, &scb);
  585.             outb(old_scbptr, O_SCBPTR(base));
  586.  
  587.             UNPAUSE_SEQUENCER(p);
  588.  
  589.             cmd->result = scb.target_status |
  590.                      (aha274x_parity(cmd) << 16);
  591.  
  592.             /*
  593.              *  Did we underflow?  At this time, there's only
  594.              *  one other driver that bothers to check for this,
  595.              *  and cmd->underflow seems to be set rather half-
  596.              *  heartedly in the higher-level SCSI code.
  597.              */
  598.             actual = aha274x_length(cmd,
  599.                         scb.residual_SG_segment_count);
  600.  
  601.             actual -= ((scb.residual_data_count[2] << 16) |
  602.                    (scb.residual_data_count[1] <<  8) |
  603.                    (scb.residual_data_count[0]));
  604.  
  605.             if (actual < cmd->underflow) {
  606.                 printk("aha274x: target %d underflow - "
  607.                        "wanted (at least) %u, got %u\n",
  608.                        cmd->target, cmd->underflow, actual);
  609.  
  610.                 cmd->result = scb.target_status |
  611.                          (DID_UNDERFLOW << 16);
  612.             }
  613.  
  614.             cmd->scsi_done(cmd);
  615.  
  616.             /*
  617.              *  Clear interrupt status before checking
  618.              *  the output queue again.  This eliminates
  619.              *  a race condition whereby a command could
  620.              *  complete between the queue poll and the
  621.              *  interrupt clearing, so notification of the
  622.              *  command being complete never made it back
  623.              *  up to the kernel.
  624.              */
  625.             outb(0x2, O_CLRINT(base));    /* CLRCMDINT */
  626.  
  627.         } while (inb(O_QOUTCNT(base)));
  628.     }
  629.  
  630.     if (intstat & 0x1) {                /* SEQINT */
  631.  
  632.         unsigned char transfer, offset, rate;
  633.  
  634.         /*
  635.          *  Although the sequencer is paused immediately on
  636.          *  a SEQINT, an interrupt for a SCSIINT or a CMDCMPLT
  637.          *  condition will have unpaused the sequencer before
  638.          *  this point.
  639.          */
  640.         PAUSE_SEQUENCER(p);
  641.  
  642.         switch (intstat & 0xf0) {
  643.             case 0x00:
  644.             panic("aha274x_isr: unknown scsi bus phase\n");
  645.             case 0x10:
  646.             debug("aha274x_isr warning: "
  647.                   "issuing message reject, 1st byte 0x%x\n",
  648.                   inb(HA_REJBYTE(base)));
  649.             break;
  650.             case 0x20:
  651.             panic("aha274x_isr: reconnecting target %d "
  652.                   "didn't issue IDENTIFY message\n",
  653.                   (inb(O_SELID(base)) >> 4) & 0xf);
  654.             case 0x30:
  655.             debug("aha274x_isr: sequencer couldn't find match "
  656.                   "for reconnecting target %d - issuing ABORT\n",
  657.                   (inb(O_SELID(base)) >> 4) & 0xf);
  658.             break;
  659.             case 0x40:
  660.             transfer = inb(HA_ARG_1(base));
  661.             offset = inb(HA_ARG_2(base));
  662.             aha274x_to_scsirate(&rate, transfer, offset);
  663.             outb(rate, HA_RETURN_1(base));
  664.             debug_rate(base, rate);
  665.             break;
  666.             default:
  667.             debug("aha274x_isr: seqint, "
  668.                   "intstat = 0x%x, scsisigi = 0x%x\n",
  669.                   intstat, inb(O_SCSISIGI(base)));
  670.             break;
  671.         }
  672.  
  673.         outb(0x1, O_CLRINT(base));        /* CLRSEQINT */
  674.         UNPAUSE_SEQUENCER(p);
  675.     }
  676. }
  677.  
  678. /*
  679.  *  Probing for EISA boards: it looks like the first two bytes
  680.  *  are a manufacturer code - three characters, five bits each:
  681.  *
  682.  *         BYTE 0   BYTE 1   BYTE 2   BYTE 3
  683.  *        ?1111122 22233333 PPPPPPPP RRRRRRRR
  684.  *
  685.  *  The characters are baselined off ASCII '@', so add that value
  686.  *  to each to get the real ASCII code for it.  The next two bytes
  687.  *  appear to be a product and revision number, probably vendor-
  688.  *  specific.  This is what is being searched for at each port,
  689.  *  and what should probably correspond to the ID= field in the
  690.  *  ECU's .cfg file for the card - if your card is not detected,
  691.  *  make sure your signature is listed in the array.
  692.  *
  693.  *  The fourth byte's lowest bit seems to be an enabled/disabled
  694.  *  flag (rest of the bits are reserved?).
  695.  */
  696.  
  697. static
  698. enum aha_type aha274x_probe(int slot, int s_base)
  699. {
  700.     int i;
  701.     unsigned char buf[4];
  702.  
  703.     static struct {
  704.         int n;
  705.         unsigned char signature[sizeof(buf)];
  706.         enum aha_type type;
  707.     } S[] = {
  708.         4, { 0x04, 0x90, 0x77, 0x71 }, T_274X,    /* host adapter 274x */
  709.         4, { 0x04, 0x90, 0x77, 0x70 }, T_274X,    /* motherboard 274x  */
  710.         4, { 0x04, 0x90, 0x77, 0x56 }, T_284X,    /* 284x, BIOS enabled */
  711.     };
  712.  
  713.     for (i = 0; i < sizeof(buf); i++) {
  714.         /*
  715.          *  The VL-bus cards need to be primed by
  716.          *  writing before a signature check.
  717.          */
  718.         outb(0x80 + i, s_base);
  719.         buf[i] = inb(s_base + i);
  720.     }
  721.  
  722.     for (i = 0; i < sizeof(S)/sizeof(S[0]); i++) {
  723.         if (!memcmp(buf, S[i].signature, S[i].n)) {
  724.             /*
  725.              *  Signature match on enabled card?
  726.              */
  727.             if (inb(s_base + 4) & 1)
  728.                 return(S[i].type);
  729.             printk("aha274x disabled at slot %d, ignored\n", slot);
  730.         }
  731.     }
  732.     return(T_NONE);
  733. }
  734.  
  735. /*
  736.  *  Return ' ' for plain 274x, 'T' for twin-channel, 'W' for
  737.  *  wide channel, '?' for anything else.
  738.  */
  739.  
  740. static
  741. char aha274x_type(int base)
  742. {
  743.     /*
  744.      *  The AIC-7770 can be wired so that, on chip reset,
  745.      *  the SCSI Block Control register indicates how many
  746.      *  busses the chip is configured for.
  747.      */
  748.     switch (inb(O_SBLKCTL(base))) {
  749.         case 0:
  750.         return(' ');
  751.         case 2:
  752.         return('W');
  753.         case 8:
  754.         return('T');
  755.         default:
  756.         printk("aha274x has unknown bus configuration\n");
  757.         return('?');
  758.     }
  759. }
  760.  
  761. static
  762. void aha274x_loadram(int base)
  763. {
  764.     static unsigned char seqprog[] = {
  765.         /*
  766.          *  Each sequencer instruction is 29 bits
  767.          *  long (fill in the excess with zeroes)
  768.          *  and has to be loaded from least -> most
  769.          *  significant byte, so this table has the
  770.          *  byte ordering reversed.
  771.          */
  772. #        include "aha274x_seq.h"
  773.     };
  774.  
  775.     /*
  776.      *  When the AIC-7770 is paused (as on chip reset), the
  777.      *  sequencer address can be altered and a sequencer
  778.      *  program can be loaded by writing it, byte by byte, to
  779.      *  the sequencer RAM port - the Adaptec documentation
  780.      *  recommends using REP OUTSB to do this, hence the inline
  781.      *  assembly.  Since the address autoincrements as we load
  782.      *  the program, reset it back to zero afterward.  Disable
  783.      *  sequencer RAM parity error detection while loading, and
  784.      *  make sure the LOADRAM bit is enabled for loading.
  785.      */
  786.     outb(0x83, O_SEQCTL(base));    /* PERRORDIS|SEQRESET|LOADRAM */
  787.  
  788.     asm volatile("cld\n\t"
  789.              "rep\n\t"
  790.              "outsb"
  791.              : /* no output */
  792.              :"S" (seqprog), "c" (sizeof(seqprog)), "d" (O_SEQRAM(base))
  793.              :"si", "cx", "dx");
  794.  
  795.     /*
  796.      *  WARNING!  This is a magic sequence!  After extensive
  797.      *  experimentation, it seems that you MUST turn off the
  798.      *  LOADRAM bit before you play with SEQADDR again, else
  799.      *  you will end up with parity errors being flagged on
  800.      *  your sequencer program.  (You would also think that
  801.      *  turning off LOADRAM and setting SEQRESET to reset the
  802.      *  address to zero would work, but you need to do it twice
  803.      *  for it to take effect on the address.  Timing problem?)
  804.      */
  805.     outb(0, O_SEQCTL(base));
  806.     do {
  807.         /*
  808.          *  Actually, reset it until
  809.          *  the address shows up as
  810.          *  zero just to be safe..
  811.          */
  812.         outb(0x2, O_SEQCTL(base));    /* SEQRESET */
  813.  
  814.     } while (inw(O_SEQADDR(base)) != 0);
  815. }
  816.  
  817. static
  818. int aha274x_register(Scsi_Host_Template *template,
  819.              enum aha_type type,
  820.              int base)
  821. {
  822.     int i, irq, scsi_id;
  823.     struct Scsi_Host *host;
  824.     struct aha274x_host *p;
  825.  
  826.     /*
  827.      *  Give the AIC-7770 a reset - reading the 274x's registers
  828.      *  returns zeroes unless you do.  This forces a pause of the
  829.      *  Sequencer.
  830.      */
  831.     outb(1, O_HCNTRL(base));    /* CHIPRST */
  832.  
  833.     /*
  834.      *  The IRQ level in i/o port 4 maps directly onto the real
  835.      *  IRQ number.  If it's ok, register it with the kernel.
  836.      *
  837.      *  NB. the Adaptec documentation says the IRQ number is only
  838.      *    in the lower four bits; the ECU information shows the
  839.      *    high bit being used as well.  Which is correct?
  840.      */
  841.     irq = inb(HA_INTDEF(base)) & 0xf;
  842.     if (irq < 9 || irq > 15) {
  843.         printk("aha274x uses unsupported IRQ level, ignoring\n");
  844.         return(0);
  845.     }
  846.     
  847.     /*
  848.      *  Lock out other contenders for our i/o space.
  849.      */
  850.     snarf_region(O_MINREG(base), O_MAXREG(base)-O_MINREG(base));
  851.  
  852.     /*
  853.      *  Any card-type-specific adjustments before we register
  854.      *  the scsi host(s).
  855.      */
  856.  
  857.     scsi_id = inb(HA_SCSICONF(base)) & 0x7;
  858.  
  859.     switch (aha274x_type(base)) {
  860.         case 'T':
  861.         printk("aha274x warning: ignoring channel B of 274x-twin\n");
  862.         break;
  863.         case ' ':
  864.         break;
  865.         default:
  866.         printk("aha274x is an unsupported type, ignoring\n");
  867.         free_irq(irq);
  868.         return(0);
  869.     }
  870.  
  871.     /*
  872.      *  Before registry, make sure that the offsets of the
  873.      *  struct scatterlist are what the sequencer will expect,
  874.      *  otherwise disable scatter-gather altogether until someone
  875.      *  can fix it.  This is important since the sequencer will
  876.      *  DMA elements of the SG array in while executing commands.
  877.      */
  878.     if (template->sg_tablesize != SG_NONE) {
  879.         struct scatterlist sg;
  880.  
  881.         if (SG_STRUCT_CHECK(sg)) {
  882.             printk("aha274x warning: kernel scatter-gather "
  883.                    "structures changed, disabling it\n");
  884.             template->sg_tablesize = SG_NONE;
  885.         }
  886.     }
  887.     
  888.     /*
  889.      *  Register each "host" and fill in the returned Scsi_Host
  890.      *  structure as best we can.  Some of the parameters aren't
  891.      *  really relevant for EISA, and none of the high-level SCSI
  892.      *  code looks at it anyway.. why are the fields there?  Also
  893.      *  save the pointer so that we can find the information when
  894.      *  an IRQ is triggered.
  895.      */
  896.     host = scsi_register(template, sizeof(struct aha274x_host));
  897.     host->this_id = scsi_id;
  898.     host->irq = irq;
  899.  
  900.     aha274x_boards[irq] = host;
  901.     
  902.     p = (struct aha274x_host *)host->hostdata;
  903.     for (i = 0; i < AHA274X_MAXSCB; i++)
  904.         p->SCB_array[i] = NULL;
  905.     p->base = base;
  906.  
  907.     /*
  908.      *  The interrupt trigger is different depending
  909.      *  on whether the card is EISA or VL-bus.
  910.      */
  911.     p->unpause = (type != T_274X ? 0x2 : 0xa);
  912.  
  913.     /*
  914.      *  Register IRQ with the kernel _after_ the host information
  915.      *  is set up, in case we take an interrupt right away.
  916.      *
  917.      *  XXX - the card is reset and disabled, so why would we be
  918.      *      getting an interrupt?
  919.      */
  920.     if (request_irq(irq, aha274x_isr, SA_INTERRUPT, "AHA274x/284x")) {
  921.         printk("aha274x couldn't register irq %d, ignoring\n", irq);
  922.         return(0);
  923.     }
  924.  
  925.     /*
  926.      *  Print out debugging information before re-enabling
  927.      *  the card - a lot of registers on it can't be read
  928.      *  when the sequencer is active.
  929.      */
  930.     debug_config(type, base);
  931.  
  932.     /*
  933.      *  Load the sequencer program, then re-enable the board -
  934.      *  resetting the AIC-7770 disables it, leaving the lights
  935.      *  on with nobody home.
  936.      */
  937.     aha274x_loadram(base);
  938.     outb(1, O_BCTL(base));        /* ENABLE */
  939.  
  940.     /*
  941.      *  Set the host adapter registers to indicate that synchronous
  942.      *  negotiation should be attempted the first time the targets
  943.      *  are communicated with.  Also initialize the active message
  944.      *  flag to indicate that there is no message.
  945.      */
  946.     outb(0xff, HA_NEEDSDTR(base));
  947.     outb(0, HA_MSG_FLAGS(base));
  948.  
  949.     /*
  950.      *  Unpause the sequencer before returning and enable
  951.      *  interrupts - we shouldn't get any until the first
  952.      *  command is sent to us by the high-level SCSI code.
  953.      */
  954.     UNPAUSE_SEQUENCER(p);
  955.     return(1);
  956. }
  957.  
  958. int aha274x_detect(Scsi_Host_Template *template)
  959. {
  960.     enum aha_type type;
  961.     int found = 0, slot, base;
  962.  
  963.     for (slot = MINEISA; slot <= MAXEISA; slot++) {
  964.  
  965.         base = SLOTBASE(slot);
  966.         
  967.         if (check_region(O_MINREG(base),
  968.                  O_MAXREG(base)-O_MINREG(base)))
  969.         {
  970.             /*
  971.              *  Some other driver has staked a
  972.              *  claim to this i/o region already.
  973.              */
  974.             continue;
  975.         }
  976.  
  977.         type = aha274x_probe(slot, O_BIDx(base));
  978.  
  979.         if (type != T_NONE) {
  980.             /*
  981.              *  We "find" a 274x if we locate the card
  982.              *  signature and we can set it up and register
  983.              *  it with the kernel without incident.
  984.              */
  985.             found += aha274x_register(template, type, base);
  986.         }
  987.     }
  988.     template->name = (char *)aha274x_info();
  989.     return(found);
  990. }
  991.  
  992. const char *aha274x_info(void)
  993. {
  994.     return("Adaptec AHA274x/284x (EISA/VL-bus -> Fast SCSI) "
  995.            AHA274X_SEQ_VERSION "/"
  996.            AHA274X_H_VERSION "/"
  997.            "1.25");
  998. }
  999.  
  1000. int aha274x_command(Scsi_Cmnd *cmd)
  1001. {
  1002.     /*
  1003.      *  This is a relic of non-interrupt-driven SCSI
  1004.      *  drivers.  With the can_queue variable set, this
  1005.      *  should never be called.
  1006.      */
  1007.     panic("aha274x_command was called\n");
  1008. }
  1009.  
  1010. static
  1011. void aha274x_buildscb(struct aha274x_host *p,
  1012.               Scsi_Cmnd *cmd,
  1013.               struct aha274x_scb *scb)
  1014. {
  1015.     void *addr;
  1016.     unsigned length;
  1017.  
  1018.     memset(scb, 0, sizeof(*scb));
  1019.  
  1020.     /*
  1021.      *  NB. channel selection (bit 3) is always zero.
  1022.      */
  1023.     scb->target_channel_lun = ((cmd->target << 4) & 0xf0) |
  1024.                    (cmd->lun & 0x7);
  1025.  
  1026.     /*
  1027.      *  The interpretation of request_buffer and request_bufflen
  1028.      *  changes depending on whether or not use_sg is zero; a
  1029.      *  non-zero use_sg indicates the number of elements in the
  1030.      *  scatter-gather array.
  1031.      *
  1032.      *  The AIC-7770 can't support transfers of any sort larger
  1033.      *  than 2^24 (three-byte count) without backflips.  For what
  1034.      *  the kernel is doing, this shouldn't occur.  I hope.
  1035.      */
  1036.     length = aha274x_length(cmd, 0);
  1037.  
  1038.     /*
  1039.      *  The sequencer code cannot yet handle scatter-gather segments
  1040.      *  larger than 64k (two-byte length).  The 1.1.x kernels, however,
  1041.      *  have a four-byte length field in the struct scatterlist, so
  1042.      *  make sure we don't exceed 64k on these kernels for now.
  1043.      */
  1044.     aha274x_sg_check(cmd);
  1045.  
  1046.     if (length > 0xffffff) {
  1047.         panic("aha274x_buildscb: can't transfer > 2^24 - 1 bytes\n");
  1048.     }
  1049.  
  1050.     /*
  1051.      *  XXX - this relies on the host data being stored in a
  1052.      *      little-endian format.
  1053.      */
  1054.     addr = cmd->cmnd;
  1055.     scb->SCSI_cmd_length = COMMAND_SIZE(cmd->cmnd[0]);
  1056.     memcpy(scb->SCSI_cmd_pointer, &addr, sizeof(scb->SCSI_cmd_pointer));
  1057.  
  1058.     if (cmd->use_sg) {
  1059. #if 0
  1060.         debug("aha274x_buildscb: SG used, %d segments, length %u\n",
  1061.               cmd->use_sg,
  1062.               length);
  1063. #endif
  1064.         scb->SG_segment_count = cmd->use_sg;
  1065.         memcpy(scb->SG_list_pointer,
  1066.                &cmd->request_buffer,
  1067.                sizeof(scb->SG_list_pointer));
  1068.     } else {
  1069.         scb->SG_segment_count = 0;
  1070.         memcpy(scb->data_pointer,
  1071.                &cmd->request_buffer,
  1072.                sizeof(scb->data_pointer));
  1073.         memcpy(scb->data_count,
  1074.                &cmd->request_bufflen,
  1075.                sizeof(scb->data_count));
  1076.     }
  1077. }
  1078.  
  1079. static
  1080. void aha274x_putscb(int base, struct aha274x_scb *scb)
  1081. {
  1082.     /*
  1083.      *  By turning on the SCB auto increment, any reference
  1084.      *  to the SCB I/O space postincrements the SCB address
  1085.      *  we're looking at.  So turn this on and dump the relevant
  1086.      *  portion of the SCB to the card.
  1087.      */
  1088.     outb(0x80, O_SCBCNT(base));    /* SCBAUTO */
  1089.  
  1090.     asm volatile("cld\n\t"
  1091.              "rep\n\t"
  1092.              "outsb"
  1093.              : /* no output */
  1094.              :"S" (scb), "c" (sizeof(*scb)), "d" (O_SCBARRAY(base))
  1095.              :"si", "cx", "dx");
  1096.  
  1097.     outb(0, O_SCBCNT(base));
  1098. }
  1099.  
  1100. int aha274x_queue(Scsi_Cmnd *cmd, void (*fn)(Scsi_Cmnd *))
  1101. {
  1102.     long flags;
  1103.     int empty, old_scbptr;
  1104.     struct aha274x_host *p;
  1105.     struct aha274x_scb scb;
  1106.  
  1107. #if 0
  1108.     debug("aha274x_queue: cmd 0x%x (size %u), target %d, lun %d\n",
  1109.           cmd->cmnd[0],
  1110.           COMMAND_SIZE(cmd->cmnd[0]),
  1111.           cmd->target,
  1112.           cmd->lun);
  1113. #endif
  1114.  
  1115.     p = (struct aha274x_host *)cmd->host->hostdata;
  1116.  
  1117.     /*
  1118.      *  Construct the SCB beforehand, so the sequencer is
  1119.      *  paused a minimal amount of time.
  1120.      */
  1121.     aha274x_buildscb(p, cmd, &scb);
  1122.  
  1123.     /*
  1124.      *  This is a critical section, since we don't want the
  1125.      *  interrupt routine mucking with the host data or the
  1126.      *  card.  Since the kernel documentation is vague on
  1127.      *  whether or not we are in a cli/sti pair already, save
  1128.      *  the flags to be on the safe side.
  1129.      */
  1130.     save_flags(flags);
  1131.     cli();
  1132.  
  1133.     /*
  1134.      *  Find a free slot in the SCB array to load this command
  1135.      *  into.  Since can_queue is set to AHA274X_MAXSCB, we
  1136.      *  should always find one.
  1137.      */
  1138.     for (empty = 0; empty < AHA274X_MAXSCB; empty++)
  1139.         if (!p->SCB_array[empty])
  1140.             break;
  1141.     if (empty == AHA274X_MAXSCB)
  1142.         panic("aha274x_queue: couldn't find a free scb\n");
  1143.  
  1144.     /*
  1145.      *  Pause the sequencer so we can play with its registers -
  1146.      *  wait for it to acknowledge the pause.
  1147.      *
  1148.      *  XXX - should the interrupts be left on while doing this?
  1149.      */
  1150.     PAUSE_SEQUENCER(p);
  1151.  
  1152.     /*
  1153.      *  Save the SCB pointer and put our own pointer in - this
  1154.      *  selects one of the four banks of SCB registers.  Load
  1155.      *  the SCB, then write its pointer into the queue in FIFO
  1156.      *  and restore the saved SCB pointer.
  1157.      */
  1158.     old_scbptr = inb(O_SCBPTR(p->base));
  1159.     outb(empty, O_SCBPTR(p->base));
  1160.     
  1161.     aha274x_putscb(p->base, &scb);
  1162.  
  1163.     outb(empty, O_QINFIFO(p->base));
  1164.     outb(old_scbptr, O_SCBPTR(p->base));
  1165.  
  1166.     /*
  1167.      *  Make sure the Scsi_Cmnd pointer is saved, the struct it
  1168.      *  points to is set up properly, and the parity error flag
  1169.      *  is reset, then unpause the sequencer and watch the fun
  1170.      *  begin.
  1171.      */
  1172.     cmd->scsi_done = fn;
  1173.     p->SCB_array[empty] = cmd;
  1174.     aha274x_parity(cmd) = DID_OK;
  1175.  
  1176.     UNPAUSE_SEQUENCER(p);
  1177.  
  1178.     restore_flags(flags);
  1179.     return(0);
  1180. }
  1181.  
  1182. /* return values from aha274x_kill */
  1183.  
  1184. enum k_state {
  1185.     k_ok,                /* scb found and message sent */
  1186.     k_busy,                /* message already present */
  1187.     k_absent,            /* couldn't locate scb */
  1188.     k_disconnect,            /* scb found, but disconnected */
  1189. };
  1190.  
  1191. /*
  1192.  *  This must be called with interrupts disabled - it's going to
  1193.  *  be messing around with the host data, and an interrupt being
  1194.  *  fielded in the middle could get ugly.
  1195.  *
  1196.  *  Since so much of the abort and reset code is shared, this
  1197.  *  function performs more magic than it really should.  If the
  1198.  *  command completes ok, then it will call scsi_done with the
  1199.  *  result code passed in.  The unpause parameter controls whether
  1200.  *  or not the sequencer gets unpaused - the reset function, for
  1201.  *  instance, may want to do something more aggressive.
  1202.  *
  1203.  *  Note that the command is checked for in our SCB_array first
  1204.  *  before the sequencer is paused, so if k_absent is returned,
  1205.  *  then the sequencer is NOT paused.
  1206.  */
  1207.  
  1208. static
  1209. enum k_state aha274x_kill(Scsi_Cmnd *cmd, unsigned char message,
  1210.               unsigned int result, int unpause)
  1211. {
  1212.     struct aha274x_host *p;
  1213.     int i, scb, found, queued;
  1214.     unsigned char scbsave[AHA274X_MAXSCB];
  1215.  
  1216.     p = (struct aha274x_host *)cmd->host->hostdata;
  1217.  
  1218.     /*
  1219.      *  If we can't find the command, assume it just completed
  1220.      *  and shrug it away.
  1221.      */
  1222.     for (scb = 0; scb < AHA274X_MAXSCB; scb++)
  1223.         if (p->SCB_array[scb] == cmd)
  1224.             break;
  1225.  
  1226.     if (scb == AHA274X_MAXSCB)
  1227.         return(k_absent);
  1228.  
  1229.     PAUSE_SEQUENCER(p);
  1230.  
  1231.     /*
  1232.      *  This is the best case, really.  Check to see if the
  1233.      *  command is still in the sequencer's input queue.  If
  1234.      *  so, simply remove it.  Reload the queue afterward.
  1235.      */
  1236.     queued = inb(O_QINCNT(p->base));
  1237.     
  1238.     for (i = found = 0; i < queued; i++) {
  1239.         scbsave[i] = inb(O_QINFIFO(p->base));
  1240.  
  1241.         if (scbsave[i] == scb) {
  1242.             found = 1;
  1243.             i -= 1;
  1244.         }
  1245.     }
  1246.  
  1247.     queued -= found;
  1248.     for (i = 0; i < queued; i++)
  1249.         outb(scbsave[i], O_QINFIFO(p->base));
  1250.  
  1251.     if (found)
  1252.         goto complete;
  1253.  
  1254.     /*
  1255.      *  Check the current SCB bank.  If it's not the one belonging
  1256.      *  to the command we want to kill, assume that the command
  1257.      *  is disconnected.  It's rather a pain to force a reconnect
  1258.      *  and send a message to the target, so we abdicate responsibility
  1259.      *  in this case.
  1260.      */
  1261.     if (inb(O_SCBPTR(p->base)) != scb) {
  1262.         if (unpause)
  1263.             UNPAUSE_SEQUENCER(p);
  1264.         return(k_disconnect);
  1265.     }
  1266.  
  1267.     /*
  1268.      *  Presumably at this point our target command is active.  Check
  1269.      *  to see if there's a message already in effect.  If not, place
  1270.      *  our message in and assert ATN so the target goes into MESSAGE
  1271.      *  OUT phase.
  1272.      */
  1273.     if (inb(HA_MSG_FLAGS(p->base)) & 0x80) {
  1274.         if (unpause)
  1275.             UNPAUSE_SEQUENCER(p);
  1276.         return(k_busy);
  1277.     }
  1278.  
  1279.     outb(0x80, HA_MSG_FLAGS(p->base));        /* active message */
  1280.     outb(1, HA_MSG_LEN(p->base));            /* length = 1 */
  1281.     outb(message, HA_MSG_START(p->base));        /* message body */
  1282.  
  1283.     /*
  1284.      *  Assert ATN.  Use the value of SCSISIGO saved by the
  1285.      *  sequencer code so we don't alter its contents radically
  1286.      *  in the middle of something critical.
  1287.      */
  1288.     outb(inb(HA_SIGSTATE(p->base)) | 0x10, O_SCSISIGO(p->base));
  1289.  
  1290.     /*
  1291.      *  The command has been killed.  Do the bookkeeping, unpause
  1292.      *  the sequencer, and notify the higher-level SCSI code.
  1293.      */
  1294. complete:
  1295.     p->SCB_array[scb] = NULL;
  1296.     if (unpause)
  1297.         UNPAUSE_SEQUENCER(p);
  1298.  
  1299.     cmd->result = result << 16;
  1300.     cmd->scsi_done(cmd);
  1301.     return(k_ok);
  1302. }
  1303.  
  1304. int aha274x_abort(Scsi_Cmnd *cmd)
  1305. {
  1306.     int rv;
  1307.     long flags;
  1308.  
  1309.     save_flags(flags);
  1310.     cli();
  1311.  
  1312.     switch (aha274x_kill(cmd, ABORT, DID_ABORT, !0)) {
  1313.         case k_ok:        rv = SCSI_ABORT_SUCCESS;    break;
  1314.         case k_busy:    rv = SCSI_ABORT_BUSY;        break;
  1315.         case k_absent:    rv = SCSI_ABORT_NOT_RUNNING;    break;
  1316.         case k_disconnect:    rv = SCSI_ABORT_SNOOZE;        break;
  1317.         default:
  1318.         panic("aha274x_do_abort: internal error\n");
  1319.     }
  1320.  
  1321.     restore_flags(flags);
  1322.     return(rv);
  1323. }
  1324.  
  1325. /*
  1326.  *  Resetting the bus always succeeds - is has to, otherwise the
  1327.  *  kernel will panic!  Try a surgical technique - sending a BUS
  1328.  *  DEVICE RESET message - on the offending target before pulling
  1329.  *  the SCSI bus reset line.
  1330.  */
  1331.  
  1332. int aha274x_reset(Scsi_Cmnd *cmd)
  1333. {
  1334.     int i;
  1335.     long flags;
  1336.     Scsi_Cmnd *reset;
  1337.     struct aha274x_host *p;
  1338.  
  1339.     p = (struct aha274x_host *)cmd->host->hostdata;
  1340.     save_flags(flags);
  1341.     cli();
  1342.  
  1343.     switch (aha274x_kill(cmd, BUS_DEVICE_RESET, DID_RESET, 0)) {
  1344.  
  1345.         case k_ok:
  1346.         /*
  1347.          *  The RESET message was sent to the target
  1348.          *  with no problems.  Flag that target as
  1349.          *  needing a SDTR negotiation on the next
  1350.          *  connection and restart the sequencer.
  1351.          */
  1352.         outb((1 << cmd->target), HA_NEEDSDTR(p->base));
  1353.         UNPAUSE_SEQUENCER(p);
  1354.         break;
  1355.  
  1356.         case k_absent:
  1357.         /*
  1358.          *  The sequencer will not be paused if aha274x_kill()
  1359.          *  couldn't find the command.
  1360.          */
  1361.         PAUSE_SEQUENCER(p);
  1362.         /* falls through */
  1363.  
  1364.         case k_busy:
  1365.         case k_disconnect:
  1366.         /*
  1367.          *  Do a hard reset of the SCSI bus.  According to the
  1368.          *  SCSI-2 draft specification, reset has to be asserted
  1369.          *  for at least 25us.  I'm invoking the kernel delay
  1370.          *  function for 30us since I'm not totally trusting of
  1371.          *  the busy loop timing.
  1372.          *
  1373.          *  XXX - I'm not convinced this works.  I tried resetting
  1374.          *      the bus before, trying to get the devices on the
  1375.          *      bus to revert to asynchronous transfer, and it
  1376.          *      never seemed to work.
  1377.          */
  1378.         debug("aha274x: attempting to reset scsi bus and card\n");
  1379.  
  1380.         outb(1, O_SCSISEQ(p->base));        /* SCSIRSTO */
  1381.         udelay(30);
  1382.         outb(0, O_SCSISEQ(p->base));        /* !SCSIRSTO */
  1383.  
  1384.         outb(0xff, HA_NEEDSDTR(p->base));
  1385.         UNPAUSE_SEQUENCER(p);
  1386.  
  1387.         /*
  1388.          *  Locate the command and return a "reset" status
  1389.          *  for it.  This is not completely correct and will
  1390.          *  probably return to haunt me later.
  1391.          */
  1392.         for (i = 0; i < AHA274X_MAXSCB; i++) {
  1393.             if (cmd == p->SCB_array[i]) {
  1394.                 reset = (Scsi_Cmnd *)p->SCB_array[i];
  1395.                 p->SCB_array[i] = NULL;
  1396.                 reset->result = DID_RESET << 16;
  1397.                 reset->scsi_done(reset);
  1398.                 break;
  1399.             }
  1400.         }
  1401.         break;
  1402.  
  1403.         default:
  1404.         panic("aha274x_reset: internal error\n");
  1405.     }
  1406.  
  1407.     restore_flags(flags);
  1408.     return(SCSI_RESET_SUCCESS);
  1409. }
  1410.  
  1411. int aha274x_biosparam(Disk *disk, int devno, int geom[])
  1412. {
  1413.     /*
  1414.      *  XXX - when I find the EISA configuration information,
  1415.      *      this should change to handle the "extended translation
  1416.      *      for drives >1G" option, which uses 255 heads and
  1417.      *      63 sectors/track for drives >1G.  Right now, just
  1418.      *      assume it's turned off.
  1419.      */
  1420.     debug("aha274x_biosparam warning: don't know translation config\n");
  1421.  
  1422.     geom[0] = 64;
  1423.     geom[1] = 32;
  1424.     geom[2] = disk->capacity / (64 * 32);
  1425.  
  1426.     return(0);
  1427. }
  1428.  
  1429.