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