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