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