xref: /netbsd/sys/arch/sun3/dev/xy.c (revision bf9ec67e)
1 /*	$NetBSD: xy.c,v 1.35 2001/09/05 14:03:49 tsutsui 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 <uvm/uvm_extern.h>
78 
79 #include <dev/sun/disklabel.h>
80 
81 #include <machine/autoconf.h>
82 #include <machine/dvma.h>
83 
84 #include <sun3/dev/xyreg.h>
85 #include <sun3/dev/xyvar.h>
86 #include <sun3/dev/xio.h>
87 
88 #include "locators.h"
89 
90 /*
91  * Print a complaint when no xy children were specified
92  * in the config file.  Better than a link error...
93  *
94  * XXX: Some folks say this driver should be split in two,
95  * but that seems pointless with ONLY one type of child.
96  */
97 #include "xy.h"
98 #if NXY == 0
99 #error "xyc but no xy?"
100 #endif
101 
102 /*
103  * macros
104  */
105 
106 /*
107  * XYC_GO: start iopb ADDR (DVMA addr in a u_long) on XYC
108  */
109 #define XYC_GO(XYC, ADDR) { \
110 	(XYC)->xyc_addr_lo = ((ADDR) & 0xff); \
111 	(ADDR) = ((ADDR) >> 8); \
112 	(XYC)->xyc_addr_hi = ((ADDR) & 0xff); \
113 	(ADDR) = ((ADDR) >> 8); \
114 	(XYC)->xyc_reloc_lo = ((ADDR) & 0xff); \
115 	(ADDR) = ((ADDR) >> 8); \
116 	(XYC)->xyc_reloc_hi = (ADDR); \
117 	(XYC)->xyc_csr = XYC_GBSY; /* go! */ \
118 }
119 
120 /*
121  * XYC_DONE: don't need IORQ, get error code and free (done after xyc_cmd)
122  */
123 
124 #define XYC_DONE(SC,ER) { \
125 	if ((ER) == XY_ERR_AOK) { \
126 		(ER) = (SC)->ciorq->errno; \
127 		(SC)->ciorq->mode = XY_SUB_FREE; \
128 		wakeup((SC)->ciorq); \
129 	} \
130 	}
131 
132 /*
133  * XYC_ADVANCE: advance iorq's pointers by a number of sectors
134  */
135 
136 #define XYC_ADVANCE(IORQ, N) { \
137 	if (N) { \
138 		(IORQ)->sectcnt -= (N); \
139 		(IORQ)->blockno += (N); \
140 		(IORQ)->dbuf += ((N)*XYFM_BPS); \
141 	} \
142 }
143 
144 /*
145  * note - addresses you can sleep on:
146  *   [1] & of xy_softc's "state" (waiting for a chance to attach a drive)
147  *   [2] & an iorq (waiting for an XY_SUB_WAIT iorq to finish)
148  */
149 
150 
151 /*
152  * function prototypes
153  * "xyc_*" functions are internal, all others are external interfaces
154  */
155 
156 /* internals */
157 struct xy_iopb *xyc_chain __P((struct xyc_softc *, struct xy_iorq *));
158 int	xyc_cmd __P((struct xyc_softc *, int, int, int, int, int, char *, int));
159 char   *xyc_e2str __P((int));
160 int	xyc_entoact __P((int));
161 int	xyc_error __P((struct xyc_softc *, struct xy_iorq *,
162 		   struct xy_iopb *, int));
163 int	xyc_ioctlcmd __P((struct xy_softc *, dev_t dev, struct xd_iocmd *));
164 void	xyc_perror __P((struct xy_iorq *, struct xy_iopb *, int));
165 int	xyc_piodriver __P((struct xyc_softc *, struct xy_iorq *));
166 int	xyc_remove_iorq __P((struct xyc_softc *));
167 int	xyc_reset __P((struct xyc_softc *, int, struct xy_iorq *, int,
168 			struct xy_softc *));
169 inline void xyc_rqinit __P((struct xy_iorq *, struct xyc_softc *,
170 			    struct xy_softc *, int, u_long, int,
171 			    caddr_t, struct buf *));
172 void	xyc_rqtopb __P((struct xy_iorq *, struct xy_iopb *, int, int));
173 void	xyc_start __P((struct xyc_softc *, struct xy_iorq *));
174 int	xyc_startbuf __P((struct xyc_softc *, struct xy_softc *, struct buf *));
175 int	xyc_submit_iorq __P((struct xyc_softc *, struct xy_iorq *, int));
176 void	xyc_tick __P((void *));
177 int	xyc_unbusy __P((struct xyc *, int));
178 void	xyc_xyreset __P((struct xyc_softc *, struct xy_softc *));
179 
180 /* machine interrupt hook */
181 int	xycintr __P((void *));
182 
183 /* bdevsw, cdevsw */
184 bdev_decl(xy);
185 cdev_decl(xy);
186 
187 /* autoconf */
188 static int	xycmatch __P((struct device *, struct cfdata *, void *));
189 static void	xycattach __P((struct device *, struct device *, void *));
190 static int  xyc_print __P((void *, const char *name));
191 
192 static int	xymatch __P((struct device *, struct cfdata *, void *));
193 static void	xyattach __P((struct device *, struct device *, void *));
194 static void xy_init __P((struct xy_softc *));
195 
196 static	void xydummystrat __P((struct buf *));
197 int	xygetdisklabel __P((struct xy_softc *, void *));
198 
199 /*
200  * cfattach's: device driver interface to autoconfig
201  */
202 
203 struct cfattach xyc_ca = {
204 	sizeof(struct xyc_softc), xycmatch, xycattach
205 };
206 
207 struct cfattach xy_ca = {
208 	sizeof(struct xy_softc), xymatch, xyattach
209 };
210 
211 extern struct cfdriver xy_cd;
212 
213 struct xyc_attach_args {	/* this is the "aux" args to xyattach */
214 	int	driveno;	/* unit number */
215 };
216 
217 /*
218  * dkdriver
219  */
220 
221 struct dkdriver xydkdriver = { xystrategy };
222 
223 /*
224  * start: disk label fix code (XXX)
225  */
226 
227 static void *xy_labeldata;
228 
229 static void
230 xydummystrat(bp)
231 	struct buf *bp;
232 {
233 	if (bp->b_bcount != XYFM_BPS)
234 		panic("xydummystrat");
235 	memcpy(bp->b_data, xy_labeldata, XYFM_BPS);
236 	bp->b_flags |= B_DONE;
237 	bp->b_flags &= ~B_BUSY;
238 }
239 
240 int
241 xygetdisklabel(xy, b)
242 	struct xy_softc *xy;
243 	void *b;
244 {
245 	char *err;
246 	struct sun_disklabel *sdl;
247 
248 	/* We already have the label data in `b'; setup for dummy strategy */
249 	xy_labeldata = b;
250 
251 	/* Required parameter for readdisklabel() */
252 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
253 
254 	err = readdisklabel(MAKEDISKDEV(0, xy->sc_dev.dv_unit, RAW_PART),
255 					xydummystrat,
256 				xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
257 	if (err) {
258 		printf("%s: %s\n", xy->sc_dev.dv_xname, err);
259 		return(XY_ERR_FAIL);
260 	}
261 
262 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
263 	sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
264 	if (sdl->sl_magic == SUN_DKMAGIC)
265 		xy->pcyl = sdl->sl_pcyl;
266 	else {
267 		printf("%s: WARNING: no `pcyl' in disk label.\n",
268 			   xy->sc_dev.dv_xname);
269 		xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
270 			xy->sc_dk.dk_label->d_acylinders;
271 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
272 		xy->sc_dev.dv_xname, xy->pcyl);
273 	}
274 
275 	xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
276 	xy->acyl = xy->sc_dk.dk_label->d_acylinders;
277 	xy->nhead = xy->sc_dk.dk_label->d_ntracks;
278 	xy->nsect = xy->sc_dk.dk_label->d_nsectors;
279 	xy->sectpercyl = xy->nhead * xy->nsect;
280 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
281                                           	  * sun->bsd */
282 	return(XY_ERR_AOK);
283 }
284 
285 /*
286  * end: disk label fix code (XXX)
287  */
288 
289 /*
290  * a u t o c o n f i g   f u n c t i o n s
291  */
292 
293 /*
294  * xycmatch: determine if xyc is present or not.   we do a
295  * soft reset to detect the xyc.
296  */
297 static int
298 xycmatch(parent, cf, aux)
299 	struct device *parent;
300 	struct cfdata *cf;
301 	void *aux;
302 {
303 	struct confargs *ca = aux;
304 
305 	/* No default VME address. */
306 	if (ca->ca_paddr == -1)
307 		return (0);
308 
309 	/* Make sure something is there... */
310 	if (bus_peek(ca->ca_bustype, ca->ca_paddr + 5, 1) == -1)
311 		return (0);
312 
313 	/* Default interrupt priority. */
314 	if (ca->ca_intpri == -1)
315 		ca->ca_intpri = 2;
316 
317 	return (1);
318 }
319 
320 /*
321  * xycattach: attach controller
322  */
323 static void
324 xycattach(parent, self, aux)
325 	struct device *parent, *self;
326 	void   *aux;
327 {
328 	struct xyc_softc *xyc = (void *) self;
329 	struct confargs *ca = aux;
330 	struct xyc_attach_args xa;
331 	int     lcv, err, res, pbsz;
332 	void	*tmp, *tmp2;
333 	u_long	ultmp;
334 
335 	/* get addressing and intr level stuff from autoconfig and load it
336 	 * into our xyc_softc. */
337 
338 	xyc->xyc = (struct xyc *)
339 		bus_mapin(ca->ca_bustype, ca->ca_paddr, sizeof(struct xyc));
340 	xyc->bustype = ca->ca_bustype;
341 	xyc->ipl     = ca->ca_intpri;
342 	xyc->vector  = ca->ca_intvec;
343 	xyc->no_ols = 0; /* XXX should be from config */
344 
345 	for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
346 		xyc->sc_drives[lcv] = (struct xy_softc *) 0;
347 
348 	/*
349 	 * allocate and zero buffers
350 	 * check boundaries of the KVA's ... all IOPBs must reside in
351  	 * the same 64K region.
352 	 */
353 
354 	pbsz = XYC_MAXIOPB * sizeof(struct xy_iopb);
355 	tmp = tmp2 = (struct xy_iopb *) dvma_malloc(pbsz);	/* KVA */
356 	ultmp = (u_long) tmp;
357 	if ((ultmp & 0xffff0000) != ((ultmp + pbsz) & 0xffff0000)) {
358 		tmp = (struct xy_iopb *) dvma_malloc(pbsz); /* retry! */
359 		dvma_free(tmp2, pbsz);
360 		ultmp = (u_long) tmp;
361 		if ((ultmp & 0xffff0000) != ((ultmp + pbsz) & 0xffff0000)) {
362 			printf("%s: can't alloc IOPB mem in 64K\n",
363 				xyc->sc_dev.dv_xname);
364 			return;
365 		}
366 	}
367 	memset(tmp, 0, pbsz);
368 	xyc->iopbase = tmp;
369 	xyc->dvmaiopb = (struct xy_iopb *)
370 		dvma_kvtopa(xyc->iopbase, xyc->bustype);
371 	xyc->reqs = (struct xy_iorq *)
372 	    malloc(XYC_MAXIOPB * sizeof(struct xy_iorq), M_DEVBUF, M_NOWAIT);
373 	if (xyc->reqs == NULL)
374 		panic("xyc malloc");
375 	memset(xyc->reqs, 0, XYC_MAXIOPB * sizeof(struct xy_iorq));
376 
377 	/*
378 	 * init iorq to iopb pointers, and non-zero fields in the
379 	 * iopb which never change.
380 	 */
381 
382 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
383 		xyc->xy_chain[lcv] = NULL;
384 		xyc->reqs[lcv].iopb = &xyc->iopbase[lcv];
385 		xyc->iopbase[lcv].asr = 1;	/* always the same */
386 		xyc->iopbase[lcv].eef = 1;	/* always the same */
387 		xyc->iopbase[lcv].ecm = XY_ECM;	/* always the same */
388 		xyc->iopbase[lcv].aud = 1;	/* always the same */
389 		xyc->iopbase[lcv].relo = 1;	/* always the same */
390 		xyc->iopbase[lcv].thro = XY_THRO;/* always the same */
391 	}
392 	xyc->ciorq = &xyc->reqs[XYC_CTLIOPB];    /* short hand name */
393 	xyc->ciopb = &xyc->iopbase[XYC_CTLIOPB]; /* short hand name */
394 	xyc->xy_hand = 0;
395 
396 	/* read controller parameters and insure we have a 450/451 */
397 
398 	err = xyc_cmd(xyc, XYCMD_ST, 0, 0, 0, 0, 0, XY_SUB_POLL);
399 	res = xyc->ciopb->ctyp;
400 	XYC_DONE(xyc, err);
401 	if (res != XYCT_450) {
402 		if (err)
403 			printf(": %s: ", xyc_e2str(err));
404 		printf(": doesn't identify as a 450/451\n");
405 		return;
406 	}
407 	printf(": Xylogics 450/451");
408 	if (xyc->no_ols)
409 		printf(" [OLS disabled]"); /* 450 doesn't overlap seek right */
410 	printf("\n");
411 	if (err) {
412 		printf("%s: error: %s\n", xyc->sc_dev.dv_xname,
413 				xyc_e2str(err));
414 		return;
415 	}
416 	if ((xyc->xyc->xyc_csr & XYC_ADRM) == 0) {
417 		printf("%s: 24 bit addressing turned off\n",
418 			   xyc->sc_dev.dv_xname);
419 		printf("please set hardware jumpers JM1-JM2=in, JM3-JM4=out\n");
420 		printf("to enable 24 bit mode and this driver\n");
421 		return;
422 	}
423 
424 	/* link in interrupt with higher level software */
425 	isr_add_vectored(xycintr, (void *)xyc,
426 	                 ca->ca_intpri, ca->ca_intvec);
427 	evcnt_attach_dynamic(&xyc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
428 	    xyc->sc_dev.dv_xname, "intr");
429 
430 	callout_init(&xyc->sc_tick_ch);
431 
432 	/* now we must look for disks using autoconfig */
433 	for (xa.driveno = 0; xa.driveno < XYC_MAXDEV; xa.driveno++)
434 		(void) config_found(self, (void *) &xa, xyc_print);
435 
436 	/* start the watchdog clock */
437 	callout_reset(&xyc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xyc);
438 }
439 
440 static int
441 xyc_print(aux, name)
442 	void *aux;
443 	const char *name;
444 {
445 	struct xyc_attach_args *xa = aux;
446 
447 	if (name != NULL)
448 		printf("%s: ", name);
449 
450 	if (xa->driveno != -1)
451 		printf(" drive %d", xa->driveno);
452 
453 	return UNCONF;
454 }
455 
456 /*
457  * xymatch: probe for disk.
458  *
459  * note: we almost always say disk is present.   this allows us to
460  * spin up and configure a disk after the system is booted (we can
461  * call xyattach!).  Also, wire down the relationship between the
462  * xy* and xyc* devices, to simplify boot device identification.
463  */
464 static int
465 xymatch(parent, cf, aux)
466 	struct device *parent;
467 	struct cfdata *cf;
468 	void *aux;
469 {
470 	struct xyc_attach_args *xa = aux;
471 	int xy_unit;
472 
473 	/* Match only on the "wired-down" controller+disk. */
474 	xy_unit = parent->dv_unit * 2 + xa->driveno;
475 	if (cf->cf_unit != xy_unit)
476 		return (0);
477 
478 	return (1);
479 }
480 
481 /*
482  * xyattach: attach a disk.
483  */
484 static void
485 xyattach(parent, self, aux)
486 	struct device *parent, *self;
487 	void   *aux;
488 {
489 	struct xy_softc *xy = (void *) self;
490 	struct xyc_softc *xyc = (void *) parent;
491 	struct xyc_attach_args *xa = aux;
492 
493 	printf("\n");
494 
495 	/*
496 	 * Always re-initialize the disk structure.  We want statistics
497 	 * to start with a clean slate.
498 	 */
499 	memset(&xy->sc_dk, 0, sizeof(xy->sc_dk));
500 	xy->sc_dk.dk_driver = &xydkdriver;
501 	xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
502 
503 	xy->state = XY_DRIVE_UNKNOWN;	/* to start */
504 	xy->flags = 0;
505 	xy->parent = xyc;
506 
507 	/* init queue of waiting bufs */
508 	BUFQ_INIT(&xy->xyq);
509 	xy->xyrq = &xyc->reqs[xa->driveno];
510 
511 	xy->xy_drive = xa->driveno;
512 	xyc->sc_drives[xa->driveno] = xy;
513 
514 	/* Do init work common to attach and open. */
515 	xy_init(xy);
516 }
517 
518 /*
519  * end of autoconfig functions
520  */
521 
522 /*
523  * Initialize a disk.  This can be called from both autoconf and
524  * also from xyopen/xystrategy.
525  */
526 static void
527 xy_init(xy)
528 	struct xy_softc *xy;
529 {
530 	struct xyc_softc *xyc;
531 	struct dkbad *dkb;
532 	void *dvmabuf;
533 	int err, spt, mb, blk, lcv, fullmode, newstate;
534 
535 	xyc = xy->parent;
536 	xy->state = XY_DRIVE_ATTACHING;
537 	newstate = XY_DRIVE_UNKNOWN;
538 	fullmode = (cold) ? XY_SUB_POLL : XY_SUB_WAIT;
539 	dvmabuf  = dvma_malloc(XYFM_BPS);
540 
541 	/* first try and reset the drive */
542 
543 	err = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fullmode);
544 	XYC_DONE(xyc, err);
545 	if (err == XY_ERR_DNRY) {
546 		printf("%s: drive %d: off-line\n",
547 			   xy->sc_dev.dv_xname, xy->xy_drive);
548 		goto done;
549 	}
550 	if (err) {
551 		printf("%s: ERROR 0x%02x (%s)\n",
552 			   xy->sc_dev.dv_xname, err, xyc_e2str(err));
553 		goto done;
554 	}
555 	printf("%s: drive %d ready",
556 		   xy->sc_dev.dv_xname, xy->xy_drive);
557 
558 	/*
559 	 * now set drive parameters (to semi-bogus values) so we can read the
560 	 * disk label.
561 	 */
562 	xy->pcyl = xy->ncyl = 1;
563 	xy->acyl = 0;
564 	xy->nhead = 1;
565 	xy->nsect = 1;
566 	xy->sectpercyl = 1;
567 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
568 		xy->dkb.bt_bad[lcv].bt_cyl =
569 			xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
570 
571 	/* read disk label */
572 	for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
573 						xy->drive_type++) {
574 		err = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
575 						dvmabuf, fullmode);
576 		XYC_DONE(xyc, err);
577 		if (err == XY_ERR_AOK) break;
578 	}
579 
580 	if (err != XY_ERR_AOK) {
581 		printf("%s: reading disk label failed: %s\n",
582 			xy->sc_dev.dv_xname, xyc_e2str(err));
583 		goto done;
584 	}
585 	printf("%s: drive type %d\n",
586 		   xy->sc_dev.dv_xname, xy->drive_type);
587 
588 	newstate = XY_DRIVE_NOLABEL;
589 
590 	xy->hw_spt = spt = 0; /* XXX needed ? */
591 	/* Attach the disk: must be before getdisklabel to malloc label */
592 	disk_attach(&xy->sc_dk);
593 
594 	if (xygetdisklabel(xy, dvmabuf) != XY_ERR_AOK)
595 		goto done;
596 
597 	/* inform the user of what is up */
598 	printf("%s: <%s>, pcyl %d\n",
599 		   xy->sc_dev.dv_xname,
600 		   (char *)dvmabuf, xy->pcyl);
601 	mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
602 	printf("%s: %dMB, %d cyl, %d head, %d sec\n",
603 		xy->sc_dev.dv_xname, mb,
604 		xy->ncyl, xy->nhead, xy->nsect);
605 
606 	/*
607 	 * 450/451 stupidity: the drive type is encoded into the format
608 	 * of the disk.   the drive type in the IOPB must match the drive
609 	 * type in the format, or you will not be able to do I/O to the
610 	 * disk (you get header not found errors).  if you have two drives
611 	 * of different sizes that have the same drive type in their
612 	 * formatting then you are out of luck.
613 	 *
614 	 * this problem was corrected in the 753/7053.
615 	 */
616 
617 	for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
618 		struct xy_softc *oxy;
619 
620 		oxy = xyc->sc_drives[lcv];
621 		if (oxy == NULL || oxy == xy) continue;
622 		if (oxy->drive_type != xy->drive_type) continue;
623 		if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
624 			xy->nhead != oxy->nhead) {
625 			printf("%s: %s and %s must be the same size!\n",
626 				xyc->sc_dev.dv_xname,
627 				xy ->sc_dev.dv_xname,
628 				oxy->sc_dev.dv_xname);
629 			panic("xy drive size mismatch");
630 		}
631 	}
632 
633 
634 	/* now set the real drive parameters! */
635 	blk = (xy->nsect - 1) +
636 		((xy->nhead - 1) * xy->nsect) +
637 		((xy->pcyl - 1) * xy->nsect * xy->nhead);
638 	err = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fullmode);
639 	XYC_DONE(xyc, err);
640 	if (err) {
641 		printf("%s: write drive size failed: %s\n",
642 			xy->sc_dev.dv_xname, xyc_e2str(err));
643 		goto done;
644 	}
645 	newstate = XY_DRIVE_ONLINE;
646 
647 	/*
648 	 * read bad144 table. this table resides on the first sector of the
649 	 * last track of the disk (i.e. second cyl of "acyl" area).
650 	 */
651 	blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
652 								/* last cyl */
653 	    (xy->nhead - 1) * xy->nsect;	/* last head */
654 	err = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
655 						dvmabuf, fullmode);
656 	XYC_DONE(xyc, err);
657 	if (err) {
658 		printf("%s: reading bad144 failed: %s\n",
659 			xy->sc_dev.dv_xname, xyc_e2str(err));
660 		goto done;
661 	}
662 
663 	/* check dkbad for sanity */
664 	dkb = (struct dkbad *) dvmabuf;
665 	for (lcv = 0; lcv < 126; lcv++) {
666 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
667 				dkb->bt_bad[lcv].bt_cyl == 0) &&
668 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
669 			continue;	/* blank */
670 		if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
671 			break;
672 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
673 			break;
674 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
675 			break;
676 	}
677 	if (lcv != 126) {
678 		printf("%s: warning: invalid bad144 sector!\n",
679 			xy->sc_dev.dv_xname);
680 	} else {
681 		memcpy(&xy->dkb, dvmabuf, XYFM_BPS);
682 	}
683 
684 done:
685 	xy->state = newstate;
686 	dvma_free(dvmabuf, XYFM_BPS);
687 }
688 
689 /*
690  * { b , c } d e v s w   f u n c t i o n s
691  */
692 
693 /*
694  * xyclose: close device
695  */
696 int
697 xyclose(dev, flag, fmt, p)
698 	dev_t   dev;
699 	int     flag, fmt;
700 	struct proc *p;
701 {
702 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
703 	int     part = DISKPART(dev);
704 
705 	/* clear mask bits */
706 
707 	switch (fmt) {
708 	case S_IFCHR:
709 		xy->sc_dk.dk_copenmask &= ~(1 << part);
710 		break;
711 	case S_IFBLK:
712 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
713 		break;
714 	}
715 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
716 
717 	return 0;
718 }
719 
720 /*
721  * xydump: crash dump system
722  */
723 int
724 xydump(dev, blkno, va, sz)
725 	dev_t dev;
726 	daddr_t blkno;
727 	caddr_t va;
728 	size_t sz;
729 {
730 	int     unit, part;
731 	struct xy_softc *xy;
732 
733 	unit = DISKUNIT(dev);
734 	if (unit >= xy_cd.cd_ndevs)
735 		return ENXIO;
736 	part = DISKPART(dev);
737 
738 	xy = xy_cd.cd_devs[unit];
739 
740 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
741 	    'a' + part);
742 
743 	return ENXIO;
744 
745 	/* outline: globals: "dumplo" == sector number of partition to start
746 	 * dump at (convert to physical sector with partition table)
747 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
748 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
749 	 * physmem)
750 	 *
751 	 * dump a copy of physical memory to the dump device starting at sector
752 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
753 	 * we go.   use polled I/O.
754 	 *
755 	 * XXX how to handle NON_CONTIG?
756 	 */
757 }
758 
759 /*
760  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
761  */
762 int
763 xyioctl(dev, command, addr, flag, p)
764 	dev_t   dev;
765 	u_long  command;
766 	caddr_t addr;
767 	int     flag;
768 	struct proc *p;
769 
770 {
771 	struct xy_softc *xy;
772 	struct xd_iocmd *xio;
773 	int     error, s, unit;
774 
775 	unit = DISKUNIT(dev);
776 
777 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
778 		return (ENXIO);
779 
780 	/* switch on ioctl type */
781 
782 	switch (command) {
783 	case DIOCSBAD:		/* set bad144 info */
784 		if ((flag & FWRITE) == 0)
785 			return EBADF;
786 		s = splbio();
787 		memcpy(&xy->dkb, addr, sizeof(xy->dkb));
788 		splx(s);
789 		return 0;
790 
791 	case DIOCGDINFO:	/* get disk label */
792 		memcpy(addr, xy->sc_dk.dk_label, sizeof(struct disklabel));
793 		return 0;
794 
795 	case DIOCGPART:	/* get partition info */
796 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
797 		((struct partinfo *) addr)->part =
798 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
799 		return 0;
800 
801 	case DIOCSDINFO:	/* set disk label */
802 		if ((flag & FWRITE) == 0)
803 			return EBADF;
804 		error = setdisklabel(xy->sc_dk.dk_label,
805 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
806 		    xy->sc_dk.dk_cpulabel);
807 		if (error == 0) {
808 			if (xy->state == XY_DRIVE_NOLABEL)
809 				xy->state = XY_DRIVE_ONLINE;
810 		}
811 		return error;
812 
813 	case DIOCWLABEL:	/* change write status of disk label */
814 		if ((flag & FWRITE) == 0)
815 			return EBADF;
816 		if (*(int *) addr)
817 			xy->flags |= XY_WLABEL;
818 		else
819 			xy->flags &= ~XY_WLABEL;
820 		return 0;
821 
822 	case DIOCWDINFO:	/* write disk label */
823 		if ((flag & FWRITE) == 0)
824 			return EBADF;
825 		error = setdisklabel(xy->sc_dk.dk_label,
826 		    (struct disklabel *) addr, /* xy->sc_dk.dk_openmask : */ 0,
827 		    xy->sc_dk.dk_cpulabel);
828 		if (error == 0) {
829 			if (xy->state == XY_DRIVE_NOLABEL)
830 				xy->state = XY_DRIVE_ONLINE;
831 
832 			/* Simulate opening partition 0 so write succeeds. */
833 			xy->sc_dk.dk_openmask |= (1 << 0);
834 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
835 			    xystrategy, xy->sc_dk.dk_label,
836 			    xy->sc_dk.dk_cpulabel);
837 			xy->sc_dk.dk_openmask =
838 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
839 		}
840 		return error;
841 
842 	case DIOSXDCMD:
843 		xio = (struct xd_iocmd *) addr;
844 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
845 			return (error);
846 		return (xyc_ioctlcmd(xy, dev, xio));
847 
848 	default:
849 		return ENOTTY;
850 	}
851 }
852 
853 /*
854  * xyopen: open drive
855  */
856 int
857 xyopen(dev, flag, fmt, p)
858 	dev_t   dev;
859 	int     flag, fmt;
860 	struct proc *p;
861 {
862 	int err, unit, part, s;
863 	struct xy_softc *xy;
864 
865 	/* first, could it be a valid target? */
866 	unit = DISKUNIT(dev);
867 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
868 		return (ENXIO);
869 	part = DISKPART(dev);
870 	err = 0;
871 
872 	/*
873 	 * If some other processing is doing init, sleep.
874 	 */
875 	s = splbio();
876 	while (xy->state == XY_DRIVE_ATTACHING) {
877 		if (tsleep(&xy->state, PRIBIO, "xyopen", 0)) {
878 			err = EINTR;
879 			goto done;
880 		}
881 	}
882 	/* Do we need to init the drive? */
883 	if (xy->state == XY_DRIVE_UNKNOWN) {
884 		xy_init(xy);
885 		wakeup(&xy->state);
886 	}
887 	/* Was the init successful? */
888 	if (xy->state == XY_DRIVE_UNKNOWN) {
889 		err = EIO;
890 		goto done;
891 	}
892 
893 	/* check for partition */
894 	if (part != RAW_PART &&
895 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
896 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
897 		err = ENXIO;
898 		goto done;
899 	}
900 
901 	/* set open masks */
902 	switch (fmt) {
903 	case S_IFCHR:
904 		xy->sc_dk.dk_copenmask |= (1 << part);
905 		break;
906 	case S_IFBLK:
907 		xy->sc_dk.dk_bopenmask |= (1 << part);
908 		break;
909 	}
910 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
911 
912 done:
913 	splx(s);
914 	return (err);
915 }
916 
917 int
918 xyread(dev, uio, flags)
919 	dev_t   dev;
920 	struct uio *uio;
921 	int flags;
922 {
923 
924 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
925 }
926 
927 int
928 xywrite(dev, uio, flags)
929 	dev_t   dev;
930 	struct uio *uio;
931 	int flags;
932 {
933 
934 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
935 }
936 
937 
938 /*
939  * xysize: return size of a partition for a dump
940  */
941 
942 int
943 xysize(dev)
944 	dev_t   dev;
945 
946 {
947 	struct xy_softc *xysc;
948 	int     unit, part, size, omask;
949 
950 	/* valid unit? */
951 	unit = DISKUNIT(dev);
952 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
953 		return (-1);
954 
955 	part = DISKPART(dev);
956 	omask = xysc->sc_dk.dk_openmask & (1 << part);
957 
958 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
959 		return (-1);
960 
961 	/* do it */
962 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
963 		size = -1;	/* only give valid size for swap partitions */
964 	else
965 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
966 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
967 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
968 		return (-1);
969 	return (size);
970 }
971 
972 /*
973  * xystrategy: buffering system interface to xy.
974  */
975 void
976 xystrategy(bp)
977 	struct buf *bp;
978 
979 {
980 	struct xy_softc *xy;
981 	int     s, unit;
982 	struct disklabel *lp;
983 	daddr_t blkno;
984 
985 	unit = DISKUNIT(bp->b_dev);
986 
987 	/* check for live device */
988 
989 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
990 	    bp->b_blkno < 0 ||
991 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
992 		bp->b_error = EINVAL;
993 		goto bad;
994 	}
995 
996 	/* There should always be an open first. */
997 	if (xy->state == XY_DRIVE_UNKNOWN) {
998 		bp->b_error = EIO;
999 		goto bad;
1000 	}
1001 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1002 		/* no I/O to unlabeled disks, unless raw partition */
1003 		bp->b_error = EIO;
1004 		goto bad;
1005 	}
1006 	/* short circuit zero length request */
1007 
1008 	if (bp->b_bcount == 0)
1009 		goto done;
1010 
1011 	/* check bounds with label (disksubr.c).  Determine the size of the
1012 	 * transfer, and make sure it is within the boundaries of the
1013 	 * partition. Adjust transfer if needed, and signal errors or early
1014 	 * completion. */
1015 
1016 	lp = xy->sc_dk.dk_label;
1017 
1018 	if (bounds_check_with_label(bp, lp,
1019 		(xy->flags & XY_WLABEL) != 0) <= 0)
1020 		goto done;
1021 
1022 	/*
1023 	 * Now convert the block number to absolute and put it in
1024 	 * terms of the device's logical block size.
1025 	 */
1026 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1027 	if (DISKPART(bp->b_dev) != RAW_PART)
1028 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
1029 
1030 	bp->b_rawblkno = blkno;
1031 
1032 	/*
1033 	 * now we know we have a valid buf structure that we need to do I/O
1034 	 * on.
1035 	 */
1036 
1037 	s = splbio();		/* protect the queues */
1038 
1039 	disksort_blkno(&xy->xyq, bp);	 /* XXX disksort_cylinder */
1040 
1041 	/* start 'em up */
1042 
1043 	xyc_start(xy->parent, NULL);
1044 
1045 	/* done! */
1046 
1047 	splx(s);
1048 	return;
1049 
1050 bad:				/* tells upper layers we have an error */
1051 	bp->b_flags |= B_ERROR;
1052 done:				/* tells upper layers we are done with this
1053 				 * buf */
1054 	bp->b_resid = bp->b_bcount;
1055 	biodone(bp);
1056 }
1057 /*
1058  * end of {b,c}devsw functions
1059  */
1060 
1061 /*
1062  * i n t e r r u p t   f u n c t i o n
1063  *
1064  * xycintr: hardware interrupt.
1065  */
1066 int
1067 xycintr(v)
1068 	void   *v;
1069 
1070 {
1071 	struct xyc_softc *xycsc = v;
1072 
1073 	/* kick the event counter */
1074 	xycsc->sc_intrcnt.ev_count++;
1075 
1076 	/* remove as many done IOPBs as possible */
1077 	xyc_remove_iorq(xycsc);
1078 
1079 	/* start any iorq's already waiting */
1080 	xyc_start(xycsc, NULL);
1081 
1082 	return (1);
1083 }
1084 /*
1085  * end of interrupt function
1086  */
1087 
1088 /*
1089  * i n t e r n a l   f u n c t i o n s
1090  */
1091 
1092 /*
1093  * xyc_rqinit: fill out the fields of an I/O request
1094  */
1095 
1096 inline void
1097 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
1098 	struct xy_iorq *rq;
1099 	struct xyc_softc *xyc;
1100 	struct xy_softc *xy;
1101 	int     md;
1102 	u_long  blk;
1103 	int     cnt;
1104 	caddr_t db;
1105 	struct buf *bp;
1106 {
1107 	rq->xyc = xyc;
1108 	rq->xy = xy;
1109 	rq->ttl = XYC_MAXTTL + 10;
1110 	rq->mode = md;
1111 	rq->tries = rq->errno = rq->lasterror = 0;
1112 	rq->blockno = blk;
1113 	rq->sectcnt = cnt;
1114 	rq->dbuf = rq->dbufbase = db;
1115 	rq->buf = bp;
1116 }
1117 
1118 /*
1119  * xyc_rqtopb: load up an IOPB based on an iorq
1120  */
1121 
1122 void
1123 xyc_rqtopb(iorq, iopb, cmd, subfun)
1124 	struct xy_iorq *iorq;
1125 	struct xy_iopb *iopb;
1126 	int     cmd, subfun;
1127 
1128 {
1129 	u_long  block, dp;
1130 
1131 	/* normal IOPB case, standard stuff */
1132 
1133 	/* chain bit handled later */
1134 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
1135 	iopb->com = cmd;
1136 	iopb->errno = 0;
1137 	iopb->errs = 0;
1138 	iopb->done = 0;
1139 	if (iorq->xy) {
1140 		iopb->unit = iorq->xy->xy_drive;
1141 		iopb->dt = iorq->xy->drive_type;
1142 	} else {
1143 		iopb->unit = 0;
1144 		iopb->dt = 0;
1145 	}
1146 	block = iorq->blockno;
1147 	if (iorq->xy == NULL || block == 0) {
1148 		iopb->sect = iopb->head = iopb->cyl = 0;
1149 	} else {
1150 		iopb->sect = block % iorq->xy->nsect;
1151 		block = block / iorq->xy->nsect;
1152 		iopb->head = block % iorq->xy->nhead;
1153 		block = block / iorq->xy->nhead;
1154 		iopb->cyl = block;
1155 	}
1156 	iopb->scnt = iorq->sectcnt;
1157 	if (iorq->dbuf == NULL) {
1158 		iopb->dataa = 0;
1159 		iopb->datar = 0;
1160 	} else {
1161 		dp = dvma_kvtopa(iorq->dbuf, iorq->xyc->bustype);
1162 		iopb->dataa = (dp & 0xffff);
1163 		iopb->datar = ((dp & 0xff0000) >> 16);
1164 	}
1165 	iopb->subfn = subfun;
1166 }
1167 
1168 
1169 /*
1170  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
1171  */
1172 
1173 int
1174 xyc_unbusy(xyc, del)
1175 
1176 struct xyc *xyc;
1177 int del;
1178 
1179 {
1180 	while (del-- > 0) {
1181 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
1182 			break;
1183 		DELAY(1);
1184 	}
1185 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
1186 }
1187 
1188 /*
1189  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
1190  * note that NORM requests are handled separately.
1191  */
1192 int
1193 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1194 	struct xyc_softc *xycsc;
1195 	int     cmd, subfn, unit, block, scnt;
1196 	char   *dptr;
1197 	int     fullmode;
1198 {
1199 	struct xy_iorq *iorq = xycsc->ciorq;
1200 	struct xy_iopb *iopb = xycsc->ciopb;
1201 	int submode = XY_STATE(fullmode);
1202 
1203 	/*
1204 	 * is someone else using the control iopq wait for it if we can
1205 	 */
1206 start:
1207 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
1208 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
1209                                 return(XY_ERR_FAIL);
1210 		goto start;
1211 	}
1212 
1213 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
1214 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
1215 		iorq->mode = XY_SUB_FREE;
1216 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
1217 	}
1218 
1219 	/* init iorq/iopb */
1220 
1221 	xyc_rqinit(iorq, xycsc,
1222 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
1223 	    fullmode, block, scnt, dptr, NULL);
1224 
1225 	/* load IOPB from iorq */
1226 
1227 	xyc_rqtopb(iorq, iopb, cmd, subfn);
1228 
1229 	/* submit it for processing */
1230 
1231 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
1232 
1233 	return(XY_ERR_AOK);
1234 }
1235 
1236 /*
1237  * xyc_startbuf
1238  * start a buffer for running
1239  */
1240 
1241 int
1242 xyc_startbuf(xycsc, xysc, bp)
1243 	struct xyc_softc *xycsc;
1244 	struct xy_softc *xysc;
1245 	struct buf *bp;
1246 
1247 {
1248 	int     partno;
1249 	struct xy_iorq *iorq;
1250 	struct xy_iopb *iopb;
1251 	u_long  block;
1252 	caddr_t dbuf;
1253 
1254 	iorq = xysc->xyrq;
1255 	iopb = iorq->iopb;
1256 
1257 	/* get buf */
1258 
1259 	if (bp == NULL)
1260 		panic("xyc_startbuf null buf");
1261 
1262 	partno = DISKPART(bp->b_dev);
1263 #ifdef XYC_DEBUG
1264 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
1265 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1266 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1267 	    bp->b_bcount, bp->b_data);
1268 #endif
1269 
1270 	/*
1271 	 * load request.
1272 	 *
1273 	 * also, note that there are two kinds of buf structures, those with
1274 	 * B_PHYS set and those without B_PHYS.   if B_PHYS is set, then it is
1275 	 * a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
1276 	 * buffer which has already been mapped into DVMA space. (Not on sun3)
1277 	 * However, if B_PHYS is not set, then the buffer is a normal system
1278 	 * buffer which does *not* live in DVMA space.  In that case we call
1279 	 * dvma_mapin to map it into DVMA space so we can do the DMA to it.
1280 	 *
1281 	 * in cases where we do a dvma_mapin, note that iorq points to the buffer
1282 	 * as mapped into DVMA space, where as the bp->b_data points to its
1283 	 * non-DVMA mapping.
1284 	 *
1285 	 * XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
1286 	 * into dvma space, only that it was remapped into the kernel.
1287 	 * We ALWAYS have to remap the kernel buf into DVMA space.
1288 	 * (It is done inexpensively, using whole segments!)
1289 	 */
1290 
1291 	block = bp->b_rawblkno;
1292 
1293 	dbuf = dvma_mapin(bp->b_data, bp->b_bcount, 0);
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 				(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
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(iopb, xycsc->bustype);
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 			(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
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 	struct xyc_softc *xycsc;
1429 	struct xy_iorq *iorq;
1430 {
1431 	int togo, chain, hand;
1432 	struct xy_iopb *iopb, *prev_iopb;
1433 	memset(xycsc->xy_chain, 0, sizeof(xycsc->xy_chain));
1434 
1435 	/*
1436 	 * promote control IOPB to the top
1437 	 */
1438 	if (iorq == NULL) {
1439 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1440 			 XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1441 			xycsc->iopbase[XYC_CTLIOPB].done == 0)
1442 		  iorq = &xycsc->reqs[XYC_CTLIOPB];
1443 	}
1444 	/*
1445 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
1446 	 * we let these take priority and do them first.
1447 	 */
1448 	if (iorq) {
1449 		xycsc->xy_chain[0] = iorq;
1450 		iorq->iopb->chen = 0;
1451 		return(iorq->iopb);
1452 	}
1453 
1454 	/*
1455 	 * NORM case: do round robin and maybe chain (if allowed and possible)
1456 	 */
1457 
1458 	chain = 0;
1459 	hand = xycsc->xy_hand;
1460 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1461 
1462 	for (togo = XYC_MAXIOPB ;
1463 		 togo > 0 ;
1464 		 togo--, hand = (hand + 1) % XYC_MAXIOPB)
1465 	{
1466 
1467 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1468 			xycsc->iopbase[hand].done)
1469 			continue;   /* not ready-for-i/o */
1470 
1471 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1472 		iopb = xycsc->xy_chain[chain]->iopb;
1473 		iopb->chen = 0;
1474 		if (chain != 0) {   /* adding a link to a chain? */
1475 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1476 			prev_iopb->chen = 1;
1477 			prev_iopb->nxtiopb = 0xffff &
1478 			  dvma_kvtopa(iopb, xycsc->bustype);
1479 		} else {            /* head of chain */
1480 			iorq = xycsc->xy_chain[chain];
1481 		}
1482 		chain++;
1483 		if (xycsc->no_ols) break;   /* quit if chaining dis-allowed */
1484 	}
1485 	return(iorq ? iorq->iopb : NULL);
1486 }
1487 
1488 /*
1489  * xyc_piodriver
1490  *
1491  * programmed i/o driver.   this function takes over the computer
1492  * and drains off the polled i/o request.   it returns the status of the iorq
1493  * the caller is interesting in.
1494  */
1495 int
1496 xyc_piodriver(xycsc, iorq)
1497 	struct xyc_softc *xycsc;
1498 	struct xy_iorq  *iorq;
1499 
1500 {
1501 	int     nreset = 0;
1502 	int     retval = 0;
1503 	u_long  res;
1504 
1505 #ifdef XYC_DEBUG
1506 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
1507 #endif
1508 
1509 	while (iorq->iopb->done == 0) {
1510 
1511 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1512 
1513 		/* we expect some progress soon */
1514 		if (res == XY_ERR_FAIL && nreset >= 2) {
1515 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1516 #ifdef XYC_DEBUG
1517 			printf("xyc_piodriver: timeout\n");
1518 #endif
1519 			return (XY_ERR_FAIL);
1520 		}
1521 		if (res == XY_ERR_FAIL) {
1522 			if (xyc_reset(xycsc, 0,
1523 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
1524 				      XY_ERR_FAIL,
1525 				      0) == XY_ERR_FAIL)
1526 				return (XY_ERR_FAIL);	/* flushes all but POLL
1527 							 * requests, resets */
1528 			continue;
1529 		}
1530 
1531 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
1532 
1533 		if (iorq->iopb->done == 0)
1534 			xyc_start(xycsc, iorq);
1535 	}
1536 
1537 	/* get return value */
1538 
1539 	retval = iorq->errno;
1540 
1541 #ifdef XYC_DEBUG
1542 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1543 	    iorq->errno, xyc_e2str(iorq->errno));
1544 #endif
1545 
1546 	/* start up any bufs that have queued */
1547 
1548 	xyc_start(xycsc, NULL);
1549 
1550 	return (retval);
1551 }
1552 
1553 /*
1554  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
1555  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1556  */
1557 void
1558 xyc_xyreset(xycsc, xysc)
1559 	struct xyc_softc *xycsc;
1560 	struct xy_softc *xysc;
1561 
1562 {
1563 	struct xy_iopb tmpiopb;
1564 	u_long  addr;
1565 	int     del;
1566 	memcpy(&tmpiopb, xycsc->ciopb, sizeof(tmpiopb));
1567 	xycsc->ciopb->chen = xycsc->ciopb->done = xycsc->ciopb->errs = 0;
1568 	xycsc->ciopb->ien = 0;
1569 	xycsc->ciopb->com = XYCMD_RST;
1570 	xycsc->ciopb->unit = xysc->xy_drive;
1571 	addr = dvma_kvtopa(xycsc->ciopb, xycsc->bustype);
1572 
1573 	XYC_GO(xycsc->xyc, addr);
1574 
1575 	del = XYC_RESETUSEC;
1576 	while (del > 0) {
1577 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0) break;
1578 		DELAY(1);
1579 		del--;
1580 	}
1581 
1582 	if (del <= 0 || xycsc->ciopb->errs) {
1583 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
1584 		    xyc_e2str(xycsc->ciopb->errno));
1585 		del = xycsc->xyc->xyc_rsetup;
1586 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1587 			panic("xyc_reset");
1588 	} else {
1589 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
1590 	}
1591 	memcpy(xycsc->ciopb, &tmpiopb, sizeof(tmpiopb));
1592 }
1593 
1594 
1595 /*
1596  * xyc_reset: reset everything: requests are marked as errors except
1597  * a polled request (which is resubmitted)
1598  */
1599 int
1600 xyc_reset(xycsc, quiet, blastmode, error, xysc)
1601 	struct xyc_softc *xycsc;
1602 	int     quiet, error;
1603 	struct xy_iorq *blastmode;
1604 	struct xy_softc *xysc;
1605 
1606 {
1607 	int     del = 0, lcv, retval = XY_ERR_AOK;
1608 	struct xy_iorq *iorq;
1609 
1610 	/* soft reset hardware */
1611 
1612 	if (!quiet)
1613 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
1614 	del = xycsc->xyc->xyc_rsetup;
1615 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1616 	if (del == XY_ERR_FAIL) {
1617 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
1618 		retval = XY_ERR_FAIL;
1619 	}
1620 	if (xysc)
1621 		xyc_xyreset(xycsc, xysc);
1622 
1623 	/* fix queues based on "blast-mode" */
1624 
1625 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1626 		iorq = &xycsc->reqs[lcv];
1627 
1628 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1629 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1630 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
1631 			/* is it active? */
1632 			continue;
1633 
1634 		if (blastmode == XY_RSET_ALL ||
1635 				blastmode != iorq) {
1636 			/* failed */
1637 			iorq->errno = error;
1638 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1639 			switch (XY_STATE(iorq->mode)) {
1640 			case XY_SUB_NORM:
1641 			    iorq->buf->b_error = EIO;
1642 			    iorq->buf->b_flags |= B_ERROR;
1643 			    iorq->buf->b_resid =
1644 			       iorq->sectcnt * XYFM_BPS;
1645 				/* Sun3: map/unmap regardless of B_PHYS */
1646 				dvma_mapout(iorq->dbufbase,
1647 				            iorq->buf->b_bcount);
1648 			    BUFQ_REMOVE(&iorq->xy->xyq, iorq->buf);
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 void
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 (BUFQ_FIRST(&xy->xyq) == NULL) continue;
1695 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
1696 			xyc_startbuf(xycsc, xy, BUFQ_FIRST(&xy->xyq));
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(iorq->dbuf, xycsc->bustype);
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 			BUFQ_REMOVE(&iorq->xy->xyq, bp);
1827 			disk_unbusy(&iorq->xy->sc_dk,
1828 			    (bp->b_bcount - bp->b_resid));
1829 			iorq->mode = XY_SUB_FREE;
1830 			biodone(bp);
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 	callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
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;
2000 	void * 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 			err = copyin(xio->dptr, dvmabuf, xio->dlen);
2035 			if (err) {
2036 				dvma_free(dvmabuf, xio->dlen);
2037 				return (err);
2038 			}
2039 		}
2040 	}
2041 	/* do it! */
2042 
2043 	err = 0;
2044 	xycsc = xy->parent;
2045 	s = splbio();
2046 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2047 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2048 	if (rqno == XY_ERR_FAIL) {
2049 		err = EIO;
2050 		goto done;
2051 	}
2052 	xio->errno = xycsc->ciorq->errno;
2053 	xio->tries = xycsc->ciorq->tries;
2054 	XYC_DONE(xycsc, err);
2055 
2056 	if (xio->cmd == XYCMD_RD)
2057 		err = copyout(dvmabuf, xio->dptr, xio->dlen);
2058 
2059 done:
2060 	splx(s);
2061 	if (dvmabuf)
2062 		dvma_free(dvmabuf, xio->dlen);
2063 	return (err);
2064 }
2065 
2066 /*
2067  * xyc_e2str: convert error code number into an error string
2068  */
2069 char *
2070 xyc_e2str(no)
2071 	int     no;
2072 {
2073 	switch (no) {
2074 	case XY_ERR_FAIL:
2075 		return ("Software fatal error");
2076 	case XY_ERR_DERR:
2077 		return ("DOUBLE ERROR");
2078 	case XY_ERR_AOK:
2079 		return ("Successful completion");
2080 	case XY_ERR_IPEN:
2081 		return("Interrupt pending");
2082 	case XY_ERR_BCFL:
2083 		return("Busy conflict");
2084 	case XY_ERR_TIMO:
2085 		return("Operation timeout");
2086 	case XY_ERR_NHDR:
2087 		return("Header not found");
2088 	case XY_ERR_HARD:
2089 		return("Hard ECC error");
2090 	case XY_ERR_ICYL:
2091 		return("Illegal cylinder address");
2092 	case XY_ERR_ISEC:
2093 		return("Illegal sector address");
2094 	case XY_ERR_SMAL:
2095 		return("Last sector too small");
2096 	case XY_ERR_SACK:
2097 		return("Slave ACK error (non-existent memory)");
2098 	case XY_ERR_CHER:
2099 		return("Cylinder and head/header error");
2100 	case XY_ERR_SRTR:
2101 		return("Auto-seek retry successful");
2102 	case XY_ERR_WPRO:
2103 		return("Write-protect error");
2104 	case XY_ERR_UIMP:
2105 		return("Unimplemented command");
2106 	case XY_ERR_DNRY:
2107 		return("Drive not ready");
2108 	case XY_ERR_SZER:
2109 		return("Sector count zero");
2110 	case XY_ERR_DFLT:
2111 		return("Drive faulted");
2112 	case XY_ERR_ISSZ:
2113 		return("Illegal sector size");
2114 	case XY_ERR_SLTA:
2115 		return("Self test A");
2116 	case XY_ERR_SLTB:
2117 		return("Self test B");
2118 	case XY_ERR_SLTC:
2119 		return("Self test C");
2120 	case XY_ERR_SOFT:
2121 		return("Soft ECC error");
2122 	case XY_ERR_SFOK:
2123 		return("Soft ECC error recovered");
2124 	case XY_ERR_IHED:
2125 		return("Illegal head");
2126 	case XY_ERR_DSEQ:
2127 		return("Disk sequencer error");
2128 	case XY_ERR_SEEK:
2129 		return("Seek error");
2130 	default:
2131 		return ("Unknown error");
2132 	}
2133 }
2134 
2135 int
2136 xyc_entoact(errno)
2137 
2138 int errno;
2139 
2140 {
2141   switch (errno) {
2142     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
2143     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
2144     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
2145     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
2146     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
2147 
2148 	return(XY_ERA_PROG); /* program error ! */
2149 
2150     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
2151     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
2152     case XY_ERR_SOFT:
2153 
2154 	return(XY_ERA_HARD); /* hard error, retry */
2155 
2156     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
2157 
2158 	return(XY_ERA_RSET); /* hard error reset */
2159 
2160     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
2161 
2162 	return(XY_ERA_SOFT); /* an FYI error */
2163 
2164     case XY_ERR_WPRO:
2165 
2166 	return(XY_ERA_WPRO); /* write protect */
2167   }
2168 
2169   return(XY_ERA_PROG); /* ??? */
2170 }
2171