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