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