Home | History | Annotate | Line # | Download | only in dev
xy.c revision 1.60
      1 /*	$NetBSD: xy.c,v 1.60 2007/07/09 20:52:34 ad 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.60 2007/07/09 20:52:34 ad 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_flags |= B_DONE;
    251 	bp->b_flags &= ~B_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 	xy->sc_dk.dk_driver = &xydkdriver;
    501 	xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
    502 
    503 	xy->state = XY_DRIVE_UNKNOWN;	/* to start */
    504 	xy->flags = 0;
    505 	xy->parent = xyc;
    506 
    507 	/* init queue of waiting bufs */
    508 	bufq_alloc(&xy->xyq, "disksort", BUFQ_SORT_RAWBLOCK);
    509 	xy->xyrq = &xyc->reqs[xa->driveno];
    510 
    511 	xy->xy_drive = xa->driveno;
    512 	xyc->sc_drives[xa->driveno] = xy;
    513 
    514 	/* Do init work common to attach and open. */
    515 	xy_init(xy);
    516 }
    517 
    518 /*
    519  * end of autoconfig functions
    520  */
    521 
    522 /*
    523  * Initialize a disk.  This can be called from both autoconf and
    524  * also from xyopen/xystrategy.
    525  */
    526 static void
    527 xy_init(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 		memcpy(&xy->dkb, dvmabuf, 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_t dev, int flag, int fmt, struct lwp *l)
    697 {
    698 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
    699 	int     part = DISKPART(dev);
    700 
    701 	/* clear mask bits */
    702 
    703 	switch (fmt) {
    704 	case S_IFCHR:
    705 		xy->sc_dk.dk_copenmask &= ~(1 << part);
    706 		break;
    707 	case S_IFBLK:
    708 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
    709 		break;
    710 	}
    711 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    712 
    713 	return 0;
    714 }
    715 
    716 /*
    717  * xydump: crash dump system
    718  */
    719 int
    720 xydump(dev_t dev, daddr_t blkno, void *va, size_t sz)
    721 {
    722 	int     unit, part;
    723 	struct xy_softc *xy;
    724 
    725 	unit = DISKUNIT(dev);
    726 	if (unit >= xy_cd.cd_ndevs)
    727 		return ENXIO;
    728 	part = DISKPART(dev);
    729 
    730 	xy = xy_cd.cd_devs[unit];
    731 
    732 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
    733 	    'a' + part);
    734 
    735 	return ENXIO;
    736 
    737 	/* outline: globals: "dumplo" == sector number of partition to start
    738 	 * dump at (convert to physical sector with partition table)
    739 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
    740 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
    741 	 * physmem)
    742 	 *
    743 	 * dump a copy of physical memory to the dump device starting at sector
    744 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
    745 	 * we go.   use polled I/O.
    746 	 *
    747 	 * XXX how to handle NON_CONTIG?
    748 	 */
    749 }
    750 
    751 /*
    752  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
    753  */
    754 int
    755 xyioctl(dev_t dev, u_long command, void *addr, int flag, struct lwp *l)
    756 {
    757 	struct xy_softc *xy;
    758 	struct xd_iocmd *xio;
    759 	int     error, s, unit;
    760 
    761 	unit = DISKUNIT(dev);
    762 
    763 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
    764 		return (ENXIO);
    765 
    766 	/* switch on ioctl type */
    767 
    768 	switch (command) {
    769 	case DIOCSBAD:		/* set bad144 info */
    770 		if ((flag & FWRITE) == 0)
    771 			return EBADF;
    772 		s = splbio();
    773 		memcpy(&xy->dkb, addr, sizeof(xy->dkb));
    774 		splx(s);
    775 		return 0;
    776 
    777 	case DIOCGDINFO:	/* get disk label */
    778 		memcpy(addr, xy->sc_dk.dk_label, sizeof(struct disklabel));
    779 		return 0;
    780 
    781 	case DIOCGPART:	/* get partition info */
    782 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
    783 		((struct partinfo *) addr)->part =
    784 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    785 		return 0;
    786 
    787 	case DIOCSDINFO:	/* set disk label */
    788 		if ((flag & FWRITE) == 0)
    789 			return EBADF;
    790 		error = setdisklabel(xy->sc_dk.dk_label,
    791 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
    792 		    xy->sc_dk.dk_cpulabel);
    793 		if (error == 0) {
    794 			if (xy->state == XY_DRIVE_NOLABEL)
    795 				xy->state = XY_DRIVE_ONLINE;
    796 		}
    797 		return error;
    798 
    799 	case DIOCWLABEL:	/* change write status of disk label */
    800 		if ((flag & FWRITE) == 0)
    801 			return EBADF;
    802 		if (*(int *) addr)
    803 			xy->flags |= XY_WLABEL;
    804 		else
    805 			xy->flags &= ~XY_WLABEL;
    806 		return 0;
    807 
    808 	case DIOCWDINFO:	/* write disk label */
    809 		if ((flag & FWRITE) == 0)
    810 			return EBADF;
    811 		error = setdisklabel(xy->sc_dk.dk_label,
    812 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
    813 		    xy->sc_dk.dk_cpulabel);
    814 		if (error == 0) {
    815 			if (xy->state == XY_DRIVE_NOLABEL)
    816 				xy->state = XY_DRIVE_ONLINE;
    817 
    818 			/* Simulate opening partition 0 so write succeeds. */
    819 			xy->sc_dk.dk_openmask |= (1 << 0);
    820 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
    821 			    xystrategy, xy->sc_dk.dk_label,
    822 			    xy->sc_dk.dk_cpulabel);
    823 			xy->sc_dk.dk_openmask =
    824 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    825 		}
    826 		return error;
    827 
    828 	case DIOSXDCMD:
    829 		xio = (struct xd_iocmd *) addr;
    830 		if ((error = kauth_authorize_generic(l->l_cred,
    831 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
    832 			return (error);
    833 		return (xyc_ioctlcmd(xy, dev, xio));
    834 
    835 	default:
    836 		return ENOTTY;
    837 	}
    838 }
    839 
    840 /*
    841  * xyopen: open drive
    842  */
    843 int
    844 xyopen(dev_t dev, int flag, int fmt, struct lwp *l)
    845 {
    846 	int err, unit, part, s;
    847 	struct xy_softc *xy;
    848 
    849 	/* first, could it be a valid target? */
    850 	unit = DISKUNIT(dev);
    851 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
    852 		return (ENXIO);
    853 	part = DISKPART(dev);
    854 	err = 0;
    855 
    856 	/*
    857 	 * If some other processing is doing init, sleep.
    858 	 */
    859 	s = splbio();
    860 	while (xy->state == XY_DRIVE_ATTACHING) {
    861 		if (tsleep(&xy->state, PRIBIO, "xyopen", 0)) {
    862 			err = EINTR;
    863 			goto done;
    864 		}
    865 	}
    866 	/* Do we need to init the drive? */
    867 	if (xy->state == XY_DRIVE_UNKNOWN) {
    868 		xy_init(xy);
    869 		wakeup(&xy->state);
    870 	}
    871 	/* Was the init successful? */
    872 	if (xy->state == XY_DRIVE_UNKNOWN) {
    873 		err = EIO;
    874 		goto done;
    875 	}
    876 
    877 	/* check for partition */
    878 	if (part != RAW_PART &&
    879 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
    880 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    881 		err = ENXIO;
    882 		goto done;
    883 	}
    884 
    885 	/* set open masks */
    886 	switch (fmt) {
    887 	case S_IFCHR:
    888 		xy->sc_dk.dk_copenmask |= (1 << part);
    889 		break;
    890 	case S_IFBLK:
    891 		xy->sc_dk.dk_bopenmask |= (1 << part);
    892 		break;
    893 	}
    894 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
    895 
    896 done:
    897 	splx(s);
    898 	return (err);
    899 }
    900 
    901 int
    902 xyread(dev_t dev, struct uio *uio, int flags)
    903 {
    904 
    905 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
    906 }
    907 
    908 int
    909 xywrite(dev_t dev, struct uio *uio, int flags)
    910 {
    911 
    912 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
    913 }
    914 
    915 
    916 /*
    917  * xysize: return size of a partition for a dump
    918  */
    919 
    920 int
    921 xysize(dev_t dev)
    922 {
    923 	struct xy_softc *xysc;
    924 	int     unit, part, size, omask;
    925 
    926 	/* valid unit? */
    927 	unit = DISKUNIT(dev);
    928 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
    929 		return (-1);
    930 
    931 	part = DISKPART(dev);
    932 	omask = xysc->sc_dk.dk_openmask & (1 << part);
    933 
    934 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
    935 		return (-1);
    936 
    937 	/* do it */
    938 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    939 		size = -1;	/* only give valid size for swap partitions */
    940 	else
    941 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
    942 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    943 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
    944 		return (-1);
    945 	return (size);
    946 }
    947 
    948 /*
    949  * xystrategy: buffering system interface to xy.
    950  */
    951 void
    952 xystrategy(struct buf *bp)
    953 {
    954 	struct xy_softc *xy;
    955 	int     s, unit;
    956 	struct disklabel *lp;
    957 	daddr_t blkno;
    958 
    959 	unit = DISKUNIT(bp->b_dev);
    960 
    961 	/* check for live device */
    962 
    963 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
    964 	    bp->b_blkno < 0 ||
    965 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
    966 		bp->b_error = EINVAL;
    967 		goto bad;
    968 	}
    969 
    970 	/* There should always be an open first. */
    971 	if (xy->state == XY_DRIVE_UNKNOWN) {
    972 		bp->b_error = EIO;
    973 		goto bad;
    974 	}
    975 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
    976 		/* no I/O to unlabeled disks, unless raw partition */
    977 		bp->b_error = EIO;
    978 		goto bad;
    979 	}
    980 	/* short circuit zero length request */
    981 
    982 	if (bp->b_bcount == 0)
    983 		goto done;
    984 
    985 	/* check bounds with label (disksubr.c).  Determine the size of the
    986 	 * transfer, and make sure it is within the boundaries of the
    987 	 * partition. Adjust transfer if needed, and signal errors or early
    988 	 * completion. */
    989 
    990 	lp = xy->sc_dk.dk_label;
    991 
    992 	if (bounds_check_with_label(&xy->sc_dk, bp,
    993 		(xy->flags & XY_WLABEL) != 0) <= 0)
    994 		goto done;
    995 
    996 	/*
    997 	 * Now convert the block number to absolute and put it in
    998 	 * terms of the device's logical block size.
    999 	 */
   1000 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
   1001 	if (DISKPART(bp->b_dev) != RAW_PART)
   1002 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
   1003 
   1004 	bp->b_rawblkno = blkno;
   1005 
   1006 	/*
   1007 	 * now we know we have a valid buf structure that we need to do I/O
   1008 	 * on.
   1009 	 */
   1010 
   1011 	s = splbio();		/* protect the queues */
   1012 
   1013 	BUFQ_PUT(xy->xyq, bp);	 /* XXX disksort_cylinder */
   1014 
   1015 	/* start 'em up */
   1016 
   1017 	xyc_start(xy->parent, NULL);
   1018 
   1019 	/* done! */
   1020 
   1021 	splx(s);
   1022 	return;
   1023 
   1024 bad:				/* tells upper layers we have an error */
   1025 	bp->b_flags |= B_ERROR;
   1026 done:				/* tells upper layers we are done with this
   1027 				 * buf */
   1028 	bp->b_resid = bp->b_bcount;
   1029 	biodone(bp);
   1030 }
   1031 /*
   1032  * end of {b,c}devsw functions
   1033  */
   1034 
   1035 /*
   1036  * i n t e r r u p t   f u n c t i o n
   1037  *
   1038  * xycintr: hardware interrupt.
   1039  */
   1040 int
   1041 xycintr(void *v)
   1042 {
   1043 	struct xyc_softc *xycsc = v;
   1044 
   1045 	/* kick the event counter */
   1046 	xycsc->sc_intrcnt.ev_count++;
   1047 
   1048 	/* remove as many done IOPBs as possible */
   1049 	xyc_remove_iorq(xycsc);
   1050 
   1051 	/* start any iorq's already waiting */
   1052 	xyc_start(xycsc, NULL);
   1053 
   1054 	return (1);
   1055 }
   1056 /*
   1057  * end of interrupt function
   1058  */
   1059 
   1060 /*
   1061  * i n t e r n a l   f u n c t i o n s
   1062  */
   1063 
   1064 /*
   1065  * xyc_rqinit: fill out the fields of an I/O request
   1066  */
   1067 
   1068 inline void
   1069 xyc_rqinit(struct xy_iorq *rq, struct xyc_softc *xyc, struct xy_softc *xy,
   1070     int md, u_long blk, int cnt, void *db, struct buf *bp)
   1071 {
   1072 	rq->xyc = xyc;
   1073 	rq->xy = xy;
   1074 	rq->ttl = XYC_MAXTTL + 10;
   1075 	rq->mode = md;
   1076 	rq->tries = rq->errno = rq->lasterror = 0;
   1077 	rq->blockno = blk;
   1078 	rq->sectcnt = cnt;
   1079 	rq->dbuf = rq->dbufbase = db;
   1080 	rq->buf = bp;
   1081 }
   1082 
   1083 /*
   1084  * xyc_rqtopb: load up an IOPB based on an iorq
   1085  */
   1086 
   1087 void
   1088 xyc_rqtopb(struct xy_iorq *iorq, struct xy_iopb *iopb, int cmd, int subfun)
   1089 {
   1090 	u_long  block, dp;
   1091 
   1092 	/* normal IOPB case, standard stuff */
   1093 
   1094 	/* chain bit handled later */
   1095 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
   1096 	iopb->com = cmd;
   1097 	iopb->errno = 0;
   1098 	iopb->errs = 0;
   1099 	iopb->done = 0;
   1100 	if (iorq->xy) {
   1101 		iopb->unit = iorq->xy->xy_drive;
   1102 		iopb->dt = iorq->xy->drive_type;
   1103 	} else {
   1104 		iopb->unit = 0;
   1105 		iopb->dt = 0;
   1106 	}
   1107 	block = iorq->blockno;
   1108 	if (iorq->xy == NULL || block == 0) {
   1109 		iopb->sect = iopb->head = iopb->cyl = 0;
   1110 	} else {
   1111 		iopb->sect = block % iorq->xy->nsect;
   1112 		block = block / iorq->xy->nsect;
   1113 		iopb->head = block % iorq->xy->nhead;
   1114 		block = block / iorq->xy->nhead;
   1115 		iopb->cyl = block;
   1116 	}
   1117 	iopb->scnt = iorq->sectcnt;
   1118 	if (iorq->dbuf == NULL) {
   1119 		iopb->dataa = 0;
   1120 		iopb->datar = 0;
   1121 	} else {
   1122 		dp = dvma_kvtopa(iorq->dbuf, iorq->xyc->bustype);
   1123 		iopb->dataa = (dp & 0xffff);
   1124 		iopb->datar = ((dp & 0xff0000) >> 16);
   1125 	}
   1126 	iopb->subfn = subfun;
   1127 }
   1128 
   1129 
   1130 /*
   1131  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
   1132  */
   1133 
   1134 int
   1135 xyc_unbusy(struct xyc *xyc, int del)
   1136 {
   1137 	while (del-- > 0) {
   1138 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
   1139 			break;
   1140 		DELAY(1);
   1141 	}
   1142 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
   1143 }
   1144 
   1145 /*
   1146  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
   1147  * note that NORM requests are handled separately.
   1148  */
   1149 int
   1150 xyc_cmd(struct xyc_softc *xycsc, int cmd, int subfn, int unit, int block,
   1151     int scnt, char *dptr, int fullmode)
   1152 {
   1153 	struct xy_iorq *iorq = xycsc->ciorq;
   1154 	struct xy_iopb *iopb = xycsc->ciopb;
   1155 	int submode = XY_STATE(fullmode);
   1156 
   1157 	/*
   1158 	 * is someone else using the control iopq wait for it if we can
   1159 	 */
   1160 start:
   1161 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1162 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
   1163                                 return(XY_ERR_FAIL);
   1164 		goto start;
   1165 	}
   1166 
   1167 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
   1168 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
   1169 		iorq->mode = XY_SUB_FREE;
   1170 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
   1171 	}
   1172 
   1173 	/* init iorq/iopb */
   1174 
   1175 	xyc_rqinit(iorq, xycsc,
   1176 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
   1177 	    fullmode, block, scnt, dptr, NULL);
   1178 
   1179 	/* load IOPB from iorq */
   1180 
   1181 	xyc_rqtopb(iorq, iopb, cmd, subfn);
   1182 
   1183 	/* submit it for processing */
   1184 
   1185 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
   1186 
   1187 	return(XY_ERR_AOK);
   1188 }
   1189 
   1190 /*
   1191  * xyc_startbuf
   1192  * start a buffer for running
   1193  */
   1194 
   1195 int
   1196 xyc_startbuf(struct xyc_softc *xycsc, struct xy_softc *xysc, struct buf *bp)
   1197 {
   1198 	int     partno;
   1199 	struct xy_iorq *iorq;
   1200 	struct xy_iopb *iopb;
   1201 	u_long  block;
   1202 	void *dbuf;
   1203 
   1204 	iorq = xysc->xyrq;
   1205 	iopb = iorq->iopb;
   1206 
   1207 	/* get buf */
   1208 
   1209 	if (bp == NULL)
   1210 		panic("xyc_startbuf null buf");
   1211 
   1212 	partno = DISKPART(bp->b_dev);
   1213 #ifdef XYC_DEBUG
   1214 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
   1215 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
   1216 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
   1217 	    bp->b_bcount, bp->b_data);
   1218 #endif
   1219 
   1220 	/*
   1221 	 * load request.
   1222 	 *
   1223 	 * also, note that there are two kinds of buf structures, those with
   1224 	 * B_PHYS set and those without B_PHYS.   if B_PHYS is set, then it is
   1225 	 * a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
   1226 	 * buffer which has already been mapped into DVMA space. (Not on sun3)
   1227 	 * However, if B_PHYS is not set, then the buffer is a normal system
   1228 	 * buffer which does *not* live in DVMA space.  In that case we call
   1229 	 * dvma_mapin to map it into DVMA space so we can do the DMA to it.
   1230 	 *
   1231 	 * in cases where we do a dvma_mapin, note that iorq points to the buffer
   1232 	 * as mapped into DVMA space, where as the bp->b_data points to its
   1233 	 * non-DVMA mapping.
   1234 	 *
   1235 	 * XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
   1236 	 * into dvma space, only that it was remapped into the kernel.
   1237 	 * We ALWAYS have to remap the kernel buf into DVMA space.
   1238 	 * (It is done inexpensively, using whole segments!)
   1239 	 */
   1240 
   1241 	block = bp->b_rawblkno;
   1242 
   1243 	dbuf = dvma_mapin(bp->b_data, bp->b_bcount, 0);
   1244 	if (dbuf == NULL) {	/* out of DVMA space */
   1245 		printf("%s: warning: out of DVMA space\n",
   1246 			   xycsc->sc_dev.dv_xname);
   1247 		return (XY_ERR_FAIL);	/* XXX: need some sort of
   1248 		                         * call-back scheme here? */
   1249 	}
   1250 
   1251 	/* init iorq and load iopb from it */
   1252 
   1253 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
   1254 	    bp->b_bcount / XYFM_BPS, dbuf, bp);
   1255 
   1256 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
   1257 
   1258 	/* Instrumentation. */
   1259 	disk_busy(&xysc->sc_dk);
   1260 
   1261 	return (XY_ERR_AOK);
   1262 }
   1263 
   1264 
   1265 /*
   1266  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
   1267  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
   1268  *
   1269  * note: caller frees iorq in all cases except NORM
   1270  *
   1271  * return value:
   1272  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
   1273  *   WAIT: XY_AOK (success), <error-code> (failed)
   1274  *   POLL: <same as WAIT>
   1275  *   NOQ : <same as NORM>
   1276  *
   1277  * there are three sources for i/o requests:
   1278  * [1] xystrategy: normal block I/O, using "struct buf" system.
   1279  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
   1280  * [3] open/ioctl: these are I/O requests done in the context of a process,
   1281  *                 and the process should block until they are done.
   1282  *
   1283  * software state is stored in the iorq structure.  each iorq has an
   1284  * iopb structure.  the hardware understands the iopb structure.
   1285  * every command must go through an iopb.  a 450 handles one iopb at a
   1286  * time, where as a 451 can take them in chains.  [the 450 claims it
   1287  * can handle chains, but is appears to be buggy...]   iopb are allocated
   1288  * in DVMA space at boot up time.  each disk gets one iopb, and the
   1289  * controller gets one (for POLL and WAIT commands).  what happens if
   1290  * the iopb is busy?  for i/o type [1], the buffers are queued at the
   1291  * "buff" layer and * picked up later by the interrupt routine.  for case
   1292  * [2] we can only be blocked if there is a WAIT type I/O request being
   1293  * run.   since this can only happen when we are crashing, we wait a sec
   1294  * and then steal the IOPB.  for case [3] the process can sleep
   1295  * on the iorq free list until some iopbs are available.
   1296  */
   1297 
   1298 int
   1299 xyc_submit_iorq(struct xyc_softc *xycsc, struct xy_iorq *iorq, int type)
   1300 {
   1301 	struct xy_iopb *iopb;
   1302 	u_long  iopbaddr;
   1303 
   1304 #ifdef XYC_DEBUG
   1305 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
   1306 		xycsc->sc_dev.dv_xname, iorq, type);
   1307 #endif
   1308 
   1309 	/* first check and see if controller is busy */
   1310 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
   1311 #ifdef XYC_DEBUG
   1312 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
   1313 #endif
   1314 		if (type == XY_SUB_NOQ)
   1315 			return (XY_ERR_FAIL);	/* failed */
   1316 		switch (type) {
   1317 		case XY_SUB_NORM:
   1318 			return XY_ERR_AOK;	/* success */
   1319 		case XY_SUB_WAIT:
   1320 			while (iorq->iopb->done == 0) {
   1321 				(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
   1322 			}
   1323 			return (iorq->errno);
   1324 		case XY_SUB_POLL:		/* steal controller */
   1325 			iopbaddr = xycsc->xyc->xyc_rsetup; /* RESET */
   1326 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
   1327 				panic("xyc_submit_iorq: stuck xyc");
   1328 			printf("%s: stole controller\n",
   1329 				xycsc->sc_dev.dv_xname);
   1330 			break;
   1331 		default:
   1332 			panic("xyc_submit_iorq adding");
   1333 		}
   1334 	}
   1335 
   1336 	iopb = xyc_chain(xycsc, iorq);	 /* build chain */
   1337 	if (iopb == NULL) { /* nothing doing? */
   1338 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
   1339 			return(XY_ERR_AOK);
   1340 		panic("xyc_submit_iorq: xyc_chain failed!");
   1341 	}
   1342 	iopbaddr = dvma_kvtopa(iopb, xycsc->bustype);
   1343 
   1344 	XYC_GO(xycsc->xyc, iopbaddr);
   1345 
   1346 	/* command now running, wrap it up */
   1347 	switch (type) {
   1348 	case XY_SUB_NORM:
   1349 	case XY_SUB_NOQ:
   1350 		return (XY_ERR_AOK);	/* success */
   1351 	case XY_SUB_WAIT:
   1352 		while (iorq->iopb->done == 0) {
   1353 			(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
   1354 		}
   1355 		return (iorq->errno);
   1356 	case XY_SUB_POLL:
   1357 		return (xyc_piodriver(xycsc, iorq));
   1358 	default:
   1359 		panic("xyc_submit_iorq wrap up");
   1360 	}
   1361 	panic("xyc_submit_iorq");
   1362 	return 0;	/* not reached */
   1363 }
   1364 
   1365 
   1366 /*
   1367  * xyc_chain: build a chain.  return dvma address of first element in
   1368  * the chain.   iorq != NULL: means we only want that item on the chain.
   1369  */
   1370 
   1371 struct xy_iopb *
   1372 xyc_chain(struct xyc_softc *xycsc, struct xy_iorq *iorq)
   1373 {
   1374 	int togo, chain, hand;
   1375 	struct xy_iopb *iopb, *prev_iopb;
   1376 
   1377 	memset(xycsc->xy_chain, 0, sizeof(xycsc->xy_chain));
   1378 
   1379 	/*
   1380 	 * promote control IOPB to the top
   1381 	 */
   1382 	if (iorq == NULL) {
   1383 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
   1384 			 XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
   1385 			xycsc->iopbase[XYC_CTLIOPB].done == 0)
   1386 		  iorq = &xycsc->reqs[XYC_CTLIOPB];
   1387 	}
   1388 
   1389 	/*
   1390 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
   1391 	 * we let these take priority and do them first.
   1392 	 */
   1393 	if (iorq) {
   1394 		xycsc->xy_chain[0] = iorq;
   1395 		iorq->iopb->chen = 0;
   1396 		return(iorq->iopb);
   1397 	}
   1398 
   1399 	/*
   1400 	 * NORM case: do round robin and maybe chain (if allowed and possible)
   1401 	 */
   1402 
   1403 	chain = 0;
   1404 	hand = xycsc->xy_hand;
   1405 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
   1406 
   1407 	for (togo = XYC_MAXIOPB ;
   1408 		 togo > 0 ;
   1409 		 togo--, hand = (hand + 1) % XYC_MAXIOPB)
   1410 	{
   1411 
   1412 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
   1413 			xycsc->iopbase[hand].done)
   1414 			continue;   /* not ready-for-i/o */
   1415 
   1416 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
   1417 		iopb = xycsc->xy_chain[chain]->iopb;
   1418 		iopb->chen = 0;
   1419 		if (chain != 0) {   /* adding a link to a chain? */
   1420 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
   1421 			prev_iopb->chen = 1;
   1422 			prev_iopb->nxtiopb = 0xffff &
   1423 			  dvma_kvtopa(iopb, xycsc->bustype);
   1424 		} else {            /* head of chain */
   1425 			iorq = xycsc->xy_chain[chain];
   1426 		}
   1427 		chain++;
   1428 		if (xycsc->no_ols) break;   /* quit if chaining dis-allowed */
   1429 	}
   1430 	return(iorq ? iorq->iopb : NULL);
   1431 }
   1432 
   1433 /*
   1434  * xyc_piodriver
   1435  *
   1436  * programmed i/o driver.   this function takes over the computer
   1437  * and drains off the polled i/o request.   it returns the status of the iorq
   1438  * the caller is interesting in.
   1439  */
   1440 int
   1441 xyc_piodriver(struct xyc_softc *xycsc, struct xy_iorq *iorq)
   1442 {
   1443 	int     nreset = 0;
   1444 	int     retval = 0;
   1445 	u_long  res;
   1446 
   1447 #ifdef XYC_DEBUG
   1448 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
   1449 #endif
   1450 
   1451 	while (iorq->iopb->done == 0) {
   1452 
   1453 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
   1454 
   1455 		/* we expect some progress soon */
   1456 		if (res == XY_ERR_FAIL && nreset >= 2) {
   1457 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
   1458 #ifdef XYC_DEBUG
   1459 			printf("xyc_piodriver: timeout\n");
   1460 #endif
   1461 			return (XY_ERR_FAIL);
   1462 		}
   1463 		if (res == XY_ERR_FAIL) {
   1464 			if (xyc_reset(xycsc, 0,
   1465 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
   1466 				      XY_ERR_FAIL,
   1467 				      0) == XY_ERR_FAIL)
   1468 				return (XY_ERR_FAIL);	/* flushes all but POLL
   1469 							 * requests, resets */
   1470 			continue;
   1471 		}
   1472 
   1473 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
   1474 
   1475 		if (iorq->iopb->done == 0)
   1476 			xyc_start(xycsc, iorq);
   1477 	}
   1478 
   1479 	/* get return value */
   1480 
   1481 	retval = iorq->errno;
   1482 
   1483 #ifdef XYC_DEBUG
   1484 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
   1485 	    iorq->errno, xyc_e2str(iorq->errno));
   1486 #endif
   1487 
   1488 	/* start up any bufs that have queued */
   1489 
   1490 	xyc_start(xycsc, NULL);
   1491 
   1492 	return (retval);
   1493 }
   1494 
   1495 /*
   1496  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
   1497  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
   1498  */
   1499 void
   1500 xyc_xyreset(struct xyc_softc *xycsc, struct xy_softc *xysc)
   1501 {
   1502 	struct xy_iopb tmpiopb;
   1503 	u_long  addr;
   1504 	int     del;
   1505 	memcpy(&tmpiopb, xycsc->ciopb, sizeof(tmpiopb));
   1506 	xycsc->ciopb->chen = xycsc->ciopb->done = xycsc->ciopb->errs = 0;
   1507 	xycsc->ciopb->ien = 0;
   1508 	xycsc->ciopb->com = XYCMD_RST;
   1509 	xycsc->ciopb->unit = xysc->xy_drive;
   1510 	addr = dvma_kvtopa(xycsc->ciopb, xycsc->bustype);
   1511 
   1512 	XYC_GO(xycsc->xyc, addr);
   1513 
   1514 	del = XYC_RESETUSEC;
   1515 	while (del > 0) {
   1516 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0) break;
   1517 		DELAY(1);
   1518 		del--;
   1519 	}
   1520 
   1521 	if (del <= 0 || xycsc->ciopb->errs) {
   1522 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
   1523 		    xyc_e2str(xycsc->ciopb->errno));
   1524 		del = xycsc->xyc->xyc_rsetup;
   1525 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
   1526 			panic("xyc_reset");
   1527 	} else {
   1528 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
   1529 	}
   1530 	memcpy(xycsc->ciopb, &tmpiopb, sizeof(tmpiopb));
   1531 }
   1532 
   1533 
   1534 /*
   1535  * xyc_reset: reset everything: requests are marked as errors except
   1536  * a polled request (which is resubmitted)
   1537  */
   1538 int
   1539 xyc_reset(struct xyc_softc *xycsc, int quiet, struct xy_iorq *blastmode,
   1540     int error, struct xy_softc *xysc)
   1541 {
   1542 	int     del = 0, lcv, retval = XY_ERR_AOK;
   1543 	struct xy_iorq *iorq;
   1544 
   1545 	/* soft reset hardware */
   1546 
   1547 	if (!quiet)
   1548 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
   1549 	del = xycsc->xyc->xyc_rsetup;
   1550 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
   1551 	if (del == XY_ERR_FAIL) {
   1552 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
   1553 		retval = XY_ERR_FAIL;
   1554 	}
   1555 	if (xysc)
   1556 		xyc_xyreset(xycsc, xysc);
   1557 
   1558 	/* fix queues based on "blast-mode" */
   1559 
   1560 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   1561 		iorq = &xycsc->reqs[lcv];
   1562 
   1563 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
   1564 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
   1565 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
   1566 			/* is it active? */
   1567 			continue;
   1568 
   1569 		if (blastmode == XY_RSET_ALL ||
   1570 				blastmode != iorq) {
   1571 			/* failed */
   1572 			iorq->errno = error;
   1573 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
   1574 			switch (XY_STATE(iorq->mode)) {
   1575 			case XY_SUB_NORM:
   1576 			    iorq->buf->b_error = EIO;
   1577 			    iorq->buf->b_flags |= B_ERROR;
   1578 			    iorq->buf->b_resid =
   1579 			       iorq->sectcnt * XYFM_BPS;
   1580 				/* Sun3: map/unmap regardless of B_PHYS */
   1581 				dvma_mapout(iorq->dbufbase,
   1582 				            iorq->buf->b_bcount);
   1583 			    (void)BUFQ_GET(iorq->xy->xyq);
   1584 			    disk_unbusy(&iorq->xy->sc_dk,
   1585 				(iorq->buf->b_bcount - iorq->buf->b_resid),
   1586 				(iorq->buf->b_flags & B_READ));
   1587 			    biodone(iorq->buf);
   1588 			    iorq->mode = XY_SUB_FREE;
   1589 			    break;
   1590 			case XY_SUB_WAIT:
   1591 			    wakeup(iorq);
   1592 			case XY_SUB_POLL:
   1593 			    iorq->mode =
   1594 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1595 			    break;
   1596 			}
   1597 
   1598 		} else {
   1599 
   1600 			/* resubmit, no need to do anything here */
   1601 		}
   1602 	}
   1603 
   1604 	/*
   1605 	 * now, if stuff is waiting, start it.
   1606 	 * since we just reset it should go
   1607 	 */
   1608 	xyc_start(xycsc, NULL);
   1609 
   1610 	return (retval);
   1611 }
   1612 
   1613 /*
   1614  * xyc_start: start waiting buffers
   1615  */
   1616 
   1617 void
   1618 xyc_start(struct xyc_softc *xycsc, struct xy_iorq *iorq)
   1619 {
   1620 	int lcv;
   1621 	struct xy_softc *xy;
   1622 
   1623 	if (iorq == NULL) {
   1624 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
   1625 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
   1626 			if (BUFQ_PEEK(xy->xyq) == NULL) continue;
   1627 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
   1628 			xyc_startbuf(xycsc, xy, BUFQ_PEEK(xy->xyq));
   1629 		}
   1630 	}
   1631 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
   1632 }
   1633 
   1634 /*
   1635  * xyc_remove_iorq: remove "done" IOPB's.
   1636  */
   1637 
   1638 int
   1639 xyc_remove_iorq(struct xyc_softc *xycsc)
   1640 {
   1641 	int     errno, rq, comm, errs;
   1642 	struct xyc *xyc = xycsc->xyc;
   1643 	u_long  addr;
   1644 	struct xy_iopb *iopb;
   1645 	struct xy_iorq *iorq;
   1646 	struct buf *bp;
   1647 
   1648 	if (xyc->xyc_csr & XYC_DERR) {
   1649 		/*
   1650 		 * DOUBLE ERROR: should never happen under normal use. This
   1651 		 * error is so bad, you can't even tell which IOPB is bad, so
   1652 		 * we dump them all.
   1653 		 */
   1654 		errno = XY_ERR_DERR;
   1655 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
   1656 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
   1657 			printf("%s: soft reset failed!\n",
   1658 				xycsc->sc_dev.dv_xname);
   1659 			panic("xyc_remove_iorq: controller DEAD");
   1660 		}
   1661 		return (XY_ERR_AOK);
   1662 	}
   1663 
   1664 	/*
   1665 	 * get iopb that is done, loop down the chain
   1666 	 */
   1667 
   1668 	if (xyc->xyc_csr & XYC_ERR) {
   1669 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
   1670 	}
   1671 	if (xyc->xyc_csr & XYC_IPND) {
   1672 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
   1673 	}
   1674 
   1675 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
   1676 		iorq = xycsc->xy_chain[rq];
   1677 		if (iorq == NULL) break; /* done ! */
   1678 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
   1679 			continue;	/* free, or done */
   1680 		iopb = iorq->iopb;
   1681 		if (iopb->done == 0)
   1682 			continue;	/* not done yet */
   1683 
   1684 		comm = iopb->com;
   1685 		errs = iopb->errs;
   1686 
   1687 		if (errs)
   1688 			iorq->errno = iopb->errno;
   1689 		else
   1690 			iorq->errno = 0;
   1691 
   1692 		/* handle non-fatal errors */
   1693 
   1694 		if (errs &&
   1695 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
   1696 			continue;	/* AOK: we resubmitted it */
   1697 
   1698 
   1699 		/* this iorq is now done (hasn't been restarted or anything) */
   1700 
   1701 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   1702 			xyc_perror(iorq, iopb, 0);
   1703 
   1704 		/* now, if read/write check to make sure we got all the data
   1705 		 * we needed. (this may not be the case if we got an error in
   1706 		 * the middle of a multisector request).   */
   1707 
   1708 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
   1709 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
   1710 			/* we just successfully processed a bad144 sector
   1711 			 * note: if we are in bad 144 mode, the pointers have
   1712 			 * been advanced already (see above) and are pointing
   1713 			 * at the bad144 sector.   to exit bad144 mode, we
   1714 			 * must advance the pointers 1 sector and issue a new
   1715 			 * request if there are still sectors left to process
   1716 			 *
   1717 			 */
   1718 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
   1719 
   1720 			/* exit b144 mode */
   1721 			iorq->mode = iorq->mode & (~XY_MODE_B144);
   1722 
   1723 			if (iorq->sectcnt) {	/* more to go! */
   1724 				iorq->lasterror = iorq->errno = iopb->errno = 0;
   1725 				iopb->errs = iopb->done = 0;
   1726 				iorq->tries = 0;
   1727 				iopb->scnt = iorq->sectcnt;
   1728 				iopb->cyl = iorq->blockno /
   1729 						iorq->xy->sectpercyl;
   1730 				iopb->head =
   1731 					(iorq->blockno / iorq->xy->nhead) %
   1732 						iorq->xy->nhead;
   1733 				iopb->sect = iorq->blockno % XYFM_BPS;
   1734 				addr = dvma_kvtopa(iorq->dbuf, xycsc->bustype);
   1735 				iopb->dataa = (addr & 0xffff);
   1736 				iopb->datar = ((addr & 0xff0000) >> 16);
   1737 				/* will resubit at end */
   1738 				continue;
   1739 			}
   1740 		}
   1741 		/* final cleanup, totally done with this request */
   1742 
   1743 		switch (XY_STATE(iorq->mode)) {
   1744 		case XY_SUB_NORM:
   1745 			bp = iorq->buf;
   1746 			if (errs) {
   1747 				bp->b_error = EIO;
   1748 				bp->b_flags |= B_ERROR;
   1749 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
   1750 			} else {
   1751 				bp->b_resid = 0;	/* done */
   1752 			}
   1753 			/* Sun3: map/unmap regardless of B_PHYS */
   1754 			dvma_mapout(iorq->dbufbase,
   1755 					    iorq->buf->b_bcount);
   1756 			(void)BUFQ_GET(iorq->xy->xyq);
   1757 			disk_unbusy(&iorq->xy->sc_dk,
   1758 			    (bp->b_bcount - bp->b_resid),
   1759 			    (bp->b_flags & B_READ));
   1760 			iorq->mode = XY_SUB_FREE;
   1761 			biodone(bp);
   1762 			break;
   1763 		case XY_SUB_WAIT:
   1764 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1765 			wakeup(iorq);
   1766 			break;
   1767 		case XY_SUB_POLL:
   1768 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
   1769 			break;
   1770 		}
   1771 	}
   1772 
   1773 	return (XY_ERR_AOK);
   1774 }
   1775 
   1776 /*
   1777  * xyc_perror: print error.
   1778  * - if still_trying is true: we got an error, retried and got a
   1779  *   different error.  in that case lasterror is the old error,
   1780  *   and errno is the new one.
   1781  * - if still_trying is not true, then if we ever had an error it
   1782  *   is in lasterror. also, if iorq->errno == 0, then we recovered
   1783  *   from that error (otherwise iorq->errno == iorq->lasterror).
   1784  */
   1785 void
   1786 xyc_perror(struct xy_iorq *iorq, struct xy_iopb *iopb, int still_trying)
   1787 {
   1788 
   1789 	int     error = iorq->lasterror;
   1790 
   1791 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
   1792 	    : iorq->xyc->sc_dev.dv_xname);
   1793 	if (iorq->buf)
   1794 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
   1795 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
   1796 		printf("%s %d/%d/%d: ",
   1797 			(iopb->com == XYCMD_RD) ? "read" : "write",
   1798 			iopb->cyl, iopb->head, iopb->sect);
   1799 	printf("%s", xyc_e2str(error));
   1800 
   1801 	if (still_trying)
   1802 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
   1803 	else
   1804 		if (iorq->errno == 0)
   1805 			printf(" [recovered in %d tries]", iorq->tries);
   1806 
   1807 	printf("\n");
   1808 }
   1809 
   1810 /*
   1811  * xyc_error: non-fatal error encountered... recover.
   1812  * return AOK if resubmitted, return FAIL if this iopb is done
   1813  */
   1814 int
   1815 xyc_error(struct xyc_softc *xycsc, struct xy_iorq *iorq, struct xy_iopb *iopb,
   1816     int comm)
   1817 {
   1818 	int     errno = iorq->errno;
   1819 	int     erract = xyc_entoact(errno);
   1820 	int     oldmode, advance, i;
   1821 
   1822 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
   1823 		oldmode = iorq->mode;
   1824 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
   1825 		/* make xyc_start ignore us */
   1826 		xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
   1827 		iorq->mode = oldmode;
   1828 	}
   1829 	/* check for read/write to a sector in bad144 table if bad: redirect
   1830 	 * request to bad144 area */
   1831 
   1832 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
   1833 	    (iorq->mode & XY_MODE_B144) == 0) {
   1834 		advance = iorq->sectcnt - iopb->scnt;
   1835 		XYC_ADVANCE(iorq, advance);
   1836 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
   1837 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
   1838 			    iorq->blockno % iorq->xy->nsect)) != -1) {
   1839 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
   1840 							 * redirect */
   1841 			iopb->errno = iopb->done = iopb->errs = 0;
   1842 			iopb->scnt = 1;
   1843 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
   1844 			/* second to last acyl */
   1845 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
   1846 								 * standard */
   1847 			iopb->head = i / iorq->xy->nhead;
   1848 			iopb->sect = i % iorq->xy->nhead;
   1849 			/* will resubmit when we come out of remove_iorq */
   1850 			return (XY_ERR_AOK);	/* recovered! */
   1851 		}
   1852 	}
   1853 
   1854 	/*
   1855 	 * it isn't a bad144 sector, must be real error! see if we can retry
   1856 	 * it?
   1857 	 */
   1858 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
   1859 		xyc_perror(iorq, iopb, 1);	/* inform of error state
   1860 						 * change */
   1861 	iorq->lasterror = errno;
   1862 
   1863 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
   1864 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
   1865 		iorq->tries++;
   1866 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
   1867 		/* will resubmit at end of remove_iorq */
   1868 		return (XY_ERR_AOK);	/* recovered! */
   1869 	}
   1870 
   1871 	/* failed to recover from this error */
   1872 	return (XY_ERR_FAIL);
   1873 }
   1874 
   1875 /*
   1876  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
   1877  */
   1878 void
   1879 xyc_tick(void *arg)
   1880 {
   1881 	struct xyc_softc *xycsc = arg;
   1882 	int     lcv, s, reset = 0;
   1883 
   1884 	/* reduce ttl for each request if one goes to zero, reset xyc */
   1885 	s = splbio();
   1886 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
   1887 		if (xycsc->reqs[lcv].mode == 0 ||
   1888 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
   1889 			continue;
   1890 		xycsc->reqs[lcv].ttl--;
   1891 		if (xycsc->reqs[lcv].ttl == 0)
   1892 			reset = 1;
   1893 	}
   1894 	if (reset) {
   1895 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
   1896 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
   1897 	}
   1898 	splx(s);
   1899 
   1900 	/* until next time */
   1901 
   1902 	callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
   1903 }
   1904 
   1905 /*
   1906  * xyc_ioctlcmd: this function provides a user level interface to the
   1907  * controller via ioctl.   this allows "format" programs to be written
   1908  * in user code, and is also useful for some debugging.   we return
   1909  * an error code.   called at user priority.
   1910  *
   1911  * XXX missing a few commands (see the 7053 driver for ideas)
   1912  */
   1913 int
   1914 xyc_ioctlcmd(struct xy_softc *xy, dev_t dev, struct xd_iocmd *xio)
   1915 {
   1916 	int     s, err, rqno;
   1917 	void *dvmabuf = NULL;
   1918 	struct xyc_softc *xycsc;
   1919 
   1920 	/* check sanity of requested command */
   1921 
   1922 	switch (xio->cmd) {
   1923 
   1924 	case XYCMD_NOP:	/* no op: everything should be zero */
   1925 		if (xio->subfn || xio->dptr || xio->dlen ||
   1926 		    xio->block || xio->sectcnt)
   1927 			return (EINVAL);
   1928 		break;
   1929 
   1930 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
   1931 	case XYCMD_WR:
   1932 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
   1933 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
   1934 			return (EINVAL);
   1935 		break;
   1936 
   1937 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
   1938 		return (EINVAL);
   1939 
   1940 		break;
   1941 
   1942 	default:
   1943 		return (EINVAL);/* ??? */
   1944 	}
   1945 
   1946 	/* create DVMA buffer for request if needed */
   1947 
   1948 	if (xio->dlen) {
   1949 		dvmabuf = dvma_malloc(xio->dlen);
   1950 		if (xio->cmd == XYCMD_WR) {
   1951 			err = copyin(xio->dptr, dvmabuf, xio->dlen);
   1952 			if (err) {
   1953 				dvma_free(dvmabuf, xio->dlen);
   1954 				return (err);
   1955 			}
   1956 		}
   1957 	}
   1958 	/* do it! */
   1959 
   1960 	err = 0;
   1961 	xycsc = xy->parent;
   1962 	s = splbio();
   1963 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
   1964 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
   1965 	if (rqno == XY_ERR_FAIL) {
   1966 		err = EIO;
   1967 		goto done;
   1968 	}
   1969 	xio->errno = xycsc->ciorq->errno;
   1970 	xio->tries = xycsc->ciorq->tries;
   1971 	XYC_DONE(xycsc, err);
   1972 
   1973 	if (xio->cmd == XYCMD_RD)
   1974 		err = copyout(dvmabuf, xio->dptr, xio->dlen);
   1975 
   1976 done:
   1977 	splx(s);
   1978 	if (dvmabuf)
   1979 		dvma_free(dvmabuf, xio->dlen);
   1980 	return (err);
   1981 }
   1982 
   1983 /*
   1984  * xyc_e2str: convert error code number into an error string
   1985  */
   1986 const char *
   1987 xyc_e2str(int no)
   1988 {
   1989 	switch (no) {
   1990 	case XY_ERR_FAIL:
   1991 		return ("Software fatal error");
   1992 	case XY_ERR_DERR:
   1993 		return ("DOUBLE ERROR");
   1994 	case XY_ERR_AOK:
   1995 		return ("Successful completion");
   1996 	case XY_ERR_IPEN:
   1997 		return("Interrupt pending");
   1998 	case XY_ERR_BCFL:
   1999 		return("Busy conflict");
   2000 	case XY_ERR_TIMO:
   2001 		return("Operation timeout");
   2002 	case XY_ERR_NHDR:
   2003 		return("Header not found");
   2004 	case XY_ERR_HARD:
   2005 		return("Hard ECC error");
   2006 	case XY_ERR_ICYL:
   2007 		return("Illegal cylinder address");
   2008 	case XY_ERR_ISEC:
   2009 		return("Illegal sector address");
   2010 	case XY_ERR_SMAL:
   2011 		return("Last sector too small");
   2012 	case XY_ERR_SACK:
   2013 		return("Slave ACK error (non-existent memory)");
   2014 	case XY_ERR_CHER:
   2015 		return("Cylinder and head/header error");
   2016 	case XY_ERR_SRTR:
   2017 		return("Auto-seek retry successful");
   2018 	case XY_ERR_WPRO:
   2019 		return("Write-protect error");
   2020 	case XY_ERR_UIMP:
   2021 		return("Unimplemented command");
   2022 	case XY_ERR_DNRY:
   2023 		return("Drive not ready");
   2024 	case XY_ERR_SZER:
   2025 		return("Sector count zero");
   2026 	case XY_ERR_DFLT:
   2027 		return("Drive faulted");
   2028 	case XY_ERR_ISSZ:
   2029 		return("Illegal sector size");
   2030 	case XY_ERR_SLTA:
   2031 		return("Self test A");
   2032 	case XY_ERR_SLTB:
   2033 		return("Self test B");
   2034 	case XY_ERR_SLTC:
   2035 		return("Self test C");
   2036 	case XY_ERR_SOFT:
   2037 		return("Soft ECC error");
   2038 	case XY_ERR_SFOK:
   2039 		return("Soft ECC error recovered");
   2040 	case XY_ERR_IHED:
   2041 		return("Illegal head");
   2042 	case XY_ERR_DSEQ:
   2043 		return("Disk sequencer error");
   2044 	case XY_ERR_SEEK:
   2045 		return("Seek error");
   2046 	default:
   2047 		return ("Unknown error");
   2048 	}
   2049 }
   2050 
   2051 int
   2052 xyc_entoact(int errno)
   2053 {
   2054 	switch (errno) {
   2055 	case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
   2056 	case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
   2057 	case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
   2058 	case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
   2059 	case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
   2060 
   2061 		return(XY_ERA_PROG); /* program error ! */
   2062 
   2063 	case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
   2064 	case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
   2065 	case XY_ERR_SOFT:
   2066 
   2067 		return(XY_ERA_HARD); /* hard error, retry */
   2068 
   2069 	case XY_ERR_DFLT:	case XY_ERR_DSEQ:
   2070 
   2071 		return(XY_ERA_RSET); /* hard error reset */
   2072 
   2073 	case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
   2074 
   2075 		return(XY_ERA_SOFT); /* an FYI error */
   2076 
   2077 	case XY_ERR_WPRO:
   2078 
   2079 		return(XY_ERA_WPRO); /* write protect */
   2080 	}
   2081 
   2082 	return(XY_ERA_PROG); /* ??? */
   2083 }
   2084