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