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