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