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