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