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