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