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