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