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