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