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