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