Home | History | Annotate | Line # | Download | only in ic
cac.c revision 1.15.2.4
      1  1.15.2.4  bouyer /*	$NetBSD: cac.c,v 1.15.2.4 2000/12/13 15:50:01 bouyer Exp $	*/
      2  1.15.2.2  bouyer 
      3  1.15.2.2  bouyer /*-
      4  1.15.2.2  bouyer  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.15.2.2  bouyer  * All rights reserved.
      6  1.15.2.2  bouyer  *
      7  1.15.2.2  bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8  1.15.2.2  bouyer  * by Andrew Doran.
      9  1.15.2.2  bouyer  *
     10  1.15.2.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.15.2.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.15.2.2  bouyer  * are met:
     13  1.15.2.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.15.2.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.15.2.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.15.2.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.15.2.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.15.2.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     19  1.15.2.2  bouyer  *    must display the following acknowledgement:
     20  1.15.2.2  bouyer  *        This product includes software developed by the NetBSD
     21  1.15.2.2  bouyer  *        Foundation, Inc. and its contributors.
     22  1.15.2.2  bouyer  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.15.2.2  bouyer  *    contributors may be used to endorse or promote products derived
     24  1.15.2.2  bouyer  *    from this software without specific prior written permission.
     25  1.15.2.2  bouyer  *
     26  1.15.2.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.15.2.2  bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.15.2.2  bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.15.2.2  bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.15.2.2  bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.15.2.2  bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.15.2.2  bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.15.2.2  bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.15.2.2  bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.15.2.2  bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.15.2.2  bouyer  * POSSIBILITY OF SUCH DAMAGE.
     37  1.15.2.2  bouyer  */
     38  1.15.2.2  bouyer 
     39  1.15.2.2  bouyer /*
     40  1.15.2.2  bouyer  * Driver for Compaq array controllers.
     41  1.15.2.2  bouyer  */
     42  1.15.2.2  bouyer 
     43  1.15.2.2  bouyer #include <sys/param.h>
     44  1.15.2.2  bouyer #include <sys/systm.h>
     45  1.15.2.2  bouyer #include <sys/kernel.h>
     46  1.15.2.2  bouyer #include <sys/device.h>
     47  1.15.2.2  bouyer #include <sys/queue.h>
     48  1.15.2.2  bouyer #include <sys/proc.h>
     49  1.15.2.2  bouyer #include <sys/buf.h>
     50  1.15.2.2  bouyer #include <sys/endian.h>
     51  1.15.2.2  bouyer #include <sys/malloc.h>
     52  1.15.2.2  bouyer #include <sys/pool.h>
     53  1.15.2.2  bouyer 
     54  1.15.2.3  bouyer #include <uvm/uvm_extern.h>
     55  1.15.2.3  bouyer 
     56  1.15.2.2  bouyer #include <machine/bswap.h>
     57  1.15.2.2  bouyer #include <machine/bus.h>
     58  1.15.2.2  bouyer 
     59  1.15.2.2  bouyer #include <dev/ic/cacreg.h>
     60  1.15.2.2  bouyer #include <dev/ic/cacvar.h>
     61  1.15.2.2  bouyer 
     62  1.15.2.2  bouyer static struct	cac_ccb *cac_ccb_alloc(struct cac_softc *, int);
     63  1.15.2.2  bouyer static void	cac_ccb_done(struct cac_softc *, struct cac_ccb *);
     64  1.15.2.2  bouyer static void	cac_ccb_free(struct cac_softc *, struct cac_ccb *);
     65  1.15.2.2  bouyer static int	cac_ccb_poll(struct cac_softc *, struct cac_ccb *, int);
     66  1.15.2.2  bouyer static int	cac_ccb_start(struct cac_softc *, struct cac_ccb *);
     67  1.15.2.2  bouyer static int	cac_print(void *, const char *);
     68  1.15.2.2  bouyer static void	cac_shutdown(void *);
     69  1.15.2.2  bouyer static int	cac_submatch(struct device *, struct cfdata *, void *);
     70  1.15.2.2  bouyer 
     71  1.15.2.2  bouyer static struct	cac_ccb *cac_l0_completed(struct cac_softc *);
     72  1.15.2.2  bouyer static int	cac_l0_fifo_full(struct cac_softc *);
     73  1.15.2.2  bouyer static void	cac_l0_intr_enable(struct cac_softc *, int);
     74  1.15.2.2  bouyer static int	cac_l0_intr_pending(struct cac_softc *);
     75  1.15.2.2  bouyer static void	cac_l0_submit(struct cac_softc *, struct cac_ccb *);
     76  1.15.2.2  bouyer 
     77  1.15.2.2  bouyer static void	*cac_sdh;	/* shutdown hook */
     78  1.15.2.2  bouyer 
     79  1.15.2.2  bouyer struct cac_linkage cac_l0 = {
     80  1.15.2.2  bouyer 	cac_l0_completed,
     81  1.15.2.2  bouyer 	cac_l0_fifo_full,
     82  1.15.2.2  bouyer 	cac_l0_intr_enable,
     83  1.15.2.2  bouyer 	cac_l0_intr_pending,
     84  1.15.2.2  bouyer 	cac_l0_submit
     85  1.15.2.2  bouyer };
     86  1.15.2.2  bouyer 
     87  1.15.2.2  bouyer /*
     88  1.15.2.2  bouyer  * Initialise our interface to the controller.
     89  1.15.2.2  bouyer  */
     90  1.15.2.2  bouyer int
     91  1.15.2.2  bouyer cac_init(struct cac_softc *sc, const char *intrstr, int startfw)
     92  1.15.2.2  bouyer {
     93  1.15.2.2  bouyer 	struct cac_controller_info cinfo;
     94  1.15.2.2  bouyer 	struct cac_attach_args caca;
     95  1.15.2.2  bouyer 	int error, rseg, size, i;
     96  1.15.2.2  bouyer 	bus_dma_segment_t seg;
     97  1.15.2.2  bouyer 	struct cac_ccb *ccb;
     98  1.15.2.2  bouyer 
     99  1.15.2.2  bouyer 	if (intrstr != NULL)
    100  1.15.2.2  bouyer 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
    101  1.15.2.2  bouyer 		    intrstr);
    102  1.15.2.2  bouyer 
    103  1.15.2.2  bouyer 	SIMPLEQ_INIT(&sc->sc_ccb_free);
    104  1.15.2.2  bouyer 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    105  1.15.2.2  bouyer 
    106  1.15.2.2  bouyer         size = sizeof(struct cac_ccb) * CAC_MAX_CCBS;
    107  1.15.2.2  bouyer 
    108  1.15.2.3  bouyer 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
    109  1.15.2.2  bouyer 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    110  1.15.2.2  bouyer 		printf("%s: unable to allocate CCBs, error = %d\n",
    111  1.15.2.2  bouyer 		    sc->sc_dv.dv_xname, error);
    112  1.15.2.2  bouyer 		return (-1);
    113  1.15.2.2  bouyer 	}
    114  1.15.2.2  bouyer 
    115  1.15.2.2  bouyer 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    116  1.15.2.2  bouyer 	    (caddr_t *)&sc->sc_ccbs,
    117  1.15.2.2  bouyer 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    118  1.15.2.2  bouyer 		printf("%s: unable to map CCBs, error = %d\n",
    119  1.15.2.2  bouyer 		    sc->sc_dv.dv_xname, error);
    120  1.15.2.2  bouyer 		return (-1);
    121  1.15.2.2  bouyer 	}
    122  1.15.2.2  bouyer 
    123  1.15.2.2  bouyer 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    124  1.15.2.2  bouyer 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    125  1.15.2.2  bouyer 		printf("%s: unable to create CCB DMA map, error = %d\n",
    126  1.15.2.2  bouyer 		    sc->sc_dv.dv_xname, error);
    127  1.15.2.2  bouyer 		return (-1);
    128  1.15.2.2  bouyer 	}
    129  1.15.2.2  bouyer 
    130  1.15.2.2  bouyer 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ccbs,
    131  1.15.2.2  bouyer 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    132  1.15.2.2  bouyer 		printf("%s: unable to load CCB DMA map, error = %d\n",
    133  1.15.2.2  bouyer 		    sc->sc_dv.dv_xname, error);
    134  1.15.2.2  bouyer 		return (-1);
    135  1.15.2.2  bouyer 	}
    136  1.15.2.2  bouyer 
    137  1.15.2.2  bouyer 	sc->sc_ccbs_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    138  1.15.2.2  bouyer 	memset(sc->sc_ccbs, 0, size);
    139  1.15.2.2  bouyer 	ccb = (struct cac_ccb *)sc->sc_ccbs;
    140  1.15.2.2  bouyer 
    141  1.15.2.2  bouyer 	for (i = 0; i < CAC_MAX_CCBS; i++, ccb++) {
    142  1.15.2.2  bouyer 		/* Create the DMA map for this CCB's data */
    143  1.15.2.2  bouyer 		error = bus_dmamap_create(sc->sc_dmat, CAC_MAX_XFER,
    144  1.15.2.2  bouyer 		    CAC_SG_SIZE, CAC_MAX_XFER, 0,
    145  1.15.2.2  bouyer 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    146  1.15.2.2  bouyer 		    &ccb->ccb_dmamap_xfer);
    147  1.15.2.2  bouyer 
    148  1.15.2.2  bouyer 		if (error) {
    149  1.15.2.2  bouyer 			printf("%s: can't create ccb dmamap (%d)\n",
    150  1.15.2.2  bouyer 			    sc->sc_dv.dv_xname, error);
    151  1.15.2.2  bouyer 			break;
    152  1.15.2.2  bouyer 		}
    153  1.15.2.2  bouyer 
    154  1.15.2.2  bouyer 		ccb->ccb_flags = 0;
    155  1.15.2.2  bouyer 		ccb->ccb_paddr = sc->sc_ccbs_paddr + i * sizeof(struct cac_ccb);
    156  1.15.2.2  bouyer 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_chain);
    157  1.15.2.2  bouyer 	}
    158  1.15.2.2  bouyer 
    159  1.15.2.2  bouyer 	/* Start firmware background tasks, if needed. */
    160  1.15.2.2  bouyer 	if (startfw) {
    161  1.15.2.2  bouyer 		if (cac_cmd(sc, CAC_CMD_START_FIRMWARE, &cinfo, sizeof(cinfo),
    162  1.15.2.2  bouyer 		    0, 0, CAC_CCB_DATA_IN, NULL)) {
    163  1.15.2.2  bouyer 			printf("%s: CAC_CMD_START_FIRMWARE failed\n",
    164  1.15.2.2  bouyer 			    sc->sc_dv.dv_xname);
    165  1.15.2.2  bouyer 			return (-1);
    166  1.15.2.2  bouyer 		}
    167  1.15.2.2  bouyer 	}
    168  1.15.2.2  bouyer 
    169  1.15.2.2  bouyer 	if (cac_cmd(sc, CAC_CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 0, 0,
    170  1.15.2.2  bouyer 	    CAC_CCB_DATA_IN, NULL)) {
    171  1.15.2.2  bouyer 		printf("%s: CAC_CMD_GET_CTRL_INFO failed\n",
    172  1.15.2.2  bouyer 		    sc->sc_dv.dv_xname);
    173  1.15.2.2  bouyer 		return (-1);
    174  1.15.2.2  bouyer 	}
    175  1.15.2.2  bouyer 
    176  1.15.2.3  bouyer 	sc->sc_nunits = cinfo.num_drvs;
    177  1.15.2.2  bouyer 	for (i = 0; i < cinfo.num_drvs; i++) {
    178  1.15.2.2  bouyer 		caca.caca_unit = i;
    179  1.15.2.2  bouyer 		config_found_sm(&sc->sc_dv, &caca, cac_print, cac_submatch);
    180  1.15.2.2  bouyer 	}
    181  1.15.2.2  bouyer 
    182  1.15.2.2  bouyer 	/* Set our `shutdownhook' before we start any device activity. */
    183  1.15.2.2  bouyer 	if (cac_sdh == NULL)
    184  1.15.2.2  bouyer 		cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
    185  1.15.2.2  bouyer 
    186  1.15.2.2  bouyer 	(*sc->sc_cl->cl_intr_enable)(sc, CAC_INTR_ENABLE);
    187  1.15.2.2  bouyer 	return (0);
    188  1.15.2.2  bouyer }
    189  1.15.2.2  bouyer 
    190  1.15.2.2  bouyer /*
    191  1.15.2.2  bouyer  * Shut down all `cac' controllers.
    192  1.15.2.2  bouyer  */
    193  1.15.2.2  bouyer static void
    194  1.15.2.2  bouyer cac_shutdown(void *cookie)
    195  1.15.2.2  bouyer {
    196  1.15.2.2  bouyer 	extern struct cfdriver cac_cd;
    197  1.15.2.2  bouyer 	struct cac_softc *sc;
    198  1.15.2.2  bouyer 	u_int8_t buf[512];
    199  1.15.2.2  bouyer 	int i;
    200  1.15.2.2  bouyer 
    201  1.15.2.2  bouyer 	for (i = 0; i < cac_cd.cd_ndevs; i++) {
    202  1.15.2.2  bouyer 		if ((sc = device_lookup(&cac_cd, i)) == NULL)
    203  1.15.2.2  bouyer 			continue;
    204  1.15.2.2  bouyer 		memset(buf, 0, sizeof(buf));
    205  1.15.2.2  bouyer 		buf[0] = 1;
    206  1.15.2.2  bouyer 		cac_cmd(sc, CAC_CMD_FLUSH_CACHE, buf, sizeof(buf), 0, 0,
    207  1.15.2.2  bouyer 		    CAC_CCB_DATA_OUT, NULL);
    208  1.15.2.2  bouyer 	}
    209  1.15.2.2  bouyer }
    210  1.15.2.2  bouyer 
    211  1.15.2.2  bouyer /*
    212  1.15.2.2  bouyer  * Print autoconfiguration message for a sub-device.
    213  1.15.2.2  bouyer  */
    214  1.15.2.2  bouyer static int
    215  1.15.2.2  bouyer cac_print(void *aux, const char *pnp)
    216  1.15.2.2  bouyer {
    217  1.15.2.2  bouyer 	struct cac_attach_args *caca;
    218  1.15.2.2  bouyer 
    219  1.15.2.2  bouyer 	caca = (struct cac_attach_args *)aux;
    220  1.15.2.2  bouyer 
    221  1.15.2.2  bouyer 	if (pnp != NULL)
    222  1.15.2.2  bouyer 		printf("block device at %s", pnp);
    223  1.15.2.2  bouyer 	printf(" unit %d", caca->caca_unit);
    224  1.15.2.2  bouyer 	return (UNCONF);
    225  1.15.2.2  bouyer }
    226  1.15.2.2  bouyer 
    227  1.15.2.2  bouyer /*
    228  1.15.2.2  bouyer  * Match a sub-device.
    229  1.15.2.2  bouyer  */
    230  1.15.2.2  bouyer static int
    231  1.15.2.2  bouyer cac_submatch(struct device *parent, struct cfdata *cf, void *aux)
    232  1.15.2.2  bouyer {
    233  1.15.2.2  bouyer 	struct cac_attach_args *caca;
    234  1.15.2.2  bouyer 
    235  1.15.2.2  bouyer 	caca = (struct cac_attach_args *)aux;
    236  1.15.2.2  bouyer 
    237  1.15.2.2  bouyer 	if (cf->cacacf_unit != CACCF_UNIT_DEFAULT &&
    238  1.15.2.2  bouyer 	    cf->cacacf_unit != caca->caca_unit)
    239  1.15.2.2  bouyer 		return (0);
    240  1.15.2.2  bouyer 
    241  1.15.2.2  bouyer 	return (cf->cf_attach->ca_match(parent, cf, aux));
    242  1.15.2.2  bouyer }
    243  1.15.2.2  bouyer 
    244  1.15.2.2  bouyer /*
    245  1.15.2.2  bouyer  * Handle an interrupt from the controller: process finished CCBs and
    246  1.15.2.2  bouyer  * dequeue any waiting CCBs.
    247  1.15.2.2  bouyer  */
    248  1.15.2.2  bouyer int
    249  1.15.2.2  bouyer cac_intr(void *cookie)
    250  1.15.2.2  bouyer {
    251  1.15.2.2  bouyer 	struct cac_softc *sc;
    252  1.15.2.2  bouyer 	struct cac_ccb *ccb;
    253  1.15.2.2  bouyer 
    254  1.15.2.2  bouyer 	sc = (struct cac_softc *)cookie;
    255  1.15.2.2  bouyer 
    256  1.15.2.2  bouyer 	if (!(*sc->sc_cl->cl_intr_pending)(sc)) {
    257  1.15.2.2  bouyer #ifdef DEBUG
    258  1.15.2.2  bouyer 		printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
    259  1.15.2.2  bouyer #endif
    260  1.15.2.2  bouyer 		return (0);
    261  1.15.2.2  bouyer 	}
    262  1.15.2.2  bouyer 
    263  1.15.2.2  bouyer 	while ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL) {
    264  1.15.2.2  bouyer 		cac_ccb_done(sc, ccb);
    265  1.15.2.2  bouyer 		cac_ccb_start(sc, NULL);
    266  1.15.2.2  bouyer 	}
    267  1.15.2.2  bouyer 
    268  1.15.2.2  bouyer 	return (1);
    269  1.15.2.2  bouyer }
    270  1.15.2.2  bouyer 
    271  1.15.2.2  bouyer /*
    272  1.15.2.2  bouyer  * Execute a [polled] command.
    273  1.15.2.2  bouyer  */
    274  1.15.2.2  bouyer int
    275  1.15.2.2  bouyer cac_cmd(struct cac_softc *sc, int command, void *data, int datasize,
    276  1.15.2.2  bouyer 	int drive, int blkno, int flags, struct cac_context *context)
    277  1.15.2.2  bouyer {
    278  1.15.2.2  bouyer 	struct cac_ccb *ccb;
    279  1.15.2.2  bouyer 	struct cac_sgb *sgb;
    280  1.15.2.2  bouyer 	int s, i, rv, size, nsegs;
    281  1.15.2.2  bouyer 
    282  1.15.2.2  bouyer 	size = 0;
    283  1.15.2.2  bouyer 
    284  1.15.2.2  bouyer 	if ((ccb = cac_ccb_alloc(sc, 0)) == NULL) {
    285  1.15.2.2  bouyer 		printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
    286  1.15.2.2  bouyer 		return (ENOMEM);
    287  1.15.2.2  bouyer 	}
    288  1.15.2.2  bouyer 
    289  1.15.2.2  bouyer 	if ((flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
    290  1.15.2.2  bouyer 		bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer,
    291  1.15.2.2  bouyer 		    (void *)data, datasize, NULL, BUS_DMA_NOWAIT);
    292  1.15.2.2  bouyer 
    293  1.15.2.2  bouyer 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0, datasize,
    294  1.15.2.2  bouyer 		    (flags & CAC_CCB_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
    295  1.15.2.2  bouyer 		    BUS_DMASYNC_PREWRITE);
    296  1.15.2.2  bouyer 
    297  1.15.2.2  bouyer 		sgb = ccb->ccb_seg;
    298  1.15.2.2  bouyer 		nsegs = min(ccb->ccb_dmamap_xfer->dm_nsegs, CAC_SG_SIZE);
    299  1.15.2.2  bouyer 
    300  1.15.2.2  bouyer 		for (i = 0; i < nsegs; i++, sgb++) {
    301  1.15.2.2  bouyer 			size += ccb->ccb_dmamap_xfer->dm_segs[i].ds_len;
    302  1.15.2.2  bouyer 			sgb->length =
    303  1.15.2.2  bouyer 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    304  1.15.2.2  bouyer 			sgb->addr =
    305  1.15.2.2  bouyer 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    306  1.15.2.2  bouyer 		}
    307  1.15.2.2  bouyer 	} else {
    308  1.15.2.2  bouyer 		size = datasize;
    309  1.15.2.2  bouyer 		nsegs = 0;
    310  1.15.2.2  bouyer 	}
    311  1.15.2.2  bouyer 
    312  1.15.2.2  bouyer 	ccb->ccb_hdr.drive = drive;
    313  1.15.2.2  bouyer 	ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
    314  1.15.2.2  bouyer 	    sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
    315  1.15.2.2  bouyer 
    316  1.15.2.2  bouyer 	ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
    317  1.15.2.2  bouyer 	ccb->ccb_req.command = command;
    318  1.15.2.2  bouyer 	ccb->ccb_req.sgcount = nsegs;
    319  1.15.2.2  bouyer 	ccb->ccb_req.blkno = htole32(blkno);
    320  1.15.2.2  bouyer 
    321  1.15.2.2  bouyer 	ccb->ccb_flags = flags;
    322  1.15.2.2  bouyer 	ccb->ccb_datasize = size;
    323  1.15.2.2  bouyer 
    324  1.15.2.2  bouyer 	if (context == NULL) {
    325  1.15.2.2  bouyer 		memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
    326  1.15.2.2  bouyer 		s = splbio();
    327  1.15.2.2  bouyer 
    328  1.15.2.2  bouyer 		/* Synchronous commands musn't wait. */
    329  1.15.2.2  bouyer 		if ((*sc->sc_cl->cl_fifo_full)(sc)) {
    330  1.15.2.2  bouyer 			cac_ccb_free(sc, ccb);
    331  1.15.2.2  bouyer 			rv = -1;
    332  1.15.2.2  bouyer 		} else {
    333  1.15.2.2  bouyer #ifdef DIAGNOSTIC
    334  1.15.2.2  bouyer 			ccb->ccb_flags |= CAC_CCB_ACTIVE;
    335  1.15.2.2  bouyer #endif
    336  1.15.2.2  bouyer 			(*sc->sc_cl->cl_submit)(sc, ccb);
    337  1.15.2.2  bouyer 			rv = cac_ccb_poll(sc, ccb, 2000);
    338  1.15.2.2  bouyer 			cac_ccb_free(sc, ccb);
    339  1.15.2.2  bouyer 		}
    340  1.15.2.2  bouyer 	} else {
    341  1.15.2.2  bouyer 		memcpy(&ccb->ccb_context, context, sizeof(struct cac_context));
    342  1.15.2.2  bouyer 		s = splbio();
    343  1.15.2.2  bouyer 		rv = cac_ccb_start(sc, ccb);
    344  1.15.2.2  bouyer 	}
    345  1.15.2.2  bouyer 
    346  1.15.2.2  bouyer 	splx(s);
    347  1.15.2.2  bouyer 	return (rv);
    348  1.15.2.2  bouyer }
    349  1.15.2.2  bouyer 
    350  1.15.2.2  bouyer /*
    351  1.15.2.2  bouyer  * Wait for the specified CCB to complete.  Must be called at splbio.
    352  1.15.2.2  bouyer  */
    353  1.15.2.2  bouyer static int
    354  1.15.2.2  bouyer cac_ccb_poll(struct cac_softc *sc, struct cac_ccb *wantccb, int timo)
    355  1.15.2.2  bouyer {
    356  1.15.2.2  bouyer 	struct cac_ccb *ccb;
    357  1.15.2.2  bouyer 
    358  1.15.2.2  bouyer 	timo *= 10;
    359  1.15.2.2  bouyer 
    360  1.15.2.2  bouyer 	do {
    361  1.15.2.2  bouyer 		for (; timo != 0; timo--) {
    362  1.15.2.2  bouyer 			if ((ccb = (*sc->sc_cl->cl_completed)(sc)) != NULL)
    363  1.15.2.2  bouyer 				break;
    364  1.15.2.2  bouyer 			DELAY(100);
    365  1.15.2.2  bouyer 		}
    366  1.15.2.2  bouyer 
    367  1.15.2.2  bouyer 		if (timo == 0) {
    368  1.15.2.2  bouyer 			printf("%s: timeout", sc->sc_dv.dv_xname);
    369  1.15.2.2  bouyer 			return (EBUSY);
    370  1.15.2.2  bouyer 		}
    371  1.15.2.2  bouyer 		cac_ccb_done(sc, ccb);
    372  1.15.2.2  bouyer 	} while (ccb != wantccb);
    373  1.15.2.2  bouyer 
    374  1.15.2.2  bouyer 	return (0);
    375  1.15.2.2  bouyer }
    376  1.15.2.2  bouyer 
    377  1.15.2.2  bouyer /*
    378  1.15.2.2  bouyer  * Enqueue the specifed command (if any) and attempt to start all enqueued
    379  1.15.2.2  bouyer  * commands.  Must be called at splbio.
    380  1.15.2.2  bouyer  */
    381  1.15.2.2  bouyer static int
    382  1.15.2.2  bouyer cac_ccb_start(struct cac_softc *sc, struct cac_ccb *ccb)
    383  1.15.2.2  bouyer {
    384  1.15.2.2  bouyer 
    385  1.15.2.2  bouyer 	if (ccb != NULL)
    386  1.15.2.2  bouyer 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain);
    387  1.15.2.2  bouyer 
    388  1.15.2.2  bouyer 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
    389  1.15.2.2  bouyer 		if ((*sc->sc_cl->cl_fifo_full)(sc))
    390  1.15.2.2  bouyer 			return (EBUSY);
    391  1.15.2.2  bouyer 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain);
    392  1.15.2.2  bouyer #ifdef DIAGNOSTIC
    393  1.15.2.2  bouyer 		ccb->ccb_flags |= CAC_CCB_ACTIVE;
    394  1.15.2.2  bouyer #endif
    395  1.15.2.2  bouyer 		(*sc->sc_cl->cl_submit)(sc, ccb);
    396  1.15.2.2  bouyer 	}
    397  1.15.2.2  bouyer 
    398  1.15.2.2  bouyer 	return (0);
    399  1.15.2.2  bouyer }
    400  1.15.2.2  bouyer 
    401  1.15.2.2  bouyer /*
    402  1.15.2.2  bouyer  * Process a finished CCB.
    403  1.15.2.2  bouyer  */
    404  1.15.2.2  bouyer static void
    405  1.15.2.2  bouyer cac_ccb_done(struct cac_softc *sc, struct cac_ccb *ccb)
    406  1.15.2.2  bouyer {
    407  1.15.2.3  bouyer 	struct device *dv;
    408  1.15.2.3  bouyer 	void *context;
    409  1.15.2.2  bouyer 	int error;
    410  1.15.2.2  bouyer 
    411  1.15.2.2  bouyer 	error = 0;
    412  1.15.2.2  bouyer 
    413  1.15.2.2  bouyer #ifdef DIAGNOSTIC
    414  1.15.2.2  bouyer 	if ((ccb->ccb_flags & CAC_CCB_ACTIVE) == 0)
    415  1.15.2.2  bouyer 		panic("cac_ccb_done: CCB not active");
    416  1.15.2.2  bouyer 	ccb->ccb_flags &= ~CAC_CCB_ACTIVE;
    417  1.15.2.2  bouyer #endif
    418  1.15.2.2  bouyer 
    419  1.15.2.2  bouyer 	if ((ccb->ccb_flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
    420  1.15.2.2  bouyer 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    421  1.15.2.2  bouyer 		    ccb->ccb_datasize, ccb->ccb_flags & CAC_CCB_DATA_IN ?
    422  1.15.2.2  bouyer 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    423  1.15.2.2  bouyer 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
    424  1.15.2.2  bouyer 	}
    425  1.15.2.2  bouyer 
    426  1.15.2.4  bouyer 	error = ccb->ccb_req.error;
    427  1.15.2.2  bouyer 	if (ccb->ccb_context.cc_handler != NULL) {
    428  1.15.2.3  bouyer 		dv = ccb->ccb_context.cc_dv;
    429  1.15.2.3  bouyer 		context = ccb->ccb_context.cc_context;
    430  1.15.2.2  bouyer 		cac_ccb_free(sc, ccb);
    431  1.15.2.3  bouyer 		(*ccb->ccb_context.cc_handler)(dv, context, error);
    432  1.15.2.4  bouyer 	} else {
    433  1.15.2.4  bouyer 		if ((error & CAC_RET_SOFT_ERROR) != 0)
    434  1.15.2.4  bouyer 			printf("%s: soft error; array may be degraded\n",
    435  1.15.2.4  bouyer 			    sc->sc_dv.dv_xname);
    436  1.15.2.4  bouyer 		if ((error & CAC_RET_HARD_ERROR) != 0)
    437  1.15.2.4  bouyer 			printf("%s: hard error\n", sc->sc_dv.dv_xname);
    438  1.15.2.4  bouyer 		if ((error & CAC_RET_CMD_REJECTED) != 0) {
    439  1.15.2.4  bouyer 			error = 1;
    440  1.15.2.4  bouyer 			printf("%s: invalid request\n", sc->sc_dv.dv_xname);
    441  1.15.2.4  bouyer 		}
    442  1.15.2.2  bouyer 	}
    443  1.15.2.2  bouyer }
    444  1.15.2.2  bouyer 
    445  1.15.2.2  bouyer /*
    446  1.15.2.2  bouyer  * Allocate a CCB.
    447  1.15.2.2  bouyer  */
    448  1.15.2.2  bouyer struct cac_ccb *
    449  1.15.2.2  bouyer cac_ccb_alloc(struct cac_softc *sc, int nosleep)
    450  1.15.2.2  bouyer {
    451  1.15.2.2  bouyer 	struct cac_ccb *ccb;
    452  1.15.2.2  bouyer 	int s;
    453  1.15.2.2  bouyer 
    454  1.15.2.2  bouyer 	s = splbio();
    455  1.15.2.2  bouyer 
    456  1.15.2.2  bouyer 	for (;;) {
    457  1.15.2.2  bouyer 		if ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) != NULL) {
    458  1.15.2.2  bouyer 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
    459  1.15.2.2  bouyer 			break;
    460  1.15.2.2  bouyer 		}
    461  1.15.2.2  bouyer 		if (nosleep) {
    462  1.15.2.2  bouyer 			ccb = NULL;
    463  1.15.2.2  bouyer 			break;
    464  1.15.2.2  bouyer 		}
    465  1.15.2.2  bouyer 		tsleep(&sc->sc_ccb_free, PRIBIO, "cacccb", 0);
    466  1.15.2.2  bouyer 	}
    467  1.15.2.2  bouyer 
    468  1.15.2.2  bouyer 	splx(s);
    469  1.15.2.2  bouyer 	return (ccb);
    470  1.15.2.2  bouyer }
    471  1.15.2.2  bouyer 
    472  1.15.2.2  bouyer /*
    473  1.15.2.2  bouyer  * Put a CCB onto the freelist.
    474  1.15.2.2  bouyer  */
    475  1.15.2.2  bouyer void
    476  1.15.2.2  bouyer cac_ccb_free(struct cac_softc *sc, struct cac_ccb *ccb)
    477  1.15.2.2  bouyer {
    478  1.15.2.2  bouyer 	int s;
    479  1.15.2.2  bouyer 
    480  1.15.2.2  bouyer 	ccb->ccb_flags = 0;
    481  1.15.2.2  bouyer 	s = splbio();
    482  1.15.2.2  bouyer 	SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
    483  1.15.2.2  bouyer 	if (SIMPLEQ_NEXT(ccb, ccb_chain) == NULL)
    484  1.15.2.2  bouyer 		wakeup_one(&sc->sc_ccb_free);
    485  1.15.2.2  bouyer 	splx(s);
    486  1.15.2.2  bouyer }
    487  1.15.2.2  bouyer 
    488  1.15.2.2  bouyer /*
    489  1.15.2.2  bouyer  * Board specific linkage shared between multiple bus types.
    490  1.15.2.2  bouyer  */
    491  1.15.2.2  bouyer 
    492  1.15.2.2  bouyer static int
    493  1.15.2.2  bouyer cac_l0_fifo_full(struct cac_softc *sc)
    494  1.15.2.2  bouyer {
    495  1.15.2.2  bouyer 
    496  1.15.2.2  bouyer 	return (cac_inl(sc, CAC_REG_CMD_FIFO) == 0);
    497  1.15.2.2  bouyer }
    498  1.15.2.2  bouyer 
    499  1.15.2.2  bouyer static void
    500  1.15.2.2  bouyer cac_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
    501  1.15.2.2  bouyer {
    502  1.15.2.2  bouyer 
    503  1.15.2.2  bouyer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
    504  1.15.2.2  bouyer 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    505  1.15.2.2  bouyer 	cac_outl(sc, CAC_REG_CMD_FIFO, ccb->ccb_paddr);
    506  1.15.2.2  bouyer }
    507  1.15.2.2  bouyer 
    508  1.15.2.2  bouyer static struct cac_ccb *
    509  1.15.2.2  bouyer cac_l0_completed(struct cac_softc *sc)
    510  1.15.2.2  bouyer {
    511  1.15.2.2  bouyer 	struct cac_ccb *ccb;
    512  1.15.2.2  bouyer 	paddr_t off;
    513  1.15.2.2  bouyer 
    514  1.15.2.2  bouyer 	off = (cac_inl(sc, CAC_REG_DONE_FIFO) & ~3) - sc->sc_ccbs_paddr;
    515  1.15.2.2  bouyer 	ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
    516  1.15.2.2  bouyer 
    517  1.15.2.2  bouyer 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
    518  1.15.2.2  bouyer 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    519  1.15.2.2  bouyer 
    520  1.15.2.2  bouyer 	return (ccb);
    521  1.15.2.2  bouyer }
    522  1.15.2.2  bouyer 
    523  1.15.2.2  bouyer static int
    524  1.15.2.2  bouyer cac_l0_intr_pending(struct cac_softc *sc)
    525  1.15.2.2  bouyer {
    526  1.15.2.2  bouyer 
    527  1.15.2.2  bouyer 	return (cac_inl(sc, CAC_REG_INTR_PENDING));
    528  1.15.2.2  bouyer }
    529  1.15.2.2  bouyer 
    530  1.15.2.2  bouyer static void
    531  1.15.2.2  bouyer cac_l0_intr_enable(struct cac_softc *sc, int state)
    532  1.15.2.2  bouyer {
    533  1.15.2.2  bouyer 
    534  1.15.2.2  bouyer 	cac_outl(sc, CAC_REG_INTR_MASK,
    535  1.15.2.2  bouyer 	    state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
    536  1.15.2.2  bouyer }
    537