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