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