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