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