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