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