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