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