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