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