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