Home | History | Annotate | Line # | Download | only in vme
xy.c revision 1.15
      1 /*	$NetBSD: xy.c,v 1.15 2000/01/21 23:43:10 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 
   1049 	unit = DISKUNIT(bp->b_dev);
   1050 
   1051 	/* check for live device */
   1052 
   1053 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
   1054 	    bp->b_blkno < 0 ||
   1055 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
   1056 		bp->b_error = EINVAL;
   1057 		goto bad;
   1058 	}
   1059 	/* do we need to attach the drive? */
   1060 
   1061 	if (xy->state == XY_DRIVE_UNKNOWN) {
   1062 		xa.driveno = xy->xy_drive;
   1063 		xa.fullmode = XY_SUB_WAIT;
   1064 		xa.booting = 0;
   1065 		xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
   1066 		if (xy->state == XY_DRIVE_UNKNOWN) {
   1067 			bp->b_error = EIO;
   1068 			goto bad;
   1069 		}
   1070 	}
   1071 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
   1072 		/* no I/O to unlabeled disks, unless raw partition */
   1073 		bp->b_error = EIO;
   1074 		goto bad;
   1075 	}
   1076 	/* short circuit zero length request */
   1077 
   1078 	if (bp->b_bcount == 0)
   1079 		goto done;
   1080 
   1081 	/* check bounds with label (disksubr.c).  Determine the size of the
   1082 	 * transfer, and make sure it is within the boundaries of the
   1083 	 * partition. Adjust transfer if needed, and signal errors or early
   1084 	 * completion. */
   1085 
   1086 	if (bounds_check_with_label(bp, xy->sc_dk.dk_label,
   1087 		(xy->flags & XY_WLABEL) != 0) <= 0)
   1088 		goto done;
   1089 
   1090 	/*
   1091 	 * now we know we have a valid buf structure that we need to do I/O
   1092 	 * on.
   1093 	 */
   1094 	s = splbio();		/* protect the queues */
   1095 
   1096 	disksort_blkno(&xy->xyq, bp);	/* XXX disksort_cylinder */
   1097 
   1098 	/* start 'em up */
   1099 
   1100 	xyc_start(xy->parent, NULL);
   1101 
   1102 	/* done! */
   1103 
   1104 	splx(s);
   1105 	return;
   1106 
   1107 bad:				/* tells upper layers we have an error */
   1108 	bp->b_flags |= B_ERROR;
   1109 done:				/* tells upper layers we are done with this
   1110 				 * buf */
   1111 	bp->b_resid = bp->b_bcount;
   1112 	biodone(bp);
   1113 }
   1114 /*
   1115  * end of {b,c}devsw functions
   1116  */
   1117 
   1118 /*
   1119  * i n t e r r u p t   f u n c t i o n
   1120  *
   1121  * xycintr: hardware interrupt.
   1122  */
   1123 int
   1124 xycintr(v)
   1125 	void   *v;
   1126 
   1127 {
   1128 	struct xyc_softc *xycsc = v;
   1129 
   1130 	/* kick the event counter */
   1131 
   1132 	xycsc->sc_intrcnt.ev_count++;
   1133 
   1134 	/* remove as many done IOPBs as possible */
   1135 
   1136 	xyc_remove_iorq(xycsc);
   1137 
   1138 	/* start any iorq's already waiting */
   1139 
   1140 	xyc_start(xycsc, NULL);
   1141 
   1142 	return (1);
   1143 }
   1144 /*
   1145  * end of interrupt function
   1146  */
   1147 
   1148 /*
   1149  * i n t e r n a l   f u n c t i o n s
   1150  */
   1151 
   1152 /*
   1153  * xyc_rqinit: fill out the fields of an I/O request
   1154  */
   1155 
   1156 inline void
   1157 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
   1158 	struct xy_iorq *rq;
   1159 	struct xyc_softc *xyc;
   1160 	struct xy_softc *xy;
   1161 	int     md;
   1162 	u_long  blk;
   1163 	int     cnt;
   1164 	caddr_t db;
   1165 	struct buf *bp;
   1166 {
   1167 	rq->xyc = xyc;
   1168 	rq->xy = xy;
   1169 	rq->ttl = XYC_MAXTTL + 10;
   1170 	rq->mode = md;
   1171 	rq->tries = rq->errno = rq->lasterror = 0;
   1172 	rq->blockno = blk;
   1173 	rq->sectcnt = cnt;
   1174 	rq->dbuf = db;
   1175 	rq->buf = bp;
   1176 }
   1177 
   1178 /*
   1179  * xyc_rqtopb: load up an IOPB based on an iorq
   1180  */
   1181 
   1182 void
   1183 xyc_rqtopb(iorq, iopb, cmd, subfun)
   1184 	struct xy_iorq *iorq;
   1185 	struct xy_iopb *iopb;
   1186 	int     cmd, subfun;
   1187 
   1188 {
   1189 	u_long  block, dp;
   1190 
   1191 	/* normal IOPB case, standard stuff */
   1192 
   1193 	/* chain bit handled later */
   1194 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
   1195 	iopb->com = cmd;
   1196 	iopb->errno = 0;
   1197 	iopb->errs = 0;
   1198 	iopb->done = 0;
   1199 	if (iorq->xy) {
   1200 		iopb->unit = iorq->xy->xy_drive;
   1201 		iopb->dt = iorq->xy->drive_type;
   1202 	} else {
   1203 		iopb->unit = 0;
   1204 		iopb->dt = 0;
   1205 	}
   1206 	block = iorq->blockno;
   1207 	if (iorq->xy == NULL || block == 0) {
   1208 		iopb->sect = iopb->head = iopb->cyl = 0;
   1209 	} else {
   1210 		iopb->sect = block % iorq->xy->nsect;
   1211 		block = block / iorq->xy->nsect;
   1212 		iopb->head = block % iorq->xy->nhead;
   1213 		block = block / iorq->xy->nhead;
   1214 		iopb->cyl = block;
   1215 	}
   1216 	iopb->scnt = iorq->sectcnt;
   1217 	dp = (u_long) iorq->dbuf;
   1218 	if (iorq->dbuf == NULL) {
   1219 		iopb->dataa = 0;
   1220 		iopb->datar = 0;
   1221 	} else {
   1222 		iopb->dataa = (dp & 0xffff);
   1223 		iopb->datar = ((dp & 0xff0000) >> 16);
   1224 	}
   1225 	iopb->subfn = subfun;
   1226 }
   1227 
   1228 
   1229 /*
   1230  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
   1231  */
   1232 
   1233 int
   1234 xyc_unbusy(xyc, del)
   1235 
   1236 struct xyc *xyc;
   1237 int del;
   1238 
   1239 {
   1240 	while (del-- > 0) {
   1241 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
   1242 			break;
   1243 		DELAY(1);
   1244 	}
   1245 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
   1246 }
   1247 
   1248 /*
   1249  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
   1250  * note that NORM requests are handled seperately.
   1251  */
   1252 int
   1253 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
   1254 	struct xyc_softc *xycsc;
   1255 	int     cmd, subfn, unit, block, scnt;
   1256 	char   *dptr;
   1257 	int     fullmode;
   1258 
   1259 {
   1260 	int     submode = XY_STATE(fullmode);
   1261 	struct xy_iorq *iorq = xycsc->ciorq;
   1262 	struct xy_iopb *iopb = xycsc->ciopb;
   1263 
   1264 	/*
   1265 	 * is someone else using the control iopq wait for it if we can
   1266 	 */
   1267 start:
   1268 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1269 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
   1270                                 return(XY_ERR_FAIL);
   1271 		goto start;
   1272 	}
   1273 
   1274 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1275 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
   1276 		iorq->mode = XY_SUB_FREE;
   1277 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
   1278 	}
   1279 
   1280 	/* init iorq/iopb */
   1281 
   1282 	xyc_rqinit(iorq, xycsc,
   1283 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
   1284 	    fullmode, block, scnt, dptr, NULL);
   1285 
   1286 	/* load IOPB from iorq */
   1287 
   1288 	xyc_rqtopb(iorq, iopb, cmd, subfn);
   1289 
   1290 	/* submit it for processing */
   1291 
   1292 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
   1293 
   1294 	return(XY_ERR_AOK);
   1295 }
   1296 
   1297 /*
   1298  * xyc_startbuf
   1299  * start a buffer for running
   1300  */
   1301 
   1302 int
   1303 xyc_startbuf(xycsc, xysc, bp)
   1304 	struct xyc_softc *xycsc;
   1305 	struct xy_softc *xysc;
   1306 	struct buf *bp;
   1307 
   1308 {
   1309 	int     partno, error;
   1310 	struct xy_iorq *iorq;
   1311 	struct xy_iopb *iopb;
   1312 	u_long  block;
   1313 
   1314 	iorq = xysc->xyrq;
   1315 	iopb = iorq->iopb;
   1316 
   1317 	/* get buf */
   1318 
   1319 	if (bp == NULL)
   1320 		panic("xyc_startbuf null buf");
   1321 
   1322 	partno = DISKPART(bp->b_dev);
   1323 #ifdef XYC_DEBUG
   1324 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
   1325 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
   1326 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
   1327 	    bp->b_bcount, bp->b_data);
   1328 #endif
   1329 
   1330 	/*
   1331 	 * load request.  we have to calculate the correct block number based
   1332 	 * on partition info.
   1333 	 *
   1334 	 * note that iorq points to the buffer as mapped into DVMA space,
   1335 	 * where as the bp->b_data points to its non-DVMA mapping.
   1336 	 */
   1337 
   1338 	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
   1339 	    xysc->sc_dk.dk_label->d_partitions[partno].p_offset);
   1340 
   1341 	error = bus_dmamap_load(xycsc->dmatag, iorq->dmamap,
   1342 			bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
   1343 	if (error != 0) {
   1344 		printf("%s: warning: cannot load DMA map\n",
   1345 			xycsc->sc_dev.dv_xname);
   1346 		return (XY_ERR_FAIL);	/* XXX: need some sort of
   1347 					 * call-back scheme here? */
   1348 	}
   1349 
   1350 	bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
   1351 			iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
   1352 				? BUS_DMASYNC_PREREAD
   1353 				: BUS_DMASYNC_PREWRITE);
   1354 
   1355 	/* init iorq and load iopb from it */
   1356 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
   1357 		   bp->b_bcount / XYFM_BPS,
   1358 		   (caddr_t)iorq->dmamap->dm_segs[0].ds_addr,
   1359 		   bp);
   1360 
   1361 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
   1362 
   1363 	/* Instrumentation. */
   1364 	disk_busy(&xysc->sc_dk);
   1365 
   1366 	return (XY_ERR_AOK);
   1367 }
   1368 
   1369 
   1370 /*
   1371  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
   1372  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
   1373  *
   1374  * note: caller frees iorq in all cases except NORM
   1375  *
   1376  * return value:
   1377  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
   1378  *   WAIT: XY_AOK (success), <error-code> (failed)
   1379  *   POLL: <same as WAIT>
   1380  *   NOQ : <same as NORM>
   1381  *
   1382  * there are three sources for i/o requests:
   1383  * [1] xystrategy: normal block I/O, using "struct buf" system.
   1384  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
   1385  * [3] open/ioctl: these are I/O requests done in the context of a process,
   1386  *                 and the process should block until they are done.
   1387  *
   1388  * software state is stored in the iorq structure.  each iorq has an
   1389  * iopb structure.  the hardware understands the iopb structure.
   1390  * every command must go through an iopb.  a 450 handles one iopb at a
   1391  * time, where as a 451 can take them in chains.  [the 450 claims it
   1392  * can handle chains, but is appears to be buggy...]   iopb are allocated
   1393  * in DVMA space at boot up time.  each disk gets one iopb, and the
   1394  * controller gets one (for POLL and WAIT commands).  what happens if
   1395  * the iopb is busy?  for i/o type [1], the buffers are queued at the
   1396  * "buff" layer and * picked up later by the interrupt routine.  for case
   1397  * [2] we can only be blocked if there is a WAIT type I/O request being
   1398  * run.   since this can only happen when we are crashing, we wait a sec
   1399  * and then steal the IOPB.  for case [3] the process can sleep
   1400  * on the iorq free list until some iopbs are avaliable.
   1401  */
   1402 
   1403 
   1404 int
   1405 xyc_submit_iorq(xycsc, iorq, type)
   1406 	struct xyc_softc *xycsc;
   1407 	struct xy_iorq *iorq;
   1408 	int     type;
   1409 
   1410 {
   1411 	struct xy_iopb *dmaiopb;
   1412 
   1413 #ifdef XYC_DEBUG
   1414 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
   1415 		xycsc->sc_dev.dv_xname, iorq, type);
   1416 #endif
   1417 
   1418 	/* first check and see if controller is busy */
   1419 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
   1420 #ifdef XYC_DEBUG
   1421 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
   1422 #endif
   1423 		if (type == XY_SUB_NOQ)
   1424 			return (XY_ERR_FAIL);	/* failed */
   1425 		switch (type) {
   1426 		case XY_SUB_NORM:
   1427 			return XY_ERR_AOK;	/* success */
   1428 		case XY_SUB_WAIT:
   1429 			while (iorq->iopb->done == 0) {
   1430 				sleep(iorq, PRIBIO);
   1431 			}
   1432 			return (iorq->errno);
   1433 		case XY_SUB_POLL:		/* steal controller */
   1434 			(void)xycsc->xyc->xyc_rsetup; /* RESET */
   1435 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
   1436 				panic("xyc_submit_iorq: stuck xyc");
   1437 			printf("%s: stole controller\n",
   1438 				xycsc->sc_dev.dv_xname);
   1439 			break;
   1440 		default:
   1441 			panic("xyc_submit_iorq adding");
   1442 		}
   1443 	}
   1444 
   1445 	dmaiopb = xyc_chain(xycsc, iorq);	 /* build chain */
   1446 	if (dmaiopb == NULL) { /* nothing doing? */
   1447 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
   1448 			return(XY_ERR_AOK);
   1449 		panic("xyc_submit_iorq: xyc_chain failed!\n");
   1450 	}
   1451 
   1452 	XYC_GO(xycsc->xyc, (u_long)dmaiopb);
   1453 
   1454 	/* command now running, wrap it up */
   1455 	switch (type) {
   1456 	case XY_SUB_NORM:
   1457 	case XY_SUB_NOQ:
   1458 		return (XY_ERR_AOK);	/* success */
   1459 	case XY_SUB_WAIT:
   1460 		while (iorq->iopb->done == 0) {
   1461 			sleep(iorq, PRIBIO);
   1462 		}
   1463 		return (iorq->errno);
   1464 	case XY_SUB_POLL:
   1465 		return (xyc_piodriver(xycsc, iorq));
   1466 	default:
   1467 		panic("xyc_submit_iorq wrap up");
   1468 	}
   1469 	panic("xyc_submit_iorq");
   1470 	return 0;	/* not reached */
   1471 }
   1472 
   1473 
   1474 /*
   1475  * xyc_chain: build a chain.  return dvma address of first element in
   1476  * the chain.   iorq != NULL: means we only want that item on the chain.
   1477  */
   1478 
   1479 struct xy_iopb *
   1480 xyc_chain(xycsc, iorq)
   1481 	struct xyc_softc *xycsc;
   1482 	struct xy_iorq *iorq;
   1483 
   1484 {
   1485 	int togo, chain, hand;
   1486 
   1487 	bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
   1488 
   1489 	/*
   1490 	 * promote control IOPB to the top
   1491 	 */
   1492 	if (iorq == NULL) {
   1493 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
   1494 		     XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
   1495 		     xycsc->iopbase[XYC_CTLIOPB].done == 0)
   1496 			iorq = &xycsc->reqs[XYC_CTLIOPB];
   1497 	}
   1498 
   1499 	/*
   1500 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
   1501 	 * we let these take priority and do them first.
   1502 	 */
   1503 	if (iorq) {
   1504 		xycsc->xy_chain[0] = iorq;
   1505 		iorq->iopb->chen = 0;
   1506 		return(iorq->dmaiopb);
   1507 	}
   1508 
   1509 	/*
   1510 	 * NORM case: do round robin and maybe chain (if allowed and possible)
   1511 	 */
   1512 	chain = 0;
   1513 	hand = xycsc->xy_hand;
   1514 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
   1515 
   1516 	for (togo = XYC_MAXIOPB; togo > 0;
   1517 	     togo--, hand = (hand + 1) % XYC_MAXIOPB) {
   1518 		struct xy_iopb *iopb, *prev_iopb, *dmaiopb;
   1519 
   1520 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
   1521 		    xycsc->iopbase[hand].done)
   1522 			continue;   /* not ready-for-i/o */
   1523 
   1524 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
   1525 		iopb = xycsc->xy_chain[chain]->iopb;
   1526 		iopb->chen = 0;
   1527 		if (chain != 0) {
   1528 			/* adding a link to a chain */
   1529 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
   1530 			prev_iopb->chen = 1;
   1531 			dmaiopb = xycsc->xy_chain[chain]->dmaiopb;
   1532 			prev_iopb->nxtiopb = ((u_long)dmaiopb) & 0xffff;
   1533 		} else {
   1534 			/* head of chain */
   1535 			iorq = xycsc->xy_chain[chain];
   1536 		}
   1537 		chain++;
   1538 
   1539 		/* quit if chaining dis-allowed */
   1540 		if (xycsc->no_ols)
   1541 			break;
   1542 	}
   1543 
   1544 	return(iorq ? iorq->dmaiopb : NULL);
   1545 }
   1546 
   1547 /*
   1548  * xyc_piodriver
   1549  *
   1550  * programmed i/o driver.   this function takes over the computer
   1551  * and drains off the polled i/o request.   it returns the status of the iorq
   1552  * the caller is interesting in.
   1553  */
   1554 int
   1555 xyc_piodriver(xycsc, iorq)
   1556 	struct xyc_softc *xycsc;
   1557 	struct xy_iorq  *iorq;
   1558 
   1559 {
   1560 	int     nreset = 0;
   1561 	int     retval = 0;
   1562 	u_long  res;
   1563 #ifdef XYC_DEBUG
   1564 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
   1565 #endif
   1566 
   1567 	while (iorq->iopb->done == 0) {
   1568 
   1569 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
   1570 
   1571 		/* we expect some progress soon */
   1572 		if (res == XY_ERR_FAIL && nreset >= 2) {
   1573 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
   1574 #ifdef XYC_DEBUG
   1575 			printf("xyc_piodriver: timeout\n");
   1576 #endif
   1577 			return (XY_ERR_FAIL);
   1578 		}
   1579 		if (res == XY_ERR_FAIL) {
   1580 			if (xyc_reset(xycsc, 0,
   1581 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
   1582 				      XY_ERR_FAIL,
   1583 				      0) == XY_ERR_FAIL)
   1584 				return (XY_ERR_FAIL);	/* flushes all but POLL
   1585 							 * requests, resets */
   1586 			continue;
   1587 		}
   1588 
   1589 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
   1590 
   1591 		if (iorq->iopb->done == 0)
   1592 			xyc_start(xycsc, iorq);
   1593 	}
   1594 
   1595 	/* get return value */
   1596 
   1597 	retval = iorq->errno;
   1598 
   1599 #ifdef XYC_DEBUG
   1600 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
   1601 	    iorq->errno, xyc_e2str(iorq->errno));
   1602 #endif
   1603 
   1604 	/* start up any bufs that have queued */
   1605 
   1606 	xyc_start(xycsc, NULL);
   1607 
   1608 	return (retval);
   1609 }
   1610 
   1611 /*
   1612  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
   1613  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
   1614  */
   1615 void
   1616 xyc_xyreset(xycsc, xysc)
   1617 	struct xyc_softc *xycsc;
   1618 	struct xy_softc *xysc;
   1619 
   1620 {
   1621 	struct xy_iopb tmpiopb;
   1622 	struct xy_iopb *iopb;
   1623 	int     del;
   1624 
   1625 	iopb = xycsc->ciopb;
   1626 
   1627 	/* Save contents */
   1628 	bcopy(iopb, &tmpiopb, sizeof(struct xy_iopb));
   1629 
   1630 	iopb->chen = iopb->done = iopb->errs = 0;
   1631 	iopb->ien = 0;
   1632 	iopb->com = XYCMD_RST;
   1633 	iopb->unit = xysc->xy_drive;
   1634 
   1635 	XYC_GO(xycsc->xyc, (u_long)xycsc->ciorq->dmaiopb);
   1636 
   1637 	del = XYC_RESETUSEC;
   1638 	while (del > 0) {
   1639 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0)
   1640 			break;
   1641 		DELAY(1);
   1642 		del--;
   1643 	}
   1644 
   1645 	if (del <= 0 || iopb->errs) {
   1646 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
   1647 		    xyc_e2str(iopb->errno));
   1648 		del = xycsc->xyc->xyc_rsetup;
   1649 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
   1650 			panic("xyc_reset");
   1651 	} else {
   1652 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
   1653 	}
   1654 
   1655 	/* Restore contents */
   1656 	bcopy(&tmpiopb, iopb, sizeof(struct xy_iopb));
   1657 }
   1658 
   1659 
   1660 /*
   1661  * xyc_reset: reset everything: requests are marked as errors except
   1662  * a polled request (which is resubmitted)
   1663  */
   1664 int
   1665 xyc_reset(xycsc, quiet, blastmode, error, xysc)
   1666 	struct xyc_softc *xycsc;
   1667 	int     quiet, error;
   1668 	struct xy_iorq *blastmode;
   1669 	struct xy_softc *xysc;
   1670 
   1671 {
   1672 	int     del = 0, lcv, retval = XY_ERR_AOK;
   1673 
   1674 	/* soft reset hardware */
   1675 
   1676 	if (!quiet)
   1677 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
   1678 	del = xycsc->xyc->xyc_rsetup;
   1679 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
   1680 	if (del == XY_ERR_FAIL) {
   1681 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
   1682 		retval = XY_ERR_FAIL;
   1683 	}
   1684 	if (xysc)
   1685 		xyc_xyreset(xycsc, xysc);
   1686 
   1687 	/* fix queues based on "blast-mode" */
   1688 
   1689 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   1690 		register struct xy_iorq *iorq = &xycsc->reqs[lcv];
   1691 
   1692 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
   1693 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
   1694 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
   1695 			/* is it active? */
   1696 			continue;
   1697 
   1698 		if (blastmode == XY_RSET_ALL ||
   1699 				blastmode != iorq) {
   1700 			/* failed */
   1701 			iorq->errno = error;
   1702 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
   1703 			switch (XY_STATE(iorq->mode)) {
   1704 			case XY_SUB_NORM:
   1705 			    iorq->buf->b_error = EIO;
   1706 			    iorq->buf->b_flags |= B_ERROR;
   1707 			    iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
   1708 
   1709 			    bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
   1710 					iorq->dmamap->dm_mapsize,
   1711 					(iorq->buf->b_flags & B_READ)
   1712 						? BUS_DMASYNC_POSTREAD
   1713 						: BUS_DMASYNC_POSTWRITE);
   1714 
   1715 			    bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
   1716 
   1717 			    BUFQ_REMOVE(&iorq->xy->xyq, iorq->buf);
   1718 			    disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
   1719 				(xycsc->reqs[lcv].buf->b_bcount -
   1720 				xycsc->reqs[lcv].buf->b_resid));
   1721 			    biodone(iorq->buf);
   1722 			    iorq->mode = XY_SUB_FREE;
   1723 			    break;
   1724 			case XY_SUB_WAIT:
   1725 			    wakeup(iorq);
   1726 			case XY_SUB_POLL:
   1727 			    iorq->mode =
   1728 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1729 			    break;
   1730 			}
   1731 
   1732 		} else {
   1733 
   1734 			/* resubmit, no need to do anything here */
   1735 		}
   1736 	}
   1737 
   1738 	/*
   1739 	 * now, if stuff is waiting, start it.
   1740 	 * since we just reset it should go
   1741 	 */
   1742 	xyc_start(xycsc, NULL);
   1743 
   1744 	return (retval);
   1745 }
   1746 
   1747 /*
   1748  * xyc_start: start waiting buffers
   1749  */
   1750 
   1751 void
   1752 xyc_start(xycsc, iorq)
   1753 	struct xyc_softc *xycsc;
   1754 	struct xy_iorq *iorq;
   1755 
   1756 {
   1757 	int lcv;
   1758 	struct xy_softc *xy;
   1759 
   1760 	if (iorq == NULL) {
   1761 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
   1762 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
   1763 			if (BUFQ_FIRST(&xy->xyq) == NULL) continue;
   1764 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
   1765 			xyc_startbuf(xycsc, xy, BUFQ_FIRST(&xy->xyq));
   1766 		}
   1767 	}
   1768 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
   1769 }
   1770 
   1771 /*
   1772  * xyc_remove_iorq: remove "done" IOPB's.
   1773  */
   1774 
   1775 int
   1776 xyc_remove_iorq(xycsc)
   1777 	struct xyc_softc *xycsc;
   1778 
   1779 {
   1780 	int     errno, rq, comm, errs;
   1781 	struct xyc *xyc = xycsc->xyc;
   1782 	u_long  addr;
   1783 	struct xy_iopb *iopb;
   1784 	struct xy_iorq *iorq;
   1785 	struct buf *bp;
   1786 
   1787 	if (xyc->xyc_csr & XYC_DERR) {
   1788 		/*
   1789 		 * DOUBLE ERROR: should never happen under normal use. This
   1790 		 * error is so bad, you can't even tell which IOPB is bad, so
   1791 		 * we dump them all.
   1792 		 */
   1793 		errno = XY_ERR_DERR;
   1794 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
   1795 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
   1796 			printf("%s: soft reset failed!\n",
   1797 				xycsc->sc_dev.dv_xname);
   1798 			panic("xyc_remove_iorq: controller DEAD");
   1799 		}
   1800 		return (XY_ERR_AOK);
   1801 	}
   1802 
   1803 	/*
   1804 	 * get iopb that is done, loop down the chain
   1805 	 */
   1806 
   1807 	if (xyc->xyc_csr & XYC_ERR) {
   1808 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
   1809 	}
   1810 	if (xyc->xyc_csr & XYC_IPND) {
   1811 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
   1812 	}
   1813 
   1814 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
   1815 		iorq = xycsc->xy_chain[rq];
   1816 		if (iorq == NULL) break; /* done ! */
   1817 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
   1818 			continue;	/* free, or done */
   1819 		iopb = iorq->iopb;
   1820 		if (iopb->done == 0)
   1821 			continue;	/* not done yet */
   1822 
   1823 		comm = iopb->com;
   1824 		errs = iopb->errs;
   1825 
   1826 		if (errs)
   1827 			iorq->errno = iopb->errno;
   1828 		else
   1829 			iorq->errno = 0;
   1830 
   1831 		/* handle non-fatal errors */
   1832 
   1833 		if (errs &&
   1834 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
   1835 			continue;	/* AOK: we resubmitted it */
   1836 
   1837 
   1838 		/* this iorq is now done (hasn't been restarted or anything) */
   1839 
   1840 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   1841 			xyc_perror(iorq, iopb, 0);
   1842 
   1843 		/* now, if read/write check to make sure we got all the data
   1844 		 * we needed. (this may not be the case if we got an error in
   1845 		 * the middle of a multisector request).   */
   1846 
   1847 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
   1848 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
   1849 			/* we just successfully processed a bad144 sector
   1850 			 * note: if we are in bad 144 mode, the pointers have
   1851 			 * been advanced already (see above) and are pointing
   1852 			 * at the bad144 sector.   to exit bad144 mode, we
   1853 			 * must advance the pointers 1 sector and issue a new
   1854 			 * request if there are still sectors left to process
   1855 			 *
   1856 			 */
   1857 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
   1858 
   1859 			/* exit b144 mode */
   1860 			iorq->mode = iorq->mode & (~XY_MODE_B144);
   1861 
   1862 			if (iorq->sectcnt) {	/* more to go! */
   1863 				iorq->lasterror = iorq->errno = iopb->errno = 0;
   1864 				iopb->errs = iopb->done = 0;
   1865 				iorq->tries = 0;
   1866 				iopb->scnt = iorq->sectcnt;
   1867 				iopb->cyl = iorq->blockno /
   1868 						iorq->xy->sectpercyl;
   1869 				iopb->head =
   1870 					(iorq->blockno / iorq->xy->nhead) %
   1871 						iorq->xy->nhead;
   1872 				iopb->sect = iorq->blockno % XYFM_BPS;
   1873 				addr = (u_long) iorq->dbuf;
   1874 				iopb->dataa = (addr & 0xffff);
   1875 				iopb->datar = ((addr & 0xff0000) >> 16);
   1876 				/* will resubit at end */
   1877 				continue;
   1878 			}
   1879 		}
   1880 		/* final cleanup, totally done with this request */
   1881 
   1882 		switch (XY_STATE(iorq->mode)) {
   1883 		case XY_SUB_NORM:
   1884 			bp = iorq->buf;
   1885 			if (errs) {
   1886 				bp->b_error = EIO;
   1887 				bp->b_flags |= B_ERROR;
   1888 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
   1889 			} else {
   1890 				bp->b_resid = 0;	/* done */
   1891 			}
   1892 			bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
   1893 					iorq->dmamap->dm_mapsize,
   1894 					(iorq->buf->b_flags & B_READ)
   1895 						? BUS_DMASYNC_POSTREAD
   1896 						: BUS_DMASYNC_POSTWRITE);
   1897 
   1898 			bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
   1899 
   1900 			BUFQ_REMOVE(&iorq->xy->xyq, bp);
   1901 			disk_unbusy(&iorq->xy->sc_dk,
   1902 			    (bp->b_bcount - bp->b_resid));
   1903 			iorq->mode = XY_SUB_FREE;
   1904 			biodone(bp);
   1905 			break;
   1906 		case XY_SUB_WAIT:
   1907 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1908 			wakeup(iorq);
   1909 			break;
   1910 		case XY_SUB_POLL:
   1911 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1912 			break;
   1913 		}
   1914 	}
   1915 
   1916 	return (XY_ERR_AOK);
   1917 }
   1918 
   1919 /*
   1920  * xyc_perror: print error.
   1921  * - if still_trying is true: we got an error, retried and got a
   1922  *   different error.  in that case lasterror is the old error,
   1923  *   and errno is the new one.
   1924  * - if still_trying is not true, then if we ever had an error it
   1925  *   is in lasterror. also, if iorq->errno == 0, then we recovered
   1926  *   from that error (otherwise iorq->errno == iorq->lasterror).
   1927  */
   1928 void
   1929 xyc_perror(iorq, iopb, still_trying)
   1930 	struct xy_iorq *iorq;
   1931 	struct xy_iopb *iopb;
   1932 	int     still_trying;
   1933 
   1934 {
   1935 
   1936 	int     error = iorq->lasterror;
   1937 
   1938 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
   1939 	    : iorq->xyc->sc_dev.dv_xname);
   1940 	if (iorq->buf)
   1941 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
   1942 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
   1943 		printf("%s %d/%d/%d: ",
   1944 			(iopb->com == XYCMD_RD) ? "read" : "write",
   1945 			iopb->cyl, iopb->head, iopb->sect);
   1946 	printf("%s", xyc_e2str(error));
   1947 
   1948 	if (still_trying)
   1949 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
   1950 	else
   1951 		if (iorq->errno == 0)
   1952 			printf(" [recovered in %d tries]", iorq->tries);
   1953 
   1954 	printf("\n");
   1955 }
   1956 
   1957 /*
   1958  * xyc_error: non-fatal error encountered... recover.
   1959  * return AOK if resubmitted, return FAIL if this iopb is done
   1960  */
   1961 int
   1962 xyc_error(xycsc, iorq, iopb, comm)
   1963 	struct xyc_softc *xycsc;
   1964 	struct xy_iorq *iorq;
   1965 	struct xy_iopb *iopb;
   1966 	int     comm;
   1967 
   1968 {
   1969 	int     errno = iorq->errno;
   1970 	int     erract = xyc_entoact(errno);
   1971 	int     oldmode, advance;
   1972 #ifdef sparc
   1973 	int i;
   1974 #endif
   1975 
   1976 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
   1977 		oldmode = iorq->mode;
   1978 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
   1979 		/* make xyc_start ignore us */
   1980 		xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
   1981 		iorq->mode = oldmode;
   1982 	}
   1983 	/* check for read/write to a sector in bad144 table if bad: redirect
   1984 	 * request to bad144 area */
   1985 
   1986 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
   1987 	    (iorq->mode & XY_MODE_B144) == 0) {
   1988 		advance = iorq->sectcnt - iopb->scnt;
   1989 		XYC_ADVANCE(iorq, advance);
   1990 #ifdef sparc
   1991 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
   1992 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
   1993 			    iorq->blockno % iorq->xy->nsect)) != -1) {
   1994 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
   1995 							 * redirect */
   1996 			iopb->errno = iopb->done = iopb->errs = 0;
   1997 			iopb->scnt = 1;
   1998 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
   1999 			/* second to last acyl */
   2000 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
   2001 								 * standard */
   2002 			iopb->head = i / iorq->xy->nhead;
   2003 			iopb->sect = i % iorq->xy->nhead;
   2004 			/* will resubmit when we come out of remove_iorq */
   2005 			return (XY_ERR_AOK);	/* recovered! */
   2006 		}
   2007 #endif
   2008 	}
   2009 
   2010 	/*
   2011 	 * it isn't a bad144 sector, must be real error! see if we can retry
   2012 	 * it?
   2013 	 */
   2014 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   2015 		xyc_perror(iorq, iopb, 1);	/* inform of error state
   2016 						 * change */
   2017 	iorq->lasterror = errno;
   2018 
   2019 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
   2020 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
   2021 		iorq->tries++;
   2022 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
   2023 		/* will resubmit at end of remove_iorq */
   2024 		return (XY_ERR_AOK);	/* recovered! */
   2025 	}
   2026 
   2027 	/* failed to recover from this error */
   2028 	return (XY_ERR_FAIL);
   2029 }
   2030 
   2031 /*
   2032  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
   2033  */
   2034 void
   2035 xyc_tick(arg)
   2036 	void   *arg;
   2037 
   2038 {
   2039 	struct xyc_softc *xycsc = arg;
   2040 	int     lcv, s, reset = 0;
   2041 
   2042 	/* reduce ttl for each request if one goes to zero, reset xyc */
   2043 	s = splbio();
   2044 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   2045 		if (xycsc->reqs[lcv].mode == 0 ||
   2046 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
   2047 			continue;
   2048 		xycsc->reqs[lcv].ttl--;
   2049 		if (xycsc->reqs[lcv].ttl == 0)
   2050 			reset = 1;
   2051 	}
   2052 	if (reset) {
   2053 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
   2054 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
   2055 	}
   2056 	splx(s);
   2057 
   2058 	/* until next time */
   2059 
   2060 	timeout(xyc_tick, xycsc, XYC_TICKCNT);
   2061 }
   2062 
   2063 /*
   2064  * xyc_ioctlcmd: this function provides a user level interface to the
   2065  * controller via ioctl.   this allows "format" programs to be written
   2066  * in user code, and is also useful for some debugging.   we return
   2067  * an error code.   called at user priority.
   2068  *
   2069  * XXX missing a few commands (see the 7053 driver for ideas)
   2070  */
   2071 int
   2072 xyc_ioctlcmd(xy, dev, xio)
   2073 	struct xy_softc *xy;
   2074 	dev_t   dev;
   2075 	struct xd_iocmd *xio;
   2076 
   2077 {
   2078 	int     s, rqno, dummy = 0;
   2079 	caddr_t dvmabuf = NULL, buf = NULL;
   2080 	struct xyc_softc *xycsc;
   2081 	int			rseg, error;
   2082 	bus_dma_segment_t	seg;
   2083 
   2084 	/* check sanity of requested command */
   2085 
   2086 	switch (xio->cmd) {
   2087 
   2088 	case XYCMD_NOP:	/* no op: everything should be zero */
   2089 		if (xio->subfn || xio->dptr || xio->dlen ||
   2090 		    xio->block || xio->sectcnt)
   2091 			return (EINVAL);
   2092 		break;
   2093 
   2094 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
   2095 	case XYCMD_WR:
   2096 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
   2097 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
   2098 			return (EINVAL);
   2099 		break;
   2100 
   2101 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
   2102 		return (EINVAL);
   2103 
   2104 		break;
   2105 
   2106 	default:
   2107 		return (EINVAL);/* ??? */
   2108 	}
   2109 
   2110 	xycsc = xy->parent;
   2111 
   2112 	/* create DVMA buffer for request if needed */
   2113 	if (xio->dlen) {
   2114 		error = bus_dmamem_alloc(xycsc->dmatag, xio->dlen, NBPG, 0,
   2115 					 &seg, 1, &rseg, BUS_DMA_WAITOK);
   2116 		if (error) {
   2117 			return (error);
   2118 		}
   2119 		dvmabuf = (caddr_t)seg.ds_addr;
   2120 
   2121 		error = bus_dmamem_map(xycsc->dmatag, &seg, rseg, xio->dlen,
   2122 					&buf,
   2123 					BUS_DMA_WAITOK|BUS_DMA_COHERENT);
   2124 		if (error) {
   2125 			bus_dmamem_free(xycsc->dmatag, &seg, rseg);
   2126 			return (error);
   2127 		}
   2128 		if (xio->cmd == XYCMD_WR) {
   2129 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
   2130 				bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
   2131 				bus_dmamem_free(xycsc->dmatag, &seg, rseg);
   2132 				return (error);
   2133 			}
   2134 		}
   2135 	}
   2136 	/* do it! */
   2137 
   2138 	error = 0;
   2139 	s = splbio();
   2140 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
   2141 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
   2142 	if (rqno == XY_ERR_FAIL) {
   2143 		error = EIO;
   2144 		goto done;
   2145 	}
   2146 	xio->errno = xycsc->ciorq->errno;
   2147 	xio->tries = xycsc->ciorq->tries;
   2148 	XYC_DONE(xycsc, dummy);
   2149 
   2150 	if (xio->cmd == XYCMD_RD)
   2151 		error = copyout(buf, xio->dptr, xio->dlen);
   2152 
   2153 done:
   2154 	splx(s);
   2155 	if (dvmabuf) {
   2156 		bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
   2157 		bus_dmamem_free(xycsc->dmatag, &seg, rseg);
   2158 	}
   2159 	return (error);
   2160 }
   2161 
   2162 /*
   2163  * xyc_e2str: convert error code number into an error string
   2164  */
   2165 char *
   2166 xyc_e2str(no)
   2167 	int     no;
   2168 {
   2169 	switch (no) {
   2170 	case XY_ERR_FAIL:
   2171 		return ("Software fatal error");
   2172 	case XY_ERR_DERR:
   2173 		return ("DOUBLE ERROR");
   2174 	case XY_ERR_AOK:
   2175 		return ("Successful completion");
   2176 	case XY_ERR_IPEN:
   2177 		return("Interrupt pending");
   2178 	case XY_ERR_BCFL:
   2179 		return("Busy conflict");
   2180 	case XY_ERR_TIMO:
   2181 		return("Operation timeout");
   2182 	case XY_ERR_NHDR:
   2183 		return("Header not found");
   2184 	case XY_ERR_HARD:
   2185 		return("Hard ECC error");
   2186 	case XY_ERR_ICYL:
   2187 		return("Illegal cylinder address");
   2188 	case XY_ERR_ISEC:
   2189 		return("Illegal sector address");
   2190 	case XY_ERR_SMAL:
   2191 		return("Last sector too small");
   2192 	case XY_ERR_SACK:
   2193 		return("Slave ACK error (non-existent memory)");
   2194 	case XY_ERR_CHER:
   2195 		return("Cylinder and head/header error");
   2196 	case XY_ERR_SRTR:
   2197 		return("Auto-seek retry successful");
   2198 	case XY_ERR_WPRO:
   2199 		return("Write-protect error");
   2200 	case XY_ERR_UIMP:
   2201 		return("Unimplemented command");
   2202 	case XY_ERR_DNRY:
   2203 		return("Drive not ready");
   2204 	case XY_ERR_SZER:
   2205 		return("Sector count zero");
   2206 	case XY_ERR_DFLT:
   2207 		return("Drive faulted");
   2208 	case XY_ERR_ISSZ:
   2209 		return("Illegal sector size");
   2210 	case XY_ERR_SLTA:
   2211 		return("Self test A");
   2212 	case XY_ERR_SLTB:
   2213 		return("Self test B");
   2214 	case XY_ERR_SLTC:
   2215 		return("Self test C");
   2216 	case XY_ERR_SOFT:
   2217 		return("Soft ECC error");
   2218 	case XY_ERR_SFOK:
   2219 		return("Soft ECC error recovered");
   2220 	case XY_ERR_IHED:
   2221 		return("Illegal head");
   2222 	case XY_ERR_DSEQ:
   2223 		return("Disk sequencer error");
   2224 	case XY_ERR_SEEK:
   2225 		return("Seek error");
   2226 	default:
   2227 		return ("Unknown error");
   2228 	}
   2229 }
   2230 
   2231 int
   2232 xyc_entoact(errno)
   2233 
   2234 int errno;
   2235 
   2236 {
   2237   switch (errno) {
   2238     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
   2239     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
   2240     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
   2241     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
   2242     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
   2243 
   2244 	return(XY_ERA_PROG); /* program error ! */
   2245 
   2246     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
   2247     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
   2248     case XY_ERR_SOFT:
   2249 
   2250 	return(XY_ERA_HARD); /* hard error, retry */
   2251 
   2252     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
   2253 
   2254 	return(XY_ERA_RSET); /* hard error reset */
   2255 
   2256     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
   2257 
   2258 	return(XY_ERA_SOFT); /* an FYI error */
   2259 
   2260     case XY_ERR_WPRO:
   2261 
   2262 	return(XY_ERA_WPRO); /* write protect */
   2263   }
   2264 
   2265   return(XY_ERA_PROG); /* ??? */
   2266 }
   2267