code:
/*	$OpenBSD: xd.c,v 1.72 2015/02/08 01:30:09 dlg Exp $	*/
/*	$NetBSD: xd.c,v 1.37 1997/07/29 09:58:16 fair Exp $	*/

/*
 * Copyright (c) 1995 Charles D. Cranor
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *
 * 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
 *
 * author: Chuck Cranor <chuck@ccrc.wustl.edu>
 * id: $NetBSD: xd.c,v 1.37 1997/07/29 09:58:16 fair Exp $
 * started: 27-Feb-95
 * references: [1] Xylogics Model 753 User's Manual
 *                 part number: 166-753-001, Revision B, May 21, 1988.
 *                 "Your Partner For Performance"
 *             [2] other NetBSD disk device drivers
 *
 * Special thanks go to Scott E. Campbell of Xylogics, Inc. for taking
 * the time to answer some of my questions about the 753/7053.
 *
 * note: the 753 and the 7053 are programmed the same way, but are
 * different sizes.   the 753 is a 6U VME card, while the 7053 is a 9U
 * VME card (found in many VME based suns).
 */

#undef XDC_DEBUG		/* full debug */
#if !defined(SMALL_KERNEL)
#define XDC_DIAG		/* extra sanity checks */
#endif
#if defined(DIAGNOSTIC) && !defined(XDC_DIAG)
#define XDC_DIAG		/* link in with master DIAG option */
#endif

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/syslog.h>
#include <sys/dkbad.h>
#include <sys/conf.h>
#include <sys/timeout.h>
#include <sys/dkio.h>

#include <uvm/uvm_extern.h>

#include <machine/autoconf.h>
#include <dev/sun/disklabel.h>
#include <machine/conf.h>

#include <sparc/dev/xdreg.h>
#include <sparc/dev/xdvar.h>
#include <sparc/sparc/vaddrs.h>
#include <sparc/sparc/cpuvar.h>

/*
 * macros
 */

/*
 * XDC_TWAIT: add iorq "N" to tail of SC's wait queue
 */
#define XDC_TWAIT(SC, N) { \
	(SC)->waitq[(SC)->waitend] = (N); \
	(SC)->waitend = ((SC)->waitend + 1) % XDC_MAXIOPB; \
	(SC)->nwait++; \
}

/*
 * XDC_HWAIT: add iorq "N" to head of SC's wait queue
 */
#define XDC_HWAIT(SC, N) { \
	(SC)->waithead = ((SC)->waithead == 0) ? \
		(XDC_MAXIOPB - 1) : ((SC)->waithead - 1); \
	(SC)->waitq[(SC)->waithead] = (N); \
	(SC)->nwait++; \
}

/*
 * XDC_GET_WAITER: gets the first request waiting on the waitq
 * and removes it (so it can be submitted)
 */
#define XDC_GET_WAITER(XDCSC, RQ) { \
	(RQ) = (XDCSC)->waitq[(XDCSC)->waithead]; \
	(XDCSC)->waithead = ((XDCSC)->waithead + 1) % XDC_MAXIOPB; \
	xdcsc->nwait--; \
}

/*
 * XDC_FREE: add iorq "N" to SC's free list
 */
#define XDC_FREE(SC, N) { \
	(SC)->freereq[(SC)->nfree++] = (N); \
	(SC)->reqs[N].mode = 0; \
	if ((SC)->nfree == 1) wakeup(&(SC)->nfree); \
}

/*
 * XDC_RQALLOC: allocate an iorq off the free list (assume nfree > 0).
 */
#define XDC_RQALLOC(XDCSC) (XDCSC)->freereq[--((XDCSC)->nfree)]

/*
 * XDC_GO: start iopb ADDR (DVMA addr in a u_long) on XDC
 */
#define XDC_GO(XDC, ADDR) { \
	(XDC)->xdc_iopbaddr0 = ((ADDR) & 0xff); \
	(ADDR) = ((ADDR) >> 8); \
	(XDC)->xdc_iopbaddr1 = ((ADDR) & 0xff); \
	(ADDR) = ((ADDR) >> 8); \
	(XDC)->xdc_iopbaddr2 = ((ADDR) & 0xff); \
	(ADDR) = ((ADDR) >> 8); \
	(XDC)->xdc_iopbaddr3 = (ADDR); \
	(XDC)->xdc_iopbamod = XDC_ADDRMOD; \
	(XDC)->xdc_csr = XDC_ADDIOPB; /* go! */ \
}

/*
 * XDC_WAIT: wait for XDC's csr "BITS" to come on in "TIME".
 *   LCV is a counter.  If it goes to zero then we timed out.
 */
#define XDC_WAIT(XDC, LCV, TIME, BITS) { \
	(LCV) = (TIME); \
	while ((LCV) > 0) { \
		if ((XDC)->xdc_csr & (BITS)) break; \
		(LCV) = (LCV) - 1; \
		DELAY(1); \
	} \
}

/*
 * XDC_DONE: don't need IORQ, get error code and free (done after xdc_cmd)
 */
#define XDC_DONE(SC,RQ,ER) { \
	if ((RQ) == XD_ERR_FAIL) { \
		(ER) = (RQ); \
	} else { \
		if ((SC)->ndone-- == XDC_SUBWAITLIM) \
		wakeup(&(SC)->ndone); \
		(ER) = (SC)->reqs[RQ].errno; \
		XDC_FREE((SC), (RQ)); \
	} \
}

/*
 * XDC_ADVANCE: advance iorq's pointers by a number of sectors
 */
#define XDC_ADVANCE(IORQ, N) { \
	if (N) { \
		(IORQ)->sectcnt -= (N); \
		(IORQ)->blockno += (N); \
		(IORQ)->dbuf += ((N)*XDFM_BPS); \
	} \
}

/*
 * note - addresses you can sleep on:
 *   [1] & of xd_softc's "state" (waiting for a chance to attach a drive)
 *   [2] & of xdc_softc's "nfree" (waiting for a free iorq/iopb)
 *   [3] & of xdc_softc's "ndone" (waiting for number of done iorq/iopb's
 *                                 to drop below XDC_SUBWAITLIM)
 *   [4] & an iorq (waiting for an XD_SUB_WAIT iorq to finish)
 */

/*
 * function prototypes
 * "xdc_*" functions are internal, all others are external interfaces
 */

extern int pil_to_vme[];	/* from obio.c */

/* internals */
int	xdc_cmd(struct xdc_softc *, int, int, int, int, int, char *, int);
const char *xdc_e2str(int);
int	xdc_error(struct xdc_softc *, struct xd_iorq *,
		   struct xd_iopb *, int, int);
void	xdc_perror(struct xd_iorq *, struct xd_iopb *, int);
int	xdc_piodriver(struct xdc_softc *, int, int);
int	xdc_remove_iorq(struct xdc_softc *);
int	xdc_reset(struct xdc_softc *, int, int, int, struct xd_softc *);
inline void xdc_rqinit(struct xd_iorq *, struct xdc_softc *,
			    struct xd_softc *, int, u_long, int,
			    caddr_t, struct buf *);
void	xdc_rqtopb(struct xd_iorq *, struct xd_iopb *, int, int);
void	xdc_start(struct xdc_softc *, int);
int	xdc_startbuf(struct xdc_softc *);
int	xdc_submit_iorq(struct xdc_softc *, int, int);
void	xdc_tick(void *);
void	xdc_xdreset(struct xdc_softc *, struct xd_softc *);

/* machine interrupt hook */
int	xdcintr(void *);

/* autoconf */
int	xdcmatch(struct device *, void *, void *);
void	xdcattach(struct device *, struct device *, void *);
int	xdmatch(struct device *, void *, void *);
void	xdattach(struct device *, struct device *, void *);

static	void xddummystrat(struct buf *);
int	xdgetdisklabel(dev_t, struct xd_softc *, struct disklabel *, int);

/*
 * cfdrivers: device driver interface to autoconfig
 */

struct cfattach xdc_ca = {
	sizeof(struct xdc_softc), xdcmatch, xdcattach
};

struct cfdriver xdc_cd = {
	NULL, "xdc", DV_DULL
};

struct cfattach xd_ca = {
	sizeof(struct xd_softc), xdmatch, xdattach
};

struct cfdriver xd_cd = {
	NULL, "xd", DV_DISK
};

struct xdc_attach_args {	/* this is the "aux" args to xdattach */
	int	driveno;	/* unit number */
	char	*buf;		/* scratch buffer for reading disk label */
	char	*dvmabuf;	/* DVMA address of above */
	int	fullmode;	/* submit mode */
	int	booting;	/* are we booting or not? */
};

static void
xddummystrat(bp)
	struct buf *bp;
{
	struct xd_softc *xd;
	size_t sz;

	xd = (struct xd_softc *)xd_cd.cd_devs[DISKUNIT(bp->b_dev)];
	sz = MIN(bp->b_bcount, XDFM_BPS);
	bcopy(xd->xd_labeldata, bp->b_data, sz);
	bp->b_resid = bp->b_bcount - sz;
	if (bp->b_resid != 0) {
		bp->b_flags |= B_ERROR;
		bp->b_error = EIO;
	}
	bp->b_flags |= B_DONE;
}

int
xdgetdisklabel(dev, xd, lp, spoofonly)
	dev_t dev;
	struct xd_softc *xd;
	struct disklabel *lp;
	int spoofonly;
{
	int error;

	bzero(lp, sizeof(struct disklabel));
	/* Required parameters for readdisklabel() */
	lp->d_secsize = XDFM_BPS;
	if (xd->state == XD_DRIVE_ATTACHING || xd->state == XD_DRIVE_NOLABEL) {
		/* needs to be nonzero */
		lp->d_secpercyl = 1;
	} else {
		/*
		 * Disk geometry is known from a previously found label.
		 * Use it.
		 */
		lp->d_ntracks = xd->nhead;
		lp->d_nsectors = xd->nsect;
		lp->d_ncylinders = xd->ncyl;
		lp->d_acylinders = xd->acyl;
		lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
		DL_SETDSIZE(lp, (u_int64_t)lp->d_secpercyl * lp->d_ncylinders);
	}
	lp->d_type = DTYPE_SMD;
	lp->d_version = 1;

	/* These are as defined in <ufs/ffs/fs.h> */
	lp->d_bbsize = 8192; /* BBSIZE */
	lp->d_sbsize = 8192; /* SBSIZE */

	lp->d_magic = DISKMAGIC;
	lp->d_magic2 = DISKMAGIC;
	lp->d_checksum = dkcksum(lp);

	error = readdisklabel(DISKLABELDEV(dev),
	    xd->state == XD_DRIVE_ATTACHING ? xddummystrat : xdstrategy,
	    lp, spoofonly);
	if (error)
		return error;

	/*
	 * If a label was found, get our geometry from it.
	 */
	if (xd->state == XD_DRIVE_ATTACHING || xd->state == XD_DRIVE_NOLABEL) {
		/*
		 * Note that this relies upon pcyl == cyl + acyl, and will
		 * ignore the explicit pcyl value from the converted SunOS
		 * label.
		 */
		xd->pcyl = lp->d_ncylinders + lp->d_acylinders;
		xd->ncyl = lp->d_ncylinders;
		xd->acyl = lp->d_acylinders;
		xd->nhead = lp->d_ntracks;
		xd->nsect = lp->d_nsectors;
		xd->sectpercyl = lp->d_secpercyl;
	}

	return 0;
}

/*
 * a u t o c o n f i g   f u n c t i o n s
 */

/*
 * xdcmatch: determine if xdc is present or not.   we do a
 * soft reset to detect the xdc.
 */

int xdcmatch(parent, vcf, aux)
	struct device *parent;
	void *vcf, *aux;
{
	struct cfdata *cf = vcf;
	struct confargs *ca = aux;
	struct romaux *ra = &ca->ca_ra;
	struct xdc *xdc;
	int     del = 0;

	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
		return (0);

	switch (ca->ca_bustype) {
	case BUS_OBIO:
	case BUS_SBUS:
	case BUS_VME16:
	default:
		return (0);
	case BUS_VME32:
		xdc = (struct xdc *) ra->ra_vaddr;
		if (probeget((caddr_t) &xdc->xdc_csr, 1) == -1)
			return (0);
		xdc->xdc_csr = XDC_RESET;
		XDC_WAIT(xdc, del, XDC_RESETUSEC, XDC_RESET);
		if (del <= 0)
			return (0);
		return (1);
	}
}

/*
 * xdcattach: attach controller
 */
void
xdcattach(parent, self, aux)
	struct device *parent, *self;
	void   *aux;

