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