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