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