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