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