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