Home | History | Annotate | Line # | Download | only in vme
xy.c revision 1.1
      1 /*	$NetBSD: xy.c,v 1.1 1997/11/02 23:09:25 pk Exp $	*/
      2 
      3 /*
      4  *
      5  * Copyright (c) 1995 Charles D. Cranor
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Charles D. Cranor.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  *
     36  * x y . c   x y l o g i c s   4 5 0 / 4 5 1   s m d   d r i v e r
     37  *
     38  * author: Chuck Cranor <chuck (at) ccrc.wustl.edu>
     39  * id: $NetBSD: xy.c,v 1.1 1997/11/02 23:09:25 pk Exp $
     40  * started: 14-Sep-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  *	       [3] Xylogics Model 450 User's Manual
     46  *		   part number: 166-017-001, Revision B, 1983.
     47  *	       [4] Addendum to Xylogics Model 450 Disk Controller User's
     48  *			Manual, Jan. 1985.
     49  *	       [5] The 451 Controller, Rev. B3, September 2, 1986.
     50  *	       [6] David Jones <dej (at) achilles.net>'s unfinished 450/451 driver
     51  *
     52  */
     53 
     54 #undef XYC_DEBUG		/* full debug */
     55 #undef XYC_DIAG			/* extra sanity checks */
     56 #if defined(DIAGNOSTIC) && !defined(XYC_DIAG)
     57 #define XYC_DIAG		/* link in with master DIAG option */
     58 #endif
     59 
     60 #include <sys/param.h>
     61 #include <sys/proc.h>
     62 #include <sys/systm.h>
     63 #include <sys/kernel.h>
     64 #include <sys/file.h>
     65 #include <sys/stat.h>
     66 #include <sys/ioctl.h>
     67 #include <sys/buf.h>
     68 #include <sys/uio.h>
     69 #include <sys/malloc.h>
     70 #include <sys/device.h>
     71 #include <sys/disklabel.h>
     72 #include <sys/disk.h>
     73 #include <sys/syslog.h>
     74 #include <sys/dkbad.h>
     75 #include <sys/conf.h>
     76 
     77 #include <vm/vm.h>
     78 #include <vm/vm_kern.h>
     79 
     80 #include <machine/bus.h>
     81 #include <machine/autoconf.h>
     82 #include <machine/sun_disklabel.h>
     83 #include <machine/conf.h>
     84 
     85 #include <dev/vme/vmevar.h>
     86 #include <dev/vme/xyreg.h>
     87 #include <dev/vme/xyvar.h>
     88 #include <dev/vme/xio.h>
     89 
     90 #include "locators.h"
     91 
     92 /*
     93  * macros
     94  */
     95 
     96 /*
     97  * XYC_GO: start iopb ADDR (DVMA addr in a u_long) on XYC
     98  */
     99 #define XYC_GO(XYC, ADDR) { \
    100 	(XYC)->xyc_addr_lo = ((ADDR) & 0xff); \
    101 	(ADDR) = ((ADDR) >> 8); \
    102 	(XYC)->xyc_addr_hi = ((ADDR) & 0xff); \
    103 	(ADDR) = ((ADDR) >> 8); \
    104 	(XYC)->xyc_reloc_lo = ((ADDR) & 0xff); \
    105 	(ADDR) = ((ADDR) >> 8); \
    106 	(XYC)->xyc_reloc_hi = (ADDR); \
    107 	(XYC)->xyc_csr = XYC_GBSY; /* go! */ \
    108 }
    109 
    110 /*
    111  * XYC_DONE: don't need IORQ, get error code and free (done after xyc_cmd)
    112  */
    113 
    114 #define XYC_DONE(SC,ER) { \
    115 	if ((ER) == XY_ERR_AOK) { \
    116 		(ER) = (SC)->ciorq->errno; \
    117 		(SC)->ciorq->mode = XY_SUB_FREE; \
    118 		wakeup((SC)->ciorq); \
    119 	} \
    120 	}
    121 
    122 /*
    123  * XYC_ADVANCE: advance iorq's pointers by a number of sectors
    124  */
    125 
    126 #define XYC_ADVANCE(IORQ, N) { \
    127 	if (N) { \
    128 		(IORQ)->sectcnt -= (N); \
    129 		(IORQ)->blockno += (N); \
    130 		(IORQ)->dbuf += ((N)*XYFM_BPS); \
    131 	} \
    132 }
    133 
    134 /*
    135  * note - addresses you can sleep on:
    136  *   [1] & of xy_softc's "state" (waiting for a chance to attach a drive)
    137  *   [2] & an iorq (waiting for an XY_SUB_WAIT iorq to finish)
    138  */
    139 
    140 
    141 /*
    142  * function prototypes
    143  * "xyc_*" functions are internal, all others are external interfaces
    144  */
    145 
    146 extern int pil_to_vme[];	/* from obio.c */
    147 
    148 /* internals */
    149 struct xy_iopb *xyc_chain __P((struct xyc_softc *, struct xy_iorq *));
    150 int	xyc_cmd __P((struct xyc_softc *, int, int, int, int, int, char *, int));
    151 char   *xyc_e2str __P((int));
    152 int	xyc_entoact __P((int));
    153 int	xyc_error __P((struct xyc_softc *, struct xy_iorq *,
    154 		   struct xy_iopb *, int));
    155 int	xyc_ioctlcmd __P((struct xy_softc *, dev_t dev, struct xd_iocmd *));
    156 void	xyc_perror __P((struct xy_iorq *, struct xy_iopb *, int));
    157 int	xyc_piodriver __P((struct xyc_softc *, struct xy_iorq *));
    158 int	xyc_remove_iorq __P((struct xyc_softc *));
    159 int	xyc_reset __P((struct xyc_softc *, int, struct xy_iorq *, int,
    160 			struct xy_softc *));
    161 inline void xyc_rqinit __P((struct xy_iorq *, struct xyc_softc *,
    162 			    struct xy_softc *, int, u_long, int,
    163 			    caddr_t, struct buf *));
    164 void	xyc_rqtopb __P((struct xy_iorq *, struct xy_iopb *, int, int));
    165 void	xyc_start __P((struct xyc_softc *, struct xy_iorq *));
    166 int	xyc_startbuf __P((struct xyc_softc *, struct xy_softc *, struct buf *));
    167 int	xyc_submit_iorq __P((struct xyc_softc *, struct xy_iorq *, int));
    168 void	xyc_tick __P((void *));
    169 int	xyc_unbusy __P((struct xyc *, int));
    170 void	xyc_xyreset __P((struct xyc_softc *, struct xy_softc *));
    171 
    172 /* machine interrupt hook */
    173 int	xycintr __P((void *));
    174 
    175 /* autoconf */
    176 int	xycmatch __P((struct device *, struct cfdata *, void *));
    177 void	xycattach __P((struct device *, struct device *, void *));
    178 int	xymatch __P((struct device *, struct cfdata *, void *));
    179 void	xyattach __P((struct device *, struct device *, void *));
    180 
    181 static	void xydummystrat __P((struct buf *));
    182 int	xygetdisklabel __P((struct xy_softc *, void *));
    183 
    184 /*
    185  * cfdrivers: device driver interface to autoconfig
    186  */
    187 
    188 struct cfattach xyc_ca = {
    189 	sizeof(struct xyc_softc), xycmatch, xycattach
    190 };
    191 
    192 struct cfdriver xyc_cd = {
    193 	NULL, "xyc", DV_DULL
    194 };
    195 
    196 struct cfattach xy_ca = {
    197 	sizeof(struct xy_softc), xymatch, xyattach
    198 };
    199 
    200 struct cfdriver xy_cd = {
    201 	NULL, "xy", DV_DISK
    202 };
    203 
    204 struct xyc_attach_args {	/* this is the "aux" args to xyattach */
    205 	int	driveno;	/* unit number */
    206 	char	*buf;		/* scratch buffer for reading disk label */
    207 	char	*dvmabuf;	/* DVMA address of above */
    208 	int	fullmode;	/* submit mode */
    209 	int	booting;	/* are we booting or not? */
    210 };
    211 
    212 /*
    213  * dkdriver
    214  */
    215 
    216 struct dkdriver xydkdriver = { xystrategy };
    217 
    218 /*
    219  * start: disk label fix code (XXX)
    220  */
    221 
    222 static void *xy_labeldata;
    223 
    224 static void
    225 xydummystrat(bp)
    226 	struct buf *bp;
    227 {
    228 	if (bp->b_bcount != XYFM_BPS)
    229 		panic("xydummystrat");
    230 	bcopy(xy_labeldata, bp->b_un.b_addr, XYFM_BPS);
    231 	bp->b_flags |= B_DONE;
    232 	bp->b_flags &= ~B_BUSY;
    233 }
    234 
    235 int
    236 xygetdisklabel(xy, b)
    237 	struct xy_softc *xy;
    238 	void *b;
    239 {
    240 	char *err;
    241 	struct sun_disklabel *sdl;
    242 
    243 	/* We already have the label data in `b'; setup for dummy strategy */
    244 	xy_labeldata = b;
    245 
    246 	/* Required parameter for readdisklabel() */
    247 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
    248 
    249 	err = readdisklabel(MAKEDISKDEV(0, xy->sc_dev.dv_unit, RAW_PART),
    250 					xydummystrat,
    251 				xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
    252 	if (err) {
    253 		printf("%s: %s\n", xy->sc_dev.dv_xname, err);
    254 		return(XY_ERR_FAIL);
    255 	}
    256 
    257 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
    258 	sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
    259 	if (sdl->sl_magic == SUN_DKMAGIC)
    260 		xy->pcyl = sdl->sl_pcylinders;
    261 	else {
    262 		printf("%s: WARNING: no `pcyl' in disk label.\n",
    263 			xy->sc_dev.dv_xname);
    264 		xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
    265 			xy->sc_dk.dk_label->d_acylinders;
    266 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
    267 		xy->sc_dev.dv_xname, xy->pcyl);
    268 	}
    269 
    270 	xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
    271 	xy->acyl = xy->sc_dk.dk_label->d_acylinders;
    272 	xy->nhead = xy->sc_dk.dk_label->d_ntracks;
    273 	xy->nsect = xy->sc_dk.dk_label->d_nsectors;
    274 	xy->sectpercyl = xy->nhead * xy->nsect;
    275 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
    276                                           	  * sun->bsd */
    277 	return(XY_ERR_AOK);
    278 }
    279 
    280 /*
    281  * end: disk label fix code (XXX)
    282  */
    283 
    284 /*
    285  * a u t o c o n f i g   f u n c t i o n s
    286  */
    287 
    288 /*
    289  * xycmatch: determine if xyc is present or not.   we do a
    290  * soft reset to detect the xyc.
    291  */
    292 
    293 int xycmatch(parent, cf, aux)
    294 	struct device *parent;
    295 	struct cfdata *cf;
    296 	void *aux;
    297 {
    298 	struct vme_attach_args	*va = aux;
    299 	vme_chipset_tag_t	ct = va->vma_chipset_tag;
    300 	bus_space_tag_t		bt = va->vma_bustag;
    301 	struct xyc		*xyc;
    302 	vme_mod_t		mod;
    303 
    304 	mod = VMEMOD_A16 | VMEMOD_S | VMEMOD_D;
    305 
    306 	xyc = (struct xyc *) va->vma_reg[0];
    307 	if (vme_bus_probe(ct, bt, (vme_addr_t)&xyc->xyc_rsetup, 1, mod)) {
    308 		if (xyc_unbusy(xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
    309 			return(0);
    310 		return (1);
    311 	}
    312 	return (0);
    313 }
    314 
    315 /*
    316  * xycattach: attach controller
    317  */
    318 void
    319 xycattach(parent, self, aux)
    320 	struct device *parent, *self;
    321 	void   *aux;
    322 
    323 {
    324 	struct vme_attach_args	*va = aux;
    325 	vme_chipset_tag_t	ct = va->vma_chipset_tag;
    326 	bus_space_tag_t		bt = va->vma_bustag;
    327 	bus_space_handle_t	bh;
    328 	vme_intr_handle_t	ih;
    329 	vme_mod_t		mod;
    330 	struct xyc_softc	*xyc = (void *) self;
    331 	struct xyc_attach_args	xa;
    332 	int			lcv, err, res, pbsz;
    333 	void			*tmp, *tmp2;
    334 	void			*dtmp, *dtmp2;
    335 	u_long			ultmp;
    336 
    337 	/* get addressing and intr level stuff from autoconfig and load it
    338 	 * into our xyc_softc. */
    339 
    340 	mod = VMEMOD_A16 | VMEMOD_S | VMEMOD_D;
    341 
    342 	if (vme_bus_map(ct, va->vma_reg[0], sizeof(struct xyc),
    343 			mod, bt, &bh) != 0)
    344 		panic("xyc: vme_map");
    345 
    346 	xyc->xyc = (struct xyc *) bh;
    347 	xyc->ipl = va->vma_pri;
    348 	xyc->vector = va->vma_vec;
    349 	printf(" pri %d", va->vma_pri);
    350 	xyc->no_ols = 0; /* XXX should be from config */
    351 
    352 	for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
    353 		xyc->sc_drives[lcv] = (struct xy_softc *) 0;
    354 
    355 	/*
    356 	 * allocate and zero buffers
    357 	 * check boundaries of the KVA's ... all IOPBs must reside in
    358  	 * the same 64K region.
    359 	 */
    360 
    361 	pbsz = XYC_MAXIOPB * sizeof(struct xy_iopb);
    362 	dtmp = dtmp2 = (struct xy_iopb *)dvma_malloc(pbsz, &tmp, M_NOWAIT);
    363 	tmp2 = tmp;
    364 	ultmp = (u_long) dtmp;
    365 	if ((ultmp & 0xffff0000) != ((ultmp + pbsz) & 0xffff0000)) {
    366 		dtmp = (struct xy_iopb *)
    367 		    dvma_malloc(pbsz, &tmp, M_NOWAIT); /* retry! */
    368 		dvma_free(dtmp2, pbsz, &tmp2);
    369 		ultmp = (u_long) dtmp;
    370 		if ((ultmp & 0xffff0000) != ((ultmp + pbsz) & 0xffff0000)) {
    371 			printf("%s: can't alloc IOPB mem in 64K\n",
    372 				xyc->sc_dev.dv_xname);
    373 			return;
    374 		}
    375 	}
    376 	bzero(tmp, pbsz);
    377 	xyc->iopbase = tmp;
    378 	xyc->iopbase = dtmp; /* XXX TMP HACK */
    379 	xyc->dvmaiopb = (struct xy_iopb *) ((u_long)dtmp - DVMA_BASE);
    380 	xyc->reqs = (struct xy_iorq *)
    381 	    malloc(XYC_MAXIOPB * sizeof(struct xy_iorq), M_DEVBUF, M_NOWAIT);
    382 	if (xyc->reqs == NULL)
    383 		panic("xyc malloc");
    384 	bzero(xyc->reqs, XYC_MAXIOPB * sizeof(struct xy_iorq));
    385 
    386 	/*
    387 	 * init iorq to iopb pointers, and non-zero fields in the
    388 	 * iopb which never change.
    389 	 */
    390 
    391 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
    392 		xyc->xy_chain[lcv] = NULL;
    393 		xyc->reqs[lcv].iopb = &xyc->iopbase[lcv];
    394 		xyc->iopbase[lcv].asr = 1;	/* always the same */
    395 		xyc->iopbase[lcv].eef = 1;	/* always the same */
    396 		xyc->iopbase[lcv].ecm = XY_ECM;	/* always the same */
    397 		xyc->iopbase[lcv].aud = 1;	/* always the same */
    398 		xyc->iopbase[lcv].relo = 1;	/* always the same */
    399 		xyc->iopbase[lcv].thro = XY_THRO;/* always the same */
    400 	}
    401 	xyc->ciorq = &xyc->reqs[XYC_CTLIOPB];    /* short hand name */
    402 	xyc->ciopb = &xyc->iopbase[XYC_CTLIOPB]; /* short hand name */
    403 	xyc->xy_hand = 0;
    404 
    405 	/* read controller parameters and insure we have a 450/451 */
    406 
    407 	err = xyc_cmd(xyc, XYCMD_ST, 0, 0, 0, 0, 0, XY_SUB_POLL);
    408 	res = xyc->ciopb->ctyp;
    409 	XYC_DONE(xyc, err);
    410 	if (res != XYCT_450) {
    411 		if (err)
    412 			printf(": %s: ", xyc_e2str(err));
    413 		printf(": doesn't identify as a 450/451\n");
    414 		return;
    415 	}
    416 	printf(": Xylogics 450/451");
    417 	if (xyc->no_ols)
    418 		printf(" [OLS disabled]"); /* 450 doesn't overlap seek right */
    419 	printf("\n");
    420 	if (err) {
    421 		printf("%s: error: %s\n", xyc->sc_dev.dv_xname,
    422 				xyc_e2str(err));
    423 		return;
    424 	}
    425 	if ((xyc->xyc->xyc_csr & XYC_ADRM) == 0) {
    426 		printf("%s: 24 bit addressing turned off\n",
    427 			xyc->sc_dev.dv_xname);
    428 		printf("please set hardware jumpers JM1-JM2=in, JM3-JM4=out\n");
    429 		printf("to enable 24 bit mode and this driver\n");
    430 		return;
    431 	}
    432 
    433 	/* link in interrupt with higher level software */
    434 	vme_intr_map(ct, va->vma_vec, va->vma_pri, &ih);
    435 	vme_intr_establish(ct, ih, xycintr, xyc);
    436 	evcnt_attach(&xyc->sc_dev, "intr", &xyc->sc_intrcnt);
    437 
    438 
    439 	/* now we must look for disks using autoconfig */
    440 	xa.dvmabuf = (char *)dvma_malloc(XYFM_BPS, &xa.buf, M_NOWAIT);
    441 	xa.fullmode = XY_SUB_POLL;
    442 	xa.booting = 1;
    443 
    444 #if 0	/* XXX - deal with this disksubr */
    445 	if (ca->ca_ra.ra_bp && ca->ca_ra.ra_bp->val[0] == -1 &&
    446 	    ca->ca_ra.ra_bp->val[1] == xyc->sc_dev.dv_unit) {
    447 		bootpath_store(1, ca->ca_ra.ra_bp + 1); /* advance bootpath */
    448 	}
    449 #endif
    450 
    451 	for (xa.driveno = 0; xa.driveno < XYC_MAXDEV; xa.driveno++)
    452 		(void) config_found(self, (void *) &xa, NULL);
    453 
    454 	dvma_free(xa.dvmabuf, XYFM_BPS, &xa.buf);
    455 #if 0
    456 	bootpath_store(1, NULL);
    457 #endif
    458 
    459 	/* start the watchdog clock */
    460 	timeout(xyc_tick, xyc, XYC_TICKCNT);
    461 
    462 }
    463 
    464 /*
    465  * xymatch: probe for disk.
    466  *
    467  * note: we almost always say disk is present.   this allows us to
    468  * spin up and configure a disk after the system is booted (we can
    469  * call xyattach!).
    470  */
    471 int
    472 xymatch(parent, cf, aux)
    473 	struct device *parent;
    474 	struct cfdata *cf;
    475 	void *aux;
    476 {
    477 	struct xyc_attach_args *xa = aux;
    478 
    479 	/* looking for autoconf wildcard or exact match */
    480 
    481 	if (cf->cf_loc[XYCCF_DRIVE] != XYCCF_DRIVE_DEFAULT &&
    482 	    cf->cf_loc[XYCCF_DRIVE] != xa->driveno)
    483 		return 0;
    484 
    485 	return 1;
    486 
    487 }
    488 
    489 /*
    490  * xyattach: attach a disk.   this can be called from autoconf and also
    491  * from xyopen/xystrategy.
    492  */
    493 void
    494 xyattach(parent, self, aux)
    495 	struct device *parent, *self;
    496 	void   *aux;
    497 
    498 {
    499 	struct xy_softc *xy = (void *) self, *oxy;
    500 	struct xyc_softc *xyc = (void *) parent;
    501 	struct xyc_attach_args *xa = aux;
    502 	int     err, spt, mb, blk, lcv, fmode, s = 0, newstate;
    503 	struct dkbad *dkb;
    504 	struct bootpath *bp;
    505 
    506 	/*
    507 	 * Always re-initialize the disk structure.  We want statistics
    508 	 * to start with a clean slate.
    509 	 */
    510 	bzero(&xy->sc_dk, sizeof(xy->sc_dk));
    511 	xy->sc_dk.dk_driver = &xydkdriver;
    512 	xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
    513 
    514 	/* if booting, init the xy_softc */
    515 
    516 	if (xa->booting) {
    517 		xy->state = XY_DRIVE_UNKNOWN;	/* to start */
    518 		xy->flags = 0;
    519 		xy->parent = xyc;
    520 
    521 		/* init queue of waiting bufs */
    522 
    523 		xy->xyq.b_active = 0;
    524 		xy->xyq.b_actf = 0;
    525 		xy->xyq.b_actb = &xy->xyq.b_actf; /* XXX b_actb: not used? */
    526 
    527 		xy->xyrq = &xyc->reqs[xa->driveno];
    528 
    529 	}
    530 	xy->xy_drive = xa->driveno;
    531 	fmode = xa->fullmode;
    532 	xyc->sc_drives[xa->driveno] = xy;
    533 
    534 	/* if not booting, make sure we are the only process in the attach for
    535 	 * this drive.   if locked out, sleep on it. */
    536 
    537 	if (!xa->booting) {
    538 		s = splbio();
    539 		while (xy->state == XY_DRIVE_ATTACHING) {
    540 			if (tsleep(&xy->state, PRIBIO, "xyattach", 0)) {
    541 				splx(s);
    542 				return;
    543 			}
    544 		}
    545 		printf("%s at %s",
    546 			xy->sc_dev.dv_xname, xy->parent->sc_dev.dv_xname);
    547 	}
    548 	/* we now have control */
    549 
    550 	xy->state = XY_DRIVE_ATTACHING;
    551 	newstate = XY_DRIVE_UNKNOWN;
    552 
    553 	/* first try and reset the drive */
    554 
    555 	err = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fmode);
    556 	XYC_DONE(xyc, err);
    557 	if (err == XY_ERR_DNRY) {
    558 		printf(" drive %d: off-line\n", xa->driveno);
    559 		goto done;
    560 	}
    561 	if (err) {
    562 		printf(": ERROR 0x%02x (%s)\n", err, xyc_e2str(err));
    563 		goto done;
    564 	}
    565 	printf(" drive %d: ready", xa->driveno);
    566 
    567 	/*
    568 	 * now set drive parameters (to semi-bogus values) so we can read the
    569 	 * disk label.
    570 	 */
    571 	xy->pcyl = xy->ncyl = 1;
    572 	xy->acyl = 0;
    573 	xy->nhead = 1;
    574 	xy->nsect = 1;
    575 	xy->sectpercyl = 1;
    576 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
    577 		xy->dkb.bt_bad[lcv].bt_cyl =
    578 			xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
    579 
    580 	/* read disk label */
    581 	for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
    582 						xy->drive_type++) {
    583 		err = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
    584 						xa->dvmabuf, fmode);
    585 		XYC_DONE(xyc, err);
    586 		if (err == XY_ERR_AOK) break;
    587 	}
    588 
    589 	if (err != XY_ERR_AOK) {
    590 		printf("\n%s: reading disk label failed: %s\n",
    591 			xy->sc_dev.dv_xname, xyc_e2str(err));
    592 		goto done;
    593 	}
    594 	printf(" (drive type %d)\n", xy->drive_type);
    595 
    596 	newstate = XY_DRIVE_NOLABEL;
    597 
    598 	xy->hw_spt = spt = 0; /* XXX needed ? */
    599 	/* Attach the disk: must be before getdisklabel to malloc label */
    600 	disk_attach(&xy->sc_dk);
    601 
    602 	if (xygetdisklabel(xy, xa->buf) != XY_ERR_AOK)
    603 		goto done;
    604 
    605 	/* inform the user of what is up */
    606 	printf("%s: <%s>, pcyl %d\n", xy->sc_dev.dv_xname,
    607 		xa->buf, xy->pcyl);
    608 	mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
    609 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
    610 		xy->sc_dev.dv_xname, mb, xy->ncyl, xy->nhead, xy->nsect,
    611 		XYFM_BPS);
    612 
    613 	/*
    614 	 * 450/451 stupidity: the drive type is encoded into the format
    615 	 * of the disk.   the drive type in the IOPB must match the drive
    616 	 * type in the format, or you will not be able to do I/O to the
    617 	 * disk (you get header not found errors).  if you have two drives
    618 	 * of different sizes that have the same drive type in their
    619 	 * formatting then you are out of luck.
    620 	 *
    621 	 * this problem was corrected in the 753/7053.
    622 	 */
    623 
    624 	for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
    625 		oxy = xyc->sc_drives[lcv];
    626 		if (oxy == NULL || oxy == xy) continue;
    627 		if (oxy->drive_type != xy->drive_type) continue;
    628 		if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
    629 			xy->nhead != oxy->nhead) {
    630 			printf("%s: %s and %s must be the same size!\n",
    631 				xyc->sc_dev.dv_xname, xy->sc_dev.dv_xname,
    632 				oxy->sc_dev.dv_xname);
    633 			panic("xy drive size mismatch");
    634 		}
    635 	}
    636 
    637 
    638 	/* now set the real drive parameters! */
    639 
    640 	blk = (xy->nsect - 1) +
    641 		((xy->nhead - 1) * xy->nsect) +
    642 		((xy->pcyl - 1) * xy->nsect * xy->nhead);
    643 	err = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fmode);
    644 	XYC_DONE(xyc, err);
    645 	if (err) {
    646 		printf("%s: write drive size failed: %s\n",
    647 			xy->sc_dev.dv_xname, xyc_e2str(err));
    648 		goto done;
    649 	}
    650 	newstate = XY_DRIVE_ONLINE;
    651 
    652 	/*
    653 	 * read bad144 table. this table resides on the first sector of the
    654 	 * last track of the disk (i.e. second cyl of "acyl" area).
    655 	 */
    656 
    657 	blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
    658 								/* last cyl */
    659 	    (xy->nhead - 1) * xy->nsect;	/* last head */
    660 	err = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
    661 						xa->dvmabuf, fmode);
    662 	XYC_DONE(xyc, err);
    663 	if (err) {
    664 		printf("%s: reading bad144 failed: %s\n",
    665 			xy->sc_dev.dv_xname, xyc_e2str(err));
    666 		goto done;
    667 	}
    668 
    669 	/* check dkbad for sanity */
    670 	dkb = (struct dkbad *) xa->buf;
    671 	for (lcv = 0; lcv < 126; lcv++) {
    672 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
    673 				dkb->bt_bad[lcv].bt_cyl == 0) &&
    674 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
    675 			continue;	/* blank */
    676 		if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
    677 			break;
    678 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
    679 			break;
    680 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
    681 			break;
    682 	}
    683 	if (lcv != 126) {
    684 		printf("%s: warning: invalid bad144 sector!\n",
    685 			xy->sc_dev.dv_xname);
    686 	} else {
    687 		bcopy(xa->buf, &xy->dkb, XYFM_BPS);
    688 	}
    689 
    690 	if (xa->booting) {
    691 		/* restore bootpath! (do this via attach_args again?)*/
    692 		bp = bootpath_store(0, NULL);
    693 		if (bp && strcmp("xy", bp->name) == 0 &&
    694 					xy->xy_drive == bp->val[0])
    695 			bp->dev = &xy->sc_dev;
    696 	}
    697 
    698 	dk_establish(&xy->sc_dk, &xy->sc_dev);		/* XXX */
    699 
    700 done:
    701 	xy->state = newstate;
    702 	if (!xa->booting) {
    703 		wakeup(&xy->state);
    704 		splx(s);
    705 	}
    706 }
    707 
    708 /*
    709  * end of autoconfig functions
    710  */
    711 
    712 /*
    713  * { b , c } d e v s w   f u n c t i o n s
    714  */
    715 
    716 /*
    717  * xyclose: close device
    718  */
    719 int
    720 xyclose(dev, flag, fmt, p)
    721 	dev_t   dev;
    722 	int     flag, fmt;
    723 	struct proc *p;
    724 
    725 {
    726 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
    727 	int     part = DISKPART(dev);
    728 
    729 	/* clear mask bits */
    730 
    731 	switch (fmt) {
    732 	case S_IFCHR:
    733 		xy->sc_dk.dk_copenmask &= ~(1 << part);
    734 		break;
    735 	case S_IFBLK:
    736 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
    737 		break;
    738 	}
    739 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    740 
    741 	return 0;
    742 }
    743 
    744 /*
    745  * xydump: crash dump system
    746  */
    747 int
    748 xydump(dev, blkno, va, size)
    749 	dev_t dev;
    750 	daddr_t blkno;
    751 	caddr_t va;
    752 	size_t size;
    753 {
    754 	int     unit, part;
    755 	struct xy_softc *xy;
    756 
    757 	unit = DISKUNIT(dev);
    758 	if (unit >= xy_cd.cd_ndevs)
    759 		return ENXIO;
    760 	part = DISKPART(dev);
    761 
    762 	xy = xy_cd.cd_devs[unit];
    763 
    764 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
    765 	    'a' + part);
    766 
    767 	return ENXIO;
    768 
    769 	/* outline: globals: "dumplo" == sector number of partition to start
    770 	 * dump at (convert to physical sector with partition table)
    771 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
    772 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
    773 	 * physmem)
    774 	 *
    775 	 * dump a copy of physical memory to the dump device starting at sector
    776 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
    777 	 * we go.   use polled I/O.
    778 	 *
    779 	 * XXX how to handle NON_CONTIG? */
    780 
    781 }
    782 
    783 /*
    784  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
    785  */
    786 int
    787 xyioctl(dev, command, addr, flag, p)
    788 	dev_t   dev;
    789 	u_long  command;
    790 	caddr_t addr;
    791 	int     flag;
    792 	struct proc *p;
    793 
    794 {
    795 	struct xy_softc *xy;
    796 	struct xd_iocmd *xio;
    797 	int     error, s, unit;
    798 
    799 	unit = DISKUNIT(dev);
    800 
    801 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
    802 		return (ENXIO);
    803 
    804 	/* switch on ioctl type */
    805 
    806 	switch (command) {
    807 	case DIOCSBAD:		/* set bad144 info */
    808 		if ((flag & FWRITE) == 0)
    809 			return EBADF;
    810 		s = splbio();
    811 		bcopy(addr, &xy->dkb, sizeof(xy->dkb));
    812 		splx(s);
    813 		return 0;
    814 
    815 	case DIOCGDINFO:	/* get disk label */
    816 		bcopy(xy->sc_dk.dk_label, addr, sizeof(struct disklabel));
    817 		return 0;
    818 
    819 	case DIOCGPART:	/* get partition info */
    820 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
    821 		((struct partinfo *) addr)->part =
    822 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    823 		return 0;
    824 
    825 	case DIOCSDINFO:	/* set disk label */
    826 		if ((flag & FWRITE) == 0)
    827 			return EBADF;
    828 		error = setdisklabel(xy->sc_dk.dk_label,
    829 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
    830 		    xy->sc_dk.dk_cpulabel);
    831 		if (error == 0) {
    832 			if (xy->state == XY_DRIVE_NOLABEL)
    833 				xy->state = XY_DRIVE_ONLINE;
    834 		}
    835 		return error;
    836 
    837 	case DIOCWLABEL:	/* change write status of disk label */
    838 		if ((flag & FWRITE) == 0)
    839 			return EBADF;
    840 		if (*(int *) addr)
    841 			xy->flags |= XY_WLABEL;
    842 		else
    843 			xy->flags &= ~XY_WLABEL;
    844 		return 0;
    845 
    846 	case DIOCWDINFO:	/* write disk label */
    847 		if ((flag & FWRITE) == 0)
    848 			return EBADF;
    849 		error = setdisklabel(xy->sc_dk.dk_label,
    850 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
    851 		    xy->sc_dk.dk_cpulabel);
    852 		if (error == 0) {
    853 			if (xy->state == XY_DRIVE_NOLABEL)
    854 				xy->state = XY_DRIVE_ONLINE;
    855 
    856 			/* Simulate opening partition 0 so write succeeds. */
    857 			xy->sc_dk.dk_openmask |= (1 << 0);
    858 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    859 			    xystrategy, xy->sc_dk.dk_label,
    860 			    xy->sc_dk.dk_cpulabel);
    861 			xy->sc_dk.dk_openmask =
    862 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    863 		}
    864 		return error;
    865 
    866 	case DIOSXDCMD:
    867 		xio = (struct xd_iocmd *) addr;
    868 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    869 			return (error);
    870 		return (xyc_ioctlcmd(xy, dev, xio));
    871 
    872 	default:
    873 		return ENOTTY;
    874 	}
    875 }
    876 
    877 /*
    878  * xyopen: open drive
    879  */
    880 
    881 int
    882 xyopen(dev, flag, fmt, p)
    883 	dev_t   dev;
    884 	int     flag, fmt;
    885 	struct proc *p;
    886 {
    887 	int     unit, part;
    888 	struct xy_softc *xy;
    889 	struct xyc_attach_args xa;
    890 
    891 	/* first, could it be a valid target? */
    892 
    893 	unit = DISKUNIT(dev);
    894 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
    895 		return (ENXIO);
    896 	part = DISKPART(dev);
    897 
    898 	/* do we need to attach the drive? */
    899 
    900 	if (xy->state == XY_DRIVE_UNKNOWN) {
    901 		xa.driveno = xy->xy_drive;
    902 		xa.dvmabuf = (char *)dvma_malloc(XYFM_BPS, &xa.buf, M_NOWAIT);
    903 		xa.fullmode = XY_SUB_WAIT;
    904 		xa.booting = 0;
    905 		xyattach((struct device *) xy->parent,
    906 						(struct device *) xy, &xa);
    907 		dvma_free(xa.dvmabuf, XYFM_BPS, &xa.buf);
    908 		if (xy->state == XY_DRIVE_UNKNOWN) {
    909 			return (EIO);
    910 		}
    911 	}
    912 	/* check for partition */
    913 
    914 	if (part != RAW_PART &&
    915 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
    916 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    917 		return (ENXIO);
    918 	}
    919 	/* set open masks */
    920 
    921 	switch (fmt) {
    922 	case S_IFCHR:
    923 		xy->sc_dk.dk_copenmask |= (1 << part);
    924 		break;
    925 	case S_IFBLK:
    926 		xy->sc_dk.dk_bopenmask |= (1 << part);
    927 		break;
    928 	}
    929 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    930 
    931 	return 0;
    932 }
    933 
    934 int
    935 xyread(dev, uio, flags)
    936 	dev_t   dev;
    937 	struct uio *uio;
    938 	int flags;
    939 {
    940 
    941 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
    942 }
    943 
    944 int
    945 xywrite(dev, uio, flags)
    946 	dev_t   dev;
    947 	struct uio *uio;
    948 	int flags;
    949 {
    950 
    951 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
    952 }
    953 
    954 
    955 /*
    956  * xysize: return size of a partition for a dump
    957  */
    958 
    959 int
    960 xysize(dev)
    961 	dev_t   dev;
    962 
    963 {
    964 	struct xy_softc *xysc;
    965 	int     unit, part, size, omask;
    966 
    967 	/* valid unit? */
    968 	unit = DISKUNIT(dev);
    969 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
    970 		return (-1);
    971 
    972 	part = DISKPART(dev);
    973 	omask = xysc->sc_dk.dk_openmask & (1 << part);
    974 
    975 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
    976 		return (-1);
    977 
    978 	/* do it */
    979 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    980 		size = -1;	/* only give valid size for swap partitions */
    981 	else
    982 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
    983 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    984 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
    985 		return (-1);
    986 	return (size);
    987 }
    988 
    989 /*
    990  * xystrategy: buffering system interface to xy.
    991  */
    992 
    993 void
    994 xystrategy(bp)
    995 	struct buf *bp;
    996 
    997 {
    998 	struct xy_softc *xy;
    999 	int     s, unit;
   1000 	struct xyc_attach_args xa;
   1001 
   1002 	unit = DISKUNIT(bp->b_dev);
   1003 
   1004 	/* check for live device */
   1005 
   1006 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
   1007 	    bp->b_blkno < 0 ||
   1008 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
   1009 		bp->b_error = EINVAL;
   1010 		goto bad;
   1011 	}
   1012 	/* do we need to attach the drive? */
   1013 
   1014 	if (xy->state == XY_DRIVE_UNKNOWN) {
   1015 		xa.driveno = xy->xy_drive;
   1016 		xa.dvmabuf = (char *)dvma_malloc(XYFM_BPS, &xa.buf, M_NOWAIT);
   1017 		xa.fullmode = XY_SUB_WAIT;
   1018 		xa.booting = 0;
   1019 		xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
   1020 		dvma_free(xa.dvmabuf, XYFM_BPS, &xa.buf);
   1021 		if (xy->state == XY_DRIVE_UNKNOWN) {
   1022 			bp->b_error = EIO;
   1023 			goto bad;
   1024 		}
   1025 	}
   1026 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
   1027 		/* no I/O to unlabeled disks, unless raw partition */
   1028 		bp->b_error = EIO;
   1029 		goto bad;
   1030 	}
   1031 	/* short circuit zero length request */
   1032 
   1033 	if (bp->b_bcount == 0)
   1034 		goto done;
   1035 
   1036 	/* check bounds with label (disksubr.c).  Determine the size of the
   1037 	 * transfer, and make sure it is within the boundaries of the
   1038 	 * partition. Adjust transfer if needed, and signal errors or early
   1039 	 * completion. */
   1040 
   1041 	if (bounds_check_with_label(bp, xy->sc_dk.dk_label,
   1042 		(xy->flags & XY_WLABEL) != 0) <= 0)
   1043 		goto done;
   1044 
   1045 	/*
   1046 	 * now we know we have a valid buf structure that we need to do I/O
   1047 	 * on.
   1048 	 */
   1049 	s = splbio();		/* protect the queues */
   1050 
   1051 	disksort(&xy->xyq, bp);
   1052 
   1053 	/* start 'em up */
   1054 
   1055 	xyc_start(xy->parent, NULL);
   1056 
   1057 	/* done! */
   1058 
   1059 	splx(s);
   1060 	return;
   1061 
   1062 bad:				/* tells upper layers we have an error */
   1063 	bp->b_flags |= B_ERROR;
   1064 done:				/* tells upper layers we are done with this
   1065 				 * buf */
   1066 	bp->b_resid = bp->b_bcount;
   1067 	biodone(bp);
   1068 }
   1069 /*
   1070  * end of {b,c}devsw functions
   1071  */
   1072 
   1073 /*
   1074  * i n t e r r u p t   f u n c t i o n
   1075  *
   1076  * xycintr: hardware interrupt.
   1077  */
   1078 int
   1079 xycintr(v)
   1080 	void   *v;
   1081 
   1082 {
   1083 	struct xyc_softc *xycsc = v;
   1084 
   1085 	/* kick the event counter */
   1086 
   1087 	xycsc->sc_intrcnt.ev_count++;
   1088 
   1089 	/* remove as many done IOPBs as possible */
   1090 
   1091 	xyc_remove_iorq(xycsc);
   1092 
   1093 	/* start any iorq's already waiting */
   1094 
   1095 	xyc_start(xycsc, NULL);
   1096 
   1097 	return (1);
   1098 }
   1099 /*
   1100  * end of interrupt function
   1101  */
   1102 
   1103 /*
   1104  * i n t e r n a l   f u n c t i o n s
   1105  */
   1106 
   1107 /*
   1108  * xyc_rqinit: fill out the fields of an I/O request
   1109  */
   1110 
   1111 inline void
   1112 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
   1113 	struct xy_iorq *rq;
   1114 	struct xyc_softc *xyc;
   1115 	struct xy_softc *xy;
   1116 	int     md;
   1117 	u_long  blk;
   1118 	int     cnt;
   1119 	caddr_t db;
   1120 	struct buf *bp;
   1121 {
   1122 	rq->xyc = xyc;
   1123 	rq->xy = xy;
   1124 	rq->ttl = XYC_MAXTTL + 10;
   1125 	rq->mode = md;
   1126 	rq->tries = rq->errno = rq->lasterror = 0;
   1127 	rq->blockno = blk;
   1128 	rq->sectcnt = cnt;
   1129 	rq->dbuf = rq->dbufbase = db;
   1130 	rq->buf = bp;
   1131 }
   1132 
   1133 /*
   1134  * xyc_rqtopb: load up an IOPB based on an iorq
   1135  */
   1136 
   1137 void
   1138 xyc_rqtopb(iorq, iopb, cmd, subfun)
   1139 	struct xy_iorq *iorq;
   1140 	struct xy_iopb *iopb;
   1141 	int     cmd, subfun;
   1142 
   1143 {
   1144 	u_long  block, dp;
   1145 
   1146 	/* normal IOPB case, standard stuff */
   1147 
   1148 	/* chain bit handled later */
   1149 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
   1150 	iopb->com = cmd;
   1151 	iopb->errno = 0;
   1152 	iopb->errs = 0;
   1153 	iopb->done = 0;
   1154 	if (iorq->xy) {
   1155 		iopb->unit = iorq->xy->xy_drive;
   1156 		iopb->dt = iorq->xy->drive_type;
   1157 	} else {
   1158 		iopb->unit = 0;
   1159 		iopb->dt = 0;
   1160 	}
   1161 	block = iorq->blockno;
   1162 	if (iorq->xy == NULL || block == 0) {
   1163 		iopb->sect = iopb->head = iopb->cyl = 0;
   1164 	} else {
   1165 		iopb->sect = block % iorq->xy->nsect;
   1166 		block = block / iorq->xy->nsect;
   1167 		iopb->head = block % iorq->xy->nhead;
   1168 		block = block / iorq->xy->nhead;
   1169 		iopb->cyl = block;
   1170 	}
   1171 	iopb->scnt = iorq->sectcnt;
   1172 	dp = (u_long) iorq->dbuf - DVMA_BASE;
   1173 	if (iorq->dbuf == NULL) {
   1174 		iopb->dataa = 0;
   1175 		iopb->datar = 0;
   1176 	} else {
   1177 		iopb->dataa = (dp & 0xffff);
   1178 		iopb->datar = ((dp & 0xff0000) >> 16);
   1179 	}
   1180 	iopb->subfn = subfun;
   1181 }
   1182 
   1183 
   1184 /*
   1185  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
   1186  */
   1187 
   1188 int
   1189 xyc_unbusy(xyc, del)
   1190 
   1191 struct xyc *xyc;
   1192 int del;
   1193 
   1194 {
   1195 	while (del-- > 0) {
   1196 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
   1197 			break;
   1198 		DELAY(1);
   1199 	}
   1200 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
   1201 }
   1202 
   1203 /*
   1204  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
   1205  * note that NORM requests are handled seperately.
   1206  */
   1207 int
   1208 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
   1209 	struct xyc_softc *xycsc;
   1210 	int     cmd, subfn, unit, block, scnt;
   1211 	char   *dptr;
   1212 	int     fullmode;
   1213 
   1214 {
   1215 	int     submode = XY_STATE(fullmode);
   1216 	struct xy_iorq *iorq = xycsc->ciorq;
   1217 	struct xy_iopb *iopb = xycsc->ciopb;
   1218 
   1219 	/*
   1220 	 * is someone else using the control iopq wait for it if we can
   1221 	 */
   1222 start:
   1223 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1224 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
   1225                                 return(XY_ERR_FAIL);
   1226 		goto start;
   1227 	}
   1228 
   1229 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1230 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
   1231 		iorq->mode = XY_SUB_FREE;
   1232 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
   1233 	}
   1234 
   1235 	/* init iorq/iopb */
   1236 
   1237 	xyc_rqinit(iorq, xycsc,
   1238 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
   1239 	    fullmode, block, scnt, dptr, NULL);
   1240 
   1241 	/* load IOPB from iorq */
   1242 
   1243 	xyc_rqtopb(iorq, iopb, cmd, subfn);
   1244 
   1245 	/* submit it for processing */
   1246 
   1247 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
   1248 
   1249 	return(XY_ERR_AOK);
   1250 }
   1251 
   1252 /*
   1253  * xyc_startbuf
   1254  * start a buffer for running
   1255  */
   1256 
   1257 int
   1258 xyc_startbuf(xycsc, xysc, bp)
   1259 	struct xyc_softc *xycsc;
   1260 	struct xy_softc *xysc;
   1261 	struct buf *bp;
   1262 
   1263 {
   1264 	int     partno;
   1265 	struct xy_iorq *iorq;
   1266 	struct xy_iopb *iopb;
   1267 	u_long  block;
   1268 	caddr_t dbuf;
   1269 
   1270 	iorq = xysc->xyrq;
   1271 	iopb = iorq->iopb;
   1272 
   1273 	/* get buf */
   1274 
   1275 	if (bp == NULL)
   1276 		panic("xyc_startbuf null buf");
   1277 
   1278 	partno = DISKPART(bp->b_dev);
   1279 #ifdef XYC_DEBUG
   1280 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
   1281 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
   1282 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
   1283 	    bp->b_bcount, bp->b_data);
   1284 #endif
   1285 
   1286 	/*
   1287 	 * load request.  we have to calculate the correct block number based
   1288 	 * on partition info.
   1289 	 *
   1290 	 * note that iorq points to the buffer as mapped into DVMA space,
   1291 	 * where as the bp->b_data points to its non-DVMA mapping.
   1292 	 */
   1293 
   1294 	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
   1295 	    xysc->sc_dk.dk_label->d_partitions[partno].p_offset);
   1296 
   1297 	dbuf = kdvma_mapin(bp->b_data, bp->b_bcount, 0);
   1298 	if (dbuf == NULL) {	/* out of DVMA space */
   1299 		printf("%s: warning: out of DVMA space\n",
   1300 			xycsc->sc_dev.dv_xname);
   1301 		return (XY_ERR_FAIL);	/* XXX: need some sort of
   1302 					 * call-back scheme here? */
   1303 	}
   1304 
   1305 	/* init iorq and load iopb from it */
   1306 
   1307 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
   1308 	    bp->b_bcount / XYFM_BPS, dbuf, bp);
   1309 
   1310 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
   1311 
   1312 	/* Instrumentation. */
   1313 	disk_busy(&xysc->sc_dk);
   1314 
   1315 	return (XY_ERR_AOK);
   1316 }
   1317 
   1318 
   1319 /*
   1320  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
   1321  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
   1322  *
   1323  * note: caller frees iorq in all cases except NORM
   1324  *
   1325  * return value:
   1326  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
   1327  *   WAIT: XY_AOK (success), <error-code> (failed)
   1328  *   POLL: <same as WAIT>
   1329  *   NOQ : <same as NORM>
   1330  *
   1331  * there are three sources for i/o requests:
   1332  * [1] xystrategy: normal block I/O, using "struct buf" system.
   1333  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
   1334  * [3] open/ioctl: these are I/O requests done in the context of a process,
   1335  *                 and the process should block until they are done.
   1336  *
   1337  * software state is stored in the iorq structure.  each iorq has an
   1338  * iopb structure.  the hardware understands the iopb structure.
   1339  * every command must go through an iopb.  a 450 handles one iopb at a
   1340  * time, where as a 451 can take them in chains.  [the 450 claims it
   1341  * can handle chains, but is appears to be buggy...]   iopb are allocated
   1342  * in DVMA space at boot up time.  each disk gets one iopb, and the
   1343  * controller gets one (for POLL and WAIT commands).  what happens if
   1344  * the iopb is busy?  for i/o type [1], the buffers are queued at the
   1345  * "buff" layer and * picked up later by the interrupt routine.  for case
   1346  * [2] we can only be blocked if there is a WAIT type I/O request being
   1347  * run.   since this can only happen when we are crashing, we wait a sec
   1348  * and then steal the IOPB.  for case [3] the process can sleep
   1349  * on the iorq free list until some iopbs are avaliable.
   1350  */
   1351 
   1352 
   1353 int
   1354 xyc_submit_iorq(xycsc, iorq, type)
   1355 	struct xyc_softc *xycsc;
   1356 	struct xy_iorq *iorq;
   1357 	int     type;
   1358 
   1359 {
   1360 	struct xy_iopb *iopb;
   1361 	u_long  iopbaddr;
   1362 
   1363 #ifdef XYC_DEBUG
   1364 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
   1365 		xycsc->sc_dev.dv_xname, iorq, type);
   1366 #endif
   1367 
   1368 	/* first check and see if controller is busy */
   1369 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
   1370 #ifdef XYC_DEBUG
   1371 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
   1372 #endif
   1373 		if (type == XY_SUB_NOQ)
   1374 			return (XY_ERR_FAIL);	/* failed */
   1375 		switch (type) {
   1376 		case XY_SUB_NORM:
   1377 			return XY_ERR_AOK;	/* success */
   1378 		case XY_SUB_WAIT:
   1379 			while (iorq->iopb->done == 0) {
   1380 				sleep(iorq, PRIBIO);
   1381 			}
   1382 			return (iorq->errno);
   1383 		case XY_SUB_POLL:		/* steal controller */
   1384 			iopbaddr = xycsc->xyc->xyc_rsetup; /* RESET */
   1385 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
   1386 				panic("xyc_submit_iorq: stuck xyc");
   1387 			printf("%s: stole controller\n",
   1388 				xycsc->sc_dev.dv_xname);
   1389 			break;
   1390 		default:
   1391 			panic("xyc_submit_iorq adding");
   1392 		}
   1393 	}
   1394 
   1395 	iopb = xyc_chain(xycsc, iorq);	 /* build chain */
   1396 	if (iopb == NULL) { /* nothing doing? */
   1397 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
   1398 			return(XY_ERR_AOK);
   1399 		panic("xyc_submit_iorq: xyc_chain failed!\n");
   1400 	}
   1401 	iopbaddr = (u_long) iopb - DVMA_BASE;
   1402 
   1403 	XYC_GO(xycsc->xyc, iopbaddr);
   1404 
   1405 	/* command now running, wrap it up */
   1406 	switch (type) {
   1407 	case XY_SUB_NORM:
   1408 	case XY_SUB_NOQ:
   1409 		return (XY_ERR_AOK);	/* success */
   1410 	case XY_SUB_WAIT:
   1411 		while (iorq->iopb->done == 0) {
   1412 			sleep(iorq, PRIBIO);
   1413 		}
   1414 		return (iorq->errno);
   1415 	case XY_SUB_POLL:
   1416 		return (xyc_piodriver(xycsc, iorq));
   1417 	default:
   1418 		panic("xyc_submit_iorq wrap up");
   1419 	}
   1420 	panic("xyc_submit_iorq");
   1421 	return 0;	/* not reached */
   1422 }
   1423 
   1424 
   1425 /*
   1426  * xyc_chain: build a chain.  return dvma address of first element in
   1427  * the chain.   iorq != NULL: means we only want that item on the chain.
   1428  */
   1429 
   1430 struct xy_iopb *
   1431 xyc_chain(xycsc, iorq)
   1432 
   1433 struct xyc_softc *xycsc;
   1434 struct xy_iorq *iorq;
   1435 
   1436 {
   1437   int togo, chain, hand;
   1438   struct xy_iopb *iopb, *prev_iopb;
   1439   bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
   1440 
   1441   /*
   1442    * promote control IOPB to the top
   1443    */
   1444   if (iorq == NULL) {
   1445     if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
   1446         XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
   1447 		xycsc->iopbase[XYC_CTLIOPB].done == 0)
   1448     iorq = &xycsc->reqs[XYC_CTLIOPB];
   1449   }
   1450   /*
   1451    * special case: if iorq != NULL then we have a POLL or WAIT request.
   1452    * we let these take priority and do them first.
   1453    */
   1454   if (iorq) {
   1455     xycsc->xy_chain[0] = iorq;
   1456     iorq->iopb->chen = 0;
   1457     return(iorq->iopb);
   1458   }
   1459 
   1460   /*
   1461    * NORM case: do round robin and maybe chain (if allowed and possible)
   1462    */
   1463 
   1464   chain = 0;
   1465   hand = xycsc->xy_hand;
   1466   xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
   1467 
   1468   for (togo = XYC_MAXIOPB ; togo > 0 ; togo--, hand = (hand + 1) % XYC_MAXIOPB){
   1469 
   1470     if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
   1471 		xycsc->iopbase[hand].done)
   1472       continue;   /* not ready-for-i/o */
   1473 
   1474     xycsc->xy_chain[chain] = &xycsc->reqs[hand];
   1475     iopb = xycsc->xy_chain[chain]->iopb;
   1476     iopb->chen = 0;
   1477     if (chain != 0) {   /* adding a link to a chain? */
   1478       prev_iopb = xycsc->xy_chain[chain-1]->iopb;
   1479       prev_iopb->chen = 1;
   1480       prev_iopb->nxtiopb = ((u_long) iopb - DVMA_BASE) & 0xffff;
   1481     } else {            /* head of chain */
   1482       iorq = xycsc->xy_chain[chain];
   1483     }
   1484     chain++;
   1485     if (xycsc->no_ols) break;   /* quit if chaining dis-allowed */
   1486   }
   1487   return(iorq ? iorq->iopb : NULL);
   1488 }
   1489 
   1490 /*
   1491  * xyc_piodriver
   1492  *
   1493  * programmed i/o driver.   this function takes over the computer
   1494  * and drains off the polled i/o request.   it returns the status of the iorq
   1495  * the caller is interesting in.
   1496  */
   1497 int
   1498 xyc_piodriver(xycsc, iorq)
   1499 	struct xyc_softc *xycsc;
   1500 	struct xy_iorq  *iorq;
   1501 
   1502 {
   1503 	int     nreset = 0;
   1504 	int     retval = 0;
   1505 	u_long  res;
   1506 #ifdef XYC_DEBUG
   1507 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
   1508 #endif
   1509 
   1510 	while (iorq->iopb->done == 0) {
   1511 
   1512 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
   1513 
   1514 		/* we expect some progress soon */
   1515 		if (res == XY_ERR_FAIL && nreset >= 2) {
   1516 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
   1517 #ifdef XYC_DEBUG
   1518 			printf("xyc_piodriver: timeout\n");
   1519 #endif
   1520 			return (XY_ERR_FAIL);
   1521 		}
   1522 		if (res == XY_ERR_FAIL) {
   1523 			if (xyc_reset(xycsc, 0,
   1524 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
   1525 				      XY_ERR_FAIL,
   1526 				      0) == XY_ERR_FAIL)
   1527 				return (XY_ERR_FAIL);	/* flushes all but POLL
   1528 							 * requests, resets */
   1529 			continue;
   1530 		}
   1531 
   1532 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
   1533 
   1534 		if (iorq->iopb->done == 0)
   1535 			xyc_start(xycsc, iorq);
   1536 	}
   1537 
   1538 	/* get return value */
   1539 
   1540 	retval = iorq->errno;
   1541 
   1542 #ifdef XYC_DEBUG
   1543 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
   1544 	    iorq->errno, xyc_e2str(iorq->errno));
   1545 #endif
   1546 
   1547 	/* start up any bufs that have queued */
   1548 
   1549 	xyc_start(xycsc, NULL);
   1550 
   1551 	return (retval);
   1552 }
   1553 
   1554 /*
   1555  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
   1556  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
   1557  */
   1558 void
   1559 xyc_xyreset(xycsc, xysc)
   1560 	struct xyc_softc *xycsc;
   1561 	struct xy_softc *xysc;
   1562 
   1563 {
   1564 	struct xy_iopb tmpiopb;
   1565 	u_long  addr;
   1566 	int     del;
   1567 	bcopy(xycsc->ciopb, &tmpiopb, sizeof(tmpiopb));
   1568 	xycsc->ciopb->chen = xycsc->ciopb->done = xycsc->ciopb->errs = 0;
   1569 	xycsc->ciopb->ien = 0;
   1570 	xycsc->ciopb->com = XYCMD_RST;
   1571 	xycsc->ciopb->unit = xysc->xy_drive;
   1572 	addr = (u_long) xycsc->ciopb - DVMA_BASE;
   1573 
   1574 	XYC_GO(xycsc->xyc, addr);
   1575 
   1576 	del = XYC_RESETUSEC;
   1577 	while (del > 0) {
   1578 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0) break;
   1579 		DELAY(1);
   1580 		del--;
   1581 	}
   1582 
   1583 	if (del <= 0 || xycsc->ciopb->errs) {
   1584 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
   1585 		    xyc_e2str(xycsc->ciopb->errno));
   1586 		del = xycsc->xyc->xyc_rsetup;
   1587 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
   1588 			panic("xyc_reset");
   1589 	} else {
   1590 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
   1591 	}
   1592 	bcopy(&tmpiopb, xycsc->ciopb, sizeof(tmpiopb));
   1593 }
   1594 
   1595 
   1596 /*
   1597  * xyc_reset: reset everything: requests are marked as errors except
   1598  * a polled request (which is resubmitted)
   1599  */
   1600 int
   1601 xyc_reset(xycsc, quiet, blastmode, error, xysc)
   1602 	struct xyc_softc *xycsc;
   1603 	int     quiet, error;
   1604 	struct xy_iorq *blastmode;
   1605 	struct xy_softc *xysc;
   1606 
   1607 {
   1608 	int     del = 0, lcv, retval = XY_ERR_AOK;
   1609 
   1610 	/* soft reset hardware */
   1611 
   1612 	if (!quiet)
   1613 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
   1614 	del = xycsc->xyc->xyc_rsetup;
   1615 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
   1616 	if (del == XY_ERR_FAIL) {
   1617 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
   1618 		retval = XY_ERR_FAIL;
   1619 	}
   1620 	if (xysc)
   1621 		xyc_xyreset(xycsc, xysc);
   1622 
   1623 	/* fix queues based on "blast-mode" */
   1624 
   1625 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   1626 		register struct xy_iorq *iorq = &xycsc->reqs[lcv];
   1627 
   1628 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
   1629 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
   1630 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
   1631 			/* is it active? */
   1632 			continue;
   1633 
   1634 		if (blastmode == XY_RSET_ALL ||
   1635 				blastmode != iorq) {
   1636 			/* failed */
   1637 			iorq->errno = error;
   1638 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
   1639 			switch (XY_STATE(iorq->mode)) {
   1640 			case XY_SUB_NORM:
   1641 			    iorq->buf->b_error = EIO;
   1642 			    iorq->buf->b_flags |= B_ERROR;
   1643 			    iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
   1644 			    dvma_mapout((vm_offset_t)iorq->dbufbase,
   1645 					(vm_offset_t)iorq->buf->b_un.b_addr,
   1646 					iorq->buf->b_bcount);
   1647 			    iorq->xy->xyq.b_actf = iorq->buf->b_actf;
   1648 			    disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
   1649 				(xycsc->reqs[lcv].buf->b_bcount -
   1650 				xycsc->reqs[lcv].buf->b_resid));
   1651 			    biodone(iorq->buf);
   1652 			    iorq->mode = XY_SUB_FREE;
   1653 			    break;
   1654 			case XY_SUB_WAIT:
   1655 			    wakeup(iorq);
   1656 			case XY_SUB_POLL:
   1657 			    iorq->mode =
   1658 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1659 			    break;
   1660 			}
   1661 
   1662 		} else {
   1663 
   1664 			/* resubmit, no need to do anything here */
   1665 		}
   1666 	}
   1667 
   1668 	/*
   1669 	 * now, if stuff is waiting, start it.
   1670 	 * since we just reset it should go
   1671 	 */
   1672 	xyc_start(xycsc, NULL);
   1673 
   1674 	return (retval);
   1675 }
   1676 
   1677 /*
   1678  * xyc_start: start waiting buffers
   1679  */
   1680 
   1681 void
   1682 xyc_start(xycsc, iorq)
   1683 	struct xyc_softc *xycsc;
   1684 	struct xy_iorq *iorq;
   1685 
   1686 {
   1687 	int lcv;
   1688 	struct xy_softc *xy;
   1689 
   1690 	if (iorq == NULL) {
   1691 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
   1692 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
   1693 			if (xy->xyq.b_actf == NULL) continue;
   1694 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
   1695 			xyc_startbuf(xycsc, xy, xy->xyq.b_actf);
   1696 		}
   1697 	}
   1698 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
   1699 }
   1700 
   1701 /*
   1702  * xyc_remove_iorq: remove "done" IOPB's.
   1703  */
   1704 
   1705 int
   1706 xyc_remove_iorq(xycsc)
   1707 	struct xyc_softc *xycsc;
   1708 
   1709 {
   1710 	int     errno, rq, comm, errs;
   1711 	struct xyc *xyc = xycsc->xyc;
   1712 	u_long  addr;
   1713 	struct xy_iopb *iopb;
   1714 	struct xy_iorq *iorq;
   1715 	struct buf *bp;
   1716 
   1717 	if (xyc->xyc_csr & XYC_DERR) {
   1718 		/*
   1719 		 * DOUBLE ERROR: should never happen under normal use. This
   1720 		 * error is so bad, you can't even tell which IOPB is bad, so
   1721 		 * we dump them all.
   1722 		 */
   1723 		errno = XY_ERR_DERR;
   1724 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
   1725 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
   1726 			printf("%s: soft reset failed!\n",
   1727 				xycsc->sc_dev.dv_xname);
   1728 			panic("xyc_remove_iorq: controller DEAD");
   1729 		}
   1730 		return (XY_ERR_AOK);
   1731 	}
   1732 
   1733 	/*
   1734 	 * get iopb that is done, loop down the chain
   1735 	 */
   1736 
   1737 	if (xyc->xyc_csr & XYC_ERR) {
   1738 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
   1739 	}
   1740 	if (xyc->xyc_csr & XYC_IPND) {
   1741 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
   1742 	}
   1743 
   1744 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
   1745 		iorq = xycsc->xy_chain[rq];
   1746 		if (iorq == NULL) break; /* done ! */
   1747 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
   1748 			continue;	/* free, or done */
   1749 		iopb = iorq->iopb;
   1750 		if (iopb->done == 0)
   1751 			continue;	/* not done yet */
   1752 
   1753 		comm = iopb->com;
   1754 		errs = iopb->errs;
   1755 
   1756 		if (errs)
   1757 			iorq->errno = iopb->errno;
   1758 		else
   1759 			iorq->errno = 0;
   1760 
   1761 		/* handle non-fatal errors */
   1762 
   1763 		if (errs &&
   1764 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
   1765 			continue;	/* AOK: we resubmitted it */
   1766 
   1767 
   1768 		/* this iorq is now done (hasn't been restarted or anything) */
   1769 
   1770 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   1771 			xyc_perror(iorq, iopb, 0);
   1772 
   1773 		/* now, if read/write check to make sure we got all the data
   1774 		 * we needed. (this may not be the case if we got an error in
   1775 		 * the middle of a multisector request).   */
   1776 
   1777 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
   1778 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
   1779 			/* we just successfully processed a bad144 sector
   1780 			 * note: if we are in bad 144 mode, the pointers have
   1781 			 * been advanced already (see above) and are pointing
   1782 			 * at the bad144 sector.   to exit bad144 mode, we
   1783 			 * must advance the pointers 1 sector and issue a new
   1784 			 * request if there are still sectors left to process
   1785 			 *
   1786 			 */
   1787 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
   1788 
   1789 			/* exit b144 mode */
   1790 			iorq->mode = iorq->mode & (~XY_MODE_B144);
   1791 
   1792 			if (iorq->sectcnt) {	/* more to go! */
   1793 				iorq->lasterror = iorq->errno = iopb->errno = 0;
   1794 				iopb->errs = iopb->done = 0;
   1795 				iorq->tries = 0;
   1796 				iopb->scnt = iorq->sectcnt;
   1797 				iopb->cyl = iorq->blockno /
   1798 						iorq->xy->sectpercyl;
   1799 				iopb->head =
   1800 					(iorq->blockno / iorq->xy->nhead) %
   1801 						iorq->xy->nhead;
   1802 				iopb->sect = iorq->blockno % XYFM_BPS;
   1803 				addr = (u_long) iorq->dbuf - DVMA_BASE;
   1804 				iopb->dataa = (addr & 0xffff);
   1805 				iopb->datar = ((addr & 0xff0000) >> 16);
   1806 				/* will resubit at end */
   1807 				continue;
   1808 			}
   1809 		}
   1810 		/* final cleanup, totally done with this request */
   1811 
   1812 		switch (XY_STATE(iorq->mode)) {
   1813 		case XY_SUB_NORM:
   1814 			bp = iorq->buf;
   1815 			if (errs) {
   1816 				bp->b_error = EIO;
   1817 				bp->b_flags |= B_ERROR;
   1818 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
   1819 			} else {
   1820 				bp->b_resid = 0;	/* done */
   1821 			}
   1822 			dvma_mapout((vm_offset_t) iorq->dbufbase,
   1823 				    (vm_offset_t) bp->b_un.b_addr,
   1824 				    bp->b_bcount);
   1825 			iorq->xy->xyq.b_actf = bp->b_actf;
   1826 			disk_unbusy(&iorq->xy->sc_dk,
   1827 			    (bp->b_bcount - bp->b_resid));
   1828 			iorq->mode = XY_SUB_FREE;
   1829 			biodone(bp);
   1830 			break;
   1831 		case XY_SUB_WAIT:
   1832 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1833 			wakeup(iorq);
   1834 			break;
   1835 		case XY_SUB_POLL:
   1836 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1837 			break;
   1838 		}
   1839 	}
   1840 
   1841 	return (XY_ERR_AOK);
   1842 }
   1843 
   1844 /*
   1845  * xyc_perror: print error.
   1846  * - if still_trying is true: we got an error, retried and got a
   1847  *   different error.  in that case lasterror is the old error,
   1848  *   and errno is the new one.
   1849  * - if still_trying is not true, then if we ever had an error it
   1850  *   is in lasterror. also, if iorq->errno == 0, then we recovered
   1851  *   from that error (otherwise iorq->errno == iorq->lasterror).
   1852  */
   1853 void
   1854 xyc_perror(iorq, iopb, still_trying)
   1855 	struct xy_iorq *iorq;
   1856 	struct xy_iopb *iopb;
   1857 	int     still_trying;
   1858 
   1859 {
   1860 
   1861 	int     error = iorq->lasterror;
   1862 
   1863 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
   1864 	    : iorq->xyc->sc_dev.dv_xname);
   1865 	if (iorq->buf)
   1866 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
   1867 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
   1868 		printf("%s %d/%d/%d: ",
   1869 			(iopb->com == XYCMD_RD) ? "read" : "write",
   1870 			iopb->cyl, iopb->head, iopb->sect);
   1871 	printf("%s", xyc_e2str(error));
   1872 
   1873 	if (still_trying)
   1874 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
   1875 	else
   1876 		if (iorq->errno == 0)
   1877 			printf(" [recovered in %d tries]", iorq->tries);
   1878 
   1879 	printf("\n");
   1880 }
   1881 
   1882 /*
   1883  * xyc_error: non-fatal error encountered... recover.
   1884  * return AOK if resubmitted, return FAIL if this iopb is done
   1885  */
   1886 int
   1887 xyc_error(xycsc, iorq, iopb, comm)
   1888 	struct xyc_softc *xycsc;
   1889 	struct xy_iorq *iorq;
   1890 	struct xy_iopb *iopb;
   1891 	int     comm;
   1892 
   1893 {
   1894 	int     errno = iorq->errno;
   1895 	int     erract = xyc_entoact(errno);
   1896 	int     oldmode, advance, i;
   1897 
   1898 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
   1899 		oldmode = iorq->mode;
   1900 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
   1901 		/* make xyc_start ignore us */
   1902 		xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
   1903 		iorq->mode = oldmode;
   1904 	}
   1905 	/* check for read/write to a sector in bad144 table if bad: redirect
   1906 	 * request to bad144 area */
   1907 
   1908 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
   1909 	    (iorq->mode & XY_MODE_B144) == 0) {
   1910 		advance = iorq->sectcnt - iopb->scnt;
   1911 		XYC_ADVANCE(iorq, advance);
   1912 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
   1913 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
   1914 			    iorq->blockno % iorq->xy->nsect)) != -1) {
   1915 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
   1916 							 * redirect */
   1917 			iopb->errno = iopb->done = iopb->errs = 0;
   1918 			iopb->scnt = 1;
   1919 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
   1920 			/* second to last acyl */
   1921 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
   1922 								 * standard */
   1923 			iopb->head = i / iorq->xy->nhead;
   1924 			iopb->sect = i % iorq->xy->nhead;
   1925 			/* will resubmit when we come out of remove_iorq */
   1926 			return (XY_ERR_AOK);	/* recovered! */
   1927 		}
   1928 	}
   1929 
   1930 	/*
   1931 	 * it isn't a bad144 sector, must be real error! see if we can retry
   1932 	 * it?
   1933 	 */
   1934 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   1935 		xyc_perror(iorq, iopb, 1);	/* inform of error state
   1936 						 * change */
   1937 	iorq->lasterror = errno;
   1938 
   1939 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
   1940 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
   1941 		iorq->tries++;
   1942 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
   1943 		/* will resubmit at end of remove_iorq */
   1944 		return (XY_ERR_AOK);	/* recovered! */
   1945 	}
   1946 
   1947 	/* failed to recover from this error */
   1948 	return (XY_ERR_FAIL);
   1949 }
   1950 
   1951 /*
   1952  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
   1953  */
   1954 void
   1955 xyc_tick(arg)
   1956 	void   *arg;
   1957 
   1958 {
   1959 	struct xyc_softc *xycsc = arg;
   1960 	int     lcv, s, reset = 0;
   1961 
   1962 	/* reduce ttl for each request if one goes to zero, reset xyc */
   1963 	s = splbio();
   1964 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   1965 		if (xycsc->reqs[lcv].mode == 0 ||
   1966 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
   1967 			continue;
   1968 		xycsc->reqs[lcv].ttl--;
   1969 		if (xycsc->reqs[lcv].ttl == 0)
   1970 			reset = 1;
   1971 	}
   1972 	if (reset) {
   1973 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
   1974 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
   1975 	}
   1976 	splx(s);
   1977 
   1978 	/* until next time */
   1979 
   1980 	timeout(xyc_tick, xycsc, XYC_TICKCNT);
   1981 }
   1982 
   1983 /*
   1984  * xyc_ioctlcmd: this function provides a user level interface to the
   1985  * controller via ioctl.   this allows "format" programs to be written
   1986  * in user code, and is also useful for some debugging.   we return
   1987  * an error code.   called at user priority.
   1988  *
   1989  * XXX missing a few commands (see the 7053 driver for ideas)
   1990  */
   1991 int
   1992 xyc_ioctlcmd(xy, dev, xio)
   1993 	struct xy_softc *xy;
   1994 	dev_t   dev;
   1995 	struct xd_iocmd *xio;
   1996 
   1997 {
   1998 	int     s, err, rqno, dummy = 0;
   1999 	caddr_t dvmabuf = NULL, buf = NULL;
   2000 	struct xyc_softc *xycsc;
   2001 
   2002 	/* check sanity of requested command */
   2003 
   2004 	switch (xio->cmd) {
   2005 
   2006 	case XYCMD_NOP:	/* no op: everything should be zero */
   2007 		if (xio->subfn || xio->dptr || xio->dlen ||
   2008 		    xio->block || xio->sectcnt)
   2009 			return (EINVAL);
   2010 		break;
   2011 
   2012 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
   2013 	case XYCMD_WR:
   2014 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
   2015 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
   2016 			return (EINVAL);
   2017 		break;
   2018 
   2019 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
   2020 		return (EINVAL);
   2021 
   2022 		break;
   2023 
   2024 	default:
   2025 		return (EINVAL);/* ??? */
   2026 	}
   2027 
   2028 	/* create DVMA buffer for request if needed */
   2029 
   2030 	if (xio->dlen) {
   2031 		dvmabuf = dvma_malloc(xio->dlen, &buf, M_WAITOK);
   2032 		if (xio->cmd == XYCMD_WR) {
   2033 			if ((err = copyin(xio->dptr, buf, xio->dlen)) != 0) {
   2034 				dvma_free(dvmabuf, xio->dlen, &buf);
   2035 				return (err);
   2036 			}
   2037 		}
   2038 	}
   2039 	/* do it! */
   2040 
   2041 	err = 0;
   2042 	xycsc = xy->parent;
   2043 	s = splbio();
   2044 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
   2045 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
   2046 	if (rqno == XY_ERR_FAIL) {
   2047 		err = EIO;
   2048 		goto done;
   2049 	}
   2050 	xio->errno = xycsc->ciorq->errno;
   2051 	xio->tries = xycsc->ciorq->tries;
   2052 	XYC_DONE(xycsc, dummy);
   2053 
   2054 	if (xio->cmd == XYCMD_RD)
   2055 		err = copyout(buf, xio->dptr, xio->dlen);
   2056 
   2057 done:
   2058 	splx(s);
   2059 	if (dvmabuf)
   2060 		dvma_free(dvmabuf, xio->dlen, &buf);
   2061 	return (err);
   2062 }
   2063 
   2064 /*
   2065  * xyc_e2str: convert error code number into an error string
   2066  */
   2067 char *
   2068 xyc_e2str(no)
   2069 	int     no;
   2070 {
   2071 	switch (no) {
   2072 	case XY_ERR_FAIL:
   2073 		return ("Software fatal error");
   2074 	case XY_ERR_DERR:
   2075 		return ("DOUBLE ERROR");
   2076 	case XY_ERR_AOK:
   2077 		return ("Successful completion");
   2078 	case XY_ERR_IPEN:
   2079 		return("Interrupt pending");
   2080 	case XY_ERR_BCFL:
   2081 		return("Busy conflict");
   2082 	case XY_ERR_TIMO:
   2083 		return("Operation timeout");
   2084 	case XY_ERR_NHDR:
   2085 		return("Header not found");
   2086 	case XY_ERR_HARD:
   2087 		return("Hard ECC error");
   2088 	case XY_ERR_ICYL:
   2089 		return("Illegal cylinder address");
   2090 	case XY_ERR_ISEC:
   2091 		return("Illegal sector address");
   2092 	case XY_ERR_SMAL:
   2093 		return("Last sector too small");
   2094 	case XY_ERR_SACK:
   2095 		return("Slave ACK error (non-existent memory)");
   2096 	case XY_ERR_CHER:
   2097 		return("Cylinder and head/header error");
   2098 	case XY_ERR_SRTR:
   2099 		return("Auto-seek retry successful");
   2100 	case XY_ERR_WPRO:
   2101 		return("Write-protect error");
   2102 	case XY_ERR_UIMP:
   2103 		return("Unimplemented command");
   2104 	case XY_ERR_DNRY:
   2105 		return("Drive not ready");
   2106 	case XY_ERR_SZER:
   2107 		return("Sector count zero");
   2108 	case XY_ERR_DFLT:
   2109 		return("Drive faulted");
   2110 	case XY_ERR_ISSZ:
   2111 		return("Illegal sector size");
   2112 	case XY_ERR_SLTA:
   2113 		return("Self test A");
   2114 	case XY_ERR_SLTB:
   2115 		return("Self test B");
   2116 	case XY_ERR_SLTC:
   2117 		return("Self test C");
   2118 	case XY_ERR_SOFT:
   2119 		return("Soft ECC error");
   2120 	case XY_ERR_SFOK:
   2121 		return("Soft ECC error recovered");
   2122 	case XY_ERR_IHED:
   2123 		return("Illegal head");
   2124 	case XY_ERR_DSEQ:
   2125 		return("Disk sequencer error");
   2126 	case XY_ERR_SEEK:
   2127 		return("Seek error");
   2128 	default:
   2129 		return ("Unknown error");
   2130 	}
   2131 }
   2132 
   2133 int
   2134 xyc_entoact(errno)
   2135 
   2136 int errno;
   2137 
   2138 {
   2139   switch (errno) {
   2140     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
   2141     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
   2142     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
   2143     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
   2144     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
   2145 
   2146 	return(XY_ERA_PROG); /* program error ! */
   2147 
   2148     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
   2149     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
   2150     case XY_ERR_SOFT:
   2151 
   2152 	return(XY_ERA_HARD); /* hard error, retry */
   2153 
   2154     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
   2155 
   2156 	return(XY_ERA_RSET); /* hard error reset */
   2157 
   2158     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
   2159 
   2160 	return(XY_ERA_SOFT); /* an FYI error */
   2161 
   2162     case XY_ERR_WPRO:
   2163 
   2164 	return(XY_ERA_WPRO); /* write protect */
   2165   }
   2166 
   2167   return(XY_ERA_PROG); /* ??? */
   2168 }
   2169