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