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