wd.c revision 1.46 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.46 1994/02/26 17:10:12 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 int wdc_wait(int, int, int, int);
166 #define wait_for_drq(p, u) wdc_wait((p)+wd_status, u, 0, WDCS_DRQ)
167 #define wait_for_ready(p, u) wdc_wait((p)+wd_status, u, 0, WDCS_READY)
168 #define wait_for_unbusy(p, u) wdc_wait((p)+wd_status, u, WDCS_BUSY, 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(dvp->id_unit, wdc, 0);
199
200 /* execute a controller only command */
201 if (wdcommand(du, WDCC_DIAGNOSE) < 0)
202 goto nodevice;
203
204 bzero(&wdtab[du->dk_ctrlr], sizeof(struct buf));
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 disksort(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 * Routine to queue a command to the controller. The unit's
352 * request is linked into the active list for the controller.
353 * If the controller is idle, the transfer is started.
354 */
355 static void
356 wdustart(register struct disk *du)
357 {
358 register struct buf *bp, *dp = &wdutab[du->dk_lunit];
359 int ctrlr = du->dk_ctrlr;
360
361 /* unit already active? */
362 if (dp->b_active)
363 return;
364
365 /* anything to start? */
366 bp = dp->b_actf;
367 if (bp == NULL)
368 return;
369
370 /* link onto controller queue */
371 dp->b_forw = NULL;
372 if (wdtab[ctrlr].b_actf == NULL)
373 wdtab[ctrlr].b_actf = dp;
374 else
375 wdtab[ctrlr].b_actl->b_forw = dp;
376 wdtab[ctrlr].b_actl = dp;
377
378 /* mark the drive unit as busy */
379 dp->b_active = 1;
380 }
381
382 /*
383 * Controller startup routine. This does the calculation, and starts
384 * a single-sector read or write operation. Called to start a transfer,
385 * or from the interrupt routine to continue a multi-sector transfer.
386 * RESTRICTIONS:
387 * 1. The transfer length must be an exact multiple of the sector size.
388 */
389 static void
390 wdstart(int ctrlr)
391 {
392 register struct disk *du; /* disk unit for IO */
393 register struct buf *bp;
394 struct disklabel *lp;
395 struct buf *dp;
396 long blknum, cylin, head, sector;
397 long secpertrk, secpercyl, addr;
398 int lunit, wdc;
399 int xfrblknum;
400 unsigned char status;
401
402 loop:
403 /* is there a drive for the controller to do a transfer with? */
404 dp = wdtab[ctrlr].b_actf;
405 if (dp == NULL)
406 return;
407
408 /* is there a transfer to this drive ? if so, link it on
409 the controller's queue */
410 bp = dp->b_actf;
411 if (bp == NULL) {
412 wdtab[ctrlr].b_actf = dp->b_forw;
413 goto loop;
414 }
415
416 /* obtain controller and drive information */
417 lunit = wdunit(bp->b_dev);
418 du = wddrives[lunit];
419
420 /* if not really a transfer, do control operations specially */
421 if (du->dk_state < OPEN) {
422 (void) wdcontrol(bp);
423 return;
424 }
425
426 /* calculate transfer details */
427 blknum = bp->b_blkno + du->dk_skip;
428 #ifdef WDDEBUG
429 if (du->dk_skip == 0)
430 printf("\nwdstart %d: %s %d@%d; map ", lunit,
431 (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
432 blknum);
433 else
434 printf(" %d)%x", du->dk_skip, inb(du->dk_port+wd_altsts));
435 #endif
436 addr = (int)bp->b_un.b_addr;
437 if (du->dk_skip == 0)
438 du->dk_bc = bp->b_bcount;
439 if (du->dk_skipm == 0) {
440 struct buf *oldbp, *nextbp;
441 oldbp = bp;
442 nextbp = bp->b_actf;
443 du->dk_bct = du->dk_bc;
444 oldbp->b_flags |= B_XXX;
445 while (nextbp && (oldbp->b_flags & DKFL_SINGLE) == 0 &&
446 oldbp->b_dev == nextbp->b_dev && nextbp->b_blkno ==
447 (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
448 (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
449 if ((du->dk_bct + nextbp->b_bcount) / DEV_BSIZE >= 240)
450 break;
451 du->dk_bct += nextbp->b_bcount;
452 oldbp->b_flags |= B_XXX;
453 oldbp = nextbp;
454 nextbp = nextbp->b_actf;
455 }
456 }
457
458 lp = &du->dk_dd;
459 secpertrk = lp->d_nsectors;
460 secpercyl = lp->d_secpercyl;
461 if ((du->dk_flags & DKFL_BSDLABEL) != 0 && wdpart(bp->b_dev) != WDRAW)
462 blknum += lp->d_partitions[wdpart(bp->b_dev)].p_offset;
463 cylin = blknum / secpercyl;
464 head = (blknum % secpercyl) / secpertrk;
465 sector = blknum % secpertrk;
466
467 /* Check for bad sectors if we have them, and not formatting */
468 /* Only do this in single-sector mode, or when starting a */
469 /* multiple-sector transfer. */
470 if ((du->dk_flags & DKFL_BADSECT) &&
471 #ifdef B_FORMAT
472 (bp->b_flags & B_FORMAT) == 0 &&
473 #endif
474 (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE))) {
475
476 long blkchk, blkend, blknew;
477 int i;
478
479 blkend = blknum + howmany(du->dk_bct, DEV_BSIZE) - 1;
480 for (i = 0; (blkchk = du->dk_badsect[i]) != -1; i++) {
481 if (blkchk > blkend)
482 break; /* transfer is completely OK; done */
483 if (blkchk == blknum) {
484 blknew =
485 lp->d_secperunit - lp->d_nsectors - i - 1;
486 cylin = blknew / secpercyl;
487 head = (blknew % secpercyl) / secpertrk;
488 sector = blknew % secpertrk;
489 du->dk_flags |= DKFL_SINGLE;
490 /* found and replaced first blk of transfer; done */
491 break;
492 } else if (blkchk > blknum) {
493 du->dk_flags |= DKFL_SINGLE;
494 break; /* bad block inside transfer; done */
495 }
496 }
497 }
498 if (du->dk_flags & DKFL_SINGLE) {
499 du->dk_bct = du->dk_bc;
500 du->dk_skipm = du->dk_skip;
501 }
502
503 #ifdef WDDEBUG
504 pg("c%d h%d s%d ", cylin, head, sector);
505 #endif
506
507 sector += 1; /* sectors begin with 1, not 0 */
508
509 wdtab[ctrlr].b_active = 1; /* mark controller active */
510 wdc = du->dk_port;
511
512 #ifdef INSTRUMENT
513 /* instrumentation */
514 if (du->dk_unit >= 0 && du->dk_skip == 0) {
515 dk_busy |= 1 << du->dk_lunit;
516 dk_wds[du->dk_lunit] += bp->b_bcount >> 6;
517 }
518 if (du->dk_unit >= 0 && du->dk_skipm == 0) {
519 ++dk_seek[du->dk_lunit];
520 ++dk_xfer[du->dk_lunit];
521 }
522 #endif
523
524 retry:
525 /* if starting a multisector transfer, or doing single transfers */
526 if (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE)) {
527 if (wdtab[ctrlr].b_errcnt && (bp->b_flags & B_READ) == 0) {
528 du->dk_bc += DEV_BSIZE;
529 du->dk_bct += DEV_BSIZE;
530 }
531
532 /* controller idle? */
533 if (wait_for_unbusy(wdc, ctrlr) == -1)
534 wdreset(ctrlr, wdc, 1);
535
536 /* stuff the task file */
537 outb(wdc+wd_precomp, lp->d_precompcyl / 4);
538 #ifdef B_FORMAT
539 if (bp->b_flags & B_FORMAT) {
540 outb(wdc+wd_sector, lp->d_gap3);
541 outb(wdc+wd_seccnt, lp->d_nsectors);
542 } else {
543 if (du->dk_flags & DKFL_SINGLE)
544 outb(wdc+wd_seccnt, 1);
545 else
546 outb(wdc+wd_seccnt,
547 howmany(du->dk_bct, DEV_BSIZE));
548 outb(wdc+wd_sector, sector);
549 }
550 #else
551 if (du->dk_flags & DKFL_SINGLE)
552 outb(wdc+wd_seccnt, 1);
553 else
554 outb(wdc+wd_seccnt, howmany(du->dk_bct, DEV_BSIZE));
555 outb(wdc+wd_sector, sector);
556 #endif
557 outb(wdc+wd_cyl_lo, cylin);
558 outb(wdc+wd_cyl_hi, cylin >> 8);
559
560 /* set up the SDH register (select drive) */
561 outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit<<4) | (head & 0xf));
562
563 /* wait for drive to become ready */
564 if (wait_for_ready(wdc, ctrlr) == -1) {
565 wdreset(ctrlr, wdc, 1);
566 goto retry;
567 }
568
569 /* initiate command! */
570 #ifdef B_FORMAT
571 if (bp->b_flags & B_FORMAT)
572 outb(wdc+wd_command, WDCC_FORMAT);
573 else
574 outb(wdc+wd_command,
575 (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE);
576 #else
577 outb(wdc+wd_command,
578 (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE);
579 #endif
580 #ifdef WDDEBUG
581 printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
582 cylin, head, addr, inb(wdc+wd_altsts));
583 #endif
584 }
585
586 /* if this is a read operation, just go away until it's done. */
587 if (bp->b_flags & B_READ) {
588 wdtimeoutstatus[lunit] = 2;
589 return;
590 }
591
592 /* ready to send data? */
593 if (wait_for_drq(wdc, ctrlr) == -1) {
594 wdreset(ctrlr, wdc, 1);
595 goto retry;
596 }
597
598 /* then send it! */
599 outagain:
600 outsw(wdc+wd_data, addr + du->dk_skip * DEV_BSIZE,
601 DEV_BSIZE / sizeof(short));
602 du->dk_bc -= DEV_BSIZE;
603 du->dk_bct -= DEV_BSIZE;
604 wdtimeoutstatus[lunit] = 2;
605 }
606
607 /* Interrupt routine for the controller. Acknowledge the interrupt, check for
608 * errors on the current operation, mark it done if necessary, and start
609 * the next request. Also check for a partially done transfer, and
610 * continue with the next chunk if so.
611 */
612 void
613 wdintr(struct intrframe wdif)
614 {
615 register struct disk *du;
616 register struct buf *bp, *dp;
617 int stat, wdc, ctrlr;
618
619 ctrlr = wdif.if_vec;
620
621 if (!wdtab[ctrlr].b_active) {
622 printf("wdc%d: extra interrupt\n", ctrlr);
623 return;
624 }
625
626 dp = wdtab[ctrlr].b_actf;
627 bp = dp->b_actf;
628 du = wddrives[wdunit(bp->b_dev)];
629 wdc = du->dk_port;
630 wdtimeoutstatus[wdunit(bp->b_dev)] = 0;
631
632 #ifdef WDDEBUG
633 printf("I%d ", ctrlr);
634 #endif
635
636 stat = wait_for_unbusy(wdc, ctrlr);
637 if (stat == -1) {
638 /* XXXX looks bogus */
639 wdstart(ctrlr);
640 printf("wdc%d: timeout in wdintr WDCS_BUSY\n", ctrlr);
641 }
642
643 /* is it not a transfer, but a control operation? */
644 if (du->dk_state < OPEN) {
645 wdtab[unit].b_active = 0;
646 if (wdcontrol(bp))
647 wdstart(ctrlr);
648 return;
649 }
650
651 /* have we an error? */
652 if (stat & (WDCS_ERR | WDCS_ECCCOR)) {
653 du->dk_status = stat;
654 du->dk_error = inb(wdc+wd_error);
655 #ifdef WDDEBUG
656 printf("stat %x error %x\n", stat, du->dk_error);
657 #endif
658 if ((du->dk_flags & DKFL_SINGLE) == 0) {
659 du->dk_flags |= DKFL_ERROR;
660 goto outt;
661 }
662 #ifdef B_FORMAT
663 if (bp->b_flags & B_FORMAT) {
664 bp->b_error = EIO;
665 bp->b_flags |= B_ERROR;
666 goto done;
667 }
668 #endif
669
670 /* error or error correction? */
671 if (stat & WDCS_ERR) {
672 if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
673 wdtab[ctrlr].b_active = 0;
674 else {
675 if ((du->dk_flags & DKFL_QUIET) == 0) {
676 diskerr(bp, "wd", "hard error",
677 LOG_PRINTF, du->dk_skip,
678 &du->dk_dd);
679 #ifdef WDDEBUG
680 printf("stat %b error %b\n", stat,
681 WDCS_BITS, inb(wdc+wd_error),
682 WDERR_BITS);
683 #endif
684 }
685 bp->b_flags |= B_ERROR; /* flag the error */
686 }
687 } else if ((du->dk_flags & DKFL_QUIET) == 0)
688 diskerr(bp, "wd", "soft ecc", 0, du->dk_skip,
689 &du->dk_dd);
690 }
691
692 /*
693 * If this was a successful read operation, fetch the data.
694 */
695 if (((bp->b_flags & (B_READ | B_ERROR)) == B_READ) &&
696 wdtab[ctrlr].b_active) {
697 int chk, dummy;
698
699 chk = min(DEV_BSIZE / sizeof(short), du->dk_bc / sizeof(short));
700
701 /* ready to receive data? */
702 if (wait_for_drq(wdc, ctrlr) == -1) {
703 /* XXXX abort here? */
704 wdstart(ctrlr);
705 printf("wdc%d: timeout in wdintr WDCS_DRQ\n", ctrlr);
706 }
707
708 /* suck in data */
709 insw(wdc+wd_data,
710 (int)bp->b_un.b_addr + du->dk_skip * DEV_BSIZE, chk);
711 du->dk_bc -= chk * sizeof(short);
712 du->dk_bct -= chk * sizeof(short);
713
714 /* for obselete fractional sector reads */
715 while (chk++ < (DEV_BSIZE / sizeof(short)))
716 insw(wdc+wd_data, &dummy, 1);
717 }
718
719 wdxfer[du->dk_lunit]++;
720 outt:
721 if (wdtab[ctrlr].b_active) {
722 #ifdef INSTRUMENT
723 if (du->dk_unit >= 0)
724 dk_busy &= ~(1 << du->dk_unit);
725 #endif
726 if ((bp->b_flags & B_ERROR) == 0) {
727 du->dk_skip++; /* Add to succ. sect */
728 du->dk_skipm++; /* Add to succ. sect for multitransfer */
729 if (wdtab[ctrlr].b_errcnt &&
730 (du->dk_flags & DKFL_QUIET) == 0)
731 diskerr(bp, "wd", "soft error", 0, du->dk_skip,
732 &du->dk_dd);
733 wdtab[ctrlr].b_errcnt = 0;
734 wdtab[ctrlr].b_active = 0;
735
736 /* see if more to transfer */
737 if (du->dk_bc > 0 && (du->dk_flags & DKFL_ERROR) == 0) {
738 if ((du->dk_flags & DKFL_SINGLE) ||
739 (bp->b_flags & B_READ) == 0) {
740 wdstart(ctrlr);
741 return; /* next chunk is started */
742 }
743 } else if ((du->dk_flags & (DKFL_SINGLE | DKFL_ERROR)) == DKFL_ERROR) {
744 du->dk_skip = 0;
745 du->dk_skipm = 0;
746 du->dk_flags &= ~DKFL_ERROR;
747 du->dk_flags |= DKFL_SINGLE;
748 wdstart(ctrlr);
749 return; /* redo xfer sector by sector */
750 }
751 }
752
753 done:
754 /* done with this transfer, with or without error */
755 du->dk_flags &= ~DKFL_SINGLE;
756 wdtab[ctrlr].b_errcnt = 0;
757 du->dk_skip = 0;
758 if (du->dk_bct == 0) {
759 wdtab[ctrlr].b_actf = dp->b_forw;
760 du->dk_skipm = 0;
761 dp->b_active = 0;
762 }
763 dp->b_actf = bp->b_actf;
764 dp->b_errcnt = 0;
765 bp->b_resid = bp->b_bcount - du->dk_skip * DEV_BSIZE;
766 bp->b_flags &= ~B_XXX;
767 biodone(bp);
768 }
769
770 wdtab[ctrlr].b_active = 0;
771
772 /* anything more on drive queue? */
773 if (dp->b_actf && du->dk_bct == 0)
774 wdustart(du);
775
776 /* anything more for controller to do? */
777 if (wdtab[ctrlr].b_actf)
778 wdstart(ctrlr);
779 }
780
781 /*
782 * Initialize a drive.
783 */
784 int
785 wdopen(dev_t dev, int flags, int fmt, struct proc *p)
786 {
787 register unsigned int lunit;
788 register struct disk *du;
789 int part = wdpart(dev), mask = 1 << part;
790 struct partition *pp;
791 char *msg;
792
793 lunit = wdunit(dev);
794 if (lunit >= NWD)
795 return ENXIO;
796 du = wddrives[lunit];
797 if (du == 0)
798 return ENXIO;
799
800 #ifdef QUIETWORKS
801 if (part == WDRAW)
802 du->dk_flags |= DKFL_QUIET;
803 else
804 du->dk_flags &= ~DKFL_QUIET;
805 #else
806 du->dk_flags &= ~DKFL_QUIET;
807 #endif
808
809 if ((du->dk_flags & DKFL_BSDLABEL) == 0) {
810 du->dk_flags |= DKFL_WRITEPROT;
811 wdutab[lunit].b_actf = NULL;
812
813 /*
814 * Use the default sizes until we've read the label,
815 * or longer if there isn't one there.
816 */
817 bzero(&du->dk_dd, sizeof(du->dk_dd));
818 du->dk_dd.d_type = DTYPE_ST506;
819 du->dk_dd.d_ncylinders = 1024;
820 du->dk_dd.d_secsize = DEV_BSIZE;
821 du->dk_dd.d_ntracks = 8;
822 du->dk_dd.d_nsectors = 17;
823 du->dk_dd.d_secpercyl = 17*8;
824 du->dk_dd.d_secperunit = 17*8*1024;
825 du->dk_state = WANTOPEN;
826
827 /* read label using "raw" partition */
828 #if defined(TIHMODS) && defined(garbage)
829 /* wdsetctlr(dev, du); */ /* Maybe do this TIH */
830 msg = readdisklabel(makewddev(major(dev), wdunit(dev), WDRAW),
831 wdstrategy, &du->dk_dd, &du->dk_cpd);
832 wdsetctlr(dev, du);
833 #endif
834 if (msg = readdisklabel(makewddev(major(dev), wdunit(dev),
835 WDRAW), wdstrategy, &du->dk_dd, &du->dk_cpd)) {
836 if ((du->dk_flags & DKFL_QUIET) == 0) {
837 log(LOG_WARNING,
838 "wd%d: cannot find label (%s)\n",
839 lunit, msg);
840 return EINVAL; /* XXX needs translation */
841 }
842 } else {
843 wdsetctlr(dev, du);
844 du->dk_flags |= DKFL_BSDLABEL;
845 du->dk_flags &= ~DKFL_WRITEPROT;
846 if (du->dk_dd.d_flags & D_BADSECT)
847 du->dk_flags |= DKFL_BADSECT;
848 }
849 }
850
851 if (du->dk_flags & DKFL_BADSECT)
852 bad144intern(du);
853
854 /*
855 * Warn if a partion is opened
856 * that overlaps another partition which is open
857 * unless one is the "raw" partition (whole disk).
858 */
859 if ((du->dk_openpart & mask) == 0 && part != WDRAW) {
860 int start, end;
861
862 pp = &du->dk_dd.d_partitions[part];
863 start = pp->p_offset;
864 end = pp->p_offset + pp->p_size;
865 for (pp = du->dk_dd.d_partitions;
866 pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions];
867 pp++) {
868 if (pp->p_offset + pp->p_size <= start ||
869 pp->p_offset >= end)
870 continue;
871 if (pp - du->dk_dd.d_partitions == WDRAW)
872 continue;
873 if (du->dk_openpart & (1 << (pp - du->dk_dd.d_partitions)))
874 log(LOG_WARNING,
875 "wd%d%c: overlaps open partition (%c)\n",
876 lunit, part + 'a',
877 pp - du->dk_dd.d_partitions + 'a');
878 }
879 }
880 if (part >= du->dk_dd.d_npartitions && part != WDRAW)
881 return ENXIO;
882
883 /* insure only one open at a time */
884 du->dk_openpart |= mask;
885 switch (fmt) {
886 case S_IFCHR:
887 du->dk_copenpart |= mask;
888 break;
889 case S_IFBLK:
890 du->dk_bopenpart |= mask;
891 break;
892 }
893 return 0;
894 }
895
896 /*
897 * Implement operations other than read/write.
898 * Called from wdstart or wdintr during opens and formats.
899 * Uses finite-state-machine to track progress of operation in progress.
900 * Returns 0 if operation still in progress, 1 if completed.
901 */
902 static int
903 wdcontrol(register struct buf *bp)
904 {
905 register struct disk *du;
906 register unit, lunit;
907 unsigned char stat;
908 int s, ctrlr;
909 int wdc;
910
911 du = wddrives[wdunit(bp->b_dev)];
912 ctrlr = du->dk_ctrlr;
913 unit = du->dk_unit;
914 lunit = du->dk_lunit;
915 wdc = du->dk_port;
916
917 switch (du->dk_state) {
918 case WANTOPEN: /* set SDH, step rate, do restore */
919 tryagainrecal:
920 #ifdef WDDEBUG
921 printf("wd%d: recal ", lunit);
922 #endif
923 s = splbio(); /* not called from intr level ... */
924 wdgetctlr(unit, du);
925
926 if (wait_for_ready(wdc, ctrlr) == -1) {
927 wdreset(ctrlr, wdc, 1);
928 goto tryagainrecal;
929 }
930 outb(wdc+wd_sdh, WDSD_IBM | (unit << 4));
931 wdtab[ctrlr].b_active = 1;
932 outb(wdc+wd_command, WDCC_RESTORE | WD_STEP);
933 if (wait_for_ready(wdc, ctrlr) == -1) {
934 wdreset(ctrlr, wdc, 1);
935 goto tryagainrecal;
936 }
937 du->dk_state = RECAL;
938 splx(s);
939 return 0;
940 case RECAL:
941 if ((stat = inb(wdc+wd_status)) & WDCS_ERR) {
942 if ((du->dk_flags & DKFL_QUIET) == 0) {
943 printf("wd%d: recal", du->dk_lunit);
944 printf(": status %b error %b\n", stat,
945 WDCS_BITS, inb(wdc+wd_error), WDERR_BITS);
946 }
947 if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
948 goto tryagainrecal;
949 bp->b_error = ENXIO; /* XXX needs translation */
950 goto badopen;
951 }
952
953 /* some controllers require this ... */
954 wdsetctlr(bp->b_dev, du);
955
956 wdtab[ctrlr].b_errcnt = 0;
957 du->dk_state = OPEN;
958 /*
959 * The rest of the initialization can be done
960 * by normal means.
961 */
962 return 1;
963 default:
964 panic("wdcontrol");
965 }
966 /* NOTREACHED */
967
968 badopen:
969 if ((du->dk_flags & DKFL_QUIET) == 0)
970 printf(": status %b error %b\n", stat, WDCS_BITS,
971 inb(wdc+wd_error), WDERR_BITS);
972 bp->b_flags |= B_ERROR;
973 return 1;
974 }
975
976 /*
977 * send a command and wait uninterruptibly until controller is finished.
978 * return -1 if controller busy for too long, otherwise
979 * return status. intended for brief controller commands at critical points.
980 * assumes interrupts are blocked.
981 */
982 static int
983 wdcommand(struct disk *du, int cmd)
984 {
985 int timeout, stat, wdc;
986
987 /*DELAY(2000);*/
988 wdc = du->dk_port;
989
990 /* controller ready for command? */
991 if (wait_for_unbusy(wdc, du->dk_ctrlr) == -1)
992 return -1;
993
994 /* send command, await results */
995 outb(wdc+wd_command, cmd);
996 stat = wait_for_unbusy(wdc, du->dk_ctrlr);
997 if (stat == -1)
998 return -1;
999
1000 if (cmd != WDCC_READP)
1001 return stat;
1002
1003 /* is controller ready to return data? */
1004 return wdc_wait(wdc+wd_status, du->dk_ctrlr, 0, WDCS_ERR | WDCS_DRQ);
1005 }
1006
1007 /*
1008 * issue IDC to drive to tell it just what geometry it is to be.
1009 */
1010 static int
1011 wdsetctlr(dev_t dev, struct disk *du)
1012 {
1013 int stat, x, wdc;
1014
1015 #ifdef WDDEBUG
1016 printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
1017 du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
1018 #endif
1019
1020 wdc = du->dk_port;
1021
1022 x = splbio();
1023 outb(wdc+wd_cyl_lo, du->dk_dd.d_ncylinders); /* TIH: was ...ders+1 */
1024 outb(wdc+wd_cyl_hi, du->dk_dd.d_ncylinders>>8); /* TIH: was ...ders+1 */
1025 outb(wdc+wd_sdh,
1026 WDSD_IBM | (du->dk_unit << 4) + du->dk_dd.d_ntracks - 1);
1027 outb(wdc+wd_seccnt, du->dk_dd.d_nsectors);
1028 stat = wdcommand(du, WDCC_IDC);
1029
1030 if (stat & WDCS_ERR)
1031 printf("wdsetctlr: stat %b error %b\n", stat, WDCS_BITS,
1032 inb(wdc+wd_error), WDERR_BITS);
1033 splx(x);
1034 return stat;
1035 }
1036
1037 /*
1038 * issue READP to drive to ask it what it is.
1039 */
1040 static int
1041 wdgetctlr(int u, struct disk *du)
1042 {
1043 int stat, x, i, wdc;
1044 char tb[DEV_BSIZE];
1045 struct wdparams *wp;
1046
1047 x = splbio(); /* not called from intr level ... */
1048 wdc = du->dk_port;
1049 if (wait_for_unbusy(wdc, du->dk_ctrlr) == -1) {
1050 splx(x);
1051 return -1;
1052 }
1053
1054 outb(wdc+wd_sdh, WDSD_IBM | (u << 4));
1055 if (wait_for_ready(wdc, du->dk_ctrlr) == -1) {
1056 splx(x);
1057 return -1;
1058 }
1059
1060 stat = wdcommand(du, WDCC_READP);
1061 if (wait_for_ready(wdc, du->dk_ctrlr) == -1) {
1062 splx(x);
1063 return -1;
1064 }
1065
1066 if (stat < 0) {
1067 splx(x);
1068 return stat;
1069 }
1070
1071 if ((stat & WDCS_ERR) == 0) {
1072 /* obtain parameters */
1073 wp = &du->dk_params;
1074 insw(wdc+wd_data, tb, sizeof(tb)/sizeof(short));
1075 bcopy(tb, wp, sizeof(struct wdparams));
1076
1077 /* shuffle string byte order */
1078 for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
1079 u_short *p;
1080 p = (u_short *)(wp->wdp_model + i);
1081 *p = ntohs(*p);
1082 }
1083
1084 strncpy(du->dk_dd.d_typename, "ESDI/IDE",
1085 sizeof du->dk_dd.d_typename);
1086 du->dk_dd.d_type = DTYPE_ESDI;
1087 bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
1088
1089 /* update disklabel given drive information */
1090 du->dk_dd.d_ncylinders =
1091 wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
1092 du->dk_dd.d_ntracks = wp->wdp_heads;
1093 du->dk_dd.d_nsectors = wp->wdp_sectors;
1094 du->dk_dd.d_secpercyl =
1095 du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
1096 du->dk_dd.d_partitions[1].p_size =
1097 du->dk_dd.d_secpercyl * wp->wdp_sectors;
1098 du->dk_dd.d_partitions[1].p_offset = 0;
1099 } else {
1100 /*
1101 * If WDCC_READP fails then we might have an old drive
1102 * so we try a seek to 0; if that passes then the
1103 * drive is there but it's OLD AND KRUSTY.
1104 */
1105 stat = wdcommand(du, WDCC_RESTORE | WD_STEP);
1106 if (stat & WDCS_ERR) {
1107 splx(x);
1108 return inb(wdc+wd_error);
1109 }
1110
1111 strncpy(du->dk_dd.d_typename, "ST506",
1112 sizeof du->dk_dd.d_typename);
1113 strncpy(du->dk_params.wdp_model, "Unknown Type",
1114 sizeof du->dk_params.wdp_model);
1115 du->dk_dd.d_type = DTYPE_ST506;
1116 }
1117
1118 #if 0
1119 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1120 wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1121 wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1122 #endif
1123
1124 /* better ... */
1125 du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
1126
1127 /* XXX sometimes possibly needed */
1128 (void) inb(wdc+wd_status);
1129 splx(x);
1130 return 0;
1131 }
1132
1133
1134 /* ARGSUSED */
1135 int
1136 wdclose(dev_t dev, int flags, int fmt)
1137 {
1138 register struct disk *du;
1139 int part = wdpart(dev), mask = 1 << part;
1140
1141 du = wddrives[wdunit(dev)];
1142
1143 /* insure only one open at a time */
1144 du->dk_openpart &= ~mask;
1145 switch (fmt) {
1146 case S_IFCHR:
1147 du->dk_copenpart &= ~mask;
1148 break;
1149 case S_IFBLK:
1150 du->dk_bopenpart &= ~mask;
1151 break;
1152 }
1153 return 0;
1154 }
1155
1156 int
1157 wdioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
1158 {
1159 int lunit = wdunit(dev);
1160 register struct disk *du;
1161 int error = 0;
1162 struct uio auio;
1163 struct iovec aiov;
1164
1165 du = wddrives[lunit];
1166
1167 switch (cmd) {
1168 case DIOCSBAD:
1169 if ((flag & FWRITE) == 0)
1170 error = EBADF;
1171 else {
1172 du->dk_cpd.bad = *(struct dkbad *)addr;
1173 bad144intern(du);
1174 }
1175 break;
1176
1177 case DIOCGDINFO:
1178 *(struct disklabel *)addr = du->dk_dd;
1179 break;
1180
1181 case DIOCGPART:
1182 ((struct partinfo *)addr)->disklab = &du->dk_dd;
1183 ((struct partinfo *)addr)->part =
1184 &du->dk_dd.d_partitions[wdpart(dev)];
1185 break;
1186
1187 case DIOCSDINFO:
1188 if ((flag & FWRITE) == 0)
1189 error = EBADF;
1190 else {
1191 error = setdisklabel(&du->dk_dd, (struct disklabel *)addr,
1192 /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart : */0,
1193 &du->dk_cpd);
1194 }
1195 if (error == 0) {
1196 du->dk_flags |= DKFL_BSDLABEL;
1197 wdsetctlr(dev, du);
1198 }
1199 break;
1200
1201 case DIOCWLABEL:
1202 du->dk_flags &= ~DKFL_WRITEPROT;
1203 if ((flag & FWRITE) == 0)
1204 error = EBADF;
1205 else
1206 du->dk_wlabel = *(int *)addr;
1207 break;
1208
1209 case DIOCWDINFO:
1210 du->dk_flags &= ~DKFL_WRITEPROT;
1211 if ((flag & FWRITE) == 0)
1212 error = EBADF;
1213 else if ((error = setdisklabel(&du->dk_dd,
1214 (struct disklabel *)addr,
1215 /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/0,
1216 &du->dk_cpd)) == 0) {
1217 int wlab;
1218
1219 du->dk_flags |= DKFL_BSDLABEL;
1220 wdsetctlr(dev, du);
1221
1222 /* simulate opening partition 0 so write succeeds */
1223 du->dk_openpart |= (1 << 0); /* XXX */
1224 wlab = du->dk_wlabel;
1225 du->dk_wlabel = 1;
1226 error = writedisklabel(dev, wdstrategy, &du->dk_dd, &du->dk_cpd);
1227 du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
1228 du->dk_wlabel = wlab;
1229 }
1230 break;
1231
1232 #ifdef notyet
1233 case DIOCGDINFOP:
1234 *(struct disklabel **)addr = &du->dk_dd;
1235 break;
1236
1237 case DIOCWFORMAT:
1238 if ((flag & FWRITE) == 0)
1239 error = EBADF;
1240 else {
1241 register struct format_op *fop;
1242
1243 fop = (struct format_op *)addr;
1244 aiov.iov_base = fop->df_buf;
1245 aiov.iov_len = fop->df_count;
1246 auio.uio_iov = &aiov;
1247 auio.uio_iovcnt = 1;
1248 auio.uio_resid = fop->df_count;
1249 auio.uio_segflg = 0;
1250 auio.uio_offset =
1251 fop->df_startblk * du->dk_dd.d_secsize;
1252 error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
1253 minphys, &auio);
1254 fop->df_count -= auio.uio_resid;
1255 fop->df_reg[0] = du->dk_status;
1256 fop->df_reg[1] = du->dk_error;
1257 }
1258 break;
1259 #endif
1260
1261 default:
1262 error = ENOTTY;
1263 break;
1264 }
1265 return error;
1266 }
1267
1268 #ifdef B_FORMAT
1269 int
1270 wdformat(struct buf *bp)
1271 {
1272
1273 bp->b_flags |= B_FORMAT;
1274 return wdstrategy(bp);
1275 }
1276 #endif
1277
1278 int
1279 wdsize(dev_t dev)
1280 {
1281 int lunit = wdunit(dev), part = wdpart(dev);
1282 struct disk *du;
1283
1284 if (lunit >= NWD)
1285 return -1;
1286 du = wddrives[lunit];
1287 if (du == 0)
1288 return -1;
1289
1290 if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
1291 int val;
1292 val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
1293 S_IFBLK, 0);
1294 if (val != 0)
1295 return -1;
1296 }
1297
1298 if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
1299 return -1;
1300
1301 return (int)du->dk_dd.d_partitions[part].p_size;
1302 }
1303
1304 extern char *vmmap; /* poor name! */
1305
1306 /* dump core after a system crash */
1307 int
1308 wddump(dev_t dev)
1309 {
1310 register struct disk *du; /* disk unit to do the IO */
1311 long num; /* number of sectors to write */
1312 int ctrlr, lunit, part, wdc;
1313 long blkoff, blknum;
1314 long cylin, head, sector, stat;
1315 long secpertrk, secpercyl, nblocks, i;
1316 char *addr;
1317 static wddoingadump = 0;
1318 extern caddr_t CADDR1;
1319 extern struct pte *CMAP1;
1320
1321 addr = (char *)0; /* starting address */
1322
1323 #if DO_NOT_KNOW_HOW
1324 /* toss any characters present prior to dump, ie. non-blocking getc */
1325 while (cngetc())
1326 ;
1327 #endif
1328
1329 /* size of memory to dump */
1330 lunit = wdunit(dev);
1331 part = wdpart(dev); /* file system */
1332 /* check for acceptable drive number */
1333 if (lunit >= NWD)
1334 return ENXIO;
1335 du = wddrives[lunit];
1336 /* was it ever initialized ? */
1337 if (du == 0 || du->dk_state < OPEN || du->dk_flags & DKFL_WRITEPROT)
1338 return ENXIO;
1339
1340 wdc = du->dk_port;
1341 ctrlr = du->dk_ctrlr;
1342
1343 /* Convert to disk sectors */
1344 num = ctob(physmem) / du->dk_dd.d_secsize;
1345
1346 /* check if controller active */
1347 /*if (wdtab[ctrlr].b_active)
1348 return EFAULT;*/
1349 if (wddoingadump)
1350 return EFAULT;
1351
1352 secpertrk = du->dk_dd.d_nsectors;
1353 secpercyl = du->dk_dd.d_secpercyl;
1354 nblocks = du->dk_dd.d_partitions[part].p_size;
1355 blkoff = du->dk_dd.d_partitions[part].p_offset;
1356
1357 /*pg("xunit %x, nblocks %d, dumplo %d num %d\n", part, nblocks, dumplo,
1358 num);*/
1359 /* check transfer bounds against partition size */
1360 if (dumplo < 0 || dumplo + num > nblocks)
1361 return EINVAL;
1362
1363 /* mark controller active for if we panic during the dump */
1364 /*wdtab[ctrlr].b_active = 1;*/
1365 wddoingadump = 1;
1366 i = 200000000;
1367 while ((inb(wdc+wd_status) & WDCS_BUSY) && (i-- > 0))
1368 ;
1369 outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
1370 outb(wdc+wd_command, WDCC_RESTORE | WD_STEP);
1371 while (inb(wdc+wd_status) & WDCS_BUSY)
1372 ;
1373
1374 /* some compaq controllers require this ... */
1375 wdsetctlr(dev, du);
1376
1377 blknum = dumplo + blkoff;
1378 while (num > 0) {
1379 /* compute disk address */
1380 cylin = blknum / secpercyl;
1381 head = (blknum % secpercyl) / secpertrk;
1382 sector = blknum % secpertrk;
1383
1384 if (du->dk_flags & DKFL_BADSECT) {
1385 long newblk;
1386 int i;
1387
1388 for (i = 0; du->dk_badsect[i] != -1; i++) {
1389 if (blknum < du->dk_badsect[i]) {
1390 /* sorted list, passed our block by */
1391 break;
1392 } else if (blknum == du->dk_badsect[i]) {
1393 newblk = du->dk_dd.d_secperunit -
1394 du->dk_dd.d_nsectors - i - 1;
1395 cylin = newblk / secpercyl;
1396 head = (newblk % secpercyl) / secpertrk;
1397 sector = newblk % secpertrk;
1398 /* found and replaced; done */
1399 break;
1400 }
1401 }
1402 }
1403 sector++; /* origin 1 */
1404
1405 /* select drive. */
1406 outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | (head & 0xf));
1407 while ((inb(wdc+wd_status) & WDCS_READY) == 0)
1408 ;
1409
1410 /* transfer some blocks */
1411 outb(wdc+wd_sector, sector);
1412 outb(wdc+wd_seccnt, 1);
1413 outb(wdc+wd_cyl_lo, cylin);
1414 outb(wdc+wd_cyl_hi, cylin >> 8);
1415 #ifdef notdef
1416 /* lets just talk about this first...*/
1417 pg("sdh 0%o sector %d cyl %d addr 0x%x", inb(wdc+wd_sdh),
1418 inb(wdc+wd_sector),
1419 (inb(wdc+wd_cyl_hi) << 8) + inb(wdc+wd_cyl_lo), addr);
1420 #endif
1421 outb(wdc+wd_command, WDCC_WRITE);
1422
1423 #ifdef notdef /* cannot use this since this address was mapped differently */
1424 pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1425 #else
1426 *(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
1427 tlbflush();
1428 #endif
1429
1430 /* Ready to send data? */
1431 while ((inb(wdc+wd_status) & WDCS_DRQ) == 0)
1432 ;
1433 if (inb(wdc+wd_status) & WDCS_ERR)
1434 return EIO;
1435
1436 outsw(wdc+wd_data, CADDR1 + ((int)addr & PGOFSET),
1437 DEV_BSIZE / 2);
1438
1439 if (inb(wdc+wd_status) & WDCS_ERR)
1440 return EIO;
1441 /* Check data request (should be done). */
1442 if (inb(wdc+wd_status) & WDCS_DRQ)
1443 return EIO;
1444
1445 /* wait for completion */
1446 for (i = 200000000; inb(wdc+wd_status) & WDCS_BUSY; i--) {
1447 if (i < 0)
1448 return EIO;
1449 }
1450
1451 /* error check the xfer */
1452 if (inb(wdc+wd_status) & WDCS_ERR)
1453 return EIO;
1454
1455 if ((unsigned)addr % 1048576 == 0)
1456 printf("%d ", num / (1048576 / DEV_BSIZE));
1457
1458 /* update block count */
1459 num--;
1460 blknum++;
1461 (int)addr += DEV_BSIZE;
1462
1463 #if DO_NOT_KNOW_HOW
1464 /* operator aborting dump? non-blocking getc() */
1465 if (cngetc())
1466 return EINTR;
1467 #endif
1468 }
1469 return 0;
1470 }
1471
1472 /*
1473 * Internalize the bad sector table.
1474 */
1475 void
1476 bad144intern(struct disk *du)
1477 {
1478 int i;
1479 if (du->dk_flags & DKFL_BADSECT) {
1480 for (i = 0; i < 127; i++)
1481 du->dk_badsect[i] = -1;
1482 for (i = 0; i < 126; i++) {
1483 if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
1484 break;
1485 du->dk_badsect[i] =
1486 du->dk_cpd.bad.bt_bad[i].bt_cyl *
1487 du->dk_dd.d_secpercyl +
1488 (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
1489 du->dk_dd.d_nsectors +
1490 (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
1491 }
1492 }
1493 }
1494
1495 static int
1496 wdreset(int ctrlr, int wdc, int err)
1497 {
1498 int stat;
1499
1500 if (err)
1501 printf("wdc%d: busy too long, resetting\n", ctrlr);
1502
1503 /* reset the device */
1504 outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1505 DELAY(1000);
1506 outb(wdc+wd_ctlr, WDCTL_4BIT);
1507
1508 if (wait_for_unbusy(wdc, ctrlr) == -1)
1509 printf("wdc%d: failed to reset controller\n", ctrlr);
1510 }
1511
1512 int
1513 wdc_wait(port, ctrlr, on, off)
1514 int port, ctrlr;
1515 int on, off;
1516 {
1517 int timeout = 0;
1518 int stat;
1519
1520 for (;;) {
1521 stat = inb(port);
1522 if ((stat & on) != on || (stat & off) != 0)
1523 break;
1524 DELAY(WDCDELAY);
1525 if (++timeout > WDCNDELAY)
1526 return -1;
1527 }
1528 #ifdef WDCNDELAY_DEBUG
1529 if (timeout > WDCNDELAY_DEBUG)
1530 printf("wdc%d: timeout took %dus\n", ctrlr, WDCDELAY * timeout);
1531 #endif
1532 return stat;
1533 }
1534
1535 static int
1536 wdtimeout(caddr_t arg)
1537 {
1538 int x = splbio();
1539 register int unit = (int)arg;
1540
1541 if (wdtimeoutstatus[unit] && --wdtimeoutstatus[unit] == 0) {
1542 struct disk *du = wddrives[unit];
1543 int wdc = du->dk_port;
1544
1545 printf("wd%d: lost interrupt - status %x, error %x\n",
1546 unit, inb(wdc+wd_status), inb(wdc+wd_error));
1547 outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1548 DELAY(1000);
1549 outb(wdc+wd_ctlr, WDCTL_IDS);
1550 DELAY(1000);
1551 (void) inb(wdc+wd_error);
1552 outb(wdc+wd_ctlr, WDCTL_4BIT);
1553 du->dk_skip = 0;
1554 du->dk_flags |= DKFL_SINGLE;
1555 wdstart(du->dk_ctrlr); /* start controller */
1556 }
1557 timeout((timeout_t)wdtimeout, (caddr_t)unit, hz/2);
1558 splx(x);
1559 return 0;
1560 }
1561
1562 #endif /* NWDC > 0 */
1563