xref: /netbsd-src/sys/arch/sun3/dev/xd.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: xd.c,v 1.68 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 d . c   x y l o g i c s   7 5 3 / 7 0 5 3   v m e / s m d   d r i v e r
31  *
32  * author: Chuck Cranor <chuck@netbsd>
33  * id: &Id: xd.c,v 1.9 1995/09/25 20:12:44 chuck Exp &
34  * started: 27-Feb-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  *
40  * Special thanks go to Scott E. Campbell of Xylogics, Inc. for taking
41  * the time to answer some of my questions about the 753/7053.
42  *
43  * note: the 753 and the 7053 are programmed the same way, but are
44  * different sizes.   the 753 is a 6U VME card, while the 7053 is a 9U
45  * VME card (found in many VME based suns).
46  */
47 
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: xd.c,v 1.68 2011/02/01 20:19:32 chuck Exp $");
50 
51 #undef XDC_DEBUG		/* full debug */
52 #define XDC_DIAG		/* extra sanity checks */
53 #if defined(DIAGNOSTIC) && !defined(XDC_DIAG)
54 #define XDC_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 <machine/autoconf.h>
79 #include <machine/dvma.h>
80 
81 #include <dev/sun/disklabel.h>
82 
83 #include <sun3/dev/xdreg.h>
84 #include <sun3/dev/xdvar.h>
85 #include <sun3/dev/xio.h>
86 
87 #include "ioconf.h"
88 #include "locators.h"
89 
90 /*
91  * Print a complaint when no xd 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 "xd.h"
98 #if NXD == 0
99 #error "xdc but no xd?"
100 #endif
101 
102 /*
103  * macros
104  */
105 
106 /*
107  * XDC_TWAIT: add iorq "N" to tail of SC's wait queue
108  */
109 #define XDC_TWAIT(SC, N)						\
110 do {									\
111 	(SC)->waitq[(SC)->waitend] = (N);				\
112 	(SC)->waitend = ((SC)->waitend + 1) % XDC_MAXIOPB;		\
113 	(SC)->nwait++;							\
114 } while (/* CONSTCOND */ 0)
115 
116 /*
117  * XDC_HWAIT: add iorq "N" to head of SC's wait queue
118  */
119 #define XDC_HWAIT(SC, N)						\
120 do {									\
121 	(SC)->waithead = ((SC)->waithead == 0) ?			\
122 	    (XDC_MAXIOPB - 1) : ((SC)->waithead - 1);			\
123 	(SC)->waitq[(SC)->waithead] = (N);				\
124 	(SC)->nwait++;							\
125 } while (/* CONSTCOND */ 0)
126 
127 /*
128  * XDC_GET_WAITER: gets the first request waiting on the waitq
129  * and removes it (so it can be submitted)
130  */
131 #define XDC_GET_WAITER(XDCSC, RQ)					\
132 do {									\
133 	(RQ) = (XDCSC)->waitq[(XDCSC)->waithead];			\
134 	(XDCSC)->waithead = ((XDCSC)->waithead + 1) % XDC_MAXIOPB;	\
135 	xdcsc->nwait--;							\
136 } while (/* CONSTCOND */ 0)
137 
138 /*
139  * XDC_FREE: add iorq "N" to SC's free list
140  */
141 #define XDC_FREE(SC, N)							\
142 do {									\
143 	(SC)->freereq[(SC)->nfree++] = (N);				\
144 	(SC)->reqs[N].mode = 0;						\
145 	if ((SC)->nfree == 1)						\
146 		wakeup(&(SC)->nfree);					\
147 } while (/* CONSTCOND */ 0)
148 
149 
150 /*
151  * XDC_RQALLOC: allocate an iorq off the free list (assume nfree > 0).
152  */
153 #define XDC_RQALLOC(XDCSC) (XDCSC)->freereq[--((XDCSC)->nfree)]
154 
155 /*
156  * XDC_GO: start iopb ADDR (DVMA addr in a u_long) on XDC
157  */
158 #define XDC_GO(XDC, ADDR)						\
159 do {									\
160 	(XDC)->xdc_iopbaddr0 = ((ADDR) & 0xff);				\
161 	(ADDR) = ((ADDR) >> 8);						\
162 	(XDC)->xdc_iopbaddr1 = ((ADDR) & 0xff);				\
163 	(ADDR) = ((ADDR) >> 8);						\
164 	(XDC)->xdc_iopbaddr2 = ((ADDR) & 0xff);				\
165 	(ADDR) = ((ADDR) >> 8);						\
166 	(XDC)->xdc_iopbaddr3 = (ADDR);					\
167 	(XDC)->xdc_iopbamod = XDC_ADDRMOD;				\
168 	(XDC)->xdc_csr = XDC_ADDIOPB; /* go! */				\
169 } while (/* CONSTCOND */ 0)
170 
171 /*
172  * XDC_WAIT: wait for XDC's csr "BITS" to come on in "TIME".
173  *   LCV is a counter.  If it goes to zero then we timed out.
174  */
175 #define XDC_WAIT(XDC, LCV, TIME, BITS)					\
176 do {									\
177 	(LCV) = (TIME);							\
178 	while ((LCV) > 0) {						\
179 		if ((XDC)->xdc_csr & (BITS))				\
180 			break;						\
181 		(LCV) = (LCV) - 1;					\
182 		DELAY(1);						\
183 	}								\
184 } while (/* CONSTCOND */ 0)
185 
186 /*
187  * XDC_DONE: don't need IORQ, get error code and free (done after xdc_cmd)
188  */
189 #define XDC_DONE(SC,RQ,ER)						\
190 do {									\
191 	if ((RQ) == XD_ERR_FAIL) {					\
192 		(ER) = (RQ);						\
193 	} else {							\
194 		if ((SC)->ndone-- == XDC_SUBWAITLIM)			\
195 		wakeup(&(SC)->ndone);					\
196 		(ER) = (SC)->reqs[RQ].errno;				\
197 		XDC_FREE((SC), (RQ));					\
198 	}								\
199 } while (/* CONSTCOND */ 0)
200 
201 /*
202  * XDC_ADVANCE: advance iorq's pointers by a number of sectors
203  */
204 #define XDC_ADVANCE(IORQ, N)						\
205 do {									\
206 	if (N) {							\
207 		(IORQ)->sectcnt -= (N);					\
208 		(IORQ)->blockno += (N);					\
209 		(IORQ)->dbuf += ((N) * XDFM_BPS);			\
210 	}								\
211 } while (/* CONSTCOND */ 0)
212 
213 /*
214  * note - addresses you can sleep on:
215  *   [1] & of xd_softc's "state" (waiting for a chance to attach a drive)
216  *   [2] & of xdc_softc's "nfree" (waiting for a free iorq/iopb)
217  *   [3] & of xdc_softc's "ndone" (waiting for number of done iorq/iopb's
218  *                                 to drop below XDC_SUBWAITLIM)
219  *   [4] & an iorq (waiting for an XD_SUB_WAIT iorq to finish)
220  */
221 
222 
223 /*
224  * function prototypes
225  * "xdc_*" functions are internal, all others are external interfaces
226  */
227 
228 /* internals */
229 int	xdc_cmd(struct xdc_softc *, int, int, int, int, int, char *, int);
230 const char *xdc_e2str(int);
231 int	xdc_error(struct xdc_softc *, struct xd_iorq *, struct xd_iopb *, int,
232 	    int);
233 int	xdc_ioctlcmd(struct xd_softc *, dev_t dev, struct xd_iocmd *);
234 void	xdc_perror(struct xd_iorq *, struct xd_iopb *, int);
235 int	xdc_piodriver(struct xdc_softc *, int, int);
236 int	xdc_remove_iorq(struct xdc_softc *);
237 int	xdc_reset(struct xdc_softc *, int, int, int, struct xd_softc *);
238 inline void xdc_rqinit(struct xd_iorq *, struct xdc_softc *, struct xd_softc *,
239 	    int, u_long, int, void *, struct buf *);
240 void	xdc_rqtopb(struct xd_iorq *, struct xd_iopb *, int, int);
241 void	xdc_start(struct xdc_softc *, int);
242 int	xdc_startbuf(struct xdc_softc *, struct xd_softc *, struct buf *);
243 int	xdc_submit_iorq(struct xdc_softc *, int, int);
244 void	xdc_tick(void *);
245 void	xdc_xdreset(struct xdc_softc *, struct xd_softc *);
246 
247 /* machine interrupt hook */
248 int	xdcintr(void *);
249 
250 /* autoconf */
251 static int	xdcmatch(device_t, cfdata_t, void *);
252 static void	xdcattach(device_t, device_t, void *);
253 static int	xdc_print(void *, const char *);
254 
255 static int	xdmatch(device_t, cfdata_t, void *);
256 static void	xdattach(device_t, device_t, void *);
257 static void	xd_init(struct xd_softc *);
258 
259 static	void xddummystrat(struct buf *);
260 int	xdgetdisklabel(struct xd_softc *, void *);
261 
262 /*
263  * cfattach's: device driver interface to autoconfig
264  */
265 
266 CFATTACH_DECL_NEW(xdc, sizeof(struct xdc_softc),
267     xdcmatch, xdcattach, NULL, NULL);
268 
269 CFATTACH_DECL_NEW(xd, sizeof(struct xd_softc),
270     xdmatch, xdattach, NULL, NULL);
271 
272 struct xdc_attach_args {	/* this is the "aux" args to xdattach */
273 	int	driveno;	/* unit number */
274 	char	*dvmabuf;	/* scratch buffer for reading disk label */
275 	int	fullmode;	/* submit mode */
276 	int	booting;	/* are we booting or not? */
277 };
278 
279 dev_type_open(xdopen);
280 dev_type_close(xdclose);
281 dev_type_read(xdread);
282 dev_type_write(xdwrite);
283 dev_type_ioctl(xdioctl);
284 dev_type_strategy(xdstrategy);
285 dev_type_dump(xddump);
286 dev_type_size(xdsize);
287 
288 const struct bdevsw xd_bdevsw = {
289 	xdopen, xdclose, xdstrategy, xdioctl, xddump, xdsize, D_DISK
290 };
291 
292 const struct cdevsw xd_cdevsw = {
293 	xdopen, xdclose, xdread, xdwrite, xdioctl,
294 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
295 };
296 
297 /*
298  * dkdriver
299  */
300 
301 struct dkdriver xddkdriver = { xdstrategy };
302 
303 /*
304  * start: disk label fix code (XXX)
305  */
306 
307 static void *xd_labeldata;
308 
309 static void
310 xddummystrat(struct buf *bp)
311 {
312 	if (bp->b_bcount != XDFM_BPS)
313 		panic("%s: b_bcount", __func__);
314 	memcpy(bp->b_data, xd_labeldata, XDFM_BPS);
315 	bp->b_oflags |= BO_DONE;
316 	bp->b_cflags &= ~BC_BUSY;
317 }
318 
319 int
320 xdgetdisklabel(struct xd_softc *xd, void *b)
321 {
322 	const char *err;
323 	struct sun_disklabel *sdl;
324 
325 	/* We already have the label data in `b'; setup for dummy strategy */
326 	xd_labeldata = b;
327 
328 	/* Required parameter for readdisklabel() */
329 	xd->sc_dk.dk_label->d_secsize = XDFM_BPS;
330 
331 	err = readdisklabel(MAKEDISKDEV(0, device_unit(xd->sc_dev), RAW_PART),
332 	    xddummystrat, xd->sc_dk.dk_label, xd->sc_dk.dk_cpulabel);
333 	if (err) {
334 		printf("%s: %s\n", device_xname(xd->sc_dev), err);
335 		return XD_ERR_FAIL;
336 	}
337 
338 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
339 	sdl = (struct sun_disklabel *)xd->sc_dk.dk_cpulabel->cd_block;
340 	if (sdl->sl_magic == SUN_DKMAGIC)
341 		xd->pcyl = sdl->sl_pcyl;
342 	else {
343 		printf("%s: WARNING: no `pcyl' in disk label.\n",
344 		    device_xname(xd->sc_dev));
345 		xd->pcyl = xd->sc_dk.dk_label->d_ncylinders +
346 		    xd->sc_dk.dk_label->d_acylinders;
347 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
348 		    device_xname(xd->sc_dev), xd->pcyl);
349 	}
350 
351 	xd->ncyl = xd->sc_dk.dk_label->d_ncylinders;
352 	xd->acyl = xd->sc_dk.dk_label->d_acylinders;
353 	xd->nhead = xd->sc_dk.dk_label->d_ntracks;
354 	xd->nsect = xd->sc_dk.dk_label->d_nsectors;
355 	xd->sectpercyl = xd->nhead * xd->nsect;
356 	xd->sc_dk.dk_label->d_secsize = XDFM_BPS; /* not handled by
357 						  * sun->bsd */
358 	return XD_ERR_AOK;
359 }
360 
361 /*
362  * end: disk label fix code (XXX)
363  */
364 
365 /*
366  * a u t o c o n f i g   f u n c t i o n s
367  */
368 
369 /*
370  * xdcmatch: determine if xdc is present or not.   we do a
371  * soft reset to detect the xdc.
372  */
373 
374 int
375 xdcmatch(device_t parent, cfdata_t cf, void *aux)
376 {
377 	struct confargs *ca = aux;
378 
379 	/* No default VME address. */
380 	if (ca->ca_paddr == -1)
381 		return 0;
382 
383 	/* Make sure something is there... */
384 	if (bus_peek(ca->ca_bustype, ca->ca_paddr + 11, 1) == -1)
385 		return 0;
386 
387 	/* Default interrupt priority. */
388 	if (ca->ca_intpri == -1)
389 		ca->ca_intpri = 2;
390 
391 	return 1;
392 }
393 
394 /*
395  * xdcattach: attach controller
396  */
397 void
398 xdcattach(device_t parent, device_t self, void *aux)
399 {
400 	struct xdc_softc *xdc = device_private(self);
401 	struct confargs *ca = aux;
402 	struct xdc_attach_args xa;
403 	int lcv, rqno, err;
404 	struct xd_iopb_ctrl *ctl;
405 
406 	/* get addressing and intr level stuff from autoconfig and load it
407 	 * into our xdc_softc. */
408 
409 	xdc->sc_dev = self;
410 	xdc->xdc = (struct xdc *)bus_mapin(ca->ca_bustype, ca->ca_paddr,
411 	    sizeof(struct xdc));
412 	xdc->bustype = ca->ca_bustype;
413 	xdc->ipl     = ca->ca_intpri;
414 	xdc->vector  = ca->ca_intvec;
415 
416 	for (lcv = 0; lcv < XDC_MAXDEV; lcv++)
417 		xdc->sc_drives[lcv] = NULL;
418 
419 	/* allocate and zero buffers
420 	 *
421 	 * note: we simplify the code by allocating the max number of iopbs and
422 	 * iorq's up front.   thus, we avoid linked lists and the costs
423 	 * associated with them in exchange for wasting a little memory. */
424 
425 	xdc->iopbase = (struct xd_iopb *)dvma_malloc(XDC_MAXIOPB *
426 	    sizeof(struct xd_iopb));	/* KVA */
427 	memset(xdc->iopbase, 0, XDC_MAXIOPB * sizeof(struct xd_iopb));
428 	xdc->dvmaiopb = (struct xd_iopb *)dvma_kvtopa(xdc->iopbase,
429 	    xdc->bustype);
430 	xdc->reqs = malloc(XDC_MAXIOPB * sizeof(struct xd_iorq),
431 	    M_DEVBUF, M_NOWAIT | M_ZERO);
432 	if (xdc->reqs == NULL)
433 		panic("xdc malloc");
434 
435 	/* init free list, iorq to iopb pointers, and non-zero fields in the
436 	 * iopb which never change. */
437 
438 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
439 		xdc->reqs[lcv].iopb = &xdc->iopbase[lcv];
440 		xdc->freereq[lcv] = lcv;
441 		xdc->iopbase[lcv].fixd = 1;	/* always the same */
442 		xdc->iopbase[lcv].naddrmod = XDC_ADDRMOD; /* always the same */
443 		xdc->iopbase[lcv].intr_vec = xdc->vector; /* always the same */
444 	}
445 	xdc->nfree = XDC_MAXIOPB;
446 	xdc->nrun = 0;
447 	xdc->waithead = xdc->waitend = xdc->nwait = 0;
448 	xdc->ndone = 0;
449 
450 	/* init queue of waiting bufs */
451 
452 	bufq_alloc(&xdc->sc_wq, "fcfs", 0);
453 	callout_init(&xdc->sc_tick_ch, 0);
454 
455 	/*
456 	 * section 7 of the manual tells us how to init the controller:
457 	 * - read controller parameters (6/0)
458 	 * - write controller parameters (5/0)
459 	 */
460 
461 	/* read controller parameters and insure we have a 753/7053 */
462 
463 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
464 	if (rqno == XD_ERR_FAIL) {
465 		aprint_error(": couldn't read controller params\n");
466 		return;		/* shouldn't ever happen */
467 	}
468 	ctl = (struct xd_iopb_ctrl *)&xdc->iopbase[rqno];
469 	if (ctl->ctype != XDCT_753) {
470 		if (xdc->reqs[rqno].errno)
471 			aprint_error(": %s: ",
472 			    xdc_e2str(xdc->reqs[rqno].errno));
473 		aprint_error(": doesn't identify as a 753/7053\n");
474 		XDC_DONE(xdc, rqno, err);
475 		return;
476 	}
477 	aprint_normal(": Xylogics 753/7053, PROM=0x%x.%02x.%02x\n",
478 	    ctl->eprom_partno, ctl->eprom_lvl, ctl->eprom_rev);
479 	XDC_DONE(xdc, rqno, err);
480 
481 	/* now write controller parameters (xdc_cmd sets all params for us) */
482 
483 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_CTL, 0, 0, 0, 0, XD_SUB_POLL);
484 	XDC_DONE(xdc, rqno, err);
485 	if (err) {
486 		aprint_error_dev(self, "controller config error: %s\n",
487 		    xdc_e2str(err));
488 		return;
489 	}
490 
491 	/* link in interrupt with higher level software */
492 	isr_add_vectored(xdcintr, xdc, ca->ca_intpri, ca->ca_intvec);
493 	evcnt_attach_dynamic(&xdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
494 	    device_xname(self), "intr");
495 
496 	/* now we must look for disks using autoconfig */
497 	xa.booting = 1;
498 	for (xa.driveno = 0; xa.driveno < XDC_MAXDEV; xa.driveno++)
499 		(void)config_found(self, (void *)&xa, xdc_print);
500 
501 	/* start the watchdog clock */
502 	callout_reset(&xdc->sc_tick_ch, XDC_TICKCNT, xdc_tick, xdc);
503 }
504 
505 int
506 xdc_print(void *aux, const char *name)
507 {
508 	struct xdc_attach_args *xa = aux;
509 
510 	if (name != NULL)
511 		aprint_normal("%s: ", name);
512 
513 	if (xa->driveno != -1)
514 		aprint_normal(" drive %d", xa->driveno);
515 
516 	return UNCONF;
517 }
518 
519 /*
520  * xdmatch: probe for disk.
521  *
522  * note: we almost always say disk is present.   this allows us to
523  * spin up and configure a disk after the system is booted (we can
524  * call xdattach!).  Also, wire down the relationship between the
525  * xd* and xdc* devices, to simplify boot device identification.
526  */
527 int
528 xdmatch(device_t parent, cfdata_t cf, void *aux)
529 {
530 	struct xdc_attach_args *xa = aux;
531 	int xd_unit;
532 
533 	/* Match only on the "wired-down" controller+disk. */
534 	xd_unit = device_unit(parent) * 2 + xa->driveno;
535 	if (cf->cf_unit != xd_unit)
536 		return 0;
537 
538 	return 1;
539 }
540 
541 /*
542  * xdattach: attach a disk.
543  */
544 void
545 xdattach(device_t parent, device_t self, void *aux)
546 {
547 	struct xd_softc *xd = device_private(self);
548 	struct xdc_softc *xdc = device_private(parent);
549 	struct xdc_attach_args *xa = aux;
550 
551 	xd->sc_dev = self;
552 	aprint_normal("\n");
553 
554 	/*
555 	 * Always re-initialize the disk structure.  We want statistics
556 	 * to start with a clean slate.
557 	 */
558 	memset(&xd->sc_dk, 0, sizeof(xd->sc_dk));
559 	disk_init(&xd->sc_dk, device_xname(self), &xddkdriver);
560 
561 	xd->state = XD_DRIVE_UNKNOWN;	/* to start */
562 	xd->flags = 0;
563 	xd->parent = xdc;
564 
565 	xd->xd_drive = xa->driveno;
566 	xdc->sc_drives[xa->driveno] = xd;
567 
568 	/* Do init work common to attach and open. */
569 	xd_init(xd);
570 }
571 
572 /*
573  * end of autoconfig functions
574  */
575 
576 /*
577  * Initialize a disk.  This can be called from both autoconf and
578  * also from xdopen/xdstrategy.
579  */
580 static void
581 xd_init(struct xd_softc *xd)
582 {
583 	struct xdc_softc *xdc;
584 	struct dkbad *dkb;
585 	struct xd_iopb_drive *driopb;
586 	void *dvmabuf;
587 	int rqno, err, spt, mb, blk, lcv, fullmode, newstate;
588 
589 	xdc = xd->parent;
590 	xd->state = XD_DRIVE_ATTACHING;
591 	newstate = XD_DRIVE_UNKNOWN;
592 	fullmode = (cold) ? XD_SUB_POLL : XD_SUB_WAIT;
593 	dvmabuf = dvma_malloc(XDFM_BPS);
594 
595 	/* first try and reset the drive */
596 	rqno = xdc_cmd(xdc, XDCMD_RST, 0, xd->xd_drive, 0, 0, 0, fullmode);
597 	XDC_DONE(xdc, rqno, err);
598 	if (err == XD_ERR_NRDY) {
599 		printf("%s: drive %d: off-line\n",
600 		    device_xname(xd->sc_dev), xd->xd_drive);
601 		goto done;
602 	}
603 	if (err) {
604 		printf("%s: ERROR 0x%02x (%s)\n",
605 		    device_xname(xd->sc_dev), err, xdc_e2str(err));
606 		goto done;
607 	}
608 	printf("%s: drive %d ready\n",
609 	    device_xname(xd->sc_dev), xd->xd_drive);
610 
611 	/* now set format parameters */
612 
613 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_FMT, xd->xd_drive,
614 	    0, 0, 0, fullmode);
615 	XDC_DONE(xdc, rqno, err);
616 	if (err) {
617 		printf("%s: write format parameters failed: %s\n",
618 		    device_xname(xd->sc_dev), xdc_e2str(err));
619 		goto done;
620 	}
621 
622 	/* get drive parameters */
623 	spt = 0;
624 	rqno = xdc_cmd(xdc, XDCMD_RDP, XDFUN_DRV, xd->xd_drive,
625 	    0, 0, 0, fullmode);
626 	if (rqno != XD_ERR_FAIL) {
627 		driopb = (struct xd_iopb_drive *)&xdc->iopbase[rqno];
628 		spt = driopb->sectpertrk;
629 	}
630 	XDC_DONE(xdc, rqno, err);
631 	if (err) {
632 		printf("%s: read drive parameters failed: %s\n",
633 		    device_xname(xd->sc_dev), xdc_e2str(err));
634 		goto done;
635 	}
636 
637 	/*
638 	 * now set drive parameters (to semi-bogus values) so we can read the
639 	 * disk label.
640 	 */
641 	xd->pcyl = xd->ncyl = 1;
642 	xd->acyl = 0;
643 	xd->nhead = 1;
644 	xd->nsect = 1;
645 	xd->sectpercyl = 1;
646 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
647 		xd->dkb.bt_bad[lcv].bt_cyl =
648 		    xd->dkb.bt_bad[lcv].bt_trksec = 0xffff;
649 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive,
650 	    0, 0, 0, fullmode);
651 	XDC_DONE(xdc, rqno, err);
652 	if (err) {
653 		printf("%s: write drive parameters failed: %s\n",
654 		    device_xname(xd->sc_dev), xdc_e2str(err));
655 		goto done;
656 	}
657 
658 	/* read disk label */
659 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive,
660 	    0, 1, dvmabuf, fullmode);
661 	XDC_DONE(xdc, rqno, err);
662 	if (err) {
663 		printf("%s: reading disk label failed: %s\n",
664 		    device_xname(xd->sc_dev), xdc_e2str(err));
665 		goto done;
666 	}
667 	newstate = XD_DRIVE_NOLABEL;
668 
669 	xd->hw_spt = spt;
670 	/* Attach the disk: must be before getdisklabel to malloc label */
671 	disk_attach(&xd->sc_dk);
672 
673 	if (xdgetdisklabel(xd, dvmabuf) != XD_ERR_AOK)
674 		goto done;
675 
676 	/* inform the user of what is up */
677 	printf("%s: <%s>, pcyl %d, hw_spt %d\n",
678 	    device_xname(xd->sc_dev), (char *)dvmabuf, xd->pcyl, spt);
679 	mb = xd->ncyl * (xd->nhead * xd->nsect) / (1048576 / XDFM_BPS);
680 	printf("%s: %dMB, %d cyl, %d head, %d sec\n",
681 	    device_xname(xd->sc_dev), mb,
682 	    xd->ncyl, xd->nhead, xd->nsect);
683 
684 	/* now set the real drive parameters! */
685 	rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive,
686 	    0, 0, 0, fullmode);
687 	XDC_DONE(xdc, rqno, err);
688 	if (err) {
689 		printf("%s: write real drive parameters failed: %s\n",
690 		    device_xname(xd->sc_dev), xdc_e2str(err));
691 		goto done;
692 	}
693 	newstate = XD_DRIVE_ONLINE;
694 
695 	/*
696 	 * read bad144 table. this table resides on the first sector of the
697 	 * last track of the disk (i.e. second cyl of "acyl" area).
698 	 */
699 	blk = (xd->ncyl + xd->acyl - 1) * (xd->nhead * xd->nsect) + /* last cyl */
700 	    (xd->nhead - 1) * xd->nsect;	/* last head */
701 	rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive,
702 				   blk, 1, dvmabuf, fullmode);
703 	XDC_DONE(xdc, rqno, err);
704 	if (err) {
705 		printf("%s: reading bad144 failed: %s\n",
706 		    device_xname(xd->sc_dev), xdc_e2str(err));
707 		goto done;
708 	}
709 
710 	/* check dkbad for sanity */
711 	dkb = (struct dkbad *)dvmabuf;
712 	for (lcv = 0; lcv < 126; lcv++) {
713 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
714 		    dkb->bt_bad[lcv].bt_cyl == 0) &&
715 		    dkb->bt_bad[lcv].bt_trksec == 0xffff)
716 			continue;	/* blank */
717 		if (dkb->bt_bad[lcv].bt_cyl >= xd->ncyl)
718 			break;
719 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xd->nhead)
720 			break;
721 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xd->nsect)
722 			break;
723 	}
724 	if (lcv != 126) {
725 		printf("%s: warning: invalid bad144 sector!\n",
726 		    device_xname(xd->sc_dev));
727 	} else {
728 		memcpy(&xd->dkb, dvmabuf, XDFM_BPS);
729 	}
730 
731  done:
732 	xd->state = newstate;
733 	dvma_free(dvmabuf, XDFM_BPS);
734 }
735 
736 /*
737  * { b , c } d e v s w   f u n c t i o n s
738  */
739 
740 /*
741  * xdclose: close device
742  */
743 int
744 xdclose(dev_t dev, int flag, int fmt, struct lwp *l)
745 {
746 	struct xd_softc *xd = device_lookup_private(&xd_cd, DISKUNIT(dev));
747 	int part = DISKPART(dev);
748 
749 	/* clear mask bits */
750 
751 	switch (fmt) {
752 	case S_IFCHR:
753 		xd->sc_dk.dk_copenmask &= ~(1 << part);
754 		break;
755 	case S_IFBLK:
756 		xd->sc_dk.dk_bopenmask &= ~(1 << part);
757 		break;
758 	}
759 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
760 
761 	return 0;
762 }
763 
764 /*
765  * xddump: crash dump system
766  */
767 int
768 xddump(dev_t dev, daddr_t blkno, void *va, size_t sz)
769 {
770 	int unit, part;
771 	struct xd_softc *xd;
772 
773 	unit = DISKUNIT(dev);
774 	part = DISKPART(dev);
775 
776 	xd = device_lookup_private(&xd_cd, unit);
777 	if (xd == NULL)
778 		return ENXIO;
779 
780 	printf("%s%c: crash dump not supported (yet)\n",
781 	    device_xname(xd->sc_dev), 'a' + part);
782 
783 	return ENXIO;
784 
785 	/* outline: globals: "dumplo" == sector number of partition to start
786 	 * dump at (convert to physical sector with partition table)
787 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
788 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
789 	 * physmem)
790 	 *
791 	 * dump a copy of physical memory to the dump device starting at sector
792 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
793 	 * we go.   use polled I/O.
794 	 *
795 	 * XXX how to handle NON_CONTIG?
796 	 */
797 }
798 
799 static enum kauth_device_req
800 xd_getkauthreq(u_char cmd)
801 {
802 	enum kauth_device_req req;
803 
804 	switch (cmd) {
805 	case XDCMD_WR:
806 	case XDCMD_XWR:
807 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITE;
808 		break;
809 
810 	case XDCMD_RD:
811 	case XDCMD_XRD:
812 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READ;
813 		break;
814 
815 	case XDCMD_RDP:
816 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF;
817 		break;
818 
819 	case XDCMD_WRP:
820 	case XDCMD_RST:
821 		req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF;
822 		break;
823 
824 	case XDCMD_NOP:
825 	case XDCMD_SK:
826 	case XDCMD_TST:
827 	default:
828 		req = 0;
829 		break;
830 	}
831 
832 	return req;
833 }
834 
835 /*
836  * xdioctl: ioctls on XD drives.   based on ioctl's of other netbsd disks.
837  */
838 int
839 xdioctl(dev_t dev, u_long command, void *addr, int flag, struct lwp *l)
840 {
841 	struct xd_softc *xd;
842 	struct xd_iocmd *xio;
843 	int     error, s, unit;
844 
845 	unit = DISKUNIT(dev);
846 
847 	xd = device_lookup_private(&xd_cd, unit);
848 	if (xd == NULL)
849 		return (ENXIO);
850 
851 	/* switch on ioctl type */
852 
853 	switch (command) {
854 	case DIOCSBAD:		/* set bad144 info */
855 		if ((flag & FWRITE) == 0)
856 			return EBADF;
857 		s = splbio();
858 		memcpy(&xd->dkb, addr, sizeof(xd->dkb));
859 		splx(s);
860 		return 0;
861 
862 	case DIOCGDINFO:	/* get disk label */
863 		memcpy(addr, xd->sc_dk.dk_label, sizeof(struct disklabel));
864 		return 0;
865 
866 	case DIOCGPART:	/* get partition info */
867 		((struct partinfo *)addr)->disklab = xd->sc_dk.dk_label;
868 		((struct partinfo *)addr)->part =
869 		    &xd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
870 		return 0;
871 
872 	case DIOCSDINFO:	/* set disk label */
873 		if ((flag & FWRITE) == 0)
874 			return EBADF;
875 		error = setdisklabel(xd->sc_dk.dk_label,
876 		    (struct disklabel *)addr, /* xd->sc_dk.dk_openmask : */ 0,
877 		    xd->sc_dk.dk_cpulabel);
878 		if (error == 0) {
879 			if (xd->state == XD_DRIVE_NOLABEL)
880 				xd->state = XD_DRIVE_ONLINE;
881 		}
882 		return error;
883 
884 	case DIOCWLABEL:	/* change write status of disk label */
885 		if ((flag & FWRITE) == 0)
886 			return EBADF;
887 		if (*(int *)addr)
888 			xd->flags |= XD_WLABEL;
889 		else
890 			xd->flags &= ~XD_WLABEL;
891 		return 0;
892 
893 	case DIOCWDINFO:	/* write disk label */
894 		if ((flag & FWRITE) == 0)
895 			return EBADF;
896 		error = setdisklabel(xd->sc_dk.dk_label,
897 		    (struct disklabel *)addr, /* xd->sc_dk.dk_openmask : */ 0,
898 		    xd->sc_dk.dk_cpulabel);
899 		if (error == 0) {
900 			if (xd->state == XD_DRIVE_NOLABEL)
901 				xd->state = XD_DRIVE_ONLINE;
902 
903 			/* Simulate opening partition 0 so write succeeds. */
904 			xd->sc_dk.dk_openmask |= (1 << 0);
905 			error = writedisklabel(MAKEDISKDEV(major(dev),
906 			    DISKUNIT(dev), RAW_PART),
907 			    xdstrategy, xd->sc_dk.dk_label,
908 			    xd->sc_dk.dk_cpulabel);
909 			xd->sc_dk.dk_openmask =
910 			    xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
911 		}
912 		return error;
913 
914 	case DIOSXDCMD: {
915 		enum kauth_device_req req;
916 
917 		xio = (struct xd_iocmd *)addr;
918 		req = xd_getkauthreq(xio->cmd);
919 		if ((error = kauth_authorize_device_passthru(l->l_cred,
920 		    dev, req, xio)) != 0)
921 			return error;
922 		return xdc_ioctlcmd(xd, dev, xio);
923 		}
924 
925 	default:
926 		return ENOTTY;
927 	}
928 }
929 
930 /*
931  * xdopen: open drive
932  */
933 int
934 xdopen(dev_t dev, int flag, int fmt, struct lwp *l)
935 {
936 	int err, unit, part, s;
937 	struct xd_softc *xd;
938 
939 	/* first, could it be a valid target? */
940 	unit = DISKUNIT(dev);
941 	xd = device_lookup_private(&xd_cd, unit);
942 	if (xd == NULL)
943 		return ENXIO;
944 	part = DISKPART(dev);
945 	err = 0;
946 
947 	/*
948 	 * If some other processing is doing init, sleep.
949 	 */
950 	s = splbio();
951 	while (xd->state == XD_DRIVE_ATTACHING) {
952 		if (tsleep(&xd->state, PRIBIO, "xdopen", 0)) {
953 			err = EINTR;
954 			goto done;
955 		}
956 	}
957 	/* Do we need to init the drive? */
958 	if (xd->state == XD_DRIVE_UNKNOWN) {
959 		xd_init(xd);
960 		wakeup(&xd->state);
961 	}
962 	/* Was the init successful? */
963 	if (xd->state == XD_DRIVE_UNKNOWN) {
964 		err = EIO;
965 		goto done;
966 	}
967 
968 	/* check for partition */
969 	if (part != RAW_PART &&
970 	    (part >= xd->sc_dk.dk_label->d_npartitions ||
971 		xd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
972 		err = ENXIO;
973 		goto done;
974 	}
975 
976 	/* set open masks */
977 	switch (fmt) {
978 	case S_IFCHR:
979 		xd->sc_dk.dk_copenmask |= (1 << part);
980 		break;
981 	case S_IFBLK:
982 		xd->sc_dk.dk_bopenmask |= (1 << part);
983 		break;
984 	}
985 	xd->sc_dk.dk_openmask = xd->sc_dk.dk_copenmask | xd->sc_dk.dk_bopenmask;
986 
987  done:
988 	splx(s);
989 	return err;
990 }
991 
992 int
993 xdread(dev_t dev, struct uio *uio, int flags)
994 {
995 
996 	return physio(xdstrategy, NULL, dev, B_READ, minphys, uio);
997 }
998 
999 int
1000 xdwrite(dev_t dev, struct uio *uio, int flags)
1001 {
1002 
1003 	return physio(xdstrategy, NULL, dev, B_WRITE, minphys, uio);
1004 }
1005 
1006 
1007 /*
1008  * xdsize: return size of a partition for a dump
1009  */
1010 int
1011 xdsize(dev_t dev)
1012 {
1013 	struct xd_softc *xdsc;
1014 	int unit, part, size, omask;
1015 
1016 	/* valid unit? */
1017 	unit = DISKUNIT(dev);
1018 	xdsc = device_lookup_private(&xd_cd, unit);
1019 	if (xdsc == NULL)
1020 		return -1;
1021 
1022 	part = DISKPART(dev);
1023 	omask = xdsc->sc_dk.dk_openmask & (1 << part);
1024 
1025 	if (omask == 0 && xdopen(dev, 0, S_IFBLK, NULL) != 0)
1026 		return -1;
1027 
1028 	/* do it */
1029 	if (xdsc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1030 		size = -1;	/* only give valid size for swap partitions */
1031 	else
1032 		size = xdsc->sc_dk.dk_label->d_partitions[part].p_size *
1033 		    (xdsc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1034 	if (omask == 0 && xdclose(dev, 0, S_IFBLK, NULL) != 0)
1035 		return -1;
1036 	return size;
1037 }
1038 
1039 /*
1040  * xdstrategy: buffering system interface to xd.
1041  */
1042 void
1043 xdstrategy(struct buf *bp)
1044 {
1045 	struct xd_softc *xd;
1046 	struct xdc_softc *parent;
1047 	int s, unit;
1048 
1049 	unit = DISKUNIT(bp->b_dev);
1050 
1051 	/* check for live device */
1052 
1053 	xd = device_lookup_private(&xd_cd, unit);
1054 	if (xd == NULL ||
1055 	    bp->b_blkno < 0 ||
1056 	    (bp->b_bcount % xd->sc_dk.dk_label->d_secsize) != 0) {
1057 		bp->b_error = EINVAL;
1058 		goto done;
1059 	}
1060 
1061 	/* There should always be an open first. */
1062 	if (xd->state == XD_DRIVE_UNKNOWN) {
1063 		bp->b_error = EIO;
1064 		goto done;
1065 	}
1066 
1067 	if (xd->state != XD_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1068 		/* no I/O to unlabeled disks, unless raw partition */
1069 		bp->b_error = EIO;
1070 		goto done;
1071 	}
1072 	/* short circuit zero length request */
1073 
1074 	if (bp->b_bcount == 0)
1075 		goto done;
1076 
1077 	/* check bounds with label (disksubr.c).  Determine the size of the
1078 	 * transfer, and make sure it is within the boundaries of the
1079 	 * partition. Adjust transfer if needed, and signal errors or early
1080 	 * completion. */
1081 
1082 	if (bounds_check_with_label(&xd->sc_dk, bp,
1083 		(xd->flags & XD_WLABEL) != 0) <= 0)
1084 		goto done;
1085 
1086 	/*
1087 	 * now we know we have a valid buf structure that we need to do I/O
1088 	 * on.
1089 	 *
1090 	 * note that we don't disksort because the controller has a sorting
1091 	 * algorithm built into the hardware.
1092 	 */
1093 
1094 	s = splbio();		/* protect the queues */
1095 
1096 	/* first, give jobs in front of us a chance */
1097 	parent = xd->parent;
1098 	while (parent->nfree > 0 && bufq_peek(parent->sc_wq) != NULL)
1099 		if (xdc_startbuf(parent, NULL, NULL) != XD_ERR_AOK)
1100 			break;
1101 
1102 	/*
1103 	 * if there are no free iorq's, then we just queue and return. the
1104 	 * buffs will get picked up later by xdcintr().
1105 	 */
1106 	if (parent->nfree == 0) {
1107 		bufq_put(parent->sc_wq, bp);
1108 		splx(s);
1109 		return;
1110 	}
1111 
1112 	/* now we have free iopb's and we are at splbio... start 'em up */
1113 	if (xdc_startbuf(parent, xd, bp) != XD_ERR_AOK) {
1114 		return;
1115 	}
1116 
1117 	/* done! */
1118 
1119 	splx(s);
1120 	return;
1121 
1122  done:
1123 	/* tells upper layers we are done with this buf */
1124 	bp->b_resid = bp->b_bcount;
1125 	biodone(bp);
1126 }
1127 /*
1128  * end of {b,c}devsw functions
1129  */
1130 
1131 /*
1132  * i n t e r r u p t   f u n c t i o n
1133  *
1134  * xdcintr: hardware interrupt.
1135  */
1136 int
1137 xdcintr(void *v)
1138 {
1139 	struct xdc_softc *xdcsc = v;
1140 
1141 	/* kick the event counter */
1142 	xdcsc->sc_intrcnt.ev_count++;
1143 
1144 	/* remove as many done IOPBs as possible */
1145 	xdc_remove_iorq(xdcsc);
1146 
1147 	/* start any iorq's already waiting */
1148 	xdc_start(xdcsc, XDC_MAXIOPB);
1149 
1150 	/* fill up any remaining iorq's with queue'd buffers */
1151 	while (xdcsc->nfree > 0 && bufq_peek(xdcsc->sc_wq) != NULL)
1152 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
1153 			break;
1154 
1155 	return 1;
1156 }
1157 /*
1158  * end of interrupt function
1159  */
1160 
1161 /*
1162  * i n t e r n a l   f u n c t i o n s
1163  */
1164 
1165 /*
1166  * xdc_rqinit: fill out the fields of an I/O request
1167  */
1168 
1169 inline void
1170 xdc_rqinit(struct xd_iorq *rq, struct xdc_softc *xdc, struct xd_softc *xd,
1171     int md, u_long blk, int cnt, void *db, struct buf *bp)
1172 {
1173 
1174 	rq->xdc = xdc;
1175 	rq->xd = xd;
1176 	rq->ttl = XDC_MAXTTL + 10;
1177 	rq->mode = md;
1178 	rq->tries = rq->errno = rq->lasterror = 0;
1179 	rq->blockno = blk;
1180 	rq->sectcnt = cnt;
1181 	rq->dbuf = rq->dbufbase = db;
1182 	rq->buf = bp;
1183 }
1184 
1185 /*
1186  * xdc_rqtopb: load up an IOPB based on an iorq
1187  */
1188 void
1189 xdc_rqtopb(struct xd_iorq *iorq, struct xd_iopb *iopb, int cmd, int subfun)
1190 {
1191 	u_long  block, dp;
1192 
1193 	/* standard stuff */
1194 
1195 	iopb->errs = iopb->done = 0;
1196 	iopb->comm = cmd;
1197 	iopb->errno = iopb->status = 0;
1198 	iopb->subfun = subfun;
1199 	if (iorq->xd)
1200 		iopb->unit = iorq->xd->xd_drive;
1201 	else
1202 		iopb->unit = 0;
1203 
1204 	/* check for alternate IOPB format */
1205 
1206 	if (cmd == XDCMD_WRP) {
1207 		switch (subfun) {
1208 		case XDFUN_CTL:{
1209 			struct xd_iopb_ctrl *ctrl =
1210 			    (struct xd_iopb_ctrl *)iopb;
1211 			iopb->lll = 0;
1212 			iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
1213 			    ? 0 : iorq->xdc->ipl;
1214 			ctrl->param_a = XDPA_TMOD | XDPA_DACF;
1215 			ctrl->param_b = XDPB_ROR | XDPB_TDT_3_2USEC;
1216 			ctrl->param_c = XDPC_OVS | XDPC_COP | XDPC_ASR |
1217 			    XDPC_RBC | XDPC_ECC2;
1218 			ctrl->throttle = XDC_THROTTLE;
1219 #ifdef sparc
1220 			if (CPU_ISSUN4 && cpuinfo.cpu_type == CPUTYP_4_300)
1221 				ctrl->delay = XDC_DELAY_4_300;
1222 			else
1223 				ctrl->delay = XDC_DELAY_SPARC;
1224 #endif
1225 #ifdef sun3
1226 			ctrl->delay = XDC_DELAY_SUN3;
1227 #endif
1228 			break;
1229 			}
1230 		case XDFUN_DRV:{
1231 			struct xd_iopb_drive *drv =
1232 			    (struct xd_iopb_drive *)iopb;
1233 			/* we assume that the disk label has the right
1234 			 * info */
1235 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
1236 				drv->dparam_ipl = (XDC_DPARAM << 3);
1237 			else
1238 				drv->dparam_ipl = (XDC_DPARAM << 3) |
1239 				    iorq->xdc->ipl;
1240 			drv->maxsect = iorq->xd->nsect - 1;
1241 			drv->maxsector = drv->maxsect;
1242 			/* note: maxsector != maxsect only if you are
1243 			 * doing cyl sparing */
1244 			drv->headoff = 0;
1245 			drv->maxcyl = iorq->xd->pcyl - 1;
1246 			drv->maxhead = iorq->xd->nhead - 1;
1247 			break;
1248 			}
1249 		case XDFUN_FMT:
1250 		    {
1251 			struct xd_iopb_format *form =
1252 			    (struct xd_iopb_format *)iopb;
1253 
1254 			if (XD_STATE(iorq->mode) == XD_SUB_POLL)
1255 				form->interleave_ipl = (XDC_INTERLEAVE << 3);
1256 			else
1257 				form->interleave_ipl = (XDC_INTERLEAVE << 3) |
1258 				    iorq->xdc->ipl;
1259 			form->field1 = XDFM_FIELD1;
1260 			form->field2 = XDFM_FIELD2;
1261 			form->field3 = XDFM_FIELD3;
1262 			form->field4 = XDFM_FIELD4;
1263 			form->bytespersec = XDFM_BPS;
1264 			form->field6 = XDFM_FIELD6;
1265 			form->field7 = XDFM_FIELD7;
1266 			break;
1267 		    }
1268 		}
1269 	} else {
1270 
1271 		/* normal IOPB case (harmless to RDP command) */
1272 
1273 		iopb->lll = 0;
1274 		iopb->intl = (XD_STATE(iorq->mode) == XD_SUB_POLL)
1275 		    ? 0 : iorq->xdc->ipl;
1276 		iopb->sectcnt = iorq->sectcnt;
1277 		block = iorq->blockno;
1278 		if (iorq->xd == NULL || block == 0) {
1279 			iopb->sectno = iopb->headno = iopb->cylno = 0;
1280 		} else {
1281 			iopb->sectno = block % iorq->xd->nsect;
1282 			block = block / iorq->xd->nsect;
1283 			iopb->headno = block % iorq->xd->nhead;
1284 			block = block / iorq->xd->nhead;
1285 			iopb->cylno = block;
1286 		}
1287 		iopb->daddr = dp = (iorq->dbuf == NULL) ? 0 :
1288 		    dvma_kvtopa(iorq->dbuf, iorq->xdc->bustype);
1289 		iopb->addrmod = XDC_ADDRMOD;
1290 	}
1291 }
1292 
1293 /*
1294  * xdc_cmd: front end for POLL'd and WAIT'd commands.  Returns rqno.
1295  * If you've already got an IORQ, you can call submit directly (currently
1296  * there is no need to do this).    NORM requests are handled separately.
1297  */
1298 int
1299 xdc_cmd(struct xdc_softc *xdcsc, int cmd, int subfn, int unit, int block,
1300     int scnt, char *dptr, int fullmode)
1301 {
1302 	struct xd_iorq *iorq;
1303 	struct xd_iopb *iopb;
1304 	int rqno, retry;
1305 	int submode = XD_STATE(fullmode);
1306 
1307 	/* get iorq/iopb */
1308 	switch (submode) {
1309 	case XD_SUB_POLL:
1310 		while (xdcsc->nfree == 0) {
1311 			if (xdc_piodriver(xdcsc, 0, 1) != XD_ERR_AOK)
1312 				return XD_ERR_FAIL;
1313 		}
1314 		break;
1315 	case XD_SUB_WAIT:
1316 		retry = 1;
1317 		while (retry) {
1318 			while (xdcsc->nfree == 0) {
1319 			    if (tsleep(&xdcsc->nfree, PRIBIO, "xdnfree", 0))
1320 				return XD_ERR_FAIL;
1321 			}
1322 			while (xdcsc->ndone > XDC_SUBWAITLIM) {
1323 			    if (tsleep(&xdcsc->ndone, PRIBIO, "xdsubwait", 0))
1324 				return XD_ERR_FAIL;
1325 			}
1326 			if (xdcsc->nfree)
1327 				retry = 0;	/* got it */
1328 		}
1329 		break;
1330 	default:
1331 		return XD_ERR_FAIL;	/* illegal */
1332 	}
1333 	if (xdcsc->nfree == 0)
1334 		panic("xdcmd nfree");
1335 	rqno = XDC_RQALLOC(xdcsc);
1336 	iorq = &xdcsc->reqs[rqno];
1337 	iopb = iorq->iopb;
1338 
1339 
1340 	/* init iorq/iopb */
1341 	xdc_rqinit(iorq, xdcsc,
1342 	    (unit == XDC_NOUNIT) ? NULL : xdcsc->sc_drives[unit],
1343 	    fullmode, block, scnt, dptr, NULL);
1344 
1345 	/* load IOPB from iorq */
1346 	xdc_rqtopb(iorq, iopb, cmd, subfn);
1347 
1348 	/* submit it for processing */
1349 	xdc_submit_iorq(xdcsc, rqno, fullmode);	/* error code will be in iorq */
1350 
1351 	return rqno;
1352 }
1353 
1354 /*
1355  * xdc_startbuf
1356  * start a buffer running, assumes nfree > 0
1357  */
1358 int
1359 xdc_startbuf(struct xdc_softc *xdcsc, struct xd_softc *xdsc, struct buf *bp)
1360 {
1361 	int rqno, partno;
1362 	struct xd_iorq *iorq;
1363 	struct xd_iopb *iopb;
1364 	u_long  block;
1365 	void *dbuf;
1366 
1367 	if (xdcsc->nfree == 0)
1368 		panic("xdc_startbuf free");
1369 	rqno = XDC_RQALLOC(xdcsc);
1370 	iorq = &xdcsc->reqs[rqno];
1371 	iopb = iorq->iopb;
1372 
1373 	/* get buf */
1374 
1375 	if (bp == NULL) {
1376 		bp = bufq_get(xdcsc->sc_wq);
1377 		if (bp == NULL)
1378 			panic("%s bp", __func__);
1379 		xdsc = xdcsc->sc_drives[DISKUNIT(bp->b_dev)];
1380 	}
1381 	partno = DISKPART(bp->b_dev);
1382 #ifdef XDC_DEBUG
1383 	printf("xdc_startbuf: %s%c: %s block %d\n", device_xname(xdsc->sc_dev),
1384 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1385 	printf("%s: b_bcount %d, b_data 0x%x\n", __func__,
1386 	    bp->b_bcount, bp->b_data);
1387 #endif
1388 
1389 	/*
1390 	 * load request.  we have to calculate the correct block number based
1391 	 * on partition info.
1392 	 *
1393 	 * also, note that there are two kinds of buf structures, those with
1394 	 * B_PHYS set and those without B_PHYS.   if B_PHYS is set, then it is
1395 	 * a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
1396 	 * buffer which has already been mapped into DVMA space. (Not on sun3)
1397 	 * However, if B_PHYS is not set, then the buffer is a normal system
1398 	 * buffer which does *not* live in DVMA space.  In that case we call
1399 	 * dvma_mapin to map it into DVMA space so we can do the DMA to it.
1400 	 *
1401 	 * in cases where we do a dvma_mapin, note that iorq points to the
1402 	 * buffer as mapped into DVMA space, where as the bp->b_data points
1403 	 * to its non-DVMA mapping.
1404 	 *
1405 	 * XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
1406 	 * into dvma space, only that it was remapped into the kernel.
1407 	 * We ALWAYS have to remap the kernel buf into DVMA space.
1408 	 * (It is done inexpensively, using whole segments!)
1409 	 */
1410 
1411 	block = bp->b_blkno + ((partno == RAW_PART) ? 0 :
1412 	    xdsc->sc_dk.dk_label->d_partitions[partno].p_offset);
1413 
1414 	dbuf = dvma_mapin(bp->b_data, bp->b_bcount, 0);
1415 	if (dbuf == NULL) {	/* out of DVMA space */
1416 		printf("%s: warning: out of DVMA space\n",
1417 		    device_xname(xdcsc->sc_dev));
1418 		XDC_FREE(xdcsc, rqno);
1419 		bufq_put(xdcsc->sc_wq, bp);
1420 		return XD_ERR_FAIL;	/* XXX: need some sort of
1421 		                         * call-back scheme here? */
1422 	}
1423 
1424 	/* init iorq and load iopb from it */
1425 
1426 	xdc_rqinit(iorq, xdcsc, xdsc, XD_SUB_NORM | XD_MODE_VERBO, block,
1427 	    bp->b_bcount / XDFM_BPS, dbuf, bp);
1428 
1429 	xdc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XDCMD_RD : XDCMD_WR, 0);
1430 
1431 	/* Instrumentation. */
1432 	disk_busy(&xdsc->sc_dk);
1433 
1434 	/* now submit [note that xdc_submit_iorq can never fail on NORM reqs] */
1435 
1436 	xdc_submit_iorq(xdcsc, rqno, XD_SUB_NORM);
1437 	return XD_ERR_AOK;
1438 }
1439 
1440 
1441 /*
1442  * xdc_submit_iorq: submit an iorq for processing.  returns XD_ERR_AOK
1443  * if ok.  if it fail returns an error code.  type is XD_SUB_*.
1444  *
1445  * note: caller frees iorq in all cases except NORM
1446  *
1447  * return value:
1448  *   NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
1449  *   WAIT: XD_AOK (success), <error-code> (failed)
1450  *   POLL: <same as WAIT>
1451  *   NOQ : <same as NORM>
1452  *
1453  * there are three sources for i/o requests:
1454  * [1] xdstrategy: normal block I/O, using "struct buf" system.
1455  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1456  * [3] open/ioctl: these are I/O requests done in the context of a process,
1457  *                 and the process should block until they are done.
1458  *
1459  * software state is stored in the iorq structure.  each iorq has an
1460  * iopb structure.  the hardware understands the iopb structure.
1461  * every command must go through an iopb.  a 7053 can only handle
1462  * XDC_MAXIOPB (31) active iopbs at one time.  iopbs are allocated in
1463  * DVMA space at boot up time.  what happens if we run out of iopb's?
1464  * for i/o type [1], the buffers are queued at the "buff" layer and
1465  * picked up later by the interrupt routine.  for case [2] the
1466  * programmed i/o driver is called with a special flag that says
1467  * return when one iopb is free.  for case [3] the process can sleep
1468  * on the iorq free list until some iopbs are available.
1469  */
1470 
1471 int
1472 xdc_submit_iorq(struct xdc_softc *xdcsc, int iorqno, int type)
1473 {
1474 	u_long  iopbaddr;
1475 	struct xd_iorq *iorq = &xdcsc->reqs[iorqno];
1476 
1477 #ifdef XDC_DEBUG
1478 	printf("xdc_submit_iorq(%s, no=%d, type=%d)\n",
1479 	    device_xname(xdcsc->sc_dev), iorqno, type);
1480 #endif
1481 
1482 	/* first check and see if controller is busy */
1483 	if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
1484 #ifdef XDC_DEBUG
1485 		printf("%s: XDC not ready (ADDING)\n", __func__);
1486 #endif
1487 		if (type == XD_SUB_NOQ)
1488 			return XD_ERR_FAIL;	/* failed */
1489 		XDC_TWAIT(xdcsc, iorqno);	/* put at end of waitq */
1490 		switch (type) {
1491 		case XD_SUB_NORM:
1492 			return XD_ERR_AOK;	/* success */
1493 		case XD_SUB_WAIT:
1494 			while (iorq->iopb->done == 0) {
1495 				(void)tsleep(iorq, PRIBIO, "xdciorq", 0);
1496 			}
1497 			return iorq->errno;
1498 		case XD_SUB_POLL:
1499 			return xdc_piodriver(xdcsc, iorqno, 0);
1500 		default:
1501 			panic("%s adding", __func__);
1502 		}
1503 	}
1504 #ifdef XDC_DEBUG
1505 	{
1506 		u_char *rio = (u_char *)iorq->iopb;
1507 		int sz = sizeof(struct xd_iopb), lcv;
1508 		printf("%s: aio #%d [",
1509 		    device_xname(xdcsc->sc_dev), iorq - xdcsc->reqs);
1510 		for (lcv = 0; lcv < sz; lcv++)
1511 			printf(" %02x", rio[lcv]);
1512 		printf("]\n");
1513 	}
1514 #endif				/* XDC_DEBUG */
1515 
1516 	/* controller not busy, start command */
1517 	iopbaddr = dvma_kvtopa(iorq->iopb, xdcsc->bustype);
1518 	XDC_GO(xdcsc->xdc, iopbaddr);	/* go! */
1519 	xdcsc->nrun++;
1520 	/* command now running, wrap it up */
1521 	switch (type) {
1522 	case XD_SUB_NORM:
1523 	case XD_SUB_NOQ:
1524 		return XD_ERR_AOK;	/* success */
1525 	case XD_SUB_WAIT:
1526 		while (iorq->iopb->done == 0) {
1527 			(void)tsleep(iorq, PRIBIO, "xdciorq", 0);
1528 		}
1529 		return iorq->errno;
1530 	case XD_SUB_POLL:
1531 		return xdc_piodriver(xdcsc, iorqno, 0);
1532 	default:
1533 		panic("%s wrap up", __func__);
1534 	}
1535 	panic("%s: impossible", __func__);
1536 	return 0;	/* not reached */
1537 }
1538 
1539 
1540 /*
1541  * xdc_piodriver
1542  *
1543  * programmed i/o driver.   this function takes over the computer
1544  * and drains off all i/o requests.   it returns the status of the iorq
1545  * the caller is interesting in.   if freeone is true, then it returns
1546  * when there is a free iorq.
1547  */
1548 int
1549 xdc_piodriver(struct xdc_softc *xdcsc, int iorqno, int freeone)
1550 {
1551 	int nreset = 0;
1552 	int retval = 0;
1553 	u_long  count;
1554 	struct xdc *xdc = xdcsc->xdc;
1555 #ifdef XDC_DEBUG
1556 	printf("%s(%s, %d, freeone=%d)\n", __func__,
1557 	    device_xname(xdcsc->sc_dev), iorqno, freeone);
1558 #endif
1559 
1560 	while (xdcsc->nwait || xdcsc->nrun) {
1561 #ifdef XDC_DEBUG
1562 		printf("%s: wait=%d, run=%d\n", __func__,
1563 		    xdcsc->nwait, xdcsc->nrun);
1564 #endif
1565 		XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
1566 #ifdef XDC_DEBUG
1567 		printf("%s: done wait with count = %d\n", __func__, count);
1568 #endif
1569 		/* we expect some progress soon */
1570 		if (count == 0 && nreset >= 2) {
1571 			xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
1572 #ifdef XDC_DEBUG
1573 			printf("%s: timeout\n", __func__);
1574 #endif
1575 			return XD_ERR_FAIL;
1576 		}
1577 		if (count == 0) {
1578 			if (xdc_reset(xdcsc, 0,
1579 			    (nreset++ == 0) ? XD_RSET_NONE : iorqno,
1580 			    XD_ERR_FAIL, 0) == XD_ERR_FAIL)
1581 				return XD_ERR_FAIL;	/* flushes all but POLL
1582 							 * requests, resets */
1583 			continue;
1584 		}
1585 		xdc_remove_iorq(xdcsc);	/* could resubmit request */
1586 		if (freeone) {
1587 			if (xdcsc->nrun < XDC_MAXIOPB) {
1588 #ifdef XDC_DEBUG
1589 				printf("%s: done: one free\n", __func__);
1590 #endif
1591 				return XD_ERR_AOK;
1592 			}
1593 			continue;	/* don't xdc_start */
1594 		}
1595 		xdc_start(xdcsc, XDC_MAXIOPB);
1596 	}
1597 
1598 	/* get return value */
1599 
1600 	retval = xdcsc->reqs[iorqno].errno;
1601 
1602 #ifdef XDC_DEBUG
1603 	printf("%s: done, retval = 0x%x (%s)\n", __func__,
1604 	    xdcsc->reqs[iorqno].errno, xdc_e2str(xdcsc->reqs[iorqno].errno));
1605 #endif
1606 
1607 	/*
1608 	 * now that we've drained everything, start up any bufs that have
1609 	 * queued
1610 	 */
1611 
1612 	while (xdcsc->nfree > 0 && bufq_peek(xdcsc->sc_wq) != NULL)
1613 		if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
1614 			break;
1615 
1616 	return retval;
1617 }
1618 
1619 /*
1620  * xdc_reset: reset one drive.   NOTE: assumes xdc was just reset.
1621  * we steal iopb[0] for this, but we put it back when we are done.
1622  */
1623 void
1624 xdc_xdreset(struct xdc_softc *xdcsc, struct xd_softc *xdsc)
1625 {
1626 	struct xd_iopb tmpiopb;
1627 	u_long  addr;
1628 	int del;
1629 
1630 	memcpy(&tmpiopb, xdcsc->iopbase, sizeof(tmpiopb));
1631 	memset(xdcsc->iopbase, 0, sizeof(tmpiopb));
1632 	xdcsc->iopbase->comm = XDCMD_RST;
1633 	xdcsc->iopbase->unit = xdsc->xd_drive;
1634 	addr = (u_long)xdcsc->dvmaiopb;
1635 	XDC_GO(xdcsc->xdc, addr);	/* go! */
1636 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_REMIOPB);
1637 	if (del <= 0 || xdcsc->iopbase->errs) {
1638 		printf("%s: off-line: %s\n", device_xname(xdcsc->sc_dev),
1639 		    xdc_e2str(xdcsc->iopbase->errno));
1640 		xdcsc->xdc->xdc_csr = XDC_RESET;
1641 		XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
1642 		if (del <= 0)
1643 			panic("%s", __func__);
1644 	} else {
1645 		xdcsc->xdc->xdc_csr = XDC_CLRRIO;	/* clear RIO */
1646 	}
1647 	memcpy(xdcsc->iopbase, &tmpiopb, sizeof(tmpiopb));
1648 }
1649 
1650 
1651 /*
1652  * xdc_reset: reset everything: requests are marked as errors except
1653  * a polled request (which is resubmitted)
1654  */
1655 int
1656 xdc_reset(struct xdc_softc *xdcsc, int quiet, int blastmode, int error,
1657     struct xd_softc *xdsc)
1658 {
1659 	int del = 0, lcv, retval = XD_ERR_AOK;
1660 	int oldfree = xdcsc->nfree;
1661 	struct xd_iorq *iorq;
1662 
1663 	/* soft reset hardware */
1664 
1665 	if (quiet == 0)
1666 		printf("%s: soft reset\n", device_xname(xdcsc->sc_dev));
1667 	xdcsc->xdc->xdc_csr = XDC_RESET;
1668 	XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
1669 	if (del <= 0) {
1670 		blastmode = XD_RSET_ALL;	/* dead, flush all requests */
1671 		retval = XD_ERR_FAIL;
1672 	}
1673 	if (xdsc)
1674 		xdc_xdreset(xdcsc, xdsc);
1675 
1676 	/* fix queues based on "blast-mode" */
1677 
1678 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
1679 		iorq = &xdcsc->reqs[lcv];
1680 
1681 		if (XD_STATE(iorq->mode) != XD_SUB_POLL &&
1682 		    XD_STATE(iorq->mode) != XD_SUB_WAIT &&
1683 		    XD_STATE(iorq->mode) != XD_SUB_NORM)
1684 			/* is it active? */
1685 			continue;
1686 
1687 		xdcsc->nrun--;	/* it isn't running any more */
1688 		if (blastmode == XD_RSET_ALL || blastmode != lcv) {
1689 			/* failed */
1690 			iorq->errno = error;
1691 			xdcsc->iopbase[lcv].done = xdcsc->iopbase[lcv].errs = 1;
1692 			switch (XD_STATE(iorq->mode)) {
1693 			case XD_SUB_NORM:
1694 				iorq->buf->b_error = EIO;
1695 				iorq->buf->b_resid = iorq->sectcnt * XDFM_BPS;
1696 				/* Sun3: map/unmap regardless of B_PHYS */
1697 				dvma_mapout(iorq->dbufbase,
1698 				    iorq->buf->b_bcount);
1699 				disk_unbusy(&iorq->xd->sc_dk,
1700 				    (iorq->buf->b_bcount - iorq->buf->b_resid),
1701 				    (iorq->buf->b_flags & B_READ));
1702 				biodone(iorq->buf);
1703 				XDC_FREE(xdcsc, lcv);	/* add to free list */
1704 				break;
1705 			case XD_SUB_WAIT:
1706 				wakeup(iorq);
1707 			case XD_SUB_POLL:
1708 				xdcsc->ndone++;
1709 				iorq->mode =
1710 				    XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1711 				break;
1712 			}
1713 
1714 		} else {
1715 
1716 			/* resubmit, put at front of wait queue */
1717 			XDC_HWAIT(xdcsc, lcv);
1718 		}
1719 	}
1720 
1721 	/*
1722 	 * now, if stuff is waiting, start it.
1723 	 * since we just reset it should go
1724 	 */
1725 	xdc_start(xdcsc, XDC_MAXIOPB);
1726 
1727 	/* ok, we did it */
1728 	if (oldfree == 0 && xdcsc->nfree)
1729 		wakeup(&xdcsc->nfree);
1730 
1731 #ifdef XDC_DIAG
1732 	del = xdcsc->nwait + xdcsc->nrun + xdcsc->nfree + xdcsc->ndone;
1733 	if (del != XDC_MAXIOPB)
1734 		printf("%s: diag: xdc_reset miscount (%d should be %d)!\n",
1735 		    device_xname(xdcsc->sc_dev), del, XDC_MAXIOPB);
1736 	else
1737 		if (xdcsc->ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
1738 			printf("%s: diag: lots of done jobs (%d)\n",
1739 			    device_xname(xdcsc->sc_dev), xdcsc->ndone);
1740 #endif
1741 	printf("RESET DONE\n");
1742 	return retval;
1743 }
1744 
1745 /*
1746  * xdc_start: start all waiting buffers
1747  */
1748 void
1749 xdc_start(struct xdc_softc *xdcsc, int maxio)
1750 {
1751 	int rqno;
1752 	while (maxio && xdcsc->nwait &&
1753 		(xdcsc->xdc->xdc_csr & XDC_ADDING) == 0) {
1754 		XDC_GET_WAITER(xdcsc, rqno);	/* note: rqno is an "out"
1755 						 * param */
1756 		if (xdc_submit_iorq(xdcsc, rqno, XD_SUB_NOQ) != XD_ERR_AOK)
1757 			panic("%s", __func__);	/* should never happen */
1758 		maxio--;
1759 	}
1760 }
1761 
1762 /*
1763  * xdc_remove_iorq: remove "done" IOPB's.
1764  */
1765 int
1766 xdc_remove_iorq(struct xdc_softc *xdcsc)
1767 {
1768 	int errno, rqno, comm, errs;
1769 	struct xdc *xdc = xdcsc->xdc;
1770 	struct xd_iopb *iopb;
1771 	struct xd_iorq *iorq;
1772 	struct buf *bp;
1773 
1774 	if (xdc->xdc_csr & XDC_F_ERROR) {
1775 		/*
1776 		 * FATAL ERROR: should never happen under normal use. This
1777 		 * error is so bad, you can't even tell which IOPB is bad, so
1778 		 * we dump them all.
1779 		 */
1780 		errno = xdc->xdc_f_err;
1781 		printf("%s: fatal error 0x%02x: %s\n",
1782 		    device_xname(xdcsc->sc_dev), errno, xdc_e2str(errno));
1783 		if (xdc_reset(xdcsc, 0, XD_RSET_ALL, errno, 0) != XD_ERR_AOK) {
1784 			printf("%s: soft reset failed!\n",
1785 			    device_xname(xdcsc->sc_dev));
1786 			panic("%s: controller DEAD", __func__);
1787 		}
1788 		return XD_ERR_AOK;
1789 	}
1790 
1791 	/*
1792 	 * get iopb that is done
1793 	 *
1794 	 * hmm... I used to read the address of the done IOPB off the VME
1795 	 * registers and calculate the rqno directly from that.   that worked
1796 	 * until I started putting a load on the controller.   when loaded, i
1797 	 * would get interrupts but neither the REMIOPB or F_ERROR bits would
1798 	 * be set, even after DELAY'ing a while!   later on the timeout
1799 	 * routine would detect IOPBs that were marked "running" but their
1800 	 * "done" bit was set.   rather than dealing directly with this
1801 	 * problem, it is just easier to look at all running IOPB's for the
1802 	 * done bit.
1803 	 */
1804 	if (xdc->xdc_csr & XDC_REMIOPB) {
1805 		xdc->xdc_csr = XDC_CLRRIO;
1806 	}
1807 
1808 	for (rqno = 0; rqno < XDC_MAXIOPB; rqno++) {
1809 		iorq = &xdcsc->reqs[rqno];
1810 		if (iorq->mode == 0 || XD_STATE(iorq->mode) == XD_SUB_DONE)
1811 			continue;	/* free, or done */
1812 		iopb = &xdcsc->iopbase[rqno];
1813 		if (iopb->done == 0)
1814 			continue;	/* not done yet */
1815 
1816 #ifdef XDC_DEBUG
1817 		{
1818 			u_char *rio = (u_char *)iopb;
1819 			int sz = sizeof(struct xd_iopb), lcv;
1820 
1821 			printf("%s: rio #%d [",
1822 			    device_xname(xdcsc->sc_dev), rqno);
1823 			for (lcv = 0; lcv < sz; lcv++)
1824 				printf(" %02x", rio[lcv]);
1825 			printf("]\n");
1826 		}
1827 #endif				/* XDC_DEBUG */
1828 
1829 		xdcsc->nrun--;
1830 
1831 		comm = iopb->comm;
1832 		errs = iopb->errs;
1833 
1834 		if (errs)
1835 			iorq->errno = iopb->errno;
1836 		else
1837 			iorq->errno = 0;
1838 
1839 		/* handle non-fatal errors */
1840 
1841 		if (errs &&
1842 		    xdc_error(xdcsc, iorq, iopb, rqno, comm) == XD_ERR_AOK)
1843 			continue;	/* AOK: we resubmitted it */
1844 
1845 
1846 		/* this iorq is now done (hasn't been restarted or anything) */
1847 
1848 		if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
1849 			xdc_perror(iorq, iopb, 0);
1850 
1851 		/* now, if read/write check to make sure we got all the data
1852 		 * we needed. (this may not be the case if we got an error in
1853 		 * the middle of a multisector request).   */
1854 
1855 		if ((iorq->mode & XD_MODE_B144) != 0 && errs == 0 &&
1856 		    (comm == XDCMD_RD || comm == XDCMD_WR)) {
1857 			/* we just successfully processed a bad144 sector
1858 			 * note: if we are in bad 144 mode, the pointers have
1859 			 * been advanced already (see above) and are pointing
1860 			 * at the bad144 sector.   to exit bad144 mode, we
1861 			 * must advance the pointers 1 sector and issue a new
1862 			 * request if there are still sectors left to process
1863 			 *
1864 			 */
1865 			XDC_ADVANCE(iorq, 1);	/* advance 1 sector */
1866 
1867 			/* exit b144 mode */
1868 			iorq->mode = iorq->mode & (~XD_MODE_B144);
1869 
1870 			if (iorq->sectcnt) {	/* more to go! */
1871 				iorq->lasterror = iorq->errno = iopb->errno = 0;
1872 				iopb->errs = iopb->done = 0;
1873 				iorq->tries = 0;
1874 				iopb->sectcnt = iorq->sectcnt;
1875 				iopb->cylno =
1876 				    iorq->blockno / iorq->xd->sectpercyl;
1877 				iopb->headno =
1878 				    (iorq->blockno / iorq->xd->nhead) %
1879 				    iorq->xd->nhead;
1880 				iopb->sectno = iorq->blockno % XDFM_BPS;
1881 				iopb->daddr =
1882 				    dvma_kvtopa(iorq->dbuf, xdcsc->bustype);
1883 				XDC_HWAIT(xdcsc, rqno);
1884 				xdc_start(xdcsc, 1);	/* resubmit */
1885 				continue;
1886 			}
1887 		}
1888 		/* final cleanup, totally done with this request */
1889 
1890 		switch (XD_STATE(iorq->mode)) {
1891 		case XD_SUB_NORM:
1892 			bp = iorq->buf;
1893 			if (errs) {
1894 				bp->b_error = EIO;
1895 				bp->b_resid = iorq->sectcnt * XDFM_BPS;
1896 			} else {
1897 				bp->b_resid = 0;	/* done */
1898 			}
1899 			/* Sun3: map/unmap regardless of B_PHYS */
1900 			dvma_mapout(iorq->dbufbase, iorq->buf->b_bcount);
1901 			disk_unbusy(&iorq->xd->sc_dk,
1902 			    (bp->b_bcount - bp->b_resid),
1903 			    (bp->b_flags & B_READ));
1904 			XDC_FREE(xdcsc, rqno);
1905 			biodone(bp);
1906 			break;
1907 		case XD_SUB_WAIT:
1908 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1909 			xdcsc->ndone++;
1910 			wakeup(iorq);
1911 			break;
1912 		case XD_SUB_POLL:
1913 			iorq->mode = XD_NEWSTATE(iorq->mode, XD_SUB_DONE);
1914 			xdcsc->ndone++;
1915 			break;
1916 		}
1917 	}
1918 
1919 	return XD_ERR_AOK;
1920 }
1921 
1922 /*
1923  * xdc_perror: print error.
1924  * - if still_trying is true: we got an error, retried and got a
1925  *   different error.  in that case lasterror is the old error,
1926  *   and errno is the new one.
1927  * - if still_trying is not true, then if we ever had an error it
1928  *   is in lasterror. also, if iorq->errno == 0, then we recovered
1929  *   from that error (otherwise iorq->errno == iorq->lasterror).
1930  */
1931 void
1932 xdc_perror(struct xd_iorq *iorq, struct xd_iopb *iopb, int still_trying)
1933 {
1934 	int error = iorq->lasterror;
1935 
1936 	printf("%s", (iorq->xd) ?
1937 	    device_xname(iorq->xd->sc_dev) :
1938 	    device_xname(iorq->xdc->sc_dev));
1939 	if (iorq->buf)
1940 		printf("%c: ", 'a' + (char)DISKPART(iorq->buf->b_dev));
1941 	if (iopb->comm == XDCMD_RD || iopb->comm == XDCMD_WR)
1942 		printf("%s %d/%d/%d: ",
1943 		    (iopb->comm == XDCMD_RD) ? "read" : "write",
1944 		    iopb->cylno, iopb->headno, iopb->sectno);
1945 	printf("%s", xdc_e2str(error));
1946 
1947 	if (still_trying)
1948 		printf(" [still trying, new error=%s]", xdc_e2str(iorq->errno));
1949 	else
1950 		if (iorq->errno == 0)
1951 			printf(" [recovered in %d tries]", iorq->tries);
1952 
1953 	printf("\n");
1954 }
1955 
1956 /*
1957  * xdc_error: non-fatal error encountered... recover.
1958  * return AOK if resubmitted, return FAIL if this iopb is done
1959  */
1960 int
1961 xdc_error(struct xdc_softc *xdcsc, struct xd_iorq *iorq, struct xd_iopb *iopb,
1962     int rqno, int comm)
1963 
1964 {
1965 	int errno = iorq->errno;
1966 	int erract = errno & XD_ERA_MASK;
1967 	int oldmode, advance, i;
1968 
1969 	if (erract == XD_ERA_RSET) {	/* some errors require a reset */
1970 		oldmode = iorq->mode;
1971 		iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
1972 		xdcsc->ndone++;
1973 		/* make xdc_start ignore us */
1974 		xdc_reset(xdcsc, 1, XD_RSET_NONE, errno, iorq->xd);
1975 		iorq->mode = oldmode;
1976 		xdcsc->ndone--;
1977 	}
1978 	/* check for read/write to a sector in bad144 table if bad: redirect
1979 	 * request to bad144 area */
1980 
1981 	if ((comm == XDCMD_RD || comm == XDCMD_WR) &&
1982 	    (iorq->mode & XD_MODE_B144) == 0) {
1983 		advance = iorq->sectcnt - iopb->sectcnt;
1984 		XDC_ADVANCE(iorq, advance);
1985 		if ((i = isbad(&iorq->xd->dkb,
1986 		    iorq->blockno / iorq->xd->sectpercyl,
1987 		    (iorq->blockno / iorq->xd->nsect) % iorq->xd->nhead,
1988 		    iorq->blockno % iorq->xd->nsect)) != -1) {
1989 			iorq->mode |= XD_MODE_B144;	/* enter bad144 mode &
1990 							 * redirect */
1991 			iopb->errno = iopb->done = iopb->errs = 0;
1992 			iopb->sectcnt = 1;
1993 			iopb->cylno = (iorq->xd->ncyl + iorq->xd->acyl) - 2;
1994 			/* second to last acyl */
1995 			i = iorq->xd->sectpercyl - 1 - i;	/* follow bad144
1996 								 * standard */
1997 			iopb->headno = i / iorq->xd->nhead;
1998 			iopb->sectno = i % iorq->xd->nhead;
1999 			XDC_HWAIT(xdcsc, rqno);
2000 			xdc_start(xdcsc, 1);	/* resubmit */
2001 			return XD_ERR_AOK;	/* recovered! */
2002 		}
2003 	}
2004 
2005 	/*
2006 	 * it isn't a bad144 sector, must be real error! see if we can retry
2007 	 * it?
2008 	 */
2009 	if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
2010 		xdc_perror(iorq, iopb, 1);	/* inform of error state
2011 						 * change */
2012 	iorq->lasterror = errno;
2013 
2014 	if ((erract == XD_ERA_RSET || erract == XD_ERA_HARD)
2015 	    && iorq->tries < XDC_MAXTRIES) {	/* retry? */
2016 		iorq->tries++;
2017 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
2018 		XDC_HWAIT(xdcsc, rqno);
2019 		xdc_start(xdcsc, 1);	/* restart */
2020 		return XD_ERR_AOK;	/* recovered! */
2021 	}
2022 
2023 	/* failed to recover from this error */
2024 	return XD_ERR_FAIL;
2025 }
2026 
2027 /*
2028  * xdc_tick: make sure xd is still alive and ticking (err, kicking).
2029  */
2030 void
2031 xdc_tick(void *arg)
2032 {
2033 	struct xdc_softc *xdcsc = arg;
2034 	int     lcv, s, reset = 0;
2035 #ifdef XDC_DIAG
2036 	int     nwait, nrun, nfree, ndone, whd = 0;
2037 	uint8_t  fqc[XDC_MAXIOPB], wqc[XDC_MAXIOPB], mark[XDC_MAXIOPB];
2038 	s = splbio();
2039 	nwait = xdcsc->nwait;
2040 	nrun = xdcsc->nrun;
2041 	nfree = xdcsc->nfree;
2042 	ndone = xdcsc->ndone;
2043 	memcpy(wqc, xdcsc->waitq, sizeof(wqc));
2044 	memcpy(fqc, xdcsc->freereq, sizeof(fqc));
2045 	splx(s);
2046 	if (nwait + nrun + nfree + ndone != XDC_MAXIOPB) {
2047 		printf("%s: diag: IOPB miscount "
2048 		    "(got w/f/r/d %d/%d/%d/%d, wanted %d)\n",
2049 		    device_xname(xdcsc->sc_dev), nwait, nfree, nrun, ndone,
2050 		    XDC_MAXIOPB);
2051 		memset(mark, 0, sizeof(mark));
2052 		printf("FREE: ");
2053 		for (lcv = nfree; lcv > 0; lcv--) {
2054 			printf("%d ", fqc[lcv - 1]);
2055 			mark[fqc[lcv - 1]] = 1;
2056 		}
2057 		printf("\nWAIT: ");
2058 		lcv = nwait;
2059 		while (lcv > 0) {
2060 			printf("%d ", wqc[whd]);
2061 			mark[wqc[whd]] = 1;
2062 			whd = (whd + 1) % XDC_MAXIOPB;
2063 			lcv--;
2064 		}
2065 		printf("\n");
2066 		for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2067 			if (mark[lcv] == 0) {
2068 				printf("MARK: running %d: mode %d done %d "
2069 				    "errs %d errno 0x%x ttl %d buf %p\n",
2070 				    lcv, xdcsc->reqs[lcv].mode,
2071 				    xdcsc->iopbase[lcv].done,
2072 				    xdcsc->iopbase[lcv].errs,
2073 				    xdcsc->iopbase[lcv].errno,
2074 				    xdcsc->reqs[lcv].ttl,
2075 				    xdcsc->reqs[lcv].buf);
2076 			}
2077 		}
2078 	} else
2079 		if (ndone > XDC_MAXIOPB - XDC_SUBWAITLIM)
2080 			printf("%s: diag: lots of done jobs (%d)\n",
2081 			    device_xname(xdcsc->sc_dev), ndone);
2082 
2083 #endif
2084 #ifdef XDC_DEBUG
2085 	printf("%s: tick: csr 0x%x, w/f/r/d %d/%d/%d/%d\n",
2086 	    device_xname(xdcsc->sc_dev),
2087 	    xdcsc->xdc->xdc_csr, xdcsc->nwait, xdcsc->nfree, xdcsc->nrun,
2088 	    xdcsc->ndone);
2089 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2090 		if (xdcsc->reqs[lcv].mode) {
2091 			printf("running %d: "
2092 			    "mode %d done %d errs %d errno 0x%x\n", lcv,
2093 			    xdcsc->reqs[lcv].mode, xdcsc->iopbase[lcv].done,
2094 			    xdcsc->iopbase[lcv].errs,
2095 			    xdcsc->iopbase[lcv].errno);
2096 		}
2097 	}
2098 #endif
2099 
2100 	/* reduce ttl for each request if one goes to zero, reset xdc */
2101 	s = splbio();
2102 	for (lcv = 0; lcv < XDC_MAXIOPB; lcv++) {
2103 		if (xdcsc->reqs[lcv].mode == 0 ||
2104 		    XD_STATE(xdcsc->reqs[lcv].mode) == XD_SUB_DONE)
2105 			continue;
2106 		xdcsc->reqs[lcv].ttl--;
2107 		if (xdcsc->reqs[lcv].ttl == 0)
2108 			reset = 1;
2109 	}
2110 	if (reset) {
2111 		printf("%s: watchdog timeout\n", device_xname(xdcsc->sc_dev));
2112 		xdc_reset(xdcsc, 0, XD_RSET_NONE, XD_ERR_FAIL, NULL);
2113 	}
2114 	splx(s);
2115 
2116 	/* until next time */
2117 
2118 	callout_reset(&xdcsc->sc_tick_ch, XDC_TICKCNT, xdc_tick, xdcsc);
2119 }
2120 
2121 /*
2122  * xdc_ioctlcmd: this function provides a user level interface to the
2123  * controller via ioctl.   this allows "format" programs to be written
2124  * in user code, and is also useful for some debugging.   we return
2125  * an error code.   called at user priority.
2126  */
2127 int
2128 xdc_ioctlcmd(struct xd_softc *xd, dev_t dev, struct xd_iocmd *xio)
2129 {
2130 	int s, err, rqno;
2131 	void *dvmabuf = NULL;
2132 	struct xdc_softc *xdcsc;
2133 
2134 	/* check sanity of requested command */
2135 
2136 	switch (xio->cmd) {
2137 
2138 	case XDCMD_NOP:	/* no op: everything should be zero */
2139 		if (xio->subfn || xio->dptr || xio->dlen ||
2140 		    xio->block || xio->sectcnt)
2141 			return EINVAL;
2142 		break;
2143 
2144 	case XDCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
2145 	case XDCMD_WR:
2146 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2147 		    xio->sectcnt * XDFM_BPS != xio->dlen || xio->dptr == NULL)
2148 			return EINVAL;
2149 		break;
2150 
2151 	case XDCMD_SK:		/* seek: doesn't seem useful to export this */
2152 		return EINVAL;
2153 
2154 	case XDCMD_WRP:	/* write parameters */
2155 		return EINVAL;	/* not useful, except maybe drive
2156 				 * parameters... but drive parameters should
2157 				 * go via disklabel changes */
2158 
2159 	case XDCMD_RDP:	/* read parameters */
2160 		if (xio->subfn != XDFUN_DRV ||
2161 		    xio->dlen || xio->block || xio->dptr)
2162 			return EINVAL;		/* allow read drive params to
2163 						 * get hw_spt */
2164 		xio->sectcnt = xd->hw_spt;	/* we already know the answer */
2165 		return 0;
2166 		break;
2167 
2168 	case XDCMD_XRD:	/* extended read/write */
2169 	case XDCMD_XWR:
2170 
2171 		switch (xio->subfn) {
2172 
2173 		case XDFUN_THD:/* track headers */
2174 			if (xio->sectcnt != xd->hw_spt ||
2175 			    (xio->block % xd->nsect) != 0 ||
2176 			    xio->dlen != XD_IOCMD_HSZ * xd->hw_spt ||
2177 			    xio->dptr == NULL)
2178 				return EINVAL;
2179 			xio->sectcnt = 0;
2180 			break;
2181 
2182 		case XDFUN_FMT:/* NOTE: also XDFUN_VFY */
2183 			if (xio->cmd == XDCMD_XRD)
2184 				return EINVAL;	/* no XDFUN_VFY */
2185 			if (xio->sectcnt || xio->dlen ||
2186 			    (xio->block % xd->nsect) != 0 || xio->dptr)
2187 				return EINVAL;
2188 			break;
2189 
2190 		case XDFUN_HDR:/* header, header verify, data, data ECC */
2191 			return EINVAL;	/* not yet */
2192 
2193 		case XDFUN_DM:	/* defect map */
2194 		case XDFUN_DMX:/* defect map (alternate location) */
2195 			if (xio->sectcnt || xio->dlen != XD_IOCMD_DMSZ ||
2196 			    (xio->block % xd->nsect) != 0 || xio->dptr == NULL)
2197 				return EINVAL;
2198 			break;
2199 
2200 		default:
2201 			return EINVAL;
2202 		}
2203 		break;
2204 
2205 	case XDCMD_TST:	/* diagnostics */
2206 		return EINVAL;
2207 
2208 	default:
2209 		return EINVAL;/* ??? */
2210 	}
2211 
2212 	/* create DVMA buffer for request if needed */
2213 
2214 	if (xio->dlen) {
2215 		dvmabuf = dvma_malloc(xio->dlen);
2216 		if (xio->cmd == XDCMD_WR || xio->cmd == XDCMD_XWR) {
2217 			err = copyin(xio->dptr, dvmabuf, xio->dlen);
2218 			if (err) {
2219 				dvma_free(dvmabuf, xio->dlen);
2220 				return err;
2221 			}
2222 		}
2223 	}
2224 	/* do it! */
2225 
2226 	err = 0;
2227 	xdcsc = xd->parent;
2228 	s = splbio();
2229 	rqno = xdc_cmd(xdcsc, xio->cmd, xio->subfn, xd->xd_drive, xio->block,
2230 	    xio->sectcnt, dvmabuf, XD_SUB_WAIT);
2231 	if (rqno == XD_ERR_FAIL) {
2232 		err = EIO;
2233 		goto done;
2234 	}
2235 	xio->errno = xdcsc->reqs[rqno].errno;
2236 	xio->tries = xdcsc->reqs[rqno].tries;
2237 	XDC_DONE(xdcsc, rqno, err);
2238 
2239 	if (xio->cmd == XDCMD_RD || xio->cmd == XDCMD_XRD)
2240 		err = copyout(dvmabuf, xio->dptr, xio->dlen);
2241 
2242  done:
2243 	splx(s);
2244 	if (dvmabuf)
2245 		dvma_free(dvmabuf, xio->dlen);
2246 	return err;
2247 }
2248 
2249 /*
2250  * xdc_e2str: convert error code number into an error string
2251  */
2252 const char *
2253 xdc_e2str(int no)
2254 {
2255 
2256 	switch (no) {
2257 	case XD_ERR_FAIL:
2258 		return "Software fatal error";
2259 	case XD_ERR_AOK:
2260 		return "Successful completion";
2261 	case XD_ERR_ICYL:
2262 		return "Illegal cylinder address";
2263 	case XD_ERR_IHD:
2264 		return "Illegal head address";
2265 	case XD_ERR_ISEC:
2266 		return "Illgal sector address";
2267 	case XD_ERR_CZER:
2268 		return "Count zero";
2269 	case XD_ERR_UIMP:
2270 		return "Unimplemented command";
2271 	case XD_ERR_IF1:
2272 		return "Illegal field length 1";
2273 	case XD_ERR_IF2:
2274 		return "Illegal field length 2";
2275 	case XD_ERR_IF3:
2276 		return "Illegal field length 3";
2277 	case XD_ERR_IF4:
2278 		return "Illegal field length 4";
2279 	case XD_ERR_IF5:
2280 		return "Illegal field length 5";
2281 	case XD_ERR_IF6:
2282 		return "Illegal field length 6";
2283 	case XD_ERR_IF7:
2284 		return "Illegal field length 7";
2285 	case XD_ERR_ISG:
2286 		return "Illegal scatter/gather length";
2287 	case XD_ERR_ISPT:
2288 		return "Not enough sectors per track";
2289 	case XD_ERR_ALGN:
2290 		return "Next IOPB address alignment error";
2291 	case XD_ERR_SGAL:
2292 		return "Scatter/gather address alignment error";
2293 	case XD_ERR_SGEC:
2294 		return "Scatter/gather with auto-ECC";
2295 	case XD_ERR_SECC:
2296 		return "Soft ECC corrected";
2297 	case XD_ERR_SIGN:
2298 		return "ECC ignored";
2299 	case XD_ERR_ASEK:
2300 		return "Auto-seek retry recovered";
2301 	case XD_ERR_RTRY:
2302 		return "Soft retry recovered";
2303 	case XD_ERR_HECC:
2304 		return "Hard data ECC";
2305 	case XD_ERR_NHDR:
2306 		return "Header not found";
2307 	case XD_ERR_NRDY:
2308 		return "Drive not ready";
2309 	case XD_ERR_TOUT:
2310 		return "Operation timeout";
2311 	case XD_ERR_VTIM:
2312 		return "VMEDMA timeout";
2313 	case XD_ERR_DSEQ:
2314 		return "Disk sequencer error";
2315 	case XD_ERR_HDEC:
2316 		return "Header ECC error";
2317 	case XD_ERR_RVFY:
2318 		return "Read verify";
2319 	case XD_ERR_VFER:
2320 		return "Fatail VMEDMA error";
2321 	case XD_ERR_VBUS:
2322 		return "VMEbus error";
2323 	case XD_ERR_DFLT:
2324 		return "Drive faulted";
2325 	case XD_ERR_HECY:
2326 		return "Header error/cyliner";
2327 	case XD_ERR_HEHD:
2328 		return "Header error/head";
2329 	case XD_ERR_NOCY:
2330 		return "Drive not on-cylinder";
2331 	case XD_ERR_SEEK:
2332 		return "Seek error";
2333 	case XD_ERR_ILSS:
2334 		return "Illegal sector size";
2335 	case XD_ERR_SEC:
2336 		return "Soft ECC";
2337 	case XD_ERR_WPER:
2338 		return "Write-protect error";
2339 	case XD_ERR_IRAM:
2340 		return "IRAM self test failure";
2341 	case XD_ERR_MT3:
2342 		return "Maintenance test 3 failure (DSKCEL RAM)";
2343 	case XD_ERR_MT4:
2344 		return "Maintenance test 4 failure (header shift reg)";
2345 	case XD_ERR_MT5:
2346 		return "Maintenance test 5 failure (VMEDMA regs)";
2347 	case XD_ERR_MT6:
2348 		return "Maintenance test 6 failure (REGCEL chip)";
2349 	case XD_ERR_MT7:
2350 		return "Maintenance test 7 failure (buffer parity)";
2351 	case XD_ERR_MT8:
2352 		return "Maintenance test 8 failure (disk FIFO)";
2353 	case XD_ERR_IOCK:
2354 		return "IOPB checksum miscompare";
2355 	case XD_ERR_IODM:
2356 		return "IOPB DMA fatal";
2357 	case XD_ERR_IOAL:
2358 		return "IOPB address alignment error";
2359 	case XD_ERR_FIRM:
2360 		return "Firmware error";
2361 	case XD_ERR_MMOD:
2362 		return "Illegal maintenance mode test number";
2363 	case XD_ERR_ACFL:
2364 		return "ACFAIL asserted";
2365 	default:
2366 		return "Unknown error";
2367 	}
2368 }
2369