wd.c revision 1.77 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.77 1994/04/09 03:43:36 mycroft Exp $
39 */
40
41 #define INSTRUMENT /* instrumentation stuff by Brad Parker */
42
43 #include <sys/param.h>
44 #include <sys/dkbad.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/conf.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/ioctl.h>
51 #include <sys/disklabel.h>
52 #include <sys/buf.h>
53 #include <sys/uio.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56 #include <sys/syslog.h>
57 #ifdef INSTRUMENT
58 #include <sys/dkstat.h>
59 #endif
60
61 #include <vm/vm.h>
62
63 #include <machine/cpu.h>
64 #include <machine/cpufunc.h>
65 #include <machine/pio.h>
66
67 #include <i386/isa/isa.h>
68 #ifndef NEWCONFIG
69 #include <i386/isa/isa_device.h>
70 #endif
71 #include <i386/isa/isavar.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 0
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_drive; /* physical unit number */
115 char sc_state; /* control state */
116
117 u_long sc_copenpart; /* character units open on this drive */
118 u_long sc_bopenpart; /* block units open on this drive */
119 u_long sc_openpart; /* all units open on this drive */
120 short sc_wlabel; /* label writable? */
121 short sc_flags; /* drive characteistics found */
122 #define WDF_BSDLABEL 0x00010 /* has a BSD disk label */
123 #define WDF_BADSECT 0x00020 /* has a bad144 badsector table */
124 #define WDF_WRITEPROT 0x00040 /* manual unit write protect */
125 struct buf sc_q;
126 struct wdparams sc_params; /* ESDI/IDE drive/controller parameters */
127 struct disklabel sc_label; /* device configuration data */
128 struct cpu_disklabel sc_cpulabel;
129 long sc_badsect[127]; /* 126 plus trailing -1 marker */
130 };
131
132 struct wdc_softc {
133 struct device sc_dev;
134 struct intrhand sc_ih;
135
136 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 };
145
146 int wdcprobe(), wdprobe(), wdcintr();
147 void wdcattach(), wdattach();
148
149 struct cfdriver wdccd = {
150 NULL, "wdc", wdcprobe, wdcattach, DV_DULL, sizeof(struct wd_softc)
151 };
152
153 struct cfdriver wdcd = {
154 NULL, "wd", wdprobe, wdattach, DV_DISK, sizeof(struct wd_softc)
155 };
156
157 void wdfinish __P((struct wd_softc *, struct buf *));
158 static void wdstart __P((struct wd_softc *));
159 static void wdcstart __P((struct wdc_softc *));
160 static int wdcommand __P((struct wd_softc *, int, int, int, int, int));
161 static int wdcontrol __P((struct wd_softc *));
162 static int wdsetctlr __P((struct wd_softc *));
163 static int wdgetctlr __P((struct wd_softc *));
164 static void bad144intern __P((struct wd_softc *));
165 static int wdcreset __P((struct wdc_softc *));
166 static void wdcrestart __P((struct wdc_softc *));
167 static void wdcunwedge __P((struct wdc_softc *));
168 static void wdctimeout __P((struct wdc_softc *));
169 void wddisksort __P((struct buf *, struct buf *));
170 static void wderror __P((void *, struct buf *, char *));
171 int wdcwait __P((struct wdc_softc *, int));
172 #define wait_for_drq(d) wdcwait(d, WDCS_DRQ)
173 #define wait_for_ready(d) wdcwait(d, WDCS_READY | WDCS_SEEKCMPLT)
174 #define wait_for_unbusy(d) wdcwait(d, 0)
175
176 /*
177 * Probe for controller.
178 */
179 int
180 wdcprobe(parent, self, aux)
181 struct device *parent, *self;
182 void *aux;
183 {
184 struct wdc_softc *wdc = (void *)self;
185 struct isa_attach_args *ia = aux;
186 struct wd_softc *wd;
187 u_short iobase;
188
189 wdc->sc_iobase = iobase = ia->ia_iobase;
190
191 /* Check if we have registers that work. */
192 outb(iobase+wd_error, 0x5a); /* Error register not writable. */
193 outb(iobase+wd_cyl_lo, 0xa5); /* But all of cyllo are implemented. */
194 if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
195 return 0;
196
197 if (wdcreset(wdc) != 0) {
198 delay(500000);
199 if (wdcreset(wdc) != 0)
200 return 0;
201 }
202
203 /*
204 * XXX wdcommand() accepts a wd_softc, so we have to make it one.
205 */
206 wd = (void *)malloc(sizeof(struct wd_softc), M_TEMP, M_NOWAIT);
207 bzero(wd, sizeof(struct wd_softc));
208 wd->sc_drive = 0;
209 wd->sc_dev.dv_unit = 0;
210 wd->sc_dev.dv_parent = (void *)wdc;
211
212 /* Execute a controller only command. */
213 if (wdcommand(wd, 0, 0, 0, 0, WDCC_DIAGNOSE) != 0 ||
214 wait_for_unbusy(wdc) != 0)
215 goto lose;
216
217 free(wd, M_TEMP);
218 ia->ia_iosize = 8;
219 ia->ia_msize = 0;
220 return 1;
221
222 lose:
223 free(wd, M_TEMP);
224 return 0;
225 }
226
227 struct wdc_attach_args {
228 int wa_drive;
229 };
230
231 int
232 wdprint(aux, wdc)
233 void *aux;
234 char *wdc;
235 {
236 struct wdc_attach_args *wa = aux;
237
238 if (!wdc)
239 printf(" drive %d", wa->wa_drive);
240 return QUIET;
241 }
242
243 void
244 wdcattach(parent, self, aux)
245 struct device *parent, *self;
246 void *aux;
247 {
248 struct wdc_softc *wdc = (void *)self;
249 struct isa_attach_args *ia = aux;
250 struct wdc_attach_args wa;
251
252 printf("\n");
253
254 for (wa.wa_drive = 0; wa.wa_drive < 2; wa.wa_drive++)
255 (void)config_found(self, &wa, wdprint);
256
257 wdctimeout(wdc);
258 wdc->sc_ih.ih_fun = wdcintr;
259 wdc->sc_ih.ih_arg = wdc;
260 wdc->sc_ih.ih_level = IPL_BIO;
261 intr_establish(ia->ia_irq, &wdc->sc_ih);
262 }
263
264 int
265 wdprobe(parent, self, aux)
266 struct device *parent, *self;
267 void *aux;
268 {
269 struct wd_softc *wd = (void *)self;
270 struct cfdata *cf = wd->sc_dev.dv_cfdata;
271 struct wdc_attach_args *wa = aux;
272 int drive = wa->wa_drive;
273 #ifdef NEWCONFIG
274
275 #define cf_drive cf_loc[0]
276 if (cf->cf_drive != -1 && cf->cf_drive != drive)
277 return 0;
278 #undef cf_drive
279 #else
280 struct isa_device *id = (void *)cf->cf_loc;
281
282 if (id->id_physid != -1 && id->id_physid != drive)
283 return 0;
284 #endif
285
286 wd->sc_drive = drive;
287
288 if (wdgetctlr(wd) != 0)
289 return 0;
290
291 return 1;
292 }
293
294 void
295 wdattach(parent, self, aux)
296 struct device *parent, *self;
297 void *aux;
298 {
299 struct wd_softc *wd = (void *)self;
300 int i, blank;
301
302 if (wd->sc_params.wdp_heads == 0)
303 printf(": (unknown size) <");
304 else
305 printf(": %dMB %d cyl, %d head, %d sec <",
306 wd->sc_label.d_ncylinders * wd->sc_label.d_secpercyl /
307 (1048576 / DEV_BSIZE),
308 wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
309 wd->sc_label.d_nsectors);
310 for (i = blank = 0; i < sizeof(wd->sc_params.wdp_model); i++) {
311 char c = wd->sc_params.wdp_model[i];
312 if (c == '\0')
313 break;
314 if (c != ' ') {
315 if (blank)
316 printf(" %c", c);
317 else
318 printf("%c", c);
319 blank = 0;
320 } else
321 blank = 1;
322 }
323 printf(">\n");
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 >= wdcd.cd_ndevs || bp->b_blkno < 0 ||
344 howmany(bp->b_bcount, DEV_BSIZE) >= (1 << NBBY) ||
345 (wd = wdcd.cd_devs[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_drive);
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 = wdcd.cd_devs[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 wdcintr() 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_dev.dv_unit;
637 dk_wds[wd->sc_dev.dv_unit] += 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_dev.dv_unit];
647 ++dk_xfer[wd->sc_dev.dv_unit];
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 wdcintr(wdc)
707 struct wdc_softc *wdc;
708 {
709 struct wd_softc *wd;
710 struct buf *bp;
711
712 /* Clear the pending interrupt. */
713 (void) inb(wdc->sc_iobase+wd_status);
714
715 if (!wdc->sc_q.b_active)
716 return 0;
717
718 bp = wdc->sc_q.b_forw->b_actf;
719 wd = wdcd.cd_devs[WDUNIT(bp->b_dev)];
720 wdc->sc_timeout = 0;
721
722 #ifdef WDDEBUG
723 printf("I%d ", ctrlr);
724 #endif
725
726 if (wait_for_unbusy(wdc) < 0) {
727 wderror(wd, NULL, "wdcintr: timeout waiting for unbusy");
728 wdc->sc_status |= WDCS_ERR; /* XXX */
729 }
730
731 /* Is it not a transfer, but a control operation? */
732 if (wd->sc_state < OPEN) {
733 if (wdcontrol(wd))
734 wdcstart(wdc);
735 return 1;
736 }
737
738 /* Have we an error? */
739 if (wdc->sc_status & (WDCS_ERR | WDCS_ECCCOR)) {
740 lose:
741 #ifdef WDDEBUG
742 wderror(wd, NULL, "wdcintr");
743 #endif
744 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
745 wdc->sc_flags |= WDCF_ERROR;
746 goto restart;
747 }
748 #ifdef B_FORMAT
749 if (bp->b_flags & B_FORMAT) {
750 bp->b_error = EIO;
751 bp->b_flags |= B_ERROR;
752 goto done;
753 }
754 #endif
755
756 /* Error or error correction? */
757 if (wdc->sc_status & WDCS_ERR) {
758 if (++wdc->sc_q.b_errcnt >= WDIORETRIES) {
759 wderror(wd, bp, "hard error");
760 bp->b_error = EIO;
761 bp->b_flags |= B_ERROR; /* Flag the error. */
762 goto done;
763 } else
764 goto restart;
765 } else
766 wderror(wd, bp, "soft ecc");
767 }
768
769 /* If this was a read, fetch the data. */
770 if (bp->b_flags & B_READ) {
771 /* Ready to receive data? */
772 if (wait_for_drq(wdc) != 0) {
773 wderror(wd, NULL, "wdcintr: read error detected late");
774 wdcunwedge(wdc);
775 return 1;
776 }
777
778 /* Suck in data. */
779 insw(wdc->sc_iobase+wd_data,
780 bp->b_un.b_addr + wd->sc_skip * DEV_BSIZE,
781 DEV_BSIZE / sizeof(short));
782 }
783
784 /* If we encountered any abnormalities, flag it as a soft error. */
785 if (wdc->sc_q.b_errcnt) {
786 wderror(wd, bp, "soft error");
787 wdc->sc_q.b_errcnt = 0;
788 }
789
790 /* Ready for the next block, if any. */
791 wd->sc_skip++;
792 wd->sc_mskip++;
793 wd->sc_bcount -= DEV_BSIZE;
794 wd->sc_mbcount -= DEV_BSIZE;
795
796 /* See if more to transfer. */
797 if (wd->sc_bcount > 0)
798 goto restart;
799
800 done:
801 /* Done with this transfer, with or without error. */
802 wdfinish(wd, bp);
803
804 restart:
805 /* Start the next transfer, if any. */
806 wdcstart(wdc);
807
808 return 1;
809 }
810
811 /*
812 * Initialize a drive.
813 */
814 int
815 wdopen(dev, flag, fmt, p)
816 dev_t dev;
817 int flag;
818 int fmt;
819 struct proc *p;
820 {
821 int lunit;
822 struct wd_softc *wd;
823 int part = WDPART(dev), mask = 1 << part;
824 struct partition *pp;
825 char *msg;
826
827 lunit = WDUNIT(dev);
828 if (lunit >= wdcd.cd_ndevs)
829 return ENXIO;
830 wd = wdcd.cd_devs[lunit];
831 if (wd == 0)
832 return ENXIO;
833
834 if ((wd->sc_flags & WDF_BSDLABEL) == 0) {
835 wd->sc_flags |= WDF_WRITEPROT;
836 wd->sc_q.b_actf = NULL;
837
838 /*
839 * Use the default sizes until we've read the label, or longer
840 * if there isn't one there.
841 */
842 bzero(&wd->sc_label, sizeof(wd->sc_label));
843 wd->sc_label.d_type = DTYPE_ST506;
844 wd->sc_label.d_ncylinders = 1024;
845 wd->sc_label.d_secsize = DEV_BSIZE;
846 wd->sc_label.d_ntracks = 8;
847 wd->sc_label.d_nsectors = 17;
848 wd->sc_label.d_secpercyl = 17*8;
849 wd->sc_label.d_secperunit = 17*8*1024;
850 wd->sc_state = RECAL;
851
852 /* Read label using "raw" partition. */
853 msg = readdisklabel(makewddev(major(dev), WDUNIT(dev), WDRAW),
854 wdstrategy, &wd->sc_label, &wd->sc_cpulabel);
855 if (msg) {
856 /*
857 * This probably happened because the drive's default
858 * geometry doesn't match the DOS geometry. We
859 * assume the DOS geometry is now in the label and try
860 * again. XXX This is a kluge.
861 */
862 if (wd->sc_state > GEOMETRY)
863 wd->sc_state = GEOMETRY;
864 msg = readdisklabel(makewddev(major(dev), WDUNIT(dev),
865 WDRAW), wdstrategy, &wd->sc_label,
866 &wd->sc_cpulabel);
867 }
868 if (msg) {
869 log(LOG_WARNING, "%s: cannot find label (%s)\n",
870 wd->sc_dev.dv_xname, msg);
871 if (part != WDRAW)
872 return EINVAL; /* XXX needs translation */
873 } else {
874 if (wd->sc_state > GEOMETRY)
875 wd->sc_state = GEOMETRY;
876 wd->sc_flags |= WDF_BSDLABEL;
877 wd->sc_flags &= ~WDF_WRITEPROT;
878 if (wd->sc_label.d_flags & D_BADSECT)
879 wd->sc_flags |= WDF_BADSECT;
880 }
881 }
882
883 if (wd->sc_flags & WDF_BADSECT)
884 bad144intern(wd);
885
886 /*
887 * Warn if a partition is opened that overlaps another partition which
888 * is open unless one is the "raw" partition (whole disk).
889 */
890 if ((wd->sc_openpart & mask) == 0 && part != WDRAW) {
891 int start, end;
892
893 pp = &wd->sc_label.d_partitions[part];
894 start = pp->p_offset;
895 end = pp->p_offset + pp->p_size;
896 for (pp = wd->sc_label.d_partitions;
897 pp < &wd->sc_label.d_partitions[wd->sc_label.d_npartitions];
898 pp++) {
899 if (pp->p_offset + pp->p_size <= start ||
900 pp->p_offset >= end)
901 continue;
902 if (pp - wd->sc_label.d_partitions == WDRAW)
903 continue;
904 if (wd->sc_openpart & (1 << (pp - wd->sc_label.d_partitions)))
905 log(LOG_WARNING,
906 "%s%c: overlaps open partition (%c)\n",
907 wd->sc_dev.dv_xname, part + 'a',
908 pp - wd->sc_label.d_partitions + 'a');
909 }
910 }
911 if (part >= wd->sc_label.d_npartitions && part != WDRAW)
912 return ENXIO;
913
914 /* Insure only one open at a time. */
915 switch (fmt) {
916 case S_IFCHR:
917 wd->sc_copenpart |= mask;
918 break;
919 case S_IFBLK:
920 wd->sc_bopenpart |= mask;
921 break;
922 }
923 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
924
925 return 0;
926 }
927
928 /*
929 * Implement operations other than read/write.
930 * Called from wdcstart or wdcintr during opens and formats.
931 * Uses finite-state-machine to track progress of operation in progress.
932 * Returns 0 if operation still in progress, 1 if completed.
933 */
934 static int
935 wdcontrol(wd)
936 struct wd_softc *wd;
937 {
938 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
939
940 switch (wd->sc_state) {
941 case RECAL: /* Set SDH, step rate, do restore. */
942 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0) {
943 wderror(wd, NULL, "wdcontrol: wdcommand failed");
944 wdcunwedge(wdc);
945 return 0;
946 }
947 wd->sc_state = RECAL_WAIT;
948 return 0;
949
950 case RECAL_WAIT:
951 if (wdc->sc_status & WDCS_ERR) {
952 wderror(wd, NULL, "wdcontrol: recal failed");
953 wdcunwedge(wdc);
954 return 0;
955 }
956 /* fall through */
957 case GEOMETRY:
958 if (wdsetctlr(wd) != 0) {
959 /* Already printed a message. */
960 wdcunwedge(wdc);
961 return 0;
962 }
963 wd->sc_state = GEOMETRY_WAIT;
964 return 0;
965
966 case GEOMETRY_WAIT:
967 if (wdc->sc_status & WDCS_ERR) {
968 wderror(wd, NULL, "wdcontrol: geometry failed");
969 wdcunwedge(wdc);
970 return 0;
971 }
972
973 wdc->sc_q.b_errcnt = 0;
974 wd->sc_state = OPEN;
975 /*
976 * The rest of the initialization can be done by normal means.
977 */
978 return 1;
979 }
980
981 #ifdef DIAGNOSTIC
982 panic("wdcontrol: impossible");
983 #endif
984 }
985
986 /*
987 * Send a command and wait uninterruptibly until controller is finished.
988 * Return -1 if controller busy for too long, otherwise return non-zero if
989 * error. Intended for brief controller commands at critical points.
990 * Assumes interrupts are blocked.
991 */
992 static int
993 wdcommand(wd, cylin, head, sector, count, cmd)
994 struct wd_softc *wd;
995 int cylin, head, sector, count;
996 int cmd;
997 {
998 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
999 int stat;
1000 u_short iobase;
1001
1002 /* Controller ready for command? */
1003 if (wait_for_unbusy(wdc) < 0)
1004 return -1;
1005
1006 /* Select drive. */
1007 iobase = wdc->sc_iobase;
1008 outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_drive << 4) | head);
1009 if (cmd == WDCC_DIAGNOSE || cmd == WDCC_IDC)
1010 stat = wait_for_unbusy(wdc);
1011 else
1012 stat = wdcwait(wdc, WDCS_READY);
1013 if (stat < 0)
1014 return -1;
1015
1016 /* Load parameters. */
1017 outb(iobase+wd_precomp, wd->sc_label.d_precompcyl / 4);
1018 outb(iobase+wd_cyl_lo, cylin);
1019 outb(iobase+wd_cyl_hi, cylin >> 8);
1020 outb(iobase+wd_sector, sector);
1021 outb(iobase+wd_seccnt, count);
1022
1023 /* Send command, await results. */
1024 outb(iobase+wd_command, cmd);
1025
1026 return 0;
1027 }
1028
1029 /*
1030 * Issue IDC to drive to tell it just what geometry it is to be.
1031 */
1032 static int
1033 wdsetctlr(wd)
1034 struct wd_softc *wd;
1035 {
1036 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1037
1038 #ifdef WDDEBUG
1039 printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_ctrlr, wd->sc_drive,
1040 wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks,
1041 wd->sc_label.d_nsectors);
1042 #endif
1043
1044 if (wdcommand(wd, wd->sc_label.d_ncylinders, wd->sc_label.d_ntracks - 1,
1045 0, wd->sc_label.d_nsectors, WDCC_IDC) != 0) {
1046 wderror(wd, NULL, "wdsetctlr: wdcommand failed");
1047 return -1;
1048 }
1049
1050 return 0;
1051 }
1052
1053 /*
1054 * Issue READP to drive to ask it what it is.
1055 */
1056 static int
1057 wdgetctlr(wd)
1058 struct wd_softc *wd;
1059 {
1060 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1061 int i;
1062 char tb[DEV_BSIZE];
1063 struct wdparams *wp;
1064
1065 if (wdcommand(wd, 0, 0, 0, 0, WDCC_READP) != 0 ||
1066 wait_for_drq(wdc) != 0) {
1067 /*
1068 * If WDCC_READP fails then we might have an old drive so we
1069 * try a seek to 0; if that passes then the drive is there but
1070 * it's OLD AND KRUSTY.
1071 */
1072 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
1073 wait_for_ready(wdc) != 0)
1074 return -1;
1075
1076 strncpy(wd->sc_label.d_typename, "ST506",
1077 sizeof wd->sc_label.d_typename);
1078 strncpy(wd->sc_params.wdp_model, "Unknown Type",
1079 sizeof wd->sc_params.wdp_model);
1080 wd->sc_label.d_type = DTYPE_ST506;
1081 } else {
1082 /* Obtain parameters. */
1083 wp = &wd->sc_params;
1084 insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
1085 bcopy(tb, wp, sizeof(struct wdparams));
1086
1087 /* Shuffle string byte order. */
1088 for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
1089 u_short *p;
1090 p = (u_short *)(wp->wdp_model + i);
1091 *p = ntohs(*p);
1092 }
1093
1094 strncpy(wd->sc_label.d_typename, "ESDI/IDE",
1095 sizeof wd->sc_label.d_typename);
1096 wd->sc_label.d_type = DTYPE_ESDI;
1097 bcopy(wp->wdp_model+20, wd->sc_label.d_packname, 14-1);
1098
1099 /* Update disklabel given drive information. */
1100 wd->sc_label.d_ncylinders =
1101 wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
1102 wd->sc_label.d_ntracks = wp->wdp_heads;
1103 wd->sc_label.d_nsectors = wp->wdp_sectors;
1104 wd->sc_label.d_secpercyl =
1105 wd->sc_label.d_ntracks * wd->sc_label.d_nsectors;
1106 wd->sc_label.d_partitions[1].p_size =
1107 wd->sc_label.d_secpercyl * wp->wdp_sectors;
1108 wd->sc_label.d_partitions[1].p_offset = 0;
1109 }
1110
1111 #if 0
1112 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1113 wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1114 wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1115 #endif
1116
1117 wd->sc_label.d_subtype |= DSTYPE_GEOMETRY;
1118
1119 /* XXX sometimes possibly needed */
1120 (void) inb(wdc->sc_iobase+wd_status);
1121
1122 return 0;
1123 }
1124
1125 int
1126 wdclose(dev, flag, fmt)
1127 dev_t dev;
1128 int flag;
1129 int fmt;
1130 {
1131 struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1132 int part = WDPART(dev), mask = 1 << part;
1133
1134 switch (fmt) {
1135 case S_IFCHR:
1136 wd->sc_copenpart &= ~mask;
1137 break;
1138 case S_IFBLK:
1139 wd->sc_bopenpart &= ~mask;
1140 break;
1141 }
1142 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
1143
1144 return 0;
1145 }
1146
1147 int
1148 wdioctl(dev, cmd, addr, flag, p)
1149 dev_t dev;
1150 int cmd;
1151 caddr_t addr;
1152 int flag;
1153 struct proc *p;
1154 {
1155 int lunit = WDUNIT(dev);
1156 struct wd_softc *wd = wdcd.cd_devs[lunit];
1157 int error;
1158
1159 switch (cmd) {
1160 case DIOCSBAD:
1161 if ((flag & FWRITE) == 0)
1162 return EBADF;
1163 wd->sc_cpulabel.bad = *(struct dkbad *)addr;
1164 bad144intern(wd);
1165 return 0;
1166
1167 case DIOCGDINFO:
1168 *(struct disklabel *)addr = wd->sc_label;
1169 return 0;
1170
1171 case DIOCGPART:
1172 ((struct partinfo *)addr)->disklab = &wd->sc_label;
1173 ((struct partinfo *)addr)->part =
1174 &wd->sc_label.d_partitions[WDPART(dev)];
1175 return 0;
1176
1177 case DIOCSDINFO:
1178 if ((flag & FWRITE) == 0)
1179 return EBADF;
1180 error = setdisklabel(&wd->sc_label,
1181 (struct disklabel *)addr,
1182 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart : */0,
1183 &wd->sc_cpulabel);
1184 if (error == 0) {
1185 wd->sc_flags |= WDF_BSDLABEL;
1186 if (wd->sc_state > GEOMETRY)
1187 wd->sc_state = GEOMETRY;
1188 }
1189 return error;
1190
1191 case DIOCWLABEL:
1192 wd->sc_flags &= ~WDF_WRITEPROT;
1193 if ((flag & FWRITE) == 0)
1194 return EBADF;
1195 wd->sc_wlabel = *(int *)addr;
1196 return 0;
1197
1198 case DIOCWDINFO:
1199 wd->sc_flags &= ~WDF_WRITEPROT;
1200 if ((flag & FWRITE) == 0)
1201 return EBADF;
1202 error = setdisklabel(&wd->sc_label,
1203 (struct disklabel *)addr,
1204 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_openpart :*/0,
1205 &wd->sc_cpulabel);
1206 if (error == 0) {
1207 int wlab;
1208
1209 wd->sc_flags |= WDF_BSDLABEL;
1210 if (wd->sc_state > GEOMETRY)
1211 wd->sc_state = GEOMETRY;
1212
1213 /* Simulate opening partition 0 so write succeeds. */
1214 wd->sc_openpart |= (1 << 0); /* XXX */
1215 wlab = wd->sc_wlabel;
1216 wd->sc_wlabel = 1;
1217 error = writedisklabel(dev, wdstrategy, &wd->sc_label,
1218 &wd->sc_cpulabel);
1219 wd->sc_openpart = wd->sc_copenpart | wd->sc_bopenpart;
1220 wd->sc_wlabel = wlab;
1221 }
1222 return error;
1223
1224 #ifdef notyet
1225 case DIOCGDINFOP:
1226 *(struct disklabel **)addr = &wd->sc_label;
1227 return 0;
1228
1229 case DIOCWFORMAT:
1230 if ((flag & FWRITE) == 0)
1231 return EBADF;
1232 {
1233 register struct format_op *fop;
1234 struct iovec aiov;
1235 struct uio auio;
1236
1237 fop = (struct format_op *)addr;
1238 aiov.iov_base = fop->df_buf;
1239 aiov.iov_len = fop->df_count;
1240 auio.uio_iov = &aiov;
1241 auio.uio_iovcnt = 1;
1242 auio.uio_resid = fop->df_count;
1243 auio.uio_segflg = 0;
1244 auio.uio_offset =
1245 fop->df_startblk * wd->sc_label.d_secsize;
1246 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1247 &auio);
1248 fop->df_count -= auio.uio_resid;
1249 fop->df_reg[0] = wdc->sc_status;
1250 fop->df_reg[1] = wdc->sc_error;
1251 return error;
1252 }
1253 #endif
1254
1255 default:
1256 return ENOTTY;
1257 }
1258
1259 #ifdef DIAGNOSTIC
1260 panic("wdioctl: impossible");
1261 #endif
1262 }
1263
1264 #ifdef B_FORMAT
1265 int
1266 wdformat(struct buf *bp)
1267 {
1268
1269 bp->b_flags |= B_FORMAT;
1270 return wdstrategy(bp);
1271 }
1272 #endif
1273
1274 int
1275 wdsize(dev)
1276 dev_t dev;
1277 {
1278 int lunit = WDUNIT(dev), part = WDPART(dev);
1279 struct wd_softc *wd;
1280
1281 if (lunit >= wdcd.cd_ndevs)
1282 return -1;
1283 wd = wdcd.cd_devs[lunit];
1284 if (wd == 0)
1285 return -1;
1286
1287 if (wd->sc_state < OPEN || (wd->sc_flags & WDF_BSDLABEL) == 0) {
1288 int val;
1289 val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
1290 S_IFBLK, 0);
1291 /* XXX Clear the open flag? */
1292 if (val != 0)
1293 return -1;
1294 }
1295
1296 if ((wd->sc_flags & (WDF_WRITEPROT | WDF_BSDLABEL)) != WDF_BSDLABEL)
1297 return -1;
1298
1299 return (int)wd->sc_label.d_partitions[part].p_size;
1300 }
1301
1302 /*
1303 * Dump core after a system crash.
1304 */
1305 int
1306 wddump(dev)
1307 dev_t dev;
1308 {
1309 struct wd_softc *wd; /* disk unit to do the IO */
1310 struct wdc_softc *wdc;
1311 long num; /* number of sectors to write */
1312 int lunit, part;
1313 long blkoff, blknum;
1314 long cylin, head, sector;
1315 long secpertrk, secpercyl, nblocks;
1316 char *addr;
1317 static wddoingadump = 0;
1318 extern caddr_t CADDR1;
1319 extern struct pte *CMAP1;
1320
1321 addr = (char *)0; /* starting address */
1322
1323 #if DO_NOT_KNOW_HOW
1324 /* Toss any characters present prior to dump, ie. non-blocking getc. */
1325 while (cngetc())
1326 ;
1327 #endif
1328
1329 lunit = WDUNIT(dev);
1330 /* Check for acceptable drive number. */
1331 if (lunit >= wdcd.cd_ndevs)
1332 return ENXIO;
1333 wd = wdcd.cd_devs[lunit];
1334 /* Was it ever initialized? */
1335 if (wd == 0 || wd->sc_state < OPEN || wd->sc_flags & WDF_WRITEPROT)
1336 return ENXIO;
1337
1338 wdc = (void *)wd->sc_dev.dv_parent;
1339
1340 /* Convert to disk sectors. */
1341 num = ctob(physmem) / wd->sc_label.d_secsize;
1342
1343 secpertrk = wd->sc_label.d_nsectors;
1344 secpercyl = wd->sc_label.d_secpercyl;
1345 part = WDPART(dev);
1346 nblocks = wd->sc_label.d_partitions[part].p_size;
1347 blkoff = wd->sc_label.d_partitions[part].p_offset;
1348
1349 /*printf("part %x, nblocks %d, dumplo %d, num %d\n", part, nblocks,
1350 dumplo, num);*/
1351
1352 /* Check transfer bounds against partition size. */
1353 if (dumplo < 0 || dumplo + num > nblocks)
1354 return EINVAL;
1355
1356 if (wddoingadump)
1357 return EFAULT;
1358 wddoingadump = 1;
1359
1360 /* Recalibrate. */
1361 if (wdcommand(wd, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
1362 wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0 ||
1363 wait_for_ready(wdc) != 0) {
1364 wderror(wd, NULL, "wddump: recal failed");
1365 return EIO;
1366 }
1367
1368 blknum = dumplo + blkoff;
1369 while (num > 0) {
1370 /* Compute disk address. */
1371 cylin = blknum / secpercyl;
1372 head = (blknum % secpercyl) / secpertrk;
1373 sector = blknum % secpertrk;
1374
1375 if (wd->sc_flags & WDF_BADSECT) {
1376 long newblk;
1377 int i;
1378
1379 for (i = 0; wd->sc_badsect[i] != -1; i++) {
1380 if (blknum < wd->sc_badsect[i]) {
1381 /* Sorted list, passed our block by. */
1382 break;
1383 } else if (blknum == wd->sc_badsect[i]) {
1384 newblk = wd->sc_label.d_secperunit -
1385 wd->sc_label.d_nsectors - i - 1;
1386 cylin = newblk / secpercyl;
1387 head = (newblk % secpercyl) / secpertrk;
1388 sector = newblk % secpertrk;
1389 /* Found and replaced; done. */
1390 break;
1391 }
1392 }
1393 }
1394 sector++; /* origin 1 */
1395
1396 #ifdef notdef
1397 /* Let's just talk about this first. */
1398 printf("cylin %d, head %d, sector %d, addr 0x%x", cylin, head,
1399 sector, addr);
1400 #endif
1401 if (wdcommand(wd, cylin, head, sector, 1, WDCC_WRITE) != 0) {
1402 wderror(wd, NULL, "wddump: wdcommand failed");
1403 return EIO;
1404 }
1405 if (wait_for_drq(wdc) != 0) {
1406 wderror(wd, NULL, "wddump: timeout waiting for drq");
1407 return EIO;
1408 }
1409
1410 #ifdef notdef /* Cannot use this since this address was mapped differently. */
1411 pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1412 #else
1413 *(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
1414 tlbflush();
1415 #endif
1416
1417 outsw(wdc->sc_iobase+wd_data, CADDR1 + ((int)addr & PGOFSET),
1418 DEV_BSIZE / sizeof(short));
1419
1420 /* Check data request (should be done). */
1421 if (wait_for_ready(wdc) != 0) {
1422 wderror(wd, NULL, "wddump: timeout waiting for ready");
1423 return EIO;
1424 }
1425 if (wdc->sc_status & WDCS_DRQ) {
1426 wderror(wd, NULL, "wddump: extra drq");
1427 return EIO;
1428 }
1429
1430 if ((unsigned)addr % 1048576 == 0)
1431 printf("%d ", num / (1048576 / DEV_BSIZE));
1432
1433 /* Update block count. */
1434 num--;
1435 blknum++;
1436 (int)addr += DEV_BSIZE;
1437
1438 #if DO_NOT_KNOW_HOW
1439 /* Operator aborting dump? non-blocking getc() */
1440 if (cngetc())
1441 return EINTR;
1442 #endif
1443 }
1444 return 0;
1445 }
1446
1447 /*
1448 * Internalize the bad sector table.
1449 */
1450 void
1451 bad144intern(wd)
1452 struct wd_softc *wd;
1453 {
1454 int i;
1455
1456 if ((wd->sc_flags & WDF_BADSECT) == 0)
1457 return;
1458
1459 for (i = 0; i < 127; i++)
1460 wd->sc_badsect[i] = -1;
1461 for (i = 0; i < 126; i++) {
1462 if (wd->sc_cpulabel.bad.bt_bad[i].bt_cyl == 0xffff)
1463 break;
1464 wd->sc_badsect[i] =
1465 wd->sc_cpulabel.bad.bt_bad[i].bt_cyl *
1466 wd->sc_label.d_secpercyl +
1467 (wd->sc_cpulabel.bad.bt_bad[i].bt_trksec >> 8) *
1468 wd->sc_label.d_nsectors +
1469 (wd->sc_cpulabel.bad.bt_bad[i].bt_trksec & 0x00ff);
1470 }
1471 }
1472
1473 static int
1474 wdcreset(wdc)
1475 struct wdc_softc *wdc;
1476 {
1477 u_short iobase = wdc->sc_iobase;
1478
1479 /* Reset the device. */
1480 outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1481 delay(1000);
1482 outb(iobase+wd_ctlr, WDCTL_IDS);
1483 delay(1000);
1484 (void) inb(iobase+wd_error);
1485 outb(iobase+wd_ctlr, WDCTL_4BIT);
1486
1487 if (wait_for_unbusy(wdc) < 0) {
1488 printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1489 return 1;
1490 }
1491
1492 return 0;
1493 }
1494
1495 static void
1496 wdcrestart(wdc)
1497 struct wdc_softc *wdc;
1498 {
1499 int s = splbio();
1500
1501 wdc->sc_q.b_active = 0;
1502 wdcstart(wdc);
1503 splx(s);
1504 }
1505
1506 /*
1507 * Unwedge the controller after an unexpected error. We do this by resetting
1508 * it, marking all drives for recalibration, and stalling the queue for a short
1509 * period to give the reset time to finish.
1510 * NOTE: We use a timeout here, so this routine must not be called during
1511 * autoconfig or dump.
1512 */
1513 static void
1514 wdcunwedge(wdc)
1515 struct wdc_softc *wdc;
1516 {
1517 int lunit;
1518
1519 wdc->sc_timeout = 0;
1520 (void) wdcreset(wdc);
1521
1522 /* Schedule recalibrate for all drives on this controller. */
1523 for (lunit = 0; lunit < wdcd.cd_ndevs; lunit++) {
1524 struct wd_softc *wd = wdcd.cd_devs[lunit];
1525 if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
1526 continue;
1527 if (wd->sc_state > RECAL)
1528 wd->sc_state = RECAL;
1529 }
1530
1531 wdc->sc_flags |= WDCF_ERROR;
1532 ++wdc->sc_q.b_errcnt;
1533
1534 /* Wake up in a little bit and restart the operation. */
1535 timeout((timeout_t)wdcrestart, (caddr_t)wdc, RECOVERYTIME);
1536 }
1537
1538 int
1539 wdcwait(wdc, mask)
1540 struct wdc_softc *wdc;
1541 int mask;
1542 {
1543 u_short iobase = wdc->sc_iobase;
1544 int timeout = 0;
1545 u_char status;
1546
1547 for (;;) {
1548 wdc->sc_status = status = inb(iobase+wd_altsts);
1549 if ((status & WDCS_BUSY) == 0 && (status & mask) == mask)
1550 break;
1551 if (++timeout > WDCNDELAY)
1552 return -1;
1553 delay(WDCDELAY);
1554 }
1555 if (status & WDCS_ERR)
1556 wdc->sc_error = inb(iobase+wd_error);
1557 #ifdef WDCNDELAY_DEBUG
1558 if (timeout > WDCNDELAY_DEBUG)
1559 printf("%s: ignore this: busy-wait took %dus\n",
1560 wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1561 #endif
1562 return status & WDCS_ERR;
1563 }
1564
1565 static void
1566 wdctimeout(wdc)
1567 struct wdc_softc *wdc;
1568 {
1569 int s = splbio();
1570
1571 if (wdc->sc_timeout && --wdc->sc_timeout == 0) {
1572 wderror(wdc, NULL, "lost interrupt");
1573 wdcunwedge(wdc);
1574 }
1575 timeout((timeout_t)wdctimeout, (caddr_t)wdc, hz);
1576 splx(s);
1577 }
1578
1579 static void
1580 wderror(dev, bp, msg)
1581 void *dev;
1582 struct buf *bp;
1583 char *msg;
1584 {
1585 struct wd_softc *wd = dev;
1586 struct wdc_softc *wdc = dev;
1587
1588 if (bp)
1589 diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip, &wd->sc_label);
1590 else
1591 printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
1592 msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
1593 }
1594