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