xref: /netbsd-src/sys/dev/vme/xy.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: xy.c,v 1.94 2013/10/19 21:00:32 mrg 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.94 2013/10/19 21:00:32 mrg 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 #ifdef XYC_DEBUG
1401 	int     partno;
1402 #endif
1403 	int     error;
1404 	struct xy_iorq *iorq;
1405 	struct xy_iopb *iopb;
1406 	u_long  block;
1407 
1408 	iorq = xysc->xyrq;
1409 	iopb = iorq->iopb;
1410 
1411 	/* get buf */
1412 
1413 	if (bp == NULL)
1414 		panic("xyc_startbuf null buf");
1415 
1416 #ifdef XYC_DEBUG
1417 	partno = DISKPART(bp->b_dev);
1418 	printf("xyc_startbuf: %s%c: %s block %d\n", device_xname(xysc->sc_dev),
1419 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1420 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1421 	    bp->b_bcount, bp->b_data);
1422 #endif
1423 
1424 	/*
1425 	 * load request.
1426 	 *
1427 	 * note that iorq points to the buffer as mapped into DVMA space,
1428 	 * where as the bp->b_data points to its non-DVMA mapping.
1429 	 */
1430 
1431 	block = bp->b_rawblkno;
1432 
1433 	error = bus_dmamap_load(xycsc->dmatag, iorq->dmamap,
1434 			bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
1435 	if (error != 0) {
1436 		aprint_error_dev(xycsc->sc_dev, "warning: cannot load DMA map\n");
1437 		return (XY_ERR_FAIL);	/* XXX: need some sort of
1438 					 * call-back scheme here? */
1439 	}
1440 
1441 	bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1442 			iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
1443 				? BUS_DMASYNC_PREREAD
1444 				: BUS_DMASYNC_PREWRITE);
1445 
1446 	/* init iorq and load iopb from it */
1447 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
1448 		   bp->b_bcount / XYFM_BPS,
1449 		   (void *)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
1450 		   bp);
1451 
1452 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
1453 
1454 	/* Instrumentation. */
1455 	disk_busy(&xysc->sc_dk);
1456 
1457 	return (XY_ERR_AOK);
1458 }
1459 
1460 
1461 /*
1462  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
1463  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
1464  *
1465  * note: caller frees iorq in all cases except NORM
1466  *
1467  * return value:
1468  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
1469  *   WAIT: XY_AOK (success), <error-code> (failed)
1470  *   POLL: <same as WAIT>
1471  *   NOQ : <same as NORM>
1472  *
1473  * there are three sources for i/o requests:
1474  * [1] xystrategy: normal block I/O, using "struct buf" system.
1475  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1476  * [3] open/ioctl: these are I/O requests done in the context of a process,
1477  *                 and the process should block until they are done.
1478  *
1479  * software state is stored in the iorq structure.  each iorq has an
1480  * iopb structure.  the hardware understands the iopb structure.
1481  * every command must go through an iopb.  a 450 handles one iopb at a
1482  * time, where as a 451 can take them in chains.  [the 450 claims it
1483  * can handle chains, but is appears to be buggy...]   iopb are allocated
1484  * in DVMA space at boot up time.  each disk gets one iopb, and the
1485  * controller gets one (for POLL and WAIT commands).  what happens if
1486  * the iopb is busy?  for i/o type [1], the buffers are queued at the
1487  * "buff" layer and * picked up later by the interrupt routine.  for case
1488  * [2] we can only be blocked if there is a WAIT type I/O request being
1489  * run.   since this can only happen when we are crashing, we wait a sec
1490  * and then steal the IOPB.  for case [3] the process can sleep
1491  * on the iorq free list until some iopbs are available.
1492  */
1493 
1494 
1495 int
1496 xyc_submit_iorq(struct xyc_softc *xycsc, struct xy_iorq *iorq, int type)
1497 {
1498 	struct xy_iopb *dmaiopb;
1499 
1500 #ifdef XYC_DEBUG
1501 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
1502 		device_xname(xycsc->sc_dev), iorq, type);
1503 #endif
1504 
1505 	/* first check and see if controller is busy */
1506 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
1507 #ifdef XYC_DEBUG
1508 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
1509 #endif
1510 		if (type == XY_SUB_NOQ)
1511 			return (XY_ERR_FAIL);	/* failed */
1512 		switch (type) {
1513 		case XY_SUB_NORM:
1514 			return XY_ERR_AOK;	/* success */
1515 		case XY_SUB_WAIT:
1516 			while (iorq->iopb->done == 0) {
1517 				(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1518 			}
1519 			return (iorq->errnum);
1520 		case XY_SUB_POLL:		/* steal controller */
1521 			(void)xycsc->xyc->xyc_rsetup; /* RESET */
1522 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
1523 				panic("xyc_submit_iorq: stuck xyc");
1524 			printf("%s: stole controller\n",
1525 				device_xname(xycsc->sc_dev));
1526 			break;
1527 		default:
1528 			panic("xyc_submit_iorq adding");
1529 		}
1530 	}
1531 
1532 	dmaiopb = xyc_chain(xycsc, iorq);	 /* build chain */
1533 	if (dmaiopb == NULL) { /* nothing doing? */
1534 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
1535 			return(XY_ERR_AOK);
1536 		panic("xyc_submit_iorq: xyc_chain failed!");
1537 	}
1538 
1539 	XYC_GO(xycsc->xyc, dmaiopb);
1540 
1541 	/* command now running, wrap it up */
1542 	switch (type) {
1543 	case XY_SUB_NORM:
1544 	case XY_SUB_NOQ:
1545 		return (XY_ERR_AOK);	/* success */
1546 	case XY_SUB_WAIT:
1547 		while (iorq->iopb->done == 0) {
1548 			(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1549 		}
1550 		return (iorq->errnum);
1551 	case XY_SUB_POLL:
1552 		return (xyc_piodriver(xycsc, iorq));
1553 	default:
1554 		panic("xyc_submit_iorq wrap up");
1555 	}
1556 	panic("xyc_submit_iorq");
1557 	return 0;	/* not reached */
1558 }
1559 
1560 
1561 /*
1562  * xyc_chain: build a chain.  return dvma address of first element in
1563  * the chain.   iorq != NULL: means we only want that item on the chain.
1564  */
1565 
1566 struct xy_iopb *
1567 xyc_chain(struct xyc_softc *xycsc, struct xy_iorq *iorq)
1568 {
1569 	int togo, chain, hand;
1570 
1571 	memset(xycsc->xy_chain, 0, sizeof(xycsc->xy_chain));
1572 
1573 	/*
1574 	 * promote control IOPB to the top
1575 	 */
1576 	if (iorq == NULL) {
1577 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1578 		     XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1579 		     xycsc->iopbase[XYC_CTLIOPB].done == 0)
1580 			iorq = &xycsc->reqs[XYC_CTLIOPB];
1581 	}
1582 
1583 	/*
1584 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
1585 	 * we let these take priority and do them first.
1586 	 */
1587 	if (iorq) {
1588 		xycsc->xy_chain[0] = iorq;
1589 		iorq->iopb->chen = 0;
1590 		return(iorq->dmaiopb);
1591 	}
1592 
1593 	/*
1594 	 * NORM case: do round robin and maybe chain (if allowed and possible)
1595 	 */
1596 	chain = 0;
1597 	hand = xycsc->xy_hand;
1598 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1599 
1600 	for (togo = XYC_MAXIOPB; togo > 0;
1601 	     togo--, hand = (hand + 1) % XYC_MAXIOPB) {
1602 		struct xy_iopb *iopb, *prev_iopb, *dmaiopb;
1603 
1604 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1605 		    xycsc->iopbase[hand].done)
1606 			continue;   /* not ready-for-i/o */
1607 
1608 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1609 		iopb = xycsc->xy_chain[chain]->iopb;
1610 		iopb->chen = 0;
1611 		if (chain != 0) {
1612 			/* adding a link to a chain */
1613 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1614 			prev_iopb->chen = 1;
1615 			dmaiopb = xycsc->xy_chain[chain]->dmaiopb;
1616 			prev_iopb->nxtiopb = ((u_long)dmaiopb) & 0xffff;
1617 		} else {
1618 			/* head of chain */
1619 			iorq = xycsc->xy_chain[chain];
1620 		}
1621 		chain++;
1622 
1623 		/* quit if chaining dis-allowed */
1624 		if (xycsc->no_ols)
1625 			break;
1626 	}
1627 
1628 	return(iorq ? iorq->dmaiopb : NULL);
1629 }
1630 
1631 /*
1632  * xyc_piodriver
1633  *
1634  * programmed i/o driver.   this function takes over the computer
1635  * and drains off the polled i/o request.   it returns the status of the iorq
1636  * the caller is interesting in.
1637  */
1638 int
1639 xyc_piodriver(struct xyc_softc *xycsc, struct xy_iorq *iorq)
1640 {
1641 	int     nreset = 0;
1642 	int     retval = 0;
1643 	u_long  res;
1644 #ifdef XYC_DEBUG
1645 	printf("xyc_piodriver(%s, 0x%x)\n", device_xname(xycsc->sc_dev), iorq);
1646 #endif
1647 
1648 	while (iorq->iopb->done == 0) {
1649 
1650 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1651 
1652 		/* we expect some progress soon */
1653 		if (res == XY_ERR_FAIL && nreset >= 2) {
1654 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1655 #ifdef XYC_DEBUG
1656 			printf("xyc_piodriver: timeout\n");
1657 #endif
1658 			return (XY_ERR_FAIL);
1659 		}
1660 		if (res == XY_ERR_FAIL) {
1661 			if (xyc_reset(xycsc, 0,
1662 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
1663 				      XY_ERR_FAIL,
1664 				      0) == XY_ERR_FAIL)
1665 				return (XY_ERR_FAIL);	/* flushes all but POLL
1666 							 * requests, resets */
1667 			continue;
1668 		}
1669 
1670 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
1671 
1672 		if (iorq->iopb->done == 0)
1673 			xyc_start(xycsc, iorq);
1674 	}
1675 
1676 	/* get return value */
1677 
1678 	retval = iorq->errnum;
1679 
1680 #ifdef XYC_DEBUG
1681 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1682 	    iorq->errnum, xyc_e2str(iorq->errnum));
1683 #endif
1684 
1685 	/* start up any bufs that have queued */
1686 
1687 	xyc_start(xycsc, NULL);
1688 
1689 	return (retval);
1690 }
1691 
1692 /*
1693  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
1694  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1695  */
1696 void
1697 xyc_xyreset(struct xyc_softc *xycsc, struct xy_softc *xysc)
1698 {
1699 	struct xy_iopb tmpiopb;
1700 	struct xy_iopb *iopb;
1701 	int     del;
1702 
1703 	iopb = xycsc->ciopb;
1704 
1705 	/* Save contents */
1706 	memcpy(&tmpiopb, iopb, sizeof(struct xy_iopb));
1707 
1708 	iopb->chen = iopb->done = iopb->errs = 0;
1709 	iopb->ien = 0;
1710 	iopb->com = XYCMD_RST;
1711 	iopb->unit = xysc->xy_drive;
1712 
1713 	XYC_GO(xycsc->xyc, xycsc->ciorq->dmaiopb);
1714 
1715 	del = XYC_RESETUSEC;
1716 	while (del > 0) {
1717 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0)
1718 			break;
1719 		DELAY(1);
1720 		del--;
1721 	}
1722 
1723 	if (del <= 0 || iopb->errs) {
1724 		printf("%s: off-line: %s\n", device_xname(xycsc->sc_dev),
1725 		    xyc_e2str(iopb->errnum));
1726 		del = xycsc->xyc->xyc_rsetup;
1727 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1728 			panic("xyc_reset");
1729 	} else {
1730 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
1731 	}
1732 
1733 	/* Restore contents */
1734 	memcpy(iopb, &tmpiopb, sizeof(struct xy_iopb));
1735 }
1736 
1737 
1738 /*
1739  * xyc_reset: reset everything: requests are marked as errors except
1740  * a polled request (which is resubmitted)
1741  */
1742 int
1743 xyc_reset(struct xyc_softc *xycsc, int quiet, struct xy_iorq *blastmode,
1744 	int error, struct xy_softc *xysc)
1745 {
1746 	int     del = 0, lcv, retval = XY_ERR_AOK;
1747 
1748 	/* soft reset hardware */
1749 
1750 	if (!quiet)
1751 		printf("%s: soft reset\n", device_xname(xycsc->sc_dev));
1752 	del = xycsc->xyc->xyc_rsetup;
1753 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1754 	if (del == XY_ERR_FAIL) {
1755 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
1756 		retval = XY_ERR_FAIL;
1757 	}
1758 	if (xysc)
1759 		xyc_xyreset(xycsc, xysc);
1760 
1761 	/* fix queues based on "blast-mode" */
1762 
1763 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1764 		register struct xy_iorq *iorq = &xycsc->reqs[lcv];
1765 
1766 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1767 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1768 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
1769 			/* is it active? */
1770 			continue;
1771 
1772 		if (blastmode == XY_RSET_ALL ||
1773 				blastmode != iorq) {
1774 			/* failed */
1775 			iorq->errnum = error;
1776 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1777 			switch (XY_STATE(iorq->mode)) {
1778 			case XY_SUB_NORM:
1779 			    iorq->buf->b_error = EIO;
1780 			    iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
1781 
1782 			    bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1783 					iorq->dmamap->dm_mapsize,
1784 					(iorq->buf->b_flags & B_READ)
1785 						? BUS_DMASYNC_POSTREAD
1786 						: BUS_DMASYNC_POSTWRITE);
1787 
1788 			    bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1789 
1790 			    (void)bufq_get(iorq->xy->xyq);
1791 			    disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1792 				(xycsc->reqs[lcv].buf->b_bcount -
1793 				xycsc->reqs[lcv].buf->b_resid),
1794 				(xycsc->reqs[lcv].buf->b_flags & B_READ));
1795 			    biodone(iorq->buf);
1796 			    iorq->mode = XY_SUB_FREE;
1797 			    break;
1798 			case XY_SUB_WAIT:
1799 			    wakeup(iorq);
1800 			case XY_SUB_POLL:
1801 			    iorq->mode =
1802 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1803 			    break;
1804 			}
1805 
1806 		} else {
1807 
1808 			/* resubmit, no need to do anything here */
1809 		}
1810 	}
1811 
1812 	/*
1813 	 * now, if stuff is waiting, start it.
1814 	 * since we just reset it should go
1815 	 */
1816 	xyc_start(xycsc, NULL);
1817 
1818 	return (retval);
1819 }
1820 
1821 /*
1822  * xyc_start: start waiting buffers
1823  */
1824 
1825 void
1826 xyc_start(struct xyc_softc *xycsc, struct xy_iorq *iorq)
1827 {
1828 	int lcv;
1829 	struct xy_softc *xy;
1830 
1831 	if (iorq == NULL) {
1832 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1833 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1834 			if (bufq_peek(xy->xyq) == NULL) continue;
1835 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
1836 			xyc_startbuf(xycsc, xy, bufq_peek(xy->xyq));
1837 		}
1838 	}
1839 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1840 }
1841 
1842 /*
1843  * xyc_remove_iorq: remove "done" IOPB's.
1844  */
1845 
1846 int
1847 xyc_remove_iorq(struct xyc_softc *xycsc)
1848 {
1849 	int     errnum, rq, comm, errs;
1850 	struct xyc *xyc = xycsc->xyc;
1851 	u_long  addr;
1852 	struct xy_iopb *iopb;
1853 	struct xy_iorq *iorq;
1854 	struct buf *bp;
1855 
1856 	if (xyc->xyc_csr & XYC_DERR) {
1857 		/*
1858 		 * DOUBLE ERROR: should never happen under normal use. This
1859 		 * error is so bad, you can't even tell which IOPB is bad, so
1860 		 * we dump them all.
1861 		 */
1862 		errnum = XY_ERR_DERR;
1863 		aprint_error_dev(xycsc->sc_dev, "DOUBLE ERROR!\n");
1864 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errnum, 0) != XY_ERR_AOK) {
1865 			aprint_error_dev(xycsc->sc_dev, "soft reset failed!\n");
1866 			panic("xyc_remove_iorq: controller DEAD");
1867 		}
1868 		return (XY_ERR_AOK);
1869 	}
1870 
1871 	/*
1872 	 * get iopb that is done, loop down the chain
1873 	 */
1874 
1875 	if (xyc->xyc_csr & XYC_ERR) {
1876 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
1877 	}
1878 	if (xyc->xyc_csr & XYC_IPND) {
1879 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1880 	}
1881 
1882 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1883 		iorq = xycsc->xy_chain[rq];
1884 		if (iorq == NULL) break; /* done ! */
1885 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1886 			continue;	/* free, or done */
1887 		iopb = iorq->iopb;
1888 		if (iopb->done == 0)
1889 			continue;	/* not done yet */
1890 
1891 		comm = iopb->com;
1892 		errs = iopb->errs;
1893 
1894 		if (errs)
1895 			iorq->errnum = iopb->errnum;
1896 		else
1897 			iorq->errnum = 0;
1898 
1899 		/* handle non-fatal errors */
1900 
1901 		if (errs &&
1902 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1903 			continue;	/* AOK: we resubmitted it */
1904 
1905 
1906 		/* this iorq is now done (hasn't been restarted or anything) */
1907 
1908 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1909 			xyc_perror(iorq, iopb, 0);
1910 
1911 		/* now, if read/write check to make sure we got all the data
1912 		 * we needed. (this may not be the case if we got an error in
1913 		 * the middle of a multisector request).   */
1914 
1915 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1916 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
1917 			/* we just successfully processed a bad144 sector
1918 			 * note: if we are in bad 144 mode, the pointers have
1919 			 * been advanced already (see above) and are pointing
1920 			 * at the bad144 sector.   to exit bad144 mode, we
1921 			 * must advance the pointers 1 sector and issue a new
1922 			 * request if there are still sectors left to process
1923 			 *
1924 			 */
1925 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
1926 
1927 			/* exit b144 mode */
1928 			iorq->mode = iorq->mode & (~XY_MODE_B144);
1929 
1930 			if (iorq->sectcnt) {	/* more to go! */
1931 				iorq->lasterror = iorq->errnum = iopb->errnum = 0;
1932 				iopb->errs = iopb->done = 0;
1933 				iorq->tries = 0;
1934 				iopb->scnt = iorq->sectcnt;
1935 				iopb->cyl = iorq->blockno /
1936 						iorq->xy->sectpercyl;
1937 				iopb->head =
1938 					(iorq->blockno / iorq->xy->nhead) %
1939 						iorq->xy->nhead;
1940 				iopb->sect = iorq->blockno % XYFM_BPS;
1941 				addr = (u_long) iorq->dbuf;
1942 				iopb->dataa = (addr & 0xffff);
1943 				iopb->datar = ((addr & 0xff0000) >> 16);
1944 				/* will resubit at end */
1945 				continue;
1946 			}
1947 		}
1948 		/* final cleanup, totally done with this request */
1949 
1950 		switch (XY_STATE(iorq->mode)) {
1951 		case XY_SUB_NORM:
1952 			bp = iorq->buf;
1953 			if (errs) {
1954 				bp->b_error = EIO;
1955 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
1956 			} else {
1957 				bp->b_resid = 0;	/* done */
1958 			}
1959 			bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1960 					iorq->dmamap->dm_mapsize,
1961 					(iorq->buf->b_flags & B_READ)
1962 						? BUS_DMASYNC_POSTREAD
1963 						: BUS_DMASYNC_POSTWRITE);
1964 
1965 			bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1966 
1967 			(void)bufq_get(iorq->xy->xyq);
1968 			disk_unbusy(&iorq->xy->sc_dk,
1969 			    (bp->b_bcount - bp->b_resid),
1970 			    (bp->b_flags & B_READ));
1971 			iorq->mode = XY_SUB_FREE;
1972 			biodone(bp);
1973 			break;
1974 		case XY_SUB_WAIT:
1975 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1976 			wakeup(iorq);
1977 			break;
1978 		case XY_SUB_POLL:
1979 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1980 			break;
1981 		}
1982 	}
1983 
1984 	return (XY_ERR_AOK);
1985 }
1986 
1987 /*
1988  * xyc_perror: print error.
1989  * - if still_trying is true: we got an error, retried and got a
1990  *   different error.  in that case lasterror is the old error,
1991  *   and errnum is the new one.
1992  * - if still_trying is not true, then if we ever had an error it
1993  *   is in lasterror. also, if iorq->errnum == 0, then we recovered
1994  *   from that error (otherwise iorq->errnum == iorq->lasterror).
1995  */
1996 void
1997 xyc_perror(struct xy_iorq *iorq, struct xy_iopb *iopb, int still_trying)
1998 {
1999 
2000 	int     error = iorq->lasterror;
2001 
2002 	printf("%s", (iorq->xy) ? device_xname(iorq->xy->sc_dev)
2003 	    : device_xname(iorq->xyc->sc_dev));
2004 	if (iorq->buf)
2005 		printf("%c: ", 'a' + (char)DISKPART(iorq->buf->b_dev));
2006 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
2007 		printf("%s %d/%d/%d: ",
2008 			(iopb->com == XYCMD_RD) ? "read" : "write",
2009 			iopb->cyl, iopb->head, iopb->sect);
2010 	printf("%s", xyc_e2str(error));
2011 
2012 	if (still_trying)
2013 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errnum));
2014 	else
2015 		if (iorq->errnum == 0)
2016 			printf(" [recovered in %d tries]", iorq->tries);
2017 
2018 	printf("\n");
2019 }
2020 
2021 /*
2022  * xyc_error: non-fatal error encountered... recover.
2023  * return AOK if resubmitted, return FAIL if this iopb is done
2024  */
2025 int
2026 xyc_error(struct xyc_softc *xycsc, struct xy_iorq *iorq, struct xy_iopb *iopb,
2027 	int comm)
2028 {
2029 	int     errnum = iorq->errnum;
2030 	int     erract = xyc_entoact(errnum);
2031 	int     oldmode, advance;
2032 #ifdef __sparc__
2033 	int i;
2034 #endif
2035 
2036 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
2037 		oldmode = iorq->mode;
2038 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
2039 		/* make xyc_start ignore us */
2040 		xyc_reset(xycsc, 1, XY_RSET_NONE, errnum, iorq->xy);
2041 		iorq->mode = oldmode;
2042 	}
2043 	/* check for read/write to a sector in bad144 table if bad: redirect
2044 	 * request to bad144 area */
2045 
2046 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
2047 	    (iorq->mode & XY_MODE_B144) == 0) {
2048 		advance = iorq->sectcnt - iopb->scnt;
2049 		XYC_ADVANCE(iorq, advance);
2050 #ifdef __sparc__
2051 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
2052 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
2053 			    iorq->blockno % iorq->xy->nsect)) != -1) {
2054 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
2055 							 * redirect */
2056 			iopb->errnum = iopb->done = iopb->errs = 0;
2057 			iopb->scnt = 1;
2058 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
2059 			/* second to last acyl */
2060 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
2061 								 * standard */
2062 			iopb->head = i / iorq->xy->nhead;
2063 			iopb->sect = i % iorq->xy->nhead;
2064 			/* will resubmit when we come out of remove_iorq */
2065 			return (XY_ERR_AOK);	/* recovered! */
2066 		}
2067 #endif
2068 	}
2069 
2070 	/*
2071 	 * it isn't a bad144 sector, must be real error! see if we can retry
2072 	 * it?
2073 	 */
2074 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
2075 		xyc_perror(iorq, iopb, 1);	/* inform of error state
2076 						 * change */
2077 	iorq->lasterror = errnum;
2078 
2079 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
2080 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
2081 		iorq->tries++;
2082 		iorq->errnum = iopb->errnum = iopb->done = iopb->errs = 0;
2083 		/* will resubmit at end of remove_iorq */
2084 		return (XY_ERR_AOK);	/* recovered! */
2085 	}
2086 
2087 	/* failed to recover from this error */
2088 	return (XY_ERR_FAIL);
2089 }
2090 
2091 /*
2092  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
2093  */
2094 void
2095 xyc_tick(void *arg)
2096 {
2097 	struct xyc_softc *xycsc = arg;
2098 	int     lcv, s, reset = 0;
2099 
2100 	/* reduce ttl for each request if one goes to zero, reset xyc */
2101 	s = splbio();
2102 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
2103 		if (xycsc->reqs[lcv].mode == 0 ||
2104 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
2105 			continue;
2106 		xycsc->reqs[lcv].ttl--;
2107 		if (xycsc->reqs[lcv].ttl == 0)
2108 			reset = 1;
2109 	}
2110 	if (reset) {
2111 		printf("%s: watchdog timeout\n", device_xname(xycsc->sc_dev));
2112 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
2113 	}
2114 	splx(s);
2115 
2116 	/* until next time */
2117 
2118 	callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
2119 }
2120 
2121 /*
2122  * xyc_ioctlcmd: this function provides a user level interface to the
2123  * controller via ioctl.   this allows "format" programs to be written
2124  * in user code, and is also useful for some debugging.   we return
2125  * an error code.   called at user priority.
2126  *
2127  * XXX missing a few commands (see the 7053 driver for ideas)
2128  */
2129 int
2130 xyc_ioctlcmd(struct xy_softc *xy, dev_t dev, struct xd_iocmd *xio)
2131 {
2132 	int     s, rqno, dummy = 0;
2133 	char *dvmabuf = NULL, *buf = NULL;
2134 	struct xyc_softc *xycsc;
2135 	int			rseg, error;
2136 	bus_dma_segment_t	seg;
2137 
2138 	/* check sanity of requested command */
2139 
2140 	switch (xio->cmd) {
2141 
2142 	case XYCMD_NOP:	/* no op: everything should be zero */
2143 		if (xio->subfn || xio->dptr || xio->dlen ||
2144 		    xio->block || xio->sectcnt)
2145 			return (EINVAL);
2146 		break;
2147 
2148 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
2149 	case XYCMD_WR:
2150 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2151 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2152 			return (EINVAL);
2153 		break;
2154 
2155 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
2156 		return (EINVAL);
2157 
2158 		break;
2159 
2160 	default:
2161 		return (EINVAL);/* ??? */
2162 	}
2163 
2164 	xycsc = xy->parent;
2165 
2166 	/* create DVMA buffer for request if needed */
2167 	if (xio->dlen) {
2168 		bus_addr_t busbuf;
2169 
2170 		if ((error = xy_dmamem_alloc(xycsc->dmatag, xycsc->auxmap,
2171 					     &seg, &rseg,
2172 					     xio->dlen, (void **)&buf,
2173 					     &busbuf)) != 0) {
2174 			return (error);
2175 		}
2176 		dvmabuf = (void *)(u_long)BUS_ADDR_PADDR(busbuf);
2177 
2178 		if (xio->cmd == XYCMD_WR) {
2179 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
2180 				bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
2181 				bus_dmamem_free(xycsc->dmatag, &seg, rseg);
2182 				return (error);
2183 			}
2184 		}
2185 	}
2186 	/* do it! */
2187 
2188 	error = 0;
2189 	s = splbio();
2190 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2191 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2192 	if (rqno == XY_ERR_FAIL) {
2193 		error = EIO;
2194 		goto done;
2195 	}
2196 	xio->errnum = xycsc->ciorq->errnum;
2197 	xio->tries = xycsc->ciorq->tries;
2198 	XYC_DONE(xycsc, dummy);
2199 
2200 	if (xio->cmd == XYCMD_RD)
2201 		error = copyout(buf, xio->dptr, xio->dlen);
2202 
2203 done:
2204 	splx(s);
2205 	if (dvmabuf) {
2206 		xy_dmamem_free(xycsc->dmatag, xycsc->auxmap, &seg, rseg,
2207 				xio->dlen, buf);
2208 	}
2209 	return (error);
2210 }
2211 
2212 /*
2213  * xyc_e2str: convert error code number into an error string
2214  */
2215 const char *
2216 xyc_e2str(int no)
2217 {
2218 	switch (no) {
2219 	case XY_ERR_FAIL:
2220 		return ("Software fatal error");
2221 	case XY_ERR_DERR:
2222 		return ("DOUBLE ERROR");
2223 	case XY_ERR_AOK:
2224 		return ("Successful completion");
2225 	case XY_ERR_IPEN:
2226 		return("Interrupt pending");
2227 	case XY_ERR_BCFL:
2228 		return("Busy conflict");
2229 	case XY_ERR_TIMO:
2230 		return("Operation timeout");
2231 	case XY_ERR_NHDR:
2232 		return("Header not found");
2233 	case XY_ERR_HARD:
2234 		return("Hard ECC error");
2235 	case XY_ERR_ICYL:
2236 		return("Illegal cylinder address");
2237 	case XY_ERR_ISEC:
2238 		return("Illegal sector address");
2239 	case XY_ERR_SMAL:
2240 		return("Last sector too small");
2241 	case XY_ERR_SACK:
2242 		return("Slave ACK error (non-existent memory)");
2243 	case XY_ERR_CHER:
2244 		return("Cylinder and head/header error");
2245 	case XY_ERR_SRTR:
2246 		return("Auto-seek retry successful");
2247 	case XY_ERR_WPRO:
2248 		return("Write-protect error");
2249 	case XY_ERR_UIMP:
2250 		return("Unimplemented command");
2251 	case XY_ERR_DNRY:
2252 		return("Drive not ready");
2253 	case XY_ERR_SZER:
2254 		return("Sector count zero");
2255 	case XY_ERR_DFLT:
2256 		return("Drive faulted");
2257 	case XY_ERR_ISSZ:
2258 		return("Illegal sector size");
2259 	case XY_ERR_SLTA:
2260 		return("Self test A");
2261 	case XY_ERR_SLTB:
2262 		return("Self test B");
2263 	case XY_ERR_SLTC:
2264 		return("Self test C");
2265 	case XY_ERR_SOFT:
2266 		return("Soft ECC error");
2267 	case XY_ERR_SFOK:
2268 		return("Soft ECC error recovered");
2269 	case XY_ERR_IHED:
2270 		return("Illegal head");
2271 	case XY_ERR_DSEQ:
2272 		return("Disk sequencer error");
2273 	case XY_ERR_SEEK:
2274 		return("Seek error");
2275 	default:
2276 		return ("Unknown error");
2277 	}
2278 }
2279 
2280 int
2281 xyc_entoact(int errnum)
2282 {
2283   switch (errnum) {
2284     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
2285     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
2286     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
2287     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
2288     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
2289 
2290 	return(XY_ERA_PROG); /* program error ! */
2291 
2292     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
2293     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
2294     case XY_ERR_SOFT:
2295 
2296 	return(XY_ERA_HARD); /* hard error, retry */
2297 
2298     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
2299 
2300 	return(XY_ERA_RSET); /* hard error reset */
2301 
2302     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
2303 
2304 	return(XY_ERA_SOFT); /* an FYI error */
2305 
2306     case XY_ERR_WPRO:
2307 
2308 	return(XY_ERA_WPRO); /* write protect */
2309   }
2310 
2311   return(XY_ERA_PROG); /* ??? */
2312 }
2313