xref: /netbsd-src/sys/dev/vme/xy.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: xy.c,v 1.41 2002/09/06 13:18:43 gehenna 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.41 2002/09/06 13:18:43 gehenna 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 struct cfattach xyc_ca = {
197 	sizeof(struct xyc_softc), xycmatch, xycattach
198 };
199 
200 struct cfattach xy_ca = {
201 	sizeof(struct xy_softc), xymatch, xyattach
202 };
203 
204 extern struct cfdriver xy_cd;
205 
206 dev_type_open(xyopen);
207 dev_type_close(xyclose);
208 dev_type_read(xyread);
209 dev_type_write(xywrite);
210 dev_type_ioctl(xyioctl);
211 dev_type_strategy(xystrategy);
212 dev_type_dump(xydump);
213 dev_type_size(xysize);
214 
215 const struct bdevsw xy_bdevsw = {
216 	xyopen, xyclose, xystrategy, xyioctl, xydump, xysize, D_DISK
217 };
218 
219 const struct cdevsw xy_cdevsw = {
220 	xyopen, xyclose, xyread, xywrite, xyioctl,
221 	nostop, notty, nopoll, nommap, D_DISK
222 };
223 
224 struct xyc_attach_args {	/* this is the "aux" args to xyattach */
225 	int	driveno;	/* unit number */
226 	int	fullmode;	/* submit mode */
227 	int	booting;	/* are we booting or not? */
228 };
229 
230 /*
231  * dkdriver
232  */
233 
234 struct dkdriver xydkdriver = { xystrategy };
235 
236 /*
237  * start: disk label fix code (XXX)
238  */
239 
240 static void *xy_labeldata;
241 
242 static void
243 xydummystrat(bp)
244 	struct buf *bp;
245 {
246 	if (bp->b_bcount != XYFM_BPS)
247 		panic("xydummystrat");
248 	bcopy(xy_labeldata, bp->b_data, XYFM_BPS);
249 	bp->b_flags |= B_DONE;
250 	bp->b_flags &= ~B_BUSY;
251 }
252 
253 int
254 xygetdisklabel(xy, b)
255 	struct xy_softc *xy;
256 	void *b;
257 {
258 	char *err;
259 #if defined(__sparc__) || defined(sun3)
260 	struct sun_disklabel *sdl;
261 #endif
262 
263 	/* We already have the label data in `b'; setup for dummy strategy */
264 	xy_labeldata = b;
265 
266 	/* Required parameter for readdisklabel() */
267 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
268 
269 	err = readdisklabel(MAKEDISKDEV(0, xy->sc_dev.dv_unit, RAW_PART),
270 					xydummystrat,
271 				xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
272 	if (err) {
273 		printf("%s: %s\n", xy->sc_dev.dv_xname, err);
274 		return(XY_ERR_FAIL);
275 	}
276 
277 #if defined(__sparc__) || defined(sun3)
278 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
279 	sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
280 	if (sdl->sl_magic == SUN_DKMAGIC) {
281 		xy->pcyl = sdl->sl_pcylinders;
282 	} else
283 #endif
284 	{
285 		printf("%s: WARNING: no `pcyl' in disk label.\n",
286 			xy->sc_dev.dv_xname);
287 		xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
288 			xy->sc_dk.dk_label->d_acylinders;
289 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
290 		xy->sc_dev.dv_xname, xy->pcyl);
291 	}
292 
293 	xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
294 	xy->acyl = xy->sc_dk.dk_label->d_acylinders;
295 	xy->nhead = xy->sc_dk.dk_label->d_ntracks;
296 	xy->nsect = xy->sc_dk.dk_label->d_nsectors;
297 	xy->sectpercyl = xy->nhead * xy->nsect;
298 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
299                                           	  * sun->bsd */
300 	return(XY_ERR_AOK);
301 }
302 
303 /*
304  * end: disk label fix code (XXX)
305  */
306 
307 /*
308  * Shorthand for allocating, mapping and loading a DMA buffer
309  */
310 int
311 xy_dmamem_alloc(tag, map, seg, nsegp, len, kvap, dmap)
312 	bus_dma_tag_t		tag;
313 	bus_dmamap_t		map;
314 	bus_dma_segment_t	*seg;
315 	int			*nsegp;
316 	bus_size_t		len;
317 	caddr_t			*kvap;
318 	bus_addr_t		*dmap;
319 {
320 	int nseg;
321 	int error;
322 
323 	if ((error = bus_dmamem_alloc(tag, len, 0, 0,
324 				      seg, 1, &nseg, BUS_DMA_NOWAIT)) != 0) {
325 		return (error);
326 	}
327 
328 	if ((error = bus_dmamem_map(tag, seg, nseg,
329 				    len, kvap,
330 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
331 		bus_dmamem_free(tag, seg, nseg);
332 		return (error);
333 	}
334 
335 	if ((error = bus_dmamap_load(tag, map, *kvap, len, NULL,
336 				     BUS_DMA_NOWAIT)) != 0) {
337 		bus_dmamem_unmap(tag, *kvap, len);
338 		bus_dmamem_free(tag, seg, nseg);
339 		return (error);
340 	}
341 
342 	*dmap = map->dm_segs[0].ds_addr;
343 	*nsegp = nseg;
344 	return (0);
345 }
346 
347 void
348 xy_dmamem_free(tag, map, seg, nseg, len, kva)
349 	bus_dma_tag_t		tag;
350 	bus_dmamap_t		map;
351 	bus_dma_segment_t	*seg;
352 	int			nseg;
353 	bus_size_t		len;
354 	caddr_t			kva;
355 {
356 
357 	bus_dmamap_unload(tag, map);
358 	bus_dmamem_unmap(tag, kva, len);
359 	bus_dmamem_free(tag, seg, nseg);
360 }
361 
362 
363 /*
364  * a u t o c o n f i g   f u n c t i o n s
365  */
366 
367 /*
368  * xycmatch: determine if xyc is present or not.   we do a
369  * soft reset to detect the xyc.
370  */
371 int
372 xyc_probe(arg, tag, handle)
373 	void *arg;
374 	bus_space_tag_t tag;
375 	bus_space_handle_t handle;
376 {
377 	struct xyc *xyc = (void *)handle; /* XXX */
378 
379 	return ((xyc_unbusy(xyc, XYC_RESETUSEC) != XY_ERR_FAIL) ? 0 : EIO);
380 }
381 
382 int xycmatch(parent, cf, aux)
383 	struct device *parent;
384 	struct cfdata *cf;
385 	void *aux;
386 {
387 	struct vme_attach_args	*va = aux;
388 	vme_chipset_tag_t	ct = va->va_vct;
389 	vme_am_t		mod;
390 	int error;
391 
392 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
393 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
394 		return (0);
395 
396 	error = vme_probe(ct, va->r[0].offset, sizeof(struct xyc),
397 			  mod, VME_D16, xyc_probe, 0);
398 	vme_space_free(va->va_vct, va->r[0].offset, sizeof(struct xyc), mod);
399 
400 	return (error == 0);
401 }
402 
403 /*
404  * xycattach: attach controller
405  */
406 void
407 xycattach(parent, self, aux)
408 	struct device *parent, *self;
409 	void   *aux;
410 
411 {
412 	struct xyc_softc	*xyc = (void *) self;
413 	struct vme_attach_args	*va = aux;
414 	vme_chipset_tag_t	ct = va->va_vct;
415 	bus_space_tag_t		bt;
416 	bus_space_handle_t	bh;
417 	vme_intr_handle_t	ih;
418 	vme_am_t		mod;
419 	struct xyc_attach_args	xa;
420 	int			lcv, res, error;
421 	bus_dma_segment_t	seg;
422 	int			rseg;
423 	vme_mapresc_t resc;
424 
425 	/* get addressing and intr level stuff from autoconfig and load it
426 	 * into our xyc_softc. */
427 
428 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
429 
430 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
431 		panic("xyc: vme alloc");
432 
433 	if (vme_space_map(ct, va->r[0].offset, sizeof(struct xyc),
434 			  mod, VME_D16, 0, &bt, &bh, &resc) != 0)
435 		panic("xyc: vme_map");
436 
437 	xyc->xyc = (struct xyc *) bh; /* XXX */
438 	xyc->ipl = va->ilevel;
439 	xyc->vector = va->ivector;
440 	xyc->no_ols = 0; /* XXX should be from config */
441 
442 	for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
443 		xyc->sc_drives[lcv] = (struct xy_softc *) 0;
444 
445 	/*
446 	 * allocate and zero buffers
447 	 * check boundaries of the KVA's ... all IOPBs must reside in
448  	 * the same 64K region.
449 	 */
450 
451 	/* Get DMA handle for misc. transfers */
452 	if ((error = vme_dmamap_create(
453 				ct,		/* VME chip tag */
454 				MAXPHYS,	/* size */
455 				VME_AM_A24,	/* address modifier */
456 				VME_D16,	/* data size */
457 				0,		/* swap */
458 				1,		/* nsegments */
459 				MAXPHYS,	/* maxsegsz */
460 				0,		/* boundary */
461 				BUS_DMA_NOWAIT,
462 				&xyc->reqs[lcv].dmamap)) != 0) {
463 
464 		printf("%s: DMA buffer map create error %d\n",
465 			xyc->sc_dev.dv_xname, error);
466 		return;
467 	}
468 
469 	/* Get DMA handle for mapping iorq descriptors */
470 	if ((error = vme_dmamap_create(
471 				ct,		/* VME chip tag */
472 				XYC_MAXIOPB * sizeof(struct xy_iopb),
473 				VME_AM_A24,	/* address modifier */
474 				VME_D16,	/* data size */
475 				0,		/* swap */
476 				1,		/* nsegments */
477 				XYC_MAXIOPB * sizeof(struct xy_iopb),
478 				64*1024,	/* boundary */
479 				BUS_DMA_NOWAIT,
480 				&xyc->iopmap)) != 0) {
481 
482 		printf("%s: DMA buffer map create error %d\n",
483 			xyc->sc_dev.dv_xname, error);
484 		return;
485 	}
486 
487 	/* Get DMA buffer for iorq descriptors */
488 	if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->iopmap, &seg, &rseg,
489 				     XYC_MAXIOPB * sizeof(struct xy_iopb),
490 				     (caddr_t *)&xyc->iopbase,
491 				     (bus_addr_t *)&xyc->dvmaiopb)) != 0) {
492 		printf("%s: DMA buffer alloc error %d\n",
493 			xyc->sc_dev.dv_xname, error);
494 		return;
495 	}
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 	caddr_t			dmaddr;
633 	caddr_t			buf;
634 
635 	/*
636 	 * Always re-initialize the disk structure.  We want statistics
637 	 * to start with a clean slate.
638 	 */
639 	bzero(&xy->sc_dk, sizeof(xy->sc_dk));
640 	xy->sc_dk.dk_driver = &xydkdriver;
641 	xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
642 
643 	/* if booting, init the xy_softc */
644 
645 	if (xa->booting) {
646 		xy->state = XY_DRIVE_UNKNOWN;	/* to start */
647 		xy->flags = 0;
648 		xy->parent = xyc;
649 
650 		/* init queue of waiting bufs */
651 
652 		bufq_alloc(&xy->xyq, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
653 
654 		xy->xyrq = &xyc->reqs[xa->driveno];
655 
656 	}
657 	xy->xy_drive = xa->driveno;
658 	fmode = xa->fullmode;
659 	xyc->sc_drives[xa->driveno] = xy;
660 
661 	/* if not booting, make sure we are the only process in the attach for
662 	 * this drive.   if locked out, sleep on it. */
663 
664 	if (!xa->booting) {
665 		s = splbio();
666 		while (xy->state == XY_DRIVE_ATTACHING) {
667 			if (tsleep(&xy->state, PRIBIO, "xyattach", 0)) {
668 				splx(s);
669 				return;
670 			}
671 		}
672 		printf("%s at %s",
673 			xy->sc_dev.dv_xname, xy->parent->sc_dev.dv_xname);
674 	}
675 
676 	/* we now have control */
677 	xy->state = XY_DRIVE_ATTACHING;
678 	newstate = XY_DRIVE_UNKNOWN;
679 
680 	buf = NULL;
681 	if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->auxmap, &seg, &rseg,
682 				     XYFM_BPS,
683 				     (caddr_t *)&buf,
684 				     (bus_addr_t *)&dmaddr)) != 0) {
685 		printf("%s: DMA buffer alloc error %d\n",
686 			xyc->sc_dev.dv_xname, error);
687 		return;
688 	}
689 
690 	/* first try and reset the drive */
691 	error = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fmode);
692 	XYC_DONE(xyc, error);
693 	if (error == XY_ERR_DNRY) {
694 		printf(" drive %d: off-line\n", xa->driveno);
695 		goto done;
696 	}
697 	if (error) {
698 		printf(": ERROR 0x%02x (%s)\n", error, xyc_e2str(error));
699 		goto done;
700 	}
701 	printf(" drive %d: ready", xa->driveno);
702 
703 	/*
704 	 * now set drive parameters (to semi-bogus values) so we can read the
705 	 * disk label.
706 	 */
707 	xy->pcyl = xy->ncyl = 1;
708 	xy->acyl = 0;
709 	xy->nhead = 1;
710 	xy->nsect = 1;
711 	xy->sectpercyl = 1;
712 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
713 		xy->dkb.bt_bad[lcv].bt_cyl =
714 			xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
715 
716 	/* read disk label */
717 	for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
718 						xy->drive_type++) {
719 		error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
720 						dmaddr, fmode);
721 		XYC_DONE(xyc, error);
722 		if (error == XY_ERR_AOK) break;
723 	}
724 
725 	if (error != XY_ERR_AOK) {
726 		printf("\n%s: reading disk label failed: %s\n",
727 			xy->sc_dev.dv_xname, xyc_e2str(error));
728 		goto done;
729 	}
730 	printf(" (drive type %d)\n", xy->drive_type);
731 
732 	newstate = XY_DRIVE_NOLABEL;
733 
734 	xy->hw_spt = spt = 0; /* XXX needed ? */
735 	/* Attach the disk: must be before getdisklabel to malloc label */
736 	disk_attach(&xy->sc_dk);
737 
738 	if (xygetdisklabel(xy, buf) != XY_ERR_AOK)
739 		goto done;
740 
741 	/* inform the user of what is up */
742 	printf("%s: <%s>, pcyl %d\n", xy->sc_dev.dv_xname,
743 		buf, xy->pcyl);
744 	mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
745 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
746 		xy->sc_dev.dv_xname, mb, xy->ncyl, xy->nhead, xy->nsect,
747 		XYFM_BPS);
748 
749 	/*
750 	 * 450/451 stupidity: the drive type is encoded into the format
751 	 * of the disk.   the drive type in the IOPB must match the drive
752 	 * type in the format, or you will not be able to do I/O to the
753 	 * disk (you get header not found errors).  if you have two drives
754 	 * of different sizes that have the same drive type in their
755 	 * formatting then you are out of luck.
756 	 *
757 	 * this problem was corrected in the 753/7053.
758 	 */
759 
760 	for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
761 		oxy = xyc->sc_drives[lcv];
762 		if (oxy == NULL || oxy == xy) continue;
763 		if (oxy->drive_type != xy->drive_type) continue;
764 		if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
765 			xy->nhead != oxy->nhead) {
766 			printf("%s: %s and %s must be the same size!\n",
767 				xyc->sc_dev.dv_xname, xy->sc_dev.dv_xname,
768 				oxy->sc_dev.dv_xname);
769 			panic("xy drive size mismatch");
770 		}
771 	}
772 
773 
774 	/* now set the real drive parameters! */
775 
776 	blk = (xy->nsect - 1) +
777 		((xy->nhead - 1) * xy->nsect) +
778 		((xy->pcyl - 1) * xy->nsect * xy->nhead);
779 	error = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fmode);
780 	XYC_DONE(xyc, error);
781 	if (error) {
782 		printf("%s: write drive size failed: %s\n",
783 			xy->sc_dev.dv_xname, xyc_e2str(error));
784 		goto done;
785 	}
786 	newstate = XY_DRIVE_ONLINE;
787 
788 	/*
789 	 * read bad144 table. this table resides on the first sector of the
790 	 * last track of the disk (i.e. second cyl of "acyl" area).
791 	 */
792 
793 	blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
794 								/* last cyl */
795 	    (xy->nhead - 1) * xy->nsect;	/* last head */
796 	error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
797 						dmaddr, fmode);
798 	XYC_DONE(xyc, error);
799 	if (error) {
800 		printf("%s: reading bad144 failed: %s\n",
801 			xy->sc_dev.dv_xname, xyc_e2str(error));
802 		goto done;
803 	}
804 
805 	/* check dkbad for sanity */
806 	dkb = (struct dkbad *) buf;
807 	for (lcv = 0; lcv < 126; lcv++) {
808 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
809 				dkb->bt_bad[lcv].bt_cyl == 0) &&
810 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
811 			continue;	/* blank */
812 		if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
813 			break;
814 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
815 			break;
816 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
817 			break;
818 	}
819 	if (lcv != 126) {
820 		printf("%s: warning: invalid bad144 sector!\n",
821 			xy->sc_dev.dv_xname);
822 	} else {
823 		bcopy(buf, &xy->dkb, XYFM_BPS);
824 	}
825 
826 done:
827 	if (buf != NULL) {
828 		xy_dmamem_free(xyc->dmatag, xyc->auxmap,
829 				&seg, rseg, XYFM_BPS, buf);
830 	}
831 
832 	xy->state = newstate;
833 	if (!xa->booting) {
834 		wakeup(&xy->state);
835 		splx(s);
836 	}
837 }
838 
839 /*
840  * end of autoconfig functions
841  */
842 
843 /*
844  * { b , c } d e v s w   f u n c t i o n s
845  */
846 
847 /*
848  * xyclose: close device
849  */
850 int
851 xyclose(dev, flag, fmt, p)
852 	dev_t   dev;
853 	int     flag, fmt;
854 	struct proc *p;
855 
856 {
857 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
858 	int     part = DISKPART(dev);
859 
860 	/* clear mask bits */
861 
862 	switch (fmt) {
863 	case S_IFCHR:
864 		xy->sc_dk.dk_copenmask &= ~(1 << part);
865 		break;
866 	case S_IFBLK:
867 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
868 		break;
869 	}
870 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
871 
872 	return 0;
873 }
874 
875 /*
876  * xydump: crash dump system
877  */
878 int
879 xydump(dev, blkno, va, size)
880 	dev_t dev;
881 	daddr_t blkno;
882 	caddr_t va;
883 	size_t size;
884 {
885 	int     unit, part;
886 	struct xy_softc *xy;
887 
888 	unit = DISKUNIT(dev);
889 	if (unit >= xy_cd.cd_ndevs)
890 		return ENXIO;
891 	part = DISKPART(dev);
892 
893 	xy = xy_cd.cd_devs[unit];
894 
895 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
896 	    'a' + part);
897 
898 	return ENXIO;
899 
900 	/* outline: globals: "dumplo" == sector number of partition to start
901 	 * dump at (convert to physical sector with partition table)
902 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
903 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
904 	 * physmem)
905 	 *
906 	 * dump a copy of physical memory to the dump device starting at sector
907 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
908 	 * we go.   use polled I/O.
909 	 *
910 	 * XXX how to handle NON_CONTIG? */
911 
912 }
913 
914 /*
915  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
916  */
917 int
918 xyioctl(dev, command, addr, flag, p)
919 	dev_t   dev;
920 	u_long  command;
921 	caddr_t addr;
922 	int     flag;
923 	struct proc *p;
924 
925 {
926 	struct xy_softc *xy;
927 	struct xd_iocmd *xio;
928 	int     error, s, unit;
929 #ifdef __HAVE_OLD_DISKLABEL
930 	struct disklabel newlabel;
931 #endif
932 	struct disklabel *lp;
933 
934 	unit = DISKUNIT(dev);
935 
936 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
937 		return (ENXIO);
938 
939 	/* switch on ioctl type */
940 
941 	switch (command) {
942 	case DIOCSBAD:		/* set bad144 info */
943 		if ((flag & FWRITE) == 0)
944 			return EBADF;
945 		s = splbio();
946 		bcopy(addr, &xy->dkb, sizeof(xy->dkb));
947 		splx(s);
948 		return 0;
949 
950 	case DIOCGDINFO:	/* get disk label */
951 		bcopy(xy->sc_dk.dk_label, addr, sizeof(struct disklabel));
952 		return 0;
953 #ifdef __HAVE_OLD_DISKLABEL
954 	case ODIOCGDINFO:
955 		newlabel = *(xy->sc_dk.dk_label);
956 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
957 			return ENOTTY;
958 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
959 		return 0;
960 #endif
961 
962 	case DIOCGPART:	/* get partition info */
963 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
964 		((struct partinfo *) addr)->part =
965 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
966 		return 0;
967 
968 	case DIOCSDINFO:	/* set disk label */
969 #ifdef __HAVE_OLD_DISKLABEL
970 	case ODIOCSDINFO:
971 		if (command == ODIOCSDINFO) {
972 			memset(&newlabel, 0, sizeof newlabel);
973 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
974 			lp = &newlabel;
975 		} else
976 #endif
977 		lp = (struct disklabel *)addr;
978 
979 		if ((flag & FWRITE) == 0)
980 			return EBADF;
981 		error = setdisklabel(xy->sc_dk.dk_label,
982 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
983 		    xy->sc_dk.dk_cpulabel);
984 		if (error == 0) {
985 			if (xy->state == XY_DRIVE_NOLABEL)
986 				xy->state = XY_DRIVE_ONLINE;
987 		}
988 		return error;
989 
990 	case DIOCWLABEL:	/* change write status of disk label */
991 		if ((flag & FWRITE) == 0)
992 			return EBADF;
993 		if (*(int *) addr)
994 			xy->flags |= XY_WLABEL;
995 		else
996 			xy->flags &= ~XY_WLABEL;
997 		return 0;
998 
999 	case DIOCWDINFO:	/* write disk label */
1000 #ifdef __HAVE_OLD_DISKLABEL
1001 	case ODIOCWDINFO:
1002 		if (command == ODIOCWDINFO) {
1003 			memset(&newlabel, 0, sizeof newlabel);
1004 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1005 			lp = &newlabel;
1006 		} else
1007 #endif
1008 		lp = (struct disklabel *)addr;
1009 
1010 		if ((flag & FWRITE) == 0)
1011 			return EBADF;
1012 		error = setdisklabel(xy->sc_dk.dk_label,
1013 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
1014 		    xy->sc_dk.dk_cpulabel);
1015 		if (error == 0) {
1016 			if (xy->state == XY_DRIVE_NOLABEL)
1017 				xy->state = XY_DRIVE_ONLINE;
1018 
1019 			/* Simulate opening partition 0 so write succeeds. */
1020 			xy->sc_dk.dk_openmask |= (1 << 0);
1021 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
1022 			    xystrategy, xy->sc_dk.dk_label,
1023 			    xy->sc_dk.dk_cpulabel);
1024 			xy->sc_dk.dk_openmask =
1025 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1026 		}
1027 		return error;
1028 
1029 	case DIOSXDCMD:
1030 		xio = (struct xd_iocmd *) addr;
1031 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1032 			return (error);
1033 		return (xyc_ioctlcmd(xy, dev, xio));
1034 
1035 	default:
1036 		return ENOTTY;
1037 	}
1038 }
1039 
1040 /*
1041  * xyopen: open drive
1042  */
1043 
1044 int
1045 xyopen(dev, flag, fmt, p)
1046 	dev_t   dev;
1047 	int     flag, fmt;
1048 	struct proc *p;
1049 {
1050 	int     unit, part;
1051 	struct xy_softc *xy;
1052 	struct xyc_attach_args xa;
1053 
1054 	/* first, could it be a valid target? */
1055 
1056 	unit = DISKUNIT(dev);
1057 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
1058 		return (ENXIO);
1059 	part = DISKPART(dev);
1060 
1061 	/* do we need to attach the drive? */
1062 
1063 	if (xy->state == XY_DRIVE_UNKNOWN) {
1064 		xa.driveno = xy->xy_drive;
1065 		xa.fullmode = XY_SUB_WAIT;
1066 		xa.booting = 0;
1067 		xyattach((struct device *) xy->parent,
1068 						(struct device *) xy, &xa);
1069 		if (xy->state == XY_DRIVE_UNKNOWN) {
1070 			return (EIO);
1071 		}
1072 	}
1073 	/* check for partition */
1074 
1075 	if (part != RAW_PART &&
1076 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
1077 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1078 		return (ENXIO);
1079 	}
1080 	/* set open masks */
1081 
1082 	switch (fmt) {
1083 	case S_IFCHR:
1084 		xy->sc_dk.dk_copenmask |= (1 << part);
1085 		break;
1086 	case S_IFBLK:
1087 		xy->sc_dk.dk_bopenmask |= (1 << part);
1088 		break;
1089 	}
1090 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1091 
1092 	return 0;
1093 }
1094 
1095 int
1096 xyread(dev, uio, flags)
1097 	dev_t   dev;
1098 	struct uio *uio;
1099 	int flags;
1100 {
1101 
1102 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
1103 }
1104 
1105 int
1106 xywrite(dev, uio, flags)
1107 	dev_t   dev;
1108 	struct uio *uio;
1109 	int flags;
1110 {
1111 
1112 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
1113 }
1114 
1115 
1116 /*
1117  * xysize: return size of a partition for a dump
1118  */
1119 
1120 int
1121 xysize(dev)
1122 	dev_t   dev;
1123 
1124 {
1125 	struct xy_softc *xysc;
1126 	int     unit, part, size, omask;
1127 
1128 	/* valid unit? */
1129 	unit = DISKUNIT(dev);
1130 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
1131 		return (-1);
1132 
1133 	part = DISKPART(dev);
1134 	omask = xysc->sc_dk.dk_openmask & (1 << part);
1135 
1136 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
1137 		return (-1);
1138 
1139 	/* do it */
1140 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1141 		size = -1;	/* only give valid size for swap partitions */
1142 	else
1143 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
1144 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1145 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
1146 		return (-1);
1147 	return (size);
1148 }
1149 
1150 /*
1151  * xystrategy: buffering system interface to xy.
1152  */
1153 
1154 void
1155 xystrategy(bp)
1156 	struct buf *bp;
1157 
1158 {
1159 	struct xy_softc *xy;
1160 	int     s, unit;
1161 	struct xyc_attach_args xa;
1162 	struct disklabel *lp;
1163 	daddr_t blkno;
1164 
1165 	unit = DISKUNIT(bp->b_dev);
1166 
1167 	/* check for live device */
1168 
1169 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
1170 	    bp->b_blkno < 0 ||
1171 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
1172 		bp->b_error = EINVAL;
1173 		goto bad;
1174 	}
1175 	/* do we need to attach the drive? */
1176 
1177 	if (xy->state == XY_DRIVE_UNKNOWN) {
1178 		xa.driveno = xy->xy_drive;
1179 		xa.fullmode = XY_SUB_WAIT;
1180 		xa.booting = 0;
1181 		xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
1182 		if (xy->state == XY_DRIVE_UNKNOWN) {
1183 			bp->b_error = EIO;
1184 			goto bad;
1185 		}
1186 	}
1187 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1188 		/* no I/O to unlabeled disks, unless raw partition */
1189 		bp->b_error = EIO;
1190 		goto bad;
1191 	}
1192 	/* short circuit zero length request */
1193 
1194 	if (bp->b_bcount == 0)
1195 		goto done;
1196 
1197 	/* check bounds with label (disksubr.c).  Determine the size of the
1198 	 * transfer, and make sure it is within the boundaries of the
1199 	 * partition. Adjust transfer if needed, and signal errors or early
1200 	 * completion. */
1201 
1202 	lp = xy->sc_dk.dk_label;
1203 
1204 	if (bounds_check_with_label(bp, lp,
1205 		(xy->flags & XY_WLABEL) != 0) <= 0)
1206 		goto done;
1207 
1208 	/*
1209 	 * Now convert the block number to absolute and put it in
1210 	 * terms of the device's logical block size.
1211 	 */
1212 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1213 	if (DISKPART(bp->b_dev) != RAW_PART)
1214 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
1215 
1216 	bp->b_rawblkno = blkno;
1217 
1218 	/*
1219 	 * now we know we have a valid buf structure that we need to do I/O
1220 	 * on.
1221 	 */
1222 	s = splbio();		/* protect the queues */
1223 
1224 	BUFQ_PUT(&xy->xyq, bp);
1225 
1226 	/* start 'em up */
1227 
1228 	xyc_start(xy->parent, NULL);
1229 
1230 	/* done! */
1231 
1232 	splx(s);
1233 	return;
1234 
1235 bad:				/* tells upper layers we have an error */
1236 	bp->b_flags |= B_ERROR;
1237 done:				/* tells upper layers we are done with this
1238 				 * buf */
1239 	bp->b_resid = bp->b_bcount;
1240 	biodone(bp);
1241 }
1242 /*
1243  * end of {b,c}devsw functions
1244  */
1245 
1246 /*
1247  * i n t e r r u p t   f u n c t i o n
1248  *
1249  * xycintr: hardware interrupt.
1250  */
1251 int
1252 xycintr(v)
1253 	void   *v;
1254 
1255 {
1256 	struct xyc_softc *xycsc = v;
1257 
1258 	/* kick the event counter */
1259 
1260 	xycsc->sc_intrcnt.ev_count++;
1261 
1262 	/* remove as many done IOPBs as possible */
1263 
1264 	xyc_remove_iorq(xycsc);
1265 
1266 	/* start any iorq's already waiting */
1267 
1268 	xyc_start(xycsc, NULL);
1269 
1270 	return (1);
1271 }
1272 /*
1273  * end of interrupt function
1274  */
1275 
1276 /*
1277  * i n t e r n a l   f u n c t i o n s
1278  */
1279 
1280 /*
1281  * xyc_rqinit: fill out the fields of an I/O request
1282  */
1283 
1284 inline void
1285 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
1286 	struct xy_iorq *rq;
1287 	struct xyc_softc *xyc;
1288 	struct xy_softc *xy;
1289 	int     md;
1290 	u_long  blk;
1291 	int     cnt;
1292 	caddr_t db;
1293 	struct buf *bp;
1294 {
1295 	rq->xyc = xyc;
1296 	rq->xy = xy;
1297 	rq->ttl = XYC_MAXTTL + 10;
1298 	rq->mode = md;
1299 	rq->tries = rq->errno = rq->lasterror = 0;
1300 	rq->blockno = blk;
1301 	rq->sectcnt = cnt;
1302 	rq->dbuf = db;
1303 	rq->buf = bp;
1304 }
1305 
1306 /*
1307  * xyc_rqtopb: load up an IOPB based on an iorq
1308  */
1309 
1310 void
1311 xyc_rqtopb(iorq, iopb, cmd, subfun)
1312 	struct xy_iorq *iorq;
1313 	struct xy_iopb *iopb;
1314 	int     cmd, subfun;
1315 
1316 {
1317 	u_long  block, dp;
1318 
1319 	/* normal IOPB case, standard stuff */
1320 
1321 	/* chain bit handled later */
1322 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
1323 	iopb->com = cmd;
1324 	iopb->errno = 0;
1325 	iopb->errs = 0;
1326 	iopb->done = 0;
1327 	if (iorq->xy) {
1328 		iopb->unit = iorq->xy->xy_drive;
1329 		iopb->dt = iorq->xy->drive_type;
1330 	} else {
1331 		iopb->unit = 0;
1332 		iopb->dt = 0;
1333 	}
1334 	block = iorq->blockno;
1335 	if (iorq->xy == NULL || block == 0) {
1336 		iopb->sect = iopb->head = iopb->cyl = 0;
1337 	} else {
1338 		iopb->sect = block % iorq->xy->nsect;
1339 		block = block / iorq->xy->nsect;
1340 		iopb->head = block % iorq->xy->nhead;
1341 		block = block / iorq->xy->nhead;
1342 		iopb->cyl = block;
1343 	}
1344 	iopb->scnt = iorq->sectcnt;
1345 	dp = (u_long) iorq->dbuf;
1346 	if (iorq->dbuf == NULL) {
1347 		iopb->dataa = 0;
1348 		iopb->datar = 0;
1349 	} else {
1350 		iopb->dataa = (dp & 0xffff);
1351 		iopb->datar = ((dp & 0xff0000) >> 16);
1352 	}
1353 	iopb->subfn = subfun;
1354 }
1355 
1356 
1357 /*
1358  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
1359  */
1360 
1361 int
1362 xyc_unbusy(xyc, del)
1363 
1364 struct xyc *xyc;
1365 int del;
1366 
1367 {
1368 	while (del-- > 0) {
1369 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
1370 			break;
1371 		DELAY(1);
1372 	}
1373 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
1374 }
1375 
1376 /*
1377  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
1378  * note that NORM requests are handled separately.
1379  */
1380 int
1381 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1382 	struct xyc_softc *xycsc;
1383 	int     cmd, subfn, unit, block, scnt;
1384 	char   *dptr;
1385 	int     fullmode;
1386 
1387 {
1388 	int     submode = XY_STATE(fullmode);
1389 	struct xy_iorq *iorq = xycsc->ciorq;
1390 	struct xy_iopb *iopb = xycsc->ciopb;
1391 
1392 	/*
1393 	 * is someone else using the control iopq wait for it if we can
1394 	 */
1395 start:
1396 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
1397 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
1398                                 return(XY_ERR_FAIL);
1399 		goto start;
1400 	}
1401 
1402 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
1403 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
1404 		iorq->mode = XY_SUB_FREE;
1405 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
1406 	}
1407 
1408 	/* init iorq/iopb */
1409 
1410 	xyc_rqinit(iorq, xycsc,
1411 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
1412 	    fullmode, block, scnt, dptr, NULL);
1413 
1414 	/* load IOPB from iorq */
1415 
1416 	xyc_rqtopb(iorq, iopb, cmd, subfn);
1417 
1418 	/* submit it for processing */
1419 
1420 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
1421 
1422 	return(XY_ERR_AOK);
1423 }
1424 
1425 /*
1426  * xyc_startbuf
1427  * start a buffer for running
1428  */
1429 
1430 int
1431 xyc_startbuf(xycsc, xysc, bp)
1432 	struct xyc_softc *xycsc;
1433 	struct xy_softc *xysc;
1434 	struct buf *bp;
1435 
1436 {
1437 	int     partno, error;
1438 	struct xy_iorq *iorq;
1439 	struct xy_iopb *iopb;
1440 	u_long  block;
1441 
1442 	iorq = xysc->xyrq;
1443 	iopb = iorq->iopb;
1444 
1445 	/* get buf */
1446 
1447 	if (bp == NULL)
1448 		panic("xyc_startbuf null buf");
1449 
1450 	partno = DISKPART(bp->b_dev);
1451 #ifdef XYC_DEBUG
1452 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
1453 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1454 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1455 	    bp->b_bcount, bp->b_data);
1456 #endif
1457 
1458 	/*
1459 	 * load request.
1460 	 *
1461 	 * note that iorq points to the buffer as mapped into DVMA space,
1462 	 * where as the bp->b_data points to its non-DVMA mapping.
1463 	 */
1464 
1465 	block = bp->b_rawblkno;
1466 
1467 	error = bus_dmamap_load(xycsc->dmatag, iorq->dmamap,
1468 			bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
1469 	if (error != 0) {
1470 		printf("%s: warning: cannot load DMA map\n",
1471 			xycsc->sc_dev.dv_xname);
1472 		return (XY_ERR_FAIL);	/* XXX: need some sort of
1473 					 * call-back scheme here? */
1474 	}
1475 
1476 	bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1477 			iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
1478 				? BUS_DMASYNC_PREREAD
1479 				: BUS_DMASYNC_PREWRITE);
1480 
1481 	/* init iorq and load iopb from it */
1482 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
1483 		   bp->b_bcount / XYFM_BPS,
1484 		   (caddr_t)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
1485 		   bp);
1486 
1487 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
1488 
1489 	/* Instrumentation. */
1490 	disk_busy(&xysc->sc_dk);
1491 
1492 	return (XY_ERR_AOK);
1493 }
1494 
1495 
1496 /*
1497  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
1498  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
1499  *
1500  * note: caller frees iorq in all cases except NORM
1501  *
1502  * return value:
1503  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
1504  *   WAIT: XY_AOK (success), <error-code> (failed)
1505  *   POLL: <same as WAIT>
1506  *   NOQ : <same as NORM>
1507  *
1508  * there are three sources for i/o requests:
1509  * [1] xystrategy: normal block I/O, using "struct buf" system.
1510  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1511  * [3] open/ioctl: these are I/O requests done in the context of a process,
1512  *                 and the process should block until they are done.
1513  *
1514  * software state is stored in the iorq structure.  each iorq has an
1515  * iopb structure.  the hardware understands the iopb structure.
1516  * every command must go through an iopb.  a 450 handles one iopb at a
1517  * time, where as a 451 can take them in chains.  [the 450 claims it
1518  * can handle chains, but is appears to be buggy...]   iopb are allocated
1519  * in DVMA space at boot up time.  each disk gets one iopb, and the
1520  * controller gets one (for POLL and WAIT commands).  what happens if
1521  * the iopb is busy?  for i/o type [1], the buffers are queued at the
1522  * "buff" layer and * picked up later by the interrupt routine.  for case
1523  * [2] we can only be blocked if there is a WAIT type I/O request being
1524  * run.   since this can only happen when we are crashing, we wait a sec
1525  * and then steal the IOPB.  for case [3] the process can sleep
1526  * on the iorq free list until some iopbs are avaliable.
1527  */
1528 
1529 
1530 int
1531 xyc_submit_iorq(xycsc, iorq, type)
1532 	struct xyc_softc *xycsc;
1533 	struct xy_iorq *iorq;
1534 	int     type;
1535 
1536 {
1537 	struct xy_iopb *dmaiopb;
1538 
1539 #ifdef XYC_DEBUG
1540 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
1541 		xycsc->sc_dev.dv_xname, iorq, type);
1542 #endif
1543 
1544 	/* first check and see if controller is busy */
1545 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
1546 #ifdef XYC_DEBUG
1547 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
1548 #endif
1549 		if (type == XY_SUB_NOQ)
1550 			return (XY_ERR_FAIL);	/* failed */
1551 		switch (type) {
1552 		case XY_SUB_NORM:
1553 			return XY_ERR_AOK;	/* success */
1554 		case XY_SUB_WAIT:
1555 			while (iorq->iopb->done == 0) {
1556 				(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1557 			}
1558 			return (iorq->errno);
1559 		case XY_SUB_POLL:		/* steal controller */
1560 			(void)xycsc->xyc->xyc_rsetup; /* RESET */
1561 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
1562 				panic("xyc_submit_iorq: stuck xyc");
1563 			printf("%s: stole controller\n",
1564 				xycsc->sc_dev.dv_xname);
1565 			break;
1566 		default:
1567 			panic("xyc_submit_iorq adding");
1568 		}
1569 	}
1570 
1571 	dmaiopb = xyc_chain(xycsc, iorq);	 /* build chain */
1572 	if (dmaiopb == NULL) { /* nothing doing? */
1573 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
1574 			return(XY_ERR_AOK);
1575 		panic("xyc_submit_iorq: xyc_chain failed!\n");
1576 	}
1577 
1578 	XYC_GO(xycsc->xyc, (u_long)dmaiopb);
1579 
1580 	/* command now running, wrap it up */
1581 	switch (type) {
1582 	case XY_SUB_NORM:
1583 	case XY_SUB_NOQ:
1584 		return (XY_ERR_AOK);	/* success */
1585 	case XY_SUB_WAIT:
1586 		while (iorq->iopb->done == 0) {
1587 			(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1588 		}
1589 		return (iorq->errno);
1590 	case XY_SUB_POLL:
1591 		return (xyc_piodriver(xycsc, iorq));
1592 	default:
1593 		panic("xyc_submit_iorq wrap up");
1594 	}
1595 	panic("xyc_submit_iorq");
1596 	return 0;	/* not reached */
1597 }
1598 
1599 
1600 /*
1601  * xyc_chain: build a chain.  return dvma address of first element in
1602  * the chain.   iorq != NULL: means we only want that item on the chain.
1603  */
1604 
1605 struct xy_iopb *
1606 xyc_chain(xycsc, iorq)
1607 	struct xyc_softc *xycsc;
1608 	struct xy_iorq *iorq;
1609 
1610 {
1611 	int togo, chain, hand;
1612 
1613 	bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
1614 
1615 	/*
1616 	 * promote control IOPB to the top
1617 	 */
1618 	if (iorq == NULL) {
1619 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1620 		     XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1621 		     xycsc->iopbase[XYC_CTLIOPB].done == 0)
1622 			iorq = &xycsc->reqs[XYC_CTLIOPB];
1623 	}
1624 
1625 	/*
1626 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
1627 	 * we let these take priority and do them first.
1628 	 */
1629 	if (iorq) {
1630 		xycsc->xy_chain[0] = iorq;
1631 		iorq->iopb->chen = 0;
1632 		return(iorq->dmaiopb);
1633 	}
1634 
1635 	/*
1636 	 * NORM case: do round robin and maybe chain (if allowed and possible)
1637 	 */
1638 	chain = 0;
1639 	hand = xycsc->xy_hand;
1640 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1641 
1642 	for (togo = XYC_MAXIOPB; togo > 0;
1643 	     togo--, hand = (hand + 1) % XYC_MAXIOPB) {
1644 		struct xy_iopb *iopb, *prev_iopb, *dmaiopb;
1645 
1646 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1647 		    xycsc->iopbase[hand].done)
1648 			continue;   /* not ready-for-i/o */
1649 
1650 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1651 		iopb = xycsc->xy_chain[chain]->iopb;
1652 		iopb->chen = 0;
1653 		if (chain != 0) {
1654 			/* adding a link to a chain */
1655 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1656 			prev_iopb->chen = 1;
1657 			dmaiopb = xycsc->xy_chain[chain]->dmaiopb;
1658 			prev_iopb->nxtiopb = ((u_long)dmaiopb) & 0xffff;
1659 		} else {
1660 			/* head of chain */
1661 			iorq = xycsc->xy_chain[chain];
1662 		}
1663 		chain++;
1664 
1665 		/* quit if chaining dis-allowed */
1666 		if (xycsc->no_ols)
1667 			break;
1668 	}
1669 
1670 	return(iorq ? iorq->dmaiopb : NULL);
1671 }
1672 
1673 /*
1674  * xyc_piodriver
1675  *
1676  * programmed i/o driver.   this function takes over the computer
1677  * and drains off the polled i/o request.   it returns the status of the iorq
1678  * the caller is interesting in.
1679  */
1680 int
1681 xyc_piodriver(xycsc, iorq)
1682 	struct xyc_softc *xycsc;
1683 	struct xy_iorq  *iorq;
1684 
1685 {
1686 	int     nreset = 0;
1687 	int     retval = 0;
1688 	u_long  res;
1689 #ifdef XYC_DEBUG
1690 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
1691 #endif
1692 
1693 	while (iorq->iopb->done == 0) {
1694 
1695 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1696 
1697 		/* we expect some progress soon */
1698 		if (res == XY_ERR_FAIL && nreset >= 2) {
1699 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1700 #ifdef XYC_DEBUG
1701 			printf("xyc_piodriver: timeout\n");
1702 #endif
1703 			return (XY_ERR_FAIL);
1704 		}
1705 		if (res == XY_ERR_FAIL) {
1706 			if (xyc_reset(xycsc, 0,
1707 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
1708 				      XY_ERR_FAIL,
1709 				      0) == XY_ERR_FAIL)
1710 				return (XY_ERR_FAIL);	/* flushes all but POLL
1711 							 * requests, resets */
1712 			continue;
1713 		}
1714 
1715 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
1716 
1717 		if (iorq->iopb->done == 0)
1718 			xyc_start(xycsc, iorq);
1719 	}
1720 
1721 	/* get return value */
1722 
1723 	retval = iorq->errno;
1724 
1725 #ifdef XYC_DEBUG
1726 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1727 	    iorq->errno, xyc_e2str(iorq->errno));
1728 #endif
1729 
1730 	/* start up any bufs that have queued */
1731 
1732 	xyc_start(xycsc, NULL);
1733 
1734 	return (retval);
1735 }
1736 
1737 /*
1738  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
1739  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1740  */
1741 void
1742 xyc_xyreset(xycsc, xysc)
1743 	struct xyc_softc *xycsc;
1744 	struct xy_softc *xysc;
1745 
1746 {
1747 	struct xy_iopb tmpiopb;
1748 	struct xy_iopb *iopb;
1749 	int     del;
1750 
1751 	iopb = xycsc->ciopb;
1752 
1753 	/* Save contents */
1754 	bcopy(iopb, &tmpiopb, sizeof(struct xy_iopb));
1755 
1756 	iopb->chen = iopb->done = iopb->errs = 0;
1757 	iopb->ien = 0;
1758 	iopb->com = XYCMD_RST;
1759 	iopb->unit = xysc->xy_drive;
1760 
1761 	XYC_GO(xycsc->xyc, (u_long)xycsc->ciorq->dmaiopb);
1762 
1763 	del = XYC_RESETUSEC;
1764 	while (del > 0) {
1765 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0)
1766 			break;
1767 		DELAY(1);
1768 		del--;
1769 	}
1770 
1771 	if (del <= 0 || iopb->errs) {
1772 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
1773 		    xyc_e2str(iopb->errno));
1774 		del = xycsc->xyc->xyc_rsetup;
1775 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1776 			panic("xyc_reset");
1777 	} else {
1778 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
1779 	}
1780 
1781 	/* Restore contents */
1782 	bcopy(&tmpiopb, iopb, sizeof(struct xy_iopb));
1783 }
1784 
1785 
1786 /*
1787  * xyc_reset: reset everything: requests are marked as errors except
1788  * a polled request (which is resubmitted)
1789  */
1790 int
1791 xyc_reset(xycsc, quiet, blastmode, error, xysc)
1792 	struct xyc_softc *xycsc;
1793 	int     quiet, error;
1794 	struct xy_iorq *blastmode;
1795 	struct xy_softc *xysc;
1796 
1797 {
1798 	int     del = 0, lcv, retval = XY_ERR_AOK;
1799 
1800 	/* soft reset hardware */
1801 
1802 	if (!quiet)
1803 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
1804 	del = xycsc->xyc->xyc_rsetup;
1805 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1806 	if (del == XY_ERR_FAIL) {
1807 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
1808 		retval = XY_ERR_FAIL;
1809 	}
1810 	if (xysc)
1811 		xyc_xyreset(xycsc, xysc);
1812 
1813 	/* fix queues based on "blast-mode" */
1814 
1815 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1816 		register struct xy_iorq *iorq = &xycsc->reqs[lcv];
1817 
1818 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1819 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1820 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
1821 			/* is it active? */
1822 			continue;
1823 
1824 		if (blastmode == XY_RSET_ALL ||
1825 				blastmode != iorq) {
1826 			/* failed */
1827 			iorq->errno = error;
1828 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1829 			switch (XY_STATE(iorq->mode)) {
1830 			case XY_SUB_NORM:
1831 			    iorq->buf->b_error = EIO;
1832 			    iorq->buf->b_flags |= B_ERROR;
1833 			    iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
1834 
1835 			    bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1836 					iorq->dmamap->dm_mapsize,
1837 					(iorq->buf->b_flags & B_READ)
1838 						? BUS_DMASYNC_POSTREAD
1839 						: BUS_DMASYNC_POSTWRITE);
1840 
1841 			    bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1842 
1843 			    (void)BUFQ_GET(&iorq->xy->xyq);
1844 			    disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1845 				(xycsc->reqs[lcv].buf->b_bcount -
1846 				xycsc->reqs[lcv].buf->b_resid));
1847 			    biodone(iorq->buf);
1848 			    iorq->mode = XY_SUB_FREE;
1849 			    break;
1850 			case XY_SUB_WAIT:
1851 			    wakeup(iorq);
1852 			case XY_SUB_POLL:
1853 			    iorq->mode =
1854 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1855 			    break;
1856 			}
1857 
1858 		} else {
1859 
1860 			/* resubmit, no need to do anything here */
1861 		}
1862 	}
1863 
1864 	/*
1865 	 * now, if stuff is waiting, start it.
1866 	 * since we just reset it should go
1867 	 */
1868 	xyc_start(xycsc, NULL);
1869 
1870 	return (retval);
1871 }
1872 
1873 /*
1874  * xyc_start: start waiting buffers
1875  */
1876 
1877 void
1878 xyc_start(xycsc, iorq)
1879 	struct xyc_softc *xycsc;
1880 	struct xy_iorq *iorq;
1881 
1882 {
1883 	int lcv;
1884 	struct xy_softc *xy;
1885 
1886 	if (iorq == NULL) {
1887 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1888 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1889 			if (BUFQ_PEEK(&xy->xyq) == NULL) continue;
1890 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
1891 			xyc_startbuf(xycsc, xy, BUFQ_PEEK(&xy->xyq));
1892 		}
1893 	}
1894 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1895 }
1896 
1897 /*
1898  * xyc_remove_iorq: remove "done" IOPB's.
1899  */
1900 
1901 int
1902 xyc_remove_iorq(xycsc)
1903 	struct xyc_softc *xycsc;
1904 
1905 {
1906 	int     errno, rq, comm, errs;
1907 	struct xyc *xyc = xycsc->xyc;
1908 	u_long  addr;
1909 	struct xy_iopb *iopb;
1910 	struct xy_iorq *iorq;
1911 	struct buf *bp;
1912 
1913 	if (xyc->xyc_csr & XYC_DERR) {
1914 		/*
1915 		 * DOUBLE ERROR: should never happen under normal use. This
1916 		 * error is so bad, you can't even tell which IOPB is bad, so
1917 		 * we dump them all.
1918 		 */
1919 		errno = XY_ERR_DERR;
1920 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1921 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
1922 			printf("%s: soft reset failed!\n",
1923 				xycsc->sc_dev.dv_xname);
1924 			panic("xyc_remove_iorq: controller DEAD");
1925 		}
1926 		return (XY_ERR_AOK);
1927 	}
1928 
1929 	/*
1930 	 * get iopb that is done, loop down the chain
1931 	 */
1932 
1933 	if (xyc->xyc_csr & XYC_ERR) {
1934 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
1935 	}
1936 	if (xyc->xyc_csr & XYC_IPND) {
1937 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1938 	}
1939 
1940 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1941 		iorq = xycsc->xy_chain[rq];
1942 		if (iorq == NULL) break; /* done ! */
1943 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1944 			continue;	/* free, or done */
1945 		iopb = iorq->iopb;
1946 		if (iopb->done == 0)
1947 			continue;	/* not done yet */
1948 
1949 		comm = iopb->com;
1950 		errs = iopb->errs;
1951 
1952 		if (errs)
1953 			iorq->errno = iopb->errno;
1954 		else
1955 			iorq->errno = 0;
1956 
1957 		/* handle non-fatal errors */
1958 
1959 		if (errs &&
1960 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1961 			continue;	/* AOK: we resubmitted it */
1962 
1963 
1964 		/* this iorq is now done (hasn't been restarted or anything) */
1965 
1966 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1967 			xyc_perror(iorq, iopb, 0);
1968 
1969 		/* now, if read/write check to make sure we got all the data
1970 		 * we needed. (this may not be the case if we got an error in
1971 		 * the middle of a multisector request).   */
1972 
1973 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1974 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
1975 			/* we just successfully processed a bad144 sector
1976 			 * note: if we are in bad 144 mode, the pointers have
1977 			 * been advanced already (see above) and are pointing
1978 			 * at the bad144 sector.   to exit bad144 mode, we
1979 			 * must advance the pointers 1 sector and issue a new
1980 			 * request if there are still sectors left to process
1981 			 *
1982 			 */
1983 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
1984 
1985 			/* exit b144 mode */
1986 			iorq->mode = iorq->mode & (~XY_MODE_B144);
1987 
1988 			if (iorq->sectcnt) {	/* more to go! */
1989 				iorq->lasterror = iorq->errno = iopb->errno = 0;
1990 				iopb->errs = iopb->done = 0;
1991 				iorq->tries = 0;
1992 				iopb->scnt = iorq->sectcnt;
1993 				iopb->cyl = iorq->blockno /
1994 						iorq->xy->sectpercyl;
1995 				iopb->head =
1996 					(iorq->blockno / iorq->xy->nhead) %
1997 						iorq->xy->nhead;
1998 				iopb->sect = iorq->blockno % XYFM_BPS;
1999 				addr = (u_long) iorq->dbuf;
2000 				iopb->dataa = (addr & 0xffff);
2001 				iopb->datar = ((addr & 0xff0000) >> 16);
2002 				/* will resubit at end */
2003 				continue;
2004 			}
2005 		}
2006 		/* final cleanup, totally done with this request */
2007 
2008 		switch (XY_STATE(iorq->mode)) {
2009 		case XY_SUB_NORM:
2010 			bp = iorq->buf;
2011 			if (errs) {
2012 				bp->b_error = EIO;
2013 				bp->b_flags |= B_ERROR;
2014 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
2015 			} else {
2016 				bp->b_resid = 0;	/* done */
2017 			}
2018 			bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
2019 					iorq->dmamap->dm_mapsize,
2020 					(iorq->buf->b_flags & B_READ)
2021 						? BUS_DMASYNC_POSTREAD
2022 						: BUS_DMASYNC_POSTWRITE);
2023 
2024 			bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
2025 
2026 			(void)BUFQ_GET(&iorq->xy->xyq);
2027 			disk_unbusy(&iorq->xy->sc_dk,
2028 			    (bp->b_bcount - bp->b_resid));
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