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