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