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