wd.c revision 1.92 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.92 1994/10/14 18:26:56 cgd 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/pio.h>
65
66 #ifndef NEWCONFIG
67 #include <i386/isa/isa_device.h>
68 #endif
69 #include <i386/isa/isavar.h>
70 #include <i386/isa/icu.h>
71 #include <i386/isa/wdreg.h>
72
73 #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
74 #define WDCDELAY 100
75 #define RECOVERYTIME hz/2 /* time to recover from an error */
76
77 #if 0
78 /* If you enable this, it will report any delays more than 100us * N long. */
79 #define WDCNDELAY_DEBUG 10
80 #endif
81
82 #define WDIORETRIES 5 /* number of retries before giving up */
83
84 #define WDUNIT(dev) DISKUNIT(dev)
85 #define WDPART(dev) DISKPART(dev)
86 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
87
88 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
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((void *arg));
169 static void wdcunwedge __P((struct wdc_softc *));
170 static void wdctimeout __P((void *arg));
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 /* ST506 spec says that if READY or SEEKCMPLT go off, then the read or write
175 command is aborted. */
176 #define wait_for_drq(d) wdcwait(d, WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ)
177 #define wait_for_ready(d) wdcwait(d, WDCS_READY | WDCS_SEEKCMPLT)
178 #define wait_for_unbusy(d) wdcwait(d, 0)
179
180 /*
181 * Probe for controller.
182 */
183 int
184 wdcprobe(parent, self, aux)
185 struct device *parent, *self;
186 void *aux;
187 {
188 struct wdc_softc *wdc = (void *)self;
189 struct isa_attach_args *ia = aux;
190 struct wd_softc *wd;
191 u_short iobase;
192
193 wdc->sc_iobase = iobase = ia->ia_iobase;
194
195 /* Check if we have registers that work. */
196 outb(iobase+wd_error, 0x5a); /* Error register not writable. */
197 outb(iobase+wd_cyl_lo, 0xa5); /* But all of cyllo are implemented. */
198 if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
199 return 0;
200
201 if (wdcreset(wdc) != 0) {
202 delay(500000);
203 if (wdcreset(wdc) != 0)
204 return 0;
205 }
206
207 /*
208 * XXX wdcommand() accepts a wd_softc, so we have to make it one.
209 */
210 wd = (void *)malloc(sizeof(struct wd_softc), M_TEMP, M_NOWAIT);
211 bzero(wd, sizeof(struct wd_softc));
212 wd->sc_drive = 0;
213 wd->sc_dev.dv_unit = 0;
214 wd->sc_dev.dv_parent = (void *)wdc;
215
216 /* Execute a controller only command. */
217 if (wdcommand(wd, 0, 0, 0, 0, WDCC_DIAGNOSE) != 0 ||
218 wait_for_unbusy(wdc) != 0)
219 goto lose;
220
221 free(wd, M_TEMP);
222 ia->ia_iosize = 8;
223 ia->ia_msize = 0;
224 return 1;
225
226 lose:
227 free(wd, M_TEMP);
228 return 0;
229 }
230
231 struct wdc_attach_args {
232 int wa_drive;
233 };
234
235 int
236 wdprint(aux, wdc)
237 void *aux;
238 char *wdc;
239 {
240 struct wdc_attach_args *wa = aux;
241
242 if (!wdc)
243 printf(" drive %d", wa->wa_drive);
244 return QUIET;
245 }
246
247 void
248 wdcattach(parent, self, aux)
249 struct device *parent, *self;
250 void *aux;
251 {
252 struct wdc_softc *wdc = (void *)self;
253 struct isa_attach_args *ia = aux;
254 struct wdc_attach_args wa;
255
256 printf("\n");
257
258 for (wa.wa_drive = 0; wa.wa_drive < 2; wa.wa_drive++)
259 (void)config_found(self, &wa, wdprint);
260
261 TAILQ_INIT(&wdc->sc_drives);
262 wdctimeout(wdc);
263 wdc->sc_ih.ih_fun = wdcintr;
264 wdc->sc_ih.ih_arg = wdc;
265 wdc->sc_ih.ih_level = IPL_BIO;
266 intr_establish(ia->ia_irq, &wdc->sc_ih);
267 }
268
269 int
270 wdprobe(parent, self, aux)
271 struct device *parent, *self;
272 void *aux;
273 {
274 struct wd_softc *wd = (void *)self;
275 struct cfdata *cf = wd->sc_dev.dv_cfdata;
276 struct wdc_attach_args *wa = aux;
277 int drive = wa->wa_drive;
278 #ifdef NEWCONFIG
279
280 #define cf_drive cf_loc[0]
281 if (cf->cf_drive != -1 && cf->cf_drive != drive)
282 return 0;
283 #undef cf_drive
284 #else
285 struct isa_device *id = (void *)cf->cf_loc;
286
287 if (id->id_physid != -1 && id->id_physid != drive)
288 return 0;
289 #endif
290
291 wd->sc_drive = drive;
292
293 if (wdgetctlr(wd) != 0)
294 return 0;
295
296 return 1;
297 }
298
299 void
300 wdattach(parent, self, aux)
301 struct device *parent, *self;
302 void *aux;
303 {
304 struct wd_softc *wd = (void *)self;
305 int i, blank;
306
307 if (wd->sc_params.wdp_heads == 0)
308 printf(": (unknown size) <");
309 else
310 printf(": %dMB %d cyl, %d head, %d sec <",
311 wd->sc_label.d_ncylinders * wd->sc_label.d_secpercyl /
312 (1048576 / DEV_BSIZE),
313 wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
314 wd->sc_label.d_nsectors);
315 for (i = blank = 0; i < sizeof(wd->sc_params.wdp_model); i++) {
316 char c = wd->sc_params.wdp_model[i];
317 if (c == '\0')
318 break;
319 if (c != ' ') {
320 if (blank)
321 printf(" %c", c);
322 else
323 printf("%c", c);
324 blank = 0;
325 } else
326 blank = 1;
327 }
328 printf(">\n");
329 }
330
331 /*
332 * Read/write routine for a buffer. Finds the proper unit, range checks
333 * arguments, and schedules the transfer. Does not wait for the transfer to
334 * complete. Multi-page transfers are supported. All I/O requests must be a
335 * multiple of a sector in length.
336 */
337 void
338 wdstrategy(bp)
339 struct buf *bp;
340 {
341 struct wd_softc *wd; /* disk unit to do the IO */
342 int lunit = WDUNIT(bp->b_dev);
343 int s;
344
345 /* Valid unit, controller, and request? */
346 if (lunit >= wdcd.cd_ndevs ||
347 (wd = wdcd.cd_devs[lunit]) == 0 ||
348 bp->b_blkno < 0 ||
349 (bp->b_bcount % DEV_BSIZE) != 0 ||
350 (bp->b_bcount / DEV_BSIZE) >= (1 << NBBY)) {
351 bp->b_error = EINVAL;
352 bp->b_flags |= B_ERROR;
353 goto done;
354 }
355
356 /* "Soft" write protect check. */
357 if ((wd->sc_flags & WDF_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
358 bp->b_error = EROFS;
359 bp->b_flags |= B_ERROR;
360 goto done;
361 }
362
363 /* Have partitions and want to use them? */
364 if ((wd->sc_flags & WDF_BSDLABEL) != 0 &&
365 WDPART(bp->b_dev) != RAW_PART) {
366 /*
367 * Do bounds checking, adjust transfer. if error, process.
368 * If end of partition, just return.
369 */
370 if (bounds_check_with_label(bp, &wd->sc_label,
371 wd->sc_wlabel) <= 0)
372 goto done;
373 /* Otherwise, process transfer request. */
374 }
375
376 /* Don't bother doing rotational optimization. */
377 bp->b_cylin = 0;
378
379 /* Queue transfer on drive, activate drive and controller if idle. */
380 s = splbio();
381 wddisksort(&wd->sc_q, bp);
382 if (!wd->sc_q.b_active)
383 wdstart(wd); /* Start drive. */
384 #ifdef DIAGNOSTIC
385 else {
386 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
387 if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
388 printf("wdstrategy: controller inactive\n");
389 wdcstart(wdc);
390 }
391 }
392 #endif
393 splx(s);
394 return;
395
396 done:
397 /* Toss transfer; we're done early. */
398 biodone(bp);
399 }
400
401 /*
402 * Need to skip over multitransfer bufs.
403 */
404 void
405 wddisksort(dp, bp)
406 struct buf *dp, *bp;
407 {
408 struct buf *ap;
409
410 while ((ap = dp->b_actf) && ap->b_flags & B_XXX)
411 dp = ap;
412 disksort(dp, bp);
413 }
414
415 /*
416 * Routine to queue a command to the controller. The unit's request is linked
417 * into the active list for the controller. If the controller is idle, the
418 * transfer is started.
419 */
420 static void
421 wdstart(wd)
422 struct wd_softc *wd;
423 {
424 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
425 int active = wdc->sc_drives.tqh_first != 0;
426
427 /* Link onto controller queue. */
428 wd->sc_q.b_active = 1;
429 TAILQ_INSERT_TAIL(&wdc->sc_drives, wd, sc_drivechain);
430
431 /* If controller not already active, start it. */
432 if (!active)
433 wdcstart(wdc);
434 }
435
436 void
437 wdfinish(wd, bp)
438 struct wd_softc *wd;
439 struct buf *bp;
440 {
441 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
442
443 #ifdef INSTRUMENT
444 dk_busy &= ~(1 << wd->sc_drive);
445 #endif
446 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR);
447 wdc->sc_errors = 0;
448 /*
449 * If this is the only buf or the last buf in the transfer (taking into
450 * account any residual, in case we erred)...
451 */
452 wd->sc_mbcount -= wd->sc_bcount;
453 if (wd->sc_mbcount == 0) {
454 wd->sc_mskip = 0;
455 /*
456 * ...then move this drive to the end of the queue to give
457 * others a `fair' chance.
458 */
459 if (wd->sc_drivechain.tqe_next) {
460 TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
461 if (bp->b_actf) {
462 TAILQ_INSERT_TAIL(&wdc->sc_drives, wd,
463 sc_drivechain);
464 } else
465 wd->sc_q.b_active = 0;
466 }
467 }
468 bp->b_resid = wd->sc_bcount;
469 bp->b_flags &= ~B_XXX;
470 wd->sc_skip = 0;
471 wd->sc_q.b_actf = bp->b_actf;
472 biodone(bp);
473 }
474
475 /*
476 * Controller startup routine. This does the calculation, and starts a
477 * single-sector read or write operation. Called to start a transfer, or from
478 * the interrupt routine to continue a multi-sector transfer.
479 * RESTRICTIONS:
480 * 1. The transfer length must be an exact multiple of the sector size.
481 */
482 static void
483 wdcstart(wdc)
484 struct wdc_softc *wdc;
485 {
486 struct wd_softc *wd; /* disk unit for IO */
487 struct buf *bp;
488 struct disklabel *lp;
489 long blknum, cylin, head, sector;
490 long secpertrk, secpercyl;
491 int xfrblknum;
492
493 loop:
494 /* Is there a drive for the controller to do a transfer with? */
495 wd = wdc->sc_drives.tqh_first;
496 if (wd == NULL)
497 return;
498
499 /* Is there a transfer to this drive? If not, deactivate drive. */
500 bp = wd->sc_q.b_actf;
501 if (bp == NULL) {
502 TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
503 wd->sc_q.b_active = 0;
504 goto loop;
505 }
506
507 if (wdc->sc_errors >= WDIORETRIES) {
508 wderror(wd, bp, "hard error");
509 bp->b_error = EIO;
510 bp->b_flags |= B_ERROR;
511 wdfinish(wd, bp);
512 goto loop;
513 }
514
515 /* Mark the controller active and set a timeout. */
516 wdc->sc_flags |= WDCF_ACTIVE;
517 wdc->sc_timeout = 4;
518
519 /* Do control operations specially. */
520 if (wd->sc_state < OPEN) {
521 /*
522 * Actually, we want to be careful not to mess with the control
523 * state if the device is currently busy, but we can assume
524 * that we never get to this point if that's the case.
525 */
526 (void) wdcontrol(wd);
527 return;
528 }
529
530 /*
531 * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
532 * encountered. If we are in multi-sector mode, then we switch to
533 * single-sector mode and retry the operation from the start.
534 */
535 if (wdc->sc_flags & WDCF_ERROR) {
536 wdc->sc_flags &= ~WDCF_ERROR;
537 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
538 wdc->sc_flags |= WDCF_SINGLE;
539 wd->sc_skip = 0;
540 wd->sc_mskip = 0;
541 }
542 }
543
544 /* Calculate transfer details. */
545 blknum = bp->b_blkno + wd->sc_skip;
546 #ifdef WDDEBUG
547 if (wd->sc_skip == 0)
548 printf("\nwdcstart %s: %s %d@%d; map ", wd->sc_dev.dv_xname,
549 (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
550 blknum);
551 else
552 printf(" %d)%x", wd->sc_skip, inb(wd->sc_iobase+wd_altsts));
553 #endif
554 if (wd->sc_skip == 0)
555 wd->sc_bcount = bp->b_bcount;
556 if (wd->sc_mskip == 0) {
557 struct buf *oldbp, *nextbp;
558 oldbp = bp;
559 nextbp = bp->b_actf;
560 wd->sc_mbcount = wd->sc_bcount;
561 oldbp->b_flags |= B_XXX;
562 while (nextbp && oldbp->b_dev == nextbp->b_dev &&
563 nextbp->b_blkno ==
564 (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
565 (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
566 if ((wd->sc_mbcount + nextbp->b_bcount) / DEV_BSIZE >= 240)
567 break;
568 wd->sc_mbcount += nextbp->b_bcount;
569 nextbp->b_flags |= B_XXX;
570 oldbp = nextbp;
571 nextbp = nextbp->b_actf;
572 }
573 }
574
575 lp = &wd->sc_label;
576 secpertrk = lp->d_nsectors;
577 secpercyl = lp->d_secpercyl;
578 if ((wd->sc_flags & WDF_BSDLABEL) != 0 &&
579 WDPART(bp->b_dev) != RAW_PART)
580 blknum += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
581 cylin = blknum / secpercyl;
582 head = (blknum % secpercyl) / secpertrk;
583 sector = blknum % secpertrk;
584
585 /*
586 * Check for bad sectors if we have them, and not formatting. Only do
587 * this in single-sector mode, or when starting a multiple-sector
588 * transfer.
589 */
590 if ((wd->sc_flags & WDF_BADSECT) &&
591 #ifdef B_FORMAT
592 (bp->b_flags & B_FORMAT) == 0 &&
593 #endif
594 (wd->sc_mskip == 0 || (wdc->sc_flags & WDCF_SINGLE))) {
595
596 long blkchk, blkend, blknew;
597 int i;
598
599 blkend = blknum + howmany(wd->sc_mbcount, DEV_BSIZE) - 1;
600 for (i = 0; (blkchk = wd->sc_badsect[i]) != -1; i++) {
601 if (blkchk > blkend)
602 break; /* Transfer is completely OK; done. */
603 if (blkchk == blknum) {
604 blknew =
605 lp->d_secperunit - lp->d_nsectors - i - 1;
606 cylin = blknew / secpercyl;
607 head = (blknew % secpercyl) / secpertrk;
608 sector = blknew % secpertrk;
609 wdc->sc_flags |= WDCF_SINGLE;
610 /* Found and replaced first blk of transfer; done. */
611 break;
612 } else if (blkchk > blknum) {
613 wdc->sc_flags |= WDCF_SINGLE;
614 break; /* Bad block inside transfer; done. */
615 }
616 }
617 }
618 if (wdc->sc_flags & WDCF_SINGLE) {
619 wd->sc_mbcount = wd->sc_bcount;
620 wd->sc_mskip = wd->sc_skip;
621 }
622
623 #ifdef WDDEBUG
624 printf("c%d h%d s%d ", cylin, head, sector);
625 #endif
626
627 sector++; /* Sectors begin with 1, not 0. */
628
629 #ifdef INSTRUMENT
630 if (wd->sc_skip == 0) {
631 dk_busy |= 1 << wd->sc_dev.dv_unit;
632 dk_wds[wd->sc_dev.dv_unit] += bp->b_bcount >> 6;
633 }
634 #endif
635
636 /* If starting a multisector transfer, or doing single transfers. */
637 if (wd->sc_mskip == 0 || (wdc->sc_flags & WDCF_SINGLE)) {
638 int command, count;
639
640 #ifdef INSTRUMENT
641 ++dk_seek[wd->sc_dev.dv_unit];
642 ++dk_xfer[wd->sc_dev.dv_unit];
643 #endif
644
645 #ifdef B_FORMAT
646 if (bp->b_flags & B_FORMAT) {
647 sector = lp->d_gap3;
648 count = lp->d_nsectors;
649 command = WDCC_FORMAT;
650 } else {
651 if (wdc->sc_flags & WDCF_SINGLE)
652 count = 1;
653 else
654 count = howmany(wd->sc_mbcount, DEV_BSIZE);
655 command =
656 (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
657 }
658 #else
659 if (wdc->sc_flags & WDCF_SINGLE)
660 count = 1;
661 else
662 count = howmany(wd->sc_mbcount, DEV_BSIZE);
663 command = (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
664 #endif
665
666 /* Initiate command! */
667 if (wdcommand(wd, cylin, head, sector, count, command) != 0) {
668 wderror(wd, NULL,
669 "wdcstart: timeout waiting for unbusy");
670 wdcunwedge(wdc);
671 return;
672 }
673 #ifdef WDDEBUG
674 printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
675 cylin, head, bp->b_data, inb(wd->sc_iobase+wd_altsts));
676 #endif
677 }
678
679 /* If this is a read operation, just go away until it's done. */
680 if (bp->b_flags & B_READ)
681 return;
682
683 if (wait_for_drq(wdc) < 0) {
684 wderror(wd, NULL, "wdcstart: timeout waiting for drq");
685 wdcunwedge(wdc);
686 return;
687 }
688
689 /* Then send it! */
690 outsw(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip * DEV_BSIZE,
691 DEV_BSIZE / sizeof(short));
692 }
693
694 /*
695 * Interrupt routine for the controller. Acknowledge the interrupt, check for
696 * errors on the current operation, mark it done if necessary, and start the
697 * next request. Also check for a partially done transfer, and continue with
698 * the next chunk if so.
699 */
700 int
701 wdcintr(wdc)
702 struct wdc_softc *wdc;
703 {
704 struct wd_softc *wd;
705 struct buf *bp;
706
707 if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
708 /* Clear the pending interrupt. */
709 (void) inb(wdc->sc_iobase+wd_status);
710 return 0;
711 }
712
713 wd = wdc->sc_drives.tqh_first;
714 bp = wd->sc_q.b_actf;
715
716 #ifdef WDDEBUG
717 printf("I%d ", ctrlr);
718 #endif
719
720 if (wait_for_unbusy(wdc) < 0) {
721 wderror(wd, NULL, "wdcintr: timeout waiting for unbusy");
722 wdc->sc_status |= WDCS_ERR; /* XXX */
723 }
724
725 /* Is it not a transfer, but a control operation? */
726 if (wd->sc_state < OPEN) {
727 if (wdcontrol(wd))
728 wdcstart(wdc);
729 return 1;
730 }
731
732 wdc->sc_flags &= ~WDCF_ACTIVE;
733 wdc->sc_timeout = 0;
734
735 /* Have we an error? */
736 if (wdc->sc_status & (WDCS_ERR | WDCS_ECCCOR)) {
737 lose:
738 #ifdef WDDEBUG
739 wderror(wd, NULL, "wdcintr");
740 #endif
741 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
742 wdc->sc_flags |= WDCF_ERROR;
743 goto restart;
744 }
745 #ifdef B_FORMAT
746 if (bp->b_flags & B_FORMAT) {
747 bp->b_error = EIO;
748 bp->b_flags |= B_ERROR;
749 goto done;
750 }
751 #endif
752
753 /* Error or error correction? */
754 if (wdc->sc_status & WDCS_ERR) {
755 if (++wdc->sc_errors >= WDIORETRIES) {
756 wderror(wd, bp, "hard error");
757 bp->b_error = EIO;
758 bp->b_flags |= B_ERROR; /* Flag the error. */
759 goto done;
760 } else
761 goto restart;
762 } else
763 wderror(wd, bp, "soft ecc");
764 }
765
766 /* If this was a read, fetch the data. */
767 if (bp->b_flags & B_READ) {
768 /* Ready to receive data? */
769 if ((wdc->sc_status & (WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ))
770 != (WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ)) {
771 wderror(wd, NULL, "wdcintr: read intr before drq");
772 wdcunwedge(wdc);
773 return 1;
774 }
775
776 /* Suck in data. */
777 insw(wdc->sc_iobase+wd_data,
778 bp->b_data + wd->sc_skip * DEV_BSIZE,
779 DEV_BSIZE / sizeof(short));
780 }
781
782 /* If we encountered any abnormalities, flag it as a soft error. */
783 if (wdc->sc_errors) {
784 wderror(wd, bp, "soft error");
785 wdc->sc_errors = 0;
786 }
787
788 /* Ready for the next block, if any. */
789 wd->sc_skip++;
790 wd->sc_mskip++;
791 wd->sc_bcount -= DEV_BSIZE;
792 wd->sc_mbcount -= DEV_BSIZE;
793
794 /* See if more to transfer. */
795 if (wd->sc_bcount > 0)
796 goto restart;
797
798 done:
799 /* Done with this transfer, with or without error. */
800 wdfinish(wd, bp);
801
802 restart:
803 /* Start the next transfer, if any. */
804 wdcstart(wdc);
805
806 return 1;
807 }
808
809 /*
810 * Initialize a drive.
811 */
812 int
813 wdopen(dev, flag, fmt, p)
814 dev_t dev;
815 int flag;
816 int fmt;
817 struct proc *p;
818 {
819 int lunit;
820 struct wd_softc *wd;
821 int part = WDPART(dev), mask = 1 << part;
822 struct partition *pp;
823 char *msg;
824
825 lunit = WDUNIT(dev);
826 if (lunit >= wdcd.cd_ndevs)
827 return ENXIO;
828 wd = wdcd.cd_devs[lunit];
829 if (wd == 0)
830 return ENXIO;
831
832 if ((wd->sc_flags & WDF_BSDLABEL) == 0) {
833 wd->sc_flags |= WDF_WRITEPROT;
834 wd->sc_q.b_actf = NULL;
835
836 /*
837 * Use the default sizes until we've read the label, or longer
838 * if there isn't one there.
839 */
840 bzero(&wd->sc_label, sizeof(wd->sc_label));
841 wd->sc_label.d_type = DTYPE_ST506;
842 wd->sc_label.d_ncylinders = 1024;
843 wd->sc_label.d_secsize = DEV_BSIZE;
844 wd->sc_label.d_ntracks = 8;
845 wd->sc_label.d_nsectors = 17;
846 wd->sc_label.d_secpercyl = 17*8;
847 wd->sc_label.d_secperunit = 17*8*1024;
848 wd->sc_state = RECAL;
849
850 /* Read label using "raw" partition. */
851 msg = readdisklabel(WDLABELDEV(dev), wdstrategy, &wd->sc_label,
852 &wd->sc_cpulabel);
853 if (msg) {
854 /*
855 * This probably happened because the drive's default
856 * geometry doesn't match the DOS geometry. We
857 * assume the DOS geometry is now in the label and try
858 * again. XXX This is a kluge.
859 */
860 if (wd->sc_state > GEOMETRY)
861 wd->sc_state = GEOMETRY;
862 msg = readdisklabel(WDLABELDEV(dev), wdstrategy,
863 &wd->sc_label, &wd->sc_cpulabel);
864 }
865 if (msg) {
866 log(LOG_WARNING, "%s: cannot find label (%s)\n",
867 wd->sc_dev.dv_xname, msg);
868 if (part != RAW_PART)
869 return EINVAL; /* XXX needs translation */
870 } else {
871 if (wd->sc_state > GEOMETRY)
872 wd->sc_state = GEOMETRY;
873 wd->sc_flags |= WDF_BSDLABEL;
874 wd->sc_flags &= ~WDF_WRITEPROT;
875 if (wd->sc_label.d_flags & D_BADSECT)
876 wd->sc_flags |= WDF_BADSECT;
877 }
878 }
879
880 if (wd->sc_flags & WDF_BADSECT)
881 bad144intern(wd);
882
883 /*
884 * Warn if a partition is opened that overlaps another partition which
885 * is open unless one is the "raw" partition (whole disk).
886 */
887 if ((wd->sc_openpart & mask) == 0 && part != RAW_PART) {
888 int start, end;
889
890 pp = &wd->sc_label.d_partitions[part];
891 start = pp->p_offset;
892 end = pp->p_offset + pp->p_size;
893 for (pp = wd->sc_label.d_partitions;
894 pp < &wd->sc_label.d_partitions[wd->sc_label.d_npartitions];
895 pp++) {
896 if (pp->p_offset + pp->p_size <= start ||
897 pp->p_offset >= end)
898 continue;
899 if (pp - wd->sc_label.d_partitions == RAW_PART)
900 continue;
901 if (wd->sc_openpart & (1 << (pp - wd->sc_label.d_partitions)))
902 log(LOG_WARNING,
903 "%s%c: overlaps open partition (%c)\n",
904 wd->sc_dev.dv_xname, part + 'a',
905 pp - wd->sc_label.d_partitions + 'a');
906 }
907 }
908 if (part >= wd->sc_label.d_npartitions && part != RAW_PART)
909 return ENXIO;
910
911 /* Insure only one open at a time. */
912 switch (fmt) {
913 case S_IFCHR:
914 wd->sc_copenpart |= mask;
915 break;
916 case S_IFBLK:
917 wd->sc_bopenpart |= mask;
918 break;
919 }
920 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
921
922 return 0;
923 }
924
925 /*
926 * Implement operations other than read/write.
927 * Called from wdcstart or wdcintr during opens and formats.
928 * Uses finite-state-machine to track progress of operation in progress.
929 * Returns 0 if operation still in progress, 1 if completed.
930 */
931 static int
932 wdcontrol(wd)
933 struct wd_softc *wd;
934 {
935 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
936
937 switch (wd->sc_state) {
938 case RECAL: /* Set SDH, step rate, do restore. */
939 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0) {
940 wderror(wd, NULL, "wdcontrol: wdcommand failed");
941 wdcunwedge(wdc);
942 return 0;
943 }
944 wd->sc_state = RECAL_WAIT;
945 return 0;
946
947 case RECAL_WAIT:
948 if (wdc->sc_status & WDCS_ERR) {
949 wderror(wd, NULL, "wdcontrol: recal failed");
950 wdcunwedge(wdc);
951 return 0;
952 }
953 /* fall through */
954 case GEOMETRY:
955 if (wdsetctlr(wd) != 0) {
956 /* Already printed a message. */
957 wdcunwedge(wdc);
958 return 0;
959 }
960 wd->sc_state = GEOMETRY_WAIT;
961 return 0;
962
963 case GEOMETRY_WAIT:
964 if (wdc->sc_status & WDCS_ERR) {
965 wderror(wd, NULL, "wdcontrol: geometry failed");
966 wdcunwedge(wdc);
967 return 0;
968 }
969
970 wdc->sc_errors = 0;
971 wd->sc_state = OPEN;
972 /*
973 * The rest of the initialization can be done by normal means.
974 */
975 return 1;
976 }
977
978 #ifdef DIAGNOSTIC
979 panic("wdcontrol: impossible");
980 #endif
981 }
982
983 /*
984 * Send a command and wait uninterruptibly until controller is finished.
985 * Return -1 if controller busy for too long, otherwise return non-zero if
986 * error. Intended for brief controller commands at critical points.
987 * Assumes interrupts are blocked.
988 */
989 static int
990 wdcommand(wd, cylin, head, sector, count, cmd)
991 struct wd_softc *wd;
992 int cylin, head, sector, count;
993 int cmd;
994 {
995 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
996 int stat;
997 u_short iobase;
998
999 /* Select drive. */
1000 iobase = wdc->sc_iobase;
1001 outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_drive << 4) | head);
1002
1003 /* Wait for it to become ready to accept a command. */
1004 if (cmd == WDCC_DIAGNOSE || cmd == WDCC_IDC)
1005 stat = wait_for_unbusy(wdc);
1006 else
1007 stat = wdcwait(wdc, WDCS_READY);
1008 if (stat < 0)
1009 return -1;
1010
1011 /* Load parameters. */
1012 outb(iobase+wd_precomp, wd->sc_label.d_precompcyl / 4);
1013 outb(iobase+wd_cyl_lo, cylin);
1014 outb(iobase+wd_cyl_hi, cylin >> 8);
1015 outb(iobase+wd_sector, sector);
1016 outb(iobase+wd_seccnt, count);
1017
1018 /* Send command, await results. */
1019 outb(iobase+wd_command, cmd);
1020
1021 return 0;
1022 }
1023
1024 /*
1025 * Issue IDC to drive to tell it just what geometry it is to be.
1026 */
1027 static int
1028 wdsetctlr(wd)
1029 struct wd_softc *wd;
1030 {
1031 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1032
1033 #ifdef WDDEBUG
1034 printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_ctrlr, wd->sc_drive,
1035 wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
1036 wd->sc_label.d_nsectors);
1037 #endif
1038
1039 if (wdcommand(wd, wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks - 1,
1040 0, wd->sc_label.d_nsectors, WDCC_IDC) != 0) {
1041 wderror(wd, NULL, "wdsetctlr: wdcommand failed");
1042 return -1;
1043 }
1044
1045 return 0;
1046 }
1047
1048 /*
1049 * Issue READP to drive to ask it what it is.
1050 */
1051 static int
1052 wdgetctlr(wd)
1053 struct wd_softc *wd;
1054 {
1055 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1056 int i;
1057 char tb[DEV_BSIZE];
1058 struct wdparams *wp;
1059
1060 if (wdcommand(wd, 0, 0, 0, 0, WDCC_READP) != 0 ||
1061 wait_for_drq(wdc) != 0) {
1062 /*
1063 * If WDCC_READP fails then we might have an old drive so we
1064 * try a seek to 0; if that passes then the drive is there but
1065 * it's OLD AND KRUSTY.
1066 */
1067 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
1068 wait_for_ready(wdc) != 0)
1069 return -1;
1070
1071 strncpy(wd->sc_label.d_typename, "ST506",
1072 sizeof wd->sc_label.d_typename);
1073 strncpy(wd->sc_params.wdp_model, "Unknown Type",
1074 sizeof wd->sc_params.wdp_model);
1075 wd->sc_label.d_type = DTYPE_ST506;
1076 } else {
1077 /* Obtain parameters. */
1078 wp = &wd->sc_params;
1079 insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
1080 bcopy(tb, wp, sizeof(struct wdparams));
1081
1082 /* Shuffle string byte order. */
1083 for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
1084 u_short *p;
1085 p = (u_short *)(wp->wdp_model + i);
1086 *p = ntohs(*p);
1087 }
1088
1089 strncpy(wd->sc_label.d_typename, "ESDI/IDE",
1090 sizeof wd->sc_label.d_typename);
1091 wd->sc_label.d_type = DTYPE_ESDI;
1092 bcopy(wp->wdp_model+20, wd->sc_label.d_packname, 14-1);
1093
1094 /* Update disklabel given drive information. */
1095 wd->sc_label.d_ncylinders =
1096 wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
1097 wd->sc_label.d_ntracks = wp->wdp_heads;
1098 wd->sc_label.d_nsectors = wp->wdp_sectors;
1099 wd->sc_label.d_secpercyl =
1100 wd->sc_label.d_ntracks * wd->sc_label.d_nsectors;
1101 wd->sc_label.d_partitions[1].p_size =
1102 wd->sc_label.d_secpercyl * wp->wdp_sectors;
1103 wd->sc_label.d_partitions[1].p_offset = 0;
1104 }
1105
1106 #if 0
1107 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1108 wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1109 wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1110 #endif
1111
1112 wd->sc_label.d_subtype |= DSTYPE_GEOMETRY;
1113
1114 /* XXX sometimes possibly needed */
1115 (void) inb(wdc->sc_iobase+wd_status);
1116
1117 return 0;
1118 }
1119
1120 int
1121 wdclose(dev, flag, fmt)
1122 dev_t dev;
1123 int flag;
1124 int fmt;
1125 {
1126 struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1127 int part = WDPART(dev), mask = 1 << part;
1128
1129 switch (fmt) {
1130 case S_IFCHR:
1131 wd->sc_copenpart &= ~mask;
1132 break;
1133 case S_IFBLK:
1134 wd->sc_bopenpart &= ~mask;
1135 break;
1136 }
1137 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
1138
1139 return 0;
1140 }
1141
1142 int
1143 wdioctl(dev, cmd, addr, flag, p)
1144 dev_t dev;
1145 int cmd;
1146 caddr_t addr;
1147 int flag;
1148 struct proc *p;
1149 {
1150 int lunit = WDUNIT(dev);
1151 struct wd_softc *wd = wdcd.cd_devs[lunit];
1152 int error;
1153
1154 switch (cmd) {
1155 case DIOCSBAD:
1156 if ((flag & FWRITE) == 0)
1157 return EBADF;
1158 wd->sc_cpulabel.bad = *(struct dkbad *)addr;
1159 bad144intern(wd);
1160 return 0;
1161
1162 case DIOCGDINFO:
1163 *(struct disklabel *)addr = wd->sc_label;
1164 return 0;
1165
1166 case DIOCGPART:
1167 ((struct partinfo *)addr)->disklab = &wd->sc_label;
1168 ((struct partinfo *)addr)->part =
1169 &wd->sc_label.d_partitions[WDPART(dev)];
1170 return 0;
1171
1172 case DIOCSDINFO:
1173 if ((flag & FWRITE) == 0)
1174 return EBADF;
1175 error = setdisklabel(&wd->sc_label,
1176 (struct disklabel *)addr,
1177 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart : */0,
1178 &wd->sc_cpulabel);
1179 if (error == 0) {
1180 wd->sc_flags |= WDF_BSDLABEL;
1181 if (wd->sc_state > GEOMETRY)
1182 wd->sc_state = GEOMETRY;
1183 }
1184 return error;
1185
1186 case DIOCWLABEL:
1187 wd->sc_flags &= ~WDF_WRITEPROT;
1188 if ((flag & FWRITE) == 0)
1189 return EBADF;
1190 wd->sc_wlabel = *(int *)addr;
1191 return 0;
1192
1193 case DIOCWDINFO:
1194 wd->sc_flags &= ~WDF_WRITEPROT;
1195 if ((flag & FWRITE) == 0)
1196 return EBADF;
1197 error = setdisklabel(&wd->sc_label,
1198 (struct disklabel *)addr,
1199 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart :*/0,
1200 &wd->sc_cpulabel);
1201 if (error == 0) {
1202 int wlab;
1203
1204 wd->sc_flags |= WDF_BSDLABEL;
1205 if (wd->sc_state > GEOMETRY)
1206 wd->sc_state = GEOMETRY;
1207
1208 /* Simulate opening partition 0 so write succeeds. */
1209 wd->sc_openpart |= (1 << 0); /* XXX */
1210 wlab = wd->sc_wlabel;
1211 wd->sc_wlabel = 1;
1212 error = writedisklabel(WDLABELDEV(dev), wdstrategy,
1213 &wd->sc_label, &wd->sc_cpulabel);
1214 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
1215 wd->sc_wlabel = wlab;
1216 }
1217 return error;
1218
1219 #ifdef notyet
1220 case DIOCGDINFOP:
1221 *(struct disklabel **)addr = &wd->sc_label;
1222 return 0;
1223
1224 case DIOCWFORMAT:
1225 if ((flag & FWRITE) == 0)
1226 return EBADF;
1227 {
1228 register struct format_op *fop;
1229 struct iovec aiov;
1230 struct uio auio;
1231
1232 fop = (struct format_op *)addr;
1233 aiov.iov_base = fop->df_buf;
1234 aiov.iov_len = fop->df_count;
1235 auio.uio_iov = &aiov;
1236 auio.uio_iovcnt = 1;
1237 auio.uio_resid = fop->df_count;
1238 auio.uio_segflg = 0;
1239 auio.uio_offset =
1240 fop->df_startblk * wd->sc_label.d_secsize;
1241 auio.uio_procp = p;
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, RAW_PART), 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 pt_entry_t *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 *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(arg)
1493 void *arg;
1494 {
1495 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1496 int s = splbio();
1497
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_errors;
1529
1530 /* Wake up in a little bit and restart the operation. */
1531 timeout(wdcrestart, 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 extern int cold;
1543
1544 for (;;) {
1545 wdc->sc_status = status = inb(iobase+wd_status);
1546 if ((status & WDCS_BUSY) == 0 && (status & mask) == mask)
1547 break;
1548 if (++timeout > WDCNDELAY)
1549 return -1;
1550 delay(WDCDELAY);
1551 }
1552 if (status & WDCS_ERR) {
1553 wdc->sc_error = inb(iobase+wd_error);
1554 return WDCS_ERR;
1555 }
1556 #ifdef WDCNDELAY_DEBUG
1557 /* After autoconfig, there should be no long delays. */
1558 if (!cold && timeout > WDCNDELAY_DEBUG)
1559 printf("%s: warning: busy-wait took %dus\n",
1560 wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1561 #endif
1562 return 0;
1563 }
1564
1565 static void
1566 wdctimeout(arg)
1567 void *arg;
1568 {
1569 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1570 int s = splbio();
1571
1572 if (wdc->sc_timeout && --wdc->sc_timeout == 0) {
1573 wderror(wdc, NULL, "lost interrupt");
1574 wdcunwedge(wdc);
1575 }
1576 timeout(wdctimeout, wdc, hz);
1577 splx(s);
1578 }
1579
1580 static void
1581 wderror(dev, bp, msg)
1582 void *dev;
1583 struct buf *bp;
1584 char *msg;
1585 {
1586 struct wd_softc *wd = dev;
1587 struct wdc_softc *wdc = dev;
1588
1589 if (bp)
1590 diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip, &wd->sc_label);
1591 else
1592 printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
1593 msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
1594 }
1595