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