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