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