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