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