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