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