wd.c revision 1.52 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.52 1994/03/02 22:07:00 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_FORMAT:
1030 case WDCC_RESTORE | WD_STEP:
1031 case WDCC_IDC:
1032 case WDCC_DIAGNOSE:
1033 #endif
1034 return wait_for_unbusy(du);
1035 case WDCC_READ:
1036 case WDCC_WRITE:
1037 return 0;
1038 case WDCC_READP:
1039 return wait_for_drq(du);
1040 }
1041 }
1042
1043 /*
1044 * issue IDC to drive to tell it just what geometry it is to be.
1045 */
1046 static int
1047 wdsetctlr(dev_t dev, struct disk *du)
1048 {
1049 int stat, x, wdc;
1050
1051 #ifdef WDDEBUG
1052 printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
1053 du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
1054 #endif
1055
1056 wdc = du->dk_port;
1057
1058 x = splbio();
1059 if (wait_for_unbusy(du) < 0)
1060 return -1;
1061 outb(wdc+wd_cyl_lo, du->dk_dd.d_ncylinders); /* TIH: was ...ders+1 */
1062 outb(wdc+wd_cyl_hi, du->dk_dd.d_ncylinders>>8); /* TIH: was ...ders+1 */
1063 outb(wdc+wd_sdh,
1064 WDSD_IBM | (du->dk_unit << 4) + du->dk_dd.d_ntracks - 1);
1065 outb(wdc+wd_seccnt, du->dk_dd.d_nsectors);
1066 if (wait_for_ready(du) < 0)
1067 return -1;
1068 stat = wdcommand(du, WDCC_IDC);
1069 if (stat < 0 || stat & WDCS_ERR)
1070 printf("wdsetctlr: stat %b error %b\n", stat, WDCS_BITS,
1071 inb(wdc+wd_error), WDERR_BITS);
1072 splx(x);
1073 return stat;
1074 }
1075
1076 /*
1077 * issue READP to drive to ask it what it is.
1078 */
1079 static int
1080 wdgetctlr(int u, struct disk *du)
1081 {
1082 int stat, x, i, wdc;
1083 char tb[DEV_BSIZE];
1084 struct wdparams *wp;
1085
1086 x = splbio(); /* not called from intr level ... */
1087 wdc = du->dk_port;
1088 if (wait_for_unbusy(du) < 0) {
1089 splx(x);
1090 return -1;
1091 }
1092
1093 outb(wdc+wd_sdh, WDSD_IBM | (u << 4));
1094 if (wait_for_ready(du) < 0) {
1095 splx(x);
1096 return -1;
1097 }
1098
1099 stat = wdcommand(du, WDCC_READP);
1100 if (stat < 0) {
1101 splx(x);
1102 return -1;
1103 }
1104
1105 if ((stat & WDCS_ERR) == 0) {
1106 /* obtain parameters */
1107 wp = &du->dk_params;
1108 insw(wdc+wd_data, tb, sizeof(tb)/sizeof(short));
1109 bcopy(tb, wp, sizeof(struct wdparams));
1110
1111 /* shuffle string byte order */
1112 for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
1113 u_short *p;
1114 p = (u_short *)(wp->wdp_model + i);
1115 *p = ntohs(*p);
1116 }
1117
1118 strncpy(du->dk_dd.d_typename, "ESDI/IDE",
1119 sizeof du->dk_dd.d_typename);
1120 du->dk_dd.d_type = DTYPE_ESDI;
1121 bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
1122
1123 /* update disklabel given drive information */
1124 du->dk_dd.d_ncylinders =
1125 wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
1126 du->dk_dd.d_ntracks = wp->wdp_heads;
1127 du->dk_dd.d_nsectors = wp->wdp_sectors;
1128 du->dk_dd.d_secpercyl =
1129 du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
1130 du->dk_dd.d_partitions[1].p_size =
1131 du->dk_dd.d_secpercyl * wp->wdp_sectors;
1132 du->dk_dd.d_partitions[1].p_offset = 0;
1133 } else {
1134 /*
1135 * If WDCC_READP fails then we might have an old drive
1136 * so we try a seek to 0; if that passes then the
1137 * drive is there but it's OLD AND KRUSTY.
1138 */
1139 stat = wdcommand(du, WDCC_RESTORE | WD_STEP);
1140 if (stat < 0 || stat & WDCS_ERR) {
1141 splx(x);
1142 return inb(wdc+wd_error);
1143 }
1144
1145 strncpy(du->dk_dd.d_typename, "ST506",
1146 sizeof du->dk_dd.d_typename);
1147 strncpy(du->dk_params.wdp_model, "Unknown Type",
1148 sizeof du->dk_params.wdp_model);
1149 du->dk_dd.d_type = DTYPE_ST506;
1150 }
1151
1152 #if 0
1153 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1154 wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1155 wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1156 #endif
1157
1158 /* better ... */
1159 du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
1160
1161 /* XXX sometimes possibly needed */
1162 (void) inb(wdc+wd_status);
1163 splx(x);
1164 return 0;
1165 }
1166
1167
1168 /* ARGSUSED */
1169 int
1170 wdclose(dev_t dev, int flags, int fmt)
1171 {
1172 register struct disk *du;
1173 int part = WDPART(dev), mask = 1 << part;
1174
1175 du = wddrives[WDUNIT(dev)];
1176
1177 /* insure only one open at a time */
1178 du->dk_openpart &= ~mask;
1179 switch (fmt) {
1180 case S_IFCHR:
1181 du->dk_copenpart &= ~mask;
1182 break;
1183 case S_IFBLK:
1184 du->dk_bopenpart &= ~mask;
1185 break;
1186 }
1187 return 0;
1188 }
1189
1190 int
1191 wdioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
1192 {
1193 int lunit = WDUNIT(dev);
1194 register struct disk *du;
1195 int error = 0;
1196 struct uio auio;
1197 struct iovec aiov;
1198
1199 du = wddrives[lunit];
1200
1201 switch (cmd) {
1202 case DIOCSBAD:
1203 if ((flag & FWRITE) == 0)
1204 error = EBADF;
1205 else {
1206 du->dk_cpd.bad = *(struct dkbad *)addr;
1207 bad144intern(du);
1208 }
1209 break;
1210
1211 case DIOCGDINFO:
1212 *(struct disklabel *)addr = du->dk_dd;
1213 break;
1214
1215 case DIOCGPART:
1216 ((struct partinfo *)addr)->disklab = &du->dk_dd;
1217 ((struct partinfo *)addr)->part =
1218 &du->dk_dd.d_partitions[WDPART(dev)];
1219 break;
1220
1221 case DIOCSDINFO:
1222 if ((flag & FWRITE) == 0)
1223 error = EBADF;
1224 else {
1225 error = setdisklabel(&du->dk_dd, (struct disklabel *)addr,
1226 /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart : */0,
1227 &du->dk_cpd);
1228 }
1229 if (error == 0) {
1230 du->dk_flags |= DKFL_BSDLABEL;
1231 wdsetctlr(dev, du);
1232 }
1233 break;
1234
1235 case DIOCWLABEL:
1236 du->dk_flags &= ~DKFL_WRITEPROT;
1237 if ((flag & FWRITE) == 0)
1238 error = EBADF;
1239 else
1240 du->dk_wlabel = *(int *)addr;
1241 break;
1242
1243 case DIOCWDINFO:
1244 du->dk_flags &= ~DKFL_WRITEPROT;
1245 if ((flag & FWRITE) == 0)
1246 error = EBADF;
1247 else if ((error = setdisklabel(&du->dk_dd,
1248 (struct disklabel *)addr,
1249 /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/0,
1250 &du->dk_cpd)) == 0) {
1251 int wlab;
1252
1253 du->dk_flags |= DKFL_BSDLABEL;
1254 wdsetctlr(dev, du);
1255
1256 /* simulate opening partition 0 so write succeeds */
1257 du->dk_openpart |= (1 << 0); /* XXX */
1258 wlab = du->dk_wlabel;
1259 du->dk_wlabel = 1;
1260 error = writedisklabel(dev, wdstrategy, &du->dk_dd, &du->dk_cpd);
1261 du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
1262 du->dk_wlabel = wlab;
1263 }
1264 break;
1265
1266 #ifdef notyet
1267 case DIOCGDINFOP:
1268 *(struct disklabel **)addr = &du->dk_dd;
1269 break;
1270
1271 case DIOCWFORMAT:
1272 if ((flag & FWRITE) == 0)
1273 error = EBADF;
1274 else {
1275 register struct format_op *fop;
1276
1277 fop = (struct format_op *)addr;
1278 aiov.iov_base = fop->df_buf;
1279 aiov.iov_len = fop->df_count;
1280 auio.uio_iov = &aiov;
1281 auio.uio_iovcnt = 1;
1282 auio.uio_resid = fop->df_count;
1283 auio.uio_segflg = 0;
1284 auio.uio_offset =
1285 fop->df_startblk * du->dk_dd.d_secsize;
1286 error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
1287 minphys, &auio);
1288 fop->df_count -= auio.uio_resid;
1289 fop->df_reg[0] = du->dk_status;
1290 fop->df_reg[1] = du->dk_error;
1291 }
1292 break;
1293 #endif
1294
1295 default:
1296 error = ENOTTY;
1297 break;
1298 }
1299 return error;
1300 }
1301
1302 #ifdef B_FORMAT
1303 int
1304 wdformat(struct buf *bp)
1305 {
1306
1307 bp->b_flags |= B_FORMAT;
1308 return wdstrategy(bp);
1309 }
1310 #endif
1311
1312 int
1313 wdsize(dev_t dev)
1314 {
1315 int lunit = WDUNIT(dev), part = WDPART(dev);
1316 struct disk *du;
1317
1318 if (lunit >= NWD)
1319 return -1;
1320 du = wddrives[lunit];
1321 if (du == 0)
1322 return -1;
1323
1324 if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
1325 int val;
1326 val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
1327 S_IFBLK, 0);
1328 if (val != 0)
1329 return -1;
1330 }
1331
1332 if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
1333 return -1;
1334
1335 return (int)du->dk_dd.d_partitions[part].p_size;
1336 }
1337
1338 extern char *vmmap; /* poor name! */
1339
1340 /* dump core after a system crash */
1341 int
1342 wddump(dev_t dev)
1343 {
1344 register struct disk *du; /* disk unit to do the IO */
1345 long num; /* number of sectors to write */
1346 int ctrlr, lunit, part, wdc;
1347 long blkoff, blknum;
1348 long cylin, head, sector, stat;
1349 long secpertrk, secpercyl, nblocks, i;
1350 char *addr;
1351 static wddoingadump = 0;
1352 extern caddr_t CADDR1;
1353 extern struct pte *CMAP1;
1354
1355 addr = (char *)0; /* starting address */
1356
1357 #if DO_NOT_KNOW_HOW
1358 /* toss any characters present prior to dump, ie. non-blocking getc */
1359 while (cngetc())
1360 ;
1361 #endif
1362
1363 /* size of memory to dump */
1364 lunit = WDUNIT(dev);
1365 part = WDPART(dev); /* file system */
1366 /* check for acceptable drive number */
1367 if (lunit >= NWD)
1368 return ENXIO;
1369 du = wddrives[lunit];
1370 /* was it ever initialized ? */
1371 if (du == 0 || du->dk_state < OPEN || du->dk_flags & DKFL_WRITEPROT)
1372 return ENXIO;
1373
1374 wdc = du->dk_port;
1375 ctrlr = du->dk_ctrlr;
1376
1377 /* Convert to disk sectors */
1378 num = ctob(physmem) / du->dk_dd.d_secsize;
1379
1380 /* check if controller active */
1381 /*if (wdtab[ctrlr].b_active)
1382 return EFAULT;*/
1383 if (wddoingadump)
1384 return EFAULT;
1385
1386 secpertrk = du->dk_dd.d_nsectors;
1387 secpercyl = du->dk_dd.d_secpercyl;
1388 nblocks = du->dk_dd.d_partitions[part].p_size;
1389 blkoff = du->dk_dd.d_partitions[part].p_offset;
1390
1391 /*printf("xunit %x, nblocks %d, dumplo %d num %d\n", part, nblocks,
1392 dumplo, num);*/
1393 /* check transfer bounds against partition size */
1394 if (dumplo < 0 || dumplo + num > nblocks)
1395 return EINVAL;
1396
1397 /* mark controller active for if we panic during the dump */
1398 /*wdtab[ctrlr].b_active = 1;*/
1399 wddoingadump = 1;
1400 i = 200000000;
1401 if (wait_for_unbusy(du) < 0)
1402 return EIO;
1403 outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
1404 if (wait_for_ready(du) < 0)
1405 return EIO;
1406 if (wdcommand(du, WDCC_RESTORE | WD_STEP) < 0)
1407 return EIO;
1408
1409 /* some compaq controllers require this ... */
1410 wdsetctlr(dev, du);
1411
1412 blknum = dumplo + blkoff;
1413 while (num > 0) {
1414 /* compute disk address */
1415 cylin = blknum / secpercyl;
1416 head = (blknum % secpercyl) / secpertrk;
1417 sector = blknum % secpertrk;
1418
1419 if (du->dk_flags & DKFL_BADSECT) {
1420 long newblk;
1421 int i;
1422
1423 for (i = 0; du->dk_badsect[i] != -1; i++) {
1424 if (blknum < du->dk_badsect[i]) {
1425 /* sorted list, passed our block by */
1426 break;
1427 } else if (blknum == du->dk_badsect[i]) {
1428 newblk = du->dk_dd.d_secperunit -
1429 du->dk_dd.d_nsectors - i - 1;
1430 cylin = newblk / secpercyl;
1431 head = (newblk % secpercyl) / secpertrk;
1432 sector = newblk % secpertrk;
1433 /* found and replaced; done */
1434 break;
1435 }
1436 }
1437 }
1438 sector++; /* origin 1 */
1439
1440 if (wait_for_unbusy(du) < 0)
1441 return EIO;
1442 /* select drive. */
1443 outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | (head & 0xf));
1444 if (wait_for_ready(du) < 0)
1445 return EIO;
1446
1447 /* transfer some blocks */
1448 outb(wdc+wd_sector, sector);
1449 outb(wdc+wd_seccnt, 1);
1450 outb(wdc+wd_cyl_lo, cylin);
1451 outb(wdc+wd_cyl_hi, cylin >> 8);
1452 #ifdef notdef
1453 /* lets just talk about this first...*/
1454 printf("sdh 0%o sector %d cyl %d addr 0x%x", inb(wdc+wd_sdh),
1455 inb(wdc+wd_sector),
1456 (inb(wdc+wd_cyl_hi) << 8) + inb(wdc+wd_cyl_lo), addr);
1457 #endif
1458 stat = wdcommand(du, WDCC_WRITE);
1459 if (stat < 0 || stat & WDCS_ERR)
1460 return EIO;
1461
1462 #ifdef notdef /* cannot use this since this address was mapped differently */
1463 pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1464 #else
1465 *(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
1466 tlbflush();
1467 #endif
1468
1469 outsw(wdc+wd_data, CADDR1 + ((int)addr & PGOFSET),
1470 DEV_BSIZE / sizeof(short));
1471
1472 /* Check data request (should be done). */
1473 if (inb(wdc+wd_status) & (WDCS_ERR | WDCS_DRQ))
1474 return EIO;
1475
1476 if (wait_for_unbusy(du) < 0)
1477 return EIO;
1478
1479 /* error check the xfer */
1480 if (inb(wdc+wd_status) & WDCS_ERR)
1481 return EIO;
1482
1483 if ((unsigned)addr % 1048576 == 0)
1484 printf("%d ", num / (1048576 / DEV_BSIZE));
1485
1486 /* update block count */
1487 num--;
1488 blknum++;
1489 (int)addr += DEV_BSIZE;
1490
1491 #if DO_NOT_KNOW_HOW
1492 /* operator aborting dump? non-blocking getc() */
1493 if (cngetc())
1494 return EINTR;
1495 #endif
1496 }
1497 return 0;
1498 }
1499
1500 /*
1501 * Internalize the bad sector table.
1502 */
1503 void
1504 bad144intern(struct disk *du)
1505 {
1506 int i;
1507 if (du->dk_flags & DKFL_BADSECT) {
1508 for (i = 0; i < 127; i++)
1509 du->dk_badsect[i] = -1;
1510 for (i = 0; i < 126; i++) {
1511 if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
1512 break;
1513 du->dk_badsect[i] =
1514 du->dk_cpd.bad.bt_bad[i].bt_cyl *
1515 du->dk_dd.d_secpercyl +
1516 (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
1517 du->dk_dd.d_nsectors +
1518 (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
1519 }
1520 }
1521 }
1522
1523 static int
1524 wdreset(struct disk *du, int err)
1525 {
1526 int wdc = du->dk_port;
1527 int stat;
1528
1529 if (err)
1530 printf("wdc%d: busy too long, resetting\n", du->dk_ctrlr);
1531
1532 /* reset the device */
1533 outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1534 DELAY(1000);
1535 outb(wdc+wd_ctlr, WDCTL_4BIT);
1536
1537 if (wait_for_unbusy(du) < 0)
1538 printf("wdc%d: failed to reset controller\n", du->dk_ctrlr);
1539 }
1540
1541 int
1542 wdc_wait(du, mask)
1543 struct disk *du;
1544 int mask;
1545 {
1546 int wdc = du->dk_port;
1547 int timeout = 0;
1548 int stat;
1549
1550 for (;;) {
1551 stat = inb(wdc+wd_altsts);
1552 if ((stat & WDCS_BUSY) == 0)
1553 /* XXXX Yuck. Need to redo this junk. */
1554 if (mask == 0 || (stat & mask) != 0)
1555 break;
1556 DELAY(WDCDELAY);
1557 if (++timeout > WDCNDELAY)
1558 return -1;
1559 }
1560 #ifdef WDCNDELAY_DEBUG
1561 if (timeout > WDCNDELAY_DEBUG)
1562 printf("wdc%d: timeout took %dus\n", du->dk_ctrlr,
1563 WDCDELAY * timeout);
1564 #endif
1565 return stat;
1566 }
1567
1568 static int
1569 wdtimeout(caddr_t arg)
1570 {
1571 int x = splbio();
1572 register int unit = (int)arg;
1573
1574 if (wdtimeoutstatus[unit] && --wdtimeoutstatus[unit] == 0) {
1575 struct disk *du = wddrives[unit];
1576 int wdc = du->dk_port;
1577
1578 printf("wd%d: lost interrupt - status %x, error %x\n",
1579 unit, inb(wdc+wd_status), inb(wdc+wd_error));
1580 outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1581 DELAY(1000);
1582 outb(wdc+wd_ctlr, WDCTL_IDS);
1583 DELAY(1000);
1584 (void) inb(wdc+wd_error);
1585 outb(wdc+wd_ctlr, WDCTL_4BIT);
1586 du->dk_skip = 0;
1587 du->dk_flags |= DKFL_SINGLE;
1588 wdstart(du->dk_ctrlr); /* start controller */
1589 }
1590 timeout((timeout_t)wdtimeout, (caddr_t)unit, hz*2);
1591 splx(x);
1592 return 0;
1593 }
1594
1595 #endif /* NWDC > 0 */
1596