xref: /netbsd-src/sys/dev/vme/xy.c (revision 4b896b232495b7a9b8b94a1cf1e21873296d53b8)
1 /*	$NetBSD: xy.c,v 1.55 2003/12/23 13:15:18 pk Exp $	*/
2 
3 /*
4  *
5  * Copyright (c) 1995 Charles D. Cranor
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Charles D. Cranor.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  *
36  * x y . c   x y l o g i c s   4 5 0 / 4 5 1   s m d   d r i v e r
37  *
38  * author: Chuck Cranor <chuck@ccrc.wustl.edu>
39  * started: 14-Sep-95
40  * references: [1] Xylogics Model 753 User's Manual
41  *                 part number: 166-753-001, Revision B, May 21, 1988.
42  *                 "Your Partner For Performance"
43  *             [2] other NetBSD disk device drivers
44  *	       [3] Xylogics Model 450 User's Manual
45  *		   part number: 166-017-001, Revision B, 1983.
46  *	       [4] Addendum to Xylogics Model 450 Disk Controller User's
47  *			Manual, Jan. 1985.
48  *	       [5] The 451 Controller, Rev. B3, September 2, 1986.
49  *	       [6] David Jones <dej@achilles.net>'s unfinished 450/451 driver
50  *
51  */
52 
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: xy.c,v 1.55 2003/12/23 13:15:18 pk Exp $");
55 
56 #undef XYC_DEBUG		/* full debug */
57 #undef XYC_DIAG			/* extra sanity checks */
58 #if defined(DIAGNOSTIC) && !defined(XYC_DIAG)
59 #define XYC_DIAG		/* link in with master DIAG option */
60 #endif
61 
62 #include <sys/param.h>
63 #include <sys/proc.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/file.h>
67 #include <sys/stat.h>
68 #include <sys/ioctl.h>
69 #include <sys/buf.h>
70 #include <sys/uio.h>
71 #include <sys/malloc.h>
72 #include <sys/device.h>
73 #include <sys/disklabel.h>
74 #include <sys/disk.h>
75 #include <sys/syslog.h>
76 #include <sys/dkbad.h>
77 #include <sys/conf.h>
78 
79 #include <machine/bus.h>
80 #include <machine/intr.h>
81 
82 #if defined(__sparc__) || defined(sun3)
83 #include <dev/sun/disklabel.h>
84 #endif
85 
86 #include <dev/vme/vmereg.h>
87 #include <dev/vme/vmevar.h>
88 
89 #include <dev/vme/xyreg.h>
90 #include <dev/vme/xyvar.h>
91 #include <dev/vme/xio.h>
92 
93 #include "locators.h"
94 
95 /*
96  * macros
97  */
98 
99 /*
100  * XYC_GO: start iopb ADDR (DVMA addr in a u_long) on XYC
101  */
102 #define XYC_GO(XYC, ADDR) { \
103 	(XYC)->xyc_addr_lo = ((ADDR) & 0xff); \
104 	(ADDR) = ((ADDR) >> 8); \
105 	(XYC)->xyc_addr_hi = ((ADDR) & 0xff); \
106 	(ADDR) = ((ADDR) >> 8); \
107 	(XYC)->xyc_reloc_lo = ((ADDR) & 0xff); \
108 	(ADDR) = ((ADDR) >> 8); \
109 	(XYC)->xyc_reloc_hi = (ADDR); \
110 	(XYC)->xyc_csr = XYC_GBSY; /* go! */ \
111 }
112 
113 /*
114  * XYC_DONE: don't need IORQ, get error code and free (done after xyc_cmd)
115  */
116 
117 #define XYC_DONE(SC,ER) { \
118 	if ((ER) == XY_ERR_AOK) { \
119 		(ER) = (SC)->ciorq->errno; \
120 		(SC)->ciorq->mode = XY_SUB_FREE; \
121 		wakeup((SC)->ciorq); \
122 	} \
123 	}
124 
125 /*
126  * XYC_ADVANCE: advance iorq's pointers by a number of sectors
127  */
128 
129 #define XYC_ADVANCE(IORQ, N) { \
130 	if (N) { \
131 		(IORQ)->sectcnt -= (N); \
132 		(IORQ)->blockno += (N); \
133 		(IORQ)->dbuf += ((N)*XYFM_BPS); \
134 	} \
135 }
136 
137 /*
138  * note - addresses you can sleep on:
139  *   [1] & of xy_softc's "state" (waiting for a chance to attach a drive)
140  *   [2] & an iorq (waiting for an XY_SUB_WAIT iorq to finish)
141  */
142 
143 
144 /*
145  * function prototypes
146  * "xyc_*" functions are internal, all others are external interfaces
147  */
148 
149 extern int pil_to_vme[];	/* from obio.c */
150 
151 /* internals */
152 struct xy_iopb *xyc_chain __P((struct xyc_softc *, struct xy_iorq *));
153 int	xyc_cmd __P((struct xyc_softc *, int, int, int, int, int, char *, int));
154 char   *xyc_e2str __P((int));
155 int	xyc_entoact __P((int));
156 int	xyc_error __P((struct xyc_softc *, struct xy_iorq *,
157 		   struct xy_iopb *, int));
158 int	xyc_ioctlcmd __P((struct xy_softc *, dev_t dev, struct xd_iocmd *));
159 void	xyc_perror __P((struct xy_iorq *, struct xy_iopb *, int));
160 int	xyc_piodriver __P((struct xyc_softc *, struct xy_iorq *));
161 int	xyc_remove_iorq __P((struct xyc_softc *));
162 int	xyc_reset __P((struct xyc_softc *, int, struct xy_iorq *, int,
163 			struct xy_softc *));
164 inline void xyc_rqinit __P((struct xy_iorq *, struct xyc_softc *,
165 			    struct xy_softc *, int, u_long, int,
166 			    caddr_t, struct buf *));
167 void	xyc_rqtopb __P((struct xy_iorq *, struct xy_iopb *, int, int));
168 void	xyc_start __P((struct xyc_softc *, struct xy_iorq *));
169 int	xyc_startbuf __P((struct xyc_softc *, struct xy_softc *, struct buf *));
170 int	xyc_submit_iorq __P((struct xyc_softc *, struct xy_iorq *, int));
171 void	xyc_tick __P((void *));
172 int	xyc_unbusy __P((struct xyc *, int));
173 void	xyc_xyreset __P((struct xyc_softc *, struct xy_softc *));
174 int	xy_dmamem_alloc(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
175 			int *, bus_size_t, caddr_t *, bus_addr_t *);
176 void	xy_dmamem_free(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
177 			int, bus_size_t, caddr_t);
178 
179 /* machine interrupt hook */
180 int	xycintr __P((void *));
181 
182 /* autoconf */
183 int	xycmatch __P((struct device *, struct cfdata *, void *));
184 void	xycattach __P((struct device *, struct device *, void *));
185 int	xymatch __P((struct device *, struct cfdata *, void *));
186 void	xyattach __P((struct device *, struct device *, void *));
187 static	int xyc_probe __P((void *, bus_space_tag_t, bus_space_handle_t));
188 
189 static	void xydummystrat __P((struct buf *));
190 int	xygetdisklabel __P((struct xy_softc *, void *));
191 
192 /*
193  * cfattach's: device driver interface to autoconfig
194  */
195 
196 CFATTACH_DECL(xyc, sizeof(struct xyc_softc),
197     xycmatch, xycattach, NULL, NULL);
198 
199 CFATTACH_DECL(xy, sizeof(struct xy_softc),
200     xymatch, xyattach, NULL, NULL);
201 
202 extern struct cfdriver xy_cd;
203 
204 dev_type_open(xyopen);
205 dev_type_close(xyclose);
206 dev_type_read(xyread);
207 dev_type_write(xywrite);
208 dev_type_ioctl(xyioctl);
209 dev_type_strategy(xystrategy);
210 dev_type_dump(xydump);
211 dev_type_size(xysize);
212 
213 const struct bdevsw xy_bdevsw = {
214 	xyopen, xyclose, xystrategy, xyioctl, xydump, xysize, D_DISK
215 };
216 
217 const struct cdevsw xy_cdevsw = {
218 	xyopen, xyclose, xyread, xywrite, xyioctl,
219 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
220 };
221 
222 struct xyc_attach_args {	/* this is the "aux" args to xyattach */
223 	int	driveno;	/* unit number */
224 	int	fullmode;	/* submit mode */
225 	int	booting;	/* are we booting or not? */
226 };
227 
228 /*
229  * dkdriver
230  */
231 
232 struct dkdriver xydkdriver = { xystrategy };
233 
234 /*
235  * start: disk label fix code (XXX)
236  */
237 
238 static void *xy_labeldata;
239 
240 static void
241 xydummystrat(bp)
242 	struct buf *bp;
243 {
244 	if (bp->b_bcount != XYFM_BPS)
245 		panic("xydummystrat");
246 	bcopy(xy_labeldata, bp->b_data, XYFM_BPS);
247 	bp->b_flags |= B_DONE;
248 	bp->b_flags &= ~B_BUSY;
249 }
250 
251 int
252 xygetdisklabel(xy, b)
253 	struct xy_softc *xy;
254 	void *b;
255 {
256 	const char *err;
257 #if defined(__sparc__) || defined(sun3)
258 	struct sun_disklabel *sdl;
259 #endif
260 
261 	/* We already have the label data in `b'; setup for dummy strategy */
262 	xy_labeldata = b;
263 
264 	/* Required parameter for readdisklabel() */
265 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
266 
267 	err = readdisklabel(MAKEDISKDEV(0, xy->sc_dev.dv_unit, RAW_PART),
268 					xydummystrat,
269 				xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
270 	if (err) {
271 		printf("%s: %s\n", xy->sc_dev.dv_xname, err);
272 		return(XY_ERR_FAIL);
273 	}
274 
275 #if defined(__sparc__) || defined(sun3)
276 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
277 	sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
278 	if (sdl->sl_magic == SUN_DKMAGIC) {
279 		xy->pcyl = sdl->sl_pcylinders;
280 	} else
281 #endif
282 	{
283 		printf("%s: WARNING: no `pcyl' in disk label.\n",
284 			xy->sc_dev.dv_xname);
285 		xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
286 			xy->sc_dk.dk_label->d_acylinders;
287 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
288 		xy->sc_dev.dv_xname, xy->pcyl);
289 	}
290 
291 	xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
292 	xy->acyl = xy->sc_dk.dk_label->d_acylinders;
293 	xy->nhead = xy->sc_dk.dk_label->d_ntracks;
294 	xy->nsect = xy->sc_dk.dk_label->d_nsectors;
295 	xy->sectpercyl = xy->nhead * xy->nsect;
296 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
297                                           	  * sun->bsd */
298 	return(XY_ERR_AOK);
299 }
300 
301 /*
302  * end: disk label fix code (XXX)
303  */
304 
305 /*
306  * Shorthand for allocating, mapping and loading a DMA buffer
307  */
308 int
309 xy_dmamem_alloc(tag, map, seg, nsegp, len, kvap, dmap)
310 	bus_dma_tag_t		tag;
311 	bus_dmamap_t		map;
312 	bus_dma_segment_t	*seg;
313 	int			*nsegp;
314 	bus_size_t		len;
315 	caddr_t			*kvap;
316 	bus_addr_t		*dmap;
317 {
318 	int nseg;
319 	int error;
320 
321 	if ((error = bus_dmamem_alloc(tag, len, 0, 0,
322 				      seg, 1, &nseg, BUS_DMA_NOWAIT)) != 0) {
323 		return (error);
324 	}
325 
326 	if ((error = bus_dmamem_map(tag, seg, nseg,
327 				    len, kvap,
328 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
329 		bus_dmamem_free(tag, seg, nseg);
330 		return (error);
331 	}
332 
333 	if ((error = bus_dmamap_load(tag, map, *kvap, len, NULL,
334 				     BUS_DMA_NOWAIT)) != 0) {
335 		bus_dmamem_unmap(tag, *kvap, len);
336 		bus_dmamem_free(tag, seg, nseg);
337 		return (error);
338 	}
339 
340 	*dmap = map->dm_segs[0].ds_addr;
341 	*nsegp = nseg;
342 	return (0);
343 }
344 
345 void
346 xy_dmamem_free(tag, map, seg, nseg, len, kva)
347 	bus_dma_tag_t		tag;
348 	bus_dmamap_t		map;
349 	bus_dma_segment_t	*seg;
350 	int			nseg;
351 	bus_size_t		len;
352 	caddr_t			kva;
353 {
354 
355 	bus_dmamap_unload(tag, map);
356 	bus_dmamem_unmap(tag, kva, len);
357 	bus_dmamem_free(tag, seg, nseg);
358 }
359 
360 
361 /*
362  * a u t o c o n f i g   f u n c t i o n s
363  */
364 
365 /*
366  * xycmatch: determine if xyc is present or not.   we do a
367  * soft reset to detect the xyc.
368  */
369 int
370 xyc_probe(arg, tag, handle)
371 	void *arg;
372 	bus_space_tag_t tag;
373 	bus_space_handle_t handle;
374 {
375 	struct xyc *xyc = (void *)handle; /* XXX */
376 
377 	return ((xyc_unbusy(xyc, XYC_RESETUSEC) != XY_ERR_FAIL) ? 0 : EIO);
378 }
379 
380 int xycmatch(parent, cf, aux)
381 	struct device *parent;
382 	struct cfdata *cf;
383 	void *aux;
384 {
385 	struct vme_attach_args	*va = aux;
386 	vme_chipset_tag_t	ct = va->va_vct;
387 	vme_am_t		mod;
388 	int error;
389 
390 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
391 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
392 		return (0);
393 
394 	error = vme_probe(ct, va->r[0].offset, sizeof(struct xyc),
395 			  mod, VME_D16, xyc_probe, 0);
396 	vme_space_free(va->va_vct, va->r[0].offset, sizeof(struct xyc), mod);
397 
398 	return (error == 0);
399 }
400 
401 /*
402  * xycattach: attach controller
403  */
404 void
405 xycattach(parent, self, aux)
406 	struct device *parent, *self;
407 	void   *aux;
408 
409 {
410 	struct xyc_softc	*xyc = (void *) self;
411 	struct vme_attach_args	*va = aux;
412 	vme_chipset_tag_t	ct = va->va_vct;
413 	bus_space_tag_t		bt;
414 	bus_space_handle_t	bh;
415 	vme_intr_handle_t	ih;
416 	vme_am_t		mod;
417 	struct xyc_attach_args	xa;
418 	int			lcv, res, error;
419 	bus_dma_segment_t	seg;
420 	int			rseg;
421 	vme_mapresc_t resc;
422 	bus_addr_t		busaddr;
423 
424 	/* get addressing and intr level stuff from autoconfig and load it
425 	 * into our xyc_softc. */
426 
427 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
428 
429 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
430 		panic("xyc: vme alloc");
431 
432 	if (vme_space_map(ct, va->r[0].offset, sizeof(struct xyc),
433 			  mod, VME_D16, 0, &bt, &bh, &resc) != 0)
434 		panic("xyc: vme_map");
435 
436 	xyc->xyc = (struct xyc *) bh; /* XXX */
437 	xyc->ipl = va->ilevel;
438 	xyc->vector = va->ivector;
439 	xyc->no_ols = 0; /* XXX should be from config */
440 
441 	for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
442 		xyc->sc_drives[lcv] = (struct xy_softc *) 0;
443 
444 	/*
445 	 * allocate and zero buffers
446 	 * check boundaries of the KVA's ... all IOPBs must reside in
447  	 * the same 64K region.
448 	 */
449 
450 	/* Get DMA handle for misc. transfers */
451 	if ((error = vme_dmamap_create(
452 				ct,		/* VME chip tag */
453 				MAXPHYS,	/* size */
454 				VME_AM_A24,	/* address modifier */
455 				VME_D16,	/* data size */
456 				0,		/* swap */
457 				1,		/* nsegments */
458 				MAXPHYS,	/* maxsegsz */
459 				0,		/* boundary */
460 				BUS_DMA_NOWAIT,
461 				&xyc->auxmap)) != 0) {
462 
463 		printf("%s: DMA buffer map create error %d\n",
464 			xyc->sc_dev.dv_xname, error);
465 		return;
466 	}
467 
468 	/* Get DMA handle for mapping iorq descriptors */
469 	if ((error = vme_dmamap_create(
470 				ct,		/* VME chip tag */
471 				XYC_MAXIOPB * sizeof(struct xy_iopb),
472 				VME_AM_A24,	/* address modifier */
473 				VME_D16,	/* data size */
474 				0,		/* swap */
475 				1,		/* nsegments */
476 				XYC_MAXIOPB * sizeof(struct xy_iopb),
477 				64*1024,	/* boundary */
478 				BUS_DMA_NOWAIT,
479 				&xyc->iopmap)) != 0) {
480 
481 		printf("%s: DMA buffer map create error %d\n",
482 			xyc->sc_dev.dv_xname, error);
483 		return;
484 	}
485 
486 	/* Get DMA buffer for iorq descriptors */
487 	if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->iopmap, &seg, &rseg,
488 				     XYC_MAXIOPB * sizeof(struct xy_iopb),
489 				     (caddr_t *)&xyc->iopbase,
490 				     &busaddr)) != 0) {
491 		printf("%s: DMA buffer alloc error %d\n",
492 			xyc->sc_dev.dv_xname, error);
493 		return;
494 	}
495 	xyc->dvmaiopb = (struct xy_iopb *)(u_long)BUS_ADDR_PADDR(busaddr);
496 
497 	bzero(xyc->iopbase, XYC_MAXIOPB * sizeof(struct xy_iopb));
498 
499 	xyc->reqs = (struct xy_iorq *)
500 	    malloc(XYC_MAXIOPB * sizeof(struct xy_iorq),
501 	    M_DEVBUF, M_NOWAIT|M_ZERO);
502 	if (xyc->reqs == NULL)
503 		panic("xyc malloc");
504 
505 	/*
506 	 * init iorq to iopb pointers, and non-zero fields in the
507 	 * iopb which never change.
508 	 */
509 
510 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
511 		xyc->xy_chain[lcv] = NULL;
512 		xyc->reqs[lcv].iopb = &xyc->iopbase[lcv];
513 		xyc->reqs[lcv].dmaiopb = &xyc->dvmaiopb[lcv];
514 		xyc->iopbase[lcv].asr = 1;	/* always the same */
515 		xyc->iopbase[lcv].eef = 1;	/* always the same */
516 		xyc->iopbase[lcv].ecm = XY_ECM;	/* always the same */
517 		xyc->iopbase[lcv].aud = 1;	/* always the same */
518 		xyc->iopbase[lcv].relo = 1;	/* always the same */
519 		xyc->iopbase[lcv].thro = XY_THRO;/* always the same */
520 
521 		if ((error = vme_dmamap_create(
522 				ct,		/* VME chip tag */
523 				MAXPHYS,	/* size */
524 				VME_AM_A24,	/* address modifier */
525 				VME_D16,	/* data size */
526 				0,		/* swap */
527 				1,		/* nsegments */
528 				MAXPHYS,	/* maxsegsz */
529 				0,		/* boundary */
530 				BUS_DMA_NOWAIT,
531 				&xyc->reqs[lcv].dmamap)) != 0) {
532 
533 			printf("%s: DMA buffer map create error %d\n",
534 				xyc->sc_dev.dv_xname, error);
535 			return;
536 		}
537 	}
538 	xyc->ciorq = &xyc->reqs[XYC_CTLIOPB];    /* short hand name */
539 	xyc->ciopb = &xyc->iopbase[XYC_CTLIOPB]; /* short hand name */
540 	xyc->xy_hand = 0;
541 
542 	/* read controller parameters and insure we have a 450/451 */
543 
544 	error = xyc_cmd(xyc, XYCMD_ST, 0, 0, 0, 0, 0, XY_SUB_POLL);
545 	res = xyc->ciopb->ctyp;
546 	XYC_DONE(xyc, error);
547 	if (res != XYCT_450) {
548 		if (error)
549 			printf(": %s: ", xyc_e2str(error));
550 		printf(": doesn't identify as a 450/451\n");
551 		return;
552 	}
553 	printf(": Xylogics 450/451");
554 	if (xyc->no_ols)
555 		printf(" [OLS disabled]"); /* 450 doesn't overlap seek right */
556 	printf("\n");
557 	if (error) {
558 		printf("%s: error: %s\n", xyc->sc_dev.dv_xname,
559 				xyc_e2str(error));
560 		return;
561 	}
562 	if ((xyc->xyc->xyc_csr & XYC_ADRM) == 0) {
563 		printf("%s: 24 bit addressing turned off\n",
564 			xyc->sc_dev.dv_xname);
565 		printf("please set hardware jumpers JM1-JM2=in, JM3-JM4=out\n");
566 		printf("to enable 24 bit mode and this driver\n");
567 		return;
568 	}
569 
570 	/* link in interrupt with higher level software */
571 	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
572 	vme_intr_establish(ct, ih, IPL_BIO, xycintr, xyc);
573 	evcnt_attach_dynamic(&xyc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
574 	    xyc->sc_dev.dv_xname, "intr");
575 
576 	callout_init(&xyc->sc_tick_ch);
577 
578 	/* now we must look for disks using autoconfig */
579 	xa.fullmode = XY_SUB_POLL;
580 	xa.booting = 1;
581 
582 	for (xa.driveno = 0; xa.driveno < XYC_MAXDEV; xa.driveno++)
583 		(void) config_found(self, (void *) &xa, NULL);
584 
585 	/* start the watchdog clock */
586 	callout_reset(&xyc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xyc);
587 
588 }
589 
590 /*
591  * xymatch: probe for disk.
592  *
593  * note: we almost always say disk is present.   this allows us to
594  * spin up and configure a disk after the system is booted (we can
595  * call xyattach!).
596  */
597 int
598 xymatch(parent, cf, aux)
599 	struct device *parent;
600 	struct cfdata *cf;
601 	void *aux;
602 {
603 	struct xyc_attach_args *xa = aux;
604 
605 	/* looking for autoconf wildcard or exact match */
606 
607 	if (cf->cf_loc[XYCCF_DRIVE] != XYCCF_DRIVE_DEFAULT &&
608 	    cf->cf_loc[XYCCF_DRIVE] != xa->driveno)
609 		return 0;
610 
611 	return 1;
612 
613 }
614 
615 /*
616  * xyattach: attach a disk.   this can be called from autoconf and also
617  * from xyopen/xystrategy.
618  */
619 void
620 xyattach(parent, self, aux)
621 	struct device *parent, *self;
622 	void   *aux;
623 
624 {
625 	struct xy_softc *xy = (void *) self, *oxy;
626 	struct xyc_softc *xyc = (void *) parent;
627 	struct xyc_attach_args *xa = aux;
628 	int     spt, mb, blk, lcv, fmode, s = 0, newstate;
629 	struct dkbad *dkb;
630 	int			rseg, error;
631 	bus_dma_segment_t	seg;
632 	bus_addr_t		busaddr;
633 	caddr_t			dmaddr;
634 	caddr_t			buf;
635 
636 	/*
637 	 * Always re-initialize the disk structure.  We want statistics
638 	 * to start with a clean slate.
639 	 */
640 	bzero(&xy->sc_dk, sizeof(xy->sc_dk));
641 	xy->sc_dk.dk_driver = &xydkdriver;
642 	xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
643 
644 	/* if booting, init the xy_softc */
645 
646 	if (xa->booting) {
647 		xy->state = XY_DRIVE_UNKNOWN;	/* to start */
648 		xy->flags = 0;
649 		xy->parent = xyc;
650 
651 		/* init queue of waiting bufs */
652 
653 		bufq_alloc(&xy->xyq, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
654 
655 		xy->xyrq = &xyc->reqs[xa->driveno];
656 
657 	}
658 	xy->xy_drive = xa->driveno;
659 	fmode = xa->fullmode;
660 	xyc->sc_drives[xa->driveno] = xy;
661 
662 	/* if not booting, make sure we are the only process in the attach for
663 	 * this drive.   if locked out, sleep on it. */
664 
665 	if (!xa->booting) {
666 		s = splbio();
667 		while (xy->state == XY_DRIVE_ATTACHING) {
668 			if (tsleep(&xy->state, PRIBIO, "xyattach", 0)) {
669 				splx(s);
670 				return;
671 			}
672 		}
673 		printf("%s at %s",
674 			xy->sc_dev.dv_xname, xy->parent->sc_dev.dv_xname);
675 	}
676 
677 	/* we now have control */
678 	xy->state = XY_DRIVE_ATTACHING;
679 	newstate = XY_DRIVE_UNKNOWN;
680 
681 	buf = NULL;
682 	if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->auxmap, &seg, &rseg,
683 				     XYFM_BPS,
684 				     (caddr_t *)&buf,
685 				     &busaddr)) != 0) {
686 		printf("%s: DMA buffer alloc error %d\n",
687 			xyc->sc_dev.dv_xname, error);
688 		return;
689 	}
690 	dmaddr = (caddr_t)(u_long)BUS_ADDR_PADDR(busaddr);
691 
692 	/* first try and reset the drive */
693 	error = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fmode);
694 	XYC_DONE(xyc, error);
695 	if (error == XY_ERR_DNRY) {
696 		printf(" drive %d: off-line\n", xa->driveno);
697 		goto done;
698 	}
699 	if (error) {
700 		printf(": ERROR 0x%02x (%s)\n", error, xyc_e2str(error));
701 		goto done;
702 	}
703 	printf(" drive %d: ready", xa->driveno);
704 
705 	/*
706 	 * now set drive parameters (to semi-bogus values) so we can read the
707 	 * disk label.
708 	 */
709 	xy->pcyl = xy->ncyl = 1;
710 	xy->acyl = 0;
711 	xy->nhead = 1;
712 	xy->nsect = 1;
713 	xy->sectpercyl = 1;
714 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
715 		xy->dkb.bt_bad[lcv].bt_cyl =
716 			xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
717 
718 	/* read disk label */
719 	for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
720 						xy->drive_type++) {
721 		error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
722 						dmaddr, fmode);
723 		XYC_DONE(xyc, error);
724 		if (error == XY_ERR_AOK) break;
725 	}
726 
727 	if (error != XY_ERR_AOK) {
728 		printf("\n%s: reading disk label failed: %s\n",
729 			xy->sc_dev.dv_xname, xyc_e2str(error));
730 		goto done;
731 	}
732 	printf(" (drive type %d)\n", xy->drive_type);
733 
734 	newstate = XY_DRIVE_NOLABEL;
735 
736 	xy->hw_spt = spt = 0; /* XXX needed ? */
737 	/* Attach the disk: must be before getdisklabel to malloc label */
738 	disk_attach(&xy->sc_dk);
739 
740 	if (xygetdisklabel(xy, buf) != XY_ERR_AOK)
741 		goto done;
742 
743 	/* inform the user of what is up */
744 	printf("%s: <%s>, pcyl %d\n", xy->sc_dev.dv_xname,
745 		buf, xy->pcyl);
746 	mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
747 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
748 		xy->sc_dev.dv_xname, mb, xy->ncyl, xy->nhead, xy->nsect,
749 		XYFM_BPS);
750 
751 	/*
752 	 * 450/451 stupidity: the drive type is encoded into the format
753 	 * of the disk.   the drive type in the IOPB must match the drive
754 	 * type in the format, or you will not be able to do I/O to the
755 	 * disk (you get header not found errors).  if you have two drives
756 	 * of different sizes that have the same drive type in their
757 	 * formatting then you are out of luck.
758 	 *
759 	 * this problem was corrected in the 753/7053.
760 	 */
761 
762 	for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
763 		oxy = xyc->sc_drives[lcv];
764 		if (oxy == NULL || oxy == xy) continue;
765 		if (oxy->drive_type != xy->drive_type) continue;
766 		if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
767 			xy->nhead != oxy->nhead) {
768 			printf("%s: %s and %s must be the same size!\n",
769 				xyc->sc_dev.dv_xname, xy->sc_dev.dv_xname,
770 				oxy->sc_dev.dv_xname);
771 			panic("xy drive size mismatch");
772 		}
773 	}
774 
775 
776 	/* now set the real drive parameters! */
777 
778 	blk = (xy->nsect - 1) +
779 		((xy->nhead - 1) * xy->nsect) +
780 		((xy->pcyl - 1) * xy->nsect * xy->nhead);
781 	error = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fmode);
782 	XYC_DONE(xyc, error);
783 	if (error) {
784 		printf("%s: write drive size failed: %s\n",
785 			xy->sc_dev.dv_xname, xyc_e2str(error));
786 		goto done;
787 	}
788 	newstate = XY_DRIVE_ONLINE;
789 
790 	/*
791 	 * read bad144 table. this table resides on the first sector of the
792 	 * last track of the disk (i.e. second cyl of "acyl" area).
793 	 */
794 
795 	blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
796 								/* last cyl */
797 	    (xy->nhead - 1) * xy->nsect;	/* last head */
798 	error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
799 						dmaddr, fmode);
800 	XYC_DONE(xyc, error);
801 	if (error) {
802 		printf("%s: reading bad144 failed: %s\n",
803 			xy->sc_dev.dv_xname, xyc_e2str(error));
804 		goto done;
805 	}
806 
807 	/* check dkbad for sanity */
808 	dkb = (struct dkbad *) buf;
809 	for (lcv = 0; lcv < 126; lcv++) {
810 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
811 				dkb->bt_bad[lcv].bt_cyl == 0) &&
812 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
813 			continue;	/* blank */
814 		if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
815 			break;
816 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
817 			break;
818 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
819 			break;
820 	}
821 	if (lcv != 126) {
822 		printf("%s: warning: invalid bad144 sector!\n",
823 			xy->sc_dev.dv_xname);
824 	} else {
825 		bcopy(buf, &xy->dkb, XYFM_BPS);
826 	}
827 
828 done:
829 	if (buf != NULL) {
830 		xy_dmamem_free(xyc->dmatag, xyc->auxmap,
831 				&seg, rseg, XYFM_BPS, buf);
832 	}
833 
834 	xy->state = newstate;
835 	if (!xa->booting) {
836 		wakeup(&xy->state);
837 		splx(s);
838 	}
839 }
840 
841 /*
842  * end of autoconfig functions
843  */
844 
845 /*
846  * { b , c } d e v s w   f u n c t i o n s
847  */
848 
849 /*
850  * xyclose: close device
851  */
852 int
853 xyclose(dev, flag, fmt, p)
854 	dev_t   dev;
855 	int     flag, fmt;
856 	struct proc *p;
857 
858 {
859 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
860 	int     part = DISKPART(dev);
861 
862 	/* clear mask bits */
863 
864 	switch (fmt) {
865 	case S_IFCHR:
866 		xy->sc_dk.dk_copenmask &= ~(1 << part);
867 		break;
868 	case S_IFBLK:
869 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
870 		break;
871 	}
872 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
873 
874 	return 0;
875 }
876 
877 /*
878  * xydump: crash dump system
879  */
880 int
881 xydump(dev, blkno, va, size)
882 	dev_t dev;
883 	daddr_t blkno;
884 	caddr_t va;
885 	size_t size;
886 {
887 	int     unit, part;
888 	struct xy_softc *xy;
889 
890 	unit = DISKUNIT(dev);
891 	if (unit >= xy_cd.cd_ndevs)
892 		return ENXIO;
893 	part = DISKPART(dev);
894 
895 	xy = xy_cd.cd_devs[unit];
896 
897 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
898 	    'a' + part);
899 
900 	return ENXIO;
901 
902 	/* outline: globals: "dumplo" == sector number of partition to start
903 	 * dump at (convert to physical sector with partition table)
904 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
905 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
906 	 * physmem)
907 	 *
908 	 * dump a copy of physical memory to the dump device starting at sector
909 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
910 	 * we go.   use polled I/O.
911 	 *
912 	 * XXX how to handle NON_CONTIG? */
913 
914 }
915 
916 /*
917  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
918  */
919 int
920 xyioctl(dev, command, addr, flag, p)
921 	dev_t   dev;
922 	u_long  command;
923 	caddr_t addr;
924 	int     flag;
925 	struct proc *p;
926 
927 {
928 	struct xy_softc *xy;
929 	struct xd_iocmd *xio;
930 	int     error, s, unit;
931 #ifdef __HAVE_OLD_DISKLABEL
932 	struct disklabel newlabel;
933 #endif
934 	struct disklabel *lp;
935 
936 	unit = DISKUNIT(dev);
937 
938 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
939 		return (ENXIO);
940 
941 	/* switch on ioctl type */
942 
943 	switch (command) {
944 	case DIOCSBAD:		/* set bad144 info */
945 		if ((flag & FWRITE) == 0)
946 			return EBADF;
947 		s = splbio();
948 		bcopy(addr, &xy->dkb, sizeof(xy->dkb));
949 		splx(s);
950 		return 0;
951 
952 	case DIOCGDINFO:	/* get disk label */
953 		bcopy(xy->sc_dk.dk_label, addr, sizeof(struct disklabel));
954 		return 0;
955 #ifdef __HAVE_OLD_DISKLABEL
956 	case ODIOCGDINFO:
957 		newlabel = *(xy->sc_dk.dk_label);
958 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
959 			return ENOTTY;
960 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
961 		return 0;
962 #endif
963 
964 	case DIOCGPART:	/* get partition info */
965 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
966 		((struct partinfo *) addr)->part =
967 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
968 		return 0;
969 
970 	case DIOCSDINFO:	/* set disk label */
971 #ifdef __HAVE_OLD_DISKLABEL
972 	case ODIOCSDINFO:
973 		if (command == ODIOCSDINFO) {
974 			memset(&newlabel, 0, sizeof newlabel);
975 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
976 			lp = &newlabel;
977 		} else
978 #endif
979 		lp = (struct disklabel *)addr;
980 
981 		if ((flag & FWRITE) == 0)
982 			return EBADF;
983 		error = setdisklabel(xy->sc_dk.dk_label,
984 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
985 		    xy->sc_dk.dk_cpulabel);
986 		if (error == 0) {
987 			if (xy->state == XY_DRIVE_NOLABEL)
988 				xy->state = XY_DRIVE_ONLINE;
989 		}
990 		return error;
991 
992 	case DIOCWLABEL:	/* change write status of disk label */
993 		if ((flag & FWRITE) == 0)
994 			return EBADF;
995 		if (*(int *) addr)
996 			xy->flags |= XY_WLABEL;
997 		else
998 			xy->flags &= ~XY_WLABEL;
999 		return 0;
1000 
1001 	case DIOCWDINFO:	/* write disk label */
1002 #ifdef __HAVE_OLD_DISKLABEL
1003 	case ODIOCWDINFO:
1004 		if (command == ODIOCWDINFO) {
1005 			memset(&newlabel, 0, sizeof newlabel);
1006 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1007 			lp = &newlabel;
1008 		} else
1009 #endif
1010 		lp = (struct disklabel *)addr;
1011 
1012 		if ((flag & FWRITE) == 0)
1013 			return EBADF;
1014 		error = setdisklabel(xy->sc_dk.dk_label,
1015 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
1016 		    xy->sc_dk.dk_cpulabel);
1017 		if (error == 0) {
1018 			if (xy->state == XY_DRIVE_NOLABEL)
1019 				xy->state = XY_DRIVE_ONLINE;
1020 
1021 			/* Simulate opening partition 0 so write succeeds. */
1022 			xy->sc_dk.dk_openmask |= (1 << 0);
1023 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
1024 			    xystrategy, xy->sc_dk.dk_label,
1025 			    xy->sc_dk.dk_cpulabel);
1026 			xy->sc_dk.dk_openmask =
1027 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1028 		}
1029 		return error;
1030 
1031 	case DIOSXDCMD:
1032 		xio = (struct xd_iocmd *) addr;
1033 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1034 			return (error);
1035 		return (xyc_ioctlcmd(xy, dev, xio));
1036 
1037 	default:
1038 		return ENOTTY;
1039 	}
1040 }
1041 
1042 /*
1043  * xyopen: open drive
1044  */
1045 
1046 int
1047 xyopen(dev, flag, fmt, p)
1048 	dev_t   dev;
1049 	int     flag, fmt;
1050 	struct proc *p;
1051 {
1052 	int     unit, part;
1053 	struct xy_softc *xy;
1054 	struct xyc_attach_args xa;
1055 
1056 	/* first, could it be a valid target? */
1057 
1058 	unit = DISKUNIT(dev);
1059 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
1060 		return (ENXIO);
1061 	part = DISKPART(dev);
1062 
1063 	/* do we need to attach the drive? */
1064 
1065 	if (xy->state == XY_DRIVE_UNKNOWN) {
1066 		xa.driveno = xy->xy_drive;
1067 		xa.fullmode = XY_SUB_WAIT;
1068 		xa.booting = 0;
1069 		xyattach((struct device *) xy->parent,
1070 						(struct device *) xy, &xa);
1071 		if (xy->state == XY_DRIVE_UNKNOWN) {
1072 			return (EIO);
1073 		}
1074 	}
1075 	/* check for partition */
1076 
1077 	if (part != RAW_PART &&
1078 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
1079 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1080 		return (ENXIO);
1081 	}
1082 	/* set open masks */
1083 
1084 	switch (fmt) {
1085 	case S_IFCHR:
1086 		xy->sc_dk.dk_copenmask |= (1 << part);
1087 		break;
1088 	case S_IFBLK:
1089 		xy->sc_dk.dk_bopenmask |= (1 << part);
1090 		break;
1091 	}
1092 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1093 
1094 	return 0;
1095 }
1096 
1097 int
1098 xyread(dev, uio, flags)
1099 	dev_t   dev;
1100 	struct uio *uio;
1101 	int flags;
1102 {
1103 
1104 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
1105 }
1106 
1107 int
1108 xywrite(dev, uio, flags)
1109 	dev_t   dev;
1110 	struct uio *uio;
1111 	int flags;
1112 {
1113 
1114 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
1115 }
1116 
1117 
1118 /*
1119  * xysize: return size of a partition for a dump
1120  */
1121 
1122 int
1123 xysize(dev)
1124 	dev_t   dev;
1125 
1126 {
1127 	struct xy_softc *xysc;
1128 	int     unit, part, size, omask;
1129 
1130 	/* valid unit? */
1131 	unit = DISKUNIT(dev);
1132 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
1133 		return (-1);
1134 
1135 	part = DISKPART(dev);
1136 	omask = xysc->sc_dk.dk_openmask & (1 << part);
1137 
1138 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
1139 		return (-1);
1140 
1141 	/* do it */
1142 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1143 		size = -1;	/* only give valid size for swap partitions */
1144 	else
1145 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
1146 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1147 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
1148 		return (-1);
1149 	return (size);
1150 }
1151 
1152 /*
1153  * xystrategy: buffering system interface to xy.
1154  */
1155 
1156 void
1157 xystrategy(bp)
1158 	struct buf *bp;
1159 
1160 {
1161 	struct xy_softc *xy;
1162 	int     s, unit;
1163 	struct xyc_attach_args xa;
1164 	struct disklabel *lp;
1165 	daddr_t blkno;
1166 
1167 	unit = DISKUNIT(bp->b_dev);
1168 
1169 	/* check for live device */
1170 
1171 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
1172 	    bp->b_blkno < 0 ||
1173 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
1174 		bp->b_error = EINVAL;
1175 		goto bad;
1176 	}
1177 	/* do we need to attach the drive? */
1178 
1179 	if (xy->state == XY_DRIVE_UNKNOWN) {
1180 		xa.driveno = xy->xy_drive;
1181 		xa.fullmode = XY_SUB_WAIT;
1182 		xa.booting = 0;
1183 		xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
1184 		if (xy->state == XY_DRIVE_UNKNOWN) {
1185 			bp->b_error = EIO;
1186 			goto bad;
1187 		}
1188 	}
1189 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1190 		/* no I/O to unlabeled disks, unless raw partition */
1191 		bp->b_error = EIO;
1192 		goto bad;
1193 	}
1194 	/* short circuit zero length request */
1195 
1196 	if (bp->b_bcount == 0)
1197 		goto done;
1198 
1199 	/* check bounds with label (disksubr.c).  Determine the size of the
1200 	 * transfer, and make sure it is within the boundaries of the
1201 	 * partition. Adjust transfer if needed, and signal errors or early
1202 	 * completion. */
1203 
1204 	lp = xy->sc_dk.dk_label;
1205 
1206 	if (bounds_check_with_label(&xy->sc_dk, bp,
1207 		(xy->flags & XY_WLABEL) != 0) <= 0)
1208 		goto done;
1209 
1210 	/*
1211 	 * Now convert the block number to absolute and put it in
1212 	 * terms of the device's logical block size.
1213 	 */
1214 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1215 	if (DISKPART(bp->b_dev) != RAW_PART)
1216 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
1217 
1218 	bp->b_rawblkno = blkno;
1219 
1220 	/*
1221 	 * now we know we have a valid buf structure that we need to do I/O
1222 	 * on.
1223 	 */
1224 	s = splbio();		/* protect the queues */
1225 
1226 	BUFQ_PUT(&xy->xyq, bp);
1227 
1228 	/* start 'em up */
1229 
1230 	xyc_start(xy->parent, NULL);
1231 
1232 	/* done! */
1233 
1234 	splx(s);
1235 	return;
1236 
1237 bad:				/* tells upper layers we have an error */
1238 	bp->b_flags |= B_ERROR;
1239 done:				/* tells upper layers we are done with this
1240 				 * buf */
1241 	bp->b_resid = bp->b_bcount;
1242 	biodone(bp);
1243 }
1244 /*
1245  * end of {b,c}devsw functions
1246  */
1247 
1248 /*
1249  * i n t e r r u p t   f u n c t i o n
1250  *
1251  * xycintr: hardware interrupt.
1252  */
1253 int
1254 xycintr(v)
1255 	void   *v;
1256 
1257 {
1258 	struct xyc_softc *xycsc = v;
1259 
1260 	/* kick the event counter */
1261 
1262 	xycsc->sc_intrcnt.ev_count++;
1263 
1264 	/* remove as many done IOPBs as possible */
1265 
1266 	xyc_remove_iorq(xycsc);
1267 
1268 	/* start any iorq's already waiting */
1269 
1270 	xyc_start(xycsc, NULL);
1271 
1272 	return (1);
1273 }
1274 /*
1275  * end of interrupt function
1276  */
1277 
1278 /*
1279  * i n t e r n a l   f u n c t i o n s
1280  */
1281 
1282 /*
1283  * xyc_rqinit: fill out the fields of an I/O request
1284  */
1285 
1286 inline void
1287 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
1288 	struct xy_iorq *rq;
1289 	struct xyc_softc *xyc;
1290 	struct xy_softc *xy;
1291 	int     md;
1292 	u_long  blk;
1293 	int     cnt;
1294 	caddr_t db;
1295 	struct buf *bp;
1296 {
1297 	rq->xyc = xyc;
1298 	rq->xy = xy;
1299 	rq->ttl = XYC_MAXTTL + 10;
1300 	rq->mode = md;
1301 	rq->tries = rq->errno = rq->lasterror = 0;
1302 	rq->blockno = blk;
1303 	rq->sectcnt = cnt;
1304 	rq->dbuf = db;
1305 	rq->buf = bp;
1306 }
1307 
1308 /*
1309  * xyc_rqtopb: load up an IOPB based on an iorq
1310  */
1311 
1312 void
1313 xyc_rqtopb(iorq, iopb, cmd, subfun)
1314 	struct xy_iorq *iorq;
1315 	struct xy_iopb *iopb;
1316 	int     cmd, subfun;
1317 
1318 {
1319 	u_long  block, dp;
1320 
1321 	/* normal IOPB case, standard stuff */
1322 
1323 	/* chain bit handled later */
1324 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
1325 	iopb->com = cmd;
1326 	iopb->errno = 0;
1327 	iopb->errs = 0;
1328 	iopb->done = 0;
1329 	if (iorq->xy) {
1330 		iopb->unit = iorq->xy->xy_drive;
1331 		iopb->dt = iorq->xy->drive_type;
1332 	} else {
1333 		iopb->unit = 0;
1334 		iopb->dt = 0;
1335 	}
1336 	block = iorq->blockno;
1337 	if (iorq->xy == NULL || block == 0) {
1338 		iopb->sect = iopb->head = iopb->cyl = 0;
1339 	} else {
1340 		iopb->sect = block % iorq->xy->nsect;
1341 		block = block / iorq->xy->nsect;
1342 		iopb->head = block % iorq->xy->nhead;
1343 		block = block / iorq->xy->nhead;
1344 		iopb->cyl = block;
1345 	}
1346 	iopb->scnt = iorq->sectcnt;
1347 	dp = (u_long) iorq->dbuf;
1348 	if (iorq->dbuf == NULL) {
1349 		iopb->dataa = 0;
1350 		iopb->datar = 0;
1351 	} else {
1352 		iopb->dataa = (dp & 0xffff);
1353 		iopb->datar = ((dp & 0xff0000) >> 16);
1354 	}
1355 	iopb->subfn = subfun;
1356 }
1357 
1358 
1359 /*
1360  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
1361  */
1362 
1363 int
1364 xyc_unbusy(xyc, del)
1365 
1366 struct xyc *xyc;
1367 int del;
1368 
1369 {
1370 	while (del-- > 0) {
1371 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
1372 			break;
1373 		DELAY(1);
1374 	}
1375 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
1376 }
1377 
1378 /*
1379  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
1380  * note that NORM requests are handled separately.
1381  */
1382 int
1383 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1384 	struct xyc_softc *xycsc;
1385 	int     cmd, subfn, unit, block, scnt;
1386 	char   *dptr;
1387 	int     fullmode;
1388 
1389 {
1390 	int     submode = XY_STATE(fullmode);
1391 	struct xy_iorq *iorq = xycsc->ciorq;
1392 	struct xy_iopb *iopb = xycsc->ciopb;
1393 
1394 	/*
1395 	 * is someone else using the control iopq wait for it if we can
1396 	 */
1397 start:
1398 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
1399 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
1400                                 return(XY_ERR_FAIL);
1401 		goto start;
1402 	}
1403 
1404 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
1405 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
1406 		iorq->mode = XY_SUB_FREE;
1407 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
1408 	}
1409 
1410 	/* init iorq/iopb */
1411 
1412 	xyc_rqinit(iorq, xycsc,
1413 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
1414 	    fullmode, block, scnt, dptr, NULL);
1415 
1416 	/* load IOPB from iorq */
1417 
1418 	xyc_rqtopb(iorq, iopb, cmd, subfn);
1419 
1420 	/* submit it for processing */
1421 
1422 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
1423 
1424 	return(XY_ERR_AOK);
1425 }
1426 
1427 /*
1428  * xyc_startbuf
1429  * start a buffer for running
1430  */
1431 
1432 int
1433 xyc_startbuf(xycsc, xysc, bp)
1434 	struct xyc_softc *xycsc;
1435 	struct xy_softc *xysc;
1436 	struct buf *bp;
1437 
1438 {
1439 	int     partno, error;
1440 	struct xy_iorq *iorq;
1441 	struct xy_iopb *iopb;
1442 	u_long  block;
1443 
1444 	iorq = xysc->xyrq;
1445 	iopb = iorq->iopb;
1446 
1447 	/* get buf */
1448 
1449 	if (bp == NULL)
1450 		panic("xyc_startbuf null buf");
1451 
1452 	partno = DISKPART(bp->b_dev);
1453 #ifdef XYC_DEBUG
1454 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
1455 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1456 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1457 	    bp->b_bcount, bp->b_data);
1458 #endif
1459 
1460 	/*
1461 	 * load request.
1462 	 *
1463 	 * note that iorq points to the buffer as mapped into DVMA space,
1464 	 * where as the bp->b_data points to its non-DVMA mapping.
1465 	 */
1466 
1467 	block = bp->b_rawblkno;
1468 
1469 	error = bus_dmamap_load(xycsc->dmatag, iorq->dmamap,
1470 			bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
1471 	if (error != 0) {
1472 		printf("%s: warning: cannot load DMA map\n",
1473 			xycsc->sc_dev.dv_xname);
1474 		return (XY_ERR_FAIL);	/* XXX: need some sort of
1475 					 * call-back scheme here? */
1476 	}
1477 
1478 	bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1479 			iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
1480 				? BUS_DMASYNC_PREREAD
1481 				: BUS_DMASYNC_PREWRITE);
1482 
1483 	/* init iorq and load iopb from it */
1484 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
1485 		   bp->b_bcount / XYFM_BPS,
1486 		   (caddr_t)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
1487 		   bp);
1488 
1489 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
1490 
1491 	/* Instrumentation. */
1492 	disk_busy(&xysc->sc_dk);
1493 
1494 	return (XY_ERR_AOK);
1495 }
1496 
1497 
1498 /*
1499  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
1500  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
1501  *
1502  * note: caller frees iorq in all cases except NORM
1503  *
1504  * return value:
1505  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
1506  *   WAIT: XY_AOK (success), <error-code> (failed)
1507  *   POLL: <same as WAIT>
1508  *   NOQ : <same as NORM>
1509  *
1510  * there are three sources for i/o requests:
1511  * [1] xystrategy: normal block I/O, using "struct buf" system.
1512  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1513  * [3] open/ioctl: these are I/O requests done in the context of a process,
1514  *                 and the process should block until they are done.
1515  *
1516  * software state is stored in the iorq structure.  each iorq has an
1517  * iopb structure.  the hardware understands the iopb structure.
1518  * every command must go through an iopb.  a 450 handles one iopb at a
1519  * time, where as a 451 can take them in chains.  [the 450 claims it
1520  * can handle chains, but is appears to be buggy...]   iopb are allocated
1521  * in DVMA space at boot up time.  each disk gets one iopb, and the
1522  * controller gets one (for POLL and WAIT commands).  what happens if
1523  * the iopb is busy?  for i/o type [1], the buffers are queued at the
1524  * "buff" layer and * picked up later by the interrupt routine.  for case
1525  * [2] we can only be blocked if there is a WAIT type I/O request being
1526  * run.   since this can only happen when we are crashing, we wait a sec
1527  * and then steal the IOPB.  for case [3] the process can sleep
1528  * on the iorq free list until some iopbs are available.
1529  */
1530 
1531 
1532 int
1533 xyc_submit_iorq(xycsc, iorq, type)
1534 	struct xyc_softc *xycsc;
1535 	struct xy_iorq *iorq;
1536 	int     type;
1537 
1538 {
1539 	struct xy_iopb *dmaiopb;
1540 
1541 #ifdef XYC_DEBUG
1542 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
1543 		xycsc->sc_dev.dv_xname, iorq, type);
1544 #endif
1545 
1546 	/* first check and see if controller is busy */
1547 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
1548 #ifdef XYC_DEBUG
1549 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
1550 #endif
1551 		if (type == XY_SUB_NOQ)
1552 			return (XY_ERR_FAIL);	/* failed */
1553 		switch (type) {
1554 		case XY_SUB_NORM:
1555 			return XY_ERR_AOK;	/* success */
1556 		case XY_SUB_WAIT:
1557 			while (iorq->iopb->done == 0) {
1558 				(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1559 			}
1560 			return (iorq->errno);
1561 		case XY_SUB_POLL:		/* steal controller */
1562 			(void)xycsc->xyc->xyc_rsetup; /* RESET */
1563 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
1564 				panic("xyc_submit_iorq: stuck xyc");
1565 			printf("%s: stole controller\n",
1566 				xycsc->sc_dev.dv_xname);
1567 			break;
1568 		default:
1569 			panic("xyc_submit_iorq adding");
1570 		}
1571 	}
1572 
1573 	dmaiopb = xyc_chain(xycsc, iorq);	 /* build chain */
1574 	if (dmaiopb == NULL) { /* nothing doing? */
1575 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
1576 			return(XY_ERR_AOK);
1577 		panic("xyc_submit_iorq: xyc_chain failed!");
1578 	}
1579 
1580 	XYC_GO(xycsc->xyc, (u_long)dmaiopb);
1581 
1582 	/* command now running, wrap it up */
1583 	switch (type) {
1584 	case XY_SUB_NORM:
1585 	case XY_SUB_NOQ:
1586 		return (XY_ERR_AOK);	/* success */
1587 	case XY_SUB_WAIT:
1588 		while (iorq->iopb->done == 0) {
1589 			(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1590 		}
1591 		return (iorq->errno);
1592 	case XY_SUB_POLL:
1593 		return (xyc_piodriver(xycsc, iorq));
1594 	default:
1595 		panic("xyc_submit_iorq wrap up");
1596 	}
1597 	panic("xyc_submit_iorq");
1598 	return 0;	/* not reached */
1599 }
1600 
1601 
1602 /*
1603  * xyc_chain: build a chain.  return dvma address of first element in
1604  * the chain.   iorq != NULL: means we only want that item on the chain.
1605  */
1606 
1607 struct xy_iopb *
1608 xyc_chain(xycsc, iorq)
1609 	struct xyc_softc *xycsc;
1610 	struct xy_iorq *iorq;
1611 
1612 {
1613 	int togo, chain, hand;
1614 
1615 	bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
1616 
1617 	/*
1618 	 * promote control IOPB to the top
1619 	 */
1620 	if (iorq == NULL) {
1621 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1622 		     XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1623 		     xycsc->iopbase[XYC_CTLIOPB].done == 0)
1624 			iorq = &xycsc->reqs[XYC_CTLIOPB];
1625 	}
1626 
1627 	/*
1628 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
1629 	 * we let these take priority and do them first.
1630 	 */
1631 	if (iorq) {
1632 		xycsc->xy_chain[0] = iorq;
1633 		iorq->iopb->chen = 0;
1634 		return(iorq->dmaiopb);
1635 	}
1636 
1637 	/*
1638 	 * NORM case: do round robin and maybe chain (if allowed and possible)
1639 	 */
1640 	chain = 0;
1641 	hand = xycsc->xy_hand;
1642 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1643 
1644 	for (togo = XYC_MAXIOPB; togo > 0;
1645 	     togo--, hand = (hand + 1) % XYC_MAXIOPB) {
1646 		struct xy_iopb *iopb, *prev_iopb, *dmaiopb;
1647 
1648 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1649 		    xycsc->iopbase[hand].done)
1650 			continue;   /* not ready-for-i/o */
1651 
1652 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1653 		iopb = xycsc->xy_chain[chain]->iopb;
1654 		iopb->chen = 0;
1655 		if (chain != 0) {
1656 			/* adding a link to a chain */
1657 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1658 			prev_iopb->chen = 1;
1659 			dmaiopb = xycsc->xy_chain[chain]->dmaiopb;
1660 			prev_iopb->nxtiopb = ((u_long)dmaiopb) & 0xffff;
1661 		} else {
1662 			/* head of chain */
1663 			iorq = xycsc->xy_chain[chain];
1664 		}
1665 		chain++;
1666 
1667 		/* quit if chaining dis-allowed */
1668 		if (xycsc->no_ols)
1669 			break;
1670 	}
1671 
1672 	return(iorq ? iorq->dmaiopb : NULL);
1673 }
1674 
1675 /*
1676  * xyc_piodriver
1677  *
1678  * programmed i/o driver.   this function takes over the computer
1679  * and drains off the polled i/o request.   it returns the status of the iorq
1680  * the caller is interesting in.
1681  */
1682 int
1683 xyc_piodriver(xycsc, iorq)
1684 	struct xyc_softc *xycsc;
1685 	struct xy_iorq  *iorq;
1686 
1687 {
1688 	int     nreset = 0;
1689 	int     retval = 0;
1690 	u_long  res;
1691 #ifdef XYC_DEBUG
1692 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
1693 #endif
1694 
1695 	while (iorq->iopb->done == 0) {
1696 
1697 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1698 
1699 		/* we expect some progress soon */
1700 		if (res == XY_ERR_FAIL && nreset >= 2) {
1701 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1702 #ifdef XYC_DEBUG
1703 			printf("xyc_piodriver: timeout\n");
1704 #endif
1705 			return (XY_ERR_FAIL);
1706 		}
1707 		if (res == XY_ERR_FAIL) {
1708 			if (xyc_reset(xycsc, 0,
1709 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
1710 				      XY_ERR_FAIL,
1711 				      0) == XY_ERR_FAIL)
1712 				return (XY_ERR_FAIL);	/* flushes all but POLL
1713 							 * requests, resets */
1714 			continue;
1715 		}
1716 
1717 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
1718 
1719 		if (iorq->iopb->done == 0)
1720 			xyc_start(xycsc, iorq);
1721 	}
1722 
1723 	/* get return value */
1724 
1725 	retval = iorq->errno;
1726 
1727 #ifdef XYC_DEBUG
1728 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1729 	    iorq->errno, xyc_e2str(iorq->errno));
1730 #endif
1731 
1732 	/* start up any bufs that have queued */
1733 
1734 	xyc_start(xycsc, NULL);
1735 
1736 	return (retval);
1737 }
1738 
1739 /*
1740  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
1741  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1742  */
1743 void
1744 xyc_xyreset(xycsc, xysc)
1745 	struct xyc_softc *xycsc;
1746 	struct xy_softc *xysc;
1747 
1748 {
1749 	struct xy_iopb tmpiopb;
1750 	struct xy_iopb *iopb;
1751 	int     del;
1752 
1753 	iopb = xycsc->ciopb;
1754 
1755 	/* Save contents */
1756 	bcopy(iopb, &tmpiopb, sizeof(struct xy_iopb));
1757 
1758 	iopb->chen = iopb->done = iopb->errs = 0;
1759 	iopb->ien = 0;
1760 	iopb->com = XYCMD_RST;
1761 	iopb->unit = xysc->xy_drive;
1762 
1763 	XYC_GO(xycsc->xyc, (u_long)xycsc->ciorq->dmaiopb);
1764 
1765 	del = XYC_RESETUSEC;
1766 	while (del > 0) {
1767 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0)
1768 			break;
1769 		DELAY(1);
1770 		del--;
1771 	}
1772 
1773 	if (del <= 0 || iopb->errs) {
1774 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
1775 		    xyc_e2str(iopb->errno));
1776 		del = xycsc->xyc->xyc_rsetup;
1777 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1778 			panic("xyc_reset");
1779 	} else {
1780 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
1781 	}
1782 
1783 	/* Restore contents */
1784 	bcopy(&tmpiopb, iopb, sizeof(struct xy_iopb));
1785 }
1786 
1787 
1788 /*
1789  * xyc_reset: reset everything: requests are marked as errors except
1790  * a polled request (which is resubmitted)
1791  */
1792 int
1793 xyc_reset(xycsc, quiet, blastmode, error, xysc)
1794 	struct xyc_softc *xycsc;
1795 	int     quiet, error;
1796 	struct xy_iorq *blastmode;
1797 	struct xy_softc *xysc;
1798 
1799 {
1800 	int     del = 0, lcv, retval = XY_ERR_AOK;
1801 
1802 	/* soft reset hardware */
1803 
1804 	if (!quiet)
1805 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
1806 	del = xycsc->xyc->xyc_rsetup;
1807 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1808 	if (del == XY_ERR_FAIL) {
1809 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
1810 		retval = XY_ERR_FAIL;
1811 	}
1812 	if (xysc)
1813 		xyc_xyreset(xycsc, xysc);
1814 
1815 	/* fix queues based on "blast-mode" */
1816 
1817 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1818 		register struct xy_iorq *iorq = &xycsc->reqs[lcv];
1819 
1820 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1821 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1822 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
1823 			/* is it active? */
1824 			continue;
1825 
1826 		if (blastmode == XY_RSET_ALL ||
1827 				blastmode != iorq) {
1828 			/* failed */
1829 			iorq->errno = error;
1830 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1831 			switch (XY_STATE(iorq->mode)) {
1832 			case XY_SUB_NORM:
1833 			    iorq->buf->b_error = EIO;
1834 			    iorq->buf->b_flags |= B_ERROR;
1835 			    iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
1836 
1837 			    bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1838 					iorq->dmamap->dm_mapsize,
1839 					(iorq->buf->b_flags & B_READ)
1840 						? BUS_DMASYNC_POSTREAD
1841 						: BUS_DMASYNC_POSTWRITE);
1842 
1843 			    bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1844 
1845 			    (void)BUFQ_GET(&iorq->xy->xyq);
1846 			    disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1847 				(xycsc->reqs[lcv].buf->b_bcount -
1848 				xycsc->reqs[lcv].buf->b_resid),
1849 				(xycsc->reqs[lcv].buf->b_flags & B_READ));
1850 			    biodone(iorq->buf);
1851 			    iorq->mode = XY_SUB_FREE;
1852 			    break;
1853 			case XY_SUB_WAIT:
1854 			    wakeup(iorq);
1855 			case XY_SUB_POLL:
1856 			    iorq->mode =
1857 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1858 			    break;
1859 			}
1860 
1861 		} else {
1862 
1863 			/* resubmit, no need to do anything here */
1864 		}
1865 	}
1866 
1867 	/*
1868 	 * now, if stuff is waiting, start it.
1869 	 * since we just reset it should go
1870 	 */
1871 	xyc_start(xycsc, NULL);
1872 
1873 	return (retval);
1874 }
1875 
1876 /*
1877  * xyc_start: start waiting buffers
1878  */
1879 
1880 void
1881 xyc_start(xycsc, iorq)
1882 	struct xyc_softc *xycsc;
1883 	struct xy_iorq *iorq;
1884 
1885 {
1886 	int lcv;
1887 	struct xy_softc *xy;
1888 
1889 	if (iorq == NULL) {
1890 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1891 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1892 			if (BUFQ_PEEK(&xy->xyq) == NULL) continue;
1893 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
1894 			xyc_startbuf(xycsc, xy, BUFQ_PEEK(&xy->xyq));
1895 		}
1896 	}
1897 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1898 }
1899 
1900 /*
1901  * xyc_remove_iorq: remove "done" IOPB's.
1902  */
1903 
1904 int
1905 xyc_remove_iorq(xycsc)
1906 	struct xyc_softc *xycsc;
1907 
1908 {
1909 	int     errno, rq, comm, errs;
1910 	struct xyc *xyc = xycsc->xyc;
1911 	u_long  addr;
1912 	struct xy_iopb *iopb;
1913 	struct xy_iorq *iorq;
1914 	struct buf *bp;
1915 
1916 	if (xyc->xyc_csr & XYC_DERR) {
1917 		/*
1918 		 * DOUBLE ERROR: should never happen under normal use. This
1919 		 * error is so bad, you can't even tell which IOPB is bad, so
1920 		 * we dump them all.
1921 		 */
1922 		errno = XY_ERR_DERR;
1923 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1924 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
1925 			printf("%s: soft reset failed!\n",
1926 				xycsc->sc_dev.dv_xname);
1927 			panic("xyc_remove_iorq: controller DEAD");
1928 		}
1929 		return (XY_ERR_AOK);
1930 	}
1931 
1932 	/*
1933 	 * get iopb that is done, loop down the chain
1934 	 */
1935 
1936 	if (xyc->xyc_csr & XYC_ERR) {
1937 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
1938 	}
1939 	if (xyc->xyc_csr & XYC_IPND) {
1940 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1941 	}
1942 
1943 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1944 		iorq = xycsc->xy_chain[rq];
1945 		if (iorq == NULL) break; /* done ! */
1946 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1947 			continue;	/* free, or done */
1948 		iopb = iorq->iopb;
1949 		if (iopb->done == 0)
1950 			continue;	/* not done yet */
1951 
1952 		comm = iopb->com;
1953 		errs = iopb->errs;
1954 
1955 		if (errs)
1956 			iorq->errno = iopb->errno;
1957 		else
1958 			iorq->errno = 0;
1959 
1960 		/* handle non-fatal errors */
1961 
1962 		if (errs &&
1963 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1964 			continue;	/* AOK: we resubmitted it */
1965 
1966 
1967 		/* this iorq is now done (hasn't been restarted or anything) */
1968 
1969 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1970 			xyc_perror(iorq, iopb, 0);
1971 
1972 		/* now, if read/write check to make sure we got all the data
1973 		 * we needed. (this may not be the case if we got an error in
1974 		 * the middle of a multisector request).   */
1975 
1976 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1977 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
1978 			/* we just successfully processed a bad144 sector
1979 			 * note: if we are in bad 144 mode, the pointers have
1980 			 * been advanced already (see above) and are pointing
1981 			 * at the bad144 sector.   to exit bad144 mode, we
1982 			 * must advance the pointers 1 sector and issue a new
1983 			 * request if there are still sectors left to process
1984 			 *
1985 			 */
1986 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
1987 
1988 			/* exit b144 mode */
1989 			iorq->mode = iorq->mode & (~XY_MODE_B144);
1990 
1991 			if (iorq->sectcnt) {	/* more to go! */
1992 				iorq->lasterror = iorq->errno = iopb->errno = 0;
1993 				iopb->errs = iopb->done = 0;
1994 				iorq->tries = 0;
1995 				iopb->scnt = iorq->sectcnt;
1996 				iopb->cyl = iorq->blockno /
1997 						iorq->xy->sectpercyl;
1998 				iopb->head =
1999 					(iorq->blockno / iorq->xy->nhead) %
2000 						iorq->xy->nhead;
2001 				iopb->sect = iorq->blockno % XYFM_BPS;
2002 				addr = (u_long) iorq->dbuf;
2003 				iopb->dataa = (addr & 0xffff);
2004 				iopb->datar = ((addr & 0xff0000) >> 16);
2005 				/* will resubit at end */
2006 				continue;
2007 			}
2008 		}
2009 		/* final cleanup, totally done with this request */
2010 
2011 		switch (XY_STATE(iorq->mode)) {
2012 		case XY_SUB_NORM:
2013 			bp = iorq->buf;
2014 			if (errs) {
2015 				bp->b_error = EIO;
2016 				bp->b_flags |= B_ERROR;
2017 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
2018 			} else {
2019 				bp->b_resid = 0;	/* done */
2020 			}
2021 			bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
2022 					iorq->dmamap->dm_mapsize,
2023 					(iorq->buf->b_flags & B_READ)
2024 						? BUS_DMASYNC_POSTREAD
2025 						: BUS_DMASYNC_POSTWRITE);
2026 
2027 			bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
2028 
2029 			(void)BUFQ_GET(&iorq->xy->xyq);
2030 			disk_unbusy(&iorq->xy->sc_dk,
2031 			    (bp->b_bcount - bp->b_resid),
2032 			    (bp->b_flags & B_READ));
2033 			iorq->mode = XY_SUB_FREE;
2034 			biodone(bp);
2035 			break;
2036 		case XY_SUB_WAIT:
2037 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2038 			wakeup(iorq);
2039 			break;
2040 		case XY_SUB_POLL:
2041 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2042 			break;
2043 		}
2044 	}
2045 
2046 	return (XY_ERR_AOK);
2047 }
2048 
2049 /*
2050  * xyc_perror: print error.
2051  * - if still_trying is true: we got an error, retried and got a
2052  *   different error.  in that case lasterror is the old error,
2053  *   and errno is the new one.
2054  * - if still_trying is not true, then if we ever had an error it
2055  *   is in lasterror. also, if iorq->errno == 0, then we recovered
2056  *   from that error (otherwise iorq->errno == iorq->lasterror).
2057  */
2058 void
2059 xyc_perror(iorq, iopb, still_trying)
2060 	struct xy_iorq *iorq;
2061 	struct xy_iopb *iopb;
2062 	int     still_trying;
2063 
2064 {
2065 
2066 	int     error = iorq->lasterror;
2067 
2068 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
2069 	    : iorq->xyc->sc_dev.dv_xname);
2070 	if (iorq->buf)
2071 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
2072 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
2073 		printf("%s %d/%d/%d: ",
2074 			(iopb->com == XYCMD_RD) ? "read" : "write",
2075 			iopb->cyl, iopb->head, iopb->sect);
2076 	printf("%s", xyc_e2str(error));
2077 
2078 	if (still_trying)
2079 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
2080 	else
2081 		if (iorq->errno == 0)
2082 			printf(" [recovered in %d tries]", iorq->tries);
2083 
2084 	printf("\n");
2085 }
2086 
2087 /*
2088  * xyc_error: non-fatal error encountered... recover.
2089  * return AOK if resubmitted, return FAIL if this iopb is done
2090  */
2091 int
2092 xyc_error(xycsc, iorq, iopb, comm)
2093 	struct xyc_softc *xycsc;
2094 	struct xy_iorq *iorq;
2095 	struct xy_iopb *iopb;
2096 	int     comm;
2097 
2098 {
2099 	int     errno = iorq->errno;
2100 	int     erract = xyc_entoact(errno);
2101 	int     oldmode, advance;
2102 #ifdef __sparc__
2103 	int i;
2104 #endif
2105 
2106 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
2107 		oldmode = iorq->mode;
2108 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
2109 		/* make xyc_start ignore us */
2110 		xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
2111 		iorq->mode = oldmode;
2112 	}
2113 	/* check for read/write to a sector in bad144 table if bad: redirect
2114 	 * request to bad144 area */
2115 
2116 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
2117 	    (iorq->mode & XY_MODE_B144) == 0) {
2118 		advance = iorq->sectcnt - iopb->scnt;
2119 		XYC_ADVANCE(iorq, advance);
2120 #ifdef __sparc__
2121 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
2122 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
2123 			    iorq->blockno % iorq->xy->nsect)) != -1) {
2124 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
2125 							 * redirect */
2126 			iopb->errno = iopb->done = iopb->errs = 0;
2127 			iopb->scnt = 1;
2128 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
2129 			/* second to last acyl */
2130 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
2131 								 * standard */
2132 			iopb->head = i / iorq->xy->nhead;
2133 			iopb->sect = i % iorq->xy->nhead;
2134 			/* will resubmit when we come out of remove_iorq */
2135 			return (XY_ERR_AOK);	/* recovered! */
2136 		}
2137 #endif
2138 	}
2139 
2140 	/*
2141 	 * it isn't a bad144 sector, must be real error! see if we can retry
2142 	 * it?
2143 	 */
2144 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
2145 		xyc_perror(iorq, iopb, 1);	/* inform of error state
2146 						 * change */
2147 	iorq->lasterror = errno;
2148 
2149 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
2150 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
2151 		iorq->tries++;
2152 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
2153 		/* will resubmit at end of remove_iorq */
2154 		return (XY_ERR_AOK);	/* recovered! */
2155 	}
2156 
2157 	/* failed to recover from this error */
2158 	return (XY_ERR_FAIL);
2159 }
2160 
2161 /*
2162  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
2163  */
2164 void
2165 xyc_tick(arg)
2166 	void   *arg;
2167 
2168 {
2169 	struct xyc_softc *xycsc = arg;
2170 	int     lcv, s, reset = 0;
2171 
2172 	/* reduce ttl for each request if one goes to zero, reset xyc */
2173 	s = splbio();
2174 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
2175 		if (xycsc->reqs[lcv].mode == 0 ||
2176 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
2177 			continue;
2178 		xycsc->reqs[lcv].ttl--;
2179 		if (xycsc->reqs[lcv].ttl == 0)
2180 			reset = 1;
2181 	}
2182 	if (reset) {
2183 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
2184 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
2185 	}
2186 	splx(s);
2187 
2188 	/* until next time */
2189 
2190 	callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
2191 }
2192 
2193 /*
2194  * xyc_ioctlcmd: this function provides a user level interface to the
2195  * controller via ioctl.   this allows "format" programs to be written
2196  * in user code, and is also useful for some debugging.   we return
2197  * an error code.   called at user priority.
2198  *
2199  * XXX missing a few commands (see the 7053 driver for ideas)
2200  */
2201 int
2202 xyc_ioctlcmd(xy, dev, xio)
2203 	struct xy_softc *xy;
2204 	dev_t   dev;
2205 	struct xd_iocmd *xio;
2206 
2207 {
2208 	int     s, rqno, dummy = 0;
2209 	caddr_t dvmabuf = NULL, buf = NULL;
2210 	struct xyc_softc *xycsc;
2211 	int			rseg, error;
2212 	bus_dma_segment_t	seg;
2213 
2214 	/* check sanity of requested command */
2215 
2216 	switch (xio->cmd) {
2217 
2218 	case XYCMD_NOP:	/* no op: everything should be zero */
2219 		if (xio->subfn || xio->dptr || xio->dlen ||
2220 		    xio->block || xio->sectcnt)
2221 			return (EINVAL);
2222 		break;
2223 
2224 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
2225 	case XYCMD_WR:
2226 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2227 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2228 			return (EINVAL);
2229 		break;
2230 
2231 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
2232 		return (EINVAL);
2233 
2234 		break;
2235 
2236 	default:
2237 		return (EINVAL);/* ??? */
2238 	}
2239 
2240 	xycsc = xy->parent;
2241 
2242 	/* create DVMA buffer for request if needed */
2243 	if (xio->dlen) {
2244 		bus_addr_t busbuf;
2245 
2246 		if ((error = xy_dmamem_alloc(xycsc->dmatag, xycsc->auxmap,
2247 					     &seg, &rseg,
2248 					     xio->dlen, &buf,
2249 					     &busbuf)) != 0) {
2250 			return (error);
2251 		}
2252 		dvmabuf = (caddr_t)(u_long)BUS_ADDR_PADDR(busbuf);
2253 
2254 		if (xio->cmd == XYCMD_WR) {
2255 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
2256 				bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
2257 				bus_dmamem_free(xycsc->dmatag, &seg, rseg);
2258 				return (error);
2259 			}
2260 		}
2261 	}
2262 	/* do it! */
2263 
2264 	error = 0;
2265 	s = splbio();
2266 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2267 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2268 	if (rqno == XY_ERR_FAIL) {
2269 		error = EIO;
2270 		goto done;
2271 	}
2272 	xio->errno = xycsc->ciorq->errno;
2273 	xio->tries = xycsc->ciorq->tries;
2274 	XYC_DONE(xycsc, dummy);
2275 
2276 	if (xio->cmd == XYCMD_RD)
2277 		error = copyout(buf, xio->dptr, xio->dlen);
2278 
2279 done:
2280 	splx(s);
2281 	if (dvmabuf) {
2282 		xy_dmamem_free(xycsc->dmatag, xycsc->auxmap, &seg, rseg,
2283 				xio->dlen, buf);
2284 	}
2285 	return (error);
2286 }
2287 
2288 /*
2289  * xyc_e2str: convert error code number into an error string
2290  */
2291 char *
2292 xyc_e2str(no)
2293 	int     no;
2294 {
2295 	switch (no) {
2296 	case XY_ERR_FAIL:
2297 		return ("Software fatal error");
2298 	case XY_ERR_DERR:
2299 		return ("DOUBLE ERROR");
2300 	case XY_ERR_AOK:
2301 		return ("Successful completion");
2302 	case XY_ERR_IPEN:
2303 		return("Interrupt pending");
2304 	case XY_ERR_BCFL:
2305 		return("Busy conflict");
2306 	case XY_ERR_TIMO:
2307 		return("Operation timeout");
2308 	case XY_ERR_NHDR:
2309 		return("Header not found");
2310 	case XY_ERR_HARD:
2311 		return("Hard ECC error");
2312 	case XY_ERR_ICYL:
2313 		return("Illegal cylinder address");
2314 	case XY_ERR_ISEC:
2315 		return("Illegal sector address");
2316 	case XY_ERR_SMAL:
2317 		return("Last sector too small");
2318 	case XY_ERR_SACK:
2319 		return("Slave ACK error (non-existent memory)");
2320 	case XY_ERR_CHER:
2321 		return("Cylinder and head/header error");
2322 	case XY_ERR_SRTR:
2323 		return("Auto-seek retry successful");
2324 	case XY_ERR_WPRO:
2325 		return("Write-protect error");
2326 	case XY_ERR_UIMP:
2327 		return("Unimplemented command");
2328 	case XY_ERR_DNRY:
2329 		return("Drive not ready");
2330 	case XY_ERR_SZER:
2331 		return("Sector count zero");
2332 	case XY_ERR_DFLT:
2333 		return("Drive faulted");
2334 	case XY_ERR_ISSZ:
2335 		return("Illegal sector size");
2336 	case XY_ERR_SLTA:
2337 		return("Self test A");
2338 	case XY_ERR_SLTB:
2339 		return("Self test B");
2340 	case XY_ERR_SLTC:
2341 		return("Self test C");
2342 	case XY_ERR_SOFT:
2343 		return("Soft ECC error");
2344 	case XY_ERR_SFOK:
2345 		return("Soft ECC error recovered");
2346 	case XY_ERR_IHED:
2347 		return("Illegal head");
2348 	case XY_ERR_DSEQ:
2349 		return("Disk sequencer error");
2350 	case XY_ERR_SEEK:
2351 		return("Seek error");
2352 	default:
2353 		return ("Unknown error");
2354 	}
2355 }
2356 
2357 int
2358 xyc_entoact(errno)
2359 
2360 int errno;
2361 
2362 {
2363   switch (errno) {
2364     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
2365     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
2366     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
2367     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
2368     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
2369 
2370 	return(XY_ERA_PROG); /* program error ! */
2371 
2372     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
2373     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
2374     case XY_ERR_SOFT:
2375 
2376 	return(XY_ERA_HARD); /* hard error, retry */
2377 
2378     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
2379 
2380 	return(XY_ERA_RSET); /* hard error reset */
2381 
2382     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
2383 
2384 	return(XY_ERA_SOFT); /* an FYI error */
2385 
2386     case XY_ERR_WPRO:
2387 
2388 	return(XY_ERA_WPRO); /* write protect */
2389   }
2390 
2391   return(XY_ERA_PROG); /* ??? */
2392 }
2393