Home | History | Annotate | Line # | Download | only in vme
xd.c revision 1.8
      1 /*	$NetBSD: xd.c,v 1.8 1998/02/04 01:19:23 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 	vme_addr_t		vaddr;
    388 
    389 	mod = VMEMOD_A16 | VMEMOD_S | VMEMOD_D | VMEMOD_D32;
    390 	vaddr = va->vma_reg[0] + offsetof(struct xdc, xdc_csr);
    391 	return (vme_bus_probe(ct, bt, va->vma_reg[0], 1, 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 	printf(" pri %d", va->vma_pri);
    432 
    433 	for (lcv = 0; lcv < XDC_MAXDEV; lcv++)
    434 		xdc->sc_drives[lcv] = (struct xd_softc *) 0;
    435 
    436 	/* allocate and zero buffers
    437 	 *
    438 	 * note: we simplify the code by allocating the max number of iopbs and
    439 	 * iorq's up front.   thus, we avoid linked lists and the costs
    440 	 * associated with them in exchange for wasting a little memory. */
    441 
    442 	error = bus_dmamem_alloc(xdc->dmatag,
    443 				 XDC_MAXIOPB * sizeof(struct xd_iopb),
    444 				 NBPG, 0,
    445 				 &seg, 1, &rseg, BUS_DMA_NOWAIT);
    446 	if (error) {
    447 		printf("%s: DMA buffer alloc error %d\n",
    448 			xdc->sc_dev.dv_xname, error);
    449 		return;
    450 	}
    451 	xdc->dvmaiopb = (struct xd_iopb *)seg.ds_addr;
    452 
    453 	error = bus_dmamem_map(xdc->dmatag, &seg, rseg,
    454 				XDC_MAXIOPB * sizeof(struct xd_iopb),
    455 				(caddr_t *)&xdc->iopbase,
    456 				BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    457 	if (error) {
    458 		bus_dmamem_free(xdc->dmatag, &seg, rseg);
    459 		printf("%s: DMA buffer map error %d\n",
    460 			xdc->sc_dev.dv_xname, error);
    461 		return;
    462 	}
    463 	bzero(xdc->iopbase, XDC_MAXIOPB * sizeof(struct xd_iopb));
    464 
    465 	xdc->reqs = (struct xd_iorq *)
    466 	    malloc(XDC_MAXIOPB * sizeof(struct xd_iorq), M_DEVBUF, M_NOWAIT);
    467 	if (xdc->reqs == NULL)
    468 		panic("xdc malloc");
    469 	bzero(xdc->reqs, XDC_MAXIOPB * sizeof(struct xd_iorq));
    470 
    471 	/* init free list, iorq to iopb pointers, and non-zero fields in the
    472 	 * iopb which never change. */
    473 
    474 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
    475 		xdc->reqs[lcv].iopb = &xdc->iopbase[lcv];
    476 		xdc->freereq[lcv] = lcv;
    477 		xdc->iopbase[lcv].fixd = 1;	/* always the same */
    478 		xdc->iopbase[lcv].naddrmod = XDC_ADDRMOD; /* always the same */
    479 		xdc->iopbase[lcv].intr_vec = xdc->vector; /* always the same */
    480 
    481 		error = bus_dmamap_create(
    482 				xdc->dmatag,
    483 				MAXPHYS,	/* size */
    484 				1,		/* nsegments */
    485 				MAXPHYS,	/* maxsegsz */
    486 				0,		/* boundary */
    487 				BUS_DMA_NOWAIT,
    488 				&xdc->reqs[lcv].dmamap);
    489 		if (error) {
    490 			printf("%s: DMA buffer map create error %d\n",
    491 				xdc->sc_dev.dv_xname, error);
    492 			return;
    493 		}
    494 	}
    495 	xdc->nfree = XDC_MAXIOPB;
    496 	xdc->nrun = 0;
    497 	xdc->waithead = xdc->waitend = xdc->nwait = 0;
    498 	xdc->ndone = 0;
    499 
    500 	/* init queue of waiting bufs */
    501 
    502 	xdc->sc_wq.b_active = 0;
    503 	xdc->sc_wq.b_actf = 0;
    504 	xdc->sc_wq.b_actb = &xdc->sc_wq.b_actf;
    505 
    506 	/*
    507 	 * section 7 of the manual tells us how to init the controller:
    508 	 * - read controller parameters (6/0)
    509 	 * - write controller parameters (5/0)
    510 	 */
    511 
    512 	/* read controller parameters and insure we have a 753/7053 */
    513 
    514 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
    515 	if (rqno == XD_ERR_FAIL) {
    516 		printf(": couldn't read controller params\n");
    517 		return;		/* shouldn't ever happen */
    518 	}
    519 	ctl = (struct xd_iopb_ctrl *) & xdc->iopbase[rqno];
    520 	if (ctl->ctype != XDCT_753) {
    521 		if (xdc->reqs[rqno].errno)
    522 			printf(": %s: ", xdc_e2str(xdc->reqs[rqno].errno));
    523 		printf(": doesn't identify as a 753/7053\n");
    524 		XDC_DONE(xdc, rqno, error);
    525 		return;
    526 	}
    527 	printf(": Xylogics 753/7053, PROM=0x%x.%02x.%02x\n",
    528 	    ctl->eprom_partno, ctl->eprom_lvl, ctl->eprom_rev);
    529 	XDC_DONE(xdc, rqno, error);
    530 
    531 	/* now write controller parameters (xdc_cmd sets all params for us) */
    532 
    533 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
    534 	XDC_DONE(xdc, rqno, error);
    535 	if (error) {
    536 		printf("%s: controller config error: %s\n",
    537 			xdc->sc_dev.dv_xname, xdc_e2str(error));
    538 		return;
    539 	}
    540 
    541 	/* link in interrupt with higher level software */
    542 	vme_intr_map(ct, va->vma_vec, va->vma_pri, &ih);
    543 	vme_intr_establish(ct, ih, xdcintr, xdc);
    544 	evcnt_attach(&xdc->sc_dev, "intr", &xdc->sc_intrcnt);
    545 	vme_bus_establish(ct, &xdc->sc_dev);
    546 
    547 
    548 	/* now we must look for disks using autoconfig */
    549 	xa.fullmode = XD_SUB_POLL;
    550 	xa.booting = 1;
    551 
    552 	for (xa.driveno = 0; xa.driveno < XDC_MAXDEV; xa.driveno++)
    553 		(void) config_found(self, (void *) &xa, NULL);
    554 
    555 	/* start the watchdog clock */
    556 	timeout(xdc_tick, xdc, XDC_TICKCNT);
    557 
    558 }
    559 
    560 /*
    561  * xdmatch: probe for disk.
    562  *
    563  * note: we almost always say disk is present.   this allows us to
    564  * spin up and configure a disk after the system is booted (we can
    565  * call xdattach!).
    566  */
    567 int
    568 xdmatch(parent, cf, aux)
    569 	struct device *parent;
    570 	struct cfdata *cf;
    571 	void *aux;
    572 {
    573 	struct xdc_attach_args *xa = aux;
    574 
    575 	/* looking for autoconf wildcard or exact match */
    576 
    577 	if (cf->cf_loc[XDCCF_DRIVE] != XDCCF_DRIVE_DEFAULT &&
    578 	    cf->cf_loc[XDCCF_DRIVE] != xa->driveno)
    579 		return 0;
    580 
    581 	return 1;
    582 
    583 }
    584 
    585 /*
    586  * xdattach: attach a disk.   this can be called from autoconf and also
    587  * from xdopen/xdstrategy.
    588  */
    589 void
    590 xdattach(parent, self, aux)
    591 	struct device *parent, *self;
    592 	void   *aux;
    593 
    594 {
    595 	struct xd_softc *xd = (void *) self;
    596 	struct xdc_softc *xdc = (void *) parent;
    597 	struct xdc_attach_args *xa = aux;
    598 	int     rqno, spt = 0, mb, blk, lcv, fmode, s = 0, newstate;
    599 	struct xd_iopb_drive *driopb;
    600 	struct dkbad *dkb;
    601 	int			rseg, error;
    602 	bus_dma_segment_t	seg;
    603 	caddr_t			dmaddr;
    604 	caddr_t			buf;
    605 
    606 	/*
    607 	 * Always re-initialize the disk structure.  We want statistics
    608 	 * to start with a clean slate.
    609 	 */
    610 	bzero(&xd->sc_dk, sizeof(xd->sc_dk));
    611 	xd->sc_dk.dk_driver = &xddkdriver;
    612 	xd->sc_dk.dk_name = xd->sc_dev.dv_xname;
    613 
    614 	/* if booting, init the xd_softc */
    615 
    616 	if (xa->booting) {
    617 		xd->state = XD_DRIVE_UNKNOWN;	/* to start */
    618 		xd->flags = 0;
    619 		xd->parent = xdc;
    620 	}
    621 	xd->xd_drive = xa->driveno;
    622 	fmode = xa->fullmode;
    623 	xdc->sc_drives[xa->driveno] = xd;
    624 
    625 	/* if not booting, make sure we are the only process in the attach for
    626 	 * this drive.   if locked out, sleep on it. */
    627 
    628 	if (!xa->booting) {
    629 		s = splbio();
    630 		while (xd->state == XD_DRIVE_ATTACHING) {
    631 			if (tsleep(&xd->state, PRIBIO, "xdattach", 0)) {
    632 				splx(s);
    633 				return;
    634 			}
    635 		}
    636 		printf("%s at %s",
    637 			xd->sc_dev.dv_xname, xd->parent->sc_dev.dv_xname);
    638 	}
    639 
    640 	/* we now have control */
    641 	xd->state = XD_DRIVE_ATTACHING;
    642 	newstate = XD_DRIVE_UNKNOWN;
    643 
    644 	buf = NULL;
    645 	error = bus_dmamem_alloc(xdc->dmatag, XDFM_BPS, NBPG, 0,
    646 				 &seg, 1, &rseg, BUS_DMA_NOWAIT);
    647 	if (error) {
    648 		printf("%s: DMA buffer alloc error %d\n",
    649 			xd->sc_dev.dv_xname, error);
    650 		goto done;
    651 	}
    652 	dmaddr = (caddr_t)seg.ds_addr;
    653 
    654 	error = bus_dmamem_map(xdc->dmatag, &seg, rseg, XDFM_BPS,
    655 				&buf,
    656 				BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    657 	if (error) {
    658 		printf("%s: DMA buffer alloc error %d\n",
    659 			xd->sc_dev.dv_xname, error);
    660 		bus_dmamem_free(xdc->dmatag, &seg, rseg);
    661 		goto done;
    662 	}
    663 
    664 
    665 	/* first try and reset the drive */
    666 
    667 	rqno = xdc_cmd(xdc, XDCMD_RST, 0, xd->xd_drive, 0, 0, 0, fmode);
    668 	XDC_DONE(xdc, rqno, error);
    669 	if (error == XD_ERR_NRDY) {
    670 		printf(" drive %d: off-line\n", xa->driveno);
    671 		goto done;
    672 	}
    673 	if (error) {
    674 		printf(": ERROR 0x%02x (%s)\n", error, xdc_e2str(error));
    675 		goto done;
    676 	}
    677 	printf(" drive %d: ready\n", xa->driveno);
    678 
    679 	/* now set format parameters */
    680 
    681 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_FMT, xd->xd_drive, 0, 0, 0, fmode);
    682 	XDC_DONE(xdc, rqno, error);
    683 	if (error) {
    684 		printf("%s: write format parameters failed: %s\n",
    685 			xd->sc_dev.dv_xname, xdc_e2str(error));
    686 		goto done;
    687 	}
    688 
    689 	/* get drive parameters */
    690 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    691 	if (rqno != XD_ERR_FAIL) {
    692 		driopb = (struct xd_iopb_drive *) & xdc->iopbase[rqno];
    693 		spt = driopb->sectpertrk;
    694 	}
    695 	XDC_DONE(xdc, rqno, error);
    696 	if (error) {
    697 		printf("%s: read drive parameters failed: %s\n",
    698 			xd->sc_dev.dv_xname, xdc_e2str(error));
    699 		goto done;
    700 	}
    701 
    702 	/*
    703 	 * now set drive parameters (to semi-bogus values) so we can read the
    704 	 * disk label.
    705 	 */
    706 	xd->pcyl = xd->ncyl = 1;
    707 	xd->acyl = 0;
    708 	xd->nhead = 1;
    709 	xd->nsect = 1;
    710 	xd->sectpercyl = 1;
    711 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
    712 		xd->dkb.bt_bad[lcv].bt_cyl = xd->dkb.bt_bad[lcv].bt_trksec = 0xffff;
    713 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    714 	XDC_DONE(xdc, rqno, error);
    715 	if (error) {
    716 		printf("%s: write drive parameters failed: %s\n",
    717 			xd->sc_dev.dv_xname, xdc_e2str(error));
    718 		goto done;
    719 	}
    720 
    721 	/* read disk label */
    722 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, 0, 1, dmaddr, fmode);
    723 	XDC_DONE(xdc, rqno, error);
    724 	if (error) {
    725 		printf("%s: reading disk label failed: %s\n",
    726 			xd->sc_dev.dv_xname, xdc_e2str(error));
    727 		goto done;
    728 	}
    729 	newstate = XD_DRIVE_NOLABEL;
    730 
    731 	xd->hw_spt = spt;
    732 	/* Attach the disk: must be before getdisklabel to malloc label */
    733 	disk_attach(&xd->sc_dk);
    734 
    735 	if (xdgetdisklabel(xd, buf) != XD_ERR_AOK)
    736 		goto done;
    737 
    738 	/* inform the user of what is up */
    739 	printf("%s: <%s>, pcyl %d, hw_spt %d\n", xd->sc_dev.dv_xname,
    740 		buf, xd->pcyl, spt);
    741 	mb = xd->ncyl * (xd->nhead * xd->nsect) / (1048576 / XDFM_BPS);
    742 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
    743 		xd->sc_dev.dv_xname, mb, xd->ncyl, xd->nhead, xd->nsect,
    744 		XDFM_BPS);
    745 
    746 	/* now set the real drive parameters! */
    747 
    748 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
    749 	XDC_DONE(xdc, rqno, error);
    750 	if (error) {
    751 		printf("%s: write real drive parameters failed: %s\n",
    752 			xd->sc_dev.dv_xname, xdc_e2str(error));
    753 		goto done;
    754 	}
    755 	newstate = XD_DRIVE_ONLINE;
    756 
    757 	/*
    758 	 * read bad144 table. this table resides on the first sector of the
    759 	 * last track of the disk (i.e. second cyl of "acyl" area).
    760 	 */
    761 
    762 	blk = (xd->ncyl + xd->acyl - 1) * (xd->nhead * xd->nsect) + /* last cyl */
    763 	    (xd->nhead - 1) * xd->nsect;	/* last head */
    764 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, blk, 1, dmaddr, fmode);
    765 	XDC_DONE(xdc, rqno, error);
    766 	if (error) {
    767 		printf("%s: reading bad144 failed: %s\n",
    768 			xd->sc_dev.dv_xname, xdc_e2str(error));
    769 		goto done;
    770 	}
    771 
    772 	/* check dkbad for sanity */
    773 	dkb = (struct dkbad *) buf;
    774 	for (lcv = 0; lcv < 126; lcv++) {
    775 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
    776 				dkb->bt_bad[lcv].bt_cyl == 0) &&
    777 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
    778 			continue;	/* blank */
    779 		if (dkb->bt_bad[lcv].bt_cyl >= xd->ncyl)
    780 			break;
    781 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xd->nhead)
    782 			break;
    783 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xd->nsect)
    784 			break;
    785 	}
    786 	if (lcv != 126) {
    787 		printf("%s: warning: invalid bad144 sector!\n",
    788 			xd->sc_dev.dv_xname);
    789 	} else {
    790 		bcopy(buf, &xd->dkb, XDFM_BPS);
    791 	}
    792 
    793 	dk_establish(&xd->sc_dk, &xd->sc_dev);		/* XXX */
    794 
    795 done:
    796 	if (buf != NULL) {
    797 		bus_dmamem_unmap(xdc->dmatag, buf, XDFM_BPS);
    798 		bus_dmamem_free(xdc->dmatag, &seg, rseg);
    799 	}
    800 
    801 	xd->state = newstate;
    802 	if (!xa->booting) {
    803 		wakeup(&xd->state);
    804 		splx(s);
    805 	}
    806 }
    807 
    808 /*
    809  * end of autoconfig functions
    810  */
    811 
    812 /*
    813  * { b , c } d e v s w   f u n c t i o n s
    814  */
    815 
    816 /*
    817  * xdclose: close device
    818  */
    819 int
    820 xdclose(dev, flag, fmt, p)
    821 	dev_t   dev;
    822 	int     flag, fmt;
    823 	struct proc *p;
    824 {
    825 	struct xd_softc *xd = xd_cd.cd_devs[DISKUNIT(dev)];
    826 	int     part = DISKPART(dev);
    827 
    828 	/* clear mask bits */
    829 
    830 	switch (fmt) {
    831 	case S_IFCHR:
    832 		xd->sc_dk.dk_copenmask &= ~(1 << part);
    833 		break;
    834 	case S_IFBLK:
    835 		xd->sc_dk.dk_bopenmask &= ~(1 << part);
    836 		break;
    837 	}
    838 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
    839 
    840 	return 0;
    841 }
    842 
    843 /*
    844  * xddump: crash dump system
    845  */
    846 int
    847 xddump(dev, blkno, va, size)
    848 	dev_t dev;
    849 	daddr_t blkno;
    850 	caddr_t va;
    851 	size_t size;
    852 {
    853 	int     unit, part;
    854 	struct xd_softc *xd;
    855 
    856 	unit = DISKUNIT(dev);
    857 	if (unit >= xd_cd.cd_ndevs)
    858 		return ENXIO;
    859 	part = DISKPART(dev);
    860 
    861 	xd = xd_cd.cd_devs[unit];
    862 
    863 	printf("%s%c: crash dump not supported (yet)\n", xd->sc_dev.dv_xname,
    864 	    'a' + part);
    865 
    866 	return ENXIO;
    867 
    868 	/* outline: globals: "dumplo" == sector number of partition to start
    869 	 * dump at (convert to physical sector with partition table)
    870 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
    871 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
    872 	 * physmem)
    873 	 *
    874 	 * dump a copy of physical memory to the dump device starting at sector
    875 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
    876 	 * we go.   use polled I/O.
    877 	 *
    878 	 * XXX how to handle NON_CONTIG? */
    879 
    880 }
    881 
    882 /*
    883  * xdioctl: ioctls on XD drives.   based on ioctl's of other netbsd disks.
    884  */
    885 int
    886 xdioctl(dev, command, addr, flag, p)
    887 	dev_t   dev;
    888 	u_long  command;
    889 	caddr_t addr;
    890 	int     flag;
    891 	struct proc *p;
    892 
    893 {
    894 	struct xd_softc *xd;
    895 	struct xd_iocmd *xio;
    896 	int     error, s, unit;
    897 
    898 	unit = DISKUNIT(dev);
    899 
    900 	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
    901 		return (ENXIO);
    902 
    903 	/* switch on ioctl type */
    904 
    905 	switch (command) {
    906 	case DIOCSBAD:		/* set bad144 info */
    907 		if ((flag & FWRITE) == 0)
    908 			return EBADF;
    909 		s = splbio();
    910 		bcopy(addr, &xd->dkb, sizeof(xd->dkb));
    911 		splx(s);
    912 		return 0;
    913 
    914 	case DIOCGDINFO:	/* get disk label */
    915 		bcopy(xd->sc_dk.dk_label, addr, sizeof(struct disklabel));
    916 		return 0;
    917 
    918 	case DIOCGPART:	/* get partition info */
    919 		((struct partinfo *) addr)->disklab = xd->sc_dk.dk_label;
    920 		((struct partinfo *) addr)->part =
    921 		    &xd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    922 		return 0;
    923 
    924 	case DIOCSDINFO:	/* set disk label */
    925 		if ((flag & FWRITE) == 0)
    926 			return EBADF;
    927 		error = setdisklabel(xd->sc_dk.dk_label,
    928 		    (struct disklabel *) addr, /* xd->sc_dk.dk_openmask : */ 0,
    929 		    xd->sc_dk.dk_cpulabel);
    930 		if (error == 0) {
    931 			if (xd->state == XD_DRIVE_NOLABEL)
    932 				xd->state = XD_DRIVE_ONLINE;
    933 		}
    934 		return error;
    935 
    936 	case DIOCWLABEL:	/* change write status of disk label */
    937 		if ((flag & FWRITE) == 0)
    938 			return EBADF;
    939 		if (*(int *) addr)
    940 			xd->flags |= XD_WLABEL;
    941 		else
    942 			xd->flags &= ~XD_WLABEL;
    943 		return 0;
    944 
    945 	case DIOCWDINFO:	/* write disk label */
    946 		if ((flag & FWRITE) == 0)
    947 			return EBADF;
    948 		error = setdisklabel(xd->sc_dk.dk_label,
    949 		    (struct disklabel *) addr, /* xd->sc_dk.dk_openmask : */ 0,
    950 		    xd->sc_dk.dk_cpulabel);
    951 		if (error == 0) {
    952 			if (xd->state == XD_DRIVE_NOLABEL)
    953 				xd->state = XD_DRIVE_ONLINE;
    954 
    955 			/* Simulate opening partition 0 so write succeeds. */
    956 			xd->sc_dk.dk_openmask |= (1 << 0);
    957 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    958 			    xdstrategy, xd->sc_dk.dk_label,
    959 			    xd->sc_dk.dk_cpulabel);
    960 			xd->sc_dk.dk_openmask =
    961 			    xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
    962 		}
    963 		return error;
    964 
    965 	case DIOSXDCMD:
    966 		xio = (struct xd_iocmd *) addr;
    967 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    968 			return (error);
    969 		return (xdc_ioctlcmd(xd, dev, xio));
    970 
    971 	default:
    972 		return ENOTTY;
    973 	}
    974 }
    975 /*
    976  * xdopen: open drive
    977  */
    978 
    979 int
    980 xdopen(dev, flag, fmt, p)
    981 	dev_t   dev;
    982 	int     flag, fmt;
    983 	struct proc *p;
    984 {
    985 	int     unit, part;
    986 	struct xd_softc *xd;
    987 	struct xdc_attach_args xa;
    988 
    989 	/* first, could it be a valid target? */
    990 
    991 	unit = DISKUNIT(dev);
    992 	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
    993 		return (ENXIO);
    994 	part = DISKPART(dev);
    995 
    996 	/* do we need to attach the drive? */
    997 
    998 	if (xd->state == XD_DRIVE_UNKNOWN) {
    999 		xa.driveno = xd->xd_drive;
   1000 		xa.fullmode = XD_SUB_WAIT;
   1001 		xa.booting = 0;
   1002 		xdattach((struct device *) xd->parent, (struct device *) xd, &xa);
   1003 		if (xd->state == XD_DRIVE_UNKNOWN) {
   1004 			return (EIO);
   1005 		}
   1006 	}
   1007 	/* check for partition */
   1008 
   1009 	if (part != RAW_PART &&
   1010 	    (part >= xd->sc_dk.dk_label->d_npartitions ||
   1011 		xd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
   1012 		return (ENXIO);
   1013 	}
   1014 	/* set open masks */
   1015 
   1016 	switch (fmt) {
   1017 	case S_IFCHR:
   1018 		xd->sc_dk.dk_copenmask |= (1 << part);
   1019 		break;
   1020 	case S_IFBLK:
   1021 		xd->sc_dk.dk_bopenmask |= (1 << part);
   1022 		break;
   1023 	}
   1024 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
   1025 
   1026 	return 0;
   1027 }
   1028 
   1029 int
   1030 xdread(dev, uio, flags)
   1031 	dev_t   dev;
   1032 	struct uio *uio;
   1033 	int flags;
   1034 {
   1035 
   1036 	return (physio(xdstrategy, NULL, dev, B_READ, minphys, uio));
   1037 }
   1038 
   1039 int
   1040 xdwrite(dev, uio, flags)
   1041 	dev_t   dev;
   1042 	struct uio *uio;
   1043 	int flags;
   1044 {
   1045 
   1046 	return (physio(xdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1047 }
   1048 
   1049 
   1050 /*
   1051  * xdsize: return size of a partition for a dump
   1052  */
   1053 
   1054 int
   1055 xdsize(dev)
   1056 	dev_t   dev;
   1057 
   1058 {
   1059 	struct xd_softc *xdsc;
   1060 	int     unit, part, size, omask;
   1061 
   1062 	/* valid unit? */
   1063 	unit = DISKUNIT(dev);
   1064 	if (unit >= xd_cd.cd_ndevs || (xdsc = xd_cd.cd_devs[unit]) == NULL)
   1065 		return (-1);
   1066 
   1067 	part = DISKPART(dev);
   1068 	omask = xdsc->sc_dk.dk_openmask & (1 << part);
   1069 
   1070 	if (omask == 0 && xdopen(dev, 0, S_IFBLK, NULL) != 0)
   1071 		return (-1);
   1072 
   1073 	/* do it */
   1074 	if (xdsc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   1075 		size = -1;	/* only give valid size for swap partitions */
   1076 	else
   1077 		size = xdsc->sc_dk.dk_label->d_partitions[part].p_size *
   1078 		    (xdsc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1079 	if (omask == 0 && xdclose(dev, 0, S_IFBLK, NULL) != 0)
   1080 		return (-1);
   1081 	return (size);
   1082 }
   1083 /*
   1084  * xdstrategy: buffering system interface to xd.
   1085  */
   1086 
   1087 void
   1088 xdstrategy(bp)
   1089 	struct buf *bp;
   1090 
   1091 {
   1092 	struct xd_softc *xd;
   1093 	struct xdc_softc *parent;
   1094 	struct buf *wq;
   1095 	int     s, unit;
   1096 	struct xdc_attach_args xa;
   1097 
   1098 	unit = DISKUNIT(bp->b_dev);
   1099 
   1100 	/* check for live device */
   1101 
   1102 	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == 0 ||
   1103 	    bp->b_blkno < 0 ||
   1104 	    (bp->b_bcount % xd->sc_dk.dk_label->d_secsize) != 0) {
   1105 		bp->b_error = EINVAL;
   1106 		goto bad;
   1107 	}
   1108 	/* do we need to attach the drive? */
   1109 
   1110 	if (xd->state == XD_DRIVE_UNKNOWN) {
   1111 		xa.driveno = xd->xd_drive;
   1112 		xa.fullmode = XD_SUB_WAIT;
   1113 		xa.booting = 0;
   1114 		xdattach((struct device *)xd->parent, (struct device *)xd, &xa);
   1115 		if (xd->state == XD_DRIVE_UNKNOWN) {
   1116 			bp->b_error = EIO;
   1117 			goto bad;
   1118 		}
   1119 	}
   1120 	if (xd->state != XD_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
   1121 		/* no I/O to unlabeled disks, unless raw partition */
   1122 		bp->b_error = EIO;
   1123 		goto bad;
   1124 	}
   1125 	/* short circuit zero length request */
   1126 
   1127 	if (bp->b_bcount == 0)
   1128 		goto done;
   1129 
   1130 	/* check bounds with label (disksubr.c).  Determine the size of the
   1131 	 * transfer, and make sure it is within the boundaries of the
   1132 	 * partition. Adjust transfer if needed, and signal errors or early
   1133 	 * completion. */
   1134 
   1135 	if (bounds_check_with_label(bp, xd->sc_dk.dk_label,
   1136 		(xd->flags & XD_WLABEL) != 0) <= 0)
   1137 		goto done;
   1138 
   1139 	/*
   1140 	 * now we know we have a valid buf structure that we need to do I/O
   1141 	 * on.
   1142 	 *
   1143 	 * note that we don't disksort because the controller has a sorting
   1144 	 * algorithm built into the hardware.
   1145 	 */
   1146 
   1147 	s = splbio();		/* protect the queues */
   1148 
   1149 	/* first, give jobs in front of us a chance */
   1150 	parent = xd->parent;
   1151 	while (parent->nfree > 0 && parent->sc_wq.b_actf)
   1152 		if (xdc_startbuf(parent, NULL, NULL) != XD_ERR_AOK)
   1153 			break;
   1154 
   1155 	/* if there are no free iorq's, then we just queue and return. the
   1156 	 * buffs will get picked up later by xdcintr().
   1157 	 */
   1158 
   1159 	if (parent->nfree == 0) {
   1160 		wq = &xd->parent->sc_wq;
   1161 		bp->b_actf = 0;
   1162 		bp->b_actb = wq->b_actb;
   1163 		*wq->b_actb = bp;
   1164 		wq->b_actb = &bp->b_actf;
   1165 		splx(s);
   1166 		return;
   1167 	}
   1168 
   1169 	/* now we have free iopb's and we are at splbio... start 'em up */
   1170 	if (xdc_startbuf(parent, xd, bp) != XD_ERR_AOK) {
   1171 		return;
   1172 	}
   1173 
   1174 	/* done! */
   1175 
   1176 	splx(s);
   1177 	return;
   1178 
   1179 bad:				/* tells upper layers we have an error */
   1180 	bp->b_flags |= B_ERROR;
   1181 done:				/* tells upper layers we are done with this
   1182 				 * buf */
   1183 	bp->b_resid = bp->b_bcount;
   1184 	biodone(bp);
   1185 }
   1186 /*
   1187  * end of {b,c}devsw functions
   1188  */
   1189 
   1190 /*
   1191  * i n t e r r u p t   f u n c t i o n
   1192  *
   1193  * xdcintr: hardware interrupt.
   1194  */
   1195 int
   1196 xdcintr(v)
   1197 	void   *v;
   1198 
   1199 {
   1200 	struct xdc_softc *xdcsc = v;
   1201 
   1202 	/* kick the event counter */
   1203 
   1204 	xdcsc->sc_intrcnt.ev_count++;
   1205 
   1206 	/* remove as many done IOPBs as possible */
   1207 
   1208 	xdc_remove_iorq(xdcsc);
   1209 
   1210 	/* start any iorq's already waiting */
   1211 
   1212 	xdc_start(xdcsc, XDC_MAXIOPB);
   1213 
   1214 	/* fill up any remaining iorq's with queue'd buffers */
   1215 
   1216 	while (xdcsc->nfree > 0 && xdcsc->sc_wq.b_actf)
   1217 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
   1218 			break;
   1219 
   1220 	return (1);
   1221 }
   1222 /*
   1223  * end of interrupt function
   1224  */
   1225 
   1226 /*
   1227  * i n t e r n a l   f u n c t i o n s
   1228  */
   1229 
   1230 /*
   1231  * xdc_rqinit: fill out the fields of an I/O request
   1232  */
   1233 
   1234 inline void
   1235 xdc_rqinit(rq, xdc, xd, md, blk, cnt, db, bp)
   1236 	struct xd_iorq *rq;
   1237 	struct xdc_softc *xdc;
   1238 	struct xd_softc *xd;
   1239 	int     md;
   1240 	u_long  blk;
   1241 	int     cnt;
   1242 	caddr_t db;
   1243 	struct buf *bp;
   1244 {
   1245 	rq->xdc = xdc;
   1246 	rq->xd = xd;
   1247 	rq->ttl = XDC_MAXTTL + 10;
   1248 	rq->mode = md;
   1249 	rq->tries = rq->errno = rq->lasterror = 0;
   1250 	rq->blockno = blk;
   1251 	rq->sectcnt = cnt;
   1252 	rq->dbuf = db;
   1253 	rq->buf = bp;
   1254 }
   1255 /*
   1256  * xdc_rqtopb: load up an IOPB based on an iorq
   1257  */
   1258 
   1259 void
   1260 xdc_rqtopb(iorq, iopb, cmd, subfun)
   1261 	struct xd_iorq *iorq;
   1262 	struct xd_iopb *iopb;
   1263 	int     cmd, subfun;
   1264 
   1265 {
   1266 	u_long  block, dp;
   1267 
   1268 	/* standard stuff */
   1269 
   1270 	iopb->errs = iopb->done = 0;
   1271 	iopb->comm = cmd;
   1272 	iopb->errno = iopb->status = 0;
   1273 	iopb->subfun = subfun;
   1274 	if (iorq->xd)
   1275 		iopb->unit = iorq->xd->xd_drive;
   1276 	else
   1277 		iopb->unit = 0;
   1278 
   1279 	/* check for alternate IOPB format */
   1280 
   1281 	if (cmd == XDCMD_WRP) {
   1282 		switch (subfun) {
   1283 		case XDFUN_CTL:{
   1284 			struct xd_iopb_ctrl *ctrl =
   1285 				(struct xd_iopb_ctrl *) iopb;
   1286 			iopb->lll = 0;
   1287 			iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1288 					? 0
   1289 					: iorq->xdc->ipl;
   1290 			ctrl->param_a = XDPA_TMOD | XDPA_DACF;
   1291 			ctrl->param_b = XDPB_ROR | XDPB_TDT_3_2USEC;
   1292 			ctrl->param_c = XDPC_OVS | XDPC_COP | XDPC_ASR |
   1293 					XDPC_RBC | XDPC_ECC2;
   1294 			ctrl->throttle = XDC_THROTTLE;
   1295 			ctrl->delay = XDC_DELAY;
   1296 			break;
   1297 			}
   1298 		case XDFUN_DRV:{
   1299 			struct xd_iopb_drive *drv =
   1300 				(struct xd_iopb_drive *)iopb;
   1301 			/* we assume that the disk label has the right
   1302 			 * info */
   1303 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1304 				drv->dparam_ipl = (XDC_DPARAM << 3);
   1305 			else
   1306 				drv->dparam_ipl = (XDC_DPARAM << 3) |
   1307 						  iorq->xdc->ipl;
   1308 			drv->maxsect = iorq->xd->nsect - 1;
   1309 			drv->maxsector = drv->maxsect;
   1310 			/* note: maxsector != maxsect only if you are
   1311 			 * doing cyl sparing */
   1312 			drv->headoff = 0;
   1313 			drv->maxcyl = iorq->xd->pcyl - 1;
   1314 			drv->maxhead = iorq->xd->nhead - 1;
   1315 			break;
   1316 			}
   1317 		case XDFUN_FMT:{
   1318 			struct xd_iopb_format *form =
   1319 					(struct xd_iopb_format *) iopb;
   1320 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1321 				form->interleave_ipl = (XDC_INTERLEAVE << 3);
   1322 			else
   1323 				form->interleave_ipl = (XDC_INTERLEAVE << 3) |
   1324 						       iorq->xdc->ipl;
   1325 			form->field1 = XDFM_FIELD1;
   1326 			form->field2 = XDFM_FIELD2;
   1327 			form->field3 = XDFM_FIELD3;
   1328 			form->field4 = XDFM_FIELD4;
   1329 			form->bytespersec = XDFM_BPS;
   1330 			form->field6 = XDFM_FIELD6;
   1331 			form->field7 = XDFM_FIELD7;
   1332 			break;
   1333 			}
   1334 		}
   1335 	} else {
   1336 
   1337 		/* normal IOPB case (harmless to RDP command) */
   1338 
   1339 		iopb->lll = 0;
   1340 		iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
   1341 				? 0
   1342 				: iorq->xdc->ipl;
   1343 		iopb->sectcnt = iorq->sectcnt;
   1344 		block = iorq->blockno;
   1345 		if (iorq->xd == NULL || block == 0) {
   1346 			iopb->sectno = iopb->headno = iopb->cylno = 0;
   1347 		} else {
   1348 			iopb->sectno = block % iorq->xd->nsect;
   1349 			block = block / iorq->xd->nsect;
   1350 			iopb->headno = block % iorq->xd->nhead;
   1351 			block = block / iorq->xd->nhead;
   1352 			iopb->cylno = block;
   1353 		}
   1354 		dp = (u_long) iorq->dbuf;
   1355 		dp = iopb->daddr = (iorq->dbuf == NULL) ? 0 : dp;
   1356 		iopb->addrmod = ((dp + (XDFM_BPS * iorq->sectcnt)) > 0x1000000)
   1357 					? XDC_ADDRMOD32
   1358 					: XDC_ADDRMOD;
   1359 	}
   1360 }
   1361 
   1362 /*
   1363  * xdc_cmd: front end for POLL'd and WAIT'd commands.  Returns rqno.
   1364  * If you've already got an IORQ, you can call submit directly (currently
   1365  * there is no need to do this).    NORM requests are handled seperately.
   1366  */
   1367 int
   1368 xdc_cmd(xdcsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
   1369 	struct xdc_softc *xdcsc;
   1370 	int     cmd, subfn, unit, block, scnt;
   1371 	char   *dptr;
   1372 	int     fullmode;
   1373 
   1374 {
   1375 	int     rqno, submode = XD_STATE(fullmode), retry;
   1376 	struct xd_iorq *iorq;
   1377 	struct xd_iopb *iopb;
   1378 
   1379 	/* get iorq/iopb */
   1380 	switch (submode) {
   1381 	case XD_SUB_POLL:
   1382 		while (xdcsc->nfree == 0) {
   1383 			if (xdc_piodriver(xdcsc, 0, 1) != XD_ERR_AOK)
   1384 				return (XD_ERR_FAIL);
   1385 		}
   1386 		break;
   1387 	case XD_SUB_WAIT:
   1388 		retry = 1;
   1389 		while (retry) {
   1390 			while (xdcsc->nfree == 0) {
   1391 			    if (tsleep(&xdcsc->nfree, PRIBIO, "xdnfree", 0))
   1392 				return (XD_ERR_FAIL);
   1393 			}
   1394 			while (xdcsc->ndone > XDC_SUBWAITLIM) {
   1395 			    if (tsleep(&xdcsc->ndone, PRIBIO, "xdsubwait", 0))
   1396 				return (XD_ERR_FAIL);
   1397 			}
   1398 			if (xdcsc->nfree)
   1399 				retry = 0;	/* got it */
   1400 		}
   1401 		break;
   1402 	default:
   1403 		return (XD_ERR_FAIL);	/* illegal */
   1404 	}
   1405 	if (xdcsc->nfree == 0)
   1406 		panic("xdcmd nfree");
   1407 	rqno = XDC_RQALLOC(xdcsc);
   1408 	iorq = &xdcsc->reqs[rqno];
   1409 	iopb = iorq->iopb;
   1410 
   1411 
   1412 	/* init iorq/iopb */
   1413 
   1414 	xdc_rqinit(iorq, xdcsc,
   1415 	    (unit == XDC_NOUNIT) ? NULL : xdcsc->sc_drives[unit],
   1416 	    fullmode, block, scnt, dptr, NULL);
   1417 
   1418 	/* load IOPB from iorq */
   1419 
   1420 	xdc_rqtopb(iorq, iopb, cmd, subfn);
   1421 
   1422 	/* submit it for processing */
   1423 
   1424 	xdc_submit_iorq(xdcsc, rqno, fullmode);	/* error code will be in iorq */
   1425 
   1426 	return (rqno);
   1427 }
   1428 /*
   1429  * xdc_startbuf
   1430  * start a buffer running, assumes nfree > 0
   1431  */
   1432 
   1433 int
   1434 xdc_startbuf(xdcsc, xdsc, bp)
   1435 	struct xdc_softc *xdcsc;
   1436 	struct xd_softc *xdsc;
   1437 	struct buf *bp;
   1438 
   1439 {
   1440 	int     rqno, partno;
   1441 	struct xd_iorq *iorq;
   1442 	struct xd_iopb *iopb;
   1443 	struct buf *wq;
   1444 	u_long  block;
   1445 /*	caddr_t dbuf;*/
   1446 	int error;
   1447 
   1448 	if (!xdcsc->nfree)
   1449 		panic("xdc_startbuf free");
   1450 	rqno = XDC_RQALLOC(xdcsc);
   1451 	iorq = &xdcsc->reqs[rqno];
   1452 	iopb = iorq->iopb;
   1453 
   1454 	/* get buf */
   1455 
   1456 	if (bp == NULL) {
   1457 		bp = xdcsc->sc_wq.b_actf;
   1458 		if (!bp)
   1459 			panic("xdc_startbuf bp");
   1460 		wq = bp->b_actf;
   1461 		if (wq)
   1462 			wq->b_actb = bp->b_actb;
   1463 		else
   1464 			xdcsc->sc_wq.b_actb = bp->b_actb;
   1465 		*bp->b_actb = wq;
   1466 		xdsc = xdcsc->sc_drives[DISKUNIT(bp->b_dev)];
   1467 	}
   1468 	partno = DISKPART(bp->b_dev);
   1469 #ifdef XDC_DEBUG
   1470 	printf("xdc_startbuf: %s%c: %s block %d\n", xdsc->sc_dev.dv_xname,
   1471 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
   1472 	printf("xdc_startbuf: b_bcount %d, b_data 0x%x\n",
   1473 	    bp->b_bcount, bp->b_data);
   1474 #endif
   1475 
   1476 	/*
   1477 	 * load request.  we have to calculate the correct block number based
   1478 	 * on partition info.
   1479 	 *
   1480 	 * note that iorq points to the buffer as mapped into DVMA space,
   1481 	 * where as the bp->b_data points to its non-DVMA mapping.
   1482 	 */
   1483 
   1484 	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
   1485 	    xdsc->sc_dk.dk_label->d_partitions[partno].p_offset);
   1486 
   1487 	error = bus_dmamap_load(xdcsc->dmatag, iorq->dmamap,
   1488 			 bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
   1489 	if (error != 0) {
   1490 		printf("%s: warning: cannot load DMA map\n",
   1491 			xdcsc->sc_dev.dv_xname);
   1492 		XDC_FREE(xdcsc, rqno);
   1493 		wq = &xdcsc->sc_wq;	/* put at end of queue */
   1494 		bp->b_actf = 0;
   1495 		bp->b_actb = wq->b_actb;
   1496 		*wq->b_actb = bp;
   1497 		wq->b_actb = &bp->b_actf;
   1498 		return (XD_ERR_FAIL);	/* XXX: need some sort of
   1499 					 * call-back scheme here? */
   1500 	}
   1501 	bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap,
   1502 			(bp->b_flags & B_READ)
   1503 				? BUS_DMASYNC_PREREAD
   1504 				: BUS_DMASYNC_PREWRITE);
   1505 
   1506 	/* init iorq and load iopb from it */
   1507 	xdc_rqinit(iorq, xdcsc, xdsc, XD_SUB_NORM | XD_MODE_VERBO, block,
   1508 		   bp->b_bcount / XDFM_BPS,
   1509 		   (caddr_t)iorq->dmamap->dm_segs[0].ds_addr,
   1510 		   bp);
   1511 
   1512 	xdc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XDCMD_RD : XDCMD_WR, 0);
   1513 
   1514 	/* Instrumentation. */
   1515 	disk_busy(&xdsc->sc_dk);
   1516 
   1517 	/* now submit [note that xdc_submit_iorq can never fail on NORM reqs] */
   1518 
   1519 	xdc_submit_iorq(xdcsc, rqno, XD_SUB_NORM);
   1520 	return (XD_ERR_AOK);
   1521 }
   1522 
   1523 
   1524 /*
   1525  * xdc_submit_iorq: submit an iorq for processing.  returns XD_ERR_AOK
   1526  * if ok.  if it fail returns an error code.  type is XD_SUB_*.
   1527  *
   1528  * note: caller frees iorq in all cases except NORM
   1529  *
   1530  * return value:
   1531  *   NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
   1532  *   WAIT: XD_AOK (success), <error-code> (failed)
   1533  *   POLL: <same as WAIT>
   1534  *   NOQ : <same as NORM>
   1535  *
   1536  * there are three sources for i/o requests:
   1537  * [1] xdstrategy: normal block I/O, using "struct buf" system.
   1538  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
   1539  * [3] open/ioctl: these are I/O requests done in the context of a process,
   1540  *                 and the process should block until they are done.
   1541  *
   1542  * software state is stored in the iorq structure.  each iorq has an
   1543  * iopb structure.  the hardware understands the iopb structure.
   1544  * every command must go through an iopb.  a 7053 can only handle
   1545  * XDC_MAXIOPB (31) active iopbs at one time.  iopbs are allocated in
   1546  * DVMA space at boot up time.  what happens if we run out of iopb's?
   1547  * for i/o type [1], the buffers are queued at the "buff" layer and
   1548  * picked up later by the interrupt routine.  for case [2] the
   1549  * programmed i/o driver is called with a special flag that says
   1550  * return when one iopb is free.  for case [3] the process can sleep
   1551  * on the iorq free list until some iopbs are avaliable.
   1552  */
   1553 
   1554 
   1555 int
   1556 xdc_submit_iorq(xdcsc, iorqno, type)
   1557 	struct xdc_softc *xdcsc;
   1558 	int     iorqno;
   1559 	int     type;
   1560 
   1561 {
   1562 	u_long  iopbaddr;
   1563 	struct xd_iorq *iorq = &xdcsc->reqs[iorqno];
   1564 
   1565 #ifdef XDC_DEBUG
   1566 	printf("xdc_submit_iorq(%s, no=%d, type=%d)\n", xdcsc->sc_dev.dv_xname,
   1567 	    iorqno, type);
   1568 #endif
   1569 
   1570 	/* first check and see if controller is busy */
   1571 	if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
   1572 #ifdef XDC_DEBUG
   1573 		printf("xdc_submit_iorq: XDC not ready (ADDING)\n");
   1574 #endif
   1575 		if (type == XD_SUB_NOQ)
   1576 			return (XD_ERR_FAIL);	/* failed */
   1577 		XDC_TWAIT(xdcsc, iorqno);	/* put at end of waitq */
   1578 		switch (type) {
   1579 		case XD_SUB_NORM:
   1580 			return XD_ERR_AOK;	/* success */
   1581 		case XD_SUB_WAIT:
   1582 			while (iorq->iopb->done == 0) {
   1583 				sleep(iorq, PRIBIO);
   1584 			}
   1585 			return (iorq->errno);
   1586 		case XD_SUB_POLL:
   1587 			return (xdc_piodriver(xdcsc, iorqno, 0));
   1588 		default:
   1589 			panic("xdc_submit_iorq adding");
   1590 		}
   1591 	}
   1592 #ifdef XDC_DEBUG
   1593 	{
   1594 		u_char *rio = (u_char *) iorq->iopb;
   1595 		int     sz = sizeof(struct xd_iopb), lcv;
   1596 		printf("%s: aio #%d [",
   1597 			xdcsc->sc_dev.dv_xname, iorq - xdcsc->reqs);
   1598 		for (lcv = 0; lcv < sz; lcv++)
   1599 			printf(" %02x", rio[lcv]);
   1600 		printf("]\n");
   1601 	}
   1602 #endif				/* XDC_DEBUG */
   1603 
   1604 	/* controller not busy, start command */
   1605 	iopbaddr = (u_long) iorq->iopb;
   1606 	XDC_GO(xdcsc->xdc, iopbaddr);	/* go! */
   1607 	xdcsc->nrun++;
   1608 	/* command now running, wrap it up */
   1609 	switch (type) {
   1610 	case XD_SUB_NORM:
   1611 	case XD_SUB_NOQ:
   1612 		return (XD_ERR_AOK);	/* success */
   1613 	case XD_SUB_WAIT:
   1614 		while (iorq->iopb->done == 0) {
   1615 			sleep(iorq, PRIBIO);
   1616 		}
   1617 		return (iorq->errno);
   1618 	case XD_SUB_POLL:
   1619 		return (xdc_piodriver(xdcsc, iorqno, 0));
   1620 	default:
   1621 		panic("xdc_submit_iorq wrap up");
   1622 	}
   1623 	panic("xdc_submit_iorq");
   1624 	return 0;	/* not reached */
   1625 }
   1626 
   1627 
   1628 /*
   1629  * xdc_piodriver
   1630  *
   1631  * programmed i/o driver.   this function takes over the computer
   1632  * and drains off all i/o requests.   it returns the status of the iorq
   1633  * the caller is interesting in.   if freeone is true, then it returns
   1634  * when there is a free iorq.
   1635  */
   1636 int
   1637 xdc_piodriver(xdcsc, iorqno, freeone)
   1638 	struct xdc_softc *xdcsc;
   1639 	char    iorqno;
   1640 	int     freeone;
   1641 
   1642 {
   1643 	int     nreset = 0;
   1644 	int     retval = 0;
   1645 	u_long  count;
   1646 	struct xdc *xdc = xdcsc->xdc;
   1647 #ifdef XDC_DEBUG
   1648 	printf("xdc_piodriver(%s, %d, freeone=%d)\n", xdcsc->sc_dev.dv_xname,
   1649 	    iorqno, freeone);
   1650 #endif
   1651 
   1652 	while (xdcsc->nwait || xdcsc->nrun) {
   1653 #ifdef XDC_DEBUG
   1654 		printf("xdc_piodriver: wait=%d, run=%d\n",
   1655 			xdcsc->nwait, xdcsc->nrun);
   1656 #endif
   1657 		XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
   1658 #ifdef XDC_DEBUG
   1659 		printf("xdc_piodriver: done wait with count = %d\n", count);
   1660 #endif
   1661 		/* we expect some progress soon */
   1662 		if (count == 0 && nreset >= 2) {
   1663 			xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
   1664 #ifdef XDC_DEBUG
   1665 			printf("xdc_piodriver: timeout\n");
   1666 #endif
   1667 			return (XD_ERR_FAIL);
   1668 		}
   1669 		if (count == 0) {
   1670 			if (xdc_reset(xdcsc, 0,
   1671 				      (nreset++ == 0) ? XD_RSET_NONE : iorqno,
   1672 				      XD_ERR_FAIL,
   1673 				      0) == XD_ERR_FAIL)
   1674 				return (XD_ERR_FAIL);	/* flushes all but POLL
   1675 							 * requests, resets */
   1676 			continue;
   1677 		}
   1678 		xdc_remove_iorq(xdcsc);	/* could resubmit request */
   1679 		if (freeone) {
   1680 			if (xdcsc->nrun < XDC_MAXIOPB) {
   1681 #ifdef XDC_DEBUG
   1682 				printf("xdc_piodriver: done: one free\n");
   1683 #endif
   1684 				return (XD_ERR_AOK);
   1685 			}
   1686 			continue;	/* don't xdc_start */
   1687 		}
   1688 		xdc_start(xdcsc, XDC_MAXIOPB);
   1689 	}
   1690 
   1691 	/* get return value */
   1692 
   1693 	retval = xdcsc->reqs[iorqno].errno;
   1694 
   1695 #ifdef XDC_DEBUG
   1696 	printf("xdc_piodriver: done, retval = 0x%x (%s)\n",
   1697 	    xdcsc->reqs[iorqno].errno, xdc_e2str(xdcsc->reqs[iorqno].errno));
   1698 #endif
   1699 
   1700 	/* now that we've drained everything, start up any bufs that have
   1701 	 * queued */
   1702 
   1703 	while (xdcsc->nfree > 0 && xdcsc->sc_wq.b_actf)
   1704 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
   1705 			break;
   1706 
   1707 	return (retval);
   1708 }
   1709 
   1710 /*
   1711  * xdc_reset: reset one drive.   NOTE: assumes xdc was just reset.
   1712  * we steal iopb[0] for this, but we put it back when we are done.
   1713  */
   1714 void
   1715 xdc_xdreset(xdcsc, xdsc)
   1716 	struct xdc_softc *xdcsc;
   1717 	struct xd_softc *xdsc;
   1718 
   1719 {
   1720 	struct xd_iopb tmpiopb;
   1721 	u_long  addr;
   1722 	int     del;
   1723 	bcopy(xdcsc->iopbase, &tmpiopb, sizeof(tmpiopb));
   1724 	bzero(xdcsc->iopbase, sizeof(tmpiopb));
   1725 	xdcsc->iopbase->comm = XDCMD_RST;
   1726 	xdcsc->iopbase->unit = xdsc->xd_drive;
   1727 	addr = (u_long) xdcsc->dvmaiopb;
   1728 	XDC_GO(xdcsc->xdc, addr);	/* go! */
   1729 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_REMIOPB);
   1730 	if (del <= 0 || xdcsc->iopbase->errs) {
   1731 		printf("%s: off-line: %s\n", xdcsc->sc_dev.dv_xname,
   1732 		    xdc_e2str(xdcsc->iopbase->errno));
   1733 		xdcsc->xdc->xdc_csr = XDC_RESET;
   1734 		XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
   1735 		if (del <= 0)
   1736 			panic("xdc_reset");
   1737 	} else {
   1738 		xdcsc->xdc->xdc_csr = XDC_CLRRIO;	/* clear RIO */
   1739 	}
   1740 	bcopy(&tmpiopb, xdcsc->iopbase, sizeof(tmpiopb));
   1741 }
   1742 
   1743 
   1744 /*
   1745  * xdc_reset: reset everything: requests are marked as errors except
   1746  * a polled request (which is resubmitted)
   1747  */
   1748 int
   1749 xdc_reset(xdcsc, quiet, blastmode, error, xdsc)
   1750 	struct xdc_softc *xdcsc;
   1751 	int     quiet, blastmode, error;
   1752 	struct xd_softc *xdsc;
   1753 
   1754 {
   1755 	int     del = 0, lcv, retval = XD_ERR_AOK;
   1756 	int     oldfree = xdcsc->nfree;
   1757 
   1758 	/* soft reset hardware */
   1759 
   1760 	if (!quiet)
   1761 		printf("%s: soft reset\n", xdcsc->sc_dev.dv_xname);
   1762 	xdcsc->xdc->xdc_csr = XDC_RESET;
   1763 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
   1764 	if (del <= 0) {
   1765 		blastmode = XD_RSET_ALL;	/* dead, flush all requests */
   1766 		retval = XD_ERR_FAIL;
   1767 	}
   1768 	if (xdsc)
   1769 		xdc_xdreset(xdcsc, xdsc);
   1770 
   1771 	/* fix queues based on "blast-mode" */
   1772 
   1773 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   1774 		register struct xd_iorq *iorq = &xdcsc->reqs[lcv];
   1775 
   1776 		if (XD_STATE(iorq->mode) != XD_SUB_POLL &&
   1777 		    XD_STATE(iorq->mode) != XD_SUB_WAIT &&
   1778 		    XD_STATE(iorq->mode) != XD_SUB_NORM)
   1779 			/* is it active? */
   1780 			continue;
   1781 
   1782 		xdcsc->nrun--;	/* it isn't running any more */
   1783 		if (blastmode == XD_RSET_ALL || blastmode != lcv) {
   1784 			/* failed */
   1785 			iorq->errno = error;
   1786 			xdcsc->iopbase[lcv].done = xdcsc->iopbase[lcv].errs = 1;
   1787 			switch (XD_STATE(xdcsc->reqs[lcv].mode)) {
   1788 			case XD_SUB_NORM:
   1789 			    iorq->buf->b_error = EIO;
   1790 			    iorq->buf->b_flags |= B_ERROR;
   1791 			    iorq->buf->b_resid =
   1792 			       iorq->sectcnt * XDFM_BPS;
   1793 
   1794 			    bus_dmamap_sync(xdcsc->dmatag, iorq->dmamap,
   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,
   2005 					(bp->b_flags & B_READ)
   2006 						? BUS_DMASYNC_POSTREAD
   2007 						: BUS_DMASYNC_POSTWRITE);
   2008 			bus_dmamap_unload(xdcsc->dmatag, iorq->dmamap);
   2009 
   2010 			disk_unbusy(&iorq->xd->sc_dk,
   2011 			    (bp->b_bcount - bp->b_resid));
   2012 			XDC_FREE(xdcsc, rqno);
   2013 			biodone(bp);
   2014 			break;
   2015 		case XD_SUB_WAIT:
   2016 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   2017 			xdcsc->ndone++;
   2018 			wakeup(iorq);
   2019 			break;
   2020 		case XD_SUB_POLL:
   2021 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
   2022 			xdcsc->ndone++;
   2023 			break;
   2024 		}
   2025 	}
   2026 
   2027 	return (XD_ERR_AOK);
   2028 }
   2029 
   2030 /*
   2031  * xdc_perror: print error.
   2032  * - if still_trying is true: we got an error, retried and got a
   2033  *   different error.  in that case lasterror is the old error,
   2034  *   and errno is the new one.
   2035  * - if still_trying is not true, then if we ever had an error it
   2036  *   is in lasterror. also, if iorq->errno == 0, then we recovered
   2037  *   from that error (otherwise iorq->errno == iorq->lasterror).
   2038  */
   2039 void
   2040 xdc_perror(iorq, iopb, still_trying)
   2041 	struct xd_iorq *iorq;
   2042 	struct xd_iopb *iopb;
   2043 	int     still_trying;
   2044 
   2045 {
   2046 
   2047 	int     error = iorq->lasterror;
   2048 
   2049 	printf("%s", (iorq->xd) ? iorq->xd->sc_dev.dv_xname
   2050 	    : iorq->xdc->sc_dev.dv_xname);
   2051 	if (iorq->buf)
   2052 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
   2053 	if (iopb->comm == XDCMD_RD || iopb->comm == XDCMD_WR)
   2054 		printf("%s %d/%d/%d: ",
   2055 			(iopb->comm == XDCMD_RD) ? "read" : "write",
   2056 			iopb->cylno, iopb->headno, iopb->sectno);
   2057 	printf("%s", xdc_e2str(error));
   2058 
   2059 	if (still_trying)
   2060 		printf(" [still trying, new error=%s]", xdc_e2str(iorq->errno));
   2061 	else
   2062 		if (iorq->errno == 0)
   2063 			printf(" [recovered in %d tries]", iorq->tries);
   2064 
   2065 	printf("\n");
   2066 }
   2067 
   2068 /*
   2069  * xdc_error: non-fatal error encountered... recover.
   2070  * return AOK if resubmitted, return FAIL if this iopb is done
   2071  */
   2072 int
   2073 xdc_error(xdcsc, iorq, iopb, rqno, comm)
   2074 	struct xdc_softc *xdcsc;
   2075 	struct xd_iorq *iorq;
   2076 	struct xd_iopb *iopb;
   2077 	int     rqno, comm;
   2078 
   2079 {
   2080 	int     errno = iorq->errno;
   2081 	int     erract = errno & XD_ERA_MASK;
   2082 	int     oldmode, advance, i;
   2083 
   2084 	if (erract == XD_ERA_RSET) {	/* some errors require a reset */
   2085 		oldmode = iorq->mode;
   2086 		iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
   2087 		xdcsc->ndone++;
   2088 		/* make xdc_start ignore us */
   2089 		xdc_reset(xdcsc, 1, XD_RSET_NONE, errno, iorq->xd);
   2090 		iorq->mode = oldmode;
   2091 		xdcsc->ndone--;
   2092 	}
   2093 	/* check for read/write to a sector in bad144 table if bad: redirect
   2094 	 * request to bad144 area */
   2095 
   2096 	if ((comm == XDCMD_RD || comm == XDCMD_WR) &&
   2097 	    (iorq->mode & XD_MODE_B144) == 0) {
   2098 		advance = iorq->sectcnt - iopb->sectcnt;
   2099 		XDC_ADVANCE(iorq, advance);
   2100 		if ((i = isbad(&iorq->xd->dkb, iorq->blockno / iorq->xd->sectpercyl,
   2101 			    (iorq->blockno / iorq->xd->nsect) % iorq->xd->nhead,
   2102 			    iorq->blockno % iorq->xd->nsect)) != -1) {
   2103 			iorq->mode |= XD_MODE_B144;	/* enter bad144 mode &
   2104 							 * redirect */
   2105 			iopb->errno = iopb->done = iopb->errs = 0;
   2106 			iopb->sectcnt = 1;
   2107 			iopb->cylno = (iorq->xd->ncyl + iorq->xd->acyl) - 2;
   2108 			/* second to last acyl */
   2109 			i = iorq->xd->sectpercyl - 1 - i;	/* follow bad144
   2110 								 * standard */
   2111 			iopb->headno = i / iorq->xd->nhead;
   2112 			iopb->sectno = i % iorq->xd->nhead;
   2113 			XDC_HWAIT(xdcsc, rqno);
   2114 			xdc_start(xdcsc, 1);	/* resubmit */
   2115 			return (XD_ERR_AOK);	/* recovered! */
   2116 		}
   2117 	}
   2118 
   2119 	/*
   2120 	 * it isn't a bad144 sector, must be real error! see if we can retry
   2121 	 * it?
   2122 	 */
   2123 	if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
   2124 		xdc_perror(iorq, iopb, 1);	/* inform of error state
   2125 						 * change */
   2126 	iorq->lasterror = errno;
   2127 
   2128 	if ((erract == XD_ERA_RSET || erract == XD_ERA_HARD)
   2129 	    && iorq->tries < XDC_MAXTRIES) {	/* retry? */
   2130 		iorq->tries++;
   2131 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
   2132 		XDC_HWAIT(xdcsc, rqno);
   2133 		xdc_start(xdcsc, 1);	/* restart */
   2134 		return (XD_ERR_AOK);	/* recovered! */
   2135 	}
   2136 
   2137 	/* failed to recover from this error */
   2138 	return (XD_ERR_FAIL);
   2139 }
   2140 
   2141 /*
   2142  * xdc_tick: make sure xd is still alive and ticking (err, kicking).
   2143  */
   2144 void
   2145 xdc_tick(arg)
   2146 	void   *arg;
   2147 
   2148 {
   2149 	struct xdc_softc *xdcsc = arg;
   2150 	int     lcv, s, reset = 0;
   2151 #ifdef XDC_DIAG
   2152 	int     wait, run, free, done, whd = 0;
   2153 	u_char  fqc[XDC_MAXIOPB], wqc[XDC_MAXIOPB], mark[XDC_MAXIOPB];
   2154 	s = splbio();
   2155 	wait = xdcsc->nwait;
   2156 	run = xdcsc->nrun;
   2157 	free = xdcsc->nfree;
   2158 	done = xdcsc->ndone;
   2159 	bcopy(xdcsc->waitq, wqc, sizeof(wqc));
   2160 	bcopy(xdcsc->freereq, fqc, sizeof(fqc));
   2161 	splx(s);
   2162 	if (wait + run + free + done != XDC_MAXIOPB) {
   2163 		printf("%s: diag: IOPB miscount (got w/f/r/d %d/%d/%d/%d, wanted %d)\n",
   2164 		    xdcsc->sc_dev.dv_xname, wait, free, run, done, XDC_MAXIOPB);
   2165 		bzero(mark, sizeof(mark));
   2166 		printf("FREE: ");
   2167 		for (lcv = free; lcv > 0; lcv--) {
   2168 			printf("%d ", fqc[lcv - 1]);
   2169 			mark[fqc[lcv - 1]] = 1;
   2170 		}
   2171 		printf("\nWAIT: ");
   2172 		lcv = wait;
   2173 		while (lcv > 0) {
   2174 			printf("%d ", wqc[whd]);
   2175 			mark[wqc[whd]] = 1;
   2176 			whd = (whd + 1) % XDC_MAXIOPB;
   2177 			lcv--;
   2178 		}
   2179 		printf("\n");
   2180 		for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2181 			if (mark[lcv] == 0)
   2182 				printf("MARK: running %d: mode %d done %d errs %d errno 0x%x ttl %d buf %p\n",
   2183 				lcv, xdcsc->reqs[lcv].mode,
   2184 				xdcsc->iopbase[lcv].done,
   2185 				xdcsc->iopbase[lcv].errs,
   2186 				xdcsc->iopbase[lcv].errno,
   2187 				xdcsc->reqs[lcv].ttl, xdcsc->reqs[lcv].buf);
   2188 		}
   2189 	} else
   2190 		if (done > XDC_MAXIOPB - XDC_SUBWAITLIM)
   2191 			printf("%s: diag: lots of done jobs (%d)\n",
   2192 				xdcsc->sc_dev.dv_xname, done);
   2193 
   2194 #endif
   2195 #ifdef XDC_DEBUG
   2196 	printf("%s: tick: csr 0x%x, w/f/r/d %d/%d/%d/%d\n",
   2197 		xdcsc->sc_dev.dv_xname,
   2198 		xdcsc->xdc->xdc_csr, xdcsc->nwait, xdcsc->nfree, xdcsc->nrun,
   2199 		xdcsc->ndone);
   2200 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2201 		if (xdcsc->reqs[lcv].mode)
   2202 		  printf("running %d: mode %d done %d errs %d errno 0x%x\n",
   2203 			 lcv,
   2204 			 xdcsc->reqs[lcv].mode, xdcsc->iopbase[lcv].done,
   2205 			 xdcsc->iopbase[lcv].errs, xdcsc->iopbase[lcv].errno);
   2206 	}
   2207 #endif
   2208 
   2209 	/* reduce ttl for each request if one goes to zero, reset xdc */
   2210 	s = splbio();
   2211 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
   2212 		if (xdcsc->reqs[lcv].mode == 0 ||
   2213 		    XD_STATE(xdcsc->reqs[lcv].mode) == XD_SUB_DONE)
   2214 			continue;
   2215 		xdcsc->reqs[lcv].ttl--;
   2216 		if (xdcsc->reqs[lcv].ttl == 0)
   2217 			reset = 1;
   2218 	}
   2219 	if (reset) {
   2220 		printf("%s: watchdog timeout\n", xdcsc->sc_dev.dv_xname);
   2221 		xdc_reset(xdcsc, 0, XD_RSET_NONE, XD_ERR_FAIL, NULL);
   2222 	}
   2223 	splx(s);
   2224 
   2225 	/* until next time */
   2226 
   2227 	timeout(xdc_tick, xdcsc, XDC_TICKCNT);
   2228 }
   2229 
   2230 /*
   2231  * xdc_ioctlcmd: this function provides a user level interface to the
   2232  * controller via ioctl.   this allows "format" programs to be written
   2233  * in user code, and is also useful for some debugging.   we return
   2234  * an error code.   called at user priority.
   2235  */
   2236 int
   2237 xdc_ioctlcmd(xd, dev, xio)
   2238 	struct xd_softc *xd;
   2239 	dev_t   dev;
   2240 	struct xd_iocmd *xio;
   2241 
   2242 {
   2243 	int     s, rqno, dummy;
   2244 	caddr_t dvmabuf = NULL, buf = NULL;
   2245 	struct xdc_softc *xdcsc;
   2246 	int			rseg, error;
   2247 	bus_dma_segment_t	seg;
   2248 
   2249 	/* check sanity of requested command */
   2250 
   2251 	switch (xio->cmd) {
   2252 
   2253 	case XDCMD_NOP:	/* no op: everything should be zero */
   2254 		if (xio->subfn || xio->dptr || xio->dlen ||
   2255 		    xio->block || xio->sectcnt)
   2256 			return (EINVAL);
   2257 		break;
   2258 
   2259 	case XDCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
   2260 	case XDCMD_WR:
   2261 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
   2262 		    xio->sectcnt * XDFM_BPS != xio->dlen || xio->dptr == NULL)
   2263 			return (EINVAL);
   2264 		break;
   2265 
   2266 	case XDCMD_SK:		/* seek: doesn't seem useful to export this */
   2267 		return (EINVAL);
   2268 
   2269 	case XDCMD_WRP:	/* write parameters */
   2270 		return (EINVAL);/* not useful, except maybe drive
   2271 				 * parameters... but drive parameters should
   2272 				 * go via disklabel changes */
   2273 
   2274 	case XDCMD_RDP:	/* read parameters */
   2275 		if (xio->subfn != XDFUN_DRV ||
   2276 		    xio->dlen || xio->block || xio->dptr)
   2277 			return (EINVAL);	/* allow read drive params to
   2278 						 * get hw_spt */
   2279 		xio->sectcnt = xd->hw_spt;	/* we already know the answer */
   2280 		return (0);
   2281 		break;
   2282 
   2283 	case XDCMD_XRD:	/* extended read/write */
   2284 	case XDCMD_XWR:
   2285 
   2286 		switch (xio->subfn) {
   2287 
   2288 		case XDFUN_THD:/* track headers */
   2289 			if (xio->sectcnt != xd->hw_spt ||
   2290 			    (xio->block % xd->nsect) != 0 ||
   2291 			    xio->dlen != XD_IOCMD_HSZ * xd->hw_spt ||
   2292 			    xio->dptr == NULL)
   2293 				return (EINVAL);
   2294 			xio->sectcnt = 0;
   2295 			break;
   2296 
   2297 		case XDFUN_FMT:/* NOTE: also XDFUN_VFY */
   2298 			if (xio->cmd == XDCMD_XRD)
   2299 				return (EINVAL);	/* no XDFUN_VFY */
   2300 			if (xio->sectcnt || xio->dlen ||
   2301 			    (xio->block % xd->nsect) != 0 || xio->dptr)
   2302 				return (EINVAL);
   2303 			break;
   2304 
   2305 		case XDFUN_HDR:/* header, header verify, data, data ECC */
   2306 			return (EINVAL);	/* not yet */
   2307 
   2308 		case XDFUN_DM:	/* defect map */
   2309 		case XDFUN_DMX:/* defect map (alternate location) */
   2310 			if (xio->sectcnt || xio->dlen != XD_IOCMD_DMSZ ||
   2311 			    (xio->block % xd->nsect) != 0 || xio->dptr == NULL)
   2312 				return (EINVAL);
   2313 			break;
   2314 
   2315 		default:
   2316 			return (EINVAL);
   2317 		}
   2318 		break;
   2319 
   2320 	case XDCMD_TST:	/* diagnostics */
   2321 		return (EINVAL);
   2322 
   2323 	default:
   2324 		return (EINVAL);/* ??? */
   2325 	}
   2326 
   2327 	xdcsc = xd->parent;
   2328 
   2329 	/* create DVMA buffer for request if needed */
   2330 	if (xio->dlen) {
   2331 		error = bus_dmamem_alloc(xdcsc->dmatag, xio->dlen, NBPG, 0,
   2332 					 &seg, 1, &rseg, BUS_DMA_WAITOK);
   2333 		if (error)
   2334 			return (error);
   2335 
   2336 		dvmabuf = (caddr_t)seg.ds_addr;
   2337 
   2338 		error = bus_dmamem_map(xdcsc->dmatag, &seg, rseg, xio->dlen,
   2339 					&buf,
   2340 					BUS_DMA_WAITOK|BUS_DMA_COHERENT);
   2341 		if (error) {
   2342 			bus_dmamem_free(xdcsc->dmatag, &seg, rseg);
   2343 			return (error);
   2344 		}
   2345 		if (xio->cmd == XDCMD_WR || xio->cmd == XDCMD_XWR) {
   2346 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
   2347 				bus_dmamem_unmap(xdcsc->dmatag, buf, xio->dlen);
   2348 				bus_dmamem_free(xdcsc->dmatag, &seg, rseg);
   2349 				return (error);
   2350 			}
   2351 		}
   2352 	}
   2353 
   2354 	/* do it! */
   2355 
   2356 	error = 0;
   2357 	s = splbio();
   2358 	rqno = xdc_cmd(xdcsc, xio->cmd, xio->subfn, xd->xd_drive, xio->block,
   2359 	    xio->sectcnt, dvmabuf, XD_SUB_WAIT);
   2360 	if (rqno == XD_ERR_FAIL) {
   2361 		error = EIO;
   2362 		goto done;
   2363 	}
   2364 	xio->errno = xdcsc->reqs[rqno].errno;
   2365 	xio->tries = xdcsc->reqs[rqno].tries;
   2366 	XDC_DONE(xdcsc, rqno, dummy);
   2367 
   2368 	if (xio->cmd == XDCMD_RD || xio->cmd == XDCMD_XRD)
   2369 		error = copyout(buf, xio->dptr, xio->dlen);
   2370 
   2371 done:
   2372 	splx(s);
   2373 	if (dvmabuf) {
   2374 		bus_dmamem_unmap(xdcsc->dmatag, buf, xio->dlen);
   2375 		bus_dmamem_free(xdcsc->dmatag, &seg, rseg);
   2376 	}
   2377 	return (error);
   2378 }
   2379 
   2380 /*
   2381  * xdc_e2str: convert error code number into an error string
   2382  */
   2383 char *
   2384 xdc_e2str(no)
   2385 	int     no;
   2386 {
   2387 	switch (no) {
   2388 	case XD_ERR_FAIL:
   2389 		return ("Software fatal error");
   2390 	case XD_ERR_AOK:
   2391 		return ("Successful completion");
   2392 	case XD_ERR_ICYL:
   2393 		return ("Illegal cylinder address");
   2394 	case XD_ERR_IHD:
   2395 		return ("Illegal head address");
   2396 	case XD_ERR_ISEC:
   2397 		return ("Illgal sector address");
   2398 	case XD_ERR_CZER:
   2399 		return ("Count zero");
   2400 	case XD_ERR_UIMP:
   2401 		return ("Unimplemented command");
   2402 	case XD_ERR_IF1:
   2403 		return ("Illegal field length 1");
   2404 	case XD_ERR_IF2:
   2405 		return ("Illegal field length 2");
   2406 	case XD_ERR_IF3:
   2407 		return ("Illegal field length 3");
   2408 	case XD_ERR_IF4:
   2409 		return ("Illegal field length 4");
   2410 	case XD_ERR_IF5:
   2411 		return ("Illegal field length 5");
   2412 	case XD_ERR_IF6:
   2413 		return ("Illegal field length 6");
   2414 	case XD_ERR_IF7:
   2415 		return ("Illegal field length 7");
   2416 	case XD_ERR_ISG:
   2417 		return ("Illegal scatter/gather length");
   2418 	case XD_ERR_ISPT:
   2419 		return ("Not enough sectors per track");
   2420 	case XD_ERR_ALGN:
   2421 		return ("Next IOPB address alignment error");
   2422 	case XD_ERR_SGAL:
   2423 		return ("Scatter/gather address alignment error");
   2424 	case XD_ERR_SGEC:
   2425 		return ("Scatter/gather with auto-ECC");
   2426 	case XD_ERR_SECC:
   2427 		return ("Soft ECC corrected");
   2428 	case XD_ERR_SIGN:
   2429 		return ("ECC ignored");
   2430 	case XD_ERR_ASEK:
   2431 		return ("Auto-seek retry recovered");
   2432 	case XD_ERR_RTRY:
   2433 		return ("Soft retry recovered");
   2434 	case XD_ERR_HECC:
   2435 		return ("Hard data ECC");
   2436 	case XD_ERR_NHDR:
   2437 		return ("Header not found");
   2438 	case XD_ERR_NRDY:
   2439 		return ("Drive not ready");
   2440 	case XD_ERR_TOUT:
   2441 		return ("Operation timeout");
   2442 	case XD_ERR_VTIM:
   2443 		return ("VMEDMA timeout");
   2444 	case XD_ERR_DSEQ:
   2445 		return ("Disk sequencer error");
   2446 	case XD_ERR_HDEC:
   2447 		return ("Header ECC error");
   2448 	case XD_ERR_RVFY:
   2449 		return ("Read verify");
   2450 	case XD_ERR_VFER:
   2451 		return ("Fatail VMEDMA error");
   2452 	case XD_ERR_VBUS:
   2453 		return ("VMEbus error");
   2454 	case XD_ERR_DFLT:
   2455 		return ("Drive faulted");
   2456 	case XD_ERR_HECY:
   2457 		return ("Header error/cyliner");
   2458 	case XD_ERR_HEHD:
   2459 		return ("Header error/head");
   2460 	case XD_ERR_NOCY:
   2461 		return ("Drive not on-cylinder");
   2462 	case XD_ERR_SEEK:
   2463 		return ("Seek error");
   2464 	case XD_ERR_ILSS:
   2465 		return ("Illegal sector size");
   2466 	case XD_ERR_SEC:
   2467 		return ("Soft ECC");
   2468 	case XD_ERR_WPER:
   2469 		return ("Write-protect error");
   2470 	case XD_ERR_IRAM:
   2471 		return ("IRAM self test failure");
   2472 	case XD_ERR_MT3:
   2473 		return ("Maintenance test 3 failure (DSKCEL RAM)");
   2474 	case XD_ERR_MT4:
   2475 		return ("Maintenance test 4 failure (header shift reg)");
   2476 	case XD_ERR_MT5:
   2477 		return ("Maintenance test 5 failure (VMEDMA regs)");
   2478 	case XD_ERR_MT6:
   2479 		return ("Maintenance test 6 failure (REGCEL chip)");
   2480 	case XD_ERR_MT7:
   2481 		return ("Maintenance test 7 failure (buffer parity)");
   2482 	case XD_ERR_MT8:
   2483 		return ("Maintenance test 8 failure (disk FIFO)");
   2484 	case XD_ERR_IOCK:
   2485 		return ("IOPB checksum miscompare");
   2486 	case XD_ERR_IODM:
   2487 		return ("IOPB DMA fatal");
   2488 	case XD_ERR_IOAL:
   2489 		return ("IOPB address alignment error");
   2490 	case XD_ERR_FIRM:
   2491 		return ("Firmware error");
   2492 	case XD_ERR_MMOD:
   2493 		return ("Illegal maintenance mode test number");
   2494 	case XD_ERR_ACFL:
   2495 		return ("ACFAIL asserted");
   2496 	default:
   2497 		return ("Unknown error");
   2498 	}
   2499 }
   2500