xref: /netbsd-src/sys/dev/vme/xy.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: xy.c,v 1.65 2006/05/15 20:40:55 yamt Exp $	*/
2 
3 /*
4  *
5  * Copyright (c) 1995 Charles D. Cranor
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Charles D. Cranor.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  *
36  * x y . c   x y l o g i c s   4 5 0 / 4 5 1   s m d   d r i v e r
37  *
38  * author: Chuck Cranor <chuck@ccrc.wustl.edu>
39  * started: 14-Sep-95
40  * references: [1] Xylogics Model 753 User's Manual
41  *                 part number: 166-753-001, Revision B, May 21, 1988.
42  *                 "Your Partner For Performance"
43  *             [2] other NetBSD disk device drivers
44  *	       [3] Xylogics Model 450 User's Manual
45  *		   part number: 166-017-001, Revision B, 1983.
46  *	       [4] Addendum to Xylogics Model 450 Disk Controller User's
47  *			Manual, Jan. 1985.
48  *	       [5] The 451 Controller, Rev. B3, September 2, 1986.
49  *	       [6] David Jones <dej@achilles.net>'s unfinished 450/451 driver
50  *
51  */
52 
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: xy.c,v 1.65 2006/05/15 20:40:55 yamt Exp $");
55 
56 #undef XYC_DEBUG		/* full debug */
57 #undef XYC_DIAG			/* extra sanity checks */
58 #if defined(DIAGNOSTIC) && !defined(XYC_DIAG)
59 #define XYC_DIAG		/* link in with master DIAG option */
60 #endif
61 
62 #include <sys/param.h>
63 #include <sys/proc.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/file.h>
67 #include <sys/stat.h>
68 #include <sys/ioctl.h>
69 #include <sys/buf.h>
70 #include <sys/bufq.h>
71 #include <sys/uio.h>
72 #include <sys/malloc.h>
73 #include <sys/device.h>
74 #include <sys/disklabel.h>
75 #include <sys/disk.h>
76 #include <sys/syslog.h>
77 #include <sys/dkbad.h>
78 #include <sys/conf.h>
79 #include <sys/kauth.h>
80 
81 #include <machine/bus.h>
82 #include <machine/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->errno; \
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 			caddr_t, 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, caddr_t *, bus_addr_t *);
179 void	xy_dmamem_free(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
180 			int, bus_size_t, caddr_t);
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 	caddr_t			*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 	caddr_t			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 				     (caddr_t *)&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);
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 	caddr_t			dmaddr;
637 	caddr_t			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 	xy->sc_dk.dk_driver = &xydkdriver;
645 	xy->sc_dk.dk_name = xy->sc_dev.dv_xname;
646 
647 	/* if booting, init the xy_softc */
648 
649 	if (xa->booting) {
650 		xy->state = XY_DRIVE_UNKNOWN;	/* to start */
651 		xy->flags = 0;
652 		xy->parent = xyc;
653 
654 		/* init queue of waiting bufs */
655 
656 		bufq_alloc(&xy->xyq, "disksort", BUFQ_SORT_RAWBLOCK);
657 
658 		xy->xyrq = &xyc->reqs[xa->driveno];
659 
660 	}
661 	xy->xy_drive = xa->driveno;
662 	fmode = xa->fullmode;
663 	xyc->sc_drives[xa->driveno] = xy;
664 
665 	/* if not booting, make sure we are the only process in the attach for
666 	 * this drive.   if locked out, sleep on it. */
667 
668 	if (!xa->booting) {
669 		s = splbio();
670 		while (xy->state == XY_DRIVE_ATTACHING) {
671 			if (tsleep(&xy->state, PRIBIO, "xyattach", 0)) {
672 				splx(s);
673 				return;
674 			}
675 		}
676 		printf("%s at %s",
677 			xy->sc_dev.dv_xname, xy->parent->sc_dev.dv_xname);
678 	}
679 
680 	/* we now have control */
681 	xy->state = XY_DRIVE_ATTACHING;
682 	newstate = XY_DRIVE_UNKNOWN;
683 
684 	buf = NULL;
685 	if ((error = xy_dmamem_alloc(xyc->dmatag, xyc->auxmap, &seg, &rseg,
686 				     XYFM_BPS,
687 				     (caddr_t *)&buf,
688 				     &busaddr)) != 0) {
689 		printf("%s: DMA buffer alloc error %d\n",
690 			xyc->sc_dev.dv_xname, error);
691 		return;
692 	}
693 	dmaddr = (caddr_t)(u_long)BUS_ADDR_PADDR(busaddr);
694 
695 	/* first try and reset the drive */
696 	error = xyc_cmd(xyc, XYCMD_RST, 0, xy->xy_drive, 0, 0, 0, fmode);
697 	XYC_DONE(xyc, error);
698 	if (error == XY_ERR_DNRY) {
699 		printf(" drive %d: off-line\n", xa->driveno);
700 		goto done;
701 	}
702 	if (error) {
703 		printf(": ERROR 0x%02x (%s)\n", error, xyc_e2str(error));
704 		goto done;
705 	}
706 	printf(" drive %d: ready", xa->driveno);
707 
708 	/*
709 	 * now set drive parameters (to semi-bogus values) so we can read the
710 	 * disk label.
711 	 */
712 	xy->pcyl = xy->ncyl = 1;
713 	xy->acyl = 0;
714 	xy->nhead = 1;
715 	xy->nsect = 1;
716 	xy->sectpercyl = 1;
717 	for (lcv = 0; lcv < 126; lcv++)	/* init empty bad144 table */
718 		xy->dkb.bt_bad[lcv].bt_cyl =
719 			xy->dkb.bt_bad[lcv].bt_trksec = 0xffff;
720 
721 	/* read disk label */
722 	for (xy->drive_type = 0 ; xy->drive_type <= XYC_MAXDT ;
723 						xy->drive_type++) {
724 		error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, 0, 1,
725 						dmaddr, fmode);
726 		XYC_DONE(xyc, error);
727 		if (error == XY_ERR_AOK) break;
728 	}
729 
730 	if (error != XY_ERR_AOK) {
731 		printf("\n%s: reading disk label failed: %s\n",
732 			xy->sc_dev.dv_xname, xyc_e2str(error));
733 		goto done;
734 	}
735 	printf(" (drive type %d)\n", xy->drive_type);
736 
737 	newstate = XY_DRIVE_NOLABEL;
738 
739 	xy->hw_spt = spt = 0; /* XXX needed ? */
740 	/* Attach the disk: must be before getdisklabel to malloc label */
741 	disk_attach(&xy->sc_dk);
742 
743 	if (xygetdisklabel(xy, buf) != XY_ERR_AOK)
744 		goto done;
745 
746 	/* inform the user of what is up */
747 	printf("%s: <%s>, pcyl %d\n", xy->sc_dev.dv_xname,
748 		buf, xy->pcyl);
749 	mb = xy->ncyl * (xy->nhead * xy->nsect) / (1048576 / XYFM_BPS);
750 	printf("%s: %dMB, %d cyl, %d head, %d sec, %d bytes/sec\n",
751 		xy->sc_dev.dv_xname, mb, xy->ncyl, xy->nhead, xy->nsect,
752 		XYFM_BPS);
753 
754 	/*
755 	 * 450/451 stupidity: the drive type is encoded into the format
756 	 * of the disk.   the drive type in the IOPB must match the drive
757 	 * type in the format, or you will not be able to do I/O to the
758 	 * disk (you get header not found errors).  if you have two drives
759 	 * of different sizes that have the same drive type in their
760 	 * formatting then you are out of luck.
761 	 *
762 	 * this problem was corrected in the 753/7053.
763 	 */
764 
765 	for (lcv = 0 ; lcv < XYC_MAXDEV ; lcv++) {
766 		oxy = xyc->sc_drives[lcv];
767 		if (oxy == NULL || oxy == xy) continue;
768 		if (oxy->drive_type != xy->drive_type) continue;
769 		if (xy->nsect != oxy->nsect || xy->pcyl != oxy->pcyl ||
770 			xy->nhead != oxy->nhead) {
771 			printf("%s: %s and %s must be the same size!\n",
772 				xyc->sc_dev.dv_xname, xy->sc_dev.dv_xname,
773 				oxy->sc_dev.dv_xname);
774 			panic("xy drive size mismatch");
775 		}
776 	}
777 
778 
779 	/* now set the real drive parameters! */
780 
781 	blk = (xy->nsect - 1) +
782 		((xy->nhead - 1) * xy->nsect) +
783 		((xy->pcyl - 1) * xy->nsect * xy->nhead);
784 	error = xyc_cmd(xyc, XYCMD_SDS, 0, xy->xy_drive, blk, 0, 0, fmode);
785 	XYC_DONE(xyc, error);
786 	if (error) {
787 		printf("%s: write drive size failed: %s\n",
788 			xy->sc_dev.dv_xname, xyc_e2str(error));
789 		goto done;
790 	}
791 	newstate = XY_DRIVE_ONLINE;
792 
793 	/*
794 	 * read bad144 table. this table resides on the first sector of the
795 	 * last track of the disk (i.e. second cyl of "acyl" area).
796 	 */
797 
798 	blk = (xy->ncyl + xy->acyl - 1) * (xy->nhead * xy->nsect) +
799 								/* last cyl */
800 	    (xy->nhead - 1) * xy->nsect;	/* last head */
801 	error = xyc_cmd(xyc, XYCMD_RD, 0, xy->xy_drive, blk, 1,
802 						dmaddr, fmode);
803 	XYC_DONE(xyc, error);
804 	if (error) {
805 		printf("%s: reading bad144 failed: %s\n",
806 			xy->sc_dev.dv_xname, xyc_e2str(error));
807 		goto done;
808 	}
809 
810 	/* check dkbad for sanity */
811 	dkb = (struct dkbad *) buf;
812 	for (lcv = 0; lcv < 126; lcv++) {
813 		if ((dkb->bt_bad[lcv].bt_cyl == 0xffff ||
814 				dkb->bt_bad[lcv].bt_cyl == 0) &&
815 		     dkb->bt_bad[lcv].bt_trksec == 0xffff)
816 			continue;	/* blank */
817 		if (dkb->bt_bad[lcv].bt_cyl >= xy->ncyl)
818 			break;
819 		if ((dkb->bt_bad[lcv].bt_trksec >> 8) >= xy->nhead)
820 			break;
821 		if ((dkb->bt_bad[lcv].bt_trksec & 0xff) >= xy->nsect)
822 			break;
823 	}
824 	if (lcv != 126) {
825 		printf("%s: warning: invalid bad144 sector!\n",
826 			xy->sc_dev.dv_xname);
827 	} else {
828 		bcopy(buf, &xy->dkb, XYFM_BPS);
829 	}
830 
831 done:
832 	if (buf != NULL) {
833 		xy_dmamem_free(xyc->dmatag, xyc->auxmap,
834 				&seg, rseg, XYFM_BPS, buf);
835 	}
836 
837 	xy->state = newstate;
838 	if (!xa->booting) {
839 		wakeup(&xy->state);
840 		splx(s);
841 	}
842 }
843 
844 /*
845  * end of autoconfig functions
846  */
847 
848 /*
849  * { b , c } d e v s w   f u n c t i o n s
850  */
851 
852 /*
853  * xyclose: close device
854  */
855 int
856 xyclose(dev, flag, fmt, l)
857 	dev_t   dev;
858 	int     flag, fmt;
859 	struct lwp *l;
860 
861 {
862 	struct xy_softc *xy = xy_cd.cd_devs[DISKUNIT(dev)];
863 	int     part = DISKPART(dev);
864 
865 	/* clear mask bits */
866 
867 	switch (fmt) {
868 	case S_IFCHR:
869 		xy->sc_dk.dk_copenmask &= ~(1 << part);
870 		break;
871 	case S_IFBLK:
872 		xy->sc_dk.dk_bopenmask &= ~(1 << part);
873 		break;
874 	}
875 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
876 
877 	return 0;
878 }
879 
880 /*
881  * xydump: crash dump system
882  */
883 int
884 xydump(dev, blkno, va, size)
885 	dev_t dev;
886 	daddr_t blkno;
887 	caddr_t va;
888 	size_t size;
889 {
890 	int     unit, part;
891 	struct xy_softc *xy;
892 
893 	unit = DISKUNIT(dev);
894 	if (unit >= xy_cd.cd_ndevs)
895 		return ENXIO;
896 	part = DISKPART(dev);
897 
898 	xy = xy_cd.cd_devs[unit];
899 
900 	printf("%s%c: crash dump not supported (yet)\n", xy->sc_dev.dv_xname,
901 	    'a' + part);
902 
903 	return ENXIO;
904 
905 	/* outline: globals: "dumplo" == sector number of partition to start
906 	 * dump at (convert to physical sector with partition table)
907 	 * "dumpsize" == size of dump in clicks "physmem" == size of physical
908 	 * memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
909 	 * physmem)
910 	 *
911 	 * dump a copy of physical memory to the dump device starting at sector
912 	 * "dumplo" in the swap partition (make sure > 0).   map in pages as
913 	 * we go.   use polled I/O.
914 	 *
915 	 * XXX how to handle NON_CONTIG? */
916 
917 }
918 
919 /*
920  * xyioctl: ioctls on XY drives.   based on ioctl's of other netbsd disks.
921  */
922 int
923 xyioctl(dev, command, addr, flag, l)
924 	dev_t   dev;
925 	u_long  command;
926 	caddr_t addr;
927 	int     flag;
928 	struct lwp *l;
929 
930 {
931 	struct xy_softc *xy;
932 	struct xd_iocmd *xio;
933 	int     error, s, unit;
934 #ifdef __HAVE_OLD_DISKLABEL
935 	struct disklabel newlabel;
936 #endif
937 	struct disklabel *lp;
938 
939 	unit = DISKUNIT(dev);
940 
941 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
942 		return (ENXIO);
943 
944 	/* switch on ioctl type */
945 
946 	switch (command) {
947 	case DIOCSBAD:		/* set bad144 info */
948 		if ((flag & FWRITE) == 0)
949 			return EBADF;
950 		s = splbio();
951 		bcopy(addr, &xy->dkb, sizeof(xy->dkb));
952 		splx(s);
953 		return 0;
954 
955 	case DIOCGDINFO:	/* get disk label */
956 		bcopy(xy->sc_dk.dk_label, addr, sizeof(struct disklabel));
957 		return 0;
958 #ifdef __HAVE_OLD_DISKLABEL
959 	case ODIOCGDINFO:
960 		newlabel = *(xy->sc_dk.dk_label);
961 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
962 			return ENOTTY;
963 		memcpy(addr, &newlabel, sizeof (struct olddisklabel));
964 		return 0;
965 #endif
966 
967 	case DIOCGPART:	/* get partition info */
968 		((struct partinfo *) addr)->disklab = xy->sc_dk.dk_label;
969 		((struct partinfo *) addr)->part =
970 		    &xy->sc_dk.dk_label->d_partitions[DISKPART(dev)];
971 		return 0;
972 
973 	case DIOCSDINFO:	/* set disk label */
974 #ifdef __HAVE_OLD_DISKLABEL
975 	case ODIOCSDINFO:
976 		if (command == ODIOCSDINFO) {
977 			memset(&newlabel, 0, sizeof newlabel);
978 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
979 			lp = &newlabel;
980 		} else
981 #endif
982 		lp = (struct disklabel *)addr;
983 
984 		if ((flag & FWRITE) == 0)
985 			return EBADF;
986 		error = setdisklabel(xy->sc_dk.dk_label,
987 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
988 		    xy->sc_dk.dk_cpulabel);
989 		if (error == 0) {
990 			if (xy->state == XY_DRIVE_NOLABEL)
991 				xy->state = XY_DRIVE_ONLINE;
992 		}
993 		return error;
994 
995 	case DIOCWLABEL:	/* change write status of disk label */
996 		if ((flag & FWRITE) == 0)
997 			return EBADF;
998 		if (*(int *) addr)
999 			xy->flags |= XY_WLABEL;
1000 		else
1001 			xy->flags &= ~XY_WLABEL;
1002 		return 0;
1003 
1004 	case DIOCWDINFO:	/* write disk label */
1005 #ifdef __HAVE_OLD_DISKLABEL
1006 	case ODIOCWDINFO:
1007 		if (command == ODIOCWDINFO) {
1008 			memset(&newlabel, 0, sizeof newlabel);
1009 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1010 			lp = &newlabel;
1011 		} else
1012 #endif
1013 		lp = (struct disklabel *)addr;
1014 
1015 		if ((flag & FWRITE) == 0)
1016 			return EBADF;
1017 		error = setdisklabel(xy->sc_dk.dk_label,
1018 		    lp, /* xy->sc_dk.dk_openmask : */ 0,
1019 		    xy->sc_dk.dk_cpulabel);
1020 		if (error == 0) {
1021 			if (xy->state == XY_DRIVE_NOLABEL)
1022 				xy->state = XY_DRIVE_ONLINE;
1023 
1024 			/* Simulate opening partition 0 so write succeeds. */
1025 			xy->sc_dk.dk_openmask |= (1 << 0);
1026 			error = writedisklabel(MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART),
1027 			    xystrategy, xy->sc_dk.dk_label,
1028 			    xy->sc_dk.dk_cpulabel);
1029 			xy->sc_dk.dk_openmask =
1030 			    xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1031 		}
1032 		return error;
1033 
1034 	case DIOSXDCMD:
1035 		xio = (struct xd_iocmd *) addr;
1036 		if ((error = kauth_authorize_generic(l->l_proc->p_cred,
1037 		    KAUTH_GENERIC_ISSUSER, &l->l_proc->p_acflag)) != 0)
1038 			return (error);
1039 		return (xyc_ioctlcmd(xy, dev, xio));
1040 
1041 	default:
1042 		return ENOTTY;
1043 	}
1044 }
1045 
1046 /*
1047  * xyopen: open drive
1048  */
1049 
1050 int
1051 xyopen(dev, flag, fmt, l)
1052 	dev_t   dev;
1053 	int     flag, fmt;
1054 	struct lwp *l;
1055 {
1056 	int     unit, part;
1057 	struct xy_softc *xy;
1058 	struct xyc_attach_args xa;
1059 
1060 	/* first, could it be a valid target? */
1061 
1062 	unit = DISKUNIT(dev);
1063 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == NULL)
1064 		return (ENXIO);
1065 	part = DISKPART(dev);
1066 
1067 	/* do we need to attach the drive? */
1068 
1069 	if (xy->state == XY_DRIVE_UNKNOWN) {
1070 		xa.driveno = xy->xy_drive;
1071 		xa.fullmode = XY_SUB_WAIT;
1072 		xa.booting = 0;
1073 		xyattach((struct device *) xy->parent,
1074 						(struct device *) xy, &xa);
1075 		if (xy->state == XY_DRIVE_UNKNOWN) {
1076 			return (EIO);
1077 		}
1078 	}
1079 	/* check for partition */
1080 
1081 	if (part != RAW_PART &&
1082 	    (part >= xy->sc_dk.dk_label->d_npartitions ||
1083 		xy->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1084 		return (ENXIO);
1085 	}
1086 	/* set open masks */
1087 
1088 	switch (fmt) {
1089 	case S_IFCHR:
1090 		xy->sc_dk.dk_copenmask |= (1 << part);
1091 		break;
1092 	case S_IFBLK:
1093 		xy->sc_dk.dk_bopenmask |= (1 << part);
1094 		break;
1095 	}
1096 	xy->sc_dk.dk_openmask = xy->sc_dk.dk_copenmask | xy->sc_dk.dk_bopenmask;
1097 
1098 	return 0;
1099 }
1100 
1101 int
1102 xyread(dev, uio, flags)
1103 	dev_t   dev;
1104 	struct uio *uio;
1105 	int flags;
1106 {
1107 
1108 	return (physio(xystrategy, NULL, dev, B_READ, minphys, uio));
1109 }
1110 
1111 int
1112 xywrite(dev, uio, flags)
1113 	dev_t   dev;
1114 	struct uio *uio;
1115 	int flags;
1116 {
1117 
1118 	return (physio(xystrategy, NULL, dev, B_WRITE, minphys, uio));
1119 }
1120 
1121 
1122 /*
1123  * xysize: return size of a partition for a dump
1124  */
1125 
1126 int
1127 xysize(dev)
1128 	dev_t   dev;
1129 
1130 {
1131 	struct xy_softc *xysc;
1132 	int     unit, part, size, omask;
1133 
1134 	/* valid unit? */
1135 	unit = DISKUNIT(dev);
1136 	if (unit >= xy_cd.cd_ndevs || (xysc = xy_cd.cd_devs[unit]) == NULL)
1137 		return (-1);
1138 
1139 	part = DISKPART(dev);
1140 	omask = xysc->sc_dk.dk_openmask & (1 << part);
1141 
1142 	if (omask == 0 && xyopen(dev, 0, S_IFBLK, NULL) != 0)
1143 		return (-1);
1144 
1145 	/* do it */
1146 	if (xysc->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1147 		size = -1;	/* only give valid size for swap partitions */
1148 	else
1149 		size = xysc->sc_dk.dk_label->d_partitions[part].p_size *
1150 		    (xysc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1151 	if (omask == 0 && xyclose(dev, 0, S_IFBLK, NULL) != 0)
1152 		return (-1);
1153 	return (size);
1154 }
1155 
1156 /*
1157  * xystrategy: buffering system interface to xy.
1158  */
1159 
1160 void
1161 xystrategy(bp)
1162 	struct buf *bp;
1163 
1164 {
1165 	struct xy_softc *xy;
1166 	int     s, unit;
1167 	struct xyc_attach_args xa;
1168 	struct disklabel *lp;
1169 	daddr_t blkno;
1170 
1171 	unit = DISKUNIT(bp->b_dev);
1172 
1173 	/* check for live device */
1174 
1175 	if (unit >= xy_cd.cd_ndevs || (xy = xy_cd.cd_devs[unit]) == 0 ||
1176 	    bp->b_blkno < 0 ||
1177 	    (bp->b_bcount % xy->sc_dk.dk_label->d_secsize) != 0) {
1178 		bp->b_error = EINVAL;
1179 		goto bad;
1180 	}
1181 	/* do we need to attach the drive? */
1182 
1183 	if (xy->state == XY_DRIVE_UNKNOWN) {
1184 		xa.driveno = xy->xy_drive;
1185 		xa.fullmode = XY_SUB_WAIT;
1186 		xa.booting = 0;
1187 		xyattach((struct device *)xy->parent, (struct device *)xy, &xa);
1188 		if (xy->state == XY_DRIVE_UNKNOWN) {
1189 			bp->b_error = EIO;
1190 			goto bad;
1191 		}
1192 	}
1193 	if (xy->state != XY_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
1194 		/* no I/O to unlabeled disks, unless raw partition */
1195 		bp->b_error = EIO;
1196 		goto bad;
1197 	}
1198 	/* short circuit zero length request */
1199 
1200 	if (bp->b_bcount == 0)
1201 		goto done;
1202 
1203 	/* check bounds with label (disksubr.c).  Determine the size of the
1204 	 * transfer, and make sure it is within the boundaries of the
1205 	 * partition. Adjust transfer if needed, and signal errors or early
1206 	 * completion. */
1207 
1208 	lp = xy->sc_dk.dk_label;
1209 
1210 	if (bounds_check_with_label(&xy->sc_dk, bp,
1211 		(xy->flags & XY_WLABEL) != 0) <= 0)
1212 		goto done;
1213 
1214 	/*
1215 	 * Now convert the block number to absolute and put it in
1216 	 * terms of the device's logical block size.
1217 	 */
1218 	blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
1219 	if (DISKPART(bp->b_dev) != RAW_PART)
1220 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
1221 
1222 	bp->b_rawblkno = blkno;
1223 
1224 	/*
1225 	 * now we know we have a valid buf structure that we need to do I/O
1226 	 * on.
1227 	 */
1228 	s = splbio();		/* protect the queues */
1229 
1230 	BUFQ_PUT(xy->xyq, bp);
1231 
1232 	/* start 'em up */
1233 
1234 	xyc_start(xy->parent, NULL);
1235 
1236 	/* done! */
1237 
1238 	splx(s);
1239 	return;
1240 
1241 bad:				/* tells upper layers we have an error */
1242 	bp->b_flags |= B_ERROR;
1243 done:				/* tells upper layers we are done with this
1244 				 * buf */
1245 	bp->b_resid = bp->b_bcount;
1246 	biodone(bp);
1247 }
1248 /*
1249  * end of {b,c}devsw functions
1250  */
1251 
1252 /*
1253  * i n t e r r u p t   f u n c t i o n
1254  *
1255  * xycintr: hardware interrupt.
1256  */
1257 int
1258 xycintr(v)
1259 	void   *v;
1260 
1261 {
1262 	struct xyc_softc *xycsc = v;
1263 
1264 	/* kick the event counter */
1265 
1266 	xycsc->sc_intrcnt.ev_count++;
1267 
1268 	/* remove as many done IOPBs as possible */
1269 
1270 	xyc_remove_iorq(xycsc);
1271 
1272 	/* start any iorq's already waiting */
1273 
1274 	xyc_start(xycsc, NULL);
1275 
1276 	return (1);
1277 }
1278 /*
1279  * end of interrupt function
1280  */
1281 
1282 /*
1283  * i n t e r n a l   f u n c t i o n s
1284  */
1285 
1286 /*
1287  * xyc_rqinit: fill out the fields of an I/O request
1288  */
1289 
1290 inline void
1291 xyc_rqinit(rq, xyc, xy, md, blk, cnt, db, bp)
1292 	struct xy_iorq *rq;
1293 	struct xyc_softc *xyc;
1294 	struct xy_softc *xy;
1295 	int     md;
1296 	u_long  blk;
1297 	int     cnt;
1298 	caddr_t db;
1299 	struct buf *bp;
1300 {
1301 	rq->xyc = xyc;
1302 	rq->xy = xy;
1303 	rq->ttl = XYC_MAXTTL + 10;
1304 	rq->mode = md;
1305 	rq->tries = rq->errno = rq->lasterror = 0;
1306 	rq->blockno = blk;
1307 	rq->sectcnt = cnt;
1308 	rq->dbuf = db;
1309 	rq->buf = bp;
1310 }
1311 
1312 /*
1313  * xyc_rqtopb: load up an IOPB based on an iorq
1314  */
1315 
1316 void
1317 xyc_rqtopb(iorq, iopb, cmd, subfun)
1318 	struct xy_iorq *iorq;
1319 	struct xy_iopb *iopb;
1320 	int     cmd, subfun;
1321 
1322 {
1323 	u_long  block, dp;
1324 
1325 	/* normal IOPB case, standard stuff */
1326 
1327 	/* chain bit handled later */
1328 	iopb->ien = (XY_STATE(iorq->mode) == XY_SUB_POLL) ? 0 : 1;
1329 	iopb->com = cmd;
1330 	iopb->errno = 0;
1331 	iopb->errs = 0;
1332 	iopb->done = 0;
1333 	if (iorq->xy) {
1334 		iopb->unit = iorq->xy->xy_drive;
1335 		iopb->dt = iorq->xy->drive_type;
1336 	} else {
1337 		iopb->unit = 0;
1338 		iopb->dt = 0;
1339 	}
1340 	block = iorq->blockno;
1341 	if (iorq->xy == NULL || block == 0) {
1342 		iopb->sect = iopb->head = iopb->cyl = 0;
1343 	} else {
1344 		iopb->sect = block % iorq->xy->nsect;
1345 		block = block / iorq->xy->nsect;
1346 		iopb->head = block % iorq->xy->nhead;
1347 		block = block / iorq->xy->nhead;
1348 		iopb->cyl = block;
1349 	}
1350 	iopb->scnt = iorq->sectcnt;
1351 	dp = (u_long) iorq->dbuf;
1352 	if (iorq->dbuf == NULL) {
1353 		iopb->dataa = 0;
1354 		iopb->datar = 0;
1355 	} else {
1356 		iopb->dataa = (dp & 0xffff);
1357 		iopb->datar = ((dp & 0xff0000) >> 16);
1358 	}
1359 	iopb->subfn = subfun;
1360 }
1361 
1362 
1363 /*
1364  * xyc_unbusy: wait for the xyc to go unbusy, or timeout.
1365  */
1366 
1367 int
1368 xyc_unbusy(xyc, del)
1369 
1370 struct xyc *xyc;
1371 int del;
1372 
1373 {
1374 	while (del-- > 0) {
1375 		if ((xyc->xyc_csr & XYC_GBSY) == 0)
1376 			break;
1377 		DELAY(1);
1378 	}
1379 	return(del == 0 ? XY_ERR_FAIL : XY_ERR_AOK);
1380 }
1381 
1382 /*
1383  * xyc_cmd: front end for POLL'd and WAIT'd commands.  Returns 0 or error.
1384  * note that NORM requests are handled separately.
1385  */
1386 int
1387 xyc_cmd(xycsc, cmd, subfn, unit, block, scnt, dptr, fullmode)
1388 	struct xyc_softc *xycsc;
1389 	int     cmd, subfn, unit, block, scnt;
1390 	char   *dptr;
1391 	int     fullmode;
1392 
1393 {
1394 	int     submode = XY_STATE(fullmode);
1395 	struct xy_iorq *iorq = xycsc->ciorq;
1396 	struct xy_iopb *iopb = xycsc->ciopb;
1397 
1398 	/*
1399 	 * is someone else using the control iopq wait for it if we can
1400 	 */
1401 start:
1402 	if (submode == XY_SUB_WAIT && XY_STATE(iorq->mode) != XY_SUB_FREE) {
1403 		if (tsleep(iorq, PRIBIO, "xyc_cmd", 0))
1404                                 return(XY_ERR_FAIL);
1405 		goto start;
1406 	}
1407 
1408 	if (XY_STATE(iorq->mode) != XY_SUB_FREE) {
1409 		DELAY(1000000);		/* XY_SUB_POLL: steal the iorq */
1410 		iorq->mode = XY_SUB_FREE;
1411 		printf("%s: stole control iopb\n", xycsc->sc_dev.dv_xname);
1412 	}
1413 
1414 	/* init iorq/iopb */
1415 
1416 	xyc_rqinit(iorq, xycsc,
1417 	    (unit == XYC_NOUNIT) ? NULL : xycsc->sc_drives[unit],
1418 	    fullmode, block, scnt, dptr, NULL);
1419 
1420 	/* load IOPB from iorq */
1421 
1422 	xyc_rqtopb(iorq, iopb, cmd, subfn);
1423 
1424 	/* submit it for processing */
1425 
1426 	xyc_submit_iorq(xycsc, iorq, fullmode);	/* error code will be in iorq */
1427 
1428 	return(XY_ERR_AOK);
1429 }
1430 
1431 /*
1432  * xyc_startbuf
1433  * start a buffer for running
1434  */
1435 
1436 int
1437 xyc_startbuf(xycsc, xysc, bp)
1438 	struct xyc_softc *xycsc;
1439 	struct xy_softc *xysc;
1440 	struct buf *bp;
1441 
1442 {
1443 	int     partno, error;
1444 	struct xy_iorq *iorq;
1445 	struct xy_iopb *iopb;
1446 	u_long  block;
1447 
1448 	iorq = xysc->xyrq;
1449 	iopb = iorq->iopb;
1450 
1451 	/* get buf */
1452 
1453 	if (bp == NULL)
1454 		panic("xyc_startbuf null buf");
1455 
1456 	partno = DISKPART(bp->b_dev);
1457 #ifdef XYC_DEBUG
1458 	printf("xyc_startbuf: %s%c: %s block %d\n", xysc->sc_dev.dv_xname,
1459 	    'a' + partno, (bp->b_flags & B_READ) ? "read" : "write", bp->b_blkno);
1460 	printf("xyc_startbuf: b_bcount %d, b_data 0x%x\n",
1461 	    bp->b_bcount, bp->b_data);
1462 #endif
1463 
1464 	/*
1465 	 * load request.
1466 	 *
1467 	 * note that iorq points to the buffer as mapped into DVMA space,
1468 	 * where as the bp->b_data points to its non-DVMA mapping.
1469 	 */
1470 
1471 	block = bp->b_rawblkno;
1472 
1473 	error = bus_dmamap_load(xycsc->dmatag, iorq->dmamap,
1474 			bp->b_data, bp->b_bcount, 0, BUS_DMA_NOWAIT);
1475 	if (error != 0) {
1476 		printf("%s: warning: cannot load DMA map\n",
1477 			xycsc->sc_dev.dv_xname);
1478 		return (XY_ERR_FAIL);	/* XXX: need some sort of
1479 					 * call-back scheme here? */
1480 	}
1481 
1482 	bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1483 			iorq->dmamap->dm_mapsize, (bp->b_flags & B_READ)
1484 				? BUS_DMASYNC_PREREAD
1485 				: BUS_DMASYNC_PREWRITE);
1486 
1487 	/* init iorq and load iopb from it */
1488 	xyc_rqinit(iorq, xycsc, xysc, XY_SUB_NORM | XY_MODE_VERBO, block,
1489 		   bp->b_bcount / XYFM_BPS,
1490 		   (caddr_t)(u_long)iorq->dmamap->dm_segs[0].ds_addr,
1491 		   bp);
1492 
1493 	xyc_rqtopb(iorq, iopb, (bp->b_flags & B_READ) ? XYCMD_RD : XYCMD_WR, 0);
1494 
1495 	/* Instrumentation. */
1496 	disk_busy(&xysc->sc_dk);
1497 
1498 	return (XY_ERR_AOK);
1499 }
1500 
1501 
1502 /*
1503  * xyc_submit_iorq: submit an iorq for processing.  returns XY_ERR_AOK
1504  * if ok.  if it fail returns an error code.  type is XY_SUB_*.
1505  *
1506  * note: caller frees iorq in all cases except NORM
1507  *
1508  * return value:
1509  *   NORM: XY_AOK (req pending), XY_FAIL (couldn't submit request)
1510  *   WAIT: XY_AOK (success), <error-code> (failed)
1511  *   POLL: <same as WAIT>
1512  *   NOQ : <same as NORM>
1513  *
1514  * there are three sources for i/o requests:
1515  * [1] xystrategy: normal block I/O, using "struct buf" system.
1516  * [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
1517  * [3] open/ioctl: these are I/O requests done in the context of a process,
1518  *                 and the process should block until they are done.
1519  *
1520  * software state is stored in the iorq structure.  each iorq has an
1521  * iopb structure.  the hardware understands the iopb structure.
1522  * every command must go through an iopb.  a 450 handles one iopb at a
1523  * time, where as a 451 can take them in chains.  [the 450 claims it
1524  * can handle chains, but is appears to be buggy...]   iopb are allocated
1525  * in DVMA space at boot up time.  each disk gets one iopb, and the
1526  * controller gets one (for POLL and WAIT commands).  what happens if
1527  * the iopb is busy?  for i/o type [1], the buffers are queued at the
1528  * "buff" layer and * picked up later by the interrupt routine.  for case
1529  * [2] we can only be blocked if there is a WAIT type I/O request being
1530  * run.   since this can only happen when we are crashing, we wait a sec
1531  * and then steal the IOPB.  for case [3] the process can sleep
1532  * on the iorq free list until some iopbs are available.
1533  */
1534 
1535 
1536 int
1537 xyc_submit_iorq(xycsc, iorq, type)
1538 	struct xyc_softc *xycsc;
1539 	struct xy_iorq *iorq;
1540 	int     type;
1541 
1542 {
1543 	struct xy_iopb *dmaiopb;
1544 
1545 #ifdef XYC_DEBUG
1546 	printf("xyc_submit_iorq(%s, addr=0x%x, type=%d)\n",
1547 		xycsc->sc_dev.dv_xname, iorq, type);
1548 #endif
1549 
1550 	/* first check and see if controller is busy */
1551 	if ((xycsc->xyc->xyc_csr & XYC_GBSY) != 0) {
1552 #ifdef XYC_DEBUG
1553 		printf("xyc_submit_iorq: XYC not ready (BUSY)\n");
1554 #endif
1555 		if (type == XY_SUB_NOQ)
1556 			return (XY_ERR_FAIL);	/* failed */
1557 		switch (type) {
1558 		case XY_SUB_NORM:
1559 			return XY_ERR_AOK;	/* success */
1560 		case XY_SUB_WAIT:
1561 			while (iorq->iopb->done == 0) {
1562 				(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1563 			}
1564 			return (iorq->errno);
1565 		case XY_SUB_POLL:		/* steal controller */
1566 			(void)xycsc->xyc->xyc_rsetup; /* RESET */
1567 			if (xyc_unbusy(xycsc->xyc,XYC_RESETUSEC) == XY_ERR_FAIL)
1568 				panic("xyc_submit_iorq: stuck xyc");
1569 			printf("%s: stole controller\n",
1570 				xycsc->sc_dev.dv_xname);
1571 			break;
1572 		default:
1573 			panic("xyc_submit_iorq adding");
1574 		}
1575 	}
1576 
1577 	dmaiopb = xyc_chain(xycsc, iorq);	 /* build chain */
1578 	if (dmaiopb == NULL) { /* nothing doing? */
1579 		if (type == XY_SUB_NORM || type == XY_SUB_NOQ)
1580 			return(XY_ERR_AOK);
1581 		panic("xyc_submit_iorq: xyc_chain failed!");
1582 	}
1583 
1584 	XYC_GO(xycsc->xyc, dmaiopb);
1585 
1586 	/* command now running, wrap it up */
1587 	switch (type) {
1588 	case XY_SUB_NORM:
1589 	case XY_SUB_NOQ:
1590 		return (XY_ERR_AOK);	/* success */
1591 	case XY_SUB_WAIT:
1592 		while (iorq->iopb->done == 0) {
1593 			(void) tsleep(iorq, PRIBIO, "xyciorq", 0);
1594 		}
1595 		return (iorq->errno);
1596 	case XY_SUB_POLL:
1597 		return (xyc_piodriver(xycsc, iorq));
1598 	default:
1599 		panic("xyc_submit_iorq wrap up");
1600 	}
1601 	panic("xyc_submit_iorq");
1602 	return 0;	/* not reached */
1603 }
1604 
1605 
1606 /*
1607  * xyc_chain: build a chain.  return dvma address of first element in
1608  * the chain.   iorq != NULL: means we only want that item on the chain.
1609  */
1610 
1611 struct xy_iopb *
1612 xyc_chain(xycsc, iorq)
1613 	struct xyc_softc *xycsc;
1614 	struct xy_iorq *iorq;
1615 
1616 {
1617 	int togo, chain, hand;
1618 
1619 	bzero(xycsc->xy_chain, sizeof(xycsc->xy_chain));
1620 
1621 	/*
1622 	 * promote control IOPB to the top
1623 	 */
1624 	if (iorq == NULL) {
1625 		if ((XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_POLL ||
1626 		     XY_STATE(xycsc->reqs[XYC_CTLIOPB].mode) == XY_SUB_WAIT) &&
1627 		     xycsc->iopbase[XYC_CTLIOPB].done == 0)
1628 			iorq = &xycsc->reqs[XYC_CTLIOPB];
1629 	}
1630 
1631 	/*
1632 	 * special case: if iorq != NULL then we have a POLL or WAIT request.
1633 	 * we let these take priority and do them first.
1634 	 */
1635 	if (iorq) {
1636 		xycsc->xy_chain[0] = iorq;
1637 		iorq->iopb->chen = 0;
1638 		return(iorq->dmaiopb);
1639 	}
1640 
1641 	/*
1642 	 * NORM case: do round robin and maybe chain (if allowed and possible)
1643 	 */
1644 	chain = 0;
1645 	hand = xycsc->xy_hand;
1646 	xycsc->xy_hand = (xycsc->xy_hand + 1) % XYC_MAXIOPB;
1647 
1648 	for (togo = XYC_MAXIOPB; togo > 0;
1649 	     togo--, hand = (hand + 1) % XYC_MAXIOPB) {
1650 		struct xy_iopb *iopb, *prev_iopb, *dmaiopb;
1651 
1652 		if (XY_STATE(xycsc->reqs[hand].mode) != XY_SUB_NORM ||
1653 		    xycsc->iopbase[hand].done)
1654 			continue;   /* not ready-for-i/o */
1655 
1656 		xycsc->xy_chain[chain] = &xycsc->reqs[hand];
1657 		iopb = xycsc->xy_chain[chain]->iopb;
1658 		iopb->chen = 0;
1659 		if (chain != 0) {
1660 			/* adding a link to a chain */
1661 			prev_iopb = xycsc->xy_chain[chain-1]->iopb;
1662 			prev_iopb->chen = 1;
1663 			dmaiopb = xycsc->xy_chain[chain]->dmaiopb;
1664 			prev_iopb->nxtiopb = ((u_long)dmaiopb) & 0xffff;
1665 		} else {
1666 			/* head of chain */
1667 			iorq = xycsc->xy_chain[chain];
1668 		}
1669 		chain++;
1670 
1671 		/* quit if chaining dis-allowed */
1672 		if (xycsc->no_ols)
1673 			break;
1674 	}
1675 
1676 	return(iorq ? iorq->dmaiopb : NULL);
1677 }
1678 
1679 /*
1680  * xyc_piodriver
1681  *
1682  * programmed i/o driver.   this function takes over the computer
1683  * and drains off the polled i/o request.   it returns the status of the iorq
1684  * the caller is interesting in.
1685  */
1686 int
1687 xyc_piodriver(xycsc, iorq)
1688 	struct xyc_softc *xycsc;
1689 	struct xy_iorq  *iorq;
1690 
1691 {
1692 	int     nreset = 0;
1693 	int     retval = 0;
1694 	u_long  res;
1695 #ifdef XYC_DEBUG
1696 	printf("xyc_piodriver(%s, 0x%x)\n", xycsc->sc_dev.dv_xname, iorq);
1697 #endif
1698 
1699 	while (iorq->iopb->done == 0) {
1700 
1701 		res = xyc_unbusy(xycsc->xyc, XYC_MAXTIME);
1702 
1703 		/* we expect some progress soon */
1704 		if (res == XY_ERR_FAIL && nreset >= 2) {
1705 			xyc_reset(xycsc, 0, XY_RSET_ALL, XY_ERR_FAIL, 0);
1706 #ifdef XYC_DEBUG
1707 			printf("xyc_piodriver: timeout\n");
1708 #endif
1709 			return (XY_ERR_FAIL);
1710 		}
1711 		if (res == XY_ERR_FAIL) {
1712 			if (xyc_reset(xycsc, 0,
1713 				      (nreset++ == 0) ? XY_RSET_NONE : iorq,
1714 				      XY_ERR_FAIL,
1715 				      0) == XY_ERR_FAIL)
1716 				return (XY_ERR_FAIL);	/* flushes all but POLL
1717 							 * requests, resets */
1718 			continue;
1719 		}
1720 
1721 		xyc_remove_iorq(xycsc);	 /* may resubmit request */
1722 
1723 		if (iorq->iopb->done == 0)
1724 			xyc_start(xycsc, iorq);
1725 	}
1726 
1727 	/* get return value */
1728 
1729 	retval = iorq->errno;
1730 
1731 #ifdef XYC_DEBUG
1732 	printf("xyc_piodriver: done, retval = 0x%x (%s)\n",
1733 	    iorq->errno, xyc_e2str(iorq->errno));
1734 #endif
1735 
1736 	/* start up any bufs that have queued */
1737 
1738 	xyc_start(xycsc, NULL);
1739 
1740 	return (retval);
1741 }
1742 
1743 /*
1744  * xyc_xyreset: reset one drive.   NOTE: assumes xyc was just reset.
1745  * we steal iopb[XYC_CTLIOPB] for this, but we put it back when we are done.
1746  */
1747 void
1748 xyc_xyreset(xycsc, xysc)
1749 	struct xyc_softc *xycsc;
1750 	struct xy_softc *xysc;
1751 
1752 {
1753 	struct xy_iopb tmpiopb;
1754 	struct xy_iopb *iopb;
1755 	int     del;
1756 
1757 	iopb = xycsc->ciopb;
1758 
1759 	/* Save contents */
1760 	bcopy(iopb, &tmpiopb, sizeof(struct xy_iopb));
1761 
1762 	iopb->chen = iopb->done = iopb->errs = 0;
1763 	iopb->ien = 0;
1764 	iopb->com = XYCMD_RST;
1765 	iopb->unit = xysc->xy_drive;
1766 
1767 	XYC_GO(xycsc->xyc, xycsc->ciorq->dmaiopb);
1768 
1769 	del = XYC_RESETUSEC;
1770 	while (del > 0) {
1771 		if ((xycsc->xyc->xyc_csr & XYC_GBSY) == 0)
1772 			break;
1773 		DELAY(1);
1774 		del--;
1775 	}
1776 
1777 	if (del <= 0 || iopb->errs) {
1778 		printf("%s: off-line: %s\n", xycsc->sc_dev.dv_xname,
1779 		    xyc_e2str(iopb->errno));
1780 		del = xycsc->xyc->xyc_rsetup;
1781 		if (xyc_unbusy(xycsc->xyc, XYC_RESETUSEC) == XY_ERR_FAIL)
1782 			panic("xyc_reset");
1783 	} else {
1784 		xycsc->xyc->xyc_csr = XYC_IPND;	/* clear IPND */
1785 	}
1786 
1787 	/* Restore contents */
1788 	bcopy(&tmpiopb, iopb, sizeof(struct xy_iopb));
1789 }
1790 
1791 
1792 /*
1793  * xyc_reset: reset everything: requests are marked as errors except
1794  * a polled request (which is resubmitted)
1795  */
1796 int
1797 xyc_reset(xycsc, quiet, blastmode, error, xysc)
1798 	struct xyc_softc *xycsc;
1799 	int     quiet, error;
1800 	struct xy_iorq *blastmode;
1801 	struct xy_softc *xysc;
1802 
1803 {
1804 	int     del = 0, lcv, retval = XY_ERR_AOK;
1805 
1806 	/* soft reset hardware */
1807 
1808 	if (!quiet)
1809 		printf("%s: soft reset\n", xycsc->sc_dev.dv_xname);
1810 	del = xycsc->xyc->xyc_rsetup;
1811 	del = xyc_unbusy(xycsc->xyc, XYC_RESETUSEC);
1812 	if (del == XY_ERR_FAIL) {
1813 		blastmode = XY_RSET_ALL;	/* dead, flush all requests */
1814 		retval = XY_ERR_FAIL;
1815 	}
1816 	if (xysc)
1817 		xyc_xyreset(xycsc, xysc);
1818 
1819 	/* fix queues based on "blast-mode" */
1820 
1821 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
1822 		register struct xy_iorq *iorq = &xycsc->reqs[lcv];
1823 
1824 		if (XY_STATE(iorq->mode) != XY_SUB_POLL &&
1825 		    XY_STATE(iorq->mode) != XY_SUB_WAIT &&
1826 		    XY_STATE(iorq->mode) != XY_SUB_NORM)
1827 			/* is it active? */
1828 			continue;
1829 
1830 		if (blastmode == XY_RSET_ALL ||
1831 				blastmode != iorq) {
1832 			/* failed */
1833 			iorq->errno = error;
1834 			xycsc->iopbase[lcv].done = xycsc->iopbase[lcv].errs = 1;
1835 			switch (XY_STATE(iorq->mode)) {
1836 			case XY_SUB_NORM:
1837 			    iorq->buf->b_error = EIO;
1838 			    iorq->buf->b_flags |= B_ERROR;
1839 			    iorq->buf->b_resid = iorq->sectcnt * XYFM_BPS;
1840 
1841 			    bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
1842 					iorq->dmamap->dm_mapsize,
1843 					(iorq->buf->b_flags & B_READ)
1844 						? BUS_DMASYNC_POSTREAD
1845 						: BUS_DMASYNC_POSTWRITE);
1846 
1847 			    bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
1848 
1849 			    (void)BUFQ_GET(iorq->xy->xyq);
1850 			    disk_unbusy(&xycsc->reqs[lcv].xy->sc_dk,
1851 				(xycsc->reqs[lcv].buf->b_bcount -
1852 				xycsc->reqs[lcv].buf->b_resid),
1853 				(xycsc->reqs[lcv].buf->b_flags & B_READ));
1854 			    biodone(iorq->buf);
1855 			    iorq->mode = XY_SUB_FREE;
1856 			    break;
1857 			case XY_SUB_WAIT:
1858 			    wakeup(iorq);
1859 			case XY_SUB_POLL:
1860 			    iorq->mode =
1861 				XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
1862 			    break;
1863 			}
1864 
1865 		} else {
1866 
1867 			/* resubmit, no need to do anything here */
1868 		}
1869 	}
1870 
1871 	/*
1872 	 * now, if stuff is waiting, start it.
1873 	 * since we just reset it should go
1874 	 */
1875 	xyc_start(xycsc, NULL);
1876 
1877 	return (retval);
1878 }
1879 
1880 /*
1881  * xyc_start: start waiting buffers
1882  */
1883 
1884 void
1885 xyc_start(xycsc, iorq)
1886 	struct xyc_softc *xycsc;
1887 	struct xy_iorq *iorq;
1888 
1889 {
1890 	int lcv;
1891 	struct xy_softc *xy;
1892 
1893 	if (iorq == NULL) {
1894 		for (lcv = 0; lcv < XYC_MAXDEV ; lcv++) {
1895 			if ((xy = xycsc->sc_drives[lcv]) == NULL) continue;
1896 			if (BUFQ_PEEK(xy->xyq) == NULL) continue;
1897 			if (xy->xyrq->mode != XY_SUB_FREE) continue;
1898 			xyc_startbuf(xycsc, xy, BUFQ_PEEK(xy->xyq));
1899 		}
1900 	}
1901 	xyc_submit_iorq(xycsc, iorq, XY_SUB_NOQ);
1902 }
1903 
1904 /*
1905  * xyc_remove_iorq: remove "done" IOPB's.
1906  */
1907 
1908 int
1909 xyc_remove_iorq(xycsc)
1910 	struct xyc_softc *xycsc;
1911 
1912 {
1913 	int     errno, rq, comm, errs;
1914 	struct xyc *xyc = xycsc->xyc;
1915 	u_long  addr;
1916 	struct xy_iopb *iopb;
1917 	struct xy_iorq *iorq;
1918 	struct buf *bp;
1919 
1920 	if (xyc->xyc_csr & XYC_DERR) {
1921 		/*
1922 		 * DOUBLE ERROR: should never happen under normal use. This
1923 		 * error is so bad, you can't even tell which IOPB is bad, so
1924 		 * we dump them all.
1925 		 */
1926 		errno = XY_ERR_DERR;
1927 		printf("%s: DOUBLE ERROR!\n", xycsc->sc_dev.dv_xname);
1928 		if (xyc_reset(xycsc, 0, XY_RSET_ALL, errno, 0) != XY_ERR_AOK) {
1929 			printf("%s: soft reset failed!\n",
1930 				xycsc->sc_dev.dv_xname);
1931 			panic("xyc_remove_iorq: controller DEAD");
1932 		}
1933 		return (XY_ERR_AOK);
1934 	}
1935 
1936 	/*
1937 	 * get iopb that is done, loop down the chain
1938 	 */
1939 
1940 	if (xyc->xyc_csr & XYC_ERR) {
1941 		xyc->xyc_csr = XYC_ERR; /* clear error condition */
1942 	}
1943 	if (xyc->xyc_csr & XYC_IPND) {
1944 		xyc->xyc_csr = XYC_IPND; /* clear interrupt */
1945 	}
1946 
1947 	for (rq = 0; rq < XYC_MAXIOPB; rq++) {
1948 		iorq = xycsc->xy_chain[rq];
1949 		if (iorq == NULL) break; /* done ! */
1950 		if (iorq->mode == 0 || XY_STATE(iorq->mode) == XY_SUB_DONE)
1951 			continue;	/* free, or done */
1952 		iopb = iorq->iopb;
1953 		if (iopb->done == 0)
1954 			continue;	/* not done yet */
1955 
1956 		comm = iopb->com;
1957 		errs = iopb->errs;
1958 
1959 		if (errs)
1960 			iorq->errno = iopb->errno;
1961 		else
1962 			iorq->errno = 0;
1963 
1964 		/* handle non-fatal errors */
1965 
1966 		if (errs &&
1967 		    xyc_error(xycsc, iorq, iopb, comm) == XY_ERR_AOK)
1968 			continue;	/* AOK: we resubmitted it */
1969 
1970 
1971 		/* this iorq is now done (hasn't been restarted or anything) */
1972 
1973 		if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
1974 			xyc_perror(iorq, iopb, 0);
1975 
1976 		/* now, if read/write check to make sure we got all the data
1977 		 * we needed. (this may not be the case if we got an error in
1978 		 * the middle of a multisector request).   */
1979 
1980 		if ((iorq->mode & XY_MODE_B144) != 0 && errs == 0 &&
1981 		    (comm == XYCMD_RD || comm == XYCMD_WR)) {
1982 			/* we just successfully processed a bad144 sector
1983 			 * note: if we are in bad 144 mode, the pointers have
1984 			 * been advanced already (see above) and are pointing
1985 			 * at the bad144 sector.   to exit bad144 mode, we
1986 			 * must advance the pointers 1 sector and issue a new
1987 			 * request if there are still sectors left to process
1988 			 *
1989 			 */
1990 			XYC_ADVANCE(iorq, 1);	/* advance 1 sector */
1991 
1992 			/* exit b144 mode */
1993 			iorq->mode = iorq->mode & (~XY_MODE_B144);
1994 
1995 			if (iorq->sectcnt) {	/* more to go! */
1996 				iorq->lasterror = iorq->errno = iopb->errno = 0;
1997 				iopb->errs = iopb->done = 0;
1998 				iorq->tries = 0;
1999 				iopb->scnt = iorq->sectcnt;
2000 				iopb->cyl = iorq->blockno /
2001 						iorq->xy->sectpercyl;
2002 				iopb->head =
2003 					(iorq->blockno / iorq->xy->nhead) %
2004 						iorq->xy->nhead;
2005 				iopb->sect = iorq->blockno % XYFM_BPS;
2006 				addr = (u_long) iorq->dbuf;
2007 				iopb->dataa = (addr & 0xffff);
2008 				iopb->datar = ((addr & 0xff0000) >> 16);
2009 				/* will resubit at end */
2010 				continue;
2011 			}
2012 		}
2013 		/* final cleanup, totally done with this request */
2014 
2015 		switch (XY_STATE(iorq->mode)) {
2016 		case XY_SUB_NORM:
2017 			bp = iorq->buf;
2018 			if (errs) {
2019 				bp->b_error = EIO;
2020 				bp->b_flags |= B_ERROR;
2021 				bp->b_resid = iorq->sectcnt * XYFM_BPS;
2022 			} else {
2023 				bp->b_resid = 0;	/* done */
2024 			}
2025 			bus_dmamap_sync(xycsc->dmatag, iorq->dmamap, 0,
2026 					iorq->dmamap->dm_mapsize,
2027 					(iorq->buf->b_flags & B_READ)
2028 						? BUS_DMASYNC_POSTREAD
2029 						: BUS_DMASYNC_POSTWRITE);
2030 
2031 			bus_dmamap_unload(xycsc->dmatag, iorq->dmamap);
2032 
2033 			(void)BUFQ_GET(iorq->xy->xyq);
2034 			disk_unbusy(&iorq->xy->sc_dk,
2035 			    (bp->b_bcount - bp->b_resid),
2036 			    (bp->b_flags & B_READ));
2037 			iorq->mode = XY_SUB_FREE;
2038 			biodone(bp);
2039 			break;
2040 		case XY_SUB_WAIT:
2041 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2042 			wakeup(iorq);
2043 			break;
2044 		case XY_SUB_POLL:
2045 			iorq->mode = XY_NEWSTATE(iorq->mode, XY_SUB_DONE);
2046 			break;
2047 		}
2048 	}
2049 
2050 	return (XY_ERR_AOK);
2051 }
2052 
2053 /*
2054  * xyc_perror: print error.
2055  * - if still_trying is true: we got an error, retried and got a
2056  *   different error.  in that case lasterror is the old error,
2057  *   and errno is the new one.
2058  * - if still_trying is not true, then if we ever had an error it
2059  *   is in lasterror. also, if iorq->errno == 0, then we recovered
2060  *   from that error (otherwise iorq->errno == iorq->lasterror).
2061  */
2062 void
2063 xyc_perror(iorq, iopb, still_trying)
2064 	struct xy_iorq *iorq;
2065 	struct xy_iopb *iopb;
2066 	int     still_trying;
2067 
2068 {
2069 
2070 	int     error = iorq->lasterror;
2071 
2072 	printf("%s", (iorq->xy) ? iorq->xy->sc_dev.dv_xname
2073 	    : iorq->xyc->sc_dev.dv_xname);
2074 	if (iorq->buf)
2075 		printf("%c: ", 'a' + DISKPART(iorq->buf->b_dev));
2076 	if (iopb->com == XYCMD_RD || iopb->com == XYCMD_WR)
2077 		printf("%s %d/%d/%d: ",
2078 			(iopb->com == XYCMD_RD) ? "read" : "write",
2079 			iopb->cyl, iopb->head, iopb->sect);
2080 	printf("%s", xyc_e2str(error));
2081 
2082 	if (still_trying)
2083 		printf(" [still trying, new error=%s]", xyc_e2str(iorq->errno));
2084 	else
2085 		if (iorq->errno == 0)
2086 			printf(" [recovered in %d tries]", iorq->tries);
2087 
2088 	printf("\n");
2089 }
2090 
2091 /*
2092  * xyc_error: non-fatal error encountered... recover.
2093  * return AOK if resubmitted, return FAIL if this iopb is done
2094  */
2095 int
2096 xyc_error(xycsc, iorq, iopb, comm)
2097 	struct xyc_softc *xycsc;
2098 	struct xy_iorq *iorq;
2099 	struct xy_iopb *iopb;
2100 	int     comm;
2101 
2102 {
2103 	int     errno = iorq->errno;
2104 	int     erract = xyc_entoact(errno);
2105 	int     oldmode, advance;
2106 #ifdef __sparc__
2107 	int i;
2108 #endif
2109 
2110 	if (erract == XY_ERA_RSET) {	/* some errors require a reset */
2111 		oldmode = iorq->mode;
2112 		iorq->mode = XY_SUB_DONE | (~XY_SUB_MASK & oldmode);
2113 		/* make xyc_start ignore us */
2114 		xyc_reset(xycsc, 1, XY_RSET_NONE, errno, iorq->xy);
2115 		iorq->mode = oldmode;
2116 	}
2117 	/* check for read/write to a sector in bad144 table if bad: redirect
2118 	 * request to bad144 area */
2119 
2120 	if ((comm == XYCMD_RD || comm == XYCMD_WR) &&
2121 	    (iorq->mode & XY_MODE_B144) == 0) {
2122 		advance = iorq->sectcnt - iopb->scnt;
2123 		XYC_ADVANCE(iorq, advance);
2124 #ifdef __sparc__
2125 		if ((i = isbad(&iorq->xy->dkb, iorq->blockno / iorq->xy->sectpercyl,
2126 			    (iorq->blockno / iorq->xy->nsect) % iorq->xy->nhead,
2127 			    iorq->blockno % iorq->xy->nsect)) != -1) {
2128 			iorq->mode |= XY_MODE_B144;	/* enter bad144 mode &
2129 							 * redirect */
2130 			iopb->errno = iopb->done = iopb->errs = 0;
2131 			iopb->scnt = 1;
2132 			iopb->cyl = (iorq->xy->ncyl + iorq->xy->acyl) - 2;
2133 			/* second to last acyl */
2134 			i = iorq->xy->sectpercyl - 1 - i;	/* follow bad144
2135 								 * standard */
2136 			iopb->head = i / iorq->xy->nhead;
2137 			iopb->sect = i % iorq->xy->nhead;
2138 			/* will resubmit when we come out of remove_iorq */
2139 			return (XY_ERR_AOK);	/* recovered! */
2140 		}
2141 #endif
2142 	}
2143 
2144 	/*
2145 	 * it isn't a bad144 sector, must be real error! see if we can retry
2146 	 * it?
2147 	 */
2148 	if ((iorq->mode & XY_MODE_VERBO) && iorq->lasterror)
2149 		xyc_perror(iorq, iopb, 1);	/* inform of error state
2150 						 * change */
2151 	iorq->lasterror = errno;
2152 
2153 	if ((erract == XY_ERA_RSET || erract == XY_ERA_HARD)
2154 	    && iorq->tries < XYC_MAXTRIES) {	/* retry? */
2155 		iorq->tries++;
2156 		iorq->errno = iopb->errno = iopb->done = iopb->errs = 0;
2157 		/* will resubmit at end of remove_iorq */
2158 		return (XY_ERR_AOK);	/* recovered! */
2159 	}
2160 
2161 	/* failed to recover from this error */
2162 	return (XY_ERR_FAIL);
2163 }
2164 
2165 /*
2166  * xyc_tick: make sure xy is still alive and ticking (err, kicking).
2167  */
2168 void
2169 xyc_tick(arg)
2170 	void   *arg;
2171 
2172 {
2173 	struct xyc_softc *xycsc = arg;
2174 	int     lcv, s, reset = 0;
2175 
2176 	/* reduce ttl for each request if one goes to zero, reset xyc */
2177 	s = splbio();
2178 	for (lcv = 0; lcv < XYC_MAXIOPB; lcv++) {
2179 		if (xycsc->reqs[lcv].mode == 0 ||
2180 		    XY_STATE(xycsc->reqs[lcv].mode) == XY_SUB_DONE)
2181 			continue;
2182 		xycsc->reqs[lcv].ttl--;
2183 		if (xycsc->reqs[lcv].ttl == 0)
2184 			reset = 1;
2185 	}
2186 	if (reset) {
2187 		printf("%s: watchdog timeout\n", xycsc->sc_dev.dv_xname);
2188 		xyc_reset(xycsc, 0, XY_RSET_NONE, XY_ERR_FAIL, NULL);
2189 	}
2190 	splx(s);
2191 
2192 	/* until next time */
2193 
2194 	callout_reset(&xycsc->sc_tick_ch, XYC_TICKCNT, xyc_tick, xycsc);
2195 }
2196 
2197 /*
2198  * xyc_ioctlcmd: this function provides a user level interface to the
2199  * controller via ioctl.   this allows "format" programs to be written
2200  * in user code, and is also useful for some debugging.   we return
2201  * an error code.   called at user priority.
2202  *
2203  * XXX missing a few commands (see the 7053 driver for ideas)
2204  */
2205 int
2206 xyc_ioctlcmd(xy, dev, xio)
2207 	struct xy_softc *xy;
2208 	dev_t   dev;
2209 	struct xd_iocmd *xio;
2210 
2211 {
2212 	int     s, rqno, dummy = 0;
2213 	caddr_t dvmabuf = NULL, buf = NULL;
2214 	struct xyc_softc *xycsc;
2215 	int			rseg, error;
2216 	bus_dma_segment_t	seg;
2217 
2218 	/* check sanity of requested command */
2219 
2220 	switch (xio->cmd) {
2221 
2222 	case XYCMD_NOP:	/* no op: everything should be zero */
2223 		if (xio->subfn || xio->dptr || xio->dlen ||
2224 		    xio->block || xio->sectcnt)
2225 			return (EINVAL);
2226 		break;
2227 
2228 	case XYCMD_RD:		/* read / write sectors (up to XD_IOCMD_MAXS) */
2229 	case XYCMD_WR:
2230 		if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
2231 		    xio->sectcnt * XYFM_BPS != xio->dlen || xio->dptr == NULL)
2232 			return (EINVAL);
2233 		break;
2234 
2235 	case XYCMD_SK:		/* seek: doesn't seem useful to export this */
2236 		return (EINVAL);
2237 
2238 		break;
2239 
2240 	default:
2241 		return (EINVAL);/* ??? */
2242 	}
2243 
2244 	xycsc = xy->parent;
2245 
2246 	/* create DVMA buffer for request if needed */
2247 	if (xio->dlen) {
2248 		bus_addr_t busbuf;
2249 
2250 		if ((error = xy_dmamem_alloc(xycsc->dmatag, xycsc->auxmap,
2251 					     &seg, &rseg,
2252 					     xio->dlen, &buf,
2253 					     &busbuf)) != 0) {
2254 			return (error);
2255 		}
2256 		dvmabuf = (caddr_t)(u_long)BUS_ADDR_PADDR(busbuf);
2257 
2258 		if (xio->cmd == XYCMD_WR) {
2259 			if ((error = copyin(xio->dptr, buf, xio->dlen)) != 0) {
2260 				bus_dmamem_unmap(xycsc->dmatag, buf, xio->dlen);
2261 				bus_dmamem_free(xycsc->dmatag, &seg, rseg);
2262 				return (error);
2263 			}
2264 		}
2265 	}
2266 	/* do it! */
2267 
2268 	error = 0;
2269 	s = splbio();
2270 	rqno = xyc_cmd(xycsc, xio->cmd, xio->subfn, xy->xy_drive, xio->block,
2271 	    xio->sectcnt, dvmabuf, XY_SUB_WAIT);
2272 	if (rqno == XY_ERR_FAIL) {
2273 		error = EIO;
2274 		goto done;
2275 	}
2276 	xio->errno = xycsc->ciorq->errno;
2277 	xio->tries = xycsc->ciorq->tries;
2278 	XYC_DONE(xycsc, dummy);
2279 
2280 	if (xio->cmd == XYCMD_RD)
2281 		error = copyout(buf, xio->dptr, xio->dlen);
2282 
2283 done:
2284 	splx(s);
2285 	if (dvmabuf) {
2286 		xy_dmamem_free(xycsc->dmatag, xycsc->auxmap, &seg, rseg,
2287 				xio->dlen, buf);
2288 	}
2289 	return (error);
2290 }
2291 
2292 /*
2293  * xyc_e2str: convert error code number into an error string
2294  */
2295 const char *
2296 xyc_e2str(no)
2297 	int     no;
2298 {
2299 	switch (no) {
2300 	case XY_ERR_FAIL:
2301 		return ("Software fatal error");
2302 	case XY_ERR_DERR:
2303 		return ("DOUBLE ERROR");
2304 	case XY_ERR_AOK:
2305 		return ("Successful completion");
2306 	case XY_ERR_IPEN:
2307 		return("Interrupt pending");
2308 	case XY_ERR_BCFL:
2309 		return("Busy conflict");
2310 	case XY_ERR_TIMO:
2311 		return("Operation timeout");
2312 	case XY_ERR_NHDR:
2313 		return("Header not found");
2314 	case XY_ERR_HARD:
2315 		return("Hard ECC error");
2316 	case XY_ERR_ICYL:
2317 		return("Illegal cylinder address");
2318 	case XY_ERR_ISEC:
2319 		return("Illegal sector address");
2320 	case XY_ERR_SMAL:
2321 		return("Last sector too small");
2322 	case XY_ERR_SACK:
2323 		return("Slave ACK error (non-existent memory)");
2324 	case XY_ERR_CHER:
2325 		return("Cylinder and head/header error");
2326 	case XY_ERR_SRTR:
2327 		return("Auto-seek retry successful");
2328 	case XY_ERR_WPRO:
2329 		return("Write-protect error");
2330 	case XY_ERR_UIMP:
2331 		return("Unimplemented command");
2332 	case XY_ERR_DNRY:
2333 		return("Drive not ready");
2334 	case XY_ERR_SZER:
2335 		return("Sector count zero");
2336 	case XY_ERR_DFLT:
2337 		return("Drive faulted");
2338 	case XY_ERR_ISSZ:
2339 		return("Illegal sector size");
2340 	case XY_ERR_SLTA:
2341 		return("Self test A");
2342 	case XY_ERR_SLTB:
2343 		return("Self test B");
2344 	case XY_ERR_SLTC:
2345 		return("Self test C");
2346 	case XY_ERR_SOFT:
2347 		return("Soft ECC error");
2348 	case XY_ERR_SFOK:
2349 		return("Soft ECC error recovered");
2350 	case XY_ERR_IHED:
2351 		return("Illegal head");
2352 	case XY_ERR_DSEQ:
2353 		return("Disk sequencer error");
2354 	case XY_ERR_SEEK:
2355 		return("Seek error");
2356 	default:
2357 		return ("Unknown error");
2358 	}
2359 }
2360 
2361 int
2362 xyc_entoact(errno)
2363 
2364 int errno;
2365 
2366 {
2367   switch (errno) {
2368     case XY_ERR_FAIL:	case XY_ERR_DERR:	case XY_ERR_IPEN:
2369     case XY_ERR_BCFL:	case XY_ERR_ICYL:	case XY_ERR_ISEC:
2370     case XY_ERR_UIMP:	case XY_ERR_SZER:	case XY_ERR_ISSZ:
2371     case XY_ERR_SLTA:	case XY_ERR_SLTB:	case XY_ERR_SLTC:
2372     case XY_ERR_IHED:	case XY_ERR_SACK:	case XY_ERR_SMAL:
2373 
2374 	return(XY_ERA_PROG); /* program error ! */
2375 
2376     case XY_ERR_TIMO:	case XY_ERR_NHDR:	case XY_ERR_HARD:
2377     case XY_ERR_DNRY:	case XY_ERR_CHER:	case XY_ERR_SEEK:
2378     case XY_ERR_SOFT:
2379 
2380 	return(XY_ERA_HARD); /* hard error, retry */
2381 
2382     case XY_ERR_DFLT:	case XY_ERR_DSEQ:
2383 
2384 	return(XY_ERA_RSET); /* hard error reset */
2385 
2386     case XY_ERR_SRTR:	case XY_ERR_SFOK:	case XY_ERR_AOK:
2387 
2388 	return(XY_ERA_SOFT); /* an FYI error */
2389 
2390     case XY_ERR_WPRO:
2391 
2392 	return(XY_ERA_WPRO); /* write protect */
2393   }
2394 
2395   return(XY_ERA_PROG); /* ??? */
2396 }
2397