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