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