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