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