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