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