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