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