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