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