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