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