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