xref: /netbsd-src/sys/arch/sun3/dev/xy.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: xy.c,v 1.15 1997/06/24 00:58:16 thorpej 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.15 1997/06/24 00:58:16 thorpej 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, omask;
942 
943 	/* valid unit? */
944 	unit = DISKUNIT(dev);
945 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
946 		return (-1);
947 
948 	part = DISKPART(dev);
949 	omask = xysc->sc_dk.dk_openmask & (1 << part);
950 
951 	if (omask == 0 && xyopen(dev, 0, S_IFBLK) != 0)
952 		return (-1);
953 
954 	/* do it */
955 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
956 		size = -1;	/* only give valid size for swap partitions */
957 	else
958 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
959 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
960 	if (omask == 0 && xyclose(dev, 0, S_IFBLK) != 0)
961 		return (-1);
962 	return (size);
963 }
964 
965 /*
966  * xystrategy: buffering system interface to xy.
967  */
968 
969 void
970 xystrategy(bp)
971 	struct buf *bp;
972 
973 {
974 	struct xy_softc *xy;
975 	struct xyc_softc *parent;
976 	struct buf *wq;
977 	int     s, unit;
978 	struct xyc_attach_args xa;
979 
980 	unit = DISKUNIT(bp->b_dev);
981 
982 	/* check for live device */
983 
984 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
985 	    bp->b_blkno < 0 ||
986 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
987 		bp->b_error = EINVAL;
988 		goto bad;
989 	}
990 	/* do we need to attach the drive? */
991 
992 	if (xy->state == XY_DRIVE_UNKNOWN) {
993 		xa.driveno = xy->xy_drive;
994 		xa.dvmabuf = (char *) dvma_malloc(XYFM_BPS);
995 		xa.fullmode = XY_SUB_WAIT;
996 		xa.booting = 0;
997 		xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
998 		dvma_free(xa.dvmabuf, XYFM_BPS);
999 		if (xy->state == XY_DRIVE_UNKNOWN) {
1000 			bp->b_error = EIO;
1001 			goto bad;
1002 		}
1003 	}
1004 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1005 		/* no I/O to unlabeled disks, unless raw partition */
1006 		bp->b_error = EIO;
1007 		goto bad;
1008 	}
1009 	/* short circuit zero length request */
1010 
1011 	if (bp->b_bcount == 0)
1012 		goto done;
1013 
1014 	/* check bounds with label (disksubr.c).  Determine the size of the
1015 	 * transfer, and make sure it is within the boundaries of the
1016 	 * partition. Adjust transfer if needed, and signal errors or early
1017 	 * completion. */
1018 
1019 	if (bounds_check_with_label(bp, xy->sc_dk.dk_label,
1020 		(xy->flags & XY_WLABEL) != 0) <= 0)
1021 		goto done;
1022 
1023 	/*
1024 	 * now we know we have a valid buf structure that we need to do I/O
1025 	 * on.
1026 	 */
1027 
1028 	s = splbio();		/* protect the queues */
1029 
1030 	disksort(&xy->xyq, bp);
1031 
1032 	/* start 'em up */
1033 
1034 	xyc_start(xy->parent, NULL);
1035 
1036 	/* done! */
1037 
1038 	splx(s);
1039 	return;
1040 
1041 bad:				/* tells upper layers we have an error */
1042 	bp->b_flags |= B_ERROR;
1043 done:				/* tells upper layers we are done with this
1044 				 * buf */
1045 	bp->b_resid = bp->b_bcount;
1046 	biodone(bp);
1047 }
1048 /*
1049  * end of {b,c}devsw functions
1050  */
1051 
1052 /*
1053  * i n t e r r u p t   f u n c t i o n
1054  *
1055  * xycintr: hardware interrupt.
1056  */
1057 int
1058 xycintr(v)
1059 	void   *v;
1060 
1061 {
1062 	struct xyc_softc *xycsc = v;
1063 	struct xy_softc *xy;
1064 	struct buf *bp;
1065 
1066 	/* kick the event counter */
1067 
1068 	xycsc->sc_intrcnt.ev_count++;
1069 
1070 	/* remove as many done IOPBs as possible */
1071 
1072 	xyc_remove_iorq(xycsc);
1073 
1074 	/* start any iorq's already waiting */
1075 
1076 	xyc_start(xycsc, NULL);
1077 
1078 	return (1);
1079 }
1080 /*
1081  * end of interrupt function
1082  */
1083 
1084 /*
1085  * i n t e r n a l   f u n c t i o n s
1086  */
1087 
1088 /*
1089  * xyc_rqinit: fill out the fields of an I/O request
1090  */
1091 
1092 inline void
1093 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
1094 	struct xy_iorq *rq;
1095 	struct xyc_softc *xyc;
1096 	struct xy_softc *xy;
1097 	int     md;
1098 	u_long  blk;
1099 	int     cnt;
1100 	caddr_t db;
1101 	struct buf *bp;
1102 {
1103 	rq->xyc = xyc;
1104 	rq->xy = xy;
1105 	rq->ttl = XYC_MAXTTL + 10;
1106 	rq->mode = md;
1107 	rq->tries = rq->errno = rq->lasterror = 0;
1108 	rq->blockno = blk;
1109 	rq->sectcnt = cnt;
1110 	rq->dbuf = rq->dbufbase = db;
1111 	rq->buf = bp;
1112 }
1113 
1114 /*
1115  * xyc_rqtopb: load up an IOPB based on an iorq
1116  */
1117 
1118 void
1119 xyc_rqtopb(iorq, iopb, cmd, subfun)
1120 	struct xy_iorq *iorq;
1121 	struct xy_iopb *iopb;
1122 	int     cmd, subfun;
1123 
1124 {
1125 	u_long  block, dp;
1126 
1127 	/* normal IOPB case, standard stuff */
1128 
1129 	/* chain bit handled later */
1130 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
1131 	iopb->com = cmd;
1132 	iopb->errno = 0;
1133 	iopb->errs = 0;
1134 	iopb->done = 0;
1135 	if (iorq->xy) {
1136 		iopb->unit = iorq->xy->xy_drive;
1137 		iopb->dt = iorq->xy->drive_type;
1138 	} else {
1139 		iopb->unit = 0;
1140 		iopb->dt = 0;
1141 	}
1142 	block = iorq->blockno;
1143 	if (iorq->xy == NULL || block == 0) {
1144 		iopb->sect = iopb->head = iopb->cyl = 0;
1145 	} else {
1146 		iopb->sect = block % iorq->xy->nsect;
1147 		block = block / iorq->xy->nsect;
1148 		iopb->head = block % iorq->xy->nhead;
1149 		block = block / iorq->xy->nhead;
1150 		iopb->cyl = block;
1151 	}
1152 	iopb->scnt = iorq->sectcnt;
1153 	if (iorq->dbuf == NULL) {
1154 		iopb->dataa = 0;
1155 		iopb->datar = 0;
1156 	} else {
1157 		dp = dvma_kvtopa((long)iorq->dbuf, BUS_VME16);
1158 		iopb->dataa = (dp & 0xffff);
1159 		iopb->datar = ((dp & 0xff0000) >> 16);
1160 	}
1161 	iopb->subfn = subfun;
1162 }
1163 
1164 
1165 /*
1166  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
1167  */
1168 
1169 int
1170 xyc_unbusy(xyc, del)
1171 
1172 struct xyc *xyc;
1173 int del;
1174 
1175 {
1176 	while (del-- > 0) {
1177 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
1178 			break;
1179 		DELAY(1);
1180 	}
1181 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
1182 }
1183 
1184 /*
1185  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
1186  * note that NORM requests are handled seperately.
1187  */
1188 int
1189 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1190 	struct xyc_softc *xycsc;
1191 	int     cmd, subfn, unit, block, scnt;
1192 	char   *dptr;
1193 	int     fullmode;
1194 
1195 {
1196 	int     submode = XY_STATE(fullmode), retry;
1197 	u_long  dp;
1198 	struct xy_iorq *iorq = xycsc->ciorq;
1199 	struct xy_iopb *iopb = xycsc->ciopb;
1200 
1201 	/*
1202 	 * is someone else using the control iopq wait for it if we can
1203 	 */
1204 start:
1205 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
1206 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
1207                                 return(XY_ERR_FAIL);
1208 		goto start;
1209 	}
1210 
1211 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
1212 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
1213 		iorq->mode = XY_SUB_FREE;
1214 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
1215 	}
1216 
1217 	/* init iorq/iopb */
1218 
1219 	xyc_rqinit(iorq, xycsc,
1220 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
1221 	    fullmode, block, scnt, dptr, NULL);
1222 
1223 	/* load IOPB from iorq */
1224 
1225 	xyc_rqtopb(iorq, iopb, cmd, subfn);
1226 
1227 	/* submit it for processing */
1228 
1229 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
1230 
1231 	return(XY_ERR_AOK);
1232 }
1233 
1234 /*
1235  * xyc_startbuf
1236  * start a buffer for running
1237  */
1238 
1239 int
1240 xyc_startbuf(xycsc, xysc, bp)
1241 	struct xyc_softc *xycsc;
1242 	struct xy_softc *xysc;
1243 	struct buf *bp;
1244 
1245 {
1246 	int     partno;
1247 	struct xy_iorq *iorq;
1248 	struct xy_iopb *iopb;
1249 	u_long  block, dp;
1250 	caddr_t dbuf;
1251 
1252 	iorq = xysc->xyrq;
1253 	iopb = iorq->iopb;
1254 
1255 	/* get buf */
1256 
1257 	if (bp == NULL)
1258 		panic("xyc_startbuf null buf");
1259 
1260 	partno = DISKPART(bp->b_dev);
1261 #ifdef XYC_DEBUG
1262 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
1263 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1264 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1265 	    bp->b_bcount, bp->b_data);
1266 #endif
1267 
1268 	/*
1269 	 * load request.  we have to calculate the correct block number based
1270 	 * on partition info.
1271 	 *
1272 	 * also, note that there are two kinds of buf structures, those with
1273 	 * B_PHYS set and those without B_PHYS.   if B_PHYS is set, then it is
1274 	 * a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
1275 	 * buffer which has already been mapped into DVMA space. (Not on sun3)
1276 	 * However, if B_PHYS is not set, then the buffer is a normal system
1277 	 * buffer which does *not* live in DVMA space.  In that case we call
1278 	 * dvma_mapin to map it into DVMA space so we can do the DMA to it.
1279 	 *
1280 	 * in cases where we do a dvma_mapin, note that iorq points to the buffer
1281 	 * as mapped into DVMA space, where as the bp->b_data points to its
1282 	 * non-DVMA mapping.
1283 	 *
1284 	 * XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
1285 	 * into dvma space, only that it was remapped into the kernel.
1286 	 * We ALWAYS have to remap the kernel buf into DVMA space.
1287 	 * (It is done inexpensively, using whole segments!)
1288 	 */
1289 
1290 	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
1291 	    xysc->sc_dk.dk_label->d_partitions[partno].p_offset);
1292 
1293 	dbuf = dvma_mapin(bp->b_data, bp->b_bcount);
1294 	if (dbuf == NULL) {	/* out of DVMA space */
1295 		printf("%s: warning: out of DVMA space\n",
1296 			   xycsc->sc_dev.dv_xname);
1297 		return (XY_ERR_FAIL);	/* XXX: need some sort of
1298 		                         * call-back scheme here? */
1299 	}
1300 
1301 	/* init iorq and load iopb from it */
1302 
1303 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
1304 	    bp->b_bcount / XYFM_BPS, dbuf, bp);
1305 
1306 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
1307 
1308 	/* Instrumentation. */
1309 	disk_busy(&xysc->sc_dk);
1310 
1311 	return (XY_ERR_AOK);
1312 }
1313 
1314 
1315 /*
1316  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
1317  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
1318  *
1319  * note: caller frees iorq in all cases except NORM
1320  *
1321  * return value:
1322  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
1323  *   WAIT: XY_AOK (success), <error-code> (failed)
1324  *   POLL: <same as WAIT>
1325  *   NOQ : <same as NORM>
1326  *
1327  * there are three sources for i/o requests:
1328  * [1] xystrategy: normal block I/O, using "struct buf" system.
1329  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1330  * [3] open/ioctl: these are I/O requests done in the context of a process,
1331  *                 and the process should block until they are done.
1332  *
1333  * software state is stored in the iorq structure.  each iorq has an
1334  * iopb structure.  the hardware understands the iopb structure.
1335  * every command must go through an iopb.  a 450 handles one iopb at a
1336  * time, where as a 451 can take them in chains.  [the 450 claims it
1337  * can handle chains, but is appears to be buggy...]   iopb are allocated
1338  * in DVMA space at boot up time.  each disk gets one iopb, and the
1339  * controller gets one (for POLL and WAIT commands).  what happens if
1340  * the iopb is busy?  for i/o type [1], the buffers are queued at the
1341  * "buff" layer and * picked up later by the interrupt routine.  for case
1342  * [2] we can only be blocked if there is a WAIT type I/O request being
1343  * run.   since this can only happen when we are crashing, we wait a sec
1344  * and then steal the IOPB.  for case [3] the process can sleep
1345  * on the iorq free list until some iopbs are avaliable.
1346  */
1347 
1348 
1349 int
1350 xyc_submit_iorq(xycsc, iorq, type)
1351 	struct xyc_softc *xycsc;
1352 	struct xy_iorq *iorq;
1353 	int     type;
1354 
1355 {
1356 	struct xy_iopb *iopb;
1357 	u_long  iopbaddr;
1358 
1359 #ifdef XYC_DEBUG
1360 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
1361 		xycsc->sc_dev.dv_xname, iorq, type);
1362 #endif
1363 
1364 	/* first check and see if controller is busy */
1365 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
1366 #ifdef XYC_DEBUG
1367 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
1368 #endif
1369 		if (type == XY_SUB_NOQ)
1370 			return (XY_ERR_FAIL);	/* failed */
1371 		switch (type) {
1372 		case XY_SUB_NORM:
1373 			return XY_ERR_AOK;	/* success */
1374 		case XY_SUB_WAIT:
1375 			while (iorq->iopb->done == 0) {
1376 				sleep(iorq, PRIBIO);
1377 			}
1378 			return (iorq->errno);
1379 		case XY_SUB_POLL:		/* steal controller */
1380 			iopbaddr = xycsc->xyc->xyc_rsetup; /* RESET */
1381 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
1382 				panic("xyc_submit_iorq: stuck xyc");
1383 			printf("%s: stole controller\n",
1384 				xycsc->sc_dev.dv_xname);
1385 			break;
1386 		default:
1387 			panic("xyc_submit_iorq adding");
1388 		}
1389 	}
1390 
1391 	iopb = xyc_chain(xycsc, iorq);	 /* build chain */
1392 	if (iopb == NULL) { /* nothing doing? */
1393 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
1394 			return(XY_ERR_AOK);
1395 		panic("xyc_submit_iorq: xyc_chain failed!\n");
1396 	}
1397 	iopbaddr = dvma_kvtopa((long) iopb, BUS_VME16);
1398 
1399 	XYC_GO(xycsc->xyc, iopbaddr);
1400 
1401 	/* command now running, wrap it up */
1402 	switch (type) {
1403 	case XY_SUB_NORM:
1404 	case XY_SUB_NOQ:
1405 		return (XY_ERR_AOK);	/* success */
1406 	case XY_SUB_WAIT:
1407 		while (iorq->iopb->done == 0) {
1408 			sleep(iorq, PRIBIO);
1409 		}
1410 		return (iorq->errno);
1411 	case XY_SUB_POLL:
1412 		return (xyc_piodriver(xycsc, iorq));
1413 	default:
1414 		panic("xyc_submit_iorq wrap up");
1415 	}
1416 	panic("xyc_submit_iorq");
1417 	return 0;	/* not reached */
1418 }
1419 
1420 
1421 /*
1422  * xyc_chain: build a chain.  return dvma address of first element in
1423  * the chain.   iorq != NULL: means we only want that item on the chain.
1424  */
1425 
1426 struct xy_iopb *
1427 xyc_chain(xycsc, iorq)
1428 
1429 struct xyc_softc *xycsc;
1430 struct xy_iorq *iorq;
1431 
1432 {
1433   int togo, chain, hand;
1434   struct xy_iopb *iopb, *prev_iopb;
1435   bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
1436 
1437   /*
1438    * promote control IOPB to the top
1439    */
1440   if (iorq == NULL) {
1441     if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1442         XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1443 		xycsc->iopbase[XYC_CTLIOPB].done == 0)
1444     iorq = &xycsc->reqs[XYC_CTLIOPB];
1445   }
1446   /*
1447    * special case: if iorq != NULL then we have a POLL or WAIT request.
1448    * we let these take priority and do them first.
1449    */
1450   if (iorq) {
1451     xycsc->xy_chain[0] = iorq;
1452     iorq->iopb->chen = 0;
1453     return(iorq->iopb);
1454   }
1455 
1456   /*
1457    * NORM case: do round robin and maybe chain (if allowed and possible)
1458    */
1459 
1460   chain = 0;
1461   hand = xycsc->xy_hand;
1462   xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1463 
1464   for (togo = XYC_MAXIOPB ; togo > 0 ; togo--, hand = (hand + 1) % XYC_MAXIOPB){
1465 
1466     if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1467 		xycsc->iopbase[hand].done)
1468       continue;   /* not ready-for-i/o */
1469 
1470     xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1471     iopb = xycsc->xy_chain[chain]->iopb;
1472     iopb->chen = 0;
1473     if (chain != 0) {   /* adding a link to a chain? */
1474 	  prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1475 	  prev_iopb->chen = 1;
1476 	  prev_iopb->nxtiopb = 0xffff &
1477 		dvma_kvtopa((long) iopb, BUS_VME16);
1478     } else {            /* head of chain */
1479 		iorq = xycsc->xy_chain[chain];
1480     }
1481     chain++;
1482     if (xycsc->no_ols) break;   /* quit if chaining dis-allowed */
1483   }
1484   return(iorq ? iorq->iopb : NULL);
1485 }
1486 
1487 /*
1488  * xyc_piodriver
1489  *
1490  * programmed i/o driver.   this function takes over the computer
1491  * and drains off the polled i/o request.   it returns the status of the iorq
1492  * the caller is interesting in.
1493  */
1494 int
1495 xyc_piodriver(xycsc, iorq)
1496 	struct xyc_softc *xycsc;
1497 	struct xy_iorq  *iorq;
1498 
1499 {
1500 	int     nreset = 0;
1501 	int     retval = 0;
1502 	u_long  res;
1503 	struct xyc *xyc = xycsc->xyc;
1504 #ifdef XYC_DEBUG
1505 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
1506 #endif
1507 
1508 	while (iorq->iopb->done == 0) {
1509 
1510 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1511 
1512 		/* we expect some progress soon */
1513 		if (res == XY_ERR_FAIL && nreset >= 2) {
1514 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1515 #ifdef XYC_DEBUG
1516 			printf("xyc_piodriver: timeout\n");
1517 #endif
1518 			return (XY_ERR_FAIL);
1519 		}
1520 		if (res == XY_ERR_FAIL) {
1521 			if (xyc_reset(xycsc, 0,
1522 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
1523 				      XY_ERR_FAIL,
1524 				      0) == XY_ERR_FAIL)
1525 				return (XY_ERR_FAIL);	/* flushes all but POLL
1526 							 * requests, resets */
1527 			continue;
1528 		}
1529 
1530 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
1531 
1532 		if (iorq->iopb->done == 0)
1533 			xyc_start(xycsc, iorq);
1534 	}
1535 
1536 	/* get return value */
1537 
1538 	retval = iorq->errno;
1539 
1540 #ifdef XYC_DEBUG
1541 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1542 	    iorq->errno, xyc_e2str(iorq->errno));
1543 #endif
1544 
1545 	/* start up any bufs that have queued */
1546 
1547 	xyc_start(xycsc, NULL);
1548 
1549 	return (retval);
1550 }
1551 
1552 /*
1553  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
1554  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1555  */
1556 int
1557 xyc_xyreset(xycsc, xysc)
1558 	struct xyc_softc *xycsc;
1559 	struct xy_softc *xysc;
1560 
1561 {
1562 	struct xy_iopb tmpiopb;
1563 	u_long  addr;
1564 	int     del;
1565 	bcopy(xycsc->ciopb, &tmpiopb, sizeof(tmpiopb));
1566 	xycsc->ciopb->chen = xycsc->ciopb->done = xycsc->ciopb->errs = 0;
1567 	xycsc->ciopb->ien = 0;
1568 	xycsc->ciopb->com = XYCMD_RST;
1569 	xycsc->ciopb->unit = xysc->xy_drive;
1570 	addr = dvma_kvtopa((long) xycsc->ciopb, BUS_VME16);
1571 
1572 	XYC_GO(xycsc->xyc, addr);
1573 
1574 	del = XYC_RESETUSEC;
1575 	while (del > 0) {
1576 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0) break;
1577 		DELAY(1);
1578 		del--;
1579 	}
1580 
1581 	if (del <= 0 || xycsc->ciopb->errs) {
1582 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
1583 		    xyc_e2str(xycsc->ciopb->errno));
1584 		del = xycsc->xyc->xyc_rsetup;
1585 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1586 			panic("xyc_reset");
1587 	} else {
1588 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
1589 	}
1590 	bcopy(&tmpiopb, xycsc->ciopb, sizeof(tmpiopb));
1591 }
1592 
1593 
1594 /*
1595  * xyc_reset: reset everything: requests are marked as errors except
1596  * a polled request (which is resubmitted)
1597  */
1598 int
1599 xyc_reset(xycsc, quiet, blastmode, error, xysc)
1600 	struct xyc_softc *xycsc;
1601 	int     quiet, error;
1602 	struct xy_iorq *blastmode;
1603 	struct xy_softc *xysc;
1604 
1605 {
1606 	int     del = 0, lcv, poll = -1, retval = XY_ERR_AOK;
1607 	struct xy_iorq *iorq;
1608 
1609 	/* soft reset hardware */
1610 
1611 	if (!quiet)
1612 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
1613 	del = xycsc->xyc->xyc_rsetup;
1614 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1615 	if (del == XY_ERR_FAIL) {
1616 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
1617 		retval = XY_ERR_FAIL;
1618 	}
1619 	if (xysc)
1620 		xyc_xyreset(xycsc, xysc);
1621 
1622 	/* fix queues based on "blast-mode" */
1623 
1624 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1625 		iorq = &xycsc->reqs[lcv];
1626 
1627 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1628 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1629 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
1630 			/* is it active? */
1631 			continue;
1632 
1633 		if (blastmode == XY_RSET_ALL ||
1634 				blastmode != iorq) {
1635 			/* failed */
1636 			iorq->errno = error;
1637 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1638 			switch (XY_STATE(iorq->mode)) {
1639 			case XY_SUB_NORM:
1640 			    iorq->buf->b_error = EIO;
1641 			    iorq->buf->b_flags |= B_ERROR;
1642 			    iorq->buf->b_resid =
1643 			       iorq->sectcnt * XYFM_BPS;
1644 				/* Sun3: map/unmap regardless of B_PHYS */
1645 				dvma_mapout(iorq->dbufbase,
1646 				            iorq->buf->b_bcount);
1647 			    iorq->xy->xyq.b_actf =
1648 					iorq->buf->b_actf;
1649 			    disk_unbusy(&iorq->xy->sc_dk,
1650 					        (iorq->buf->b_bcount -
1651 					         iorq->buf->b_resid));
1652 			    biodone(iorq->buf);
1653 			    iorq->mode = XY_SUB_FREE;
1654 			    break;
1655 			case XY_SUB_WAIT:
1656 			    wakeup(iorq);
1657 			case XY_SUB_POLL:
1658 			    iorq->mode =
1659 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1660 			    break;
1661 			}
1662 
1663 		} else {
1664 
1665 			/* resubmit, no need to do anything here */
1666 		}
1667 	}
1668 
1669 	/*
1670 	 * now, if stuff is waiting, start it.
1671 	 * since we just reset it should go
1672 	 */
1673 	xyc_start(xycsc, NULL);
1674 
1675 	return (retval);
1676 }
1677 
1678 /*
1679  * xyc_start: start waiting buffers
1680  */
1681 
1682 int
1683 xyc_start(xycsc, iorq)
1684 	struct xyc_softc *xycsc;
1685 	struct xy_iorq *iorq;
1686 
1687 {
1688 	int lcv;
1689 	struct xy_softc *xy;
1690 
1691 	if (iorq == NULL) {
1692 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1693 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1694 			if (xy->xyq.b_actf == NULL) continue;
1695 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
1696 			xyc_startbuf(xycsc, xy, xy->xyq.b_actf);
1697 		}
1698 	}
1699 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1700 }
1701 
1702 /*
1703  * xyc_remove_iorq: remove "done" IOPB's.
1704  */
1705 
1706 int
1707 xyc_remove_iorq(xycsc)
1708 	struct xyc_softc *xycsc;
1709 
1710 {
1711 	int     errno, rq, comm, errs;
1712 	struct xyc *xyc = xycsc->xyc;
1713 	u_long  addr;
1714 	struct xy_iopb *iopb;
1715 	struct xy_iorq *iorq;
1716 	struct buf *bp;
1717 
1718 	if (xyc->xyc_csr & XYC_DERR) {
1719 		/*
1720 		 * DOUBLE ERROR: should never happen under normal use. This
1721 		 * error is so bad, you can't even tell which IOPB is bad, so
1722 		 * we dump them all.
1723 		 */
1724 		errno = XY_ERR_DERR;
1725 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1726 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
1727 			printf("%s: soft reset failed!\n",
1728 				xycsc->sc_dev.dv_xname);
1729 			panic("xyc_remove_iorq: controller DEAD");
1730 		}
1731 		return (XY_ERR_AOK);
1732 	}
1733 
1734 	/*
1735 	 * get iopb that is done, loop down the chain
1736 	 */
1737 
1738 	if (xyc->xyc_csr & XYC_ERR) {
1739 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
1740 	}
1741 	if (xyc->xyc_csr & XYC_IPND) {
1742 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1743 	}
1744 
1745 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1746 		iorq = xycsc->xy_chain[rq];
1747 		if (iorq == NULL) break; /* done ! */
1748 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1749 			continue;	/* free, or done */
1750 		iopb = iorq->iopb;
1751 		if (iopb->done == 0)
1752 			continue;	/* not done yet */
1753 
1754 		comm = iopb->com;
1755 		errs = iopb->errs;
1756 
1757 		if (errs)
1758 			iorq->errno = iopb->errno;
1759 		else
1760 			iorq->errno = 0;
1761 
1762 		/* handle non-fatal errors */
1763 
1764 		if (errs &&
1765 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1766 			continue;	/* AOK: we resubmitted it */
1767 
1768 
1769 		/* this iorq is now done (hasn't been restarted or anything) */
1770 
1771 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1772 			xyc_perror(iorq, iopb, 0);
1773 
1774 		/* now, if read/write check to make sure we got all the data
1775 		 * we needed. (this may not be the case if we got an error in
1776 		 * the middle of a multisector request).   */
1777 
1778 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1779 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
1780 			/* we just successfully processed a bad144 sector
1781 			 * note: if we are in bad 144 mode, the pointers have
1782 			 * been advanced already (see above) and are pointing
1783 			 * at the bad144 sector.   to exit bad144 mode, we
1784 			 * must advance the pointers 1 sector and issue a new
1785 			 * request if there are still sectors left to process
1786 			 *
1787 			 */
1788 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
1789 
1790 			/* exit b144 mode */
1791 			iorq->mode = iorq->mode & (~XY_MODE_B144);
1792 
1793 			if (iorq->sectcnt) {	/* more to go! */
1794 				iorq->lasterror = iorq->errno = iopb->errno = 0;
1795 				iopb->errs = iopb->done = 0;
1796 				iorq->tries = 0;
1797 				iopb->scnt = iorq->sectcnt;
1798 				iopb->cyl = iorq->blockno /
1799 						iorq->xy->sectpercyl;
1800 				iopb->head =
1801 					(iorq->blockno / iorq->xy->nhead) %
1802 						iorq->xy->nhead;
1803 				iopb->sect = iorq->blockno % XYFM_BPS;
1804 				addr = dvma_kvtopa((long) iorq->dbuf, BUS_VME16);
1805 				iopb->dataa = (addr & 0xffff);
1806 				iopb->datar = ((addr & 0xff0000) >> 16);
1807 				/* will resubit at end */
1808 				continue;
1809 			}
1810 		}
1811 		/* final cleanup, totally done with this request */
1812 
1813 		switch (XY_STATE(iorq->mode)) {
1814 		case XY_SUB_NORM:
1815 			bp = iorq->buf;
1816 			if (errs) {
1817 				bp->b_error = EIO;
1818 				bp->b_flags |= B_ERROR;
1819 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
1820 			} else {
1821 				bp->b_resid = 0;	/* done */
1822 			}
1823 			/* Sun3: map/unmap regardless of B_PHYS */
1824 			dvma_mapout(iorq->dbufbase,
1825 					    iorq->buf->b_bcount);
1826 			iorq->xy->xyq.b_actf = bp->b_actf;
1827 			disk_unbusy(&iorq->xy->sc_dk,
1828 			    (bp->b_bcount - bp->b_resid));
1829 			biodone(bp);
1830 			iorq->mode = XY_SUB_FREE;
1831 			break;
1832 		case XY_SUB_WAIT:
1833 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1834 			wakeup(iorq);
1835 			break;
1836 		case XY_SUB_POLL:
1837 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1838 			break;
1839 		}
1840 	}
1841 
1842 	return (XY_ERR_AOK);
1843 }
1844 
1845 /*
1846  * xyc_perror: print error.
1847  * - if still_trying is true: we got an error, retried and got a
1848  *   different error.  in that case lasterror is the old error,
1849  *   and errno is the new one.
1850  * - if still_trying is not true, then if we ever had an error it
1851  *   is in lasterror. also, if iorq->errno == 0, then we recovered
1852  *   from that error (otherwise iorq->errno == iorq->lasterror).
1853  */
1854 void
1855 xyc_perror(iorq, iopb, still_trying)
1856 	struct xy_iorq *iorq;
1857 	struct xy_iopb *iopb;
1858 	int     still_trying;
1859 
1860 {
1861 
1862 	int     error = iorq->lasterror;
1863 
1864 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
1865 	    : iorq->xyc->sc_dev.dv_xname);
1866 	if (iorq->buf)
1867 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
1868 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
1869 		printf("%s %d/%d/%d: ",
1870 			(iopb->com == XYCMD_RD) ? "read" : "write",
1871 			iopb->cyl, iopb->head, iopb->sect);
1872 	printf("%s", xyc_e2str(error));
1873 
1874 	if (still_trying)
1875 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
1876 	else
1877 		if (iorq->errno == 0)
1878 			printf(" [recovered in %d tries]", iorq->tries);
1879 
1880 	printf("\n");
1881 }
1882 
1883 /*
1884  * xyc_error: non-fatal error encountered... recover.
1885  * return AOK if resubmitted, return FAIL if this iopb is done
1886  */
1887 int
1888 xyc_error(xycsc, iorq, iopb, comm)
1889 	struct xyc_softc *xycsc;
1890 	struct xy_iorq *iorq;
1891 	struct xy_iopb *iopb;
1892 	int     comm;
1893 
1894 {
1895 	int     errno = iorq->errno;
1896 	int     erract = xyc_entoact(errno);
1897 	int     oldmode, advance, i;
1898 
1899 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
1900 		oldmode = iorq->mode;
1901 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
1902 		/* make xyc_start ignore us */
1903 		xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
1904 		iorq->mode = oldmode;
1905 	}
1906 	/* check for read/write to a sector in bad144 table if bad: redirect
1907 	 * request to bad144 area */
1908 
1909 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
1910 	    (iorq->mode & XY_MODE_B144) == 0) {
1911 		advance = iorq->sectcnt - iopb->scnt;
1912 		XYC_ADVANCE(iorq, advance);
1913 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
1914 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
1915 			    iorq->blockno % iorq->xy->nsect)) != -1) {
1916 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
1917 							 * redirect */
1918 			iopb->errno = iopb->done = iopb->errs = 0;
1919 			iopb->scnt = 1;
1920 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
1921 			/* second to last acyl */
1922 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
1923 								 * standard */
1924 			iopb->head = i / iorq->xy->nhead;
1925 			iopb->sect = i % iorq->xy->nhead;
1926 			/* will resubmit when we come out of remove_iorq */
1927 			return (XY_ERR_AOK);	/* recovered! */
1928 		}
1929 	}
1930 
1931 	/*
1932 	 * it isn't a bad144 sector, must be real error! see if we can retry
1933 	 * it?
1934 	 */
1935 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1936 		xyc_perror(iorq, iopb, 1);	/* inform of error state
1937 						 * change */
1938 	iorq->lasterror = errno;
1939 
1940 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
1941 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
1942 		iorq->tries++;
1943 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
1944 		/* will resubmit at end of remove_iorq */
1945 		return (XY_ERR_AOK);	/* recovered! */
1946 	}
1947 
1948 	/* failed to recover from this error */
1949 	return (XY_ERR_FAIL);
1950 }
1951 
1952 /*
1953  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
1954  */
1955 void
1956 xyc_tick(arg)
1957 	void   *arg;
1958 
1959 {
1960 	struct xyc_softc *xycsc = arg;
1961 	int     lcv, s, reset = 0;
1962 
1963 	/* reduce ttl for each request if one goes to zero, reset xyc */
1964 	s = splbio();
1965 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1966 		if (xycsc->reqs[lcv].mode == 0 ||
1967 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
1968 			continue;
1969 		xycsc->reqs[lcv].ttl--;
1970 		if (xycsc->reqs[lcv].ttl == 0)
1971 			reset = 1;
1972 	}
1973 	if (reset) {
1974 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
1975 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
1976 	}
1977 	splx(s);
1978 
1979 	/* until next time */
1980 
1981 	timeout(xyc_tick, xycsc, XYC_TICKCNT);
1982 }
1983 
1984 /*
1985  * xyc_ioctlcmd: this function provides a user level interface to the
1986  * controller via ioctl.   this allows "format" programs to be written
1987  * in user code, and is also useful for some debugging.   we return
1988  * an error code.   called at user priority.
1989  *
1990  * XXX missing a few commands (see the 7053 driver for ideas)
1991  */
1992 int
1993 xyc_ioctlcmd(xy, dev, xio)
1994 	struct xy_softc *xy;
1995 	dev_t   dev;
1996 	struct xd_iocmd *xio;
1997 
1998 {
1999 	int     s, err, rqno, dummy;
2000 	caddr_t dvmabuf = NULL;
2001 	struct xyc_softc *xycsc;
2002 
2003 	/* check sanity of requested command */
2004 
2005 	switch (xio->cmd) {
2006 
2007 	case XYCMD_NOP:	/* no op: everything should be zero */
2008 		if (xio->subfn || xio->dptr || xio->dlen ||
2009 		    xio->block || xio->sectcnt)
2010 			return (EINVAL);
2011 		break;
2012 
2013 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
2014 	case XYCMD_WR:
2015 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2016 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2017 			return (EINVAL);
2018 		break;
2019 
2020 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
2021 		return (EINVAL);
2022 
2023 		break;
2024 
2025 	default:
2026 		return (EINVAL);/* ??? */
2027 	}
2028 
2029 	/* create DVMA buffer for request if needed */
2030 
2031 	if (xio->dlen) {
2032 		dvmabuf = dvma_malloc(xio->dlen);
2033 		if (xio->cmd == XYCMD_WR) {
2034 			if (err = copyin(xio->dptr, dvmabuf, xio->dlen)) {
2035 				dvma_free(dvmabuf, xio->dlen);
2036 				return (err);
2037 			}
2038 		}
2039 	}
2040 	/* do it! */
2041 
2042 	err = 0;
2043 	xycsc = xy->parent;
2044 	s = splbio();
2045 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2046 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2047 	if (rqno == XY_ERR_FAIL) {
2048 		err = EIO;
2049 		goto done;
2050 	}
2051 	xio->errno = xycsc->ciorq->errno;
2052 	xio->tries = xycsc->ciorq->tries;
2053 	XYC_DONE(xycsc, dummy);
2054 
2055 	if (xio->cmd == XYCMD_RD)
2056 		err = copyout(dvmabuf, xio->dptr, xio->dlen);
2057 
2058 done:
2059 	splx(s);
2060 	if (dvmabuf)
2061 		dvma_free(dvmabuf, xio->dlen);
2062 	return (err);
2063 }
2064 
2065 /*
2066  * xyc_e2str: convert error code number into an error string
2067  */
2068 char *
2069 xyc_e2str(no)
2070 	int     no;
2071 {
2072 	switch (no) {
2073 	case XY_ERR_FAIL:
2074 		return ("Software fatal error");
2075 	case XY_ERR_DERR:
2076 		return ("DOUBLE ERROR");
2077 	case XY_ERR_AOK:
2078 		return ("Successful completion");
2079 	case XY_ERR_IPEN:
2080 		return("Interrupt pending");
2081 	case XY_ERR_BCFL:
2082 		return("Busy conflict");
2083 	case XY_ERR_TIMO:
2084 		return("Operation timeout");
2085 	case XY_ERR_NHDR:
2086 		return("Header not found");
2087 	case XY_ERR_HARD:
2088 		return("Hard ECC error");
2089 	case XY_ERR_ICYL:
2090 		return("Illegal cylinder address");
2091 	case XY_ERR_ISEC:
2092 		return("Illegal sector address");
2093 	case XY_ERR_SMAL:
2094 		return("Last sector too small");
2095 	case XY_ERR_SACK:
2096 		return("Slave ACK error (non-existent memory)");
2097 	case XY_ERR_CHER:
2098 		return("Cylinder and head/header error");
2099 	case XY_ERR_SRTR:
2100 		return("Auto-seek retry successful");
2101 	case XY_ERR_WPRO:
2102 		return("Write-protect error");
2103 	case XY_ERR_UIMP:
2104 		return("Unimplemented command");
2105 	case XY_ERR_DNRY:
2106 		return("Drive not ready");
2107 	case XY_ERR_SZER:
2108 		return("Sector count zero");
2109 	case XY_ERR_DFLT:
2110 		return("Drive faulted");
2111 	case XY_ERR_ISSZ:
2112 		return("Illegal sector size");
2113 	case XY_ERR_SLTA:
2114 		return("Self test A");
2115 	case XY_ERR_SLTB:
2116 		return("Self test B");
2117 	case XY_ERR_SLTC:
2118 		return("Self test C");
2119 	case XY_ERR_SOFT:
2120 		return("Soft ECC error");
2121 	case XY_ERR_SFOK:
2122 		return("Soft ECC error recovered");
2123 	case XY_ERR_IHED:
2124 		return("Illegal head");
2125 	case XY_ERR_DSEQ:
2126 		return("Disk sequencer error");
2127 	case XY_ERR_SEEK:
2128 		return("Seek error");
2129 	default:
2130 		return ("Unknown error");
2131 	}
2132 }
2133 
2134 int
2135 xyc_entoact(errno)
2136 
2137 int errno;
2138 
2139 {
2140   switch (errno) {
2141     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
2142     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
2143     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
2144     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
2145     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
2146 
2147 	return(XY_ERA_PROG); /* program error ! */
2148 
2149     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
2150     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
2151     case XY_ERR_SOFT:
2152 
2153 	return(XY_ERA_HARD); /* hard error, retry */
2154 
2155     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
2156 
2157 	return(XY_ERA_RSET); /* hard error reset */
2158 
2159     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
2160 
2161 	return(XY_ERA_SOFT); /* an FYI error */
2162 
2163     case XY_ERR_WPRO:
2164 
2165 	return(XY_ERA_WPRO); /* write protect */
2166   }
2167 
2168   return(XY_ERA_PROG); /* ??? */
2169 }
2170