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