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