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