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