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