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