wd.c revision 1.112 1 /* $NetBSD: wd.c,v 1.112 1994/11/22 05:50:48 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1994 Charles Hannum.
5 * Copyright (c) 1990 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)wd.c 7.2 (Berkeley) 5/9/91
40 */
41
42 #define INSTRUMENT /* instrumentation stuff by Brad Parker */
43
44 #include <sys/param.h>
45 #include <sys/dkbad.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/conf.h>
49 #include <sys/file.h>
50 #include <sys/stat.h>
51 #include <sys/ioctl.h>
52 #include <sys/buf.h>
53 #include <sys/uio.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56 #include <sys/disklabel.h>
57 #include <sys/disk.h>
58 #include <sys/syslog.h>
59 #ifdef INSTRUMENT
60 #include <sys/dkstat.h>
61 #endif
62
63 #include <vm/vm.h>
64
65 #include <machine/cpu.h>
66 #include <machine/pio.h>
67
68 #include <i386/isa/isavar.h>
69 #include <i386/isa/wdreg.h>
70
71 #define WDCNDELAY 100000 /* delay = 100us; so 10s for a controller state change */
72 #define WDCDELAY 100
73
74 #define WAITTIME (4 * hz) /* time to wait for a completion */
75 #define RECOVERYTIME (hz / 2) /* time to recover from an error */
76
77 #if 0
78 /* If you enable this, it will report any delays more than 100us * N long. */
79 #define WDCNDELAY_DEBUG 10
80 #endif
81
82 #define WDIORETRIES 5 /* number of retries before giving up */
83
84 #define WDUNIT(dev) DISKUNIT(dev)
85 #define WDPART(dev) DISKPART(dev)
86 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
87
88 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
89
90 #define b_cylin b_resid /* cylinder number for doing IO to */
91 /* shares an entry in the buf struct */
92
93 /*
94 * Drive states. Used to initialize drive.
95 */
96 #define CLOSED 0 /* disk is closed */
97 #define RECAL 1 /* recalibrate */
98 #define RECAL_WAIT 2 /* done recalibrating */
99 #define GEOMETRY 3 /* upload geometry */
100 #define GEOMETRY_WAIT 4 /* done uploading geometry */
101 #define OPEN 5 /* done with open */
102
103 /*
104 * Drive status.
105 */
106 struct wd_softc {
107 struct device sc_dev;
108 struct dkdevice sc_dk;
109
110 long sc_bcount; /* byte count left */
111 long sc_mbcount; /* total byte count left */
112 short sc_skip; /* blocks already transferred */
113 short sc_mskip; /* blocks already transferred for multi */
114 char sc_drive; /* physical unit number */
115 char sc_state; /* control state */
116
117 u_char sc_flags; /* drive characteistics found */
118 #define WDF_LOCKED 0x01
119 #define WDF_WANTED 0x02
120 #define WDF_LOADED 0x04
121 #define WDF_BSDLABEL 0x08 /* has a BSD disk label */
122 #define WDF_WLABEL 0x10 /* label is writable */
123 TAILQ_ENTRY(wd_softc) sc_drivechain;
124 struct buf sc_q;
125 struct wdparams sc_params; /* ESDI/IDE drive/controller parameters */
126 long sc_badsect[127]; /* 126 plus trailing -1 marker */
127 };
128
129 struct wdc_softc {
130 struct device sc_dev;
131 struct intrhand sc_ih;
132
133 u_char sc_flags;
134 #define WDCF_ACTIVE 0x00001 /* controller is active */
135 #define WDCF_SINGLE 0x00004 /* sector at a time mode */
136 #define WDCF_ERROR 0x00008 /* processing a disk error */
137 u_char sc_status; /* copy of status register */
138 u_char sc_error; /* copy of error register */
139 int sc_iobase; /* i/o port base */
140 int sc_errors; /* count of errors during current transfer */
141 TAILQ_HEAD(drivehead, wd_softc) sc_drives;
142 };
143
144 int wdcprobe __P((struct device *, void *, void *));
145 void wdcattach __P((struct device *, struct device *, void *));
146
147 struct cfdriver wdccd = {
148 NULL, "wdc", wdcprobe, wdcattach, DV_DULL, sizeof(struct wd_softc)
149 };
150
151 int wdprobe __P((struct device *, void *, void *));
152 void wdattach __P((struct device *, struct device *, void *));
153
154 struct cfdriver wdcd = {
155 NULL, "wd", wdprobe, wdattach, DV_DISK, sizeof(struct wd_softc)
156 };
157
158 void wdgetdisklabel __P((struct wd_softc *));
159 int wd_get_parms __P((struct wd_softc *));
160 void wdstrategy __P((struct buf *));
161 void wdstart __P((struct wd_softc *));
162
163 struct dkdriver wddkdriver = { wdstrategy };
164
165 void wdfinish __P((struct wd_softc *, struct buf *));
166 int wdcintr __P((struct wdc_softc *));
167 static void wdcstart __P((struct wdc_softc *));
168 static int wdcommand __P((struct wd_softc *, int, int, int, int, int));
169 static int wdcommandshort __P((struct wdc_softc *, int, int));
170 static int wdcontrol __P((struct wd_softc *));
171 static int wdsetctlr __P((struct wd_softc *));
172 static void bad144intern __P((struct wd_softc *));
173 static int wdcreset __P((struct wdc_softc *));
174 static void wdcrestart __P((void *arg));
175 static void wdcunwedge __P((struct wdc_softc *));
176 static void wdctimeout __P((void *arg));
177 void wddisksort __P((struct buf *, struct buf *));
178 static void wderror __P((void *, struct buf *, char *));
179 int wdcwait __P((struct wdc_softc *, int));
180 /* ST506 spec says that if READY or SEEKCMPLT go off, then the read or write
181 command is aborted. */
182 #define wait_for_drq(d) wdcwait(d, WDCS_DRDY | WDCS_DSC | WDCS_DRQ)
183 #define wait_for_ready(d) wdcwait(d, WDCS_DRDY | WDCS_DSC)
184 #define wait_for_unbusy(d) wdcwait(d, 0)
185
186 /*
187 * Probe for controller.
188 */
189 int
190 wdcprobe(parent, match, aux)
191 struct device *parent;
192 void *match, *aux;
193 {
194 struct wdc_softc *wdc = match;
195 struct isa_attach_args *ia = aux;
196 int iobase;
197
198 wdc->sc_iobase = iobase = ia->ia_iobase;
199
200 /* Check if we have registers that work. */
201 outb(iobase+wd_error, 0x5a); /* Error register not writable. */
202 outb(iobase+wd_cyl_lo, 0xa5); /* But all of cyllo are implemented. */
203 if (inb(iobase+wd_error) == 0x5a || inb(iobase+wd_cyl_lo) != 0xa5)
204 return 0;
205
206 if (wdcreset(wdc) != 0) {
207 delay(500000);
208 if (wdcreset(wdc) != 0)
209 return 0;
210 }
211
212 outb(iobase+wd_sdh, WDSD_IBM | 0);
213
214 /* Wait for controller to become ready. */
215 if (wait_for_unbusy(wdc) < 0)
216 return 0;
217
218 /* Send command. */
219 outb(iobase+wd_command, WDCC_DIAGNOSE);
220
221 /* Wait for command to complete. */
222 if (wait_for_unbusy(wdc) != 0)
223 return 0;
224
225 ia->ia_iosize = 8;
226 ia->ia_msize = 0;
227 return 1;
228 }
229
230 struct wdc_attach_args {
231 int wa_drive;
232 };
233
234 int
235 wdprint(aux, wdc)
236 void *aux;
237 char *wdc;
238 {
239 struct wdc_attach_args *wa = aux;
240
241 if (!wdc)
242 printf(" drive %d", wa->wa_drive);
243 return QUIET;
244 }
245
246 void
247 wdcattach(parent, self, aux)
248 struct device *parent, *self;
249 void *aux;
250 {
251 struct wdc_softc *wdc = (void *)self;
252 struct isa_attach_args *ia = aux;
253 struct wdc_attach_args wa;
254
255 TAILQ_INIT(&wdc->sc_drives);
256
257 printf("\n");
258
259 wdc->sc_ih.ih_fun = wdcintr;
260 wdc->sc_ih.ih_arg = wdc;
261 wdc->sc_ih.ih_level = IPL_BIO;
262 intr_establish(ia->ia_irq, &wdc->sc_ih);
263
264 for (wa.wa_drive = 0; wa.wa_drive < 2; wa.wa_drive++)
265 (void)config_found(self, (void *)&wa, wdprint);
266 }
267
268 int
269 wdprobe(parent, match, aux)
270 struct device *parent;
271 void *match, *aux;
272 {
273 struct wdc_softc *wdc = (void *)parent;
274 struct cfdata *cf = match;
275 struct wdc_attach_args *wa = aux;
276 int drive = wa->wa_drive;
277
278 if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != drive)
279 return 0;
280
281 if (wdcommandshort(wdc, drive, WDCC_RECAL) != 0 ||
282 wait_for_ready(wdc) != 0)
283 return 0;
284
285 return 1;
286 }
287
288 void
289 wdattach(parent, self, aux)
290 struct device *parent, *self;
291 void *aux;
292 {
293 struct wd_softc *wd = (void *)self;
294 struct wdc_attach_args *wa = aux;
295 int i, blank;
296
297 wd->sc_drive = wa->wa_drive;
298
299 wd_get_parms(wd);
300 printf(": %dMB, %d cyl, %d head, %d sec, %d bytes/sec <",
301 wd->sc_params.wdp_cylinders *
302 (wd->sc_params.wdp_heads * wd->sc_params.wdp_sectors) /
303 (1048576 / DEV_BSIZE),
304 wd->sc_params.wdp_cylinders,
305 wd->sc_params.wdp_heads,
306 wd->sc_params.wdp_sectors,
307 DEV_BSIZE);
308 for (i = blank = 0; i < sizeof(wd->sc_params.wdp_model); i++) {
309 char c = wd->sc_params.wdp_model[i];
310 if (c == '\0')
311 break;
312 if (c != ' ') {
313 if (blank)
314 printf(" %c", c);
315 else
316 printf("%c", c);
317 blank = 0;
318 } else
319 blank = 1;
320 }
321 printf(">\n");
322 wd->sc_dk.dk_driver = &wddkdriver;
323 }
324
325 /*
326 * Read/write routine for a buffer. Finds the proper unit, range checks
327 * arguments, and schedules the transfer. Does not wait for the transfer to
328 * complete. Multi-page transfers are supported. All I/O requests must be a
329 * multiple of a sector in length.
330 */
331 void
332 wdstrategy(bp)
333 struct buf *bp;
334 {
335 struct wd_softc *wd; /* disk unit to do the IO */
336 int unit = WDUNIT(bp->b_dev);
337 int s;
338
339 /* Valid unit, controller, and request? */
340 if (unit >= wdcd.cd_ndevs ||
341 (wd = wdcd.cd_devs[unit]) == 0 ||
342 bp->b_blkno < 0 ||
343 (bp->b_bcount % DEV_BSIZE) != 0 ||
344 (bp->b_bcount / DEV_BSIZE) >= (1 << NBBY)) {
345 bp->b_error = EINVAL;
346 goto bad;
347 }
348
349 #if 0
350 /* "Soft" write protect check. */
351 if ((wd->sc_flags & WDF_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
352 bp->b_error = EROFS;
353 goto bad;
354 }
355 #endif
356
357 /* If it's a null transfer, return immediately. */
358 if (bp->b_bcount == 0)
359 goto done;
360
361 /* Have partitions and want to use them? */
362 if (WDPART(bp->b_dev) != RAW_PART) {
363 if ((wd->sc_flags & WDF_BSDLABEL) == 0) {
364 bp->b_error = EIO;
365 goto bad;
366 }
367 /*
368 * Do bounds checking, adjust transfer. if error, process.
369 * If end of partition, just return.
370 */
371 if (bounds_check_with_label(bp, &wd->sc_dk.dk_label,
372 (wd->sc_flags & WDF_WLABEL) != 0) <= 0)
373 goto done;
374 /* Otherwise, process transfer request. */
375 }
376
377 /* Don't bother doing rotational optimization. */
378 bp->b_cylin = 0;
379
380 /* Queue transfer on drive, activate drive and controller if idle. */
381 s = splbio();
382 wddisksort(&wd->sc_q, bp);
383 if (!wd->sc_q.b_active)
384 wdstart(wd); /* Start drive. */
385 #ifdef DIAGNOSTIC
386 else {
387 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
388 if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
389 printf("wdstrategy: controller inactive\n");
390 wdcstart(wdc);
391 }
392 }
393 #endif
394 splx(s);
395 return;
396
397 bad:
398 bp->b_flags |= B_ERROR;
399 done:
400 /* Toss transfer; we're done early. */
401 biodone(bp);
402 }
403
404 /*
405 * Need to skip over multitransfer bufs.
406 */
407 void
408 wddisksort(dp, bp)
409 struct buf *dp, *bp;
410 {
411 struct buf *ap;
412
413 while ((ap = dp->b_actf) && ap->b_flags & B_XXX)
414 dp = ap;
415 disksort(dp, bp);
416 }
417
418 /*
419 * Routine to queue a command to the controller. The unit's request is linked
420 * into the active list for the controller. If the controller is idle, the
421 * transfer is started.
422 */
423 void
424 wdstart(wd)
425 struct wd_softc *wd;
426 {
427 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
428 int active = wdc->sc_drives.tqh_first != 0;
429
430 /* Link onto controller queue. */
431 wd->sc_q.b_active = 1;
432 TAILQ_INSERT_TAIL(&wdc->sc_drives, wd, sc_drivechain);
433
434 /* If controller not already active, start it. */
435 if (!active)
436 wdcstart(wdc);
437 }
438
439 void
440 wdfinish(wd, bp)
441 struct wd_softc *wd;
442 struct buf *bp;
443 {
444 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
445
446 #ifdef INSTRUMENT
447 dk_busy &= ~(1 << wd->sc_dev.dv_unit);
448 #endif
449 wdc->sc_flags &= ~(WDCF_SINGLE | WDCF_ERROR);
450 wdc->sc_errors = 0;
451 /*
452 * If this is the only buf or the last buf in the transfer (taking into
453 * account any residual, in case we erred)...
454 */
455 wd->sc_mbcount -= wd->sc_bcount;
456 if (wd->sc_mbcount == 0) {
457 wd->sc_mskip = 0;
458 /*
459 * ...then move this drive to the end of the queue to give
460 * others a `fair' chance.
461 */
462 if (wd->sc_drivechain.tqe_next) {
463 TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
464 if (bp->b_actf) {
465 TAILQ_INSERT_TAIL(&wdc->sc_drives, wd,
466 sc_drivechain);
467 } else
468 wd->sc_q.b_active = 0;
469 }
470 }
471 bp->b_resid = wd->sc_bcount;
472 bp->b_flags &= ~B_XXX;
473 wd->sc_skip = 0;
474 wd->sc_q.b_actf = bp->b_actf;
475 biodone(bp);
476 }
477
478 /*
479 * Controller startup routine. This does the calculation, and starts a
480 * single-sector read or write operation. Called to start a transfer, or from
481 * the interrupt routine to continue a multi-sector transfer.
482 * RESTRICTIONS:
483 * 1. The transfer length must be an exact multiple of the sector size.
484 */
485 static void
486 wdcstart(wdc)
487 struct wdc_softc *wdc;
488 {
489 struct wd_softc *wd; /* disk unit for IO */
490 struct buf *bp;
491
492 loop:
493 /* Is there a drive for the controller to do a transfer with? */
494 wd = wdc->sc_drives.tqh_first;
495 if (wd == NULL)
496 return;
497
498 /* Is there a transfer to this drive? If not, deactivate drive. */
499 bp = wd->sc_q.b_actf;
500 if (bp == NULL) {
501 TAILQ_REMOVE(&wdc->sc_drives, wd, sc_drivechain);
502 wd->sc_q.b_active = 0;
503 goto loop;
504 }
505
506 if (wdc->sc_errors >= WDIORETRIES) {
507 wderror(wd, bp, "hard error");
508 bp->b_error = EIO;
509 bp->b_flags |= B_ERROR;
510 wdfinish(wd, bp);
511 goto loop;
512 }
513
514 /* Mark the controller active and set a timeout. */
515 if (wdc->sc_flags & WDCF_ACTIVE)
516 untimeout(wdctimeout, wdc);
517 else
518 wdc->sc_flags |= WDCF_ACTIVE;
519 timeout(wdctimeout, wdc, WAITTIME);
520
521 /* Do control operations specially. */
522 if (wd->sc_state < OPEN) {
523 /*
524 * Actually, we want to be careful not to mess with the control
525 * state if the device is currently busy, but we can assume
526 * that we never get to this point if that's the case.
527 */
528 (void) wdcontrol(wd);
529 return;
530 }
531
532 /*
533 * WDCF_ERROR is set by wdcunwedge() and wdcintr() when an error is
534 * encountered. If we are in multi-sector mode, then we switch to
535 * single-sector mode and retry the operation from the start.
536 */
537 if (wdc->sc_flags & WDCF_ERROR) {
538 wdc->sc_flags &= ~WDCF_ERROR;
539 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
540 wdc->sc_flags |= WDCF_SINGLE;
541 wd->sc_skip = wd->sc_mskip = 0;
542 }
543 }
544
545 if (wd->sc_skip == 0) {
546 #ifdef WDDEBUG
547 printf("\n%s: wdcstart %s %d@%d; map ", wd->sc_dev.dv_xname,
548 (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
549 bp->b_blkno);
550 #endif
551 wd->sc_bcount = bp->b_bcount;
552 #ifdef INSTRUMENT
553 dk_busy |= (1 << wd->sc_dev.dv_unit);
554 dk_wds[wd->sc_dev.dv_unit] += bp->b_bcount >> 6;
555 #endif
556 } else {
557 #ifdef WDDEBUG
558 printf(" %d)%x", wd->sc_skip, inb(wd->sc_iobase+wd_altsts));
559 #endif
560 }
561
562 if (wd->sc_mskip == 0) {
563 wd->sc_mbcount = wd->sc_bcount;
564 /* If multi-sector, try to cluster. */
565 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
566 struct buf *oldbp, *nextbp;
567 oldbp = bp;
568 nextbp = bp->b_actf;
569 oldbp->b_flags |= B_XXX;
570 while (nextbp && oldbp->b_dev == nextbp->b_dev &&
571 nextbp->b_blkno ==
572 (oldbp->b_blkno + (oldbp->b_bcount / wd->sc_dk.dk_label.d_secsize)) &&
573 (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
574 if ((wd->sc_mbcount + nextbp->b_bcount) / wd->sc_dk.dk_label.d_secsize >= 240)
575 break;
576 wd->sc_mbcount += nextbp->b_bcount;
577 nextbp->b_flags |= B_XXX;
578 oldbp = nextbp;
579 nextbp = nextbp->b_actf;
580 }
581 }
582 }
583
584 /* If starting a multisector transfer, or doing single transfers. */
585 if (wd->sc_mskip == 0 || (wdc->sc_flags & WDCF_SINGLE)) {
586 struct disklabel *lp;
587 long blkno, nblks;
588 long cylin, head, sector;
589 int command;
590
591 lp = &wd->sc_dk.dk_label;
592 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE) + wd->sc_skip;
593 if (WDPART(bp->b_dev) != RAW_PART)
594 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
595 if (wdc->sc_flags & WDCF_SINGLE)
596 nblks = 1;
597 else
598 nblks = howmany(wd->sc_mbcount, lp->d_secsize);
599
600 /*
601 * Check for bad sectors if we have them, and not formatting. Only do
602 * this in single-sector mode, or when starting a multiple-sector
603 * transfer.
604 */
605 if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0
606 #ifdef B_FORMAT
607 && (bp->b_flags & B_FORMAT) == 0
608 #endif
609 ) {
610 long blkdiff;
611 int i;
612
613 for (i = 0; (blkdiff = wd->sc_badsect[i]) != -1; i++) {
614 blkdiff -= blkno;
615 if (blkdiff < 0)
616 continue;
617 if (blkdiff >= nblks)
618 break;
619 if (blkdiff == 0) {
620 /* Replace current block of transfer. */
621 blkno =
622 lp->d_secperunit - lp->d_nsectors - i - 1;
623 if (wdc->sc_flags & WDCF_SINGLE)
624 break;
625 } else {
626 /* If clustered, try unclustering. */
627 if (wd->sc_mbcount > wd->sc_bcount) {
628 nblks = howmany(wd->sc_bcount, lp->d_secsize);
629 if (blkdiff >= nblks) {
630 /* Unclustering was enough. */
631 wd->sc_mbcount = wd->sc_bcount;
632 break;
633 }
634 }
635 /* Bad block inside transfer. */
636 }
637 /* Force single-sector mode. */
638 wd->sc_mbcount = wd->sc_bcount;
639 wdc->sc_flags |= WDCF_SINGLE;
640 nblks = 1;
641 }
642 /* Tranfer is okay now. */
643 }
644
645 cylin = blkno / lp->d_secpercyl;
646 head = (blkno % lp->d_secpercyl) / lp->d_nsectors;
647 sector = blkno % lp->d_nsectors;
648 sector++; /* Sectors begin with 1, not 0. */
649
650 #ifdef INSTRUMENT
651 ++dk_seek[wd->sc_dev.dv_unit];
652 ++dk_xfer[wd->sc_dev.dv_unit];
653 #endif
654
655 #ifdef B_FORMAT
656 if (bp->b_flags & B_FORMAT) {
657 sector = lp->d_gap3;
658 nblks = lp->d_nsectors;
659 command = WDCC_FORMAT;
660 } else
661 #endif
662 command =
663 (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
664
665 /* Initiate command! */
666 if (wdcommand(wd, command, cylin, head, sector, nblks) != 0) {
667 wderror(wd, NULL,
668 "wdcstart: timeout waiting for unbusy");
669 wdcunwedge(wdc);
670 return;
671 }
672 #ifdef WDDEBUG
673 printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
674 cylin, head, bp->b_data, inb(wd->sc_iobase+wd_altsts));
675 #endif
676 }
677
678 /* If this is a read operation, just go away until it's done. */
679 if (bp->b_flags & B_READ)
680 return;
681
682 if (wait_for_drq(wdc) < 0) {
683 wderror(wd, NULL, "wdcstart: timeout waiting for drq");
684 wdcunwedge(wdc);
685 return;
686 }
687
688 /* Then send it! */
689 outsw(wdc->sc_iobase+wd_data, bp->b_data + wd->sc_skip * DEV_BSIZE,
690 DEV_BSIZE / sizeof(short));
691 }
692
693 /*
694 * Interrupt routine for the controller. Acknowledge the interrupt, check for
695 * errors on the current operation, mark it done if necessary, and start the
696 * next request. Also check for a partially done transfer, and continue with
697 * the next chunk if so.
698 */
699 int
700 wdcintr(wdc)
701 struct wdc_softc *wdc;
702 {
703 struct wd_softc *wd;
704 struct buf *bp;
705
706 if ((wdc->sc_flags & WDCF_ACTIVE) == 0) {
707 /* Clear the pending interrupt. */
708 (void) inb(wdc->sc_iobase+wd_status);
709 return 0;
710 }
711
712 wd = wdc->sc_drives.tqh_first;
713 bp = wd->sc_q.b_actf;
714
715 #ifdef WDDEBUG
716 printf("I%d ", ctrlr);
717 #endif
718
719 if (wait_for_unbusy(wdc) < 0) {
720 wderror(wd, NULL, "wdcintr: timeout waiting for unbusy");
721 wdc->sc_status |= WDCS_ERR; /* XXX */
722 }
723
724 /* Is it not a transfer, but a control operation? */
725 if (wd->sc_state < OPEN) {
726 if (wdcontrol(wd))
727 wdcstart(wdc);
728 return 1;
729 }
730
731 wdc->sc_flags &= ~WDCF_ACTIVE;
732 untimeout(wdctimeout, wdc);
733
734 /* Have we an error? */
735 if (wdc->sc_status & WDCS_ERR) {
736 lose:
737 #ifdef WDDEBUG
738 wderror(wd, NULL, "wdcintr");
739 #endif
740 if ((wdc->sc_flags & WDCF_SINGLE) == 0) {
741 wdc->sc_flags |= WDCF_ERROR;
742 goto restart;
743 }
744
745 #ifdef B_FORMAT
746 if (bp->b_flags & B_FORMAT)
747 goto bad;
748 #endif
749
750 if (++wdc->sc_errors < WDIORETRIES)
751 goto restart;
752 wderror(wd, bp, "hard error");
753
754 bad:
755 bp->b_error = EIO;
756 bp->b_flags |= B_ERROR;
757 goto done;
758 }
759
760 if (wdc->sc_status & WDCS_CORR)
761 wderror(wd, bp, "soft ecc");
762
763 /* If this was a read, fetch the data. */
764 if (bp->b_flags & B_READ) {
765 /* Ready to receive data? */
766 if ((wdc->sc_status & (WDCS_DRDY | WDCS_DSC | WDCS_DRQ))
767 != (WDCS_DRDY | WDCS_DSC | WDCS_DRQ)) {
768 wderror(wd, NULL, "wdcintr: read intr before drq");
769 wdcunwedge(wdc);
770 return 1;
771 }
772
773 /* Suck in data. */
774 insw(wdc->sc_iobase+wd_data,
775 bp->b_data + wd->sc_skip * DEV_BSIZE,
776 DEV_BSIZE / sizeof(short));
777 }
778
779 /* If we encountered any abnormalities, flag it as a soft error. */
780 if (wdc->sc_errors) {
781 wderror(wd, bp, "soft error");
782 wdc->sc_errors = 0;
783 }
784
785 /* Ready for the next block, if any. */
786 wd->sc_skip++;
787 wd->sc_mskip++;
788 wd->sc_bcount -= DEV_BSIZE;
789 wd->sc_mbcount -= DEV_BSIZE;
790
791 /* See if more to transfer. */
792 if (wd->sc_bcount > 0)
793 goto restart;
794
795 done:
796 /* Done with this transfer, with or without error. */
797 wdfinish(wd, bp);
798
799 restart:
800 /* Start the next transfer, if any. */
801 wdcstart(wdc);
802
803 return 1;
804 }
805
806 /*
807 * Initialize a drive.
808 */
809 int
810 wdopen(dev, flag, fmt)
811 dev_t dev;
812 int flag, fmt;
813 {
814 int error;
815 int unit, part;
816 struct wd_softc *wd;
817
818 unit = WDUNIT(dev);
819 if (unit >= wdcd.cd_ndevs)
820 return ENXIO;
821 wd = wdcd.cd_devs[unit];
822 if (wd == 0)
823 return ENXIO;
824
825 part = WDPART(dev);
826
827 while ((wd->sc_flags & WDF_LOCKED) != 0) {
828 wd->sc_flags |= WDF_WANTED;
829 if ((error = tsleep(wd, PRIBIO | PCATCH, "wdopn", 0)) != 0)
830 return error;
831 }
832
833 if (wd->sc_dk.dk_openmask != 0) {
834 /*
835 * If any partition is open, but the disk has been invalidated,
836 * disallow further opens.
837 */
838 if ((wd->sc_flags & WDF_LOADED) == 0)
839 return ENXIO;
840 } else {
841 wd->sc_flags |= WDF_LOCKED;
842
843 if ((wd->sc_flags & WDF_LOADED) == 0) {
844 wd->sc_flags &= ~WDF_BSDLABEL;
845 wd->sc_flags |= WDF_LOADED;
846
847 /* Load the physical device parameters. */
848 if (wd_get_parms(wd) != 0) {
849 error = ENXIO;
850 goto bad2;
851 }
852
853 /* Load the partition info if not already loaded. */
854 wdgetdisklabel(wd);
855 }
856
857 wd->sc_flags &= ~WDF_LOCKED;
858 if ((wd->sc_flags & WDF_WANTED) != 0) {
859 wd->sc_flags &= ~WDF_WANTED;
860 wakeup(wd);
861 }
862 }
863
864 /* Check that the partition exists. */
865 if (part != RAW_PART &&
866 (part >= wd->sc_dk.dk_label.d_npartitions ||
867 wd->sc_dk.dk_label.d_partitions[part].p_fstype == FS_UNUSED)) {
868 error = ENXIO;
869 goto bad;
870 }
871
872 /* Insure only one open at a time. */
873 switch (fmt) {
874 case S_IFCHR:
875 wd->sc_dk.dk_copenmask |= (1 << part);
876 break;
877 case S_IFBLK:
878 wd->sc_dk.dk_bopenmask |= (1 << part);
879 break;
880 }
881 wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
882
883 return 0;
884
885 bad2:
886 wd->sc_flags &= ~WDF_LOADED;
887
888 bad:
889 wd->sc_flags &= ~WDF_LOCKED;
890 if ((wd->sc_flags & WDF_WANTED) != 0) {
891 wd->sc_flags &= ~WDF_WANTED;
892 wakeup(wd);
893 }
894
895 return error;
896 }
897
898 void
899 wdgetdisklabel(wd)
900 struct wd_softc *wd;
901 {
902 char *errstring;
903
904 if ((wd->sc_flags & WDF_BSDLABEL) != 0)
905 return;
906
907 bzero(&wd->sc_dk.dk_label, sizeof(struct disklabel));
908 bzero(&wd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
909
910 wd->sc_dk.dk_label.d_secsize = DEV_BSIZE;
911 wd->sc_dk.dk_label.d_ntracks = wd->sc_params.wdp_heads;
912 wd->sc_dk.dk_label.d_nsectors = wd->sc_params.wdp_sectors;
913 wd->sc_dk.dk_label.d_ncylinders = wd->sc_params.wdp_cylinders;
914 wd->sc_dk.dk_label.d_secpercyl =
915 wd->sc_dk.dk_label.d_ntracks * wd->sc_dk.dk_label.d_nsectors;
916
917 #if 0
918 strncpy(wd->sc_dk.dk_label.d_typename, "ST506 disk", 16);
919 wd->sc_dk.dk_label.d_type = DTYPE_ST506;
920 #endif
921 strncpy(wd->sc_dk.dk_label.d_packname, wd->sc_params.wdp_model, 16);
922 wd->sc_dk.dk_label.d_secperunit =
923 wd->sc_dk.dk_label.d_secpercyl * wd->sc_dk.dk_label.d_ncylinders;
924 wd->sc_dk.dk_label.d_rpm = 3600;
925 wd->sc_dk.dk_label.d_interleave = 1;
926 wd->sc_dk.dk_label.d_flags = 0;
927
928 wd->sc_dk.dk_label.d_partitions[RAW_PART].p_offset = 0;
929 wd->sc_dk.dk_label.d_partitions[RAW_PART].p_size =
930 wd->sc_dk.dk_label.d_secperunit *
931 (wd->sc_dk.dk_label.d_secsize / DEV_BSIZE);
932 wd->sc_dk.dk_label.d_partitions[RAW_PART].p_fstype = FS_UNUSED;
933 wd->sc_dk.dk_label.d_npartitions = RAW_PART + 1;
934
935 wd->sc_dk.dk_label.d_magic = DISKMAGIC;
936 wd->sc_dk.dk_label.d_magic2 = DISKMAGIC;
937 wd->sc_dk.dk_label.d_checksum = dkcksum(&wd->sc_dk.dk_label);
938
939 if (wd->sc_state > RECAL)
940 wd->sc_state = RECAL;
941 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
942 wdstrategy, &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
943 if (errstring) {
944 /*
945 * This probably happened because the drive's default
946 * geometry doesn't match the DOS geometry. We
947 * assume the DOS geometry is now in the label and try
948 * again. XXX This is a kluge.
949 */
950 if (wd->sc_state > GEOMETRY)
951 wd->sc_state = GEOMETRY;
952 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
953 wdstrategy, &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
954 }
955 if (errstring) {
956 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
957 return;
958 }
959
960 if (wd->sc_state > GEOMETRY)
961 wd->sc_state = GEOMETRY;
962 if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0)
963 bad144intern(wd);
964
965 wd->sc_flags |= WDF_BSDLABEL;
966 }
967
968 /*
969 * Implement operations other than read/write.
970 * Called from wdcstart or wdcintr during opens and formats.
971 * Uses finite-state-machine to track progress of operation in progress.
972 * Returns 0 if operation still in progress, 1 if completed.
973 */
974 static int
975 wdcontrol(wd)
976 struct wd_softc *wd;
977 {
978 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
979
980 switch (wd->sc_state) {
981 case RECAL: /* Set SDH, step rate, do recal. */
982 if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0) {
983 wderror(wd, NULL, "wdcontrol: wdcommandshort failed");
984 wdcunwedge(wdc);
985 return 0;
986 }
987 wd->sc_state = RECAL_WAIT;
988 return 0;
989
990 case RECAL_WAIT:
991 if (wdc->sc_status & WDCS_ERR) {
992 wderror(wd, NULL, "wdcontrol: recal failed");
993 wdcunwedge(wdc);
994 return 0;
995 }
996 /* fall through */
997 case GEOMETRY:
998 if (wdsetctlr(wd) != 0) {
999 /* Already printed a message. */
1000 wdcunwedge(wdc);
1001 return 0;
1002 }
1003 wd->sc_state = GEOMETRY_WAIT;
1004 return 0;
1005
1006 case GEOMETRY_WAIT:
1007 if (wdc->sc_status & WDCS_ERR) {
1008 wderror(wd, NULL, "wdcontrol: geometry failed");
1009 wdcunwedge(wdc);
1010 return 0;
1011 }
1012
1013 wdc->sc_errors = 0;
1014 wd->sc_state = OPEN;
1015 /*
1016 * The rest of the initialization can be done by normal means.
1017 */
1018 return 1;
1019 }
1020
1021 #ifdef DIAGNOSTIC
1022 panic("wdcontrol: impossible");
1023 #endif
1024 }
1025
1026 /*
1027 * Send a command and wait uninterruptibly until controller is finished.
1028 * Return -1 if controller busy for too long, otherwise return non-zero if
1029 * error. Intended for brief controller commands at critical points.
1030 * Assumes interrupts are blocked.
1031 */
1032 static int
1033 wdcommand(wd, command, cylin, head, sector, count)
1034 struct wd_softc *wd;
1035 int command;
1036 int cylin, head, sector, count;
1037 {
1038 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1039 int iobase = wdc->sc_iobase;
1040 int stat;
1041
1042 /* Select drive. */
1043 outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_drive << 4) | head);
1044
1045 /* Wait for it to become ready to accept a command. */
1046 if (command == WDCC_IDP)
1047 stat = wait_for_unbusy(wdc);
1048 else
1049 stat = wdcwait(wdc, WDCS_DRDY);
1050 if (stat < 0)
1051 return -1;
1052
1053 /* Load parameters. */
1054 if (wd->sc_dk.dk_label.d_type == DTYPE_ST506)
1055 outb(iobase+wd_precomp, wd->sc_dk.dk_label.d_precompcyl / 4);
1056 else
1057 outb(iobase+wd_features, 0);
1058 outb(iobase+wd_cyl_lo, cylin);
1059 outb(iobase+wd_cyl_hi, cylin >> 8);
1060 outb(iobase+wd_sector, sector);
1061 outb(iobase+wd_seccnt, count);
1062
1063 /* Send command. */
1064 outb(iobase+wd_command, command);
1065
1066 return 0;
1067 }
1068
1069 int
1070 wdcommandshort(wdc, drive, command)
1071 struct wdc_softc *wdc;
1072 int drive;
1073 int command;
1074 {
1075 int iobase = wdc->sc_iobase;
1076
1077 /* Select drive. */
1078 outb(iobase+wd_sdh, WDSD_IBM | (drive << 4));
1079
1080 if (wdcwait(wdc, WDCS_DRDY) < 0)
1081 return -1;
1082
1083 outb(iobase+wd_command, command);
1084
1085 return 0;
1086 }
1087
1088 /*
1089 * Issue IDP to drive to tell it just what geometry it is to be.
1090 */
1091 static int
1092 wdsetctlr(wd)
1093 struct wd_softc *wd;
1094 {
1095 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1096
1097 #ifdef WDDEBUG
1098 printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_dev.dv_unit, wd->sc_drive,
1099 wd->sc_dk.dk_label.d_ncylinders, wd->sc_dk.dk_label.d_ntracks,
1100 wd->sc_dk.dk_label.d_nsectors);
1101 #endif
1102
1103 if (wdcommand(wd, WDCC_IDP, wd->sc_dk.dk_label.d_ncylinders,
1104 wd->sc_dk.dk_label.d_ntracks - 1, 0, wd->sc_dk.dk_label.d_nsectors)
1105 != 0) {
1106 wderror(wd, NULL, "wdsetctlr: geometry upload failed");
1107 return -1;
1108 }
1109
1110 return 0;
1111 }
1112
1113 /*
1114 * Issue IDENTIFY to drive to ask it what it is.
1115 */
1116 int
1117 wd_get_parms(wd)
1118 struct wd_softc *wd;
1119 {
1120 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1121 int i;
1122 char tb[DEV_BSIZE];
1123
1124 if (wdcommandshort(wdc, wd->sc_drive, WDCC_IDENTIFY) != 0 ||
1125 wait_for_drq(wdc) != 0) {
1126 /*
1127 * We `know' there's a drive here; just assume it's old.
1128 */
1129 strncpy(wd->sc_dk.dk_label.d_typename, "ST506",
1130 sizeof wd->sc_dk.dk_label.d_typename);
1131 wd->sc_dk.dk_label.d_type = DTYPE_ST506;
1132
1133 strncpy(wd->sc_params.wdp_model, "unknown",
1134 sizeof wd->sc_params.wdp_model);
1135 wd->sc_params.wdp_config = WD_CFG_FIXED;
1136 wd->sc_params.wdp_cylinders = 1024;
1137 wd->sc_params.wdp_heads = 8;
1138 wd->sc_params.wdp_sectors = 17;
1139 wd->sc_params.wdp_buftype = WD_BUF_SINGLEPORTSECTOR;
1140 wd->sc_params.wdp_maxmulti = 0;
1141 wd->sc_params.wdp_usedmovsd = 0;
1142 wd->sc_params.wdp_capabilities = 0;
1143 } else {
1144 /* Obtain parameters. */
1145 insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
1146 bcopy(tb, &wd->sc_params, sizeof(struct wdparams));
1147
1148 /* Shuffle string byte order. */
1149 for (i = 0; i < sizeof(wd->sc_params.wdp_model); i += 2) {
1150 u_short *p;
1151 p = (u_short *)(wd->sc_params.wdp_model + i);
1152 *p = ntohs(*p);
1153 }
1154
1155 strncpy(wd->sc_dk.dk_label.d_typename, "ESDI/IDE",
1156 sizeof wd->sc_dk.dk_label.d_typename);
1157 wd->sc_dk.dk_label.d_type = DTYPE_ESDI;
1158 }
1159
1160 #if 0
1161 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1162 wp->wdp_config, wp->wdp_cylinders, wp->wdp_heads, wp->wdp_sectors,
1163 wp->wdp_buftype, wp->wdp_bufsize, wp->wdp_model);
1164 #endif
1165
1166 /* XXX sometimes possibly needed */
1167 (void) inb(wdc->sc_iobase+wd_status);
1168
1169 return 0;
1170 }
1171
1172 int
1173 wdclose(dev, flag, fmt)
1174 dev_t dev;
1175 int flag, fmt;
1176 {
1177 struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1178 int part = WDPART(dev);
1179 int s;
1180
1181 switch (fmt) {
1182 case S_IFCHR:
1183 wd->sc_dk.dk_copenmask &= ~(1 << part);
1184 break;
1185 case S_IFBLK:
1186 wd->sc_dk.dk_bopenmask &= ~(1 << part);
1187 break;
1188 }
1189 wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1190
1191 #if 0
1192 if (wd->sc_dk.dk_openmask == 0) {
1193 wd->sc_flags |= WDF_LOCKED;
1194
1195 s = splbio();
1196 while (...) {
1197 wd->sc_flags |= WDF_WAITING;
1198 if ((error = tsleep(wd, PRIBIO | PCATCH, "wdcls", 0)) != 0)
1199 return error;
1200 }
1201 splx(s);
1202
1203 wd->sc_flags &= ~WDF_LOCKED;
1204 if ((wd->sc_flags & WDF_WANTED) != 0) {
1205 wd->sc_flags &= WDF_WANTED;
1206 wakeup(wd);
1207 }
1208 }
1209 #endif
1210
1211 return 0;
1212 }
1213
1214 int
1215 wdioctl(dev, command, addr, flag, p)
1216 dev_t dev;
1217 u_long command;
1218 caddr_t addr;
1219 int flag;
1220 struct proc *p;
1221 {
1222 struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1223 int error;
1224
1225 if ((wd->sc_flags & WDF_LOADED) == 0)
1226 return EIO;
1227
1228 switch (command) {
1229 case DIOCSBAD:
1230 if ((flag & FWRITE) == 0)
1231 return EBADF;
1232 wd->sc_dk.dk_cpulabel.bad = *(struct dkbad *)addr;
1233 wd->sc_dk.dk_label.d_flags |= D_BADSECT;
1234 bad144intern(wd);
1235 return 0;
1236
1237 case DIOCGDINFO:
1238 *(struct disklabel *)addr = wd->sc_dk.dk_label;
1239 return 0;
1240
1241 case DIOCGPART:
1242 ((struct partinfo *)addr)->disklab = &wd->sc_dk.dk_label;
1243 ((struct partinfo *)addr)->part =
1244 &wd->sc_dk.dk_label.d_partitions[WDPART(dev)];
1245 return 0;
1246
1247 case DIOCSDINFO:
1248 if ((flag & FWRITE) == 0)
1249 return EBADF;
1250 error = setdisklabel(&wd->sc_dk.dk_label,
1251 (struct disklabel *)addr,
1252 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_dk.dk_openmask : */0,
1253 &wd->sc_dk.dk_cpulabel);
1254 if (error == 0) {
1255 wd->sc_flags |= WDF_BSDLABEL;
1256 if (wd->sc_state > GEOMETRY)
1257 wd->sc_state = GEOMETRY;
1258 }
1259 return error;
1260
1261 case DIOCWLABEL:
1262 if ((flag & FWRITE) == 0)
1263 return EBADF;
1264 if (*(int *)addr)
1265 wd->sc_flags |= WDF_WLABEL;
1266 else
1267 wd->sc_flags &= ~WDF_WLABEL;
1268 return 0;
1269
1270 case DIOCWDINFO:
1271 if ((flag & FWRITE) == 0)
1272 return EBADF;
1273 error = setdisklabel(&wd->sc_dk.dk_label,
1274 (struct disklabel *)addr,
1275 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_dk.dk_openmask : */0,
1276 &wd->sc_dk.dk_cpulabel);
1277 if (error == 0) {
1278 wd->sc_flags |= WDF_BSDLABEL;
1279 if (wd->sc_state > GEOMETRY)
1280 wd->sc_state = GEOMETRY;
1281
1282 /* Simulate opening partition 0 so write succeeds. */
1283 wd->sc_dk.dk_openmask |= (1 << 0); /* XXX */
1284 error = writedisklabel(WDLABELDEV(dev), wdstrategy,
1285 &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
1286 wd->sc_dk.dk_openmask =
1287 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1288 }
1289 return error;
1290
1291 #ifdef notyet
1292 case DIOCGDINFOP:
1293 *(struct disklabel **)addr = &wd->sc_dk.dk_label;
1294 return 0;
1295
1296 case DIOCWFORMAT:
1297 if ((flag & FWRITE) == 0)
1298 return EBADF;
1299 {
1300 register struct format_op *fop;
1301 struct iovec aiov;
1302 struct uio auio;
1303
1304 fop = (struct format_op *)addr;
1305 aiov.iov_base = fop->df_buf;
1306 aiov.iov_len = fop->df_count;
1307 auio.uio_iov = &aiov;
1308 auio.uio_iovcnt = 1;
1309 auio.uio_resid = fop->df_count;
1310 auio.uio_segflg = 0;
1311 auio.uio_offset =
1312 fop->df_startblk * wd->sc_dk.dk_label.d_secsize;
1313 auio.uio_procp = p;
1314 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1315 &auio);
1316 fop->df_count -= auio.uio_resid;
1317 fop->df_reg[0] = wdc->sc_status;
1318 fop->df_reg[1] = wdc->sc_error;
1319 return error;
1320 }
1321 #endif
1322
1323 default:
1324 return ENOTTY;
1325 }
1326
1327 #ifdef DIAGNOSTIC
1328 panic("wdioctl: impossible");
1329 #endif
1330 }
1331
1332 #ifdef B_FORMAT
1333 int
1334 wdformat(struct buf *bp)
1335 {
1336
1337 bp->b_flags |= B_FORMAT;
1338 return wdstrategy(bp);
1339 }
1340 #endif
1341
1342 int
1343 wdsize(dev)
1344 dev_t dev;
1345 {
1346 struct wd_softc *wd;
1347 int part;
1348 int size;
1349
1350 if (wdopen(dev, 0, S_IFBLK) != 0)
1351 return -1;
1352 wd = wdcd.cd_devs[WDUNIT(dev)];
1353 part = WDPART(dev);
1354 if ((wd->sc_flags & WDF_BSDLABEL) == 0 ||
1355 wd->sc_dk.dk_label.d_partitions[part].p_fstype != FS_SWAP)
1356 size = -1;
1357 else
1358 size = wd->sc_dk.dk_label.d_partitions[part].p_size;
1359 if (wdclose(dev, 0, S_IFBLK) != 0)
1360 return -1;
1361 return size;
1362 }
1363
1364 /*
1365 * Dump core after a system crash.
1366 */
1367 int
1368 wddump(dev)
1369 dev_t dev;
1370 {
1371 struct wd_softc *wd; /* disk unit to do the IO */
1372 struct wdc_softc *wdc;
1373 long num; /* number of sectors to write */
1374 int unit, part;
1375 long blkoff, blkno;
1376 long cylin, head, sector;
1377 long secpertrk, secpercyl, nblocks;
1378 char *addr;
1379 static wddoingadump = 0;
1380 extern caddr_t CADDR1;
1381 extern pt_entry_t *CMAP1;
1382
1383 addr = (char *)0; /* starting address */
1384
1385 #if DO_NOT_KNOW_HOW
1386 /* Toss any characters present prior to dump, ie. non-blocking getc. */
1387 while (cngetc())
1388 ;
1389 #endif
1390
1391 unit = WDUNIT(dev);
1392 /* Check for acceptable drive number. */
1393 if (unit >= wdcd.cd_ndevs)
1394 return ENXIO;
1395 wd = wdcd.cd_devs[unit];
1396 /* Was it ever initialized? */
1397 if (wd == 0 || wd->sc_state < OPEN)
1398 return ENXIO;
1399
1400 wdc = (void *)wd->sc_dev.dv_parent;
1401
1402 /* Convert to disk sectors. */
1403 num = ctob(physmem) / wd->sc_dk.dk_label.d_secsize;
1404
1405 secpertrk = wd->sc_dk.dk_label.d_nsectors;
1406 secpercyl = wd->sc_dk.dk_label.d_secpercyl;
1407 part = WDPART(dev);
1408 nblocks = wd->sc_dk.dk_label.d_partitions[part].p_size;
1409 blkoff = wd->sc_dk.dk_label.d_partitions[part].p_offset;
1410
1411 /*printf("part %x, nblocks %d, dumplo %d, num %d\n", part, nblocks,
1412 dumplo, num);*/
1413
1414 /* Check transfer bounds against partition size. */
1415 if (dumplo < 0 || dumplo + num > nblocks)
1416 return EINVAL;
1417
1418 if (wddoingadump)
1419 return EFAULT;
1420 wddoingadump = 1;
1421
1422 /* Recalibrate. */
1423 if (wdcommandshort(wdc, wd->sc_drive, WDCC_RECAL) != 0 ||
1424 wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0 ||
1425 wait_for_ready(wdc) != 0) {
1426 wderror(wd, NULL, "wddump: recal failed");
1427 return EIO;
1428 }
1429
1430 blkno = dumplo + blkoff;
1431 while (num > 0) {
1432 /* Compute disk address. */
1433 cylin = blkno / secpercyl;
1434 head = (blkno % secpercyl) / secpertrk;
1435 sector = blkno % secpertrk;
1436
1437 if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0) {
1438 long newblk;
1439 int i;
1440
1441 for (i = 0; wd->sc_badsect[i] != -1; i++) {
1442 if (blkno < wd->sc_badsect[i]) {
1443 /* Sorted list, passed our block by. */
1444 break;
1445 } else if (blkno == wd->sc_badsect[i]) {
1446 newblk =
1447 wd->sc_dk.dk_label.d_secperunit -
1448 wd->sc_dk.dk_label.d_nsectors -
1449 i - 1;
1450 cylin = newblk / secpercyl;
1451 head = (newblk % secpercyl) / secpertrk;
1452 sector = newblk % secpertrk;
1453 /* Found and replaced; done. */
1454 break;
1455 }
1456 }
1457 }
1458 sector++; /* origin 1 */
1459
1460 #ifdef notdef
1461 /* Let's just talk about this first. */
1462 printf("cylin %d, head %d, sector %d, addr 0x%x", cylin, head,
1463 sector, addr);
1464 #endif
1465 if (wdcommand(wd, WDCC_WRITE, cylin, head, sector, 1) != 0 ||
1466 wait_for_drq(wdc) != 0) {
1467 wderror(wd, NULL, "wddump: write failed");
1468 return EIO;
1469 }
1470
1471 #ifdef notdef /* Cannot use this since this address was mapped differently. */
1472 pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1473 #else
1474 *CMAP1 = PG_V | PG_KW | ctob((long)addr);
1475 tlbflush();
1476 #endif
1477
1478 outsw(wdc->sc_iobase+wd_data, CADDR1 + ((int)addr & PGOFSET),
1479 DEV_BSIZE / sizeof(short));
1480
1481 /* Check data request (should be done). */
1482 if (wait_for_ready(wdc) != 0) {
1483 wderror(wd, NULL, "wddump: timeout waiting for ready");
1484 return EIO;
1485 }
1486 if (wdc->sc_status & WDCS_DRQ) {
1487 wderror(wd, NULL, "wddump: extra drq");
1488 return EIO;
1489 }
1490
1491 if ((unsigned)addr % 1048576 == 0)
1492 printf("%d ", num / (1048576 / DEV_BSIZE));
1493
1494 /* Update block count. */
1495 num--;
1496 blkno++;
1497 (int)addr += DEV_BSIZE;
1498
1499 #if DO_NOT_KNOW_HOW
1500 /* Operator aborting dump? non-blocking getc() */
1501 if (cngetc())
1502 return EINTR;
1503 #endif
1504 }
1505 return 0;
1506 }
1507
1508 /*
1509 * Internalize the bad sector table.
1510 */
1511 void
1512 bad144intern(wd)
1513 struct wd_softc *wd;
1514 {
1515 struct dkbad *bt = &wd->sc_dk.dk_cpulabel.bad;
1516 struct disklabel *lp = &wd->sc_dk.dk_label;
1517 int i = 0;
1518
1519 for (; i < 126; i++) {
1520 if (bt->bt_bad[i].bt_cyl == 0xffff)
1521 break;
1522 wd->sc_badsect[i] =
1523 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1524 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1525 (bt->bt_bad[i].bt_trksec & 0xff);
1526 }
1527 for (; i < 127; i++)
1528 wd->sc_badsect[i] = -1;
1529 }
1530
1531 static int
1532 wdcreset(wdc)
1533 struct wdc_softc *wdc;
1534 {
1535 int iobase = wdc->sc_iobase;
1536
1537 /* Reset the device. */
1538 outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1539 delay(1000);
1540 outb(iobase+wd_ctlr, WDCTL_IDS);
1541 delay(1000);
1542 (void) inb(iobase+wd_error);
1543 outb(iobase+wd_ctlr, WDCTL_4BIT);
1544
1545 if (wait_for_unbusy(wdc) < 0) {
1546 printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1547 return 1;
1548 }
1549
1550 return 0;
1551 }
1552
1553 static void
1554 wdcrestart(arg)
1555 void *arg;
1556 {
1557 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1558 int s;
1559
1560 s = splbio();
1561 wdcstart(wdc);
1562 splx(s);
1563 }
1564
1565 /*
1566 * Unwedge the controller after an unexpected error. We do this by resetting
1567 * it, marking all drives for recalibration, and stalling the queue for a short
1568 * period to give the reset time to finish.
1569 * NOTE: We use a timeout here, so this routine must not be called during
1570 * autoconfig or dump.
1571 */
1572 static void
1573 wdcunwedge(wdc)
1574 struct wdc_softc *wdc;
1575 {
1576 int unit;
1577
1578 untimeout(wdctimeout, wdc);
1579 (void) wdcreset(wdc);
1580
1581 /* Schedule recalibrate for all drives on this controller. */
1582 for (unit = 0; unit < wdcd.cd_ndevs; unit++) {
1583 struct wd_softc *wd = wdcd.cd_devs[unit];
1584 if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
1585 continue;
1586 if (wd->sc_state > RECAL)
1587 wd->sc_state = RECAL;
1588 }
1589
1590 wdc->sc_flags |= WDCF_ERROR;
1591 ++wdc->sc_errors;
1592
1593 /* Wake up in a little bit and restart the operation. */
1594 timeout(wdcrestart, wdc, RECOVERYTIME);
1595 }
1596
1597 int
1598 wdcwait(wdc, mask)
1599 struct wdc_softc *wdc;
1600 int mask;
1601 {
1602 int iobase = wdc->sc_iobase;
1603 int timeout = 0;
1604 u_char status;
1605 extern int cold;
1606
1607 for (;;) {
1608 wdc->sc_status = status = inb(iobase+wd_status);
1609 if ((status & WDCS_BSY) == 0 && (status & mask) == mask)
1610 break;
1611 if (++timeout > WDCNDELAY)
1612 return -1;
1613 delay(WDCDELAY);
1614 }
1615 if (status & WDCS_ERR) {
1616 wdc->sc_error = inb(iobase+wd_error);
1617 return WDCS_ERR;
1618 }
1619 #ifdef WDCNDELAY_DEBUG
1620 /* After autoconfig, there should be no long delays. */
1621 if (!cold && timeout > WDCNDELAY_DEBUG)
1622 printf("%s: warning: busy-wait took %dus\n",
1623 wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1624 #endif
1625 return 0;
1626 }
1627
1628 static void
1629 wdctimeout(arg)
1630 void *arg;
1631 {
1632 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1633 int s;
1634
1635 s = splbio();
1636 wderror(wdc, NULL, "lost interrupt");
1637 wdcunwedge(wdc);
1638 splx(s);
1639 }
1640
1641 static void
1642 wderror(dev, bp, msg)
1643 void *dev;
1644 struct buf *bp;
1645 char *msg;
1646 {
1647 struct wd_softc *wd = dev;
1648 struct wdc_softc *wdc = dev;
1649
1650 if (bp)
1651 diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip,
1652 &wd->sc_dk.dk_label);
1653 else
1654 printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
1655 msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
1656 }
1657