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

  1. /*
  2.  *    buslogic.c    Copyright (C) 1993, 1994 David B. Gentzel
  3.  *    Low-level scsi driver for BusLogic adapters
  4.  *    by David B. Gentzel, Whitfield Software Services, Carnegie, PA
  5.  *        (gentzel@nova.enet.dec.com)
  6.  *    Thanks to BusLogic for providing the necessary documentation
  7.  *
  8.  *    The original version of this driver was derived from aha1542.[ch],
  9.  *    which is Copyright (C) 1992 Tommy Thorn.  Much has been reworked, but
  10.  *    most of basic structure and substantial chunks of code still remain.
  11.  *
  12.  *    Furthermore, many subsequent fixes and improvements to the aha1542
  13.  *    driver have been folded back into this driver.  These changes to
  14.  *    aha1542.[ch] are Copyright (C) 1993, 1994 Eric Youngdale.
  15.  *
  16.  *    Thanks to the following individuals who have made contributions (of
  17.  *    (code, information, support, or testing) to this driver:
  18.  *        Eric Youngdale        Leonard Zubkoff
  19.  *        Tomas Hurka        Andrew Walker
  20.  */
  21.  
  22. /*
  23.  * TODO:
  24.  *    1. Clean up error handling & reporting.
  25.  *    2. Find out why scatter/gather is limited to 16 requests per command.
  26.  *    3. Test/improve/fix abort & reset functions.
  27.  *    4. Look at command linking.
  28.  *    5. Allow multiple boards to share an IRQ if the bus allows (EISA, MCA,
  29.  *       and PCI).
  30.  *    6. Avoid using the 445S workaround for board revs >= D.
  31.  */
  32.  
  33. /*
  34.  * NOTES:
  35.  *    BusLogic (formerly BusTek) manufactures an extensive family of
  36.  *    intelligent, high performance SCSI-2 host adapters.  They all support
  37.  *    command queueing and scatter/gather I/O.  Most importantly, they all
  38.  *    support identical programming interfaces, so a single driver can be used
  39.  *    for all boards.
  40.  *
  41.  *    Actually, they all support TWO identical programming interfaces!  They
  42.  *    have an Adaptec 154x compatible interface (complete with 24 bit
  43.  *    addresses) as well as a "native" 32 bit interface.  As such, the Linux
  44.  *    aha1542 driver can be used to drive them, but with less than optimal
  45.  *    performance (at least for the EISA, VESA, and MCA boards).
  46.  *
  47.  *    Here is the scoop on the various models:
  48.  *    BT-542B - ISA first-party DMA with floppy support.
  49.  *    BT-545S - 542B + FAST SCSI and active termination.
  50.  *    BT-545D - 545S + differential termination.
  51.  *    BT-640A - MCA bus-master with floppy support.
  52.  *    BT-646S - 640A + FAST SCSI and active termination.
  53.  *    BT-646D - 646S + differential termination.
  54.  *    BT-742A - EISA bus-master with floppy support.
  55.  *    BT-747S - 742A + FAST SCSI, active termination, and 2.88M floppy.
  56.  *    BT-747D - 747S + differential termination.
  57.  *    BT-757S - 747S + WIDE SCSI.
  58.  *    BT-757D - 747D + WIDE SCSI.
  59.  *    BT-445S - VESA bus-master FAST SCSI with active termination and floppy
  60.  *          support.
  61.  *    BT-445C - 445S + enhanced BIOS & firmware options.
  62.  *    BT-946C - PCI bus-master FAST SCSI. (??? Nothing else known.)
  63.  *
  64.  *    ??? I believe other boards besides the 445 now have a "C" model, but I
  65.  *    have no facts on them.
  66.  *
  67.  *    This driver SHOULD support all of these boards.  It has only been tested
  68.  *    with a 747S and 445S.
  69.  *
  70.  *    Should you require further information on any of these boards, BusLogic
  71.  *    can be reached at (408)492-9090.  Their BBS # is (408)492-1984 (maybe BBS
  72.  *    stands for "Big Brother System"?).
  73.  *
  74.  *    Places flagged with a triple question-mark are things which are either
  75.  *    unfinished, questionable, or wrong.
  76.  */
  77.  
  78. #ifdef MODULE
  79. #include <linux/module.h>
  80. #endif
  81.  
  82. #include <linux/string.h>
  83. #include <linux/sched.h>
  84. #include <linux/kernel.h>
  85. #include <linux/head.h>
  86. #include <linux/types.h>
  87. #include <linux/ioport.h>
  88. #include <linux/delay.h>
  89. #include <linux/config.h>
  90.  
  91. #include <asm/io.h>
  92. #include <asm/system.h>
  93. #include <asm/dma.h>
  94.  
  95. #include "../block/blk.h"
  96. #include "scsi.h"
  97. #include "hosts.h"
  98. #include "sd.h"
  99. #define BUSLOGIC_PRIVATE_H    /* Get the "private" stuff */
  100. #include "buslogic.h"
  101.  
  102. #ifndef BUSLOGIC_DEBUG
  103. # define BUSLOGIC_DEBUG 0
  104. #endif
  105.  
  106. /* ??? Until kmalloc actually implements GFP_DMA, we can't depend on it... */
  107. #undef GFP_DMA
  108.  
  109. /* If different port addresses are needed (e.g. to install more than two
  110.    cards), you must define BUSLOGIC_PORT_OVERRIDE to be a comma-separated list
  111.    of the addresses which will be checked.  This can also be used to resolve a
  112.    conflict if the port-probing at a standard port causes problems with
  113.    another board. */
  114. /* #define BUSLOGIC_PORT_OVERRIDE 0x330, 0x334, 0x130, 0x134, 0x230, 0x234 */
  115.  
  116. /* Define this to be either BIOS_TRANSLATION_DEFAULT or BIOS_TRANSLATION_BIG
  117.    if you wish to bypass the test for this, which uses an undocumented port.
  118.    The test is believed to fail on at least some AMI BusLogic clones. */
  119. /* #define BIOS_TRANSLATION_OVERRIDE BIOS_TRANSLATION_BIG */
  120.  
  121. #define BUSLOGIC_VERSION "1.14"
  122.  
  123. /* Not a random value - if this is too large, the system hangs for a long time
  124.    waiting for something to happen if a board is not installed. */
  125. /* ??? I don't really like this as it will wait longer on slow machines.
  126.    Perhaps we should base this on the loops_per_second "Bogomips" value? */
  127. #define WAITNEXTTIMEOUT 3000000
  128.  
  129. /* This is for the scsi_malloc call in buslogic_queuecommand. */
  130. /* ??? I'd up this to 4096, but would we be in danger of using up the
  131.    scsi_malloc memory pool? */
  132. /* This could be a concern, I guess.  It may be possible to fix things so that
  133.    the table generated in sd.c is compatible with the low-level code, but
  134.    don't hold your breath.  -ERY */
  135. #define BUSLOGIC_SG_MALLOC 512
  136.  
  137. /* Since the SG list is malloced, we have to limit the length. */
  138. #define BUSLOGIC_MAX_SG (BUSLOGIC_SG_MALLOC / sizeof (struct chain))
  139.  
  140. /* ??? Arbitrary.  If we can dynamically allocate the mailbox arrays, I may
  141.    bump up this number. */
  142. #define BUSLOGIC_MAILBOXES 16
  143.  
  144. #define BUSLOGIC_CMDLUN 4        /* ??? Arbitrary */
  145.  
  146. /* BusLogic boards can be configured for quite a number of port addresses (six
  147.    to be exact), but I generally do not want the driver poking around at
  148.    random.  We allow two port addresses - this allows people to use a BusLogic
  149.    with a MIDI card, which frequently also uses 0x330.
  150.  
  151.    This can also be overridden on the command line to the kernel, via LILO or
  152.    LOADLIN. */
  153. static unsigned short bases[7] = {
  154. #ifdef BUSLOGIC_PORT_OVERRIDE
  155.     BUSLOGIC_PORT_OVERRIDE,
  156. #else
  157.     0x330, 0x334, /* 0x130, 0x134, 0x230, 0x234, */
  158. #endif
  159.     0
  160. };
  161.  
  162. #define BIOS_TRANSLATION_DEFAULT 0    /* Default case */
  163. #define BIOS_TRANSLATION_BIG 1        /* Big disk (> 1G) case */
  164.  
  165. struct hostdata {
  166.     unsigned int bus_type;
  167.     unsigned int bios_translation: 1;    /* BIOS mapping (for compatibility) */
  168.     size_t last_mbi_used;
  169.     size_t last_mbo_used;
  170.     char model[7];
  171.     char firmware_rev[6];
  172.     Scsi_Cmnd *sc[BUSLOGIC_MAILBOXES];
  173.     struct mailbox mb[2 * BUSLOGIC_MAILBOXES];
  174.     struct ccb ccbs[BUSLOGIC_MAILBOXES];
  175. };
  176.  
  177. #define HOSTDATA(host) ((struct hostdata *)&(host)->hostdata)
  178.  
  179. /* One for each IRQ level (9-15), although 13 will never be used. */
  180. static struct Scsi_Host *host[7] = { NULL, };
  181.  
  182. static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt);
  183. static int restart(struct Scsi_Host *shpnt);
  184.  
  185. #define INTR_RESET(base) outb(RINT, CONTROL(base))
  186.  
  187. #define buslogic_printk buslogic_prefix(__PRETTY_FUNCTION__),printk
  188.  
  189. #if defined(MODULE) && !defined(GFP_DMA)
  190. # define CHECK_DMA_ADDR(isa, addr, badstmt) \
  191.     do { if ((isa) && (addr) > (void *)ISA_DMA_THRESHOLD) badstmt; } while (0)
  192. #else
  193. # define CHECK_DMA_ADDR(isa, addr, badstmt)
  194. #endif
  195.  
  196. #define CHECK(cond) if (cond) ; else goto fail
  197.  
  198. #define WAIT(port, allof, noneof) \
  199.     CHECK(wait(port, allof, noneof, WAITNEXTTIMEOUT, FALSE))
  200. #define WAIT_WHILE(port, mask) WAIT(port, 0, mask)
  201. #define WAIT_UNTIL(port, mask) WAIT(port, mask, 0)
  202. #define WAIT_FAST(port, allof, noneof) \
  203.     CHECK(wait(port, allof, noneof, 100, TRUE))
  204. #define WAIT_WHILE_FAST(port, mask) WAIT_FAST(port, 0, mask)
  205. #define WAIT_UNTIL_FAST(port, mask) WAIT_FAST(port, mask, 0)
  206.  
  207. /* If delay != 0, we use the udelay call to regulate the amount of time we
  208.    wait.
  209.  
  210.    This is inline as it is always called with constant arguments and hence
  211.    will be very well optimized. */
  212. static __inline__ int wait(unsigned short port,
  213.                unsigned char allof, unsigned char noneof,
  214.                unsigned int timeout, int delay)
  215. {
  216.     int bits;
  217.  
  218.     for (;;) {
  219.     bits = inb(port);
  220.     if ((bits & allof) == allof && (bits & noneof) == 0)
  221.         return TRUE;
  222.     if (delay)
  223.         udelay(1000);
  224.     if (--timeout == 0)
  225.         return FALSE;
  226.     }
  227. }
  228.  
  229. static void buslogic_prefix(const char *func)
  230. {
  231.     printk("BusLogic SCSI: %s: ", func);
  232. }
  233.  
  234. static void buslogic_stat(unsigned int base)
  235. {
  236.     int s = inb(STATUS(base)), i = inb(INTERRUPT(base));
  237.  
  238.     buslogic_printk("status=%02X intrflags=%02X\n", s, i);
  239. }
  240.  
  241. /* This is a bit complicated, but we need to make sure that an interrupt
  242.    routine does not send something out while we are in the middle of this.
  243.    Fortunately, it is only at boot time that multi-byte messages are ever
  244.    sent. */
  245. static int buslogic_out(unsigned int base, const unsigned char *cmdp,
  246.             size_t len)
  247. {
  248.     unsigned long flags = 0;
  249.     
  250.     if (len == 1) {
  251.     for (;;) {
  252.         WAIT_WHILE(STATUS(base), CPRBSY);
  253.         save_flags(flags);
  254.         cli();
  255.         if (!(inb(STATUS(base)) & CPRBSY)) {
  256.         outb(*cmdp, COMMAND_PARAMETER(base));
  257.         restore_flags(flags);
  258.         return FALSE;
  259.         }
  260.         restore_flags(flags);
  261.     }
  262.     } else {
  263.     save_flags(flags);
  264.     cli();
  265.     while (len--) {
  266.         WAIT_WHILE(STATUS(base), CPRBSY);
  267.         outb(*cmdp++, COMMAND_PARAMETER(base));
  268.     }
  269.     restore_flags(flags);
  270.     }
  271.     return FALSE;
  272.   fail:
  273.     restore_flags(flags);
  274.     buslogic_printk("failed(%u): ", len + 1);
  275.     buslogic_stat(base);
  276.     return TRUE;
  277. }
  278.  
  279. /* Only used at boot time, so we do not need to worry about latency as much
  280.    here.  This waits a very short period of time.  We use this if we are not
  281.    sure whether the board will respond to the command we just sent. */
  282. static int buslogic_in(unsigned int base, unsigned char *cmdp, size_t len)
  283. {
  284.     unsigned long flags;
  285.     
  286.     save_flags(flags);
  287.     cli();
  288.     while (len--) {
  289.     WAIT_UNTIL_FAST(STATUS(base), DIRRDY);
  290.     *cmdp++ = inb(DATA_IN(base));
  291.     }
  292.     restore_flags(flags);
  293.     return FALSE;
  294.   fail:
  295.     restore_flags(flags);
  296. #if (BUSLOGIC_DEBUG & BD_IO)
  297.     buslogic_printk("failed(%u): ", len + 1);
  298.     buslogic_stat(base);
  299. #endif
  300.     return TRUE;
  301. }
  302.  
  303. static unsigned int makecode(unsigned int haerr, unsigned int scsierr)
  304. {
  305.     unsigned int hosterr;
  306.     const char *errstr = NULL;
  307. #if (BUSLOGIC_DEBUG & BD_ERRORS) && defined(CONFIG_SCSI_CONSTANTS)
  308.     static const char *const buslogic_status[] = {
  309.     /* 00 */    "Command completed normally",
  310.     /* 01-07 */    NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  311.     /* 08-09 */    NULL, NULL,
  312.     /* 0A */    "Linked command completed normally",
  313.     /* 0B */    "Linked command completed normally, interrupt generated",
  314.     /* 0C-0F */    NULL, NULL, NULL, NULL,
  315.     /* 10 */    NULL,
  316.     /* 11 */    "Selection timed out",
  317.     /* 12 */    "Data overrun/underrun",
  318.     /* 13 */    "Unexpected bus free",
  319.     /* 14 */    "Target bus phase sequence failure",
  320.     /* 15 */    "First byte of outgoing MB was invalid",
  321.     /* 16 */    "Invalid CCB Operation Code",
  322.     /* 17 */    "Linked CCB does not have the same LUN",
  323.     /* 18 */    "Invalid Target Direction received from Host",
  324.     /* 19 */    "Duplicate CCB Received in Target Mode",
  325.     /* 1A */    "Invalid CCB or Segment List Parameter",
  326.     /* 1B */    "Auto request sense failed",
  327.     /* 1C */    "SCSI-2 tagged queueing message was rejected by the target",
  328.     /* 1D-1F */    NULL, NULL, NULL,
  329.     /* 20 */    "Host adapter hardware failure",
  330.     /* 21 */    "Target did not respond to SCSI ATN and the HA SCSI bus reset",
  331.     /* 22 */    "Host adapter asserted a SCSI bus reset",
  332.     /* 23 */    "Other SCSI devices asserted a SCSI bus reset",
  333.     };
  334. #endif
  335.  
  336.     switch (haerr) {
  337.       case 0x00:    /* Normal completion. */
  338.       case 0x0A:    /* Linked command complete without error and linked
  339.                normally. */
  340.       case 0x0B:    /* Linked command complete without error, interrupt
  341.                generated. */
  342.     hosterr = DID_OK;
  343.     break;
  344.  
  345.       case 0x11:    /* Selection time out: the initiator selection or
  346.                target reselection was not complete within the SCSI
  347.                time out period. */
  348.     hosterr = DID_TIME_OUT;
  349.     break;
  350.  
  351.       case 0x14:    /* Target bus phase sequence failure - An invalid bus
  352.                phase or bus phase sequence was requested by the
  353.                target.  The host adapter will generate a SCSI
  354.                Reset Condition, notifying the host with a RSTS
  355.                interrupt. */
  356.       case 0x21:    /* The target did not respond to SCSI ATN and the host
  357.                adapter consequently issued a SCSI bus reset to
  358.                clear up the failure. */
  359.       case 0x22:    /* The host adapter asserted a SCSI bus reset. */
  360.     hosterr = DID_RESET;
  361.     break;
  362.  
  363.       case 0x12:    /* Data overrun/underrun: the target attempted to
  364.                transfer more data than was allocated by the Data
  365.                Length field or the sum of the Scatter/Gather Data
  366.                Length fields. */
  367.       case 0x13:    /* Unexpected bus free - The target dropped the SCSI
  368.                BSY at an unexpected time. */
  369.       case 0x15:    /* MBO command was not 00, 01, or 02 - The first byte
  370.                of the MB was invalid.  This usually indicates a
  371.                software failure. */
  372.       case 0x16:    /* Invalid CCB Operation Code - The first byte of the
  373.                CCB was invalid.  This usually indicates a software
  374.                failure. */
  375.       case 0x17:    /* Linked CCB does not have the same LUN - A
  376.                subsequent CCB of a set of linked CCB's does not
  377.                specify the same logical unit number as the
  378.                first. */
  379.       case 0x18:    /* Invalid Target Direction received from Host - The
  380.                direction of a Target Mode CCB was invalid. */
  381.       case 0x19:    /* Duplicate CCB Received in Target Mode - More than
  382.                once CCB was received to service data transfer
  383.                between the same target LUN and initiator SCSI ID
  384.                in the same direction. */
  385.       case 0x1A:    /* Invalid CCB or Segment List Parameter - A segment
  386.                list with a zero length segment or invalid segment
  387.                list boundaries was received.  A CCB parameter was
  388.                invalid. */
  389.       case 0x1B:    /* Auto request sense failed. */
  390.       case 0x1C:    /* SCSI-2 tagged queueing message was rejected by the
  391.                target. */
  392.       case 0x20:    /* The host adapter hardware failed. */
  393.       case 0x23:    /* Other SCSI devices asserted a SCSI bus reset. */
  394.     hosterr = DID_ERROR;    /* ??? Couldn't find any better. */
  395.     break;
  396.  
  397.       default:
  398. #ifndef CONFIG_SCSI_CONSTANTS
  399.     errstr = "unknown hoststatus";
  400. #endif
  401.     hosterr = DID_ERROR;
  402.     break;
  403.     }
  404. #if (BUSLOGIC_DEBUG & BD_ERRORS)
  405. # ifdef CONFIG_SCSI_CONSTANTS
  406.     if (hosterr != DID_OK) {
  407.     if (haerr < ARRAY_SIZE(buslogic_status))
  408.         errstr = buslogic_status[haerr];
  409.     if (errstr == NULL)
  410.         errstr = "unknown hoststatus";
  411.     }
  412. # else
  413.     if (hosterr == DID_ERROR)
  414.     errstr = "";
  415. # endif
  416. #endif
  417.     if (errstr != NULL)
  418.     buslogic_printk("%s (%02X)\n", errstr, haerr);
  419.     return (hosterr << 16) | scsierr;
  420. }
  421.  
  422. /* ??? this should really be "const struct Scsi_Host *" */
  423. const char *buslogic_info(struct Scsi_Host *shpnt)
  424. {
  425.     return "BusLogic SCSI driver " BUSLOGIC_VERSION;
  426. }
  427.  
  428. /* A "high" level interrupt handler. */
  429. static void buslogic_interrupt(int irq, struct pt_regs * regs)
  430. {
  431.     void (*my_done)(Scsi_Cmnd *) = NULL;
  432.     int errstatus, mbistatus = MBX_NOT_IN_USE, number_serviced, found;
  433.     size_t mbi, mbo = 0;
  434.     struct Scsi_Host *shpnt;
  435.     Scsi_Cmnd *sctmp;
  436.     unsigned long flags;
  437.     int base, flag;
  438.     int needs_restart;
  439.     struct mailbox *mb;
  440.     struct ccb *ccb;
  441.  
  442.     shpnt = host[irq - 9];
  443.     if (!shpnt)
  444.     panic("buslogic_interrupt: NULL SCSI host entry");
  445.  
  446.     mb = HOSTDATA(shpnt)->mb;
  447.     ccb = HOSTDATA(shpnt)->ccbs;
  448.     base = shpnt->io_port;
  449.  
  450. #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
  451.     flag = inb(INTERRUPT(base));
  452.  
  453.     buslogic_printk("");
  454.     if (!(flag & INTV))
  455.     printk("no interrupt? ");
  456.     if (flag & IMBL)
  457.     printk("IMBL ");
  458.     if (flag & MBOR)
  459.     printk("MBOR ");
  460.     if (flag & CMDC)
  461.     printk("CMDC ");
  462.     if (flag & RSTS)
  463.     printk("RSTS ");
  464.     printk("status %02X\n", inb(STATUS(base)));
  465. #endif
  466.  
  467.     number_serviced = 0;
  468.     needs_restart = 0;
  469.  
  470.     for (;;) {
  471.     flag = inb(INTERRUPT(base));
  472.  
  473.     /* Check for unusual interrupts.  If any of these happen, we should
  474.        probably do something special, but for now just printing a message
  475.        is sufficient.  A SCSI reset detected is something that we really
  476.        need to deal with in some way. */
  477.     if (flag & (MBOR | CMDC | RSTS)) {
  478.         buslogic_printk("unusual flag:");
  479.         if (flag & MBOR)
  480.         printk(" MBOR");
  481.         if (flag & CMDC)
  482.         printk(" CMDC");
  483.         if (flag & RSTS) {
  484.         needs_restart = 1;
  485.         printk(" RSTS");
  486.         }
  487.         printk("\n");
  488.     }
  489.  
  490.     INTR_RESET(base);
  491.  
  492.     save_flags(flags);
  493.     cli();
  494.  
  495.     mbi = HOSTDATA(shpnt)->last_mbi_used + 1;
  496.     if (mbi >= 2 * BUSLOGIC_MAILBOXES)
  497.         mbi = BUSLOGIC_MAILBOXES;
  498.  
  499.     /* I use the "found" variable as I like to keep cli/sti pairs at the
  500.        same block level.  Debugging dropped sti's is no fun... */
  501.  
  502.     found = FALSE;
  503.     do {
  504.         if (mb[mbi].status != MBX_NOT_IN_USE) {
  505.         found = TRUE;
  506.         break;
  507.         }
  508.         mbi++;
  509.         if (mbi >= 2 * BUSLOGIC_MAILBOXES)
  510.         mbi = BUSLOGIC_MAILBOXES;
  511.     } while (mbi != HOSTDATA(shpnt)->last_mbi_used);
  512.  
  513.     if (found) {
  514.         mbo = (struct ccb *)mb[mbi].ccbptr - ccb;
  515.         mbistatus = mb[mbi].status;
  516.         mb[mbi].status = MBX_NOT_IN_USE;
  517.         HOSTDATA(shpnt)->last_mbi_used = mbi;
  518.     }
  519.  
  520.     restore_flags(flags);
  521.  
  522.     if (!found) {
  523.         /* Hmm, no mail.  Must have read it the last time around. */
  524.         if (!number_serviced && !needs_restart)
  525.         buslogic_printk("interrupt received, but no mail.\n");
  526.         /* We detected a reset.  Restart all pending commands for devices
  527.            that use the hard reset option. */
  528.         if (needs_restart)
  529.         restart(shpnt);
  530.         return;
  531.     }
  532.  
  533. #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
  534.     if (ccb[mbo].tarstat || ccb[mbo].hastat)
  535.         buslogic_printk("returning %08X (status %d).\n",
  536.                 ((int)ccb[mbo].hastat << 16) | ccb[mbo].tarstat,
  537.                 mb[mbi].status);
  538. #endif
  539.  
  540.     if (mbistatus == MBX_COMPLETION_NOT_FOUND)
  541.         continue;
  542.  
  543. #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
  544.     buslogic_printk("...done %u %u\n", mbo, mbi);
  545. #endif
  546.  
  547.     sctmp = HOSTDATA(shpnt)->sc[mbo];
  548.  
  549.     if (!sctmp || !sctmp->scsi_done) {
  550.         buslogic_printk("unexpected interrupt.\n");
  551.         buslogic_printk("tarstat=%02X, hastat=%02X id=%d lun=%d ccb#=%u\n",
  552.                 ccb[mbo].tarstat, ccb[mbo].hastat,
  553.                 ccb[mbo].id, ccb[mbo].lun, mbo);
  554.         return;
  555.     }
  556.  
  557.     my_done = sctmp->scsi_done;
  558.     if (sctmp->host_scribble)
  559.         scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
  560.  
  561.     /* ??? more error checking left out here */
  562.     if (mbistatus != MBX_COMPLETION_OK) {
  563.         /* ??? This is surely wrong, but I don't know what's right. */
  564.         errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
  565.     } else
  566.         errstatus = 0;
  567.  
  568. #if (BUSLOGIC_DEBUG & BD_INTERRUPT)
  569.     if (errstatus)
  570.         buslogic_printk("error: %04X %04X\n",
  571.                 ccb[mbo].hastat, ccb[mbo].tarstat);
  572.  
  573.     if (status_byte(ccb[mbo].tarstat) == CHECK_CONDITION) {
  574.         size_t i;
  575.  
  576.         buslogic_printk("sense:");
  577.         for (i = 0; i < sizeof sctmp->sense_buffer; i++)
  578.         printk(" %02X", sctmp->sense_buffer[i]);
  579.         printk("\n");
  580.     }
  581.  
  582.     if (errstatus)
  583.         buslogic_printk("returning %08X.\n", errstatus);
  584. #endif
  585.  
  586.     sctmp->result = errstatus;
  587.     HOSTDATA(shpnt)->sc[mbo] = NULL;    /* This effectively frees up
  588.                            the mailbox slot, as far as
  589.                            queuecommand is
  590.                            concerned. */
  591.     my_done(sctmp);
  592.     number_serviced++;
  593.     }
  594. }
  595.  
  596. /* ??? Why does queuecommand return a value?  scsi.c never looks at it... */
  597. int buslogic_queuecommand(Scsi_Cmnd *scpnt, void (*done)(Scsi_Cmnd *))
  598. {
  599.     static const unsigned char buscmd[] = { CMD_START_SCSI };
  600.     unsigned char direction;
  601.     unsigned char *cmd = (unsigned char *)scpnt->cmnd;
  602.     unsigned char target = scpnt->target;
  603.     unsigned char lun = scpnt->lun;
  604.     void *buff = scpnt->request_buffer;
  605.     int bufflen = scpnt->request_bufflen;
  606.     int mbo;
  607.     unsigned long flags;
  608.     struct mailbox *mb;
  609.     struct ccb *ccb;
  610.     struct Scsi_Host *shpnt = scpnt->host;
  611.  
  612. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  613.     if (target > 1) {
  614.     scpnt->result = DID_TIME_OUT << 16;
  615.     done(scpnt);
  616.     return 0;
  617.     }
  618. #endif
  619.  
  620.     if (*cmd == REQUEST_SENSE) {
  621. #if (BUSLOGIC_DEBUG & (BD_COMMAND | BD_ERRORS))
  622.     if (bufflen != sizeof scpnt->sense_buffer) {
  623.         buslogic_printk("wrong buffer length supplied for request sense"
  624.                 " (%d).\n",
  625.                 bufflen);
  626.     }
  627. #endif
  628.     scpnt->result = 0;
  629.     done(scpnt);
  630.     return 0;
  631.     }
  632.  
  633. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  634.     {
  635.     int i;
  636.  
  637.     if (*cmd == READ_10 || *cmd == WRITE_10
  638.         || *cmd == READ_6 || *cmd == WRITE_6)
  639.         i = *(int *)(cmd + 2);
  640.     else
  641.         i = -1;
  642.     buslogic_printk("dev %d cmd %02X pos %d len %d ",
  643.             target, *cmd, i, bufflen);
  644.     buslogic_stat(shpnt->io_port);
  645.     buslogic_printk("dumping scsi cmd:");
  646.     for (i = 0; i < scpnt->cmd_len; i++)
  647.         printk(" %02X", cmd[i]);
  648.     printk("\n");
  649.     if (*cmd == WRITE_10 || *cmd == WRITE_6)
  650.         return 0;    /* we are still testing, so *don't* write */
  651.     }
  652. #endif
  653.  
  654.     mb = HOSTDATA(shpnt)->mb;
  655.     ccb = HOSTDATA(shpnt)->ccbs;
  656.  
  657.     /* Use the outgoing mailboxes in a round-robin fashion, because this
  658.        is how the host adapter will scan for them. */
  659.  
  660.     save_flags(flags);
  661.     cli();
  662.  
  663.     mbo = HOSTDATA(shpnt)->last_mbo_used + 1;
  664.     if (mbo >= BUSLOGIC_MAILBOXES)
  665.     mbo = 0;
  666.  
  667.     do {
  668.     if (mb[mbo].status == MBX_NOT_IN_USE
  669.         && HOSTDATA(shpnt)->sc[mbo] == NULL)
  670.         break;
  671.     mbo++;
  672.     if (mbo >= BUSLOGIC_MAILBOXES)
  673.         mbo = 0;
  674.     } while (mbo != HOSTDATA(shpnt)->last_mbo_used);
  675.  
  676.     if (mb[mbo].status != MBX_NOT_IN_USE || HOSTDATA(shpnt)->sc[mbo]) {
  677.     /* ??? Instead of failing, should we enable OMBR interrupts and sleep
  678.        until we get one? */
  679.     restore_flags(flags);
  680.     buslogic_printk("unable to find empty mailbox.\n");
  681.     goto fail;
  682.     }
  683.  
  684.     HOSTDATA(shpnt)->sc[mbo] = scpnt;        /* This will effectively
  685.                            prevent someone else from
  686.                            screwing with this cdb. */
  687.  
  688.     HOSTDATA(shpnt)->last_mbo_used = mbo;
  689.  
  690.     restore_flags(flags);
  691.  
  692. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  693.     buslogic_printk("sending command (%d %08X)...", mbo, done);
  694. #endif
  695.  
  696.     /* This gets trashed for some reason */
  697.     mb[mbo].ccbptr = &ccb[mbo];
  698.  
  699.     memset(&ccb[mbo], 0, sizeof (struct ccb));
  700.  
  701.     ccb[mbo].cdblen = scpnt->cmd_len;        /* SCSI Command Descriptor
  702.                            Block Length */
  703.  
  704.     direction = 0;
  705.     if (*cmd == READ_10 || *cmd == READ_6)
  706.     direction = 8;
  707.     else if (*cmd == WRITE_10 || *cmd == WRITE_6)
  708.     direction = 16;
  709.  
  710.     memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
  711.  
  712.     if (scpnt->use_sg) {
  713.     struct scatterlist *sgpnt;
  714.     struct chain *cptr;
  715.     size_t i;
  716.  
  717.     ccb[mbo].op = CCB_OP_INIT_SG;    /* SCSI Initiator Command
  718.                        w/scatter-gather */
  719.     scpnt->host_scribble
  720.         = (unsigned char *)scsi_malloc(BUSLOGIC_SG_MALLOC);
  721.     if (scpnt->host_scribble == NULL) {
  722.         buslogic_printk("unable to allocate DMA memory.\n");
  723.         goto fail;
  724.     }
  725.     sgpnt = (struct scatterlist *)scpnt->request_buffer;
  726.     cptr = (struct chain *)scpnt->host_scribble;
  727.     if (scpnt->use_sg > shpnt->sg_tablesize) {
  728.         buslogic_printk("bad segment list, %d > %d.\n",
  729.                 scpnt->use_sg, shpnt->sg_tablesize);
  730.         goto fail;
  731.     }
  732.     for (i = 0; i < scpnt->use_sg; i++) {
  733.         CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, sgpnt[i].address,
  734.                goto baddma);
  735.         cptr[i].dataptr = sgpnt[i].address;
  736.         cptr[i].datalen = sgpnt[i].length;
  737.     }
  738.     ccb[mbo].datalen = scpnt->use_sg * sizeof (struct chain);
  739.     ccb[mbo].dataptr = cptr;
  740. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  741.     {
  742.         unsigned char *ptr;
  743.  
  744.         buslogic_printk("cptr %08X:", cptr);
  745.         ptr = (unsigned char *)cptr;
  746.         for (i = 0; i < 18; i++)
  747.         printk(" %02X", ptr[i]);
  748.         printk("\n");
  749.     }
  750. #endif
  751.     } else {
  752.     ccb[mbo].op = CCB_OP_INIT;    /* SCSI Initiator Command */
  753.     scpnt->host_scribble = NULL;
  754.     CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, buff, goto baddma);
  755.     ccb[mbo].datalen = bufflen;
  756.     ccb[mbo].dataptr = buff;
  757.     }
  758.     ccb[mbo].id = target;
  759.     ccb[mbo].lun = lun;
  760.     ccb[mbo].dir = direction;
  761.     ccb[mbo].rsalen = sizeof scpnt->sense_buffer;
  762.     ccb[mbo].senseptr = scpnt->sense_buffer;
  763.     ccb[mbo].linkptr = NULL;
  764.     ccb[mbo].commlinkid = 0;
  765.  
  766. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  767.     {
  768.     size_t i;
  769.  
  770.     buslogic_printk("sending...");
  771.     for (i = 0; i < sizeof ccb[mbo] - 10; i++)
  772.         printk(" %02X", ((unsigned char *)&ccb[mbo])[i]);
  773.     printk("\n");
  774.     }
  775. #endif
  776.  
  777.     if (done) {
  778. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  779.     buslogic_printk("now waiting for interrupt: ");
  780.     buslogic_stat(shpnt->io_port);
  781. #endif
  782.     scpnt->scsi_done = done;
  783.     mb[mbo].status = MBX_ACTION_START;
  784.     /* start scsi command */
  785.     buslogic_out(shpnt->io_port, buscmd, sizeof buscmd);
  786. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  787.     buslogic_stat(shpnt->io_port);
  788. #endif
  789.     } else
  790.     buslogic_printk("done can't be NULL.\n");
  791.  
  792.     while (0) {
  793. #if defined(MODULE) && !defined(GFP_DMA)
  794.       baddma:
  795.     buslogic_printk("address > 16MB used for ISA HA.\n");
  796. #endif
  797.       fail:
  798.     scpnt->result = DID_ERROR << 16;
  799.     done(scpnt);
  800.     }
  801.  
  802.     return 0;
  803. }
  804.  
  805. #if 0
  806. static void internal_done(Scsi_Cmnd *scpnt)
  807. {
  808.     scpnt->SCp.Status++;
  809. }
  810.  
  811. int buslogic_command(Scsi_Cmnd *scpnt)
  812. {
  813. #if (BUSLOGIC_DEBUG & BD_COMMAND)
  814.     buslogic_printk("calling buslogic_queuecommand.\n");
  815. #endif
  816.  
  817.     buslogic_queuecommand(scpnt, internal_done);
  818.  
  819.     scpnt->SCp.Status = 0;
  820.     while (!scpnt->SCp.Status)
  821.     continue;
  822.     return scpnt->result;
  823. }
  824. #endif
  825.  
  826. /* Initialize mailboxes. */
  827. static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt)
  828. {
  829.     size_t i;
  830.     int ok = FALSE;        /* Innocent until proven guilty... */
  831.     struct mailbox *mb = HOSTDATA(shpnt)->mb;
  832.     struct ccb *ccb = HOSTDATA(shpnt)->ccbs;
  833.     struct {
  834.     unsigned char cmd, count;
  835.     void *base PACKED;
  836.     } cmd = { CMD_INITEXTMB, BUSLOGIC_MAILBOXES, mb };
  837.  
  838.     for (i = 0; i < BUSLOGIC_MAILBOXES; i++) {
  839.     mb[i].status = mb[BUSLOGIC_MAILBOXES + i].status = MBX_NOT_IN_USE;
  840.     mb[i].ccbptr = &ccb[i];
  841.     }
  842.     INTR_RESET(base);    /* reset interrupts, so they don't block */
  843.  
  844.     if (buslogic_out(base, (unsigned char *)&cmd, sizeof cmd))
  845.     goto fail;
  846.     WAIT_UNTIL(INTERRUPT(base), CMDC);
  847.  
  848.     ok = TRUE;
  849.  
  850.     while (0) {
  851.       fail:
  852.     buslogic_printk("failed setting up mailboxes.\n");
  853.     }
  854.  
  855.     INTR_RESET(base);
  856.  
  857.     return !ok;
  858. }
  859.  
  860. static int getconfig(unsigned int base, unsigned char *irq,
  861.              unsigned char *dma, unsigned char *id,
  862.              char *bus_type, unsigned short *max_sg,
  863.              const unsigned char **bios)
  864. {
  865.     unsigned char inquiry_cmd[2];
  866.     unsigned char inquiry_result[4];
  867.     int i;
  868.  
  869. #if (BUSLOGIC_DEBUG & BD_DETECT)
  870.     buslogic_printk("called\n");
  871. #endif
  872.  
  873.     i = inb(STATUS(base));
  874.     if (i & DIRRDY)
  875.     i = inb(DATA_IN(base));
  876.     inquiry_cmd[0] = CMD_RETCONF;
  877.     buslogic_out(base, inquiry_cmd, 1);
  878.     if (buslogic_in(base, inquiry_result, 3))
  879.     goto fail;
  880.     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
  881.     INTR_RESET(base);
  882.     /* Defer using the DMA value until we know the bus type. */
  883.     *dma = inquiry_result[0];
  884.     switch (inquiry_result[1]) {
  885.       case 0x01:
  886.     *irq = 9;
  887.     break;
  888.       case 0x02:
  889.     *irq = 10;
  890.     break;
  891.       case 0x04:
  892.     *irq = 11;
  893.     break;
  894.       case 0x08:
  895.     *irq = 12;
  896.     break;
  897.       case 0x20:
  898.     *irq = 14;
  899.     break;
  900.       case 0x40:
  901.     *irq = 15;
  902.     break;
  903.       default:
  904.     buslogic_printk("unable to determine BusLogic IRQ level, "
  905.             " disabling board.\n");
  906.     goto fail;
  907.     }
  908.     *id = inquiry_result[2] & 0x7;
  909.  
  910.     /* I expected Adaptec boards to fail on this, but it doesn't happen... */
  911.     inquiry_cmd[0] = CMD_INQEXTSETUP;
  912.     inquiry_cmd[1] = 4;
  913.     if (buslogic_out(base, inquiry_cmd, 2))
  914.     goto fail;
  915.     if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
  916.     goto fail;
  917.     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
  918.     if (inb(STATUS(base)) & CMDINV)
  919.     goto fail;
  920.     INTR_RESET(base);
  921.  
  922.     *bus_type = inquiry_result[0];
  923.     CHECK(*bus_type == 'A' || *bus_type == 'E' || *bus_type == 'M');
  924.  
  925.     *bios = (const unsigned char *)((unsigned int)inquiry_result[1] << 12);
  926.  
  927.     *max_sg = (inquiry_result[3] << 8) | inquiry_result[2];
  928.  
  929.     /* We only need a DMA channel for ISA boards.  Some other types of boards
  930.        (such as the 747S) have an option to report a DMA channel even though
  931.        none is used (for compatibility with Adaptec drivers which require a
  932.        DMA channel).  We ignore this. */
  933.     if (*bus_type == 'A')
  934.     switch (*dma) {
  935.       case 0:    /* This indicates that no DMA channel is used. */
  936.         *dma = 0;
  937.         break;
  938.       case 0x20:
  939.         *dma = 5;
  940.         break;
  941.       case 0x40:
  942.         *dma = 6;
  943.         break;
  944.       case 0x80:
  945.         *dma = 7;
  946.         break;
  947.       default:
  948.         buslogic_printk("unable to determine BusLogic DMA channel,"
  949.                 " disabling board.\n");
  950.         goto fail;
  951.     }
  952.     else
  953.     *dma = 0;
  954.  
  955.     while (0) {
  956.       fail:
  957. #if (BUSLOGIC_DEBUG & BD_DETECT)
  958.     buslogic_printk("query board settings\n");
  959. #endif
  960.     return TRUE;
  961.     }
  962.  
  963.     return FALSE;
  964. }
  965.  
  966. /* Query the board.  This acts both as part of the detection sequence and as a
  967.    means to get necessary configuration information. */
  968. static int buslogic_query(unsigned int base, unsigned char *trans,
  969.               unsigned char *irq, unsigned char *dma,
  970.               unsigned char *id, char *bus_type,
  971.               unsigned short *max_sg, const unsigned char **bios,
  972.               char *model, char *firmware_rev)
  973. {
  974.     unsigned char inquiry_cmd[2];
  975.     unsigned char inquiry_result[6];
  976.     unsigned char geo;
  977.     unsigned int i;
  978.  
  979. #if (BUSLOGIC_DEBUG & BD_DETECT)
  980.     buslogic_printk("called\n");
  981. #endif
  982.  
  983.     /* Quick and dirty test for presence of the card. */
  984.     if (inb(STATUS(base)) == 0xFF)
  985.     goto fail;
  986.  
  987.     /* Check the GEOMETRY port early for quick bailout on Adaptec boards. */
  988.     geo = inb(GEOMETRY(base));
  989. #if (BUSLOGIC_DEBUG & BD_DETECT)
  990.     buslogic_printk("geometry bits: %02X\n", geo);
  991. #endif
  992.     /* Here is where we tell the men from the boys (i.e. Adaptec's don't
  993.        support the GEOMETRY port, the men do :-) */
  994.     if (geo == 0xFF)
  995.     goto fail;
  996.  
  997.     /* In case some other card was probing here, reset interrupts. */
  998.     INTR_RESET(base);
  999.  
  1000.     /* Reset the adapter.  I ought to make a hard reset, but it's not really
  1001.        necessary. */
  1002.     outb(RSOFT | RINT/* | RSBUS*/, CONTROL(base));
  1003.  
  1004.     /* Wait a little bit for things to settle down. */
  1005.     i = jiffies + 2;
  1006.     while (i > jiffies);
  1007.  
  1008.     /* Expect INREQ and HARDY, any of the others are bad. */
  1009.     WAIT(STATUS(base), INREQ | HARDY, DACT | DFAIL | CMDINV | DIRRDY | CPRBSY);
  1010.  
  1011.     /* Shouldn't have generated any interrupts during reset. */
  1012.     if (inb(INTERRUPT(base)) & INTRMASK)
  1013.     goto fail;
  1014.  
  1015.     /* Getting the BusLogic firmware revision level is a bit tricky.  We get
  1016.        the first two digits (d.d) from CMD_INQUIRY and then use two undocumented
  1017.        commands to get the remaining digit and letter (d.ddl as in 3.31C). */
  1018.  
  1019.     inquiry_cmd[0] = CMD_INQUIRY;
  1020.     buslogic_out(base, inquiry_cmd, 1);
  1021.     if (buslogic_in(base, inquiry_result, 4))
  1022.     goto fail;
  1023.     /* Reading port should reset DIRRDY. */
  1024.     if (inb(STATUS(base)) & DIRRDY)
  1025.     goto fail;
  1026.     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
  1027.     INTR_RESET(base);
  1028.     firmware_rev[0] = inquiry_result[2];
  1029.     firmware_rev[1] = '.';
  1030.     firmware_rev[2] = inquiry_result[3];
  1031.     firmware_rev[3] = '\0';
  1032. #if 0
  1033.     buslogic_printk("inquiry bytes: %02X(%c) %02X(%c)\n",
  1034.             inquiry_result[0], inquiry_result[0],
  1035.             inquiry_result[1], inquiry_result[1]);
  1036. #endif
  1037.  
  1038.     if (getconfig(base, irq, dma, id, bus_type, max_sg, bios))
  1039.     goto fail;
  1040.  
  1041.     /* Set up defaults */
  1042. #ifdef BIOS_TRANSLATION_OVERRIDE
  1043.     *trans = BIOS_TRANSLATION_OVERRIDE;
  1044. #else
  1045.     *trans = BIOS_TRANSLATION_DEFAULT;
  1046. #endif
  1047.     model[0] = '\0';
  1048.     model[6] = 0;
  1049.  
  1050.     /* ??? Begin undocumented command use.
  1051.        These may not be supported by clones. */
  1052.  
  1053.     do {
  1054.     /* ??? It appears as though AMI BusLogic clones don't implement this
  1055.        feature.  As an experiment, if we read a 00 we ignore the GEO_GT_1GB
  1056.        bit and skip all further undocumented commands. */
  1057.     if (geo == 0x00)
  1058.         break;
  1059. #ifndef BIOS_TRANSLATION_OVERRIDE
  1060.     *trans = ((geo & GEO_GT_1GB)
  1061.           ? BIOS_TRANSLATION_BIG : BIOS_TRANSLATION_DEFAULT);
  1062. #endif
  1063.  
  1064.     inquiry_cmd[0] = CMD_VER_NO_LAST;
  1065.     buslogic_out(base, inquiry_cmd, 1);
  1066.     if (buslogic_in(base, inquiry_result, 1))
  1067.         break;
  1068.     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
  1069.     INTR_RESET(base);
  1070.     firmware_rev[3] = inquiry_result[0];
  1071.     firmware_rev[4] = '\0';
  1072.  
  1073.     inquiry_cmd[0] = CMD_VER_NO_LETTER;
  1074.     buslogic_out(base, inquiry_cmd, 1);
  1075.     if (buslogic_in(base, inquiry_result, 1))
  1076.         break;
  1077.     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
  1078.     INTR_RESET(base);
  1079.     firmware_rev[4] = inquiry_result[0];
  1080.     firmware_rev[5] = '\0';
  1081.  
  1082.     /* Use undocumented command to get model number and revision. */
  1083.  
  1084.     inquiry_cmd[0] = CMD_RET_MODEL_NO;
  1085.     inquiry_cmd[1] = 6;
  1086.     buslogic_out(base, inquiry_cmd, 2);
  1087.     if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
  1088.         break;
  1089.     WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
  1090.     INTR_RESET(base);
  1091.     memcpy(model, inquiry_result, 5);
  1092.     model[5] = '\0';
  1093.     model[6] = inquiry_result[5];
  1094.     } while (0);
  1095.  
  1096.     /* ??? End undocumented command use. */
  1097.  
  1098.     /* bus_type from getconfig doesn't differentiate between EISA/VESA.  We
  1099.        override using the model number here. */
  1100.     switch (*bus_type) {
  1101.       case 'E':
  1102.     switch (model[0]) {
  1103.       case '4':
  1104.         *bus_type = 'V';
  1105.         break;
  1106.       case '9':
  1107.         *bus_type = 'P';
  1108.         break;
  1109.       case '7':
  1110.         break;
  1111.       default:
  1112.         *bus_type = 'X';
  1113.         break;
  1114.     }
  1115.     break;
  1116.       default:
  1117.     break;
  1118.     }
  1119.  
  1120.     while (0) {
  1121.       fail:
  1122. #if (BUSLOGIC_DEBUG & BD_DETECT)
  1123.     buslogic_printk("query board settings\n");
  1124. #endif
  1125.     return TRUE;
  1126.     }
  1127.  
  1128.     return FALSE;
  1129. }
  1130.  
  1131. /* return non-zero on detection */
  1132. int buslogic_detect(Scsi_Host_Template *tpnt)
  1133. {
  1134.     unsigned char dma;
  1135.     unsigned char irq;
  1136.     unsigned int base;
  1137.     unsigned char id;
  1138.     char bus_type;
  1139.     unsigned short max_sg;
  1140.     unsigned char bios_translation;
  1141.     unsigned long flags;
  1142.     const unsigned char *bios;
  1143.     char *model;
  1144.     char *firmware_rev;
  1145.     struct Scsi_Host *shpnt;
  1146.     size_t indx;
  1147.     int unchecked_isa_dma;
  1148.     int count = 0;
  1149.  
  1150. #if (BUSLOGIC_DEBUG & BD_DETECT)
  1151.     buslogic_printk("called\n");
  1152. #endif
  1153.  
  1154.     tpnt->can_queue = BUSLOGIC_MAILBOXES;
  1155.     for (indx = 0; bases[indx] != 0; indx++)
  1156.     if (!check_region(bases[indx], 4)) {
  1157.         shpnt = scsi_register(tpnt, sizeof (struct hostdata));
  1158.  
  1159.         base = bases[indx];
  1160.  
  1161.         model = HOSTDATA(shpnt)->model;
  1162.         firmware_rev = HOSTDATA(shpnt)->firmware_rev;
  1163.         if (buslogic_query(base, &bios_translation, &irq, &dma, &id,
  1164.                    &bus_type, &max_sg, &bios, model, firmware_rev))
  1165.         goto unregister;
  1166.  
  1167. #if (BUSLOGIC_DEBUG & BD_DETECT)
  1168.         buslogic_stat(base);
  1169. #endif
  1170.  
  1171.         /* Only type 'A' (AT/ISA) bus adapters use unchecked DMA. */
  1172.         unchecked_isa_dma = (bus_type == 'A');
  1173. #ifndef CONFIG_NO_BUGGY_BUSLOGIC
  1174.         /* There is a hardware bug in the BT-445S prior to revision D.
  1175.            When the BIOS is enabled and you have more than 16MB of memory,
  1176.            the card mishandles memory transfers over 16MB which (if viewed
  1177.            as a 24-bit address) overlap with the BIOS address space.  For
  1178.            example if you have the BIOS located at physical address
  1179.            0xDC000 and a DMA transfer from the card to RAM starts at
  1180.            physical address 0x10DC000 then the transfer is messed up.  To
  1181.            be more precise every fourth byte of the transfer is messed up.
  1182.            (This analysis courtesy of Tomas Hurka, author of the NeXTSTEP
  1183.            BusLogic driver.) */
  1184.  
  1185.         if (bus_type == 'V'                    /* 445 */
  1186.         && firmware_rev[0] <= '3'            /* S */
  1187.         && bios != NULL) {                /* BIOS enabled */
  1188. #if 1
  1189.         /* Now that LNZ's forbidden_addr stuff is in the higher level
  1190.            scsi code, we can use this instead. */
  1191.         /* Avoid addresses which "mirror" the BIOS for DMA. */
  1192.         shpnt->forbidden_addr = (unsigned long)bios;
  1193.         shpnt->forbidden_size = 16 * 1024;
  1194. #else
  1195.         /* Use double-buffering. */
  1196.         unchecked_isa_dma = TRUE;
  1197. #endif
  1198.         }
  1199. #endif
  1200.  
  1201.         CHECK_DMA_ADDR(unchecked_isa_dma, shpnt, goto unregister);
  1202.  
  1203.         if (setup_mailboxes(base, shpnt))
  1204.         goto unregister;
  1205.  
  1206.         /* Set the Bus on/off-times as not to ruin floppy performance.
  1207.            CMD_BUSOFF_TIME is a noop for EISA boards (and possibly
  1208.            others???). */
  1209.         if (bus_type != 'E' && bus_type != 'P') {
  1210.         /* The default ON/OFF times for BusLogic adapters is 7/4. */
  1211.         static const unsigned char oncmd[] = { CMD_BUSON_TIME, 7 };
  1212.         static const unsigned char offcmd[] = { CMD_BUSOFF_TIME, 5 };
  1213.  
  1214.         INTR_RESET(base);
  1215.         buslogic_out(base, oncmd, sizeof oncmd);
  1216.         WAIT_UNTIL(INTERRUPT(base), CMDC);
  1217.         INTR_RESET(base);
  1218.         buslogic_out(base, offcmd, sizeof offcmd);
  1219.         WAIT_UNTIL(INTERRUPT(base), CMDC);
  1220.         while (0) {
  1221.           fail:
  1222.             buslogic_printk("setting bus on/off-time failed.\n");
  1223.         }
  1224.         INTR_RESET(base);
  1225.         }
  1226.  
  1227.         buslogic_printk("configuring %s HA at port 0x%03X, IRQ %u",
  1228.                 (bus_type == 'A' ? "ISA"
  1229.                  : (bus_type == 'E' ? "EISA"
  1230.                 : (bus_type == 'M' ? "MCA"
  1231.                    : (bus_type == 'P' ? "PCI"
  1232.                       : (bus_type == 'V' ? "VESA"
  1233.                      : (bus_type == 'X' ? "EISA/VESA/PCI"
  1234.                         : "Unknown")))))),
  1235.                 base, irq);
  1236.         if (bios != NULL)
  1237.         printk(", BIOS 0x%05X", (unsigned int)bios);
  1238.         if (dma != 0)
  1239.         printk(", DMA %u", dma);
  1240.         printk(", ID %u\n", id);
  1241.         buslogic_printk("Model Number: %s",
  1242.                 (model[0] ? model : "Unknown"));
  1243.         if (model[0])
  1244.         printk(" (revision %d)", model[6]);
  1245.         printk("\n");
  1246.         buslogic_printk("firmware revision: %s\n", firmware_rev);
  1247.  
  1248. #if (BUSLOGIC_DEBUG & BD_DETECT)
  1249.         buslogic_stat(base);
  1250. #endif
  1251.  
  1252. #if (BUSLOGIC_DEBUG & BD_DETECT)
  1253.         buslogic_printk("enable interrupt channel %d.\n", irq);
  1254. #endif
  1255.  
  1256.         save_flags(flags);
  1257.         cli();
  1258.         if (request_irq(irq, buslogic_interrupt, 0, "buslogic")) {
  1259.         buslogic_printk("unable to allocate IRQ for "
  1260.                 "BusLogic controller.\n");
  1261.         restore_flags(flags);
  1262.         goto unregister;
  1263.         }
  1264.  
  1265.         if (dma) {
  1266.         if (request_dma(dma, "buslogic")) {
  1267.             buslogic_printk("unable to allocate DMA channel for "
  1268.                     "BusLogic controller.\n");
  1269.             free_irq(irq);
  1270.             restore_flags(flags);
  1271.             goto unregister;
  1272.         }
  1273.  
  1274.         /* The DMA-Controller.  We need to fool with this because we
  1275.            want to be able to use an ISA BusLogic without having to
  1276.            have the BIOS enabled. */
  1277.         set_dma_mode(dma, DMA_MODE_CASCADE);
  1278.         enable_dma(dma);
  1279.         }
  1280.  
  1281.         host[irq - 9] = shpnt;
  1282.         shpnt->this_id = id;
  1283.         shpnt->unchecked_isa_dma = unchecked_isa_dma;
  1284.         /* Have to keep cmd_per_lun at 1 for ISA machines otherwise lots
  1285.            of memory gets sucked up for bounce buffers.  */
  1286.         shpnt->cmd_per_lun = (unchecked_isa_dma ? 1 : BUSLOGIC_CMDLUN);
  1287.         shpnt->sg_tablesize = max_sg;
  1288.         if (shpnt->sg_tablesize > BUSLOGIC_MAX_SG)
  1289.         shpnt->sg_tablesize = BUSLOGIC_MAX_SG;
  1290.         /* ??? shpnt->base should really be "const unsigned char *"... */
  1291.         shpnt->base = (unsigned char *)bios;
  1292.         shpnt->io_port = base;
  1293.         shpnt->n_io_port = 4;    /* Number of bytes of I/O space used */
  1294.         shpnt->dma_channel = dma;
  1295.         shpnt->irq = irq;
  1296.         HOSTDATA(shpnt)->bios_translation = bios_translation;
  1297.         if (bios_translation == BIOS_TRANSLATION_BIG)
  1298.         buslogic_printk("using extended bios translation.\n");
  1299.         HOSTDATA(shpnt)->last_mbi_used = 2 * BUSLOGIC_MAILBOXES - 1;
  1300.         HOSTDATA(shpnt)->last_mbo_used = BUSLOGIC_MAILBOXES - 1;
  1301.         memset(HOSTDATA(shpnt)->sc, 0, sizeof HOSTDATA(shpnt)->sc);
  1302.         restore_flags(flags);
  1303.  
  1304. #if 0
  1305.         {
  1306.         unsigned char buf[8];
  1307.         unsigned char cmd[]
  1308.             = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  1309.         size_t i;
  1310.  
  1311. #if (BUSLOGIC_DEBUG & BD_DETECT)
  1312.         buslogic_printk("*** READ CAPACITY ***\n");
  1313. #endif
  1314.         for (i = 0; i < sizeof buf; i++)
  1315.             buf[i] = 0x87;
  1316.         for (i = 0; i < 2; i++)
  1317.             if (!buslogic_command(i, cmd, buf, sizeof buf)) {
  1318.             buslogic_printk("LU %u sector_size %d device_size %d\n",
  1319.                     i, *(int *)(buf + 4), *(int *)buf);
  1320.             }
  1321.  
  1322. #if (BUSLOGIC_DEBUG & BD_DETECT)
  1323.         buslogic_printk("*** NOW RUNNING MY OWN TEST ***\n");
  1324. #endif
  1325.         for (i = 0; i < 4; i++) {
  1326.             static buffer[512];
  1327.  
  1328.             cmd[0] = READ_10;
  1329.             cmd[1] = 0;
  1330.             xany2scsi(cmd + 2, i);
  1331.             cmd[6] = 0;
  1332.             cmd[7] = 0;
  1333.             cmd[8] = 1;
  1334.             cmd[9] = 0;
  1335.             buslogic_command(0, cmd, buffer, sizeof buffer);
  1336.         }
  1337.         }
  1338. #endif
  1339.  
  1340.         request_region(bases[indx], 4,"buslogic");
  1341.         /* Register the IO ports that we use */
  1342.         count++;
  1343.         continue;
  1344.       unregister:
  1345.         scsi_unregister(shpnt);
  1346.     }
  1347.     return count;
  1348. }
  1349.  
  1350. static int restart(struct Scsi_Host *shpnt)
  1351. {
  1352.     unsigned int i;
  1353.     unsigned int count = 0;
  1354. #if 0
  1355.     static const unsigned char buscmd[] = { CMD_START_SCSI };
  1356. #endif
  1357.  
  1358.     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
  1359.     if (HOSTDATA(shpnt)->sc[i]
  1360.         && !HOSTDATA(shpnt)->sc[i]->device->soft_reset) {
  1361. #if 0
  1362.         HOSTDATA(shpnt)->mb[i].status
  1363.         = MBX_ACTION_START;    /* Indicate ready to restart... */
  1364. #endif
  1365.         count++;
  1366.     }
  1367.  
  1368.     buslogic_printk("potential to restart %d stalled commands...\n", count);
  1369. #if 0
  1370.     /* start scsi command */
  1371.     if (count)
  1372.     buslogic_out(shpnt->host->io_port, buscmd, sizeof buscmd);
  1373. #endif
  1374.     return 0;
  1375. }
  1376.  
  1377. /* ??? The abort command for the aha1542 does not leave the device in a clean
  1378.    state where it is available to be used again.  As it is not clear whether
  1379.    the same problem exists with BusLogic boards, we will enable this and see
  1380.    if it works. */
  1381. int buslogic_abort(Scsi_Cmnd *scpnt)
  1382. {
  1383. #if 1
  1384.     static const unsigned char buscmd[] = { CMD_START_SCSI };
  1385.     struct mailbox *mb;
  1386.     size_t mbi, mbo;
  1387.     unsigned long flags;
  1388.     unsigned int i;
  1389.  
  1390.     buslogic_printk("%X %X\n",
  1391.             inb(STATUS(scpnt->host->io_port)),
  1392.             inb(INTERRUPT(scpnt->host->io_port)));
  1393.  
  1394.     save_flags(flags);
  1395.     cli();
  1396.     mb = HOSTDATA(scpnt->host)->mb;
  1397.     mbi = HOSTDATA(scpnt->host)->last_mbi_used + 1;
  1398.     if (mbi >= 2 * BUSLOGIC_MAILBOXES)
  1399.     mbi = BUSLOGIC_MAILBOXES;
  1400.  
  1401.     do {
  1402.     if (mb[mbi].status != MBX_NOT_IN_USE)
  1403.         break;
  1404.     mbi++;
  1405.     if (mbi >= 2 * BUSLOGIC_MAILBOXES)
  1406.         mbi = BUSLOGIC_MAILBOXES;
  1407.     } while (mbi != HOSTDATA(scpnt->host)->last_mbi_used);
  1408.     restore_flags(flags);
  1409.  
  1410.     if (mb[mbi].status != MBX_NOT_IN_USE) {
  1411.     buslogic_printk("lost interrupt discovered on irq %d"
  1412.             " - attempting to recover...\n",
  1413.             scpnt->host->irq);
  1414.     {
  1415.         buslogic_interrupt(scpnt->host->irq, NULL);
  1416.         return SCSI_ABORT_SUCCESS;
  1417.     }
  1418.     }
  1419.  
  1420.     /* OK, no lost interrupt.  Try looking to see how many pending commands we
  1421.        think we have. */
  1422.     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
  1423.     if (HOSTDATA(scpnt->host)->sc[i]) {
  1424.         if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
  1425.         buslogic_printk("timed out command pending for %4.4X.\n",
  1426.                 scpnt->request.dev);
  1427.         if (HOSTDATA(scpnt->host)->mb[i].status != MBX_NOT_IN_USE) {
  1428.             buslogic_printk("OGMB still full - restarting...\n");
  1429.             buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
  1430.         }
  1431.         } else
  1432.         buslogic_printk("other pending command: %4.4X\n",
  1433.                 scpnt->request.dev);
  1434.     }
  1435. #endif
  1436.  
  1437. #if (BUSLOGIC_DEBUG & BD_ABORT)
  1438.     buslogic_printk("called\n");
  1439. #endif
  1440.  
  1441. #if 1
  1442.     /* This section of code should be used carefully - some devices cannot
  1443.        abort a command, and this merely makes it worse. */
  1444.     save_flags(flags);
  1445.     cli();
  1446.     for (mbo = 0; mbo < BUSLOGIC_MAILBOXES; mbo++)
  1447.     if (scpnt == HOSTDATA(scpnt->host)->sc[mbo]) {
  1448.         mb[mbo].status = MBX_ACTION_ABORT;
  1449.         buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
  1450.         break;
  1451.     }
  1452.     restore_flags(flags);
  1453. #endif
  1454.  
  1455.     return SCSI_ABORT_SNOOZE;
  1456. }
  1457.  
  1458. /* We do not implement a reset function here, but the upper level code assumes
  1459.    that it will get some kind of response for the command in scpnt.  We must
  1460.    oblige, or the command will hang the SCSI system.  For a first go, we assume
  1461.    that the BusLogic notifies us with all of the pending commands (it does
  1462.    implement soft reset, after all). */
  1463. int buslogic_reset(Scsi_Cmnd *scpnt)
  1464. {
  1465.     static const unsigned char buscmd[] = { CMD_START_SCSI };
  1466.     unsigned int i;
  1467.  
  1468. #if (BUSLOGIC_DEBUG & BD_RESET)
  1469.     buslogic_printk("called\n");
  1470. #endif
  1471. #if 0
  1472.     /* This does a scsi reset for all devices on the bus. */
  1473.     outb(RSBUS, CONTROL(scpnt->host->io_port));
  1474. #else
  1475.     /* This does a selective reset of just the one device. */
  1476.     /* First locate the ccb for this command. */
  1477.     for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
  1478.     if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
  1479.         HOSTDATA(scpnt->host)->ccbs[i].op = CCB_OP_BUS_RESET;
  1480.  
  1481.         /* Now tell the BusLogic to flush all pending commands for this
  1482.            target. */
  1483.         buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
  1484.  
  1485.         /* Here is the tricky part.  What to do next.  Do we get an
  1486.            interrupt for the commands that we aborted with the specified
  1487.            target, or do we generate this on our own?  Try it without first
  1488.            and see what happens. */
  1489.         buslogic_printk("sent BUS DEVICE RESET to target %d.\n",
  1490.                 scpnt->target);
  1491.  
  1492.         /* If the first does not work, then try the second.  I think the
  1493.            first option is more likely to be correct.  Free the command
  1494.            block for all commands running on this target... */
  1495. #if 1
  1496.         for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
  1497.         if (HOSTDATA(scpnt->host)->sc[i]
  1498.             && HOSTDATA(scpnt->host)->sc[i]->target == scpnt->target) {
  1499.             Scsi_Cmnd *sctmp = HOSTDATA(scpnt->host)->sc[i];
  1500.  
  1501.             sctmp->result = DID_RESET << 16;
  1502.             if (sctmp->host_scribble)
  1503.             scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
  1504.             buslogic_printk("sending DID_RESET for target %d.\n",
  1505.                     scpnt->target);
  1506.             sctmp->scsi_done(scpnt);
  1507.  
  1508.             HOSTDATA(scpnt->host)->sc[i] = NULL;
  1509.             HOSTDATA(scpnt->host)->mb[i].status = MBX_NOT_IN_USE;
  1510.         }
  1511.         return SCSI_RESET_SUCCESS;
  1512. #else
  1513.         return SCSI_RESET_PENDING;
  1514. #endif
  1515.     }
  1516. #endif
  1517.     /* No active command at this time, so this means that each time we got some
  1518.        kind of response the last time through.  Tell the mid-level code to
  1519.        request sense information in order to decide what to do next. */
  1520.     return SCSI_RESET_PUNT;
  1521. }
  1522.  
  1523. /* ??? This is probably not correct for series "C" boards.  I believe these
  1524.    support separate mappings for each disk.  We would need to issue a
  1525.    CMD_READ_FW_LOCAL_RAM command to check for the particular drive being
  1526.    queried.  Note that series "C" boards can be differentiated by having
  1527.    HOSTDATA(disk->device->host)->firmware_rev[0] >= '4'. */
  1528. int buslogic_biosparam(Disk *disk, int dev, int *ip)
  1529. {
  1530.     unsigned int size = disk->capacity;
  1531.  
  1532.     /* ip[0] == heads, ip[1] == sectors, ip[2] == cylinders */
  1533.     if (HOSTDATA(disk->device->host)->bios_translation == BIOS_TRANSLATION_BIG
  1534.     && size >= 0x200000) {        /* 1GB */
  1535.     if (size >= 0x400000) {        /* 2GB */
  1536. #if 0    /* ??? Used in earlier kernels, but disagrees with BusLogic info. */
  1537.         if (mb >= 0x800000) {    /* 4GB */
  1538.         ip[0] = 256;
  1539.         ip[1] = 64;
  1540.         } else {
  1541.         ip[0] = 256;
  1542.         ip[1] = 32;
  1543.         }
  1544. #else
  1545.         ip[0] = 256;
  1546.         ip[1] = 64;
  1547. #endif
  1548.     } else {
  1549.         ip[0] = 128;
  1550.         ip[1] = 32;
  1551.     }
  1552.     } else {
  1553.     ip[0] = 64;
  1554.     ip[1] = 32;
  1555.     }
  1556.     ip[2] = size / (ip[0] * ip[1]);
  1557. /*    if (ip[2] > 1024)
  1558.     ip[2] = 1024; */
  1559.     return 0;
  1560. }
  1561.  
  1562. /* called from init/main.c */
  1563. void buslogic_setup(char *str, int *ints)
  1564. {
  1565.     static const unsigned short valid_bases[]
  1566.     = { 0x130, 0x134, 0x230, 0x234, 0x330, 0x334 };
  1567.     static size_t setup_idx = 0;
  1568.     size_t i;
  1569.  
  1570.     if (setup_idx >= ARRAY_SIZE(bases) - 1) {
  1571.     buslogic_printk("called too many times.  Bad LILO params?\n");
  1572.     return;
  1573.     }
  1574.     if (ints[0] != 1) {
  1575.     buslogic_printk("malformed command line.\n");
  1576.     buslogic_printk("usage: buslogic=<portbase>\n");
  1577.     return;
  1578.     }
  1579.     for (i = 0; i < ARRAY_SIZE(valid_bases); i++)
  1580.     if (valid_bases[i] == ints[1]) {
  1581.         bases[setup_idx++] = ints[1];
  1582.         bases[setup_idx] = 0;
  1583.         return;
  1584.     }
  1585.     buslogic_printk("invalid base 0x%X specified.\n", ints[i]);
  1586. }
  1587.  
  1588. #ifdef MODULE
  1589. /* Eventually this will go into an include file, but that's later... */
  1590. Scsi_Host_Template driver_template = BUSLOGIC;
  1591.  
  1592. # include "scsi_module.c"
  1593. #endif
  1594.