{
	struct xdc_softc *xdc = (void *) self;
	struct confargs *ca = aux;
	struct xdc_attach_args xa;
	int     lcv, rqno, err, pri;
	struct xd_iopb_ctrl *ctl;

	/* get addressing and intr level stuff from autoconfig and load it
	 * into our xdc_softc. */

	ca->ca_ra.ra_vaddr = mapiodev(ca->ca_ra.ra_reg, 0, sizeof(struct xdc));

	xdc->xdc = (struct xdc *) ca->ca_ra.ra_vaddr;
	pri = ca->ca_ra.ra_intr[0].int_pri;
	xdc->ipl = pil_to_vme[pri];
	xdc->vector = ca->ca_ra.ra_intr[0].int_vec;
	printf(" pri %d", pri);

	for (lcv = 0; lcv < XDC_MAXDEV; lcv++)
		xdc->sc_drives[lcv] = (struct xd_softc *) 0;

	/* allocate and zero buffers
	 *
	 * note: we simplify the code by allocating the max number of iopbs and
	 * iorq's up front.   thus, we avoid linked lists and the costs
	 * associated with them in exchange for wasting a little memory. */

	xdc->dvmaiopb = (struct xd_iopb *)
	    dvma_malloc(XDC_MAXIOPB * sizeof(struct xd_iopb), &xdc->iopbase,
	      M_NOWAIT | M_ZERO);
	xdc->iopbase = xdc->dvmaiopb; /* XXX TMP HACK */
	/* Setup device view of DVMA address */
	xdc->dvmaiopb = (struct xd_iopb *) ((u_long) xdc->iopbase - DVMA_BASE);

	xdc->reqs = malloc(XDC_MAXIOPB * sizeof(struct xd_iorq), M_DEVBUF,
	    M_NOWAIT | M_ZERO);
	if (xdc->reqs == NULL)
		panic("xdc malloc");

	/* init free list, iorq to iopb pointers, and non-zero fields in the
	 * iopb which never change. */

	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
		xdc->reqs[lcv].iopb = &xdc->iopbase[lcv];
		xdc->freereq[lcv] = lcv;
		xdc->iopbase[lcv].fixd = 1;	/* always the same */
		xdc->iopbase[lcv].naddrmod = XDC_ADDRMOD; /* always the same */
		xdc->iopbase[lcv].intr_vec = xdc->vector; /* always the same */
	}
	xdc->nfree = XDC_MAXIOPB;
	xdc->nrun = 0;
	xdc->waithead = xdc->waitend = xdc->nwait = 0;
	xdc->ndone = 0;

	/* init queue of waiting bufs */
	bufq_init(&xdc->sc_bufq, BUFQ_FIFO);

	/*
	 * section 7 of the manual tells us how to init the controller:
	 * - read controller parameters (6/0)
	 * - write controller parameters (5/0)
	 */

	/* read controller parameters and insure we have a 753/7053 */

	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
	if (rqno == XD_ERR_FAIL) {
		printf(": couldn't read controller params\n");
		return;		/* shouldn't ever happen */
	}
	ctl = (struct xd_iopb_ctrl *) & xdc->iopbase[rqno];
	if (ctl->ctype != XDCT_753) {
		if (xdc->reqs[rqno].errno)
			printf(": %s: ", xdc_e2str(xdc->reqs[rqno].errno));
		printf(": doesn't identify as a 753/7053\n");
		XDC_DONE(xdc, rqno, err);
		return;
	}
	printf(": Xylogics 753/7053, PROM=0x%x.%02x.%02x\n",
	    ctl->eprom_partno, ctl->eprom_lvl, ctl->eprom_rev);
	XDC_DONE(xdc, rqno, err);

	/* now write controller parameters (xdc_cmd sets all params for us) */

	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
	XDC_DONE(xdc, rqno, err);
	if (err) {
		printf("%s: controller config error: %s\n",
			xdc->sc_dev.dv_xname, xdc_e2str(err));
		return;
	}
	/* link in interrupt with higher level software */

	xdc->sc_ih.ih_fun = xdcintr;
	xdc->sc_ih.ih_arg = xdc;
	vmeintr_establish(ca->ca_ra.ra_intr[0].int_vec,
	    ca->ca_ra.ra_intr[0].int_pri, &xdc->sc_ih, IPL_BIO,
	    self->dv_xname);

	/* now we must look for disks using autoconfig */
	xa.dvmabuf = (char *)dvma_malloc(XDFM_BPS, &xa.buf, M_NOWAIT);
	xa.fullmode = XD_SUB_POLL;
	xa.booting = 1;

	if (ca->ca_ra.ra_bp && ca->ca_ra.ra_bp->val[0] == -1 &&
	    ca->ca_ra.ra_bp->val[1] == xdc->sc_dev.dv_unit) {
		bootpath_store(1, ca->ca_ra.ra_bp + 1); /* advance bootpath */
	}

	for (xa.driveno = 0; xa.driveno < XDC_MAXDEV; xa.driveno++)
		(void) config_found(self, (void *) &xa, NULL);

	dvma_free(xa.dvmabuf, XDFM_BPS, &xa.buf);
	bootpath_store(1, NULL);

	/* start the watchdog clock */
	timeout_set(&xdc->xdc_tick_tmo, xdc_tick, xdc);
	timeout_add(&xdc->xdc_tick_tmo, XDC_TICKCNT);

}

/*
 * xdmatch: probe for disk.
 *
 * note: we almost always say disk is present.   this allows us to
 * spin up and configure a disk after the system is booted (we can
 * call xdattach!).
 */
int
xdmatch(parent, vcf, aux)
	struct device *parent;
	void *vcf, *aux;
{
	struct cfdata *cf = vcf;
	struct xdc_attach_args *xa = aux;

	/* looking for autoconf wildcard or exact match */

	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != xa->driveno)
		return 0;

	return 1;

}

/*
 * xdattach: attach a disk.   this can be called from autoconf and also
 * from xdopen/xdstrategy.
 */
void
xdattach(parent, self, aux)
	struct device *parent, *self;
	void   *aux;

{
	struct xd_softc *xd = (void *) self;
	struct xdc_softc *xdc = (void *) parent;
	struct xdc_attach_args *xa = aux;
	int     rqno, err, spt = 0, mb, blk, lcv, fmode, s = 0, newstate;
	struct xd_iopb_drive *driopb;
	struct dkbad *dkb;
	struct bootpath *bp;

	/*
	 * Always re-initialize the disk structure.  We want statistics
	 * to start with a clean slate.
	 */
	bzero(&xd->sc_dk, sizeof(xd->sc_dk));
	xd->sc_dk.dk_name = xd->sc_dev.dv_xname;

	/* if booting, init the xd_softc */

	if (xa->booting) {
		xd->state = XD_DRIVE_UNKNOWN;	/* to start */
		xd->flags = 0;
		xd->parent = xdc;
	}
	xd->xd_drive = xa->driveno;
	fmode = xa->fullmode;
	xdc->sc_drives[xa->driveno] = xd;

	/* if not booting, make sure we are the only process in the attach for
	 * this drive.   if locked out, sleep on it. */

	if (!xa->booting) {
		s = splbio();
		while (xd->state == XD_DRIVE_ATTACHING) {
			if (tsleep(&xd->state, PRIBIO, "xdattach", 0)) {
				splx(s);
				return;
			}
		}
		printf("%s at %s",
			xd->sc_dev.dv_xname, xd->parent->sc_dev.dv_xname);
	}
	/* we now have control */

	xd->state = XD_DRIVE_ATTACHING;
	newstate = XD_DRIVE_UNKNOWN;

	/* first try and reset the drive */

	rqno = xdc_cmd(xdc, XDCMD_RST, 0, xd->xd_drive, 0, 0, 0, fmode);
	XDC_DONE(xdc, rqno, err);
	if (err == XD_ERR_NRDY) {
		printf(" drive %d: off-line\n", xa->driveno);
		goto done;
	}
	if (err) {
		printf(": ERROR 0x%02x (%s)\n", err, xdc_e2str(err));
		goto done;
	}
	printf(" drive %d: ready\n", xa->driveno);

	/* now set format parameters */

	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_FMT, xd->xd_drive, 0, 0, 0, fmode);
	XDC_DONE(xdc, rqno, err);
	if (err) {
		printf("%s: write format parameters failed: %s\n",
			xd->sc_dev.dv_xname, xdc_e2str(err));
		goto done;
	}

	/* get drive parameters */
	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
	if (rqno != XD_ERR_FAIL) {
		driopb = (struct xd_iopb_drive *) & xdc->iopbase[rqno];
		spt = driopb->sectpertrk;
	}
	XDC_DONE(xdc, rqno, err);
	if (err) {
		printf("%s: read drive parameters failed: %s\n",
			xd->sc_dev.dv_xname, xdc_e2str(err));
		goto done;
	}

	/*
	 * now set drive parameters (to semi-bogus values) so we can read the
	 * disk label.
	 */
	xd->pcyl = xd->ncyl = 1;
	xd->acyl = 0;
	xd->nhead = 1;
	xd->nsect = 1;
	xd->sectpercyl = 1;
	for (lcv = 0; lcv < NBT_BAD; lcv++)	/* init empty bad144 table */
		xd->dkb.bt_bad[lcv].bt_cyl = xd->dkb.bt_bad[lcv].bt_trksec = 0xffff;
	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
	XDC_DONE(xdc, rqno, err);
	if (err) {
		printf("%s: write drive parameters failed: %s\n",
			xd->sc_dev.dv_xname, xdc_e2str(err));
		goto done;
	}

	/* read disk label */
	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, 0, 1, xa->dvmabuf, fmode);
	XDC_DONE(xdc, rqno, err);
	if (err) {
		printf("%s: reading disk label failed: %s\n",
			xd->sc_dev.dv_xname, xdc_e2str(err));
		goto done;
	}
	newstate = XD_DRIVE_NOLABEL;

	xd->hw_spt = spt;
	/* Attach the disk: must be before getdisklabel to malloc label */
	disk_attach(&xd->sc_dev, &xd->sc_dk);

	xd->xd_labeldata = xa->buf;
	if (xdgetdisklabel(MAKEDISKDEV(0, xd->sc_dev.dv_unit, 0), xd,
	    xd->sc_dk.dk_label, 0) != 0) {
		printf("%s: no label, unknown geometry\n", xd->sc_dev.dv_xname);
		goto done;
	}

	/* inform the user of what is up */
	printf("%s: <%s>, pcyl %d, hw_spt %d\n", xd->sc_dev.dv_xname,
		xa->buf, xd->pcyl, spt);
	mb = xd->ncyl * (xd->nhead * xd->nsect) / (1048576 / XDFM_BPS);
	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
		xd->sc_dev.dv_xname, mb, xd->ncyl, xd->nhead, xd->nsect,
		XDFM_BPS);

	/* now set the real drive parameters! */

	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive, 0, 0, 0, fmode);
	XDC_DONE(xdc, rqno, err);
	if (err) {
		printf("%s: write real drive parameters failed: %s\n",
			xd->sc_dev.dv_xname, xdc_e2str(err));
		goto done;
	}
	newstate = XD_DRIVE_ONLINE;

	/*
	 * read bad144 table. this table resides on the first sector of the
	 * last track of the disk (i.e. second cyl of "acyl" area).
	 */

	blk = (xd->ncyl + xd->acyl - 1) * (xd->nhead * xd->nsect) + /* last cyl */
	    (xd->nhead - 1) * xd->nsect;	/* last head */
	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive, blk, 1, xa->dvmabuf, fmode);
	XDC_DONE(xdc, rqno, err);
	if (err) {
		printf("%s: reading bad144 failed: %s\n",
			xd->sc_dev.dv_xname, xdc_e2str(err));
		goto done;
	}

	/* check dkbad for sanity */
	dkb = (struct dkbad *) xa->buf;
	for (lcv = 0; lcv < NBT_BAD; lcv++) {
		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
				dkb->bt_bad[lcv].bt_cyl == 0) &&
		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
			continue;	/* blank */
		if (dkb->bt_bad[lcv].bt_cyl >= xd->ncyl)
			break;
		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xd->nhead)
			break;
		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xd->nsect)
			break;
	}
	if (lcv != NBT_BAD) {
		printf("%s: warning: invalid bad144 sector!\n",
			xd->sc_dev.dv_xname);
	} else {
		bcopy(xa->buf, &xd->dkb, XDFM_BPS);
	}

	if (xa->booting) {
		/* restore bootpath! (do this via attach_args again?)*/
		bp = bootpath_store(0, NULL);
		if (bp && strcmp("xd", bp->name) == 0 &&
						xd->xd_drive == bp->val[0])
			bp->dev = &xd->sc_dev;
	}

