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