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