done:
	xd->state = newstate;
	if (!xa->booting) {
		wakeup(&xd->state);
		splx(s);
	}
}

/*
 * end of autoconfig functions
 */

/*
 * { b , c } d e v s w   f u n c t i o n s
 */

/*
 * xdclose: close device
 */
int
xdclose(dev, flag, fmt, p)
	dev_t   dev;
	int     flag, fmt;
	struct proc *p;
{
	struct xd_softc *xd = xd_cd.cd_devs[DISKUNIT(dev)];
	int     part = DISKPART(dev);

	/* clear mask bits */

	switch (fmt) {
	case S_IFCHR:
		xd->sc_dk.dk_copenmask &= ~(1 << part);
		break;
	case S_IFBLK:
		xd->sc_dk.dk_bopenmask &= ~(1 << part);
		break;
	}
	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;

	return 0;
}

/*
 * xddump: crash dump system
 */
int
xddump(dev, blkno, va, size)
	dev_t dev;
	daddr_t blkno;
	caddr_t va;
	size_t size;
{
	int     unit, part;
	struct xd_softc *xd;

	unit = DISKUNIT(dev);
	if (unit >= xd_cd.cd_ndevs)
		return ENXIO;
	part = DISKPART(dev);

	xd = xd_cd.cd_devs[unit];

	printf("%s%c: crash dump not supported (yet)\n", xd->sc_dev.dv_xname,
	    'a' + part);

	return ENXIO;
}

/*
 * xdioctl: ioctls on XD drives.   based on ioctl's of other netbsd disks.
 */
int
xdioctl(dev, command, addr, flag, p)
	dev_t   dev;
	u_long  command;
	caddr_t addr;
	int     flag;
	struct proc *p;

{
	struct xd_softc *xd;
	int     error, s, unit;

	unit = DISKUNIT(dev);

	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
		return (ENXIO);

	/* switch on ioctl type */

	switch (command) {
	case DIOCSBAD:		/* set bad144 info */
		if ((flag & FWRITE) == 0)
			return EBADF;
		s = splbio();
		bcopy(addr, &xd->dkb, sizeof(xd->dkb));
		splx(s);
		return 0;

	case DIOCGPDINFO:
		xdgetdisklabel(dev, xd, (struct disklabel *)addr, 1);
		return 0;

	case DIOCGDINFO:	/* get disk label */
		bcopy(xd->sc_dk.dk_label, addr, sizeof(struct disklabel));
		return 0;

	case DIOCGPART:	/* get partition info */
		((struct partinfo *)addr)->disklab = xd->sc_dk.dk_label;
		((struct partinfo *)addr)->part =
		    &xd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
		return 0;

	case DIOCWDINFO:	/* write disk label */
	case DIOCSDINFO:	/* set disk label */
		if ((flag & FWRITE) == 0)
			return EBADF;
		error = setdisklabel(xd->sc_dk.dk_label,
		    (struct disklabel *)addr, /* xd->sc_dk.dk_openmask : */ 0);
		if (error == 0) {
			if (xd->state == XD_DRIVE_NOLABEL)
				xd->state = XD_DRIVE_ONLINE;

			if (command == DIOCWDINFO) {
				/*
				 * Simulate opening partition 0 so write
				 * succeeds.
				 */
				xd->sc_dk.dk_openmask |= (1 << 0);
				error = writedisklabel(DISKLABELDEV(dev),
				    xdstrategy, xd->sc_dk.dk_label);
				xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask |
				    xd->sc_dk.dk_bopenmask;
			}
		}
		return error;

	default:
		return ENOTTY;
	}
}
/*
 * xdopen: open drive
 */

int
xdopen(dev, flag, fmt, p)
	dev_t   dev;
	int     flag, fmt;
	struct proc *p;
{
	int     unit, part;
	struct xd_softc *xd;
	struct xdc_attach_args xa;

	/* first, could it be a valid target? */

	unit = DISKUNIT(dev);
	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == NULL)
		return (ENXIO);
	part = DISKPART(dev);

	/* do we need to attach the drive? */

	if (xd->state == XD_DRIVE_UNKNOWN) {
		xa.driveno = xd->xd_drive;
		xa.dvmabuf = (char *)dvma_malloc(XDFM_BPS, &xa.buf, M_NOWAIT);
		xa.fullmode = XD_SUB_WAIT;
		xa.booting = 0;
		xdattach((struct device *) xd->parent, (struct device *) xd, &xa);
		dvma_free(xa.dvmabuf, XDFM_BPS, &xa.buf);
		if (xd->state == XD_DRIVE_UNKNOWN) {
			return (EIO);
		}
	}
	/* check for partition */

	if (part != RAW_PART &&
	    (part >= xd->sc_dk.dk_label->d_npartitions ||
		xd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
		return (ENXIO);
	}
	/* set open masks */

	switch (fmt) {
	case S_IFCHR:
		xd->sc_dk.dk_copenmask |= (1 << part);
		break;
	case S_IFBLK:
		xd->sc_dk.dk_bopenmask |= (1 << part);
		break;
	}
	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;

	return 0;
}

int
xdread(dev, uio, flags)
	dev_t   dev;
	struct uio *uio;
	int flags;
{

	return (physio(xdstrategy, dev, B_READ, minphys, uio));
}

int
xdwrite(dev, uio, flags)
	dev_t   dev;
	struct uio *uio;
	int flags;
{

	return (physio(xdstrategy, dev, B_WRITE, minphys, uio));
}

/*
 * xdsize: return size of a partition for a dump
 */

daddr_t
xdsize(dev)
	dev_t   dev;

