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