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