xref: /netbsd-src/sys/dev/vme/xy.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: xy.c,v 1.74 2007/10/19 12:01:23 ad 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.74 2007/10/19 12:01:23 ad 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 #include <sys/kauth.h>
80 
81 #include <sys/bus.h>
82 #include <sys/intr.h>
83 
84 #if defined(__sparc__) || defined(sun3)
85 #include <dev/sun/disklabel.h>
86 #endif
87 
88 #include <dev/vme/vmereg.h>
89 #include <dev/vme/vmevar.h>
90 
91 #include <dev/vme/xyreg.h>
92 #include <dev/vme/xyvar.h>
93 #include <dev/vme/xio.h>
94 
95 #include "locators.h"
96 
97 /*
98  * macros
99  */
100 
101 /*
102  * XYC_GO: start iopb ADDR (DVMA addr in a u_long) on XYC
103  */
104 #define XYC_GO(XYC, ADDR) { \
105 	u_long addr = (u_long)ADDR; \
106 	(XYC)->xyc_addr_lo = ((addr) & 0xff); \
107 	(addr) = ((addr) >> 8); \
108 	(XYC)->xyc_addr_hi = ((addr) & 0xff); \
109 	(addr) = ((addr) >> 8); \
110 	(XYC)->xyc_reloc_lo = ((addr) & 0xff); \
111 	(addr) = ((addr) >> 8); \
112 	(XYC)->xyc_reloc_hi = (addr); \
113 	(XYC)->xyc_csr = XYC_GBSY; /* go! */ \
114 }
115 
116 /*
117  * XYC_DONE: don't need IORQ, get error code and free (done after xyc_cmd)
118  */
119 
120 #define XYC_DONE(SC,ER) { \
121 	if ((ER) == XY_ERR_AOK) { \
122 		(ER) = (SC)->ciorq->errnum; \
123 		(SC)->ciorq->mode = XY_SUB_FREE; \
124 		wakeup((SC)->ciorq); \
125 	} \
126 	}
127 
128 /*
129  * XYC_ADVANCE: advance iorq's pointers by a number of sectors
130  */
131 
132 #define XYC_ADVANCE(IORQ, N) { \
133 	if (N) { \
134 		(IORQ)->sectcnt -= (N); \
135 		(IORQ)->blockno += (N); \
136 		(IORQ)->dbuf += ((N)*XYFM_BPS); \
137 	} \
138 }
139 
140 /*
141  * note - addresses you can sleep on:
142  *   [1] & of xy_softc's "state" (waiting for a chance to attach a drive)
143  *   [2] & an iorq (waiting for an XY_SUB_WAIT iorq to finish)
144  */
145 
146 
147 /*
148  * function prototypes
149  * "xyc_*" functions are internal, all others are external interfaces
150  */
151 
152 extern int pil_to_vme[];	/* from obio.c */
153 
154 /* internals */
155 struct xy_iopb *xyc_chain(struct xyc_softc *, struct xy_iorq *);
156 int	xyc_cmd(struct xyc_softc *, int, int, int, int, int, char *, int);
157 const char *xyc_e2str(int);
158 int	xyc_entoact(int);
159 int	xyc_error(struct xyc_softc *, struct xy_iorq *,
160 		   struct xy_iopb *, int);
161 int	xyc_ioctlcmd(struct xy_softc *, dev_t dev, struct xd_iocmd *);
162 void	xyc_perror(struct xy_iorq *, struct xy_iopb *, int);
163 int	xyc_piodriver(struct xyc_softc *, struct xy_iorq *);
164 int	xyc_remove_iorq(struct xyc_softc *);
165 int	xyc_reset(struct xyc_softc *, int, struct xy_iorq *, int,
166 		  struct xy_softc *);
167 inline void xyc_rqinit(struct xy_iorq *, struct xyc_softc *,
168 			struct xy_softc *, int, u_long, int,
169 			void *, struct buf *);
170 void	xyc_rqtopb(struct xy_iorq *, struct xy_iopb *, int, int);
171 void	xyc_start(struct xyc_softc *, struct xy_iorq *);
172 int	xyc_startbuf(struct xyc_softc *, struct xy_softc *, struct buf *);
173 int	xyc_submit_iorq(struct xyc_softc *, struct xy_iorq *, int);
174 void	xyc_tick(void *);
175 int	xyc_unbusy(struct xyc *, int);
176 void	xyc_xyreset(struct xyc_softc *, struct xy_softc *);
177 int	xy_dmamem_alloc(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
178 			int *, bus_size_t, void **, bus_addr_t *);
179 void	xy_dmamem_free(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
180 			int, bus_size_t, void *);
181 
182 /* machine interrupt hook */
183 int	xycintr(void *);
184 
185 /* autoconf */
186 int	xycmatch(struct device *, struct cfdata *, void *);
187 void	xycattach(struct device *, struct device *, void *);
188 int	xymatch(struct device *, struct cfdata *, void *);
189 void	xyattach(struct device *, struct device *, void *);
190 static	int xyc_probe(void *, bus_space_tag_t, bus_space_handle_t);
191 
192 static	void xydummystrat(struct buf *);
193 int	xygetdisklabel(struct xy_softc *, void *);
194 
195 /*
196  * cfattach's: device driver interface to autoconfig
197  */
198 
199 CFATTACH_DECL(xyc, sizeof(struct xyc_softc),
200     xycmatch, xycattach, NULL, NULL);
201 
202 CFATTACH_DECL(xy, sizeof(struct xy_softc),
203     xymatch, xyattach, NULL, NULL);
204 
205 extern struct cfdriver xy_cd;
206 
207 dev_type_open(xyopen);
208 dev_type_close(xyclose);
209 dev_type_read(xyread);
210 dev_type_write(xywrite);
211 dev_type_ioctl(xyioctl);
212 dev_type_strategy(xystrategy);
213 dev_type_dump(xydump);
214 dev_type_size(xysize);
215 
216 const struct bdevsw xy_bdevsw = {
217 	xyopen, xyclose, xystrategy, xyioctl, xydump, xysize, D_DISK
218 };
219 
220 const struct cdevsw xy_cdevsw = {
221 	xyopen, xyclose, xyread, xywrite, xyioctl,
222 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
223 };
224 
225 struct xyc_attach_args {	/* this is the "aux" args to xyattach */
226 	int	driveno;	/* unit number */
227 	int	fullmode;	/* submit mode */
228 	int	booting;	/* are we booting or not? */
229 };
230 
231 /*
232  * dkdriver
233  */
234 
235 struct dkdriver xydkdriver = { xystrategy };
236 
237 /*
238  * start: disk label fix code (XXX)
239  */
240 
241 static void *xy_labeldata;
242 
243 static void
244 xydummystrat(bp)
245 	struct buf *bp;
246 {
247 	if (bp->b_bcount != XYFM_BPS)
248 		panic("xydummystrat");
249 	bcopy(xy_labeldata, bp->b_data, XYFM_BPS);
250 	bp->b_flags |= B_DONE;
251 	bp->b_flags &= ~B_BUSY;
252 }
253 
254 int
255 xygetdisklabel(xy, b)
256 	struct xy_softc *xy;
257 	void *b;
258 {
259 	const char *err;
260 #if defined(__sparc__) || defined(sun3)
261 	struct sun_disklabel *sdl;
262 #endif
263 
264 	/* We already have the label data in `b'; setup for dummy strategy */
265 	xy_labeldata = b;
266 
267 	/* Required parameter for readdisklabel() */
268 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS;
269 
270 	err = readdisklabel(MAKEDISKDEV(0, device_unit(&xy->sc_dev), RAW_PART),
271 					xydummystrat,
272 				xy->sc_dk.dk_label, xy->sc_dk.dk_cpulabel);
273 	if (err) {
274 		printf("%s: %s\n", xy->sc_dev.dv_xname, err);
275 		return(XY_ERR_FAIL);
276 	}
277 
278 #if defined(__sparc__) || defined(sun3)
279 	/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
280 	sdl = (struct sun_disklabel *)xy->sc_dk.dk_cpulabel->cd_block;
281 	if (sdl->sl_magic == SUN_DKMAGIC) {
282 		xy->pcyl = sdl->sl_pcylinders;
283 	} else
284 #endif
285 	{
286 		printf("%s: WARNING: no `pcyl' in disk label.\n",
287 			xy->sc_dev.dv_xname);
288 		xy->pcyl = xy->sc_dk.dk_label->d_ncylinders +
289 			xy->sc_dk.dk_label->d_acylinders;
290 		printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
291 		xy->sc_dev.dv_xname, xy->pcyl);
292 	}
293 
294 	xy->ncyl = xy->sc_dk.dk_label->d_ncylinders;
295 	xy->acyl = xy->sc_dk.dk_label->d_acylinders;
296 	xy->nhead = xy->sc_dk.dk_label->d_ntracks;
297 	xy->nsect = xy->sc_dk.dk_label->d_nsectors;
298 	xy->sectpercyl = xy->nhead * xy->nsect;
299 	xy->sc_dk.dk_label->d_secsize = XYFM_BPS; /* not handled by
300                                           	  * sun->bsd */
301 	return(XY_ERR_AOK);
302 }
303 
304 /*
305  * end: disk label fix code (XXX)
306  */
307 
308 /*
309  * Shorthand for allocating, mapping and loading a DMA buffer
310  */
311 int
312 xy_dmamem_alloc(tag, map, seg, nsegp, len, kvap, dmap)
313 	bus_dma_tag_t		tag;
314 	bus_dmamap_t		map;
315 	bus_dma_segment_t	*seg;
316 	int			*nsegp;
317 	bus_size_t		len;
318 	void *			*kvap;
319 	bus_addr_t		*dmap;
320 {
321 	int nseg;
322 	int error;
323 
324 	if ((error = bus_dmamem_alloc(tag, len, 0, 0,
325 				      seg, 1, &nseg, BUS_DMA_NOWAIT)) != 0) {
326 		return (error);
327 	}
328 
329 	if ((error = bus_dmamem_map(tag, seg, nseg,
330 				    len, kvap,
331 				    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
332 		bus_dmamem_free(tag, seg, nseg);
333 		return (error);
334 	}
335 
336 	if ((error = bus_dmamap_load(tag, map, *kvap, len, NULL,
337 				     BUS_DMA_NOWAIT)) != 0) {
338 		bus_dmamem_unmap(tag, *kvap, len);
339 		bus_dmamem_free(tag, seg, nseg);
340 		return (error);
341 	}
342 
343 	*dmap = map->dm_segs[0].ds_addr;
344 	*nsegp = nseg;
345 	return (0);
346 }
347 
348 void
349 xy_dmamem_free(tag, map, seg, nseg, len, kva)
350 	bus_dma_tag_t		tag;
351 	bus_dmamap_t		map;
352 	bus_dma_segment_t	*seg;
353 	int			nseg;
354 	bus_size_t		len;
355 	void *			kva;
356 {
357 
358 	bus_dmamap_unload(tag, map);
359 	bus_dmamem_unmap(tag, kva, len);
360 	bus_dmamem_free(tag, seg, nseg);
361 }
362 
363 
364 /*
365  * a u t o c o n f i g   f u n c t i o n s
366  */
367 
368 /*
369  * xycmatch: determine if xyc is present or not.   we do a
370  * soft reset to detect the xyc.
371  */
372 int
373 xyc_probe(arg, tag, handle)
374 	void *arg;
375 	bus_space_tag_t tag;
376 	bus_space_handle_t handle;
377 {
378 	struct xyc *xyc = (void *)handle; /* XXX */
379 
380 	return ((xyc_unbusy(xyc, XYC_RESETUSEC) != XY_ERR_FAIL) ? 0 : EIO);
381 }
382 
383 int xycmatch(parent, cf, aux)
384 	struct device *parent;
385 	struct cfdata *cf;
386 	void *aux;
387 {
388 	struct vme_attach_args	*va = aux;
389 	vme_chipset_tag_t	ct = va->va_vct;
390 	vme_am_t		mod;
391 	int error;
392 
393 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
394 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
395 		return (0);
396 
397 	error = vme_probe(ct, va->r[0].offset, sizeof(struct xyc),
398 			  mod, VME_D16, xyc_probe, 0);
399 	vme_space_free(va->va_vct, va->r[0].offset, sizeof(struct xyc), mod);
400 
401 	return (error == 0);
402 }
403 
404 /*
405  * xycattach: attach controller
406  */
407 void
408 xycattach(parent, self, aux)
409 	struct device *parent, *self;
410 	void   *aux;
411 
412 {
413 	struct xyc_softc	*xyc = (void *) self;
414 	struct vme_attach_args	*va = aux;
415 	vme_chipset_tag_t	ct = va->va_vct;
416 	bus_space_tag_t		bt;
417 	bus_space_handle_t	bh;
418 	vme_intr_handle_t	ih;
419 	vme_am_t		mod;
420 	struct xyc_attach_args	xa;
421 	int			lcv, res, error;
422 	bus_dma_segment_t	seg;
423 	int			rseg;
424 	vme_mapresc_t resc;
425 	bus_addr_t		busaddr;
426 
427 	/* get addressing and intr level stuff from autoconfig and load it
428 	 * into our xyc_softc. */
429 
430 	mod = VME_AM_A16 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
431 
432 	if (vme_space_alloc(ct, va->r[0].offset, sizeof(struct xyc), mod))
433 		panic("xyc: vme alloc");
434 
435 	if (vme_space_map(ct, va->r[0].offset, sizeof(struct xyc),
436 			  mod, VME_D16, 0, &bt, &bh, &resc) != 0)
437 		panic("xyc: vme_map");
438 
439 	xyc->xyc = (struct xyc *) bh; /* XXX */
440 	xyc->ipl = va->ilevel;
441 	xyc->vector = va->ivector;
442 	xyc->no_ols = 0; /* XXX should be from config */
443 
444 	for (lcv = 0; lcv < XYC_MAXDEV; lcv++)
445 		xyc->sc_drives[lcv] = (struct xy_softc *) 0;
446 
447 	/*
448 	 * allocate and zero buffers
449 	 * check boundaries of the KVA's ... all IOPBs must reside in
450  	 * the same 64K region.
451 	 */
452 
453 	/* Get DMA handle for misc. transfers */
454 	if ((error = vme_dmamap_create(
455 				ct,		/* VME chip tag */
456 				MAXPHYS,	/* size */
457 				VME_AM_A24,	/* address modifier */
458 				VME_D16,	/* data size */
459 				0,		/* swap */
460 				1,		/* nsegments */
461 				MAXPHYS,	/* maxsegsz */
462 				0,		/* boundary */
463 				BUS_DMA_NOWAIT,
464 				&xyc->auxmap)) != 0) {
465 
466 		printf("%s: DMA buffer map create error %d\n",
467 			xyc->sc_dev.dv_xname, error);
468 		return;
469 	}
470 
471 	/* Get DMA handle for mapping iorq descriptors */
472 	if ((error = vme_dmamap_create(
473 				ct,		/* VME chip tag */
474 				XYC_MAXIOPB * sizeof(struct xy_iopb),
475 				VME_AM_A24,	/* address modifier */
476 				VME_D16,	/* data size */
477 				0,		/* swap */
478 				1,		/* nsegments */
479 				XYC_MAXIOPB * sizeof(struct xy_iopb),
480 				64*1024,	/* boundary */
481 				BUS_DMA_NOWAIT,
482 				&xyc->iopmap)) != 0) {
483 
484 		printf("%s: DMA buffer map create error %d\n",
485 			xyc->sc_dev.dv_xname, error);
486 		return;
487 	}
488 
489 	/* Get DMA buffer for iorq descriptors */
490 	if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->iopmap, &seg, &rseg,
491 				     XYC_MAXIOPB * sizeof(struct xy_iopb),
492 				     (void **)&xyc->iopbase,
493 				     &busaddr)) != 0) {
494 		printf("%s: DMA buffer alloc error %d\n",
495 			xyc->sc_dev.dv_xname, error);
496 		return;
497 	}
498 	xyc->dvmaiopb = (struct xy_iopb *)(u_long)BUS_ADDR_PADDR(busaddr);
499 
500 	bzero(xyc->iopbase, XYC_MAXIOPB * sizeof(struct xy_iopb));
501 
502 	xyc->reqs = (struct xy_iorq *)
503 	    malloc(XYC_MAXIOPB * sizeof(struct xy_iorq),
504 	    M_DEVBUF, M_NOWAIT|M_ZERO);
505 	if (xyc->reqs == NULL)
506 		panic("xyc malloc");
507 
508 	/*
509 	 * init iorq to iopb pointers, and non-zero fields in the
510 	 * iopb which never change.
511 	 */
512 
513 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
514 		xyc->xy_chain[lcv] = NULL;
515 		xyc->reqs[lcv].iopb = &xyc->iopbase[lcv];
516 		xyc->reqs[lcv].dmaiopb = &xyc->dvmaiopb[lcv];
517 		xyc->iopbase[lcv].asr = 1;	/* always the same */
518 		xyc->iopbase[lcv].eef = 1;	/* always the same */
519 		xyc->iopbase[lcv].ecm = XY_ECM;	/* always the same */
520 		xyc->iopbase[lcv].aud = 1;	/* always the same */
521 		xyc->iopbase[lcv].relo = 1;	/* always the same */
522 		xyc->iopbase[lcv].thro = XY_THRO;/* always the same */
523 
524 		if ((error = vme_dmamap_create(
525 				ct,		/* VME chip tag */
526 				MAXPHYS,	/* size */
527 				VME_AM_A24,	/* address modifier */
528 				VME_D16,	/* data size */
529 				0,		/* swap */
530 				1,		/* nsegments */
531 				MAXPHYS,	/* maxsegsz */
532 				0,		/* boundary */
533 				BUS_DMA_NOWAIT,
534 				&xyc->reqs[lcv].dmamap)) != 0) {
535 
536 			printf("%s: DMA buffer map create error %d\n",
537 				xyc->sc_dev.dv_xname, error);
538 			return;
539 		}
540 	}
541 	xyc->ciorq = &xyc->reqs[XYC_CTLIOPB];    /* short hand name */
542 	xyc->ciopb = &xyc->iopbase[XYC_CTLIOPB]; /* short hand name */
543 	xyc->xy_hand = 0;
544 
545 	/* read controller parameters and insure we have a 450/451 */
546 
547 	error = xyc_cmd(xyc, XYCMD_ST, 0, 0, 0, 0, 0, XY_SUB_POLL);
548 	res = xyc->ciopb->ctyp;
549 	XYC_DONE(xyc, error);
550 	if (res != XYCT_450) {
551 		if (error)
552 			printf(": %s: ", xyc_e2str(error));
553 		printf(": doesn't identify as a 450/451\n");
554 		return;
555 	}
556 	printf(": Xylogics 450/451");
557 	if (xyc->no_ols)
558 		printf(" [OLS disabled]"); /* 450 doesn't overlap seek right */
559 	printf("\n");
560 	if (error) {
561 		printf("%s: error: %s\n", xyc->sc_dev.dv_xname,
562 				xyc_e2str(error));
563 		return;
564 	}
565 	if ((xyc->xyc->xyc_csr & XYC_ADRM) == 0) {
566 		printf("%s: 24 bit addressing turned off\n",
567 			xyc->sc_dev.dv_xname);
568 		printf("please set hardware jumpers JM1-JM2=in, JM3-JM4=out\n");
569 		printf("to enable 24 bit mode and this driver\n");
570 		return;
571 	}
572 
573 	/* link in interrupt with higher level software */
574 	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
575 	vme_intr_establish(ct, ih, IPL_BIO, xycintr, xyc);
576 	evcnt_attach_dynamic(&xyc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
577 	    xyc->sc_dev.dv_xname, "intr");
578 
579 	callout_init(&xyc->sc_tick_ch, 0);
580 
581 	/* now we must look for disks using autoconfig */
582 	xa.fullmode = XY_SUB_POLL;
583 	xa.booting = 1;
584 
585 	for (xa.driveno = 0; xa.driveno < XYC_MAXDEV; xa.driveno++)
586 		(void) config_found(self, (void *) &xa, NULL);
587 
588 	/* start the watchdog clock */
589 	callout_reset(&xyc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xyc);
590 
591 }
592 
593 /*
594  * xymatch: probe for disk.
595  *
596  * note: we almost always say disk is present.   this allows us to
597  * spin up and configure a disk after the system is booted (we can
598  * call xyattach!).
599  */
600 int
601 xymatch(parent, cf, aux)
602 	struct device *parent;
603 	struct cfdata *cf;
604 	void *aux;
605 {
606 	struct xyc_attach_args *xa = aux;
607 
608 	/* looking for autoconf wildcard or exact match */
609 
610 	if (cf->cf_loc[XYCCF_DRIVE] != XYCCF_DRIVE_DEFAULT &&
611 	    cf->cf_loc[XYCCF_DRIVE] != xa->driveno)
612 		return 0;
613 
614 	return 1;
615 
616 }
617 
618 /*
619  * xyattach: attach a disk.   this can be called from autoconf and also
620  * from xyopen/xystrategy.
621  */
622 void
623 xyattach(parent, self, aux)
624 	struct device *parent, *self;
625 	void   *aux;
626 
627 {
628 	struct xy_softc *xy = (void *) self, *oxy;
629 	struct xyc_softc *xyc = (void *) parent;
630 	struct xyc_attach_args *xa = aux;
631 	int     spt, mb, blk, lcv, fmode, s = 0, newstate;
632 	struct dkbad *dkb;
633 	int			rseg, error;
634 	bus_dma_segment_t	seg;
635 	bus_addr_t		busaddr;
636 	void *			dmaddr;
637 	char *			buf;
638 
639 	/*
640 	 * Always re-initialize the disk structure.  We want statistics
641 	 * to start with a clean slate.
642 	 */
643 	bzero(&xy->sc_dk, sizeof(xy->sc_dk));
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, "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 				     (void **)&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 = (void *)(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_init(&xy->sc_dk, xy->sc_dev.dv_xname, &xydkdriver);
740 	disk_attach(&xy->sc_dk);
741 
742 	if (xygetdisklabel(xy, buf) != XY_ERR_AOK)
743 		goto done;
744 
745 	/* inform the user of what is up */
746 	printf("%s: <%s>, pcyl %d\n", xy->sc_dev.dv_xname,
747 		buf, xy->pcyl);
748 	mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
749 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
750 		xy->sc_dev.dv_xname, mb, xy->ncyl, xy->nhead, xy->nsect,
751 		XYFM_BPS);
752 
753 	/*
754 	 * 450/451 stupidity: the drive type is encoded into the format
755 	 * of the disk.   the drive type in the IOPB must match the drive
756 	 * type in the format, or you will not be able to do I/O to the
757 	 * disk (you get header not found errors).  if you have two drives
758 	 * of different sizes that have the same drive type in their
759 	 * formatting then you are out of luck.
760 	 *
761 	 * this problem was corrected in the 753/7053.
762 	 */
763 
764 	for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
765 		oxy = xyc->sc_drives[lcv];
766 		if (oxy == NULL || oxy == xy) continue;
767 		if (oxy->drive_type != xy->drive_type) continue;
768 		if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
769 			xy->nhead != oxy->nhead) {
770 			printf("%s: %s and %s must be the same size!\n",
771 				xyc->sc_dev.dv_xname, xy->sc_dev.dv_xname,
772 				oxy->sc_dev.dv_xname);
773 			panic("xy drive size mismatch");
774 		}
775 	}
776 
777 
778 	/* now set the real drive parameters! */
779 
780 	blk = (xy->nsect - 1) +
781 		((xy->nhead - 1) * xy->nsect) +
782 		((xy->pcyl - 1) * xy->nsect * xy->nhead);
783 	error = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fmode);
784 	XYC_DONE(xyc, error);
785 	if (error) {
786 		printf("%s: write drive size failed: %s\n",
787 			xy->sc_dev.dv_xname, xyc_e2str(error));
788 		goto done;
789 	}
790 	newstate = XY_DRIVE_ONLINE;
791 
792 	/*
793 	 * read bad144 table. this table resides on the first sector of the
794 	 * last track of the disk (i.e. second cyl of "acyl" area).
795 	 */
796 
797 	blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
798 								/* last cyl */
799 	    (xy->nhead - 1) * xy->nsect;	/* last head */
800 	error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
801 						dmaddr, fmode);
802 	XYC_DONE(xyc, error);
803 	if (error) {
804 		printf("%s: reading bad144 failed: %s\n",
805 			xy->sc_dev.dv_xname, xyc_e2str(error));
806 		goto done;
807 	}
808 
809 	/* check dkbad for sanity */
810 	dkb = (struct dkbad *) buf;
811 	for (lcv = 0; lcv < 126; lcv++) {
812 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
813 				dkb->bt_bad[lcv].bt_cyl == 0) &&
814 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
815 			continue;	/* blank */
816 		if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
817 			break;
818 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
819 			break;
820 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
821 			break;
822 	}
823 	if (lcv != 126) {
824 		printf("%s: warning: invalid bad144 sector!\n",
825 			xy->sc_dev.dv_xname);
826 	} else {
827 		bcopy(buf, &xy->dkb, XYFM_BPS);
828 	}
829 
830 done:
831 	if (buf != NULL) {
832 		xy_dmamem_free(xyc->dmatag, xyc->auxmap,
833 				&seg, rseg, XYFM_BPS, buf);
834 	}
835 
836 	xy->state = newstate;
837 	if (!xa->booting) {
838 		wakeup(&xy->state);
839 		splx(s);
840 	}
841 }
842 
843 /*
844  * end of autoconfig functions
845  */
846 
847 /*
848  * { b , c } d e v s w   f u n c t i o n s
849  */
850 
851 /*
852  * xyclose: close device
853  */
854 int
855 xyclose(dev, flag, fmt, l)
856 	dev_t   dev;
857 	int     flag, fmt;
858 	struct lwp *l;
859 
860 {
861 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
862 	int     part = DISKPART(dev);
863 
864 	/* clear mask bits */
865 
866 	switch (fmt) {
867 	case S_IFCHR:
868 		xy->sc_dk.dk_copenmask &= ~(1 << part);
869 		break;
870 	case S_IFBLK:
871 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
872 		break;
873 	}
874 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
875 
876 	return 0;
877 }
878 
879 /*
880  * xydump: crash dump system
881  */
882 int
883 xydump(dev, blkno, va, size)
884 	dev_t dev;
885 	daddr_t blkno;
886 	void *va;
887 	size_t size;
888 {
889 	int     unit, part;
890 	struct xy_softc *xy;
891 
892 	unit = DISKUNIT(dev);
893 	if (unit >= xy_cd.cd_ndevs)
894 		return ENXIO;
895 	part = DISKPART(dev);
896 
897 	xy = xy_cd.cd_devs[unit];
898 
899 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
900 	    'a' + part);
901 
902 	return ENXIO;
903 
904 	/* outline: globals: "dumplo" == sector number of partition to start
905 	 * dump at (convert to physical sector with partition table)
906 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
907 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
908 	 * physmem)
909 	 *
910 	 * dump a copy of physical memory to the dump device starting at sector
911 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
912 	 * we go.   use polled I/O.
913 	 *
914 	 * XXX how to handle NON_CONTIG? */
915 
916 }
917 
918 /*
919  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
920  */
921 int
922 xyioctl(dev, command, addr, flag, l)
923 	dev_t   dev;
924 	u_long  command;
925 	void *addr;
926 	int     flag;
927 	struct lwp *l;
928 
929 {
930 	struct xy_softc *xy;
931 	struct xd_iocmd *xio;
932 	int     error, s, unit;
933 #ifdef __HAVE_OLD_DISKLABEL
934 	struct disklabel newlabel;
935 #endif
936 	struct disklabel *lp;
937 
938 	unit = DISKUNIT(dev);
939 
940 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
941 		return (ENXIO);
942 
943 	/* switch on ioctl type */
944 
945 	switch (command) {
946 	case DIOCSBAD:		/* set bad144 info */
947 		if ((flag & FWRITE) == 0)
948 			return EBADF;
949 		s = splbio();
950 		bcopy(addr, &xy->dkb, sizeof(xy->dkb));
951 		splx(s);
952 		return 0;
953 
954 	case DIOCGDINFO:	/* get disk label */
955 		bcopy(xy->sc_dk.dk_label, addr, sizeof(struct disklabel));
956 		return 0;
957 #ifdef __HAVE_OLD_DISKLABEL
958 	case ODIOCGDINFO:
959 		newlabel = *(xy->sc_dk.dk_label);
960 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
961 			return ENOTTY;
962 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
963 		return 0;
964 #endif
965 
966 	case DIOCGPART:	/* get partition info */
967 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
968 		((struct partinfo *) addr)->part =
969 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
970 		return 0;
971 
972 	case DIOCSDINFO:	/* set disk label */
973 #ifdef __HAVE_OLD_DISKLABEL
974 	case ODIOCSDINFO:
975 		if (command == ODIOCSDINFO) {
976 			memset(&newlabel, 0, sizeof newlabel);
977 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
978 			lp = &newlabel;
979 		} else
980 #endif
981 		lp = (struct disklabel *)addr;
982 
983 		if ((flag & FWRITE) == 0)
984 			return EBADF;
985 		error = setdisklabel(xy->sc_dk.dk_label,
986 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
987 		    xy->sc_dk.dk_cpulabel);
988 		if (error == 0) {
989 			if (xy->state == XY_DRIVE_NOLABEL)
990 				xy->state = XY_DRIVE_ONLINE;
991 		}
992 		return error;
993 
994 	case DIOCWLABEL:	/* change write status of disk label */
995 		if ((flag & FWRITE) == 0)
996 			return EBADF;
997 		if (*(int *) addr)
998 			xy->flags |= XY_WLABEL;
999 		else
1000 			xy->flags &= ~XY_WLABEL;
1001 		return 0;
1002 
1003 	case DIOCWDINFO:	/* write disk label */
1004 #ifdef __HAVE_OLD_DISKLABEL
1005 	case ODIOCWDINFO:
1006 		if (command == ODIOCWDINFO) {
1007 			memset(&newlabel, 0, sizeof newlabel);
1008 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1009 			lp = &newlabel;
1010 		} else
1011 #endif
1012 		lp = (struct disklabel *)addr;
1013 
1014 		if ((flag & FWRITE) == 0)
1015 			return EBADF;
1016 		error = setdisklabel(xy->sc_dk.dk_label,
1017 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
1018 		    xy->sc_dk.dk_cpulabel);
1019 		if (error == 0) {
1020 			if (xy->state == XY_DRIVE_NOLABEL)
1021 				xy->state = XY_DRIVE_ONLINE;
1022 
1023 			/* Simulate opening partition 0 so write succeeds. */
1024 			xy->sc_dk.dk_openmask |= (1 << 0);
1025 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
1026 			    xystrategy, xy->sc_dk.dk_label,
1027 			    xy->sc_dk.dk_cpulabel);
1028 			xy->sc_dk.dk_openmask =
1029 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1030 		}
1031 		return error;
1032 
1033 	case DIOSXDCMD:
1034 		xio = (struct xd_iocmd *) addr;
1035 		if ((error = kauth_authorize_generic(l->l_cred,
1036 		    KAUTH_GENERIC_ISSUSER, NULL)) != 0)
1037 			return (error);
1038 		return (xyc_ioctlcmd(xy, dev, xio));
1039 
1040 	default:
1041 		return ENOTTY;
1042 	}
1043 }
1044 
1045 /*
1046  * xyopen: open drive
1047  */
1048 
1049 int
1050 xyopen(dev, flag, fmt, l)
1051 	dev_t   dev;
1052 	int     flag, fmt;
1053 	struct lwp *l;
1054 {
1055 	int     unit, part;
1056 	struct xy_softc *xy;
1057 	struct xyc_attach_args xa;
1058 
1059 	/* first, could it be a valid target? */
1060 
1061 	unit = DISKUNIT(dev);
1062 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
1063 		return (ENXIO);
1064 	part = DISKPART(dev);
1065 
1066 	/* do we need to attach the drive? */
1067 
1068 	if (xy->state == XY_DRIVE_UNKNOWN) {
1069 		xa.driveno = xy->xy_drive;
1070 		xa.fullmode = XY_SUB_WAIT;
1071 		xa.booting = 0;
1072 		xyattach((struct device *) xy->parent,
1073 						(struct device *) xy, &xa);
1074 		if (xy->state == XY_DRIVE_UNKNOWN) {
1075 			return (EIO);
1076 		}
1077 	}
1078 	/* check for partition */
1079 
1080 	if (part != RAW_PART &&
1081 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
1082 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1083 		return (ENXIO);
1084 	}
1085 	/* set open masks */
1086 
1087 	switch (fmt) {
1088 	case S_IFCHR:
1089 		xy->sc_dk.dk_copenmask |= (1 << part);
1090 		break;
1091 	case S_IFBLK:
1092 		xy->sc_dk.dk_bopenmask |= (1 << part);
1093 		break;
1094 	}
1095 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1096 
1097 	return 0;
1098 }
1099 
1100 int
1101 xyread(dev, uio, flags)
1102 	dev_t   dev;
1103 	struct uio *uio;
1104 	int flags;
1105 {
1106 
1107 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
1108 }
1109 
1110 int
1111 xywrite(dev, uio, flags)
1112 	dev_t   dev;
1113 	struct uio *uio;
1114 	int flags;
1115 {
1116 
1117 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
1118 }
1119 
1120 
1121 /*
1122  * xysize: return size of a partition for a dump
1123  */
1124 
1125 int
1126 xysize(dev)
1127 	dev_t   dev;
1128 
1129 {
1130 	struct xy_softc *xysc;
1131 	int     unit, part, size, omask;
1132 
1133 	/* valid unit? */
1134 	unit = DISKUNIT(dev);
1135 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
1136 		return (-1);
1137 
1138 	part = DISKPART(dev);
1139 	omask = xysc->sc_dk.dk_openmask & (1 << part);
1140 
1141 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
1142 		return (-1);
1143 
1144 	/* do it */
1145 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1146 		size = -1;	/* only give valid size for swap partitions */
1147 	else
1148 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
1149 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1150 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
1151 		return (-1);
1152 	return (size);
1153 }
1154 
1155 /*
1156  * xystrategy: buffering system interface to xy.
1157  */
1158 
1159 void
1160 xystrategy(bp)
1161 	struct buf *bp;
1162 
1163 {
1164 	struct xy_softc *xy;
1165 	int     s, unit;
1166 	struct xyc_attach_args xa;
1167 	struct disklabel *lp;
1168 	daddr_t blkno;
1169 
1170 	unit = DISKUNIT(bp->b_dev);
1171 
1172 	/* check for live device */
1173 
1174 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
1175 	    bp->b_blkno < 0 ||
1176 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
1177 		bp->b_error = EINVAL;
1178 		goto done;
1179 	}
1180 	/* do we need to attach the drive? */
1181 
1182 	if (xy->state == XY_DRIVE_UNKNOWN) {
1183 		xa.driveno = xy->xy_drive;
1184 		xa.fullmode = XY_SUB_WAIT;
1185 		xa.booting = 0;
1186 		xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
1187 		if (xy->state == XY_DRIVE_UNKNOWN) {
1188 			bp->b_error = EIO;
1189 			goto done;
1190 		}
1191 	}
1192 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1193 		/* no I/O to unlabeled disks, unless raw partition */
1194 		bp->b_error = EIO;
1195 		goto done;
1196 	}
1197 	/* short circuit zero length request */
1198 
1199 	if (bp->b_bcount == 0)
1200 		goto done;
1201 
1202 	/* check bounds with label (disksubr.c).  Determine the size of the
1203 	 * transfer, and make sure it is within the boundaries of the
1204 	 * partition. Adjust transfer if needed, and signal errors or early
1205 	 * completion. */
1206 
1207 	lp = xy->sc_dk.dk_label;
1208 
1209 	if (bounds_check_with_label(&xy->sc_dk, bp,
1210 		(xy->flags & XY_WLABEL) != 0) <= 0)
1211 		goto done;
1212 
1213 	/*
1214 	 * Now convert the block number to absolute and put it in
1215 	 * terms of the device's logical block size.
1216 	 */
1217 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1218 	if (DISKPART(bp->b_dev) != RAW_PART)
1219 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
1220 
1221 	bp->b_rawblkno = blkno;
1222 
1223 	/*
1224 	 * now we know we have a valid buf structure that we need to do I/O
1225 	 * on.
1226 	 */
1227 	s = splbio();		/* protect the queues */
1228 
1229 	BUFQ_PUT(xy->xyq, bp);
1230 
1231 	/* start 'em up */
1232 
1233 	xyc_start(xy->parent, NULL);
1234 
1235 	/* done! */
1236 
1237 	splx(s);
1238 	return;
1239 
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 	void *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->errnum = 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->errnum = 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 		   (void *)(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->errnum);
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, 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->errnum);
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->errnum;
1727 
1728 #ifdef XYC_DEBUG
1729 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1730 	    iorq->errnum, xyc_e2str(iorq->errnum));
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, 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->errnum));
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->errnum = 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_resid = iorq->sectcnt * XYFM_BPS;
1836 
1837 			    bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1838 					iorq->dmamap->dm_mapsize,
1839 					(iorq->buf->b_flags & B_READ)
1840 						? BUS_DMASYNC_POSTREAD
1841 						: BUS_DMASYNC_POSTWRITE);
1842 
1843 			    bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1844 
1845 			    (void)BUFQ_GET(iorq->xy->xyq);
1846 			    disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1847 				(xycsc->reqs[lcv].buf->b_bcount -
1848 				xycsc->reqs[lcv].buf->b_resid),
1849 				(xycsc->reqs[lcv].buf->b_flags & B_READ));
1850 			    biodone(iorq->buf);
1851 			    iorq->mode = XY_SUB_FREE;
1852 			    break;
1853 			case XY_SUB_WAIT:
1854 			    wakeup(iorq);
1855 			case XY_SUB_POLL:
1856 			    iorq->mode =
1857 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1858 			    break;
1859 			}
1860 
1861 		} else {
1862 
1863 			/* resubmit, no need to do anything here */
1864 		}
1865 	}
1866 
1867 	/*
1868 	 * now, if stuff is waiting, start it.
1869 	 * since we just reset it should go
1870 	 */
1871 	xyc_start(xycsc, NULL);
1872 
1873 	return (retval);
1874 }
1875 
1876 /*
1877  * xyc_start: start waiting buffers
1878  */
1879 
1880 void
1881 xyc_start(xycsc, iorq)
1882 	struct xyc_softc *xycsc;
1883 	struct xy_iorq *iorq;
1884 
1885 {
1886 	int lcv;
1887 	struct xy_softc *xy;
1888 
1889 	if (iorq == NULL) {
1890 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1891 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1892 			if (BUFQ_PEEK(xy->xyq) == NULL) continue;
1893 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
1894 			xyc_startbuf(xycsc, xy, BUFQ_PEEK(xy->xyq));
1895 		}
1896 	}
1897 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1898 }
1899 
1900 /*
1901  * xyc_remove_iorq: remove "done" IOPB's.
1902  */
1903 
1904 int
1905 xyc_remove_iorq(xycsc)
1906 	struct xyc_softc *xycsc;
1907 
1908 {
1909 	int     errnum, rq, comm, errs;
1910 	struct xyc *xyc = xycsc->xyc;
1911 	u_long  addr;
1912 	struct xy_iopb *iopb;
1913 	struct xy_iorq *iorq;
1914 	struct buf *bp;
1915 
1916 	if (xyc->xyc_csr & XYC_DERR) {
1917 		/*
1918 		 * DOUBLE ERROR: should never happen under normal use. This
1919 		 * error is so bad, you can't even tell which IOPB is bad, so
1920 		 * we dump them all.
1921 		 */
1922 		errnum = XY_ERR_DERR;
1923 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1924 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errnum, 0) != XY_ERR_AOK) {
1925 			printf("%s: soft reset failed!\n",
1926 				xycsc->sc_dev.dv_xname);
1927 			panic("xyc_remove_iorq: controller DEAD");
1928 		}
1929 		return (XY_ERR_AOK);
1930 	}
1931 
1932 	/*
1933 	 * get iopb that is done, loop down the chain
1934 	 */
1935 
1936 	if (xyc->xyc_csr & XYC_ERR) {
1937 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
1938 	}
1939 	if (xyc->xyc_csr & XYC_IPND) {
1940 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1941 	}
1942 
1943 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1944 		iorq = xycsc->xy_chain[rq];
1945 		if (iorq == NULL) break; /* done ! */
1946 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1947 			continue;	/* free, or done */
1948 		iopb = iorq->iopb;
1949 		if (iopb->done == 0)
1950 			continue;	/* not done yet */
1951 
1952 		comm = iopb->com;
1953 		errs = iopb->errs;
1954 
1955 		if (errs)
1956 			iorq->errnum = iopb->errnum;
1957 		else
1958 			iorq->errnum = 0;
1959 
1960 		/* handle non-fatal errors */
1961 
1962 		if (errs &&
1963 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1964 			continue;	/* AOK: we resubmitted it */
1965 
1966 
1967 		/* this iorq is now done (hasn't been restarted or anything) */
1968 
1969 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1970 			xyc_perror(iorq, iopb, 0);
1971 
1972 		/* now, if read/write check to make sure we got all the data
1973 		 * we needed. (this may not be the case if we got an error in
1974 		 * the middle of a multisector request).   */
1975 
1976 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1977 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
1978 			/* we just successfully processed a bad144 sector
1979 			 * note: if we are in bad 144 mode, the pointers have
1980 			 * been advanced already (see above) and are pointing
1981 			 * at the bad144 sector.   to exit bad144 mode, we
1982 			 * must advance the pointers 1 sector and issue a new
1983 			 * request if there are still sectors left to process
1984 			 *
1985 			 */
1986 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
1987 
1988 			/* exit b144 mode */
1989 			iorq->mode = iorq->mode & (~XY_MODE_B144);
1990 
1991 			if (iorq->sectcnt) {	/* more to go! */
1992 				iorq->lasterror = iorq->errnum = iopb->errnum = 0;
1993 				iopb->errs = iopb->done = 0;
1994 				iorq->tries = 0;
1995 				iopb->scnt = iorq->sectcnt;
1996 				iopb->cyl = iorq->blockno /
1997 						iorq->xy->sectpercyl;
1998 				iopb->head =
1999 					(iorq->blockno / iorq->xy->nhead) %
2000 						iorq->xy->nhead;
2001 				iopb->sect = iorq->blockno % XYFM_BPS;
2002 				addr = (u_long) iorq->dbuf;
2003 				iopb->dataa = (addr & 0xffff);
2004 				iopb->datar = ((addr & 0xff0000) >> 16);
2005 				/* will resubit at end */
2006 				continue;
2007 			}
2008 		}
2009 		/* final cleanup, totally done with this request */
2010 
2011 		switch (XY_STATE(iorq->mode)) {
2012 		case XY_SUB_NORM:
2013 			bp = iorq->buf;
2014 			if (errs) {
2015 				bp->b_error = EIO;
2016 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
2017 			} else {
2018 				bp->b_resid = 0;	/* done */
2019 			}
2020 			bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
2021 					iorq->dmamap->dm_mapsize,
2022 					(iorq->buf->b_flags & B_READ)
2023 						? BUS_DMASYNC_POSTREAD
2024 						: BUS_DMASYNC_POSTWRITE);
2025 
2026 			bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
2027 
2028 			(void)BUFQ_GET(iorq->xy->xyq);
2029 			disk_unbusy(&iorq->xy->sc_dk,
2030 			    (bp->b_bcount - bp->b_resid),
2031 			    (bp->b_flags & B_READ));
2032 			iorq->mode = XY_SUB_FREE;
2033 			biodone(bp);
2034 			break;
2035 		case XY_SUB_WAIT:
2036 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2037 			wakeup(iorq);
2038 			break;
2039 		case XY_SUB_POLL:
2040 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2041 			break;
2042 		}
2043 	}
2044 
2045 	return (XY_ERR_AOK);
2046 }
2047 
2048 /*
2049  * xyc_perror: print error.
2050  * - if still_trying is true: we got an error, retried and got a
2051  *   different error.  in that case lasterror is the old error,
2052  *   and errnum is the new one.
2053  * - if still_trying is not true, then if we ever had an error it
2054  *   is in lasterror. also, if iorq->errnum == 0, then we recovered
2055  *   from that error (otherwise iorq->errnum == iorq->lasterror).
2056  */
2057 void
2058 xyc_perror(iorq, iopb, still_trying)
2059 	struct xy_iorq *iorq;
2060 	struct xy_iopb *iopb;
2061 	int     still_trying;
2062 
2063 {
2064 
2065 	int     error = iorq->lasterror;
2066 
2067 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
2068 	    : iorq->xyc->sc_dev.dv_xname);
2069 	if (iorq->buf)
2070 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
2071 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
2072 		printf("%s %d/%d/%d: ",
2073 			(iopb->com == XYCMD_RD) ? "read" : "write",
2074 			iopb->cyl, iopb->head, iopb->sect);
2075 	printf("%s", xyc_e2str(error));
2076 
2077 	if (still_trying)
2078 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errnum));
2079 	else
2080 		if (iorq->errnum == 0)
2081 			printf(" [recovered in %d tries]", iorq->tries);
2082 
2083 	printf("\n");
2084 }
2085 
2086 /*
2087  * xyc_error: non-fatal error encountered... recover.
2088  * return AOK if resubmitted, return FAIL if this iopb is done
2089  */
2090 int
2091 xyc_error(xycsc, iorq, iopb, comm)
2092 	struct xyc_softc *xycsc;
2093 	struct xy_iorq *iorq;
2094 	struct xy_iopb *iopb;
2095 	int     comm;
2096 
2097 {
2098 	int     errnum = iorq->errnum;
2099 	int     erract = xyc_entoact(errnum);
2100 	int     oldmode, advance;
2101 #ifdef __sparc__
2102 	int i;
2103 #endif
2104 
2105 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
2106 		oldmode = iorq->mode;
2107 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
2108 		/* make xyc_start ignore us */
2109 		xyc_reset(xycsc, 1, XY_RSET_NONE, errnum, iorq->xy);
2110 		iorq->mode = oldmode;
2111 	}
2112 	/* check for read/write to a sector in bad144 table if bad: redirect
2113 	 * request to bad144 area */
2114 
2115 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
2116 	    (iorq->mode & XY_MODE_B144) == 0) {
2117 		advance = iorq->sectcnt - iopb->scnt;
2118 		XYC_ADVANCE(iorq, advance);
2119 #ifdef __sparc__
2120 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
2121 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
2122 			    iorq->blockno % iorq->xy->nsect)) != -1) {
2123 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
2124 							 * redirect */
2125 			iopb->errnum = iopb->done = iopb->errs = 0;
2126 			iopb->scnt = 1;
2127 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
2128 			/* second to last acyl */
2129 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
2130 								 * standard */
2131 			iopb->head = i / iorq->xy->nhead;
2132 			iopb->sect = i % iorq->xy->nhead;
2133 			/* will resubmit when we come out of remove_iorq */
2134 			return (XY_ERR_AOK);	/* recovered! */
2135 		}
2136 #endif
2137 	}
2138 
2139 	/*
2140 	 * it isn't a bad144 sector, must be real error! see if we can retry
2141 	 * it?
2142 	 */
2143 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
2144 		xyc_perror(iorq, iopb, 1);	/* inform of error state
2145 						 * change */
2146 	iorq->lasterror = errnum;
2147 
2148 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
2149 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
2150 		iorq->tries++;
2151 		iorq->errnum = iopb->errnum = iopb->done = iopb->errs = 0;
2152 		/* will resubmit at end of remove_iorq */
2153 		return (XY_ERR_AOK);	/* recovered! */
2154 	}
2155 
2156 	/* failed to recover from this error */
2157 	return (XY_ERR_FAIL);
2158 }
2159 
2160 /*
2161  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
2162  */
2163 void
2164 xyc_tick(arg)
2165 	void   *arg;
2166 
2167 {
2168 	struct xyc_softc *xycsc = arg;
2169 	int     lcv, s, reset = 0;
2170 
2171 	/* reduce ttl for each request if one goes to zero, reset xyc */
2172 	s = splbio();
2173 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
2174 		if (xycsc->reqs[lcv].mode == 0 ||
2175 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
2176 			continue;
2177 		xycsc->reqs[lcv].ttl--;
2178 		if (xycsc->reqs[lcv].ttl == 0)
2179 			reset = 1;
2180 	}
2181 	if (reset) {
2182 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
2183 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
2184 	}
2185 	splx(s);
2186 
2187 	/* until next time */
2188 
2189 	callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
2190 }
2191 
2192 /*
2193  * xyc_ioctlcmd: this function provides a user level interface to the
2194  * controller via ioctl.   this allows "format" programs to be written
2195  * in user code, and is also useful for some debugging.   we return
2196  * an error code.   called at user priority.
2197  *
2198  * XXX missing a few commands (see the 7053 driver for ideas)
2199  */
2200 int
2201 xyc_ioctlcmd(xy, dev, xio)
2202 	struct xy_softc *xy;
2203 	dev_t   dev;
2204 	struct xd_iocmd *xio;
2205 
2206 {
2207 	int     s, rqno, dummy = 0;
2208 	char *dvmabuf = NULL, *buf = NULL;
2209 	struct xyc_softc *xycsc;
2210 	int			rseg, error;
2211 	bus_dma_segment_t	seg;
2212 
2213 	/* check sanity of requested command */
2214 
2215 	switch (xio->cmd) {
2216 
2217 	case XYCMD_NOP:	/* no op: everything should be zero */
2218 		if (xio->subfn || xio->dptr || xio->dlen ||
2219 		    xio->block || xio->sectcnt)
2220 			return (EINVAL);
2221 		break;
2222 
2223 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
2224 	case XYCMD_WR:
2225 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2226 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2227 			return (EINVAL);
2228 		break;
2229 
2230 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
2231 		return (EINVAL);
2232 
2233 		break;
2234 
2235 	default:
2236 		return (EINVAL);/* ??? */
2237 	}
2238 
2239 	xycsc = xy->parent;
2240 
2241 	/* create DVMA buffer for request if needed */
2242 	if (xio->dlen) {
2243 		bus_addr_t busbuf;
2244 
2245 		if ((error = xy_dmamem_alloc(xycsc->dmatag, xycsc->auxmap,
2246 					     &seg, &rseg,
2247 					     xio->dlen, (void **)&buf,
2248 					     &busbuf)) != 0) {
2249 			return (error);
2250 		}
2251 		dvmabuf = (void *)(u_long)BUS_ADDR_PADDR(busbuf);
2252 
2253 		if (xio->cmd == XYCMD_WR) {
2254 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
2255 				bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
2256 				bus_dmamem_free(xycsc->dmatag, &seg, rseg);
2257 				return (error);
2258 			}
2259 		}
2260 	}
2261 	/* do it! */
2262 
2263 	error = 0;
2264 	s = splbio();
2265 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2266 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2267 	if (rqno == XY_ERR_FAIL) {
2268 		error = EIO;
2269 		goto done;
2270 	}
2271 	xio->errnum = xycsc->ciorq->errnum;
2272 	xio->tries = xycsc->ciorq->tries;
2273 	XYC_DONE(xycsc, dummy);
2274 
2275 	if (xio->cmd == XYCMD_RD)
2276 		error = copyout(buf, xio->dptr, xio->dlen);
2277 
2278 done:
2279 	splx(s);
2280 	if (dvmabuf) {
2281 		xy_dmamem_free(xycsc->dmatag, xycsc->auxmap, &seg, rseg,
2282 				xio->dlen, buf);
2283 	}
2284 	return (error);
2285 }
2286 
2287 /*
2288  * xyc_e2str: convert error code number into an error string
2289  */
2290 const char *
2291 xyc_e2str(no)
2292 	int     no;
2293 {
2294 	switch (no) {
2295 	case XY_ERR_FAIL:
2296 		return ("Software fatal error");
2297 	case XY_ERR_DERR:
2298 		return ("DOUBLE ERROR");
2299 	case XY_ERR_AOK:
2300 		return ("Successful completion");
2301 	case XY_ERR_IPEN:
2302 		return("Interrupt pending");
2303 	case XY_ERR_BCFL:
2304 		return("Busy conflict");
2305 	case XY_ERR_TIMO:
2306 		return("Operation timeout");
2307 	case XY_ERR_NHDR:
2308 		return("Header not found");
2309 	case XY_ERR_HARD:
2310 		return("Hard ECC error");
2311 	case XY_ERR_ICYL:
2312 		return("Illegal cylinder address");
2313 	case XY_ERR_ISEC:
2314 		return("Illegal sector address");
2315 	case XY_ERR_SMAL:
2316 		return("Last sector too small");
2317 	case XY_ERR_SACK:
2318 		return("Slave ACK error (non-existent memory)");
2319 	case XY_ERR_CHER:
2320 		return("Cylinder and head/header error");
2321 	case XY_ERR_SRTR:
2322 		return("Auto-seek retry successful");
2323 	case XY_ERR_WPRO:
2324 		return("Write-protect error");
2325 	case XY_ERR_UIMP:
2326 		return("Unimplemented command");
2327 	case XY_ERR_DNRY:
2328 		return("Drive not ready");
2329 	case XY_ERR_SZER:
2330 		return("Sector count zero");
2331 	case XY_ERR_DFLT:
2332 		return("Drive faulted");
2333 	case XY_ERR_ISSZ:
2334 		return("Illegal sector size");
2335 	case XY_ERR_SLTA:
2336 		return("Self test A");
2337 	case XY_ERR_SLTB:
2338 		return("Self test B");
2339 	case XY_ERR_SLTC:
2340 		return("Self test C");
2341 	case XY_ERR_SOFT:
2342 		return("Soft ECC error");
2343 	case XY_ERR_SFOK:
2344 		return("Soft ECC error recovered");
2345 	case XY_ERR_IHED:
2346 		return("Illegal head");
2347 	case XY_ERR_DSEQ:
2348 		return("Disk sequencer error");
2349 	case XY_ERR_SEEK:
2350 		return("Seek error");
2351 	default:
2352 		return ("Unknown error");
2353 	}
2354 }
2355 
2356 int
2357 xyc_entoact(errnum)
2358 
2359 int errnum;
2360 
2361 {
2362   switch (errnum) {
2363     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
2364     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
2365     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
2366     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
2367     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
2368 
2369 	return(XY_ERA_PROG); /* program error ! */
2370 
2371     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
2372     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
2373     case XY_ERR_SOFT:
2374 
2375 	return(XY_ERA_HARD); /* hard error, retry */
2376 
2377     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
2378 
2379 	return(XY_ERA_RSET); /* hard error reset */
2380 
2381     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
2382 
2383 	return(XY_ERA_SOFT); /* an FYI error */
2384 
2385     case XY_ERR_WPRO:
2386 
2387 	return(XY_ERA_WPRO); /* write protect */
2388   }
2389 
2390   return(XY_ERA_PROG); /* ??? */
2391 }
2392