wd.c revision 1.109 1 /* $NetBSD: wd.c,v 1.109 1994/11/22 03:24:53 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_READY | WDCS_SEEKCMPLT | WDCS_DRQ)
183 #define wait_for_ready(d) wdcwait(d, WDCS_READY | WDCS_SEEKCMPLT)
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_RESTORE) != 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_fixedcyl + wd->sc_params.wdp_removcyl) *
302 (wd->sc_params.wdp_heads * wd->sc_params.wdp_sectors) /
303 (1048576 / DEV_BSIZE),
304 (wd->sc_params.wdp_fixedcyl + wd->sc_params.wdp_removcyl),
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_ECCCOR)
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_READY | WDCS_SEEKCMPLT | WDCS_DRQ))
767 != (WDCS_READY | WDCS_SEEKCMPLT | 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 =
914 wd->sc_params.wdp_fixedcyl + wd->sc_params.wdp_removcyl /*+- 1*/;
915 wd->sc_dk.dk_label.d_secpercyl =
916 wd->sc_dk.dk_label.d_ntracks * wd->sc_dk.dk_label.d_nsectors;
917
918 #if 0
919 strncpy(wd->sc_dk.dk_label.d_typename, "ST506 disk", 16);
920 wd->sc_dk.dk_label.d_type = DTYPE_ST506;
921 #endif
922 strncpy(wd->sc_dk.dk_label.d_packname, wd->sc_params.wdp_model, 16);
923 wd->sc_dk.dk_label.d_secperunit =
924 wd->sc_dk.dk_label.d_secpercyl * wd->sc_dk.dk_label.d_ncylinders;
925 wd->sc_dk.dk_label.d_rpm = 3600;
926 wd->sc_dk.dk_label.d_interleave = 1;
927 wd->sc_dk.dk_label.d_flags = 0;
928
929 wd->sc_dk.dk_label.d_partitions[RAW_PART].p_offset = 0;
930 wd->sc_dk.dk_label.d_partitions[RAW_PART].p_size =
931 wd->sc_dk.dk_label.d_secperunit *
932 (wd->sc_dk.dk_label.d_secsize / DEV_BSIZE);
933 wd->sc_dk.dk_label.d_partitions[RAW_PART].p_fstype = FS_UNUSED;
934 wd->sc_dk.dk_label.d_npartitions = RAW_PART + 1;
935
936 wd->sc_dk.dk_label.d_magic = DISKMAGIC;
937 wd->sc_dk.dk_label.d_magic2 = DISKMAGIC;
938 wd->sc_dk.dk_label.d_checksum = dkcksum(&wd->sc_dk.dk_label);
939
940 if (wd->sc_state > RECAL)
941 wd->sc_state = RECAL;
942 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
943 wdstrategy, &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
944 if (errstring) {
945 /*
946 * This probably happened because the drive's default
947 * geometry doesn't match the DOS geometry. We
948 * assume the DOS geometry is now in the label and try
949 * again. XXX This is a kluge.
950 */
951 if (wd->sc_state > GEOMETRY)
952 wd->sc_state = GEOMETRY;
953 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
954 wdstrategy, &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
955 }
956 if (errstring) {
957 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
958 return;
959 }
960
961 if (wd->sc_state > GEOMETRY)
962 wd->sc_state = GEOMETRY;
963 if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0)
964 bad144intern(wd);
965
966 wd->sc_flags |= WDF_BSDLABEL;
967 }
968
969 /*
970 * Implement operations other than read/write.
971 * Called from wdcstart or wdcintr during opens and formats.
972 * Uses finite-state-machine to track progress of operation in progress.
973 * Returns 0 if operation still in progress, 1 if completed.
974 */
975 static int
976 wdcontrol(wd)
977 struct wd_softc *wd;
978 {
979 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
980
981 switch (wd->sc_state) {
982 case RECAL: /* Set SDH, step rate, do restore. */
983 if (wdcommandshort(wdc, wd->sc_drive, WDCC_RESTORE) != 0) {
984 wderror(wd, NULL, "wdcontrol: wdcommandshort failed");
985 wdcunwedge(wdc);
986 return 0;
987 }
988 wd->sc_state = RECAL_WAIT;
989 return 0;
990
991 case RECAL_WAIT:
992 if (wdc->sc_status & WDCS_ERR) {
993 wderror(wd, NULL, "wdcontrol: recal failed");
994 wdcunwedge(wdc);
995 return 0;
996 }
997 /* fall through */
998 case GEOMETRY:
999 if (wdsetctlr(wd) != 0) {
1000 /* Already printed a message. */
1001 wdcunwedge(wdc);
1002 return 0;
1003 }
1004 wd->sc_state = GEOMETRY_WAIT;
1005 return 0;
1006
1007 case GEOMETRY_WAIT:
1008 if (wdc->sc_status & WDCS_ERR) {
1009 wderror(wd, NULL, "wdcontrol: geometry failed");
1010 wdcunwedge(wdc);
1011 return 0;
1012 }
1013
1014 wdc->sc_errors = 0;
1015 wd->sc_state = OPEN;
1016 /*
1017 * The rest of the initialization can be done by normal means.
1018 */
1019 return 1;
1020 }
1021
1022 #ifdef DIAGNOSTIC
1023 panic("wdcontrol: impossible");
1024 #endif
1025 }
1026
1027 /*
1028 * Send a command and wait uninterruptibly until controller is finished.
1029 * Return -1 if controller busy for too long, otherwise return non-zero if
1030 * error. Intended for brief controller commands at critical points.
1031 * Assumes interrupts are blocked.
1032 */
1033 static int
1034 wdcommand(wd, command, cylin, head, sector, count)
1035 struct wd_softc *wd;
1036 int command;
1037 int cylin, head, sector, count;
1038 {
1039 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1040 int iobase = wdc->sc_iobase;
1041 int stat;
1042
1043 /* Select drive. */
1044 outb(iobase+wd_sdh, WDSD_IBM | (wd->sc_drive << 4) | head);
1045
1046 /* Wait for it to become ready to accept a command. */
1047 if (command == WDCC_IDC)
1048 stat = wait_for_unbusy(wdc);
1049 else
1050 stat = wdcwait(wdc, WDCS_READY);
1051 if (stat < 0)
1052 return -1;
1053
1054 /* Load parameters. */
1055 outb(iobase+wd_precomp, wd->sc_dk.dk_label.d_precompcyl / 4);
1056 outb(iobase+wd_cyl_lo, cylin);
1057 outb(iobase+wd_cyl_hi, cylin >> 8);
1058 outb(iobase+wd_sector, sector);
1059 outb(iobase+wd_seccnt, count);
1060
1061 /* Send command. */
1062 outb(iobase+wd_command, command);
1063
1064 return 0;
1065 }
1066
1067 int
1068 wdcommandshort(wdc, drive, command)
1069 struct wdc_softc *wdc;
1070 int drive;
1071 int command;
1072 {
1073 int iobase = wdc->sc_iobase;
1074
1075 /* Select drive. */
1076 outb(iobase+wd_sdh, WDSD_IBM | (drive << 4));
1077
1078 if (wdcwait(wdc, WDCS_READY) < 0)
1079 return -1;
1080
1081 outb(iobase+wd_command, command);
1082
1083 return 0;
1084 }
1085
1086 /*
1087 * Issue IDC to drive to tell it just what geometry it is to be.
1088 */
1089 static int
1090 wdsetctlr(wd)
1091 struct wd_softc *wd;
1092 {
1093 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1094
1095 #ifdef WDDEBUG
1096 printf("wd(%d,%d) C%dH%dS%d\n", wd->sc_ctrlr, wd->sc_drive,
1097 wd->sc_dk.dk_label.d_ncylinders, wd->sc_dk.dk_label.d_ntracks,
1098 wd->sc_dk.dk_label.d_nsectors);
1099 #endif
1100
1101 if (wdcommand(wd, WDCC_IDC, wd->sc_dk.dk_label.d_ncylinders,
1102 wd->sc_dk.dk_label.d_ntracks - 1, 0, wd->sc_dk.dk_label.d_nsectors)
1103 != 0) {
1104 wderror(wd, NULL, "wdsetctlr: geometry upload failed");
1105 return -1;
1106 }
1107
1108 return 0;
1109 }
1110
1111 /*
1112 * Issue READP to drive to ask it what it is.
1113 */
1114 int
1115 wd_get_parms(wd)
1116 struct wd_softc *wd;
1117 {
1118 struct wdc_softc *wdc = (void *)wd->sc_dev.dv_parent;
1119 int i;
1120 char tb[DEV_BSIZE];
1121
1122 if (wdcommandshort(wdc, wd->sc_drive, WDCC_READP) != 0 ||
1123 wait_for_drq(wdc) != 0) {
1124 /*
1125 * We `know' there's a drive here; just assume it's old.
1126 */
1127 strncpy(wd->sc_dk.dk_label.d_typename, "ST506",
1128 sizeof wd->sc_dk.dk_label.d_typename);
1129 wd->sc_dk.dk_label.d_type = DTYPE_ST506;
1130
1131 strncpy(wd->sc_params.wdp_model, "unknown",
1132 sizeof wd->sc_params.wdp_model);
1133 wd->sc_params.wdp_fixedcyl = 1024;
1134 wd->sc_params.wdp_removcyl = 0;
1135 wd->sc_params.wdp_heads = 8;
1136 wd->sc_params.wdp_sectors = 17;
1137 } else {
1138 /* Obtain parameters. */
1139 insw(wdc->sc_iobase+wd_data, tb, sizeof(tb) / sizeof(short));
1140 bcopy(tb, &wd->sc_params, sizeof(struct wdparams));
1141
1142 /* Shuffle string byte order. */
1143 for (i = 0; i < sizeof(wd->sc_params.wdp_model); i += 2) {
1144 u_short *p;
1145 p = (u_short *)(wd->sc_params.wdp_model + i);
1146 *p = ntohs(*p);
1147 }
1148
1149 strncpy(wd->sc_dk.dk_label.d_typename, "ESDI/IDE",
1150 sizeof wd->sc_dk.dk_label.d_typename);
1151 wd->sc_dk.dk_label.d_type = DTYPE_ESDI;
1152 }
1153
1154 #if 0
1155 printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1156 wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
1157 wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
1158 #endif
1159
1160 /* XXX sometimes possibly needed */
1161 (void) inb(wdc->sc_iobase+wd_status);
1162
1163 return 0;
1164 }
1165
1166 int
1167 wdclose(dev, flag, fmt)
1168 dev_t dev;
1169 int flag, fmt;
1170 {
1171 struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1172 int part = WDPART(dev);
1173 int s;
1174
1175 switch (fmt) {
1176 case S_IFCHR:
1177 wd->sc_dk.dk_copenmask &= ~(1 << part);
1178 break;
1179 case S_IFBLK:
1180 wd->sc_dk.dk_bopenmask &= ~(1 << part);
1181 break;
1182 }
1183 wd->sc_dk.dk_openmask = wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1184
1185 #if 0
1186 if (wd->sc_dk.dk_openmask == 0) {
1187 wd->sc_flags |= WDF_LOCKED;
1188
1189 s = splbio();
1190 while (...) {
1191 wd->sc_flags |= WDF_WAITING;
1192 if ((error = tsleep(wd, PRIBIO | PCATCH, "wdcls", 0)) != 0)
1193 return error;
1194 }
1195 splx(s);
1196
1197 wd->sc_flags &= ~WDF_LOCKED;
1198 if ((wd->sc_flags & WDF_WANTED) != 0) {
1199 wd->sc_flags &= WDF_WANTED;
1200 wakeup(wd);
1201 }
1202 }
1203 #endif
1204
1205 return 0;
1206 }
1207
1208 int
1209 wdioctl(dev, command, addr, flag, p)
1210 dev_t dev;
1211 u_long command;
1212 caddr_t addr;
1213 int flag;
1214 struct proc *p;
1215 {
1216 struct wd_softc *wd = wdcd.cd_devs[WDUNIT(dev)];
1217 int error;
1218
1219 if ((wd->sc_flags & WDF_LOADED) == 0)
1220 return EIO;
1221
1222 switch (command) {
1223 case DIOCSBAD:
1224 if ((flag & FWRITE) == 0)
1225 return EBADF;
1226 wd->sc_dk.dk_cpulabel.bad = *(struct dkbad *)addr;
1227 wd->sc_dk.dk_label.d_flags |= D_BADSECT;
1228 bad144intern(wd);
1229 return 0;
1230
1231 case DIOCGDINFO:
1232 *(struct disklabel *)addr = wd->sc_dk.dk_label;
1233 return 0;
1234
1235 case DIOCGPART:
1236 ((struct partinfo *)addr)->disklab = &wd->sc_dk.dk_label;
1237 ((struct partinfo *)addr)->part =
1238 &wd->sc_dk.dk_label.d_partitions[WDPART(dev)];
1239 return 0;
1240
1241 case DIOCSDINFO:
1242 if ((flag & FWRITE) == 0)
1243 return EBADF;
1244 error = setdisklabel(&wd->sc_dk.dk_label,
1245 (struct disklabel *)addr,
1246 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_dk.dk_openmask : */0,
1247 &wd->sc_dk.dk_cpulabel);
1248 if (error == 0) {
1249 wd->sc_flags |= WDF_BSDLABEL;
1250 if (wd->sc_state > GEOMETRY)
1251 wd->sc_state = GEOMETRY;
1252 }
1253 return error;
1254
1255 case DIOCWLABEL:
1256 if ((flag & FWRITE) == 0)
1257 return EBADF;
1258 if (*(int *)addr)
1259 wd->sc_flags |= WDF_WLABEL;
1260 else
1261 wd->sc_flags &= ~WDF_WLABEL;
1262 return 0;
1263
1264 case DIOCWDINFO:
1265 if ((flag & FWRITE) == 0)
1266 return EBADF;
1267 error = setdisklabel(&wd->sc_dk.dk_label,
1268 (struct disklabel *)addr,
1269 /*(wd->sc_flags & WDF_BSDLABEL) ? wd->sc_dk.dk_openmask : */0,
1270 &wd->sc_dk.dk_cpulabel);
1271 if (error == 0) {
1272 wd->sc_flags |= WDF_BSDLABEL;
1273 if (wd->sc_state > GEOMETRY)
1274 wd->sc_state = GEOMETRY;
1275
1276 /* Simulate opening partition 0 so write succeeds. */
1277 wd->sc_dk.dk_openmask |= (1 << 0); /* XXX */
1278 error = writedisklabel(WDLABELDEV(dev), wdstrategy,
1279 &wd->sc_dk.dk_label, &wd->sc_dk.dk_cpulabel);
1280 wd->sc_dk.dk_openmask =
1281 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1282 }
1283 return error;
1284
1285 #ifdef notyet
1286 case DIOCGDINFOP:
1287 *(struct disklabel **)addr = &wd->sc_dk.dk_label;
1288 return 0;
1289
1290 case DIOCWFORMAT:
1291 if ((flag & FWRITE) == 0)
1292 return EBADF;
1293 {
1294 register struct format_op *fop;
1295 struct iovec aiov;
1296 struct uio auio;
1297
1298 fop = (struct format_op *)addr;
1299 aiov.iov_base = fop->df_buf;
1300 aiov.iov_len = fop->df_count;
1301 auio.uio_iov = &aiov;
1302 auio.uio_iovcnt = 1;
1303 auio.uio_resid = fop->df_count;
1304 auio.uio_segflg = 0;
1305 auio.uio_offset =
1306 fop->df_startblk * wd->sc_dk.dk_label.d_secsize;
1307 auio.uio_procp = p;
1308 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1309 &auio);
1310 fop->df_count -= auio.uio_resid;
1311 fop->df_reg[0] = wdc->sc_status;
1312 fop->df_reg[1] = wdc->sc_error;
1313 return error;
1314 }
1315 #endif
1316
1317 default:
1318 return ENOTTY;
1319 }
1320
1321 #ifdef DIAGNOSTIC
1322 panic("wdioctl: impossible");
1323 #endif
1324 }
1325
1326 #ifdef B_FORMAT
1327 int
1328 wdformat(struct buf *bp)
1329 {
1330
1331 bp->b_flags |= B_FORMAT;
1332 return wdstrategy(bp);
1333 }
1334 #endif
1335
1336 int
1337 wdsize(dev)
1338 dev_t dev;
1339 {
1340 struct wd_softc *wd;
1341 int part;
1342 int size;
1343
1344 if (wdopen(dev, 0, S_IFBLK) != 0)
1345 return -1;
1346 wd = wdcd.cd_devs[WDUNIT(dev)];
1347 part = WDPART(dev);
1348 if ((wd->sc_flags & WDF_BSDLABEL) == 0 ||
1349 wd->sc_dk.dk_label.d_partitions[part].p_fstype != FS_SWAP)
1350 size = -1;
1351 else
1352 size = wd->sc_dk.dk_label.d_partitions[part].p_size;
1353 if (wdclose(dev, 0, S_IFBLK) != 0)
1354 return -1;
1355 return size;
1356 }
1357
1358 /*
1359 * Dump core after a system crash.
1360 */
1361 int
1362 wddump(dev)
1363 dev_t dev;
1364 {
1365 struct wd_softc *wd; /* disk unit to do the IO */
1366 struct wdc_softc *wdc;
1367 long num; /* number of sectors to write */
1368 int unit, part;
1369 long blkoff, blkno;
1370 long cylin, head, sector;
1371 long secpertrk, secpercyl, nblocks;
1372 char *addr;
1373 static wddoingadump = 0;
1374 extern caddr_t CADDR1;
1375 extern pt_entry_t *CMAP1;
1376
1377 addr = (char *)0; /* starting address */
1378
1379 #if DO_NOT_KNOW_HOW
1380 /* Toss any characters present prior to dump, ie. non-blocking getc. */
1381 while (cngetc())
1382 ;
1383 #endif
1384
1385 unit = WDUNIT(dev);
1386 /* Check for acceptable drive number. */
1387 if (unit >= wdcd.cd_ndevs)
1388 return ENXIO;
1389 wd = wdcd.cd_devs[unit];
1390 /* Was it ever initialized? */
1391 if (wd == 0 || wd->sc_state < OPEN)
1392 return ENXIO;
1393
1394 wdc = (void *)wd->sc_dev.dv_parent;
1395
1396 /* Convert to disk sectors. */
1397 num = ctob(physmem) / wd->sc_dk.dk_label.d_secsize;
1398
1399 secpertrk = wd->sc_dk.dk_label.d_nsectors;
1400 secpercyl = wd->sc_dk.dk_label.d_secpercyl;
1401 part = WDPART(dev);
1402 nblocks = wd->sc_dk.dk_label.d_partitions[part].p_size;
1403 blkoff = wd->sc_dk.dk_label.d_partitions[part].p_offset;
1404
1405 /*printf("part %x, nblocks %d, dumplo %d, num %d\n", part, nblocks,
1406 dumplo, num);*/
1407
1408 /* Check transfer bounds against partition size. */
1409 if (dumplo < 0 || dumplo + num > nblocks)
1410 return EINVAL;
1411
1412 if (wddoingadump)
1413 return EFAULT;
1414 wddoingadump = 1;
1415
1416 /* Recalibrate. */
1417 if (wdcommandshort(wdc, wd->sc_drive, WDCC_RESTORE) != 0 ||
1418 wait_for_ready(wdc) != 0 || wdsetctlr(wd) != 0 ||
1419 wait_for_ready(wdc) != 0) {
1420 wderror(wd, NULL, "wddump: recal failed");
1421 return EIO;
1422 }
1423
1424 blkno = dumplo + blkoff;
1425 while (num > 0) {
1426 /* Compute disk address. */
1427 cylin = blkno / secpercyl;
1428 head = (blkno % secpercyl) / secpertrk;
1429 sector = blkno % secpertrk;
1430
1431 if ((wd->sc_dk.dk_label.d_flags & D_BADSECT) != 0) {
1432 long newblk;
1433 int i;
1434
1435 for (i = 0; wd->sc_badsect[i] != -1; i++) {
1436 if (blkno < wd->sc_badsect[i]) {
1437 /* Sorted list, passed our block by. */
1438 break;
1439 } else if (blkno == wd->sc_badsect[i]) {
1440 newblk =
1441 wd->sc_dk.dk_label.d_secperunit -
1442 wd->sc_dk.dk_label.d_nsectors -
1443 i - 1;
1444 cylin = newblk / secpercyl;
1445 head = (newblk % secpercyl) / secpertrk;
1446 sector = newblk % secpertrk;
1447 /* Found and replaced; done. */
1448 break;
1449 }
1450 }
1451 }
1452 sector++; /* origin 1 */
1453
1454 #ifdef notdef
1455 /* Let's just talk about this first. */
1456 printf("cylin %d, head %d, sector %d, addr 0x%x", cylin, head,
1457 sector, addr);
1458 #endif
1459 if (wdcommand(wd, WDCC_WRITE, cylin, head, sector, 1) != 0 ||
1460 wait_for_drq(wdc) != 0) {
1461 wderror(wd, NULL, "wddump: write failed");
1462 return EIO;
1463 }
1464
1465 #ifdef notdef /* Cannot use this since this address was mapped differently. */
1466 pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
1467 #else
1468 *CMAP1 = PG_V | PG_KW | ctob((long)addr);
1469 tlbflush();
1470 #endif
1471
1472 outsw(wdc->sc_iobase+wd_data, CADDR1 + ((int)addr & PGOFSET),
1473 DEV_BSIZE / sizeof(short));
1474
1475 /* Check data request (should be done). */
1476 if (wait_for_ready(wdc) != 0) {
1477 wderror(wd, NULL, "wddump: timeout waiting for ready");
1478 return EIO;
1479 }
1480 if (wdc->sc_status & WDCS_DRQ) {
1481 wderror(wd, NULL, "wddump: extra drq");
1482 return EIO;
1483 }
1484
1485 if ((unsigned)addr % 1048576 == 0)
1486 printf("%d ", num / (1048576 / DEV_BSIZE));
1487
1488 /* Update block count. */
1489 num--;
1490 blkno++;
1491 (int)addr += DEV_BSIZE;
1492
1493 #if DO_NOT_KNOW_HOW
1494 /* Operator aborting dump? non-blocking getc() */
1495 if (cngetc())
1496 return EINTR;
1497 #endif
1498 }
1499 return 0;
1500 }
1501
1502 /*
1503 * Internalize the bad sector table.
1504 */
1505 void
1506 bad144intern(wd)
1507 struct wd_softc *wd;
1508 {
1509 struct dkbad *bt = &wd->sc_dk.dk_cpulabel.bad;
1510 struct disklabel *lp = &wd->sc_dk.dk_label;
1511 int i = 0;
1512
1513 for (; i < 126; i++) {
1514 if (bt->bt_bad[i].bt_cyl == 0xffff)
1515 break;
1516 wd->sc_badsect[i] =
1517 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1518 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1519 (bt->bt_bad[i].bt_trksec & 0xff);
1520 }
1521 for (; i < 127; i++)
1522 wd->sc_badsect[i] = -1;
1523 }
1524
1525 static int
1526 wdcreset(wdc)
1527 struct wdc_softc *wdc;
1528 {
1529 int iobase = wdc->sc_iobase;
1530
1531 /* Reset the device. */
1532 outb(iobase+wd_ctlr, WDCTL_RST | WDCTL_IDS);
1533 delay(1000);
1534 outb(iobase+wd_ctlr, WDCTL_IDS);
1535 delay(1000);
1536 (void) inb(iobase+wd_error);
1537 outb(iobase+wd_ctlr, WDCTL_4BIT);
1538
1539 if (wait_for_unbusy(wdc) < 0) {
1540 printf("%s: reset failed\n", wdc->sc_dev.dv_xname);
1541 return 1;
1542 }
1543
1544 return 0;
1545 }
1546
1547 static void
1548 wdcrestart(arg)
1549 void *arg;
1550 {
1551 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1552 int s;
1553
1554 s = splbio();
1555 wdcstart(wdc);
1556 splx(s);
1557 }
1558
1559 /*
1560 * Unwedge the controller after an unexpected error. We do this by resetting
1561 * it, marking all drives for recalibration, and stalling the queue for a short
1562 * period to give the reset time to finish.
1563 * NOTE: We use a timeout here, so this routine must not be called during
1564 * autoconfig or dump.
1565 */
1566 static void
1567 wdcunwedge(wdc)
1568 struct wdc_softc *wdc;
1569 {
1570 int unit;
1571
1572 untimeout(wdctimeout, wdc);
1573 (void) wdcreset(wdc);
1574
1575 /* Schedule recalibrate for all drives on this controller. */
1576 for (unit = 0; unit < wdcd.cd_ndevs; unit++) {
1577 struct wd_softc *wd = wdcd.cd_devs[unit];
1578 if (!wd || (void *)wd->sc_dev.dv_parent != wdc)
1579 continue;
1580 if (wd->sc_state > RECAL)
1581 wd->sc_state = RECAL;
1582 }
1583
1584 wdc->sc_flags |= WDCF_ERROR;
1585 ++wdc->sc_errors;
1586
1587 /* Wake up in a little bit and restart the operation. */
1588 timeout(wdcrestart, wdc, RECOVERYTIME);
1589 }
1590
1591 int
1592 wdcwait(wdc, mask)
1593 struct wdc_softc *wdc;
1594 int mask;
1595 {
1596 int iobase = wdc->sc_iobase;
1597 int timeout = 0;
1598 u_char status;
1599 extern int cold;
1600
1601 for (;;) {
1602 wdc->sc_status = status = inb(iobase+wd_status);
1603 if ((status & WDCS_BUSY) == 0 && (status & mask) == mask)
1604 break;
1605 if (++timeout > WDCNDELAY)
1606 return -1;
1607 delay(WDCDELAY);
1608 }
1609 if (status & WDCS_ERR) {
1610 wdc->sc_error = inb(iobase+wd_error);
1611 return WDCS_ERR;
1612 }
1613 #ifdef WDCNDELAY_DEBUG
1614 /* After autoconfig, there should be no long delays. */
1615 if (!cold && timeout > WDCNDELAY_DEBUG)
1616 printf("%s: warning: busy-wait took %dus\n",
1617 wdc->sc_dev.dv_xname, WDCDELAY * timeout);
1618 #endif
1619 return 0;
1620 }
1621
1622 static void
1623 wdctimeout(arg)
1624 void *arg;
1625 {
1626 struct wdc_softc *wdc = (struct wdc_softc *)arg;
1627 int s;
1628
1629 s = splbio();
1630 wderror(wdc, NULL, "lost interrupt");
1631 wdcunwedge(wdc);
1632 splx(s);
1633 }
1634
1635 static void
1636 wderror(dev, bp, msg)
1637 void *dev;
1638 struct buf *bp;
1639 char *msg;
1640 {
1641 struct wd_softc *wd = dev;
1642 struct wdc_softc *wdc = dev;
1643
1644 if (bp)
1645 diskerr(bp, "wd", msg, LOG_PRINTF, wd->sc_skip,
1646 &wd->sc_dk.dk_label);
1647 else
1648 printf("%s: %s: status %b error %b\n", wdc->sc_dev.dv_xname,
1649 msg, wdc->sc_status, WDCS_BITS, wdc->sc_error, WDERR_BITS);
1650 }
1651