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