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