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