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