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