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