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