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