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