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