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