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