Home | History | Annotate | Line # | Download | only in ic
cac.c revision 1.41.8.3
      1  1.41.8.3      matt /*	cac.c,v 1.41.8.2 2008/01/09 01:52:49 matt Exp	*/
      2       1.1        ad 
      3       1.1        ad /*-
      4      1.39        ad  * Copyright (c) 2000, 2006, 2007 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.41.8.3      matt __KERNEL_RCSID(0, "cac.c,v 1.41.8.2 2008/01/09 01:52:49 matt Exp");
     45  1.41.8.3      matt 
     46  1.41.8.3      matt #include "bio.h"
     47       1.1        ad 
     48       1.1        ad #include <sys/param.h>
     49       1.1        ad #include <sys/systm.h>
     50       1.1        ad #include <sys/kernel.h>
     51       1.1        ad #include <sys/device.h>
     52       1.1        ad #include <sys/queue.h>
     53       1.1        ad #include <sys/proc.h>
     54       1.1        ad #include <sys/buf.h>
     55       1.1        ad #include <sys/endian.h>
     56       1.1        ad #include <sys/malloc.h>
     57       1.1        ad #include <sys/pool.h>
     58       1.1        ad 
     59      1.15   thorpej #include <uvm/uvm_extern.h>
     60      1.15   thorpej 
     61      1.34       dsl #include <sys/bswap.h>
     62  1.41.8.1      matt #include <sys/bus.h>
     63       1.1        ad 
     64       1.1        ad #include <dev/ic/cacreg.h>
     65       1.1        ad #include <dev/ic/cacvar.h>
     66       1.1        ad 
     67  1.41.8.3      matt #if NBIO > 0
     68  1.41.8.3      matt #include <dev/biovar.h>
     69  1.41.8.3      matt #endif /* NBIO > 0 */
     70  1.41.8.3      matt 
     71      1.28  drochner #include "locators.h"
     72      1.28  drochner 
     73      1.27   thorpej static struct	cac_ccb *cac_ccb_alloc(struct cac_softc *, int);
     74      1.27   thorpej static void	cac_ccb_done(struct cac_softc *, struct cac_ccb *);
     75      1.27   thorpej static void	cac_ccb_free(struct cac_softc *, struct cac_ccb *);
     76      1.27   thorpej static int	cac_ccb_poll(struct cac_softc *, struct cac_ccb *, int);
     77      1.27   thorpej static int	cac_ccb_start(struct cac_softc *, struct cac_ccb *);
     78      1.27   thorpej static int	cac_print(void *, const char *);
     79      1.27   thorpej static void	cac_shutdown(void *);
     80      1.27   thorpej 
     81      1.27   thorpej static struct	cac_ccb *cac_l0_completed(struct cac_softc *);
     82      1.27   thorpej static int	cac_l0_fifo_full(struct cac_softc *);
     83      1.27   thorpej static void	cac_l0_intr_enable(struct cac_softc *, int);
     84      1.27   thorpej static int	cac_l0_intr_pending(struct cac_softc *);
     85      1.27   thorpej static void	cac_l0_submit(struct cac_softc *, struct cac_ccb *);
     86      1.10        ad 
     87      1.10        ad static void	*cac_sdh;	/* shutdown hook */
     88      1.10        ad 
     89  1.41.8.3      matt #if NBIO > 0
     90  1.41.8.3      matt int		cac_ioctl(struct device *, u_long, void *);
     91  1.41.8.3      matt int		cac_ioctl_vol(struct cac_softc *, struct bioc_vol *);
     92  1.41.8.3      matt int		cac_create_sensors(struct cac_softc *);
     93  1.41.8.3      matt void		cac_sensor_refresh(struct sysmon_envsys *, envsys_data_t *);
     94  1.41.8.3      matt #endif /* NBIO > 0 */
     95  1.41.8.3      matt 
     96      1.20        ad const struct cac_linkage cac_l0 = {
     97      1.10        ad 	cac_l0_completed,
     98      1.10        ad 	cac_l0_fifo_full,
     99      1.10        ad 	cac_l0_intr_enable,
    100      1.10        ad 	cac_l0_intr_pending,
    101      1.10        ad 	cac_l0_submit
    102      1.10        ad };
    103       1.1        ad 
    104       1.1        ad /*
    105       1.1        ad  * Initialise our interface to the controller.
    106       1.1        ad  */
    107       1.1        ad int
    108      1.10        ad cac_init(struct cac_softc *sc, const char *intrstr, int startfw)
    109       1.1        ad {
    110       1.1        ad 	struct cac_controller_info cinfo;
    111       1.1        ad 	struct cac_attach_args caca;
    112       1.1        ad 	int error, rseg, size, i;
    113       1.1        ad 	bus_dma_segment_t seg;
    114       1.1        ad 	struct cac_ccb *ccb;
    115      1.31  drochner 	int locs[CACCF_NLOCS];
    116      1.38        ad 	char firm[8];
    117      1.29     perry 
    118       1.1        ad 	if (intrstr != NULL)
    119      1.25   thorpej 		aprint_normal("%s: interrupting at %s\n", sc->sc_dv.dv_xname,
    120      1.10        ad 		    intrstr);
    121       1.1        ad 
    122       1.1        ad 	SIMPLEQ_INIT(&sc->sc_ccb_free);
    123       1.1        ad 	SIMPLEQ_INIT(&sc->sc_ccb_queue);
    124  1.41.8.2      matt 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
    125      1.39        ad 	cv_init(&sc->sc_ccb_cv, "cacccb");
    126       1.1        ad 
    127       1.1        ad         size = sizeof(struct cac_ccb) * CAC_MAX_CCBS;
    128       1.1        ad 
    129      1.29     perry 	if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
    130       1.1        ad 	    &rseg, BUS_DMA_NOWAIT)) != 0) {
    131      1.25   thorpej 		aprint_error("%s: unable to allocate CCBs, 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.29     perry 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
    137      1.40  christos 	    (void **)&sc->sc_ccbs,
    138      1.10        ad 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
    139      1.25   thorpej 		aprint_error("%s: unable to map CCBs, error = %d\n",
    140       1.1        ad 		    sc->sc_dv.dv_xname, error);
    141       1.1        ad 		return (-1);
    142       1.1        ad 	}
    143       1.1        ad 
    144      1.29     perry 	if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    145       1.1        ad 	    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
    146      1.25   thorpej 		aprint_error("%s: unable to create CCB DMA map, error = %d\n",
    147       1.1        ad 		    sc->sc_dv.dv_xname, error);
    148       1.1        ad 		return (-1);
    149       1.1        ad 	}
    150       1.1        ad 
    151      1.29     perry 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_ccbs,
    152       1.1        ad 	    size, NULL, BUS_DMA_NOWAIT)) != 0) {
    153      1.25   thorpej 		aprint_error("%s: unable to load CCB DMA map, error = %d\n",
    154       1.1        ad 		    sc->sc_dv.dv_xname, error);
    155       1.1        ad 		return (-1);
    156       1.1        ad 	}
    157       1.1        ad 
    158       1.1        ad 	sc->sc_ccbs_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    159       1.1        ad 	memset(sc->sc_ccbs, 0, size);
    160       1.1        ad 	ccb = (struct cac_ccb *)sc->sc_ccbs;
    161       1.1        ad 
    162       1.1        ad 	for (i = 0; i < CAC_MAX_CCBS; i++, ccb++) {
    163       1.1        ad 		/* Create the DMA map for this CCB's data */
    164      1.12        ad 		error = bus_dmamap_create(sc->sc_dmat, CAC_MAX_XFER,
    165      1.12        ad 		    CAC_SG_SIZE, CAC_MAX_XFER, 0,
    166      1.12        ad 		    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    167      1.12        ad 		    &ccb->ccb_dmamap_xfer);
    168      1.10        ad 
    169       1.1        ad 		if (error) {
    170      1.29     perry 			aprint_error("%s: can't create ccb dmamap (%d)\n",
    171      1.10        ad 			    sc->sc_dv.dv_xname, error);
    172       1.1        ad 			break;
    173       1.1        ad 		}
    174       1.1        ad 
    175       1.1        ad 		ccb->ccb_flags = 0;
    176       1.1        ad 		ccb->ccb_paddr = sc->sc_ccbs_paddr + i * sizeof(struct cac_ccb);
    177       1.1        ad 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_chain);
    178       1.1        ad 	}
    179      1.29     perry 
    180      1.10        ad 	/* Start firmware background tasks, if needed. */
    181      1.10        ad 	if (startfw) {
    182      1.10        ad 		if (cac_cmd(sc, CAC_CMD_START_FIRMWARE, &cinfo, sizeof(cinfo),
    183      1.10        ad 		    0, 0, CAC_CCB_DATA_IN, NULL)) {
    184      1.25   thorpej 			aprint_error("%s: CAC_CMD_START_FIRMWARE failed\n",
    185      1.10        ad 			    sc->sc_dv.dv_xname);
    186      1.10        ad 			return (-1);
    187      1.10        ad 		}
    188      1.10        ad 	}
    189      1.10        ad 
    190      1.29     perry 	if (cac_cmd(sc, CAC_CMD_GET_CTRL_INFO, &cinfo, sizeof(cinfo), 0, 0,
    191       1.1        ad 	    CAC_CCB_DATA_IN, NULL)) {
    192      1.29     perry 		aprint_error("%s: CAC_CMD_GET_CTRL_INFO failed\n",
    193       1.1        ad 		    sc->sc_dv.dv_xname);
    194       1.1        ad 		return (-1);
    195       1.1        ad 	}
    196       1.1        ad 
    197      1.38        ad 	strlcpy(firm, cinfo.firm_rev, 4+1);
    198      1.38        ad 	printf("%s: %d channels, firmware <%s>\n", sc->sc_dv.dv_xname,
    199      1.38        ad 	    cinfo.scsi_chips, firm);
    200      1.38        ad 
    201      1.14        ad 	sc->sc_nunits = cinfo.num_drvs;
    202       1.1        ad 	for (i = 0; i < cinfo.num_drvs; i++) {
    203       1.1        ad 		caca.caca_unit = i;
    204      1.28  drochner 
    205      1.31  drochner 		locs[CACCF_UNIT] = i;
    206      1.28  drochner 
    207      1.31  drochner 		config_found_sm_loc(&sc->sc_dv, "cac", locs, &caca,
    208      1.38        ad 		    cac_print, config_stdsubmatch);
    209       1.1        ad 	}
    210       1.1        ad 
    211      1.10        ad 	/* Set our `shutdownhook' before we start any device activity. */
    212       1.4        ad 	if (cac_sdh == NULL)
    213       1.1        ad 		cac_sdh = shutdownhook_establish(cac_shutdown, NULL);
    214      1.29     perry 
    215      1.39        ad 	mutex_enter(&sc->sc_mutex);
    216      1.20        ad 	(*sc->sc_cl.cl_intr_enable)(sc, CAC_INTR_ENABLE);
    217      1.39        ad 	mutex_exit(&sc->sc_mutex);
    218      1.39        ad 
    219  1.41.8.3      matt #if NBIO > 0
    220  1.41.8.3      matt 	if (bio_register(&sc->sc_dv, cac_ioctl) != 0)
    221  1.41.8.3      matt 		printf("%s: controller registration failed",
    222  1.41.8.3      matt 		    sc->sc_dv.dv_xname);
    223  1.41.8.3      matt 	else
    224  1.41.8.3      matt 		sc->sc_ioctl = cac_ioctl;
    225  1.41.8.3      matt 	if (cac_create_sensors(sc) != 0)
    226  1.41.8.3      matt 		aprint_error("%s: unable to create sensors\n", sc->sc_dv.dv_xname);
    227  1.41.8.3      matt #endif
    228  1.41.8.3      matt 
    229       1.1        ad 	return (0);
    230       1.1        ad }
    231       1.1        ad 
    232       1.1        ad /*
    233      1.10        ad  * Shut down all `cac' controllers.
    234       1.1        ad  */
    235      1.27   thorpej static void
    236      1.36  christos cac_shutdown(void *cookie)
    237       1.1        ad {
    238       1.4        ad 	extern struct cfdriver cac_cd;
    239       1.1        ad 	struct cac_softc *sc;
    240      1.30  christos 	u_int8_t tbuf[512];
    241       1.4        ad 	int i;
    242       1.1        ad 
    243       1.4        ad 	for (i = 0; i < cac_cd.cd_ndevs; i++) {
    244       1.7   thorpej 		if ((sc = device_lookup(&cac_cd, i)) == NULL)
    245      1.29     perry 			continue;
    246      1.30  christos 		memset(tbuf, 0, sizeof(tbuf));
    247      1.30  christos 		tbuf[0] = 1;
    248      1.30  christos 		cac_cmd(sc, CAC_CMD_FLUSH_CACHE, tbuf, sizeof(tbuf), 0, 0,
    249       1.1        ad 		    CAC_CCB_DATA_OUT, NULL);
    250       1.1        ad 	}
    251      1.29     perry }
    252       1.1        ad 
    253       1.1        ad /*
    254      1.10        ad  * Print autoconfiguration message for a sub-device.
    255       1.1        ad  */
    256      1.27   thorpej static int
    257      1.10        ad cac_print(void *aux, const char *pnp)
    258       1.1        ad {
    259       1.1        ad 	struct cac_attach_args *caca;
    260       1.1        ad 
    261       1.1        ad 	caca = (struct cac_attach_args *)aux;
    262       1.1        ad 
    263      1.10        ad 	if (pnp != NULL)
    264      1.23   thorpej 		aprint_normal("block device at %s", pnp);
    265      1.23   thorpej 	aprint_normal(" unit %d", caca->caca_unit);
    266       1.1        ad 	return (UNCONF);
    267       1.1        ad }
    268       1.1        ad 
    269       1.1        ad /*
    270       1.1        ad  * Handle an interrupt from the controller: process finished CCBs and
    271       1.1        ad  * dequeue any waiting CCBs.
    272       1.1        ad  */
    273       1.1        ad int
    274      1.10        ad cac_intr(void *cookie)
    275       1.1        ad {
    276       1.1        ad 	struct cac_softc *sc;
    277       1.1        ad 	struct cac_ccb *ccb;
    278      1.39        ad 	int rv;
    279       1.1        ad 
    280      1.10        ad 	sc = (struct cac_softc *)cookie;
    281       1.1        ad 
    282      1.39        ad 	mutex_enter(&sc->sc_mutex);
    283      1.39        ad 
    284      1.39        ad 	if ((*sc->sc_cl.cl_intr_pending)(sc)) {
    285      1.39        ad 		while ((ccb = (*sc->sc_cl.cl_completed)(sc)) != NULL) {
    286      1.39        ad 			cac_ccb_done(sc, ccb);
    287      1.39        ad 			cac_ccb_start(sc, NULL);
    288      1.39        ad 		}
    289      1.39        ad 		rv = 1;
    290      1.39        ad 	} else
    291      1.39        ad 		rv = 0;
    292       1.1        ad 
    293      1.39        ad 	mutex_exit(&sc->sc_mutex);
    294       1.1        ad 
    295      1.39        ad 	return (rv);
    296       1.1        ad }
    297       1.1        ad 
    298       1.1        ad /*
    299       1.1        ad  * Execute a [polled] command.
    300       1.1        ad  */
    301       1.1        ad int
    302      1.10        ad cac_cmd(struct cac_softc *sc, int command, void *data, int datasize,
    303      1.10        ad 	int drive, int blkno, int flags, struct cac_context *context)
    304       1.1        ad {
    305       1.1        ad 	struct cac_ccb *ccb;
    306       1.1        ad 	struct cac_sgb *sgb;
    307      1.39        ad 	int i, rv, size, nsegs;
    308       1.2        ad 
    309       1.2        ad 	size = 0;
    310       1.1        ad 
    311      1.26        pk 	if ((ccb = cac_ccb_alloc(sc, 1)) == NULL) {
    312       1.1        ad 		printf("%s: unable to alloc CCB", sc->sc_dv.dv_xname);
    313      1.26        pk 		return (EAGAIN);
    314       1.1        ad 	}
    315       1.1        ad 
    316       1.1        ad 	if ((flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
    317      1.10        ad 		bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer,
    318      1.17   thorpej 		    (void *)data, datasize, NULL, BUS_DMA_NOWAIT |
    319      1.18   thorpej 		    BUS_DMA_STREAMING | ((flags & CAC_CCB_DATA_IN) ?
    320      1.18   thorpej 		    BUS_DMA_READ : BUS_DMA_WRITE));
    321      1.10        ad 
    322       1.1        ad 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0, datasize,
    323       1.1        ad 		    (flags & CAC_CCB_DATA_IN) != 0 ? BUS_DMASYNC_PREREAD :
    324       1.1        ad 		    BUS_DMASYNC_PREWRITE);
    325      1.29     perry 
    326       1.1        ad 		sgb = ccb->ccb_seg;
    327       1.2        ad 		nsegs = min(ccb->ccb_dmamap_xfer->dm_nsegs, CAC_SG_SIZE);
    328       1.1        ad 
    329       1.2        ad 		for (i = 0; i < nsegs; i++, sgb++) {
    330       1.2        ad 			size += ccb->ccb_dmamap_xfer->dm_segs[i].ds_len;
    331      1.29     perry 			sgb->length =
    332       1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
    333      1.29     perry 			sgb->addr =
    334       1.1        ad 			    htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
    335       1.1        ad 		}
    336       1.2        ad 	} else {
    337       1.2        ad 		size = datasize;
    338       1.2        ad 		nsegs = 0;
    339       1.1        ad 	}
    340       1.2        ad 
    341       1.1        ad 	ccb->ccb_hdr.drive = drive;
    342      1.37        ad 	ccb->ccb_hdr.priority = 0;
    343      1.29     perry 	ccb->ccb_hdr.size = htole16((sizeof(struct cac_req) +
    344       1.1        ad 	    sizeof(struct cac_sgb) * CAC_SG_SIZE) >> 2);
    345       1.1        ad 
    346      1.37        ad 	ccb->ccb_req.next = 0;
    347      1.37        ad 	ccb->ccb_req.error = 0;
    348      1.37        ad 	ccb->ccb_req.reserved = 0;
    349       1.2        ad 	ccb->ccb_req.bcount = htole16(howmany(size, DEV_BSIZE));
    350       1.1        ad 	ccb->ccb_req.command = command;
    351       1.5   thorpej 	ccb->ccb_req.sgcount = nsegs;
    352       1.1        ad 	ccb->ccb_req.blkno = htole32(blkno);
    353      1.29     perry 
    354       1.1        ad 	ccb->ccb_flags = flags;
    355       1.2        ad 	ccb->ccb_datasize = size;
    356       1.1        ad 
    357      1.39        ad 	mutex_enter(&sc->sc_mutex);
    358      1.39        ad 
    359       1.1        ad 	if (context == NULL) {
    360       1.1        ad 		memset(&ccb->ccb_context, 0, sizeof(struct cac_context));
    361      1.10        ad 
    362      1.10        ad 		/* Synchronous commands musn't wait. */
    363      1.20        ad 		if ((*sc->sc_cl.cl_fifo_full)(sc)) {
    364       1.1        ad 			cac_ccb_free(sc, ccb);
    365      1.26        pk 			rv = EAGAIN;
    366       1.1        ad 		} else {
    367      1.12        ad #ifdef DIAGNOSTIC
    368      1.12        ad 			ccb->ccb_flags |= CAC_CCB_ACTIVE;
    369      1.12        ad #endif
    370      1.20        ad 			(*sc->sc_cl.cl_submit)(sc, ccb);
    371      1.13        ad 			rv = cac_ccb_poll(sc, ccb, 2000);
    372       1.1        ad 			cac_ccb_free(sc, ccb);
    373       1.1        ad 		}
    374       1.1        ad 	} else {
    375       1.1        ad 		memcpy(&ccb->ccb_context, context, sizeof(struct cac_context));
    376      1.26        pk 		(void)cac_ccb_start(sc, ccb);
    377      1.26        pk 		rv = 0;
    378       1.1        ad 	}
    379      1.29     perry 
    380      1.39        ad 	mutex_exit(&sc->sc_mutex);
    381       1.1        ad 	return (rv);
    382       1.1        ad }
    383       1.1        ad 
    384       1.1        ad /*
    385      1.39        ad  * Wait for the specified CCB to complete.
    386       1.1        ad  */
    387      1.27   thorpej static int
    388      1.10        ad cac_ccb_poll(struct cac_softc *sc, struct cac_ccb *wantccb, int timo)
    389      1.10        ad {
    390       1.1        ad 	struct cac_ccb *ccb;
    391      1.13        ad 
    392      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    393      1.39        ad 
    394      1.38        ad 	timo *= 1000;
    395       1.1        ad 
    396      1.10        ad 	do {
    397      1.10        ad 		for (; timo != 0; timo--) {
    398      1.20        ad 			ccb = (*sc->sc_cl.cl_completed)(sc);
    399      1.20        ad 			if (ccb != NULL)
    400       1.1        ad 				break;
    401      1.38        ad 			DELAY(1);
    402       1.1        ad 		}
    403       1.1        ad 
    404      1.13        ad 		if (timo == 0) {
    405      1.20        ad 			printf("%s: timeout\n", sc->sc_dv.dv_xname);
    406      1.13        ad 			return (EBUSY);
    407      1.13        ad 		}
    408      1.10        ad 		cac_ccb_done(sc, ccb);
    409      1.10        ad 	} while (ccb != wantccb);
    410      1.13        ad 
    411      1.13        ad 	return (0);
    412       1.1        ad }
    413       1.1        ad 
    414       1.1        ad /*
    415      1.29     perry  * Enqueue the specified command (if any) and attempt to start all enqueued
    416      1.39        ad  * commands.
    417       1.1        ad  */
    418      1.27   thorpej static int
    419      1.10        ad cac_ccb_start(struct cac_softc *sc, struct cac_ccb *ccb)
    420       1.1        ad {
    421      1.10        ad 
    422      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    423      1.39        ad 
    424       1.1        ad 	if (ccb != NULL)
    425       1.1        ad 		SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain);
    426       1.1        ad 
    427       1.1        ad 	while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
    428      1.20        ad 		if ((*sc->sc_cl.cl_fifo_full)(sc))
    429      1.26        pk 			return (EAGAIN);
    430      1.21     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb_chain);
    431      1.10        ad #ifdef DIAGNOSTIC
    432      1.10        ad 		ccb->ccb_flags |= CAC_CCB_ACTIVE;
    433      1.10        ad #endif
    434      1.20        ad 		(*sc->sc_cl.cl_submit)(sc, ccb);
    435       1.1        ad 	}
    436      1.29     perry 
    437       1.1        ad 	return (0);
    438       1.1        ad }
    439       1.1        ad 
    440       1.1        ad /*
    441       1.1        ad  * Process a finished CCB.
    442       1.1        ad  */
    443      1.27   thorpej static void
    444      1.10        ad cac_ccb_done(struct cac_softc *sc, struct cac_ccb *ccb)
    445       1.1        ad {
    446      1.14        ad 	struct device *dv;
    447      1.14        ad 	void *context;
    448       1.1        ad 	int error;
    449       1.1        ad 
    450       1.1        ad 	error = 0;
    451       1.1        ad 
    452      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    453      1.39        ad 
    454      1.10        ad #ifdef DIAGNOSTIC
    455      1.10        ad 	if ((ccb->ccb_flags & CAC_CCB_ACTIVE) == 0)
    456      1.10        ad 		panic("cac_ccb_done: CCB not active");
    457      1.10        ad 	ccb->ccb_flags &= ~CAC_CCB_ACTIVE;
    458      1.10        ad #endif
    459      1.10        ad 
    460       1.1        ad 	if ((ccb->ccb_flags & (CAC_CCB_DATA_IN | CAC_CCB_DATA_OUT)) != 0) {
    461       1.1        ad 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
    462       1.1        ad 		    ccb->ccb_datasize, ccb->ccb_flags & CAC_CCB_DATA_IN ?
    463       1.1        ad 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    464       1.1        ad 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
    465       1.1        ad 	}
    466       1.1        ad 
    467      1.16        ad 	error = ccb->ccb_req.error;
    468      1.10        ad 	if (ccb->ccb_context.cc_handler != NULL) {
    469      1.14        ad 		dv = ccb->ccb_context.cc_dv;
    470      1.14        ad 		context = ccb->ccb_context.cc_context;
    471      1.10        ad 		cac_ccb_free(sc, ccb);
    472      1.14        ad 		(*ccb->ccb_context.cc_handler)(dv, context, error);
    473      1.16        ad 	} else {
    474      1.16        ad 		if ((error & CAC_RET_SOFT_ERROR) != 0)
    475      1.16        ad 			printf("%s: soft error; array may be degraded\n",
    476      1.16        ad 			    sc->sc_dv.dv_xname);
    477      1.16        ad 		if ((error & CAC_RET_HARD_ERROR) != 0)
    478      1.16        ad 			printf("%s: hard error\n", sc->sc_dv.dv_xname);
    479      1.16        ad 		if ((error & CAC_RET_CMD_REJECTED) != 0) {
    480      1.16        ad 			error = 1;
    481      1.16        ad 			printf("%s: invalid request\n", sc->sc_dv.dv_xname);
    482      1.16        ad 		}
    483      1.10        ad 	}
    484       1.1        ad }
    485       1.1        ad 
    486       1.1        ad /*
    487      1.10        ad  * Allocate a CCB.
    488       1.1        ad  */
    489      1.27   thorpej static struct cac_ccb *
    490      1.10        ad cac_ccb_alloc(struct cac_softc *sc, int nosleep)
    491       1.1        ad {
    492       1.1        ad 	struct cac_ccb *ccb;
    493       1.1        ad 
    494      1.39        ad 	mutex_enter(&sc->sc_mutex);
    495       1.1        ad 
    496       1.1        ad 	for (;;) {
    497       1.1        ad 		if ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_free)) != NULL) {
    498      1.21     lukem 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_free, ccb_chain);
    499       1.1        ad 			break;
    500       1.1        ad 		}
    501       1.1        ad 		if (nosleep) {
    502       1.1        ad 			ccb = NULL;
    503       1.1        ad 			break;
    504       1.1        ad 		}
    505      1.39        ad 		cv_wait(&sc->sc_ccb_cv, &sc->sc_mutex);
    506       1.1        ad 	}
    507       1.1        ad 
    508      1.39        ad 	mutex_exit(&sc->sc_mutex);
    509       1.1        ad 	return (ccb);
    510       1.1        ad }
    511       1.1        ad 
    512       1.1        ad /*
    513       1.1        ad  * Put a CCB onto the freelist.
    514       1.1        ad  */
    515      1.27   thorpej static void
    516      1.10        ad cac_ccb_free(struct cac_softc *sc, struct cac_ccb *ccb)
    517       1.1        ad {
    518      1.39        ad 
    519      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    520       1.1        ad 
    521      1.13        ad 	ccb->ccb_flags = 0;
    522      1.39        ad 	if (SIMPLEQ_EMPTY(&sc->sc_ccb_free))
    523      1.39        ad 		cv_signal(&sc->sc_ccb_cv);
    524       1.1        ad 	SIMPLEQ_INSERT_HEAD(&sc->sc_ccb_free, ccb, ccb_chain);
    525      1.10        ad }
    526      1.10        ad 
    527      1.10        ad /*
    528      1.10        ad  * Board specific linkage shared between multiple bus types.
    529      1.10        ad  */
    530      1.10        ad 
    531      1.27   thorpej static int
    532      1.10        ad cac_l0_fifo_full(struct cac_softc *sc)
    533      1.10        ad {
    534      1.10        ad 
    535      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    536      1.39        ad 
    537      1.10        ad 	return (cac_inl(sc, CAC_REG_CMD_FIFO) == 0);
    538      1.10        ad }
    539      1.10        ad 
    540      1.27   thorpej static void
    541      1.10        ad cac_l0_submit(struct cac_softc *sc, struct cac_ccb *ccb)
    542      1.10        ad {
    543      1.10        ad 
    544      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    545      1.39        ad 
    546      1.40  christos 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
    547      1.40  christos 	    (char *)ccb - (char *)sc->sc_ccbs,
    548      1.10        ad 	    sizeof(struct cac_ccb), BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    549      1.10        ad 	cac_outl(sc, CAC_REG_CMD_FIFO, ccb->ccb_paddr);
    550      1.10        ad }
    551      1.10        ad 
    552      1.27   thorpej static struct cac_ccb *
    553      1.10        ad cac_l0_completed(struct cac_softc *sc)
    554      1.10        ad {
    555      1.10        ad 	struct cac_ccb *ccb;
    556      1.10        ad 	paddr_t off;
    557      1.10        ad 
    558      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    559      1.39        ad 
    560      1.20        ad 	if ((off = cac_inl(sc, CAC_REG_DONE_FIFO)) == 0)
    561      1.20        ad 		return (NULL);
    562      1.20        ad 
    563      1.20        ad 	if ((off & 3) != 0)
    564      1.20        ad 		printf("%s: failed command list returned: %lx\n",
    565      1.20        ad 		    sc->sc_dv.dv_xname, (long)off);
    566      1.20        ad 
    567      1.20        ad 	off = (off & ~3) - sc->sc_ccbs_paddr;
    568      1.40  christos 	ccb = (struct cac_ccb *)((char *)sc->sc_ccbs + off);
    569      1.10        ad 
    570      1.10        ad 	bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, off, sizeof(struct cac_ccb),
    571      1.10        ad 	    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    572      1.10        ad 
    573      1.37        ad 	if ((off & 3) != 0 && ccb->ccb_req.error == 0)
    574      1.37        ad 		ccb->ccb_req.error = CAC_RET_CMD_REJECTED;
    575      1.37        ad 
    576      1.10        ad 	return (ccb);
    577      1.10        ad }
    578      1.10        ad 
    579      1.27   thorpej static int
    580      1.10        ad cac_l0_intr_pending(struct cac_softc *sc)
    581      1.10        ad {
    582      1.10        ad 
    583      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    584      1.39        ad 
    585      1.20        ad 	return (cac_inl(sc, CAC_REG_INTR_PENDING) & CAC_INTR_ENABLE);
    586      1.10        ad }
    587      1.10        ad 
    588      1.27   thorpej static void
    589      1.10        ad cac_l0_intr_enable(struct cac_softc *sc, int state)
    590      1.10        ad {
    591      1.10        ad 
    592      1.41        ad 	KASSERT(mutex_owned(&sc->sc_mutex));
    593      1.39        ad 
    594      1.10        ad 	cac_outl(sc, CAC_REG_INTR_MASK,
    595      1.10        ad 	    state ? CAC_INTR_ENABLE : CAC_INTR_DISABLE);
    596       1.1        ad }
    597  1.41.8.3      matt 
    598  1.41.8.3      matt #if NBIO > 0
    599  1.41.8.3      matt const int cac_level[] = { 0, 4, 1, 5, 51, 7 };
    600  1.41.8.3      matt const int cac_stat[] = { BIOC_SVONLINE, BIOC_SVOFFLINE, BIOC_SVOFFLINE,
    601  1.41.8.3      matt     BIOC_SVDEGRADED, BIOC_SVREBUILD, BIOC_SVREBUILD, BIOC_SVDEGRADED,
    602  1.41.8.3      matt     BIOC_SVDEGRADED, BIOC_SVINVALID, BIOC_SVINVALID, BIOC_SVBUILDING,
    603  1.41.8.3      matt     BIOC_SVOFFLINE, BIOC_SVBUILDING };
    604  1.41.8.3      matt 
    605  1.41.8.3      matt int
    606  1.41.8.3      matt cac_ioctl(struct device *dev, u_long cmd, void *addr)
    607  1.41.8.3      matt {
    608  1.41.8.3      matt 	struct cac_softc	*sc = (struct cac_softc *)dev;
    609  1.41.8.3      matt 	struct bioc_inq *bi;
    610  1.41.8.3      matt 	struct bioc_disk *bd;
    611  1.41.8.3      matt 	cac_lock_t lock;
    612  1.41.8.3      matt 	int error = 0;
    613  1.41.8.3      matt 
    614  1.41.8.3      matt 	lock = CAC_LOCK(sc);
    615  1.41.8.3      matt 	switch (cmd) {
    616  1.41.8.3      matt 	case BIOCINQ:
    617  1.41.8.3      matt 		bi = (struct bioc_inq *)addr;
    618  1.41.8.3      matt 		strlcpy(bi->bi_dev, sc->sc_dv.dv_xname, sizeof(bi->bi_dev));
    619  1.41.8.3      matt 		bi->bi_novol = sc->sc_nunits;
    620  1.41.8.3      matt 		bi->bi_nodisk = 0;
    621  1.41.8.3      matt 		break;
    622  1.41.8.3      matt 
    623  1.41.8.3      matt 	case BIOCVOL:
    624  1.41.8.3      matt 		error = cac_ioctl_vol(sc, (struct bioc_vol *)addr);
    625  1.41.8.3      matt 		break;
    626  1.41.8.3      matt 
    627  1.41.8.3      matt 	case BIOCDISK:
    628  1.41.8.3      matt 	case BIOCDISK_NOVOL:
    629  1.41.8.3      matt 		bd = (struct bioc_disk *)addr;
    630  1.41.8.3      matt 		if (bd->bd_volid > sc->sc_nunits) {
    631  1.41.8.3      matt 			error = EINVAL;
    632  1.41.8.3      matt 			break;
    633  1.41.8.3      matt 		}
    634  1.41.8.3      matt 		/* No disk information yet */
    635  1.41.8.3      matt 		break;
    636  1.41.8.3      matt 
    637  1.41.8.3      matt 	case BIOCBLINK:
    638  1.41.8.3      matt 	case BIOCALARM:
    639  1.41.8.3      matt 	case BIOCSETSTATE:
    640  1.41.8.3      matt 	default:
    641  1.41.8.3      matt 		error = EINVAL;
    642  1.41.8.3      matt 	}
    643  1.41.8.3      matt 	CAC_UNLOCK(sc, lock);
    644  1.41.8.3      matt 
    645  1.41.8.3      matt 	return (error);
    646  1.41.8.3      matt }
    647  1.41.8.3      matt 
    648  1.41.8.3      matt int
    649  1.41.8.3      matt cac_ioctl_vol(struct cac_softc *sc, struct bioc_vol *bv)
    650  1.41.8.3      matt {
    651  1.41.8.3      matt 	struct cac_drive_info dinfo;
    652  1.41.8.3      matt 	struct cac_drive_status dstatus;
    653  1.41.8.3      matt 	u_int32_t blks;
    654  1.41.8.3      matt 
    655  1.41.8.3      matt 	if (bv->bv_volid > sc->sc_nunits) {
    656  1.41.8.3      matt 		return EINVAL;
    657  1.41.8.3      matt 	}
    658  1.41.8.3      matt 	if (cac_cmd(sc, CAC_CMD_GET_LOG_DRV_INFO, &dinfo, sizeof(dinfo),
    659  1.41.8.3      matt 	    bv->bv_volid, 0, CAC_CCB_DATA_IN, NULL)) {
    660  1.41.8.3      matt 		return EIO;
    661  1.41.8.3      matt 	}
    662  1.41.8.3      matt 	if (cac_cmd(sc, CAC_CMD_SENSE_DRV_STATUS, &dstatus, sizeof(dstatus),
    663  1.41.8.3      matt 	    bv->bv_volid, 0, CAC_CCB_DATA_IN, NULL)) {
    664  1.41.8.3      matt 		return EIO;
    665  1.41.8.3      matt 	}
    666  1.41.8.3      matt 	blks = CAC_GET2(dinfo.ncylinders) * CAC_GET1(dinfo.nheads) *
    667  1.41.8.3      matt 	    CAC_GET1(dinfo.nsectors);
    668  1.41.8.3      matt 	bv->bv_size = (off_t)blks * CAC_GET2(dinfo.secsize);
    669  1.41.8.3      matt 	bv->bv_level = cac_level[CAC_GET1(dinfo.mirror)];	/*XXX limit check */
    670  1.41.8.3      matt 	bv->bv_nodisk = 0;		/* XXX */
    671  1.41.8.3      matt 	bv->bv_status = 0;		/* XXX */
    672  1.41.8.3      matt 	bv->bv_percent = -1;
    673  1.41.8.3      matt 	bv->bv_seconds = 0;
    674  1.41.8.3      matt 	if (dstatus.stat < sizeof(cac_stat)/sizeof(cac_stat[0]))
    675  1.41.8.3      matt 		bv->bv_status = cac_stat[dstatus.stat];
    676  1.41.8.3      matt 	if (bv->bv_status == BIOC_SVREBUILD ||
    677  1.41.8.3      matt 	    bv->bv_status == BIOC_SVBUILDING)
    678  1.41.8.3      matt 		bv->bv_percent = ((blks - CAC_GET4(dstatus.prog)) * 1000ULL) /
    679  1.41.8.3      matt 		    blks;
    680  1.41.8.3      matt 	return 0;
    681  1.41.8.3      matt }
    682  1.41.8.3      matt 
    683  1.41.8.3      matt int
    684  1.41.8.3      matt cac_create_sensors(struct cac_softc *sc)
    685  1.41.8.3      matt {
    686  1.41.8.3      matt 	int			i;
    687  1.41.8.3      matt 	int nsensors = sc->sc_nunits;
    688  1.41.8.3      matt 
    689  1.41.8.3      matt 	sc->sc_sme = sysmon_envsys_create();
    690  1.41.8.3      matt 	sc->sc_sensor = malloc(sizeof(envsys_data_t) * nsensors,
    691  1.41.8.3      matt 	    M_DEVBUF, M_NOWAIT | M_ZERO);
    692  1.41.8.3      matt 	if (sc->sc_sensor == NULL) {
    693  1.41.8.3      matt 		aprint_error("%s: can't allocate envsys_data_t\n",
    694  1.41.8.3      matt 		    sc->sc_dv.dv_xname);
    695  1.41.8.3      matt 		return(ENOMEM);
    696  1.41.8.3      matt 	}
    697  1.41.8.3      matt 
    698  1.41.8.3      matt 	for (i = 0; i < nsensors; i++) {
    699  1.41.8.3      matt 		sc->sc_sensor[i].units = ENVSYS_DRIVE;
    700  1.41.8.3      matt 		sc->sc_sensor[i].monitor = true;
    701  1.41.8.3      matt 		/* Enable monitoring for drive state changes */
    702  1.41.8.3      matt 		sc->sc_sensor[i].flags |= ENVSYS_FMONSTCHANGED;
    703  1.41.8.3      matt 		/* logical drives */
    704  1.41.8.3      matt 		snprintf(sc->sc_sensor[i].desc,
    705  1.41.8.3      matt 		    sizeof(sc->sc_sensor[i].desc), "%s:%d",
    706  1.41.8.3      matt 		    sc->sc_dv.dv_xname, i);
    707  1.41.8.3      matt 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
    708  1.41.8.3      matt 		    &sc->sc_sensor[i]))
    709  1.41.8.3      matt 			goto out;
    710  1.41.8.3      matt 	}
    711  1.41.8.3      matt 	sc->sc_sme->sme_name = sc->sc_dv.dv_xname;
    712  1.41.8.3      matt 	sc->sc_sme->sme_cookie = sc;
    713  1.41.8.3      matt 	sc->sc_sme->sme_refresh = cac_sensor_refresh;
    714  1.41.8.3      matt 	if (sysmon_envsys_register(sc->sc_sme)) {
    715  1.41.8.3      matt 		printf("%s: unable to register with sysmon\n", sc->sc_dv.dv_xname);
    716  1.41.8.3      matt 		return(1);
    717  1.41.8.3      matt 	}
    718  1.41.8.3      matt 	return (0);
    719  1.41.8.3      matt 
    720  1.41.8.3      matt out:
    721  1.41.8.3      matt 	free(sc->sc_sensor, M_DEVBUF);
    722  1.41.8.3      matt 	sysmon_envsys_destroy(sc->sc_sme);
    723  1.41.8.3      matt 	return EINVAL;
    724  1.41.8.3      matt }
    725  1.41.8.3      matt 
    726  1.41.8.3      matt void
    727  1.41.8.3      matt cac_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    728  1.41.8.3      matt {
    729  1.41.8.3      matt 	struct cac_softc	*sc = sme->sme_cookie;
    730  1.41.8.3      matt 	struct bioc_vol		bv;
    731  1.41.8.3      matt 	int s;
    732  1.41.8.3      matt 
    733  1.41.8.3      matt 	if (edata->sensor >= sc->sc_nunits)
    734  1.41.8.3      matt 		return;
    735  1.41.8.3      matt 
    736  1.41.8.3      matt 	bzero(&bv, sizeof(bv));
    737  1.41.8.3      matt 	bv.bv_volid = edata->sensor;
    738  1.41.8.3      matt 	s = splbio();
    739  1.41.8.3      matt 	if (cac_ioctl_vol(sc, &bv)) {
    740  1.41.8.3      matt 		splx(s);
    741  1.41.8.3      matt 		return;
    742  1.41.8.3      matt 	}
    743  1.41.8.3      matt 	splx(s);
    744  1.41.8.3      matt 
    745  1.41.8.3      matt 	switch(bv.bv_status) {
    746  1.41.8.3      matt 	case BIOC_SVOFFLINE:
    747  1.41.8.3      matt 		edata->value_cur = ENVSYS_DRIVE_FAIL;
    748  1.41.8.3      matt 		edata->state = ENVSYS_SCRITICAL;
    749  1.41.8.3      matt 		break;
    750  1.41.8.3      matt 
    751  1.41.8.3      matt 	case BIOC_SVDEGRADED:
    752  1.41.8.3      matt 		edata->value_cur = ENVSYS_DRIVE_PFAIL;
    753  1.41.8.3      matt 		edata->state = ENVSYS_SCRITICAL;
    754  1.41.8.3      matt 		break;
    755  1.41.8.3      matt 
    756  1.41.8.3      matt 	case BIOC_SVSCRUB:
    757  1.41.8.3      matt 	case BIOC_SVONLINE:
    758  1.41.8.3      matt 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
    759  1.41.8.3      matt 		edata->state = ENVSYS_SVALID;
    760  1.41.8.3      matt 		break;
    761  1.41.8.3      matt 
    762  1.41.8.3      matt 	case BIOC_SVREBUILD:
    763  1.41.8.3      matt 	case BIOC_SVBUILDING:
    764  1.41.8.3      matt 		edata->value_cur = ENVSYS_DRIVE_REBUILD;
    765  1.41.8.3      matt 		edata->state = ENVSYS_SVALID;
    766  1.41.8.3      matt 		break;
    767  1.41.8.3      matt 
    768  1.41.8.3      matt 	case BIOC_SVINVALID:
    769  1.41.8.3      matt 		/* FALLTRHOUGH */
    770  1.41.8.3      matt 	default:
    771  1.41.8.3      matt 		edata->value_cur = 0; /* unknown */
    772  1.41.8.3      matt 		edata->state = ENVSYS_SINVALID;
    773  1.41.8.3      matt 	}
    774  1.41.8.3      matt }
    775  1.41.8.3      matt #endif /* NBIO > 0 */
    776