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