cac.c revision 1.6.2.4       1  1.6.2.4       he /*	$NetBSD: cac.c,v 1.6.2.4 2002/01/29 23:31:01 he 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.6       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  * Driver for Compaq array controllers.
     41      1.1       ad  */
     42      1.1       ad 
     43      1.1       ad #include <sys/param.h>
     44      1.1       ad #include <sys/systm.h>
     45      1.1       ad #include <sys/kernel.h>
     46      1.1       ad #include <sys/device.h>
     47      1.1       ad #include <sys/queue.h>
     48      1.1       ad #include <sys/proc.h>
     49      1.1       ad #include <sys/buf.h>
     50      1.1       ad #include <sys/endian.h>
     51      1.1       ad #include <sys/malloc.h>
     52      1.1       ad #include <sys/pool.h>
     53      1.1       ad 
     54  1.6.2.3       he #include <machine/vmparam.h>
     55      1.1       ad #include <machine/bswap.h>
     56      1.1       ad #include <machine/bus.h>
     57      1.1       ad 
     58      1.1       ad #include <dev/ic/cacreg.h>
     59      1.1       ad #include <dev/ic/cacvar.h>
     60      1.1       ad 
     61  1.6.2.4       he struct	cac_ccb *cac_ccb_alloc(struct cac_softc *, int);
     62  1.6.2.4       he void	cac_ccb_done(struct cac_softc *, struct cac_ccb *);
     63  1.6.2.4       he void	cac_ccb_free(struct cac_softc *, struct cac_ccb *);
     64  1.6.2.4       he int	cac_ccb_poll(struct cac_softc *, struct cac_ccb *, int);
     65  1.6.2.4       he int	cac_ccb_start(struct cac_softc *, struct cac_ccb *);
     66  1.6.2.4       he int	cac_print(void *, const char *);
     67  1.6.2.4       he void	cac_shutdown(void *);
     68  1.6.2.4       he int	cac_submatch(struct device *, struct cfdata *, void *);
     69  1.6.2.4       he 
     70  1.6.2.4       he struct	cac_ccb *cac_l0_completed(struct cac_softc *);
     71  1.6.2.4       he int	cac_l0_fifo_full(struct cac_softc *);
     72  1.6.2.4       he void	cac_l0_intr_enable(struct cac_softc *, int);
     73  1.6.2.4       he int	cac_l0_intr_pending(struct cac_softc *);
     74  1.6.2.4       he void	cac_l0_submit(struct cac_softc *, struct cac_ccb *);
     75  1.6.2.3       he 
     76  1.6.2.3       he static void	*cac_sdh;	/* shutdown hook */
     77  1.6.2.3       he 
     78  1.6.2.4       he const struct cac_linkage cac_l0 = {
     79  1.6.2.3       he 	cac_l0_completed,
     80  1.6.2.3       he 	cac_l0_fifo_full,
     81  1.6.2.3       he 	cac_l0_intr_enable,
     82  1.6.2.3       he 	cac_l0_intr_pending,
     83  1.6.2.3       he 	cac_l0_submit
     84  1.6.2.3       he };
     85      1.1       ad 
     86      1.1       ad /*
     87      1.1       ad  * Initialise our interface to the controller.
     88      1.1       ad  */
     89      1.1       ad int
     90  1.6.2.3       he cac_init(struct cac_softc *sc, const char *intrstr, int startfw)
     91      1.1       ad {
     92      1.1       ad 	struct cac_controller_info cinfo;
     93      1.1       ad 	struct cac_attach_args caca;
     94      1.1       ad 	int error, rseg, size, i;
     95      1.1       ad 	bus_dma_segment_t seg;
     96      1.1       ad 	struct cac_ccb *ccb;
     97      1.1       ad 
     98      1.1       ad 	if (intrstr != NULL)
     99  1.6.2.3       he 		printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
    100  1.6.2.3       he 		    intrstr);
    101      1.1       ad 
    102      1.1       ad 	SIMPLEQ_INIT(&sc->sc_ccb_free);
    103      1.1       ad 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    104      1.1       ad 
    105      1.1       ad         size = sizeof(struct cac_ccb) * CAC_MAX_CCBS;
    106      1.1       ad 
    107      1.1       ad 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &seg, 1,
    108      1.1       ad 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    109      1.1       ad 		printf("%s: unable to allocate CCBs, error = %d\n",
    110      1.1       ad 		    sc->sc_dv.dv_xname, error);
    111      1.1       ad 		return (-1);
    112      1.1       ad 	}
    113      1.1       ad 
    114      1.1       ad 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    115  1.6.2.3       he 	    (caddr_t *)&sc->sc_ccbs,
    116  1.6.2.3       he 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    117      1.1       ad 		printf("%s: unable to map CCBs, error = %d\n",
    118      1.1       ad 		    sc->sc_dv.dv_xname, error);
    119      1.1       ad 		return (-1);
    120      1.1       ad 	}
    121      1.1       ad 
    122  1.6.2.3       he 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    123      1.1       ad 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    124      1.1       ad 		printf("%s: unable to create CCB DMA map, error = %d\n",
    125      1.1       ad 		    sc->sc_dv.dv_xname, error);
    126      1.1       ad 		return (-1);
    127      1.1       ad 	}
    128      1.1       ad 
    129      1.1       ad 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ccbs,
    130      1.1       ad 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    131      1.1       ad 		printf("%s: unable to load CCB DMA map, error = %d\n",
    132      1.1       ad 		    sc->sc_dv.dv_xname, error);
    133      1.1       ad 		return (-1);
    134      1.1       ad 	}
    135      1.1       ad 
    136      1.1       ad 	sc->sc_ccbs_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    137      1.1       ad 	memset(sc->sc_ccbs, 0, size);
    138      1.1       ad 	ccb = (struct cac_ccb *)sc->sc_ccbs;
    139      1.1       ad 
    140      1.1       ad 	for (i = 0; i < CAC_MAX_CCBS; i++, ccb++) {
    141      1.1       ad 		/* Create the DMA map for this CCB's data */
    142  1.6.2.3       he 		error = bus_dmamap_create(sc->sc_dmat, CAC_MAX_XFER,
    143  1.6.2.3       he 		    CAC_SG_SIZE, CAC_MAX_XFER, 0,
    144  1.6.2.3       he 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    145  1.6.2.3       he 		    &ccb->ccb_dmamap_xfer);
    146  1.6.2.3       he 
    147      1.1       ad 		if (error) {
    148      1.1       ad 			printf("%s: can't create ccb dmamap (%d)\n",
    149  1.6.2.3       he 			    sc->sc_dv.dv_xname, error);
    150      1.1       ad 			break;
    151      1.1       ad 		}
    152      1.1       ad 
    153      1.1       ad 		ccb->ccb_flags = 0;
    154      1.1       ad 		ccb->ccb_paddr = sc->sc_ccbs_paddr + i * sizeof(struct cac_ccb);
    155      1.1       ad 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_chain);
    156      1.1       ad 	}
    157      1.1       ad 
    158  1.6.2.3       he 	/* Start firmware background tasks, if needed. */
    159  1.6.2.3       he 	if (startfw) {
    160  1.6.2.3       he 		if (cac_cmd(sc, CAC_CMD_START_FIRMWARE, &cinfo, sizeof(cinfo),
    161  1.6.2.3       he 		    0, 0, CAC_CCB_DATA_IN, NULL)) {
    162  1.6.2.3       he 			printf("%s: CAC_CMD_START_FIRMWARE failed\n",
    163  1.6.2.3       he 			    sc->sc_dv.dv_xname);
    164  1.6.2.3       he 			return (-1);
    165  1.6.2.3       he 		}
    166  1.6.2.3       he 	}
    167  1.6.2.3       he 
    168      1.1       ad 	if (cac_cmd(sc, CAC_CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 0, 0,
    169      1.1       ad 	    CAC_CCB_DATA_IN, NULL)) {
    170      1.1       ad 		printf("%s: CAC_CMD_GET_CTRL_INFO failed\n",
    171      1.1       ad 		    sc->sc_dv.dv_xname);
    172      1.1       ad 		return (-1);
    173      1.1       ad 	}
    174      1.1       ad 
    175  1.6.2.3       he 	sc->sc_nunits = cinfo.num_drvs;
    176      1.1       ad 	for (i = 0; i < cinfo.num_drvs; i++) {
    177      1.1       ad 		caca.caca_unit = i;
    178      1.1       ad 		config_found_sm(&sc->sc_dv, &caca, cac_print, cac_submatch);
    179      1.1       ad 	}
    180      1.1       ad 
    181  1.6.2.3       he 	/* Set our `shutdownhook' before we start any device activity. */
    182      1.4       ad 	if (cac_sdh == NULL)
    183      1.1       ad 		cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
    184  1.6.2.3       he 
    185  1.6.2.4       he 	(*sc->sc_cl.cl_intr_enable)(sc, CAC_INTR_ENABLE);
    186      1.1       ad 	return (0);
    187      1.1       ad }
    188      1.1       ad 
    189      1.1       ad /*
    190  1.6.2.3       he  * Shut down all `cac' controllers.
    191      1.1       ad  */
    192  1.6.2.4       he void
    193  1.6.2.3       he cac_shutdown(void *cookie)
    194      1.1       ad {
    195      1.4       ad 	extern struct cfdriver cac_cd;
    196      1.1       ad 	struct cac_softc *sc;
    197  1.6.2.3       he 	u_int8_t buf[512];
    198      1.4       ad 	int i;
    199      1.1       ad 
    200      1.4       ad 	for (i = 0; i < cac_cd.cd_ndevs; i++) {
    201      1.4       ad 		if ((sc = cac_cd.cd_devs[i]) == NULL)
    202      1.4       ad 			continue;
    203  1.6.2.3       he 		memset(buf, 0, sizeof(buf));
    204      1.1       ad 		buf[0] = 1;
    205      1.1       ad 		cac_cmd(sc, CAC_CMD_FLUSH_CACHE, buf, sizeof(buf), 0, 0,
    206      1.1       ad 		    CAC_CCB_DATA_OUT, NULL);
    207      1.1       ad 	}
    208      1.1       ad }
    209      1.1       ad 
    210      1.1       ad /*
    211  1.6.2.3       he  * Print autoconfiguration message for a sub-device.
    212      1.1       ad  */
    213  1.6.2.4       he int
    214  1.6.2.3       he cac_print(void *aux, const char *pnp)
    215      1.1       ad {
    216      1.1       ad 	struct cac_attach_args *caca;
    217      1.1       ad 
    218      1.1       ad 	caca = (struct cac_attach_args *)aux;
    219      1.1       ad 
    220  1.6.2.3       he 	if (pnp != NULL)
    221      1.1       ad 		printf("block device at %s", pnp);
    222      1.1       ad 	printf(" unit %d", caca->caca_unit);
    223      1.1       ad 	return (UNCONF);
    224      1.1       ad }
    225      1.1       ad 
    226      1.1       ad /*
    227  1.6.2.3       he  * Match a sub-device.
    228      1.1       ad  */
    229  1.6.2.4       he int
    230  1.6.2.3       he cac_submatch(struct device *parent, struct cfdata *cf, void *aux)
    231      1.1       ad {
    232      1.1       ad 	struct cac_attach_args *caca;
    233      1.1       ad 
    234      1.1       ad 	caca = (struct cac_attach_args *)aux;
    235      1.1       ad 
    236  1.6.2.3       he 	if (cf->cacacf_unit != CACCF_UNIT_DEFAULT &&
    237      1.1       ad 	    cf->cacacf_unit != caca->caca_unit)
    238      1.1       ad 		return (0);
    239      1.1       ad 
    240      1.1       ad 	return (cf->cf_attach->ca_match(parent, cf, aux));
    241      1.1       ad }
    242      1.1       ad 
    243      1.1       ad /*
    244      1.1       ad  * Handle an interrupt from the controller: process finished CCBs and
    245      1.1       ad  * dequeue any waiting CCBs.
    246      1.1       ad  */
    247      1.1       ad int
    248  1.6.2.3       he cac_intr(void *cookie)
    249      1.1       ad {
    250      1.1       ad 	struct cac_softc *sc;
    251      1.1       ad 	struct cac_ccb *ccb;
    252      1.1       ad 
    253  1.6.2.3       he 	sc = (struct cac_softc *)cookie;
    254      1.1       ad 
    255  1.6.2.4       he 	if (!(*sc->sc_cl.cl_intr_pending)(sc)) {
    256  1.6.2.3       he #ifdef DEBUG
    257  1.6.2.3       he 		printf("%s: spurious intr\n", sc->sc_dv.dv_xname);
    258  1.6.2.3       he #endif
    259      1.1       ad 		return (0);
    260  1.6.2.3       he 	}
    261      1.1       ad 
    262  1.6.2.4       he 	while ((ccb = (*sc->sc_cl.cl_completed)(sc)) != NULL) {
    263      1.1       ad 		cac_ccb_done(sc, ccb);
    264  1.6.2.3       he 		cac_ccb_start(sc, NULL);
    265      1.1       ad 	}
    266      1.1       ad 
    267      1.1       ad 	return (1);
    268      1.1       ad }
    269      1.1       ad 
    270      1.1       ad /*
    271      1.1       ad  * Execute a [polled] command.
    272      1.1       ad  */
    273      1.1       ad int
    274  1.6.2.3       he cac_cmd(struct cac_softc *sc, int command, void *data, int datasize,
    275  1.6.2.3       he 	int drive, int blkno, int flags, struct cac_context *context)
    276      1.1       ad {
    277      1.1       ad 	struct cac_ccb *ccb;
    278      1.1       ad 	struct cac_sgb *sgb;
    279      1.2       ad 	int s, i, rv, size, nsegs;
    280      1.2       ad 
    281      1.2       ad 	size = 0;
    282      1.1       ad 
    283      1.1       ad 	if ((ccb = cac_ccb_alloc(sc, 0)) == NULL) {
    284      1.1       ad 		printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
    285  1.6.2.3       he 		return (ENOMEM);
    286      1.1       ad 	}
    287      1.1       ad 
    288      1.1       ad 	if ((flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
    289  1.6.2.3       he 		bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer,
    290  1.6.2.3       he 		    (void *)data, datasize, NULL, BUS_DMA_NOWAIT);
    291  1.6.2.3       he 
    292      1.1       ad 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0, datasize,
    293      1.1       ad 		    (flags & CAC_CCB_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
    294      1.1       ad 		    BUS_DMASYNC_PREWRITE);
    295      1.1       ad 
    296      1.1       ad 		sgb = ccb->ccb_seg;
    297      1.2       ad 		nsegs = min(ccb->ccb_dmamap_xfer->dm_nsegs, CAC_SG_SIZE);
    298      1.1       ad 
    299      1.2       ad 		for (i = 0; i < nsegs; i++, sgb++) {
    300      1.2       ad 			size += ccb->ccb_dmamap_xfer->dm_segs[i].ds_len;
    301      1.1       ad 			sgb->length =
    302      1.1       ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    303      1.1       ad 			sgb->addr =
    304      1.1       ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    305      1.1       ad 		}
    306      1.2       ad 	} else {
    307      1.2       ad 		size = datasize;
    308      1.2       ad 		nsegs = 0;
    309      1.1       ad 	}
    310      1.2       ad 
    311      1.1       ad 	ccb->ccb_hdr.drive = drive;
    312      1.1       ad 	ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
    313      1.1       ad 	    sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
    314      1.1       ad 
    315      1.2       ad 	ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
    316      1.1       ad 	ccb->ccb_req.command = command;
    317      1.5  thorpej 	ccb->ccb_req.sgcount = nsegs;
    318      1.1       ad 	ccb->ccb_req.blkno = htole32(blkno);
    319      1.1       ad 
    320      1.1       ad 	ccb->ccb_flags = flags;
    321      1.2       ad 	ccb->ccb_datasize = size;
    322      1.1       ad 
    323      1.1       ad 	if (context == NULL) {
    324      1.1       ad 		memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
    325      1.1       ad 		s = splbio();
    326  1.6.2.3       he 
    327  1.6.2.3       he 		/* Synchronous commands musn't wait. */
    328  1.6.2.4       he 		if ((*sc->sc_cl.cl_fifo_full)(sc)) {
    329      1.1       ad 			cac_ccb_free(sc, ccb);
    330      1.1       ad 			rv = -1;
    331      1.1       ad 		} else {
    332  1.6.2.3       he #ifdef DIAGNOSTIC
    333  1.6.2.3       he 			ccb->ccb_flags |= CAC_CCB_ACTIVE;
    334  1.6.2.3       he #endif
    335  1.6.2.4       he 			(*sc->sc_cl.cl_submit)(sc, ccb);
    336  1.6.2.3       he 			rv = cac_ccb_poll(sc, ccb, 2000);
    337      1.1       ad 			cac_ccb_free(sc, ccb);
    338      1.1       ad 		}
    339      1.1       ad 	} else {
    340      1.1       ad 		memcpy(&ccb->ccb_context, context, sizeof(struct cac_context));
    341      1.4       ad 		s = splbio();
    342      1.1       ad 		rv = cac_ccb_start(sc, ccb);
    343      1.1       ad 	}
    344      1.1       ad 
    345      1.4       ad 	splx(s);
    346      1.1       ad 	return (rv);
    347      1.1       ad }
    348      1.1       ad 
    349      1.1       ad /*
    350  1.6.2.3       he  * Wait for the specified CCB to complete.  Must be called at splbio.
    351      1.1       ad  */
    352  1.6.2.4       he int
    353  1.6.2.3       he cac_ccb_poll(struct cac_softc *sc, struct cac_ccb *wantccb, int timo)
    354      1.1       ad {
    355  1.6.2.3       he 	struct cac_ccb *ccb;
    356  1.6.2.3       he 
    357  1.6.2.2       ad 	timo *= 10;
    358      1.1       ad 
    359  1.6.2.3       he 	do {
    360  1.6.2.3       he 		for (; timo != 0; timo--) {
    361  1.6.2.4       he 			ccb = (*sc->sc_cl.cl_completed)(sc);
    362  1.6.2.4       he 			if (ccb != NULL)
    363      1.1       ad 				break;
    364      1.1       ad 			DELAY(100);
    365      1.1       ad 		}
    366      1.1       ad 
    367  1.6.2.3       he 		if (timo == 0) {
    368  1.6.2.4       he 			printf("%s: timeout\n", sc->sc_dv.dv_xname);
    369  1.6.2.3       he 			return (EBUSY);
    370  1.6.2.3       he 		}
    371  1.6.2.3       he 		cac_ccb_done(sc, ccb);
    372  1.6.2.3       he 	} while (ccb != wantccb);
    373      1.1       ad 
    374  1.6.2.3       he 	return (0);
    375      1.1       ad }
    376      1.1       ad 
    377      1.1       ad /*
    378      1.1       ad  * Enqueue the specifed command (if any) and attempt to start all enqueued
    379  1.6.2.3       he  * commands.  Must be called at splbio.
    380      1.1       ad  */
    381  1.6.2.4       he int
    382  1.6.2.3       he cac_ccb_start(struct cac_softc *sc, struct cac_ccb *ccb)
    383      1.1       ad {
    384  1.6.2.3       he 
    385      1.1       ad 	if (ccb != NULL)
    386      1.1       ad 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain);
    387      1.1       ad 
    388      1.1       ad 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
    389  1.6.2.4       he 		if ((*sc->sc_cl.cl_fifo_full)(sc))
    390  1.6.2.3       he 			return (EBUSY);
    391      1.1       ad 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain);
    392  1.6.2.3       he #ifdef DIAGNOSTIC
    393  1.6.2.3       he 		ccb->ccb_flags |= CAC_CCB_ACTIVE;
    394  1.6.2.3       he #endif
    395  1.6.2.4       he 		(*sc->sc_cl.cl_submit)(sc, ccb);
    396      1.1       ad 	}
    397      1.1       ad 
    398      1.1       ad 	return (0);
    399      1.1       ad }
    400      1.1       ad 
    401      1.1       ad /*
    402      1.1       ad  * Process a finished CCB.
    403      1.1       ad  */
    404  1.6.2.4       he void
    405  1.6.2.3       he cac_ccb_done(struct cac_softc *sc, struct cac_ccb *ccb)
    406      1.1       ad {
    407  1.6.2.3       he 	struct device *dv;
    408  1.6.2.3       he 	void *context;
    409      1.1       ad 	int error;
    410      1.1       ad 
    411      1.1       ad 	error = 0;
    412      1.1       ad 
    413  1.6.2.3       he #ifdef DIAGNOSTIC
    414  1.6.2.3       he 	if ((ccb->ccb_flags & CAC_CCB_ACTIVE) == 0)
    415  1.6.2.3       he 		panic("cac_ccb_done: CCB not active");
    416  1.6.2.3       he 	ccb->ccb_flags &= ~CAC_CCB_ACTIVE;
    417  1.6.2.3       he #endif
    418  1.6.2.3       he 
    419      1.1       ad 	if ((ccb->ccb_flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
    420      1.1       ad 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    421      1.1       ad 		    ccb->ccb_datasize, ccb->ccb_flags & CAC_CCB_DATA_IN ?
    422      1.1       ad 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    423      1.1       ad 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
    424      1.1       ad 	}
    425      1.1       ad 
    426  1.6.2.3       he 	error = ccb->ccb_req.error;
    427  1.6.2.3       he 	if (ccb->ccb_context.cc_handler != NULL) {
    428  1.6.2.3       he 		dv = ccb->ccb_context.cc_dv;
    429  1.6.2.3       he 		context = ccb->ccb_context.cc_context;
    430  1.6.2.3       he 		cac_ccb_free(sc, ccb);
    431  1.6.2.3       he 		(*ccb->ccb_context.cc_handler)(dv, context, error);
    432  1.6.2.3       he 	} else {
    433  1.6.2.3       he 		if ((error & CAC_RET_SOFT_ERROR) != 0)
    434  1.6.2.3       he 			printf("%s: soft error; array may be degraded\n",
    435  1.6.2.3       he 			    sc->sc_dv.dv_xname);
    436  1.6.2.3       he 		if ((error & CAC_RET_HARD_ERROR) != 0)
    437  1.6.2.3       he 			printf("%s: hard error\n", sc->sc_dv.dv_xname);
    438  1.6.2.3       he 		if ((error & CAC_RET_CMD_REJECTED) != 0) {
    439  1.6.2.3       he 			error = 1;
    440  1.6.2.3       he 			printf("%s: invalid request\n", sc->sc_dv.dv_xname);
    441  1.6.2.3       he 		}
    442      1.1       ad 	}
    443      1.1       ad }
    444      1.1       ad 
    445      1.1       ad /*
    446  1.6.2.3       he  * Allocate a CCB.
    447      1.1       ad  */
    448      1.1       ad struct cac_ccb *
    449  1.6.2.3       he cac_ccb_alloc(struct cac_softc *sc, int nosleep)
    450      1.1       ad {
    451      1.1       ad 	struct cac_ccb *ccb;
    452      1.1       ad 	int s;
    453      1.1       ad 
    454      1.1       ad 	s = splbio();
    455      1.1       ad 
    456      1.1       ad 	for (;;) {
    457      1.1       ad 		if ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) != NULL) {
    458      1.1       ad 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
    459      1.1       ad 			break;
    460      1.1       ad 		}
    461      1.1       ad 		if (nosleep) {
    462      1.1       ad 			ccb = NULL;
    463      1.1       ad 			break;
    464      1.1       ad 		}
    465      1.1       ad 		tsleep(&sc->sc_ccb_free, PRIBIO, "cacccb", 0);
    466      1.1       ad 	}
    467      1.1       ad 
    468      1.1       ad 	splx(s);
    469      1.1       ad 	return (ccb);
    470      1.1       ad }
    471      1.1       ad 
    472      1.1       ad /*
    473      1.1       ad  * Put a CCB onto the freelist.
    474      1.1       ad  */
    475      1.1       ad void
    476  1.6.2.3       he cac_ccb_free(struct cac_softc *sc, struct cac_ccb *ccb)
    477      1.1       ad {
    478      1.1       ad 	int s;
    479      1.1       ad 
    480      1.1       ad 	ccb->ccb_flags = 0;
    481  1.6.2.3       he 	s = splbio();
    482      1.1       ad 	SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
    483      1.1       ad 	if (SIMPLEQ_NEXT(ccb, ccb_chain) == NULL)
    484  1.6.2.3       he 		wakeup_one(&sc->sc_ccb_free);
    485      1.1       ad 	splx(s);
    486      1.1       ad }
    487      1.1       ad 
    488      1.1       ad /*
    489  1.6.2.3       he  * Board specific linkage shared between multiple bus types.
    490      1.1       ad  */
    491  1.6.2.3       he 
    492  1.6.2.4       he int
    493  1.6.2.3       he cac_l0_fifo_full(struct cac_softc *sc)
    494  1.6.2.3       he {
    495  1.6.2.3       he 
    496  1.6.2.3       he 	return (cac_inl(sc, CAC_REG_CMD_FIFO) == 0);
    497  1.6.2.3       he }
    498  1.6.2.3       he 
    499  1.6.2.4       he void
    500  1.6.2.3       he cac_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
    501  1.6.2.3       he {
    502  1.6.2.3       he 
    503  1.6.2.3       he 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, (caddr_t)ccb - sc->sc_ccbs,
    504  1.6.2.3       he 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    505  1.6.2.3       he 	cac_outl(sc, CAC_REG_CMD_FIFO, ccb->ccb_paddr);
    506  1.6.2.3       he }
    507  1.6.2.3       he 
    508  1.6.2.4       he struct cac_ccb *
    509  1.6.2.3       he cac_l0_completed(struct cac_softc *sc)
    510  1.6.2.3       he {
    511  1.6.2.3       he 	struct cac_ccb *ccb;
    512  1.6.2.3       he 	paddr_t off;
    513  1.6.2.3       he 
    514  1.6.2.4       he 	if ((off = cac_inl(sc, CAC_REG_DONE_FIFO)) == 0)
    515  1.6.2.4       he 		return (NULL);
    516  1.6.2.4       he 
    517  1.6.2.4       he 	if ((off & 3) != 0)
    518  1.6.2.4       he 		printf("%s: failed command list returned: %lx\n",
    519  1.6.2.4       he 		    sc->sc_dv.dv_xname, (long)off);
    520  1.6.2.4       he 
    521  1.6.2.4       he 	off = (off & ~3) - sc->sc_ccbs_paddr;
    522  1.6.2.3       he 	ccb = (struct cac_ccb *)(sc->sc_ccbs + off);
    523  1.6.2.3       he 
    524  1.6.2.3       he 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
    525  1.6.2.3       he 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    526  1.6.2.3       he 
    527  1.6.2.3       he 	return (ccb);
    528  1.6.2.3       he }
    529  1.6.2.3       he 
    530  1.6.2.4       he int
    531  1.6.2.3       he cac_l0_intr_pending(struct cac_softc *sc)
    532  1.6.2.3       he {
    533  1.6.2.3       he 
    534  1.6.2.4       he 	return (cac_inl(sc, CAC_REG_INTR_PENDING) & CAC_INTR_ENABLE);
    535  1.6.2.3       he }
    536  1.6.2.3       he 
    537  1.6.2.4       he void
    538  1.6.2.3       he cac_l0_intr_enable(struct cac_softc *sc, int state)
    539      1.1       ad {
    540      1.1       ad 
    541  1.6.2.3       he 	cac_outl(sc, CAC_REG_INTR_MASK,
    542  1.6.2.3       he 	    state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
    543      1.1       ad }
    544