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