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