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