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