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