Home | History | Annotate | Line # | Download | only in ic
bha.c revision 1.38
      1  1.38    simonb /*	$NetBSD: bha.c,v 1.38 2000/10/03 14:07:36 simonb Exp $	*/
      2  1.38    simonb 
      3  1.38    simonb #include "opt_ddb.h"
      4  1.38    simonb #undef BHADIAG
      5  1.38    simonb #ifdef DDB
      6  1.38    simonb #define	integrate
      7  1.38    simonb #else
      8  1.38    simonb #define	integrate	static inline
      9  1.38    simonb #endif
     10   1.1   mycroft 
     11  1.13   thorpej /*-
     12  1.38    simonb  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
     13  1.13   thorpej  * All rights reserved.
     14  1.13   thorpej  *
     15  1.13   thorpej  * This code is derived from software contributed to The NetBSD Foundation
     16  1.25   mycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
     17  1.25   mycroft  * Simulation Facility, NASA Ames Research Center.
     18  1.13   thorpej  *
     19  1.13   thorpej  * Redistribution and use in source and binary forms, with or without
     20  1.13   thorpej  * modification, are permitted provided that the following conditions
     21  1.13   thorpej  * are met:
     22  1.13   thorpej  * 1. Redistributions of source code must retain the above copyright
     23  1.13   thorpej  *    notice, this list of conditions and the following disclaimer.
     24  1.13   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     25  1.13   thorpej  *    notice, this list of conditions and the following disclaimer in the
     26  1.13   thorpej  *    documentation and/or other materials provided with the distribution.
     27  1.13   thorpej  * 3. All advertising materials mentioning features or use of this software
     28  1.13   thorpej  *    must display the following acknowledgement:
     29  1.13   thorpej  *	This product includes software developed by the NetBSD
     30  1.13   thorpej  *	Foundation, Inc. and its contributors.
     31  1.13   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     32  1.13   thorpej  *    contributors may be used to endorse or promote products derived
     33  1.13   thorpej  *    from this software without specific prior written permission.
     34  1.13   thorpej  *
     35  1.13   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     36  1.13   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     37  1.13   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     38  1.13   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     39  1.13   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     40  1.13   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     41  1.13   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     42  1.13   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     43  1.13   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     44  1.13   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     45  1.13   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     46   1.1   mycroft  */
     47   1.1   mycroft 
     48   1.1   mycroft /*
     49   1.1   mycroft  * Originally written by Julian Elischer (julian (at) tfs.com)
     50   1.1   mycroft  * for TRW Financial Systems for use under the MACH(2.5) operating system.
     51   1.1   mycroft  *
     52   1.1   mycroft  * TRW Financial Systems, in accordance with their agreement with Carnegie
     53   1.1   mycroft  * Mellon University, makes this software available to CMU to distribute
     54   1.1   mycroft  * or use in any manner that they see fit as long as this message is kept with
     55   1.1   mycroft  * the software. For this reason TFS also grants any other persons or
     56   1.1   mycroft  * organisations permission to use or modify this software.
     57   1.1   mycroft  *
     58   1.1   mycroft  * TFS supplies this software to be publicly redistributed
     59   1.1   mycroft  * on the understanding that TFS is not responsible for the correct
     60   1.1   mycroft  * functioning of this software in any circumstances.
     61   1.1   mycroft  */
     62   1.1   mycroft 
     63   1.1   mycroft #include <sys/types.h>
     64   1.1   mycroft #include <sys/param.h>
     65   1.1   mycroft #include <sys/systm.h>
     66  1.35   thorpej #include <sys/callout.h>
     67   1.1   mycroft #include <sys/kernel.h>
     68   1.1   mycroft #include <sys/errno.h>
     69   1.1   mycroft #include <sys/ioctl.h>
     70   1.1   mycroft #include <sys/device.h>
     71   1.1   mycroft #include <sys/malloc.h>
     72   1.1   mycroft #include <sys/buf.h>
     73   1.1   mycroft #include <sys/proc.h>
     74   1.1   mycroft #include <sys/user.h>
     75   1.1   mycroft 
     76  1.37       mrg #include <uvm/uvm_extern.h>
     77  1.31   thorpej 
     78   1.1   mycroft #include <machine/bus.h>
     79   1.1   mycroft #include <machine/intr.h>
     80   1.1   mycroft 
     81  1.14    bouyer #include <dev/scsipi/scsi_all.h>
     82  1.14    bouyer #include <dev/scsipi/scsipi_all.h>
     83  1.14    bouyer #include <dev/scsipi/scsiconf.h>
     84   1.1   mycroft 
     85   1.1   mycroft #include <dev/ic/bhareg.h>
     86   1.1   mycroft #include <dev/ic/bhavar.h>
     87   1.1   mycroft 
     88   1.1   mycroft #ifndef DDB
     89   1.1   mycroft #define Debugger() panic("should call debugger here (bha.c)")
     90   1.1   mycroft #endif /* ! DDB */
     91   1.1   mycroft 
     92  1.13   thorpej #define	BHA_MAXXFER	((BHA_NSEG - 1) << PGSHIFT)
     93   1.1   mycroft 
     94   1.1   mycroft #ifdef BHADEBUG
     95   1.1   mycroft int     bha_debug = 0;
     96   1.1   mycroft #endif /* BHADEBUG */
     97   1.1   mycroft 
     98  1.38    simonb int bha_cmd __P((bus_space_tag_t, bus_space_handle_t, struct bha_softc *,
     99  1.38    simonb 		 int, u_char *, int, u_char *));
    100  1.38    simonb integrate void bha_finish_ccbs __P((struct bha_softc *));
    101  1.38    simonb integrate void bha_reset_ccb __P((struct bha_softc *, struct bha_ccb *));
    102  1.38    simonb void bha_free_ccb __P((struct bha_softc *, struct bha_ccb *));
    103  1.38    simonb integrate int bha_init_ccb __P((struct bha_softc *, struct bha_ccb *));
    104  1.38    simonb struct bha_ccb *bha_get_ccb __P((struct bha_softc *, int));
    105  1.33   mycroft struct bha_ccb *bha_ccb_phys_kv __P((struct bha_softc *, bus_addr_t));
    106  1.38    simonb void bha_queue_ccb __P((struct bha_softc *, struct bha_ccb *));
    107  1.38    simonb void bha_collect_mbo __P((struct bha_softc *));
    108  1.38    simonb void bha_start_ccbs __P((struct bha_softc *));
    109  1.38    simonb void bha_done __P((struct bha_softc *, struct bha_ccb *));
    110  1.38    simonb int bha_init __P((struct bha_softc *));
    111  1.38    simonb void bhaminphys __P((struct buf *));
    112  1.38    simonb int bha_scsi_cmd __P((struct scsipi_xfer *));
    113  1.38    simonb int bha_poll __P((struct bha_softc *, struct scsipi_xfer *, int));
    114  1.38    simonb void bha_timeout __P((void *arg));
    115  1.38    simonb int bha_create_ccbs __P((struct bha_softc *, struct bha_ccb *, int));
    116   1.1   mycroft 
    117   1.1   mycroft /* the below structure is so we have a default dev struct for out link struct */
    118  1.14    bouyer struct scsipi_device bha_dev = {
    119   1.1   mycroft 	NULL,			/* Use default error handler */
    120   1.1   mycroft 	NULL,			/* have a queue, served by this */
    121   1.1   mycroft 	NULL,			/* have no async handler */
    122   1.1   mycroft 	NULL,			/* Use default 'done' routine */
    123   1.1   mycroft };
    124   1.1   mycroft 
    125   1.1   mycroft #define BHA_RESET_TIMEOUT	2000	/* time to wait for reset (mSec) */
    126   1.1   mycroft #define	BHA_ABORT_TIMEOUT	2000	/* time to wait for abort (mSec) */
    127  1.13   thorpej 
    128   1.1   mycroft /*
    129  1.38    simonb  * bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
    130  1.38    simonb  *
    131  1.38    simonb  * Activate Adapter command
    132  1.38    simonb  *    icnt:   number of args (outbound bytes including opcode)
    133  1.38    simonb  *    ibuf:   argument buffer
    134  1.38    simonb  *    ocnt:   number of expected returned bytes
    135  1.38    simonb  *    obuf:   result buffer
    136  1.38    simonb  *    wait:   number of seconds to wait for response
    137  1.38    simonb  *
    138  1.38    simonb  * Performs an adapter command through the ports.  Not to be confused with a
    139  1.38    simonb  * scsi command, which is read in via the dma; one of the adapter commands
    140  1.38    simonb  * tells it to read in a scsi command.
    141   1.1   mycroft  */
    142  1.38    simonb int
    143  1.38    simonb bha_cmd(iot, ioh, sc, icnt, ibuf, ocnt, obuf)
    144  1.38    simonb 	bus_space_tag_t iot;
    145  1.38    simonb 	bus_space_handle_t ioh;
    146   1.1   mycroft 	struct bha_softc *sc;
    147  1.38    simonb 	int icnt, ocnt;
    148  1.38    simonb 	u_char *ibuf, *obuf;
    149   1.1   mycroft {
    150  1.38    simonb 	const char *name;
    151  1.38    simonb 	int i;
    152  1.38    simonb 	int wait;
    153  1.38    simonb 	u_char sts;
    154  1.38    simonb 	u_char opcode = ibuf[0];
    155  1.38    simonb 
    156  1.38    simonb 	if (sc != NULL)
    157  1.38    simonb 		name = sc->sc_dev.dv_xname;
    158  1.38    simonb 	else
    159  1.38    simonb 		name = "(bha probe)";
    160   1.1   mycroft 
    161  1.38    simonb 	/*
    162  1.38    simonb 	 * Calculate a reasonable timeout for the command.
    163  1.38    simonb 	 */
    164  1.38    simonb 	switch (opcode) {
    165  1.38    simonb 	case BHA_INQUIRE_DEVICES:
    166  1.38    simonb 	case BHA_INQUIRE_DEVICES_2:
    167  1.38    simonb 		wait = 90 * 20000;
    168  1.38    simonb 		break;
    169  1.38    simonb 	default:
    170  1.38    simonb 		wait = 1 * 20000;
    171  1.38    simonb 		break;
    172  1.38    simonb 	}
    173   1.1   mycroft 
    174  1.38    simonb 	/*
    175  1.38    simonb 	 * Wait for the adapter to go idle, unless it's one of
    176  1.38    simonb 	 * the commands which don't need this
    177  1.38    simonb 	 */
    178  1.38    simonb 	if (opcode != BHA_MBO_INTR_EN) {
    179  1.38    simonb 		for (i = 20000; i; i--) {	/* 1 sec? */
    180  1.38    simonb 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    181  1.38    simonb 			if (sts & BHA_STAT_IDLE)
    182  1.38    simonb 				break;
    183  1.38    simonb 			delay(50);
    184  1.38    simonb 		}
    185  1.38    simonb 		if (!i) {
    186  1.38    simonb 			printf("%s: bha_cmd, host not idle(0x%x)\n",
    187  1.38    simonb 			    name, sts);
    188  1.38    simonb 			return (1);
    189  1.38    simonb 		}
    190  1.38    simonb 	}
    191  1.38    simonb 	/*
    192  1.38    simonb 	 * Now that it is idle, if we expect output, preflush the
    193  1.38    simonb 	 * queue feeding to us.
    194  1.38    simonb 	 */
    195  1.38    simonb 	if (ocnt) {
    196  1.38    simonb 		while ((bus_space_read_1(iot, ioh, BHA_STAT_PORT)) &
    197  1.38    simonb 		    BHA_STAT_DF)
    198  1.38    simonb 			bus_space_read_1(iot, ioh, BHA_DATA_PORT);
    199  1.38    simonb 	}
    200  1.38    simonb 	/*
    201  1.38    simonb 	 * Output the command and the number of arguments given
    202  1.38    simonb 	 * for each byte, first check the port is empty.
    203  1.38    simonb 	 */
    204  1.38    simonb 	while (icnt--) {
    205  1.38    simonb 		for (i = wait; i; i--) {
    206  1.38    simonb 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    207  1.38    simonb 			if (!(sts & BHA_STAT_CDF))
    208  1.38    simonb 				break;
    209  1.38    simonb 			delay(50);
    210  1.38    simonb 		}
    211  1.38    simonb 		if (!i) {
    212  1.38    simonb 			if (opcode != BHA_INQUIRE_REVISION)
    213  1.38    simonb 				printf("%s: bha_cmd, cmd/data port full\n",
    214  1.38    simonb 				    name);
    215  1.38    simonb 			goto bad;
    216  1.38    simonb 		}
    217  1.38    simonb 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, *ibuf++);
    218  1.38    simonb 	}
    219  1.38    simonb 	/*
    220  1.38    simonb 	 * If we expect input, loop that many times, each time,
    221  1.38    simonb 	 * looking for the data register to have valid data
    222  1.38    simonb 	 */
    223  1.38    simonb 	while (ocnt--) {
    224  1.38    simonb 		for (i = wait; i; i--) {
    225  1.38    simonb 			sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    226  1.38    simonb 			if (sts & BHA_STAT_DF)
    227  1.38    simonb 				break;
    228  1.38    simonb 			delay(50);
    229  1.38    simonb 		}
    230  1.38    simonb 		if (!i) {
    231  1.38    simonb 			if (opcode != BHA_INQUIRE_REVISION)
    232  1.38    simonb 				printf("%s: bha_cmd, cmd/data port empty %d\n",
    233  1.38    simonb 				    name, ocnt);
    234  1.38    simonb 			goto bad;
    235  1.38    simonb 		}
    236  1.38    simonb 		*obuf++ = bus_space_read_1(iot, ioh, BHA_DATA_PORT);
    237  1.38    simonb 	}
    238  1.38    simonb 	/*
    239  1.38    simonb 	 * Wait for the board to report a finished instruction.
    240  1.38    simonb 	 * We may get an extra interrupt for the HACC signal, but this is
    241  1.38    simonb 	 * unimportant.
    242  1.38    simonb 	 */
    243  1.38    simonb 	if (opcode != BHA_MBO_INTR_EN && opcode != BHA_MODIFY_IOPORT) {
    244  1.38    simonb 		for (i = 20000; i; i--) {	/* 1 sec? */
    245  1.38    simonb 			sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
    246  1.38    simonb 			/* XXX Need to save this in the interrupt handler? */
    247  1.38    simonb 			if (sts & BHA_INTR_HACC)
    248  1.38    simonb 				break;
    249  1.38    simonb 			delay(50);
    250  1.38    simonb 		}
    251  1.38    simonb 		if (!i) {
    252  1.38    simonb 			printf("%s: bha_cmd, host not finished(0x%x)\n",
    253  1.38    simonb 			    name, sts);
    254  1.38    simonb 			return (1);
    255  1.38    simonb 		}
    256  1.38    simonb 	}
    257  1.38    simonb 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
    258  1.38    simonb 	return (0);
    259   1.9   mycroft 
    260  1.38    simonb bad:
    261  1.38    simonb 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_SRST);
    262  1.38    simonb 	return (1);
    263   1.1   mycroft }
    264   1.1   mycroft 
    265   1.1   mycroft /*
    266  1.38    simonb  * Attach all the sub-devices we can find
    267   1.1   mycroft  */
    268   1.1   mycroft void
    269  1.11   mycroft bha_attach(sc, bpd)
    270   1.1   mycroft 	struct bha_softc *sc;
    271  1.11   mycroft 	struct bha_probe_data *bpd;
    272   1.1   mycroft {
    273   1.1   mycroft 
    274   1.1   mycroft 	/*
    275  1.27   thorpej 	 * Fill in the adapter.
    276  1.27   thorpej 	 */
    277  1.27   thorpej 	sc->sc_adapter.scsipi_cmd = bha_scsi_cmd;
    278  1.38    simonb 	sc->sc_adapter.scsipi_minphys = bhaminphys;
    279  1.27   thorpej 
    280  1.27   thorpej 	/*
    281  1.14    bouyer 	 * fill in the prototype scsipi_link.
    282   1.1   mycroft 	 */
    283  1.14    bouyer 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    284   1.1   mycroft 	sc->sc_link.adapter_softc = sc;
    285  1.38    simonb 	sc->sc_link.scsipi_scsi.adapter_target = bpd->sc_scsi_dev;
    286  1.27   thorpej 	sc->sc_link.adapter = &sc->sc_adapter;
    287   1.1   mycroft 	sc->sc_link.device = &bha_dev;
    288   1.1   mycroft 	sc->sc_link.openings = 4;
    289  1.38    simonb 	sc->sc_link.scsipi_scsi.max_target = bpd->sc_iswide ? 15 : 7;
    290  1.38    simonb 	sc->sc_link.scsipi_scsi.max_lun = 7;
    291  1.14    bouyer 	sc->sc_link.type = BUS_SCSI;
    292  1.12   thorpej 
    293  1.13   thorpej 	TAILQ_INIT(&sc->sc_free_ccb);
    294  1.13   thorpej 	TAILQ_INIT(&sc->sc_waiting_ccb);
    295  1.29   thorpej 	TAILQ_INIT(&sc->sc_queue);
    296  1.13   thorpej 
    297  1.38    simonb 	bha_inquire_setup_information(sc);
    298  1.38    simonb 
    299  1.38    simonb 	printf("%s: model BT-%s, firmware %s\n", sc->sc_dev.dv_xname,
    300  1.38    simonb 	    sc->sc_model, sc->sc_firmware);
    301  1.38    simonb 
    302  1.38    simonb 	if (bha_init(sc) != 0) {
    303  1.38    simonb 		/* Error during initialization! */
    304  1.22   thorpej 		return;
    305  1.38    simonb 	}
    306  1.38    simonb 
    307  1.38    simonb 	/*
    308  1.38    simonb 	 * ask the adapter what subunits are present
    309  1.38    simonb 	 */
    310  1.38    simonb 	config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
    311  1.38    simonb }
    312  1.38    simonb 
    313  1.38    simonb integrate void
    314  1.38    simonb bha_finish_ccbs(sc)
    315  1.38    simonb 	struct bha_softc *sc;
    316  1.38    simonb {
    317  1.38    simonb 	struct bha_mbx_in *wmbi;
    318  1.38    simonb 	struct bha_ccb *ccb;
    319  1.38    simonb 	int i;
    320  1.38    simonb 
    321  1.38    simonb 	wmbi = wmbx->tmbi;
    322   1.1   mycroft 
    323  1.38    simonb 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    324  1.38    simonb 	    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    325  1.38    simonb 	    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    326  1.38    simonb 
    327  1.38    simonb 	if (wmbi->stat == BHA_MBI_FREE) {
    328  1.38    simonb 		for (i = 0; i < BHA_MBX_SIZE; i++) {
    329  1.38    simonb 			if (wmbi->stat != BHA_MBI_FREE) {
    330  1.38    simonb 				printf("%s: mbi not in round-robin order\n",
    331  1.38    simonb 				    sc->sc_dev.dv_xname);
    332  1.38    simonb 				goto AGAIN;
    333  1.38    simonb 			}
    334  1.38    simonb 			bha_nextmbx(wmbi, wmbx, mbi);
    335  1.38    simonb 			bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    336  1.38    simonb 			    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    337  1.38    simonb 			    BUS_DMASYNC_POSTREAD);
    338  1.38    simonb 		}
    339  1.38    simonb #ifdef BHADIAGnot
    340  1.38    simonb 		printf("%s: mbi interrupt with no full mailboxes\n",
    341   1.1   mycroft 		    sc->sc_dev.dv_xname);
    342  1.38    simonb #endif
    343   1.1   mycroft 		return;
    344   1.1   mycroft 	}
    345   1.1   mycroft 
    346  1.38    simonb AGAIN:
    347  1.38    simonb 	do {
    348  1.38    simonb 		ccb = bha_ccb_phys_kv(sc, phystol(wmbi->ccb_addr));
    349  1.38    simonb 		if (!ccb) {
    350  1.38    simonb 			printf("%s: bad mbi ccb pointer; skipping\n",
    351  1.38    simonb 			    sc->sc_dev.dv_xname);
    352  1.38    simonb 			goto next;
    353  1.38    simonb 		}
    354  1.38    simonb 
    355  1.38    simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    356  1.38    simonb 		    BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
    357  1.38    simonb 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    358  1.38    simonb 
    359  1.38    simonb #ifdef BHADEBUG
    360  1.38    simonb 		if (bha_debug) {
    361  1.38    simonb 			struct scsi_generic *cmd = &ccb->scsi_cmd;
    362  1.38    simonb 			printf("op=%x %x %x %x %x %x\n",
    363  1.38    simonb 			    cmd->opcode, cmd->bytes[0], cmd->bytes[1],
    364  1.38    simonb 			    cmd->bytes[2], cmd->bytes[3], cmd->bytes[4]);
    365  1.38    simonb 			printf("stat %x for mbi addr = 0x%p, ",
    366  1.38    simonb 			    wmbi->stat, wmbi);
    367  1.38    simonb 			printf("ccb addr = %p\n", ccb);
    368  1.38    simonb 		}
    369  1.38    simonb #endif /* BHADEBUG */
    370  1.38    simonb 
    371  1.38    simonb 		switch (wmbi->stat) {
    372  1.38    simonb 		case BHA_MBI_OK:
    373  1.38    simonb 		case BHA_MBI_ERROR:
    374  1.38    simonb 			if ((ccb->flags & CCB_ABORT) != 0) {
    375  1.38    simonb 				/*
    376  1.38    simonb 				 * If we already started an abort, wait for it
    377  1.38    simonb 				 * to complete before clearing the CCB.  We
    378  1.38    simonb 				 * could instead just clear CCB_SENDING, but
    379  1.38    simonb 				 * what if the mailbox was already received?
    380  1.38    simonb 				 * The worst that happens here is that we clear
    381  1.38    simonb 				 * the CCB a bit later than we need to.  BFD.
    382  1.38    simonb 				 */
    383  1.38    simonb 				goto next;
    384  1.38    simonb 			}
    385  1.38    simonb 			break;
    386  1.38    simonb 
    387  1.38    simonb 		case BHA_MBI_ABORT:
    388  1.38    simonb 		case BHA_MBI_UNKNOWN:
    389  1.38    simonb 			/*
    390  1.38    simonb 			 * Even if the CCB wasn't found, we clear it anyway.
    391  1.38    simonb 			 * See preceeding comment.
    392  1.38    simonb 			 */
    393  1.38    simonb 			break;
    394  1.38    simonb 
    395  1.38    simonb 		default:
    396  1.38    simonb 			printf("%s: bad mbi status %02x; skipping\n",
    397  1.38    simonb 			    sc->sc_dev.dv_xname, wmbi->stat);
    398  1.38    simonb 			goto next;
    399  1.38    simonb 		}
    400  1.38    simonb 
    401  1.38    simonb 		callout_stop(&ccb->xs->xs_callout);
    402  1.38    simonb 		bha_done(sc, ccb);
    403  1.38    simonb 
    404  1.38    simonb 	next:
    405  1.38    simonb 		wmbi->stat = BHA_MBI_FREE;
    406  1.38    simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    407  1.38    simonb 		    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    408  1.38    simonb 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    409  1.38    simonb 		bha_nextmbx(wmbi, wmbx, mbi);
    410  1.38    simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    411  1.38    simonb 		    BHA_MBI_OFF(wmbi), sizeof(struct bha_mbx_in),
    412  1.38    simonb 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    413  1.38    simonb 	} while (wmbi->stat != BHA_MBI_FREE);
    414   1.1   mycroft 
    415  1.38    simonb 	wmbx->tmbi = wmbi;
    416   1.1   mycroft }
    417   1.1   mycroft 
    418   1.1   mycroft /*
    419  1.38    simonb  * Catch an interrupt from the adaptor
    420   1.1   mycroft  */
    421   1.1   mycroft int
    422   1.1   mycroft bha_intr(arg)
    423   1.1   mycroft 	void *arg;
    424   1.1   mycroft {
    425   1.1   mycroft 	struct bha_softc *sc = arg;
    426   1.4   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    427   1.4   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    428   1.1   mycroft 	u_char sts;
    429   1.1   mycroft 
    430   1.1   mycroft #ifdef BHADEBUG
    431   1.3  christos 	printf("%s: bha_intr ", sc->sc_dev.dv_xname);
    432   1.1   mycroft #endif /* BHADEBUG */
    433   1.1   mycroft 
    434   1.1   mycroft 	/*
    435   1.1   mycroft 	 * First acknowlege the interrupt, Then if it's not telling about
    436   1.1   mycroft 	 * a completed operation just return.
    437   1.1   mycroft 	 */
    438   1.4   thorpej 	sts = bus_space_read_1(iot, ioh, BHA_INTR_PORT);
    439   1.1   mycroft 	if ((sts & BHA_INTR_ANYINTR) == 0)
    440   1.1   mycroft 		return (0);
    441   1.4   thorpej 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT, BHA_CTRL_IRST);
    442   1.1   mycroft 
    443   1.1   mycroft #ifdef BHADIAG
    444   1.1   mycroft 	/* Make sure we clear CCB_SENDING before finishing a CCB. */
    445   1.1   mycroft 	bha_collect_mbo(sc);
    446   1.1   mycroft #endif
    447   1.1   mycroft 
    448   1.1   mycroft 	/* Mail box out empty? */
    449   1.1   mycroft 	if (sts & BHA_INTR_MBOA) {
    450   1.1   mycroft 		struct bha_toggle toggle;
    451   1.1   mycroft 
    452   1.1   mycroft 		toggle.cmd.opcode = BHA_MBO_INTR_EN;
    453   1.1   mycroft 		toggle.cmd.enable = 0;
    454   1.4   thorpej 		bha_cmd(iot, ioh, sc,
    455   1.1   mycroft 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
    456   1.1   mycroft 		    0, (u_char *)0);
    457   1.1   mycroft 		bha_start_ccbs(sc);
    458   1.1   mycroft 	}
    459   1.1   mycroft 
    460   1.1   mycroft 	/* Mail box in full? */
    461   1.1   mycroft 	if (sts & BHA_INTR_MBIF)
    462   1.1   mycroft 		bha_finish_ccbs(sc);
    463   1.1   mycroft 
    464   1.1   mycroft 	return (1);
    465   1.1   mycroft }
    466   1.1   mycroft 
    467  1.38    simonb integrate void
    468  1.38    simonb bha_reset_ccb(sc, ccb)
    469  1.38    simonb 	struct bha_softc *sc;
    470  1.38    simonb 	struct bha_ccb *ccb;
    471  1.38    simonb {
    472  1.38    simonb 
    473  1.38    simonb 	ccb->flags = 0;
    474  1.38    simonb }
    475   1.1   mycroft 
    476   1.1   mycroft /*
    477  1.38    simonb  * A ccb is put onto the free list.
    478   1.1   mycroft  */
    479  1.38    simonb void
    480  1.38    simonb bha_free_ccb(sc, ccb)
    481  1.38    simonb 	struct bha_softc *sc;
    482  1.38    simonb 	struct bha_ccb *ccb;
    483  1.31   thorpej {
    484  1.38    simonb 	int s;
    485   1.1   mycroft 
    486  1.38    simonb 	s = splbio();
    487   1.1   mycroft 
    488  1.38    simonb 	bha_reset_ccb(sc, ccb);
    489  1.38    simonb 	TAILQ_INSERT_HEAD(&sc->sc_free_ccb, ccb, chain);
    490   1.1   mycroft 
    491   1.1   mycroft 	/*
    492  1.38    simonb 	 * If there were none, wake anybody waiting for one to come free,
    493  1.38    simonb 	 * starting with queued entries.
    494   1.1   mycroft 	 */
    495  1.38    simonb 	if (ccb->chain.tqe_next == 0)
    496  1.38    simonb 		wakeup(&sc->sc_free_ccb);
    497  1.31   thorpej 
    498  1.38    simonb 	splx(s);
    499  1.38    simonb }
    500  1.38    simonb 
    501  1.38    simonb integrate int
    502  1.38    simonb bha_init_ccb(sc, ccb)
    503  1.38    simonb 	struct bha_softc *sc;
    504  1.38    simonb 	struct bha_ccb *ccb;
    505  1.38    simonb {
    506  1.38    simonb 	bus_dma_tag_t dmat = sc->sc_dmat;
    507  1.38    simonb 	int hashnum, error;
    508   1.1   mycroft 
    509  1.31   thorpej 	/*
    510  1.38    simonb 	 * Create the DMA map for this CCB.
    511  1.31   thorpej 	 */
    512  1.38    simonb 	error = bus_dmamap_create(dmat, BHA_MAXXFER, BHA_NSEG, BHA_MAXXFER,
    513  1.38    simonb 	    0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW | sc->sc_dmaflags,
    514  1.38    simonb 	    &ccb->dmamap_xfer);
    515  1.38    simonb 	if (error) {
    516  1.38    simonb 		printf("%s: unable to create ccb DMA map, error = %d\n",
    517  1.38    simonb 		    sc->sc_dev.dv_xname, error);
    518  1.38    simonb 		return (error);
    519  1.31   thorpej 	}
    520   1.1   mycroft 
    521  1.13   thorpej 	/*
    522  1.38    simonb 	 * put in the phystokv hash table
    523  1.38    simonb 	 * Never gets taken out.
    524  1.13   thorpej 	 */
    525  1.38    simonb 	ccb->hashkey = sc->sc_dmamap_control->dm_segs[0].ds_addr +
    526  1.38    simonb 	    BHA_CCB_OFF(ccb);
    527  1.38    simonb 	hashnum = CCB_HASH(ccb->hashkey);
    528  1.38    simonb 	ccb->nexthash = sc->sc_ccbhash[hashnum];
    529  1.38    simonb 	sc->sc_ccbhash[hashnum] = ccb;
    530  1.38    simonb 	bha_reset_ccb(sc, ccb);
    531  1.38    simonb 	return (0);
    532  1.38    simonb }
    533  1.38    simonb 
    534  1.38    simonb /*
    535  1.38    simonb  * Create a set of ccbs and add them to the free list.  Called once
    536  1.38    simonb  * by bha_init().  We return the number of CCBs successfully created.
    537  1.38    simonb  */
    538  1.38    simonb int
    539  1.38    simonb bha_create_ccbs(sc, ccbstore, count)
    540  1.38    simonb 	struct bha_softc *sc;
    541  1.38    simonb 	struct bha_ccb *ccbstore;
    542  1.38    simonb 	int count;
    543  1.38    simonb {
    544  1.38    simonb 	struct bha_ccb *ccb;
    545  1.38    simonb 	int i, error;
    546  1.38    simonb 
    547  1.38    simonb 	bzero(ccbstore, sizeof(struct bha_ccb) * count);
    548  1.38    simonb 	for (i = 0; i < count; i++) {
    549  1.38    simonb 		ccb = &ccbstore[i];
    550  1.38    simonb 		if ((error = bha_init_ccb(sc, ccb)) != 0) {
    551  1.38    simonb 			printf("%s: unable to initialize ccb, error = %d\n",
    552  1.38    simonb 			    sc->sc_dev.dv_xname, error);
    553  1.38    simonb 			goto out;
    554  1.31   thorpej 		}
    555  1.38    simonb 		TAILQ_INSERT_TAIL(&sc->sc_free_ccb, ccb, chain);
    556  1.16   thorpej 	}
    557  1.38    simonb  out:
    558  1.38    simonb 	return (i);
    559  1.38    simonb }
    560  1.13   thorpej 
    561  1.38    simonb /*
    562  1.38    simonb  * Get a free ccb
    563  1.38    simonb  *
    564  1.38    simonb  * If there are none, see if we can allocate a new one.  If so, put it in
    565  1.38    simonb  * the hash table too otherwise either return an error or sleep.
    566  1.38    simonb  */
    567  1.38    simonb struct bha_ccb *
    568  1.38    simonb bha_get_ccb(sc, flags)
    569  1.38    simonb 	struct bha_softc *sc;
    570  1.38    simonb 	int flags;
    571  1.38    simonb {
    572  1.38    simonb 	struct bha_ccb *ccb;
    573  1.38    simonb 	int s;
    574  1.31   thorpej 
    575  1.38    simonb 	s = splbio();
    576  1.31   thorpej 
    577   1.1   mycroft 	/*
    578  1.38    simonb 	 * If we can and have to, sleep waiting for one to come free
    579  1.38    simonb 	 * but only if we can't allocate a new one.
    580   1.1   mycroft 	 */
    581  1.38    simonb 	for (;;) {
    582  1.38    simonb 		ccb = sc->sc_free_ccb.tqh_first;
    583  1.38    simonb 		if (ccb) {
    584  1.38    simonb 			TAILQ_REMOVE(&sc->sc_free_ccb, ccb, chain);
    585  1.38    simonb 			break;
    586  1.31   thorpej 		}
    587  1.38    simonb 		if ((flags & XS_CTL_NOSLEEP) != 0)
    588  1.38    simonb 			goto out;
    589  1.38    simonb 		tsleep(&sc->sc_free_ccb, PRIBIO, "bhaccb", 0);
    590   1.1   mycroft 	}
    591   1.1   mycroft 
    592  1.38    simonb 	ccb->flags |= CCB_ALLOC;
    593  1.31   thorpej 
    594  1.38    simonb out:
    595   1.1   mycroft 	splx(s);
    596  1.38    simonb 	return (ccb);
    597  1.38    simonb }
    598   1.1   mycroft 
    599  1.38    simonb /*
    600  1.38    simonb  * Given a physical address, find the ccb that it corresponds to.
    601  1.38    simonb  */
    602  1.38    simonb struct bha_ccb *
    603  1.38    simonb bha_ccb_phys_kv(sc, ccb_phys)
    604  1.38    simonb 	struct bha_softc *sc;
    605  1.38    simonb 	bus_addr_t ccb_phys;
    606  1.38    simonb {
    607  1.38    simonb 	int hashnum = CCB_HASH(ccb_phys);
    608  1.38    simonb 	struct bha_ccb *ccb = sc->sc_ccbhash[hashnum];
    609   1.1   mycroft 
    610  1.38    simonb 	while (ccb) {
    611  1.38    simonb 		if (ccb->hashkey == ccb_phys)
    612  1.38    simonb 			break;
    613  1.38    simonb 		ccb = ccb->nexthash;
    614   1.1   mycroft 	}
    615  1.38    simonb 	return (ccb);
    616   1.1   mycroft }
    617   1.1   mycroft 
    618   1.1   mycroft /*
    619  1.38    simonb  * Queue a CCB to be sent to the controller, and send it if possible.
    620   1.1   mycroft  */
    621   1.1   mycroft void
    622  1.38    simonb bha_queue_ccb(sc, ccb)
    623  1.38    simonb 	struct bha_softc *sc;
    624  1.38    simonb 	struct bha_ccb *ccb;
    625   1.1   mycroft {
    626   1.1   mycroft 
    627  1.38    simonb 	TAILQ_INSERT_TAIL(&sc->sc_waiting_ccb, ccb, chain);
    628  1.38    simonb 	bha_start_ccbs(sc);
    629   1.1   mycroft }
    630   1.1   mycroft 
    631   1.1   mycroft /*
    632  1.38    simonb  * Garbage collect mailboxes that are no longer in use.
    633   1.1   mycroft  */
    634   1.1   mycroft void
    635  1.38    simonb bha_collect_mbo(sc)
    636   1.1   mycroft 	struct bha_softc *sc;
    637  1.38    simonb {
    638  1.38    simonb 	struct bha_mbx_out *wmbo;	/* Mail Box Out pointer */
    639  1.38    simonb #ifdef BHADIAG
    640  1.31   thorpej 	struct bha_ccb *ccb;
    641  1.38    simonb #endif
    642  1.38    simonb 
    643  1.38    simonb 	wmbo = wmbx->cmbo;
    644   1.1   mycroft 
    645  1.38    simonb 	while (sc->sc_mbofull > 0) {
    646  1.38    simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    647  1.38    simonb 		    BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
    648  1.38    simonb 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    649  1.38    simonb 		if (wmbo->cmd != BHA_MBO_FREE)
    650  1.38    simonb 			break;
    651   1.1   mycroft 
    652   1.1   mycroft #ifdef BHADIAG
    653  1.38    simonb 		ccb = bha_ccb_phys_kv(sc, phystol(wmbo->ccb_addr));
    654  1.38    simonb 		ccb->flags &= ~CCB_SENDING;
    655   1.1   mycroft #endif
    656  1.38    simonb 
    657  1.38    simonb 		--sc->sc_mbofull;
    658  1.38    simonb 		bha_nextmbx(wmbo, wmbx, mbo);
    659  1.31   thorpej 	}
    660   1.1   mycroft 
    661  1.38    simonb 	wmbx->cmbo = wmbo;
    662  1.38    simonb }
    663  1.38    simonb 
    664  1.38    simonb /*
    665  1.38    simonb  * Send as many CCBs as we have empty mailboxes for.
    666  1.38    simonb  */
    667  1.38    simonb void
    668  1.38    simonb bha_start_ccbs(sc)
    669  1.38    simonb 	struct bha_softc *sc;
    670  1.38    simonb {
    671  1.38    simonb 	bus_space_tag_t iot = sc->sc_iot;
    672  1.38    simonb 	bus_space_handle_t ioh = sc->sc_ioh;
    673  1.38    simonb 	struct bha_mbx_out *wmbo;	/* Mail Box Out pointer */
    674  1.38    simonb 	struct bha_ccb *ccb;
    675  1.38    simonb 
    676  1.38    simonb 	wmbo = wmbx->tmbo;
    677  1.38    simonb 
    678  1.38    simonb 	while ((ccb = sc->sc_waiting_ccb.tqh_first) != NULL) {
    679  1.38    simonb 		if (sc->sc_mbofull >= BHA_MBX_SIZE) {
    680  1.38    simonb 			bha_collect_mbo(sc);
    681  1.38    simonb 			if (sc->sc_mbofull >= BHA_MBX_SIZE) {
    682  1.38    simonb 				struct bha_toggle toggle;
    683  1.38    simonb 
    684  1.38    simonb 				toggle.cmd.opcode = BHA_MBO_INTR_EN;
    685  1.38    simonb 				toggle.cmd.enable = 1;
    686  1.38    simonb 				bha_cmd(iot, ioh, sc,
    687  1.38    simonb 				    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
    688  1.38    simonb 				    0, (u_char *)0);
    689  1.38    simonb 				break;
    690  1.38    simonb 			}
    691  1.38    simonb 		}
    692  1.38    simonb 
    693  1.38    simonb 		TAILQ_REMOVE(&sc->sc_waiting_ccb, ccb, chain);
    694  1.38    simonb #ifdef BHADIAG
    695  1.38    simonb 		ccb->flags |= CCB_SENDING;
    696  1.38    simonb #endif
    697  1.38    simonb 
    698  1.38    simonb 		/* Link ccb to mbo. */
    699  1.38    simonb 		ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
    700  1.38    simonb 		    BHA_CCB_OFF(ccb), wmbo->ccb_addr);
    701  1.38    simonb 		if (ccb->flags & CCB_ABORT)
    702  1.38    simonb 			wmbo->cmd = BHA_MBO_ABORT;
    703  1.38    simonb 		else
    704  1.38    simonb 			wmbo->cmd = BHA_MBO_START;
    705  1.38    simonb 
    706  1.38    simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
    707  1.38    simonb 		    BHA_MBO_OFF(wmbo), sizeof(struct bha_mbx_out),
    708  1.38    simonb 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    709  1.38    simonb 
    710  1.38    simonb 		/* Tell the card to poll immediately. */
    711  1.38    simonb 		bus_space_write_1(iot, ioh, BHA_CMD_PORT, BHA_START_SCSI);
    712  1.38    simonb 
    713  1.38    simonb 		if ((ccb->xs->xs_control & XS_CTL_POLL) == 0)
    714  1.38    simonb 			callout_reset(&ccb->xs->xs_callout,
    715  1.38    simonb 			    (ccb->timeout * hz) / 1000, bha_timeout, ccb);
    716  1.38    simonb 
    717  1.38    simonb 
    718  1.38    simonb 		++sc->sc_mbofull;
    719  1.38    simonb 		bha_nextmbx(wmbo, wmbx, mbo);
    720  1.38    simonb 	}
    721  1.38    simonb 
    722  1.38    simonb 	wmbx->tmbo = wmbo;
    723  1.38    simonb }
    724  1.38    simonb 
    725  1.38    simonb /*
    726  1.38    simonb  * We have a ccb which has been processed by the
    727  1.38    simonb  * adaptor, now we look to see how the operation
    728  1.38    simonb  * went. Wake up the owner if waiting
    729  1.38    simonb  */
    730  1.38    simonb void
    731  1.38    simonb bha_done(sc, ccb)
    732  1.38    simonb 	struct bha_softc *sc;
    733  1.38    simonb 	struct bha_ccb *ccb;
    734  1.38    simonb {
    735  1.38    simonb 	bus_dma_tag_t dmat = sc->sc_dmat;
    736  1.38    simonb 	struct scsipi_sense_data *s1, *s2;
    737  1.38    simonb 	struct scsipi_xfer *xs = ccb->xs;
    738  1.38    simonb 
    739  1.38    simonb 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("bha_done\n"));
    740  1.38    simonb 
    741  1.31   thorpej 	/*
    742  1.31   thorpej 	 * If we were a data transfer, unload the map that described
    743  1.31   thorpej 	 * the data buffer.
    744  1.31   thorpej 	 */
    745  1.31   thorpej 	if (xs->datalen) {
    746  1.31   thorpej 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
    747  1.31   thorpej 		    ccb->dmamap_xfer->dm_mapsize,
    748  1.31   thorpej 		    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_POSTREAD :
    749  1.31   thorpej 		    BUS_DMASYNC_POSTWRITE);
    750  1.31   thorpej 		bus_dmamap_unload(dmat, ccb->dmamap_xfer);
    751   1.1   mycroft 	}
    752   1.1   mycroft 
    753  1.38    simonb 	/*
    754  1.38    simonb 	 * Otherwise, put the results of the operation
    755  1.38    simonb 	 * into the xfer and call whoever started it
    756  1.38    simonb 	 */
    757  1.38    simonb #ifdef BHADIAG
    758  1.38    simonb 	if (ccb->flags & CCB_SENDING) {
    759  1.38    simonb 		printf("%s: exiting ccb still in transit!\n",
    760  1.38    simonb 		    sc->sc_dev.dv_xname);
    761  1.38    simonb 		Debugger();
    762  1.38    simonb 		return;
    763  1.38    simonb 	}
    764  1.38    simonb #endif
    765  1.38    simonb 	if ((ccb->flags & CCB_ALLOC) == 0) {
    766  1.38    simonb 		printf("%s: exiting ccb not allocated!\n",
    767  1.38    simonb 		    sc->sc_dev.dv_xname);
    768  1.38    simonb 		Debugger();
    769  1.38    simonb 		return;
    770  1.38    simonb 	}
    771  1.31   thorpej 	if (xs->error == XS_NOERROR) {
    772  1.31   thorpej 		if (ccb->host_stat != BHA_OK) {
    773  1.31   thorpej 			switch (ccb->host_stat) {
    774  1.31   thorpej 			case BHA_SEL_TIMEOUT:	/* No response */
    775  1.31   thorpej 				xs->error = XS_SELTIMEOUT;
    776  1.31   thorpej 				break;
    777  1.31   thorpej 			default:	/* Other scsi protocol messes */
    778  1.31   thorpej 				printf("%s: host_stat %x\n",
    779   1.1   mycroft 				    sc->sc_dev.dv_xname, ccb->host_stat);
    780   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    781   1.1   mycroft 				break;
    782   1.1   mycroft 			}
    783   1.1   mycroft 		} else if (ccb->target_stat != SCSI_OK) {
    784   1.1   mycroft 			switch (ccb->target_stat) {
    785   1.1   mycroft 			case SCSI_CHECK:
    786  1.38    simonb 				s1 = &ccb->scsi_sense;
    787  1.38    simonb 				s2 = &xs->sense.scsi_sense;
    788  1.38    simonb 				*s2 = *s1;
    789   1.1   mycroft 				xs->error = XS_SENSE;
    790   1.1   mycroft 				break;
    791   1.1   mycroft 			case SCSI_BUSY:
    792   1.1   mycroft 				xs->error = XS_BUSY;
    793   1.1   mycroft 				break;
    794   1.1   mycroft 			default:
    795   1.3  christos 				printf("%s: target_stat %x\n",
    796   1.1   mycroft 				    sc->sc_dev.dv_xname, ccb->target_stat);
    797   1.1   mycroft 				xs->error = XS_DRIVER_STUFFUP;
    798   1.1   mycroft 				break;
    799   1.1   mycroft 			}
    800   1.1   mycroft 		} else
    801   1.1   mycroft 			xs->resid = 0;
    802   1.1   mycroft 	}
    803   1.1   mycroft 	bha_free_ccb(sc, ccb);
    804  1.31   thorpej 	xs->xs_status |= XS_STS_DONE;
    805  1.14    bouyer 	scsipi_done(xs);
    806  1.17   thorpej 
    807  1.17   thorpej 	/*
    808  1.17   thorpej 	 * If there are queue entries in the software queue, try to
    809  1.17   thorpej 	 * run the first one.  We should be more or less guaranteed
    810  1.17   thorpej 	 * to succeed, since we just freed a CCB.
    811  1.17   thorpej 	 *
    812  1.17   thorpej 	 * NOTE: bha_scsi_cmd() relies on our calling it with
    813  1.17   thorpej 	 * the first entry in the queue.
    814  1.17   thorpej 	 */
    815  1.29   thorpej 	if ((xs = TAILQ_FIRST(&sc->sc_queue)) != NULL)
    816  1.17   thorpej 		(void) bha_scsi_cmd(xs);
    817   1.1   mycroft }
    818   1.1   mycroft 
    819   1.1   mycroft /*
    820  1.38    simonb  * Find the board and find it's irq/drq
    821  1.31   thorpej  */
    822  1.31   thorpej int
    823  1.38    simonb bha_find(iot, ioh, sc)
    824  1.38    simonb 	bus_space_tag_t iot;
    825  1.38    simonb 	bus_space_handle_t ioh;
    826  1.38    simonb 	struct bha_probe_data *sc;
    827  1.31   thorpej {
    828  1.38    simonb 	int i, iswide;
    829  1.38    simonb 	u_char sts;
    830  1.38    simonb 	struct bha_extended_inquire inquire;
    831  1.38    simonb 	struct bha_config config;
    832  1.38    simonb 	int irq, drq;
    833  1.31   thorpej 
    834  1.38    simonb 	/* Check something is at the ports we need to access */
    835  1.38    simonb 	sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    836  1.38    simonb 	if (sts == 0xFF)
    837  1.38    simonb 		return (0);
    838  1.31   thorpej 
    839  1.38    simonb 	/*
    840  1.38    simonb 	 * Reset board, If it doesn't respond, assume
    841  1.38    simonb 	 * that it's not there.. good for the probe
    842  1.38    simonb 	 */
    843  1.31   thorpej 
    844  1.38    simonb 	bus_space_write_1(iot, ioh, BHA_CTRL_PORT,
    845  1.38    simonb 	    BHA_CTRL_HRST | BHA_CTRL_SRST);
    846  1.31   thorpej 
    847  1.38    simonb 	delay(100);
    848  1.38    simonb 	for (i = BHA_RESET_TIMEOUT; i; i--) {
    849  1.38    simonb 		sts = bus_space_read_1(iot, ioh, BHA_STAT_PORT);
    850  1.38    simonb 		if (sts == (BHA_STAT_IDLE | BHA_STAT_INIT))
    851  1.38    simonb 			break;
    852  1.38    simonb 		delay(1000);
    853  1.38    simonb 	}
    854  1.38    simonb 	if (!i) {
    855  1.38    simonb #ifdef BHADEBUG
    856  1.38    simonb 		if (bha_debug)
    857  1.38    simonb 			printf("bha_find: No answer from buslogic board\n");
    858  1.38    simonb #endif /* BHADEBUG */
    859  1.38    simonb 		return (0);
    860  1.38    simonb 	}
    861  1.31   thorpej 
    862  1.31   thorpej 	/*
    863  1.38    simonb 	 * The BusLogic cards implement an Adaptec 1542 (aha)-compatible
    864  1.38    simonb 	 * interface. The native bha interface is not compatible with
    865  1.38    simonb 	 * an aha. 1542. We need to ensure that we never match an
    866  1.38    simonb 	 * Adaptec 1542. We must also avoid sending Adaptec-compatible
    867  1.38    simonb 	 * commands to a real bha, lest it go into 1542 emulation mode.
    868  1.38    simonb 	 * (On an indirect bus like ISA, we should always probe for BusLogic
    869  1.38    simonb 	 * interfaces  before Adaptec interfaces).
    870  1.31   thorpej 	 */
    871  1.31   thorpej 
    872  1.31   thorpej 	/*
    873  1.38    simonb 	 * Make sure we don't match an AHA-1542A or AHA-1542B, by checking
    874  1.38    simonb 	 * for an extended-geometry register.  The 1542[AB] don't have one.
    875  1.31   thorpej 	 */
    876  1.38    simonb 	sts = bus_space_read_1(iot, ioh, BHA_EXTGEOM_PORT);
    877  1.38    simonb 	if (sts == 0xFF)
    878  1.38    simonb 		return (0);
    879  1.31   thorpej 
    880  1.38    simonb 	/*
    881  1.38    simonb 	 * Check that we actually know how to use this board.
    882  1.38    simonb 	 */
    883  1.38    simonb 	delay(1000);
    884  1.38    simonb 	inquire.cmd.opcode = BHA_INQUIRE_EXTENDED;
    885  1.38    simonb 	inquire.cmd.len = sizeof(inquire.reply);
    886  1.38    simonb 	i = bha_cmd(iot, ioh, (struct bha_softc *)0,
    887  1.38    simonb 	    sizeof(inquire.cmd), (u_char *)&inquire.cmd,
    888  1.38    simonb 	    sizeof(inquire.reply), (u_char *)&inquire.reply);
    889  1.31   thorpej 
    890  1.38    simonb 	/*
    891  1.38    simonb 	 * Some 1542Cs (CP, perhaps not CF, may depend on firmware rev)
    892  1.38    simonb 	 * have the extended-geometry register and also respond to
    893  1.38    simonb 	 * BHA_INQUIRE_EXTENDED.  Make sure we never  match such cards,
    894  1.38    simonb 	 * by checking the size of the reply is what a BusLogic card returns.
    895  1.38    simonb 	 */
    896  1.38    simonb 	if (i) {
    897  1.38    simonb #ifdef BHADEBUG
    898  1.38    simonb 		printf("bha_find: board returned %d instead of %d to %s\n",
    899  1.38    simonb 		       i, sizeof(inquire.reply), "INQUIRE_EXTENDED");
    900  1.38    simonb #endif
    901  1.38    simonb 		return (0);
    902  1.38    simonb 	}
    903  1.31   thorpej 
    904  1.38    simonb 	/* OK, we know we've found a buslogic adaptor. */
    905   1.1   mycroft 
    906  1.38    simonb 	switch (inquire.reply.bus_type) {
    907  1.38    simonb 	case BHA_BUS_TYPE_24BIT:
    908  1.38    simonb 	case BHA_BUS_TYPE_32BIT:
    909  1.31   thorpej 		break;
    910  1.38    simonb 	case BHA_BUS_TYPE_MCA:
    911  1.38    simonb 		/* We don't grok MicroChannel (yet). */
    912  1.38    simonb 		return (0);
    913  1.31   thorpej 	default:
    914  1.38    simonb 		printf("bha_find: illegal bus type %c\n",
    915  1.38    simonb 		    inquire.reply.bus_type);
    916  1.38    simonb 		return (0);
    917   1.1   mycroft 	}
    918   1.1   mycroft 
    919  1.38    simonb 	/* Note if we have a wide bus. */
    920  1.38    simonb 	iswide = inquire.reply.scsi_flags & BHA_SCSI_WIDE;
    921  1.38    simonb 
    922   1.1   mycroft 	/*
    923  1.38    simonb 	 * Assume we have a board at this stage setup dma channel from
    924  1.38    simonb 	 * jumpers and save int level
    925   1.5  jonathan 	 */
    926  1.38    simonb 	delay(1000);
    927  1.38    simonb 	config.cmd.opcode = BHA_INQUIRE_CONFIG;
    928  1.38    simonb 	bha_cmd(iot, ioh, (struct bha_softc *)0,
    929  1.38    simonb 	    sizeof(config.cmd), (u_char *)&config.cmd,
    930  1.38    simonb 	    sizeof(config.reply), (u_char *)&config.reply);
    931  1.38    simonb 	switch (config.reply.chan) {
    932  1.38    simonb 	case EISADMA:
    933  1.38    simonb 		drq = -1;
    934  1.38    simonb 		break;
    935  1.38    simonb 	case CHAN0:
    936  1.38    simonb 		drq = 0;
    937  1.38    simonb 		break;
    938  1.38    simonb 	case CHAN5:
    939   1.1   mycroft 		drq = 5;
    940   1.1   mycroft 		break;
    941   1.1   mycroft 	case CHAN6:
    942   1.1   mycroft 		drq = 6;
    943   1.1   mycroft 		break;
    944   1.1   mycroft 	case CHAN7:
    945   1.1   mycroft 		drq = 7;
    946   1.1   mycroft 		break;
    947   1.1   mycroft 	default:
    948   1.4   thorpej 		printf("bha_find: illegal drq setting %x\n",
    949   1.4   thorpej 		    config.reply.chan);
    950   1.1   mycroft 		return (0);
    951   1.1   mycroft 	}
    952   1.1   mycroft 
    953   1.1   mycroft 	switch (config.reply.intr) {
    954   1.1   mycroft 	case INT9:
    955   1.1   mycroft 		irq = 9;
    956   1.1   mycroft 		break;
    957   1.1   mycroft 	case INT10:
    958   1.1   mycroft 		irq = 10;
    959   1.1   mycroft 		break;
    960   1.1   mycroft 	case INT11:
    961   1.1   mycroft 		irq = 11;
    962   1.1   mycroft 		break;
    963   1.1   mycroft 	case INT12:
    964   1.1   mycroft 		irq = 12;
    965   1.1   mycroft 		break;
    966   1.1   mycroft 	case INT14:
    967   1.1   mycroft 		irq = 14;
    968   1.1   mycroft 		break;
    969   1.1   mycroft 	case INT15:
    970   1.1   mycroft 		irq = 15;
    971   1.1   mycroft 		break;
    972   1.1   mycroft 	default:
    973   1.4   thorpej 		printf("bha_find: illegal irq setting %x\n",
    974   1.4   thorpej 		    config.reply.intr);
    975   1.1   mycroft 		return (0);
    976  1.31   thorpej 	}
    977  1.31   thorpej 
    978  1.31   thorpej 	/* if we want to fill in softc, do so now */
    979  1.31   thorpej 	if (sc != NULL) {
    980  1.31   thorpej 		sc->sc_irq = irq;
    981  1.31   thorpej 		sc->sc_drq = drq;
    982  1.38    simonb 		sc->sc_scsi_dev = config.reply.scsi_dev;
    983  1.38    simonb 		sc->sc_iswide = iswide;
    984  1.31   thorpej 	}
    985  1.31   thorpej 
    986  1.31   thorpej 	return (1);
    987  1.31   thorpej }
    988  1.31   thorpej 
    989  1.38    simonb 
    990  1.31   thorpej /*
    991  1.38    simonb  * Disable the ISA-compatiblity ioports on  PCI bha devices,
    992  1.38    simonb  * to ensure they're not autoconfigured a second time as an ISA bha.
    993  1.31   thorpej  */
    994  1.31   thorpej int
    995  1.31   thorpej bha_disable_isacompat(sc)
    996  1.31   thorpej 	struct bha_softc *sc;
    997  1.31   thorpej {
    998  1.31   thorpej 	struct bha_isadisable isa_disable;
    999  1.31   thorpej 
   1000  1.31   thorpej 	isa_disable.cmd.opcode = BHA_MODIFY_IOPORT;
   1001  1.31   thorpej 	isa_disable.cmd.modifier = BHA_IOMODIFY_DISABLE1;
   1002  1.31   thorpej 	bha_cmd(sc->sc_iot, sc->sc_ioh, sc,
   1003  1.31   thorpej 	    sizeof(isa_disable.cmd), (u_char*)&isa_disable.cmd,
   1004  1.31   thorpej 	    0, (u_char *)0);
   1005  1.31   thorpej 	return (0);
   1006  1.31   thorpej }
   1007  1.31   thorpej 
   1008  1.38    simonb 
   1009  1.31   thorpej /*
   1010  1.38    simonb  * Start the board, ready for normal operation
   1011  1.31   thorpej  */
   1012  1.31   thorpej int
   1013  1.38    simonb bha_init(sc)
   1014  1.31   thorpej 	struct bha_softc *sc;
   1015  1.31   thorpej {
   1016  1.31   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1017  1.31   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1018  1.38    simonb 	bus_dma_segment_t seg;
   1019  1.31   thorpej 	struct bha_devices devices;
   1020  1.31   thorpej 	struct bha_setup setup;
   1021  1.38    simonb 	struct bha_mailbox mailbox;
   1022  1.38    simonb 	struct bha_period period;
   1023  1.38    simonb 	int error, i, j, initial_ccbs, rlen, rseg;
   1024  1.38    simonb 
   1025  1.38    simonb 	/* Enable round-robin scheme - appeared at firmware rev. 3.31. */
   1026  1.38    simonb 	if (strcmp(sc->sc_firmware, "3.31") >= 0) {
   1027  1.38    simonb 		struct bha_toggle toggle;
   1028  1.38    simonb 
   1029  1.38    simonb 		toggle.cmd.opcode = BHA_ROUND_ROBIN;
   1030  1.38    simonb 		toggle.cmd.enable = 1;
   1031  1.38    simonb 		bha_cmd(iot, ioh, sc,
   1032  1.38    simonb 		    sizeof(toggle.cmd), (u_char *)&toggle.cmd,
   1033  1.38    simonb 		    0, (u_char *)0);
   1034  1.38    simonb 	}
   1035  1.31   thorpej 
   1036  1.31   thorpej 	/*
   1037  1.38    simonb 	 * Inquire installed devices (to force synchronous negotiation).
   1038  1.31   thorpej 	 */
   1039  1.31   thorpej 
   1040  1.31   thorpej 	/*
   1041  1.38    simonb 	 * Poll targets 0 - 7.
   1042  1.31   thorpej 	 */
   1043  1.38    simonb 	devices.cmd.opcode = BHA_INQUIRE_DEVICES;
   1044  1.31   thorpej 	bha_cmd(iot, ioh, sc,
   1045  1.38    simonb 	    sizeof(devices.cmd), (u_char *)&devices.cmd,
   1046  1.38    simonb 	    sizeof(devices.reply), (u_char *)&devices.reply);
   1047  1.31   thorpej 
   1048  1.38    simonb 	/* Count installed units. */
   1049  1.38    simonb 	initial_ccbs = 0;
   1050  1.38    simonb 	for (i = 0; i < 8; i++) {
   1051  1.38    simonb 		for (j = 0; j < 8; j++) {
   1052  1.38    simonb 			if (((devices.reply.lun_map[i] >> j) & 1) == 1)
   1053  1.38    simonb 				initial_ccbs++;
   1054  1.38    simonb 		}
   1055  1.38    simonb 	}
   1056  1.31   thorpej 
   1057  1.31   thorpej 	/*
   1058  1.38    simonb 	 * Poll targets 8 - 15 if we have a wide bus.
   1059  1.38    simonb 	 */
   1060  1.38    simonb 	if (ISWIDE(sc)) {
   1061  1.38    simonb 		devices.cmd.opcode = BHA_INQUIRE_DEVICES_2;
   1062  1.38    simonb 		bha_cmd(iot, ioh, sc,
   1063  1.38    simonb 		    sizeof(devices.cmd), (u_char *)&devices.cmd,
   1064  1.38    simonb 		    sizeof(devices.reply), (u_char *)&devices.reply);
   1065  1.38    simonb 
   1066  1.38    simonb 		for (i = 0; i < 8; i++) {
   1067  1.38    simonb 			for (j = 0; j < 8; j++) {
   1068  1.38    simonb 				if (((devices.reply.lun_map[i] >> j) & 1) == 1)
   1069  1.38    simonb 					initial_ccbs++;
   1070  1.38    simonb 			}
   1071  1.38    simonb 		}
   1072  1.38    simonb 	}
   1073  1.38    simonb 
   1074  1.38    simonb 	initial_ccbs *= sc->sc_link.openings;
   1075  1.38    simonb 	if (initial_ccbs > BHA_CCB_MAX)
   1076  1.38    simonb 		initial_ccbs = BHA_CCB_MAX;
   1077  1.38    simonb 	if (initial_ccbs == 0)	/* yes, this can happen */
   1078  1.38    simonb 		initial_ccbs = sc->sc_link.openings;
   1079  1.38    simonb 
   1080  1.38    simonb 	/* Obtain setup information from. */
   1081  1.38    simonb 	rlen = sizeof(setup.reply) +
   1082  1.38    simonb 	    (ISWIDE(sc) ? sizeof(setup.reply_w) : 0);
   1083  1.38    simonb 	setup.cmd.opcode = BHA_INQUIRE_SETUP;
   1084  1.38    simonb 	setup.cmd.len = rlen;
   1085  1.38    simonb 	bha_cmd(iot, ioh, sc,
   1086  1.38    simonb 	    sizeof(setup.cmd), (u_char *)&setup.cmd,
   1087  1.38    simonb 	    rlen, (u_char *)&setup.reply);
   1088  1.38    simonb 
   1089  1.38    simonb 	printf("%s: %s, %s\n",
   1090  1.38    simonb 	    sc->sc_dev.dv_xname,
   1091  1.38    simonb 	    setup.reply.sync_neg ? "sync" : "async",
   1092  1.38    simonb 	    setup.reply.parity ? "parity" : "no parity");
   1093  1.38    simonb 
   1094  1.38    simonb 	for (i = 0; i < 8; i++)
   1095  1.38    simonb 		period.reply.period[i] = setup.reply.sync[i].period * 5 + 20;
   1096  1.38    simonb 	if (ISWIDE(sc)) {
   1097  1.38    simonb 		for (i = 0; i < 8; i++)
   1098  1.38    simonb 			period.reply_w.period[i] =
   1099  1.38    simonb 			    setup.reply_w.sync[i].period * 5 + 20;
   1100  1.38    simonb 	}
   1101  1.38    simonb 
   1102  1.38    simonb 	if (sc->sc_firmware[0] >= '3') {
   1103  1.38    simonb 		rlen = sizeof(period.reply) +
   1104  1.38    simonb 		    (ISWIDE(sc) ? sizeof(period.reply_w) : 0);
   1105  1.38    simonb 		period.cmd.opcode = BHA_INQUIRE_PERIOD;
   1106  1.38    simonb 		period.cmd.len = rlen;
   1107  1.38    simonb 		bha_cmd(iot, ioh, sc,
   1108  1.38    simonb 		    sizeof(period.cmd), (u_char *)&period.cmd,
   1109  1.38    simonb 		    rlen, (u_char *)&period.reply);
   1110  1.38    simonb 	}
   1111  1.38    simonb 
   1112  1.38    simonb 	for (i = 0; i < 8; i++) {
   1113  1.38    simonb 		if (!setup.reply.sync[i].valid ||
   1114  1.38    simonb 		    (!setup.reply.sync[i].offset &&
   1115  1.38    simonb 		     !setup.reply.sync[i].period))
   1116  1.38    simonb 			continue;
   1117  1.38    simonb 		printf("%s targ %d: sync, offset %d, period %dnsec\n",
   1118  1.38    simonb 		    sc->sc_dev.dv_xname, i,
   1119  1.38    simonb 		    setup.reply.sync[i].offset, period.reply.period[i] * 10);
   1120  1.38    simonb 	}
   1121  1.38    simonb 	if (ISWIDE(sc)) {
   1122  1.38    simonb 		for (i = 0; i < 8; i++) {
   1123  1.38    simonb 			if (!setup.reply_w.sync[i].valid ||
   1124  1.38    simonb 			    (!setup.reply_w.sync[i].offset &&
   1125  1.38    simonb 			     !setup.reply_w.sync[i].period))
   1126  1.38    simonb 				continue;
   1127  1.38    simonb 			printf("%s targ %d: sync, offset %d, period %dnsec\n",
   1128  1.38    simonb 			    sc->sc_dev.dv_xname, i + 8,
   1129  1.38    simonb 			    setup.reply_w.sync[i].offset,
   1130  1.38    simonb 			    period.reply_w.period[i] * 10);
   1131  1.38    simonb 		}
   1132  1.38    simonb 	}
   1133  1.38    simonb 
   1134  1.38    simonb 	/*
   1135  1.38    simonb 	 * Allocate the mailbox and control blocks.
   1136  1.38    simonb 	 */
   1137  1.38    simonb 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct bha_control),
   1138  1.38    simonb 	    NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
   1139  1.38    simonb 		printf("%s: unable to allocate control structures, "
   1140  1.38    simonb 		    "error = %d\n", sc->sc_dev.dv_xname, error);
   1141  1.38    simonb 		return (error);
   1142  1.38    simonb 	}
   1143  1.38    simonb 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
   1144  1.38    simonb 	    sizeof(struct bha_control), (caddr_t *)&sc->sc_control,
   1145  1.38    simonb 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
   1146  1.38    simonb 		printf("%s: unable to map control structures, error = %d\n",
   1147  1.38    simonb 		    sc->sc_dev.dv_xname, error);
   1148  1.38    simonb 		return (error);
   1149  1.38    simonb 	}
   1150  1.38    simonb 
   1151  1.38    simonb 	/*
   1152  1.38    simonb 	 * Create and load the DMA map used for the mailbox and
   1153  1.38    simonb 	 * control blocks.
   1154  1.38    simonb 	 */
   1155  1.38    simonb 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct bha_control),
   1156  1.38    simonb 	    1, sizeof(struct bha_control), 0, BUS_DMA_NOWAIT | sc->sc_dmaflags,
   1157  1.38    simonb 	    &sc->sc_dmamap_control)) != 0) {
   1158  1.38    simonb 		printf("%s: unable to create control DMA map, error = %d\n",
   1159  1.38    simonb 		    sc->sc_dev.dv_xname, error);
   1160  1.38    simonb 		return (error);
   1161  1.38    simonb 	}
   1162  1.38    simonb 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap_control,
   1163  1.38    simonb 	    sc->sc_control, sizeof(struct bha_control), NULL,
   1164  1.38    simonb 	    BUS_DMA_NOWAIT)) != 0) {
   1165  1.38    simonb 		printf("%s: unable to load control DMA map, error = %d\n",
   1166  1.38    simonb 		    sc->sc_dev.dv_xname, error);
   1167  1.38    simonb 		return (error);
   1168  1.38    simonb 	}
   1169  1.38    simonb 
   1170  1.38    simonb 	/*
   1171  1.38    simonb 	 * Initialize the control blocks.
   1172  1.38    simonb 	 */
   1173  1.38    simonb 	i = bha_create_ccbs(sc, sc->sc_control->bc_ccbs, initial_ccbs);
   1174  1.38    simonb 	if (i == 0) {
   1175  1.38    simonb 		printf("%s: unable to create control blocks\n",
   1176  1.38    simonb 		    sc->sc_dev.dv_xname);
   1177  1.38    simonb 		return (ENOMEM);
   1178  1.38    simonb 	} else if (i != initial_ccbs) {
   1179  1.38    simonb 		printf("%s: WARNING: only %d of %d control blocks created\n",
   1180  1.38    simonb 		    sc->sc_dev.dv_xname, i, initial_ccbs);
   1181  1.38    simonb 	}
   1182  1.38    simonb 
   1183  1.38    simonb 	/*
   1184  1.38    simonb 	 * Set up initial mail box for round-robin operation.
   1185  1.38    simonb 	 */
   1186  1.38    simonb 	for (i = 0; i < BHA_MBX_SIZE; i++) {
   1187  1.38    simonb 		wmbx->mbo[i].cmd = BHA_MBO_FREE;
   1188  1.38    simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1189  1.38    simonb 		    BHA_MBO_OFF(&wmbx->mbo[i]), sizeof(struct bha_mbx_out),
   1190  1.38    simonb 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1191  1.38    simonb 		wmbx->mbi[i].stat = BHA_MBI_FREE;
   1192  1.38    simonb 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1193  1.38    simonb 		    BHA_MBI_OFF(&wmbx->mbi[i]), sizeof(struct bha_mbx_in),
   1194  1.38    simonb 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1195  1.38    simonb 	}
   1196  1.38    simonb 	wmbx->cmbo = wmbx->tmbo = &wmbx->mbo[0];
   1197  1.38    simonb 	wmbx->tmbi = &wmbx->mbi[0];
   1198  1.38    simonb 	sc->sc_mbofull = 0;
   1199  1.38    simonb 
   1200  1.38    simonb 	/* Initialize mail box. */
   1201  1.38    simonb 	mailbox.cmd.opcode = BHA_MBX_INIT_EXTENDED;
   1202  1.38    simonb 	mailbox.cmd.nmbx = BHA_MBX_SIZE;
   1203  1.38    simonb 	ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1204  1.38    simonb 	    offsetof(struct bha_control, bc_mbx), mailbox.cmd.addr);
   1205  1.38    simonb 	bha_cmd(iot, ioh, sc,
   1206  1.38    simonb 	    sizeof(mailbox.cmd), (u_char *)&mailbox.cmd,
   1207  1.38    simonb 	    0, (u_char *)0);
   1208  1.38    simonb 	return (0);
   1209  1.38    simonb }
   1210  1.38    simonb 
   1211  1.38    simonb void
   1212  1.38    simonb bha_inquire_setup_information(sc)
   1213  1.38    simonb 	struct bha_softc *sc;
   1214  1.38    simonb {
   1215  1.38    simonb 	bus_space_tag_t iot = sc->sc_iot;
   1216  1.38    simonb 	bus_space_handle_t ioh = sc->sc_ioh;
   1217  1.38    simonb 	struct bha_model model;
   1218  1.38    simonb 	struct bha_revision revision;
   1219  1.38    simonb 	struct bha_digit digit;
   1220  1.38    simonb 	char *p;
   1221  1.38    simonb 
   1222  1.38    simonb 	/*
   1223  1.38    simonb 	 * Get the firmware revision.
   1224  1.31   thorpej 	 */
   1225  1.31   thorpej 	p = sc->sc_firmware;
   1226  1.31   thorpej 	revision.cmd.opcode = BHA_INQUIRE_REVISION;
   1227  1.31   thorpej 	bha_cmd(iot, ioh, sc,
   1228  1.31   thorpej 	    sizeof(revision.cmd), (u_char *)&revision.cmd,
   1229  1.31   thorpej 	    sizeof(revision.reply), (u_char *)&revision.reply);
   1230  1.31   thorpej 	*p++ = revision.reply.firm_revision;
   1231  1.31   thorpej 	*p++ = '.';
   1232  1.31   thorpej 	*p++ = revision.reply.firm_version;
   1233  1.31   thorpej 	digit.cmd.opcode = BHA_INQUIRE_REVISION_3;
   1234  1.31   thorpej 	bha_cmd(iot, ioh, sc,
   1235  1.31   thorpej 	    sizeof(digit.cmd), (u_char *)&digit.cmd,
   1236  1.31   thorpej 	    sizeof(digit.reply), (u_char *)&digit.reply);
   1237  1.31   thorpej 	*p++ = digit.reply.digit;
   1238  1.31   thorpej 	if (revision.reply.firm_revision >= '3' ||
   1239  1.31   thorpej 	    (revision.reply.firm_revision == '3' &&
   1240  1.31   thorpej 	     revision.reply.firm_version >= '3')) {
   1241  1.31   thorpej 		digit.cmd.opcode = BHA_INQUIRE_REVISION_4;
   1242  1.31   thorpej 		bha_cmd(iot, ioh, sc,
   1243  1.31   thorpej 		    sizeof(digit.cmd), (u_char *)&digit.cmd,
   1244  1.31   thorpej 		    sizeof(digit.reply), (u_char *)&digit.reply);
   1245  1.31   thorpej 		*p++ = digit.reply.digit;
   1246  1.31   thorpej 	}
   1247  1.31   thorpej 	while (p > sc->sc_firmware && (p[-1] == ' ' || p[-1] == '\0'))
   1248  1.31   thorpej 		p--;
   1249  1.31   thorpej 	*p = '\0';
   1250  1.31   thorpej 
   1251  1.31   thorpej 	/*
   1252  1.31   thorpej 	 * Get the model number.
   1253  1.31   thorpej 	 */
   1254  1.38    simonb 	if (revision.reply.firm_revision >= '3') {
   1255  1.31   thorpej 		p = sc->sc_model;
   1256  1.31   thorpej 		model.cmd.opcode = BHA_INQUIRE_MODEL;
   1257  1.31   thorpej 		model.cmd.len = sizeof(model.reply);
   1258  1.31   thorpej 		bha_cmd(iot, ioh, sc,
   1259  1.31   thorpej 		    sizeof(model.cmd), (u_char *)&model.cmd,
   1260  1.31   thorpej 		    sizeof(model.reply), (u_char *)&model.reply);
   1261  1.31   thorpej 		*p++ = model.reply.id[0];
   1262  1.31   thorpej 		*p++ = model.reply.id[1];
   1263  1.31   thorpej 		*p++ = model.reply.id[2];
   1264  1.31   thorpej 		*p++ = model.reply.id[3];
   1265  1.31   thorpej 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1266  1.31   thorpej 			p--;
   1267  1.31   thorpej 		*p++ = model.reply.version[0];
   1268  1.31   thorpej 		*p++ = model.reply.version[1];
   1269  1.31   thorpej 		while (p > sc->sc_model && (p[-1] == ' ' || p[-1] == '\0'))
   1270  1.31   thorpej 			p--;
   1271  1.31   thorpej 		*p = '\0';
   1272  1.38    simonb 	} else
   1273  1.38    simonb 		strcpy(sc->sc_model, "542B");
   1274  1.38    simonb }
   1275  1.31   thorpej 
   1276  1.38    simonb void
   1277  1.38    simonb bhaminphys(bp)
   1278  1.38    simonb 	struct buf *bp;
   1279  1.38    simonb {
   1280  1.31   thorpej 
   1281  1.38    simonb 	if (bp->b_bcount > BHA_MAXXFER)
   1282  1.38    simonb 		bp->b_bcount = BHA_MAXXFER;
   1283  1.38    simonb 	minphys(bp);
   1284  1.31   thorpej }
   1285  1.31   thorpej 
   1286  1.31   thorpej /*
   1287  1.38    simonb  * start a scsi operation given the command and the data address.  Also needs
   1288  1.38    simonb  * the unit, target and lu.
   1289  1.31   thorpej  */
   1290  1.31   thorpej int
   1291  1.38    simonb bha_scsi_cmd(xs)
   1292  1.38    simonb 	struct scsipi_xfer *xs;
   1293  1.31   thorpej {
   1294  1.38    simonb 	struct scsipi_link *sc_link = xs->sc_link;
   1295  1.38    simonb 	struct bha_softc *sc = sc_link->adapter_softc;
   1296  1.38    simonb 	bus_dma_tag_t dmat = sc->sc_dmat;
   1297  1.38    simonb 	struct bha_ccb *ccb;
   1298  1.38    simonb 	int error, seg, flags, s;
   1299  1.38    simonb 	int fromqueue = 0, dontqueue = 0, nowait = 0;
   1300  1.38    simonb 
   1301  1.38    simonb 	SC_DEBUG(sc_link, SDEV_DB2, ("bha_scsi_cmd\n"));
   1302  1.38    simonb 
   1303  1.38    simonb 	s = splbio();		/* protect the queue */
   1304  1.31   thorpej 
   1305  1.31   thorpej 	/*
   1306  1.38    simonb 	 * If we're running the queue from bha_done(), we've been
   1307  1.38    simonb 	 * called with the first queue entry as our argument.
   1308  1.31   thorpej 	 */
   1309  1.38    simonb 	if (xs == TAILQ_FIRST(&sc->sc_queue)) {
   1310  1.38    simonb 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
   1311  1.38    simonb 		fromqueue = 1;
   1312  1.38    simonb 		nowait = 1;
   1313  1.38    simonb 		goto get_ccb;
   1314  1.31   thorpej 	}
   1315  1.31   thorpej 
   1316  1.38    simonb 	/* Polled requests can't be queued for later. */
   1317  1.38    simonb 	dontqueue = xs->xs_control & XS_CTL_POLL;
   1318  1.31   thorpej 
   1319  1.31   thorpej 	/*
   1320  1.38    simonb 	 * If there are jobs in the queue, run them first.
   1321  1.31   thorpej 	 */
   1322  1.38    simonb 	if (TAILQ_FIRST(&sc->sc_queue) != NULL) {
   1323  1.38    simonb 		/*
   1324  1.38    simonb 		 * If we can't queue, we have to abort, since
   1325  1.38    simonb 		 * we have to preserve order.
   1326  1.38    simonb 		 */
   1327  1.38    simonb 		if (dontqueue) {
   1328  1.38    simonb 			splx(s);
   1329  1.38    simonb 			xs->error = XS_DRIVER_STUFFUP;
   1330  1.38    simonb 			return (TRY_AGAIN_LATER);
   1331  1.38    simonb 		}
   1332  1.38    simonb 
   1333  1.38    simonb 		/*
   1334  1.38    simonb 		 * Swap with the first queue entry.
   1335  1.38    simonb 		 */
   1336  1.38    simonb 		TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
   1337  1.38    simonb 		xs = TAILQ_FIRST(&sc->sc_queue);
   1338  1.38    simonb 		TAILQ_REMOVE(&sc->sc_queue, xs, adapter_q);
   1339  1.38    simonb 		fromqueue = 1;
   1340  1.31   thorpej 	}
   1341  1.31   thorpej 
   1342  1.38    simonb  get_ccb:
   1343  1.31   thorpej 	/*
   1344  1.38    simonb 	 * get a ccb to use. If the transfer
   1345  1.38    simonb 	 * is from a buf (possibly from interrupt time)
   1346  1.38    simonb 	 * then we can't allow it to sleep
   1347  1.31   thorpej 	 */
   1348  1.38    simonb 	flags = xs->xs_control;
   1349  1.38    simonb 	if (nowait)
   1350  1.38    simonb 		flags |= XS_CTL_NOSLEEP;
   1351  1.31   thorpej 
   1352  1.38    simonb 	if ((ccb = bha_get_ccb(sc, flags)) == NULL) {
   1353  1.38    simonb 		/*
   1354  1.38    simonb 		 * If we can't queue, we lose.
   1355  1.38    simonb 		 */
   1356  1.38    simonb 		if (dontqueue) {
   1357  1.38    simonb 			splx(s);
   1358  1.38    simonb 			xs->error = XS_DRIVER_STUFFUP;
   1359  1.38    simonb 			return (TRY_AGAIN_LATER);
   1360  1.31   thorpej 		}
   1361  1.31   thorpej 
   1362  1.31   thorpej 		/*
   1363  1.38    simonb 		 * Stuff ourselves into the queue, in front
   1364  1.38    simonb 		 * if we came off in the first place.
   1365  1.31   thorpej 		 */
   1366  1.38    simonb 		if (fromqueue)
   1367  1.38    simonb 			TAILQ_INSERT_HEAD(&sc->sc_queue, xs, adapter_q);
   1368  1.31   thorpej 		else
   1369  1.38    simonb 			TAILQ_INSERT_TAIL(&sc->sc_queue, xs, adapter_q);
   1370  1.38    simonb 		splx(s);
   1371  1.38    simonb 		return (SUCCESSFULLY_QUEUED);
   1372  1.31   thorpej 	}
   1373  1.31   thorpej 
   1374  1.38    simonb 	splx(s);		/* done playing with the queue */
   1375   1.1   mycroft 
   1376  1.38    simonb 	ccb->xs = xs;
   1377  1.38    simonb 	ccb->timeout = xs->timeout;
   1378  1.17   thorpej 
   1379  1.17   thorpej 	/*
   1380  1.38    simonb 	 * Put all the arguments for the xfer in the ccb
   1381  1.17   thorpej 	 */
   1382  1.38    simonb 	if (flags & XS_CTL_RESET) {
   1383  1.38    simonb 		ccb->opcode = BHA_RESET_CCB;
   1384  1.38    simonb 		ccb->scsi_cmd_length = 0;
   1385  1.38    simonb 	} else {
   1386  1.38    simonb 		/* can't use S/G if zero length */
   1387  1.38    simonb 		ccb->opcode = (xs->datalen ? BHA_INIT_SCAT_GATH_CCB
   1388  1.38    simonb 					   : BHA_INITIATOR_CCB);
   1389  1.38    simonb 		bcopy(xs->cmd, &ccb->scsi_cmd,
   1390  1.38    simonb 		    ccb->scsi_cmd_length = xs->cmdlen);
   1391  1.38    simonb 	}
   1392  1.17   thorpej 
   1393  1.38    simonb 	if (xs->datalen) {
   1394  1.38    simonb 		/*
   1395  1.38    simonb 		 * Map the DMA transfer.
   1396  1.38    simonb 		 */
   1397  1.38    simonb #ifdef TFS
   1398  1.38    simonb 		if (flags & XS_CTL_DATA_UIO) {
   1399  1.38    simonb 			error = bus_dmamap_load_uio(dmat,
   1400  1.38    simonb 			    ccb->dmamap_xfer, (struct uio *)xs->data,
   1401  1.38    simonb 			    (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
   1402  1.38    simonb 			    BUS_DMA_WAITOK);
   1403  1.38    simonb 		} else
   1404  1.38    simonb #endif /* TFS */
   1405  1.38    simonb 		{
   1406  1.38    simonb 			error = bus_dmamap_load(dmat,
   1407  1.38    simonb 			    ccb->dmamap_xfer, xs->data, xs->datalen, NULL,
   1408  1.38    simonb 			    (flags & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT :
   1409  1.38    simonb 			    BUS_DMA_WAITOK);
   1410  1.38    simonb 		}
   1411  1.31   thorpej 
   1412  1.38    simonb 		if (error) {
   1413  1.38    simonb 			if (error == EFBIG) {
   1414  1.38    simonb 				printf("%s: bha_scsi_cmd, more than %d"
   1415  1.38    simonb 				    " dma segments\n",
   1416  1.38    simonb 				    sc->sc_dev.dv_xname, BHA_NSEG);
   1417  1.38    simonb 			} else {
   1418  1.38    simonb 				printf("%s: bha_scsi_cmd, error %d loading"
   1419  1.38    simonb 				    " dma map\n",
   1420  1.38    simonb 				    sc->sc_dev.dv_xname, error);
   1421  1.38    simonb 			}
   1422  1.38    simonb 			goto bad;
   1423  1.38    simonb 		}
   1424  1.17   thorpej 
   1425  1.38    simonb 		bus_dmamap_sync(dmat, ccb->dmamap_xfer, 0,
   1426  1.38    simonb 		    ccb->dmamap_xfer->dm_mapsize,
   1427  1.38    simonb 		    (flags & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
   1428  1.38    simonb 		    BUS_DMASYNC_PREWRITE);
   1429  1.17   thorpej 
   1430  1.38    simonb 		/*
   1431  1.38    simonb 		 * Load the hardware scatter/gather map with the
   1432  1.38    simonb 		 * contents of the DMA map.
   1433  1.38    simonb 		 */
   1434  1.38    simonb 		for (seg = 0; seg < ccb->dmamap_xfer->dm_nsegs; seg++) {
   1435  1.38    simonb 			ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_addr,
   1436  1.38    simonb 			    ccb->scat_gath[seg].seg_addr);
   1437  1.38    simonb 			ltophys(ccb->dmamap_xfer->dm_segs[seg].ds_len,
   1438  1.38    simonb 			    ccb->scat_gath[seg].seg_len);
   1439  1.38    simonb 		}
   1440  1.31   thorpej 
   1441  1.38    simonb 		ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1442  1.38    simonb 		    BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scat_gath),
   1443  1.38    simonb 		    ccb->data_addr);
   1444  1.38    simonb 		ltophys(ccb->dmamap_xfer->dm_nsegs *
   1445  1.38    simonb 		    sizeof(struct bha_scat_gath), ccb->data_length);
   1446  1.38    simonb 	} else {
   1447  1.38    simonb 		/*
   1448  1.38    simonb 		 * No data xfer, use non S/G values.
   1449  1.38    simonb 		 */
   1450  1.38    simonb 		ltophys(0, ccb->data_addr);
   1451  1.38    simonb 		ltophys(0, ccb->data_length);
   1452  1.31   thorpej 	}
   1453  1.17   thorpej 
   1454  1.38    simonb 	ccb->data_out = 0;
   1455  1.38    simonb 	ccb->data_in = 0;
   1456  1.38    simonb 	ccb->target = sc_link->scsipi_scsi.target;
   1457  1.38    simonb 	ccb->lun = sc_link->scsipi_scsi.lun;
   1458  1.38    simonb 	ltophys(sc->sc_dmamap_control->dm_segs[0].ds_addr +
   1459  1.38    simonb 	    BHA_CCB_OFF(ccb) + offsetof(struct bha_ccb, scsi_sense),
   1460  1.38    simonb 	    ccb->sense_ptr);
   1461  1.38    simonb 	ccb->req_sense_length = sizeof(ccb->scsi_sense);
   1462  1.38    simonb 	ccb->host_stat = 0x00;
   1463  1.38    simonb 	ccb->target_stat = 0x00;
   1464  1.38    simonb 	ccb->link_id = 0;
   1465  1.38    simonb 	ltophys(0, ccb->link_addr);
   1466  1.17   thorpej 
   1467  1.38    simonb 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap_control,
   1468  1.38    simonb 	    BHA_CCB_OFF(ccb), sizeof(struct bha_ccb),
   1469  1.38    simonb 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1470  1.17   thorpej 
   1471  1.38    simonb 	s = splbio();
   1472  1.38    simonb 	bha_queue_ccb(sc, ccb);
   1473  1.38    simonb 	splx(s);
   1474   1.1   mycroft 
   1475   1.1   mycroft 	/*
   1476  1.38    simonb 	 * Usually return SUCCESSFULLY QUEUED
   1477   1.1   mycroft 	 */
   1478  1.38    simonb 	SC_DEBUG(sc_link, SDEV_DB3, ("cmd_sent\n"));
   1479  1.38    simonb 	if ((flags & XS_CTL_POLL) == 0)
   1480  1.38    simonb 		return (SUCCESSFULLY_QUEUED);
   1481   1.1   mycroft 
   1482  1.31   thorpej 	/*
   1483  1.38    simonb 	 * If we can't use interrupts, poll on completion
   1484  1.31   thorpej 	 */
   1485  1.38    simonb 	if (bha_poll(sc, xs, ccb->timeout)) {
   1486  1.38    simonb 		bha_timeout(ccb);
   1487  1.38    simonb 		if (bha_poll(sc, xs, ccb->timeout))
   1488  1.38    simonb 			bha_timeout(ccb);
   1489   1.1   mycroft 	}
   1490  1.38    simonb 	return (COMPLETE);
   1491   1.1   mycroft 
   1492  1.38    simonb bad:
   1493  1.38    simonb 	xs->error = XS_DRIVER_STUFFUP;
   1494  1.38    simonb 	bha_free_ccb(sc, ccb);
   1495  1.38    simonb 	return (COMPLETE);
   1496  1.31   thorpej }
   1497   1.1   mycroft 
   1498  1.31   thorpej /*
   1499  1.38    simonb  * Poll a particular unit, looking for a particular xs
   1500  1.31   thorpej  */
   1501  1.31   thorpej int
   1502  1.38    simonb bha_poll(sc, xs, count)
   1503  1.31   thorpej 	struct bha_softc *sc;
   1504  1.38    simonb 	struct scsipi_xfer *xs;
   1505  1.38    simonb 	int count;
   1506  1.31   thorpej {
   1507  1.38    simonb 	bus_space_tag_t iot = sc->sc_iot;
   1508  1.38    simonb 	bus_space_handle_t ioh = sc->sc_ioh;
   1509   1.1   mycroft 
   1510  1.38    simonb 	/* timeouts are in msec, so we loop in 1000 usec cycles */
   1511  1.38    simonb 	while (count) {
   1512  1.38    simonb 		/*
   1513  1.38    simonb 		 * If we had interrupts enabled, would we
   1514  1.38    simonb 		 * have got an interrupt?
   1515  1.38    simonb 		 */
   1516  1.38    simonb 		if (bus_space_read_1(iot, ioh, BHA_INTR_PORT) &
   1517  1.38    simonb 		    BHA_INTR_ANYINTR)
   1518  1.38    simonb 			bha_intr(sc);
   1519  1.38    simonb 		if (xs->xs_status & XS_STS_DONE)
   1520  1.38    simonb 			return (0);
   1521  1.38    simonb 		delay(1000);	/* only happens in boot so ok */
   1522  1.38    simonb 		count--;
   1523  1.31   thorpej 	}
   1524  1.38    simonb 	return (1);
   1525   1.1   mycroft }
   1526   1.1   mycroft 
   1527  1.38    simonb void
   1528  1.38    simonb bha_timeout(arg)
   1529  1.38    simonb 	void *arg;
   1530   1.1   mycroft {
   1531  1.38    simonb 	struct bha_ccb *ccb = arg;
   1532  1.38    simonb 	struct scsipi_xfer *xs = ccb->xs;
   1533  1.38    simonb 	struct scsipi_link *sc_link = xs->sc_link;
   1534  1.38    simonb 	struct bha_softc *sc = sc_link->adapter_softc;
   1535  1.31   thorpej 	int s;
   1536  1.31   thorpej 
   1537  1.38    simonb 	scsi_print_addr(sc_link);
   1538  1.38    simonb 	printf("timed out");
   1539  1.38    simonb 
   1540  1.31   thorpej 	s = splbio();
   1541   1.1   mycroft 
   1542  1.38    simonb #ifdef BHADIAG
   1543  1.38    simonb 	/*
   1544  1.38    simonb 	 * If the ccb's mbx is not free, then the board has gone Far East?
   1545  1.38    simonb 	 */
   1546  1.38    simonb 	bha_collect_mbo(sc);
   1547  1.38    simonb 	if (ccb->flags & CCB_SENDING) {
   1548  1.38    simonb 		printf("%s: not taking commands!\n", sc->sc_dev.dv_xname);
   1549  1.38    simonb 		Debugger();
   1550   1.1   mycroft 	}
   1551  1.38    simonb #endif
   1552   1.1   mycroft 
   1553   1.1   mycroft 	/*
   1554  1.38    simonb 	 * If it has been through before, then
   1555  1.38    simonb 	 * a previous abort has failed, don't
   1556  1.38    simonb 	 * try abort again
   1557   1.1   mycroft 	 */
   1558  1.38    simonb 	if (ccb->flags & CCB_ABORT) {
   1559  1.38    simonb 		/* abort timed out */
   1560  1.38    simonb 		printf(" AGAIN\n");
   1561  1.38    simonb 		/* XXX Must reset! */
   1562  1.38    simonb 	} else {
   1563  1.38    simonb 		/* abort the operation that has timed out */
   1564  1.38    simonb 		printf("\n");
   1565  1.38    simonb 		ccb->xs->error = XS_TIMEOUT;
   1566  1.38    simonb 		ccb->timeout = BHA_ABORT_TIMEOUT;
   1567  1.38    simonb 		ccb->flags |= CCB_ABORT;
   1568  1.38    simonb 		bha_queue_ccb(sc, ccb);
   1569  1.38    simonb 	}
   1570   1.1   mycroft 
   1571   1.1   mycroft 	splx(s);
   1572   1.1   mycroft }
   1573