{
	struct xd_softc *xdsc;
	int     unit, part, omask;
	daddr_t size;

	/* valid unit? */
	unit = DISKUNIT(dev);
	if (unit >= xd_cd.cd_ndevs || (xdsc = xd_cd.cd_devs[unit]) == NULL)
		return (-1);

	part = DISKPART(dev);
	omask = xdsc->sc_dk.dk_openmask & (1 << part);

	if (omask == 0 && xdopen(dev, 0, S_IFBLK, NULL) != 0)
		return (-1);

	/* do it */
	if (xdsc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
		size = -1;	/* only give valid size for swap partitions */
	else
		size = DL_GETPSIZE(&xdsc->sc_dk.dk_label->d_partitions[part]) *
		    (xdsc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
	if (omask == 0 && xdclose(dev, 0, S_IFBLK, NULL) != 0)
		return (-1);
	return (size);
}
/*
 * xdstrategy: buffering system interface to xd.
 */

void
xdstrategy(bp)
	struct buf *bp;

{
	struct xd_softc *xd;
	struct xdc_softc *parent;
	int     s, unit;
	struct xdc_attach_args xa;

	unit = DISKUNIT(bp->b_dev);

	/* check for live device */

	if (unit >= xd_cd.cd_ndevs || (xd = xd_cd.cd_devs[unit]) == 0) {
		bp->b_error = EINVAL;
		goto bad;
	}
	/* do we need to attach the drive? */

	if (xd->state == XD_DRIVE_UNKNOWN) {
		xa.driveno = xd->xd_drive;
		xa.dvmabuf = (char *)dvma_malloc(XDFM_BPS, &xa.buf, M_NOWAIT);
		xa.fullmode = XD_SUB_WAIT;
		xa.booting = 0;
		xdattach((struct device *)xd->parent, (struct device *)xd, &xa);
		dvma_free(xa.dvmabuf, XDFM_BPS, &xa.buf);
		if (xd->state == XD_DRIVE_UNKNOWN) {
			bp->b_error = EIO;
			goto bad;
		}
	}
	if (xd->state != XD_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
		/* no I/O to unlabeled disks, unless raw partition */
		bp->b_error = EIO;
		goto bad;
	}

	/* Validate the request. */
	if (bounds_check_with_label(bp, xd->sc_dk.dk_label) == -1)
		goto done;

	/*
	 * now we know we have a valid buf structure that we need to do I/O
	 * on.
	 */
	parent = xd->parent;
	bufq_queue(&parent->sc_bufq, bp);

	s = splbio();
	while (parent->nfree > 0 && bufq_peek(&parent->sc_bufq)) {
		if (xdc_startbuf(parent) != XD_ERR_AOK)
			break;
	}
	splx(s);
	return;

 bad:				/* tells upper layers we have an error */
	bp->b_flags |= B_ERROR;
	bp->b_resid = bp->b_bcount;
 done:				/* tells upper layers we are done with this
				 * buf */
	s = splbio();
	biodone(bp);
	splx(s);
}
/*
 * end of {b,c}devsw functions
 */

/*
 * i n t e r r u p t   f u n c t i o n
 *
 * xdcintr: hardware interrupt.
 */
int
xdcintr(v)
	void   *v;

{
	struct xdc_softc *xdcsc = v;

	/* remove as many done IOPBs as possible */

	xdc_remove_iorq(xdcsc);

	/* start any iorq's already waiting */

	xdc_start(xdcsc, XDC_MAXIOPB);

	/* fill up any remaining iorq's with queue'd buffers */

	while (xdcsc->nfree > 0 && bufq_peek(&xdcsc->sc_bufq))
		if (xdc_startbuf(xdcsc) != XD_ERR_AOK)
			break;

	return (1);
}
/*
 * end of interrupt function
 */

/*
 * i n t e r n a l   f u n c t i o n s
 */

/*
 * xdc_rqinit: fill out the fields of an I/O request
 */

inline void
xdc_rqinit(rq, xdc, xd, md, blk, cnt, db, bp)
	struct xd_iorq *rq;
	struct xdc_softc *xdc;
	struct xd_softc *xd;
	int     md;
	u_long  blk;
	int     cnt;
	caddr_t db;
	struct buf *bp;
{
	rq->xdc = xdc;
	rq->xd = xd;
	rq->ttl = XDC_MAXTTL + 10;
	rq->mode = md;
	rq->tries = rq->errno = rq->lasterror = 0;
	rq->blockno = blk;
	rq->sectcnt = cnt;
	rq->dbuf = rq->dbufbase = db;
	rq->buf = bp;
}
/*
 * xdc_rqtopb: load up an IOPB based on an iorq
 */

void
xdc_rqtopb(iorq, iopb, cmd, subfun)
	struct xd_iorq *iorq;
	struct xd_iopb *iopb;
	int     cmd, subfun;

{
	u_long  block, dp;

	/* standard stuff */

	iopb->errs = iopb->done = 0;
	iopb->comm = cmd;
	iopb->errno = iopb->status = 0;
	iopb->subfun = subfun;
	if (iorq->xd)
		iopb->unit = iorq->xd->xd_drive;
	else
		iopb->unit = 0;

	/* check for alternate IOPB format */

	if (cmd == XDCMD_WRP) {
		switch (subfun) {
		case XDFUN_CTL:{
			struct xd_iopb_ctrl *ctrl =
				(struct xd_iopb_ctrl *) iopb;
			iopb->lll = 0;
			iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
					? 0
					: iorq->xdc->ipl;
			ctrl->param_a = XDPA_TMOD | XDPA_DACF;
			ctrl->param_b = XDPB_ROR | XDPB_TDT_3_2USEC;
			ctrl->param_c = XDPC_OVS | XDPC_COP | XDPC_ASR |
					XDPC_RBC | XDPC_ECC2;
			ctrl->throttle = XDC_THROTTLE;
#ifdef __sparc__
			if (CPU_ISSUN4 && cpuinfo.cpu_type == CPUTYP_4_300)
				ctrl->delay = XDC_DELAY_4_300;
			else
				ctrl->delay = XDC_DELAY_SPARC;
#endif
#ifdef sun3
			ctrl->delay = XDC_DELAY_SUN3;
#endif
			break;
			}
		case XDFUN_DRV:{
			struct xd_iopb_drive *drv =
				(struct xd_iopb_drive *)iopb;
			/* we assume that the disk label has the right
			 * info */
			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
				drv->dparam_ipl = (XDC_DPARAM << 3);
			else
				drv->dparam_ipl = (XDC_DPARAM << 3) |
						  iorq->xdc->ipl;
			drv->maxsect = iorq->xd->nsect - 1;
			drv->maxsector = drv->maxsect;
			/* note: maxsector != maxsect only if you are
			 * doing cyl sparing */
			drv->headoff = 0;
			drv->maxcyl = iorq->xd->pcyl - 1;
			drv->maxhead = iorq->xd->nhead - 1;
			break;
			}
		case XDFUN_FMT:{
			struct xd_iopb_format *form =
					(struct xd_iopb_format *) iopb;
			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
				form->interleave_ipl = (XDC_INTERLEAVE << 3);
			else
				form->interleave_ipl = (XDC_INTERLEAVE << 3) |
						       iorq->xdc->ipl;
			form->field1 = XDFM_FIELD1;
			form->field2 = XDFM_FIELD2;
			form->field3 = XDFM_FIELD3;
			form->field4 = XDFM_FIELD4;
			form->bytespersec = XDFM_BPS;
			form->field6 = XDFM_FIELD6;
			form->field7 = XDFM_FIELD7;
			break;
			}
		}
	} else {

		/* normal IOPB case (harmless to RDP command) */

		iopb->lll = 0;
		iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
				? 0
				: iorq->xdc->ipl;
		iopb->sectcnt = iorq->sectcnt;
		block = iorq->blockno;
		if (iorq->xd == NULL || block == 0) {
			iopb->sectno = iopb->headno = iopb->cylno = 0;
		} else {
			iopb->sectno = block % iorq->xd->nsect;
			block = block / iorq->xd->nsect;
			iopb->headno = block % iorq->xd->nhead;
			block = block / iorq->xd->nhead;
			iopb->cylno = block;
		}
		dp = (u_long) iorq->dbuf - DVMA_BASE;
		dp = iopb->daddr = (iorq->dbuf == NULL) ? 0 : dp;
		iopb->addrmod = ((dp + (XDFM_BPS * iorq->sectcnt)) > 0x1000000)
					? XDC_ADDRMOD32
					: XDC_ADDRMOD;
	}
}

/*
 * xdc_cmd: front end for POLL'd and WAIT'd commands.  Returns rqno.
 * If you've already got an IORQ, you can call submit directly (currently
 * there is no need to do this).    NORM requests are handled separately.
 */
int
xdc_cmd(xdcsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
	struct xdc_softc *xdcsc;
	int     cmd, subfn, unit, block, scnt;
	char   *dptr;
	int     fullmode;

{
	int     rqno, submode = XD_STATE(fullmode), retry;
	struct xd_iorq *iorq;
	struct xd_iopb *iopb;

	/* get iorq/iopb */
	switch (submode) {
	case XD_SUB_POLL:
		while (xdcsc->nfree == 0) {
			if (xdc_piodriver(xdcsc, 0, 1) != XD_ERR_AOK)
				return (XD_ERR_FAIL);
		}
		break;
	case XD_SUB_WAIT:
		retry = 1;
		while (retry) {
			while (xdcsc->nfree == 0) {
			    if (tsleep(&xdcsc->nfree, PRIBIO, "xdnfree", 0))
				return (XD_ERR_FAIL);
			}
			while (xdcsc->ndone > XDC_SUBWAITLIM) {
			    if (tsleep(&xdcsc->ndone, PRIBIO, "xdsubwait", 0))
				return (XD_ERR_FAIL);
			}
			if (xdcsc->nfree)
				retry = 0;	/* got it */
		}
		break;
	default:
		return (XD_ERR_FAIL);	/* illegal */
	}
	if (xdcsc->nfree == 0)
		panic("xdcmd nfree");
	rqno = XDC_RQALLOC(xdcsc);
	iorq = &xdcsc->reqs[rqno];
	iopb = iorq->iopb;

	/* init iorq/iopb */

	xdc_rqinit(iorq, xdcsc,
	    (unit == XDC_NOUNIT) ? NULL : xdcsc->sc_drives[unit],
	    fullmode, block, scnt, dptr, NULL);

	/* load IOPB from iorq */

	xdc_rqtopb(iorq, iopb, cmd, subfn);

	/* submit it for processing */

	xdc_submit_iorq(xdcsc, rqno, fullmode);	/* error code will be in iorq */

	return (rqno);
}
/*
 * xdc_startbuf
 * start a buffer running, assumes nfree > 0
 */

int
xdc_startbuf(xdcsc)
	struct xdc_softc *xdcsc;

{
	int     rqno, partno;
	struct xd_softc *xdsc;
	struct xd_iorq *iorq;
	struct xd_iopb *iopb;
	struct buf *bp;
	u_long  block;
	caddr_t dbuf;

	if (!xdcsc->nfree)
		panic("xdc_startbuf free");
	rqno = XDC_RQALLOC(xdcsc);
	iorq = &xdcsc->reqs[rqno];
	iopb = iorq->iopb;

	/* get buf */
	bp = bufq_dequeue(&xdcsc->sc_bufq);
	if (bp == NULL)
		panic("xdc_startbuf bp");

	xdsc = xdcsc->sc_drives[DISKUNIT(bp->b_dev)];
	partno = DISKPART(bp->b_dev);
#ifdef XDC_DEBUG
	printf("xdc_startbuf: %s%c: %s block %lld\n",
	    xdsc->sc_dev.dv_xname, 'a' + partno,
	    (bp->b_flags & B_READ) ? "read" : "write", (long long)bp->b_blkno);
	printf("xdc_startbuf: b_bcount %d, b_data 0x%x\n",
	    bp->b_bcount, bp->b_data);
#endif

	/*
	 * load request.  we have to calculate the correct block number based
	 * on partition info.
	 *
	 * note that iorq points to the buffer as mapped into DVMA space,
	 * where as the bp->b_data points to its non-DVMA mapping.
	 */

	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
	    DL_GETPOFFSET(&xdsc->sc_dk.dk_label->d_partitions[partno]));

	dbuf = kdvma_mapin(bp->b_data, bp->b_bcount, 0);
	if (dbuf == NULL) {	/* out of DVMA space */
		printf("%s: warning: out of DVMA space\n",
			xdcsc->sc_dev.dv_xname);
		XDC_FREE(xdcsc, rqno);
		bufq_queue(&xdcsc->sc_bufq, bp); /* put at end of queue */
		return (XD_ERR_FAIL);	/* XXX: need some sort of
					 * call-back scheme here? */
	}

	/* init iorq and load iopb from it */

	xdc_rqinit(iorq, xdcsc, xdsc, XD_SUB_NORM | XD_MODE_VERBO, block,
	    bp->b_bcount / XDFM_BPS, dbuf, bp);

	xdc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XDCMD_RD : XDCMD_WR, 0);

	/* Instrumentation. */
	disk_busy(&xdsc->sc_dk);

	/* now submit [note that xdc_submit_iorq can never fail on NORM reqs] */

	xdc_submit_iorq(xdcsc, rqno, XD_SUB_NORM);
	return (XD_ERR_AOK);
}

/*
 * xdc_submit_iorq: submit an iorq for processing.  returns XD_ERR_AOK
 * if ok.  if it fail returns an error code.  type is XD_SUB_*.
 *
 * note: caller frees iorq in all cases except NORM
 *
 * return value:
 *   NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
 *   WAIT: XD_AOK (success), <error-code> (failed)
 *   POLL: <same as WAIT>
 *   NOQ : <same as NORM>
 *
 * there are three sources for i/o requests:
 * [1] xdstrategy: normal block I/O, using "struct buf" system.
 * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
 * [3] open/ioctl: these are I/O requests done in the context of a process,
 *                 and the process should block until they are done.
 *
 * software state is stored in the iorq structure.  each iorq has an
 * iopb structure.  the hardware understands the iopb structure.
 * every command must go through an iopb.  a 7053 can only handle
 * XDC_MAXIOPB (31) active iopbs at one time.  iopbs are allocated in
 * DVMA space at boot up time.  what happens if we run out of iopb's?
 * for i/o type [1], the buffers are queued at the "buff" layer and
 * picked up later by the interrupt routine.  for case [2] the
 * programmed i/o driver is called with a special flag that says
 * return when one iopb is free.  for case [3] the process can sleep
 * on the iorq free list until some iopbs are available.
 */

int
xdc_submit_iorq(xdcsc, iorqno, type)
	struct xdc_softc *xdcsc;
	int     iorqno;
	int     type;

{
	u_long  iopbaddr;
	struct xd_iorq *iorq = &xdcsc->reqs[iorqno];

#ifdef XDC_DEBUG
	printf("xdc_submit_iorq(%s, no=%d, type=%d)\n", xdcsc->sc_dev.dv_xname,
	    iorqno, type);
#endif

	/* first check and see if controller is busy */
	if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
#ifdef XDC_DEBUG
		printf("xdc_submit_iorq: XDC not ready (ADDING)\n");
#endif
		if (type == XD_SUB_NOQ)
			return (XD_ERR_FAIL);	/* failed */
		XDC_TWAIT(xdcsc, iorqno);	/* put at end of waitq */
		switch (type) {
		case XD_SUB_NORM:
			return XD_ERR_AOK;	/* success */
		case XD_SUB_WAIT:
			while (iorq->iopb->done == 0) {
				tsleep(iorq, PRIBIO, "xdiorq", 0);
			}
			return (iorq->errno);
		case XD_SUB_POLL:
			return (xdc_piodriver(xdcsc, iorqno, 0));
		default:
			panic("xdc_submit_iorq adding");
		}
	}
#ifdef XDC_DEBUG
	{
		u_char *rio = (u_char *) iorq->iopb;
		int     sz = sizeof(struct xd_iopb), lcv;
		printf("%s: aio #%d [",
			xdcsc->sc_dev.dv_xname, iorq - xdcsc->reqs);
		for (lcv = 0; lcv < sz; lcv++)
			printf(" %02x", rio[lcv]);
		printf("]\n");
	}
#endif				/* XDC_DEBUG */

	/* controller not busy, start command */
	iopbaddr = (u_long) iorq->iopb - (u_long) DVMA_BASE;
	XDC_GO(xdcsc->xdc, iopbaddr);	/* go! */
	xdcsc->nrun++;
	/* command now running, wrap it up */
	switch (type) {
	case XD_SUB_NORM:
	case XD_SUB_NOQ:
		return (XD_ERR_AOK);	/* success */
	case XD_SUB_WAIT:
		while (iorq->iopb->done == 0) {
			tsleep(iorq, PRIBIO, "xdiorq", 0);
		}
		return (iorq->errno);
	case XD_SUB_POLL:
		return (xdc_piodriver(xdcsc, iorqno, 0));
	default:
		panic("xdc_submit_iorq wrap up");
	}
	panic("xdc_submit_iorq");
	return 0;	/* not reached */
}

/*
 * xdc_piodriver
 *
 * programmed i/o driver.   this function takes over the computer
 * and drains off all i/o requests.   it returns the status of the iorq
 * the caller is interesting in.   if freeone is true, then it returns
 * when there is a free iorq.
 */
int
xdc_piodriver(xdcsc, iorqno, freeone)
	struct xdc_softc *xdcsc;
	int    iorqno;
	int    freeone;

{
	int     nreset = 0;
	int     retval = 0;
	u_long  count;
	struct xdc *xdc = xdcsc->xdc;
#ifdef XDC_DEBUG
	printf("xdc_piodriver(%s, %d, freeone=%d)\n", xdcsc->sc_dev.dv_xname,
	    iorqno, freeone);
#endif

	while (xdcsc->nwait || xdcsc->nrun) {
#ifdef XDC_DEBUG
		printf("xdc_piodriver: wait=%d, run=%d\n",
			xdcsc->nwait, xdcsc->nrun);
#endif
		XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
#ifdef XDC_DEBUG
		printf("xdc_piodriver: done wait with count = %d\n", count);
#endif
		/* we expect some progress soon */
		if (count == 0 && nreset >= 2) {
			xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
#ifdef XDC_DEBUG
			printf("xdc_piodriver: timeout\n");
#endif
			return (XD_ERR_FAIL);
		}
		if (count == 0) {
			if (xdc_reset(xdcsc, 0,
				      (nreset++ == 0) ? XD_RSET_NONE : iorqno,
				      XD_ERR_FAIL,
				      0) == XD_ERR_FAIL)
				return (XD_ERR_FAIL);	/* flushes all but POLL
							 * requests, resets */
			continue;
		}
		xdc_remove_iorq(xdcsc);	/* could resubmit request */
		if (freeone) {
			if (xdcsc->nrun < XDC_MAXIOPB) {
#ifdef XDC_DEBUG
				printf("xdc_piodriver: done: one free\n");
#endif
				return (XD_ERR_AOK);
			}
			continue;	/* don't xdc_start */
		}
		xdc_start(xdcsc, XDC_MAXIOPB);
	}

	/* get return value */

	retval = xdcsc->reqs[iorqno].errno;

#ifdef XDC_DEBUG
	printf("xdc_piodriver: done, retval = 0x%x (%s)\n",
	    xdcsc->reqs[iorqno].errno, xdc_e2str(xdcsc->reqs[iorqno].errno));
#endif

	/* now that we've drained everything, start up any bufs that have
	 * queued */

	while (xdcsc->nfree > 0 && bufq_peek(&xdcsc->sc_bufq))
		if (xdc_startbuf(xdcsc) != XD_ERR_AOK)
			break;

	return (retval);
}

