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