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