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