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