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