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