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