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