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