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