FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/wd.c
1 /*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)wd.c 7.2 (Berkeley) 5/9/91
37 * $FreeBSD$
38 */
39
40 /* TODO:
41 * o Bump error count after timeout.
42 * o Satisfy ATA timing in all cases.
43 * o Finish merging berry/sos timeout code (bump error count...).
44 * o Don't use polling except for initialization. Need to
45 * reorganize the state machine. Then "extra" interrupts
46 * shouldn't happen (except maybe one for initialization).
47 * o Support extended DOS partitions.
48 * o Support swapping to DOS partitions.
49 * o Handle clustering, disklabelling, DOS
50 * partitions and swapping driver-independently.
51 * Swapping will need new
52 * driver entries for polled reinit and polled write).
53 */
54
55 #include "wd.h"
56 #ifdef NWDC
57 #undef NWDC
58 #endif
59
60 #include "wdc.h"
61
62 #if NWDC > 0
63
64 #include "opt_hw_wdog.h"
65 #include "opt_ide_delay.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/conf.h>
71 #include <sys/bus.h>
72 #include <sys/disklabel.h>
73 #include <sys/diskslice.h>
74 #include <sys/buf.h>
75 #include <sys/devicestat.h>
76 #include <sys/malloc.h>
77 #include <sys/cons.h>
78 #include <machine/bootinfo.h>
79 #include <machine/clock.h>
80 #include <machine/md_var.h>
81 #include <i386/isa/isa.h>
82 #include <i386/isa/isa_device.h>
83 #include <i386/isa/wdreg.h>
84 #include <sys/syslog.h>
85 #include <vm/vm.h>
86 #include <vm/pmap.h>
87
88 #include <i386/isa/atapi.h>
89
90 extern void wdstart(int ctrlr);
91
92 #ifdef IDE_DELAY
93 #define TIMEOUT IDE_DELAY
94 #else
95 #define TIMEOUT 10000
96 #endif
97 #define RETRIES 5 /* number of retries before giving up */
98 #define RECOVERYTIME 500000 /* usec for controller to recover after err */
99 #define MAXTRANSFER 255 /* max size of transfer in sectors */
100 /* correct max is 256 but some controllers */
101 /* can't handle that in all cases */
102 #define WDOPT_32BIT 0x8000
103 #define WDOPT_SLEEPHACK 0x4000
104 #define WDOPT_DMA 0x2000
105 #define WDOPT_LBA 0x1000
106 #define WDOPT_FORCEHD(x) (((x)&0x0f00)>>8)
107 #define WDOPT_MULTIMASK 0x00ff
108
109 /*
110 * Drive states. Used to initialize drive.
111 */
112
113 #define CLOSED 0 /* disk is closed. */
114 #define WANTOPEN 1 /* open requested, not started */
115 #define RECAL 2 /* doing restore */
116 #define OPEN 3 /* done with open */
117
118 #define PRIMARY 0
119
120 /*
121 * Disk geometry. A small part of struct disklabel.
122 * XXX disklabel.5 contains an old clone of disklabel.h.
123 */
124 struct diskgeom {
125 u_long d_secsize; /* # of bytes per sector */
126 u_long d_nsectors; /* # of data sectors per track */
127 u_long d_ntracks; /* # of tracks per cylinder */
128 u_long d_ncylinders; /* # of data cylinders per unit */
129 u_long d_secpercyl; /* # of data sectors per cylinder */
130 u_long d_secperunit; /* # of data sectors per unit */
131 u_long d_precompcyl; /* XXX always 0 */
132 };
133
134 /*
135 * The structure of a disk drive.
136 */
137 struct disk {
138 u_int dk_bc; /* byte count left */
139 short dk_skip; /* blocks already transferred */
140 int dk_ctrlr; /* physical controller number */
141 int dk_ctrlr_cmd640;/* controller number for CMD640 quirk */
142 u_int32_t dk_unit; /* physical unit number */
143 u_int32_t dk_lunit; /* logical unit number */
144 u_int32_t dk_interface; /* interface (two ctrlrs per interface) */
145 char dk_state; /* control state */
146 u_char dk_status; /* copy of status reg. */
147 u_char dk_error; /* copy of error reg. */
148 u_char dk_timeout; /* countdown to next timeout */
149 u_int32_t dk_port; /* i/o port base */
150 u_int32_t dk_altport; /* altstatus port base */
151 u_long cfg_flags; /* configured characteristics */
152 short dk_flags; /* drive characteristics found */
153 #define DKFL_SINGLE 0x00004 /* sector at a time mode */
154 #define DKFL_ERROR 0x00008 /* processing a disk error */
155 #define DKFL_LABELLING 0x00080 /* readdisklabel() in progress */
156 #define DKFL_32BIT 0x00100 /* use 32-bit i/o mode */
157 #define DKFL_MULTI 0x00200 /* use multi-i/o mode */
158 #define DKFL_USEDMA 0x00800 /* use DMA for data transfers */
159 #define DKFL_DMA 0x01000 /* using DMA on this transfer-- DKFL_SINGLE
160 * overrides this
161 */
162 #define DKFL_LBA 0x02000 /* use LBA for data transfers */
163 struct wdparams dk_params; /* ESDI/IDE drive/controller parameters */
164 unsigned int dk_multi; /* multi transfers */
165 int dk_currentiosize; /* current io size */
166 struct diskgeom dk_dd; /* device configuration data */
167 struct diskslices *dk_slices; /* virtual drives */
168 void *dk_dmacookie; /* handle for DMA services */
169
170 struct devstat dk_stats; /* devstat entry */
171 };
172
173 #define WD_COUNT_RETRIES
174 static int wdtest = 0;
175
176 static struct disk *wddrives[NWD]; /* table of units */
177 static struct buf_queue_head drive_queue[NWD]; /* head of queue per drive */
178 static struct {
179 int b_active;
180 } wdutab[NWD];
181 /*
182 static struct buf wdtab[NWDC];
183 */
184 static struct {
185 struct buf_queue_head controller_queue;
186 int b_errcnt;
187 int b_active;
188 } wdtab[NWDC];
189
190 struct wddma wddma[NWDC];
191
192 #ifdef notyet
193 static struct buf rwdbuf[NWD]; /* buffers for raw IO */
194 #endif
195
196 static int wdprobe(struct isa_device *dvp);
197 static int wdattach(struct isa_device *dvp);
198 static void wdustart(struct disk *du);
199 static int wdcontrol(struct buf *bp);
200 static int wdcommand(struct disk *du, u_int cylinder, u_int head,
201 u_int sector, u_int count, u_int command);
202 static int wdsetctlr(struct disk *du);
203 #if 0
204 static int wdwsetctlr(struct disk *du);
205 #endif
206 static int wdsetmode(int mode, void *wdinfo);
207 static int wdgetctlr(struct disk *du);
208 static void wderror(struct buf *bp, struct disk *du, char *mesg);
209 static void wdflushirq(struct disk *du, int old_ipl);
210 static int wdreset(struct disk *du);
211 static void wdsleep(int ctrlr, char *wmesg);
212 static timeout_t wdtimeout;
213 static int wdunwedge(struct disk *du);
214 static int wdwait(struct disk *du, u_char bits_wanted, int timeout);
215
216 struct isa_driver wdcdriver = {
217 wdprobe, wdattach, "wdc",
218 };
219
220
221
222 static d_open_t wdopen;
223 static d_close_t wdclose;
224 static d_strategy_t wdstrategy;
225 static d_ioctl_t wdioctl;
226 static d_dump_t wddump;
227 static d_psize_t wdsize;
228
229 #define CDEV_MAJOR 3
230 #define BDEV_MAJOR 0
231
232
233 static struct cdevsw wd_cdevsw = {
234 /* open */ wdopen,
235 /* close */ wdclose,
236 /* read */ physread,
237 /* write */ physwrite,
238 /* ioctl */ wdioctl,
239 /* poll */ nopoll,
240 /* mmap */ nommap,
241 /* strategy */ wdstrategy,
242 /* name */ "wd",
243 /* maj */ CDEV_MAJOR,
244 /* dump */ wddump,
245 /* psize */ wdsize,
246 /* flags */ D_DISK,
247 /* bmaj */ BDEV_MAJOR
248 };
249
250
251 static int atapictrlr;
252 static int eide_quirks;
253
254
255 /*
256 * Here we use the pci-subsystem to find out, whether there is
257 * a cmd640b-chip attached on this pci-bus. This public routine
258 * will be called by ide_pci.c
259 */
260
261 void
262 wdc_pci(int quirks)
263 {
264 eide_quirks = quirks;
265 }
266
267 /*
268 * Probe for controller.
269 */
270 static int
271 wdprobe(struct isa_device *dvp)
272 {
273 int unit = dvp->id_unit;
274 int interface;
275 struct disk *du;
276
277 if (unit >= NWDC)
278 return (0);
279
280 du = malloc(sizeof *du, M_TEMP, M_NOWAIT);
281 if (du == NULL)
282 return (0);
283 bzero(du, sizeof *du);
284 du->dk_ctrlr = dvp->id_unit;
285 interface = du->dk_ctrlr / 2;
286 du->dk_interface = interface;
287 du->dk_port = dvp->id_iobase;
288 if (wddma[interface].wdd_candma != NULL) {
289 du->dk_dmacookie =
290 wddma[interface].wdd_candma(dvp->id_iobase, du->dk_ctrlr,
291 du->dk_unit);
292 du->dk_altport =
293 wddma[interface].wdd_altiobase(du->dk_dmacookie);
294 }
295 if (du->dk_altport == 0)
296 du->dk_altport = du->dk_port + wd_ctlr;
297
298 /* check if we have registers that work */
299 outb(du->dk_port + wd_sdh, WDSD_IBM); /* set unit 0 */
300 outb(du->dk_port + wd_cyl_lo, 0xa5); /* wd_cyl_lo is read/write */
301 if (inb(du->dk_port + wd_cyl_lo) == 0xff) { /* XXX too weak */
302 /* There is no master, try the ATAPI slave. */
303 du->dk_unit = 1;
304 outb(du->dk_port + wd_sdh, WDSD_IBM | 0x10);
305 outb(du->dk_port + wd_cyl_lo, 0xa5);
306 if (inb(du->dk_port + wd_cyl_lo) == 0xff)
307 goto nodevice;
308 }
309
310 if (wdreset(du) == 0)
311 goto reset_ok;
312 /* test for ATAPI signature */
313 outb(du->dk_port + wd_sdh, WDSD_IBM); /* master */
314 if (inb(du->dk_port + wd_cyl_lo) == 0x14 &&
315 inb(du->dk_port + wd_cyl_hi) == 0xeb)
316 goto reset_ok;
317 du->dk_unit = 1;
318 outb(du->dk_port + wd_sdh, WDSD_IBM | 0x10); /* slave */
319 if (inb(du->dk_port + wd_cyl_lo) == 0x14 &&
320 inb(du->dk_port + wd_cyl_hi) == 0xeb)
321 goto reset_ok;
322 DELAY(RECOVERYTIME);
323 if (wdreset(du) != 0) {
324 goto nodevice;
325 }
326 reset_ok:
327
328 /* execute a controller only command */
329 if (wdcommand(du, 0, 0, 0, 0, WDCC_DIAGNOSE) != 0
330 || wdwait(du, 0, TIMEOUT) < 0) {
331 goto nodevice;
332 }
333
334 /*
335 * drive(s) did not time out during diagnostic :
336 * Get error status and check that both drives are OK.
337 * Table 9-2 of ATA specs suggests that we must check for
338 * a value of 0x01
339 *
340 * Strangely, some controllers will return a status of
341 * 0x81 (drive 0 OK, drive 1 failure), and then when
342 * the DRV bit is set, return status of 0x01 (OK) for
343 * drive 2. (This seems to contradict the ATA spec.)
344 */
345 du->dk_error = inb(du->dk_port + wd_error);
346 if(du->dk_error != 0x01 && du->dk_error != 0) {
347 if(du->dk_error & 0x80) { /* drive 1 failure */
348
349 /* first set the DRV bit */
350 u_int sdh;
351 sdh = inb(du->dk_port+ wd_sdh);
352 sdh = sdh | 0x10;
353 outb(du->dk_port+ wd_sdh, sdh);
354
355 /* Wait, to make sure drv 1 has completed diags */
356 if ( wdwait(du, 0, TIMEOUT) < 0)
357 goto nodevice;
358
359 /* Get status for drive 1 */
360 du->dk_error = inb(du->dk_port + wd_error);
361 /* printf("Error (drv 1) : %x\n", du->dk_error); */
362 /*
363 * Sometimes (apparently mostly with ATAPI
364 * drives involved) 0x81 really means 0x81
365 * (drive 0 OK, drive 1 failed).
366 */
367 if(du->dk_error != 0x01 && du->dk_error != 0x81)
368 goto nodevice;
369 } else /* drive 0 fail */
370 goto nodevice;
371 }
372
373
374 free(du, M_TEMP);
375 return (IO_WDCSIZE);
376
377 nodevice:
378 free(du, M_TEMP);
379 return (0);
380 }
381
382 /*
383 * Attach each drive if possible.
384 */
385 static int
386 wdattach(struct isa_device *dvp)
387 {
388 int unit, lunit, flags, i;
389 struct disk *du;
390 struct wdparams *wp;
391 static char buf[] = "wdcXXX";
392 static int once;
393
394 dvp->id_intr = wdintr;
395
396 if (dvp->id_unit >= NWDC)
397 return (0);
398
399 if (!once) {
400 cdevsw_add(&wd_cdevsw);
401 once++;
402 }
403
404 if (eide_quirks & Q_CMD640B) {
405 if (dvp->id_unit == PRIMARY) {
406 printf("wdc0: CMD640B workaround enabled\n");
407 bufq_init(&wdtab[PRIMARY].controller_queue);
408 }
409 } else
410 bufq_init(&wdtab[dvp->id_unit].controller_queue);
411
412 sprintf(buf, "wdc%d", dvp->id_unit);
413 for (i = resource_query_string(-1, "at", buf);
414 i != -1;
415 i = resource_query_string(i, "at", buf)) {
416 if (strcmp(resource_query_name(i), "wd"))
417 /* Avoid a bit of foot shooting. */
418 continue;
419
420 lunit = resource_query_unit(i);
421 if (lunit >= NWD)
422 continue;
423
424 if (resource_int_value("wd", lunit, "drive", &unit) != 0)
425 continue;
426 if (resource_int_value("wd", lunit, "flags", &flags) != 0)
427 flags = 0;
428
429 du = malloc(sizeof *du, M_TEMP, M_NOWAIT);
430 if (du == NULL)
431 continue;
432 if (wddrives[lunit] != NULL)
433 panic("drive attached twice");
434 wddrives[lunit] = du;
435 bufq_init(&drive_queue[lunit]);
436 bzero(du, sizeof *du);
437 du->dk_ctrlr = dvp->id_unit;
438 if (eide_quirks & Q_CMD640B) {
439 du->dk_ctrlr_cmd640 = PRIMARY;
440 } else {
441 du->dk_ctrlr_cmd640 = du->dk_ctrlr;
442 }
443 du->dk_unit = unit;
444 du->dk_lunit = lunit;
445 du->dk_port = dvp->id_iobase;
446
447 du->dk_altport = du->dk_port + wd_ctlr;
448 /*
449 * Use the individual device flags or the controller
450 * flags.
451 */
452 du->cfg_flags = flags |
453 ((dvp->id_flags) >> (16 * unit));
454
455 if (wdgetctlr(du) == 0) {
456 /*
457 * Print out description of drive.
458 * wdp_model may not be null terminated.
459 */
460 printf("wdc%d: unit %d (wd%d): <%.*s>",
461 dvp->id_unit, unit, lunit,
462 (int)sizeof(du->dk_params.wdp_model),
463 du->dk_params.wdp_model);
464 if (du->dk_flags & DKFL_LBA)
465 printf(", LBA");
466 if (du->dk_flags & DKFL_USEDMA)
467 printf(", DMA");
468 if (du->dk_flags & DKFL_32BIT)
469 printf(", 32-bit");
470 if (du->dk_multi > 1)
471 printf(", multi-block-%d", du->dk_multi);
472 if (du->cfg_flags & WDOPT_SLEEPHACK)
473 printf(", sleep-hack");
474 printf("\n");
475 if (du->dk_params.wdp_heads == 0)
476 printf("wd%d: size unknown, using %s values\n",
477 lunit, du->dk_dd.d_secperunit > 17
478 ? "BIOS" : "fake");
479 printf( "wd%d: %luMB (%lu sectors), "
480 "%lu cyls, %lu heads, %lu S/T, %lu B/S\n",
481 lunit,
482 du->dk_dd.d_secperunit
483 / ((1024L * 1024L) / du->dk_dd.d_secsize),
484 du->dk_dd.d_secperunit,
485 du->dk_dd.d_ncylinders,
486 du->dk_dd.d_ntracks,
487 du->dk_dd.d_nsectors,
488 du->dk_dd.d_secsize);
489
490 if (bootverbose) {
491 wp = &du->dk_params;
492 printf( "wd%d: ATA INQUIRE valid = %04x, "
493 "dmamword = %04x, apio = %04x, "
494 "udma = %04x\n",
495 du->dk_lunit,
496 wp->wdp_atavalid,
497 wp->wdp_dmamword,
498 wp->wdp_eidepiomodes,
499 wp->wdp_udmamode);
500 }
501
502 /*
503 * Start timeout routine for this drive.
504 * XXX timeout should be per controller.
505 */
506 wdtimeout(du);
507
508 make_dev(&wd_cdevsw,
509 dkmakeminor(lunit, WHOLE_DISK_SLICE, RAW_PART),
510 UID_ROOT, GID_OPERATOR, 0640, "rwd%d", lunit);
511
512 /*
513 * Export the drive to the devstat interface.
514 */
515 devstat_add_entry(&du->dk_stats, "wd",
516 lunit, du->dk_dd.d_secsize,
517 DEVSTAT_NO_ORDERED_TAGS,
518 DEVSTAT_TYPE_DIRECT |
519 DEVSTAT_TYPE_IF_IDE,
520 DEVSTAT_PRIORITY_DISK);
521
522 } else {
523 free(du, M_TEMP);
524 wddrives[lunit] = NULL;
525 }
526 }
527 /*
528 * Probe all free IDE units, searching for ATAPI drives.
529 */
530 for (unit=0; unit<2; ++unit) {
531 for (lunit=0; lunit<NWD; ++lunit)
532 if (wddrives[lunit] &&
533 wddrives[lunit]->dk_ctrlr == dvp->id_unit &&
534 wddrives[lunit]->dk_unit == unit)
535 goto next;
536 if (atapi_attach (dvp->id_unit, unit, dvp->id_iobase))
537 atapictrlr = dvp->id_unit;
538 next: ;
539 }
540 /*
541 * Discard any interrupts generated by wdgetctlr(). wdflushirq()
542 * doesn't work now because the ambient ipl is too high.
543 */
544 if (eide_quirks & Q_CMD640B) {
545 wdtab[PRIMARY].b_active = 2;
546 } else {
547 wdtab[dvp->id_unit].b_active = 2;
548 }
549
550 return (1);
551 }
552
553 /* Read/write routine for a buffer. Finds the proper unit, range checks
554 * arguments, and schedules the transfer. Does not wait for the transfer
555 * to complete. Multi-page transfers are supported. All I/O requests must
556 * be a multiple of a sector in length.
557 */
558 void
559 wdstrategy(register struct buf *bp)
560 {
561 struct disk *du;
562 int lunit = dkunit(bp->b_dev);
563 int s;
564
565 /* valid unit, controller, and request? */
566 if (lunit >= NWD || bp->b_blkno < 0 || (du = wddrives[lunit]) == NULL
567 || bp->b_bcount % DEV_BSIZE != 0) {
568
569 bp->b_error = EINVAL;
570 bp->b_flags |= B_ERROR;
571 goto done;
572 }
573
574 /*
575 * Do bounds checking, adjust transfer, and set b_pblkno.
576 */
577 if (dscheck(bp, du->dk_slices) <= 0)
578 goto done;
579
580 /* queue transfer on drive, activate drive and controller if idle */
581 s = splbio();
582
583 /* Pick up changes made by readdisklabel(). */
584 if (du->dk_flags & DKFL_LABELLING && du->dk_state > RECAL) {
585 wdsleep(du->dk_ctrlr, "wdlab");
586 du->dk_state = WANTOPEN;
587 }
588
589 bufqdisksort(&drive_queue[lunit], bp);
590
591 if (wdutab[lunit].b_active == 0)
592 wdustart(du); /* start drive */
593
594 if (wdtab[du->dk_ctrlr_cmd640].b_active == 0)
595 wdstart(du->dk_ctrlr); /* start controller */
596
597 /* Tell devstat that we have started a transaction on this drive */
598 devstat_start_transaction(&du->dk_stats);
599
600 splx(s);
601 return;
602
603 done:
604 /* toss transfer, we're done early */
605 biodone(bp);
606 }
607
608 /*
609 * Routine to queue a command to the controller. The unit's
610 * request is linked into the active list for the controller.
611 * If the controller is idle, the transfer is started.
612 */
613 static void
614 wdustart(register struct disk *du)
615 {
616 register struct buf *bp;
617 int ctrlr = du->dk_ctrlr_cmd640;
618
619 /* unit already active? */
620 if (wdutab[du->dk_lunit].b_active)
621 return;
622
623
624 bp = bufq_first(&drive_queue[du->dk_lunit]);
625 if (bp == NULL) { /* yes, an assign */
626 return;
627 }
628 /*
629 * store away which device we came from.
630 */
631 bp->b_driver1 = du;
632
633 bufq_remove(&drive_queue[du->dk_lunit], bp);
634
635 /* link onto controller queue */
636 bufq_insert_tail(&wdtab[ctrlr].controller_queue, bp);
637
638 /* mark the drive unit as busy */
639 wdutab[du->dk_lunit].b_active = 1;
640
641 }
642
643 /*
644 * Controller startup routine. This does the calculation, and starts
645 * a single-sector read or write operation. Called to start a transfer,
646 * or from the interrupt routine to continue a multi-sector transfer.
647 * RESTRICTIONS:
648 * 1. The transfer length must be an exact multiple of the sector size.
649 */
650
651 void
652 wdstart(int ctrlr)
653 {
654 register struct disk *du;
655 register struct buf *bp;
656 struct diskgeom *lp; /* XXX sic */
657 long blknum;
658 long secpertrk, secpercyl;
659 u_int lunit;
660 u_int count;
661 int ctrlr_atapi;
662
663 if (eide_quirks & Q_CMD640B) {
664 ctrlr = PRIMARY;
665 ctrlr_atapi = atapictrlr;
666 } else {
667 ctrlr_atapi = ctrlr;
668 }
669
670 if (wdtab[ctrlr].b_active == 2)
671 wdtab[ctrlr].b_active = 0;
672 if (wdtab[ctrlr].b_active)
673 return;
674 /* is there a drive for the controller to do a transfer with? */
675 bp = bufq_first(&wdtab[ctrlr].controller_queue);
676 if (bp == NULL) {
677 if (atapi_strt && atapi_strt (ctrlr_atapi))
678 /* mark controller active in ATAPI mode */
679 wdtab[ctrlr].b_active = 3;
680 return;
681 }
682
683 /* obtain controller and drive information */
684 lunit = dkunit(bp->b_dev);
685 du = wddrives[lunit];
686
687 /* if not really a transfer, do control operations specially */
688 if (du->dk_state < OPEN) {
689 if (du->dk_state != WANTOPEN)
690 printf("wd%d: wdstart: weird dk_state %d\n",
691 du->dk_lunit, du->dk_state);
692 if (wdcontrol(bp) != 0)
693 printf("wd%d: wdstart: wdcontrol returned nonzero, state = %d\n",
694 du->dk_lunit, du->dk_state);
695 return;
696 }
697
698 /* calculate transfer details */
699 blknum = bp->b_pblkno + du->dk_skip;
700 #ifdef WDDEBUG
701 if (du->dk_skip == 0)
702 printf("wd%d: wdstart: %s %d@%d; map ", lunit,
703 (bp->b_flags & B_READ) ? "read" : "write",
704 bp->b_bcount, blknum);
705 else
706 printf(" %d)%x", du->dk_skip, inb(du->dk_altport));
707 #endif
708
709 lp = &du->dk_dd;
710 secpertrk = lp->d_nsectors;
711 secpercyl = lp->d_secpercyl;
712
713 if (du->dk_skip == 0)
714 du->dk_bc = bp->b_bcount;
715
716 wdtab[ctrlr].b_active = 1; /* mark controller active */
717
718 /* if starting a multisector transfer, or doing single transfers */
719 if (du->dk_skip == 0 || (du->dk_flags & DKFL_SINGLE)) {
720 u_int command;
721 u_int count1;
722 long cylin, head, sector;
723
724 if (du->dk_flags & DKFL_LBA) {
725 sector = (blknum >> 0) & 0xff;
726 cylin = (blknum >> 8) & 0xffff;
727 head = ((blknum >> 24) & 0xf) | WDSD_LBA;
728 } else {
729 cylin = blknum / secpercyl;
730 head = (blknum % secpercyl) / secpertrk;
731 sector = blknum % secpertrk;
732 }
733 /*
734 * XXX this looks like an attempt to skip bad sectors
735 * on write.
736 */
737 if (wdtab[ctrlr].b_errcnt && (bp->b_flags & B_READ) == 0)
738 du->dk_bc += DEV_BSIZE;
739
740 count1 = howmany( du->dk_bc, DEV_BSIZE);
741
742 du->dk_flags &= ~DKFL_MULTI;
743
744 if (du->dk_flags & DKFL_SINGLE) {
745 command = (bp->b_flags & B_READ)
746 ? WDCC_READ : WDCC_WRITE;
747 count1 = 1;
748 du->dk_currentiosize = 1;
749 } else {
750 if((du->dk_flags & DKFL_USEDMA) &&
751 wddma[du->dk_interface].wdd_dmaverify(du->dk_dmacookie,
752 (void *)((int)bp->b_data +
753 du->dk_skip * DEV_BSIZE),
754 du->dk_bc,
755 bp->b_flags & B_READ)) {
756 du->dk_flags |= DKFL_DMA;
757 if( bp->b_flags & B_READ)
758 command = WDCC_READ_DMA;
759 else
760 command = WDCC_WRITE_DMA;
761 du->dk_currentiosize = count1;
762 } else if( (count1 > 1) && (du->dk_multi > 1)) {
763 du->dk_flags |= DKFL_MULTI;
764 if( bp->b_flags & B_READ) {
765 command = WDCC_READ_MULTI;
766 } else {
767 command = WDCC_WRITE_MULTI;
768 }
769 du->dk_currentiosize = du->dk_multi;
770 if( du->dk_currentiosize > count1)
771 du->dk_currentiosize = count1;
772 } else {
773 if( bp->b_flags & B_READ) {
774 command = WDCC_READ;
775 } else {
776 command = WDCC_WRITE;
777 }
778 du->dk_currentiosize = 1;
779 }
780 }
781
782 /*
783 * XXX this loop may never terminate. The code to handle
784 * counting down of retries and eventually failing the i/o
785 * is in wdintr() and we can't get there from here.
786 */
787 if (wdtest != 0) {
788 if (--wdtest == 0) {
789 wdtest = 100;
790 printf("dummy wdunwedge\n");
791 wdunwedge(du);
792 }
793 }
794
795 if ((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA) {
796 wddma[du->dk_interface].wdd_dmaprep(du->dk_dmacookie,
797 (void *)((int)bp->b_data +
798 du->dk_skip * DEV_BSIZE),
799 du->dk_bc,
800 bp->b_flags & B_READ);
801 }
802 while (wdcommand(du, cylin, head, sector, count1, command)
803 != 0) {
804 wderror(bp, du,
805 "wdstart: timeout waiting to give command");
806 wdunwedge(du);
807 }
808 #ifdef WDDEBUG
809 printf("cylin %ld head %ld sector %ld addr %x sts %x\n",
810 cylin, head, sector,
811 (int)bp->b_data + du->dk_skip * DEV_BSIZE,
812 inb(du->dk_altport));
813 #endif
814 }
815
816 /*
817 * Schedule wdtimeout() to wake up after a few seconds. Retrying
818 * unmarked bad blocks can take 3 seconds! Then it is not good that
819 * we retry 5 times.
820 *
821 * On the first try, we give it 10 seconds, for drives that may need
822 * to spin up.
823 *
824 * XXX wdtimeout() doesn't increment the error count so we may loop
825 * forever. More seriously, the loop isn't forever but causes a
826 * crash.
827 *
828 * TODO fix b_resid bug elsewhere (fd.c....). Fix short but positive
829 * counts being discarded after there is an error (in physio I
830 * think). Discarding them would be OK if the (special) file offset
831 * was not advanced.
832 */
833 if (wdtab[ctrlr].b_errcnt == 0)
834 du->dk_timeout = 1 + 10;
835 else
836 du->dk_timeout = 1 + 3;
837
838 /* if this is a DMA op, start DMA and go away until it's done. */
839 if ((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA) {
840 wddma[du->dk_interface].wdd_dmastart(du->dk_dmacookie);
841 return;
842 }
843
844 /* If this is a read operation, just go away until it's done. */
845 if (bp->b_flags & B_READ)
846 return;
847
848 /* Ready to send data? */
849 if (wdwait(du, WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ, TIMEOUT) < 0) {
850 wderror(bp, du, "wdstart: timeout waiting for DRQ");
851 /*
852 * XXX what do we do now? If we've just issued the command,
853 * then we can treat this failure the same as a command
854 * failure. But if we are continuing a multi-sector write,
855 * the command was issued ages ago, so we can't simply
856 * restart it.
857 *
858 * XXX we waste a lot of time unnecessarily translating block
859 * numbers to cylin/head/sector for continued i/o's.
860 */
861 }
862
863 count = 1;
864 if( du->dk_flags & DKFL_MULTI) {
865 count = howmany(du->dk_bc, DEV_BSIZE);
866 if( count > du->dk_multi)
867 count = du->dk_multi;
868 if( du->dk_currentiosize > count)
869 du->dk_currentiosize = count;
870 }
871
872 if (du->dk_flags & DKFL_32BIT)
873 outsl(du->dk_port + wd_data,
874 (void *)((int)bp->b_data + du->dk_skip * DEV_BSIZE),
875 (count * DEV_BSIZE) / sizeof(long));
876 else
877 outsw(du->dk_port + wd_data,
878 (void *)((int)bp->b_data + du->dk_skip * DEV_BSIZE),
879 (count * DEV_BSIZE) / sizeof(short));
880 du->dk_bc -= DEV_BSIZE * count;
881 }
882
883 /* Interrupt routine for the controller. Acknowledge the interrupt, check for
884 * errors on the current operation, mark it done if necessary, and start
885 * the next request. Also check for a partially done transfer, and
886 * continue with the next chunk if so.
887 */
888
889 void
890 wdintr(void *unitnum)
891 {
892 register struct disk *du;
893 register struct buf *bp;
894 int dmastat = 0; /* Shut up GCC */
895 int unit = (int)unitnum;
896
897 int ctrlr_atapi;
898
899 if (eide_quirks & Q_CMD640B) {
900 unit = PRIMARY;
901 ctrlr_atapi = atapictrlr;
902 } else {
903 ctrlr_atapi = unit;
904 }
905
906 if (wdtab[unit].b_active == 2)
907 return; /* intr in wdflushirq() */
908 if (!wdtab[unit].b_active) {
909 #ifdef WDDEBUG
910 /*
911 * These happen mostly because the power-mgt part of the
912 * bios shuts us down, and we just manage to see the
913 * interrupt from the "SLEEP" command.
914 */
915 printf("wdc%d: extra interrupt\n", unit);
916 #endif
917 return;
918 }
919 if (wdtab[unit].b_active == 3) {
920 /* process an ATAPI interrupt */
921 if (atapi_intr && atapi_intr (ctrlr_atapi))
922 /* ATAPI op continues */
923 return;
924 /* controller is free, start new op */
925 wdtab[unit].b_active = 0;
926 wdstart (unit);
927 return;
928 }
929 bp = bufq_first(&wdtab[unit].controller_queue);
930 du = wddrives[dkunit(bp->b_dev)];
931
932 /* finish off DMA */
933 if ((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA) {
934 /* XXX SMP boxes sometimes generate an early intr. Why? */
935 if ((wddma[du->dk_interface].wdd_dmastatus(du->dk_dmacookie) &
936 WDDS_INTERRUPT) == 0)
937 return;
938 dmastat = wddma[du->dk_interface].wdd_dmadone(du->dk_dmacookie);
939 }
940
941 du->dk_timeout = 0;
942
943 /* check drive status/failure */
944 if (wdwait(du, 0, TIMEOUT) < 0) {
945 wderror(bp, du, "wdintr: timeout waiting for status");
946 du->dk_status |= WDCS_ERR; /* XXX */
947 }
948
949 /* is it not a transfer, but a control operation? */
950 if (du->dk_state < OPEN) {
951 wdtab[unit].b_active = 0;
952 switch (wdcontrol(bp)) {
953 case 0:
954 return;
955 case 1:
956 wdstart(unit);
957 return;
958 case 2:
959 goto done;
960 }
961 }
962
963 /* have we an error? */
964 if ((du->dk_status & (WDCS_ERR | WDCS_ECCCOR))
965 || (((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA)
966 && dmastat != WDDS_INTERRUPT)) {
967
968 unsigned int errstat;
969 oops:
970 /*
971 * XXX bogus inb() here
972 */
973 errstat = inb(du->dk_port + wd_error);
974
975 if(((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA) &&
976 (errstat & WDERR_ABORT)) {
977 wderror(bp, du, "reverting to PIO mode");
978 du->dk_flags &= ~DKFL_USEDMA;
979 } else if((du->dk_flags & DKFL_MULTI) &&
980 (errstat & WDERR_ABORT)) {
981 wderror(bp, du, "reverting to non-multi sector mode");
982 du->dk_multi = 1;
983 }
984
985 if (!(du->dk_status & (WDCS_ERR | WDCS_ECCCOR)) &&
986 (((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA) &&
987 (dmastat != WDDS_INTERRUPT)))
988 printf("wd%d: DMA failure, DMA status %b\n",
989 du->dk_lunit, dmastat, WDDS_BITS);
990 #ifdef WDDEBUG
991 wderror(bp, du, "wdintr");
992 #endif
993 if ((du->dk_flags & DKFL_SINGLE) == 0) {
994 du->dk_flags |= DKFL_ERROR;
995 goto outt;
996 }
997
998 if (du->dk_status & WDCS_ERR) {
999 if (++wdtab[unit].b_errcnt < RETRIES) {
1000 wdtab[unit].b_active = 0;
1001 } else {
1002 wderror(bp, du, "hard error");
1003 bp->b_error = EIO;
1004 bp->b_flags |= B_ERROR; /* flag the error */
1005 }
1006 } else if (du->dk_status & WDCS_ECCCOR)
1007 wderror(bp, du, "soft ecc");
1008 }
1009
1010 /*
1011 * If this was a successful read operation, fetch the data.
1012 */
1013 if (((bp->b_flags & (B_READ | B_ERROR)) == B_READ)
1014 && !((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA)
1015 && wdtab[unit].b_active) {
1016 u_int chk, dummy, multisize;
1017 multisize = chk = du->dk_currentiosize * DEV_BSIZE;
1018 if( du->dk_bc < chk) {
1019 chk = du->dk_bc;
1020 if( ((chk + DEV_BSIZE - 1) / DEV_BSIZE) < du->dk_currentiosize) {
1021 du->dk_currentiosize = (chk + DEV_BSIZE - 1) / DEV_BSIZE;
1022 multisize = du->dk_currentiosize * DEV_BSIZE;
1023 }
1024 }
1025
1026 /* ready to receive data? */
1027 if ((du->dk_status & (WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ))
1028 != (WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ))
1029 wderror(bp, du, "wdintr: read intr arrived early");
1030 if (wdwait(du, WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ, TIMEOUT) != 0) {
1031 wderror(bp, du, "wdintr: read error detected late");
1032 goto oops;
1033 }
1034
1035 /* suck in data */
1036 if( du->dk_flags & DKFL_32BIT)
1037 insl(du->dk_port + wd_data,
1038 (void *)((int)bp->b_data + du->dk_skip * DEV_BSIZE),
1039 chk / sizeof(long));
1040 else
1041 insw(du->dk_port + wd_data,
1042 (void *)((int)bp->b_data + du->dk_skip * DEV_BSIZE),
1043 chk / sizeof(short));
1044 du->dk_bc -= chk;
1045
1046 /* XXX for obsolete fractional sector reads. */
1047 while (chk < multisize) {
1048 insw(du->dk_port + wd_data, &dummy, 1);
1049 chk += sizeof(short);
1050 }
1051
1052 }
1053
1054 /* final cleanup on DMA */
1055 if (((bp->b_flags & B_ERROR) == 0)
1056 && ((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA)
1057 && wdtab[unit].b_active) {
1058 int iosize;
1059
1060 iosize = du->dk_currentiosize * DEV_BSIZE;
1061
1062 du->dk_bc -= iosize;
1063
1064 }
1065
1066 outt:
1067 if (wdtab[unit].b_active) {
1068 if ((bp->b_flags & B_ERROR) == 0) {
1069 du->dk_skip += du->dk_currentiosize;/* add to successful sectors */
1070 if (wdtab[unit].b_errcnt)
1071 wderror(bp, du, "soft error");
1072 wdtab[unit].b_errcnt = 0;
1073
1074 /* see if more to transfer */
1075 if (du->dk_bc > 0 && (du->dk_flags & DKFL_ERROR) == 0) {
1076 if( (du->dk_flags & DKFL_SINGLE) ||
1077 ((bp->b_flags & B_READ) == 0)) {
1078 wdtab[unit].b_active = 0;
1079 wdstart(unit);
1080 } else {
1081 du->dk_timeout = 1 + 3;
1082 }
1083 return; /* next chunk is started */
1084 } else if ((du->dk_flags & (DKFL_SINGLE | DKFL_ERROR))
1085 == DKFL_ERROR) {
1086 du->dk_skip = 0;
1087 du->dk_flags &= ~DKFL_ERROR;
1088 du->dk_flags |= DKFL_SINGLE;
1089 wdtab[unit].b_active = 0;
1090 wdstart(unit);
1091 return; /* redo xfer sector by sector */
1092 }
1093 }
1094
1095 done: ;
1096 /* done with this transfer, with or without error */
1097 du->dk_flags &= ~(DKFL_SINGLE|DKFL_DMA);
1098 bufq_remove( &wdtab[unit].controller_queue, bp);
1099 wdtab[unit].b_errcnt = 0;
1100 bp->b_resid = bp->b_bcount - du->dk_skip * DEV_BSIZE;
1101 wdutab[du->dk_lunit].b_active = 0;
1102 du->dk_skip = 0;
1103 devstat_end_transaction_buf(&du->dk_stats, bp);
1104 biodone(bp);
1105 }
1106
1107 /* controller idle */
1108 wdtab[unit].b_active = 0;
1109
1110 /* anything more on drive queue? */
1111 wdustart(du);
1112 /* anything more for controller to do? */
1113 wdstart(unit);
1114 }
1115
1116 /*
1117 * Initialize a drive.
1118 */
1119 int
1120 wdopen(dev_t dev, int flags, int fmt, struct proc *p)
1121 {
1122 register unsigned int lunit;
1123 register struct disk *du;
1124 int error;
1125
1126 lunit = dkunit(dev);
1127 if (lunit >= NWD || dktype(dev) != 0)
1128 return (ENXIO);
1129 du = wddrives[lunit];
1130 if (du == NULL)
1131 return (ENXIO);
1132
1133 dev->si_iosize_max = 248 * 512;
1134 /* Finish flushing IRQs left over from wdattach(). */
1135 if (wdtab[du->dk_ctrlr_cmd640].b_active == 2)
1136 wdtab[du->dk_ctrlr_cmd640].b_active = 0;
1137
1138 /* spin waiting for anybody else reading the disk label */
1139 while (du->dk_flags & DKFL_LABELLING)
1140 tsleep((caddr_t)&du->dk_flags, PZERO - 1, "wdopen", 1);
1141 #if 1
1142 wdsleep(du->dk_ctrlr, "wdopn1");
1143 du->dk_flags |= DKFL_LABELLING;
1144 du->dk_state = WANTOPEN;
1145 {
1146 struct disklabel label;
1147
1148 bzero(&label, sizeof label);
1149 label.d_secsize = du->dk_dd.d_secsize;
1150 label.d_nsectors = du->dk_dd.d_nsectors;
1151 label.d_ntracks = du->dk_dd.d_ntracks;
1152 label.d_ncylinders = du->dk_dd.d_ncylinders;
1153 label.d_secpercyl = du->dk_dd.d_secpercyl;
1154 label.d_secperunit = du->dk_dd.d_secperunit;
1155 error = dsopen(dev, fmt, 0, &du->dk_slices, &label);
1156 }
1157 du->dk_flags &= ~DKFL_LABELLING;
1158 wdsleep(du->dk_ctrlr, "wdopn2");
1159 return (error);
1160 #else
1161 if ((du->dk_flags & DKFL_BSDLABEL) == 0) {
1162 /*
1163 * wdtab[ctrlr].b_active != 0 implies XXX applicable now ??
1164 * drive_queue[lunit].b_act == NULL (?) XXX applicable now ??
1165 * so the following guards most things (until the next i/o).
1166 * It doesn't guard against a new i/o starting and being
1167 * affected by the label being changed. Sigh.
1168 */
1169 wdsleep(du->dk_ctrlr, "wdopn1");
1170
1171 du->dk_flags |= DKFL_LABELLING;
1172 du->dk_state = WANTOPEN;
1173
1174 error = dsinit(dkmodpart(dev, RAW_PART),
1175 &du->dk_dd, &du->dk_slices);
1176 if (error != 0) {
1177 du->dk_flags &= ~DKFL_LABELLING;
1178 return (error);
1179 }
1180 /* XXX check value returned by wdwsetctlr(). */
1181 wdwsetctlr(du);
1182 if (dkslice(dev) == WHOLE_DISK_SLICE) {
1183 dsopen(dev, fmt, du->dk_slices);
1184 return (0);
1185 }
1186
1187 /*
1188 * Read label using RAW_PART partition.
1189 *
1190 * If the drive has an MBR, then the current geometry (from
1191 * wdgetctlr()) is used to read it; then the BIOS/DOS
1192 * geometry is inferred and used to read the label off the
1193 * 'c' partition. Otherwise the label is read using the
1194 * current geometry. The label gives the final geometry.
1195 * If bad sector handling is enabled, then this geometry
1196 * is used to read the bad sector table. The geometry
1197 * changes occur inside readdisklabel() and are propagated
1198 * to the driver by resetting the state machine.
1199 *
1200 * XXX can now handle changes directly since dsinit() doesn't
1201 * do too much.
1202 */
1203 msg = correct_readdisklabel(dkmodpart(dev, RAW_PART),
1204 &du->dk_dd);
1205 /* XXX check value returned by wdwsetctlr(). */
1206 wdwsetctlr(du);
1207 du->dk_flags &= ~DKFL_LABELLING;
1208 if (msg != NULL) {
1209 log(LOG_WARNING, "wd%d: cannot find label (%s)\n",
1210 lunit, msg);
1211 if (part != RAW_PART)
1212 return (EINVAL); /* XXX needs translation */
1213 /*
1214 * Soon return. This is how slices without labels
1215 * are allowed. They only work on the raw partition.
1216 */
1217 } else {
1218 unsigned long newsize, offset, size;
1219 #if 0
1220 /*
1221 * Force RAW_PART partition to be the whole disk.
1222 */
1223 offset = du->dk_dd.d_partitions[RAW_PART].p_offset;
1224 if (offset != 0) {
1225 printf(
1226 "wd%d: changing offset of '%c' partition from %lu to 0\n",
1227 du->dk_lunit, 'a' + RAW_PART, offset);
1228 du->dk_dd.d_partitions[RAW_PART].p_offset = 0;
1229 }
1230 size = du->dk_dd.d_partitions[RAW_PART].p_size;
1231 newsize = du->dk_dd.d_secperunit; /* XXX */
1232 if (size != newsize) {
1233 printf(
1234 "wd%d: changing size of '%c' partition from %lu to %lu\n",
1235 du->dk_lunit, 'a' + RAW_PART, size,
1236 newsize);
1237 du->dk_dd.d_partitions[RAW_PART].p_size
1238 = newsize;
1239 }
1240 #endif
1241 }
1242
1243 /* Pick up changes made by readdisklabel(). */
1244 wdsleep(du->dk_ctrlr, "wdopn2");
1245 du->dk_state = WANTOPEN;
1246 }
1247
1248 /*
1249 * Warn if a partion is opened that overlaps another partition which
1250 * is open unless one is the "raw" partition (whole disk).
1251 */
1252 if ((du->dk_openpart & mask) == 0 && part != RAW_PART) {
1253 int start, end;
1254
1255 pp = &du->dk_dd.d_partitions[part];
1256 start = pp->p_offset;
1257 end = pp->p_offset + pp->p_size;
1258 for (pp = du->dk_dd.d_partitions;
1259 pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions];
1260 pp++) {
1261 if (pp->p_offset + pp->p_size <= start ||
1262 pp->p_offset >= end)
1263 continue;
1264 if (pp - du->dk_dd.d_partitions == RAW_PART)
1265 continue;
1266 if (du->dk_openpart
1267 & (1 << (pp - du->dk_dd.d_partitions)))
1268 log(LOG_WARNING,
1269 "wd%d%c: overlaps open partition (%c)\n",
1270 lunit, part + 'a',
1271 pp - du->dk_dd.d_partitions + 'a');
1272 }
1273 }
1274 if (part >= du->dk_dd.d_npartitions && part != RAW_PART)
1275 return (ENXIO);
1276
1277 dsopen(dev, fmt, du->dk_slices);
1278
1279 return (0);
1280 #endif
1281 }
1282
1283 /*
1284 * Implement operations other than read/write.
1285 * Called from wdstart or wdintr during opens.
1286 * Uses finite-state-machine to track progress of operation in progress.
1287 * Returns 0 if operation still in progress, 1 if completed, 2 if error.
1288 */
1289 static int
1290 wdcontrol(register struct buf *bp)
1291 {
1292 register struct disk *du;
1293 int ctrlr;
1294
1295 du = wddrives[dkunit(bp->b_dev)];
1296 ctrlr = du->dk_ctrlr_cmd640;
1297
1298 switch (du->dk_state) {
1299 case WANTOPEN:
1300 tryagainrecal:
1301 wdtab[ctrlr].b_active = 1;
1302 if (wdcommand(du, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0) {
1303 wderror(bp, du, "wdcontrol: wdcommand failed");
1304 goto maybe_retry;
1305 }
1306 du->dk_state = RECAL;
1307 return (0);
1308 case RECAL:
1309 if (du->dk_status & WDCS_ERR || wdsetctlr(du) != 0) {
1310 wderror(bp, du, "wdcontrol: recal failed");
1311 maybe_retry:
1312 if (du->dk_status & WDCS_ERR)
1313 wdunwedge(du);
1314 du->dk_state = WANTOPEN;
1315 if (++wdtab[ctrlr].b_errcnt < RETRIES)
1316 goto tryagainrecal;
1317 bp->b_error = ENXIO; /* XXX needs translation */
1318 bp->b_flags |= B_ERROR;
1319 return (2);
1320 }
1321 wdtab[ctrlr].b_errcnt = 0;
1322 du->dk_state = OPEN;
1323 /*
1324 * The rest of the initialization can be done by normal
1325 * means.
1326 */
1327 return (1);
1328 }
1329 panic("wdcontrol");
1330 return (2);
1331 }
1332
1333 /*
1334 * Wait uninterruptibly until controller is not busy, then send it a command.
1335 * The wait usually terminates immediately because we waited for the previous
1336 * command to terminate.
1337 */
1338 static int
1339 wdcommand(struct disk *du, u_int cylinder, u_int head, u_int sector,
1340 u_int count, u_int command)
1341 {
1342 u_int wdc;
1343
1344 wdc = du->dk_port;
1345 if (du->cfg_flags & WDOPT_SLEEPHACK) {
1346 /* OK, so the APM bios has put the disk into SLEEP mode,
1347 * how can we tell ? Uhm, we can't. There is no
1348 * standardized way of finding out, and the only way to
1349 * wake it up is to reset it. Bummer.
1350 *
1351 * All the many and varied versions of the IDE/ATA standard
1352 * explicitly tells us not to look at these registers if
1353 * the disk is in SLEEP mode. Well, too bad really, we
1354 * have to find out if it's in sleep mode before we can
1355 * avoid reading the registers.
1356 *
1357 * I have reason to belive that most disks will return
1358 * either 0xff or 0x00 in all but the status register
1359 * when in SLEEP mode, but I have yet to see one return
1360 * 0x00, so we don't check for that yet.
1361 *
1362 * The check for WDCS_BUSY is for the case where the
1363 * bios spins up the disk for us, but doesn't initialize
1364 * it correctly /phk
1365 */
1366 if(inb(wdc + wd_precomp) + inb(wdc + wd_cyl_lo) +
1367 inb(wdc + wd_cyl_hi) + inb(wdc + wd_sdh) +
1368 inb(wdc + wd_sector) + inb(wdc + wd_seccnt) == 6 * 0xff) {
1369 if (bootverbose)
1370 printf("wd(%d,%d): disk aSLEEP\n",
1371 du->dk_ctrlr, du->dk_unit);
1372 wdunwedge(du);
1373 } else if(inb(wdc + wd_status) == WDCS_BUSY) {
1374 if (bootverbose)
1375 printf("wd(%d,%d): disk is BUSY\n",
1376 du->dk_ctrlr, du->dk_unit);
1377 wdunwedge(du);
1378 }
1379 }
1380
1381 if (wdwait(du, 0, TIMEOUT) < 0)
1382 return (1);
1383 if( command == WDCC_FEATURES) {
1384 outb(wdc + wd_sdh, WDSD_IBM | (du->dk_unit << 4) | head);
1385 outb(wdc + wd_features, count);
1386 if ( count == WDFEA_SETXFER )
1387 outb(wdc + wd_seccnt, sector);
1388 } else {
1389 outb(wdc + wd_precomp, du->dk_dd.d_precompcyl / 4);
1390 outb(wdc + wd_cyl_lo, cylinder);
1391 outb(wdc + wd_cyl_hi, cylinder >> 8);
1392 outb(wdc + wd_sdh, WDSD_IBM | (du->dk_unit << 4) | head);
1393 if (head & WDSD_LBA)
1394 outb(wdc + wd_sector, sector);
1395 else
1396 outb(wdc + wd_sector, sector + 1);
1397 outb(wdc + wd_seccnt, count);
1398 }
1399 if (wdwait(du, (command == WDCC_DIAGNOSE || command == WDCC_IDC)
1400 ? 0 : WDCS_READY, TIMEOUT) < 0)
1401 return (1);
1402 outb(wdc + wd_command, command);
1403 return (0);
1404 }
1405
1406 static void
1407 wdsetmulti(struct disk *du)
1408 {
1409 /*
1410 * The config option flags low 8 bits define the maximum multi-block
1411 * transfer size. If the user wants the maximum that the drive
1412 * is capable of, just set the low bits of the config option to
1413 * 0x00ff.
1414 */
1415 if ((du->cfg_flags & WDOPT_MULTIMASK) != 0 && (du->dk_multi > 1)) {
1416 int configval = du->cfg_flags & WDOPT_MULTIMASK;
1417 du->dk_multi = min(du->dk_multi, configval);
1418 if (wdcommand(du, 0, 0, 0, du->dk_multi, WDCC_SET_MULTI)) {
1419 du->dk_multi = 1;
1420 } else {
1421 if (wdwait(du, WDCS_READY, TIMEOUT) < 0) {
1422 du->dk_multi = 1;
1423 }
1424 }
1425 } else {
1426 du->dk_multi = 1;
1427 }
1428 }
1429
1430 /*
1431 * issue IDC to drive to tell it just what geometry it is to be.
1432 */
1433 static int
1434 wdsetctlr(struct disk *du)
1435 {
1436 int error = 0;
1437 #ifdef WDDEBUG
1438 printf("wd(%d,%d): wdsetctlr: C %lu H %lu S %lu\n",
1439 du->dk_ctrlr, du->dk_unit,
1440 du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks,
1441 du->dk_dd.d_nsectors);
1442 #endif
1443 if (!(du->dk_flags & DKFL_LBA)) {
1444 if (du->dk_dd.d_ntracks == 0 || du->dk_dd.d_ntracks > 16) {
1445 struct wdparams *wp;
1446
1447 printf("wd%d: can't handle %lu heads from partition table ",
1448 du->dk_lunit, du->dk_dd.d_ntracks);
1449 /* obtain parameters */
1450 wp = &du->dk_params;
1451 if (wp->wdp_heads > 0 && wp->wdp_heads <= 16) {
1452 printf("(controller value %u restored)\n",
1453 wp->wdp_heads);
1454 du->dk_dd.d_ntracks = wp->wdp_heads;
1455 }
1456 else {
1457 printf("(truncating to 16)\n");
1458 du->dk_dd.d_ntracks = 16;
1459 }
1460 }
1461
1462 if (du->dk_dd.d_nsectors == 0 || du->dk_dd.d_nsectors > 255) {
1463 printf("wd%d: cannot handle %lu sectors (max 255)\n",
1464 du->dk_lunit, du->dk_dd.d_nsectors);
1465 error = 1;
1466 }
1467 if (error) {
1468 wdtab[du->dk_ctrlr_cmd640].b_errcnt += RETRIES;
1469 return (1);
1470 }
1471 if (wdcommand(du, du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks - 1, 0,
1472 du->dk_dd.d_nsectors, WDCC_IDC) != 0
1473 || wdwait(du, WDCS_READY, TIMEOUT) < 0) {
1474 wderror((struct buf *)NULL, du, "wdsetctlr failed");
1475 return (1);
1476 }
1477 }
1478
1479 wdsetmulti(du);
1480
1481 #ifdef NOTYET
1482 /* set read caching and write caching */
1483 wdcommand(du, 0, 0, 0, WDFEA_RCACHE, WDCC_FEATURES);
1484 wdwait(du, WDCS_READY, TIMEOUT);
1485
1486 wdcommand(du, 0, 0, 0, WDFEA_WCACHE, WDCC_FEATURES);
1487 wdwait(du, WDCS_READY, TIMEOUT);
1488 #endif
1489
1490 return (0);
1491 }
1492
1493 #if 0
1494 /*
1495 * Wait until driver is inactive, then set up controller.
1496 */
1497 static int
1498 wdwsetctlr(struct disk *du)
1499 {
1500 int stat;
1501 int x;
1502
1503 wdsleep(du->dk_ctrlr, "wdwset");
1504 x = splbio();
1505 stat = wdsetctlr(du);
1506 wdflushirq(du, x);
1507 splx(x);
1508 return (stat);
1509 }
1510 #endif
1511
1512 /*
1513 * gross little callback function for wdddma interface. returns 1 for
1514 * success, 0 for failure.
1515 */
1516 static int
1517 wdsetmode(int mode, void *wdinfo)
1518 {
1519 int i;
1520 struct disk *du;
1521
1522 du = wdinfo;
1523 if (bootverbose)
1524 printf("wd%d: wdsetmode() setting transfer mode to %02x\n",
1525 du->dk_lunit, mode);
1526 i = wdcommand(du, 0, 0, mode, WDFEA_SETXFER,
1527 WDCC_FEATURES) == 0 &&
1528 wdwait(du, WDCS_READY, TIMEOUT) == 0;
1529 return i;
1530 }
1531
1532 /*
1533 * issue READP to drive to ask it what it is.
1534 */
1535 static int
1536 wdgetctlr(struct disk *du)
1537 {
1538 int i;
1539 char tb[DEV_BSIZE], tb2[DEV_BSIZE];
1540 struct wdparams *wp = NULL;
1541 u_long flags = du->cfg_flags;
1542 again:
1543 if (wdcommand(du, 0, 0, 0, 0, WDCC_READP) != 0
1544 || wdwait(du, WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ, TIMEOUT) != 0) {
1545
1546 /*
1547 * if we failed on the second try, assume non-32bit
1548 */
1549 if( du->dk_flags & DKFL_32BIT)
1550 goto failed;
1551
1552 /* XXX need to check error status after final transfer. */
1553 /*
1554 * Old drives don't support WDCC_READP. Try a seek to 0.
1555 * Some IDE controllers return trash if there is no drive
1556 * attached, so first test that the drive can be selected.
1557 * This also avoids long waits for nonexistent drives.
1558 */
1559 if (wdwait(du, 0, TIMEOUT) < 0)
1560 return (1);
1561 outb(du->dk_port + wd_sdh, WDSD_IBM | (du->dk_unit << 4));
1562 DELAY(5000); /* usually unnecessary; drive select is fast */
1563 /*
1564 * Do this twice: may get a false WDCS_READY the first time.
1565 */
1566 inb(du->dk_port + wd_status);
1567 if ((inb(du->dk_port + wd_status) & (WDCS_BUSY | WDCS_READY))
1568 != WDCS_READY
1569 || wdcommand(du, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0
1570 || wdwait(du, WDCS_READY | WDCS_SEEKCMPLT, TIMEOUT) != 0)
1571 return (1);
1572
1573 if (du->dk_unit == bootinfo.bi_n_bios_used) {
1574 du->dk_dd.d_secsize = DEV_BSIZE;
1575 du->dk_dd.d_nsectors =
1576 bootinfo.bi_bios_geom[du->dk_unit] & 0xff;
1577 du->dk_dd.d_ntracks =
1578 ((bootinfo.bi_bios_geom[du->dk_unit] >> 8) & 0xff)
1579 + 1;
1580 /* XXX Why 2 ? */
1581 du->dk_dd.d_ncylinders =
1582 (bootinfo.bi_bios_geom[du->dk_unit] >> 16) + 2;
1583 du->dk_dd.d_secpercyl =
1584 du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
1585 du->dk_dd.d_secperunit =
1586 du->dk_dd.d_secpercyl * du->dk_dd.d_ncylinders;
1587 #if 0
1588 du->dk_dd.d_partitions[WDRAW].p_size =
1589 du->dk_dd.d_secperunit;
1590 du->dk_dd.d_type = DTYPE_ST506;
1591 du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
1592 strncpy(du->dk_dd.d_typename, "Bios geometry",
1593 sizeof du->dk_dd.d_typename);
1594 strncpy(du->dk_params.wdp_model, "ST506",
1595 sizeof du->dk_params.wdp_model);
1596 #endif
1597 bootinfo.bi_n_bios_used ++;
1598 return 0;
1599 }
1600 /*
1601 * Fake minimal drive geometry for reading the MBR.
1602 * readdisklabel() may enlarge it to read the label.
1603 */
1604 du->dk_dd.d_secsize = DEV_BSIZE;
1605 du->dk_dd.d_nsectors = 17;
1606 du->dk_dd.d_ntracks = 1;
1607 du->dk_dd.d_ncylinders = 1;
1608 du->dk_dd.d_secpercyl = 17;
1609 du->dk_dd.d_secperunit = 17;
1610
1611 #if 0
1612 /*
1613 * Fake maximal drive size for writing the label.
1614 */
1615 du->dk_dd.d_partitions[RAW_PART].p_size = 64 * 16 * 1024;
1616
1617 /*
1618 * Fake some more of the label for printing by disklabel(1)
1619 * in case there is no real label.
1620 */
1621 du->dk_dd.d_type = DTYPE_ST506;
1622 du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
1623 strncpy(du->dk_dd.d_typename, "Fake geometry",
1624 sizeof du->dk_dd.d_typename);
1625 #endif
1626
1627 /* Fake the model name for printing by wdattach(). */
1628 strncpy(du->dk_params.wdp_model, "unknown",
1629 sizeof du->dk_params.wdp_model);
1630
1631 return (0);
1632 }
1633
1634 /* obtain parameters */
1635 wp = &du->dk_params;
1636 if (du->dk_flags & DKFL_32BIT)
1637 insl(du->dk_port + wd_data, tb, sizeof(tb) / sizeof(long));
1638 else
1639 insw(du->dk_port + wd_data, tb, sizeof(tb) / sizeof(short));
1640
1641 /* try 32-bit data path (VLB IDE controller) */
1642 if (flags & WDOPT_32BIT) {
1643 if (! (du->dk_flags & DKFL_32BIT)) {
1644 bcopy(tb, tb2, sizeof(struct wdparams));
1645 du->dk_flags |= DKFL_32BIT;
1646 goto again;
1647 }
1648
1649 /* check that we really have 32-bit controller */
1650 if (bcmp (tb, tb2, sizeof(struct wdparams)) != 0) {
1651 failed:
1652 /* test failed, use 16-bit i/o mode */
1653 bcopy(tb2, tb, sizeof(struct wdparams));
1654 du->dk_flags &= ~DKFL_32BIT;
1655 }
1656 }
1657
1658 bcopy(tb, wp, sizeof(struct wdparams));
1659
1660 /* shuffle string byte order */
1661 for (i = 0; (unsigned)i < sizeof(wp->wdp_model); i += 2) {
1662 u_short *p;
1663
1664 p = (u_short *) (wp->wdp_model + i);
1665 *p = ntohs(*p);
1666 }
1667 /*
1668 * Clean up the wdp_model by converting nulls to spaces, and
1669 * then removing the trailing spaces.
1670 */
1671 for (i = 0; (unsigned)i < sizeof(wp->wdp_model); i++) {
1672 if (wp->wdp_model[i] == '\0') {
1673 wp->wdp_model[i] = ' ';
1674 }
1675 }
1676 for (i = sizeof(wp->wdp_model) - 1;
1677 (i >= 0 && wp->wdp_model[i] == ' '); i--) {
1678 wp->wdp_model[i] = '\0';
1679 }
1680
1681 /*
1682 * find out the drives maximum multi-block transfer capability
1683 */
1684 du->dk_multi = wp->wdp_nsecperint & 0xff;
1685 wdsetmulti(du);
1686
1687 /*
1688 * check drive's DMA capability
1689 */
1690 if (wddma[du->dk_interface].wdd_candma) {
1691 du->dk_dmacookie = wddma[du->dk_interface].wdd_candma(
1692 du->dk_port, du->dk_ctrlr, du->dk_unit);
1693 /* does user want this? */
1694 if ((du->cfg_flags & WDOPT_DMA) &&
1695 /* have we got a DMA controller? */
1696 du->dk_dmacookie &&
1697 /* can said drive do DMA? */
1698 wddma[du->dk_interface].wdd_dmainit(du->dk_dmacookie, wp, wdsetmode, du)) {
1699 du->dk_flags |= DKFL_USEDMA;
1700 }
1701 } else {
1702 du->dk_dmacookie = NULL;
1703 }
1704
1705 #ifdef WDDEBUG
1706 printf(
1707 "\nwd(%d,%d): wdgetctlr: gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
1708 du->dk_ctrlr, du->dk_unit, wp->wdp_config, wp->wdp_cylinders,
1709 wp->wdp_heads, wp->wdp_sectors, wp->wdp_buffertype,
1710 wp->wdp_buffersize, wp->wdp_model);
1711 #endif
1712
1713 /* update disklabel given drive information */
1714 du->dk_dd.d_secsize = DEV_BSIZE;
1715 if ((du->cfg_flags & WDOPT_LBA) && wp->wdp_lbasize) {
1716 du->dk_dd.d_nsectors = 63;
1717 if (wp->wdp_lbasize < 16*63*1024) { /* <=528.4 MB */
1718 du->dk_dd.d_ntracks = 16;
1719 }
1720 else if (wp->wdp_lbasize < 32*63*1024) { /* <=1.057 GB */
1721 du->dk_dd.d_ntracks = 32;
1722 }
1723 else if (wp->wdp_lbasize < 64*63*1024) { /* <=2.114 GB */
1724 du->dk_dd.d_ntracks = 64;
1725 }
1726 else if (wp->wdp_lbasize < 128*63*1024) { /* <=4.228 GB */
1727 du->dk_dd.d_ntracks = 128;
1728 }
1729 else if (wp->wdp_lbasize < 255*63*1024) { /* <=8.422 GB */
1730 du->dk_dd.d_ntracks = 255;
1731 }
1732 else { /* >8.422 GB */
1733 du->dk_dd.d_ntracks = 255; /* XXX */
1734 }
1735 du->dk_dd.d_secpercyl= du->dk_dd.d_ntracks*du->dk_dd.d_nsectors;
1736 du->dk_dd.d_ncylinders = wp->wdp_lbasize/du->dk_dd.d_secpercyl;
1737 du->dk_dd.d_secperunit = wp->wdp_lbasize;
1738 du->dk_flags |= DKFL_LBA;
1739 }
1740 else {
1741 du->dk_dd.d_ncylinders = wp->wdp_cylinders; /* +- 1 */
1742 du->dk_dd.d_ntracks = wp->wdp_heads;
1743 du->dk_dd.d_nsectors = wp->wdp_sectors;
1744 du->dk_dd.d_secpercyl =
1745 du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
1746 du->dk_dd.d_secperunit =
1747 du->dk_dd.d_secpercyl * du->dk_dd.d_ncylinders;
1748 if (wp->wdp_cylinders == 16383 &&
1749 du->dk_dd.d_secperunit < wp->wdp_lbasize) {
1750 du->dk_dd.d_secperunit = wp->wdp_lbasize;
1751 du->dk_dd.d_ncylinders =
1752 du->dk_dd.d_secperunit / du->dk_dd.d_secpercyl;
1753 }
1754 }
1755 if (WDOPT_FORCEHD(du->cfg_flags)) {
1756 du->dk_dd.d_ntracks = WDOPT_FORCEHD(du->cfg_flags);
1757 du->dk_dd.d_secpercyl =
1758 du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
1759 du->dk_dd.d_ncylinders =
1760 du->dk_dd.d_secperunit / du->dk_dd.d_secpercyl;
1761 }
1762 if (du->dk_dd.d_ncylinders > 0x10000 && !(du->cfg_flags & WDOPT_LBA)) {
1763 du->dk_dd.d_ncylinders = 0x10000;
1764 du->dk_dd.d_secperunit = du->dk_dd.d_secpercyl *
1765 du->dk_dd.d_ncylinders;
1766 printf(
1767 "wd%d: cannot handle %d total sectors; truncating to %lu\n",
1768 du->dk_lunit, wp->wdp_lbasize, du->dk_dd.d_secperunit);
1769 }
1770 #if 0
1771 du->dk_dd.d_partitions[RAW_PART].p_size = du->dk_dd.d_secperunit;
1772 /* dubious ... */
1773 bcopy("ESDI/IDE", du->dk_dd.d_typename, 9);
1774 bcopy(wp->wdp_model + 20, du->dk_dd.d_packname, 14 - 1);
1775 /* better ... */
1776 du->dk_dd.d_type = DTYPE_ESDI;
1777 du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
1778 #endif
1779
1780 return (0);
1781 }
1782
1783 int
1784 wdclose(dev_t dev, int flags, int fmt, struct proc *p)
1785 {
1786 dsclose(dev, fmt, wddrives[dkunit(dev)]->dk_slices);
1787 return (0);
1788 }
1789
1790 int
1791 wdioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
1792 {
1793 int lunit = dkunit(dev);
1794 register struct disk *du;
1795 int error;
1796
1797 du = wddrives[lunit];
1798 wdsleep(du->dk_ctrlr, "wdioct");
1799 error = dsioctl(dev, cmd, addr, flags, &du->dk_slices);
1800 if (error != ENOIOCTL)
1801 return (error);
1802 return (ENOTTY);
1803 }
1804
1805 int
1806 wdsize(dev_t dev)
1807 {
1808 struct disk *du;
1809 int lunit;
1810
1811 lunit = dkunit(dev);
1812 if (lunit >= NWD || dktype(dev) != 0)
1813 return (-1);
1814 du = wddrives[lunit];
1815 if (du == NULL)
1816 return (-1);
1817 return (dssize(dev, &du->dk_slices));
1818 }
1819
1820 int
1821 wddump(dev_t dev)
1822 {
1823 register struct disk *du;
1824 struct disklabel *lp;
1825 long num; /* number of sectors to write */
1826 int lunit, part;
1827 long blkoff, blknum;
1828 long blkcnt, blknext;
1829 u_long ds_offset;
1830 u_long nblocks;
1831 static int wddoingadump = 0;
1832 long cylin, head, sector;
1833 long secpertrk, secpercyl;
1834 vm_paddr_t addr;
1835
1836 /* Toss any characters present prior to dump. */
1837 while (cncheckc() != -1)
1838 ;
1839
1840 /* Check for acceptable device. */
1841 /* XXX should reset to maybe allow du->dk_state < OPEN. */
1842 lunit = dkunit(dev); /* eventually support floppies? */
1843 part = dkpart(dev);
1844 if (lunit >= NWD || (du = wddrives[lunit]) == NULL
1845 || du->dk_state < OPEN
1846 || (lp = dsgetlabel(dev, du->dk_slices)) == NULL)
1847 return (ENXIO);
1848
1849 /* Size of memory to dump, in disk sectors. */
1850 num = (u_long)Maxmem * PAGE_SIZE / du->dk_dd.d_secsize;
1851
1852
1853 secpertrk = du->dk_dd.d_nsectors;
1854 secpercyl = du->dk_dd.d_secpercyl;
1855 nblocks = lp->d_partitions[part].p_size;
1856 blkoff = lp->d_partitions[part].p_offset;
1857 /* XXX */
1858 ds_offset = du->dk_slices->dss_slices[dkslice(dev)].ds_offset;
1859 blkoff += ds_offset;
1860
1861 #if 0
1862 printf("part %d, nblocks %lu, dumplo %ld num %ld\n",
1863 part, nblocks, dumplo, num);
1864 #endif
1865
1866 /* Check transfer bounds against partition size. */
1867 if (dumplo < 0 || dumplo + num > nblocks)
1868 return (EINVAL);
1869
1870 /* Check if we are being called recursively. */
1871 if (wddoingadump)
1872 return (EFAULT);
1873
1874 #if 0
1875 /* Mark controller active for if we panic during the dump. */
1876 wdtab[du->dk_ctrlr].b_active = 1;
1877 #endif
1878 wddoingadump = 1;
1879
1880 /* Recalibrate the drive. */
1881 DELAY(5); /* ATA spec XXX NOT */
1882 if (wdcommand(du, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0
1883 || wdwait(du, WDCS_READY | WDCS_SEEKCMPLT, TIMEOUT) != 0
1884 || wdsetctlr(du) != 0) {
1885 wderror((struct buf *)NULL, du, "wddump: recalibrate failed");
1886 return (EIO);
1887 }
1888
1889 du->dk_flags |= DKFL_SINGLE;
1890 addr = 0;
1891 blknum = dumplo + blkoff;
1892 while (num > 0) {
1893 blkcnt = num;
1894 if (blkcnt > MAXTRANSFER)
1895 blkcnt = MAXTRANSFER;
1896 if ((du->dk_flags & DKFL_LBA) == 0) {
1897 /* XXX keep transfer within current cylinder. */
1898 if ((blknum + blkcnt - 1) / secpercyl !=
1899 blknum / secpercyl)
1900 blkcnt = secpercyl - (blknum % secpercyl);
1901 }
1902 blknext = blknum + blkcnt;
1903
1904 /* Compute disk address. */
1905 if (du->dk_flags & DKFL_LBA) {
1906 sector = (blknum >> 0) & 0xff;
1907 cylin = (blknum >> 8) & 0xffff;
1908 head = ((blknum >> 24) & 0xf) | WDSD_LBA;
1909 } else {
1910 cylin = blknum / secpercyl;
1911 head = (blknum % secpercyl) / secpertrk;
1912 sector = blknum % secpertrk;
1913 }
1914
1915 #if 0
1916 /* Let's just talk about this first... */
1917 printf("cylin %ld head %ld sector %ld addr %p count %ld\n",
1918 cylin, head, sector, addr, blkcnt);
1919 cngetc();
1920 #endif
1921
1922 /* Do the write. */
1923 if (wdcommand(du, cylin, head, sector, blkcnt, WDCC_WRITE)
1924 != 0) {
1925 wderror((struct buf *)NULL, du,
1926 "wddump: timeout waiting to to give command");
1927 return (EIO);
1928 }
1929 while (blkcnt != 0) {
1930 caddr_t va;
1931
1932 if (is_physical_memory(addr))
1933 va = pmap_kenter_temporary(trunc_page(addr), 0);
1934 else
1935 va = pmap_kenter_temporary(trunc_page(0), 0);
1936
1937 /* Ready to send data? */
1938 DELAY(5); /* ATA spec */
1939 if (wdwait(du, WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ, TIMEOUT)
1940 < 0) {
1941 wderror((struct buf *)NULL, du,
1942 "wddump: timeout waiting for DRQ");
1943 return (EIO);
1944 }
1945 if (du->dk_flags & DKFL_32BIT)
1946 outsl(du->dk_port + wd_data,
1947 va + ((int)addr & PAGE_MASK),
1948 DEV_BSIZE / sizeof(long));
1949 else
1950 outsw(du->dk_port + wd_data,
1951 va + ((int)addr & PAGE_MASK),
1952 DEV_BSIZE / sizeof(short));
1953 addr += DEV_BSIZE;
1954 /*
1955 * If we are dumping core, it may take a while.
1956 * So reassure the user and hold off any watchdogs.
1957 */
1958 if ((unsigned)addr % (1024 * 1024) == 0) {
1959 #ifdef HW_WDOG
1960 if (wdog_tickler)
1961 (*wdog_tickler)();
1962 #endif /* HW_WDOG */
1963 printf("%ld ", num / (1024 * 1024 / DEV_BSIZE));
1964 }
1965 num--;
1966 blkcnt--;
1967 }
1968
1969 /* Wait for completion. */
1970 DELAY(5); /* ATA spec XXX NOT */
1971 if (wdwait(du, WDCS_READY | WDCS_SEEKCMPLT, TIMEOUT) < 0) {
1972 wderror((struct buf *)NULL, du,
1973 "wddump: timeout waiting for status");
1974 return (EIO);
1975 }
1976
1977 /* Check final status. */
1978 if ((du->dk_status
1979 & (WDCS_READY | WDCS_SEEKCMPLT | WDCS_DRQ | WDCS_ERR))
1980 != (WDCS_READY | WDCS_SEEKCMPLT)) {
1981 wderror((struct buf *)NULL, du,
1982 "wddump: extra DRQ, or error");
1983 return (EIO);
1984 }
1985
1986 /* Update block count. */
1987 blknum = blknext;
1988
1989 /* Operator aborting dump? */
1990 if (cncheckc() != -1)
1991 return (EINTR);
1992 }
1993 return (0);
1994 }
1995
1996 static void
1997 wderror(struct buf *bp, struct disk *du, char *mesg)
1998 {
1999 if (bp == NULL)
2000 printf("wd%d: %s", du->dk_lunit, mesg);
2001 else
2002 diskerr(bp, mesg, LOG_PRINTF, du->dk_skip,
2003 dsgetlabel(bp->b_dev, du->dk_slices));
2004 printf(" (status %b error %b)\n",
2005 du->dk_status, WDCS_BITS, du->dk_error, WDERR_BITS);
2006 }
2007
2008 /*
2009 * Discard any interrupts that were latched by the interrupt system while
2010 * we were doing polled i/o.
2011 */
2012 static void
2013 wdflushirq(struct disk *du, int old_ipl)
2014 {
2015 wdtab[du->dk_ctrlr_cmd640].b_active = 2;
2016 splx(old_ipl);
2017 (void)splbio();
2018 wdtab[du->dk_ctrlr_cmd640].b_active = 0;
2019 }
2020
2021 /*
2022 * Reset the controller.
2023 */
2024 static int
2025 wdreset(struct disk *du)
2026 {
2027 int err = 0;
2028
2029 if ((du->dk_flags & (DKFL_DMA|DKFL_SINGLE)) == DKFL_DMA)
2030 wddma[du->dk_interface].wdd_dmadone(du->dk_dmacookie);
2031 (void)wdwait(du, 0, TIMEOUT);
2032 outb(du->dk_altport, WDCTL_IDS | WDCTL_RST);
2033 DELAY(10 * 1000);
2034 outb(du->dk_altport, WDCTL_IDS);
2035 outb(du->dk_port + wd_sdh, WDSD_IBM | (du->dk_unit << 4));
2036 if (wdwait(du, 0, TIMEOUT) != 0)
2037 err = 1; /* no IDE drive found */
2038 du->dk_error = inb(du->dk_port + wd_error);
2039 if (du->dk_error != 0x01)
2040 err = 1; /* the drive is incompatible */
2041 outb(du->dk_altport, WDCTL_4BIT);
2042 return (err);
2043 }
2044
2045 /*
2046 * Sleep until driver is inactive.
2047 * This is used only for avoiding rare race conditions, so it is unimportant
2048 * that the sleep may be far too short or too long.
2049 */
2050 static void
2051 wdsleep(int ctrlr, char *wmesg)
2052 {
2053 int s = splbio();
2054 if (eide_quirks & Q_CMD640B)
2055 ctrlr = PRIMARY;
2056 while (wdtab[ctrlr].b_active)
2057 tsleep((caddr_t)&wdtab[ctrlr].b_active, PZERO - 1, wmesg, 1);
2058 splx(s);
2059 }
2060
2061 static void
2062 wdtimeout(void *cdu)
2063 {
2064 struct disk *du;
2065 int x;
2066 static int timeouts;
2067
2068 du = (struct disk *)cdu;
2069 x = splbio();
2070 if (du->dk_timeout != 0 && --du->dk_timeout == 0) {
2071 if(timeouts++ <= 5) {
2072 char *msg;
2073
2074 msg = (timeouts > 5) ?
2075 "Last time I say: interrupt timeout. Probably a portable PC." :
2076 "interrupt timeout";
2077 wderror((struct buf *)NULL, du, msg);
2078 if (du->dk_dmacookie)
2079 printf("wd%d: wdtimeout() DMA status %b\n",
2080 du->dk_lunit,
2081 wddma[du->dk_interface].wdd_dmastatus(du->dk_dmacookie),
2082 WDDS_BITS);
2083 }
2084 wdunwedge(du);
2085 wdflushirq(du, x);
2086 du->dk_skip = 0;
2087 du->dk_flags |= DKFL_SINGLE;
2088 wdstart(du->dk_ctrlr);
2089 }
2090 timeout(wdtimeout, cdu, hz);
2091 splx(x);
2092 }
2093
2094 /*
2095 * Reset the controller after it has become wedged. This is different from
2096 * wdreset() so that wdreset() can be used in the probe and so that this
2097 * can restore the geometry .
2098 */
2099 static int
2100 wdunwedge(struct disk *du)
2101 {
2102 struct disk *du1;
2103 int lunit;
2104
2105 /* Schedule other drives for recalibration. */
2106 for (lunit = 0; lunit < NWD; lunit++)
2107 if ((du1 = wddrives[lunit]) != NULL && du1 != du
2108 && du1->dk_ctrlr == du->dk_ctrlr
2109 && du1->dk_state > WANTOPEN)
2110 du1->dk_state = WANTOPEN;
2111
2112 DELAY(RECOVERYTIME);
2113 if (wdreset(du) == 0) {
2114 /*
2115 * XXX - recalibrate current drive now because some callers
2116 * aren't prepared to have its state change.
2117 */
2118 if (wdcommand(du, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) == 0
2119 && wdwait(du, WDCS_READY | WDCS_SEEKCMPLT, TIMEOUT) == 0
2120 && wdsetctlr(du) == 0)
2121 return (0);
2122 }
2123 wderror((struct buf *)NULL, du, "wdunwedge failed");
2124 return (1);
2125 }
2126
2127 /*
2128 * Wait uninterruptibly until controller is not busy and either certain
2129 * status bits are set or an error has occurred.
2130 * The wait is usually short unless it is for the controller to process
2131 * an entire critical command.
2132 * Return 1 for (possibly stale) controller errors, -1 for timeout errors,
2133 * or 0 for no errors.
2134 * Return controller status in du->dk_status and, if there was a controller
2135 * error, return the error code in du->dk_error.
2136 */
2137 #ifdef WD_COUNT_RETRIES
2138 static int min_retries[NWDC];
2139 #endif
2140
2141 static int
2142 wdwait(struct disk *du, u_char bits_wanted, int timeout)
2143 {
2144 int wdc;
2145 u_char status;
2146
2147 #define POLLING 1000
2148
2149 wdc = du->dk_port;
2150 timeout += POLLING;
2151
2152 /*
2153 * This delay is really too long, but does not impact the performance
2154 * as much when using the multi-sector option. Shorter delays have
2155 * caused I/O errors on some drives and system configs. This should
2156 * probably be fixed if we develop a better short term delay mechanism.
2157 */
2158 DELAY(1);
2159
2160 do {
2161 #ifdef WD_COUNT_RETRIES
2162 if (min_retries[du->dk_ctrlr] > timeout
2163 || min_retries[du->dk_ctrlr] == 0)
2164 min_retries[du->dk_ctrlr] = timeout;
2165 #endif
2166 du->dk_status = status = inb(wdc + wd_status);
2167 /*
2168 * Atapi drives have a very interesting feature, when attached
2169 * as a slave on the IDE bus, and there is no master.
2170 * They release the bus after getting the command.
2171 * We should reselect the drive here to get the status.
2172 */
2173 if (status == 0xff) {
2174 outb(wdc + wd_sdh, WDSD_IBM | du->dk_unit << 4);
2175 du->dk_status = status = inb(wdc + wd_status);
2176 }
2177 if (!(status & WDCS_BUSY)) {
2178 if (status & WDCS_ERR) {
2179 du->dk_error = inb(wdc + wd_error);
2180 /*
2181 * We once returned here. This is wrong
2182 * because the error bit is apparently only
2183 * valid after the controller has interrupted
2184 * (e.g., the error bit is stale when we wait
2185 * for DRQ for writes). So we can't depend
2186 * on the error bit at all when polling for
2187 * command completion.
2188 */
2189 }
2190 if ((status & bits_wanted) == bits_wanted) {
2191 return (status & WDCS_ERR);
2192 }
2193 }
2194 if (timeout < TIMEOUT)
2195 /*
2196 * Switch to a polling rate of about 1 KHz so that
2197 * the timeout is almost machine-independent. The
2198 * controller is taking a long time to respond, so
2199 * an extra msec won't matter.
2200 */
2201 DELAY(1000);
2202 else
2203 DELAY(1);
2204 } while (--timeout != 0);
2205 return (-1);
2206 }
2207
2208 #endif /* NWDC > 0 */
Cache object: fc4f765fc2812be515e11da19f2e5c7f
|