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