/*
 * xdc_reset: reset one drive.   NOTE: assumes xdc was just reset.
 * we steal iopb[0] for this, but we put it back when we are done.
 */
void
xdc_xdreset(xdcsc, xdsc)
	struct xdc_softc *xdcsc;
	struct xd_softc *xdsc;

{
	struct xd_iopb tmpiopb;
	u_long  addr;
	int     del;
	bcopy(xdcsc->iopbase, &tmpiopb, sizeof(tmpiopb));
	bzero(xdcsc->iopbase, sizeof(tmpiopb));
	xdcsc->iopbase->comm = XDCMD_RST;
	xdcsc->iopbase->unit = xdsc->xd_drive;
	addr = (u_long) xdcsc->dvmaiopb;
	XDC_GO(xdcsc->xdc, addr);	/* go! */
	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_REMIOPB);
	if (del <= 0 || xdcsc->iopbase->errs) {
		printf("%s: off-line: %s\n", xdcsc->sc_dev.dv_xname,
		    xdc_e2str(xdcsc->iopbase->errno));
		xdcsc->xdc->xdc_csr = XDC_RESET;
		XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
		if (del <= 0)
			panic("xdc_reset");
	} else {
		xdcsc->xdc->xdc_csr = XDC_CLRRIO;	/* clear RIO */
	}
	bcopy(&tmpiopb, xdcsc->iopbase, sizeof(tmpiopb));
}

/*
 * xdc_reset: reset everything: requests are marked as errors except
 * a polled request (which is resubmitted)
 */
int
xdc_reset(xdcsc, quiet, blastmode, error, xdsc)
	struct xdc_softc *xdcsc;
	int     quiet, blastmode, error;
	struct xd_softc *xdsc;

{
	int     del = 0, lcv, retval = XD_ERR_AOK;
	int     oldfree = xdcsc->nfree;

	/* soft reset hardware */

	if (!quiet)
		printf("%s: soft reset\n", xdcsc->sc_dev.dv_xname);
	xdcsc->xdc->xdc_csr = XDC_RESET;
	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
	if (del <= 0) {
		blastmode = XD_RSET_ALL;	/* dead, flush all requests */
		retval = XD_ERR_FAIL;
	}
	if (xdsc)
		xdc_xdreset(xdcsc, xdsc);

	/* fix queues based on "blast-mode" */

	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
		struct xd_iorq *iorq = &xdcsc->reqs[lcv];

		if (XD_STATE(iorq->mode) != XD_SUB_POLL &&
		    XD_STATE(iorq->mode) != XD_SUB_WAIT &&
		    XD_STATE(iorq->mode) != XD_SUB_NORM)
			/* is it active? */
			continue;

		xdcsc->nrun--;	/* it isn't running any more */
		if (blastmode == XD_RSET_ALL || blastmode != lcv) {
			/* failed */
			iorq->errno = error;
			xdcsc->iopbase[lcv].done = xdcsc->iopbase[lcv].errs = 1;
			switch (XD_STATE(xdcsc->reqs[lcv].mode)) {
			case XD_SUB_NORM:
			    iorq->buf->b_error = EIO;
			    iorq->buf->b_flags |= B_ERROR;
			    iorq->buf->b_resid =
			       iorq->sectcnt * XDFM_BPS;
			    dvma_mapout((vaddr_t)iorq->dbufbase,
				    (vaddr_t)iorq->buf->b_data,
				    iorq->buf->b_bcount);
			    disk_unbusy(&xdcsc->reqs[lcv].xd->sc_dk,
				(xdcsc->reqs[lcv].buf->b_bcount -
				xdcsc->reqs[lcv].buf->b_resid),
				(xdcsc->reqs[lcv].buf->b_flags & B_READ));
			    biodone(iorq->buf);
			    XDC_FREE(xdcsc, lcv);	/* add to free list */
			    break;
			case XD_SUB_WAIT:
			    wakeup(iorq);
			case XD_SUB_POLL:
			    xdcsc->ndone++;
			    iorq->mode =
				XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
			    break;
			}

		} else {

			/* resubmit, put at front of wait queue */
			XDC_HWAIT(xdcsc, lcv);
		}
	}

	/*
	 * now, if stuff is waiting, start it.
	 * since we just reset it should go
	 */
	xdc_start(xdcsc, XDC_MAXIOPB);

	/* ok, we did it */
	if (oldfree == 0 && xdcsc->nfree)
		wakeup(&xdcsc->nfree);

#ifdef XDC_DIAG
	del = xdcsc->nwait + xdcsc->nrun + xdcsc->nfree + xdcsc->ndone;
	if (del != XDC_MAXIOPB)
		printf("%s: diag: xdc_reset miscount (%d should be %d)!\n",
		    xdcsc->sc_dev.dv_xname, del, XDC_MAXIOPB);
	else
		if (xdcsc->ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
			printf("%s: diag: lots of done jobs (%d)\n",
			    xdcsc->sc_dev.dv_xname, xdcsc->ndone);
#endif
	printf("RESET DONE\n");
	return (retval);
}
/*
 * xdc_start: start all waiting buffers
 */

void
xdc_start(xdcsc, maxio)
	struct xdc_softc *xdcsc;
	int     maxio;

{
	int     rqno;
	while (maxio && xdcsc->nwait &&
		(xdcsc->xdc->xdc_csr & XDC_ADDING) == 0) {
		XDC_GET_WAITER(xdcsc, rqno);	/* note: rqno is an "out"
						 * param */
		if (xdc_submit_iorq(xdcsc, rqno, XD_SUB_NOQ) != XD_ERR_AOK)
			panic("xdc_start");	/* should never happen */
		maxio--;
	}
}
/*
 * xdc_remove_iorq: remove "done" IOPB's.
 */

int
xdc_remove_iorq(xdcsc)
	struct xdc_softc *xdcsc;

{
	int     errno, rqno, comm, errs;
	struct xdc *xdc = xdcsc->xdc;
	struct xd_iopb *iopb;
	struct xd_iorq *iorq;
	struct buf *bp;

	if (xdc->xdc_csr & XDC_F_ERROR) {
		/*
		 * FATAL ERROR: should never happen under normal use. This
		 * error is so bad, you can't even tell which IOPB is bad, so
		 * we dump them all.
		 */
		errno = xdc->xdc_f_err;
		printf("%s: fatal error 0x%02x: %s\n", xdcsc->sc_dev.dv_xname,
		    errno, xdc_e2str(errno));
		if (xdc_reset(xdcsc, 0, XD_RSET_ALL, errno, 0) != XD_ERR_AOK) {
			printf("%s: soft reset failed!\n",
				xdcsc->sc_dev.dv_xname);
			panic("xdc_remove_iorq: controller DEAD");
		}
		return (XD_ERR_AOK);
	}

	/*
	 * get iopb that is done
	 *
	 * hmm... I used to read the address of the done IOPB off the VME
	 * registers and calculate the rqno directly from that.   that worked
	 * until I started putting a load on the controller.   when loaded, i
	 * would get interrupts but neither the REMIOPB or F_ERROR bits would
	 * be set, even after DELAY'ing a while!   later on the timeout
	 * routine would detect IOPBs that were marked "running" but their
	 * "done" bit was set.   rather than dealing directly with this
	 * problem, it is just easier to look at all running IOPB's for the
	 * done bit.
	 */
	if (xdc->xdc_csr & XDC_REMIOPB) {
		xdc->xdc_csr = XDC_CLRRIO;
	}

	for (rqno = 0; rqno < XDC_MAXIOPB; rqno++) {
		iorq = &xdcsc->reqs[rqno];
		if (iorq->mode == 0 || XD_STATE(iorq->mode) == XD_SUB_DONE)
			continue;	/* free, or done */
		iopb = &xdcsc->iopbase[rqno];
		if (iopb->done == 0)
			continue;	/* not done yet */

#ifdef XDC_DEBUG
		{
			u_char *rio = (u_char *) iopb;
			int     sz = sizeof(struct xd_iopb), lcv;
			printf("%s: rio #%d [", xdcsc->sc_dev.dv_xname, rqno);
			for (lcv = 0; lcv < sz; lcv++)
				printf(" %02x", rio[lcv]);
			printf("]\n");
		}
#endif				/* XDC_DEBUG */

		xdcsc->nrun--;

		comm = iopb->comm;
		errs = iopb->errs;

		if (errs)
			iorq->errno = iopb->errno;
		else
			iorq->errno = 0;

		/* handle non-fatal errors */

		if (errs &&
		    xdc_error(xdcsc, iorq, iopb, rqno, comm) == XD_ERR_AOK)
			continue;	/* AOK: we resubmitted it */

		/* this iorq is now done (hasn't been restarted or anything) */

		if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
			xdc_perror(iorq, iopb, 0);

		/* now, if read/write check to make sure we got all the data
		 * we needed. (this may not be the case if we got an error in
		 * the middle of a multisector request).   */

		if ((iorq->mode & XD_MODE_B144) != 0 && errs == 0 &&
		    (comm == XDCMD_RD || comm == XDCMD_WR)) {
			/* we just successfully processed a bad144 sector
			 * note: if we are in bad 144 mode, the pointers have
			 * been advanced already (see above) and are pointing
			 * at the bad144 sector.   to exit bad144 mode, we
			 * must advance the pointers 1 sector and issue a new
			 * request if there are still sectors left to process
			 *
			 */
			XDC_ADVANCE(iorq, 1);	/* advance 1 sector */

			/* exit b144 mode */
			iorq->mode = iorq->mode & (~XD_MODE_B144);

			if (iorq->sectcnt) {	/* more to go! */
				iorq->lasterror = iorq->errno = iopb->errno = 0;
				iopb->errs = iopb->done = 0;
				iorq->tries = 0;
				iopb->sectcnt = iorq->sectcnt;
				iopb->cylno = iorq->blockno /
						iorq->xd->sectpercyl;
				iopb->headno =
					(iorq->blockno / iorq->xd->nhead) %
						iorq->xd->nhead;
				iopb->sectno = iorq->blockno % iorq->xd->nsect;
				iopb->daddr = (u_long) iorq->dbuf - DVMA_BASE;
				XDC_HWAIT(xdcsc, rqno);
				xdc_start(xdcsc, 1);	/* resubmit */
				continue;
			}
		}
		/* final cleanup, totally done with this request */

		switch (XD_STATE(iorq->mode)) {
		case XD_SUB_NORM:
			bp = iorq->buf;
			if (errs) {
				bp->b_error = EIO;
				bp->b_flags |= B_ERROR;
				bp->b_resid = iorq->sectcnt * XDFM_BPS;
			} else {
				bp->b_resid = 0;	/* done */
			}
			dvma_mapout((vaddr_t) iorq->dbufbase,
				    (vaddr_t) bp->b_data,
				    bp->b_bcount);
			disk_unbusy(&iorq->xd->sc_dk,
			    (bp->b_bcount - bp->b_resid),
			    (bp->b_flags & B_READ));
			XDC_FREE(xdcsc, rqno);
			biodone(bp);
			break;
		case XD_SUB_WAIT:
			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
			xdcsc->ndone++;
			wakeup(iorq);
			break;
		case XD_SUB_POLL:
			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
			xdcsc->ndone++;
			break;
		}
	}

	return (XD_ERR_AOK);
}

/*
 * xdc_perror: print error.
 * - if still_trying is true: we got an error, retried and got a
 *   different error.  in that case lasterror is the old error,
 *   and errno is the new one.
 * - if still_trying is not true, then if we ever had an error it
 *   is in lasterror. also, if iorq->errno == 0, then we recovered
 *   from that error (otherwise iorq->errno == iorq->lasterror).
 */
void
xdc_perror(iorq, iopb, still_trying)
	struct xd_iorq *iorq;
	struct xd_iopb *iopb;
	int     still_trying;

