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