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