Home | History | Annotate | Line # | Download | only in vme
xd.c revision 1.92
      1 /*	$NetBSD: xd.c,v 1.92 2014/07/25 08:02:20 dholland Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Charles D. Cranor
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  *
     30  * x d . c   x y l o g i c s   7 5 3 / 7 0 5 3   v m e / s m d   d r i v e r
     31  *
     32  * author: Chuck Cranor <chuck@netbsd>
     33  * started: 27-Feb-95
     34  * references: [1] Xylogics Model 753 User's Manual
     35  *                 part number: 166-753-001, Revision B, May 21, 1988.
     36  *                 "Your Partner For Performance"
     37  *             [2] other NetBSD disk device drivers
     38  *
     39  * Special thanks go to Scott E. Campbell of Xylogics, Inc. for taking
     40  * the time to answer some of my questions about the 753/7053.
     41  *
     42  * note: the 753 and the 7053 are programmed the same way, but are
     43  * different sizes.   the 753 is a 6U VME card, while the 7053 is a 9U
     44  * VME card (found in many VME based suns).
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: xd.c,v 1.92 2014/07/25 08:02:20 dholland Exp $");
     49 
     50 #undef XDC_DEBUG		/* full debug */
     51 #define XDC_DIAG		/* extra sanity checks */
     52 #if defined(DIAGNOSTIC) && !defined(XDC_DIAG)
     53 #define XDC_DIAG		/* link in with master DIAG option */
     54 #endif
     55 
     56 #include <sys/param.h>
     57 #include <sys/proc.h>
     58 #include <sys/systm.h>
     59 #include <sys/kernel.h>
     60 #include <sys/file.h>
     61 #include <sys/stat.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/buf.h>
     64 #include <sys/bufq.h>
     65 #include <sys/uio.h>
     66 #include <sys/malloc.h>
     67 #include <sys/device.h>
     68 #include <sys/disklabel.h>
     69 #include <sys/disk.h>
     70 #include <sys/syslog.h>
     71 #include <sys/dkbad.h>
     72 #include <sys/conf.h>
     73 #include <sys/kauth.h>
     74 
     75 #include <sys/bus.h>
     76 #include <sys/intr.h>
     77 
     78 #if defined(__sparc__) || defined(sun3)
     79 #include <dev/sun/disklabel.h>
     80 #endif
     81 
     82 #include <dev/vme/vmereg.h>
     83 #include <dev/vme/vmevar.h>
     84 
     85 #include <dev/vme/xdreg.h>
     86 #include <dev/vme/xdvar.h>
     87 #include <dev/vme/xio.h>
     88 
     89 #include "locators.h"
     90 
     91 /*
     92  * macros
     93  */
     94 
     95 /*
     96  * XDC_TWAIT: add iorq "N" to tail of SC's wait queue
     97  */
     98 #define XDC_TWAIT(SC, N) { \
     99 	(SC)->waitq[(SC)->waitend] = (N); \
    100 	(SC)->waitend = ((SC)->waitend + 1) % XDC_MAXIOPB; \
    101 	(SC)->nwait++; \
    102 }
    103 
    104 /*
    105  * XDC_HWAIT: add iorq "N" to head of SC's wait queue
    106  */
    107 #define XDC_HWAIT(SC, N) { \
    108 	(SC)->waithead = ((SC)->waithead == 0) ? \
    109 		(XDC_MAXIOPB - 1) : ((SC)->waithead - 1); \
    110 	(SC)->waitq[(SC)->waithead] = (N); \
    111 	(SC)->nwait++; \
    112 }
    113 
    114 /*
    115  * XDC_GET_WAITER: gets the first request waiting on the waitq
    116  * and removes it (so it can be submitted)
    117  */
    118 #define XDC_GET_WAITER(XDCSC, RQ) { \
    119 	(RQ) = (XDCSC)->waitq[(XDCSC)->waithead]; \
    120 	(XDCSC)->waithead = ((XDCSC)->waithead + 1) % XDC_MAXIOPB; \
    121 	xdcsc->nwait--; \
    122 }
    123 
    124 /*
    125  * XDC_FREE: add iorq "N" to SC's free list
    126  */
    127 #define XDC_FREE(SC, N) { \
    128 	(SC)->freereq[(SC)->nfree++] = (N); \
    129 	(SC)->reqs[N].mode = 0; \
    130 	if ((SC)->nfree == 1) wakeup(&(SC)->nfree); \
    131 }
    132 
    133 
    134 /*
    135  * XDC_RQALLOC: allocate an iorq off the free list (assume nfree > 0).
    136  */
    137 #define XDC_RQALLOC(XDCSC) (XDCSC)->freereq[--((XDCSC)->nfree)]
    138 
    139 /*
    140  * XDC_GO: start iopb ADDR (DVMA addr in a u_long) on XDC
    141  */
    142 #define XDC_GO(XDC, ADDR) { \
    143 	(XDC)->xdc_iopbaddr0 = ((ADDR) & 0xff); \
    144 	(ADDR) = ((ADDR) >> 8); \
    145 	(XDC)->xdc_iopbaddr1 = ((ADDR) & 0xff); \
    146 	(ADDR) = ((ADDR) >> 8); \
    147 	(XDC)->xdc_iopbaddr2 = ((ADDR) & 0xff); \
    148 	(ADDR) = ((ADDR) >> 8); \
    149 	(XDC)->xdc_iopbaddr3 = (ADDR); \
    150 	(XDC)->xdc_iopbamod = XDC_ADDRMOD; \
    151 	(XDC)->xdc_csr = XDC_ADDIOPB; /* go! */ \
    152 }
    153 
    154 /*
    155  * XDC_WAIT: wait for XDC's csr "BITS" to come on in "TIME".
    156  *   LCV is a counter.  If it goes to zero then we timed out.
    157  */
    158 #define XDC_WAIT(XDC, LCV, TIME, BITS) { \
    159 	(LCV) = (TIME); \
    160 	while ((LCV) > 0) { \
    161 		if ((XDC)->xdc_csr & (BITS)) break; \
    162 		(LCV) = (LCV) - 1; \
    163 		DELAY(1); \
    164 	} \
    165 }
    166 
    167 /*
    168  * XDC_DONE: don't need IORQ, get error code and free (done after xdc_cmd)
    169  */
    170 #define XDC_DONE(SC,RQ,ER) { \
    171 	if ((RQ) == XD_ERR_FAIL) { \
    172 		(ER) = (RQ); \
    173 	} else { \
    174 		if ((SC)->ndone-- == XDC_SUBWAITLIM) \
    175 		wakeup(&(SC)->ndone); \
    176 		(ER) = (SC)->reqs[RQ].errnum; \
    177 		XDC_FREE((SC), (RQ)); \
    178 	} \
    179 }
    180 
    181 /*
    182  * XDC_ADVANCE: advance iorq's pointers by a number of sectors
    183  */
    184 #define XDC_ADVANCE(IORQ, N) { \
    185 	if (N) { \
    186 		(IORQ)->sectcnt -= (N); \
    187 		(IORQ)->blockno += (N); \
    188 		(IORQ)->dbuf += ((N)*XDFM_BPS); \
    189 	} \
    190 }
    191 
    192 /*
    193  * note - addresses you can sleep on:
    194  *   [1] & of xd_softc's "state" (waiting for a chance to attach a drive)
    195  *   [2] & of xdc_softc's "nfree" (waiting for a free iorq/iopb)
    196  *   [3] & of xdc_softc's "ndone" (waiting for number of done iorq/iopb's
    197  *                                 to drop below XDC_SUBWAITLIM)
    198  *   [4] & an iorq (waiting for an XD_SUB_WAIT iorq to finish)
    199  */
    200 
    201 
    202 /*
    203  * function prototypes
    204  * "xdc_*" functions are internal, all others are external interfaces
    205  */
    206 
    207 extern int pil_to_vme[];	/* from obio.c */
    208 
    209 /* internals */
    210 int	xdc_cmd(struct xdc_softc *, int, int, int, int, int, char *, int);
    211 const char *xdc_e2str(int);
    212 int	xdc_error(struct xdc_softc *, struct xd_iorq *,
    213 		   struct xd_iopb *, int, int);
    214 int	xdc_ioctlcmd(struct xd_softc *, dev_t dev, struct xd_iocmd *);
    215 void	xdc_perror(struct xd_iorq *, struct xd_iopb *, int);
    216 int	xdc_piodriver(struct xdc_softc *, int, int);
    217 int	xdc_remove_iorq(struct xdc_softc *);
    218 int	xdc_reset(struct xdc_softc *, int, int, int, struct xd_softc *);
    219 inline void xdc_rqinit(struct xd_iorq *, struct xdc_softc *,
    220 			struct xd_softc *, int, u_long, int,
    221 			void *, struct buf *);
    222 void	xdc_rqtopb(struct xd_iorq *, struct xd_iopb *, int, int);
    223 void	xdc_start(struct xdc_softc *, int);
    224 int	xdc_startbuf(struct xdc_softc *, struct xd_softc *, struct buf *);
    225 int	xdc_submit_iorq(struct xdc_softc *, int, int);
    226 void	xdc_tick(void *);
    227 void	xdc_xdreset(struct xdc_softc *, struct xd_softc *);
    228 int	xd_dmamem_alloc(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
    229 			int *, bus_size_t, void **, bus_addr_t *);
    230 void	xd_dmamem_free(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
    231 			int, bus_size_t, void *);
    232 
    233 
    234 /* machine interrupt hook */
    235 int	xdcintr(void *);
    236 
    237 /* autoconf */
    238 int	xdcmatch(device_t, cfdata_t, void *);
    239 void	xdcattach(device_t, device_t, void *);
    240 int	xdmatch(device_t, cfdata_t, void *);
    241 void	xdattach(device_t, device_t, void *);
    242 static	int xdc_probe(void *, bus_space_tag_t, bus_space_handle_t);
    243 
    244 static	void xddummystrat(struct buf *);
    245 int	xdgetdisklabel(struct xd_softc *, void *);
    246 
    247 /* XXX - think about this more.. xd_machdep? */
    248 void xdc_md_setup(void);
    249 int	XDC_DELAY;
    250 
    251 #if defined(__sparc__)
    252 #include <sparc/sparc/vaddrs.h>
    253 #include <sparc/sparc/cpuvar.h>
    254 void xdc_md_setup(void)
    255 {
    256 	if (CPU_ISSUN4 && cpuinfo.cpu_type == CPUTYP_4_300)
    257 		XDC_DELAY = XDC_DELAY_4_300;
    258 	else
    259 		XDC_DELAY = XDC_DELAY_SPARC;
    260 }
    261 #elif defined(sun3)
    262 void xdc_md_setup(void)
    263 {
    264 	XDC_DELAY = XDC_DELAY_SUN3;
    265 }
    266 #else
    267 void xdc_md_setup(void)
    268 {
    269 	XDC_DELAY = 0;
    270 }
    271 #endif
    272 
    273 /*
    274  * cfattach's: device driver interface to autoconfig
    275  */
    276 
    277 CFATTACH_DECL_NEW(xdc, sizeof(struct xdc_softc),
    278     xdcmatch, xdcattach, NULL, NULL);
    279 
    280 CFATTACH_DECL_NEW(xd, sizeof(struct xd_softc),
    281     xdmatch, xdattach, NULL, NULL);
    282 
    283 extern struct cfdriver xd_cd;
    284 
    285 dev_type_open(xdopen);
    286 dev_type_close(xdclose);
    287 dev_type_read(xdread);
    288 dev_type_write(xdwrite);
    289 dev_type_ioctl(xdioctl);
    290 dev_type_strategy(xdstrategy);
    291 dev_type_dump(xddump);
    292 dev_type_size(xdsize);
    293 
    294 const struct bdevsw xd_bdevsw = {
    295 	.d_open = xdopen,
    296 	.d_close = xdclose,
    297 	.d_strategy = xdstrategy,
    298 	.d_ioctl = xdioctl,
    299 	.d_dump = xddump,
    300 	.d_psize = xdsize,
    301 	.d_discard = nodiscard,
    302 	.d_flag = D_DISK
    303 };
    304 
    305 const struct cdevsw xd_cdevsw = {
    306 	.d_open = xdopen,
    307 	.d_close = xdclose,
    308 	.d_read = xdread,
    309 	.d_write = xdwrite,
    310 	.d_ioctl = xdioctl,
    311 	.d_stop = nostop,
    312 	.d_tty = notty,
    313 	.d_poll = nopoll,
    314 	.d_mmap = nommap,
    315 	.d_kqfilter = nokqfilter,
    316 	.d_flag = D_DISK
    317 };
    318 
    319 struct xdc_attach_args {	/* this is the "aux" args to xdattach */
    320 	int	driveno;	/* unit number */
    321 	int	fullmode;	/* submit mode */
    322 	int	booting;	/* are we booting or not? */
    323 };
    324 
    325 /*
    326  * dkdriver
    327  */
    328 
    329 struct dkdriver xddkdriver = {xdstrategy};
    330 
    331 /*
    332  * start: disk label fix code (XXX)
    333  */
    334 
    335 static void *xd_labeldata;
    336 
    337 static void
    338 xddummystrat(struct buf *bp)
    339 {
    340 	if (bp->b_bcount != XDFM_BPS)
    341 		panic("xddummystrat");
    342 	memcpy(bp->b_data, xd_labeldata, XDFM_BPS);
    343 	bp->b_oflags |= BO_DONE;
    344 	bp->b_cflags &= ~BC_BUSY;
    345 }
    346 
    347 int
    348 xdgetdisklabel(struct xd_softc *xd, void *b)
    349 {
    350 	const char *err;
    351 #if defined(__sparc__) || defined(sun3)
    352 	struct sun_disklabel *sdl;
    353 #endif
    354 
    355 	/* We already have the label data in `b'; setup for dummy strategy */
    356 	xd_labeldata = b;
    357 
    358 	/* Required parameter for readdisklabel() */
    359 	xd->sc_dk.dk_label->d_secsize = XDFM_BPS;
    360 
    361 	err = readdisklabel(MAKEDISKDEV(0, device_unit(xd->sc_dev), RAW_PART),
    362 			    xddummystrat,
    363 			    xd->sc_dk.dk_label, xd->sc_dk.dk_cpulabel);
    364 	if (err) {
    365 		aprint_error_dev(xd->sc_dev, "%s\n", err);
    366 		return(XD_ERR_FAIL);
    367 	}
    368 
    369 #if defined(__sparc__) || defined(sun3)
    370 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
    371 	sdl = (struct sun_disklabel *)xd->sc_dk.dk_cpulabel->cd_block;
    372 	if (sdl->sl_magic == SUN_DKMAGIC) {
    373 		xd->pcyl = sdl->sl_pcylinders;
    374 	} else
    375 #endif
    376 	{
    377 		printf("%s: WARNING: no `pcyl' in disk label.\n",
    378 			device_xname(xd->sc_dev));
    379 		xd->pcyl = xd->sc_dk.dk_label->d_ncylinders +
    380 			xd->sc_dk.dk_label->d_acylinders;
    381 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
    382 			device_xname(xd->sc_dev), xd->pcyl);
    383 	}
    384 
    385 	xd->ncyl = xd->sc_dk.dk_label->d_ncylinders;
    386 	xd->acyl = xd->sc_dk.dk_label->d_acylinders;
    387 	xd->nhead = xd->sc_dk.dk_label->d_ntracks;
    388 	xd->nsect = xd->sc_dk.dk_label->d_nsectors;
    389 	xd->sectpercyl = xd->nhead * xd->nsect;
    390 	xd->sc_dk.dk_label->d_secsize = XDFM_BPS; /* not handled by
    391 						  * sun->bsd */
    392 	return(XD_ERR_AOK);
    393 }
    394 
    395 /*
    396  * end: disk label fix code (XXX)
    397  */
    398 
    399 /*
    400  * Shorthand for allocating, mapping and loading a DMA buffer
    401  */
    402 int
    403 xd_dmamem_alloc(bus_dma_tag_t tag, bus_dmamap_t map, bus_dma_segment_t *seg, int *nsegp, bus_size_t len, void * *kvap, bus_addr_t *dmap)
    404 {
    405 	int nseg;
    406 	int error;
    407 
    408 	if ((error = bus_dmamem_alloc(tag, len, 0, 0,
    409 				      seg, 1, &nseg, BUS_DMA_NOWAIT)) != 0) {
    410 		return (error);
    411 	}
    412 
    413 	if ((error = bus_dmamem_map(tag, seg, nseg,
    414 				    len, kvap,
    415 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    416 		bus_dmamem_free(tag, seg, nseg);
    417 		return (error);
    418 	}
    419 
    420 	if ((error = bus_dmamap_load(tag, map,
    421 				     *kvap, len, NULL,
    422 				     BUS_DMA_NOWAIT)) != 0) {
    423 		bus_dmamem_unmap(tag, *kvap, len);
    424 		bus_dmamem_free(tag, seg, nseg);
    425 		return (error);
    426 	}
    427 
    428 	*dmap = map->dm_segs[0].ds_addr;
    429 	*nsegp = nseg;
    430 	return (0);
    431 }
    432 
    433 void
    434 xd_dmamem_free(bus_dma_tag_t tag, bus_dmamap_t map, bus_dma_segment_t *seg, int nseg, bus_size_t len, void * kva)
    435 {
    436 
    437 	bus_dmamap_unload(tag, map);
    438 	bus_dmamem_unmap(tag, kva, len);
    439 	bus_dmamem_free(tag, seg, nseg);
    440 }
    441 
    442 
    443 /*
    444  * a u t o c o n f i g   f u n c t i o n s
    445  */
    446 
    447 /*
    448  * xdcmatch: determine if xdc is present or not.   we do a
    449  * soft reset to detect the xdc.
    450  */
    451 
    452 int
    453 xdc_probe(void *arg, bus_space_tag_t tag, bus_space_handle_t handle)
    454 {
    455 	struct xdc *xdc = (void *)handle; /* XXX */
    456 	int del = 0;
    457 
    458 	xdc->xdc_csr = XDC_RESET;
    459 	XDC_WAIT(xdc, del, XDC_RESETUSEC, XDC_RESET);
    460 	return (del > 0 ? 0 : EIO);
    461 }
    462 
    463 int
    464 xdcmatch(device_t parent, cfdata_t cf, void *aux)
    465 {
    466 	struct vme_attach_args	*va = aux;
    467 	vme_chipset_tag_t	ct = va->va_vct;
    468 	vme_am_t		mod;
    469 	int error;
    470 
    471 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
    472 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xdc), mod))
    473 		return (0);
    474 
    475 	error = vme_probe(ct, va->r[0].offset, sizeof(struct xdc),
    476 			  mod, VME_D32, xdc_probe, 0);
    477 	vme_space_free(va->va_vct, va->r[0].offset, sizeof(struct xdc), mod);
    478 
    479 	return (error == 0);
    480 }
    481 
    482 /*
    483  * xdcattach: attach controller
    484  */
    485 void
    486 xdcattach(device_t parent, device_t self, void *aux)
    487 {
    488 	struct vme_attach_args	*va = aux;
    489 	vme_chipset_tag_t	ct = va->va_vct;
    490 	bus_space_tag_t		bt;
    491 	bus_space_handle_t	bh;
    492 	vme_intr_handle_t	ih;
    493 	vme_am_t		mod;
    494 	struct xdc_softc	*xdc = device_private(self);
    495 	struct xdc_attach_args	xa;
    496 	int			lcv, rqno, error;
    497 	struct xd_iopb_ctrl	*ctl;
    498 	bus_dma_segment_t	seg;
    499 	int			rseg;
    500 	vme_mapresc_t resc;
    501 	bus_addr_t		busaddr;
    502 
    503 	xdc->sc_dev = self;
    504 	xdc_md_setup();
    505 
    506 	/* get addressing and intr level stuff from autoconfig and load it
    507 	 * into our xdc_softc. */
    508 
    509 	xdc->dmatag = va->va_bdt;
    510 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
    511 
    512 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xdc), mod))
    513 		panic("xdc: vme alloc");
    514 
    515 	if (vme_space_map(ct, va->r[0].offset, sizeof(struct xdc),
    516 			  mod, VME_D32, 0, &bt, &bh, &resc) != 0)
    517 		panic("xdc: vme_map");
    518 
    519 	xdc->xdc = (struct xdc *) bh; /* XXX */
    520 	xdc->ipl = va->ilevel;
    521 	xdc->vector = va->ivector;
    522 
    523 	for (lcv = 0; lcv < XDC_MAXDEV; lcv++)
    524 		xdc->sc_drives[lcv] = NULL;
    525 
    526 	/*
    527 	 * allocate and zero buffers
    528 	 *
    529 	 * note: we simplify the code by allocating the max number of iopbs and
    530 	 * iorq's up front.   thus, we avoid linked lists and the costs
    531 	 * associated with them in exchange for wasting a little memory.
    532 	 */
    533 
    534 	/* Get DMA handle for misc. transfers */
    535 	if ((error = vme_dmamap_create(
    536 				ct,		/* VME chip tag */
    537 				MAXPHYS,	/* size */
    538 				VME_AM_A24,	/* address modifier */
    539 				VME_D32,	/* data size */
    540 				0,		/* swap */
    541 				1,		/* nsegments */
    542 				MAXPHYS,	/* maxsegsz */
    543 				0,		/* boundary */
    544 				BUS_DMA_NOWAIT,
    545 				&xdc->auxmap)) != 0) {
    546 
    547 		aprint_error_dev(xdc->sc_dev, "DMA buffer map create error %d\n",
    548 			error);
    549 		return;
    550 	}
    551 
    552 
    553 	/* Get DMA handle for mapping iorq descriptors */
    554 	if ((error = vme_dmamap_create(
    555 				ct,		/* VME chip tag */
    556 				XDC_MAXIOPB * sizeof(struct xd_iopb),
    557 				VME_AM_A24,	/* address modifier */
    558 				VME_D32,	/* data size */
    559 				0,		/* swap */
    560 				1,		/* nsegments */
    561 				XDC_MAXIOPB * sizeof(struct xd_iopb),
    562 				0,		/* boundary */
    563 				BUS_DMA_NOWAIT,
    564 				&xdc->iopmap)) != 0) {
    565 
    566 		aprint_error_dev(xdc->sc_dev, "DMA buffer map create error %d\n",
    567 			error);
    568 		return;
    569 	}
    570 
    571 	/* Get DMA buffer for iorq descriptors */
    572 	if ((error = xd_dmamem_alloc(xdc->dmatag, xdc->iopmap, &seg, &rseg,
    573 				     XDC_MAXIOPB * sizeof(struct xd_iopb),
    574 				     (void **)&xdc->iopbase,
    575 				     &busaddr)) != 0) {
    576 		aprint_error_dev(xdc->sc_dev, "DMA buffer alloc error %d\n",
    577 			error);
    578 		return;
    579 	}
    580 	xdc->dvmaiopb = (struct xd_iopb *)(u_long)BUS_ADDR_PADDR(busaddr);
    581 
    582 	memset(xdc->iopbase, 0, XDC_MAXIOPB * sizeof(struct xd_iopb));
    583 
    584 	xdc->reqs = (struct xd_iorq *)
    585 	    malloc(XDC_MAXIOPB * sizeof(struct xd_iorq),
    586 	    M_DEVBUF, M_NOWAIT|M_ZERO);
    587 	if (xdc->reqs == NULL)
    588 		panic("xdc malloc");
    589 
    590 	/* init free list, iorq to iopb pointers, and non-zero fields in the
    591 	 * iopb which never change. */
    592 
    593 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
    594 		xdc->reqs[lcv].iopb = &xdc->iopbase[lcv];
    595 		xdc->reqs[lcv].dmaiopb = &xdc->dvmaiopb[lcv];
    596 		xdc->freereq[lcv] = lcv;
    597 		xdc->iopbase[lcv].fixd = 1;	/* always the same */
    598 		xdc->iopbase[lcv].naddrmod = XDC_ADDRMOD; /* always the same */
    599 		xdc->iopbase[lcv].intr_vec = xdc->vector; /* always the same */
    600 
    601 		if ((error = vme_dmamap_create(
    602 				ct,		/* VME chip tag */
    603 				MAXPHYS,	/* size */
    604 				VME_AM_A24,	/* address modifier */
    605 				VME_D32,	/* data size */
    606 				0,		/* swap */
    607 				1,		/* nsegments */
    608 				MAXPHYS,	/* maxsegsz */
    609 				0,		/* boundary */
    610 				BUS_DMA_NOWAIT,
    611 				&xdc->reqs[lcv].dmamap)) != 0) {
    612 
    613 			aprint_error_dev(xdc->sc_dev, "DMA buffer map create error %d\n",
    614 				error);
    615 			return;
    616 		}
    617 	}
    618 	xdc->nfree = XDC_MAXIOPB;
    619 	xdc->nrun = 0;
    620 	xdc->waithead = xdc->waitend = xdc->nwait = 0;
    621 	xdc->ndone = 0;
    622 
    623 	/* init queue of waiting bufs */
    624 
    625 	bufq_alloc(&xdc->sc_wq, "fcfs", 0);
    626 	callout_init(&xdc->sc_tick_ch, 0);
    627 
    628 	/*
    629 	 * section 7 of the manual tells us how to init the controller:
    630 	 * - read controller parameters (6/0)
    631 	 * - write controller parameters (5/0)
    632 	 */
    633 
    634 	/* read controller parameters and insure we have a 753/7053 */
    635 
    636 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
    637 	if (rqno == XD_ERR_FAIL) {
    638 		printf(": couldn't read controller params\n");
    639 		return;		/* shouldn't ever happen */
    640 	}
    641 	ctl = (struct xd_iopb_ctrl *) &xdc->iopbase[rqno];
    642 	if (ctl->ctype != XDCT_753) {
    643 		if (xdc->reqs[rqno].errnum)
    644 			printf(": %s: ", xdc_e2str(xdc->reqs[rqno].errnum));
    645 		printf(": doesn't identify as a 753/7053\n");
    646 		XDC_DONE(xdc, rqno, error);
    647 		return;
    648 	}
    649 	printf(": Xylogics 753/7053, PROM=0x%x.%02x.%02x\n",
    650 	    ctl->eprom_partno, ctl->eprom_lvl, ctl->eprom_rev);
    651 	XDC_DONE(xdc, rqno, error);
    652 
    653 	/* now write controller parameters (xdc_cmd sets all params for us) */
    654 
    655 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
    656 	XDC_DONE(xdc, rqno, error);
    657 	if (error) {
    658 		aprint_error_dev(xdc->sc_dev, "controller config error: %s\n",
    659 			xdc_e2str(error));
    660 		return;
    661 	}
    662 
    663 	/* link in interrupt with higher level software */
    664 	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
    665 	vme_intr_establish(ct, ih, IPL_BIO, xdcintr, xdc);
    666 	evcnt_attach_dynamic(&xdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    667 	    device_xname(xdc->sc_dev), "intr");
    668 
    669 
    670 	/* now we must look for disks using autoconfig */
    671 	xa.fullmode = XD_SUB_POLL;
    672 	xa.booting = 1;
    673 
    674 	for (xa.driveno = 0; xa.driveno < XDC_MAXDEV; xa.driveno++)
    675 		(void) config_found(self, (void *) &xa, NULL);
    676 
    677 	/* start the watchdog clock */
    678 	callout_reset(&xdc->sc_tick_ch, XDC_TICKCNT, xdc_tick, xdc);
    679 
    680 }
    681 
    682 /*
    683  * xdmatch: probe for disk.
    684  *
    685  * note: we almost always say disk is present.   this allows us to
    686  * spin up and configure a disk after the system is booted (we can
    687  * call xdattach!).
    688  */
    689 int
    690 xdmatch(device_t parent, cfdata_t cf, void *aux)
    691 {
    692 	struct xdc_attach_args *xa = aux;
    693 
    694 	/* looking for autoconf wildcard or exact match */
    695 
    696 	if (cf->cf_loc[XDCCF_DRIVE] != XDCCF_DRIVE_DEFAULT &&
    697 	    cf->cf_loc[XDCCF_DRIVE] != xa->driveno)
    698 		return 0;
    699 
    700 	return 1;
    701 
    702 }
    703 
    704 /*
    705  * xdattach: attach a disk.   this can be called from autoconf and also
    706  * from xdopen/xdstrategy.
    707  */
    708 void
    709 xdattach(device_t parent, device_t self, void *aux)
    710 {
    711 	struct xd_softc *xd = device_private(self);
    712 	struct xdc_softc *xdc = device_private(parent);
    713 	struct xdc_attach_args *xa = aux;
    714 	int     rqno, spt = 0, mb, blk, lcv, fmode, s = 0, newstate;
    715 	struct xd_iopb_drive *driopb;
    716 	struct dkbad *dkb;
    717 	int			rseg, error;
    718 	bus_dma_segment_t	seg;
    719 	bus_addr_t		busaddr;
    720 	void *			dmaddr;
    721 	char *			buf;
    722 
    723 	xd->sc_dev = self;
    724 
    725 	/*
    726 	 * Always re-initialize the disk structure.  We want statistics
    727 	 * to start with a clean slate.
    728 	 */
    729 	memset(&xd->sc_dk, 0, sizeof(xd->sc_dk));
    730 
    731 	/* if booting, init the xd_softc */
    732 
    733 	if (xa->booting) {
    734 		xd->state = XD_DRIVE_UNKNOWN;	/* to start */
    735 		xd->flags = 0;
    736 		xd->parent = xdc;
    737 	}
    738 	xd->xd_drive = xa->driveno;
    739 	fmode = xa->fullmode;
    740 	xdc->sc_drives[xa->driveno] = xd;
    741 
    742 	/* if not booting, make sure we are the only process in the attach for
    743 	 * this drive.   if locked out, sleep on it. */
    744 
    745 	if (!xa->booting) {
    746 		s = splbio();
    747 		while (xd->state == XD_DRIVE_ATTACHING) {
    748 			if (tsleep(&xd->state, PRIBIO, "xdattach", 0)) {
    749 				splx(s);
    750 				return;
    751 			}
    752 		}
    753 		printf("%s at %s",
    754 			device_xname(xd->sc_dev), device_xname(xd->parent->sc_dev));
    755 	}
    756 
    757 	/* we now have control */
    758 	xd->state = XD_DRIVE_ATTACHING;
    759 	newstate = XD_DRIVE_UNKNOWN;
    760 
    761 	buf = NULL;
    762 	if ((error = xd_dmamem_alloc(xdc->dmatag, xdc->auxmap, &seg, &rseg,
    763 				     XDFM_BPS,
    764 				     (void **)&buf,
    765 				     &busaddr)) != 0) {
    766 		aprint_error_dev(xdc->sc_dev, "DMA buffer alloc error %d\n",
    767 			error);
    768 		return;
    769 	}
    770 	dmaddr = (void *)(u_long)BUS_ADDR_PADDR(busaddr);
    771 
    772 	/* first try and reset the drive */
    773 
    774 	rqno = xdc_cmd(xdc, XDCMD_RST, 0, xd->xd_drive, 0, 0, 0, fmode);
    775 	XDC_DONE(xdc, rqno, error);
    776 	if (error == XD_ERR_NRDY) {
    777 		printf(" drive %d: off-line\n", xa->driveno);
    778 		goto done;
    779 	}
    780 	if (error) {
    781 		printf(": ERROR 0x%02x (%s)\n", error, xdc_e2str(error));
    782 		goto done;
    783 	}
    784 	printf(" drive %d: ready\n", xa->driveno);
    785 
    786 	/* now set format parameters */
    787 
    788 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_FMT, xd->xd_drive, 0, 0, 0, fmode);
    789 	XDC_DONE(xdc, rqno, error);
    790 	if (error) {
    791 		aprint_error_dev(xd->sc_dev, "write format parameters failed: %s\n",
    792 			xdc_e2str(error));
    793 		goto done;
    794 	}
    795 
    796 	/* get drive parameters */
    797 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    798 	if (rqno != XD_ERR_FAIL) {
    799 		driopb = (struct xd_iopb_drive *) &xdc->iopbase[rqno];
    800 		spt = driopb->sectpertrk;
    801 	}
    802 	XDC_DONE(xdc, rqno, error);
    803 	if (error) {
    804 		aprint_error_dev(xd->sc_dev, "read drive parameters failed: %s\n",
    805 			xdc_e2str(error));
    806 		goto done;
    807 	}
    808 
    809 	/*
    810 	 * now set drive parameters (to semi-bogus values) so we can read the
    811 	 * disk label.
    812 	 */
    813 	xd->pcyl = xd->ncyl = 1;
    814 	xd->acyl = 0;
    815 	xd->nhead = 1;
    816 	xd->nsect = 1;
    817 	xd->sectpercyl = 1;
    818 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
    819 		xd->dkb.bt_bad[lcv].bt_cyl = xd->dkb.bt_bad[lcv].bt_trksec = 0xffff;
    820 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    821 	XDC_DONE(xdc, rqno, error);
    822 	if (error) {
    823 		aprint_error_dev(xd->sc_dev, "write drive parameters failed: %s\n",
    824 			xdc_e2str(error));
    825 		goto done;
    826 	}
    827 
    828 	/* read disk label */
    829 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, 0, 1, dmaddr, fmode);
    830 	XDC_DONE(xdc, rqno, error);
    831 	if (error) {
    832 		aprint_error_dev(xd->sc_dev, "reading disk label failed: %s\n",
    833 			xdc_e2str(error));
    834 		goto done;
    835 	}
    836 	newstate = XD_DRIVE_NOLABEL;
    837 
    838 	xd->hw_spt = spt;
    839 	/* Attach the disk: must be before getdisklabel to malloc label */
    840 	disk_init(&xd->sc_dk, device_xname(xd->sc_dev), &xddkdriver);
    841 	disk_attach(&xd->sc_dk);
    842 
    843 	if (xdgetdisklabel(xd, buf) != XD_ERR_AOK)
    844 		goto done;
    845 
    846 	/* inform the user of what is up */
    847 	printf("%s: <%s>, pcyl %d, hw_spt %d\n", device_xname(xd->sc_dev),
    848 		buf, xd->pcyl, spt);
    849 	mb = xd->ncyl * (xd->nhead * xd->nsect) / (1048576 / XDFM_BPS);
    850 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
    851 		device_xname(xd->sc_dev), mb, xd->ncyl, xd->nhead, xd->nsect,
    852 		XDFM_BPS);
    853 
    854 	/* now set the real drive parameters! */
    855 
    856 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    857 	XDC_DONE(xdc, rqno, error);
    858 	if (error) {
    859 		aprint_error_dev(xd->sc_dev, "write real drive parameters failed: %s\n",
    860 			xdc_e2str(error));
    861 		goto done;
    862 	}
    863 	newstate = XD_DRIVE_ONLINE;
    864 
    865 	/*
    866 	 * read bad144 table. this table resides on the first sector of the
    867 	 * last track of the disk (i.e. second cyl of "acyl" area).
    868 	 */
    869 
    870 	blk = (xd->ncyl + xd->acyl - 1) * (xd->nhead * xd->nsect) + /* last cyl */
    871 	    (xd->nhead - 1) * xd->nsect;	/* last head */
    872 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, blk, 1, dmaddr, fmode);
    873 	XDC_DONE(xdc, rqno, error);
    874 	if (error) {
    875 		aprint_error_dev(xd->sc_dev, "reading bad144 failed: %s\n",
    876 			xdc_e2str(error));
    877 		goto done;
    878 	}
    879 
    880 	/* check dkbad for sanity */
    881 	dkb = (struct dkbad *) buf;
    882 	for (lcv = 0; lcv < 126; lcv++) {
    883 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
    884 				dkb->bt_bad[lcv].bt_cyl == 0) &&
    885 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
    886 			continue;	/* blank */
    887 		if (dkb->bt_bad[lcv].bt_cyl >= xd->ncyl)
    888 			break;
    889 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xd->nhead)
    890 			break;
    891 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xd->nsect)
    892 			break;
    893 	}
    894 	if (lcv != 126) {
    895 		aprint_error_dev(xd->sc_dev, "warning: invalid bad144 sector!\n");
    896 	} else {
    897 		memcpy(&xd->dkb, buf, XDFM_BPS);
    898 	}
    899 
    900 done:
    901 	if (buf != NULL) {
    902 		xd_dmamem_free(xdc->dmatag, xdc->auxmap,
    903 				&seg, rseg, XDFM_BPS, buf);
    904 	}
    905 
    906 	xd->state = newstate;
    907 	if (!xa->booting) {
    908 		wakeup(&xd->state);
    909 		splx(s);
    910 	}
    911 }
    912 
    913 /*
    914  * end of autoconfig functions
    915  */
    916 
    917 /*
    918  * { b , c } d e v s w   f u n c t i o n s
    919  */
    920 
    921 /*
    922  * xdclose: close device
    923  */
    924 int
    925 xdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    926 {
    927 	struct xd_softc *xd = device_lookup_private(&xd_cd, DISKUNIT(dev));
    928 	int     part = DISKPART(dev);
    929 
    930 	/* clear mask bits */
    931 
    932 	switch (fmt) {
    933 	case S_IFCHR:
    934 		xd->sc_dk.dk_copenmask &= ~(1 << part);
    935 		break;
    936 	case S_IFBLK:
    937 		xd->sc_dk.dk_bopenmask &= ~(1 << part);
    938 		break;
    939 	}
    940 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
    941 
    942 	return 0;
    943 }
    944 
    945 /*
    946  * xddump: crash dump system
    947  */
    948 int
    949 xddump(dev_t dev, daddr_t blkno, void *va, size_t size)
    950 {
    951 	int     unit, part;
    952 	struct xd_softc *xd;
    953 
    954 	unit = DISKUNIT(dev);
    955 	part = DISKPART(dev);
    956 
    957 	xd = device_lookup_private(&xd_cd, unit);
    958 	if (!xd)
    959 		return ENXIO;
    960 
    961 	printf("%s%c: crash dump not supported (yet)\n", device_xname(xd->sc_dev),
    962 	    'a' + part);
    963 
    964 	return ENXIO;
    965 
    966 	/* outline: globals: "dumplo" == sector number of partition to start
    967 	 * dump at (convert to physical sector with partition table)
    968 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
    969 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
    970 	 * physmem)
    971 	 *
    972 	 * dump a copy of physical memory to the dump device starting at sector
    973 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
    974 	 * we go.   use polled I/O.
    975 	 *
    976 	 * XXX how to handle NON_CONTIG? */
    977 
    978 }
    979 
    980 static enum kauth_device_req
    981 xd_getkauthreq(u_char cmd)
    982 {
    983 	enum kauth_device_req req;
    984 
    985 	switch (cmd) {
    986 	case XDCMD_WR:
    987 	case XDCMD_XWR:
    988 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITE;
    989 		break;
    990 
    991 	case XDCMD_RD:
    992 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READ;
    993 		break;
    994 
    995 	case XDCMD_RDP:
    996 	case XDCMD_XRD:
    997 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF;
    998 		break;
    999 
   1000 	case XDCMD_WRP:
   1001 	case XDCMD_RST:
   1002 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF;
   1003 		break;
   1004 
   1005 	case XDCMD_NOP:
   1006 	case XDCMD_SK:
   1007 	case XDCMD_TST:
   1008 	default:
   1009 		req = 0;
   1010 		break;
   1011 	}
   1012 
   1013 	return (req);
   1014 }
   1015 
   1016 /*
   1017  * xdioctl: ioctls on XD drives.   based on ioctl's of other netbsd disks.
   1018  */
   1019 int
   1020 xdioctl(dev_t dev, u_long command, void *addr, int flag, struct lwp *l)
   1021 
   1022 {
   1023 	struct xd_softc *xd;
   1024 	struct xd_iocmd *xio;
   1025 	int     error, s, unit;
   1026 #ifdef __HAVE_OLD_DISKLABEL
   1027 	struct disklabel newlabel;
   1028 #endif
   1029 	struct disklabel *lp;
   1030 
   1031 	unit = DISKUNIT(dev);
   1032 
   1033 	if ((xd = device_lookup_private(&xd_cd, unit)) == NULL)
   1034 		return (ENXIO);
   1035 
   1036 	/* switch on ioctl type */
   1037 
   1038 	switch (command) {
   1039 	case DIOCSBAD:		/* set bad144 info */
   1040 		if ((flag & FWRITE) == 0)
   1041 			return EBADF;
   1042 		s = splbio();
   1043 		memcpy(&xd->dkb, addr, sizeof(xd->dkb));
   1044 		splx(s);
   1045 		return 0;
   1046 
   1047 	case DIOCGDINFO:	/* get disk label */
   1048 		memcpy(addr, xd->sc_dk.dk_label, sizeof(struct disklabel));
   1049 		return 0;
   1050 #ifdef __HAVE_OLD_DISKLABEL
   1051 	case ODIOCGDINFO:
   1052 		newlabel = *(xd->sc_dk.dk_label);
   1053 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1054 			return ENOTTY;
   1055 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
   1056 		return 0;
   1057 #endif
   1058 
   1059 	case DIOCGPART:	/* get partition info */
   1060 		((struct partinfo *) addr)->disklab = xd->sc_dk.dk_label;
   1061 		((struct partinfo *) addr)->part =
   1062 		    &xd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
   1063 		return 0;
   1064 
   1065 	case DIOCSDINFO:	/* set disk label */
   1066 #ifdef __HAVE_OLD_DISKLABEL
   1067 	case ODIOCSDINFO:
   1068 		if (command == ODIOCSDINFO) {
   1069 			memset(&newlabel, 0, sizeof newlabel);
   1070 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1071 			lp = &newlabel;
   1072 		} else
   1073 #endif
   1074 		lp = (struct disklabel *)addr;
   1075 
   1076 		if ((flag & FWRITE) == 0)
   1077 			return EBADF;
   1078 		error = setdisklabel(xd->sc_dk.dk_label,
   1079 		    lp, /* xd->sc_dk.dk_openmask : */ 0,
   1080 		    xd->sc_dk.dk_cpulabel);
   1081 		if (error == 0) {
   1082 			if (xd->state == XD_DRIVE_NOLABEL)
   1083 				xd->state = XD_DRIVE_ONLINE;
   1084 		}
   1085 		return error;
   1086 
   1087 	case DIOCWLABEL:	/* change write status of disk label */
   1088 		if ((flag & FWRITE) == 0)
   1089 			return EBADF;
   1090 		if (*(int *) addr)
   1091 			xd->flags |= XD_WLABEL;
   1092 		else
   1093 			xd->flags &= ~XD_WLABEL;
   1094 		return 0;
   1095 
   1096 	case DIOCWDINFO:	/* write disk label */
   1097 #ifdef __HAVE_OLD_DISKLABEL
   1098 	case ODIOCWDINFO:
   1099 		if (command == ODIOCWDINFO) {
   1100 			memset(&newlabel, 0, sizeof newlabel);
   1101 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
   1102 			lp = &newlabel;
   1103 		} else
   1104 #endif
   1105 		lp = (struct disklabel *)addr;
   1106 
   1107 		if ((flag & FWRITE) == 0)
   1108 			return EBADF;
   1109 		error = setdisklabel(xd->sc_dk.dk_label,
   1110 		    lp, /* xd->sc_dk.dk_openmask : */ 0,
   1111 		    xd->sc_dk.dk_cpulabel);
   1112 		if (error == 0) {
   1113 			if (xd->state == XD_DRIVE_NOLABEL)
   1114 				xd->state = XD_DRIVE_ONLINE;
   1115 
   1116 			/* Simulate opening partition 0 so write succeeds. */
   1117 			xd->sc_dk.dk_openmask |= (1 << 0);
   1118 			error = writedisklabel(MAKEDISKDEV(major(dev),
   1119 			    DISKUNIT(dev), RAW_PART),
   1120 			    xdstrategy, xd->sc_dk.dk_label,
   1121 			    xd->sc_dk.dk_cpulabel);
   1122 			xd->sc_dk.dk_openmask =
   1123 			    xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
   1124 		}
   1125 		return error;
   1126 
   1127 	case DIOSXDCMD: {
   1128 		enum kauth_device_req req;
   1129 
   1130 		xio = (struct xd_iocmd *) addr;
   1131 		req = xd_getkauthreq(xio->cmd);
   1132 		if ((error = kauth_authorize_device_passthru(l->l_cred,
   1133 		    dev, req, xio)) != 0)
   1134 			return (error);
   1135 		return (xdc_ioctlcmd(xd, dev, xio));
   1136 		}
   1137 
   1138 	default:
   1139 		return ENOTTY;
   1140 	}
   1141 }
   1142 /*
   1143  * xdopen: open drive
   1144  */
   1145 
   1146 int
   1147 xdopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1148 {
   1149 	int     unit, part;
   1150 	struct xd_softc *xd;
   1151 	struct xdc_attach_args xa;
   1152 
   1153 	/* first, could it be a valid target? */
   1154 
   1155 	unit = DISKUNIT(dev);
   1156 	if ((xd = device_lookup_private(&xd_cd, unit)) == NULL)
   1157 		return (ENXIO);
   1158 	part = DISKPART(dev);
   1159 
   1160 	/* do we need to attach the drive? */
   1161 
   1162 	if (xd->state == XD_DRIVE_UNKNOWN) {
   1163 		xa.driveno = xd->xd_drive;
   1164 		xa.fullmode = XD_SUB_WAIT;
   1165 		xa.booting = 0;
   1166 		xdattach(xd->parent->sc_dev, xd->sc_dev, &xa);
   1167 		if (xd->state == XD_DRIVE_UNKNOWN) {
   1168 			return (EIO);
   1169 		}
   1170 	}
   1171 	/* check for partition */
   1172 
   1173 	if (part != RAW_PART &&
   1174 	    (part >= xd->sc_dk.dk_label->d_npartitions ||
   1175 		xd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
   1176 		return (ENXIO);
   1177 	}
   1178 	/* set open masks */
   1179 
   1180 	switch (fmt) {
   1181 	case S_IFCHR:
   1182 		xd->sc_dk.dk_copenmask |= (1 << part);
   1183 		break;
   1184 	case S_IFBLK:
   1185 		xd->sc_dk.dk_bopenmask |= (1 << part);
   1186 		break;
   1187 	}
   1188 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
   1189 
   1190 	return 0;
   1191 }
   1192 
   1193 int
   1194 xdread(dev_t dev, struct uio *uio, int flags)
   1195 {
   1196 
   1197 	return (physio(xdstrategy, NULL, dev, B_READ, minphys, uio));
   1198 }
   1199 
   1200 int
   1201 xdwrite(dev_t dev, struct uio *uio, int flags)
   1202 {
   1203 
   1204 	return (physio(xdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1205 }
   1206 
   1207 
   1208 /*
   1209  * xdsize: return size of a partition for a dump
   1210  */
   1211 
   1212 int
   1213 xdsize(dev_t dev)
   1214 {
   1215 	struct xd_softc *xdsc;
   1216 	int     unit, part, size, omask;
   1217 
   1218 	/* valid unit? */
   1219 	unit = DISKUNIT(dev);
   1220 	if ((xdsc = device_lookup_private(&xd_cd, unit)) == NULL)
   1221 		return (-1);
   1222 
   1223 	part = DISKPART(dev);
   1224 	omask = xdsc->sc_dk.dk_openmask & (1 << part);
   1225 
   1226 	if (omask == 0 && xdopen(dev, 0, S_IFBLK, NULL) != 0)
   1227 		return (-1);
   1228 
   1229 	/* do it */
   1230 	if (xdsc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1231 		size = -1;	/* only give valid size for swap partitions */
   1232 	else
   1233 		size = xdsc->sc_dk.dk_label->d_partitions[part].p_size *
   1234 		    (xdsc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1235 	if (omask == 0 && xdclose(dev, 0, S_IFBLK, NULL) != 0)
   1236 		return (-1);
   1237 	return (size);
   1238 }
   1239 /*
   1240  * xdstrategy: buffering system interface to xd.
   1241  */
   1242 
   1243 void
   1244 xdstrategy(struct buf *bp)
   1245 {
   1246 	struct xd_softc *xd;
   1247 	struct xdc_softc *parent;
   1248 	int     s, unit;
   1249 	struct xdc_attach_args xa;
   1250 
   1251 	unit = DISKUNIT(bp->b_dev);
   1252 
   1253 	/* check for live device */
   1254 
   1255 	if (!(xd = device_lookup_private(&xd_cd, unit)) ||
   1256 	    bp->b_blkno < 0 ||
   1257 	    (bp->b_bcount % xd->sc_dk.dk_label->d_secsize) != 0) {
   1258 		bp->b_error = EINVAL;
   1259 		goto done;
   1260 	}
   1261 	/* do we need to attach the drive? */
   1262 
   1263 	if (xd->state == XD_DRIVE_UNKNOWN) {
   1264 		xa.driveno = xd->xd_drive;
   1265 		xa.fullmode = XD_SUB_WAIT;
   1266 		xa.booting = 0;
   1267 		xdattach(xd->parent->sc_dev, xd->sc_dev, &xa);
   1268 		if (xd->state == XD_DRIVE_UNKNOWN) {
   1269 			bp->b_error = EIO;
   1270 			goto done;
   1271 		}
   1272 	}
   1273 	if (xd->state != XD_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
   1274 		/* no I/O to unlabeled disks, unless raw partition */
   1275 		bp->b_error = EIO;
   1276 		goto done;
   1277 	}
   1278 	/* short circuit zero length request */
   1279 
   1280 	if (bp->b_bcount == 0)
   1281 		goto done;
   1282 
   1283 	/* check bounds with label (disksubr.c).  Determine the size of the
   1284 	 * transfer, and make sure it is within the boundaries of the
   1285 	 * partition. Adjust transfer if needed, and signal errors or early
   1286 	 * completion. */
   1287 
   1288 	if (bounds_check_with_label(&xd->sc_dk, bp,
   1289 		(xd->flags & XD_WLABEL) != 0) <= 0)
   1290 		goto done;
   1291 
   1292 	/*
   1293 	 * now we know we have a valid buf structure that we need to do I/O
   1294 	 * on.
   1295 	 *
   1296 	 * note that we don't disksort because the controller has a sorting
   1297 	 * algorithm built into the hardware.
   1298 	 */
   1299 
   1300 	s = splbio();		/* protect the queues */
   1301 
   1302 	/* first, give jobs in front of us a chance */
   1303 	parent = xd->parent;
   1304 	while (parent->nfree > 0 && bufq_peek(parent->sc_wq) != NULL)
   1305 		if (xdc_startbuf(parent, NULL, NULL) != XD_ERR_AOK)
   1306 			break;
   1307 
   1308 	/* if there are no free iorq's, then we just queue and return. the
   1309 	 * buffs will get picked up later by xdcintr().
   1310 	 */
   1311 
   1312 	if (parent->nfree == 0) {
   1313 		bufq_put(parent->sc_wq, bp);
   1314 		splx(s);
   1315 		return;
   1316 	}
   1317 
   1318 	/* now we have free iopb's and we are at splbio... start 'em up */
   1319 	if (xdc_startbuf(parent, xd, bp) != XD_ERR_AOK) {
   1320 		return;
   1321 	}
   1322 
   1323 	/* done! */
   1324 
   1325 	splx(s);
   1326 	return;
   1327 
   1328 done:				/* tells upper layers we are done with this
   1329 				 * buf */
   1330 	bp->b_resid = bp->b_bcount;
   1331 	biodone(bp);
   1332 }
   1333 /*
   1334  * end of {b,c}devsw functions
   1335  */
   1336 
   1337 /*
   1338  * i n t e r r u p t   f u n c t i o n
   1339  *
   1340  * xdcintr: hardware interrupt.
   1341  */
   1342 int
   1343 xdcintr(void *v)
   1344 {
   1345 	struct xdc_softc *xdcsc = v;
   1346 
   1347 	/* kick the event counter */
   1348 
   1349 	xdcsc->sc_intrcnt.ev_count++;
   1350 
   1351 	/* remove as many done IOPBs as possible */
   1352 
   1353 	xdc_remove_iorq(xdcsc);
   1354 
   1355 	/* start any iorq's already waiting */
   1356 
   1357 	xdc_start(xdcsc, XDC_MAXIOPB);
   1358 
   1359 	/* fill up any remaining iorq's with queue'd buffers */
   1360 
   1361 	while (xdcsc->nfree > 0 && bufq_peek(xdcsc->sc_wq) != NULL)
   1362 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
   1363 			break;
   1364 
   1365 	return (1);
   1366 }
   1367 /*
   1368  * end of interrupt function
   1369  */
   1370 
   1371 /*
   1372  * i n t e r n a l   f u n c t i o n s
   1373  */
   1374 
   1375 /*
   1376  * xdc_rqinit: fill out the fields of an I/O request
   1377  */
   1378 
   1379 inline void
   1380 xdc_rqinit(struct xd_iorq *rq, struct xdc_softc *xdc, struct xd_softc *xd, int md, u_long blk, int cnt, void *db, struct buf *bp)
   1381 {
   1382 	rq->xdc = xdc;
   1383 	rq->xd = xd;
   1384 	rq->ttl = XDC_MAXTTL + 10;
   1385 	rq->mode = md;
   1386 	rq->tries = rq->errnum = rq->lasterror = 0;
   1387 	rq->blockno = blk;
   1388 	rq->sectcnt = cnt;
   1389 	rq->dbuf = db;
   1390 	rq->buf = bp;
   1391 }
   1392 /*
   1393  * xdc_rqtopb: load up an IOPB based on an iorq
   1394  */
   1395 
   1396 void
   1397 xdc_rqtopb(struct xd_iorq *iorq, struct xd_iopb *iopb, int cmd, int subfun)
   1398 {
   1399 	u_long  block, dp;
   1400 
   1401 	/* standard stuff */
   1402 
   1403 	iopb->errs = iopb->done = 0;
   1404 	iopb->comm = cmd;
   1405 	iopb->errnum = iopb->status = 0;
   1406 	iopb->subfun = subfun;
   1407 	if (iorq->xd)
   1408 		iopb->unit = iorq->xd->xd_drive;
   1409 	else
   1410 		iopb->unit = 0;
   1411 
   1412 	/* check for alternate IOPB format */
   1413 
   1414 	if (cmd == XDCMD_WRP) {
   1415 		switch (subfun) {
   1416 		case XDFUN_CTL:{
   1417 			struct xd_iopb_ctrl *ctrl =
   1418 				(struct xd_iopb_ctrl *) iopb;
   1419 			iopb->lll = 0;
   1420 			iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1421 					? 0
   1422 					: iorq->xdc->ipl;
   1423 			ctrl->param_a = XDPA_TMOD | XDPA_DACF;
   1424 			ctrl->param_b = XDPB_ROR | XDPB_TDT_3_2USEC;
   1425 			ctrl->param_c = XDPC_OVS | XDPC_COP | XDPC_ASR |
   1426 					XDPC_RBC | XDPC_ECC2;
   1427 			ctrl->throttle = XDC_THROTTLE;
   1428 			ctrl->delay = XDC_DELAY;
   1429 			break;
   1430 			}
   1431 		case XDFUN_DRV:{
   1432 			struct xd_iopb_drive *drv =
   1433 				(struct xd_iopb_drive *)iopb;
   1434 			/* we assume that the disk label has the right
   1435 			 * info */
   1436 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1437 				drv->dparam_ipl = (XDC_DPARAM << 3);
   1438 			else
   1439 				drv->dparam_ipl = (XDC_DPARAM << 3) |
   1440 						  iorq->xdc->ipl;
   1441 			drv->maxsect = iorq->xd->nsect - 1;
   1442 			drv->maxsector = drv->maxsect;
   1443 			/* note: maxsector != maxsect only if you are
   1444 			 * doing cyl sparing */
   1445 			drv->headoff = 0;
   1446 			drv->maxcyl = iorq->xd->pcyl - 1;
   1447 			drv->maxhead = iorq->xd->nhead - 1;
   1448 			break;
   1449 			}
   1450 		case XDFUN_FMT:{
   1451 			struct xd_iopb_format *form =
   1452 					(struct xd_iopb_format *) iopb;
   1453 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1454 				form->interleave_ipl = (XDC_INTERLEAVE << 3);
   1455 			else
   1456 				form->interleave_ipl = (XDC_INTERLEAVE << 3) |
   1457 						       iorq->xdc->ipl;
   1458 			form->field1 = XDFM_FIELD1;
   1459 			form->field2 = XDFM_FIELD2;
   1460 			form->field3 = XDFM_FIELD3;
   1461 			form->field4 = XDFM_FIELD4;
   1462 			form->bytespersec = XDFM_BPS;
   1463 			form->field6 = XDFM_FIELD6;
   1464 			form->field7 = XDFM_FIELD7;
   1465 			break;
   1466 			}
   1467 		}
   1468 	} else {
   1469 
   1470 		/* normal IOPB case (harmless to RDP command) */
   1471 
   1472 		iopb->lll = 0;
   1473 		iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1474 				? 0
   1475 				: iorq->xdc->ipl;
   1476 		iopb->sectcnt = iorq->sectcnt;
   1477 		block = iorq->blockno;
   1478 		if (iorq->xd == NULL || block == 0) {
   1479 			iopb->sectno = iopb->headno = iopb->cylno = 0;
   1480 		} else {
   1481 			iopb->sectno = block % iorq->xd->nsect;
   1482 			block = block / iorq->xd->nsect;
   1483 			iopb->headno = block % iorq->xd->nhead;
   1484 			block = block / iorq->xd->nhead;
   1485 			iopb->cylno = block;
   1486 		}
   1487 		dp = (u_long) iorq->dbuf;
   1488 		dp = iopb->daddr = (iorq->dbuf == NULL) ? 0 : dp;
   1489 		iopb->addrmod = ((dp + (XDFM_BPS * iorq->sectcnt)) > 0x1000000)
   1490 					? XDC_ADDRMOD32
   1491 					: XDC_ADDRMOD;
   1492 	}
   1493 }
   1494 
   1495 /*
   1496  * xdc_cmd: front end for POLL'd and WAIT'd commands.  Returns rqno.
   1497  * If you've already got an IORQ, you can call submit directly (currently
   1498  * there is no need to do this).    NORM requests are handled separately.
   1499  */
   1500 int
   1501 xdc_cmd(struct xdc_softc *xdcsc, int cmd, int subfn, int unit, int block,
   1502 	int scnt, char *dptr, int fullmode)
   1503 {
   1504 	int     rqno, submode = XD_STATE(fullmode), retry;
   1505 	struct xd_iorq *iorq;
   1506 	struct xd_iopb *iopb;
   1507 
   1508 	/* get iorq/iopb */
   1509 	switch (submode) {
   1510 	case XD_SUB_POLL:
   1511 		while (xdcsc->nfree == 0) {
   1512 			if (xdc_piodriver(xdcsc, 0, 1) != XD_ERR_AOK)
   1513 				return (XD_ERR_FAIL);
   1514 		}
   1515 		break;
   1516 	case XD_SUB_WAIT:
   1517 		retry = 1;
   1518 		while (retry) {
   1519 			while (xdcsc->nfree == 0) {
   1520 			    if (tsleep(&xdcsc->nfree, PRIBIO, "xdnfree", 0))
   1521 				return (XD_ERR_FAIL);
   1522 			}
   1523 			while (xdcsc->ndone > XDC_SUBWAITLIM) {
   1524 			    if (tsleep(&xdcsc->ndone, PRIBIO, "xdsubwait", 0))
   1525 				return (XD_ERR_FAIL);
   1526 			}
   1527 			if (xdcsc->nfree)
   1528 				retry = 0;	/* got it */
   1529 		}
   1530 		break;
   1531 	default:
   1532 		return (XD_ERR_FAIL);	/* illegal */
   1533 	}
   1534 	if (xdcsc->nfree == 0)
   1535 		panic("xdcmd nfree");
   1536 	rqno = XDC_RQALLOC(xdcsc);
   1537 	iorq = &xdcsc->reqs[rqno];
   1538 	iopb = iorq->iopb;
   1539 
   1540 
   1541 	/* init iorq/iopb */
   1542 
   1543 	xdc_rqinit(iorq, xdcsc,
   1544 	    (unit == XDC_NOUNIT) ? NULL : xdcsc->sc_drives[unit],
   1545 	    fullmode, block, scnt, dptr, NULL);
   1546 
   1547 	/* load IOPB from iorq */
   1548 
   1549 	xdc_rqtopb(iorq, iopb, cmd, subfn);
   1550 
   1551 	/* submit it for processing */
   1552 
   1553 	xdc_submit_iorq(xdcsc, rqno, fullmode);	/* error code will be in iorq */
   1554 
   1555 	return (rqno);
   1556 }
   1557 /*
   1558  * xdc_startbuf
   1559  * start a buffer running, assumes nfree > 0
   1560  */
   1561 
   1562 int
   1563 xdc_startbuf(struct xdc_softc *xdcsc, struct xd_softc *xdsc, struct buf *bp)
   1564 {
   1565 	int     rqno, partno;
   1566 	struct xd_iorq *iorq;
   1567 	struct xd_iopb *iopb;
   1568 	u_long  block;
   1569 /*	void *dbuf;*/
   1570 	int error;
   1571 
   1572 	if (!xdcsc->nfree)
   1573 		panic("xdc_startbuf free");
   1574 	rqno = XDC_RQALLOC(xdcsc);
   1575 	iorq = &xdcsc->reqs[rqno];
   1576 	iopb = iorq->iopb;
   1577 
   1578 	/* get buf */
   1579 
   1580 	if (bp == NULL) {
   1581 		bp = bufq_get(xdcsc->sc_wq);
   1582 		if (bp == NULL)
   1583 			panic("xdc_startbuf bp");
   1584 		xdsc = xdcsc->sc_drives[DISKUNIT(bp->b_dev)];
   1585 	}
   1586 	partno = DISKPART(bp->b_dev);
   1587 #ifdef XDC_DEBUG
   1588 	printf("xdc_startbuf: %s%c: %s block %d\n", device_xname(xdsc->sc_dev),
   1589 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
   1590 	printf("xdc_startbuf: b_bcount %d, b_data 0x%x\n",
   1591 	    bp->b_bcount, bp->b_data);
   1592 #endif
   1593 
   1594 	/*
   1595 	 * load request.  we have to calculate the correct block number based
   1596 	 * on partition info.
   1597 	 *
   1598 	 * note that iorq points to the buffer as mapped into DVMA space,
   1599 	 * where as the bp->b_data points to its non-DVMA mapping.
   1600 	 */
   1601 
   1602 	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
   1603 	    xdsc->sc_dk.dk_label->d_partitions[partno].p_offset);
   1604 
   1605 	error = bus_dmamap_load(xdcsc->dmatag, iorq->dmamap,
   1606 			 bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
   1607 	if (error != 0) {
   1608 		aprint_error_dev(xdcsc->sc_dev, "warning: cannot load DMA map\n");
   1609 		XDC_FREE(xdcsc, rqno);
   1610 		bufq_put(xdcsc->sc_wq, bp);
   1611 		return (XD_ERR_FAIL);	/* XXX: need some sort of
   1612 					 * call-back scheme here? */
   1613 	}
   1614 	bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap, 0,
   1615 			iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
   1616 				? BUS_DMASYNC_PREREAD
   1617 				: BUS_DMASYNC_PREWRITE);
   1618 
   1619 	/* init iorq and load iopb from it */
   1620 	xdc_rqinit(iorq, xdcsc, xdsc, XD_SUB_NORM | XD_MODE_VERBO, block,
   1621 		   bp->b_bcount / XDFM_BPS,
   1622 		   (void *)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
   1623 		   bp);
   1624 
   1625 	xdc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XDCMD_RD : XDCMD_WR, 0);
   1626 
   1627 	/* Instrumentation. */
   1628 	disk_busy(&xdsc->sc_dk);
   1629 
   1630 	/* now submit [note that xdc_submit_iorq can never fail on NORM reqs] */
   1631 
   1632 	xdc_submit_iorq(xdcsc, rqno, XD_SUB_NORM);
   1633 	return (XD_ERR_AOK);
   1634 }
   1635 
   1636 
   1637 /*
   1638  * xdc_submit_iorq: submit an iorq for processing.  returns XD_ERR_AOK
   1639  * if ok.  if it fail returns an error code.  type is XD_SUB_*.
   1640  *
   1641  * note: caller frees iorq in all cases except NORM
   1642  *
   1643  * return value:
   1644  *   NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
   1645  *   WAIT: XD_AOK (success), <error-code> (failed)
   1646  *   POLL: <same as WAIT>
   1647  *   NOQ : <same as NORM>
   1648  *
   1649  * there are three sources for i/o requests:
   1650  * [1] xdstrategy: normal block I/O, using "struct buf" system.
   1651  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
   1652  * [3] open/ioctl: these are I/O requests done in the context of a process,
   1653  *                 and the process should block until they are done.
   1654  *
   1655  * software state is stored in the iorq structure.  each iorq has an
   1656  * iopb structure.  the hardware understands the iopb structure.
   1657  * every command must go through an iopb.  a 7053 can only handle
   1658  * XDC_MAXIOPB (31) active iopbs at one time.  iopbs are allocated in
   1659  * DVMA space at boot up time.  what happens if we run out of iopb's?
   1660  * for i/o type [1], the buffers are queued at the "buff" layer and
   1661  * picked up later by the interrupt routine.  for case [2] the
   1662  * programmed i/o driver is called with a special flag that says
   1663  * return when one iopb is free.  for case [3] the process can sleep
   1664  * on the iorq free list until some iopbs are available.
   1665  */
   1666 
   1667 
   1668 int
   1669 xdc_submit_iorq(struct xdc_softc *xdcsc, int iorqno, int type)
   1670 {
   1671 	u_long  iopbaddr;
   1672 	struct xd_iorq *iorq = &xdcsc->reqs[iorqno];
   1673 
   1674 #ifdef XDC_DEBUG
   1675 	printf("xdc_submit_iorq(%s, no=%d, type=%d)\n", device_xname(xdcsc->sc_dev),
   1676 	    iorqno, type);
   1677 #endif
   1678 
   1679 	/* first check and see if controller is busy */
   1680 	if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
   1681 #ifdef XDC_DEBUG
   1682 		printf("xdc_submit_iorq: XDC not ready (ADDING)\n");
   1683 #endif
   1684 		if (type == XD_SUB_NOQ)
   1685 			return (XD_ERR_FAIL);	/* failed */
   1686 		XDC_TWAIT(xdcsc, iorqno);	/* put at end of waitq */
   1687 		switch (type) {
   1688 		case XD_SUB_NORM:
   1689 			return XD_ERR_AOK;	/* success */
   1690 		case XD_SUB_WAIT:
   1691 			while (iorq->iopb->done == 0) {
   1692 				(void) tsleep(iorq, PRIBIO, "xdciorq", 0);
   1693 			}
   1694 			return (iorq->errnum);
   1695 		case XD_SUB_POLL:
   1696 			return (xdc_piodriver(xdcsc, iorqno, 0));
   1697 		default:
   1698 			panic("xdc_submit_iorq adding");
   1699 		}
   1700 	}
   1701 #ifdef XDC_DEBUG
   1702 	{
   1703 		u_char *rio = (u_char *) iorq->iopb;
   1704 		int     sz = sizeof(struct xd_iopb), lcv;
   1705 		printf("%s: aio #%d [",
   1706 			device_xname(xdcsc->sc_dev), iorq - xdcsc->reqs);
   1707 		for (lcv = 0; lcv < sz; lcv++)
   1708 			printf(" %02x", rio[lcv]);
   1709 		printf("]\n");
   1710 	}
   1711 #endif				/* XDC_DEBUG */
   1712 
   1713 	/* controller not busy, start command */
   1714 	iopbaddr = (u_long) iorq->dmaiopb;
   1715 	XDC_GO(xdcsc->xdc, iopbaddr);	/* go! */
   1716 	xdcsc->nrun++;
   1717 	/* command now running, wrap it up */
   1718 	switch (type) {
   1719 	case XD_SUB_NORM:
   1720 	case XD_SUB_NOQ:
   1721 		return (XD_ERR_AOK);	/* success */
   1722 	case XD_SUB_WAIT:
   1723 		while (iorq->iopb->done == 0) {
   1724 			(void) tsleep(iorq, PRIBIO, "xdciorq", 0);
   1725 		}
   1726 		return (iorq->errnum);
   1727 	case XD_SUB_POLL:
   1728 		return (xdc_piodriver(xdcsc, iorqno, 0));
   1729 	default:
   1730 		panic("xdc_submit_iorq wrap up");
   1731 	}
   1732 	panic("xdc_submit_iorq");
   1733 	return 0;	/* not reached */
   1734 }
   1735 
   1736 
   1737 /*
   1738  * xdc_piodriver
   1739  *
   1740  * programmed i/o driver.   this function takes over the computer
   1741  * and drains off all i/o requests.   it returns the status of the iorq
   1742  * the caller is interesting in.   if freeone is true, then it returns
   1743  * when there is a free iorq.
   1744  */
   1745 int
   1746 xdc_piodriver(struct xdc_softc *xdcsc, int iorqno, int freeone)
   1747 {
   1748 	int	nreset = 0;
   1749 	int	retval = 0;
   1750 	u_long	count;
   1751 	struct	xdc *xdc = xdcsc->xdc;
   1752 #ifdef XDC_DEBUG
   1753 	printf("xdc_piodriver(%s, %d, freeone=%d)\n", device_xname(xdcsc->sc_dev),
   1754 	    iorqno, freeone);
   1755 #endif
   1756 
   1757 	while (xdcsc->nwait || xdcsc->nrun) {
   1758 #ifdef XDC_DEBUG
   1759 		printf("xdc_piodriver: wait=%d, run=%d\n",
   1760 			xdcsc->nwait, xdcsc->nrun);
   1761 #endif
   1762 		XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
   1763 #ifdef XDC_DEBUG
   1764 		printf("xdc_piodriver: done wait with count = %d\n", count);
   1765 #endif
   1766 		/* we expect some progress soon */
   1767 		if (count == 0 && nreset >= 2) {
   1768 			xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
   1769 #ifdef XDC_DEBUG
   1770 			printf("xdc_piodriver: timeout\n");
   1771 #endif
   1772 			return (XD_ERR_FAIL);
   1773 		}
   1774 		if (count == 0) {
   1775 			if (xdc_reset(xdcsc, 0,
   1776 				      (nreset++ == 0) ? XD_RSET_NONE : iorqno,
   1777 				      XD_ERR_FAIL,
   1778 				      0) == XD_ERR_FAIL)
   1779 				return (XD_ERR_FAIL);	/* flushes all but POLL
   1780 							 * requests, resets */
   1781 			continue;
   1782 		}
   1783 		xdc_remove_iorq(xdcsc);	/* could resubmit request */
   1784 		if (freeone) {
   1785 			if (xdcsc->nrun < XDC_MAXIOPB) {
   1786 #ifdef XDC_DEBUG
   1787 				printf("xdc_piodriver: done: one free\n");
   1788 #endif
   1789 				return (XD_ERR_AOK);
   1790 			}
   1791 			continue;	/* don't xdc_start */
   1792 		}
   1793 		xdc_start(xdcsc, XDC_MAXIOPB);
   1794 	}
   1795 
   1796 	/* get return value */
   1797 
   1798 	retval = xdcsc->reqs[iorqno].errnum;
   1799 
   1800 #ifdef XDC_DEBUG
   1801 	printf("xdc_piodriver: done, retval = 0x%x (%s)\n",
   1802 	    xdcsc->reqs[iorqno].errnum, xdc_e2str(xdcsc->reqs[iorqno].errnum));
   1803 #endif
   1804 
   1805 	/* now that we've drained everything, start up any bufs that have
   1806 	 * queued */
   1807 
   1808 	while (xdcsc->nfree > 0 && bufq_peek(xdcsc->sc_wq) != NULL)
   1809 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
   1810 			break;
   1811 
   1812 	return (retval);
   1813 }
   1814 
   1815 /*
   1816  * xdc_reset: reset one drive.   NOTE: assumes xdc was just reset.
   1817  * we steal iopb[0] for this, but we put it back when we are done.
   1818  */
   1819 void
   1820 xdc_xdreset(struct xdc_softc *xdcsc, struct xd_softc *xdsc)
   1821 {
   1822 	struct xd_iopb tmpiopb;
   1823 	u_long  addr;
   1824 	int     del;
   1825 	memcpy(&tmpiopb, xdcsc->iopbase, sizeof(tmpiopb));
   1826 	memset(xdcsc->iopbase, 0, sizeof(tmpiopb));
   1827 	xdcsc->iopbase->comm = XDCMD_RST;
   1828 	xdcsc->iopbase->unit = xdsc->xd_drive;
   1829 	addr = (u_long) xdcsc->dvmaiopb;
   1830 	XDC_GO(xdcsc->xdc, addr);	/* go! */
   1831 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_REMIOPB);
   1832 	if (del <= 0 || xdcsc->iopbase->errs) {
   1833 		printf("%s: off-line: %s\n", device_xname(xdcsc->sc_dev),
   1834 		    xdc_e2str(xdcsc->iopbase->errnum));
   1835 		xdcsc->xdc->xdc_csr = XDC_RESET;
   1836 		XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
   1837 		if (del <= 0)
   1838 			panic("xdc_reset");
   1839 	} else {
   1840 		xdcsc->xdc->xdc_csr = XDC_CLRRIO;	/* clear RIO */
   1841 	}
   1842 	memcpy(xdcsc->iopbase, &tmpiopb, sizeof(tmpiopb));
   1843 }
   1844 
   1845 
   1846 /*
   1847  * xdc_reset: reset everything: requests are marked as errors except
   1848  * a polled request (which is resubmitted)
   1849  */
   1850 int
   1851 xdc_reset(struct xdc_softc *xdcsc, int quiet, int blastmode, int error,
   1852 	struct xd_softc *xdsc)
   1853 
   1854 {
   1855 	int     del = 0, lcv, retval = XD_ERR_AOK;
   1856 	int     oldfree = xdcsc->nfree;
   1857 
   1858 	/* soft reset hardware */
   1859 
   1860 	if (!quiet)
   1861 		printf("%s: soft reset\n", device_xname(xdcsc->sc_dev));
   1862 	xdcsc->xdc->xdc_csr = XDC_RESET;
   1863 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
   1864 	if (del <= 0) {
   1865 		blastmode = XD_RSET_ALL;	/* dead, flush all requests */
   1866 		retval = XD_ERR_FAIL;
   1867 	}
   1868 	if (xdsc)
   1869 		xdc_xdreset(xdcsc, xdsc);
   1870 
   1871 	/* fix queues based on "blast-mode" */
   1872 
   1873 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   1874 		register struct xd_iorq *iorq = &xdcsc->reqs[lcv];
   1875 
   1876 		if (XD_STATE(iorq->mode) != XD_SUB_POLL &&
   1877 		    XD_STATE(iorq->mode) != XD_SUB_WAIT &&
   1878 		    XD_STATE(iorq->mode) != XD_SUB_NORM)
   1879 			/* is it active? */
   1880 			continue;
   1881 
   1882 		xdcsc->nrun--;	/* it isn't running any more */
   1883 		if (blastmode == XD_RSET_ALL || blastmode != lcv) {
   1884 			/* failed */
   1885 			iorq->errnum = error;
   1886 			xdcsc->iopbase[lcv].done = xdcsc->iopbase[lcv].errs = 1;
   1887 			switch (XD_STATE(xdcsc->reqs[lcv].mode)) {
   1888 			case XD_SUB_NORM:
   1889 			    iorq->buf->b_error = EIO;
   1890 			    iorq->buf->b_resid =
   1891 			       iorq->sectcnt * XDFM_BPS;
   1892 
   1893 			    bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap, 0,
   1894 					    iorq->dmamap->dm_mapsize,
   1895 					    (iorq->buf->b_flags & B_READ)
   1896 						? BUS_DMASYNC_POSTREAD
   1897 						: BUS_DMASYNC_POSTWRITE);
   1898 
   1899 			    bus_dmamap_unload(xdcsc->dmatag, iorq->dmamap);
   1900 
   1901 			    disk_unbusy(&xdcsc->reqs[lcv].xd->sc_dk,
   1902 				(xdcsc->reqs[lcv].buf->b_bcount -
   1903 				xdcsc->reqs[lcv].buf->b_resid),
   1904 				(iorq->buf->b_flags & B_READ));
   1905 			    biodone(iorq->buf);
   1906 			    XDC_FREE(xdcsc, lcv);	/* add to free list */
   1907 			    break;
   1908 			case XD_SUB_WAIT:
   1909 			    wakeup(iorq);
   1910 			case XD_SUB_POLL:
   1911 			    xdcsc->ndone++;
   1912 			    iorq->mode =
   1913 				XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   1914 			    break;
   1915 			}
   1916 
   1917 		} else {
   1918 
   1919 			/* resubmit, put at front of wait queue */
   1920 			XDC_HWAIT(xdcsc, lcv);
   1921 		}
   1922 	}
   1923 
   1924 	/*
   1925 	 * now, if stuff is waiting, start it.
   1926 	 * since we just reset it should go
   1927 	 */
   1928 	xdc_start(xdcsc, XDC_MAXIOPB);
   1929 
   1930 	/* ok, we did it */
   1931 	if (oldfree == 0 && xdcsc->nfree)
   1932 		wakeup(&xdcsc->nfree);
   1933 
   1934 #ifdef XDC_DIAG
   1935 	del = xdcsc->nwait + xdcsc->nrun + xdcsc->nfree + xdcsc->ndone;
   1936 	if (del != XDC_MAXIOPB)
   1937 		printf("%s: diag: xdc_reset miscount (%d should be %d)!\n",
   1938 		    device_xname(xdcsc->sc_dev), del, XDC_MAXIOPB);
   1939 	else
   1940 		if (xdcsc->ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
   1941 			printf("%s: diag: lots of done jobs (%d)\n",
   1942 			    device_xname(xdcsc->sc_dev), xdcsc->ndone);
   1943 #endif
   1944 	printf("RESET DONE\n");
   1945 	return (retval);
   1946 }
   1947 /*
   1948  * xdc_start: start all waiting buffers
   1949  */
   1950 
   1951 void
   1952 xdc_start(struct xdc_softc *xdcsc, int maxio)
   1953 
   1954 {
   1955 	int     rqno;
   1956 	while (maxio && xdcsc->nwait &&
   1957 		(xdcsc->xdc->xdc_csr & XDC_ADDING) == 0) {
   1958 		XDC_GET_WAITER(xdcsc, rqno);	/* note: rqno is an "out"
   1959 						 * param */
   1960 		if (xdc_submit_iorq(xdcsc, rqno, XD_SUB_NOQ) != XD_ERR_AOK)
   1961 			panic("xdc_start");	/* should never happen */
   1962 		maxio--;
   1963 	}
   1964 }
   1965 /*
   1966  * xdc_remove_iorq: remove "done" IOPB's.
   1967  */
   1968 
   1969 int
   1970 xdc_remove_iorq(struct xdc_softc *xdcsc)
   1971 {
   1972 	int     errnum, rqno, comm, errs;
   1973 	struct xdc *xdc = xdcsc->xdc;
   1974 	struct xd_iopb *iopb;
   1975 	struct xd_iorq *iorq;
   1976 	struct buf *bp;
   1977 
   1978 	if (xdc->xdc_csr & XDC_F_ERROR) {
   1979 		/*
   1980 		 * FATAL ERROR: should never happen under normal use. This
   1981 		 * error is so bad, you can't even tell which IOPB is bad, so
   1982 		 * we dump them all.
   1983 		 */
   1984 		errnum = xdc->xdc_f_err;
   1985 		aprint_error_dev(xdcsc->sc_dev, "fatal error 0x%02x: %s\n",
   1986 		    errnum, xdc_e2str(errnum));
   1987 		if (xdc_reset(xdcsc, 0, XD_RSET_ALL, errnum, 0) != XD_ERR_AOK) {
   1988 			aprint_error_dev(xdcsc->sc_dev, "soft reset failed!\n");
   1989 			panic("xdc_remove_iorq: controller DEAD");
   1990 		}
   1991 		return (XD_ERR_AOK);
   1992 	}
   1993 
   1994 	/*
   1995 	 * get iopb that is done
   1996 	 *
   1997 	 * hmm... I used to read the address of the done IOPB off the VME
   1998 	 * registers and calculate the rqno directly from that.   that worked
   1999 	 * until I started putting a load on the controller.   when loaded, i
   2000 	 * would get interrupts but neither the REMIOPB or F_ERROR bits would
   2001 	 * be set, even after DELAY'ing a while!   later on the timeout
   2002 	 * routine would detect IOPBs that were marked "running" but their
   2003 	 * "done" bit was set.   rather than dealing directly with this
   2004 	 * problem, it is just easier to look at all running IOPB's for the
   2005 	 * done bit.
   2006 	 */
   2007 	if (xdc->xdc_csr & XDC_REMIOPB) {
   2008 		xdc->xdc_csr = XDC_CLRRIO;
   2009 	}
   2010 
   2011 	for (rqno = 0; rqno < XDC_MAXIOPB; rqno++) {
   2012 		iorq = &xdcsc->reqs[rqno];
   2013 		if (iorq->mode == 0 || XD_STATE(iorq->mode) == XD_SUB_DONE)
   2014 			continue;	/* free, or done */
   2015 		iopb = &xdcsc->iopbase[rqno];
   2016 		if (iopb->done == 0)
   2017 			continue;	/* not done yet */
   2018 
   2019 #ifdef XDC_DEBUG
   2020 		{
   2021 			u_char *rio = (u_char *) iopb;
   2022 			int     sz = sizeof(struct xd_iopb), lcv;
   2023 			printf("%s: rio #%d [", device_xname(xdcsc->sc_dev), rqno);
   2024 			for (lcv = 0; lcv < sz; lcv++)
   2025 				printf(" %02x", rio[lcv]);
   2026 			printf("]\n");
   2027 		}
   2028 #endif				/* XDC_DEBUG */
   2029 
   2030 		xdcsc->nrun--;
   2031 
   2032 		comm = iopb->comm;
   2033 		errs = iopb->errs;
   2034 
   2035 		if (errs)
   2036 			iorq->errnum = iopb->errnum;
   2037 		else
   2038 			iorq->errnum = 0;
   2039 
   2040 		/* handle non-fatal errors */
   2041 
   2042 		if (errs &&
   2043 		    xdc_error(xdcsc, iorq, iopb, rqno, comm) == XD_ERR_AOK)
   2044 			continue;	/* AOK: we resubmitted it */
   2045 
   2046 
   2047 		/* this iorq is now done (hasn't been restarted or anything) */
   2048 
   2049 		if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
   2050 			xdc_perror(iorq, iopb, 0);
   2051 
   2052 		/* now, if read/write check to make sure we got all the data
   2053 		 * we needed. (this may not be the case if we got an error in
   2054 		 * the middle of a multisector request).   */
   2055 
   2056 		if ((iorq->mode & XD_MODE_B144) != 0 && errs == 0 &&
   2057 		    (comm == XDCMD_RD || comm == XDCMD_WR)) {
   2058 			/* we just successfully processed a bad144 sector
   2059 			 * note: if we are in bad 144 mode, the pointers have
   2060 			 * been advanced already (see above) and are pointing
   2061 			 * at the bad144 sector.   to exit bad144 mode, we
   2062 			 * must advance the pointers 1 sector and issue a new
   2063 			 * request if there are still sectors left to process
   2064 			 *
   2065 			 */
   2066 			XDC_ADVANCE(iorq, 1);	/* advance 1 sector */
   2067 
   2068 			/* exit b144 mode */
   2069 			iorq->mode = iorq->mode & (~XD_MODE_B144);
   2070 
   2071 			if (iorq->sectcnt) {	/* more to go! */
   2072 				iorq->lasterror = iorq->errnum = iopb->errnum = 0;
   2073 				iopb->errs = iopb->done = 0;
   2074 				iorq->tries = 0;
   2075 				iopb->sectcnt = iorq->sectcnt;
   2076 				iopb->cylno = iorq->blockno /
   2077 						iorq->xd->sectpercyl;
   2078 				iopb->headno =
   2079 					(iorq->blockno / iorq->xd->nhead) %
   2080 						iorq->xd->nhead;
   2081 				iopb->sectno = iorq->blockno % XDFM_BPS;
   2082 				iopb->daddr = (u_long) iorq->dbuf;
   2083 				XDC_HWAIT(xdcsc, rqno);
   2084 				xdc_start(xdcsc, 1);	/* resubmit */
   2085 				continue;
   2086 			}
   2087 		}
   2088 		/* final cleanup, totally done with this request */
   2089 
   2090 		switch (XD_STATE(iorq->mode)) {
   2091 		case XD_SUB_NORM:
   2092 			bp = iorq->buf;
   2093 			if (errs) {
   2094 				bp->b_error = EIO;
   2095 				bp->b_resid = iorq->sectcnt * XDFM_BPS;
   2096 			} else {
   2097 				bp->b_resid = 0;	/* done */
   2098 			}
   2099 			bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap, 0,
   2100 					iorq->dmamap->dm_mapsize,
   2101 					(bp->b_flags & B_READ)
   2102 						? BUS_DMASYNC_POSTREAD
   2103 						: BUS_DMASYNC_POSTWRITE);
   2104 			bus_dmamap_unload(xdcsc->dmatag, iorq->dmamap);
   2105 
   2106 			disk_unbusy(&iorq->xd->sc_dk,
   2107 			    (bp->b_bcount - bp->b_resid),
   2108 			    (bp->b_flags & B_READ));
   2109 			XDC_FREE(xdcsc, rqno);
   2110 			biodone(bp);
   2111 			break;
   2112 		case XD_SUB_WAIT:
   2113 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   2114 			xdcsc->ndone++;
   2115 			wakeup(iorq);
   2116 			break;
   2117 		case XD_SUB_POLL:
   2118 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   2119 			xdcsc->ndone++;
   2120 			break;
   2121 		}
   2122 	}
   2123 
   2124 	return (XD_ERR_AOK);
   2125 }
   2126 
   2127 /*
   2128  * xdc_perror: print error.
   2129  * - if still_trying is true: we got an error, retried and got a
   2130  *   different error.  in that case lasterror is the old error,
   2131  *   and errnum is the new one.
   2132  * - if still_trying is not true, then if we ever had an error it
   2133  *   is in lasterror. also, if iorq->errnum == 0, then we recovered
   2134  *   from that error (otherwise iorq->errnum == iorq->lasterror).
   2135  */
   2136 void
   2137 xdc_perror(struct xd_iorq *iorq, struct xd_iopb *iopb, int still_trying)
   2138 
   2139 {
   2140 
   2141 	int     error = iorq->lasterror;
   2142 
   2143 	printf("%s", (iorq->xd) ? device_xname(iorq->xd->sc_dev)
   2144 	    : device_xname(iorq->xdc->sc_dev));
   2145 	if (iorq->buf)
   2146 		printf("%c: ", 'a' + (char)DISKPART(iorq->buf->b_dev));
   2147 	if (iopb->comm == XDCMD_RD || iopb->comm == XDCMD_WR)
   2148 		printf("%s %d/%d/%d: ",
   2149 			(iopb->comm == XDCMD_RD) ? "read" : "write",
   2150 			iopb->cylno, iopb->headno, iopb->sectno);
   2151 	printf("%s", xdc_e2str(error));
   2152 
   2153 	if (still_trying)
   2154 		printf(" [still trying, new error=%s]", xdc_e2str(iorq->errnum));
   2155 	else
   2156 		if (iorq->errnum == 0)
   2157 			printf(" [recovered in %d tries]", iorq->tries);
   2158 
   2159 	printf("\n");
   2160 }
   2161 
   2162 /*
   2163  * xdc_error: non-fatal error encountered... recover.
   2164  * return AOK if resubmitted, return FAIL if this iopb is done
   2165  */
   2166 int
   2167 xdc_error(struct xdc_softc *xdcsc, struct xd_iorq *iorq, struct xd_iopb *iopb,
   2168 	int rqno, int comm)
   2169 
   2170 {
   2171 	int     errnum = iorq->errnum;
   2172 	int     erract = errnum & XD_ERA_MASK;
   2173 	int     oldmode, advance;
   2174 #ifdef __sparc__
   2175 	int i;
   2176 #endif
   2177 
   2178 	if (erract == XD_ERA_RSET) {	/* some errors require a reset */
   2179 		oldmode = iorq->mode;
   2180 		iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
   2181 		xdcsc->ndone++;
   2182 		/* make xdc_start ignore us */
   2183 		xdc_reset(xdcsc, 1, XD_RSET_NONE, errnum, iorq->xd);
   2184 		iorq->mode = oldmode;
   2185 		xdcsc->ndone--;
   2186 	}
   2187 	/* check for read/write to a sector in bad144 table if bad: redirect
   2188 	 * request to bad144 area */
   2189 
   2190 	if ((comm == XDCMD_RD || comm == XDCMD_WR) &&
   2191 	    (iorq->mode & XD_MODE_B144) == 0) {
   2192 		advance = iorq->sectcnt - iopb->sectcnt;
   2193 		XDC_ADVANCE(iorq, advance);
   2194 #ifdef __sparc__
   2195 		if ((i = isbad(&iorq->xd->dkb, iorq->blockno / iorq->xd->sectpercyl,
   2196 			    (iorq->blockno / iorq->xd->nsect) % iorq->xd->nhead,
   2197 			    iorq->blockno % iorq->xd->nsect)) != -1) {
   2198 			iorq->mode |= XD_MODE_B144;	/* enter bad144 mode &
   2199 							 * redirect */
   2200 			iopb->errnum = iopb->done = iopb->errs = 0;
   2201 			iopb->sectcnt = 1;
   2202 			iopb->cylno = (iorq->xd->ncyl + iorq->xd->acyl) - 2;
   2203 			/* second to last acyl */
   2204 			i = iorq->xd->sectpercyl - 1 - i;	/* follow bad144
   2205 								 * standard */
   2206 			iopb->headno = i / iorq->xd->nhead;
   2207 			iopb->sectno = i % iorq->xd->nhead;
   2208 			XDC_HWAIT(xdcsc, rqno);
   2209 			xdc_start(xdcsc, 1);	/* resubmit */
   2210 			return (XD_ERR_AOK);	/* recovered! */
   2211 		}
   2212 #endif
   2213 	}
   2214 
   2215 	/*
   2216 	 * it isn't a bad144 sector, must be real error! see if we can retry
   2217 	 * it?
   2218 	 */
   2219 	if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
   2220 		xdc_perror(iorq, iopb, 1);	/* inform of error state
   2221 						 * change */
   2222 	iorq->lasterror = errnum;
   2223 
   2224 	if ((erract == XD_ERA_RSET || erract == XD_ERA_HARD)
   2225 	    && iorq->tries < XDC_MAXTRIES) {	/* retry? */
   2226 		iorq->tries++;
   2227 		iorq->errnum = iopb->errnum = iopb->done = iopb->errs = 0;
   2228 		XDC_HWAIT(xdcsc, rqno);
   2229 		xdc_start(xdcsc, 1);	/* restart */
   2230 		return (XD_ERR_AOK);	/* recovered! */
   2231 	}
   2232 
   2233 	/* failed to recover from this error */
   2234 	return (XD_ERR_FAIL);
   2235 }
   2236 
   2237 /*
   2238  * xdc_tick: make sure xd is still alive and ticking (err, kicking).
   2239  */
   2240 void
   2241 xdc_tick(void *arg)
   2242 
   2243 {
   2244 	struct xdc_softc *xdcsc = arg;
   2245 	int     lcv, s, reset = 0;
   2246 #ifdef XDC_DIAG
   2247 	int     nwait, nrun, nfree, ndone, whd = 0;
   2248 	u_char  fqc[XDC_MAXIOPB], wqc[XDC_MAXIOPB], mark[XDC_MAXIOPB];
   2249 	s = splbio();
   2250 	nwait = xdcsc->nwait;
   2251 	nrun = xdcsc->nrun;
   2252 	nfree = xdcsc->nfree;
   2253 	ndone = xdcsc->ndone;
   2254 	memcpy(wqc, xdcsc->waitq, sizeof(wqc));
   2255 	memcpy(fqc, xdcsc->freereq, sizeof(fqc));
   2256 	splx(s);
   2257 	if (nwait + nrun + nfree + ndone != XDC_MAXIOPB) {
   2258 		printf("%s: diag: IOPB miscount (got w/f/r/d %d/%d/%d/%d, wanted %d)\n",
   2259 		    device_xname(xdcsc->sc_dev), nwait, nfree, nrun, ndone,
   2260 		    XDC_MAXIOPB);
   2261 		memset(mark, 0, sizeof(mark));
   2262 		printf("FREE: ");
   2263 		for (lcv = nfree; lcv > 0; lcv--) {
   2264 			printf("%d ", fqc[lcv - 1]);
   2265 			mark[fqc[lcv - 1]] = 1;
   2266 		}
   2267 		printf("\nWAIT: ");
   2268 		lcv = nwait;
   2269 		while (lcv > 0) {
   2270 			printf("%d ", wqc[whd]);
   2271 			mark[wqc[whd]] = 1;
   2272 			whd = (whd + 1) % XDC_MAXIOPB;
   2273 			lcv--;
   2274 		}
   2275 		printf("\n");
   2276 		for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2277 			if (mark[lcv] == 0)
   2278 				printf("MARK: running %d: mode %d done %d errs %d errnum 0x%x ttl %d buf %p\n",
   2279 				lcv, xdcsc->reqs[lcv].mode,
   2280 				xdcsc->iopbase[lcv].done,
   2281 				xdcsc->iopbase[lcv].errs,
   2282 				xdcsc->iopbase[lcv].errnum,
   2283 				xdcsc->reqs[lcv].ttl, xdcsc->reqs[lcv].buf);
   2284 		}
   2285 	} else
   2286 		if (ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
   2287 			printf("%s: diag: lots of done jobs (%d)\n",
   2288 				device_xname(xdcsc->sc_dev), ndone);
   2289 
   2290 #endif
   2291 #ifdef XDC_DEBUG
   2292 	printf("%s: tick: csr 0x%x, w/f/r/d %d/%d/%d/%d\n",
   2293 		device_xname(xdcsc->sc_dev),
   2294 		xdcsc->xdc->xdc_csr, xdcsc->nwait, xdcsc->nfree, xdcsc->nrun,
   2295 		xdcsc->ndone);
   2296 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2297 		if (xdcsc->reqs[lcv].mode)
   2298 		  printf("running %d: mode %d done %d errs %d errnum 0x%x\n",
   2299 			 lcv,
   2300 			 xdcsc->reqs[lcv].mode, xdcsc->iopbase[lcv].done,
   2301 			 xdcsc->iopbase[lcv].errs, xdcsc->iopbase[lcv].errnum);
   2302 	}
   2303 #endif
   2304 
   2305 	/* reduce ttl for each request if one goes to zero, reset xdc */
   2306 	s = splbio();
   2307 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2308 		if (xdcsc->reqs[lcv].mode == 0 ||
   2309 		    XD_STATE(xdcsc->reqs[lcv].mode) == XD_SUB_DONE)
   2310 			continue;
   2311 		xdcsc->reqs[lcv].ttl--;
   2312 		if (xdcsc->reqs[lcv].ttl == 0)
   2313 			reset = 1;
   2314 	}
   2315 	if (reset) {
   2316 		printf("%s: watchdog timeout\n", device_xname(xdcsc->sc_dev));
   2317 		xdc_reset(xdcsc, 0, XD_RSET_NONE, XD_ERR_FAIL, NULL);
   2318 	}
   2319 	splx(s);
   2320 
   2321 	/* until next time */
   2322 
   2323 	callout_reset(&xdcsc->sc_tick_ch, XDC_TICKCNT, xdc_tick, xdcsc);
   2324 }
   2325 
   2326 /*
   2327  * xdc_ioctlcmd: this function provides a user level interface to the
   2328  * controller via ioctl.   this allows "format" programs to be written
   2329  * in user code, and is also useful for some debugging.   we return
   2330  * an error code.   called at user priority.
   2331  */
   2332 int
   2333 xdc_ioctlcmd(struct xd_softc *xd, dev_t dev, struct xd_iocmd *xio)
   2334 
   2335 {
   2336 	int     s, rqno, dummy;
   2337 	char *dvmabuf = NULL, *buf = NULL;
   2338 	struct xdc_softc *xdcsc;
   2339 	int			rseg, error;
   2340 	bus_dma_segment_t	seg;
   2341 
   2342 	/* check sanity of requested command */
   2343 
   2344 	switch (xio->cmd) {
   2345 
   2346 	case XDCMD_NOP:	/* no op: everything should be zero */
   2347 		if (xio->subfn || xio->dptr || xio->dlen ||
   2348 		    xio->block || xio->sectcnt)
   2349 			return (EINVAL);
   2350 		break;
   2351 
   2352 	case XDCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
   2353 	case XDCMD_WR:
   2354 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
   2355 		    xio->sectcnt * XDFM_BPS != xio->dlen || xio->dptr == NULL)
   2356 			return (EINVAL);
   2357 		break;
   2358 
   2359 	case XDCMD_SK:		/* seek: doesn't seem useful to export this */
   2360 		return (EINVAL);
   2361 
   2362 	case XDCMD_WRP:	/* write parameters */
   2363 		return (EINVAL);/* not useful, except maybe drive
   2364 				 * parameters... but drive parameters should
   2365 				 * go via disklabel changes */
   2366 
   2367 	case XDCMD_RDP:	/* read parameters */
   2368 		if (xio->subfn != XDFUN_DRV ||
   2369 		    xio->dlen || xio->block || xio->dptr)
   2370 			return (EINVAL);	/* allow read drive params to
   2371 						 * get hw_spt */
   2372 		xio->sectcnt = xd->hw_spt;	/* we already know the answer */
   2373 		return (0);
   2374 		break;
   2375 
   2376 	case XDCMD_XRD:	/* extended read/write */
   2377 	case XDCMD_XWR:
   2378 
   2379 		switch (xio->subfn) {
   2380 
   2381 		case XDFUN_THD:/* track headers */
   2382 			if (xio->sectcnt != xd->hw_spt ||
   2383 			    (xio->block % xd->nsect) != 0 ||
   2384 			    xio->dlen != XD_IOCMD_HSZ * xd->hw_spt ||
   2385 			    xio->dptr == NULL)
   2386 				return (EINVAL);
   2387 			xio->sectcnt = 0;
   2388 			break;
   2389 
   2390 		case XDFUN_FMT:/* NOTE: also XDFUN_VFY */
   2391 			if (xio->cmd == XDCMD_XRD)
   2392 				return (EINVAL);	/* no XDFUN_VFY */
   2393 			if (xio->sectcnt || xio->dlen ||
   2394 			    (xio->block % xd->nsect) != 0 || xio->dptr)
   2395 				return (EINVAL);
   2396 			break;
   2397 
   2398 		case XDFUN_HDR:/* header, header verify, data, data ECC */
   2399 			return (EINVAL);	/* not yet */
   2400 
   2401 		case XDFUN_DM:	/* defect map */
   2402 		case XDFUN_DMX:/* defect map (alternate location) */
   2403 			if (xio->sectcnt || xio->dlen != XD_IOCMD_DMSZ ||
   2404 			    (xio->block % xd->nsect) != 0 || xio->dptr == NULL)
   2405 				return (EINVAL);
   2406 			break;
   2407 
   2408 		default:
   2409 			return (EINVAL);
   2410 		}
   2411 		break;
   2412 
   2413 	case XDCMD_TST:	/* diagnostics */
   2414 		return (EINVAL);
   2415 
   2416 	default:
   2417 		return (EINVAL);/* ??? */
   2418 	}
   2419 
   2420 	xdcsc = xd->parent;
   2421 
   2422 	/* create DVMA buffer for request if needed */
   2423 	if (xio->dlen) {
   2424 		bus_addr_t busbuf;
   2425 
   2426 		if ((error = xd_dmamem_alloc(xdcsc->dmatag, xdcsc->auxmap,
   2427 					     &seg, &rseg,
   2428 					     xio->dlen, (void **)&buf,
   2429 					     &busbuf)) != 0) {
   2430 			return (error);
   2431 		}
   2432 		dvmabuf = (void *)(u_long)BUS_ADDR_PADDR(busbuf);
   2433 
   2434 		if (xio->cmd == XDCMD_WR || xio->cmd == XDCMD_XWR) {
   2435 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
   2436 				bus_dmamem_unmap(xdcsc->dmatag, buf, xio->dlen);
   2437 				bus_dmamem_free(xdcsc->dmatag, &seg, rseg);
   2438 				return (error);
   2439 			}
   2440 		}
   2441 	}
   2442 
   2443 	/* do it! */
   2444 
   2445 	error = 0;
   2446 	s = splbio();
   2447 	rqno = xdc_cmd(xdcsc, xio->cmd, xio->subfn, xd->xd_drive, xio->block,
   2448 	    xio->sectcnt, dvmabuf, XD_SUB_WAIT);
   2449 	if (rqno == XD_ERR_FAIL) {
   2450 		error = EIO;
   2451 		goto done;
   2452 	}
   2453 	xio->errnum = xdcsc->reqs[rqno].errnum;
   2454 	xio->tries = xdcsc->reqs[rqno].tries;
   2455 	XDC_DONE(xdcsc, rqno, dummy);
   2456 	__USE(dummy);
   2457 
   2458 	if (xio->cmd == XDCMD_RD || xio->cmd == XDCMD_XRD)
   2459 		error = copyout(buf, xio->dptr, xio->dlen);
   2460 
   2461 done:
   2462 	splx(s);
   2463 	if (dvmabuf) {
   2464 		xd_dmamem_free(xdcsc->dmatag, xdcsc->auxmap, &seg, rseg,
   2465 				xio->dlen, buf);
   2466 	}
   2467 	return (error);
   2468 }
   2469 
   2470 /*
   2471  * xdc_e2str: convert error code number into an error string
   2472  */
   2473 const char *
   2474 xdc_e2str(int no)
   2475 {
   2476 	switch (no) {
   2477 	case XD_ERR_FAIL:
   2478 		return ("Software fatal error");
   2479 	case XD_ERR_AOK:
   2480 		return ("Successful completion");
   2481 	case XD_ERR_ICYL:
   2482 		return ("Illegal cylinder address");
   2483 	case XD_ERR_IHD:
   2484 		return ("Illegal head address");
   2485 	case XD_ERR_ISEC:
   2486 		return ("Illgal sector address");
   2487 	case XD_ERR_CZER:
   2488 		return ("Count zero");
   2489 	case XD_ERR_UIMP:
   2490 		return ("Unimplemented command");
   2491 	case XD_ERR_IF1:
   2492 		return ("Illegal field length 1");
   2493 	case XD_ERR_IF2:
   2494 		return ("Illegal field length 2");
   2495 	case XD_ERR_IF3:
   2496 		return ("Illegal field length 3");
   2497 	case XD_ERR_IF4:
   2498 		return ("Illegal field length 4");
   2499 	case XD_ERR_IF5:
   2500 		return ("Illegal field length 5");
   2501 	case XD_ERR_IF6:
   2502 		return ("Illegal field length 6");
   2503 	case XD_ERR_IF7:
   2504 		return ("Illegal field length 7");
   2505 	case XD_ERR_ISG:
   2506 		return ("Illegal scatter/gather length");
   2507 	case XD_ERR_ISPT:
   2508 		return ("Not enough sectors per track");
   2509 	case XD_ERR_ALGN:
   2510 		return ("Next IOPB address alignment error");
   2511 	case XD_ERR_SGAL:
   2512 		return ("Scatter/gather address alignment error");
   2513 	case XD_ERR_SGEC:
   2514 		return ("Scatter/gather with auto-ECC");
   2515 	case XD_ERR_SECC:
   2516 		return ("Soft ECC corrected");
   2517 	case XD_ERR_SIGN:
   2518 		return ("ECC ignored");
   2519 	case XD_ERR_ASEK:
   2520 		return ("Auto-seek retry recovered");
   2521 	case XD_ERR_RTRY:
   2522 		return ("Soft retry recovered");
   2523 	case XD_ERR_HECC:
   2524 		return ("Hard data ECC");
   2525 	case XD_ERR_NHDR:
   2526 		return ("Header not found");
   2527 	case XD_ERR_NRDY:
   2528 		return ("Drive not ready");
   2529 	case XD_ERR_TOUT:
   2530 		return ("Operation timeout");
   2531 	case XD_ERR_VTIM:
   2532 		return ("VMEDMA timeout");
   2533 	case XD_ERR_DSEQ:
   2534 		return ("Disk sequencer error");
   2535 	case XD_ERR_HDEC:
   2536 		return ("Header ECC error");
   2537 	case XD_ERR_RVFY:
   2538 		return ("Read verify");
   2539 	case XD_ERR_VFER:
   2540 		return ("Fatail VMEDMA error");
   2541 	case XD_ERR_VBUS:
   2542 		return ("VMEbus error");
   2543 	case XD_ERR_DFLT:
   2544 		return ("Drive faulted");
   2545 	case XD_ERR_HECY:
   2546 		return ("Header error/cyliner");
   2547 	case XD_ERR_HEHD:
   2548 		return ("Header error/head");
   2549 	case XD_ERR_NOCY:
   2550 		return ("Drive not on-cylinder");
   2551 	case XD_ERR_SEEK:
   2552 		return ("Seek error");
   2553 	case XD_ERR_ILSS:
   2554 		return ("Illegal sector size");
   2555 	case XD_ERR_SEC:
   2556 		return ("Soft ECC");
   2557 	case XD_ERR_WPER:
   2558 		return ("Write-protect error");
   2559 	case XD_ERR_IRAM:
   2560 		return ("IRAM self test failure");
   2561 	case XD_ERR_MT3:
   2562 		return ("Maintenance test 3 failure (DSKCEL RAM)");
   2563 	case XD_ERR_MT4:
   2564 		return ("Maintenance test 4 failure (header shift reg)");
   2565 	case XD_ERR_MT5:
   2566 		return ("Maintenance test 5 failure (VMEDMA regs)");
   2567 	case XD_ERR_MT6:
   2568 		return ("Maintenance test 6 failure (REGCEL chip)");
   2569 	case XD_ERR_MT7:
   2570 		return ("Maintenance test 7 failure (buffer parity)");
   2571 	case XD_ERR_MT8:
   2572 		return ("Maintenance test 8 failure (disk FIFO)");
   2573 	case XD_ERR_IOCK:
   2574 		return ("IOPB checksum miscompare");
   2575 	case XD_ERR_IODM:
   2576 		return ("IOPB DMA fatal");
   2577 	case XD_ERR_IOAL:
   2578 		return ("IOPB address alignment error");
   2579 	case XD_ERR_FIRM:
   2580 		return ("Firmware error");
   2581 	case XD_ERR_MMOD:
   2582 		return ("Illegal maintenance mode test number");
   2583 	case XD_ERR_ACFL:
   2584 		return ("ACFAIL asserted");
   2585 	default:
   2586 		return ("Unknown error");
   2587 	}
   2588 }
   2589