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