wd.c revision 1.48 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.48 1994/02/26 17:59:46 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[ctrlr].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 wdstart(ctrlr);
739 return; /* next chunk is started */
740 } else if ((du->dk_flags & (DKFL_SINGLE | DKFL_ERROR)) == DKFL_ERROR) {
741 du->dk_skip = 0;
742 du->dk_skipm = 0;
743 du->dk_flags &= ~DKFL_ERROR;
744 du->dk_flags |= DKFL_SINGLE;
745 wdstart(ctrlr);
746 return; /* redo xfer sector by sector */
747 }
748 }
749
750 done:
751 /* done with this transfer, with or without error */
752 du->dk_flags &= ~DKFL_SINGLE;
753 wdtab[ctrlr].b_errcnt = 0;
754 du->dk_skip = 0;
755 if (du->dk_bct == 0) {
756 wdtab[ctrlr].b_actf = dp->b_forw;
757 du->dk_skipm = 0;
758 dp->b_active = 0;
759 }
760 dp->b_actf = bp->b_actf;
761 dp->b_errcnt = 0;
762 bp->b_resid = bp->b_bcount - du->dk_skip * DEV_BSIZE;
763 bp->b_flags &= ~B_XXX;
764 biodone(bp);
765 }
766
767 wdtab[ctrlr].b_active = 0;
768
769 /* anything more on drive queue? */
770 if (dp->b_actf && du->dk_bct == 0)
771 wdustart(du);
772
773 /* anything more for controller to do? */
774 if (wdtab[ctrlr].b_actf)
775 wdstart(ctrlr);
776 }
777
778 /*
779 * Initialize a drive.
780 */
781 int
782 wdopen(dev_t dev, int flags, int fmt, struct proc *p)
783 {
784 register unsigned int lunit;
785 register struct disk *du;
786 int part = wdpart(dev), mask = 1 << part;
787 struct partition *pp;
788 char *msg;
789
790 lunit = wdunit(dev);
791 if (lunit >= NWD)
792 return ENXIO;
793 du = wddrives[lunit];
794 if (du == 0)
795 return ENXIO;
796
797 #ifdef QUIETWORKS
798 if (part == WDRAW)
799 du->dk_flags |= DKFL_QUIET;
800 else
801 du->dk_flags &= ~DKFL_QUIET;
802 #else
803 du->dk_flags &= ~DKFL_QUIET;
804 #endif
805
806 if ((du->dk_flags & DKFL_BSDLABEL) == 0) {
807 du->dk_flags |= DKFL_WRITEPROT;
808 wdutab[lunit].b_actf = NULL;
809
810 /*
811 * Use the default sizes until we've read the label,
812 * or longer if there isn't one there.
813 */
814 bzero(&du->dk_dd, sizeof(du->dk_dd));
815 du->dk_dd.d_type = DTYPE_ST506;
816 du->dk_dd.d_ncylinders = 1024;
817 du->dk_dd.d_secsize = DEV_BSIZE;
818 du->dk_dd.d_ntracks = 8;
819 du->dk_dd.d_nsectors = 17;
820 du->dk_dd.d_secpercyl = 17*8;
821 du->dk_dd.d_secperunit = 17*8*1024;
822 du->dk_state = WANTOPEN;
823
824 /* read label using "raw" partition */
825 #if defined(TIHMODS) && defined(garbage)
826 /* wdsetctlr(dev, du); */ /* Maybe do this TIH */
827 msg = readdisklabel(makewddev(major(dev), wdunit(dev), WDRAW),
828 wdstrategy, &du->dk_dd, &du->dk_cpd);
829 wdsetctlr(dev, du);
830 #endif
831 if (msg = readdisklabel(makewddev(major(dev), wdunit(dev),
832 WDRAW), wdstrategy, &du->dk_dd, &du->dk_cpd)) {
833 if ((du->dk_flags & DKFL_QUIET) == 0) {
834 log(LOG_WARNING,
835 "wd%d: cannot find label (%s)\n",
836 lunit, msg);
837 return EINVAL; /* XXX needs translation */
838 }
839 } else {
840 wdsetctlr(dev, du);
841 du->dk_flags |= DKFL_BSDLABEL;
842 du->dk_flags &= ~DKFL_WRITEPROT;
843 if (du->dk_dd.d_flags & D_BADSECT)
844 du->dk_flags |= DKFL_BADSECT;
845 }
846 }
847
848 if (du->dk_flags & DKFL_BADSECT)
849 bad144intern(du);
850
851 /*
852 * Warn if a partion is opened
853 * that overlaps another partition which is open
854 * unless one is the "raw" partition (whole disk).
855 */
856 if ((du->dk_openpart & mask) == 0 && part != WDRAW) {
857 int start, end;
858
859 pp = &du->dk_dd.d_partitions[part];
860 start = pp->p_offset;
861 end = pp->p_offset + pp->p_size;
862 for (pp = du->dk_dd.d_partitions;
863 pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions];
864 pp++) {
865 if (pp->p_offset + pp->p_size <= start ||
866 pp->p_offset >= end)
867 continue;
868 if (pp - du->dk_dd.d_partitions == WDRAW)
869 continue;
870 if (du->dk_openpart & (1 << (pp - du->dk_dd.d_partitions)))
871 log(LOG_WARNING,
872 "wd%d%c: overlaps open partition (%c)\n",
873 lunit, part + 'a',
874 pp - du->dk_dd.d_partitions + 'a');
875 }
876 }
877 if (part >= du->dk_dd.d_npartitions && part != WDRAW)
878 return ENXIO;
879
880 /* insure only one open at a time */
881 du->dk_openpart |= mask;
882 switch (fmt) {
883 case S_IFCHR:
884 du->dk_copenpart |= mask;
885 break;
886 case S_IFBLK:
887 du->dk_bopenpart |= mask;
888 break;
889 }
890 return 0;
891 }
892
893 /*
894 * Implement operations other than read/write.
895 * Called from wdstart or wdintr during opens and formats.
896 * Uses finite-state-machine to track progress of operation in progress.
897 * Returns 0 if operation still in progress, 1 if completed.
898 */
899 static int
900 wdcontrol(register struct buf *bp)
901 {
902 register struct disk *du;
903 register unit, lunit;
904 unsigned char stat;
905 int s, ctrlr;
906 int wdc;
907
908 du = wddrives[wdunit(bp->b_dev)];
909 ctrlr = du->dk_ctrlr;
910 unit = du->dk_unit;
911 lunit = du->dk_lunit;
912 wdc = du->dk_port;
913
914 switch (du->dk_state) {
915 case WANTOPEN: /* set SDH, step rate, do restore */
916 tryagainrecal:
917 #ifdef WDDEBUG
918 printf("wd%d: recal ", lunit);
919 #endif
920 s = splbio(); /* not called from intr level ... */
921 wdgetctlr(unit, du);
922
923 if (wait_for_ready(wdc, ctrlr) == -1) {
924 wdreset(ctrlr, wdc, 1);
925 goto tryagainrecal;
926 }
927 outb(wdc+wd_sdh, WDSD_IBM | (unit << 4));
928 wdtab[ctrlr].b_active = 1;
929 outb(wdc+wd_command, WDCC_RESTORE | WD_STEP);
930 if (wait_for_ready(wdc, ctrlr) == -1) {
931 wdreset(ctrlr, wdc, 1);
932 goto tryagainrecal;
933 }
934 du->dk_state = RECAL;
935 splx(s);
936 return 0;
937 case RECAL:
938 if ((stat = inb(wdc+wd_status)) & WDCS_ERR) {
939 if ((du->dk_flags & DKFL_QUIET) == 0) {
940 printf("wd%d: recal", du->dk_lunit);
941 printf(": status %b error %b\n", stat,
942 WDCS_BITS, inb(wdc+wd_error), WDERR_BITS);
943 }
944 if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
945 goto tryagainrecal;
946 bp->b_error = ENXIO; /* XXX needs translation */
947 goto badopen;
948 }
949
950 /* some controllers require this ... */
951 wdsetctlr(bp->b_dev, du);
952
953 wdtab[ctrlr].b_errcnt = 0;
954 du->dk_state = OPEN;
955 /*
956 * The rest of the initialization can be done
957 * by normal means.
958 */
959 return 1;
960 default:
961 panic("wdcontrol");
962 }
963 /* NOTREACHED */
964
965 badopen:
966 if ((du->dk_flags & DKFL_QUIET) == 0)
967 printf(": status %b error %b\n", stat, WDCS_BITS,
968 inb(wdc+wd_error), WDERR_BITS);
969 bp->b_flags |= B_ERROR;
970 return 1;
971 }
972
973 /*
974 * send a command and wait uninterruptibly until controller is finished.
975 * return -1 if controller busy for too long, otherwise
976 * return status. intended for brief controller commands at critical points.
977 * assumes interrupts are blocked.
978 */
979 static int
980 wdcommand(struct disk *du, int cmd)
981 {
982 int timeout, stat, wdc;
983
984 /*DELAY(2000);*/
985 wdc = du->dk_port;
986
987 /* controller ready for command? */
988 if (wait_for_unbusy(wdc, du->dk_ctrlr) == -1)
989 return -1;
990
991 /* send command, await results */
992 outb(wdc+wd_command, cmd);
993 stat = wait_for_unbusy(wdc, du->dk_ctrlr);
994 if (stat == -1)
995 return -1;
996
997 if (cmd != WDCC_READP)
998 return stat;
999
1000 /* is controller ready to return data? */
1001 return wdc_wait(wdc+wd_status, du->dk_ctrlr, 0, WDCS_ERR | WDCS_DRQ);
1002 }
1003
1004 /*
1005 * issue IDC to drive to tell it just what geometry it is to be.
1006 */
1007 static int
1008 wdsetctlr(dev_t dev, struct disk *du)
1009 {
1010 int stat, x, wdc;
1011
1012 #ifdef WDDEBUG
1013 printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
1014 du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
1015 #endif
1016
1017 wdc = du->dk_port;
1018
1019 x = splbio();
1020 outb(wdc+wd_cyl_lo, du->dk_dd.d_ncylinders); /* TIH: was ...ders+1 */
1021 outb(wdc+wd_cyl_hi, du->dk_dd.d_ncylinders>>8); /* TIH: was ...ders+1 */
1022 outb(wdc+wd_sdh,
1023 WDSD_IBM | (du->dk_unit << 4) + du->dk_dd.d_ntracks - 1);
1024 outb(wdc+wd_seccnt, du->dk_dd.d_nsectors);
1025 stat = wdcommand(du, WDCC_IDC);
1026
1027 if (stat & WDCS_ERR)
1028 printf("wdsetctlr: stat %b error %b\n", stat, WDCS_BITS,
1029 inb(wdc+wd_error), WDERR_BITS);
1030 splx(x);
1031 return stat;
1032 }
1033
1034 /*
1035 * issue READP to drive to ask it what it is.
1036 */
1037 static int
1038 wdgetctlr(int u, struct disk *du)
1039 {
1040 int stat, x, i, wdc;
1041 char tb[DEV_BSIZE];
1042 struct wdparams *wp;
1043
1044 x = splbio(); /* not called from intr level ... */
1045 wdc = du->dk_port;
1046 if (wait_for_unbusy(wdc, du->dk_ctrlr) == -1) {
1047 splx(x);
1048 return -1;
1049 }
1050
1051 outb(wdc+wd_sdh, WDSD_IBM | (u << 4));
1052 if (wait_for_ready(wdc, du->dk_ctrlr) == -1) {
1053 splx(x);
1054 return -1;
1055 }
1056
1057 stat = wdcommand(du, WDCC_READP);
1058 if (wait_for_ready(wdc, du->dk_ctrlr) == -1) {
1059 splx(x);
1060 return -1;
1061 }
1062
1063 if (stat < 0) {
1064 splx(x);
1065 return stat;
1066 }
1067
1068 if ((stat & WDCS_ERR) == 0) {
1069 /* obtain parameters */
1070 wp = &du->dk_params;
1071 insw(wdc+wd_data, tb, sizeof(tb)/sizeof(short));
1072 bcopy(tb, wp, sizeof(struct wdparams));
1073
1074 /* shuffle string byte order */
1075 for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
1076 u_short *p;
1077 p = (u_short *)(wp->wdp_model + i);
1078 *p = ntohs(*p);
1079 }
1080
1081 strncpy(du->dk_dd.d_typename, "ESDI/IDE",
1082 sizeof du->dk_dd.d_typename);
1083 du->dk_dd.d_type = DTYPE_ESDI;
1084 bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
1085
1086 /* update disklabel given drive information */
1087 du->dk_dd.d_ncylinders =
1088 wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
1089 du->dk_dd.d_ntracks = wp->wdp_heads;
1090 du->dk_dd.d_nsectors = wp->wdp_sectors;
1091 du->dk_dd.d_secpercyl =
1092 du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
1093 du->dk_dd.d_partitions[1].p_size =
1094 du->dk_dd.d_secpercyl * wp->wdp_sectors;
1095 du->dk_dd.d_partitions[1].p_offset = 0;
1096 } else {
1097 /*
1098 * If WDCC_READP fails then we might have an old drive
1099 * so we try a seek to 0; if that passes then the
1100 * drive is there but it's OLD AND KRUSTY.
1101 */
1102 stat = wdcommand(du, WDCC_RESTORE | WD_STEP);
1103 if (stat & WDCS_ERR) {
1104 splx(x);
1105 return inb(wdc+wd_error);
1106 }
1107
1108 strncpy(du->dk_dd.d_typename, "ST506",
1109 sizeof du->dk_dd.d_typename);
1110 strncpy(du->dk_params.wdp_model, "Unknown Type",
1111 sizeof du->dk_params.wdp_model);
1112 du->dk_dd.d_type = DTYPE_ST506;
1113 }
1114
1115 #if 0
1116 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1117 wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1118 wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1119 #endif
1120
1121 /* better ... */
1122 du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
1123
1124 /* XXX sometimes possibly needed */
1125 (void) inb(wdc+wd_status);
1126 splx(x);
1127 return 0;
1128 }
1129
1130
1131 /* ARGSUSED */
1132 int
1133 wdclose(dev_t dev, int flags, int fmt)
1134 {
1135 register struct disk *du;
1136 int part = wdpart(dev), mask = 1 << part;
1137
1138 du = wddrives[wdunit(dev)];
1139
1140 /* insure only one open at a time */
1141 du->dk_openpart &= ~mask;
1142 switch (fmt) {
1143 case S_IFCHR:
1144 du->dk_copenpart &= ~mask;
1145 break;
1146 case S_IFBLK:
1147 du->dk_bopenpart &= ~mask;
1148 break;
1149 }
1150 return 0;
1151 }
1152
1153 int
1154 wdioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
1155 {
1156 int lunit = wdunit(dev);
1157 register struct disk *du;
1158 int error = 0;
1159 struct uio auio;
1160 struct iovec aiov;
1161
1162 du = wddrives[lunit];
1163
1164 switch (cmd) {
1165 case DIOCSBAD:
1166 if ((flag & FWRITE) == 0)
1167 error = EBADF;
1168 else {
1169 du->dk_cpd.bad = *(struct dkbad *)addr;
1170 bad144intern(du);
1171 }
1172 break;
1173
1174 case DIOCGDINFO:
1175 *(struct disklabel *)addr = du->dk_dd;
1176 break;
1177
1178 case DIOCGPART:
1179 ((struct partinfo *)addr)->disklab = &du->dk_dd;
1180 ((struct partinfo *)addr)->part =
1181 &du->dk_dd.d_partitions[wdpart(dev)];
1182 break;
1183
1184 case DIOCSDINFO:
1185 if ((flag & FWRITE) == 0)
1186 error = EBADF;
1187 else {
1188 error = setdisklabel(&du->dk_dd, (struct disklabel *)addr,
1189 /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart : */0,
1190 &du->dk_cpd);
1191 }
1192 if (error == 0) {
1193 du->dk_flags |= DKFL_BSDLABEL;
1194 wdsetctlr(dev, du);
1195 }
1196 break;
1197
1198 case DIOCWLABEL:
1199 du->dk_flags &= ~DKFL_WRITEPROT;
1200 if ((flag & FWRITE) == 0)
1201 error = EBADF;
1202 else
1203 du->dk_wlabel = *(int *)addr;
1204 break;
1205
1206 case DIOCWDINFO:
1207 du->dk_flags &= ~DKFL_WRITEPROT;
1208 if ((flag & FWRITE) == 0)
1209 error = EBADF;
1210 else if ((error = setdisklabel(&du->dk_dd,
1211 (struct disklabel *)addr,
1212 /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/0,
1213 &du->dk_cpd)) == 0) {
1214 int wlab;
1215
1216 du->dk_flags |= DKFL_BSDLABEL;
1217 wdsetctlr(dev, du);
1218
1219 /* simulate opening partition 0 so write succeeds */
1220 du->dk_openpart |= (1 << 0); /* XXX */
1221 wlab = du->dk_wlabel;
1222 du->dk_wlabel = 1;
1223 error = writedisklabel(dev, wdstrategy, &du->dk_dd, &du->dk_cpd);
1224 du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
1225 du->dk_wlabel = wlab;
1226 }
1227 break;
1228
1229 #ifdef notyet
1230 case DIOCGDINFOP:
1231 *(struct disklabel **)addr = &du->dk_dd;
1232 break;
1233
1234 case DIOCWFORMAT:
1235 if ((flag & FWRITE) == 0)
1236 error = EBADF;
1237 else {
1238 register struct format_op *fop;
1239
1240 fop = (struct format_op *)addr;
1241 aiov.iov_base = fop->df_buf;
1242 aiov.iov_len = fop->df_count;
1243 auio.uio_iov = &aiov;
1244 auio.uio_iovcnt = 1;
1245 auio.uio_resid = fop->df_count;
1246 auio.uio_segflg = 0;
1247 auio.uio_offset =
1248 fop->df_startblk * du->dk_dd.d_secsize;
1249 error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
1250 minphys, &auio);
1251 fop->df_count -= auio.uio_resid;
1252 fop->df_reg[0] = du->dk_status;
1253 fop->df_reg[1] = du->dk_error;
1254 }
1255 break;
1256 #endif
1257
1258 default:
1259 error = ENOTTY;
1260 break;
1261 }
1262 return error;
1263 }
1264
1265 #ifdef B_FORMAT
1266 int
1267 wdformat(struct buf *bp)
1268 {
1269
1270 bp->b_flags |= B_FORMAT;
1271 return wdstrategy(bp);
1272 }
1273 #endif
1274
1275 int
1276 wdsize(dev_t dev)
1277 {
1278 int lunit = wdunit(dev), part = wdpart(dev);
1279 struct disk *du;
1280
1281 if (lunit >= NWD)
1282 return -1;
1283 du = wddrives[lunit];
1284 if (du == 0)
1285 return -1;
1286
1287 if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
1288 int val;
1289 val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
1290 S_IFBLK, 0);
1291 if (val != 0)
1292 return -1;
1293 }
1294
1295 if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
1296 return -1;
1297
1298 return (int)du->dk_dd.d_partitions[part].p_size;
1299 }
1300
1301 extern char *vmmap; /* poor name! */
1302
1303 /* dump core after a system crash */
1304 int
1305 wddump(dev_t dev)
1306 {
1307 register struct disk *du; /* disk unit to do the IO */
1308 long num; /* number of sectors to write */
1309 int ctrlr, lunit, part, wdc;
1310 long blkoff, blknum;
1311 long cylin, head, sector, stat;
1312 long secpertrk, secpercyl, nblocks, i;
1313 char *addr;
1314 static wddoingadump = 0;
1315 extern caddr_t CADDR1;
1316 extern struct pte *CMAP1;
1317
1318 addr = (char *)0; /* starting address */
1319
1320 #if DO_NOT_KNOW_HOW
1321 /* toss any characters present prior to dump, ie. non-blocking getc */
1322 while (cngetc())
1323 ;
1324 #endif
1325
1326 /* size of memory to dump */
1327 lunit = wdunit(dev);
1328 part = wdpart(dev); /* file system */
1329 /* check for acceptable drive number */
1330 if (lunit >= NWD)
1331 return ENXIO;
1332 du = wddrives[lunit];
1333 /* was it ever initialized ? */
1334 if (du == 0 || du->dk_state < OPEN || du->dk_flags & DKFL_WRITEPROT)
1335 return ENXIO;
1336
1337 wdc = du->dk_port;
1338 ctrlr = du->dk_ctrlr;
1339
1340 /* Convert to disk sectors */
1341 num = ctob(physmem) / du->dk_dd.d_secsize;
1342
1343 /* check if controller active */
1344 /*if (wdtab[ctrlr].b_active)
1345 return EFAULT;*/
1346 if (wddoingadump)
1347 return EFAULT;
1348
1349 secpertrk = du->dk_dd.d_nsectors;
1350 secpercyl = du->dk_dd.d_secpercyl;
1351 nblocks = du->dk_dd.d_partitions[part].p_size;
1352 blkoff = du->dk_dd.d_partitions[part].p_offset;
1353
1354 /*pg("xunit %x, nblocks %d, dumplo %d num %d\n", part, nblocks, dumplo,
1355 num);*/
1356 /* check transfer bounds against partition size */
1357 if (dumplo < 0 || dumplo + num > nblocks)
1358 return EINVAL;
1359
1360 /* mark controller active for if we panic during the dump */
1361 /*wdtab[ctrlr].b_active = 1;*/
1362 wddoingadump = 1;
1363 i = 200000000;
1364 while ((inb(wdc+wd_status) & WDCS_BUSY) && (i-- > 0))
1365 ;
1366 outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4));
1367 outb(wdc+wd_command, WDCC_RESTORE | WD_STEP);
1368 while (inb(wdc+wd_status) & WDCS_BUSY)
1369 ;
1370
1371 /* some compaq controllers require this ... */
1372 wdsetctlr(dev, du);
1373
1374 blknum = dumplo + blkoff;
1375 while (num > 0) {
1376 /* compute disk address */
1377 cylin = blknum / secpercyl;
1378 head = (blknum % secpercyl) / secpertrk;
1379 sector = blknum % secpertrk;
1380
1381 if (du->dk_flags & DKFL_BADSECT) {
1382 long newblk;
1383 int i;
1384
1385 for (i = 0; du->dk_badsect[i] != -1; i++) {
1386 if (blknum < du->dk_badsect[i]) {
1387 /* sorted list, passed our block by */
1388 break;
1389 } else if (blknum == du->dk_badsect[i]) {
1390 newblk = du->dk_dd.d_secperunit -
1391 du->dk_dd.d_nsectors - i - 1;
1392 cylin = newblk / secpercyl;
1393 head = (newblk % secpercyl) / secpertrk;
1394 sector = newblk % secpertrk;
1395 /* found and replaced; done */
1396 break;
1397 }
1398 }
1399 }
1400 sector++; /* origin 1 */
1401
1402 /* select drive. */
1403 outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | (head & 0xf));
1404 while ((inb(wdc+wd_status) & WDCS_READY) == 0)
1405 ;
1406
1407 /* transfer some blocks */
1408 outb(wdc+wd_sector, sector);
1409 outb(wdc+wd_seccnt, 1);
1410 outb(wdc+wd_cyl_lo, cylin);
1411 outb(wdc+wd_cyl_hi, cylin >> 8);
1412 #ifdef notdef
1413 /* lets just talk about this first...*/
1414 pg("sdh 0%o sector %d cyl %d addr 0x%x", inb(wdc+wd_sdh),
1415 inb(wdc+wd_sector),
1416 (inb(wdc+wd_cyl_hi) << 8) + inb(wdc+wd_cyl_lo), addr);
1417 #endif
1418 outb(wdc+wd_command, WDCC_WRITE);
1419
1420 #ifdef notdef /* cannot use this since this address was mapped differently */
1421 pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1422 #else
1423 *(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
1424 tlbflush();
1425 #endif
1426
1427 /* Ready to send data? */
1428 while ((inb(wdc+wd_status) & WDCS_DRQ) == 0)
1429 ;
1430 if (inb(wdc+wd_status) & WDCS_ERR)
1431 return EIO;
1432
1433 outsw(wdc+wd_data, CADDR1 + ((int)addr & PGOFSET),
1434 DEV_BSIZE / 2);
1435
1436 if (inb(wdc+wd_status) & WDCS_ERR)
1437 return EIO;
1438 /* Check data request (should be done). */
1439 if (inb(wdc+wd_status) & WDCS_DRQ)
1440 return EIO;
1441
1442 /* wait for completion */
1443 for (i = 200000000; inb(wdc+wd_status) & WDCS_BUSY; i--) {
1444 if (i < 0)
1445 return EIO;
1446 }
1447
1448 /* error check the xfer */
1449 if (inb(wdc+wd_status) & WDCS_ERR)
1450 return EIO;
1451
1452 if ((unsigned)addr % 1048576 == 0)
1453 printf("%d ", num / (1048576 / DEV_BSIZE));
1454
1455 /* update block count */
1456 num--;
1457 blknum++;
1458 (int)addr += DEV_BSIZE;
1459
1460 #if DO_NOT_KNOW_HOW
1461 /* operator aborting dump? non-blocking getc() */
1462 if (cngetc())
1463 return EINTR;
1464 #endif
1465 }
1466 return 0;
1467 }
1468
1469 /*
1470 * Internalize the bad sector table.
1471 */
1472 void
1473 bad144intern(struct disk *du)
1474 {
1475 int i;
1476 if (du->dk_flags & DKFL_BADSECT) {
1477 for (i = 0; i < 127; i++)
1478 du->dk_badsect[i] = -1;
1479 for (i = 0; i < 126; i++) {
1480 if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
1481 break;
1482 du->dk_badsect[i] =
1483 du->dk_cpd.bad.bt_bad[i].bt_cyl *
1484 du->dk_dd.d_secpercyl +
1485 (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
1486 du->dk_dd.d_nsectors +
1487 (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
1488 }
1489 }
1490 }
1491
1492 static int
1493 wdreset(int ctrlr, int wdc, int err)
1494 {
1495 int stat;
1496
1497 if (err)
1498 printf("wdc%d: busy too long, resetting\n", ctrlr);
1499
1500 /* reset the device */
1501 outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1502 DELAY(1000);
1503 outb(wdc+wd_ctlr, WDCTL_4BIT);
1504
1505 if (wait_for_unbusy(wdc, ctrlr) == -1)
1506 printf("wdc%d: failed to reset controller\n", ctrlr);
1507 }
1508
1509 int
1510 wdc_wait(port, ctrlr, on, off)
1511 int port, ctrlr;
1512 int on, off;
1513 {
1514 int timeout = 0;
1515 int stat;
1516
1517 for (;;) {
1518 stat = inb(port);
1519 if ((stat & on) != on || (stat & off) != 0)
1520 break;
1521 DELAY(WDCDELAY);
1522 if (++timeout > WDCNDELAY)
1523 return -1;
1524 }
1525 #ifdef WDCNDELAY_DEBUG
1526 if (timeout > WDCNDELAY_DEBUG)
1527 printf("wdc%d: timeout took %dus\n", ctrlr, WDCDELAY * timeout);
1528 #endif
1529 return stat;
1530 }
1531
1532 static int
1533 wdtimeout(caddr_t arg)
1534 {
1535 int x = splbio();
1536 register int unit = (int)arg;
1537
1538 if (wdtimeoutstatus[unit] && --wdtimeoutstatus[unit] == 0) {
1539 struct disk *du = wddrives[unit];
1540 int wdc = du->dk_port;
1541
1542 printf("wd%d: lost interrupt - status %x, error %x\n",
1543 unit, inb(wdc+wd_status), inb(wdc+wd_error));
1544 outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1545 DELAY(1000);
1546 outb(wdc+wd_ctlr, WDCTL_IDS);
1547 DELAY(1000);
1548 (void) inb(wdc+wd_error);
1549 outb(wdc+wd_ctlr, WDCTL_4BIT);
1550 du->dk_skip = 0;
1551 du->dk_flags |= DKFL_SINGLE;
1552 wdstart(du->dk_ctrlr); /* start controller */
1553 }
1554 timeout((timeout_t)wdtimeout, (caddr_t)unit, hz/2);
1555 splx(x);
1556 return 0;
1557 }
1558
1559 #endif /* NWDC > 0 */
1560