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