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