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