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