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