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