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