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