wd.c revision 1.68 1 /*
2 * Copyright (c) 1994 Charles Hannum.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: @(#)wd.c 7.2 (Berkeley) 5/9/91
38 * $Id: wd.c,v 1.68 1994/03/10 22:30:09 mycroft Exp $
39 */
40
41 #define INSTRUMENT /* instrumentation stuff by Brad Parker */
42
43 #include "wd.h"
44 #if NWDC > 0
45
46 #include <sys/param.h>
47 #include <sys/dkbad.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/conf.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/ioctl.h>
54 #include <sys/disklabel.h>
55 #include <sys/buf.h>
56 #include <sys/uio.h>
57 #include <sys/malloc.h>
58 #include <sys/device.h>
59 #include <sys/syslog.h>
60 #ifdef INSTRUMENT
61 #include <sys/dkstat.h>
62 #endif
63
64 #include <vm/vm.h>
65
66 #include <machine/cpu.h>
67 #include <machine/cpufunc.h>
68 #include <machine/pio.h>
69
70 #include <i386/isa/isa.h>
71 #include <i386/isa/isa_device.h>
72 #include <i386/isa/icu.h>
73 #include <i386/isa/wdreg.h>
74
75 #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
76 #define WDCDELAY 100
77 #define RECOVERYTIME hz/2 /* time to recover from an error */
78
79 #if 0
80 /* If you enable this, it will report any delays more than 100us * N long. */
81 #define WDCNDELAY_DEBUG 50
82 #endif
83
84 #define WDIORETRIES 5 /* number of retries before giving up */
85
86 #define WDUNIT(dev) ((minor(dev) & 0xf8) >> 3)
87 #define WDPART(dev) ((minor(dev) & 0x07) )
88 #define makewddev(maj, unit, part) (makedev(maj, ((unit << 3) + part)))
89 #define WDRAW 3 /* 'd' partition isn't a partition! */
90
91 #define b_cylin b_resid /* cylinder number for doing IO to */
92 /* shares an entry in the buf struct */
93
94 /*
95 * Drive states. Used to initialize drive.
96 */
97 #define CLOSED 0 /* disk is closed */
98 #define WANTOPEN 1 /* open requested, not started */
99 #define RECAL 2 /* doing restore */
100 #define OPEN 3 /* done with open */
101
102 /*
103 * Drive status.
104 */
105 struct wd_softc {
106 struct device sc_dev;
107
108 long sc_bcount; /* byte count left */
109 long sc_mbcount; /* total byte count left */
110 short sc_skip; /* blocks already transferred */
111 short sc_mskip; /* blocks already transferred for multi */
112 char sc_unit; /* physical unit number */
113 char sc_lunit; /* logical unit number */
114 char sc_state; /* control state */
115
116 u_long sc_copenpart; /* character units open on this drive */
117 u_long sc_bopenpart; /* block units open on this drive */
118 u_long sc_openpart; /* all units open on this drive */
119 short sc_wlabel; /* label writable? */
120 short sc_flags; /* drive characteistics found */
121 #define WDF_SINGLE 0x00004 /* sector at a time mode */
122 #define WDF_ERROR 0x00008 /* processing a disk error */
123 #define WDF_BSDLABEL 0x00010 /* has a BSD disk label */
124 #define WDF_BADSECT 0x00020 /* has a bad144 badsector table */
125 #define WDF_WRITEPROT 0x00040 /* manual unit write protect */
126 struct buf sc_q;
127 struct wdparams sc_params; /* ESDI/IDE drive/controller parameters */
128 struct disklabel sc_label; /* device configuration data */
129 struct cpu_disklabel sc_cpulabel;
130 long sc_badsect[127]; /* 126 plus trailing -1 marker */
131 } *wd_softc[NWD];
132
133 struct wdc_softc {
134 struct device sc_dev;
135
136 struct buf sc_q;
137 u_char sc_status; /* copy of status register */
138 u_char sc_error; /* copy of error register */
139 u_short sc_iobase; /* i/o port base */
140 int sc_timeout; /* timeout counter */
141 } wdc_softc[NWDC];
142
143 int wdcprobe(), wdcattach(), wdprobe(), wdattach(), wdintr();
144
145 struct isa_driver wdcdriver = {
146 wdcprobe, wdcattach, "wdc",
147 };
148
149 struct isa_driver wddriver = {
150 wdprobe, wdattach, "wd",
151 };
152
153 void wdfinish __P((struct wd_softc *, struct buf *));
154 static void wdstart __P((struct wd_softc *));
155 static void wdcstart __P((struct wdc_softc *));
156 static int wdcommand __P((struct wd_softc *, int, int, int, int, int));
157 static int wdcontrol __P((struct buf *));
158 static int wdsetctlr __P((struct wd_softc *));
159 static int wdgetctlr __P((struct wd_softc *));
160 static void bad144intern __P((struct wd_softc *));
161 static int wdcreset __P((struct wdc_softc *));
162 static void wdcrestart __P((struct wdc_softc *));
163 static void wdcunwedge __P((struct wdc_softc *));
164 static void wdctimeout __P((struct wdc_softc *));
165 void wddisksort __P((struct buf *, struct buf *));
166 static void wderror __P((void *, struct buf *, char *));
167 int wdcwait __P((struct wdc_softc *, int));
168 #define wait_for_drq(d) wdcwait(d, WDCS_DRQ)
169 #define wait_for_ready(d) wdcwait(d, WDCS_READY | WDCS_SEEKCMPLT)
170 #define wait_for_unbusy(d) wdcwait(d, 0)
171
172 /*
173 * Probe for controller.
174 */
175 int
176 wdcprobe(dev)
177 struct isa_device *dev;
178 {
179 struct wd_softc *wd;
180 struct wdc_softc *wdc;
181 u_short iobase;
182
183 if (dev->id_unit >= NWDC)
184 return 0;
185 wdc = &wdc_softc[dev->id_unit];
186
187 /* XXX HACK */
188 sprintf(wdc->sc_dev.dv_xname, "%s%d", wdcdriver.name, dev->id_unit);
189 wdc->sc_dev.dv_unit = dev->id_unit;
190
191 wdc->sc_iobase = iobase = dev->id_iobase;
192
193 /* Check if we have registers that work. */
194 outb(iobase+wd_error, 0x5a); /* Error register not writable. */
195 outb(iobase+wd_cyl_lo, 0xa5); /* But all of cyllo are implemented. */
196 if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
197 return 0;
198
199 if (wdcreset(wdc) != 0) {
200 delay(500000);
201 if (wdcreset(wdc) != 0)
202 return 0;
203 }
204
205 /*
206 * XXX wdcommand() accepts a wd_softc, so we have to make it one.
207 */
208 wd = (void *)malloc(sizeof(struct wd_softc), M_TEMP, M_NOWAIT);
209 bzero(wd, sizeof(struct wd_softc));
210 wd->sc_unit = 0;
211 wd->sc_lunit = 0;
212 wd->sc_dev.dv_parent = (void *)wdc;
213
214 /* Execute a controller only command. */
215 if (wdcommand(wd, 0, 0, 0, 0, WDCC_DIAGNOSE) != 0 ||
216 wait_for_unbusy(wdc) != 0)
217 goto lose;
218
219 wdctimeout(wdc);
220
221 free(wd, M_TEMP);
222 return 8;
223
224 lose:
225 free(wd, M_TEMP);
226 return 0;
227 }
228
229 int
230 wdcattach(dev)
231 struct isa_device *dev;
232 {
233
234 /* XXXX */
235 if (dev->id_iobase == 0) {
236 static int warned;
237 if (!warned) {
238 warned = 1;
239 printf("PLEASE UPDATE YOUR COPY OF CONFIG(8)!\n");
240 }
241 return wdattach(dev);
242 }
243 }
244
245 int
246 wdprobe(dev)
247 struct isa_device *dev;
248 {
249
250 return 1;
251 }
252
253 /*
254 * Called for the controller too. Attach each drive if possible.
255 */
256 int
257 wdattach(dev)
258 struct isa_device *dev;
259 {
260 int lunit;
261 struct wd_softc *wd;
262 struct wdc_softc *wdc;
263 int i, blank;
264
265 if (dev->id_masunit >= NWDC)
266 return 0;
267
268 lunit = dev->id_unit;
269 if (lunit == -1) {
270 printf("%s: cannot support unit ?\n", wdc->sc_dev.dv_xname);
271 return 0;
272 }
273 if (lunit >= NWD)
274 return 0;
275
276 wdc = &wdc_softc[dev->id_masunit];
277
278 wd_softc[lunit] = wd =
279 (void *)malloc(sizeof(struct wd_softc), M_TEMP, M_NOWAIT);
280 bzero(wd, sizeof(struct wd_softc));
281 wd->sc_unit = dev->id_physid;
282 wd->sc_lunit = lunit;
283
284 /* XXX HACK */
285 sprintf(wd->sc_dev.dv_xname, "%s%d", wddriver.name, dev->id_unit);
286 wd->sc_dev.dv_unit = dev->id_unit;
287 wd->sc_dev.dv_parent = (void *)wdc;
288
289 if (wdgetctlr(wd) != 0)
290 return 0;
291
292 printf("%s at %s targ %d: ", wd->sc_dev.dv_xname, wdc->sc_dev.dv_xname,
293 dev->id_physid);
294 if (wd->sc_params.wdp_heads == 0)
295 printf("(unknown size) <");
296 else
297 printf("%dMB %d cyl, %d head, %d sec <",
298 wd->sc_label.d_ncylinders * wd->sc_label.d_secpercyl /
299 (1048576 / DEV_BSIZE),
300 wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
301 wd->sc_label.d_nsectors);
302 for (i = blank = 0; i < sizeof(wd->sc_params.wdp_model); i++) {
303 char c = wd->sc_params.wdp_model[i];
304 if (c == '\0')
305 break;
306 if (c != ' ') {
307 if (blank)
308 printf(" %c", c);
309 else
310 printf("%c", c);
311 blank = 0;
312 } else
313 blank = 1;
314 }
315 printf(">\n");
316
317 return 1;
318 }
319
320 /*
321 * Read/write routine for a buffer. Finds the proper unit, range checks
322 * arguments, and schedules the transfer. Does not wait for the transfer to
323 * complete. Multi-page transfers are supported. All I/O requests must be a
324 * multiple of a sector in length.
325 */
326 void
327 wdstrategy(bp)
328 struct buf *bp;
329 {
330 struct buf *dp;
331 struct wd_softc *wd; /* disk unit to do the IO */
332 struct wdc_softc *wdc;
333 int lunit = WDUNIT(bp->b_dev);
334 int s;
335
336 /* Valid unit, controller, and request? */
337 if (lunit >= NWD || bp->b_blkno < 0 ||
338 howmany(bp->b_bcount, DEV_BSIZE) >= (1 << NBBY) ||
339 (wd = wd_softc[lunit]) == 0) {
340 bp->b_error = EINVAL;
341 bp->b_flags |= B_ERROR;
342 goto done;
343 }
344
345 /* "Soft" write protect check. */
346 if ((wd->sc_flags & WDF_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
347 bp->b_error = EROFS;
348 bp->b_flags |= B_ERROR;
349 goto done;
350 }
351
352 /* Have partitions and want to use them? */
353 if ((wd->sc_flags & WDF_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW) {
354 /*
355 * Do bounds checking, adjust transfer. if error, process.
356 * If end of partition, just return.
357 */
358 if (bounds_check_with_label(bp, &wd->sc_label,
359 wd->sc_wlabel) <= 0)
360 goto done;
361 /* Otherwise, process transfer request. */
362 }
363
364 /* Don't bother doing rotational optimization. */
365 bp->b_cylin = 0;
366
367 /* Queue transfer on drive, activate drive and controller if idle. */
368 dp = &wd->sc_q;
369 s = splbio();
370 wddisksort(dp, bp);
371 if (dp->b_active == 0)
372 wdstart(wd); /* Start drive. */
373 wdc = (void *)wd->sc_dev.dv_parent;
374 if (wdc->sc_q.b_active == 0)
375 wdcstart(wdc); /* Start controller. */
376 splx(s);
377 return;
378
379 done:
380 /* Toss transfer; we're done early. */
381 biodone(bp);
382 }
383
384 /*
385 * Need to skip over multitransfer bufs.
386 */
387 void
388 wddisksort(dp, bp)
389 struct buf *dp, *bp;
390 {
391 struct buf *ap;
392
393 while ((ap = dp->b_actf) && ap->b_flags & B_XXX)
394 dp = ap;
395 disksort(dp, bp);
396 }
397
398 /*
399 * Routine to queue a command to the controller. The unit's request is linked
400 * into the active list for the controller. If the controller is idle, the
401 * transfer is started.
402 */
403 static void
404 wdstart(wd)
405 struct wd_softc *wd;
406 {
407 struct buf *dp;
408 struct wdc_softc *wdc;
409
410 dp = &wd->sc_q;
411
412 /* Unit already active? */
413 if (dp->b_active)
414 return;
415 /* Anything to start? */
416 if (dp->b_actf == NULL)
417 return;
418
419 /* Link onto controller queue. */
420 dp->b_forw = NULL;
421 wdc = (void *)wd->sc_dev.dv_parent;
422 if (wdc->sc_q.b_forw == NULL)
423 wdc->sc_q.b_forw = dp;
424 else
425 wdc->sc_q.b_actl->b_forw = dp;
426 wdc->sc_q.b_actl = dp;
427
428 /* Mark the drive unit as busy. */
429 dp->b_active = 1;
430 }
431
432 void
433 wdfinish(wd, bp)
434 struct wd_softc *wd;
435 struct buf *bp;
436 {
437 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
438
439 #ifdef INSTRUMENT
440 dk_busy &= ~(1 << wd->sc_unit);
441 #endif
442 wd->sc_flags &= ~(WDF_SINGLE | WDF_ERROR);
443 wdc->sc_q.b_errcnt = 0;
444 /*
445 * If this is the only buf or the last buf in the transfer (taking into
446 * account any residual, in case we erred)...
447 */
448 wd->sc_mbcount -= wd->sc_bcount;
449 if (wd->sc_mbcount == 0) {
450 /*
451 * ...then move this drive to the end of the queue to give
452 * others a `fair' chance.
453 */
454 wdc->sc_q.b_forw = wd->sc_q.b_forw;
455 wd->sc_mskip = 0;
456 wd->sc_q.b_active = 0;
457 if (wd->sc_q.b_actf)
458 wdstart(wd);
459 }
460 bp->b_resid = wd->sc_bcount;
461 bp->b_flags &= ~B_XXX;
462 wd->sc_skip = 0;
463 wd->sc_q.b_actf = bp->b_actf;
464 biodone(bp);
465 }
466
467 /*
468 * Controller startup routine. This does the calculation, and starts a
469 * single-sector read or write operation. Called to start a transfer, or from
470 * the interrupt routine to continue a multi-sector transfer.
471 * RESTRICTIONS:
472 * 1. The transfer length must be an exact multiple of the sector size.
473 */
474 static void
475 wdcstart(wdc)
476 struct wdc_softc *wdc;
477 {
478 struct wd_softc *wd; /* disk unit for IO */
479 struct buf *bp;
480 struct disklabel *lp;
481 struct buf *dp;
482 long blknum, cylin, head, sector;
483 long secpertrk, secpercyl;
484 int xfrblknum;
485 int lunit;
486
487 loop:
488 /* Is there a drive for the controller to do a transfer with? */
489 dp = wdc->sc_q.b_forw;
490 if (dp == NULL) {
491 wdc->sc_q.b_active = 0;
492 return;
493 }
494
495 /* Is there a transfer to this drive? If not, deactivate drive. */
496 bp = dp->b_actf;
497 if (bp == NULL) {
498 dp->b_active = 0;
499 wdc->sc_q.b_forw = dp->b_forw;
500 goto loop;
501 }
502
503 /* Obtain controller and drive information */
504 lunit = WDUNIT(bp->b_dev);
505 wd = wd_softc[lunit];
506
507 if (wdc->sc_q.b_errcnt >= WDIORETRIES) {
508 wderror(wd, bp, "hard error");
509 bp->b_error = EIO;
510 bp->b_flags |= B_ERROR;
511 wdfinish(wd, bp);
512 goto loop;
513 }
514
515 /* Clear any pending timeout, just in case. */
516 wdc->sc_timeout = 0;
517
518 /* If not really a transfer, do control operations specially. */
519 if (wd->sc_state < OPEN) {
520 (void) wdcontrol(bp);
521 return;
522 }
523
524 /*
525 * WDF_ERROR is set by wdcunwedge() and wdintr() when an error is
526 * encountered *only* in multi-sector mode. We switch to single-sector
527 * mode and retry the operation from the start.
528 */
529 if (wd->sc_flags & WDF_ERROR) {
530 /* Switch to single-sector mode. */
531 wd->sc_flags &= ~WDF_ERROR;
532 wd->sc_flags |= WDF_SINGLE;
533 wd->sc_skip = 0;
534 wd->sc_mskip = 0;
535 }
536
537 /* Calculate transfer details. */
538 blknum = bp->b_blkno + wd->sc_skip;
539 #ifdef WDDEBUG
540 if (wd->sc_skip == 0)
541 printf("\nwdcstart %d: %s %d@%d; map ", lunit,
542 (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
543 blknum);
544 else
545 printf(" %d)%x", wd->sc_skip, inb(wd->sc_iobase+wd_altsts));
546 #endif
547 if (wd->sc_skip == 0)
548 wd->sc_bcount = bp->b_bcount;
549 if (wd->sc_mskip == 0) {
550 struct buf *oldbp, *nextbp;
551 oldbp = bp;
552 nextbp = bp->b_actf;
553 wd->sc_mbcount = wd->sc_bcount;
554 oldbp->b_flags |= B_XXX;
555 while (nextbp && (oldbp->b_flags & WDF_SINGLE) == 0 &&
556 oldbp->b_dev == nextbp->b_dev && nextbp->b_blkno ==
557 (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
558 (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
559 if ((wd->sc_mbcount + nextbp->b_bcount) / DEV_BSIZE >= 240)
560 break;
561 wd->sc_mbcount += nextbp->b_bcount;
562 nextbp->b_flags |= B_XXX;
563 oldbp = nextbp;
564 nextbp = nextbp->b_actf;
565 }
566 }
567
568 lp = &wd->sc_label;
569 secpertrk = lp->d_nsectors;
570 secpercyl = lp->d_secpercyl;
571 if ((wd->sc_flags & WDF_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW)
572 blknum += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
573 cylin = blknum / secpercyl;
574 head = (blknum % secpercyl) / secpertrk;
575 sector = blknum % secpertrk;
576
577 /*
578 * Check for bad sectors if we have them, and not formatting. Only do
579 * this in single-sector mode, or when starting a multiple-sector
580 * transfer.
581 */
582 if ((wd->sc_flags & WDF_BADSECT) &&
583 #ifdef B_FORMAT
584 (bp->b_flags & B_FORMAT) == 0 &&
585 #endif
586 (wd->sc_mskip == 0 || (wd->sc_flags & WDF_SINGLE))) {
587
588 long blkchk, blkend, blknew;
589 int i;
590
591 blkend = blknum + howmany(wd->sc_mbcount, DEV_BSIZE) - 1;
592 for (i = 0; (blkchk = wd->sc_badsect[i]) != -1; i++) {
593 if (blkchk > blkend)
594 break; /* Transfer is completely OK; done. */
595 if (blkchk == blknum) {
596 blknew =
597 lp->d_secperunit - lp->d_nsectors - i - 1;
598 cylin = blknew / secpercyl;
599 head = (blknew % secpercyl) / secpertrk;
600 sector = blknew % secpertrk;
601 wd->sc_flags |= WDF_SINGLE;
602 /* Found and replaced first blk of transfer; done. */
603 break;
604 } else if (blkchk > blknum) {
605 wd->sc_flags |= WDF_SINGLE;
606 break; /* Bad block inside transfer; done. */
607 }
608 }
609 }
610 if (wd->sc_flags & WDF_SINGLE) {
611 wd->sc_mbcount = wd->sc_bcount;
612 wd->sc_mskip = wd->sc_skip;
613 }
614
615 #ifdef WDDEBUG
616 printf("c%d h%d s%d ", cylin, head, sector);
617 #endif
618
619 sector++; /* Sectors begin with 1, not 0. */
620
621 wdc->sc_q.b_active = 1; /* Mark controller active. */
622
623 #ifdef INSTRUMENT
624 if (wd->sc_skip == 0) {
625 dk_busy |= 1 << wd->sc_lunit;
626 dk_wds[wd->sc_lunit] += bp->b_bcount >> 6;
627 }
628 #endif
629
630 /* If starting a multisector transfer, or doing single transfers. */
631 if (wd->sc_mskip == 0 || (wd->sc_flags & WDF_SINGLE)) {
632 int command, count;
633
634 #ifdef INSTRUMENT
635 ++dk_seek[wd->sc_lunit];
636 ++dk_xfer[wd->sc_lunit];
637 #endif
638
639 #ifdef B_FORMAT
640 if (bp->b_flags & B_FORMAT) {
641 sector = lp->d_gap3;
642 count = lp->d_nsectors;
643 command = WDCC_FORMAT;
644 } else {
645 if (wd->sc_flags & WDF_SINGLE)
646 count = 1;
647 else
648 count = howmany(wd->sc_mbcount, DEV_BSIZE);
649 command =
650 (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
651 }
652 #else
653 if (wd->sc_flags & WDF_SINGLE)
654 count = 1;
655 else
656 count = howmany(wd->sc_mbcount, DEV_BSIZE);
657 command = (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
658 #endif
659
660 /* Initiate command! */
661 if (wdcommand(wd, cylin, head, sector, count, command) != 0) {
662 wderror(wd, NULL,
663 "wdcstart: timeout waiting for unbusy");
664 wdcunwedge(wdc);
665 return;
666 }
667 #ifdef WDDEBUG
668 printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
669 cylin, head, bp->b_un.b_addr, inb(wd->sc_iobase+wd_altsts));
670 #endif
671 }
672
673 wdc->sc_timeout = 4;
674
675 /* If this is a read operation, just go away until it's done. */
676 if (bp->b_flags & B_READ)
677 return;
678
679 if (wait_for_drq(wdc) < 0) {
680 wderror(wd, NULL, "wdcstart: timeout waiting for drq");
681 wdcunwedge(wdc);
682 return;
683 }
684
685 /* Then send it! */
686 outsw(wdc->sc_iobase+wd_data, bp->b_un.b_addr + wd->sc_skip * DEV_BSIZE,
687 DEV_BSIZE / sizeof(short));
688 }
689
690 /*
691 * Interrupt routine for the controller. Acknowledge the interrupt, check for
692 * errors on the current operation, mark it done if necessary, and start the
693 * next request. Also check for a partially done transfer, and continue with
694 * the next chunk if so.
695 */
696 int
697 wdintr(ctrlr)
698 int ctrlr;
699 {
700 struct wdc_softc *wdc = &wdc_softc[ctrlr];
701 struct wd_softc *wd;
702 struct buf *bp;
703
704 /* Clear the pending interrupt. */
705 (void) inb(wdc->sc_iobase+wd_status);
706
707 if (!wdc->sc_q.b_active) {
708 printf("%s: extra interrupt\n", wdc->sc_dev.dv_xname);
709 return 0;
710 }
711
712 bp = wdc->sc_q.b_forw->b_actf;
713 wd = wd_softc[WDUNIT(bp->b_dev)];
714 wdc->sc_timeout = 0;
715
716 #ifdef WDDEBUG
717 printf("I%d ", ctrlr);
718 #endif
719
720 if (wait_for_unbusy(wdc) < 0) {
721 wderror(wd, NULL, "wdintr: timeout waiting for unbusy");
722 wdc->sc_status |= WDCS_ERR; /* XXX */
723 }
724
725 /* Is it not a transfer, but a control operation? */
726 if (wd->sc_state < OPEN) {
727 wdc->sc_q.b_active = 0;
728 if (wdcontrol(bp))
729 wdcstart(wdc);
730 return 1;
731 }
732
733 /* Have we an error? */
734 if (wdc->sc_status & (WDCS_ERR | WDCS_ECCCOR)) {
735 lose:
736 #ifdef WDDEBUG
737 wderror(wd, NULL, "wdintr");
738 #endif
739 if ((wd->sc_flags & WDF_SINGLE) == 0) {
740 wd->sc_flags |= WDF_ERROR;
741 goto restart;
742 }
743 #ifdef B_FORMAT
744 if (bp->b_flags & B_FORMAT) {
745 bp->b_error = EIO;
746 bp->b_flags |= B_ERROR;
747 goto done;
748 }
749 #endif
750
751 /* Error or error correction? */
752 if (wdc->sc_status & WDCS_ERR) {
753 if (++wdc->sc_q.b_errcnt >= WDIORETRIES) {
754 wderror(wd, bp, "hard error");
755 bp->b_error = EIO;
756 bp->b_flags |= B_ERROR; /* Flag the error. */
757 goto done;
758 } else
759 goto restart;
760 } else
761 wderror(wd, bp, "soft ecc");
762 }
763
764 /* If this was a read, fetch the data. */
765 if (bp->b_flags & B_READ) {
766 /* Ready to receive data? */
767 if (wait_for_drq(wdc) != 0) {
768 wderror(wd, NULL, "wdintr: read error detected late");
769 wdcunwedge(wdc);
770 return 1;
771 }
772
773 /* Suck in data. */
774 insw(wdc->sc_iobase+wd_data,
775 bp->b_un.b_addr + wd->sc_skip * DEV_BSIZE,
776 DEV_BSIZE / sizeof(short));
777 }
778
779 /* If we encountered any abnormalities, flag it as a soft error. */
780 if (wdc->sc_q.b_errcnt) {
781 wderror(wd, bp, "soft error");
782 wdc->sc_q.b_errcnt = 0;
783 }
784
785 /* Ready for the next block, if any. */
786 wd->sc_skip++;
787 wd->sc_mskip++;
788 wd->sc_bcount -= DEV_BSIZE;
789 wd->sc_mbcount -= DEV_BSIZE;
790
791 /* See if more to transfer. */
792 if (wd->sc_bcount > 0)
793 goto restart;
794
795 done:
796 /* Done with this transfer, with or without error. */
797 wdfinish(wd, bp);
798
799 restart:
800 /* Start the next transfer, if any. */
801 wdc->sc_q.b_active = 0;
802 wdcstart(wdc);
803
804 return 1;
805 }
806
807 /*
808 * Initialize a drive.
809 */
810 int
811 wdopen(dev, flag, fmt, p)
812 dev_t dev;
813 int flag;
814 int fmt;
815 struct proc *p;
816 {
817 int lunit;
818 struct wd_softc *wd;
819 int part = WDPART(dev), mask = 1 << part;
820 struct partition *pp;
821 char *msg;
822
823 lunit = WDUNIT(dev);
824 if (lunit >= NWD)
825 return ENXIO;
826 wd = wd_softc[lunit];
827 if (wd == 0)
828 return ENXIO;
829
830 if ((wd->sc_flags & WDF_BSDLABEL) == 0) {
831 wd->sc_flags |= WDF_WRITEPROT;
832 wd->sc_q.b_actf = NULL;
833
834 /*
835 * Use the default sizes until we've read the label, or longer
836 * if there isn't one there.
837 */
838 bzero(&wd->sc_label, sizeof(wd->sc_label));
839 wd->sc_label.d_type = DTYPE_ST506;
840 wd->sc_label.d_ncylinders = 1024;
841 wd->sc_label.d_secsize = DEV_BSIZE;
842 wd->sc_label.d_ntracks = 8;
843 wd->sc_label.d_nsectors = 17;
844 wd->sc_label.d_secpercyl = 17*8;
845 wd->sc_label.d_secperunit = 17*8*1024;
846 wd->sc_state = WANTOPEN;
847
848 /* Read label using "raw" partition. */
849 #ifdef notdef
850 /* wdsetctlr(wd); */ /* Maybe do this TIH */
851 msg = readdisklabel(makewddev(major(dev), WDUNIT(dev), WDRAW),
852 wdstrategy, &wd->sc_label, &wd->sc_cpulabel);
853 wdsetctlr(wd);
854 #endif
855 if (msg = readdisklabel(makewddev(major(dev), WDUNIT(dev),
856 WDRAW), wdstrategy, &wd->sc_label, &wd->sc_cpulabel)) {
857 log(LOG_WARNING, "%s: cannot find label (%s)\n",
858 wd->sc_dev.dv_xname, msg);
859 if (part != WDRAW)
860 return EINVAL; /* XXX needs translation */
861 } else {
862 wdsetctlr(wd);
863 wd->sc_flags |= WDF_BSDLABEL;
864 wd->sc_flags &= ~WDF_WRITEPROT;
865 if (wd->sc_label.d_flags & D_BADSECT)
866 wd->sc_flags |= WDF_BADSECT;
867 }
868 }
869
870 if (wd->sc_flags & WDF_BADSECT)
871 bad144intern(wd);
872
873 /*
874 * Warn if a partition is opened that overlaps another partition which
875 * is open unless one is the "raw" partition (whole disk).
876 */
877 if ((wd->sc_openpart & mask) == 0 && part != WDRAW) {
878 int start, end;
879
880 pp = &wd->sc_label.d_partitions[part];
881 start = pp->p_offset;
882 end = pp->p_offset + pp->p_size;
883 for (pp = wd->sc_label.d_partitions;
884 pp < &wd->sc_label.d_partitions[wd->sc_label.d_npartitions];
885 pp++) {
886 if (pp->p_offset + pp->p_size <= start ||
887 pp->p_offset >= end)
888 continue;
889 if (pp - wd->sc_label.d_partitions == WDRAW)
890 continue;
891 if (wd->sc_openpart & (1 << (pp - wd->sc_label.d_partitions)))
892 log(LOG_WARNING,
893 "%s%c: overlaps open partition (%c)\n",
894 wd->sc_dev.dv_xname, part + 'a',
895 pp - wd->sc_label.d_partitions + 'a');
896 }
897 }
898 if (part >= wd->sc_label.d_npartitions && part != WDRAW)
899 return ENXIO;
900
901 /* Insure only one open at a time. */
902 switch (fmt) {
903 case S_IFCHR:
904 wd->sc_copenpart |= mask;
905 break;
906 case S_IFBLK:
907 wd->sc_bopenpart |= mask;
908 break;
909 }
910 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
911
912 return 0;
913 }
914
915 /*
916 * Implement operations other than read/write.
917 * Called from wdcstart or wdintr during opens and formats.
918 * Uses finite-state-machine to track progress of operation in progress.
919 * Returns 0 if operation still in progress, 1 if completed.
920 */
921 static int
922 wdcontrol(bp)
923 struct buf *bp;
924 {
925 struct wd_softc *wd = wd_softc[WDUNIT(bp->b_dev)];
926 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
927
928 switch (wd->sc_state) {
929 case WANTOPEN: /* Set SDH, step rate, do restore. */
930 tryagainrecal:
931 wdgetctlr(wd); /* XXX Is this necessary? */
932 wdc->sc_q.b_active = 1;
933 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0) {
934 wderror(wd, NULL, "wdcontrol: wdcommand failed");
935 wdcunwedge(wdc);
936 return 0;
937 }
938 wd->sc_state = RECAL;
939 return 0;
940
941 case RECAL:
942 if (wdc->sc_status & WDCS_ERR || wdsetctlr(wd) != 0) {
943 wderror(wd, NULL, "wdcontrol: recal failed");
944 wdcunwedge(wdc);
945 return 0;
946 }
947 wdc->sc_q.b_errcnt = 0;
948 wd->sc_state = OPEN;
949 /*
950 * The rest of the initialization can be done by normal means.
951 */
952 return 1;
953 }
954
955 #ifdef DIAGNOSTIC
956 panic("wdcontrol: impossible");
957 #endif
958 }
959
960 /*
961 * Send a command and wait uninterruptibly until controller is finished.
962 * Return -1 if controller busy for too long, otherwise return non-zero if
963 * error. Intended for brief controller commands at critical points.
964 * Assumes interrupts are blocked.
965 */
966 static int
967 wdcommand(wd, cylin, head, sector, count, cmd)
968 struct wd_softc *wd;
969 int cylin, head, sector, count;
970 int cmd;
971 {
972 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
973 int stat;
974 u_short iobase;
975
976 /* Controller ready for command? */
977 if (wait_for_unbusy(wdc) < 0)
978 return -1;
979
980 /* Select drive. */
981 iobase = wdc->sc_iobase;
982 outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_unit << 4) | head);
983 if (cmd == WDCC_DIAGNOSE || cmd == WDCC_IDC)
984 stat = wait_for_unbusy(wdc);
985 else
986 stat = wdcwait(wdc, WDCS_READY);
987 if (stat < 0)
988 return -1;
989
990 /* Load parameters. */
991 outb(iobase+wd_precomp, wd->sc_label.d_precompcyl / 4);
992 outb(iobase+wd_cyl_lo, cylin);
993 outb(iobase+wd_cyl_hi, cylin >> 8);
994 outb(iobase+wd_sector, sector);
995 outb(iobase+wd_seccnt, count);
996
997 /* Send command, await results. */
998 outb(iobase+wd_command, cmd);
999
1000 return 0;
1001 }
1002
1003 /*
1004 * Issue IDC to drive to tell it just what geometry it is to be.
1005 */
1006 static int
1007 wdsetctlr(wd)
1008 struct wd_softc *wd;
1009 {
1010 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1011
1012 #ifdef WDDEBUG
1013 printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_ctrlr, wd->sc_unit,
1014 wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
1015 wd->sc_label.d_nsectors);
1016 #endif
1017
1018 if (wdcommand(wd, wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks - 1,
1019 0, wd->sc_label.d_nsectors, WDCC_IDC) != 0 ||
1020 wdcwait(wdc, WDCS_READY) != 0) {
1021 wderror(wd, NULL, "wdsetctlr: failed");
1022 return -1;
1023 }
1024 return 0;
1025 }
1026
1027 /*
1028 * Issue READP to drive to ask it what it is.
1029 */
1030 static int
1031 wdgetctlr(wd)
1032 struct wd_softc *wd;
1033 {
1034 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1035 int i;
1036 char tb[DEV_BSIZE];
1037 struct wdparams *wp;
1038
1039 if (wdcommand(wd, 0, 0, 0, 0, WDCC_READP) != 0 ||
1040 wait_for_drq(wdc) != 0) {
1041 /*
1042 * If WDCC_READP fails then we might have an old drive so we
1043 * try a seek to 0; if that passes then the drive is there but
1044 * it's OLD AND KRUSTY.
1045 */
1046 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
1047 wait_for_ready(wdc) != 0)
1048 return -1;
1049
1050 strncpy(wd->sc_label.d_typename, "ST506",
1051 sizeof wd->sc_label.d_typename);
1052 strncpy(wd->sc_params.wdp_model, "Unknown Type",
1053 sizeof wd->sc_params.wdp_model);
1054 wd->sc_label.d_type = DTYPE_ST506;
1055 } else {
1056 /* Obtain parameters. */
1057 wp = &wd->sc_params;
1058 insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
1059 bcopy(tb, wp, sizeof(struct wdparams));
1060
1061 /* Shuffle string byte order. */
1062 for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
1063 u_short *p;
1064 p = (u_short *)(wp->wdp_model + i);
1065 *p = ntohs(*p);
1066 }
1067
1068 strncpy(wd->sc_label.d_typename, "ESDI/IDE",
1069 sizeof wd->sc_label.d_typename);
1070 wd->sc_label.d_type = DTYPE_ESDI;
1071 bcopy(wp->wdp_model+20, wd->sc_label.d_packname, 14-1);
1072
1073 /* Update disklabel given drive information. */
1074 wd->sc_label.d_ncylinders =
1075 wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
1076 wd->sc_label.d_ntracks = wp->wdp_heads;
1077 wd->sc_label.d_nsectors = wp->wdp_sectors;
1078 wd->sc_label.d_secpercyl =
1079 wd->sc_label.d_ntracks * wd->sc_label.d_nsectors;
1080 wd->sc_label.d_partitions[1].p_size =
1081 wd->sc_label.d_secpercyl * wp->wdp_sectors;
1082 wd->sc_label.d_partitions[1].p_offset = 0;
1083 }
1084
1085 #if 0
1086 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1087 wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1088 wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1089 #endif
1090
1091 wd->sc_label.d_subtype |= DSTYPE_GEOMETRY;
1092
1093 /* XXX sometimes possibly needed */
1094 (void) inb(wdc->sc_iobase+wd_status);
1095
1096 return 0;
1097 }
1098
1099 int
1100 wdclose(dev, flag, fmt)
1101 dev_t dev;
1102 int flag;
1103 int fmt;
1104 {
1105 struct wd_softc *wd = wd_softc[WDUNIT(dev)];
1106 int part = WDPART(dev), mask = 1 << part;
1107
1108 switch (fmt) {
1109 case S_IFCHR:
1110 wd->sc_copenpart &= ~mask;
1111 break;
1112 case S_IFBLK:
1113 wd->sc_bopenpart &= ~mask;
1114 break;
1115 }
1116 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
1117
1118 return 0;
1119 }
1120
1121 int
1122 wdioctl(dev, cmd, addr, flag, p)
1123 dev_t dev;
1124 int cmd;
1125 caddr_t addr;
1126 int flag;
1127 struct proc *p;
1128 {
1129 int lunit = WDUNIT(dev);
1130 struct wd_softc *wd = wd_softc[lunit];
1131 int error;
1132
1133 switch (cmd) {
1134 case DIOCSBAD:
1135 if ((flag & FWRITE) == 0)
1136 return EBADF;
1137 wd->sc_cpulabel.bad = *(struct dkbad *)addr;
1138 bad144intern(wd);
1139 return 0;
1140
1141 case DIOCGDINFO:
1142 *(struct disklabel *)addr = wd->sc_label;
1143 return 0;
1144
1145 case DIOCGPART:
1146 ((struct partinfo *)addr)->disklab = &wd->sc_label;
1147 ((struct partinfo *)addr)->part =
1148 &wd->sc_label.d_partitions[WDPART(dev)];
1149 return 0;
1150
1151 case DIOCSDINFO:
1152 if ((flag & FWRITE) == 0)
1153 return EBADF;
1154 error = setdisklabel(&wd->sc_label,
1155 (struct disklabel *)addr,
1156 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart : */0,
1157 &wd->sc_cpulabel);
1158 if (error == 0) {
1159 wd->sc_flags |= WDF_BSDLABEL;
1160 wdsetctlr(wd);
1161 }
1162 return error;
1163
1164 case DIOCWLABEL:
1165 wd->sc_flags &= ~WDF_WRITEPROT;
1166 if ((flag & FWRITE) == 0)
1167 return EBADF;
1168 wd->sc_wlabel = *(int *)addr;
1169 return 0;
1170
1171 case DIOCWDINFO:
1172 wd->sc_flags &= ~WDF_WRITEPROT;
1173 if ((flag & FWRITE) == 0)
1174 return EBADF;
1175 error = setdisklabel(&wd->sc_label,
1176 (struct disklabel *)addr,
1177 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart :*/0,
1178 &wd->sc_cpulabel);
1179 if (error == 0) {
1180 int wlab;
1181
1182 wd->sc_flags |= WDF_BSDLABEL;
1183 wdsetctlr(wd);
1184
1185 /* Simulate opening partition 0 so write succeeds. */
1186 wd->sc_openpart |= (1 << 0); /* XXX */
1187 wlab = wd->sc_wlabel;
1188 wd->sc_wlabel = 1;
1189 error = writedisklabel(dev, wdstrategy, &wd->sc_label,
1190 &wd->sc_cpulabel);
1191 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
1192 wd->sc_wlabel = wlab;
1193 }
1194 return error;
1195
1196 #ifdef notyet
1197 case DIOCGDINFOP:
1198 *(struct disklabel **)addr = &wd->sc_label;
1199 return 0;
1200
1201 case DIOCWFORMAT:
1202 if ((flag & FWRITE) == 0)
1203 return EBADF;
1204 {
1205 register struct format_op *fop;
1206 struct iovec aiov;
1207 struct uio auio;
1208
1209 fop = (struct format_op *)addr;
1210 aiov.iov_base = fop->df_buf;
1211 aiov.iov_len = fop->df_count;
1212 auio.uio_iov = &aiov;
1213 auio.uio_iovcnt = 1;
1214 auio.uio_resid = fop->df_count;
1215 auio.uio_segflg = 0;
1216 auio.uio_offset =
1217 fop->df_startblk * wd->sc_label.d_secsize;
1218 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1219 &auio);
1220 fop->df_count -= auio.uio_resid;
1221 fop->df_reg[0] = wdc->sc_status;
1222 fop->df_reg[1] = wdc->sc_error;
1223 return error;
1224 }
1225 #endif
1226
1227 default:
1228 return ENOTTY;
1229 }
1230
1231 #ifdef DIAGNOSTIC
1232 panic("wdioctl: impossible");
1233 #endif
1234 }
1235
1236 #ifdef B_FORMAT
1237 int
1238 wdformat(struct buf *bp)
1239 {
1240
1241 bp->b_flags |= B_FORMAT;
1242 return wdstrategy(bp);
1243 }
1244 #endif
1245
1246 int
1247 wdsize(dev)
1248 dev_t dev;
1249 {
1250 int lunit = WDUNIT(dev), part = WDPART(dev);
1251 struct wd_softc *wd;
1252
1253 if (lunit >= NWD)
1254 return -1;
1255 wd = wd_softc[lunit];
1256 if (wd == 0)
1257 return -1;
1258
1259 if (wd->sc_state < OPEN || (wd->sc_flags & WDF_BSDLABEL) == 0) {
1260 int val;
1261 val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
1262 S_IFBLK, 0);
1263 /* XXX Clear the open flag? */
1264 if (val != 0)
1265 return -1;
1266 }
1267
1268 if ((wd->sc_flags & (WDF_WRITEPROT | WDF_BSDLABEL)) != WDF_BSDLABEL)
1269 return -1;
1270
1271 return (int)wd->sc_label.d_partitions[part].p_size;
1272 }
1273
1274 /*
1275 * Dump core after a system crash.
1276 */
1277 int
1278 wddump(dev)
1279 dev_t dev;
1280 {
1281 struct wd_softc *wd; /* disk unit to do the IO */
1282 struct wdc_softc *wdc;
1283 long num; /* number of sectors to write */
1284 int lunit, part;
1285 long blkoff, blknum;
1286 long cylin, head, sector;
1287 long secpertrk, secpercyl, nblocks;
1288 char *addr;
1289 static wddoingadump = 0;
1290 extern caddr_t CADDR1;
1291 extern struct pte *CMAP1;
1292
1293 addr = (char *)0; /* starting address */
1294
1295 #if DO_NOT_KNOW_HOW
1296 /* Toss any characters present prior to dump, ie. non-blocking getc. */
1297 while (cngetc())
1298 ;
1299 #endif
1300
1301 lunit = WDUNIT(dev);
1302 /* Check for acceptable drive number. */
1303 if (lunit >= NWD)
1304 return ENXIO;
1305 wd = wd_softc[lunit];
1306 /* Was it ever initialized? */
1307 if (wd == 0 || wd->sc_state < OPEN || wd->sc_flags & WDF_WRITEPROT)
1308 return ENXIO;
1309
1310 wdc = (void *)wd->sc_dev.dv_parent;
1311
1312 /* Convert to disk sectors. */
1313 num = ctob(physmem) / wd->sc_label.d_secsize;
1314
1315 secpertrk = wd->sc_label.d_nsectors;
1316 secpercyl = wd->sc_label.d_secpercyl;
1317 part = WDPART(dev);
1318 nblocks = wd->sc_label.d_partitions[part].p_size;
1319 blkoff = wd->sc_label.d_partitions[part].p_offset;
1320
1321 /*printf("part %x, nblocks %d, dumplo %d, num %d\n", part, nblocks,
1322 dumplo, num);*/
1323
1324 /* Check transfer bounds against partition size. */
1325 if (dumplo < 0 || dumplo + num > nblocks)
1326 return EINVAL;
1327
1328 if (wddoingadump)
1329 return EFAULT;
1330 wddoingadump = 1;
1331
1332 /* Recalibrate. */
1333 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
1334 wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0) {
1335 wderror(wd, NULL, "wddump: recal failed");
1336 return EIO;
1337 }
1338
1339 blknum = dumplo + blkoff;
1340 while (num > 0) {
1341 /* Compute disk address. */
1342 cylin = blknum / secpercyl;
1343 head = (blknum % secpercyl) / secpertrk;
1344 sector = blknum % secpertrk;
1345
1346 if (wd->sc_flags & WDF_BADSECT) {
1347 long newblk;
1348 int i;
1349
1350 for (i = 0; wd->sc_badsect[i] != -1; i++) {
1351 if (blknum < wd->sc_badsect[i]) {
1352 /* Sorted list, passed our block by. */
1353 break;
1354 } else if (blknum == wd->sc_badsect[i]) {
1355 newblk = wd->sc_label.d_secperunit -
1356 wd->sc_label.d_nsectors - i - 1;
1357 cylin = newblk / secpercyl;
1358 head = (newblk % secpercyl) / secpertrk;
1359 sector = newblk % secpertrk;
1360 /* Found and replaced; done. */
1361 break;
1362 }
1363 }
1364 }
1365 sector++; /* origin 1 */
1366
1367 #ifdef notdef
1368 /* Let's just talk about this first. */
1369 printf("cylin %d, head %d, sector %d, addr 0x%x", cylin, head,
1370 sector, addr);
1371 #endif
1372 if (wdcommand(wd, cylin, head, sector, 1, WDCC_WRITE) != 0) {
1373 wderror(wd, NULL, "wddump: wdcommand failed");
1374 return EIO;
1375 }
1376 if (wait_for_drq(wdc) != 0) {
1377 wderror(wd, NULL, "wddump: timeout waiting for drq");
1378 return EIO;
1379 }
1380
1381 #ifdef notdef /* Cannot use this since this address was mapped differently. */
1382 pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1383 #else
1384 *(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
1385 tlbflush();
1386 #endif
1387
1388 outsw(wdc->sc_iobase+wd_data, CADDR1 + ((int)addr & PGOFSET),
1389 DEV_BSIZE / sizeof(short));
1390
1391 /* Check data request (should be done). */
1392 if (wait_for_ready(wdc) != 0) {
1393 wderror(wd, NULL, "wddump: timeout waiting for ready");
1394 return EIO;
1395 }
1396 if (wdc->sc_status & WDCS_DRQ) {
1397 wderror(wd, NULL, "wddump: extra drq");
1398 return EIO;
1399 }
1400
1401 if ((unsigned)addr % 1048576 == 0)
1402 printf("%d ", num / (1048576 / DEV_BSIZE));
1403
1404 /* Update block count. */
1405 num--;
1406 blknum++;
1407 (int)addr += DEV_BSIZE;
1408
1409 #if DO_NOT_KNOW_HOW
1410 /* Operator aborting dump? non-blocking getc() */
1411 if (cngetc())
1412 return EINTR;
1413 #endif
1414 }
1415 return 0;
1416 }
1417
1418 /*
1419 * Internalize the bad sector table.
1420 */
1421 void
1422 bad144intern(wd)
1423 struct wd_softc *wd;
1424 {
1425 int i;
1426
1427 if ((wd->sc_flags & WDF_BADSECT) == 0)
1428 return;
1429
1430 for (i = 0; i < 127; i++)
1431 wd->sc_badsect[i] = -1;
1432 for (i = 0; i < 126; i++) {
1433 if (wd->sc_cpulabel.bad.bt_bad[i].bt_cyl == 0xffff)
1434 break;
1435 wd->sc_badsect[i] =
1436 wd->sc_cpulabel.bad.bt_bad[i].bt_cyl *
1437 wd->sc_label.d_secpercyl +
1438 (wd->sc_cpulabel.bad.bt_bad[i].bt_trksec >> 8) *
1439 wd->sc_label.d_nsectors +
1440 (wd->sc_cpulabel.bad.bt_bad[i].bt_trksec & 0x00ff);
1441 }
1442 }
1443
1444 static int
1445 wdcreset(wdc)
1446 struct wdc_softc *wdc;
1447 {
1448 u_short iobase = wdc->sc_iobase;
1449
1450 /* Reset the device. */
1451 outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1452 delay(1000);
1453 outb(iobase+wd_ctlr, WDCTL_IDS);
1454 delay(1000);
1455 (void) inb(iobase+wd_error);
1456 outb(iobase+wd_ctlr, WDCTL_4BIT);
1457
1458 if (wait_for_unbusy(wdc) < 0) {
1459 printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1460 return 1;
1461 }
1462
1463 return 0;
1464 }
1465
1466 static void
1467 wdcrestart(wdc)
1468 struct wdc_softc *wdc;
1469 {
1470 int s = splbio();
1471
1472 wdc->sc_q.b_active = 0;
1473 wdcstart(wdc);
1474 splx(s);
1475 }
1476
1477 /*
1478 * Unwedge the controller after an unexpected error. We do this by resetting
1479 * it, marking all drives for recalibration, and stalling the queue for a short
1480 * period to give the reset time to finish.
1481 * NOTE: We use a timeout here, so this routine must not be called during
1482 * autoconfig or dump.
1483 */
1484 static void
1485 wdcunwedge(wdc)
1486 struct wdc_softc *wdc;
1487 {
1488 int lunit;
1489
1490 wdc->sc_timeout = 0;
1491 (void) wdcreset(wdc);
1492
1493 /* Schedule recalibrate for all drives on this controller. */
1494 for (lunit = 0; lunit < NWD; lunit++) {
1495 struct wd_softc *wd = wd_softc[lunit];
1496 if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
1497 continue;
1498 if (wd->sc_state > WANTOPEN)
1499 wd->sc_state = WANTOPEN;
1500 if (wd->sc_q.b_active && (wd->sc_flags & WDF_SINGLE) == 0)
1501 wd->sc_flags |= WDF_ERROR;
1502 }
1503
1504 ++wdc->sc_q.b_errcnt;
1505
1506 /* Wake up in a little bit and restart the operation. */
1507 timeout((timeout_t)wdcrestart, (caddr_t)wdc, RECOVERYTIME);
1508 }
1509
1510 int
1511 wdcwait(wdc, mask)
1512 struct wdc_softc *wdc;
1513 int mask;
1514 {
1515 u_short iobase = wdc->sc_iobase;
1516 int timeout = 0;
1517 u_char status;
1518
1519 for (;;) {
1520 wdc->sc_status = status = inb(iobase+wd_altsts);
1521 if ((status & WDCS_BUSY) == 0 && (status & mask) == mask)
1522 break;
1523 if (++timeout > WDCNDELAY)
1524 return -1;
1525 delay(WDCDELAY);
1526 }
1527 if (status & WDCS_ERR)
1528 wdc->sc_error = inb(iobase+wd_error);
1529 #ifdef WDCNDELAY_DEBUG
1530 if (timeout > WDCNDELAY_DEBUG)
1531 printf("%s: busy-wait took %dus\n", wdc->sc_dev.dv_xname,
1532 WDCDELAY * timeout);
1533 #endif
1534 return status & WDCS_ERR;
1535 }
1536
1537 static void
1538 wdctimeout(wdc)
1539 struct wdc_softc *wdc;
1540 {
1541 int s = splbio();
1542
1543 if (wdc->sc_timeout && --wdc->sc_timeout == 0) {
1544 wderror(wdc, NULL, "lost interrupt");
1545 wdcunwedge(wdc);
1546 }
1547 timeout((timeout_t)wdctimeout, (caddr_t)wdc, hz);
1548 splx(s);
1549 }
1550
1551 static void
1552 wderror(dev, bp, msg)
1553 void *dev;
1554 struct buf *bp;
1555 char *msg;
1556 {
1557 struct wd_softc *wd = dev;
1558 struct wdc_softc *wdc = dev;
1559
1560 if (bp)
1561 diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip, &wd->sc_label);
1562 else
1563 printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
1564 msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
1565 }
1566
1567 #endif /* NWDC > 0 */
1568