{

	int     error = iorq->lasterror;

	printf("%s", (iorq->xd) ? iorq->xd->sc_dev.dv_xname
	    : iorq->xdc->sc_dev.dv_xname);
	if (iorq->buf)
		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
	if (iopb->comm == XDCMD_RD || iopb->comm == XDCMD_WR)
		printf("%s %d/%d/%d: ",
			(iopb->comm == XDCMD_RD) ? "read" : "write",
			iopb->cylno, iopb->headno, iopb->sectno);
	printf("%s", xdc_e2str(error));

	if (still_trying)
		printf(" [still trying, new error=%s]", xdc_e2str(iorq->errno));
	else
		if (iorq->errno == 0)
			printf(" [recovered in %d tries]", iorq->tries);

	printf("\n");
}

/*
 * xdc_error: non-fatal error encountered... recover.
 * return AOK if resubmitted, return FAIL if this iopb is done
 */
int
xdc_error(xdcsc, iorq, iopb, rqno, comm)
	struct xdc_softc *xdcsc;
	struct xd_iorq *iorq;
	struct xd_iopb *iopb;
	int     rqno, comm;

{
	int     errno = iorq->errno;
	int     erract = errno & XD_ERA_MASK;
	int     oldmode, advance, i;

	if (erract == XD_ERA_RSET) {	/* some errors require a reset */
		oldmode = iorq->mode;
		iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
		xdcsc->ndone++;
		/* make xdc_start ignore us */
		xdc_reset(xdcsc, 1, XD_RSET_NONE, errno, iorq->xd);
		iorq->mode = oldmode;
		xdcsc->ndone--;
	}
	/* check for read/write to a sector in bad144 table if bad: redirect
	 * request to bad144 area */

	if ((comm == XDCMD_RD || comm == XDCMD_WR) &&
	    (iorq->mode & XD_MODE_B144) == 0) {
		advance = iorq->sectcnt - iopb->sectcnt;
		XDC_ADVANCE(iorq, advance);
		if ((i = isbad(&iorq->xd->dkb, iorq->blockno / iorq->xd->sectpercyl,
			    (iorq->blockno / iorq->xd->nsect) % iorq->xd->nhead,
			    iorq->blockno % iorq->xd->nsect)) != -1) {
			iorq->mode |= XD_MODE_B144;	/* enter bad144 mode &
							 * redirect */
			iopb->errno = iopb->done = iopb->errs = 0;
			iopb->sectcnt = 1;
			iopb->cylno = (iorq->xd->ncyl + iorq->xd->acyl) - 2;
			/* second to last acyl */
			i = iorq->xd->sectpercyl - 1 - i;	/* follow bad144
								 * standard */
			iopb->headno = i % iorq->xd->nhead;
			iopb->sectno = i / iorq->xd->nhead;
			iopb->daddr = (u_long) iorq->dbuf - DVMA_BASE;
			XDC_HWAIT(xdcsc, rqno);
			xdc_start(xdcsc, 1);	/* resubmit */
			return (XD_ERR_AOK);	/* recovered! */
		}
	}

	/*
	 * it isn't a bad144 sector, must be real error! see if we can retry
	 * it?
	 */
	if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
		xdc_perror(iorq, iopb, 1);	/* inform of error state
						 * change */
	iorq->lasterror = errno;

	if ((erract == XD_ERA_RSET || erract == XD_ERA_HARD)
	    && iorq->tries < XDC_MAXTRIES) {	/* retry? */
		iorq->tries++;
		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
		XDC_HWAIT(xdcsc, rqno);
		xdc_start(xdcsc, 1);	/* restart */
		return (XD_ERR_AOK);	/* recovered! */
	}

	/* failed to recover from this error */
	return (XD_ERR_FAIL);
}

/*
 * xdc_tick: make sure xd is still alive and ticking (err, kicking).
 */
void
xdc_tick(arg)
	void   *arg;

{
	struct xdc_softc *xdcsc = arg;
	int     lcv, s, reset = 0;
#ifdef XDC_DIAG
	int     wait, run, free, done, whd = 0;
	u_char  fqc[XDC_MAXIOPB], wqc[XDC_MAXIOPB], mark[XDC_MAXIOPB];
	s = splbio();
	wait = xdcsc->nwait;
	run = xdcsc->nrun;
	free = xdcsc->nfree;
	done = xdcsc->ndone;
	bcopy(xdcsc->waitq, wqc, sizeof(wqc));
	bcopy(xdcsc->freereq, fqc, sizeof(fqc));
	splx(s);
	if (wait + run + free + done != XDC_MAXIOPB) {
		printf("%s: diag: IOPB miscount (got w/f/r/d %d/%d/%d/%d, wanted %d)\n",
		    xdcsc->sc_dev.dv_xname, wait, free, run, done, XDC_MAXIOPB);
		bzero(mark, sizeof(mark));
		printf("FREE: ");
		for (lcv = free; lcv > 0; lcv--) {
			printf("%d ", fqc[lcv - 1]);
			mark[fqc[lcv - 1]] = 1;
		}
		printf("\nWAIT: ");
		lcv = wait;
		while (lcv > 0) {
			printf("%d ", wqc[whd]);
			mark[wqc[whd]] = 1;
			whd = (whd + 1) % XDC_MAXIOPB;
			lcv--;
		}
		printf("\n");
		for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
			if (mark[lcv] == 0)
				printf("MARK: running %d: mode %d done %d errs %d errno 0x%x ttl %d buf %p\n",
				lcv, xdcsc->reqs[lcv].mode,
				xdcsc->iopbase[lcv].done,
				xdcsc->iopbase[lcv].errs,
				xdcsc->iopbase[lcv].errno,
				xdcsc->reqs[lcv].ttl, xdcsc->reqs[lcv].buf);
		}
	} else
		if (done > XDC_MAXIOPB - XDC_SUBWAITLIM)
			printf("%s: diag: lots of done jobs (%d)\n",
				xdcsc->sc_dev.dv_xname, done);

#endif
#ifdef XDC_DEBUG
	printf("%s: tick: csr 0x%x, w/f/r/d %d/%d/%d/%d\n",
		xdcsc->sc_dev.dv_xname,
		xdcsc->xdc->xdc_csr, xdcsc->nwait, xdcsc->nfree, xdcsc->nrun,
		xdcsc->ndone);
	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
		if (xdcsc->reqs[lcv].mode)
		  printf("running %d: mode %d done %d errs %d errno 0x%x\n",
			 lcv,
			 xdcsc->reqs[lcv].mode, xdcsc->iopbase[lcv].done,
			 xdcsc->iopbase[lcv].errs, xdcsc->iopbase[lcv].errno);
	}
#endif

	/* reduce ttl for each request if one goes to zero, reset xdc */
	s = splbio();
	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
		if (xdcsc->reqs[lcv].mode == 0 ||
		    XD_STATE(xdcsc->reqs[lcv].mode) == XD_SUB_DONE)
			continue;
		xdcsc->reqs[lcv].ttl--;
		if (xdcsc->reqs[lcv].ttl == 0) {
			reset = 1;
			break;	/* we're going to fail all requests anyway */
		}
	}
	if (reset) {
		printf("%s: watchdog timeout\n", xdcsc->sc_dev.dv_xname);
		xdc_reset(xdcsc, 0, XD_RSET_NONE, XD_ERR_FAIL, NULL);
	}
	splx(s);

	/* until next time */

	timeout_add(&xdcsc->xdc_tick_tmo, XDC_TICKCNT);
}

/*
 * xdc_e2str: convert error code number into an error string
 */
const char *
xdc_e2str(no)
	int     no;
{
	switch (no) {
	case XD_ERR_FAIL:
		return ("Software fatal error");
	case XD_ERR_AOK:
		return ("Successful completion");
	case XD_ERR_ICYL:
		return ("Illegal cylinder address");
	case XD_ERR_IHD:
		return ("Illegal head address");
	case XD_ERR_ISEC:
		return ("Illgal sector address");
	case XD_ERR_CZER:
		return ("Count zero");
	case XD_ERR_UIMP:
		return ("Unimplemented command");
	case XD_ERR_IF1:
		return ("Illegal field length 1");
	case XD_ERR_IF2:
		return ("Illegal field length 2");
	case XD_ERR_IF3:
		return ("Illegal field length 3");
	case XD_ERR_IF4:
		return ("Illegal field length 4");
	case XD_ERR_IF5:
		return ("Illegal field length 5");
	case XD_ERR_IF6:
		return ("Illegal field length 6");
	case XD_ERR_IF7:
		return ("Illegal field length 7");
	case XD_ERR_ISG:
		return ("Illegal scatter/gather length");
	case XD_ERR_ISPT:
		return ("Not enough sectors per track");
	case XD_ERR_ALGN:
		return ("Next IOPB address alignment error");
	case XD_ERR_SGAL:
		return ("Scatter/gather address alignment error");
	case XD_ERR_SGEC:
		return ("Scatter/gather with auto-ECC");
	case XD_ERR_SECC:
		return ("Soft ECC corrected");
	case XD_ERR_SIGN:
		return ("ECC ignored");
	case XD_ERR_ASEK:
		return ("Auto-seek retry recovered");
	case XD_ERR_RTRY:
		return ("Soft retry recovered");
	case XD_ERR_HECC:
		return ("Hard data ECC");
	case XD_ERR_NHDR:
		return ("Header not found");
	case XD_ERR_NRDY:
		return ("Drive not ready");
	case XD_ERR_TOUT:
		return ("Operation timeout");
	case XD_ERR_VTIM:
		return ("VMEDMA timeout");
	case XD_ERR_DSEQ:
		return ("Disk sequencer error");
	case XD_ERR_HDEC:
		return ("Header ECC error");
	case XD_ERR_RVFY:
		return ("Read verify");
	case XD_ERR_VFER:
		return ("Fatal VMEDMA error");
	case XD_ERR_VBUS:
		return ("VMEbus error");
	case XD_ERR_DFLT:
		return ("Drive faulted");
	case XD_ERR_HECY:
		return ("Header error/cylinder");
	case XD_ERR_HEHD:
		return ("Header error/head");
	case XD_ERR_NOCY:
		return ("Drive not on-cylinder");
	case XD_ERR_SEEK:
		return ("Seek error");
	case XD_ERR_ILSS:
		return ("Illegal sector size");
	case XD_ERR_SEC:
		return ("Soft ECC");
	case XD_ERR_WPER:
		return ("Write-protect error");
	case XD_ERR_IRAM:
		return ("IRAM self test failure");
	case XD_ERR_MT3:
		return ("Maintenance test 3 failure (DSKCEL RAM)");
	case XD_ERR_MT4:
		return ("Maintenance test 4 failure (header shift reg)");
	case XD_ERR_MT5:
		return ("Maintenance test 5 failure (VMEDMA regs)");
	case XD_ERR_MT6:
		return ("Maintenance test 6 failure (REGCEL chip)");
	case XD_ERR_MT7:
		return ("Maintenance test 7 failure (buffer parity)");
	case XD_ERR_MT8:
		return ("Maintenance test 8 failure (disk FIFO)");
	case XD_ERR_IOCK:
		return ("IOPB checksum miscompare");
	case XD_ERR_IODM:
		return ("IOPB DMA fatal");
	case XD_ERR_IOAL:
		return ("IOPB address alignment error");
	case XD_ERR_FIRM:
		return ("Firmware error");
	case XD_ERR_MMOD:
		return ("Illegal maintenance mode test number");
	case XD_ERR_ACFL:
		return ("ACFAIL asserted");
	default:
		return ("Unknown error");
	}
}


code:
/*	$OpenBSD: xdreg.h,v 1.6 2014/07/11 16:35:40 jsg Exp $	*/
/*	$NetBSD: xdreg.h,v 1.3 1996/03/31 22:38:54 pk Exp $	*/

/*
 * Copyright (c) 1995 Charles D. Cranor
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * x d r e g . h
 *
 * this file contains the description of the Xylogics 753/7053's hardware
 * data structures.
 *
 * author: Chuck Cranor <chuck@ccrc.wustl.edu>
 */

#define XDC_MAXDEV    4       /* max devices per controller */
#define XDC_RESETUSEC 1000000 /* max time for xdc reset (page 21 says 1sec) */
#define XDC_MAXIOPB   31      /* max number of iopbs that can be active */
#define XDC_MAXTIME   4*1000000 /* four seconds before we give up and reset */
#define XDC_MAXTRIES  4       /* max number of times to retry an operation */
#define XDC_THROTTLE  32      /* dma throttle */
#define XDC_INTERLEAVE 0      /* interleave (for format param) */
#define XDC_DPARAM     0      /* dparam (drive param) XDDP_EC32 or 0 */

/*
 * xdc device interface
 * (lives in VME address space)
 */

