Home | History | Annotate | Line # | Download | only in pci
arcmsr.c revision 1.17
      1 /*	$NetBSD: arcmsr.c,v 1.17 2008/03/01 16:33:29 xtraeme Exp $ */
      2 /*	$OpenBSD: arc.c,v 1.68 2007/10/27 03:28:27 dlg Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2007, 2008 Juan Romero Pardines <xtraeme (at) netbsd.org>
      6  * Copyright (c) 2006 David Gwynne <dlg (at) openbsd.org>
      7  *
      8  * Permission to use, copy, modify, and distribute this software for any
      9  * purpose with or without fee is hereby granted, provided that the above
     10  * copyright notice and this permission notice appear in all copies.
     11  *
     12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  */
     20 
     21 #include "bio.h"
     22 
     23 #include <sys/cdefs.h>
     24 __KERNEL_RCSID(0, "$NetBSD: arcmsr.c,v 1.17 2008/03/01 16:33:29 xtraeme Exp $");
     25 
     26 #include <sys/param.h>
     27 #include <sys/buf.h>
     28 #include <sys/kernel.h>
     29 #include <sys/malloc.h>
     30 #include <sys/device.h>
     31 #include <sys/kmem.h>
     32 #include <sys/kthread.h>
     33 #include <sys/mutex.h>
     34 #include <sys/condvar.h>
     35 #include <sys/rwlock.h>
     36 
     37 #if NBIO > 0
     38 #include <sys/ioctl.h>
     39 #include <dev/biovar.h>
     40 #endif
     41 
     42 #include <dev/pci/pcireg.h>
     43 #include <dev/pci/pcivar.h>
     44 #include <dev/pci/pcidevs.h>
     45 
     46 #include <dev/scsipi/scsipi_all.h>
     47 #include <dev/scsipi/scsi_all.h>
     48 #include <dev/scsipi/scsiconf.h>
     49 
     50 #include <dev/sysmon/sysmonvar.h>
     51 
     52 #include <sys/bus.h>
     53 
     54 #include <uvm/uvm_extern.h>	/* for PAGE_SIZE */
     55 
     56 #include <dev/pci/arcmsrvar.h>
     57 
     58 /* #define ARC_DEBUG */
     59 #ifdef ARC_DEBUG
     60 #define ARC_D_INIT	(1<<0)
     61 #define ARC_D_RW	(1<<1)
     62 #define ARC_D_DB	(1<<2)
     63 
     64 int arcdebug = 0;
     65 
     66 #define DPRINTF(p...)		do { if (arcdebug) printf(p); } while (0)
     67 #define DNPRINTF(n, p...)	do { if ((n) & arcdebug) printf(p); } while (0)
     68 
     69 #else
     70 #define DPRINTF(p...)		/* p */
     71 #define DNPRINTF(n, p...)	/* n, p */
     72 #endif
     73 
     74 /*
     75  * the fw header must always equal this.
     76  */
     77 static struct arc_fw_hdr arc_fw_hdr = { 0x5e, 0x01, 0x61 };
     78 
     79 /*
     80  * autoconf(9) glue.
     81  */
     82 static int 	arc_match(device_t, struct cfdata *, void *);
     83 static void 	arc_attach(device_t, device_t, void *);
     84 static int 	arc_detach(device_t, int);
     85 static bool 	arc_shutdown(device_t, int);
     86 static int 	arc_intr(void *);
     87 static void	arc_minphys(struct buf *);
     88 
     89 CFATTACH_DECL(arcmsr, sizeof(struct arc_softc),
     90 	arc_match, arc_attach, arc_detach, NULL);
     91 
     92 /*
     93  * bio(4) and sysmon_envsys(9) glue.
     94  */
     95 #if NBIO > 0
     96 static int 	arc_bioctl(struct device *, u_long, void *);
     97 static int 	arc_bio_inq(struct arc_softc *, struct bioc_inq *);
     98 static int 	arc_bio_vol(struct arc_softc *, struct bioc_vol *);
     99 static int	arc_bio_disk_volume(struct arc_softc *, struct bioc_disk *);
    100 static int	arc_bio_disk_novol(struct arc_softc *, struct bioc_disk *);
    101 static void	arc_bio_disk_filldata(struct arc_softc *, struct bioc_disk *,
    102 				      struct arc_fw_diskinfo *, int);
    103 static int 	arc_bio_alarm(struct arc_softc *, struct bioc_alarm *);
    104 static int 	arc_bio_alarm_state(struct arc_softc *, struct bioc_alarm *);
    105 static int 	arc_bio_getvol(struct arc_softc *, int,
    106 			       struct arc_fw_volinfo *);
    107 static int	arc_bio_setstate(struct arc_softc *, struct bioc_setstate *);
    108 static int 	arc_bio_volops(struct arc_softc *, struct bioc_volops *);
    109 static void 	arc_create_sensors(void *);
    110 static void 	arc_refresh_sensors(struct sysmon_envsys *, envsys_data_t *);
    111 static int	arc_fw_parse_status_code(struct arc_softc *, uint8_t *);
    112 #endif
    113 
    114 static int
    115 arc_match(device_t parent, struct cfdata *match, void *aux)
    116 {
    117 	struct pci_attach_args *pa = aux;
    118 
    119 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ARECA) {
    120 		switch (PCI_PRODUCT(pa->pa_id)) {
    121 		case PCI_PRODUCT_ARECA_ARC1110:
    122 		case PCI_PRODUCT_ARECA_ARC1120:
    123 		case PCI_PRODUCT_ARECA_ARC1130:
    124 		case PCI_PRODUCT_ARECA_ARC1160:
    125 		case PCI_PRODUCT_ARECA_ARC1170:
    126 		case PCI_PRODUCT_ARECA_ARC1200:
    127 		case PCI_PRODUCT_ARECA_ARC1202:
    128 		case PCI_PRODUCT_ARECA_ARC1210:
    129 		case PCI_PRODUCT_ARECA_ARC1220:
    130 		case PCI_PRODUCT_ARECA_ARC1230:
    131 		case PCI_PRODUCT_ARECA_ARC1260:
    132 		case PCI_PRODUCT_ARECA_ARC1270:
    133 		case PCI_PRODUCT_ARECA_ARC1280:
    134 		case PCI_PRODUCT_ARECA_ARC1380:
    135 		case PCI_PRODUCT_ARECA_ARC1381:
    136 		case PCI_PRODUCT_ARECA_ARC1680:
    137 		case PCI_PRODUCT_ARECA_ARC1681:
    138 			return 1;
    139 		default:
    140 			break;
    141 		}
    142 	}
    143 
    144 	return 0;
    145 }
    146 
    147 static void
    148 arc_attach(device_t parent, device_t self, void *aux)
    149 {
    150 	struct arc_softc	*sc = device_private(self);
    151 	struct pci_attach_args	*pa = aux;
    152 	struct scsipi_adapter	*adapt = &sc->sc_adapter;
    153 	struct scsipi_channel	*chan = &sc->sc_chan;
    154 
    155 	sc->sc_talking = 0;
    156 	rw_init(&sc->sc_rwlock);
    157 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_BIO);
    158 	cv_init(&sc->sc_condvar, "arcdb");
    159 
    160 	if (arc_map_pci_resources(sc, pa) != 0) {
    161 		/* error message printed by arc_map_pci_resources */
    162 		return;
    163 	}
    164 
    165 	if (arc_query_firmware(sc) != 0) {
    166 		/* error message printed by arc_query_firmware */
    167 		goto unmap_pci;
    168 	}
    169 
    170 	if (arc_alloc_ccbs(sc) != 0) {
    171 		/* error message printed by arc_alloc_ccbs */
    172 		goto unmap_pci;
    173 	}
    174 
    175 	if (!pmf_device_register1(self, NULL, NULL, arc_shutdown))
    176 		panic("%s: couldn't establish shutdown handler\n",
    177 		    device_xname(self));
    178 
    179 	memset(adapt, 0, sizeof(*adapt));
    180 	adapt->adapt_dev = self;
    181 	adapt->adapt_nchannels = 1;
    182 	adapt->adapt_openings = sc->sc_req_count / ARC_MAX_TARGET;
    183 	adapt->adapt_max_periph = adapt->adapt_openings;
    184 	adapt->adapt_minphys = arc_minphys;
    185 	adapt->adapt_request = arc_scsi_cmd;
    186 
    187 	memset(chan, 0, sizeof(*chan));
    188 	chan->chan_adapter = adapt;
    189 	chan->chan_bustype = &scsi_bustype;
    190 	chan->chan_nluns = ARC_MAX_LUN;
    191 	chan->chan_ntargets = ARC_MAX_TARGET;
    192 	chan->chan_id = ARC_MAX_TARGET;
    193 	chan->chan_channel = 0;
    194 	chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
    195 
    196 	/*
    197 	 * Save the device_t returned, because we could to attach
    198 	 * devices via the management interface.
    199 	 */
    200 	sc->sc_scsibus_dv = config_found(self, &sc->sc_chan, scsiprint);
    201 
    202 	/* enable interrupts */
    203 	arc_write(sc, ARC_REG_INTRMASK,
    204 	    ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRSTAT_DOORBELL));
    205 
    206 #if NBIO > 0
    207 	/*
    208 	 * Register the driver to bio(4) and setup the sensors.
    209 	 */
    210 	if (bio_register(self, arc_bioctl) != 0)
    211 		panic("%s: bioctl registration failed\n", device_xname(self));
    212 
    213 	/*
    214 	 * you need to talk to the firmware to get volume info. our firmware
    215 	 * interface relies on being able to sleep, so we need to use a thread
    216 	 * to do the work.
    217 	 */
    218 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    219 	    arc_create_sensors, sc, &sc->sc_lwp, "arcmsr_sensors") != 0)
    220 		panic("%s: unable to create a kernel thread for sensors\n",
    221 		    device_xname(self));
    222 #endif
    223 
    224         return;
    225 
    226 unmap_pci:
    227 	arc_unmap_pci_resources(sc);
    228 }
    229 
    230 static int
    231 arc_detach(device_t self, int flags)
    232 {
    233 	struct arc_softc		*sc = device_private(self);
    234 
    235 	if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0)
    236 		aprint_error("%s: timeout waiting to stop bg rebuild\n",
    237 		    device_xname(&sc->sc_dev));
    238 
    239 	if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0)
    240 		aprint_error("%s: timeout waiting to flush cache\n",
    241 		    device_xname(&sc->sc_dev));
    242 
    243 	return 0;
    244 }
    245 
    246 static bool
    247 arc_shutdown(device_t self, int how)
    248 {
    249 	struct arc_softc		*sc = device_private(self);
    250 
    251 	if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0)
    252 		aprint_error("%s: timeout waiting to stop bg rebuild\n",
    253 		    device_xname(&sc->sc_dev));
    254 
    255 	if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0)
    256 		aprint_error("%s: timeout waiting to flush cache\n",
    257 		    device_xname(&sc->sc_dev));
    258 
    259 	return true;
    260 }
    261 
    262 static void
    263 arc_minphys(struct buf *bp)
    264 {
    265 	if (bp->b_bcount > MAXPHYS)
    266 		bp->b_bcount = MAXPHYS;
    267 	minphys(bp);
    268 }
    269 
    270 static int
    271 arc_intr(void *arg)
    272 {
    273 	struct arc_softc		*sc = arg;
    274 	struct arc_ccb			*ccb = NULL;
    275 	char				*kva = ARC_DMA_KVA(sc->sc_requests);
    276 	struct arc_io_cmd		*cmd;
    277 	uint32_t			reg, intrstat;
    278 
    279 	mutex_spin_enter(&sc->sc_mutex);
    280 	intrstat = arc_read(sc, ARC_REG_INTRSTAT);
    281 	if (intrstat == 0x0) {
    282 		mutex_spin_exit(&sc->sc_mutex);
    283 		return 0;
    284 	}
    285 
    286 	intrstat &= ARC_REG_INTRSTAT_POSTQUEUE | ARC_REG_INTRSTAT_DOORBELL;
    287 	arc_write(sc, ARC_REG_INTRSTAT, intrstat);
    288 
    289 	if (intrstat & ARC_REG_INTRSTAT_DOORBELL) {
    290 		if (sc->sc_talking) {
    291 			arc_write(sc, ARC_REG_INTRMASK,
    292 			    ~ARC_REG_INTRMASK_POSTQUEUE);
    293 			cv_broadcast(&sc->sc_condvar);
    294 		} else {
    295 			/* otherwise drop it */
    296 			reg = arc_read(sc, ARC_REG_OUTB_DOORBELL);
    297 			arc_write(sc, ARC_REG_OUTB_DOORBELL, reg);
    298 			if (reg & ARC_REG_OUTB_DOORBELL_WRITE_OK)
    299 				arc_write(sc, ARC_REG_INB_DOORBELL,
    300 				    ARC_REG_INB_DOORBELL_READ_OK);
    301 		}
    302 	}
    303 	mutex_spin_exit(&sc->sc_mutex);
    304 
    305 	while ((reg = arc_pop(sc)) != 0xffffffff) {
    306 		cmd = (struct arc_io_cmd *)(kva +
    307 		    ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) -
    308 		    (uint32_t)ARC_DMA_DVA(sc->sc_requests)));
    309 		ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)];
    310 
    311 		bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
    312 		    ccb->ccb_offset, ARC_MAX_IOCMDLEN,
    313 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    314 
    315 		arc_scsi_cmd_done(sc, ccb, reg);
    316 	}
    317 
    318 
    319 	return 1;
    320 }
    321 
    322 void
    323 arc_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
    324 {
    325 	struct scsipi_periph		*periph;
    326 	struct scsipi_xfer		*xs;
    327 	struct scsipi_adapter		*adapt = chan->chan_adapter;
    328 	struct arc_softc		*sc = device_private(adapt->adapt_dev);
    329 	struct arc_ccb			*ccb;
    330 	struct arc_msg_scsicmd		*cmd;
    331 	uint32_t			reg;
    332 	uint8_t				target;
    333 
    334 	switch (req) {
    335 	case ADAPTER_REQ_GROW_RESOURCES:
    336 		/* Not supported. */
    337 		return;
    338 	case ADAPTER_REQ_SET_XFER_MODE:
    339 		/* Not supported. */
    340 		return;
    341 	case ADAPTER_REQ_RUN_XFER:
    342 		break;
    343 	}
    344 
    345 	mutex_spin_enter(&sc->sc_mutex);
    346 
    347 	xs = arg;
    348 	periph = xs->xs_periph;
    349 	target = periph->periph_target;
    350 
    351 	if (xs->cmdlen > ARC_MSG_CDBLEN) {
    352 		memset(&xs->sense, 0, sizeof(xs->sense));
    353 		xs->sense.scsi_sense.response_code = SSD_RCODE_VALID | 0x70;
    354 		xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
    355 		xs->sense.scsi_sense.asc = 0x20;
    356 		xs->error = XS_SENSE;
    357 		xs->status = SCSI_CHECK;
    358 		mutex_spin_exit(&sc->sc_mutex);
    359 		scsipi_done(xs);
    360 		return;
    361 	}
    362 
    363 	ccb = arc_get_ccb(sc);
    364 	if (ccb == NULL) {
    365 		xs->error = XS_RESOURCE_SHORTAGE;
    366 		mutex_spin_exit(&sc->sc_mutex);
    367 		scsipi_done(xs);
    368 		return;
    369 	}
    370 
    371 	ccb->ccb_xs = xs;
    372 
    373 	if (arc_load_xs(ccb) != 0) {
    374 		xs->error = XS_DRIVER_STUFFUP;
    375 		arc_put_ccb(sc, ccb);
    376 		mutex_spin_exit(&sc->sc_mutex);
    377 		scsipi_done(xs);
    378 		return;
    379 	}
    380 
    381 	cmd = &ccb->ccb_cmd->cmd;
    382 	reg = ccb->ccb_cmd_post;
    383 
    384 	/* bus is always 0 */
    385 	cmd->target = target;
    386 	cmd->lun = periph->periph_lun;
    387 	cmd->function = 1; /* XXX magic number */
    388 
    389 	cmd->cdb_len = xs->cmdlen;
    390 	cmd->sgl_len = ccb->ccb_dmamap->dm_nsegs;
    391 	if (xs->xs_control & XS_CTL_DATA_OUT)
    392 		cmd->flags = ARC_MSG_SCSICMD_FLAG_WRITE;
    393 	if (ccb->ccb_dmamap->dm_nsegs > ARC_SGL_256LEN) {
    394 		cmd->flags |= ARC_MSG_SCSICMD_FLAG_SGL_BSIZE_512;
    395 		reg |= ARC_REG_POST_QUEUE_BIGFRAME;
    396 	}
    397 
    398 	cmd->context = htole32(ccb->ccb_id);
    399 	cmd->data_len = htole32(xs->datalen);
    400 
    401 	memcpy(cmd->cdb, xs->cmd, xs->cmdlen);
    402 
    403 	/* we've built the command, let's put it on the hw */
    404 	bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
    405 	    ccb->ccb_offset, ARC_MAX_IOCMDLEN,
    406 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    407 
    408 	arc_push(sc, reg);
    409 	if (xs->xs_control & XS_CTL_POLL) {
    410 		if (arc_complete(sc, ccb, xs->timeout) != 0) {
    411 			xs->error = XS_DRIVER_STUFFUP;
    412 			mutex_spin_exit(&sc->sc_mutex);
    413 			scsipi_done(xs);
    414 			return;
    415 		}
    416 	}
    417 
    418 	mutex_spin_exit(&sc->sc_mutex);
    419 }
    420 
    421 int
    422 arc_load_xs(struct arc_ccb *ccb)
    423 {
    424 	struct arc_softc		*sc = ccb->ccb_sc;
    425 	struct scsipi_xfer		*xs = ccb->ccb_xs;
    426 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
    427 	struct arc_sge			*sgl = ccb->ccb_cmd->sgl, *sge;
    428 	uint64_t			addr;
    429 	int				i, error;
    430 
    431 	if (xs->datalen == 0)
    432 		return 0;
    433 
    434 	error = bus_dmamap_load(sc->sc_dmat, dmap,
    435 	    xs->data, xs->datalen, NULL,
    436 	    (xs->xs_control & XS_CTL_NOSLEEP) ?
    437 	    BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
    438 	if (error != 0) {
    439 		aprint_error("%s: error %d loading dmamap\n",
    440 		    device_xname(&sc->sc_dev), error);
    441 		return 1;
    442 	}
    443 
    444 	for (i = 0; i < dmap->dm_nsegs; i++) {
    445 		sge = &sgl[i];
    446 
    447 		sge->sg_hdr = htole32(ARC_SGE_64BIT | dmap->dm_segs[i].ds_len);
    448 		addr = dmap->dm_segs[i].ds_addr;
    449 		sge->sg_hi_addr = htole32((uint32_t)(addr >> 32));
    450 		sge->sg_lo_addr = htole32((uint32_t)addr);
    451 	}
    452 
    453 	bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    454 	    (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
    455 	    BUS_DMASYNC_PREWRITE);
    456 
    457 	return 0;
    458 }
    459 
    460 void
    461 arc_scsi_cmd_done(struct arc_softc *sc, struct arc_ccb *ccb, uint32_t reg)
    462 {
    463 	struct scsipi_xfer		*xs = ccb->ccb_xs;
    464 	struct arc_msg_scsicmd		*cmd;
    465 
    466 	if (xs->datalen != 0) {
    467 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
    468 		    ccb->ccb_dmamap->dm_mapsize,
    469 		    (xs->xs_control & XS_CTL_DATA_IN) ?
    470 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    471 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
    472 	}
    473 
    474 	/* timeout_del */
    475 	xs->status |= XS_STS_DONE;
    476 
    477 	if (reg & ARC_REG_REPLY_QUEUE_ERR) {
    478 		cmd = &ccb->ccb_cmd->cmd;
    479 
    480 		switch (cmd->status) {
    481 		case ARC_MSG_STATUS_SELTIMEOUT:
    482 		case ARC_MSG_STATUS_ABORTED:
    483 		case ARC_MSG_STATUS_INIT_FAIL:
    484 			xs->status = SCSI_OK;
    485 			xs->error = XS_SELTIMEOUT;
    486 			break;
    487 
    488 		case SCSI_CHECK:
    489 			memset(&xs->sense, 0, sizeof(xs->sense));
    490 			memcpy(&xs->sense, cmd->sense_data,
    491 			    min(ARC_MSG_SENSELEN, sizeof(xs->sense)));
    492 			xs->sense.scsi_sense.response_code =
    493 			    SSD_RCODE_VALID | 0x70;
    494 			xs->status = SCSI_CHECK;
    495 			xs->error = XS_SENSE;
    496 			xs->resid = 0;
    497 			break;
    498 
    499 		default:
    500 			/* unknown device status */
    501 			xs->error = XS_BUSY; /* try again later? */
    502 			xs->status = SCSI_BUSY;
    503 			break;
    504 		}
    505 	} else {
    506 		xs->status = SCSI_OK;
    507 		xs->error = XS_NOERROR;
    508 		xs->resid = 0;
    509 	}
    510 
    511 	arc_put_ccb(sc, ccb);
    512 	scsipi_done(xs);
    513 }
    514 
    515 int
    516 arc_complete(struct arc_softc *sc, struct arc_ccb *nccb, int timeout)
    517 {
    518 	struct arc_ccb			*ccb = NULL;
    519 	char				*kva = ARC_DMA_KVA(sc->sc_requests);
    520 	struct arc_io_cmd		*cmd;
    521 	uint32_t			reg;
    522 
    523 	do {
    524 		reg = arc_pop(sc);
    525 		if (reg == 0xffffffff) {
    526 			if (timeout-- == 0)
    527 				return 1;
    528 
    529 			delay(1000);
    530 			continue;
    531 		}
    532 
    533 		cmd = (struct arc_io_cmd *)(kva +
    534 		    ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) -
    535 		    ARC_DMA_DVA(sc->sc_requests)));
    536 		ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)];
    537 
    538 		bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
    539 		    ccb->ccb_offset, ARC_MAX_IOCMDLEN,
    540 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    541 
    542 		arc_scsi_cmd_done(sc, ccb, reg);
    543 	} while (nccb != ccb);
    544 
    545 	return 0;
    546 }
    547 
    548 int
    549 arc_map_pci_resources(struct arc_softc *sc, struct pci_attach_args *pa)
    550 {
    551 	pcireg_t			memtype;
    552 	pci_intr_handle_t		ih;
    553 
    554 	sc->sc_pc = pa->pa_pc;
    555 	sc->sc_tag = pa->pa_tag;
    556 	sc->sc_dmat = pa->pa_dmat;
    557 
    558 	memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, ARC_PCI_BAR);
    559 	if (pci_mapreg_map(pa, ARC_PCI_BAR, memtype, 0, &sc->sc_iot,
    560 	    &sc->sc_ioh, NULL, &sc->sc_ios) != 0) {
    561 		aprint_error(": unable to map system interface register\n");
    562 		return 1;
    563 	}
    564 
    565 	if (pci_intr_map(pa, &ih) != 0) {
    566 		aprint_error(": unable to map interrupt\n");
    567 		goto unmap;
    568 	}
    569 
    570 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
    571 	    arc_intr, sc);
    572 	if (sc->sc_ih == NULL) {
    573 		aprint_error(": unable to map interrupt [2]\n");
    574 		goto unmap;
    575 	}
    576 
    577 	aprint_normal("\n");
    578 	aprint_normal("%s: interrupting at %s\n",
    579 	    device_xname(&sc->sc_dev), pci_intr_string(pa->pa_pc, ih));
    580 
    581 	return 0;
    582 
    583 unmap:
    584 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    585 	sc->sc_ios = 0;
    586 	return 1;
    587 }
    588 
    589 void
    590 arc_unmap_pci_resources(struct arc_softc *sc)
    591 {
    592 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
    593 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    594 	sc->sc_ios = 0;
    595 }
    596 
    597 int
    598 arc_query_firmware(struct arc_softc *sc)
    599 {
    600 	struct arc_msg_firmware_info	fwinfo;
    601 	char				string[81]; /* sizeof(vendor)*2+1 */
    602 
    603 	if (arc_wait_eq(sc, ARC_REG_OUTB_ADDR1, ARC_REG_OUTB_ADDR1_FIRMWARE_OK,
    604 	    ARC_REG_OUTB_ADDR1_FIRMWARE_OK) != 0) {
    605 		aprint_debug("%s: timeout waiting for firmware ok\n",
    606 		    device_xname(&sc->sc_dev));
    607 		return 1;
    608 	}
    609 
    610 	if (arc_msg0(sc, ARC_REG_INB_MSG0_GET_CONFIG) != 0) {
    611 		aprint_debug("%s: timeout waiting for get config\n",
    612 		    device_xname(&sc->sc_dev));
    613 		return 1;
    614 	}
    615 
    616 	if (arc_msg0(sc, ARC_REG_INB_MSG0_START_BGRB) != 0) {
    617 		aprint_debug("%s: timeout waiting to start bg rebuild\n",
    618 		    device_xname(&sc->sc_dev));
    619 		return 1;
    620 	}
    621 
    622 	arc_read_region(sc, ARC_REG_MSGBUF, &fwinfo, sizeof(fwinfo));
    623 
    624 	DNPRINTF(ARC_D_INIT, "%s: signature: 0x%08x\n",
    625 	    device_xname(&sc->sc_dev), htole32(fwinfo.signature));
    626 
    627 	if (htole32(fwinfo.signature) != ARC_FWINFO_SIGNATURE_GET_CONFIG) {
    628 		aprint_error("%s: invalid firmware info from iop\n",
    629 		    device_xname(&sc->sc_dev));
    630 		return 1;
    631 	}
    632 
    633 	DNPRINTF(ARC_D_INIT, "%s: request_len: %d\n",
    634 	    device_xname(&sc->sc_dev),
    635 	    htole32(fwinfo.request_len));
    636 	DNPRINTF(ARC_D_INIT, "%s: queue_len: %d\n",
    637 	    device_xname(&sc->sc_dev),
    638 	    htole32(fwinfo.queue_len));
    639 	DNPRINTF(ARC_D_INIT, "%s: sdram_size: %d\n",
    640 	    device_xname(&sc->sc_dev),
    641 	    htole32(fwinfo.sdram_size));
    642 	DNPRINTF(ARC_D_INIT, "%s: sata_ports: %d\n",
    643 	    device_xname(&sc->sc_dev),
    644 	    htole32(fwinfo.sata_ports));
    645 
    646 	scsipi_strvis(string, 81, fwinfo.vendor, sizeof(fwinfo.vendor));
    647 	DNPRINTF(ARC_D_INIT, "%s: vendor: \"%s\"\n",
    648 	    device_xname(&sc->sc_dev), string);
    649 
    650 	scsipi_strvis(string, 17, fwinfo.model, sizeof(fwinfo.model));
    651 	aprint_normal("%s: Areca %s Host Adapter RAID controller\n",
    652 	    device_xname(&sc->sc_dev), string);
    653 
    654 	scsipi_strvis(string, 33, fwinfo.fw_version, sizeof(fwinfo.fw_version));
    655 	DNPRINTF(ARC_D_INIT, "%s: version: \"%s\"\n",
    656 	    device_xname(&sc->sc_dev), string);
    657 
    658 	aprint_normal("%s: %d ports, %dMB SDRAM, firmware <%s>\n",
    659 	    device_xname(&sc->sc_dev), htole32(fwinfo.sata_ports),
    660 	    htole32(fwinfo.sdram_size), string);
    661 
    662 	if (htole32(fwinfo.request_len) != ARC_MAX_IOCMDLEN) {
    663 		aprint_error("%s: unexpected request frame size (%d != %d)\n",
    664 		    device_xname(&sc->sc_dev),
    665 		    htole32(fwinfo.request_len), ARC_MAX_IOCMDLEN);
    666 		return 1;
    667 	}
    668 
    669 	sc->sc_req_count = htole32(fwinfo.queue_len);
    670 
    671 	return 0;
    672 }
    673 
    674 #if NBIO > 0
    675 static int
    676 arc_bioctl(struct device *self, u_long cmd, void *addr)
    677 {
    678 	struct arc_softc *sc = device_private(self);
    679 	int error = 0;
    680 
    681 	switch (cmd) {
    682 	case BIOCINQ:
    683 		error = arc_bio_inq(sc, (struct bioc_inq *)addr);
    684 		break;
    685 
    686 	case BIOCVOL:
    687 		error = arc_bio_vol(sc, (struct bioc_vol *)addr);
    688 		break;
    689 
    690 	case BIOCDISK:
    691 		error = arc_bio_disk_volume(sc, (struct bioc_disk *)addr);
    692 		break;
    693 
    694 	case BIOCDISK_NOVOL:
    695 		error = arc_bio_disk_novol(sc, (struct bioc_disk *)addr);
    696 		break;
    697 
    698 	case BIOCALARM:
    699 		error = arc_bio_alarm(sc, (struct bioc_alarm *)addr);
    700 		break;
    701 
    702 	case BIOCSETSTATE:
    703 		error = arc_bio_setstate(sc, (struct bioc_setstate *)addr);
    704 		break;
    705 
    706 	case BIOCVOLOPS:
    707 		error = arc_bio_volops(sc, (struct bioc_volops *)addr);
    708 		break;
    709 
    710 	default:
    711 		error = ENOTTY;
    712 		break;
    713 	}
    714 
    715 	return error;
    716 }
    717 
    718 static int
    719 arc_fw_parse_status_code(struct arc_softc *sc, uint8_t *reply)
    720 {
    721 	switch (*reply) {
    722 	case ARC_FW_CMD_RAIDINVAL:
    723 		printf("%s: firmware error (invalid raid set)\n",
    724 		    device_xname(&sc->sc_dev));
    725 		return EINVAL;
    726 	case ARC_FW_CMD_VOLINVAL:
    727 		printf("%s: firmware error (invalid volume set)\n",
    728 		    device_xname(&sc->sc_dev));
    729 		return EINVAL;
    730 	case ARC_FW_CMD_NORAID:
    731 		printf("%s: firmware error (unexistent raid set)\n",
    732 		    device_xname(&sc->sc_dev));
    733 		return ENODEV;
    734 	case ARC_FW_CMD_NOVOLUME:
    735 		printf("%s: firmware error (unexistent volume set)\n",
    736 		    device_xname(&sc->sc_dev));
    737 		return ENODEV;
    738 	case ARC_FW_CMD_NOPHYSDRV:
    739 		printf("%s: firmware error (unexistent physical drive)\n",
    740 		    device_xname(&sc->sc_dev));
    741 		return ENODEV;
    742 	case ARC_FW_CMD_PARAM_ERR:
    743 		printf("%s: firmware error (parameter error)\n",
    744 		    device_xname(&sc->sc_dev));
    745 		return EINVAL;
    746 	case ARC_FW_CMD_UNSUPPORTED:
    747 		printf("%s: firmware error (unsupported command)\n",
    748 		    device_xname(&sc->sc_dev));
    749 		return EOPNOTSUPP;
    750 	case ARC_FW_CMD_DISKCFG_CHGD:
    751 		printf("%s: firmware error (disk configuration changed)\n",
    752 		    device_xname(&sc->sc_dev));
    753 		return EINVAL;
    754 	case ARC_FW_CMD_PASS_INVAL:
    755 		printf("%s: firmware error (invalid password)\n",
    756 		    device_xname(&sc->sc_dev));
    757 		return EINVAL;
    758 	case ARC_FW_CMD_NODISKSPACE:
    759 		printf("%s: firmware error (no disk space available)\n",
    760 		    device_xname(&sc->sc_dev));
    761 		return EOPNOTSUPP;
    762 	case ARC_FW_CMD_CHECKSUM_ERR:
    763 		printf("%s: firmware error (checksum error)\n",
    764 		    device_xname(&sc->sc_dev));
    765 		return EINVAL;
    766 	case ARC_FW_CMD_PASS_REQD:
    767 		printf("%s: firmware error (password required)\n",
    768 		    device_xname(&sc->sc_dev));
    769 		return EPERM;
    770 	case ARC_FW_CMD_OK:
    771 	default:
    772 		return 0;
    773 	}
    774 }
    775 
    776 static int
    777 arc_bio_alarm(struct arc_softc *sc, struct bioc_alarm *ba)
    778 {
    779 	uint8_t	request[2], reply[1];
    780 	size_t	len;
    781 	int	error = 0;
    782 
    783 	switch (ba->ba_opcode) {
    784 	case BIOC_SAENABLE:
    785 	case BIOC_SADISABLE:
    786 		request[0] = ARC_FW_SET_ALARM;
    787 		request[1] = (ba->ba_opcode == BIOC_SAENABLE) ?
    788 		    ARC_FW_SET_ALARM_ENABLE : ARC_FW_SET_ALARM_DISABLE;
    789 		len = sizeof(request);
    790 
    791 		break;
    792 
    793 	case BIOC_SASILENCE:
    794 		request[0] = ARC_FW_MUTE_ALARM;
    795 		len = 1;
    796 
    797 		break;
    798 
    799 	case BIOC_GASTATUS:
    800 		/* system info is too big/ugly to deal with here */
    801 		return arc_bio_alarm_state(sc, ba);
    802 
    803 	default:
    804 		return EOPNOTSUPP;
    805 	}
    806 
    807 	error = arc_msgbuf(sc, request, len, reply, sizeof(reply));
    808 	if (error != 0)
    809 		return error;
    810 
    811 	return arc_fw_parse_status_code(sc, &reply[0]);
    812 }
    813 
    814 static int
    815 arc_bio_alarm_state(struct arc_softc *sc, struct bioc_alarm *ba)
    816 {
    817 	struct arc_fw_sysinfo	*sysinfo;
    818 	uint8_t			request;
    819 	int			error = 0;
    820 
    821 	sysinfo = kmem_zalloc(sizeof(*sysinfo), KM_SLEEP);
    822 
    823 	request = ARC_FW_SYSINFO;
    824 	error = arc_msgbuf(sc, &request, sizeof(request),
    825 	    sysinfo, sizeof(struct arc_fw_sysinfo));
    826 
    827 	if (error != 0)
    828 		goto out;
    829 
    830 	ba->ba_status = sysinfo->alarm;
    831 
    832 out:
    833 	kmem_free(sysinfo, sizeof(*sysinfo));
    834 	return error;
    835 }
    836 
    837 static int
    838 arc_bio_volops(struct arc_softc *sc, struct bioc_volops *bc)
    839 {
    840 	/* to create a raid set */
    841 	struct req_craidset {
    842 		uint8_t		cmdcode;
    843 		uint32_t	devmask;
    844 		uint8_t 	raidset_name[16];
    845 	} __packed;
    846 
    847 	/* to create a volume set */
    848 	struct req_cvolset {
    849 		uint8_t 	cmdcode;
    850 		uint8_t 	raidset;
    851 		uint8_t 	volset_name[16];
    852 		uint64_t	capacity;
    853 		uint8_t 	raidlevel;
    854 		uint8_t 	stripe;
    855 		uint8_t 	scsi_chan;
    856 		uint8_t 	scsi_target;
    857 		uint8_t 	scsi_lun;
    858 		uint8_t 	tagqueue;
    859 		uint8_t 	cache;
    860 		uint8_t 	speed;
    861 		uint8_t 	quick_init;
    862 	} __packed;
    863 
    864 	struct scsibus_softc	*scsibus_sc = NULL;
    865 	struct req_craidset	req_craidset;
    866 	struct req_cvolset 	req_cvolset;
    867 	uint8_t 		request[2];
    868 	uint8_t 		reply[1];
    869 	int 			error = 0;
    870 
    871 	switch (bc->bc_opcode) {
    872 	case BIOC_VCREATE_VOLUME:
    873 	    {
    874 		/*
    875 		 * Zero out the structs so that we use some defaults
    876 		 * in raid and volume sets.
    877 		 */
    878 		memset(&req_craidset, 0, sizeof(req_craidset));
    879 		memset(&req_cvolset, 0, sizeof(req_cvolset));
    880 
    881 		/*
    882 		 * Firstly we have to create the raid set and
    883 		 * use the default name for all them.
    884 		 */
    885 		req_craidset.cmdcode = ARC_FW_CREATE_RAIDSET;
    886 		req_craidset.devmask = bc->bc_devmask;
    887 		error = arc_msgbuf(sc, &req_craidset, sizeof(req_craidset),
    888 		    reply, sizeof(reply));
    889 		if (error != 0)
    890 			return error;
    891 
    892 		error = arc_fw_parse_status_code(sc, &reply[0]);
    893 		if (error) {
    894 			printf("%s: create raidset%d failed\n",
    895 			    device_xname(&sc->sc_dev), bc->bc_volid);
    896 			return error;
    897 		}
    898 
    899 		/*
    900 		 * At this point the raid set was created, so it's
    901 		 * time to create the volume set.
    902 		 */
    903 		req_cvolset.cmdcode = ARC_FW_CREATE_VOLUME;
    904 		req_cvolset.raidset = bc->bc_volid;
    905 		req_cvolset.capacity = bc->bc_size * ARC_BLOCKSIZE;
    906 
    907 		/*
    908 		 * Set the RAID level.
    909 		 */
    910 		switch (bc->bc_level) {
    911 		case 0:
    912 		case 1:
    913 			req_cvolset.raidlevel = bc->bc_level;
    914 			break;
    915 		case 3:
    916 			req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_3;
    917 			break;
    918 		case 5:
    919 			req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_5;
    920 			break;
    921 		case 6:
    922 			req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_6;
    923 			break;
    924 		default:
    925 			return EOPNOTSUPP;
    926 		}
    927 
    928 		/*
    929 		 * Set the stripe size.
    930 		 */
    931 		switch (bc->bc_stripe) {
    932 		case 4:
    933 			req_cvolset.stripe = 0;
    934 			break;
    935 		case 8:
    936 			req_cvolset.stripe = 1;
    937 			break;
    938 		case 16:
    939 			req_cvolset.stripe = 2;
    940 			break;
    941 		case 32:
    942 			req_cvolset.stripe = 3;
    943 			break;
    944 		case 64:
    945 			req_cvolset.stripe = 4;
    946 			break;
    947 		case 128:
    948 			req_cvolset.stripe = 5;
    949 			break;
    950 		default:
    951 			req_cvolset.stripe = 4; /* by default 64K */
    952 			break;
    953 		}
    954 
    955 		req_cvolset.scsi_chan = bc->bc_channel;
    956 		req_cvolset.scsi_target = bc->bc_target;
    957 		req_cvolset.scsi_lun = bc->bc_lun;
    958 		req_cvolset.tagqueue = 1; /* always enabled */
    959 		req_cvolset.cache = 1; /* always enabled */
    960 		req_cvolset.speed = 4; /* always max speed */
    961 
    962 		if (bc->bc_level == 1)
    963 			req_cvolset.quick_init = 1; /* foreground init */
    964 
    965 		error = arc_msgbuf(sc, &req_cvolset, sizeof(req_cvolset),
    966 		    reply, sizeof(reply));
    967 		if (error != 0)
    968 			return error;
    969 
    970 		error = arc_fw_parse_status_code(sc, &reply[0]);
    971 		if (error) {
    972 			printf("%s: create volumeset%d failed\n",
    973 			    device_xname(&sc->sc_dev), bc->bc_volid);
    974 			return error;
    975 		}
    976 
    977 		/*
    978 		 * If we are creating a RAID 1 or RAID 1+0 volume,
    979 		 * the volume will be created immediately but it won't
    980 		 * be available until the initialization is done... so
    981 		 * don't bother attaching the sd(4) device.
    982 		 */
    983 		if (bc->bc_level == 1)
    984 			break;
    985 
    986 		/*
    987 		 * Do a rescan on the bus to attach the device associated
    988 		 * with the new volume.
    989 		 */
    990 		scsibus_sc = device_private(sc->sc_scsibus_dv);
    991 		(void)scsi_probe_bus(scsibus_sc, bc->bc_target, bc->bc_lun);
    992 
    993 		break;
    994 	    }
    995 	case BIOC_VREMOVE_VOLUME:
    996 	    {
    997 		/*
    998 		 * Remove the volume set specified in bc_volid.
    999 		 */
   1000 		request[0] = ARC_FW_DELETE_VOLUME;
   1001 		request[1] = bc->bc_volid;
   1002 		error = arc_msgbuf(sc, request, sizeof(request),
   1003 		    reply, sizeof(reply));
   1004 		if (error != 0)
   1005 			return error;
   1006 
   1007 		error = arc_fw_parse_status_code(sc, &reply[0]);
   1008 		if (error) {
   1009 			printf("%s: delete volumeset%d failed\n",
   1010 			    device_xname(&sc->sc_dev), bc->bc_volid);
   1011 			return error;
   1012 		}
   1013 
   1014 		/*
   1015 		 * Detach the sd(4) device associated with the volume,
   1016 		 * but if there's an error don't make it a priority.
   1017 		 */
   1018 		error = scsipi_target_detach(&sc->sc_chan, bc->bc_target,
   1019 					     bc->bc_lun, 0);
   1020 		if (error)
   1021 			printf("%s: couldn't detach sd device for volume %d "
   1022 			    "at %u:%u.%u (error=%d)\n",
   1023 			    device_xname(&sc->sc_dev), bc->bc_volid,
   1024 			    bc->bc_channel, bc->bc_target, bc->bc_lun, error);
   1025 
   1026 		/*
   1027 		 * and remove the raid set specified in bc_volid,
   1028 		 * we only care about volumes.
   1029 		 */
   1030 		request[0] = ARC_FW_DELETE_RAIDSET;
   1031 		request[1] = bc->bc_volid;
   1032 		error = arc_msgbuf(sc, request, sizeof(request),
   1033 		    reply, sizeof(reply));
   1034 		if (error != 0)
   1035 			return error;
   1036 
   1037 		error = arc_fw_parse_status_code(sc, &reply[0]);
   1038 		if (error) {
   1039 			printf("%s: delete raidset%d failed\n",
   1040 			    device_xname(&sc->sc_dev), bc->bc_volid);
   1041 			return error;
   1042 		}
   1043 
   1044 		break;
   1045 	    }
   1046 	default:
   1047 		return EOPNOTSUPP;
   1048 	}
   1049 
   1050 	return error;
   1051 }
   1052 
   1053 static int
   1054 arc_bio_setstate(struct arc_softc *sc, struct bioc_setstate *bs)
   1055 {
   1056 	/* for a hotspare disk */
   1057 	struct request_hs {
   1058 		uint8_t		cmdcode;
   1059 		uint32_t	devmask;
   1060 	} __packed;
   1061 
   1062 	/* for a pass-through disk */
   1063 	struct request_pt {
   1064 		uint8_t 	cmdcode;
   1065 		uint8_t		devid;
   1066 		uint8_t		scsi_chan;
   1067 		uint8_t 	scsi_id;
   1068 		uint8_t 	scsi_lun;
   1069 		uint8_t 	tagged_queue;
   1070 		uint8_t 	cache_mode;
   1071 		uint8_t 	max_speed;
   1072 	} __packed;
   1073 
   1074 	struct scsibus_softc	*scsibus_sc = NULL;
   1075 	struct request_hs	req_hs; /* to add/remove hotspare */
   1076 	struct request_pt	req_pt;	/* to add a pass-through */
   1077 	uint8_t			req_gen[2];
   1078 	uint8_t			reply[1];
   1079 	int			error = 0;
   1080 
   1081 	switch (bs->bs_status) {
   1082 	case BIOC_SSHOTSPARE:
   1083 	    {
   1084 		req_hs.cmdcode = ARC_FW_CREATE_HOTSPARE;
   1085 		req_hs.devmask = (1 << bs->bs_target);
   1086 		goto hotspare;
   1087 	    }
   1088 	case BIOC_SSDELHOTSPARE:
   1089 	    {
   1090 		req_hs.cmdcode = ARC_FW_DELETE_HOTSPARE;
   1091 		req_hs.devmask = (1 << bs->bs_target);
   1092 		goto hotspare;
   1093 	    }
   1094 	case BIOC_SSPASSTHRU:
   1095 	    {
   1096 		req_pt.cmdcode = ARC_FW_CREATE_PASSTHRU;
   1097 		req_pt.devid = bs->bs_other_id; /* this wants device# */
   1098 		req_pt.scsi_chan = bs->bs_channel;
   1099 		req_pt.scsi_id = bs->bs_target;
   1100 		req_pt.scsi_lun = bs->bs_lun;
   1101 		req_pt.tagged_queue = 1; /* always enabled */
   1102 		req_pt.cache_mode = 1; /* always enabled */
   1103 		req_pt.max_speed = 4; /* always max speed */
   1104 
   1105 		error = arc_msgbuf(sc, &req_pt, sizeof(req_pt),
   1106 		    reply, sizeof(reply));
   1107 		if (error != 0)
   1108 			return error;
   1109 
   1110 		/*
   1111 		 * Do a rescan on the bus to attach the new device
   1112 		 * associated with the pass-through disk.
   1113 		 */
   1114 		scsibus_sc = device_private(sc->sc_scsibus_dv);
   1115 		(void)scsi_probe_bus(scsibus_sc, bs->bs_target, bs->bs_lun);
   1116 
   1117 		goto out;
   1118 	    }
   1119 	case BIOC_SSDELPASSTHRU:
   1120 	    {
   1121 		req_gen[0] = ARC_FW_DELETE_PASSTHRU;
   1122 		req_gen[1] = bs->bs_target;
   1123 		error = arc_msgbuf(sc, &req_gen, sizeof(req_gen),
   1124 		    reply, sizeof(reply));
   1125 		if (error != 0)
   1126 			return error;
   1127 
   1128 		/*
   1129 		 * Detach the sd device associated with this pass-through disk.
   1130 		 */
   1131 		error = scsipi_target_detach(&sc->sc_chan, bs->bs_target,
   1132 					     bs->bs_lun, 0);
   1133 		if (error)
   1134 			printf("%s: couldn't detach sd device for the "
   1135 			    "pass-through disk at %u:%u.%u (error=%d)\n",
   1136 			    device_xname(&sc->sc_dev),
   1137 			    bs->bs_channel, bs->bs_target, bs->bs_lun, error);
   1138 
   1139 		goto out;
   1140 	    }
   1141 	case BIOC_SSCHECKSTART_VOL:
   1142 	    {
   1143 		req_gen[0] = ARC_FW_START_CHECKVOL;
   1144 		req_gen[1] = bs->bs_volid;
   1145 		error = arc_msgbuf(sc, &req_gen, sizeof(req_gen),
   1146 		    reply, sizeof(reply));
   1147 		if (error != 0)
   1148 			return error;
   1149 
   1150 		goto out;
   1151 	    }
   1152 	case BIOC_SSCHECKSTOP_VOL:
   1153 	    {
   1154 		uint8_t req = ARC_FW_STOP_CHECKVOL;
   1155 		error = arc_msgbuf(sc, &req, 1, reply, sizeof(reply));
   1156 		if (error != 0)
   1157 			return error;
   1158 
   1159 		goto out;
   1160 	    }
   1161 	default:
   1162 		return EOPNOTSUPP;
   1163 	}
   1164 
   1165 hotspare:
   1166 	error = arc_msgbuf(sc, &req_hs, sizeof(req_hs),
   1167 	    reply, sizeof(reply));
   1168 	if (error != 0)
   1169 		return error;
   1170 
   1171 out:
   1172 	return arc_fw_parse_status_code(sc, &reply[0]);
   1173 }
   1174 
   1175 static int
   1176 arc_bio_inq(struct arc_softc *sc, struct bioc_inq *bi)
   1177 {
   1178 	uint8_t			request[2];
   1179 	struct arc_fw_sysinfo	*sysinfo = NULL;
   1180 	struct arc_fw_raidinfo	*raidinfo;
   1181 	int			nvols = 0, i;
   1182 	int			error = 0;
   1183 
   1184 	raidinfo = kmem_zalloc(sizeof(*raidinfo), KM_SLEEP);
   1185 
   1186 	if (!sc->sc_maxraidset || !sc->sc_maxvolset || !sc->sc_cchans) {
   1187 		sysinfo = kmem_zalloc(sizeof(*sysinfo), KM_SLEEP);
   1188 
   1189 		request[0] = ARC_FW_SYSINFO;
   1190 		error = arc_msgbuf(sc, request, 1, sysinfo,
   1191 		    sizeof(struct arc_fw_sysinfo));
   1192 		if (error != 0)
   1193 			goto out;
   1194 
   1195 		sc->sc_maxraidset = sysinfo->max_raid_set;
   1196 		sc->sc_maxvolset = sysinfo->max_volume_set;
   1197 		sc->sc_cchans = sysinfo->ide_channels;
   1198 	}
   1199 
   1200 	request[0] = ARC_FW_RAIDINFO;
   1201 	for (i = 0; i < sc->sc_maxraidset; i++) {
   1202 		request[1] = i;
   1203 		error = arc_msgbuf(sc, request, sizeof(request), raidinfo,
   1204 		    sizeof(struct arc_fw_raidinfo));
   1205 		if (error != 0)
   1206 			goto out;
   1207 
   1208 		if (raidinfo->volumes)
   1209 			nvols++;
   1210 	}
   1211 
   1212 	strlcpy(bi->bi_dev, device_xname(&sc->sc_dev), sizeof(bi->bi_dev));
   1213 	bi->bi_novol = nvols;
   1214 	bi->bi_nodisk = sc->sc_cchans;
   1215 
   1216 out:
   1217 	if (sysinfo)
   1218 		kmem_free(sysinfo, sizeof(*sysinfo));
   1219 	kmem_free(raidinfo, sizeof(*raidinfo));
   1220 	return error;
   1221 }
   1222 
   1223 static int
   1224 arc_bio_getvol(struct arc_softc *sc, int vol, struct arc_fw_volinfo *volinfo)
   1225 {
   1226 	uint8_t			request[2];
   1227 	int			error = 0;
   1228 	int			nvols = 0, i;
   1229 
   1230 	request[0] = ARC_FW_VOLINFO;
   1231 	for (i = 0; i < sc->sc_maxvolset; i++) {
   1232 		request[1] = i;
   1233 		error = arc_msgbuf(sc, request, sizeof(request), volinfo,
   1234 		    sizeof(struct arc_fw_volinfo));
   1235 		if (error != 0)
   1236 			goto out;
   1237 
   1238 		if (volinfo->capacity == 0 && volinfo->capacity2 == 0)
   1239 			continue;
   1240 
   1241 		if (nvols == vol)
   1242 			break;
   1243 
   1244 		nvols++;
   1245 	}
   1246 
   1247 	if (nvols != vol ||
   1248 	    (volinfo->capacity == 0 && volinfo->capacity2 == 0)) {
   1249 		error = ENODEV;
   1250 		goto out;
   1251 	}
   1252 
   1253 out:
   1254 	return error;
   1255 }
   1256 
   1257 static int
   1258 arc_bio_vol(struct arc_softc *sc, struct bioc_vol *bv)
   1259 {
   1260 	struct arc_fw_volinfo	*volinfo;
   1261 	uint64_t		blocks;
   1262 	uint32_t		status;
   1263 	int			error = 0;
   1264 
   1265 	volinfo = kmem_zalloc(sizeof(*volinfo), KM_SLEEP);
   1266 
   1267 	error = arc_bio_getvol(sc, bv->bv_volid, volinfo);
   1268 	if (error != 0)
   1269 		goto out;
   1270 
   1271 	bv->bv_percent = -1;
   1272 	bv->bv_seconds = 0;
   1273 
   1274 	status = htole32(volinfo->volume_status);
   1275 	if (status == 0x0) {
   1276 		if (htole32(volinfo->fail_mask) == 0x0)
   1277 			bv->bv_status = BIOC_SVONLINE;
   1278 		else
   1279 			bv->bv_status = BIOC_SVDEGRADED;
   1280 	} else if (status & ARC_FW_VOL_STATUS_NEED_REGEN) {
   1281 		bv->bv_status = BIOC_SVDEGRADED;
   1282 	} else if (status & ARC_FW_VOL_STATUS_FAILED) {
   1283 		bv->bv_status = BIOC_SVOFFLINE;
   1284 	} else if (status & ARC_FW_VOL_STATUS_INITTING) {
   1285 		bv->bv_status = BIOC_SVBUILDING;
   1286 		bv->bv_percent = htole32(volinfo->progress);
   1287 	} else if (status & ARC_FW_VOL_STATUS_REBUILDING) {
   1288 		bv->bv_status = BIOC_SVREBUILD;
   1289 		bv->bv_percent = htole32(volinfo->progress);
   1290 	} else if (status & ARC_FW_VOL_STATUS_MIGRATING) {
   1291 		bv->bv_status = BIOC_SVMIGRATING;
   1292 		bv->bv_percent = htole32(volinfo->progress);
   1293 	} else if (status & ARC_FW_VOL_STATUS_CHECKING) {
   1294 		bv->bv_status = BIOC_SVCHECKING;
   1295 		bv->bv_percent = htole32(volinfo->progress);
   1296 	} else if (status & ARC_FW_VOL_STATUS_NEED_INIT) {
   1297 		bv->bv_status = BIOC_SVOFFLINE;
   1298 	} else {
   1299 		printf("%s: volume %d status 0x%x\n",
   1300 		    device_xname(&sc->sc_dev), bv->bv_volid, status);
   1301 	}
   1302 
   1303 	blocks = (uint64_t)htole32(volinfo->capacity2) << 32;
   1304 	blocks += (uint64_t)htole32(volinfo->capacity);
   1305 	bv->bv_size = blocks * ARC_BLOCKSIZE; /* XXX */
   1306 
   1307 	switch (volinfo->raid_level) {
   1308 	case ARC_FW_VOL_RAIDLEVEL_0:
   1309 		bv->bv_level = 0;
   1310 		break;
   1311 	case ARC_FW_VOL_RAIDLEVEL_1:
   1312 		bv->bv_level = 1;
   1313 		break;
   1314 	case ARC_FW_VOL_RAIDLEVEL_3:
   1315 		bv->bv_level = 3;
   1316 		break;
   1317 	case ARC_FW_VOL_RAIDLEVEL_5:
   1318 		bv->bv_level = 5;
   1319 		break;
   1320 	case ARC_FW_VOL_RAIDLEVEL_6:
   1321 		bv->bv_level = 6;
   1322 		break;
   1323 	case ARC_FW_VOL_RAIDLEVEL_PASSTHRU:
   1324 		bv->bv_level = BIOC_SVOL_PASSTHRU;
   1325 		break;
   1326 	default:
   1327 		bv->bv_level = -1;
   1328 		break;
   1329 	}
   1330 
   1331 	bv->bv_nodisk = volinfo->member_disks;
   1332 	bv->bv_stripe_size = volinfo->stripe_size / 2;
   1333 	snprintf(bv->bv_dev, sizeof(bv->bv_dev), "sd%d", bv->bv_volid);
   1334 	scsipi_strvis(bv->bv_vendor, sizeof(bv->bv_vendor), volinfo->set_name,
   1335 	    sizeof(volinfo->set_name));
   1336 
   1337 out:
   1338 	kmem_free(volinfo, sizeof(*volinfo));
   1339 	return error;
   1340 }
   1341 
   1342 static int
   1343 arc_bio_disk_novol(struct arc_softc *sc, struct bioc_disk *bd)
   1344 {
   1345 	struct arc_fw_diskinfo	*diskinfo;
   1346 	uint8_t			request[2];
   1347 	int			error = 0;
   1348 
   1349 	diskinfo = kmem_zalloc(sizeof(*diskinfo), KM_SLEEP);
   1350 
   1351 	if (bd->bd_diskid >= sc->sc_cchans) {
   1352 		error = ENODEV;
   1353 		goto out;
   1354 	}
   1355 
   1356 	request[0] = ARC_FW_DISKINFO;
   1357 	request[1] = bd->bd_diskid;
   1358 	error = arc_msgbuf(sc, request, sizeof(request),
   1359 	    diskinfo, sizeof(struct arc_fw_diskinfo));
   1360 	if (error != 0)
   1361 		goto out;
   1362 
   1363 	/* skip disks with no capacity */
   1364 	if (htole32(diskinfo->capacity) == 0 &&
   1365 	    htole32(diskinfo->capacity2) == 0)
   1366 		goto out;
   1367 
   1368 	bd->bd_disknovol = true;
   1369 	arc_bio_disk_filldata(sc, bd, diskinfo, bd->bd_diskid);
   1370 
   1371 out:
   1372 	kmem_free(diskinfo, sizeof(*diskinfo));
   1373 	return error;
   1374 }
   1375 
   1376 static void
   1377 arc_bio_disk_filldata(struct arc_softc *sc, struct bioc_disk *bd,
   1378 		     struct arc_fw_diskinfo *diskinfo, int diskid)
   1379 {
   1380 	uint64_t		blocks;
   1381 	char			model[81];
   1382 	char			serial[41];
   1383 	char			rev[17];
   1384 
   1385 	switch (htole32(diskinfo->device_state)) {
   1386 	case ARC_FW_DISK_PASSTHRU:
   1387 		bd->bd_status = BIOC_SDPASSTHRU;
   1388 		break;
   1389 	case ARC_FW_DISK_RAIDMEMBER:
   1390 		bd->bd_status = BIOC_SDONLINE;
   1391 		break;
   1392 	case ARC_FW_DISK_HOTSPARE:
   1393 		bd->bd_status = BIOC_SDHOTSPARE;
   1394 		break;
   1395 	case ARC_FW_DISK_UNUSED:
   1396 		bd->bd_status = BIOC_SDUNUSED;
   1397 		break;
   1398 	case 0:
   1399 		/* disk has been disconnected */
   1400 		bd->bd_status = BIOC_SDOFFLINE;
   1401 		bd->bd_channel = 1;
   1402 		bd->bd_target = 0;
   1403 		bd->bd_lun = 0;
   1404 		strlcpy(bd->bd_vendor, "disk missing", sizeof(bd->bd_vendor));
   1405 		break;
   1406 	default:
   1407 		printf("%s: unknown disk device_state: 0x%x\n", __func__,
   1408 		    htole32(diskinfo->device_state));
   1409 		bd->bd_status = BIOC_SDINVALID;
   1410 		return;
   1411 	}
   1412 
   1413 	blocks = (uint64_t)htole32(diskinfo->capacity2) << 32;
   1414 	blocks += (uint64_t)htole32(diskinfo->capacity);
   1415 	bd->bd_size = blocks * ARC_BLOCKSIZE; /* XXX */
   1416 
   1417 	scsipi_strvis(model, 81, diskinfo->model, sizeof(diskinfo->model));
   1418 	scsipi_strvis(serial, 41, diskinfo->serial, sizeof(diskinfo->serial));
   1419 	scsipi_strvis(rev, 17, diskinfo->firmware_rev,
   1420 	    sizeof(diskinfo->firmware_rev));
   1421 
   1422 	snprintf(bd->bd_vendor, sizeof(bd->bd_vendor), "%s %s", model, rev);
   1423 	strlcpy(bd->bd_serial, serial, sizeof(bd->bd_serial));
   1424 
   1425 #if 0
   1426 	bd->bd_channel = diskinfo->scsi_attr.channel;
   1427 	bd->bd_target = diskinfo->scsi_attr.target;
   1428 	bd->bd_lun = diskinfo->scsi_attr.lun;
   1429 #endif
   1430 
   1431 	/*
   1432 	 * the firwmare doesnt seem to fill scsi_attr in, so fake it with
   1433 	 * the diskid.
   1434 	 */
   1435 	bd->bd_channel = 0;
   1436 	bd->bd_target = diskid;
   1437 	bd->bd_lun = 0;
   1438 }
   1439 
   1440 static int
   1441 arc_bio_disk_volume(struct arc_softc *sc, struct bioc_disk *bd)
   1442 {
   1443 	struct arc_fw_raidinfo	*raidinfo;
   1444 	struct arc_fw_volinfo	*volinfo;
   1445 	struct arc_fw_diskinfo	*diskinfo;
   1446 	uint8_t			request[2];
   1447 	int			error = 0;
   1448 
   1449 	volinfo = kmem_zalloc(sizeof(*volinfo), KM_SLEEP);
   1450 	raidinfo = kmem_zalloc(sizeof(*raidinfo), KM_SLEEP);
   1451 	diskinfo = kmem_zalloc(sizeof(*diskinfo), KM_SLEEP);
   1452 
   1453 	error = arc_bio_getvol(sc, bd->bd_volid, volinfo);
   1454 	if (error != 0)
   1455 		goto out;
   1456 
   1457 	request[0] = ARC_FW_RAIDINFO;
   1458 	request[1] = volinfo->raid_set_number;
   1459 
   1460 	error = arc_msgbuf(sc, request, sizeof(request), raidinfo,
   1461 	    sizeof(struct arc_fw_raidinfo));
   1462 	if (error != 0)
   1463 		goto out;
   1464 
   1465 	if (bd->bd_diskid >= sc->sc_cchans ||
   1466 	    bd->bd_diskid >= raidinfo->member_devices) {
   1467 		error = ENODEV;
   1468 		goto out;
   1469 	}
   1470 
   1471 	if (raidinfo->device_array[bd->bd_diskid] == 0xff) {
   1472 		/*
   1473 		 * The disk has been disconnected, mark it offline
   1474 		 * and put it on another bus.
   1475 		 */
   1476 		bd->bd_channel = 1;
   1477 		bd->bd_target = 0;
   1478 		bd->bd_lun = 0;
   1479 		bd->bd_status = BIOC_SDOFFLINE;
   1480 		strlcpy(bd->bd_vendor, "disk missing", sizeof(bd->bd_vendor));
   1481 		goto out;
   1482 	}
   1483 
   1484 	request[0] = ARC_FW_DISKINFO;
   1485 	request[1] = raidinfo->device_array[bd->bd_diskid];
   1486 	error = arc_msgbuf(sc, request, sizeof(request), diskinfo,
   1487 	    sizeof(struct arc_fw_diskinfo));
   1488 	if (error != 0)
   1489 		goto out;
   1490 
   1491 	/* now fill our bio disk with data from the firmware */
   1492 	arc_bio_disk_filldata(sc, bd, diskinfo,
   1493 	    raidinfo->device_array[bd->bd_diskid]);
   1494 
   1495 out:
   1496 	kmem_free(raidinfo, sizeof(*raidinfo));
   1497 	kmem_free(volinfo, sizeof(*volinfo));
   1498 	kmem_free(diskinfo, sizeof(*diskinfo));
   1499 	return error;
   1500 }
   1501 #endif /* NBIO > 0 */
   1502 
   1503 uint8_t
   1504 arc_msg_cksum(void *cmd, uint16_t len)
   1505 {
   1506 	uint8_t	*buf = cmd;
   1507 	uint8_t	cksum;
   1508 	int	i;
   1509 
   1510 	cksum = (uint8_t)(len >> 8) + (uint8_t)len;
   1511 	for (i = 0; i < len; i++)
   1512 		cksum += buf[i];
   1513 
   1514 	return cksum;
   1515 }
   1516 
   1517 
   1518 int
   1519 arc_msgbuf(struct arc_softc *sc, void *wptr, size_t wbuflen, void *rptr,
   1520 	   size_t rbuflen)
   1521 {
   1522 	uint8_t			rwbuf[ARC_REG_IOC_RWBUF_MAXLEN];
   1523 	uint8_t			*wbuf, *rbuf;
   1524 	int			wlen, wdone = 0, rlen, rdone = 0;
   1525 	struct arc_fw_bufhdr	*bufhdr;
   1526 	uint32_t		reg, rwlen;
   1527 	int			error = 0;
   1528 #ifdef ARC_DEBUG
   1529 	int			i;
   1530 #endif
   1531 
   1532 	wbuf = rbuf = NULL;
   1533 
   1534 	DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wbuflen: %d rbuflen: %d\n",
   1535 	    device_xname(&sc->sc_dev), wbuflen, rbuflen);
   1536 
   1537 	wlen = sizeof(struct arc_fw_bufhdr) + wbuflen + 1; /* 1 for cksum */
   1538 	wbuf = kmem_alloc(wlen, KM_SLEEP);
   1539 
   1540 	rlen = sizeof(struct arc_fw_bufhdr) + rbuflen + 1; /* 1 for cksum */
   1541 	rbuf = kmem_alloc(rlen, KM_SLEEP);
   1542 
   1543 	DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wlen: %d rlen: %d\n",
   1544 	    device_xname(&sc->sc_dev), wlen, rlen);
   1545 
   1546 	bufhdr = (struct arc_fw_bufhdr *)wbuf;
   1547 	bufhdr->hdr = arc_fw_hdr;
   1548 	bufhdr->len = htole16(wbuflen);
   1549 	memcpy(wbuf + sizeof(struct arc_fw_bufhdr), wptr, wbuflen);
   1550 	wbuf[wlen - 1] = arc_msg_cksum(wptr, wbuflen);
   1551 
   1552 	arc_lock(sc);
   1553 	if (arc_read(sc, ARC_REG_OUTB_DOORBELL) != 0) {
   1554 		error = EBUSY;
   1555 		goto out;
   1556 	}
   1557 
   1558 	reg = ARC_REG_OUTB_DOORBELL_READ_OK;
   1559 
   1560 	do {
   1561 		if ((reg & ARC_REG_OUTB_DOORBELL_READ_OK) && wdone < wlen) {
   1562 			memset(rwbuf, 0, sizeof(rwbuf));
   1563 			rwlen = (wlen - wdone) % sizeof(rwbuf);
   1564 			memcpy(rwbuf, &wbuf[wdone], rwlen);
   1565 
   1566 #ifdef ARC_DEBUG
   1567 			if (arcdebug & ARC_D_DB) {
   1568 				printf("%s: write %d:",
   1569 				    device_xname(&sc->sc_dev), rwlen);
   1570 				for (i = 0; i < rwlen; i++)
   1571 					printf(" 0x%02x", rwbuf[i]);
   1572 				printf("\n");
   1573 			}
   1574 #endif
   1575 
   1576 			/* copy the chunk to the hw */
   1577 			arc_write(sc, ARC_REG_IOC_WBUF_LEN, rwlen);
   1578 			arc_write_region(sc, ARC_REG_IOC_WBUF, rwbuf,
   1579 			    sizeof(rwbuf));
   1580 
   1581 			/* say we have a buffer for the hw */
   1582 			arc_write(sc, ARC_REG_INB_DOORBELL,
   1583 			    ARC_REG_INB_DOORBELL_WRITE_OK);
   1584 
   1585 			wdone += rwlen;
   1586 		}
   1587 
   1588 		while ((reg = arc_read(sc, ARC_REG_OUTB_DOORBELL)) == 0)
   1589 			arc_wait(sc);
   1590 
   1591 		arc_write(sc, ARC_REG_OUTB_DOORBELL, reg);
   1592 
   1593 		DNPRINTF(ARC_D_DB, "%s: reg: 0x%08x\n",
   1594 		    device_xname(&sc->sc_dev), reg);
   1595 
   1596 		if ((reg & ARC_REG_OUTB_DOORBELL_WRITE_OK) && rdone < rlen) {
   1597 			rwlen = arc_read(sc, ARC_REG_IOC_RBUF_LEN);
   1598 			if (rwlen > sizeof(rwbuf)) {
   1599 				DNPRINTF(ARC_D_DB, "%s:  rwlen too big\n",
   1600 				    device_xname(&sc->sc_dev));
   1601 				error = EIO;
   1602 				goto out;
   1603 			}
   1604 
   1605 			arc_read_region(sc, ARC_REG_IOC_RBUF, rwbuf,
   1606 			    sizeof(rwbuf));
   1607 
   1608 			arc_write(sc, ARC_REG_INB_DOORBELL,
   1609 			    ARC_REG_INB_DOORBELL_READ_OK);
   1610 
   1611 #ifdef ARC_DEBUG
   1612 			printf("%s:  len: %d+%d=%d/%d\n",
   1613 			    device_xname(&sc->sc_dev),
   1614 			    rwlen, rdone, rwlen + rdone, rlen);
   1615 			if (arcdebug & ARC_D_DB) {
   1616 				printf("%s: read:",
   1617 				    device_xname(&sc->sc_dev));
   1618 				for (i = 0; i < rwlen; i++)
   1619 					printf(" 0x%02x", rwbuf[i]);
   1620 				printf("\n");
   1621 			}
   1622 #endif
   1623 
   1624 			if ((rdone + rwlen) > rlen) {
   1625 				DNPRINTF(ARC_D_DB, "%s:  rwbuf too big\n",
   1626 				    device_xname(&sc->sc_dev));
   1627 				error = EIO;
   1628 				goto out;
   1629 			}
   1630 
   1631 			memcpy(&rbuf[rdone], rwbuf, rwlen);
   1632 			rdone += rwlen;
   1633 		}
   1634 	} while (rdone != rlen);
   1635 
   1636 	bufhdr = (struct arc_fw_bufhdr *)rbuf;
   1637 	if (memcmp(&bufhdr->hdr, &arc_fw_hdr, sizeof(bufhdr->hdr)) != 0 ||
   1638 	    bufhdr->len != htole16(rbuflen)) {
   1639 		DNPRINTF(ARC_D_DB, "%s:  rbuf hdr is wrong\n",
   1640 		    device_xname(&sc->sc_dev));
   1641 		error = EIO;
   1642 		goto out;
   1643 	}
   1644 
   1645 	memcpy(rptr, rbuf + sizeof(struct arc_fw_bufhdr), rbuflen);
   1646 
   1647 	if (rbuf[rlen - 1] != arc_msg_cksum(rptr, rbuflen)) {
   1648 		DNPRINTF(ARC_D_DB, "%s:  invalid cksum\n",
   1649 		    device_xname(&sc->sc_dev));
   1650 		error = EIO;
   1651 		goto out;
   1652 	}
   1653 
   1654 out:
   1655 	arc_unlock(sc);
   1656 	kmem_free(wbuf, wlen);
   1657 	kmem_free(rbuf, rlen);
   1658 
   1659 	return error;
   1660 }
   1661 
   1662 void
   1663 arc_lock(struct arc_softc *sc)
   1664 {
   1665 	rw_enter(&sc->sc_rwlock, RW_WRITER);
   1666 	mutex_spin_enter(&sc->sc_mutex);
   1667 	arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE);
   1668 	sc->sc_talking = 1;
   1669 }
   1670 
   1671 void
   1672 arc_unlock(struct arc_softc *sc)
   1673 {
   1674 	KASSERT(mutex_owned(&sc->sc_mutex));
   1675 
   1676 	arc_write(sc, ARC_REG_INTRMASK,
   1677 	    ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL));
   1678 	sc->sc_talking = 0;
   1679 	mutex_spin_exit(&sc->sc_mutex);
   1680 	rw_exit(&sc->sc_rwlock);
   1681 }
   1682 
   1683 void
   1684 arc_wait(struct arc_softc *sc)
   1685 {
   1686 	KASSERT(mutex_owned(&sc->sc_mutex));
   1687 
   1688 	arc_write(sc, ARC_REG_INTRMASK,
   1689 	    ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL));
   1690 	if (cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz) == EWOULDBLOCK)
   1691 		arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE);
   1692 }
   1693 
   1694 #if NBIO > 0
   1695 static void
   1696 arc_create_sensors(void *arg)
   1697 {
   1698 	struct arc_softc	*sc = arg;
   1699 	struct bioc_inq		bi;
   1700 	struct bioc_vol		bv;
   1701 	int			i, j;
   1702 	size_t			slen, count = 0;
   1703 
   1704 	memset(&bi, 0, sizeof(bi));
   1705 	if (arc_bio_inq(sc, &bi) != 0) {
   1706 		aprint_error("%s: unable to query firmware for sensor info\n",
   1707 		    device_xname(&sc->sc_dev));
   1708 		kthread_exit(0);
   1709 	}
   1710 
   1711 	/* There's no point to continue if there are no volumes */
   1712 	if (!bi.bi_novol)
   1713 		kthread_exit(0);
   1714 
   1715 	for (i = 0; i < bi.bi_novol; i++) {
   1716 		memset(&bv, 0, sizeof(bv));
   1717 		bv.bv_volid = i;
   1718 		if (arc_bio_vol(sc, &bv) != 0)
   1719 			kthread_exit(0);
   1720 
   1721 		/* Skip passthrough volumes */
   1722 		if (bv.bv_level == BIOC_SVOL_PASSTHRU)
   1723 			continue;
   1724 
   1725 		/* new volume found */
   1726 		sc->sc_nsensors++;
   1727 		/* new disk in a volume found */
   1728 		sc->sc_nsensors+= bv.bv_nodisk;
   1729 	}
   1730 
   1731 	/* No valid volumes */
   1732 	if (!sc->sc_nsensors)
   1733 		kthread_exit(0);
   1734 
   1735 	sc->sc_sme = sysmon_envsys_create();
   1736 	slen = sizeof(envsys_data_t) * sc->sc_nsensors;
   1737 	sc->sc_sensors = kmem_zalloc(slen, KM_SLEEP);
   1738 
   1739 	/* Attach sensors for volumes and disks */
   1740 	for (i = 0; i < bi.bi_novol; i++) {
   1741 		memset(&bv, 0, sizeof(bv));
   1742 		bv.bv_volid = i;
   1743 		if (arc_bio_vol(sc, &bv) != 0)
   1744 			goto bad;
   1745 
   1746 		sc->sc_sensors[count].units = ENVSYS_DRIVE;
   1747 		sc->sc_sensors[count].monitor = true;
   1748 		sc->sc_sensors[count].flags = ENVSYS_FMONSTCHANGED;
   1749 
   1750 		/* Skip passthrough volumes */
   1751 		if (bv.bv_level == BIOC_SVOL_PASSTHRU)
   1752 			continue;
   1753 
   1754 		snprintf(sc->sc_sensors[count].desc,
   1755 		    sizeof(sc->sc_sensors[count].desc),
   1756 		    "RAID %d volume%d (%s)", bv.bv_level, i, bv.bv_dev);
   1757 		sc->sc_sensors[count].value_max = i;
   1758 
   1759 		if (sysmon_envsys_sensor_attach(sc->sc_sme,
   1760 		    &sc->sc_sensors[count]))
   1761 			goto bad;
   1762 
   1763 		count++;
   1764 
   1765 		/* Attach disk sensors for this volume */
   1766 		for (j = 0; j < bv.bv_nodisk; j++) {
   1767 			sc->sc_sensors[count].units = ENVSYS_DRIVE;
   1768 			sc->sc_sensors[count].monitor = true;
   1769 			sc->sc_sensors[count].flags = ENVSYS_FMONSTCHANGED;
   1770 
   1771 			snprintf(sc->sc_sensors[count].desc,
   1772 			    sizeof(sc->sc_sensors[count].desc),
   1773 			    "disk%d volume%d (%s)", j, i, bv.bv_dev);
   1774 			sc->sc_sensors[count].value_max = i;
   1775 			sc->sc_sensors[count].value_avg = j + 10;
   1776 
   1777 			if (sysmon_envsys_sensor_attach(sc->sc_sme,
   1778 			    &sc->sc_sensors[count]))
   1779 				goto bad;
   1780 
   1781 			count++;
   1782 		}
   1783 	}
   1784 
   1785 	/*
   1786 	 * Register our envsys driver with the framework now that the
   1787 	 * sensors were all attached.
   1788 	 */
   1789 	sc->sc_sme->sme_name = device_xname(&sc->sc_dev);
   1790 	sc->sc_sme->sme_cookie = sc;
   1791 	sc->sc_sme->sme_refresh = arc_refresh_sensors;
   1792 
   1793 	if (sysmon_envsys_register(sc->sc_sme)) {
   1794 		aprint_debug("%s: unable to register with sysmon\n",
   1795 		    device_xname(&sc->sc_dev));
   1796 		goto bad;
   1797 	}
   1798 	kthread_exit(0);
   1799 
   1800 bad:
   1801 	kmem_free(sc->sc_sensors, slen);
   1802 	sysmon_envsys_destroy(sc->sc_sme);
   1803 	kthread_exit(0);
   1804 }
   1805 
   1806 static void
   1807 arc_refresh_sensors(struct sysmon_envsys *sme, envsys_data_t *edata)
   1808 {
   1809 	struct arc_softc	*sc = sme->sme_cookie;
   1810 	struct bioc_vol		bv;
   1811 	struct bioc_disk	bd;
   1812 
   1813 	/* sanity check */
   1814 	if (edata->units != ENVSYS_DRIVE)
   1815 		return;
   1816 
   1817 	memset(&bv, 0, sizeof(bv));
   1818 	bv.bv_volid = edata->value_max;
   1819 
   1820 	if (arc_bio_vol(sc, &bv)) {
   1821 		edata->value_cur = ENVSYS_DRIVE_EMPTY;
   1822 		edata->state = ENVSYS_SINVALID;
   1823 		return;
   1824 	}
   1825 
   1826 	/* Current sensor is handling a disk volume member */
   1827 	if (edata->value_avg) {
   1828 		memset(&bd, 0, sizeof(bd));
   1829 		bd.bd_volid = edata->value_max;
   1830 		bd.bd_diskid = edata->value_avg - 10;
   1831 
   1832 		if (arc_bio_disk_volume(sc, &bd)) {
   1833 			edata->value_cur = ENVSYS_DRIVE_OFFLINE;
   1834 			edata->state = ENVSYS_SCRITICAL;
   1835 			return;
   1836 		}
   1837 
   1838 		switch (bd.bd_status) {
   1839 		case BIOC_SDONLINE:
   1840 			edata->value_cur = ENVSYS_DRIVE_ONLINE;
   1841 			edata->state = ENVSYS_SVALID;
   1842 			break;
   1843 		case BIOC_SDOFFLINE:
   1844 			edata->value_cur = ENVSYS_DRIVE_OFFLINE;
   1845 			edata->state = ENVSYS_SCRITICAL;
   1846 			break;
   1847 		default:
   1848 			edata->value_cur = ENVSYS_DRIVE_FAIL;
   1849 			edata->state = ENVSYS_SCRITICAL;
   1850 			break;
   1851 		}
   1852 
   1853 		return;
   1854 	}
   1855 
   1856 	/* Current sensor is handling a volume */
   1857 	switch (bv.bv_status) {
   1858 	case BIOC_SVOFFLINE:
   1859 		edata->value_cur = ENVSYS_DRIVE_OFFLINE;
   1860 		edata->state = ENVSYS_SCRITICAL;
   1861 		break;
   1862 	case BIOC_SVDEGRADED:
   1863 		edata->value_cur = ENVSYS_DRIVE_PFAIL;
   1864 		edata->state = ENVSYS_SCRITICAL;
   1865 		break;
   1866 	case BIOC_SVBUILDING:
   1867 		edata->value_cur = ENVSYS_DRIVE_BUILD;
   1868 		edata->state = ENVSYS_SVALID;
   1869 		break;
   1870 	case BIOC_SVMIGRATING:
   1871 		edata->value_cur = ENVSYS_DRIVE_MIGRATING;
   1872 		edata->state = ENVSYS_SVALID;
   1873 		break;
   1874 	case BIOC_SVCHECKING:
   1875 		edata->value_cur = ENVSYS_DRIVE_CHECK;
   1876 		edata->state = ENVSYS_SVALID;
   1877 		break;
   1878 	case BIOC_SVREBUILD:
   1879 		edata->value_cur = ENVSYS_DRIVE_REBUILD;
   1880 		edata->state = ENVSYS_SCRITICAL;
   1881 		break;
   1882 	case BIOC_SVSCRUB:
   1883 	case BIOC_SVONLINE:
   1884 		edata->value_cur = ENVSYS_DRIVE_ONLINE;
   1885 		edata->state = ENVSYS_SVALID;
   1886 		break;
   1887 	case BIOC_SVINVALID:
   1888 		/* FALLTHROUGH */
   1889 	default:
   1890 		edata->value_cur = ENVSYS_DRIVE_EMPTY; /* unknown state */
   1891 		edata->state = ENVSYS_SINVALID;
   1892 		break;
   1893 	}
   1894 }
   1895 #endif /* NBIO > 0 */
   1896 
   1897 uint32_t
   1898 arc_read(struct arc_softc *sc, bus_size_t r)
   1899 {
   1900 	uint32_t			v;
   1901 
   1902 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
   1903 	    BUS_SPACE_BARRIER_READ);
   1904 	v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
   1905 
   1906 	DNPRINTF(ARC_D_RW, "%s: arc_read 0x%lx 0x%08x\n",
   1907 	    device_xname(&sc->sc_dev), r, v);
   1908 
   1909 	return v;
   1910 }
   1911 
   1912 void
   1913 arc_read_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
   1914 {
   1915 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
   1916 	    BUS_SPACE_BARRIER_READ);
   1917 	bus_space_read_region_4(sc->sc_iot, sc->sc_ioh, r,
   1918 	    (uint32_t *)buf, len >> 2);
   1919 }
   1920 
   1921 void
   1922 arc_write(struct arc_softc *sc, bus_size_t r, uint32_t v)
   1923 {
   1924 	DNPRINTF(ARC_D_RW, "%s: arc_write 0x%lx 0x%08x\n",
   1925 	    device_xname(&sc->sc_dev), r, v);
   1926 
   1927 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
   1928 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
   1929 	    BUS_SPACE_BARRIER_WRITE);
   1930 }
   1931 
   1932 void
   1933 arc_write_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
   1934 {
   1935 	bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, r,
   1936 	    (const uint32_t *)buf, len >> 2);
   1937 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
   1938 	    BUS_SPACE_BARRIER_WRITE);
   1939 }
   1940 
   1941 int
   1942 arc_wait_eq(struct arc_softc *sc, bus_size_t r, uint32_t mask,
   1943 	    uint32_t target)
   1944 {
   1945 	int i;
   1946 
   1947 	DNPRINTF(ARC_D_RW, "%s: arc_wait_eq 0x%lx 0x%08x 0x%08x\n",
   1948 	    device_xname(&sc->sc_dev), r, mask, target);
   1949 
   1950 	for (i = 0; i < 10000; i++) {
   1951 		if ((arc_read(sc, r) & mask) == target)
   1952 			return 0;
   1953 		delay(1000);
   1954 	}
   1955 
   1956 	return 1;
   1957 }
   1958 
   1959 int
   1960 arc_wait_ne(struct arc_softc *sc, bus_size_t r, uint32_t mask,
   1961 	    uint32_t target)
   1962 {
   1963 	int i;
   1964 
   1965 	DNPRINTF(ARC_D_RW, "%s: arc_wait_ne 0x%lx 0x%08x 0x%08x\n",
   1966 	    device_xname(&sc->sc_dev), r, mask, target);
   1967 
   1968 	for (i = 0; i < 10000; i++) {
   1969 		if ((arc_read(sc, r) & mask) != target)
   1970 			return 0;
   1971 		delay(1000);
   1972 	}
   1973 
   1974 	return 1;
   1975 }
   1976 
   1977 int
   1978 arc_msg0(struct arc_softc *sc, uint32_t m)
   1979 {
   1980 	/* post message */
   1981 	arc_write(sc, ARC_REG_INB_MSG0, m);
   1982 	/* wait for the fw to do it */
   1983 	if (arc_wait_eq(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0,
   1984 	    ARC_REG_INTRSTAT_MSG0) != 0)
   1985 		return 1;
   1986 
   1987 	/* ack it */
   1988 	arc_write(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0);
   1989 
   1990 	return 0;
   1991 }
   1992 
   1993 struct arc_dmamem *
   1994 arc_dmamem_alloc(struct arc_softc *sc, size_t size)
   1995 {
   1996 	struct arc_dmamem		*adm;
   1997 	int				nsegs;
   1998 
   1999 	adm = kmem_zalloc(sizeof(*adm), KM_NOSLEEP);
   2000 	if (adm == NULL)
   2001 		return NULL;
   2002 
   2003 	adm->adm_size = size;
   2004 
   2005 	if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
   2006 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &adm->adm_map) != 0)
   2007 		goto admfree;
   2008 
   2009 	if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &adm->adm_seg,
   2010 	    1, &nsegs, BUS_DMA_NOWAIT) != 0)
   2011 		goto destroy;
   2012 
   2013 	if (bus_dmamem_map(sc->sc_dmat, &adm->adm_seg, nsegs, size,
   2014 	    &adm->adm_kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0)
   2015 		goto free;
   2016 
   2017 	if (bus_dmamap_load(sc->sc_dmat, adm->adm_map, adm->adm_kva, size,
   2018 	    NULL, BUS_DMA_NOWAIT) != 0)
   2019 		goto unmap;
   2020 
   2021 	memset(adm->adm_kva, 0, size);
   2022 
   2023 	return adm;
   2024 
   2025 unmap:
   2026 	bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, size);
   2027 free:
   2028 	bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
   2029 destroy:
   2030 	bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
   2031 admfree:
   2032 	kmem_free(adm, sizeof(*adm));
   2033 
   2034 	return NULL;
   2035 }
   2036 
   2037 void
   2038 arc_dmamem_free(struct arc_softc *sc, struct arc_dmamem *adm)
   2039 {
   2040 	bus_dmamap_unload(sc->sc_dmat, adm->adm_map);
   2041 	bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, adm->adm_size);
   2042 	bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
   2043 	bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
   2044 	kmem_free(adm, sizeof(*adm));
   2045 }
   2046 
   2047 int
   2048 arc_alloc_ccbs(struct arc_softc *sc)
   2049 {
   2050 	struct arc_ccb		*ccb;
   2051 	uint8_t			*cmd;
   2052 	int			i;
   2053 	size_t			ccbslen;
   2054 
   2055 	TAILQ_INIT(&sc->sc_ccb_free);
   2056 
   2057 	ccbslen = sizeof(struct arc_ccb) * sc->sc_req_count;
   2058 	sc->sc_ccbs = kmem_zalloc(ccbslen, KM_SLEEP);
   2059 
   2060 	sc->sc_requests = arc_dmamem_alloc(sc,
   2061 	    ARC_MAX_IOCMDLEN * sc->sc_req_count);
   2062 	if (sc->sc_requests == NULL) {
   2063 		aprint_error("%s: unable to allocate ccb dmamem\n",
   2064 		    device_xname(&sc->sc_dev));
   2065 		goto free_ccbs;
   2066 	}
   2067 	cmd = ARC_DMA_KVA(sc->sc_requests);
   2068 
   2069 	for (i = 0; i < sc->sc_req_count; i++) {
   2070 		ccb = &sc->sc_ccbs[i];
   2071 
   2072 		if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, ARC_SGL_MAXLEN,
   2073 		    MAXPHYS, 0, 0, &ccb->ccb_dmamap) != 0) {
   2074 			aprint_error("%s: unable to create dmamap for ccb %d\n",
   2075 			    device_xname(&sc->sc_dev), i);
   2076 			goto free_maps;
   2077 		}
   2078 
   2079 		ccb->ccb_sc = sc;
   2080 		ccb->ccb_id = i;
   2081 		ccb->ccb_offset = ARC_MAX_IOCMDLEN * i;
   2082 
   2083 		ccb->ccb_cmd = (struct arc_io_cmd *)&cmd[ccb->ccb_offset];
   2084 		ccb->ccb_cmd_post = (ARC_DMA_DVA(sc->sc_requests) +
   2085 		    ccb->ccb_offset) >> ARC_REG_POST_QUEUE_ADDR_SHIFT;
   2086 
   2087 		arc_put_ccb(sc, ccb);
   2088 	}
   2089 
   2090 	return 0;
   2091 
   2092 free_maps:
   2093 	while ((ccb = arc_get_ccb(sc)) != NULL)
   2094 	    bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
   2095 	arc_dmamem_free(sc, sc->sc_requests);
   2096 
   2097 free_ccbs:
   2098 	kmem_free(sc->sc_ccbs, ccbslen);
   2099 
   2100 	return 1;
   2101 }
   2102 
   2103 struct arc_ccb *
   2104 arc_get_ccb(struct arc_softc *sc)
   2105 {
   2106 	struct arc_ccb			*ccb;
   2107 
   2108 	ccb = TAILQ_FIRST(&sc->sc_ccb_free);
   2109 	if (ccb != NULL)
   2110 		TAILQ_REMOVE(&sc->sc_ccb_free, ccb, ccb_link);
   2111 
   2112 	return ccb;
   2113 }
   2114 
   2115 void
   2116 arc_put_ccb(struct arc_softc *sc, struct arc_ccb *ccb)
   2117 {
   2118 	ccb->ccb_xs = NULL;
   2119 	memset(ccb->ccb_cmd, 0, ARC_MAX_IOCMDLEN);
   2120 	TAILQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_link);
   2121 }
   2122