1752d4735SNate Lawson /*-
27282444bSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
37282444bSPedro F. Giffuni *
41b67be7bSPoul-Henning Kamp * Copyright (c) 2004 Poul-Henning Kamp
55b81b6b3SRodney W. Grimes * Copyright (c) 1990 The Regents of the University of California.
65b81b6b3SRodney W. Grimes * All rights reserved.
75b81b6b3SRodney W. Grimes *
85b81b6b3SRodney W. Grimes * This code is derived from software contributed to Berkeley by
95b81b6b3SRodney W. Grimes * Don Ahn.
105b81b6b3SRodney W. Grimes *
1169acd21dSWarner Losh * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
1269acd21dSWarner Losh * aided by the Linux floppy driver modifications from David Bateman
1369acd21dSWarner Losh * (dbateman@eng.uts.edu.au).
1469acd21dSWarner Losh *
15dc16046fSJoerg Wunsch * Copyright (c) 1993, 1994 by
163a2f7427SDavid Greenman * jc@irbs.UUCP (John Capo)
173a2f7427SDavid Greenman * vak@zebub.msk.su (Serge Vakulenko)
183a2f7427SDavid Greenman * ache@astral.msk.su (Andrew A. Chernov)
19dc16046fSJoerg Wunsch *
20dc16046fSJoerg Wunsch * Copyright (c) 1993, 1994, 1995 by
213a2f7427SDavid Greenman * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
22dc5df763SJoerg Wunsch * dufault@hda.com (Peter Dufault)
233a2f7427SDavid Greenman *
242995d110SJoerg Wunsch * Copyright (c) 2001 Joerg Wunsch,
2506740f7bSJoerg Wunsch * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
262995d110SJoerg Wunsch *
275b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
285b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions
295b81b6b3SRodney W. Grimes * are met:
305b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
315b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
325b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
335b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
345b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution.
35fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
3664860614SJoerg Wunsch * may be used to endorse or promote products derived from this software
3764860614SJoerg Wunsch * without specific prior written permission.
385b81b6b3SRodney W. Grimes *
395b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
405b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
415b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
425b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
435b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
445b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
455b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
465b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
475b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
485b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
495b81b6b3SRodney W. Grimes * SUCH DAMAGE.
505b81b6b3SRodney W. Grimes *
515b81b6b3SRodney W. Grimes */
525b81b6b3SRodney W. Grimes
538c9bbf48SDavid E. O'Brien #include <sys/cdefs.h>
54d2fb4892SJoerg Wunsch #include "opt_fdc.h"
555b81b6b3SRodney W. Grimes
56b99f0a4aSAndrew Moore #include <sys/param.h>
579626b608SPoul-Henning Kamp #include <sys/bio.h>
586182fdbdSPeter Wemm #include <sys/bus.h>
59b2dfb1f9SJustin T. Gibbs #include <sys/devicestat.h>
60f90c382cSPoul-Henning Kamp #include <sys/disk.h>
616182fdbdSPeter Wemm #include <sys/fcntl.h>
62e774b251SJoerg Wunsch #include <sys/fdcio.h>
631a6bed68SJoerg Wunsch #include <sys/filio.h>
64fb919e4dSMark Murray #include <sys/kernel.h>
651b67be7bSPoul-Henning Kamp #include <sys/kthread.h>
66fb919e4dSMark Murray #include <sys/lock.h>
67b99f0a4aSAndrew Moore #include <sys/malloc.h>
686182fdbdSPeter Wemm #include <sys/module.h>
69fb919e4dSMark Murray #include <sys/mutex.h>
70acd3428bSRobert Watson #include <sys/priv.h>
713a2f7427SDavid Greenman #include <sys/proc.h>
726182fdbdSPeter Wemm #include <sys/rman.h>
731b67be7bSPoul-Henning Kamp #include <sys/sysctl.h>
746d6fa4fdSWarner Losh #include <sys/systm.h>
756182fdbdSPeter Wemm
761b67be7bSPoul-Henning Kamp #include <geom/geom.h>
771b67be7bSPoul-Henning Kamp
78c3ae4c40SWarner Losh #include <machine/bus.h>
796182fdbdSPeter Wemm #include <machine/clock.h>
80dc5df763SJoerg Wunsch #include <machine/stdarg.h>
816182fdbdSPeter Wemm
826182fdbdSPeter Wemm #include <isa/isavar.h>
83a97c75b7SDoug Rabson #include <isa/isareg.h>
84a97c75b7SDoug Rabson #include <isa/rtc.h>
8583e78a7fSJohn Baldwin #include <dev/fdc/fdcvar.h>
866182fdbdSPeter Wemm
871b67be7bSPoul-Henning Kamp #include <dev/ic/nec765.h>
880722d6abSJoerg Wunsch
893fef646eSJoerg Wunsch /*
901b67be7bSPoul-Henning Kamp * Runtime configuration hints/flags
913fef646eSJoerg Wunsch */
921b67be7bSPoul-Henning Kamp
931b67be7bSPoul-Henning Kamp /* configuration flags for fd */
941b67be7bSPoul-Henning Kamp #define FD_TYPEMASK 0x0f /* drive type, matches enum
951b67be7bSPoul-Henning Kamp * fd_drivetype; on i386 machines, if
961b67be7bSPoul-Henning Kamp * given as 0, use RTC type for fd0
971b67be7bSPoul-Henning Kamp * and fd1 */
98c884498fSJung-uk Kim #define FD_NO_CHLINE 0x10 /* drive does not support changeline
99c884498fSJung-uk Kim * aka. unit attention */
1001b67be7bSPoul-Henning Kamp #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
1011b67be7bSPoul-Henning Kamp * assume it is there */
1021b67be7bSPoul-Henning Kamp
1031b67be7bSPoul-Henning Kamp /*
1041b67be7bSPoul-Henning Kamp * Things that could conceiveably considered parameters or tweakables
1051b67be7bSPoul-Henning Kamp */
1061b67be7bSPoul-Henning Kamp
1071b67be7bSPoul-Henning Kamp /*
1081b67be7bSPoul-Henning Kamp * Maximal number of bytes in a cylinder.
1091b67be7bSPoul-Henning Kamp * This is used for ISADMA bouncebuffer allocation and sets the max
1101b67be7bSPoul-Henning Kamp * xfersize we support.
1111b67be7bSPoul-Henning Kamp *
11261909e4cSPoul-Henning Kamp * 2.88M format has 2 x 36 x 512, allow for hacked up density.
1131b67be7bSPoul-Henning Kamp */
1141b67be7bSPoul-Henning Kamp #define MAX_BYTES_PER_CYL (2 * 40 * 512)
115dc5df763SJoerg Wunsch
1161a6bed68SJoerg Wunsch /*
117cb38bb6cSJoerg Wunsch * Timeout value for the PIO loops to wait until the FDC main status
118cb38bb6cSJoerg Wunsch * register matches our expectations (request for master, direction
119cb38bb6cSJoerg Wunsch * bit). This is supposed to be a number of microseconds, although
120cb38bb6cSJoerg Wunsch * timing might actually not be very accurate.
121cb38bb6cSJoerg Wunsch *
122cb38bb6cSJoerg Wunsch * Timeouts of 100 msec are believed to be required for some broken
123cb38bb6cSJoerg Wunsch * (old) hardware.
124cb38bb6cSJoerg Wunsch */
125cb38bb6cSJoerg Wunsch #define FDSTS_TIMEOUT 100000
126cb38bb6cSJoerg Wunsch
127cb38bb6cSJoerg Wunsch /*
1281b67be7bSPoul-Henning Kamp * After this many errors, stop whining. Close will reset this count.
1291a6bed68SJoerg Wunsch */
13061909e4cSPoul-Henning Kamp #define FDC_ERRMAX 100
1317ca0641bSAndrey A. Chernov
1321a6bed68SJoerg Wunsch /*
1331b67be7bSPoul-Henning Kamp * AutoDensity search lists for each drive type.
1341a6bed68SJoerg Wunsch */
1351b67be7bSPoul-Henning Kamp
1361b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_360k[] = {
1371b67be7bSPoul-Henning Kamp { FDF_5_360 },
1381b67be7bSPoul-Henning Kamp { 0 }
1395b81b6b3SRodney W. Grimes };
1405b81b6b3SRodney W. Grimes
1411a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_12m[] = {
1421b67be7bSPoul-Henning Kamp { FDF_5_1200 | FL_AUTO },
143c1a6d28aSWarner Losh { FDF_5_400 | FL_AUTO },
1441b67be7bSPoul-Henning Kamp { FDF_5_360 | FL_2STEP | FL_AUTO},
1451b67be7bSPoul-Henning Kamp { 0 }
1461b67be7bSPoul-Henning Kamp };
1471b67be7bSPoul-Henning Kamp
1481b67be7bSPoul-Henning Kamp static struct fd_type fd_searchlist_720k[] = {
1491b67be7bSPoul-Henning Kamp { FDF_3_720 },
1501b67be7bSPoul-Henning Kamp { 0 }
1511a6bed68SJoerg Wunsch };
1521a6bed68SJoerg Wunsch
1531a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_144m[] = {
1541b67be7bSPoul-Henning Kamp { FDF_3_1440 | FL_AUTO},
1551b67be7bSPoul-Henning Kamp { FDF_3_720 | FL_AUTO},
1561b67be7bSPoul-Henning Kamp { 0 }
1571a6bed68SJoerg Wunsch };
1581a6bed68SJoerg Wunsch
1591a6bed68SJoerg Wunsch static struct fd_type fd_searchlist_288m[] = {
1601b67be7bSPoul-Henning Kamp { FDF_3_1440 | FL_AUTO },
1611a6bed68SJoerg Wunsch #if 0
1621b67be7bSPoul-Henning Kamp { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
1631a6bed68SJoerg Wunsch #endif
1641b67be7bSPoul-Henning Kamp { FDF_3_720 | FL_AUTO},
1651b67be7bSPoul-Henning Kamp { 0 }
1661a6bed68SJoerg Wunsch };
167dc16046fSJoerg Wunsch
1681b67be7bSPoul-Henning Kamp /*
1691b67be7bSPoul-Henning Kamp * Order must match enum fd_drivetype in <sys/fdcio.h>.
1701b67be7bSPoul-Henning Kamp */
1711b67be7bSPoul-Henning Kamp static struct fd_type *fd_native_types[] = {
1721b67be7bSPoul-Henning Kamp NULL, /* FDT_NONE */
1731b67be7bSPoul-Henning Kamp fd_searchlist_360k, /* FDT_360K */
1741b67be7bSPoul-Henning Kamp fd_searchlist_12m, /* FDT_12M */
1751b67be7bSPoul-Henning Kamp fd_searchlist_720k, /* FDT_720K */
1761b67be7bSPoul-Henning Kamp fd_searchlist_144m, /* FDT_144M */
1778fe457d6SPeter Edwards fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
1781b67be7bSPoul-Henning Kamp fd_searchlist_288m, /* FDT_288M */
1791b67be7bSPoul-Henning Kamp };
180f8ce7dd5SJoerg Wunsch
1811b67be7bSPoul-Henning Kamp /*
1821b67be7bSPoul-Henning Kamp * Internals start here
1831b67be7bSPoul-Henning Kamp */
1841b67be7bSPoul-Henning Kamp
1851b67be7bSPoul-Henning Kamp /* registers */
1861b67be7bSPoul-Henning Kamp #define FDOUT 2 /* Digital Output Register (W) */
1871b67be7bSPoul-Henning Kamp #define FDO_FDSEL 0x03 /* floppy device select */
1881b67be7bSPoul-Henning Kamp #define FDO_FRST 0x04 /* floppy controller reset */
1891b67be7bSPoul-Henning Kamp #define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
1901b67be7bSPoul-Henning Kamp #define FDO_MOEN0 0x10 /* motor enable drive 0 */
1911b67be7bSPoul-Henning Kamp #define FDO_MOEN1 0x20 /* motor enable drive 1 */
1921b67be7bSPoul-Henning Kamp #define FDO_MOEN2 0x40 /* motor enable drive 2 */
1931b67be7bSPoul-Henning Kamp #define FDO_MOEN3 0x80 /* motor enable drive 3 */
1941b67be7bSPoul-Henning Kamp
1951b67be7bSPoul-Henning Kamp #define FDSTS 4 /* NEC 765 Main Status Register (R) */
19650d99d1aSJung-uk Kim #define FDDSR 4 /* Data Rate Select Register (W) */
1971b67be7bSPoul-Henning Kamp #define FDDATA 5 /* NEC 765 Data Register (R/W) */
1981b67be7bSPoul-Henning Kamp #define FDCTL 7 /* Control Register (W) */
1991b67be7bSPoul-Henning Kamp
2001b67be7bSPoul-Henning Kamp /*
201973bfe6cSWarner Losh * The YE-DATA PC Card floppies use PIO to read in the data rather
202973bfe6cSWarner Losh * than DMA due to the wild variability of DMA for the PC Card
203973bfe6cSWarner Losh * devices. DMA was deleted from the PC Card specification in version
204973bfe6cSWarner Losh * 7.2 of the standard, but that post-dates the YE-DATA devices by many
205973bfe6cSWarner Losh * years.
206973bfe6cSWarner Losh *
207973bfe6cSWarner Losh * In addition, if we cannot setup the DMA resources for the ISA
208973bfe6cSWarner Losh * attachment, we'll use this same offset for data transfer. However,
209973bfe6cSWarner Losh * that almost certainly won't work.
210b4046cd7SWarner Losh *
211b4046cd7SWarner Losh * For this mode, offset 0 and 1 must be used to setup the transfer
212973bfe6cSWarner Losh * for this floppy. This is OK for PC Card YE Data devices, but for
213973bfe6cSWarner Losh * ISA this is likely wrong. These registers are only available on
214973bfe6cSWarner Losh * those systems that map them to the floppy drive. Newer systems do
215973bfe6cSWarner Losh * not do this, and we should likely prohibit access to them (or
216973bfe6cSWarner Losh * disallow NODMA to be set).
2171b67be7bSPoul-Henning Kamp */
218b4046cd7SWarner Losh #define FDBCDR 0 /* And 1 */
219b4046cd7SWarner Losh #define FD_YE_DATAPORT 6 /* Drive Data port */
2201b67be7bSPoul-Henning Kamp
2211b67be7bSPoul-Henning Kamp #define FDI_DCHG 0x80 /* diskette has been changed */
2221b67be7bSPoul-Henning Kamp /* requires drive and motor being selected */
2231b67be7bSPoul-Henning Kamp /* is cleared by any step pulse to drive */
2241b67be7bSPoul-Henning Kamp
2251b67be7bSPoul-Henning Kamp /*
22661909e4cSPoul-Henning Kamp * We have three private BIO commands.
2271b67be7bSPoul-Henning Kamp */
2281b67be7bSPoul-Henning Kamp #define BIO_PROBE BIO_CMD0
2291b67be7bSPoul-Henning Kamp #define BIO_RDID BIO_CMD1
2301b67be7bSPoul-Henning Kamp #define BIO_FMT BIO_CMD2
2315b81b6b3SRodney W. Grimes
232246ed35dSJoerg Wunsch /*
233246ed35dSJoerg Wunsch * Per drive structure (softc).
234246ed35dSJoerg Wunsch */
2356182fdbdSPeter Wemm struct fd_data {
2361b67be7bSPoul-Henning Kamp u_char *fd_ioptr; /* IO pointer */
2371b67be7bSPoul-Henning Kamp u_int fd_iosize; /* Size of IO chunks */
2381b67be7bSPoul-Henning Kamp u_int fd_iocount; /* Outstanding requests */
239b99f0a4aSAndrew Moore struct fdc_data *fdc; /* pointer to controller structure */
2405b81b6b3SRodney W. Grimes int fdsu; /* this units number on this controller */
2411a6bed68SJoerg Wunsch enum fd_drivetype type; /* drive type */
2421a6bed68SJoerg Wunsch struct fd_type *ft; /* pointer to current type descriptor */
2431b67be7bSPoul-Henning Kamp struct fd_type fts; /* type descriptors */
2441b67be7bSPoul-Henning Kamp int sectorsize;
2455b81b6b3SRodney W. Grimes int flags;
2461b67be7bSPoul-Henning Kamp #define FD_WP (1<<0) /* Write protected */
2471b67be7bSPoul-Henning Kamp #define FD_MOTOR (1<<1) /* motor should be on */
2481b67be7bSPoul-Henning Kamp #define FD_MOTORWAIT (1<<2) /* motor should be on */
2491b67be7bSPoul-Henning Kamp #define FD_EMPTY (1<<3) /* no media */
2501b67be7bSPoul-Henning Kamp #define FD_NEWDISK (1<<4) /* media changed */
2511b67be7bSPoul-Henning Kamp #define FD_ISADMA (1<<5) /* isa dma started */
2525b81b6b3SRodney W. Grimes int track; /* where we think the head is */
2531b67be7bSPoul-Henning Kamp #define FD_NO_TRACK -2
2541b67be7bSPoul-Henning Kamp int options; /* FDOPT_* */
2551b67be7bSPoul-Henning Kamp struct callout toffhandle;
2561b67be7bSPoul-Henning Kamp struct g_geom *fd_geom;
2571b67be7bSPoul-Henning Kamp struct g_provider *fd_provider;
2586182fdbdSPeter Wemm device_t dev;
2591b67be7bSPoul-Henning Kamp struct bio_queue_head fd_bq;
260e2c1243fSWarner Losh bool gone;
2616182fdbdSPeter Wemm };
26237286586SPeter Wemm
2631b67be7bSPoul-Henning Kamp #define FD_NOT_VALID -2
264752d4735SNate Lawson
265246ed35dSJoerg Wunsch static driver_intr_t fdc_intr;
266ef544f63SPaolo Pisati static driver_filter_t fdc_intr_fast;
2671b67be7bSPoul-Henning Kamp static void fdc_reset(struct fdc_data *);
268c884498fSJung-uk Kim static int fd_probe_disk(struct fd_data *, int *);
269d66c374fSTor Egge
2707029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
2717029da5cSPawel Biernacki "fdc driver");
272d66c374fSTor Egge
2731b67be7bSPoul-Henning Kamp static int fifo_threshold = 8;
2741b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
2751b67be7bSPoul-Henning Kamp "FIFO threshold setting");
2761b67be7bSPoul-Henning Kamp
2771b67be7bSPoul-Henning Kamp static int debugflags = 0;
2781b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
2791b67be7bSPoul-Henning Kamp "Debug flags");
2801b67be7bSPoul-Henning Kamp
2811b67be7bSPoul-Henning Kamp static int retries = 10;
2821b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
2831b67be7bSPoul-Henning Kamp "Number of retries to attempt");
2841b67be7bSPoul-Henning Kamp
28583e78a7fSJohn Baldwin static int spec1 = NE7_SPEC_1(6, 240);
2861b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
2871b67be7bSPoul-Henning Kamp "Specification byte one (step-rate + head unload)");
2881b67be7bSPoul-Henning Kamp
28983e78a7fSJohn Baldwin static int spec2 = NE7_SPEC_2(16, 0);
2901b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
2911b67be7bSPoul-Henning Kamp "Specification byte two (head load time + no-dma)");
2921b67be7bSPoul-Henning Kamp
2931b67be7bSPoul-Henning Kamp static int settle;
2941b67be7bSPoul-Henning Kamp SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
2951b67be7bSPoul-Henning Kamp "Head settling time in sec/hz");
2961b67be7bSPoul-Henning Kamp
2971b67be7bSPoul-Henning Kamp static void
fdprinttype(struct fd_type * ft)2981b67be7bSPoul-Henning Kamp fdprinttype(struct fd_type *ft)
2991b67be7bSPoul-Henning Kamp {
3001b67be7bSPoul-Henning Kamp
3011b67be7bSPoul-Henning Kamp printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
3021b67be7bSPoul-Henning Kamp ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
3031b67be7bSPoul-Henning Kamp ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
3041b67be7bSPoul-Henning Kamp ft->offset_side2, ft->flags);
3051b67be7bSPoul-Henning Kamp }
3061b67be7bSPoul-Henning Kamp
3071b67be7bSPoul-Henning Kamp static void
fdsettype(struct fd_data * fd,struct fd_type * ft)3081b67be7bSPoul-Henning Kamp fdsettype(struct fd_data *fd, struct fd_type *ft)
3091b67be7bSPoul-Henning Kamp {
3101b67be7bSPoul-Henning Kamp fd->ft = ft;
3111b67be7bSPoul-Henning Kamp ft->size = ft->sectrac * ft->heads * ft->tracks;
3121b67be7bSPoul-Henning Kamp fd->sectorsize = 128 << fd->ft->secsize;
3131b67be7bSPoul-Henning Kamp }
3145b81b6b3SRodney W. Grimes
315246ed35dSJoerg Wunsch /*
316246ed35dSJoerg Wunsch * Bus space handling (access to low-level IO).
317246ed35dSJoerg Wunsch */
31875da3c1aSEd Schouten static inline void
fdregwr(struct fdc_data * fdc,int reg,uint8_t v)319973bfe6cSWarner Losh fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
320973bfe6cSWarner Losh {
321973bfe6cSWarner Losh
322973bfe6cSWarner Losh bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
323973bfe6cSWarner Losh }
324973bfe6cSWarner Losh
32575da3c1aSEd Schouten static inline uint8_t
fdregrd(struct fdc_data * fdc,int reg)326973bfe6cSWarner Losh fdregrd(struct fdc_data *fdc, int reg)
327973bfe6cSWarner Losh {
328973bfe6cSWarner Losh
329973bfe6cSWarner Losh return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
330973bfe6cSWarner Losh }
331973bfe6cSWarner Losh
3321b67be7bSPoul-Henning Kamp static void
fdctl_wr(struct fdc_data * fdc,u_int8_t v)3331b67be7bSPoul-Henning Kamp fdctl_wr(struct fdc_data *fdc, u_int8_t v)
334427ccf00SDoug Rabson {
3351b67be7bSPoul-Henning Kamp
336973bfe6cSWarner Losh fdregwr(fdc, FDCTL, v);
3371b67be7bSPoul-Henning Kamp }
3381b67be7bSPoul-Henning Kamp
3391b67be7bSPoul-Henning Kamp static void
fdout_wr(struct fdc_data * fdc,u_int8_t v)3401b67be7bSPoul-Henning Kamp fdout_wr(struct fdc_data *fdc, u_int8_t v)
3411b67be7bSPoul-Henning Kamp {
3421b67be7bSPoul-Henning Kamp
343973bfe6cSWarner Losh fdregwr(fdc, FDOUT, v);
344427ccf00SDoug Rabson }
345427ccf00SDoug Rabson
346427ccf00SDoug Rabson static u_int8_t
fdsts_rd(struct fdc_data * fdc)3471b67be7bSPoul-Henning Kamp fdsts_rd(struct fdc_data *fdc)
348427ccf00SDoug Rabson {
3491b67be7bSPoul-Henning Kamp
350973bfe6cSWarner Losh return fdregrd(fdc, FDSTS);
351427ccf00SDoug Rabson }
352427ccf00SDoug Rabson
353427ccf00SDoug Rabson static void
fddsr_wr(struct fdc_data * fdc,u_int8_t v)35450d99d1aSJung-uk Kim fddsr_wr(struct fdc_data *fdc, u_int8_t v)
35550d99d1aSJung-uk Kim {
35650d99d1aSJung-uk Kim
35750d99d1aSJung-uk Kim fdregwr(fdc, FDDSR, v);
35850d99d1aSJung-uk Kim }
35950d99d1aSJung-uk Kim
36050d99d1aSJung-uk Kim static void
fddata_wr(struct fdc_data * fdc,u_int8_t v)3611b67be7bSPoul-Henning Kamp fddata_wr(struct fdc_data *fdc, u_int8_t v)
362427ccf00SDoug Rabson {
3631b67be7bSPoul-Henning Kamp
364973bfe6cSWarner Losh fdregwr(fdc, FDDATA, v);
365427ccf00SDoug Rabson }
366427ccf00SDoug Rabson
367427ccf00SDoug Rabson static u_int8_t
fddata_rd(struct fdc_data * fdc)3681b67be7bSPoul-Henning Kamp fddata_rd(struct fdc_data *fdc)
369427ccf00SDoug Rabson {
3701b67be7bSPoul-Henning Kamp
371973bfe6cSWarner Losh return fdregrd(fdc, FDDATA);
372427ccf00SDoug Rabson }
373427ccf00SDoug Rabson
374427ccf00SDoug Rabson static u_int8_t
fdin_rd(struct fdc_data * fdc)3751b67be7bSPoul-Henning Kamp fdin_rd(struct fdc_data *fdc)
376427ccf00SDoug Rabson {
3771b67be7bSPoul-Henning Kamp
378973bfe6cSWarner Losh return fdregrd(fdc, FDCTL);
379427ccf00SDoug Rabson }
380427ccf00SDoug Rabson
381b4046cd7SWarner Losh /*
382b4046cd7SWarner Losh * Magic pseudo-DMA initialization for YE FDC. Sets count and
383b4046cd7SWarner Losh * direction.
384b4046cd7SWarner Losh */
385b4046cd7SWarner Losh static void
fdbcdr_wr(struct fdc_data * fdc,int iswrite,uint16_t count)386b4046cd7SWarner Losh fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
387b4046cd7SWarner Losh {
388973bfe6cSWarner Losh fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
389973bfe6cSWarner Losh fdregwr(fdc, FDBCDR + 1,
390973bfe6cSWarner Losh (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
391b4046cd7SWarner Losh }
392b4046cd7SWarner Losh
393dc5df763SJoerg Wunsch static int
fdc_err(struct fdc_data * fdc,const char * s)3946182fdbdSPeter Wemm fdc_err(struct fdc_data *fdc, const char *s)
395dc5df763SJoerg Wunsch {
3966182fdbdSPeter Wemm fdc->fdc_errs++;
39716b04b6aSJoerg Wunsch if (s) {
398b6e5f28eSPeter Wemm if (fdc->fdc_errs < FDC_ERRMAX)
399b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "%s", s);
400b6e5f28eSPeter Wemm else if (fdc->fdc_errs == FDC_ERRMAX)
401b6e5f28eSPeter Wemm device_printf(fdc->fdc_dev, "too many errors, not "
402b6e5f28eSPeter Wemm "logging any more\n");
40316b04b6aSJoerg Wunsch }
404dc5df763SJoerg Wunsch
4051b67be7bSPoul-Henning Kamp return (1);
406dc5df763SJoerg Wunsch }
407dc5df763SJoerg Wunsch
408dc5df763SJoerg Wunsch /*
4091b67be7bSPoul-Henning Kamp * FDC IO functions, take care of the main status register, timeout
4101b67be7bSPoul-Henning Kamp * in case the desired status bits are never set.
4111b67be7bSPoul-Henning Kamp *
4121b67be7bSPoul-Henning Kamp * These PIO loops initially start out with short delays between
4131b67be7bSPoul-Henning Kamp * each iteration in the expectation that the required condition
4141b67be7bSPoul-Henning Kamp * is usually met quickly, so it can be handled immediately.
415dc5df763SJoerg Wunsch */
4161b67be7bSPoul-Henning Kamp static int
fdc_in(struct fdc_data * fdc,int * ptr)4171b67be7bSPoul-Henning Kamp fdc_in(struct fdc_data *fdc, int *ptr)
418dc5df763SJoerg Wunsch {
4191b67be7bSPoul-Henning Kamp int i, j, step;
4201b67be7bSPoul-Henning Kamp
4211b67be7bSPoul-Henning Kamp step = 1;
4221b67be7bSPoul-Henning Kamp for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4231b67be7bSPoul-Henning Kamp i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4241b67be7bSPoul-Henning Kamp if (i == (NE7_DIO|NE7_RQM)) {
4251b67be7bSPoul-Henning Kamp i = fddata_rd(fdc);
4261b67be7bSPoul-Henning Kamp if (ptr)
4271b67be7bSPoul-Henning Kamp *ptr = i;
4281b67be7bSPoul-Henning Kamp return (0);
4291b67be7bSPoul-Henning Kamp }
4301b67be7bSPoul-Henning Kamp if (i == NE7_RQM)
4311b67be7bSPoul-Henning Kamp return (fdc_err(fdc, "ready for output in input\n"));
4321b67be7bSPoul-Henning Kamp step += step;
4331b67be7bSPoul-Henning Kamp DELAY(step);
4341b67be7bSPoul-Henning Kamp }
4351b67be7bSPoul-Henning Kamp return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
4361b67be7bSPoul-Henning Kamp }
4371b67be7bSPoul-Henning Kamp
4381b67be7bSPoul-Henning Kamp static int
fdc_out(struct fdc_data * fdc,int x)4391b67be7bSPoul-Henning Kamp fdc_out(struct fdc_data *fdc, int x)
4401b67be7bSPoul-Henning Kamp {
4411b67be7bSPoul-Henning Kamp int i, j, step;
4421b67be7bSPoul-Henning Kamp
4431b67be7bSPoul-Henning Kamp step = 1;
4441b67be7bSPoul-Henning Kamp for (j = 0; j < FDSTS_TIMEOUT; j += step) {
4451b67be7bSPoul-Henning Kamp i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
4461b67be7bSPoul-Henning Kamp if (i == NE7_RQM) {
4471b67be7bSPoul-Henning Kamp fddata_wr(fdc, x);
4481b67be7bSPoul-Henning Kamp return (0);
4491b67be7bSPoul-Henning Kamp }
4501b67be7bSPoul-Henning Kamp if (i == (NE7_DIO|NE7_RQM))
4511b67be7bSPoul-Henning Kamp return (fdc_err(fdc, "ready for input in output\n"));
4521b67be7bSPoul-Henning Kamp step += step;
4531b67be7bSPoul-Henning Kamp DELAY(step);
4541b67be7bSPoul-Henning Kamp }
4551b67be7bSPoul-Henning Kamp return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
4561b67be7bSPoul-Henning Kamp }
4571b67be7bSPoul-Henning Kamp
4581b67be7bSPoul-Henning Kamp /*
4591b67be7bSPoul-Henning Kamp * fdc_cmd: Send a command to the chip.
4601b67be7bSPoul-Henning Kamp * Takes a varargs with this structure:
4611b67be7bSPoul-Henning Kamp * # of output bytes
4621b67be7bSPoul-Henning Kamp * output bytes as int [...]
4631b67be7bSPoul-Henning Kamp * # of input bytes
4641b67be7bSPoul-Henning Kamp * input bytes as int* [...]
4651b67be7bSPoul-Henning Kamp */
4661b67be7bSPoul-Henning Kamp static int
fdc_cmd(struct fdc_data * fdc,int n_out,...)4671b67be7bSPoul-Henning Kamp fdc_cmd(struct fdc_data *fdc, int n_out, ...)
4681b67be7bSPoul-Henning Kamp {
4691b67be7bSPoul-Henning Kamp u_char cmd = 0;
470dc5df763SJoerg Wunsch int n_in;
4711b67be7bSPoul-Henning Kamp int n, i;
472dc5df763SJoerg Wunsch va_list ap;
473dc5df763SJoerg Wunsch
474dc5df763SJoerg Wunsch va_start(ap, n_out);
4751b67be7bSPoul-Henning Kamp for (n = 0; n < n_out; n++) {
4761b67be7bSPoul-Henning Kamp i = va_arg(ap, int);
4771b67be7bSPoul-Henning Kamp if (n == 0)
4781b67be7bSPoul-Henning Kamp cmd = i;
4791b67be7bSPoul-Henning Kamp if (fdc_out(fdc, i) < 0) {
480dc5df763SJoerg Wunsch char msg[50];
4812127f260SArchie Cobbs snprintf(msg, sizeof(msg),
482dc5df763SJoerg Wunsch "cmd %x failed at out byte %d of %d\n",
483dc5df763SJoerg Wunsch cmd, n + 1, n_out);
4841b67be7bSPoul-Henning Kamp fdc->flags |= FDC_NEEDS_RESET;
485a7e943a4SPyun YongHyeon va_end(ap);
4866182fdbdSPeter Wemm return fdc_err(fdc, msg);
487dc5df763SJoerg Wunsch }
488dc5df763SJoerg Wunsch }
489dc5df763SJoerg Wunsch n_in = va_arg(ap, int);
4901b67be7bSPoul-Henning Kamp for (n = 0; n < n_in; n++) {
491dc5df763SJoerg Wunsch int *ptr = va_arg(ap, int *);
492b3cb04b9SMark Johnston if (fdc_in(fdc, ptr) != 0) {
493dc5df763SJoerg Wunsch char msg[50];
4942127f260SArchie Cobbs snprintf(msg, sizeof(msg),
495dc5df763SJoerg Wunsch "cmd %02x failed at in byte %d of %d\n",
496dc5df763SJoerg Wunsch cmd, n + 1, n_in);
4971b67be7bSPoul-Henning Kamp fdc->flags |= FDC_NEEDS_RESET;
498a7e943a4SPyun YongHyeon va_end(ap);
4996182fdbdSPeter Wemm return fdc_err(fdc, msg);
500dc5df763SJoerg Wunsch }
501dc5df763SJoerg Wunsch }
502a7e943a4SPyun YongHyeon va_end(ap);
5031b67be7bSPoul-Henning Kamp return (0);
504dc5df763SJoerg Wunsch }
505dc5df763SJoerg Wunsch
5061b67be7bSPoul-Henning Kamp static void
fdc_reset(struct fdc_data * fdc)5071b67be7bSPoul-Henning Kamp fdc_reset(struct fdc_data *fdc)
508d66c374fSTor Egge {
5091b67be7bSPoul-Henning Kamp int i, r[10];
510d66c374fSTor Egge
51150d99d1aSJung-uk Kim if (fdc->fdct == FDC_ENHANCED) {
51250d99d1aSJung-uk Kim /* Try a software reset, default precomp, and 500 kb/s */
51350d99d1aSJung-uk Kim fddsr_wr(fdc, I8207X_DSR_SR);
51450d99d1aSJung-uk Kim } else {
51550d99d1aSJung-uk Kim /* Try a hardware reset, keep motor on */
5161b67be7bSPoul-Henning Kamp fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
5171b67be7bSPoul-Henning Kamp DELAY(100);
5181b67be7bSPoul-Henning Kamp /* enable FDC, but defer interrupts a moment */
5191b67be7bSPoul-Henning Kamp fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
52019a388e6SJung-uk Kim }
5211b67be7bSPoul-Henning Kamp DELAY(100);
5221b67be7bSPoul-Henning Kamp fdout_wr(fdc, fdc->fdout);
523d66c374fSTor Egge
5241b67be7bSPoul-Henning Kamp /* XXX after a reset, silently believe the FDC will accept commands */
5251b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
5261b67be7bSPoul-Henning Kamp device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
527d66c374fSTor Egge
5281b67be7bSPoul-Henning Kamp if (fdc->fdct == FDC_ENHANCED) {
5291b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 4,
5300da90eb8SJung-uk Kim I8207X_CONFIG,
5311b67be7bSPoul-Henning Kamp 0,
5321f97b64cSJoerg Wunsch /* 0x40 | */ /* Enable Implied Seek -
5331f97b64cSJoerg Wunsch * breaks 2step! */
5341b67be7bSPoul-Henning Kamp 0x10 | /* Polling disabled */
5351b67be7bSPoul-Henning Kamp (fifo_threshold - 1), /* Fifo threshold */
5361b67be7bSPoul-Henning Kamp 0x00, /* Precomp track */
5371b67be7bSPoul-Henning Kamp 0))
5381b67be7bSPoul-Henning Kamp device_printf(fdc->fdc_dev,
5391b67be7bSPoul-Henning Kamp " CONFIGURE failed in reset\n");
5401b67be7bSPoul-Henning Kamp if (debugflags & 1) {
5411b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 1,
5420da90eb8SJung-uk Kim I8207X_DUMPREG,
5431b67be7bSPoul-Henning Kamp 10, &r[0], &r[1], &r[2], &r[3], &r[4],
5441b67be7bSPoul-Henning Kamp &r[5], &r[6], &r[7], &r[8], &r[9]))
5451b67be7bSPoul-Henning Kamp device_printf(fdc->fdc_dev,
5461b67be7bSPoul-Henning Kamp " DUMPREG failed in reset\n");
5471b67be7bSPoul-Henning Kamp for (i = 0; i < 10; i++)
5481b67be7bSPoul-Henning Kamp printf(" %02x", r[i]);
5491b67be7bSPoul-Henning Kamp printf("\n");
550d66c374fSTor Egge }
551d0900d6bSJoerg Wunsch }
552d66c374fSTor Egge }
553d66c374fSTor Egge
554d66c374fSTor Egge static int
fdc_sense_drive(struct fdc_data * fdc,int * st3p)5551b67be7bSPoul-Henning Kamp fdc_sense_drive(struct fdc_data *fdc, int *st3p)
556dc5df763SJoerg Wunsch {
557dc5df763SJoerg Wunsch int st3;
558dc5df763SJoerg Wunsch
5591b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
5601b67be7bSPoul-Henning Kamp return (fdc_err(fdc, "Sense Drive Status failed\n"));
561dc5df763SJoerg Wunsch if (st3p)
562dc5df763SJoerg Wunsch *st3p = st3;
5631b67be7bSPoul-Henning Kamp return (0);
564dc5df763SJoerg Wunsch }
565dc5df763SJoerg Wunsch
5666f4e0bebSPoul-Henning Kamp static int
fdc_sense_int(struct fdc_data * fdc,int * st0p,int * cylp)5671b67be7bSPoul-Henning Kamp fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
568dc5df763SJoerg Wunsch {
5696182fdbdSPeter Wemm int cyl, st0, ret;
570dc5df763SJoerg Wunsch
5711b67be7bSPoul-Henning Kamp ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
5726182fdbdSPeter Wemm if (ret) {
5731b67be7bSPoul-Henning Kamp (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
5741b67be7bSPoul-Henning Kamp return (ret);
575dc5df763SJoerg Wunsch }
576dc5df763SJoerg Wunsch
577dc5df763SJoerg Wunsch if (st0p)
578dc5df763SJoerg Wunsch *st0p = st0;
579dc5df763SJoerg Wunsch
5806182fdbdSPeter Wemm if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
581dc5df763SJoerg Wunsch /*
582dc5df763SJoerg Wunsch * There doesn't seem to have been an interrupt.
583dc5df763SJoerg Wunsch */
5841b67be7bSPoul-Henning Kamp return (FD_NOT_VALID);
585dc5df763SJoerg Wunsch }
586dc5df763SJoerg Wunsch
587b3cb04b9SMark Johnston if (fdc_in(fdc, &cyl) != 0)
5886182fdbdSPeter Wemm return fdc_err(fdc, "can't get cyl num\n");
589dc5df763SJoerg Wunsch
590dc5df763SJoerg Wunsch if (cylp)
591dc5df763SJoerg Wunsch *cylp = cyl;
592dc5df763SJoerg Wunsch
5931b67be7bSPoul-Henning Kamp return (0);
594dc5df763SJoerg Wunsch }
595dc5df763SJoerg Wunsch
5966f4e0bebSPoul-Henning Kamp static int
fdc_read_status(struct fdc_data * fdc)5971b67be7bSPoul-Henning Kamp fdc_read_status(struct fdc_data *fdc)
598dc5df763SJoerg Wunsch {
5991b67be7bSPoul-Henning Kamp int i, ret, status;
600b5e8ce9fSBruce Evans
601246ed35dSJoerg Wunsch for (i = ret = 0; i < 7; i++) {
6021b67be7bSPoul-Henning Kamp ret = fdc_in(fdc, &status);
603b5e8ce9fSBruce Evans fdc->status[i] = status;
604b5e8ce9fSBruce Evans if (ret != 0)
605dc5df763SJoerg Wunsch break;
606dc5df763SJoerg Wunsch }
607dc5df763SJoerg Wunsch
608dc5df763SJoerg Wunsch if (ret == 0)
609dc5df763SJoerg Wunsch fdc->flags |= FDC_STAT_VALID;
610dc5df763SJoerg Wunsch else
611dc5df763SJoerg Wunsch fdc->flags &= ~FDC_STAT_VALID;
612dc5df763SJoerg Wunsch
613dc5df763SJoerg Wunsch return ret;
614dc5df763SJoerg Wunsch }
615dc5df763SJoerg Wunsch
6161b67be7bSPoul-Henning Kamp /*
6171b67be7bSPoul-Henning Kamp * Select this drive
6181b67be7bSPoul-Henning Kamp */
6191b67be7bSPoul-Henning Kamp static void
fd_select(struct fd_data * fd)6201b67be7bSPoul-Henning Kamp fd_select(struct fd_data *fd)
6211b67be7bSPoul-Henning Kamp {
6221b67be7bSPoul-Henning Kamp struct fdc_data *fdc;
6231b67be7bSPoul-Henning Kamp
6241b67be7bSPoul-Henning Kamp /* XXX: lock controller */
6251b67be7bSPoul-Henning Kamp fdc = fd->fdc;
6261b67be7bSPoul-Henning Kamp fdc->fdout &= ~FDO_FDSEL;
6271b67be7bSPoul-Henning Kamp fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
6281b67be7bSPoul-Henning Kamp fdout_wr(fdc, fdc->fdout);
6291b67be7bSPoul-Henning Kamp }
6301b67be7bSPoul-Henning Kamp
6311b67be7bSPoul-Henning Kamp static void
fd_turnon(void * arg)6321b67be7bSPoul-Henning Kamp fd_turnon(void *arg)
6331b67be7bSPoul-Henning Kamp {
6341b67be7bSPoul-Henning Kamp struct fd_data *fd;
6351b67be7bSPoul-Henning Kamp struct bio *bp;
6361b67be7bSPoul-Henning Kamp int once;
6371b67be7bSPoul-Henning Kamp
6381b67be7bSPoul-Henning Kamp fd = arg;
6399fc9d84bSJohn Baldwin mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
6401b67be7bSPoul-Henning Kamp fd->flags &= ~FD_MOTORWAIT;
6411b67be7bSPoul-Henning Kamp fd->flags |= FD_MOTOR;
6421b67be7bSPoul-Henning Kamp once = 0;
6431b67be7bSPoul-Henning Kamp for (;;) {
6441b67be7bSPoul-Henning Kamp bp = bioq_takefirst(&fd->fd_bq);
6451b67be7bSPoul-Henning Kamp if (bp == NULL)
6461b67be7bSPoul-Henning Kamp break;
6471b67be7bSPoul-Henning Kamp bioq_disksort(&fd->fdc->head, bp);
6481b67be7bSPoul-Henning Kamp once = 1;
6491b67be7bSPoul-Henning Kamp }
6501b67be7bSPoul-Henning Kamp if (once)
6511b67be7bSPoul-Henning Kamp wakeup(&fd->fdc->head);
6521b67be7bSPoul-Henning Kamp }
6531b67be7bSPoul-Henning Kamp
6541b67be7bSPoul-Henning Kamp static void
fd_motor(struct fd_data * fd,int turnon)6551b67be7bSPoul-Henning Kamp fd_motor(struct fd_data *fd, int turnon)
6561b67be7bSPoul-Henning Kamp {
6571b67be7bSPoul-Henning Kamp struct fdc_data *fdc;
6581b67be7bSPoul-Henning Kamp
6591b67be7bSPoul-Henning Kamp fdc = fd->fdc;
6601b67be7bSPoul-Henning Kamp /*
6611b67be7bSPoul-Henning Kamp mtx_assert(&fdc->fdc_mtx, MA_OWNED);
6621b67be7bSPoul-Henning Kamp */
6631b67be7bSPoul-Henning Kamp if (turnon) {
6641b67be7bSPoul-Henning Kamp fd->flags |= FD_MOTORWAIT;
6651b67be7bSPoul-Henning Kamp fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
6661b67be7bSPoul-Henning Kamp callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
6671b67be7bSPoul-Henning Kamp } else {
6689fc9d84bSJohn Baldwin callout_stop(&fd->toffhandle);
6691b67be7bSPoul-Henning Kamp fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
6701b67be7bSPoul-Henning Kamp fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
6711b67be7bSPoul-Henning Kamp }
6721b67be7bSPoul-Henning Kamp fdout_wr(fdc, fdc->fdout);
6731b67be7bSPoul-Henning Kamp }
6741b67be7bSPoul-Henning Kamp
6751b67be7bSPoul-Henning Kamp static void
fd_turnoff(void * xfd)6761b67be7bSPoul-Henning Kamp fd_turnoff(void *xfd)
6771b67be7bSPoul-Henning Kamp {
6781b67be7bSPoul-Henning Kamp struct fd_data *fd = xfd;
6791b67be7bSPoul-Henning Kamp
6809fc9d84bSJohn Baldwin mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
6811b67be7bSPoul-Henning Kamp fd_motor(fd, 0);
6821b67be7bSPoul-Henning Kamp }
6831b67be7bSPoul-Henning Kamp
6841b67be7bSPoul-Henning Kamp /*
68561909e4cSPoul-Henning Kamp * fdc_intr - wake up the worker thread.
6861b67be7bSPoul-Henning Kamp */
68761909e4cSPoul-Henning Kamp
6881b67be7bSPoul-Henning Kamp static void
fdc_intr(void * arg)6891b67be7bSPoul-Henning Kamp fdc_intr(void *arg)
6901b67be7bSPoul-Henning Kamp {
6911b67be7bSPoul-Henning Kamp
6921b67be7bSPoul-Henning Kamp wakeup(arg);
6931b67be7bSPoul-Henning Kamp }
6941b67be7bSPoul-Henning Kamp
695ef544f63SPaolo Pisati static int
fdc_intr_fast(void * arg)696ef544f63SPaolo Pisati fdc_intr_fast(void *arg)
697ef544f63SPaolo Pisati {
698ef544f63SPaolo Pisati
699ef544f63SPaolo Pisati wakeup(arg);
700ef544f63SPaolo Pisati return(FILTER_HANDLED);
701ef544f63SPaolo Pisati }
702ef544f63SPaolo Pisati
7031b67be7bSPoul-Henning Kamp /*
7041b67be7bSPoul-Henning Kamp * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
7051b67be7bSPoul-Henning Kamp */
7061b67be7bSPoul-Henning Kamp static void
fdc_pio(struct fdc_data * fdc)7071b67be7bSPoul-Henning Kamp fdc_pio(struct fdc_data *fdc)
7081b67be7bSPoul-Henning Kamp {
7091b67be7bSPoul-Henning Kamp u_char *cptr;
7101b67be7bSPoul-Henning Kamp struct bio *bp;
7111b67be7bSPoul-Henning Kamp u_int count;
7121b67be7bSPoul-Henning Kamp
7131b67be7bSPoul-Henning Kamp bp = fdc->bp;
7141b67be7bSPoul-Henning Kamp cptr = fdc->fd->fd_ioptr;
7151b67be7bSPoul-Henning Kamp count = fdc->fd->fd_iosize;
7161b67be7bSPoul-Henning Kamp
7171b67be7bSPoul-Henning Kamp if (bp->bio_cmd == BIO_READ) {
718b4046cd7SWarner Losh fdbcdr_wr(fdc, 0, count);
719973bfe6cSWarner Losh bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
720973bfe6cSWarner Losh fdc->ioff[FD_YE_DATAPORT], cptr, count);
7211b67be7bSPoul-Henning Kamp } else {
722973bfe6cSWarner Losh bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
723973bfe6cSWarner Losh fdc->ioff[FD_YE_DATAPORT], cptr, count);
724b4046cd7SWarner Losh fdbcdr_wr(fdc, 0, count); /* needed? */
7251b67be7bSPoul-Henning Kamp }
7261b67be7bSPoul-Henning Kamp }
7271b67be7bSPoul-Henning Kamp
7281b67be7bSPoul-Henning Kamp static int
fdc_biodone(struct fdc_data * fdc,int error)7291b67be7bSPoul-Henning Kamp fdc_biodone(struct fdc_data *fdc, int error)
7301b67be7bSPoul-Henning Kamp {
7311b67be7bSPoul-Henning Kamp struct fd_data *fd;
7321b67be7bSPoul-Henning Kamp struct bio *bp;
7331b67be7bSPoul-Henning Kamp
7341b67be7bSPoul-Henning Kamp fd = fdc->fd;
7351b67be7bSPoul-Henning Kamp bp = fdc->bp;
7361b67be7bSPoul-Henning Kamp
7371b67be7bSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
7381b67be7bSPoul-Henning Kamp if (--fd->fd_iocount == 0)
7391b67be7bSPoul-Henning Kamp callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
7401b67be7bSPoul-Henning Kamp fdc->bp = NULL;
7411b67be7bSPoul-Henning Kamp fdc->fd = NULL;
7421b67be7bSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
7431b67be7bSPoul-Henning Kamp if (bp->bio_to != NULL) {
7441b67be7bSPoul-Henning Kamp if ((debugflags & 2) && fd->fdc->retry > 0)
7451b67be7bSPoul-Henning Kamp printf("retries: %d\n", fd->fdc->retry);
7461b67be7bSPoul-Henning Kamp g_io_deliver(bp, error);
7471b67be7bSPoul-Henning Kamp return (0);
7481b67be7bSPoul-Henning Kamp }
7491b67be7bSPoul-Henning Kamp bp->bio_error = error;
7501b67be7bSPoul-Henning Kamp bp->bio_flags |= BIO_DONE;
7511b67be7bSPoul-Henning Kamp wakeup(bp);
7521b67be7bSPoul-Henning Kamp return (0);
7531b67be7bSPoul-Henning Kamp }
7541b67be7bSPoul-Henning Kamp
7551b67be7bSPoul-Henning Kamp static int retry_line;
7561b67be7bSPoul-Henning Kamp
7571b67be7bSPoul-Henning Kamp static int
fdc_worker(struct fdc_data * fdc)7581b67be7bSPoul-Henning Kamp fdc_worker(struct fdc_data *fdc)
7591b67be7bSPoul-Henning Kamp {
7601b67be7bSPoul-Henning Kamp struct fd_data *fd;
7611b67be7bSPoul-Henning Kamp struct bio *bp;
7621b67be7bSPoul-Henning Kamp int i, nsect;
7631b67be7bSPoul-Henning Kamp int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
7641b67be7bSPoul-Henning Kamp int head;
765acb3b5d2SJoerg Wunsch int override_error;
7661b67be7bSPoul-Henning Kamp static int need_recal;
7671b67be7bSPoul-Henning Kamp struct fdc_readid *idp;
7681b67be7bSPoul-Henning Kamp struct fd_formb *finfo;
7691b67be7bSPoul-Henning Kamp
770acb3b5d2SJoerg Wunsch override_error = 0;
771acb3b5d2SJoerg Wunsch
7721b67be7bSPoul-Henning Kamp /* Have we exhausted our retries ? */
7731b67be7bSPoul-Henning Kamp bp = fdc->bp;
7741b67be7bSPoul-Henning Kamp fd = fdc->fd;
7751b67be7bSPoul-Henning Kamp if (bp != NULL &&
7761217bf6aSCraig Rodrigues (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
7771b67be7bSPoul-Henning Kamp if ((debugflags & 4))
7781b67be7bSPoul-Henning Kamp printf("Too many retries (EIO)\n");
779c884498fSJung-uk Kim if (fdc->flags & FDC_NEEDS_RESET) {
780b31da03fSJung-uk Kim mtx_lock(&fdc->fdc_mtx);
781b31da03fSJung-uk Kim fd->flags |= FD_EMPTY;
782b31da03fSJung-uk Kim mtx_unlock(&fdc->fdc_mtx);
783c884498fSJung-uk Kim }
7841b67be7bSPoul-Henning Kamp return (fdc_biodone(fdc, EIO));
7851b67be7bSPoul-Henning Kamp }
7861b67be7bSPoul-Henning Kamp
7871b67be7bSPoul-Henning Kamp /* Disable ISADMA if we bailed while it was active */
7881b67be7bSPoul-Henning Kamp if (fd != NULL && (fd->flags & FD_ISADMA)) {
7891b67be7bSPoul-Henning Kamp isa_dmadone(
79080139618SWarner Losh bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
7911b67be7bSPoul-Henning Kamp fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
79261909e4cSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
7931b67be7bSPoul-Henning Kamp fd->flags &= ~FD_ISADMA;
79461909e4cSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
7951b67be7bSPoul-Henning Kamp }
7961b67be7bSPoul-Henning Kamp
7971b67be7bSPoul-Henning Kamp /* Unwedge the controller ? */
7981b67be7bSPoul-Henning Kamp if (fdc->flags & FDC_NEEDS_RESET) {
7991b67be7bSPoul-Henning Kamp fdc->flags &= ~FDC_NEEDS_RESET;
8001b67be7bSPoul-Henning Kamp fdc_reset(fdc);
801fed2c48aSJohn Baldwin if (cold)
802fed2c48aSJohn Baldwin DELAY(1000000);
803fed2c48aSJohn Baldwin else
8046e50e38fSJohn Baldwin tsleep(fdc, PRIBIO, "fdcrst", hz);
8051b67be7bSPoul-Henning Kamp /* Discard results */
8061b67be7bSPoul-Henning Kamp for (i = 0; i < 4; i++)
8071b67be7bSPoul-Henning Kamp fdc_sense_int(fdc, &st0, &cyl);
8081b67be7bSPoul-Henning Kamp /* All drives must recal */
8091b67be7bSPoul-Henning Kamp need_recal = 0xf;
8101b67be7bSPoul-Henning Kamp }
8111b67be7bSPoul-Henning Kamp
8121b67be7bSPoul-Henning Kamp /* Pick up a request, if need be wait for it */
8131b67be7bSPoul-Henning Kamp if (fdc->bp == NULL) {
8141b67be7bSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
8151b67be7bSPoul-Henning Kamp do {
8161b67be7bSPoul-Henning Kamp fdc->bp = bioq_takefirst(&fdc->head);
8171b67be7bSPoul-Henning Kamp if (fdc->bp == NULL)
8181b67be7bSPoul-Henning Kamp msleep(&fdc->head, &fdc->fdc_mtx,
81925f98057SJohn Baldwin PRIBIO, "-", 0);
82002910eeeSJoerg Wunsch } while (fdc->bp == NULL &&
82102910eeeSJoerg Wunsch (fdc->flags & FDC_KTHREAD_EXIT) == 0);
8221b67be7bSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
82302910eeeSJoerg Wunsch
82402910eeeSJoerg Wunsch if (fdc->bp == NULL)
82502910eeeSJoerg Wunsch /*
82602910eeeSJoerg Wunsch * Nothing to do, worker thread has been
82702910eeeSJoerg Wunsch * requested to stop.
82802910eeeSJoerg Wunsch */
82902910eeeSJoerg Wunsch return (0);
83002910eeeSJoerg Wunsch
8311b67be7bSPoul-Henning Kamp bp = fdc->bp;
8321b67be7bSPoul-Henning Kamp fd = fdc->fd = bp->bio_driver1;
8331b67be7bSPoul-Henning Kamp fdc->retry = 0;
8341b67be7bSPoul-Henning Kamp fd->fd_ioptr = bp->bio_data;
83580139618SWarner Losh if (bp->bio_cmd == BIO_FMT) {
8361b67be7bSPoul-Henning Kamp i = offsetof(struct fd_formb, fd_formb_cylno(0));
8371b67be7bSPoul-Henning Kamp fd->fd_ioptr += i;
8381b67be7bSPoul-Henning Kamp fd->fd_iosize = bp->bio_length - i;
8391b67be7bSPoul-Henning Kamp }
8401b67be7bSPoul-Henning Kamp }
8411b67be7bSPoul-Henning Kamp
8421b67be7bSPoul-Henning Kamp /* Select drive, setup params */
8431b67be7bSPoul-Henning Kamp fd_select(fd);
84450d99d1aSJung-uk Kim if (fdc->fdct == FDC_ENHANCED)
84550d99d1aSJung-uk Kim fddsr_wr(fdc, fd->ft->trans);
84650d99d1aSJung-uk Kim else
8471b67be7bSPoul-Henning Kamp fdctl_wr(fdc, fd->ft->trans);
8481b67be7bSPoul-Henning Kamp
84980139618SWarner Losh if (bp->bio_cmd == BIO_PROBE) {
850c884498fSJung-uk Kim if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
851c884498fSJung-uk Kim !(fdin_rd(fdc) & FDI_DCHG) &&
852c884498fSJung-uk Kim !(fd->flags & FD_EMPTY)) ||
853c884498fSJung-uk Kim fd_probe_disk(fd, &need_recal) == 0)
8541b67be7bSPoul-Henning Kamp return (fdc_biodone(fdc, 0));
8551b67be7bSPoul-Henning Kamp return (1);
8561b67be7bSPoul-Henning Kamp }
8571b67be7bSPoul-Henning Kamp
8581b67be7bSPoul-Henning Kamp /*
8591b67be7bSPoul-Henning Kamp * If we are dead just flush the requests
8601b67be7bSPoul-Henning Kamp */
8611b67be7bSPoul-Henning Kamp if (fd->flags & FD_EMPTY)
8621b67be7bSPoul-Henning Kamp return (fdc_biodone(fdc, ENXIO));
8631b67be7bSPoul-Henning Kamp
8641b67be7bSPoul-Henning Kamp /* Check if we lost our media */
8651b67be7bSPoul-Henning Kamp if (fdin_rd(fdc) & FDI_DCHG) {
8661b67be7bSPoul-Henning Kamp if (debugflags & 0x40)
8671b67be7bSPoul-Henning Kamp printf("Lost disk\n");
86861909e4cSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
8691b67be7bSPoul-Henning Kamp fd->flags |= FD_EMPTY;
8701b67be7bSPoul-Henning Kamp fd->flags |= FD_NEWDISK;
87161909e4cSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
8721b67be7bSPoul-Henning Kamp g_topology_lock();
8737c3a3fbeSEdward Tomasz Napierala g_orphan_provider(fd->fd_provider, ENXIO);
8741b67be7bSPoul-Henning Kamp fd->fd_provider->flags |= G_PF_WITHER;
8751b67be7bSPoul-Henning Kamp fd->fd_provider =
87602c62349SJaakko Heinonen g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
8771b67be7bSPoul-Henning Kamp g_error_provider(fd->fd_provider, 0);
8781b67be7bSPoul-Henning Kamp g_topology_unlock();
8791b67be7bSPoul-Henning Kamp return (fdc_biodone(fdc, ENXIO));
8801b67be7bSPoul-Henning Kamp }
8811b67be7bSPoul-Henning Kamp
8821b67be7bSPoul-Henning Kamp /* Check if the floppy is write-protected */
88380139618SWarner Losh if (bp->bio_cmd == BIO_FMT || bp->bio_cmd == BIO_WRITE) {
8841b67be7bSPoul-Henning Kamp retry_line = __LINE__;
8851b67be7bSPoul-Henning Kamp if(fdc_sense_drive(fdc, &st3) != 0)
8861b67be7bSPoul-Henning Kamp return (1);
8871b67be7bSPoul-Henning Kamp if(st3 & NE7_ST3_WP)
8881b67be7bSPoul-Henning Kamp return (fdc_biodone(fdc, EROFS));
8891b67be7bSPoul-Henning Kamp }
8901b67be7bSPoul-Henning Kamp
8911b67be7bSPoul-Henning Kamp mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
8921b67be7bSPoul-Henning Kamp steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
8931b67be7bSPoul-Henning Kamp i = fd->ft->sectrac * fd->ft->heads;
8941b67be7bSPoul-Henning Kamp cylinder = bp->bio_pblkno / i;
8951b67be7bSPoul-Henning Kamp descyl = cylinder * steptrac;
8961b67be7bSPoul-Henning Kamp sec = bp->bio_pblkno % i;
8971b67be7bSPoul-Henning Kamp nsect = i - sec;
8981b67be7bSPoul-Henning Kamp head = sec / fd->ft->sectrac;
8991b67be7bSPoul-Henning Kamp sec = sec % fd->ft->sectrac + 1;
9001b67be7bSPoul-Henning Kamp
9011b67be7bSPoul-Henning Kamp /* If everything is going swimmingly, use multisector xfer */
90280139618SWarner Losh if (fdc->retry == 0 &&
90380139618SWarner Losh (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
9041b67be7bSPoul-Henning Kamp fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
9051b67be7bSPoul-Henning Kamp nsect = fd->fd_iosize / fd->sectorsize;
90680139618SWarner Losh } else if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
9071b67be7bSPoul-Henning Kamp fd->fd_iosize = fd->sectorsize;
9081b67be7bSPoul-Henning Kamp nsect = 1;
9091b67be7bSPoul-Henning Kamp }
9101b67be7bSPoul-Henning Kamp
9111b67be7bSPoul-Henning Kamp /* Do RECAL if we need to or are going to track zero anyway */
9121b67be7bSPoul-Henning Kamp if ((need_recal & (1 << fd->fdsu)) ||
9131b67be7bSPoul-Henning Kamp (cylinder == 0 && fd->track != 0) ||
9141b67be7bSPoul-Henning Kamp fdc->retry > 2) {
9151b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9161b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
9171b67be7bSPoul-Henning Kamp return (1);
9186e50e38fSJohn Baldwin tsleep(fdc, PRIBIO, "fdrecal", hz);
9191b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9201b67be7bSPoul-Henning Kamp if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9211b67be7bSPoul-Henning Kamp return (1); /* XXX */
9221b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9231b67be7bSPoul-Henning Kamp if ((st0 & 0xc0) || cyl != 0)
9241b67be7bSPoul-Henning Kamp return (1);
9251b67be7bSPoul-Henning Kamp need_recal &= ~(1 << fd->fdsu);
9261b67be7bSPoul-Henning Kamp fd->track = 0;
9271b67be7bSPoul-Henning Kamp /* let the heads settle */
9281b67be7bSPoul-Henning Kamp if (settle)
9296e50e38fSJohn Baldwin tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
9301b67be7bSPoul-Henning Kamp }
9311b67be7bSPoul-Henning Kamp
9321b67be7bSPoul-Henning Kamp /*
9331b67be7bSPoul-Henning Kamp * SEEK to where we want to be
9341b67be7bSPoul-Henning Kamp */
9351f97b64cSJoerg Wunsch if (cylinder != fd->track) {
9361b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9371b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
9381b67be7bSPoul-Henning Kamp return (1);
9396e50e38fSJohn Baldwin tsleep(fdc, PRIBIO, "fdseek", hz);
9401b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9411b67be7bSPoul-Henning Kamp if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
9421b67be7bSPoul-Henning Kamp return (1); /* XXX */
9431b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9441b67be7bSPoul-Henning Kamp if ((st0 & 0xc0) || cyl != descyl) {
9451b67be7bSPoul-Henning Kamp need_recal |= (1 << fd->fdsu);
9461b67be7bSPoul-Henning Kamp return (1);
9471b67be7bSPoul-Henning Kamp }
9481b67be7bSPoul-Henning Kamp /* let the heads settle */
9491b67be7bSPoul-Henning Kamp if (settle)
9506e50e38fSJohn Baldwin tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
9511b67be7bSPoul-Henning Kamp }
9521b67be7bSPoul-Henning Kamp fd->track = cylinder;
9531b67be7bSPoul-Henning Kamp
9541b67be7bSPoul-Henning Kamp if (debugflags & 8)
9551b67be7bSPoul-Henning Kamp printf("op %x bn %ju siz %u ptr %p retry %d\n",
9561b67be7bSPoul-Henning Kamp bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
9571b67be7bSPoul-Henning Kamp fd->fd_ioptr, fdc->retry);
9581b67be7bSPoul-Henning Kamp
9591b67be7bSPoul-Henning Kamp /* Setup ISADMA if we need it and have it */
96080139618SWarner Losh if ((bp->bio_cmd == BIO_READ ||
96180139618SWarner Losh bp->bio_cmd == BIO_WRITE ||
96280139618SWarner Losh bp->bio_cmd == BIO_FMT)
9631b67be7bSPoul-Henning Kamp && !(fdc->flags & FDC_NODMA)) {
9641b67be7bSPoul-Henning Kamp isa_dmastart(
96580139618SWarner Losh bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
9661b67be7bSPoul-Henning Kamp fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
96761909e4cSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
9681b67be7bSPoul-Henning Kamp fd->flags |= FD_ISADMA;
96961909e4cSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
9701b67be7bSPoul-Henning Kamp }
9711b67be7bSPoul-Henning Kamp
9721b67be7bSPoul-Henning Kamp /* Do PIO if we have to */
9731b67be7bSPoul-Henning Kamp if (fdc->flags & FDC_NODMA) {
97480139618SWarner Losh if (bp->bio_cmd == BIO_READ ||
97580139618SWarner Losh bp->bio_cmd == BIO_WRITE ||
97680139618SWarner Losh bp->bio_cmd == BIO_FMT)
977b4046cd7SWarner Losh fdbcdr_wr(fdc, 1, fd->fd_iosize);
97880139618SWarner Losh if (bp->bio_cmd == BIO_WRITE ||
97980139618SWarner Losh bp->bio_cmd == BIO_FMT)
9801b67be7bSPoul-Henning Kamp fdc_pio(fdc);
9811b67be7bSPoul-Henning Kamp }
9821b67be7bSPoul-Henning Kamp
9831b67be7bSPoul-Henning Kamp switch(bp->bio_cmd) {
9841b67be7bSPoul-Henning Kamp case BIO_FMT:
9851b67be7bSPoul-Henning Kamp /* formatting */
9861b67be7bSPoul-Henning Kamp finfo = (struct fd_formb *)bp->bio_data;
9871b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9881b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 6,
9891b67be7bSPoul-Henning Kamp NE7CMD_FORMAT | mfm,
9901b67be7bSPoul-Henning Kamp head << 2 | fd->fdsu,
9911b67be7bSPoul-Henning Kamp finfo->fd_formb_secshift,
9921b67be7bSPoul-Henning Kamp finfo->fd_formb_nsecs,
9931b67be7bSPoul-Henning Kamp finfo->fd_formb_gaplen,
9941b67be7bSPoul-Henning Kamp finfo->fd_formb_fillbyte, 0))
9951b67be7bSPoul-Henning Kamp return (1);
9961b67be7bSPoul-Henning Kamp break;
9971b67be7bSPoul-Henning Kamp case BIO_RDID:
9981b67be7bSPoul-Henning Kamp retry_line = __LINE__;
9991b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 2,
10001b67be7bSPoul-Henning Kamp NE7CMD_READID | mfm,
10011b67be7bSPoul-Henning Kamp head << 2 | fd->fdsu, 0))
10021b67be7bSPoul-Henning Kamp return (1);
10031b67be7bSPoul-Henning Kamp break;
10041b67be7bSPoul-Henning Kamp case BIO_READ:
10051b67be7bSPoul-Henning Kamp retry_line = __LINE__;
10061b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 9,
10071b67be7bSPoul-Henning Kamp NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
10081b67be7bSPoul-Henning Kamp head << 2 | fd->fdsu, /* head & unit */
10091b67be7bSPoul-Henning Kamp fd->track, /* track */
10101b67be7bSPoul-Henning Kamp head, /* head */
10111b67be7bSPoul-Henning Kamp sec, /* sector + 1 */
10121b67be7bSPoul-Henning Kamp fd->ft->secsize, /* sector size */
10131b67be7bSPoul-Henning Kamp fd->ft->sectrac, /* sectors/track */
10141b67be7bSPoul-Henning Kamp fd->ft->gap, /* gap size */
10151b67be7bSPoul-Henning Kamp fd->ft->datalen, /* data length */
10161b67be7bSPoul-Henning Kamp 0))
10171b67be7bSPoul-Henning Kamp return (1);
10181b67be7bSPoul-Henning Kamp break;
10191b67be7bSPoul-Henning Kamp case BIO_WRITE:
10201b67be7bSPoul-Henning Kamp retry_line = __LINE__;
10211b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 9,
10221b67be7bSPoul-Henning Kamp NE7CMD_WRITE | mfm | NE7CMD_MT,
10231b67be7bSPoul-Henning Kamp head << 2 | fd->fdsu, /* head & unit */
10241b67be7bSPoul-Henning Kamp fd->track, /* track */
10251b67be7bSPoul-Henning Kamp head, /* head */
10261b67be7bSPoul-Henning Kamp sec, /* sector + 1 */
10271b67be7bSPoul-Henning Kamp fd->ft->secsize, /* sector size */
10281b67be7bSPoul-Henning Kamp fd->ft->sectrac, /* sectors/track */
10291b67be7bSPoul-Henning Kamp fd->ft->gap, /* gap size */
10301b67be7bSPoul-Henning Kamp fd->ft->datalen, /* data length */
10311b67be7bSPoul-Henning Kamp 0))
10321b67be7bSPoul-Henning Kamp return (1);
10331b67be7bSPoul-Henning Kamp break;
10341b67be7bSPoul-Henning Kamp default:
10351b67be7bSPoul-Henning Kamp KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
10361b67be7bSPoul-Henning Kamp }
10371b67be7bSPoul-Henning Kamp
10381b67be7bSPoul-Henning Kamp /* Wait for interrupt */
10396e50e38fSJohn Baldwin i = tsleep(fdc, PRIBIO, "fddata", hz);
10401b67be7bSPoul-Henning Kamp
10411b67be7bSPoul-Henning Kamp /* PIO if the read looks good */
104280139618SWarner Losh if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd == BIO_READ))
10431b67be7bSPoul-Henning Kamp fdc_pio(fdc);
10441b67be7bSPoul-Henning Kamp
10451b67be7bSPoul-Henning Kamp /* Finish DMA */
10461b67be7bSPoul-Henning Kamp if (fd->flags & FD_ISADMA) {
10471b67be7bSPoul-Henning Kamp isa_dmadone(
104880139618SWarner Losh bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
10491b67be7bSPoul-Henning Kamp fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
105061909e4cSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
10511b67be7bSPoul-Henning Kamp fd->flags &= ~FD_ISADMA;
105261909e4cSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
10531b67be7bSPoul-Henning Kamp }
10541b67be7bSPoul-Henning Kamp
10551b67be7bSPoul-Henning Kamp if (i != 0) {
10561b67be7bSPoul-Henning Kamp /*
10571b67be7bSPoul-Henning Kamp * Timeout.
10581b67be7bSPoul-Henning Kamp *
10591b67be7bSPoul-Henning Kamp * Due to IBM's brain-dead design, the FDC has a faked ready
10601b67be7bSPoul-Henning Kamp * signal, hardwired to ready == true. Thus, any command
10611b67be7bSPoul-Henning Kamp * issued if there's no diskette in the drive will _never_
10621b67be7bSPoul-Henning Kamp * complete, and must be aborted by resetting the FDC.
10631b67be7bSPoul-Henning Kamp * Many thanks, Big Blue!
10641b67be7bSPoul-Henning Kamp */
10651b67be7bSPoul-Henning Kamp retry_line = __LINE__;
10661b67be7bSPoul-Henning Kamp fdc->flags |= FDC_NEEDS_RESET;
10671b67be7bSPoul-Henning Kamp return (1);
10681b67be7bSPoul-Henning Kamp }
10691b67be7bSPoul-Henning Kamp
10701b67be7bSPoul-Henning Kamp retry_line = __LINE__;
10711b67be7bSPoul-Henning Kamp if (fdc_read_status(fdc))
10721b67be7bSPoul-Henning Kamp return (1);
10731b67be7bSPoul-Henning Kamp
10741b67be7bSPoul-Henning Kamp if (debugflags & 0x10)
10751b67be7bSPoul-Henning Kamp printf(" -> %x %x %x %x\n",
10761b67be7bSPoul-Henning Kamp fdc->status[0], fdc->status[1],
10771b67be7bSPoul-Henning Kamp fdc->status[2], fdc->status[3]);
10781b67be7bSPoul-Henning Kamp
10791b67be7bSPoul-Henning Kamp st0 = fdc->status[0] & NE7_ST0_IC;
10801b67be7bSPoul-Henning Kamp if (st0 != 0) {
10811b67be7bSPoul-Henning Kamp retry_line = __LINE__;
10821b67be7bSPoul-Henning Kamp if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
10831b67be7bSPoul-Henning Kamp /*
10841b67be7bSPoul-Henning Kamp * DMA overrun. Someone hogged the bus and
10851b67be7bSPoul-Henning Kamp * didn't release it in time for the next
10861b67be7bSPoul-Henning Kamp * FDC transfer.
10871b67be7bSPoul-Henning Kamp */
10881b67be7bSPoul-Henning Kamp return (1);
10891b67be7bSPoul-Henning Kamp }
10901b67be7bSPoul-Henning Kamp retry_line = __LINE__;
10911b67be7bSPoul-Henning Kamp if(st0 == NE7_ST0_IC_IV) {
10921b67be7bSPoul-Henning Kamp fdc->flags |= FDC_NEEDS_RESET;
10931b67be7bSPoul-Henning Kamp return (1);
10941b67be7bSPoul-Henning Kamp }
10951b67be7bSPoul-Henning Kamp retry_line = __LINE__;
10961b67be7bSPoul-Henning Kamp if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
10971b67be7bSPoul-Henning Kamp need_recal |= (1 << fd->fdsu);
10981b67be7bSPoul-Henning Kamp return (1);
10991b67be7bSPoul-Henning Kamp }
11001b67be7bSPoul-Henning Kamp if (debugflags & 0x20) {
11011b67be7bSPoul-Henning Kamp printf("status %02x %02x %02x %02x %02x %02x\n",
11021b67be7bSPoul-Henning Kamp fdc->status[0], fdc->status[1], fdc->status[2],
11031b67be7bSPoul-Henning Kamp fdc->status[3], fdc->status[4], fdc->status[5]);
11041b67be7bSPoul-Henning Kamp }
11051b67be7bSPoul-Henning Kamp retry_line = __LINE__;
1106acb3b5d2SJoerg Wunsch if (fd->options & FDOPT_NOERROR)
1107acb3b5d2SJoerg Wunsch override_error = 1;
1108acb3b5d2SJoerg Wunsch else
11091b67be7bSPoul-Henning Kamp return (1);
11101b67be7bSPoul-Henning Kamp }
11111b67be7bSPoul-Henning Kamp /* All OK */
11121b67be7bSPoul-Henning Kamp switch(bp->bio_cmd) {
11131b67be7bSPoul-Henning Kamp case BIO_RDID:
11141b67be7bSPoul-Henning Kamp /* copy out ID field contents */
11151b67be7bSPoul-Henning Kamp idp = (struct fdc_readid *)bp->bio_data;
11161b67be7bSPoul-Henning Kamp idp->cyl = fdc->status[3];
11171b67be7bSPoul-Henning Kamp idp->head = fdc->status[4];
11181b67be7bSPoul-Henning Kamp idp->sec = fdc->status[5];
11191b67be7bSPoul-Henning Kamp idp->secshift = fdc->status[6];
11201b67be7bSPoul-Henning Kamp if (debugflags & 0x40)
11211b67be7bSPoul-Henning Kamp printf("c %d h %d s %d z %d\n",
11221b67be7bSPoul-Henning Kamp idp->cyl, idp->head, idp->sec, idp->secshift);
11231b67be7bSPoul-Henning Kamp break;
11241b67be7bSPoul-Henning Kamp case BIO_READ:
11251b67be7bSPoul-Henning Kamp case BIO_WRITE:
11261b67be7bSPoul-Henning Kamp bp->bio_pblkno += nsect;
11271b67be7bSPoul-Henning Kamp bp->bio_resid -= fd->fd_iosize;
11281b67be7bSPoul-Henning Kamp bp->bio_completed += fd->fd_iosize;
11291b67be7bSPoul-Henning Kamp fd->fd_ioptr += fd->fd_iosize;
1130acb3b5d2SJoerg Wunsch if (override_error) {
1131acb3b5d2SJoerg Wunsch if ((debugflags & 4))
1132acb3b5d2SJoerg Wunsch printf("FDOPT_NOERROR: returning bad data\n");
1133acb3b5d2SJoerg Wunsch } else {
1134acb3b5d2SJoerg Wunsch /* Since we managed to get something done,
1135acb3b5d2SJoerg Wunsch * reset the retry */
11361b67be7bSPoul-Henning Kamp fdc->retry = 0;
11371b67be7bSPoul-Henning Kamp if (bp->bio_resid > 0)
11381b67be7bSPoul-Henning Kamp return (0);
1139acb3b5d2SJoerg Wunsch }
11401b67be7bSPoul-Henning Kamp break;
11411b67be7bSPoul-Henning Kamp case BIO_FMT:
11421b67be7bSPoul-Henning Kamp break;
11431b67be7bSPoul-Henning Kamp }
11441b67be7bSPoul-Henning Kamp return (fdc_biodone(fdc, 0));
11451b67be7bSPoul-Henning Kamp }
11461b67be7bSPoul-Henning Kamp
11471b67be7bSPoul-Henning Kamp static void
fdc_thread(void * arg)11481b67be7bSPoul-Henning Kamp fdc_thread(void *arg)
11491b67be7bSPoul-Henning Kamp {
11501b67be7bSPoul-Henning Kamp struct fdc_data *fdc;
11511b67be7bSPoul-Henning Kamp
11521b67be7bSPoul-Henning Kamp fdc = arg;
11531b67be7bSPoul-Henning Kamp int i;
11541b67be7bSPoul-Henning Kamp
11553fc95726SJoerg Wunsch mtx_lock(&fdc->fdc_mtx);
115602910eeeSJoerg Wunsch fdc->flags |= FDC_KTHREAD_ALIVE;
115702910eeeSJoerg Wunsch while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
11583fc95726SJoerg Wunsch mtx_unlock(&fdc->fdc_mtx);
11591b67be7bSPoul-Henning Kamp i = fdc_worker(fdc);
11601b67be7bSPoul-Henning Kamp if (i && debugflags & 0x20) {
1161ac03832eSConrad Meyer if (fdc->bp != NULL)
1162ac03832eSConrad Meyer g_print_bio("", fdc->bp, "");
11631b67be7bSPoul-Henning Kamp printf("Retry line %d\n", retry_line);
11641b67be7bSPoul-Henning Kamp }
11651b67be7bSPoul-Henning Kamp fdc->retry += i;
11663fc95726SJoerg Wunsch mtx_lock(&fdc->fdc_mtx);
11671b67be7bSPoul-Henning Kamp }
116802910eeeSJoerg Wunsch fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
11693fc95726SJoerg Wunsch mtx_unlock(&fdc->fdc_mtx);
11703fc95726SJoerg Wunsch
11713745c395SJulian Elischer kproc_exit(0);
11721b67be7bSPoul-Henning Kamp }
11731b67be7bSPoul-Henning Kamp
11741b67be7bSPoul-Henning Kamp /*
1175360048a7SJoerg Wunsch * Enqueue a request.
11761b67be7bSPoul-Henning Kamp */
11771b67be7bSPoul-Henning Kamp static void
fd_enqueue(struct fd_data * fd,struct bio * bp)11781b67be7bSPoul-Henning Kamp fd_enqueue(struct fd_data *fd, struct bio *bp)
11791b67be7bSPoul-Henning Kamp {
11801b67be7bSPoul-Henning Kamp struct fdc_data *fdc;
11811b67be7bSPoul-Henning Kamp
11821b67be7bSPoul-Henning Kamp fdc = fd->fdc;
11831b67be7bSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
11841b67be7bSPoul-Henning Kamp /* If we go from idle, cancel motor turnoff */
11851b67be7bSPoul-Henning Kamp if (fd->fd_iocount++ == 0)
11869fc9d84bSJohn Baldwin callout_stop(&fd->toffhandle);
11871b67be7bSPoul-Henning Kamp if (fd->flags & FD_MOTOR) {
11881b67be7bSPoul-Henning Kamp /* The motor is on, send it directly to the controller */
11891b67be7bSPoul-Henning Kamp bioq_disksort(&fdc->head, bp);
11901b67be7bSPoul-Henning Kamp wakeup(&fdc->head);
11911b67be7bSPoul-Henning Kamp } else {
11921b67be7bSPoul-Henning Kamp /* Queue it on the drive until the motor has started */
11931b67be7bSPoul-Henning Kamp bioq_insert_tail(&fd->fd_bq, bp);
11941b67be7bSPoul-Henning Kamp if (!(fd->flags & FD_MOTORWAIT))
11951b67be7bSPoul-Henning Kamp fd_motor(fd, 1);
11961b67be7bSPoul-Henning Kamp }
11971b67be7bSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
11981b67be7bSPoul-Henning Kamp }
11991b67be7bSPoul-Henning Kamp
1200c884498fSJung-uk Kim /*
1201c884498fSJung-uk Kim * Try to find out if we have a disk in the drive.
1202c884498fSJung-uk Kim */
1203c884498fSJung-uk Kim static int
fd_probe_disk(struct fd_data * fd,int * recal)1204c884498fSJung-uk Kim fd_probe_disk(struct fd_data *fd, int *recal)
1205c884498fSJung-uk Kim {
1206c884498fSJung-uk Kim struct fdc_data *fdc;
1207c884498fSJung-uk Kim int st0, st3, cyl;
1208c884498fSJung-uk Kim int oopts, ret;
1209c884498fSJung-uk Kim
1210c884498fSJung-uk Kim fdc = fd->fdc;
1211c884498fSJung-uk Kim oopts = fd->options;
1212c884498fSJung-uk Kim fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1213c884498fSJung-uk Kim ret = 1;
1214c884498fSJung-uk Kim
1215c884498fSJung-uk Kim /*
1216c884498fSJung-uk Kim * First recal, then seek to cyl#1, this clears the old condition on
1217c884498fSJung-uk Kim * the disk change line so we can examine it for current status.
1218c884498fSJung-uk Kim */
1219c884498fSJung-uk Kim if (debugflags & 0x40)
1220c884498fSJung-uk Kim printf("New disk in probe\n");
1221c884498fSJung-uk Kim mtx_lock(&fdc->fdc_mtx);
1222c884498fSJung-uk Kim fd->flags |= FD_NEWDISK;
1223c884498fSJung-uk Kim mtx_unlock(&fdc->fdc_mtx);
1224c884498fSJung-uk Kim if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1225c884498fSJung-uk Kim goto done;
1226c884498fSJung-uk Kim tsleep(fdc, PRIBIO, "fdrecal", hz);
1227c884498fSJung-uk Kim if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1228c884498fSJung-uk Kim goto done; /* XXX */
1229c884498fSJung-uk Kim if ((st0 & 0xc0) || cyl != 0)
1230c884498fSJung-uk Kim goto done;
1231c884498fSJung-uk Kim
1232c884498fSJung-uk Kim /* Seek to track 1 */
1233c884498fSJung-uk Kim if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1234c884498fSJung-uk Kim goto done;
1235c884498fSJung-uk Kim tsleep(fdc, PRIBIO, "fdseek", hz);
1236c884498fSJung-uk Kim if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1237c884498fSJung-uk Kim goto done; /* XXX */
1238c884498fSJung-uk Kim *recal |= (1 << fd->fdsu);
1239c884498fSJung-uk Kim if (fdin_rd(fdc) & FDI_DCHG) {
1240c884498fSJung-uk Kim if (debugflags & 0x40)
1241c884498fSJung-uk Kim printf("Empty in probe\n");
1242c884498fSJung-uk Kim mtx_lock(&fdc->fdc_mtx);
1243c884498fSJung-uk Kim fd->flags |= FD_EMPTY;
1244c884498fSJung-uk Kim mtx_unlock(&fdc->fdc_mtx);
1245c884498fSJung-uk Kim } else {
1246c884498fSJung-uk Kim if (fdc_sense_drive(fdc, &st3) != 0)
1247c884498fSJung-uk Kim goto done;
1248c884498fSJung-uk Kim if (debugflags & 0x40)
1249c884498fSJung-uk Kim printf("Got disk in probe\n");
1250c884498fSJung-uk Kim mtx_lock(&fdc->fdc_mtx);
1251c884498fSJung-uk Kim fd->flags &= ~FD_EMPTY;
1252c884498fSJung-uk Kim if (st3 & NE7_ST3_WP)
1253c884498fSJung-uk Kim fd->flags |= FD_WP;
1254c884498fSJung-uk Kim else
1255c884498fSJung-uk Kim fd->flags &= ~FD_WP;
1256c884498fSJung-uk Kim mtx_unlock(&fdc->fdc_mtx);
1257c884498fSJung-uk Kim }
1258c884498fSJung-uk Kim ret = 0;
1259c884498fSJung-uk Kim
1260c884498fSJung-uk Kim done:
1261c884498fSJung-uk Kim fd->options = oopts;
1262c884498fSJung-uk Kim return (ret);
1263c884498fSJung-uk Kim }
1264c884498fSJung-uk Kim
12651b67be7bSPoul-Henning Kamp static int
fdmisccmd(struct fd_data * fd,u_int cmd,void * data)12661b67be7bSPoul-Henning Kamp fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
12671b67be7bSPoul-Henning Kamp {
12681b67be7bSPoul-Henning Kamp struct bio *bp;
12691b67be7bSPoul-Henning Kamp struct fd_formb *finfo;
12701b67be7bSPoul-Henning Kamp struct fdc_readid *idfield;
12711b67be7bSPoul-Henning Kamp int error;
12721b67be7bSPoul-Henning Kamp
12731b67be7bSPoul-Henning Kamp bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
12741b67be7bSPoul-Henning Kamp
12751b67be7bSPoul-Henning Kamp /*
12761b67be7bSPoul-Henning Kamp * Set up a bio request for fdstrategy(). bio_offset is faked
12776bccea7cSRebecca Cran * so that fdstrategy() will seek to the requested
12781b67be7bSPoul-Henning Kamp * cylinder, and use the desired head.
12791b67be7bSPoul-Henning Kamp */
12801b67be7bSPoul-Henning Kamp bp->bio_cmd = cmd;
12811b67be7bSPoul-Henning Kamp if (cmd == BIO_FMT) {
12821b67be7bSPoul-Henning Kamp finfo = (struct fd_formb *)data;
12831b67be7bSPoul-Henning Kamp bp->bio_pblkno =
12841b67be7bSPoul-Henning Kamp (finfo->cyl * fd->ft->heads + finfo->head) *
12851b67be7bSPoul-Henning Kamp fd->ft->sectrac;
12861b67be7bSPoul-Henning Kamp bp->bio_length = sizeof *finfo;
12871b67be7bSPoul-Henning Kamp } else if (cmd == BIO_RDID) {
12881b67be7bSPoul-Henning Kamp idfield = (struct fdc_readid *)data;
12891b67be7bSPoul-Henning Kamp bp->bio_pblkno =
12901b67be7bSPoul-Henning Kamp (idfield->cyl * fd->ft->heads + idfield->head) *
12911b67be7bSPoul-Henning Kamp fd->ft->sectrac;
12921b67be7bSPoul-Henning Kamp bp->bio_length = sizeof(struct fdc_readid);
12931b67be7bSPoul-Henning Kamp } else if (cmd == BIO_PROBE) {
12941b67be7bSPoul-Henning Kamp /* nothing */
12951b67be7bSPoul-Henning Kamp } else
12961b67be7bSPoul-Henning Kamp panic("wrong cmd in fdmisccmd()");
12971b67be7bSPoul-Henning Kamp bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
12981b67be7bSPoul-Henning Kamp bp->bio_data = data;
12991b67be7bSPoul-Henning Kamp bp->bio_driver1 = fd;
13001b67be7bSPoul-Henning Kamp bp->bio_flags = 0;
13011b67be7bSPoul-Henning Kamp
13021b67be7bSPoul-Henning Kamp fd_enqueue(fd, bp);
13031b67be7bSPoul-Henning Kamp
13041b67be7bSPoul-Henning Kamp do {
13056e50e38fSJohn Baldwin tsleep(bp, PRIBIO, "fdwait", hz);
13061b67be7bSPoul-Henning Kamp } while (!(bp->bio_flags & BIO_DONE));
13071b67be7bSPoul-Henning Kamp error = bp->bio_error;
13081b67be7bSPoul-Henning Kamp
13091b67be7bSPoul-Henning Kamp free(bp, M_TEMP);
13101b67be7bSPoul-Henning Kamp return (error);
13111b67be7bSPoul-Henning Kamp }
13121b67be7bSPoul-Henning Kamp
13131b67be7bSPoul-Henning Kamp /*
13141b67be7bSPoul-Henning Kamp * Try figuring out the density of the media present in our device.
13151b67be7bSPoul-Henning Kamp */
13161b67be7bSPoul-Henning Kamp static int
fdautoselect(struct fd_data * fd)13171b67be7bSPoul-Henning Kamp fdautoselect(struct fd_data *fd)
13181b67be7bSPoul-Henning Kamp {
13191b67be7bSPoul-Henning Kamp struct fd_type *fdtp;
13201b67be7bSPoul-Henning Kamp struct fdc_readid id;
13211b67be7bSPoul-Henning Kamp int oopts, rv;
13221b67be7bSPoul-Henning Kamp
13231b67be7bSPoul-Henning Kamp if (!(fd->ft->flags & FL_AUTO))
13241b67be7bSPoul-Henning Kamp return (0);
13251b67be7bSPoul-Henning Kamp
13261b67be7bSPoul-Henning Kamp fdtp = fd_native_types[fd->type];
13271b67be7bSPoul-Henning Kamp fdsettype(fd, fdtp);
13281b67be7bSPoul-Henning Kamp if (!(fd->ft->flags & FL_AUTO))
13291b67be7bSPoul-Henning Kamp return (0);
13301b67be7bSPoul-Henning Kamp
13311b67be7bSPoul-Henning Kamp /*
13321b67be7bSPoul-Henning Kamp * Try reading sector ID fields, first at cylinder 0, head 0,
13331b67be7bSPoul-Henning Kamp * then at cylinder 2, head N. We don't probe cylinder 1,
13341b67be7bSPoul-Henning Kamp * since for 5.25in DD media in a HD drive, there are no data
13351b67be7bSPoul-Henning Kamp * to read (2 step pulses per media cylinder required). For
13361b67be7bSPoul-Henning Kamp * two-sided media, the second probe always goes to head 1, so
13371b67be7bSPoul-Henning Kamp * we can tell them apart from single-sided media. As a
13381b67be7bSPoul-Henning Kamp * side-effect this means that single-sided media should be
13391b67be7bSPoul-Henning Kamp * mentioned in the search list after two-sided media of an
13401b67be7bSPoul-Henning Kamp * otherwise identical density. Media with a different number
13411b67be7bSPoul-Henning Kamp * of sectors per track but otherwise identical parameters
13421b67be7bSPoul-Henning Kamp * cannot be distinguished at all.
13431b67be7bSPoul-Henning Kamp *
13441b67be7bSPoul-Henning Kamp * If we successfully read an ID field on both cylinders where
13451b67be7bSPoul-Henning Kamp * the recorded values match our expectation, we are done.
13461b67be7bSPoul-Henning Kamp * Otherwise, we try the next density entry from the table.
13471b67be7bSPoul-Henning Kamp *
13481b67be7bSPoul-Henning Kamp * Stepping to cylinder 2 has the side-effect of clearing the
13491b67be7bSPoul-Henning Kamp * unit attention bit.
13501b67be7bSPoul-Henning Kamp */
13511b67be7bSPoul-Henning Kamp oopts = fd->options;
13521b67be7bSPoul-Henning Kamp fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
13531b67be7bSPoul-Henning Kamp for (; fdtp->heads; fdtp++) {
13541b67be7bSPoul-Henning Kamp fdsettype(fd, fdtp);
13551b67be7bSPoul-Henning Kamp
13561b67be7bSPoul-Henning Kamp id.cyl = id.head = 0;
13571b67be7bSPoul-Henning Kamp rv = fdmisccmd(fd, BIO_RDID, &id);
13581b67be7bSPoul-Henning Kamp if (rv != 0)
13591b67be7bSPoul-Henning Kamp continue;
13601b67be7bSPoul-Henning Kamp if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
13611b67be7bSPoul-Henning Kamp continue;
13621b67be7bSPoul-Henning Kamp id.cyl = 2;
13631b67be7bSPoul-Henning Kamp id.head = fd->ft->heads - 1;
13641b67be7bSPoul-Henning Kamp rv = fdmisccmd(fd, BIO_RDID, &id);
13651b67be7bSPoul-Henning Kamp if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
13661b67be7bSPoul-Henning Kamp id.secshift != fdtp->secsize)
13671b67be7bSPoul-Henning Kamp continue;
13681b67be7bSPoul-Henning Kamp if (rv == 0)
13691b67be7bSPoul-Henning Kamp break;
13701b67be7bSPoul-Henning Kamp }
13711b67be7bSPoul-Henning Kamp
13721b67be7bSPoul-Henning Kamp fd->options = oopts;
13731b67be7bSPoul-Henning Kamp if (fdtp->heads == 0) {
13741b67be7bSPoul-Henning Kamp if (debugflags & 0x40)
13751b67be7bSPoul-Henning Kamp device_printf(fd->dev, "autoselection failed\n");
13761b67be7bSPoul-Henning Kamp fdsettype(fd, fd_native_types[fd->type]);
1377c884498fSJung-uk Kim return (-1);
13781b67be7bSPoul-Henning Kamp } else {
13791b67be7bSPoul-Henning Kamp if (debugflags & 0x40) {
13801b67be7bSPoul-Henning Kamp device_printf(fd->dev,
138183e78a7fSJohn Baldwin "autoselected %d KB medium\n",
138283e78a7fSJohn Baldwin fd->ft->size / 2);
13831b67be7bSPoul-Henning Kamp fdprinttype(fd->ft);
13841b67be7bSPoul-Henning Kamp }
13851b67be7bSPoul-Henning Kamp return (0);
13861b67be7bSPoul-Henning Kamp }
13871b67be7bSPoul-Henning Kamp }
13881b67be7bSPoul-Henning Kamp
13891b67be7bSPoul-Henning Kamp /*
13901b67be7bSPoul-Henning Kamp * GEOM class implementation
13911b67be7bSPoul-Henning Kamp */
13921b67be7bSPoul-Henning Kamp
13931b67be7bSPoul-Henning Kamp static g_access_t fd_access;
13941b67be7bSPoul-Henning Kamp static g_start_t fd_start;
13951b67be7bSPoul-Henning Kamp static g_ioctl_t fd_ioctl;
1396e2c1243fSWarner Losh static g_provgone_t fd_providergone;
13971b67be7bSPoul-Henning Kamp
13981b67be7bSPoul-Henning Kamp struct g_class g_fd_class = {
13991b67be7bSPoul-Henning Kamp .name = "FD",
14001b67be7bSPoul-Henning Kamp .version = G_VERSION,
14011b67be7bSPoul-Henning Kamp .start = fd_start,
14021b67be7bSPoul-Henning Kamp .access = fd_access,
14031b67be7bSPoul-Henning Kamp .ioctl = fd_ioctl,
1404e2c1243fSWarner Losh .providergone = fd_providergone,
14051b67be7bSPoul-Henning Kamp };
14061b67be7bSPoul-Henning Kamp
14071b67be7bSPoul-Henning Kamp static int
fd_access(struct g_provider * pp,int r,int w,int e)14081b67be7bSPoul-Henning Kamp fd_access(struct g_provider *pp, int r, int w, int e)
14091b67be7bSPoul-Henning Kamp {
14101b67be7bSPoul-Henning Kamp struct fd_data *fd;
14111b67be7bSPoul-Henning Kamp struct fdc_data *fdc;
14121b67be7bSPoul-Henning Kamp int ar, aw, ae;
14131b67be7bSPoul-Henning Kamp
14141b67be7bSPoul-Henning Kamp fd = pp->geom->softc;
14151b67be7bSPoul-Henning Kamp fdc = fd->fdc;
14161b67be7bSPoul-Henning Kamp
14171b67be7bSPoul-Henning Kamp /*
14181b67be7bSPoul-Henning Kamp * If our provider is withering, we can only get negative requests
14191b67be7bSPoul-Henning Kamp * and we don't want to even see them
14201b67be7bSPoul-Henning Kamp */
14211b67be7bSPoul-Henning Kamp if (pp->flags & G_PF_WITHER)
14221b67be7bSPoul-Henning Kamp return (0);
14231b67be7bSPoul-Henning Kamp
14241b67be7bSPoul-Henning Kamp ar = r + pp->acr;
14251b67be7bSPoul-Henning Kamp aw = w + pp->acw;
14261b67be7bSPoul-Henning Kamp ae = e + pp->ace;
14271b67be7bSPoul-Henning Kamp
14281b67be7bSPoul-Henning Kamp if (ar == 0 && aw == 0 && ae == 0) {
1429acb3b5d2SJoerg Wunsch fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
14301b67be7bSPoul-Henning Kamp return (0);
14311b67be7bSPoul-Henning Kamp }
14321b67be7bSPoul-Henning Kamp
14331b67be7bSPoul-Henning Kamp if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
14341b67be7bSPoul-Henning Kamp if (fdmisccmd(fd, BIO_PROBE, NULL))
14351b67be7bSPoul-Henning Kamp return (ENXIO);
14361b67be7bSPoul-Henning Kamp if (fd->flags & FD_EMPTY)
14371b67be7bSPoul-Henning Kamp return (ENXIO);
14381b67be7bSPoul-Henning Kamp if (fd->flags & FD_NEWDISK) {
1439c884498fSJung-uk Kim if (fdautoselect(fd) != 0 &&
1440c884498fSJung-uk Kim (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1441c884498fSJung-uk Kim mtx_lock(&fdc->fdc_mtx);
1442c884498fSJung-uk Kim fd->flags |= FD_EMPTY;
1443c884498fSJung-uk Kim mtx_unlock(&fdc->fdc_mtx);
1444c884498fSJung-uk Kim return (ENXIO);
1445c884498fSJung-uk Kim }
144661909e4cSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
14471b67be7bSPoul-Henning Kamp fd->flags &= ~FD_NEWDISK;
144861909e4cSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
14491b67be7bSPoul-Henning Kamp }
14501b67be7bSPoul-Henning Kamp }
14511b67be7bSPoul-Henning Kamp
1452fc9f8bcfSKonstantin Belousov if (w > 0 && (fd->flags & FD_WP)) {
14531b67be7bSPoul-Henning Kamp return (EROFS);
1454fc9f8bcfSKonstantin Belousov }
14551b67be7bSPoul-Henning Kamp
14561b67be7bSPoul-Henning Kamp pp->sectorsize = fd->sectorsize;
14571b67be7bSPoul-Henning Kamp pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
14581b67be7bSPoul-Henning Kamp pp->mediasize = pp->stripesize * fd->ft->tracks;
14591b67be7bSPoul-Henning Kamp return (0);
14601b67be7bSPoul-Henning Kamp }
14611b67be7bSPoul-Henning Kamp
14621b67be7bSPoul-Henning Kamp static void
fd_start(struct bio * bp)14631b67be7bSPoul-Henning Kamp fd_start(struct bio *bp)
14641b67be7bSPoul-Henning Kamp {
14651b67be7bSPoul-Henning Kamp struct fd_data * fd;
14661b67be7bSPoul-Henning Kamp
14671b67be7bSPoul-Henning Kamp fd = bp->bio_to->geom->softc;
14681b67be7bSPoul-Henning Kamp bp->bio_driver1 = fd;
146980139618SWarner Losh if (bp->bio_cmd == BIO_GETATTR) {
14701b67be7bSPoul-Henning Kamp if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
14711b67be7bSPoul-Henning Kamp return;
14721b67be7bSPoul-Henning Kamp if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
14731b67be7bSPoul-Henning Kamp return;
14741b67be7bSPoul-Henning Kamp g_io_deliver(bp, ENOIOCTL);
14751b67be7bSPoul-Henning Kamp return;
14761b67be7bSPoul-Henning Kamp }
147780139618SWarner Losh if (!(bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
14781b67be7bSPoul-Henning Kamp g_io_deliver(bp, EOPNOTSUPP);
14791b67be7bSPoul-Henning Kamp return;
14801b67be7bSPoul-Henning Kamp }
14811b67be7bSPoul-Henning Kamp bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
14821b67be7bSPoul-Henning Kamp bp->bio_resid = bp->bio_length;
14831b67be7bSPoul-Henning Kamp fd_enqueue(fd, bp);
14841b67be7bSPoul-Henning Kamp return;
14851b67be7bSPoul-Henning Kamp }
14861b67be7bSPoul-Henning Kamp
14871b67be7bSPoul-Henning Kamp static int
fd_ioctl(struct g_provider * pp,u_long cmd,void * data,int fflag,struct thread * td)14882221dbebSPoul-Henning Kamp fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
14891b67be7bSPoul-Henning Kamp {
14901b67be7bSPoul-Henning Kamp struct fd_data *fd;
14911b67be7bSPoul-Henning Kamp struct fdc_status *fsp;
14921b67be7bSPoul-Henning Kamp struct fdc_readid *rid;
14931b67be7bSPoul-Henning Kamp int error;
14941b67be7bSPoul-Henning Kamp
14951b67be7bSPoul-Henning Kamp fd = pp->geom->softc;
14961b67be7bSPoul-Henning Kamp
14971b67be7bSPoul-Henning Kamp switch (cmd) {
14981b67be7bSPoul-Henning Kamp case FD_GTYPE: /* get drive type */
14991b67be7bSPoul-Henning Kamp *(struct fd_type *)data = *fd->ft;
15001b67be7bSPoul-Henning Kamp return (0);
15011b67be7bSPoul-Henning Kamp
15021b67be7bSPoul-Henning Kamp case FD_STYPE: /* set drive type */
15031b67be7bSPoul-Henning Kamp /*
15041b67be7bSPoul-Henning Kamp * Allow setting drive type temporarily iff
15051b67be7bSPoul-Henning Kamp * currently unset. Used for fdformat so any
15061b67be7bSPoul-Henning Kamp * user can set it, and then start formatting.
15071b67be7bSPoul-Henning Kamp */
15081b67be7bSPoul-Henning Kamp fd->fts = *(struct fd_type *)data;
15091b67be7bSPoul-Henning Kamp if (fd->fts.sectrac) {
15101b67be7bSPoul-Henning Kamp /* XXX: check for rubbish */
15111b67be7bSPoul-Henning Kamp fdsettype(fd, &fd->fts);
15121b67be7bSPoul-Henning Kamp } else {
15131b67be7bSPoul-Henning Kamp fdsettype(fd, fd_native_types[fd->type]);
15141b67be7bSPoul-Henning Kamp }
15151b67be7bSPoul-Henning Kamp if (debugflags & 0x40)
15161b67be7bSPoul-Henning Kamp fdprinttype(fd->ft);
15171b67be7bSPoul-Henning Kamp return (0);
15181b67be7bSPoul-Henning Kamp
15191b67be7bSPoul-Henning Kamp case FD_GOPTS: /* get drive options */
15201b67be7bSPoul-Henning Kamp *(int *)data = fd->options;
15211b67be7bSPoul-Henning Kamp return (0);
15221b67be7bSPoul-Henning Kamp
15231b67be7bSPoul-Henning Kamp case FD_SOPTS: /* set drive options */
15241b67be7bSPoul-Henning Kamp fd->options = *(int *)data;
15251b67be7bSPoul-Henning Kamp return (0);
15261b67be7bSPoul-Henning Kamp
15271b67be7bSPoul-Henning Kamp case FD_CLRERR:
1528acd3428bSRobert Watson error = priv_check(td, PRIV_DRIVER);
1529acd3428bSRobert Watson if (error)
1530acd3428bSRobert Watson return (error);
15311b67be7bSPoul-Henning Kamp fd->fdc->fdc_errs = 0;
15321b67be7bSPoul-Henning Kamp return (0);
15331b67be7bSPoul-Henning Kamp
15341b67be7bSPoul-Henning Kamp case FD_GSTAT:
15351b67be7bSPoul-Henning Kamp fsp = (struct fdc_status *)data;
15361b67be7bSPoul-Henning Kamp if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
15371b67be7bSPoul-Henning Kamp return (EINVAL);
15381b67be7bSPoul-Henning Kamp memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
15391b67be7bSPoul-Henning Kamp return (0);
15401b67be7bSPoul-Henning Kamp
15411b67be7bSPoul-Henning Kamp case FD_GDTYPE:
15421b67be7bSPoul-Henning Kamp *(enum fd_drivetype *)data = fd->type;
15431b67be7bSPoul-Henning Kamp return (0);
15441b67be7bSPoul-Henning Kamp
15451b67be7bSPoul-Henning Kamp case FD_FORM:
154662da7a2dSPoul-Henning Kamp if (!(fflag & FWRITE))
15472221dbebSPoul-Henning Kamp return (EPERM);
15481b67be7bSPoul-Henning Kamp if (((struct fd_formb *)data)->format_version !=
15491b67be7bSPoul-Henning Kamp FD_FORMAT_VERSION)
15501b67be7bSPoul-Henning Kamp return (EINVAL); /* wrong version of formatting prog */
15511b67be7bSPoul-Henning Kamp error = fdmisccmd(fd, BIO_FMT, data);
155261909e4cSPoul-Henning Kamp mtx_lock(&fd->fdc->fdc_mtx);
15531b67be7bSPoul-Henning Kamp fd->flags |= FD_NEWDISK;
155461909e4cSPoul-Henning Kamp mtx_unlock(&fd->fdc->fdc_mtx);
15551b67be7bSPoul-Henning Kamp break;
15561b67be7bSPoul-Henning Kamp
15571b67be7bSPoul-Henning Kamp case FD_READID:
15581b67be7bSPoul-Henning Kamp rid = (struct fdc_readid *)data;
15591b67be7bSPoul-Henning Kamp if (rid->cyl > 85 || rid->head > 1)
15601b67be7bSPoul-Henning Kamp return (EINVAL);
15611b67be7bSPoul-Henning Kamp error = fdmisccmd(fd, BIO_RDID, data);
15621b67be7bSPoul-Henning Kamp break;
15631b67be7bSPoul-Henning Kamp
15641b67be7bSPoul-Henning Kamp case FIONBIO:
15651b67be7bSPoul-Henning Kamp case FIOASYNC:
15661b67be7bSPoul-Henning Kamp /* For backwards compat with old fd*(8) tools */
15671b67be7bSPoul-Henning Kamp error = 0;
15681b67be7bSPoul-Henning Kamp break;
15691b67be7bSPoul-Henning Kamp
15701b67be7bSPoul-Henning Kamp default:
15711b67be7bSPoul-Henning Kamp if (debugflags & 0x80)
15721b67be7bSPoul-Henning Kamp printf("Unknown ioctl %lx\n", cmd);
15731b67be7bSPoul-Henning Kamp error = ENOIOCTL;
15741b67be7bSPoul-Henning Kamp break;
15751b67be7bSPoul-Henning Kamp }
15761b67be7bSPoul-Henning Kamp return (error);
15771b67be7bSPoul-Henning Kamp };
15781b67be7bSPoul-Henning Kamp
15791b67be7bSPoul-Henning Kamp /*
15801b67be7bSPoul-Henning Kamp * Configuration/initialization stuff, per controller.
15811b67be7bSPoul-Henning Kamp */
15821b67be7bSPoul-Henning Kamp struct fdc_ivars {
15831b67be7bSPoul-Henning Kamp int fdunit;
15841b67be7bSPoul-Henning Kamp int fdtype;
15851b67be7bSPoul-Henning Kamp };
15861b67be7bSPoul-Henning Kamp
15876d6fa4fdSWarner Losh void
fdc_release_resources(struct fdc_data * fdc)158837286586SPeter Wemm fdc_release_resources(struct fdc_data *fdc)
158937286586SPeter Wemm {
159037286586SPeter Wemm device_t dev;
1591973bfe6cSWarner Losh struct resource *last;
1592973bfe6cSWarner Losh int i;
159337286586SPeter Wemm
159437286586SPeter Wemm dev = fdc->fdc_dev;
1595b4046cd7SWarner Losh if (fdc->fdc_intr)
15967964bd0cSWarner Losh bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1597a6e4d8c4SNate Lawson fdc->fdc_intr = NULL;
1598b4046cd7SWarner Losh if (fdc->res_irq != NULL)
159937286586SPeter Wemm bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
160037286586SPeter Wemm fdc->res_irq);
1601a6e4d8c4SNate Lawson fdc->res_irq = NULL;
1602973bfe6cSWarner Losh last = NULL;
1603973bfe6cSWarner Losh for (i = 0; i < FDC_MAXREG; i++) {
1604973bfe6cSWarner Losh if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1605973bfe6cSWarner Losh bus_release_resource(dev, SYS_RES_IOPORT,
1606973bfe6cSWarner Losh fdc->ridio[i], fdc->resio[i]);
1607973bfe6cSWarner Losh last = fdc->resio[i];
1608973bfe6cSWarner Losh fdc->resio[i] = NULL;
1609973bfe6cSWarner Losh }
1610973bfe6cSWarner Losh }
1611b4046cd7SWarner Losh if (fdc->res_drq != NULL)
161237286586SPeter Wemm bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
161337286586SPeter Wemm fdc->res_drq);
16145ceae6b8SYoshihiro Takahashi fdc->res_drq = NULL;
161537286586SPeter Wemm }
161637286586SPeter Wemm
16176d6fa4fdSWarner Losh int
fdc_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)16183aae7b16SBruce Evans fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
161937286586SPeter Wemm {
162037286586SPeter Wemm struct fdc_ivars *ivars = device_get_ivars(child);
162137286586SPeter Wemm
162237286586SPeter Wemm switch (which) {
162337286586SPeter Wemm case FDC_IVAR_FDUNIT:
162437286586SPeter Wemm *result = ivars->fdunit;
162537286586SPeter Wemm break;
1626752d4735SNate Lawson case FDC_IVAR_FDTYPE:
1627752d4735SNate Lawson *result = ivars->fdtype;
1628752d4735SNate Lawson break;
162937286586SPeter Wemm default:
1630752d4735SNate Lawson return (ENOENT);
163137286586SPeter Wemm }
1632752d4735SNate Lawson return (0);
1633752d4735SNate Lawson }
1634752d4735SNate Lawson
1635752d4735SNate Lawson int
fdc_write_ivar(device_t dev,device_t child,int which,uintptr_t value)1636752d4735SNate Lawson fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1637752d4735SNate Lawson {
1638752d4735SNate Lawson struct fdc_ivars *ivars = device_get_ivars(child);
1639752d4735SNate Lawson
1640752d4735SNate Lawson switch (which) {
1641752d4735SNate Lawson case FDC_IVAR_FDUNIT:
1642752d4735SNate Lawson ivars->fdunit = value;
1643752d4735SNate Lawson break;
1644752d4735SNate Lawson case FDC_IVAR_FDTYPE:
1645752d4735SNate Lawson ivars->fdtype = value;
1646752d4735SNate Lawson break;
1647752d4735SNate Lawson default:
1648752d4735SNate Lawson return (ENOENT);
1649752d4735SNate Lawson }
1650752d4735SNate Lawson return (0);
1651752d4735SNate Lawson }
1652752d4735SNate Lawson
1653752d4735SNate Lawson int
fdc_initial_reset(device_t dev,struct fdc_data * fdc)16541b67be7bSPoul-Henning Kamp fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1655752d4735SNate Lawson {
16561b67be7bSPoul-Henning Kamp int ic_type, part_id;
16571b67be7bSPoul-Henning Kamp
1658ec200f23SPoul-Henning Kamp /*
1659ec200f23SPoul-Henning Kamp * A status value of 0xff is very unlikely, but not theoretically
1660ec200f23SPoul-Henning Kamp * impossible, but it is far more likely to indicate an empty bus.
1661ec200f23SPoul-Henning Kamp */
1662ec200f23SPoul-Henning Kamp if (fdsts_rd(fdc) == 0xff)
1663ec200f23SPoul-Henning Kamp return (ENXIO);
1664ec200f23SPoul-Henning Kamp
1665ec200f23SPoul-Henning Kamp /*
1666ec200f23SPoul-Henning Kamp * Assert a reset to the floppy controller and check that the status
1667ec200f23SPoul-Henning Kamp * register goes to zero.
1668ec200f23SPoul-Henning Kamp */
1669752d4735SNate Lawson fdout_wr(fdc, 0);
1670ec200f23SPoul-Henning Kamp fdout_wr(fdc, 0);
1671ec200f23SPoul-Henning Kamp if (fdsts_rd(fdc) != 0)
1672ec200f23SPoul-Henning Kamp return (ENXIO);
1673ec200f23SPoul-Henning Kamp
1674ec200f23SPoul-Henning Kamp /*
1675ec200f23SPoul-Henning Kamp * Clear the reset and see it come ready.
1676ec200f23SPoul-Henning Kamp */
1677752d4735SNate Lawson fdout_wr(fdc, FDO_FRST);
1678ec200f23SPoul-Henning Kamp DELAY(100);
1679ec200f23SPoul-Henning Kamp if (fdsts_rd(fdc) != 0x80)
1680ec200f23SPoul-Henning Kamp return (ENXIO);
1681752d4735SNate Lawson
1682752d4735SNate Lawson /* Then, see if it can handle a command. */
168383e78a7fSJohn Baldwin if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(6, 240),
168483e78a7fSJohn Baldwin NE7_SPEC_2(31, 0), 0))
1685752d4735SNate Lawson return (ENXIO);
16861b67be7bSPoul-Henning Kamp
16871b67be7bSPoul-Henning Kamp /*
16881b67be7bSPoul-Henning Kamp * Try to identify the chip.
16891b67be7bSPoul-Henning Kamp *
16901b67be7bSPoul-Henning Kamp * The i8272 datasheet documents that unknown commands
16911b67be7bSPoul-Henning Kamp * will return ST0 as 0x80. The i8272 is supposedly identical
16921b67be7bSPoul-Henning Kamp * to the NEC765.
16931b67be7bSPoul-Henning Kamp * The i82077SL datasheet says 0x90 for the VERSION command,
16941b67be7bSPoul-Henning Kamp * and several "superio" chips emulate this.
16951b67be7bSPoul-Henning Kamp */
16961b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
16971b67be7bSPoul-Henning Kamp return (ENXIO);
16981b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
16991b67be7bSPoul-Henning Kamp return (ENXIO);
17001d4b1bb7SPoul-Henning Kamp if (bootverbose)
17011d4b1bb7SPoul-Henning Kamp device_printf(dev,
17021d4b1bb7SPoul-Henning Kamp "ic_type %02x part_id %02x\n", ic_type, part_id);
17031b67be7bSPoul-Henning Kamp switch (ic_type & 0xff) {
17041b67be7bSPoul-Henning Kamp case 0x80:
17051b67be7bSPoul-Henning Kamp device_set_desc(dev, "NEC 765 or clone");
17061b67be7bSPoul-Henning Kamp fdc->fdct = FDC_NE765;
17071b67be7bSPoul-Henning Kamp break;
17081b67be7bSPoul-Henning Kamp case 0x81:
17091b67be7bSPoul-Henning Kamp case 0x90:
17101b67be7bSPoul-Henning Kamp device_set_desc(dev,
17111b67be7bSPoul-Henning Kamp "Enhanced floppy controller");
17121b67be7bSPoul-Henning Kamp fdc->fdct = FDC_ENHANCED;
17131b67be7bSPoul-Henning Kamp break;
17141b67be7bSPoul-Henning Kamp default:
17151b67be7bSPoul-Henning Kamp device_set_desc(dev, "Generic floppy controller");
17161b67be7bSPoul-Henning Kamp fdc->fdct = FDC_UNKNOWN;
17171b67be7bSPoul-Henning Kamp break;
17181b67be7bSPoul-Henning Kamp }
1719752d4735SNate Lawson return (0);
172037286586SPeter Wemm }
172137286586SPeter Wemm
17226d6fa4fdSWarner Losh int
fdc_detach(device_t dev)1723e219897aSJoerg Wunsch fdc_detach(device_t dev)
17245f830ea2SJoerg Wunsch {
17255f830ea2SJoerg Wunsch struct fdc_data *fdc;
17265f830ea2SJoerg Wunsch int error;
17275f830ea2SJoerg Wunsch
17285f830ea2SJoerg Wunsch fdc = device_get_softc(dev);
17295f830ea2SJoerg Wunsch
17305f830ea2SJoerg Wunsch /* have our children detached first */
17315f830ea2SJoerg Wunsch if ((error = bus_generic_detach(dev)))
17325f830ea2SJoerg Wunsch return (error);
17335f830ea2SJoerg Wunsch
17344586315aSJaakko Heinonen if (fdc->fdc_intr)
17354586315aSJaakko Heinonen bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
17364586315aSJaakko Heinonen fdc->fdc_intr = NULL;
17374586315aSJaakko Heinonen
173802910eeeSJoerg Wunsch /* kill worker thread */
173902910eeeSJoerg Wunsch mtx_lock(&fdc->fdc_mtx);
17405f6fcd82SJohn Baldwin fdc->flags |= FDC_KTHREAD_EXIT;
1741f8353edeSPoul-Henning Kamp wakeup(&fdc->head);
174202910eeeSJoerg Wunsch while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
174389a828fdSKonstantin Belousov msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
174402910eeeSJoerg Wunsch mtx_unlock(&fdc->fdc_mtx);
174502910eeeSJoerg Wunsch
1746e219897aSJoerg Wunsch /* reset controller, turn motor off */
1747e219897aSJoerg Wunsch fdout_wr(fdc, 0);
1748e219897aSJoerg Wunsch
17497964bd0cSWarner Losh if (!(fdc->flags & FDC_NODMA))
17507964bd0cSWarner Losh isa_dma_release(fdc->dmachan);
17515f830ea2SJoerg Wunsch fdc_release_resources(fdc);
17521b67be7bSPoul-Henning Kamp mtx_destroy(&fdc->fdc_mtx);
17535f830ea2SJoerg Wunsch return (0);
17545f830ea2SJoerg Wunsch }
17555f830ea2SJoerg Wunsch
17565b81b6b3SRodney W. Grimes /*
175737286586SPeter Wemm * Add a child device to the fdc controller. It will then be probed etc.
17585b81b6b3SRodney W. Grimes */
1759a6e4d8c4SNate Lawson device_t
fdc_add_child(device_t dev,const char * name,int unit)176037286586SPeter Wemm fdc_add_child(device_t dev, const char *name, int unit)
17615b81b6b3SRodney W. Grimes {
176237286586SPeter Wemm struct fdc_ivars *ivar;
17636182fdbdSPeter Wemm device_t child;
176492200632SGarrett Wollman
17657cc0979fSDavid Malone ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
176637286586SPeter Wemm if (ivar == NULL)
1767a6e4d8c4SNate Lawson return (NULL);
1768fe0d4089SMatthew N. Dodd child = device_add_child(dev, name, unit);
176942117b6cSPoul-Henning Kamp if (child == NULL) {
177042117b6cSPoul-Henning Kamp free(ivar, M_DEVBUF);
1771a6e4d8c4SNate Lawson return (NULL);
177242117b6cSPoul-Henning Kamp }
177337286586SPeter Wemm device_set_ivars(child, ivar);
1774a6e4d8c4SNate Lawson ivar->fdunit = unit;
1775a6e4d8c4SNate Lawson ivar->fdtype = FDT_NONE;
17768a9bc9c0SJohn Baldwin if (resource_disabled(name, unit))
17776182fdbdSPeter Wemm device_disable(child);
1778a6e4d8c4SNate Lawson return (child);
17796182fdbdSPeter Wemm }
17806182fdbdSPeter Wemm
17816d6fa4fdSWarner Losh int
fdc_attach(device_t dev)17826182fdbdSPeter Wemm fdc_attach(device_t dev)
17836182fdbdSPeter Wemm {
178437286586SPeter Wemm struct fdc_data *fdc;
1785a6e4d8c4SNate Lawson int error;
1786427ccf00SDoug Rabson
178737286586SPeter Wemm fdc = device_get_softc(dev);
1788b0c2b925SWarner Losh fdc->fdc_dev = dev;
17891b67be7bSPoul-Henning Kamp error = fdc_initial_reset(dev, fdc);
17901b67be7bSPoul-Henning Kamp if (error) {
17911b67be7bSPoul-Henning Kamp device_printf(dev, "does not respond\n");
17921b67be7bSPoul-Henning Kamp return (error);
17931b67be7bSPoul-Henning Kamp }
1794b593dd82SWarner Losh error = bus_setup_intr(dev, fdc->res_irq,
1795ef544f63SPaolo Pisati INTR_TYPE_BIO | INTR_ENTROPY |
1796ef544f63SPaolo Pisati ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1797ef544f63SPaolo Pisati ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1798ef544f63SPaolo Pisati ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1799ef544f63SPaolo Pisati fdc, &fdc->fdc_intr);
180037286586SPeter Wemm if (error) {
180137286586SPeter Wemm device_printf(dev, "cannot setup interrupt\n");
18021b67be7bSPoul-Henning Kamp return (error);
180337286586SPeter Wemm }
18047964bd0cSWarner Losh if (!(fdc->flags & FDC_NODMA)) {
18057964bd0cSWarner Losh error = isa_dma_acquire(fdc->dmachan);
18061ef0fd82SPoul-Henning Kamp if (!error) {
18071ef0fd82SPoul-Henning Kamp error = isa_dma_init(fdc->dmachan,
18081ef0fd82SPoul-Henning Kamp MAX_BYTES_PER_CYL, M_WAITOK);
18091ef0fd82SPoul-Henning Kamp if (error)
18101ef0fd82SPoul-Henning Kamp isa_dma_release(fdc->dmachan);
18111ef0fd82SPoul-Henning Kamp }
18127964bd0cSWarner Losh if (error)
18137964bd0cSWarner Losh return (error);
18147964bd0cSWarner Losh }
181537286586SPeter Wemm fdc->fdcu = device_get_unit(dev);
1816a6e4d8c4SNate Lawson fdc->flags |= FDC_NEEDS_RESET;
18176182fdbdSPeter Wemm
18181b67be7bSPoul-Henning Kamp mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
18196182fdbdSPeter Wemm
18203a2f7427SDavid Greenman /* reset controller, turn motor off, clear fdout mirror reg */
182164860614SJoerg Wunsch fdout_wr(fdc, fdc->fdout = 0);
18228177437dSPoul-Henning Kamp bioq_init(&fdc->head);
18235b81b6b3SRodney W. Grimes
18241b67be7bSPoul-Henning Kamp settle = hz / 8;
18251b67be7bSPoul-Henning Kamp
1826a6e4d8c4SNate Lawson return (0);
1827a6e4d8c4SNate Lawson }
1828a6e4d8c4SNate Lawson
1829fed2c48aSJohn Baldwin void
fdc_start_worker(device_t dev)1830fed2c48aSJohn Baldwin fdc_start_worker(device_t dev)
1831fed2c48aSJohn Baldwin {
1832fed2c48aSJohn Baldwin struct fdc_data *fdc;
1833fed2c48aSJohn Baldwin
1834fed2c48aSJohn Baldwin fdc = device_get_softc(dev);
1835fed2c48aSJohn Baldwin kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1836fed2c48aSJohn Baldwin "fdc%d", device_get_unit(dev));
1837fed2c48aSJohn Baldwin }
1838fed2c48aSJohn Baldwin
1839a6e4d8c4SNate Lawson int
fdc_hints_probe(device_t dev)1840a6e4d8c4SNate Lawson fdc_hints_probe(device_t dev)
1841a6e4d8c4SNate Lawson {
1842a6e4d8c4SNate Lawson const char *name, *dname;
1843a6e4d8c4SNate Lawson int i, error, dunit;
1844a6e4d8c4SNate Lawson
18456182fdbdSPeter Wemm /*
184637286586SPeter Wemm * Probe and attach any children. We should probably detect
184737286586SPeter Wemm * devices from the BIOS unless overridden.
18486182fdbdSPeter Wemm */
1849ada54f9eSPeter Wemm name = device_get_nameunit(dev);
18502398f0cdSPeter Wemm i = 0;
1851a6e4d8c4SNate Lawson while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1852a6e4d8c4SNate Lawson resource_int_value(dname, dunit, "drive", &dunit);
18532398f0cdSPeter Wemm fdc_add_child(dev, dname, dunit);
1854a6e4d8c4SNate Lawson }
185537286586SPeter Wemm
185660444853SJoerg Wunsch if ((error = bus_generic_attach(dev)) != 0)
185760444853SJoerg Wunsch return (error);
185860444853SJoerg Wunsch return (0);
18596182fdbdSPeter Wemm }
18606182fdbdSPeter Wemm
18616d6fa4fdSWarner Losh int
fdc_print_child(device_t me,device_t child)18626182fdbdSPeter Wemm fdc_print_child(device_t me, device_t child)
18636182fdbdSPeter Wemm {
18641a6bed68SJoerg Wunsch int retval = 0, flags;
186515317dd8SMatthew N. Dodd
186615317dd8SMatthew N. Dodd retval += bus_print_child_header(me, child);
18671a6bed68SJoerg Wunsch retval += printf(" on %s drive %d", device_get_nameunit(me),
186837286586SPeter Wemm fdc_get_fdunit(child));
18691a6bed68SJoerg Wunsch if ((flags = device_get_flags(me)) != 0)
18701a6bed68SJoerg Wunsch retval += printf(" flags %#x", flags);
18711a6bed68SJoerg Wunsch retval += printf("\n");
187215317dd8SMatthew N. Dodd
187315317dd8SMatthew N. Dodd return (retval);
18746182fdbdSPeter Wemm }
18756182fdbdSPeter Wemm
187616e68fc6SPeter Wemm /*
1877246ed35dSJoerg Wunsch * Configuration/initialization, per drive.
187816e68fc6SPeter Wemm */
18796182fdbdSPeter Wemm static int
fd_probe(device_t dev)18806182fdbdSPeter Wemm fd_probe(device_t dev)
18816182fdbdSPeter Wemm {
188205d3c5fdSWarner Losh #if defined(__i386__) || defined(__amd64__)
188383e78a7fSJohn Baldwin int unit;
188405d3c5fdSWarner Losh #endif
188583e78a7fSJohn Baldwin int i;
18861a6bed68SJoerg Wunsch u_int st0, st3;
18876182fdbdSPeter Wemm struct fd_data *fd;
18886182fdbdSPeter Wemm struct fdc_data *fdc;
18891b67be7bSPoul-Henning Kamp int fdsu;
1890752d4735SNate Lawson int flags, type;
18916182fdbdSPeter Wemm
1892752d4735SNate Lawson fdsu = fdc_get_fdunit(dev);
18936182fdbdSPeter Wemm fd = device_get_softc(dev);
18946182fdbdSPeter Wemm fdc = device_get_softc(device_get_parent(dev));
18951a6bed68SJoerg Wunsch flags = device_get_flags(dev);
18966182fdbdSPeter Wemm
18976182fdbdSPeter Wemm fd->dev = dev;
18986182fdbdSPeter Wemm fd->fdc = fdc;
18996182fdbdSPeter Wemm fd->fdsu = fdsu;
19006182fdbdSPeter Wemm
1901752d4735SNate Lawson /* Auto-probe if fdinfo is present, but always allow override. */
19021b67be7bSPoul-Henning Kamp type = flags & FD_TYPEMASK;
1903752d4735SNate Lawson if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1904752d4735SNate Lawson fd->type = type;
1905752d4735SNate Lawson goto done;
1906752d4735SNate Lawson } else {
1907752d4735SNate Lawson /* make sure fdautoselect() will be called */
19081b67be7bSPoul-Henning Kamp fd->flags = FD_EMPTY;
1909752d4735SNate Lawson fd->type = type;
1910752d4735SNate Lawson }
1911752d4735SNate Lawson
19122b375b4eSYoshihiro Takahashi #if defined(__i386__) || defined(__amd64__)
191305d3c5fdSWarner Losh unit = device_get_unit(dev);
19141b67be7bSPoul-Henning Kamp if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
19151a6bed68SJoerg Wunsch /* Look up what the BIOS thinks we have. */
191627df52f9SWarner Losh if (unit == 0)
19171a6bed68SJoerg Wunsch fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
191827df52f9SWarner Losh else
19191a6bed68SJoerg Wunsch fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
19201a6bed68SJoerg Wunsch if (fd->type == FDT_288M_1)
19211a6bed68SJoerg Wunsch fd->type = FDT_288M;
19221a6bed68SJoerg Wunsch }
192304209354SPeter Wemm #endif /* __i386__ || __amd64__ */
19246182fdbdSPeter Wemm /* is there a unit? */
19251a6bed68SJoerg Wunsch if (fd->type == FDT_NONE)
19266182fdbdSPeter Wemm return (ENXIO);
19276182fdbdSPeter Wemm
19281b67be7bSPoul-Henning Kamp mtx_lock(&fdc->fdc_mtx);
1929fed2c48aSJohn Baldwin
19306182fdbdSPeter Wemm /* select it */
19311b67be7bSPoul-Henning Kamp fd_select(fd);
19321b67be7bSPoul-Henning Kamp fd_motor(fd, 1);
19331b67be7bSPoul-Henning Kamp fdc->fd = fd;
1934fb35bd37SJoerg Wunsch fdc_reset(fdc); /* XXX reset, then unreset, etc. */
19356182fdbdSPeter Wemm DELAY(1000000); /* 1 sec */
19366182fdbdSPeter Wemm
19371a6bed68SJoerg Wunsch if ((flags & FD_NO_PROBE) == 0) {
1938dc5df763SJoerg Wunsch /* If we're at track 0 first seek inwards. */
19391b67be7bSPoul-Henning Kamp if ((fdc_sense_drive(fdc, &st3) == 0) &&
19401a6bed68SJoerg Wunsch (st3 & NE7_ST3_T0)) {
1941dc5df763SJoerg Wunsch /* Seek some steps... */
19421b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1943dc5df763SJoerg Wunsch /* ...wait a moment... */
1944dc5df763SJoerg Wunsch DELAY(300000);
1945dc5df763SJoerg Wunsch /* make ctrlr happy: */
1946a7e943a4SPyun YongHyeon fdc_sense_int(fdc, NULL, NULL);
1947dc5df763SJoerg Wunsch }
1948dc5df763SJoerg Wunsch }
1949dc5df763SJoerg Wunsch
19506b7bd95bSJoerg Wunsch for (i = 0; i < 2; i++) {
19516b7bd95bSJoerg Wunsch /*
19526b7bd95bSJoerg Wunsch * we must recalibrate twice, just in case the
19531a6bed68SJoerg Wunsch * heads have been beyond cylinder 76, since
19541a6bed68SJoerg Wunsch * most FDCs still barf when attempting to
19551a6bed68SJoerg Wunsch * recalibrate more than 77 steps
19566b7bd95bSJoerg Wunsch */
1957dc5df763SJoerg Wunsch /* go back to 0: */
19581b67be7bSPoul-Henning Kamp if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
19596b7bd95bSJoerg Wunsch /* a second being enough for full stroke seek*/
19606b7bd95bSJoerg Wunsch DELAY(i == 0 ? 1000000 : 300000);
19615b81b6b3SRodney W. Grimes
19626b7bd95bSJoerg Wunsch /* anything responding? */
1963a7e943a4SPyun YongHyeon if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1964dc5df763SJoerg Wunsch (st0 & NE7_ST0_EC) == 0)
1965453130d9SPedro F. Giffuni break; /* already probed successfully */
19666b7bd95bSJoerg Wunsch }
1967dc5df763SJoerg Wunsch }
19681a6bed68SJoerg Wunsch }
19696b7bd95bSJoerg Wunsch
19701b67be7bSPoul-Henning Kamp fd_motor(fd, 0);
19711b67be7bSPoul-Henning Kamp fdc->fd = NULL;
19721b67be7bSPoul-Henning Kamp mtx_unlock(&fdc->fdc_mtx);
19733a2f7427SDavid Greenman
19741a6bed68SJoerg Wunsch if ((flags & FD_NO_PROBE) == 0 &&
19751a6bed68SJoerg Wunsch (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
19766182fdbdSPeter Wemm return (ENXIO);
19775b81b6b3SRodney W. Grimes
1978752d4735SNate Lawson done:
1979752d4735SNate Lawson
19801a6bed68SJoerg Wunsch switch (fd->type) {
19811a6bed68SJoerg Wunsch case FDT_12M:
19821a6bed68SJoerg Wunsch device_set_desc(dev, "1200-KB 5.25\" drive");
19831a6bed68SJoerg Wunsch break;
19841a6bed68SJoerg Wunsch case FDT_144M:
19851a6bed68SJoerg Wunsch device_set_desc(dev, "1440-KB 3.5\" drive");
19861a6bed68SJoerg Wunsch break;
19871a6bed68SJoerg Wunsch case FDT_288M:
19881a6bed68SJoerg Wunsch device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
19891a6bed68SJoerg Wunsch break;
19901a6bed68SJoerg Wunsch case FDT_360K:
19911a6bed68SJoerg Wunsch device_set_desc(dev, "360-KB 5.25\" drive");
19921a6bed68SJoerg Wunsch break;
19931a6bed68SJoerg Wunsch case FDT_720K:
19941a6bed68SJoerg Wunsch device_set_desc(dev, "720-KB 3.5\" drive");
19951a6bed68SJoerg Wunsch break;
19961a6bed68SJoerg Wunsch default:
19971a6bed68SJoerg Wunsch return (ENXIO);
19981a6bed68SJoerg Wunsch }
1999dc5df763SJoerg Wunsch fd->track = FD_NO_TRACK;
2000b99f0a4aSAndrew Moore fd->fdc = fdc;
2001b99f0a4aSAndrew Moore fd->fdsu = fdsu;
20023a2f7427SDavid Greenman fd->options = 0;
20039fc9d84bSJohn Baldwin callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
20045b81b6b3SRodney W. Grimes
20051a6bed68SJoerg Wunsch /* initialize densities for subdevices */
20061b67be7bSPoul-Henning Kamp fdsettype(fd, fd_native_types[fd->type]);
20076182fdbdSPeter Wemm return (0);
20086182fdbdSPeter Wemm }
20096182fdbdSPeter Wemm
20101b67be7bSPoul-Henning Kamp /*
20111b67be7bSPoul-Henning Kamp * We have to do this in a geom event because GEOM is not running
20121b67be7bSPoul-Henning Kamp * when fd_attach() is.
20131b67be7bSPoul-Henning Kamp * XXX: move fd_attach after geom like ata/scsi disks
20141b67be7bSPoul-Henning Kamp */
20151b67be7bSPoul-Henning Kamp static void
fd_attach2(void * arg,int flag)20161b67be7bSPoul-Henning Kamp fd_attach2(void *arg, int flag)
20171b67be7bSPoul-Henning Kamp {
20181b67be7bSPoul-Henning Kamp struct fd_data *fd;
20191b67be7bSPoul-Henning Kamp
20201b67be7bSPoul-Henning Kamp fd = arg;
20211b67be7bSPoul-Henning Kamp
20221b67be7bSPoul-Henning Kamp fd->fd_geom = g_new_geomf(&g_fd_class,
20233ba0c6b1SPoul-Henning Kamp "fd%d", device_get_unit(fd->dev));
202402c62349SJaakko Heinonen fd->fd_provider = g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
20251b67be7bSPoul-Henning Kamp fd->fd_geom->softc = fd;
20261b67be7bSPoul-Henning Kamp g_error_provider(fd->fd_provider, 0);
20271b67be7bSPoul-Henning Kamp }
20281b67be7bSPoul-Henning Kamp
20296182fdbdSPeter Wemm static int
fd_attach(device_t dev)20306182fdbdSPeter Wemm fd_attach(device_t dev)
20316182fdbdSPeter Wemm {
20326182fdbdSPeter Wemm struct fd_data *fd;
20336182fdbdSPeter Wemm
203464860614SJoerg Wunsch fd = device_get_softc(dev);
20351b67be7bSPoul-Henning Kamp g_post_event(fd_attach2, fd, M_WAITOK, NULL);
20361b67be7bSPoul-Henning Kamp fd->flags |= FD_EMPTY;
20371b67be7bSPoul-Henning Kamp bioq_init(&fd->fd_bq);
20381b67be7bSPoul-Henning Kamp
20396182fdbdSPeter Wemm return (0);
20405b81b6b3SRodney W. Grimes }
20415b81b6b3SRodney W. Grimes
20424586315aSJaakko Heinonen static void
fd_providergone(struct g_provider * pp)2043e2c1243fSWarner Losh fd_providergone(struct g_provider *pp)
2044e2c1243fSWarner Losh {
2045e2c1243fSWarner Losh struct fd_data *fd;
2046e2c1243fSWarner Losh
2047e2c1243fSWarner Losh fd = pp->geom->softc;
2048e2c1243fSWarner Losh fd->gone = true;
2049e2c1243fSWarner Losh wakeup(fd);
2050e2c1243fSWarner Losh }
2051e2c1243fSWarner Losh
2052e2c1243fSWarner Losh static void
fd_detach_geom(void * arg,int flag)20534586315aSJaakko Heinonen fd_detach_geom(void *arg, int flag)
20544586315aSJaakko Heinonen {
20554586315aSJaakko Heinonen struct fd_data *fd = arg;
20564586315aSJaakko Heinonen
20574586315aSJaakko Heinonen g_topology_assert();
20584586315aSJaakko Heinonen g_wither_geom(fd->fd_geom, ENXIO);
20594586315aSJaakko Heinonen }
20604586315aSJaakko Heinonen
20615f830ea2SJoerg Wunsch static int
fd_detach(device_t dev)20625f830ea2SJoerg Wunsch fd_detach(device_t dev)
20635f830ea2SJoerg Wunsch {
20645f830ea2SJoerg Wunsch struct fd_data *fd;
20655f830ea2SJoerg Wunsch
20665f830ea2SJoerg Wunsch fd = device_get_softc(dev);
2067e2c1243fSWarner Losh
20684586315aSJaakko Heinonen g_waitfor_event(fd_detach_geom, fd, M_WAITOK, NULL);
2069e2c1243fSWarner Losh while (!fd->gone) {
2070e2c1243fSWarner Losh tsleep(fd, PZERO, "fdgone", hz/10);
2071e2c1243fSWarner Losh }
2072e2c1243fSWarner Losh
2073e2c1243fSWarner Losh /*
2074e2c1243fSWarner Losh * There may be accesses to the floppy while we're waitng, so drain the
2075e2c1243fSWarner Losh * motor callback here. fdc_detach turns off motor if it's still on when
2076e2c1243fSWarner Losh * we get to this point.
2077e2c1243fSWarner Losh */
20781b67be7bSPoul-Henning Kamp callout_drain(&fd->toffhandle);
20795f830ea2SJoerg Wunsch
20805f830ea2SJoerg Wunsch return (0);
20815f830ea2SJoerg Wunsch }
20825f830ea2SJoerg Wunsch
208316e68fc6SPeter Wemm static device_method_t fd_methods[] = {
208416e68fc6SPeter Wemm /* Device interface */
208516e68fc6SPeter Wemm DEVMETHOD(device_probe, fd_probe),
208616e68fc6SPeter Wemm DEVMETHOD(device_attach, fd_attach),
20875f830ea2SJoerg Wunsch DEVMETHOD(device_detach, fd_detach),
208816e68fc6SPeter Wemm DEVMETHOD(device_shutdown, bus_generic_shutdown),
208916e68fc6SPeter Wemm DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
209016e68fc6SPeter Wemm DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
209116e68fc6SPeter Wemm { 0, 0 }
209216e68fc6SPeter Wemm };
209316e68fc6SPeter Wemm
209416e68fc6SPeter Wemm static driver_t fd_driver = {
209516e68fc6SPeter Wemm "fd",
209616e68fc6SPeter Wemm fd_methods,
209716e68fc6SPeter Wemm sizeof(struct fd_data)
209816e68fc6SPeter Wemm };
209916e68fc6SPeter Wemm
2100f8353edeSPoul-Henning Kamp static int
fdc_modevent(module_t mod,int type,void * data)2101f8353edeSPoul-Henning Kamp fdc_modevent(module_t mod, int type, void *data)
2102f8353edeSPoul-Henning Kamp {
2103f8353edeSPoul-Henning Kamp
21040c73f68bSJaakko Heinonen return (g_modevent(NULL, type, &g_fd_class));
2105f8353edeSPoul-Henning Kamp }
2106f8353edeSPoul-Henning Kamp
2107*7c146c0cSJohn Baldwin DRIVER_MODULE(fd, fdc, fd_driver, fdc_modevent, 0);
2108