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