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