struct xdc {
  volatile u_char filler0;
  volatile u_char xdc_iopbaddr0;       /* iopb byte 0 (LSB) */
  volatile u_char filler1;
  volatile u_char xdc_iopbaddr1;       /* iopb byte 1 */
  volatile u_char filler2;
  volatile u_char xdc_iopbaddr2;       /* iopb byte 2 */
  volatile u_char filler3;
  volatile u_char xdc_iopbaddr3;       /* iopb byte 3 (MSB) */
  volatile u_char filler4;
  volatile u_char xdc_iopbamod;        /* iopb address modifier */
  volatile u_char filler5;
  volatile u_char xdc_csr;             /* control and status register */
  volatile u_char filler6;
  volatile u_char xdc_f_err;           /* fatal error register */
};

/*
 * xdc_iopbamod: addressing modes
 * When doing DMA, if the maximum address of the buffer is greater than
 * 24 bits then you must use the 32 bit mode.   Note that on many systems
 * (e.g. sun-4/300) DVMA space is smaller than 24 bits, so there is no
 * need for the 32 bit mode.   However, the 32-bit mode hooks are in
 * the driver in case it ever gets ported to an environment that needs it.
 */

#define XDC_ADDRMOD   0x3d    /* standard address modifer, 24 bit max */
#define XDC_ADDRMOD32 0x0d    /* 32 bit version above */

/*
 * xdc_csr
 */

#define XDC_RMAINTMD 0x80     /* reserved maintenance mode (write) */
#define XDC_BUSY     0x80     /* busy (read) */
#define XDC_F_ERROR  0x40     /* fatal error (read) */
#define XDC_MAINTMOD 0x20     /* maintenance mode (read/write) */
#define XDC_RESET    0x08     /* soft reset (read/write) */
#define XDC_ADDIOPB  0x04     /* add iopb/add pending (write) */
#define XDC_ADDING   0x04     /* iopb add is pending (read) */
#define XDC_CLRRIO   0x02     /* clear RIO (remove iopb) request (write) */
#define XDC_REMIOPB  0x02     /* remove iopb (read) */
#define XDC_RBUSYSEM 0x01     /* register busy semaphore (read/write) */

/*
 * Input/Output Parameter Block (iopb)
 *
 * all controller commands are done via iopb's.   to start a command you
 * must do this:
 * [1] allocate space in DVMA space for the iopb
 * [2] fill out all the fields of the iopb
 * [3] check to see if controller can accept an iopb (XDC_ADDING bit clear)
 * [4] put the DVMA address of the iopb in the xdc registers (in vme space)
 * [5] set the XDC_ADDIOPB bit in the xdc csr
 * [6] <command started>
 *
 * when the controller is done with a command it may interrupt (if you
 * ask it to) and it will set the XDC_REMIOPB bit in the csr.   the address
 * of the finished iopb will be in the xdc registers.   after that is
 * read, set the XDC_CLRRIO to clear the iopb out of memory.
 *
 * the format of the iopb is described in section 4 of the manual.
 */

struct xd_iopb {
                                 /* section 4.1.1: byte 0 */
  volatile u_char errs:1;        /* error summary bit, only valid if
				    "done" is set.  must clear "done"
				    and "errs" bits before starting an
				    operation */
  volatile u_char done:1;        /* "done" bit */
  volatile u_char chen:1;        /* chain enable, "next iopb" is valid,
				    note xd returns one iopb at a time */
  volatile u_char sgm:1;         /* scatter/gather mode */
  volatile u_char comm:4;        /* command number (see table 4-2) */
#define XDCMD_NOP 0x0            /* no-op */
#define XDCMD_WR  0x1            /* write */
#define XDCMD_RD  0x2            /* read */
#define XDCMD_SK  0x3            /* seek */
#define XDCMD_RST 0x4            /* drive reset */
#define XDCMD_WRP 0x5            /* write params */
#define XDCMD_RDP 0x6            /* read params */
#define XDCMD_XWR 0x7            /* extended write */
#define XDCMD_XRD 0x8            /* extended read */
#define XDCMD_TST 0x9            /* diagnostic tests */
                                 /* 0xa to 0xf are reserved */
                                 /* section 4.1.2: byte 1 */
  volatile u_char errno;         /* status byte 1 (non-zero if error) */
                                 /* section 4.1.3: byte 2 */
  volatile u_char status;        /* status byte 2 (see below) */
#define XDST_SR   0x40           /* slipped revolution */
#define XDST_CSE  0x20           /* count sectors executed */
#define XDST_WRPT 0x10           /* write protected drive */
#define XDST_DFLT 0x08           /* disk fault */
#define XDST_SKER 0x04           /* seek error: >max, or timeout */
#define XDST_ONCL 0x02           /* on-cylinder */
#define XDST_DRDY 0x01           /* drive is ready! */
                                 /* section 4.1.4: byte 3 */
  volatile u_char istat;         /* internal status: reserved for xylogics */
                                 /* section 4.1.5: byte 4 */
  volatile u_char subfun;        /* sub-function of command (see below) */
#define XDFUN_R   0x00           /* XDCMD_SK: report current addr */
#define XDFUN_SR  0x01           /* XDCMD_SK: seek and report addr */
#define XDFUN_SRI 0x02           /* XDCMD_SK: start seek, report comp imm */
#define XDFUN_CTL 0x00           /* XDCMD_{WRP,RDP}: controller params */
#define XDFUN_DRV 0x80           /* XDCMD_{WRP,RDP}: drive params */
#define XDFUN_FMT 0x81           /* XDCMD_{WRP,RDP}: format params,XWR form.*/
#define XDFUN_STX 0xa0           /* XDCMD_RDP: read drive status extended */
#define XDFUN_THD 0x80           /* XDCMD_{XWR,XRD}: track headers */
#define XDFUN_VFY 0x81           /* XDCMD_XRD: verify data */
#define XDFUN_HDR 0x82           /* XDCMD_{XWR,XRD}: header, verify,data, ecc*/
#define XDFUN_DM  0xa0           /* XDCMD_{XWR,XRD}: defect map */
#define XDFUN_DMX 0xa1           /* XDCMD_{XWR,XRD}: defect map extended */
                                 /* section 4.1.6: byte 5 */
  volatile u_char fixd:1;        /* fixed media (vs removable) */
  volatile u_char reserved1:4;   /* reserved */
  volatile u_char unit:3;        /* unit number */
                                 /* note: 6 to 13 are overloaded (see below) */
                                 /* section 4.1.7: byte 6 */
  volatile u_char lll:5;         /* linked list length */
  volatile u_char intl:3;        /* interrupt level */
                                 /* section 4.1.8: byte 7 */
  volatile u_char intr_vec;      /* interrupt vector */
                                 /* section 4.1.9: bytes 8 and 9 */
  volatile u_short sectcnt;      /* sector count (# to xfer) */
                                 /* section 4.1.10: byte a and b */
  volatile u_short cylno;        /* cylinder number */
                                 /* section 4.1.11: byte c */
  volatile u_char headno;        /* head number */
                                 /* section 4.1.12: byte d */
  volatile u_char sectno;        /* sector number */
                                 /* section 4.1.13: byte e */
  volatile u_char addrmod;       /* addr modifier (bits 7,6 must be zero) */
                                 /* section 4.1.14: byte f */
  volatile u_char naddrmod;      /* next (in chain) address iobp ad. modifer */
                                 /* section 4.1.15: bytes 0x10 to 0x13 */
  volatile u_long daddr;         /* DMA data address */
                                 /* section 4.1.16: bytes 0x14 to 0x17 */
  volatile u_long nextiopb;      /* next iopb (in chain) address */
                                 /* section 4.1.17: bytes 0x18, 0x19 */
  volatile u_short cksum;        /* iopb checksum */
                                 /* section 4.1.18: bytes 0x1a, 0x1b */
  volatile u_short eccpattern;   /* ECC pattern word (ecc mode 0) */
                                 /* section 4.1.19: bytes 0x1c, 0x1d */
  volatile u_short eccoffword;   /* ECC offset word (ecc mode 0) */
};

/*
 * some commands overload bytes 6 to 0x13 of the iopb with different meanings.
 * these commands include:
 *   section 4.2: controller parameters
 *   section 4.3: drive parameters
 *   sectino 4.4: format parameters
 *
 * note that the commands that overload the iopb are not part of the
 * "critical data path" of the driver.   so, we handle them by defining
 * alternate iopb structures for these commands... it only costs us an
 * extra pointer.
 */

/*
 * controller parameters iopb: redefines bytes: 8 -> 0xe, 0x10 -> 0x13
 */

struct xd_iopb_ctrl {
  volatile u_char same[8];       /* same as xd_iopb */
                                 /* section 4.2.1: byte 8 */
  volatile u_char param_a;       /* param A (see below) */
#define XDPA_AUD  0x80           /* auto-update iopb fields when cmd done */
#define XDPA_TMOD 0x40           /* long-word transfer mode (vs word) */
#define XDPA_DACF 0x20           /* ignore vme ACFAIL signal */
#define XDPA_ICS  0x10           /* checksum check (adds 100usec per xfer) */
#define XDPA_EDT  0x08           /* enable on-board DMA timeout timer */
#define XDPA_NPRM 0x04           /* enable VME non-priv request mode */
                                 /* rest must be zero */
                                 /* section 4.2.2: byte 9 */
  volatile u_char param_b;       /* param B (see below) */
#define XDPB_TDT 0xc0            /* throttle dead time (see 8.11, below) */
#define XDPB_ROR 0x10            /* release on request */
#define XDPB_DRA 0x01            /* disable read ahead */
                                 /* TDT values: */
#define XDPB_TDT_0USEC    0x00   /* no TDT */
#define XDPB_TDT_3_2USEC  0x40   /* 3.2 usec */
#define XDPB_TDT_6_4USEC  0x80   /* 6.4 usec */
#define XDPB_TDT_12_8USEC 0xc0   /* 12.8 usec */
                                 /* section 4.2.3: byte a */
  volatile u_char param_c;       /* param C (see below) */
#define XDPC_OVS  0x80           /* over-lapped seek */
#define XDPC_COP  0x40           /* command optimiziation (elevator alg.) */
#define XDPC_ASR  0x10           /* auto-seek retry */
#define XDPC_RBC  0x04           /* retry before correction if ECC error */
#define XDPC_ECCM 0x03           /* ECC mode (0, 1, and 2) */
#define XDPC_ECC0 0x00           /* ECC mode 0 */
#define XDPC_ECC1 0x01           /* ECC mode 1 */
#define XDPC_ECC2 0x02           /* ECC mode 2 */
                                 /* section 4.2.4: byte b */
  volatile u_char throttle;      /* max dma xfers per master (0==256) */
                                 /* section 4.2.5: byte c */
  volatile u_char eprom_lvl;     /* EPROM release level */
  volatile u_char delay;         /* delay (see note below) */
                                 /* section 4.2.6: byte e */
  volatile u_char ctype;         /* controller type */
#define XDCT_753 0x53            /* xylogic 753/7053 */
  volatile u_char same2;         /* byte f: same as xd_iopb */
                                 /* section 4.2.7: byte 0x10, 0x11 */
  volatile u_short eprom_partno; /* eprom part number */
                                 /* section 4.2.8: byte 12 */
  volatile u_char eprom_rev;     /* eprom revision number */
};

/*
 * Note on byte 0xd ("delay"): This byte is not documented in the
 * Xylogics manual.  However, I contacted Xylogics and found out what
 * it does.  The controller sorts read commands into groups of
 * contiguous sectors.  After it processes a group of contiguous
 * sectors rather than immediately going on to the next group of
 * contiguous sectors, the controller can delay for a certain amount
 * of time in hopes of getting another cluster of reads in the same
 * area of the disk (thus avoiding a long seek).  Byte 0xd controls
 * how long it waits before giving up and going on and doing the next
 * contiguous cluster.
 *
 * it is unclear what unit the delay is in, but it looks like sun
 * uses the value "20" for sun3's, and "0" for sparc, except for the
 * 4/300 (where it is "4").   [see /sys/sundev/xd_conf.c on any 4.1.3
 * machine for how sun configures its controller...]
 */

#define XDC_DELAY_SUN3  20
#define XDC_DELAY_4_300 4
#define XDC_DELAY_SPARC 0

/*
 * drive parameters iopb: redefines bytes: 6, 8, 9, a, b, c, d, e
 */

