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