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