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