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