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