Home | History | Annotate | Line # | Download | only in pci
twe.c revision 1.20.4.2
      1  1.20.4.2  he /*	$NetBSD: twe.c,v 1.20.4.2 2001/10/25 18:03:02 he Exp $	*/
      2  1.20.4.2  he 
      3  1.20.4.2  he /*-
      4  1.20.4.2  he  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.20.4.2  he  * All rights reserved.
      6  1.20.4.2  he  *
      7  1.20.4.2  he  * This code is derived from software contributed to The NetBSD Foundation
      8  1.20.4.2  he  * by Andrew Doran.
      9  1.20.4.2  he  *
     10  1.20.4.2  he  * Redistribution and use in source and binary forms, with or without
     11  1.20.4.2  he  * modification, are permitted provided that the following conditions
     12  1.20.4.2  he  * are met:
     13  1.20.4.2  he  * 1. Redistributions of source code must retain the above copyright
     14  1.20.4.2  he  *    notice, this list of conditions and the following disclaimer.
     15  1.20.4.2  he  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.20.4.2  he  *    notice, this list of conditions and the following disclaimer in the
     17  1.20.4.2  he  *    documentation and/or other materials provided with the distribution.
     18  1.20.4.2  he  * 3. All advertising materials mentioning features or use of this software
     19  1.20.4.2  he  *    must display the following acknowledgement:
     20  1.20.4.2  he  *        This product includes software developed by the NetBSD
     21  1.20.4.2  he  *        Foundation, Inc. and its contributors.
     22  1.20.4.2  he  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.20.4.2  he  *    contributors may be used to endorse or promote products derived
     24  1.20.4.2  he  *    from this software without specific prior written permission.
     25  1.20.4.2  he  *
     26  1.20.4.2  he  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.20.4.2  he  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.20.4.2  he  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.20.4.2  he  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.20.4.2  he  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.20.4.2  he  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.20.4.2  he  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.20.4.2  he  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.20.4.2  he  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.20.4.2  he  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.20.4.2  he  * POSSIBILITY OF SUCH DAMAGE.
     37  1.20.4.2  he  */
     38  1.20.4.2  he 
     39  1.20.4.2  he /*-
     40  1.20.4.2  he  * Copyright (c) 2000 Michael Smith
     41  1.20.4.2  he  * Copyright (c) 2000 BSDi
     42  1.20.4.2  he  * All rights reserved.
     43  1.20.4.2  he  *
     44  1.20.4.2  he  * Redistribution and use in source and binary forms, with or without
     45  1.20.4.2  he  * modification, are permitted provided that the following conditions
     46  1.20.4.2  he  * are met:
     47  1.20.4.2  he  * 1. Redistributions of source code must retain the above copyright
     48  1.20.4.2  he  *    notice, this list of conditions and the following disclaimer.
     49  1.20.4.2  he  * 2. Redistributions in binary form must reproduce the above copyright
     50  1.20.4.2  he  *    notice, this list of conditions and the following disclaimer in the
     51  1.20.4.2  he  *    documentation and/or other materials provided with the distribution.
     52  1.20.4.2  he  *
     53  1.20.4.2  he  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     54  1.20.4.2  he  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  1.20.4.2  he  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  1.20.4.2  he  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     57  1.20.4.2  he  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  1.20.4.2  he  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  1.20.4.2  he  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  1.20.4.2  he  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  1.20.4.2  he  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  1.20.4.2  he  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  1.20.4.2  he  * SUCH DAMAGE.
     64  1.20.4.2  he  *
     65  1.20.4.2  he  * from FreeBSD: twe.c,v 1.1 2000/05/24 23:35:23 msmith Exp
     66  1.20.4.2  he  */
     67  1.20.4.2  he 
     68  1.20.4.2  he /*
     69  1.20.4.2  he  * Driver for the 3ware Escalade family of RAID controllers.
     70  1.20.4.2  he  */
     71  1.20.4.2  he 
     72  1.20.4.2  he #include <sys/param.h>
     73  1.20.4.2  he #include <sys/systm.h>
     74  1.20.4.2  he #include <sys/kernel.h>
     75  1.20.4.2  he #include <sys/device.h>
     76  1.20.4.2  he #include <sys/queue.h>
     77  1.20.4.2  he #include <sys/proc.h>
     78  1.20.4.2  he #include <sys/buf.h>
     79  1.20.4.2  he #include <sys/endian.h>
     80  1.20.4.2  he #include <sys/malloc.h>
     81  1.20.4.2  he #include <sys/disk.h>
     82  1.20.4.2  he 
     83  1.20.4.2  he #include <machine/vmparam.h>
     84  1.20.4.2  he #include <machine/bswap.h>
     85  1.20.4.2  he #include <machine/bus.h>
     86  1.20.4.2  he 
     87  1.20.4.2  he #include <dev/pci/pcireg.h>
     88  1.20.4.2  he #include <dev/pci/pcivar.h>
     89  1.20.4.2  he #include <dev/pci/pcidevs.h>
     90  1.20.4.2  he #include <dev/pci/twereg.h>
     91  1.20.4.2  he #include <dev/pci/twevar.h>
     92  1.20.4.2  he 
     93  1.20.4.2  he #define	TWE_INL(sc, port) \
     94  1.20.4.2  he     bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, port)
     95  1.20.4.2  he #define	TWE_OUTL(sc, port, val) \
     96  1.20.4.2  he     bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, port, val)
     97  1.20.4.2  he 
     98  1.20.4.2  he #define	PCI_CBIO	0x10
     99  1.20.4.2  he 
    100  1.20.4.2  he static void	twe_aen_handler(struct twe_ccb *, int);
    101  1.20.4.2  he static void	twe_attach(struct device *, struct device *, void *);
    102  1.20.4.2  he static int	twe_init_connection(struct twe_softc *);
    103  1.20.4.2  he static int	twe_intr(void *);
    104  1.20.4.2  he static int	twe_match(struct device *, struct cfdata *, void *);
    105  1.20.4.2  he static int	twe_param_get(struct twe_softc *, int, int, size_t,
    106  1.20.4.2  he 			      void (*)(struct twe_ccb *, int), void **);
    107  1.20.4.2  he static void	twe_poll(struct twe_softc *);
    108  1.20.4.2  he static int	twe_print(void *, const char *);
    109  1.20.4.2  he static int	twe_reset(struct twe_softc *);
    110  1.20.4.2  he static int	twe_submatch(struct device *, struct cfdata *, void *);
    111  1.20.4.2  he static int	twe_status_check(struct twe_softc *, u_int);
    112  1.20.4.2  he static int	twe_status_wait(struct twe_softc *, u_int, int);
    113  1.20.4.2  he 
    114  1.20.4.2  he struct cfattach twe_ca = {
    115  1.20.4.2  he 	sizeof(struct twe_softc), twe_match, twe_attach
    116  1.20.4.2  he };
    117  1.20.4.2  he 
    118  1.20.4.2  he struct {
    119  1.20.4.2  he 	const u_int	aen;		/* High byte non-zero if w/unit */
    120  1.20.4.2  he 	const char	*desc;
    121  1.20.4.2  he } static const twe_aen_names[] = {
    122  1.20.4.2  he 	{ 0x0000, "queue empty" },
    123  1.20.4.2  he 	{ 0x0001, "soft reset" },
    124  1.20.4.2  he 	{ 0x0102, "degraded mirror" },
    125  1.20.4.2  he 	{ 0x0003, "controller error" },
    126  1.20.4.2  he 	{ 0x0104, "rebuild fail" },
    127  1.20.4.2  he 	{ 0x0105, "rebuild done" },
    128  1.20.4.2  he 	{ 0x0106, "incompatible unit" },
    129  1.20.4.2  he 	{ 0x0107, "init done" },
    130  1.20.4.2  he 	{ 0x0108, "unclean shutdown" },
    131  1.20.4.2  he 	{ 0x0109, "aport timeout" },
    132  1.20.4.2  he 	{ 0x010a, "drive error" },
    133  1.20.4.2  he 	{ 0x010b, "rebuild started" },
    134  1.20.4.2  he 	{ 0x010c, "init started" },
    135  1.20.4.2  he 	{ 0x0015, "table undefined" },
    136  1.20.4.2  he 	{ 0x00ff, "aen queue full" },
    137  1.20.4.2  he };
    138  1.20.4.2  he 
    139  1.20.4.2  he /*
    140  1.20.4.2  he  * Match a supported board.
    141  1.20.4.2  he  */
    142  1.20.4.2  he static int
    143  1.20.4.2  he twe_match(struct device *parent, struct cfdata *cfdata, void *aux)
    144  1.20.4.2  he {
    145  1.20.4.2  he 	struct pci_attach_args *pa;
    146  1.20.4.2  he 
    147  1.20.4.2  he 	pa = aux;
    148  1.20.4.2  he 
    149  1.20.4.2  he 	return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
    150  1.20.4.2  he 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE ||
    151  1.20.4.2  he 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE_ASIC));
    152  1.20.4.2  he }
    153  1.20.4.2  he 
    154  1.20.4.2  he /*
    155  1.20.4.2  he  * Attach a supported board.
    156  1.20.4.2  he  *
    157  1.20.4.2  he  * XXX This doesn't fail gracefully.
    158  1.20.4.2  he  */
    159  1.20.4.2  he static void
    160  1.20.4.2  he twe_attach(struct device *parent, struct device *self, void *aux)
    161  1.20.4.2  he {
    162  1.20.4.2  he 	struct pci_attach_args *pa;
    163  1.20.4.2  he 	struct twe_softc *sc;
    164  1.20.4.2  he 	pci_chipset_tag_t pc;
    165  1.20.4.2  he 	pci_intr_handle_t ih;
    166  1.20.4.2  he 	pcireg_t csr;
    167  1.20.4.2  he 	const char *intrstr;
    168  1.20.4.2  he 	int size, i, rv, rseg;
    169  1.20.4.2  he 	struct twe_param *dtp, *ctp;
    170  1.20.4.2  he 	bus_dma_segment_t seg;
    171  1.20.4.2  he 	struct twe_cmd *tc;
    172  1.20.4.2  he 	struct twe_attach_args twea;
    173  1.20.4.2  he 	struct twe_ccb *ccb;
    174  1.20.4.2  he 
    175  1.20.4.2  he 	sc = (struct twe_softc *)self;
    176  1.20.4.2  he 	pa = aux;
    177  1.20.4.2  he 	pc = pa->pa_pc;
    178  1.20.4.2  he 	sc->sc_dmat = pa->pa_dmat;
    179  1.20.4.2  he 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    180  1.20.4.2  he 	SLIST_INIT(&sc->sc_ccb_freelist);
    181  1.20.4.2  he 
    182  1.20.4.2  he 	printf(": 3ware Escalade\n");
    183  1.20.4.2  he 
    184  1.20.4.2  he 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    185  1.20.4.2  he 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    186  1.20.4.2  he 		printf("%s: can't map i/o space\n", sc->sc_dv.dv_xname);
    187  1.20.4.2  he 		return;
    188  1.20.4.2  he 	}
    189  1.20.4.2  he 
    190  1.20.4.2  he 	/* Enable the device. */
    191  1.20.4.2  he 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    192  1.20.4.2  he 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    193  1.20.4.2  he 	    csr | PCI_COMMAND_MASTER_ENABLE);
    194  1.20.4.2  he 
    195  1.20.4.2  he 	/* Map and establish the interrupt. */
    196  1.20.4.2  he 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline,
    197  1.20.4.2  he 	    &ih)) {
    198  1.20.4.2  he 		printf("%s: can't map interrupt\n", sc->sc_dv.dv_xname);
    199  1.20.4.2  he 		return;
    200  1.20.4.2  he 	}
    201  1.20.4.2  he 	intrstr = pci_intr_string(pc, ih);
    202  1.20.4.2  he 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
    203  1.20.4.2  he 	if (sc->sc_ih == NULL) {
    204  1.20.4.2  he 		printf("%s: can't establish interrupt", sc->sc_dv.dv_xname);
    205  1.20.4.2  he 		if (intrstr != NULL)
    206  1.20.4.2  he 			printf(" at %s", intrstr);
    207  1.20.4.2  he 		printf("\n");
    208  1.20.4.2  he 		return;
    209  1.20.4.2  he 	}
    210  1.20.4.2  he 	if (intrstr != NULL)
    211  1.20.4.2  he 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
    212  1.20.4.2  he 
    213  1.20.4.2  he 	/*
    214  1.20.4.2  he 	 * Allocate and initialise the command blocks and CCBs.
    215  1.20.4.2  he 	 */
    216  1.20.4.2  he         size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
    217  1.20.4.2  he 
    218  1.20.4.2  he 	if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1,
    219  1.20.4.2  he 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    220  1.20.4.2  he 		printf("%s: unable to allocate commands, rv = %d\n",
    221  1.20.4.2  he 		    sc->sc_dv.dv_xname, rv);
    222  1.20.4.2  he 		return;
    223  1.20.4.2  he 	}
    224  1.20.4.2  he 
    225  1.20.4.2  he 	if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    226  1.20.4.2  he 	    (caddr_t *)&sc->sc_cmds,
    227  1.20.4.2  he 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    228  1.20.4.2  he 		printf("%s: unable to map commands, rv = %d\n",
    229  1.20.4.2  he 		    sc->sc_dv.dv_xname, rv);
    230  1.20.4.2  he 		return;
    231  1.20.4.2  he 	}
    232  1.20.4.2  he 
    233  1.20.4.2  he 	if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
    234  1.20.4.2  he 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    235  1.20.4.2  he 		printf("%s: unable to create command DMA map, rv = %d\n",
    236  1.20.4.2  he 		    sc->sc_dv.dv_xname, rv);
    237  1.20.4.2  he 		return;
    238  1.20.4.2  he 	}
    239  1.20.4.2  he 
    240  1.20.4.2  he 	if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
    241  1.20.4.2  he 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    242  1.20.4.2  he 		printf("%s: unable to load command DMA map, rv = %d\n",
    243  1.20.4.2  he 		    sc->sc_dv.dv_xname, rv);
    244  1.20.4.2  he 		return;
    245  1.20.4.2  he 	}
    246  1.20.4.2  he 
    247  1.20.4.2  he 	sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    248  1.20.4.2  he 	memset(sc->sc_cmds, 0, size);
    249  1.20.4.2  he 
    250  1.20.4.2  he 	ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
    251  1.20.4.2  he 	sc->sc_ccbs = ccb;
    252  1.20.4.2  he 	tc = (struct twe_cmd *)sc->sc_cmds;
    253  1.20.4.2  he 
    254  1.20.4.2  he 	for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
    255  1.20.4.2  he 		ccb->ccb_cmd = tc;
    256  1.20.4.2  he 		ccb->ccb_cmdid = i;
    257  1.20.4.2  he 		ccb->ccb_flags = 0;
    258  1.20.4.2  he 		rv = bus_dmamap_create(sc->sc_dmat, TWE_MAX_XFER,
    259  1.20.4.2  he 		    TWE_MAX_SEGS, NBPG, 0,
    260  1.20.4.2  he 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    261  1.20.4.2  he 		    &ccb->ccb_dmamap_xfer);
    262  1.20.4.2  he 		if (rv != 0) {
    263  1.20.4.2  he 			printf("%s: can't create dmamap, rv = %d\n",
    264  1.20.4.2  he 			    sc->sc_dv.dv_xname, rv);
    265  1.20.4.2  he 			return;
    266  1.20.4.2  he 		}
    267  1.20.4.2  he 		/* Save one CCB for parameter retrieval. */
    268  1.20.4.2  he 		if (i != 0)
    269  1.20.4.2  he 			SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
    270  1.20.4.2  he 			    ccb_chain.slist);
    271  1.20.4.2  he 	}
    272  1.20.4.2  he 
    273  1.20.4.2  he 	/* Wait for the controller to become ready. */
    274  1.20.4.2  he 	if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
    275  1.20.4.2  he 		printf("%s: microcontroller not ready\n", sc->sc_dv.dv_xname);
    276  1.20.4.2  he 		return;
    277  1.20.4.2  he 	}
    278  1.20.4.2  he 
    279  1.20.4.2  he 	TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
    280  1.20.4.2  he 
    281  1.20.4.2  he 	/* Reset the controller. */
    282  1.20.4.2  he 	if (twe_reset(sc)) {
    283  1.20.4.2  he 		printf("%s: reset failed\n", sc->sc_dv.dv_xname);
    284  1.20.4.2  he 		return;
    285  1.20.4.2  he 	}
    286  1.20.4.2  he 
    287  1.20.4.2  he 	/* Find attached units. */
    288  1.20.4.2  he 	rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
    289  1.20.4.2  he 	    TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, (void **)&dtp);
    290  1.20.4.2  he 	if (rv != 0) {
    291  1.20.4.2  he 		printf("%s: can't detect attached units (%d)\n",
    292  1.20.4.2  he 		    sc->sc_dv.dv_xname, rv);
    293  1.20.4.2  he 		return;
    294  1.20.4.2  he 	}
    295  1.20.4.2  he 
    296  1.20.4.2  he 	/* For each detected unit, collect size and store in an array. */
    297  1.20.4.2  he 	for (i = 0, sc->sc_nunits = 0; i < TWE_MAX_UNITS; i++) {
    298  1.20.4.2  he 		/* Unit present? */
    299  1.20.4.2  he 		if ((dtp->tp_data[i] & TWE_PARAM_UNITSTATUS_Online) == 0) {
    300  1.20.4.2  he 			sc->sc_dsize[i] = 0;
    301  1.20.4.2  he 	   		continue;
    302  1.20.4.2  he 	   	}
    303  1.20.4.2  he 
    304  1.20.4.2  he 		rv = twe_param_get(sc, TWE_PARAM_UNITINFO + i,
    305  1.20.4.2  he 		    TWE_PARAM_UNITINFO_Capacity, 4, NULL, (void **)&ctp);
    306  1.20.4.2  he 		if (rv != 0) {
    307  1.20.4.2  he 			printf("%s: error %d fetching capacity for unit %d\n",
    308  1.20.4.2  he 			    sc->sc_dv.dv_xname, rv, i);
    309  1.20.4.2  he 			continue;
    310  1.20.4.2  he 		}
    311  1.20.4.2  he 
    312  1.20.4.2  he 		sc->sc_dsize[i] = le32toh(*(u_int32_t *)ctp->tp_data);
    313  1.20.4.2  he 		free(ctp, M_DEVBUF);
    314  1.20.4.2  he 		sc->sc_nunits++;
    315  1.20.4.2  he 	}
    316  1.20.4.2  he 	free(dtp, M_DEVBUF);
    317  1.20.4.2  he 
    318  1.20.4.2  he 	/* Initialise connection with controller and enable interrupts. */
    319  1.20.4.2  he 	twe_init_connection(sc);
    320  1.20.4.2  he 	TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
    321  1.20.4.2  he 	    TWE_CTL_UNMASK_RESP_INTR |
    322  1.20.4.2  he 	    TWE_CTL_ENABLE_INTRS);
    323  1.20.4.2  he 
    324  1.20.4.2  he 	/* Attach sub-devices. */
    325  1.20.4.2  he 	for (i = 0; i < TWE_MAX_UNITS; i++) {
    326  1.20.4.2  he 		if (sc->sc_dsize[i] == 0)
    327  1.20.4.2  he 			continue;
    328  1.20.4.2  he 		twea.twea_unit = i;
    329  1.20.4.2  he 		config_found_sm(&sc->sc_dv, &twea, twe_print, twe_submatch);
    330  1.20.4.2  he 	}
    331  1.20.4.2  he }
    332  1.20.4.2  he 
    333  1.20.4.2  he /*
    334  1.20.4.2  he  * Reset the controller.  Currently only useful at attach time; must be
    335  1.20.4.2  he  * called with interrupts blocked.
    336  1.20.4.2  he  */
    337  1.20.4.2  he static int
    338  1.20.4.2  he twe_reset(struct twe_softc *sc)
    339  1.20.4.2  he {
    340  1.20.4.2  he 	struct twe_param *tp;
    341  1.20.4.2  he 	u_int aen, status;
    342  1.20.4.2  he 	volatile u_int32_t junk;
    343  1.20.4.2  he 	int got, rv;
    344  1.20.4.2  he 
    345  1.20.4.2  he 	/* Issue a soft reset. */
    346  1.20.4.2  he 	TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
    347  1.20.4.2  he 	    TWE_CTL_CLEAR_HOST_INTR |
    348  1.20.4.2  he 	    TWE_CTL_CLEAR_ATTN_INTR |
    349  1.20.4.2  he 	    TWE_CTL_MASK_CMD_INTR |
    350  1.20.4.2  he 	    TWE_CTL_MASK_RESP_INTR |
    351  1.20.4.2  he 	    TWE_CTL_CLEAR_ERROR_STS |
    352  1.20.4.2  he 	    TWE_CTL_DISABLE_INTRS);
    353  1.20.4.2  he 
    354  1.20.4.2  he 	if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 15)) {
    355  1.20.4.2  he 		printf("%s: no attention interrupt\n",
    356  1.20.4.2  he 		    sc->sc_dv.dv_xname);
    357  1.20.4.2  he 		return (-1);
    358  1.20.4.2  he 	}
    359  1.20.4.2  he 
    360  1.20.4.2  he 	/* Pull AENs out of the controller; look for a soft reset AEN. */
    361  1.20.4.2  he 	for (got = 0;;) {
    362  1.20.4.2  he 		rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
    363  1.20.4.2  he 		    2, NULL, (void **)&tp);
    364  1.20.4.2  he 		if (rv != 0)
    365  1.20.4.2  he 			printf("%s: error %d while draining response queue\n",
    366  1.20.4.2  he 			    sc->sc_dv.dv_xname, rv);
    367  1.20.4.2  he 		aen = TWE_AEN_CODE(le16toh(*(u_int16_t *)tp->tp_data));
    368  1.20.4.2  he 		free(tp, M_DEVBUF);
    369  1.20.4.2  he 		if (aen == TWE_AEN_QUEUE_EMPTY)
    370  1.20.4.2  he 			break;
    371  1.20.4.2  he 		if (aen == TWE_AEN_SOFT_RESET)
    372  1.20.4.2  he 			got = 1;
    373  1.20.4.2  he 	}
    374  1.20.4.2  he 	if (!got) {
    375  1.20.4.2  he 		printf("%s: reset not reported\n", sc->sc_dv.dv_xname);
    376  1.20.4.2  he 		return (-1);
    377  1.20.4.2  he 	}
    378  1.20.4.2  he 
    379  1.20.4.2  he 	/* Check controller status. */
    380  1.20.4.2  he 	status = TWE_INL(sc, TWE_REG_STS);
    381  1.20.4.2  he 	if (twe_status_check(sc, status)) {
    382  1.20.4.2  he 		printf("%s: controller errors detected\n",
    383  1.20.4.2  he 		    sc->sc_dv.dv_xname);
    384  1.20.4.2  he 		return (-1);
    385  1.20.4.2  he 	}
    386  1.20.4.2  he 
    387  1.20.4.2  he 	/* Drain the response queue. */
    388  1.20.4.2  he 	for (;;) {
    389  1.20.4.2  he 		status = TWE_INL(sc, TWE_REG_STS);
    390  1.20.4.2  he 		if (twe_status_check(sc, status) != 0) {
    391  1.20.4.2  he 			printf("%s: can't drain response queue\n",
    392  1.20.4.2  he 			    sc->sc_dv.dv_xname);
    393  1.20.4.2  he 			return (-1);
    394  1.20.4.2  he 		}
    395  1.20.4.2  he 		if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
    396  1.20.4.2  he 			break;
    397  1.20.4.2  he 		junk = TWE_INL(sc, TWE_REG_RESP_QUEUE);
    398  1.20.4.2  he 	}
    399  1.20.4.2  he 
    400  1.20.4.2  he 	return (0);
    401  1.20.4.2  he }
    402  1.20.4.2  he 
    403  1.20.4.2  he /*
    404  1.20.4.2  he  * Print autoconfiguration message for a sub-device.
    405  1.20.4.2  he  */
    406  1.20.4.2  he static int
    407  1.20.4.2  he twe_print(void *aux, const char *pnp)
    408  1.20.4.2  he {
    409  1.20.4.2  he 	struct twe_attach_args *twea;
    410  1.20.4.2  he 
    411  1.20.4.2  he 	twea = aux;
    412  1.20.4.2  he 
    413  1.20.4.2  he 	if (pnp != NULL)
    414  1.20.4.2  he 		printf("block device at %s", pnp);
    415  1.20.4.2  he 	printf(" unit %d", twea->twea_unit);
    416  1.20.4.2  he 	return (UNCONF);
    417  1.20.4.2  he }
    418  1.20.4.2  he 
    419  1.20.4.2  he /*
    420  1.20.4.2  he  * Match a sub-device.
    421  1.20.4.2  he  */
    422  1.20.4.2  he static int
    423  1.20.4.2  he twe_submatch(struct device *parent, struct cfdata *cf, void *aux)
    424  1.20.4.2  he {
    425  1.20.4.2  he 	struct twe_attach_args *twea;
    426  1.20.4.2  he 
    427  1.20.4.2  he 	twea = aux;
    428  1.20.4.2  he 
    429  1.20.4.2  he 	if (cf->tweacf_unit != TWECF_UNIT_DEFAULT &&
    430  1.20.4.2  he 	    cf->tweacf_unit != twea->twea_unit)
    431  1.20.4.2  he 		return (0);
    432  1.20.4.2  he 
    433  1.20.4.2  he 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
    434  1.20.4.2  he }
    435  1.20.4.2  he 
    436  1.20.4.2  he /*
    437  1.20.4.2  he  * Interrupt service routine.
    438  1.20.4.2  he  */
    439  1.20.4.2  he static int
    440  1.20.4.2  he twe_intr(void *arg)
    441  1.20.4.2  he {
    442  1.20.4.2  he 	struct twe_softc *sc;
    443  1.20.4.2  he 	u_int status;
    444  1.20.4.2  he 	int caught, rv;
    445  1.20.4.2  he 
    446  1.20.4.2  he 	sc = arg;
    447  1.20.4.2  he 	caught = 0;
    448  1.20.4.2  he 	status = TWE_INL(sc, TWE_REG_STS);
    449  1.20.4.2  he 	twe_status_check(sc, status);
    450  1.20.4.2  he 
    451  1.20.4.2  he 	/* Host interrupts - purpose unknown. */
    452  1.20.4.2  he 	if ((status & TWE_STS_HOST_INTR) != 0) {
    453  1.20.4.2  he #ifdef DIAGNOSTIC
    454  1.20.4.2  he 		printf("%s: host interrupt\n", sc->sc_dv.dv_xname);
    455  1.20.4.2  he #endif
    456  1.20.4.2  he 		TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
    457  1.20.4.2  he 		caught = 1;
    458  1.20.4.2  he 	}
    459  1.20.4.2  he 
    460  1.20.4.2  he 	/*
    461  1.20.4.2  he 	 * Attention interrupts, signalled when a controller or child device
    462  1.20.4.2  he 	 * state change has occurred.
    463  1.20.4.2  he 	 */
    464  1.20.4.2  he 	if ((status & TWE_STS_ATTN_INTR) != 0) {
    465  1.20.4.2  he 		if ((sc->sc_flags & TWEF_AEN) == 0) {
    466  1.20.4.2  he 			rv = twe_param_get(sc, TWE_PARAM_AEN,
    467  1.20.4.2  he 			    TWE_PARAM_AEN_UnitCode, 2, twe_aen_handler,
    468  1.20.4.2  he 			    NULL);
    469  1.20.4.2  he 			if (rv != 0) {
    470  1.20.4.2  he 				printf("%s: unable to retrieve AEN (%d)\n",
    471  1.20.4.2  he 				    sc->sc_dv.dv_xname, rv);
    472  1.20.4.2  he 				TWE_OUTL(sc, TWE_REG_CTL,
    473  1.20.4.2  he 				    TWE_CTL_CLEAR_ATTN_INTR);
    474  1.20.4.2  he 			} else
    475  1.20.4.2  he 				sc->sc_flags |= TWEF_AEN;
    476  1.20.4.2  he 		}
    477  1.20.4.2  he 		caught = 1;
    478  1.20.4.2  he 	}
    479  1.20.4.2  he 
    480  1.20.4.2  he 	/*
    481  1.20.4.2  he 	 * Command interrupts, signalled when the controller can accept more
    482  1.20.4.2  he 	 * commands.  We don't use this; instead, we try to submit commands
    483  1.20.4.2  he 	 * when we receive them, and when other commands have completed.
    484  1.20.4.2  he 	 * Mask it so we don't get another one.
    485  1.20.4.2  he 	 */
    486  1.20.4.2  he 	if ((status & TWE_STS_CMD_INTR) != 0) {
    487  1.20.4.2  he #ifdef DIAGNOSTIC
    488  1.20.4.2  he 		printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
    489  1.20.4.2  he #endif
    490  1.20.4.2  he 		TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
    491  1.20.4.2  he 		caught = 1;
    492  1.20.4.2  he 	}
    493  1.20.4.2  he 
    494  1.20.4.2  he 	if ((status & TWE_STS_RESP_INTR) != 0) {
    495  1.20.4.2  he 		twe_poll(sc);
    496  1.20.4.2  he 		caught = 1;
    497  1.20.4.2  he 	}
    498  1.20.4.2  he 
    499  1.20.4.2  he 	return (caught);
    500  1.20.4.2  he }
    501  1.20.4.2  he 
    502  1.20.4.2  he /*
    503  1.20.4.2  he  * Handle an AEN returned by the controller.
    504  1.20.4.2  he  */
    505  1.20.4.2  he static void
    506  1.20.4.2  he twe_aen_handler(struct twe_ccb *ccb, int error)
    507  1.20.4.2  he {
    508  1.20.4.2  he 	struct twe_softc *sc;
    509  1.20.4.2  he 	struct twe_param *tp;
    510  1.20.4.2  he 	const char *str;
    511  1.20.4.2  he 	u_int aen;
    512  1.20.4.2  he 	int i, hu, rv;
    513  1.20.4.2  he 
    514  1.20.4.2  he 	sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
    515  1.20.4.2  he 	tp = ccb->ccb_tx.tx_context;
    516  1.20.4.2  he 	twe_ccb_unmap(sc, ccb);
    517  1.20.4.2  he 
    518  1.20.4.2  he 	if (error) {
    519  1.20.4.2  he 		printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
    520  1.20.4.2  he 		aen = TWE_AEN_QUEUE_EMPTY;
    521  1.20.4.2  he 	} else
    522  1.20.4.2  he 		aen = le16toh(*(u_int16_t *)tp->tp_data);
    523  1.20.4.2  he 	free(tp, M_DEVBUF);
    524  1.20.4.2  he 	twe_ccb_free(sc, ccb);
    525  1.20.4.2  he 
    526  1.20.4.2  he 	if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
    527  1.20.4.2  he 		TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
    528  1.20.4.2  he 		sc->sc_flags &= ~TWEF_AEN;
    529  1.20.4.2  he 		return;
    530  1.20.4.2  he 	}
    531  1.20.4.2  he 
    532  1.20.4.2  he 	str = "<unknown>";
    533  1.20.4.2  he 	i = 0;
    534  1.20.4.2  he 	hu = 0;
    535  1.20.4.2  he 
    536  1.20.4.2  he 	while (i < sizeof(twe_aen_names) / sizeof(twe_aen_names[0])) {
    537  1.20.4.2  he 		if (TWE_AEN_CODE(twe_aen_names[i].aen) == TWE_AEN_CODE(aen)) {
    538  1.20.4.2  he 			str = twe_aen_names[i].desc;
    539  1.20.4.2  he 			hu = (TWE_AEN_UNIT(twe_aen_names[i].aen) != 0);
    540  1.20.4.2  he 			break;
    541  1.20.4.2  he 		}
    542  1.20.4.2  he 		i++;
    543  1.20.4.2  he 	}
    544  1.20.4.2  he 	printf("%s: AEN 0x%04x (%s) received", sc->sc_dv.dv_xname,
    545  1.20.4.2  he 	    TWE_AEN_CODE(aen), str);
    546  1.20.4.2  he 	if (hu != 0)
    547  1.20.4.2  he 		printf(" for unit %d", TWE_AEN_UNIT(aen));
    548  1.20.4.2  he 	printf("\n");
    549  1.20.4.2  he 
    550  1.20.4.2  he 	/*
    551  1.20.4.2  he 	 * Chain another retrieval in case interrupts have been
    552  1.20.4.2  he 	 * coalesced.
    553  1.20.4.2  he 	 */
    554  1.20.4.2  he 	rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2,
    555  1.20.4.2  he 	    twe_aen_handler, NULL);
    556  1.20.4.2  he 	if (rv != 0)
    557  1.20.4.2  he 		printf("%s: unable to retrieve AEN (%d)\n",
    558  1.20.4.2  he 		    sc->sc_dv.dv_xname, rv);
    559  1.20.4.2  he }
    560  1.20.4.2  he 
    561  1.20.4.2  he /*
    562  1.20.4.2  he  * Execute a TWE_OP_GET_PARAM command.  If a callback function is provided,
    563  1.20.4.2  he  * it will be called with generated context when the command has completed.
    564  1.20.4.2  he  * If no callback is provided, the command will be executed synchronously
    565  1.20.4.2  he  * and a pointer to a buffer containing the data returned.
    566  1.20.4.2  he  *
    567  1.20.4.2  he  * The caller or callback is responsible for freeing the buffer.
    568  1.20.4.2  he  */
    569  1.20.4.2  he static int
    570  1.20.4.2  he twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
    571  1.20.4.2  he 	      void (*func)(struct twe_ccb *, int), void **pbuf)
    572  1.20.4.2  he {
    573  1.20.4.2  he 	struct twe_ccb *ccb;
    574  1.20.4.2  he 	struct twe_cmd *tc;
    575  1.20.4.2  he 	struct twe_param *tp;
    576  1.20.4.2  he 	int rv, s;
    577  1.20.4.2  he 
    578  1.20.4.2  he 	rv = twe_ccb_alloc(sc, &ccb,
    579  1.20.4.2  he 	    TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
    580  1.20.4.2  he 	if (rv != 0)
    581  1.20.4.2  he 		return (rv);
    582  1.20.4.2  he 
    583  1.20.4.2  he 	tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
    584  1.20.4.2  he 	if (pbuf != NULL)
    585  1.20.4.2  he 		*pbuf = tp;
    586  1.20.4.2  he 
    587  1.20.4.2  he 	ccb->ccb_data = tp;
    588  1.20.4.2  he 	ccb->ccb_datasize = TWE_SECTOR_SIZE;
    589  1.20.4.2  he 	ccb->ccb_tx.tx_handler = func;
    590  1.20.4.2  he 	ccb->ccb_tx.tx_context = tp;
    591  1.20.4.2  he 	ccb->ccb_tx.tx_dv = &sc->sc_dv;
    592  1.20.4.2  he 
    593  1.20.4.2  he 	tc = ccb->ccb_cmd;
    594  1.20.4.2  he 	tc->tc_size = 2;
    595  1.20.4.2  he 	tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
    596  1.20.4.2  he 	tc->tc_unit = 0;
    597  1.20.4.2  he 	tc->tc_count = htole16(1);
    598  1.20.4.2  he 
    599  1.20.4.2  he 	/* Fill in the outbound parameter data. */
    600  1.20.4.2  he 	tp->tp_table_id = htole16(table_id);
    601  1.20.4.2  he 	tp->tp_param_id = param_id;
    602  1.20.4.2  he 	tp->tp_param_size = size;
    603  1.20.4.2  he 
    604  1.20.4.2  he 	/* Map the transfer. */
    605  1.20.4.2  he 	if ((rv = twe_ccb_map(sc, ccb)) != 0) {
    606  1.20.4.2  he 		twe_ccb_free(sc, ccb);
    607  1.20.4.2  he 		free(tp, M_DEVBUF);
    608  1.20.4.2  he 		return (rv);
    609  1.20.4.2  he 	}
    610  1.20.4.2  he 
    611  1.20.4.2  he 	/* Submit the command and either wait or let the callback handle it. */
    612  1.20.4.2  he 	if (func == NULL) {
    613  1.20.4.2  he 		s = splbio();
    614  1.20.4.2  he 		rv = twe_ccb_poll(sc, ccb, 5);
    615  1.20.4.2  he 		twe_ccb_unmap(sc, ccb);
    616  1.20.4.2  he 		twe_ccb_free(sc, ccb);
    617  1.20.4.2  he 		splx(s);
    618  1.20.4.2  he 		if (rv != 0)
    619  1.20.4.2  he 			free(tp, M_DEVBUF);
    620  1.20.4.2  he 	} else {
    621  1.20.4.2  he 		twe_ccb_enqueue(sc, ccb);
    622  1.20.4.2  he 		rv = 0;
    623  1.20.4.2  he 	}
    624  1.20.4.2  he 
    625  1.20.4.2  he 	return (rv);
    626  1.20.4.2  he }
    627  1.20.4.2  he 
    628  1.20.4.2  he /*
    629  1.20.4.2  he  * Execute a TWE_OP_INIT_CONNECTION command.  Return non-zero on error.
    630  1.20.4.2  he  * Must be called with interrupts blocked.
    631  1.20.4.2  he  */
    632  1.20.4.2  he static int
    633  1.20.4.2  he twe_init_connection(struct twe_softc *sc)
    634  1.20.4.2  he {
    635  1.20.4.2  he 	struct twe_ccb *ccb;
    636  1.20.4.2  he 	struct twe_cmd *tc;
    637  1.20.4.2  he 	int rv;
    638  1.20.4.2  he 
    639  1.20.4.2  he 	if ((rv = twe_ccb_alloc(sc, &ccb, 0)) != 0)
    640  1.20.4.2  he 		return (rv);
    641  1.20.4.2  he 
    642  1.20.4.2  he 	/* Build the command. */
    643  1.20.4.2  he 	tc = ccb->ccb_cmd;
    644  1.20.4.2  he 	tc->tc_size = 3;
    645  1.20.4.2  he 	tc->tc_opcode = TWE_OP_INIT_CONNECTION;
    646  1.20.4.2  he 	tc->tc_unit = 0;
    647  1.20.4.2  he 	tc->tc_count = htole16(TWE_MAX_CMDS);
    648  1.20.4.2  he 	tc->tc_args.init_connection.response_queue_pointer = 0;
    649  1.20.4.2  he 
    650  1.20.4.2  he 	/* Submit the command for immediate execution. */
    651  1.20.4.2  he 	rv = twe_ccb_poll(sc, ccb, 5);
    652  1.20.4.2  he 	twe_ccb_free(sc, ccb);
    653  1.20.4.2  he 	return (rv);
    654  1.20.4.2  he }
    655  1.20.4.2  he 
    656  1.20.4.2  he /*
    657  1.20.4.2  he  * Poll the controller for completed commands.  Must be called with
    658  1.20.4.2  he  * interrupts blocked.
    659  1.20.4.2  he  */
    660  1.20.4.2  he static void
    661  1.20.4.2  he twe_poll(struct twe_softc *sc)
    662  1.20.4.2  he {
    663  1.20.4.2  he 	struct twe_ccb *ccb;
    664  1.20.4.2  he 	int found;
    665  1.20.4.2  he 	u_int status, cmdid;
    666  1.20.4.2  he 
    667  1.20.4.2  he 	found = 0;
    668  1.20.4.2  he 
    669  1.20.4.2  he 	for (;;) {
    670  1.20.4.2  he 		status = TWE_INL(sc, TWE_REG_STS);
    671  1.20.4.2  he 		twe_status_check(sc, status);
    672  1.20.4.2  he 
    673  1.20.4.2  he 		if ((status & TWE_STS_RESP_QUEUE_EMPTY))
    674  1.20.4.2  he 			break;
    675  1.20.4.2  he 
    676  1.20.4.2  he 		found = 1;
    677  1.20.4.2  he 		cmdid = TWE_INL(sc, TWE_REG_RESP_QUEUE);
    678  1.20.4.2  he 		cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
    679  1.20.4.2  he 		if (cmdid >= TWE_MAX_QUEUECNT) {
    680  1.20.4.2  he 			printf("%s: bad completion\n", sc->sc_dv.dv_xname);
    681  1.20.4.2  he 			continue;
    682  1.20.4.2  he 		}
    683  1.20.4.2  he 
    684  1.20.4.2  he 		ccb = sc->sc_ccbs + cmdid;
    685  1.20.4.2  he 		if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
    686  1.20.4.2  he 			printf("%s: bad completion (not active)\n",
    687  1.20.4.2  he 			    sc->sc_dv.dv_xname);
    688  1.20.4.2  he 			continue;
    689  1.20.4.2  he 		}
    690  1.20.4.2  he 		ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
    691  1.20.4.2  he 
    692  1.20.4.2  he 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    693  1.20.4.2  he 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
    694  1.20.4.2  he 		    sizeof(struct twe_cmd),
    695  1.20.4.2  he 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
    696  1.20.4.2  he 
    697  1.20.4.2  he 		/* Pass notification to upper layers. */
    698  1.20.4.2  he 		if (ccb->ccb_tx.tx_handler != NULL)
    699  1.20.4.2  he 			(*ccb->ccb_tx.tx_handler)(ccb,
    700  1.20.4.2  he 			    ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
    701  1.20.4.2  he 	}
    702  1.20.4.2  he 
    703  1.20.4.2  he 	/* If any commands have completed, run the software queue. */
    704  1.20.4.2  he 	if (found)
    705  1.20.4.2  he 		twe_ccb_enqueue(sc, NULL);
    706  1.20.4.2  he }
    707  1.20.4.2  he 
    708  1.20.4.2  he /*
    709  1.20.4.2  he  * Wait for `status' to be set in the controller status register.  Return
    710  1.20.4.2  he  * zero if found, non-zero if the operation timed out.
    711  1.20.4.2  he  */
    712  1.20.4.2  he static int
    713  1.20.4.2  he twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
    714  1.20.4.2  he {
    715  1.20.4.2  he 
    716  1.20.4.2  he 	for (timo *= 10; timo != 0; timo--) {
    717  1.20.4.2  he 		if ((TWE_INL(sc, TWE_REG_STS) & status) == status)
    718  1.20.4.2  he 			break;
    719  1.20.4.2  he 		delay(100000);
    720  1.20.4.2  he 	}
    721  1.20.4.2  he 
    722  1.20.4.2  he 	return (timo == 0);
    723  1.20.4.2  he }
    724  1.20.4.2  he 
    725  1.20.4.2  he /*
    726  1.20.4.2  he  * Complain if the status bits aren't what we expect.
    727  1.20.4.2  he  */
    728  1.20.4.2  he static int
    729  1.20.4.2  he twe_status_check(struct twe_softc *sc, u_int status)
    730  1.20.4.2  he {
    731  1.20.4.2  he 	int rv;
    732  1.20.4.2  he 
    733  1.20.4.2  he 	rv = 0;
    734  1.20.4.2  he 
    735  1.20.4.2  he 	if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
    736  1.20.4.2  he 		printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
    737  1.20.4.2  he 		    status & ~TWE_STS_EXPECTED_BITS);
    738  1.20.4.2  he 		rv = -1;
    739  1.20.4.2  he 	}
    740  1.20.4.2  he 
    741  1.20.4.2  he 	if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
    742  1.20.4.2  he 		printf("%s: unexpected status bits: 0x%08x\n",
    743  1.20.4.2  he 		    sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
    744  1.20.4.2  he 		rv = -1;
    745  1.20.4.2  he 	}
    746  1.20.4.2  he 
    747  1.20.4.2  he 	return (rv);
    748  1.20.4.2  he }
    749  1.20.4.2  he 
    750  1.20.4.2  he /*
    751  1.20.4.2  he  * Allocate and initialise a CCB.
    752  1.20.4.2  he  */
    753  1.20.4.2  he int
    754  1.20.4.2  he twe_ccb_alloc(struct twe_softc *sc, struct twe_ccb **ccbp, int flags)
    755  1.20.4.2  he {
    756  1.20.4.2  he 	struct twe_cmd *tc;
    757  1.20.4.2  he 	struct twe_ccb *ccb;
    758  1.20.4.2  he 	int s;
    759  1.20.4.2  he 
    760  1.20.4.2  he 	s = splbio();
    761  1.20.4.2  he 	if ((flags & TWE_CCB_PARAM) != 0)
    762  1.20.4.2  he 		ccb = sc->sc_ccbs;
    763  1.20.4.2  he 	else {
    764  1.20.4.2  he 		/* Allocate a CCB and command block. */
    765  1.20.4.2  he 		if (SLIST_FIRST(&sc->sc_ccb_freelist) == NULL) {
    766  1.20.4.2  he 			splx(s);
    767  1.20.4.2  he 			return (EAGAIN);
    768  1.20.4.2  he 		}
    769  1.20.4.2  he 		ccb = SLIST_FIRST(&sc->sc_ccb_freelist);
    770  1.20.4.2  he 		SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
    771  1.20.4.2  he 	}
    772  1.20.4.2  he #ifdef DIAGNOSTIC
    773  1.20.4.2  he 	if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
    774  1.20.4.2  he 		panic("twe_ccb_alloc: CCB already allocated");
    775  1.20.4.2  he 	flags |= TWE_CCB_ALLOCED;
    776  1.20.4.2  he #endif
    777  1.20.4.2  he 	splx(s);
    778  1.20.4.2  he 
    779  1.20.4.2  he 	/* Initialise some fields and return. */
    780  1.20.4.2  he 	ccb->ccb_tx.tx_handler = NULL;
    781  1.20.4.2  he 	ccb->ccb_flags = flags;
    782  1.20.4.2  he 	tc = ccb->ccb_cmd;
    783  1.20.4.2  he 	tc->tc_status = 0;
    784  1.20.4.2  he 	tc->tc_flags = 0;
    785  1.20.4.2  he 	tc->tc_cmdid = ccb->ccb_cmdid;
    786  1.20.4.2  he 	*ccbp = ccb;
    787  1.20.4.2  he 
    788  1.20.4.2  he 	return (0);
    789  1.20.4.2  he }
    790  1.20.4.2  he 
    791  1.20.4.2  he /*
    792  1.20.4.2  he  * Free a CCB.
    793  1.20.4.2  he  */
    794  1.20.4.2  he void
    795  1.20.4.2  he twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
    796  1.20.4.2  he {
    797  1.20.4.2  he 	int s;
    798  1.20.4.2  he 
    799  1.20.4.2  he 	s = splbio();
    800  1.20.4.2  he 	if ((ccb->ccb_flags & TWE_CCB_PARAM) == 0)
    801  1.20.4.2  he 		SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
    802  1.20.4.2  he 	ccb->ccb_flags = 0;
    803  1.20.4.2  he 	splx(s);
    804  1.20.4.2  he }
    805  1.20.4.2  he 
    806  1.20.4.2  he /*
    807  1.20.4.2  he  * Map the specified CCB's command block and data buffer (if any) into
    808  1.20.4.2  he  * controller visible space.  Perform DMA synchronisation.
    809  1.20.4.2  he  */
    810  1.20.4.2  he int
    811  1.20.4.2  he twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
    812  1.20.4.2  he {
    813  1.20.4.2  he 	struct twe_buf *tb;
    814  1.20.4.2  he 	struct twe_cmd *tc;
    815  1.20.4.2  he 	int flags, nsegs, i, rv;
    816  1.20.4.2  he 	void *data;
    817  1.20.4.2  he 
    818  1.20.4.2  he 	/*
    819  1.20.4.2  he 	 * The data as a whole must be 512-byte aligned.
    820  1.20.4.2  he 	 */
    821  1.20.4.2  he 	if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
    822  1.20.4.2  he 		tb = malloc(sizeof(*ccb->ccb_buf), M_DEVBUF, M_NOWAIT);
    823  1.20.4.2  he 
    824  1.20.4.2  he 		rv = bus_dmamem_alloc(sc->sc_dmat, ccb->ccb_datasize,
    825  1.20.4.2  he 		    NBPG, 0, tb->tb_segs, TWE_MAX_SEGS, &tb->tb_rseg,
    826  1.20.4.2  he 		    BUS_DMA_NOWAIT);
    827  1.20.4.2  he 		if (rv != 0)
    828  1.20.4.2  he 			return (rv);
    829  1.20.4.2  he 
    830  1.20.4.2  he 		rv = bus_dmamem_map(sc->sc_dmat, tb->tb_segs, tb->tb_rseg,
    831  1.20.4.2  he 		    ccb->ccb_datasize, &tb->tb_vaddr, BUS_DMA_NOWAIT);
    832  1.20.4.2  he 		if (rv != 0) {
    833  1.20.4.2  he 			bus_dmamem_free(sc->sc_dmat, tb->tb_segs, tb->tb_rseg);
    834  1.20.4.2  he 			return (rv);
    835  1.20.4.2  he 		}
    836  1.20.4.2  he 
    837  1.20.4.2  he 		ccb->ccb_buf = tb;
    838  1.20.4.2  he 		data = tb->tb_vaddr;
    839  1.20.4.2  he 		if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
    840  1.20.4.2  he 			memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
    841  1.20.4.2  he 	} else {
    842  1.20.4.2  he 		tb = NULL;
    843  1.20.4.2  he 		ccb->ccb_buf = NULL;
    844  1.20.4.2  he 		data = ccb->ccb_data;
    845  1.20.4.2  he 	}
    846  1.20.4.2  he 
    847  1.20.4.2  he 	/*
    848  1.20.4.2  he 	 * Map the data buffer into bus space and build the S/G list.
    849  1.20.4.2  he 	 */
    850  1.20.4.2  he 	rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
    851  1.20.4.2  he 	    ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT);
    852  1.20.4.2  he 	if (rv != 0) {
    853  1.20.4.2  he 		if (tb != NULL) {
    854  1.20.4.2  he 			bus_dmamem_unmap(sc->sc_dmat, tb->tb_vaddr,
    855  1.20.4.2  he 			    ccb->ccb_datasize);
    856  1.20.4.2  he 			bus_dmamem_free(sc->sc_dmat, tb->tb_segs, tb->tb_rseg);
    857  1.20.4.2  he 		}
    858  1.20.4.2  he 		return (rv);
    859  1.20.4.2  he 	}
    860  1.20.4.2  he 
    861  1.20.4.2  he 	nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
    862  1.20.4.2  he 	tc = ccb->ccb_cmd;
    863  1.20.4.2  he 	tc->tc_size += 2 * nsegs;
    864  1.20.4.2  he 
    865  1.20.4.2  he 	/* The location of the S/G list is dependant upon command type. */
    866  1.20.4.2  he 	switch (tc->tc_opcode >> 5) {
    867  1.20.4.2  he 	case 2:
    868  1.20.4.2  he 		for (i = 0; i < nsegs; i++) {
    869  1.20.4.2  he 			tc->tc_args.param.sgl[i].tsg_address =
    870  1.20.4.2  he 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    871  1.20.4.2  he 			tc->tc_args.param.sgl[i].tsg_length =
    872  1.20.4.2  he 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    873  1.20.4.2  he 		}
    874  1.20.4.2  he 		/* XXX Needed? */
    875  1.20.4.2  he 		for (; i < TWE_SG_SIZE; i++) {
    876  1.20.4.2  he 			tc->tc_args.param.sgl[i].tsg_address = 0;
    877  1.20.4.2  he 			tc->tc_args.param.sgl[i].tsg_length = 0;
    878  1.20.4.2  he 		}
    879  1.20.4.2  he 		break;
    880  1.20.4.2  he 	case 3:
    881  1.20.4.2  he 		for (i = 0; i < nsegs; i++) {
    882  1.20.4.2  he 			tc->tc_args.io.sgl[i].tsg_address =
    883  1.20.4.2  he 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    884  1.20.4.2  he 			tc->tc_args.io.sgl[i].tsg_length =
    885  1.20.4.2  he 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    886  1.20.4.2  he 		}
    887  1.20.4.2  he 		/* XXX Needed? */
    888  1.20.4.2  he 		for (; i < TWE_SG_SIZE; i++) {
    889  1.20.4.2  he 			tc->tc_args.io.sgl[i].tsg_address = 0;
    890  1.20.4.2  he 			tc->tc_args.io.sgl[i].tsg_length = 0;
    891  1.20.4.2  he 		}
    892  1.20.4.2  he 		break;
    893  1.20.4.2  he #ifdef DEBUG
    894  1.20.4.2  he 	default:
    895  1.20.4.2  he 		panic("twe_ccb_map: oops");
    896  1.20.4.2  he #endif
    897  1.20.4.2  he 	}
    898  1.20.4.2  he 
    899  1.20.4.2  he 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
    900  1.20.4.2  he 		flags = BUS_DMASYNC_PREREAD;
    901  1.20.4.2  he 	else
    902  1.20.4.2  he 		flags = 0;
    903  1.20.4.2  he 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
    904  1.20.4.2  he 		flags |= BUS_DMASYNC_PREWRITE;
    905  1.20.4.2  he 
    906  1.20.4.2  he 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    907  1.20.4.2  he 	    ccb->ccb_datasize, flags);
    908  1.20.4.2  he 	return (0);
    909  1.20.4.2  he }
    910  1.20.4.2  he 
    911  1.20.4.2  he /*
    912  1.20.4.2  he  * Unmap the specified CCB's command block and data buffer (if any) and
    913  1.20.4.2  he  * perform DMA synchronisation.
    914  1.20.4.2  he  */
    915  1.20.4.2  he void
    916  1.20.4.2  he twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
    917  1.20.4.2  he {
    918  1.20.4.2  he 	struct twe_buf *tb;
    919  1.20.4.2  he 	int flags;
    920  1.20.4.2  he 
    921  1.20.4.2  he 	if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
    922  1.20.4.2  he 		flags = BUS_DMASYNC_POSTREAD;
    923  1.20.4.2  he 	else
    924  1.20.4.2  he 		flags = 0;
    925  1.20.4.2  he 	if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
    926  1.20.4.2  he 		flags |= BUS_DMASYNC_POSTWRITE;
    927  1.20.4.2  he 
    928  1.20.4.2  he 	bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    929  1.20.4.2  he 	    ccb->ccb_datasize, flags);
    930  1.20.4.2  he 	bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
    931  1.20.4.2  he 
    932  1.20.4.2  he 	if (ccb->ccb_buf != NULL) {
    933  1.20.4.2  he 		tb = ccb->ccb_buf;
    934  1.20.4.2  he 
    935  1.20.4.2  he 		if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
    936  1.20.4.2  he 			memcpy(ccb->ccb_data, tb->tb_vaddr,
    937  1.20.4.2  he 			    ccb->ccb_datasize);
    938  1.20.4.2  he 
    939  1.20.4.2  he 		bus_dmamem_unmap(sc->sc_dmat, tb->tb_vaddr, ccb->ccb_datasize);
    940  1.20.4.2  he 		bus_dmamem_free(sc->sc_dmat, tb->tb_segs, tb->tb_rseg);
    941  1.20.4.2  he 	}
    942  1.20.4.2  he }
    943  1.20.4.2  he 
    944  1.20.4.2  he /*
    945  1.20.4.2  he  * Submit a command to the controller and poll on completion.  Return
    946  1.20.4.2  he  * non-zero on timeout (but don't check status, as some command types don't
    947  1.20.4.2  he  * return status).  Must be called with interrupts blocked.
    948  1.20.4.2  he  */
    949  1.20.4.2  he int
    950  1.20.4.2  he twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
    951  1.20.4.2  he {
    952  1.20.4.2  he 	int rv;
    953  1.20.4.2  he 
    954  1.20.4.2  he 	if ((rv = twe_ccb_submit(sc, ccb)) != 0)
    955  1.20.4.2  he 		return (rv);
    956  1.20.4.2  he 
    957  1.20.4.2  he 	for (timo *= 1000; timo != 0; timo--) {
    958  1.20.4.2  he 		twe_poll(sc);
    959  1.20.4.2  he 		if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
    960  1.20.4.2  he 			break;
    961  1.20.4.2  he 		DELAY(100);
    962  1.20.4.2  he 	}
    963  1.20.4.2  he 
    964  1.20.4.2  he 	return (timo == 0);
    965  1.20.4.2  he }
    966  1.20.4.2  he 
    967  1.20.4.2  he /*
    968  1.20.4.2  he  * If a CCB is specified, enqueue it.  Pull CCBs off the software queue in
    969  1.20.4.2  he  * the order that they were enqueued and try to submit their command blocks
    970  1.20.4.2  he  * to the controller for execution.
    971  1.20.4.2  he  */
    972  1.20.4.2  he void
    973  1.20.4.2  he twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
    974  1.20.4.2  he {
    975  1.20.4.2  he 	int s;
    976  1.20.4.2  he 
    977  1.20.4.2  he 	s = splbio();
    978  1.20.4.2  he 
    979  1.20.4.2  he 	if (ccb != NULL)
    980  1.20.4.2  he 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
    981  1.20.4.2  he 
    982  1.20.4.2  he 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
    983  1.20.4.2  he 		if (twe_ccb_submit(sc, ccb))
    984  1.20.4.2  he 			break;
    985  1.20.4.2  he 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
    986  1.20.4.2  he 	}
    987  1.20.4.2  he 
    988  1.20.4.2  he 	splx(s);
    989  1.20.4.2  he }
    990  1.20.4.2  he 
    991  1.20.4.2  he /*
    992  1.20.4.2  he  * Submit the command block associated with the specified CCB to the
    993  1.20.4.2  he  * controller for execution.  Must be called with interrupts blocked.
    994  1.20.4.2  he  */
    995  1.20.4.2  he int
    996  1.20.4.2  he twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
    997  1.20.4.2  he {
    998  1.20.4.2  he 	bus_addr_t pa;
    999  1.20.4.2  he 	int rv;
   1000  1.20.4.2  he 	u_int status;
   1001  1.20.4.2  he 
   1002  1.20.4.2  he 	/* Check to see if we can post a command. */
   1003  1.20.4.2  he 	status = TWE_INL(sc, TWE_REG_STS);
   1004  1.20.4.2  he 	twe_status_check(sc, status);
   1005  1.20.4.2  he 
   1006  1.20.4.2  he 	if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
   1007  1.20.4.2  he 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
   1008  1.20.4.2  he 		    (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
   1009  1.20.4.2  he 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
   1010  1.20.4.2  he 		ccb->ccb_flags |= TWE_CCB_ACTIVE;
   1011  1.20.4.2  he 		pa = sc->sc_cmds_paddr +
   1012  1.20.4.2  he 		    ccb->ccb_cmdid * sizeof(struct twe_cmd);
   1013  1.20.4.2  he 		TWE_OUTL(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
   1014  1.20.4.2  he 		rv = 0;
   1015  1.20.4.2  he 	} else
   1016  1.20.4.2  he 		rv = EBUSY;
   1017  1.20.4.2  he 
   1018  1.20.4.2  he 	return (rv);
   1019  1.20.4.2  he }
   1020