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