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