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