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