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