struct xd_iopb_drive {
  volatile u_char same[6];       /* same as xd_iopb */
                                 /* section 4.3.1: byte 6 */
  volatile u_char dparam_ipl;    /* drive params | interrupt level */
#define XDDP_EC32 0x10           /* 32 bit ECC mode */
  volatile u_char same1;         /* byte 7: same */
                                 /* section 4.3.2: byte 8 */
  volatile u_char maxsect;       /* max sector/last head (<= byte d) */
                                 /* section 4.3.3: byte 9 */
  volatile u_char headoff;       /* head offset */
                                 /* section 4.3.4: bytes 0xa, 0xb */
  volatile u_short maxcyl;       /* max cyl (zero based!) */
                                 /* section 4.3.5: byte 0xc */
  volatile u_char maxhead;       /* max head (zero based!) */
                                 /* section 4.3.6: byte 0xd */
  volatile u_char maxsector;     /* max sector of disk (zero based!) */
                                 /* section 4.3.7: byte 0xe */
  volatile u_char sectpertrk;    /* sectors per track, not zero base, no runt*/
};

/*
 * format parameters iopb: redefines bytes: 6, 8, 9, a, b, c, d, 0x10, 0x11
 */

struct xd_iopb_format {
  volatile u_char same[6];       /* smae as xd_iopb */
                                 /* section 4.4.1: byte 6 */
  volatile u_char interleave_ipl;/* (interleave << 4) | interrupt level */
                                 /* interleave ratio 1:1 to 16:1 */
  volatile u_char same1;         /* byte 7: same */
                                 /* section 4.4.2: byte 8 */
  volatile u_char field1;        /* >= 1, xylogic says should be 1 */
#define XDFM_FIELD1 0x01         /* xylogic value */
                                 /* section 4.4.3: byte 9 */
  volatile u_char field2;        /* >0, field1+field2 <= 255 */
#define XDFM_FIELD2 0x0a         /* xylogic value */
                                 /* section 4.4.4: byte a */
  volatile u_char field3;        /* >= field1+field2 */
#define XDFM_FIELD3 0x1b         /* xylogic value */
                                 /* section 4.4.5: byte b */
  volatile u_char field4;        /* field4 */
#define XDFM_FIELD4 0x14         /* xylogic value */
                                 /* section 4.4.6: bytes 0xc, 0xd */
  volatile u_short bytespersec;  /* bytes per sector */
#define XDFM_BPS    0x200        /* must be 512! */
  volatile u_char same2[2];      /* bytes e, f */
                                 /* section 4.4.7: byte 0x10 */
  volatile u_char field6;        /* field 6 */
#define XDFM_FIELD6 0x0a         /* xylogic value */
                                 /* section 4.4.8: byte 0x11 */
  volatile u_char field7;        /* field 7, >= 1 */
#define XDFM_FIELD7 0x03         /* xylogic value */
};

/*
 * errors: errors come from either the fatal error register or the
 * iopb
 */

#define XD_ERA_MASK 0xf0         /* error action mask */
#define XD_ERA_PROG 0x10         /* program error */
#define XD_ERA_PRG2 0x20         /* program error */
#define XD_ERA_SOFT 0x30         /* soft error: we recovered */
#define XD_ERA_HARD 0x40         /* hard error: retry */
#define XD_ERA_RSET 0x60         /* hard error: reset, then retry */
#define XD_ERA_WPRO 0x90         /* write protected */

/* software error codes */
#define XD_ERR_FAIL 0xff         /* general total failure */
/* no error */
#define XD_ERR_AOK  0x00         /* success */
/* non-retryable programming error */
#define XD_ERR_ICYL 0x10         /* illegal cyl */
#define XD_ERR_IHD  0x11         /* illegal head */
#define XD_ERR_ISEC 0x12         /* illegal sector */
#define XD_ERR_CZER 0x13         /* count zero */
#define XD_ERR_UIMP 0x14         /* unknown command */
#define XD_ERR_IF1  0x15         /* illegal field 1 */
#define XD_ERR_IF2  0x16         /* illegal field 2 */
#define XD_ERR_IF3  0x17         /* illegal field 3 */
#define XD_ERR_IF4  0x18         /* illegal field 4 */
#define XD_ERR_IF5  0x19         /* illegal field 5 */
#define XD_ERR_IF6  0x1a         /* illegal field 6 */
#define XD_ERR_IF7  0x1b         /* illegal field 7 */
#define XD_ERR_ISG  0x1c         /* illegal scatter/gather */
#define XD_ERR_ISPT 0x1d         /* not enough sectors per track */
#define XD_ERR_ALGN 0x1e         /* next iopb allignment error */
#define XD_ERR_SGAL 0x1f         /* scatter gather address alignment error */
#define XD_ERR_SGEC 0x20         /* scatter gather with auto ECC */
/* successfully recovered soft errors */
#define XD_ERR_SECC 0x30         /* soft ecc corrected */
#define XD_ERR_SIGN 0x31         /* ecc ignored */
#define XD_ERR_ASEK 0x32         /* auto-seek retry recovered */
#define XD_ERR_RTRY 0x33         /* soft retry recovered */
/* hard errors: please retry */
#define XD_ERR_HECC 0x40         /* hard data ECC */
#define XD_ERR_NHDR 0x41         /* header not found */
#define XD_ERR_NRDY 0x42         /* drive not ready */
#define XD_ERR_TOUT 0x43         /* timeout */
#define XD_ERR_VTIM 0x44         /* VME DMA timeout */
#define XD_ERR_DSEQ 0x45         /* disk sequencer error */
#define XD_ERR_HDEC 0x48         /* header ECC error */
#define XD_ERR_RVFY 0x49         /* ready verify */
#define XD_ERR_VFER 0x4a         /* fatal VME DMA error */
#define XD_ERR_VBUS 0x4b         /* VME bus error */
/* hard error: reset and retry */
#define XD_ERR_DFLT 0x60         /* drive fault */
#define XD_ERR_HECY 0x61         /* header error/cyl */
#define XD_ERR_HEHD 0x62         /* header error/head */
#define XD_ERR_NOCY 0x63         /* not on cylinder */
#define XD_ERR_SEEK 0x64         /* seek error */
/* fatal hardware error */
#define XD_ERR_ILSS 0x70         /* illegal sector size */
/* misc */
#define XD_ERR_SEC  0x80         /* soft ecc */
/* requires manual intervention */
#define XD_ERR_WPER 0x90         /* write protected */
/* FATAL errors */
#define XD_ERR_IRAM 0xe1         /* IRAM self test failed */
#define XD_ERR_MT3  0xe3         /* maint test 3 failed (DSKCEL RAM) */
#define XD_ERR_MT4  0xe4         /* maint test 4 failed (Header shift reg) */
#define XD_ERR_MT5  0xe5         /* maint test 5 failed (VMEDMA regs) */
#define XD_ERR_MT6  0xe6         /* maint test 6 failed (REGCEL chip) */
#define XD_ERR_MT7  0xe7         /* maint test 7 failed (buff. parity) */
#define XD_ERR_MT8  0xe8         /* maint test 8 failed (fifo) */
#define XD_ERR_IOCK 0xf0         /* iopb checksume miscompare */
#define XD_ERR_IODM 0xf1         /* iopb dma fatal */
#define XD_ERR_IOAL 0xf2         /* iopb allignment error */
#define XD_ERR_FIRM 0xf3         /* firmware error n*/
#define XD_ERR_MMOD 0xf5         /* illegal maint mode test number */
#define XD_ERR_ACFL 0xf6         /* ACFAIL asserted */

license: unknown


code:
/*	$OpenBSD: xdvar.h,v 1.10 2015/02/08 01:30:09 dlg Exp $	*/
/*	$NetBSD: xdvar.h,v 1.5 1996/03/31 22:38:56 pk Exp $	*/

/*
 * Copyright (c) 1995 Charles D. Cranor
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * x d v a r . h
 *
 * this file defines the software structure we use to control the
 * 753/7053.
 *
 * author: Chuck Cranor <chuck@ccrc.wustl.edu>
 */

/*
 * i/o request: wrapper for hardware's iopb data structure
 */

struct xd_iorq {
  struct xd_iopb *iopb;             /* address of matching iopb */
  struct xdc_softc *xdc;            /* who we are working with */
  struct xd_softc *xd;              /* which disk */
  int ttl;                          /* time to live */
  int mode;                         /* current mode (state+other data) */
  int tries;                        /* number of times we have tried it */
  int errno;                        /* error number if we fail */
  int lasterror;		    /* last error we got */
  int blockno;                      /* starting block no for this xfer */
  int sectcnt;                      /* number of sectors in xfer */
  char *dbuf;                       /* KVA of data buffer (advances) */
  char *dbufbase;                   /* base of dbuf */
  struct buf *buf;                  /* for NORM */
};

/*
 * state
 */

#define XD_SUB_MASK 0xf0            /* mask bits for state */
#define XD_SUB_FREE 0x00            /* free */
#define XD_SUB_NORM 0x10            /* normal I/O request */
#define XD_SUB_WAIT 0x20            /* normal I/O request in the
                                             context of a process */
#define XD_SUB_POLL 0x30            /* polled mode */
#define XD_SUB_DONE 0x40            /* not active, but can't be free'd yet */
#define XD_SUB_NOQ  0x50            /* don't queue, just submit (internal) */

#define XD_STATE(X) ((X) & XD_SUB_MASK) /* extract state from mode */
#define XD_NEWSTATE(OLD, NEW) (((OLD) & ~XD_SUB_MASK) |(NEW)) /* new state */

/*
 * other mode data
 */

#define XD_MODE_VERBO 0x08          /* print error messages */
#define XD_MODE_B144  0x04          /* handling a bad144 sector */

/*
 * software timers and flags
 */

#define XDC_SUBWAITLIM 4   /* max number of "done" IOPBs there can be
				where we still allow a SUB_WAIT command */
#define XDC_TICKCNT (5*hz) /* call xdc_tick on this interval (5 sec) */
#define XDC_MAXTTL     2   /* max number of xd ticks to live */
#define XDC_NOUNIT (-1)    /* for xdcmd: no unit number */

/*
 * a "xd_softc" structure contains per-disk state info.
 */

struct xd_softc {
  struct device sc_dev;            /* device struct, reqd by autoconf */
  struct disk sc_dk;		   /* generic disk info */
  struct xdc_softc *parent;        /* parent */
  u_short flags;                   /* flags */
  u_short state;                   /* device state */
  int xd_drive;                    /* unit number */
  /* geometry */
  u_short ncyl, acyl, pcyl;        /* number of cyl's */
  u_short sectpercyl;              /* nhead*nsect */
  u_char nhead;                    /* number of heads */
  u_char nsect;                    /* number of sectors per track */
  u_char hw_spt;                   /* as above, but includes spare sectors */
  void *xd_labeldata;              /* temporary sector buffer */
  struct dkbad dkb;                /* bad144 sectors */
};

/*
 * flags
 */

/*
 * state
 */

#define XD_DRIVE_UNKNOWN 0         /* never talked to it */
#define XD_DRIVE_ATTACHING 1       /* attach in progress */
#define XD_DRIVE_NOLABEL 2         /* drive on-line, no label */
#define XD_DRIVE_ONLINE  3         /* drive is on-line */

/*
 * a "xdc_softc" structure contains per-disk-controller state info,
 * including a list of active controllers.
 */

struct xdc_softc {
  struct device sc_dev;            /* device struct, reqd by autoconf */
  struct intrhand sc_ih;           /* interrupt info */

  struct xdc *xdc;                 /* vaddr of vme registers */

  struct xd_softc *sc_drives[XDC_MAXDEV];   /* drives on this controller */
  int ipl;                         /* interrupt level */
  int vector;                      /* interrupt vector */

  struct xd_iorq *reqs;            /* i/o requests */
  struct xd_iopb *iopbase;         /* iopb base addr (maps iopb->iorq) */
  struct xd_iopb *dvmaiopb;        /* iopb base in DVMA space, not kvm */
  struct bufq sc_bufq;             /* queue'd IOPBs for this controller */
  char freereq[XDC_MAXIOPB];       /* free list (stack) */
  char waitq[XDC_MAXIOPB];         /* wait queue */
  u_char nfree;                    /* number of iopbs free */
  u_char nrun;                     /* number running */
  u_char nwait;                    /* number of waiting iopbs */
  u_char ndone;                    /* number of done IORQs */
  u_char waithead;                 /* head of queue */
  u_char waitend;		   /* end of queue */
  struct timeout xdc_tick_tmo;	   /* for xdc_tick() */
};

/*
 * reset blast modes
 */

#define XD_RSET_NONE (-1)          /* restart all requests */
#define XD_RSET_ALL  (-2)          /* don't restart anything */