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