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