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