Deleted Added
full compact
fdc.c (175230) fdc.c (184976)
1/*-
2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Don Ahn.
8 *
9 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
10 * aided by the Linux floppy driver modifications from David Bateman
11 * (dbateman@eng.uts.edu.au).
12 *
13 * Copyright (c) 1993, 1994 by
14 * jc@irbs.UUCP (John Capo)
15 * vak@zebub.msk.su (Serge Vakulenko)
16 * ache@astral.msk.su (Andrew A. Chernov)
17 *
18 * Copyright (c) 1993, 1994, 1995 by
19 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
20 * dufault@hda.com (Peter Dufault)
21 *
22 * Copyright (c) 2001 Joerg Wunsch,
23 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
24 *
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
27 * are met:
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
50 *
51 */
52
53#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Don Ahn.
8 *
9 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
10 * aided by the Linux floppy driver modifications from David Bateman
11 * (dbateman@eng.uts.edu.au).
12 *
13 * Copyright (c) 1993, 1994 by
14 * jc@irbs.UUCP (John Capo)
15 * vak@zebub.msk.su (Serge Vakulenko)
16 * ache@astral.msk.su (Andrew A. Chernov)
17 *
18 * Copyright (c) 1993, 1994, 1995 by
19 * joerg_wunsch@uriah.sax.de (Joerg Wunsch)
20 * dufault@hda.com (Peter Dufault)
21 *
22 * Copyright (c) 2001 Joerg Wunsch,
23 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
24 *
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
27 * are met:
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 4. Neither the name of the University nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 *
49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91
50 *
51 */
52
53#include <sys/cdefs.h>
54__FBSDID("$FreeBSD: head/sys/dev/fdc/fdc.c 175230 2008-01-11 16:50:52Z kib $");
54__FBSDID("$FreeBSD: head/sys/dev/fdc/fdc.c 184976 2008-11-15 01:43:34Z jkim $");
55
56#include "opt_fdc.h"
57
58#include <sys/param.h>
59#include <sys/bio.h>
60#include <sys/bus.h>
61#include <sys/devicestat.h>
62#include <sys/disk.h>
63#include <sys/fcntl.h>
64#include <sys/fdcio.h>
65#include <sys/filio.h>
66#include <sys/kernel.h>
67#include <sys/kthread.h>
68#include <sys/lock.h>
69#include <sys/malloc.h>
70#include <sys/module.h>
71#include <sys/mutex.h>
72#include <sys/priv.h>
73#include <sys/proc.h>
74#include <sys/rman.h>
75#include <sys/sysctl.h>
76#include <sys/systm.h>
77
78#include <geom/geom.h>
79
80#include <machine/bus.h>
81#include <machine/clock.h>
82#include <machine/stdarg.h>
83
84#include <isa/isavar.h>
85#include <isa/isareg.h>
86#include <dev/fdc/fdcvar.h>
87#include <isa/rtc.h>
88
89#include <dev/ic/nec765.h>
90
91/*
92 * Runtime configuration hints/flags
93 */
94
95/* configuration flags for fd */
96#define FD_TYPEMASK 0x0f /* drive type, matches enum
97 * fd_drivetype; on i386 machines, if
98 * given as 0, use RTC type for fd0
99 * and fd1 */
55
56#include "opt_fdc.h"
57
58#include <sys/param.h>
59#include <sys/bio.h>
60#include <sys/bus.h>
61#include <sys/devicestat.h>
62#include <sys/disk.h>
63#include <sys/fcntl.h>
64#include <sys/fdcio.h>
65#include <sys/filio.h>
66#include <sys/kernel.h>
67#include <sys/kthread.h>
68#include <sys/lock.h>
69#include <sys/malloc.h>
70#include <sys/module.h>
71#include <sys/mutex.h>
72#include <sys/priv.h>
73#include <sys/proc.h>
74#include <sys/rman.h>
75#include <sys/sysctl.h>
76#include <sys/systm.h>
77
78#include <geom/geom.h>
79
80#include <machine/bus.h>
81#include <machine/clock.h>
82#include <machine/stdarg.h>
83
84#include <isa/isavar.h>
85#include <isa/isareg.h>
86#include <dev/fdc/fdcvar.h>
87#include <isa/rtc.h>
88
89#include <dev/ic/nec765.h>
90
91/*
92 * Runtime configuration hints/flags
93 */
94
95/* configuration flags for fd */
96#define FD_TYPEMASK 0x0f /* drive type, matches enum
97 * fd_drivetype; on i386 machines, if
98 * given as 0, use RTC type for fd0
99 * and fd1 */
100#define FD_NO_CHLINE 0x10 /* drive does not support changeline
101 * aka. unit attention */
100#define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
101 * assume it is there */
102
103/*
104 * Things that could conceiveably considered parameters or tweakables
105 */
106
107/*
108 * Maximal number of bytes in a cylinder.
109 * This is used for ISADMA bouncebuffer allocation and sets the max
110 * xfersize we support.
111 *
112 * 2.88M format has 2 x 36 x 512, allow for hacked up density.
113 */
114#define MAX_BYTES_PER_CYL (2 * 40 * 512)
115
116/*
117 * Timeout value for the PIO loops to wait until the FDC main status
118 * register matches our expectations (request for master, direction
119 * bit). This is supposed to be a number of microseconds, although
120 * timing might actually not be very accurate.
121 *
122 * Timeouts of 100 msec are believed to be required for some broken
123 * (old) hardware.
124 */
125#define FDSTS_TIMEOUT 100000
126
127/*
128 * After this many errors, stop whining. Close will reset this count.
129 */
130#define FDC_ERRMAX 100
131
132/*
133 * AutoDensity search lists for each drive type.
134 */
135
136static struct fd_type fd_searchlist_360k[] = {
137 { FDF_5_360 },
138 { 0 }
139};
140
141static struct fd_type fd_searchlist_12m[] = {
142 { FDF_5_1200 | FL_AUTO },
143 { FDF_5_360 | FL_2STEP | FL_AUTO},
144 { 0 }
145};
146
147static struct fd_type fd_searchlist_720k[] = {
148 { FDF_3_720 },
149 { 0 }
150};
151
152static struct fd_type fd_searchlist_144m[] = {
153 { FDF_3_1440 | FL_AUTO},
154 { FDF_3_720 | FL_AUTO},
155 { 0 }
156};
157
158static struct fd_type fd_searchlist_288m[] = {
159 { FDF_3_1440 | FL_AUTO },
160#if 0
161 { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
162#endif
163 { FDF_3_720 | FL_AUTO},
164 { 0 }
165};
166
167/*
168 * Order must match enum fd_drivetype in <sys/fdcio.h>.
169 */
170static struct fd_type *fd_native_types[] = {
171 NULL, /* FDT_NONE */
172 fd_searchlist_360k, /* FDT_360K */
173 fd_searchlist_12m, /* FDT_12M */
174 fd_searchlist_720k, /* FDT_720K */
175 fd_searchlist_144m, /* FDT_144M */
176 fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
177 fd_searchlist_288m, /* FDT_288M */
178};
179
180/*
181 * Internals start here
182 */
183
184/* registers */
185#define FDOUT 2 /* Digital Output Register (W) */
186#define FDO_FDSEL 0x03 /* floppy device select */
187#define FDO_FRST 0x04 /* floppy controller reset */
188#define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
189#define FDO_MOEN0 0x10 /* motor enable drive 0 */
190#define FDO_MOEN1 0x20 /* motor enable drive 1 */
191#define FDO_MOEN2 0x40 /* motor enable drive 2 */
192#define FDO_MOEN3 0x80 /* motor enable drive 3 */
193
194#define FDSTS 4 /* NEC 765 Main Status Register (R) */
195#define FDDSR 4 /* Data Rate Select Register (W) */
196#define FDDATA 5 /* NEC 765 Data Register (R/W) */
197#define FDCTL 7 /* Control Register (W) */
198
199/*
200 * The YE-DATA PC Card floppies use PIO to read in the data rather
201 * than DMA due to the wild variability of DMA for the PC Card
202 * devices. DMA was deleted from the PC Card specification in version
203 * 7.2 of the standard, but that post-dates the YE-DATA devices by many
204 * years.
205 *
206 * In addition, if we cannot setup the DMA resources for the ISA
207 * attachment, we'll use this same offset for data transfer. However,
208 * that almost certainly won't work.
209 *
210 * For this mode, offset 0 and 1 must be used to setup the transfer
211 * for this floppy. This is OK for PC Card YE Data devices, but for
212 * ISA this is likely wrong. These registers are only available on
213 * those systems that map them to the floppy drive. Newer systems do
214 * not do this, and we should likely prohibit access to them (or
215 * disallow NODMA to be set).
216 */
217#define FDBCDR 0 /* And 1 */
218#define FD_YE_DATAPORT 6 /* Drive Data port */
219
220#define FDI_DCHG 0x80 /* diskette has been changed */
221 /* requires drive and motor being selected */
222 /* is cleared by any step pulse to drive */
223
224/*
225 * We have three private BIO commands.
226 */
227#define BIO_PROBE BIO_CMD0
228#define BIO_RDID BIO_CMD1
229#define BIO_FMT BIO_CMD2
230
231/*
232 * Per drive structure (softc).
233 */
234struct fd_data {
235 u_char *fd_ioptr; /* IO pointer */
236 u_int fd_iosize; /* Size of IO chunks */
237 u_int fd_iocount; /* Outstanding requests */
238 struct fdc_data *fdc; /* pointer to controller structure */
239 int fdsu; /* this units number on this controller */
240 enum fd_drivetype type; /* drive type */
241 struct fd_type *ft; /* pointer to current type descriptor */
242 struct fd_type fts; /* type descriptors */
243 int sectorsize;
244 int flags;
245#define FD_WP (1<<0) /* Write protected */
246#define FD_MOTOR (1<<1) /* motor should be on */
247#define FD_MOTORWAIT (1<<2) /* motor should be on */
248#define FD_EMPTY (1<<3) /* no media */
249#define FD_NEWDISK (1<<4) /* media changed */
250#define FD_ISADMA (1<<5) /* isa dma started */
251 int track; /* where we think the head is */
252#define FD_NO_TRACK -2
253 int options; /* FDOPT_* */
254 struct callout toffhandle;
255 struct g_geom *fd_geom;
256 struct g_provider *fd_provider;
257 device_t dev;
258 struct bio_queue_head fd_bq;
259};
260
261#define FD_NOT_VALID -2
262
263static driver_intr_t fdc_intr;
264static driver_filter_t fdc_intr_fast;
265static void fdc_reset(struct fdc_data *);
102#define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just
103 * assume it is there */
104
105/*
106 * Things that could conceiveably considered parameters or tweakables
107 */
108
109/*
110 * Maximal number of bytes in a cylinder.
111 * This is used for ISADMA bouncebuffer allocation and sets the max
112 * xfersize we support.
113 *
114 * 2.88M format has 2 x 36 x 512, allow for hacked up density.
115 */
116#define MAX_BYTES_PER_CYL (2 * 40 * 512)
117
118/*
119 * Timeout value for the PIO loops to wait until the FDC main status
120 * register matches our expectations (request for master, direction
121 * bit). This is supposed to be a number of microseconds, although
122 * timing might actually not be very accurate.
123 *
124 * Timeouts of 100 msec are believed to be required for some broken
125 * (old) hardware.
126 */
127#define FDSTS_TIMEOUT 100000
128
129/*
130 * After this many errors, stop whining. Close will reset this count.
131 */
132#define FDC_ERRMAX 100
133
134/*
135 * AutoDensity search lists for each drive type.
136 */
137
138static struct fd_type fd_searchlist_360k[] = {
139 { FDF_5_360 },
140 { 0 }
141};
142
143static struct fd_type fd_searchlist_12m[] = {
144 { FDF_5_1200 | FL_AUTO },
145 { FDF_5_360 | FL_2STEP | FL_AUTO},
146 { 0 }
147};
148
149static struct fd_type fd_searchlist_720k[] = {
150 { FDF_3_720 },
151 { 0 }
152};
153
154static struct fd_type fd_searchlist_144m[] = {
155 { FDF_3_1440 | FL_AUTO},
156 { FDF_3_720 | FL_AUTO},
157 { 0 }
158};
159
160static struct fd_type fd_searchlist_288m[] = {
161 { FDF_3_1440 | FL_AUTO },
162#if 0
163 { FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
164#endif
165 { FDF_3_720 | FL_AUTO},
166 { 0 }
167};
168
169/*
170 * Order must match enum fd_drivetype in <sys/fdcio.h>.
171 */
172static struct fd_type *fd_native_types[] = {
173 NULL, /* FDT_NONE */
174 fd_searchlist_360k, /* FDT_360K */
175 fd_searchlist_12m, /* FDT_12M */
176 fd_searchlist_720k, /* FDT_720K */
177 fd_searchlist_144m, /* FDT_144M */
178 fd_searchlist_288m, /* FDT_288M_1 (mapped to FDT_288M) */
179 fd_searchlist_288m, /* FDT_288M */
180};
181
182/*
183 * Internals start here
184 */
185
186/* registers */
187#define FDOUT 2 /* Digital Output Register (W) */
188#define FDO_FDSEL 0x03 /* floppy device select */
189#define FDO_FRST 0x04 /* floppy controller reset */
190#define FDO_FDMAEN 0x08 /* enable floppy DMA and Interrupt */
191#define FDO_MOEN0 0x10 /* motor enable drive 0 */
192#define FDO_MOEN1 0x20 /* motor enable drive 1 */
193#define FDO_MOEN2 0x40 /* motor enable drive 2 */
194#define FDO_MOEN3 0x80 /* motor enable drive 3 */
195
196#define FDSTS 4 /* NEC 765 Main Status Register (R) */
197#define FDDSR 4 /* Data Rate Select Register (W) */
198#define FDDATA 5 /* NEC 765 Data Register (R/W) */
199#define FDCTL 7 /* Control Register (W) */
200
201/*
202 * The YE-DATA PC Card floppies use PIO to read in the data rather
203 * than DMA due to the wild variability of DMA for the PC Card
204 * devices. DMA was deleted from the PC Card specification in version
205 * 7.2 of the standard, but that post-dates the YE-DATA devices by many
206 * years.
207 *
208 * In addition, if we cannot setup the DMA resources for the ISA
209 * attachment, we'll use this same offset for data transfer. However,
210 * that almost certainly won't work.
211 *
212 * For this mode, offset 0 and 1 must be used to setup the transfer
213 * for this floppy. This is OK for PC Card YE Data devices, but for
214 * ISA this is likely wrong. These registers are only available on
215 * those systems that map them to the floppy drive. Newer systems do
216 * not do this, and we should likely prohibit access to them (or
217 * disallow NODMA to be set).
218 */
219#define FDBCDR 0 /* And 1 */
220#define FD_YE_DATAPORT 6 /* Drive Data port */
221
222#define FDI_DCHG 0x80 /* diskette has been changed */
223 /* requires drive and motor being selected */
224 /* is cleared by any step pulse to drive */
225
226/*
227 * We have three private BIO commands.
228 */
229#define BIO_PROBE BIO_CMD0
230#define BIO_RDID BIO_CMD1
231#define BIO_FMT BIO_CMD2
232
233/*
234 * Per drive structure (softc).
235 */
236struct fd_data {
237 u_char *fd_ioptr; /* IO pointer */
238 u_int fd_iosize; /* Size of IO chunks */
239 u_int fd_iocount; /* Outstanding requests */
240 struct fdc_data *fdc; /* pointer to controller structure */
241 int fdsu; /* this units number on this controller */
242 enum fd_drivetype type; /* drive type */
243 struct fd_type *ft; /* pointer to current type descriptor */
244 struct fd_type fts; /* type descriptors */
245 int sectorsize;
246 int flags;
247#define FD_WP (1<<0) /* Write protected */
248#define FD_MOTOR (1<<1) /* motor should be on */
249#define FD_MOTORWAIT (1<<2) /* motor should be on */
250#define FD_EMPTY (1<<3) /* no media */
251#define FD_NEWDISK (1<<4) /* media changed */
252#define FD_ISADMA (1<<5) /* isa dma started */
253 int track; /* where we think the head is */
254#define FD_NO_TRACK -2
255 int options; /* FDOPT_* */
256 struct callout toffhandle;
257 struct g_geom *fd_geom;
258 struct g_provider *fd_provider;
259 device_t dev;
260 struct bio_queue_head fd_bq;
261};
262
263#define FD_NOT_VALID -2
264
265static driver_intr_t fdc_intr;
266static driver_filter_t fdc_intr_fast;
267static void fdc_reset(struct fdc_data *);
268static int fd_probe_disk(struct fd_data *, int *);
266
267SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
268
269static int fifo_threshold = 8;
270SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
271 "FIFO threshold setting");
272
273static int debugflags = 0;
274SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
275 "Debug flags");
276
277static int retries = 10;
278SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
279 "Number of retries to attempt");
280
281static int spec1 = 0xaf;
282SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
283 "Specification byte one (step-rate + head unload)");
284
285static int spec2 = 0x10;
286SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
287 "Specification byte two (head load time + no-dma)");
288
289static int settle;
290SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
291 "Head settling time in sec/hz");
292
293static void
294fdprinttype(struct fd_type *ft)
295{
296
297 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
298 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
299 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
300 ft->offset_side2, ft->flags);
301}
302
303static void
304fdsettype(struct fd_data *fd, struct fd_type *ft)
305{
306 fd->ft = ft;
307 ft->size = ft->sectrac * ft->heads * ft->tracks;
308 fd->sectorsize = 128 << fd->ft->secsize;
309}
310
311/*
312 * Bus space handling (access to low-level IO).
313 */
314__inline static void
315fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
316{
317
318 bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
319}
320
321__inline static uint8_t
322fdregrd(struct fdc_data *fdc, int reg)
323{
324
325 return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
326}
327
328static void
329fdctl_wr(struct fdc_data *fdc, u_int8_t v)
330{
331
332 fdregwr(fdc, FDCTL, v);
333}
334
335static void
336fdout_wr(struct fdc_data *fdc, u_int8_t v)
337{
338
339 fdregwr(fdc, FDOUT, v);
340}
341
342static u_int8_t
343fdsts_rd(struct fdc_data *fdc)
344{
345
346 return fdregrd(fdc, FDSTS);
347}
348
349static void
350fddsr_wr(struct fdc_data *fdc, u_int8_t v)
351{
352
353 fdregwr(fdc, FDDSR, v);
354}
355
356static void
357fddata_wr(struct fdc_data *fdc, u_int8_t v)
358{
359
360 fdregwr(fdc, FDDATA, v);
361}
362
363static u_int8_t
364fddata_rd(struct fdc_data *fdc)
365{
366
367 return fdregrd(fdc, FDDATA);
368}
369
370static u_int8_t
371fdin_rd(struct fdc_data *fdc)
372{
373
374 return fdregrd(fdc, FDCTL);
375}
376
377/*
378 * Magic pseudo-DMA initialization for YE FDC. Sets count and
379 * direction.
380 */
381static void
382fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
383{
384 fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
385 fdregwr(fdc, FDBCDR + 1,
386 (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
387}
388
389static int
390fdc_err(struct fdc_data *fdc, const char *s)
391{
392 fdc->fdc_errs++;
393 if (s) {
394 if (fdc->fdc_errs < FDC_ERRMAX)
395 device_printf(fdc->fdc_dev, "%s", s);
396 else if (fdc->fdc_errs == FDC_ERRMAX)
397 device_printf(fdc->fdc_dev, "too many errors, not "
398 "logging any more\n");
399 }
400
401 return (1);
402}
403
404/*
405 * FDC IO functions, take care of the main status register, timeout
406 * in case the desired status bits are never set.
407 *
408 * These PIO loops initially start out with short delays between
409 * each iteration in the expectation that the required condition
410 * is usually met quickly, so it can be handled immediately.
411 */
412static int
413fdc_in(struct fdc_data *fdc, int *ptr)
414{
415 int i, j, step;
416
417 step = 1;
418 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
419 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
420 if (i == (NE7_DIO|NE7_RQM)) {
421 i = fddata_rd(fdc);
422 if (ptr)
423 *ptr = i;
424 return (0);
425 }
426 if (i == NE7_RQM)
427 return (fdc_err(fdc, "ready for output in input\n"));
428 step += step;
429 DELAY(step);
430 }
431 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
432}
433
434static int
435fdc_out(struct fdc_data *fdc, int x)
436{
437 int i, j, step;
438
439 step = 1;
440 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
441 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
442 if (i == NE7_RQM) {
443 fddata_wr(fdc, x);
444 return (0);
445 }
446 if (i == (NE7_DIO|NE7_RQM))
447 return (fdc_err(fdc, "ready for input in output\n"));
448 step += step;
449 DELAY(step);
450 }
451 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
452}
453
454/*
455 * fdc_cmd: Send a command to the chip.
456 * Takes a varargs with this structure:
457 * # of output bytes
458 * output bytes as int [...]
459 * # of input bytes
460 * input bytes as int* [...]
461 */
462static int
463fdc_cmd(struct fdc_data *fdc, int n_out, ...)
464{
465 u_char cmd = 0;
466 int n_in;
467 int n, i;
468 va_list ap;
469
470 va_start(ap, n_out);
471 for (n = 0; n < n_out; n++) {
472 i = va_arg(ap, int);
473 if (n == 0)
474 cmd = i;
475 if (fdc_out(fdc, i) < 0) {
476 char msg[50];
477 snprintf(msg, sizeof(msg),
478 "cmd %x failed at out byte %d of %d\n",
479 cmd, n + 1, n_out);
480 fdc->flags |= FDC_NEEDS_RESET;
481 va_end(ap);
482 return fdc_err(fdc, msg);
483 }
484 }
485 n_in = va_arg(ap, int);
486 for (n = 0; n < n_in; n++) {
487 int *ptr = va_arg(ap, int *);
488 if (fdc_in(fdc, ptr) < 0) {
489 char msg[50];
490 snprintf(msg, sizeof(msg),
491 "cmd %02x failed at in byte %d of %d\n",
492 cmd, n + 1, n_in);
493 fdc->flags |= FDC_NEEDS_RESET;
494 va_end(ap);
495 return fdc_err(fdc, msg);
496 }
497 }
498 va_end(ap);
499 return (0);
500}
501
502static void
503fdc_reset(struct fdc_data *fdc)
504{
505 int i, r[10];
506
507 if (fdc->fdct == FDC_ENHANCED) {
508 /* Try a software reset, default precomp, and 500 kb/s */
509 fddsr_wr(fdc, I8207X_DSR_SR);
510 } else {
511 /* Try a hardware reset, keep motor on */
512 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
513 DELAY(100);
514 /* enable FDC, but defer interrupts a moment */
515 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
516 }
517 DELAY(100);
518 fdout_wr(fdc, fdc->fdout);
519
520 /* XXX after a reset, silently believe the FDC will accept commands */
521 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
522 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
523
524 if (fdc->fdct == FDC_ENHANCED) {
525 if (fdc_cmd(fdc, 4,
526 I8207X_CONFIG,
527 0,
528 0x40 | /* Enable Implied Seek */
529 0x10 | /* Polling disabled */
530 (fifo_threshold - 1), /* Fifo threshold */
531 0x00, /* Precomp track */
532 0))
533 device_printf(fdc->fdc_dev,
534 " CONFIGURE failed in reset\n");
535 if (debugflags & 1) {
536 if (fdc_cmd(fdc, 1,
537 I8207X_DUMPREG,
538 10, &r[0], &r[1], &r[2], &r[3], &r[4],
539 &r[5], &r[6], &r[7], &r[8], &r[9]))
540 device_printf(fdc->fdc_dev,
541 " DUMPREG failed in reset\n");
542 for (i = 0; i < 10; i++)
543 printf(" %02x", r[i]);
544 printf("\n");
545 }
546 }
547}
548
549static int
550fdc_sense_drive(struct fdc_data *fdc, int *st3p)
551{
552 int st3;
553
554 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
555 return (fdc_err(fdc, "Sense Drive Status failed\n"));
556 if (st3p)
557 *st3p = st3;
558 return (0);
559}
560
561static int
562fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
563{
564 int cyl, st0, ret;
565
566 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
567 if (ret) {
568 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
569 return (ret);
570 }
571
572 if (st0p)
573 *st0p = st0;
574
575 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
576 /*
577 * There doesn't seem to have been an interrupt.
578 */
579 return (FD_NOT_VALID);
580 }
581
582 if (fdc_in(fdc, &cyl) < 0)
583 return fdc_err(fdc, "can't get cyl num\n");
584
585 if (cylp)
586 *cylp = cyl;
587
588 return (0);
589}
590
591static int
592fdc_read_status(struct fdc_data *fdc)
593{
594 int i, ret, status;
595
596 for (i = ret = 0; i < 7; i++) {
597 ret = fdc_in(fdc, &status);
598 fdc->status[i] = status;
599 if (ret != 0)
600 break;
601 }
602
603 if (ret == 0)
604 fdc->flags |= FDC_STAT_VALID;
605 else
606 fdc->flags &= ~FDC_STAT_VALID;
607
608 return ret;
609}
610
611/*
612 * Select this drive
613 */
614static void
615fd_select(struct fd_data *fd)
616{
617 struct fdc_data *fdc;
618
619 /* XXX: lock controller */
620 fdc = fd->fdc;
621 fdc->fdout &= ~FDO_FDSEL;
622 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
623 fdout_wr(fdc, fdc->fdout);
624}
625
626static void
627fd_turnon(void *arg)
628{
629 struct fd_data *fd;
630 struct bio *bp;
631 int once;
632
633 fd = arg;
634 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
635 fd->flags &= ~FD_MOTORWAIT;
636 fd->flags |= FD_MOTOR;
637 once = 0;
638 for (;;) {
639 bp = bioq_takefirst(&fd->fd_bq);
640 if (bp == NULL)
641 break;
642 bioq_disksort(&fd->fdc->head, bp);
643 once = 1;
644 }
645 if (once)
646 wakeup(&fd->fdc->head);
647}
648
649static void
650fd_motor(struct fd_data *fd, int turnon)
651{
652 struct fdc_data *fdc;
653
654 fdc = fd->fdc;
655/*
656 mtx_assert(&fdc->fdc_mtx, MA_OWNED);
657*/
658 if (turnon) {
659 fd->flags |= FD_MOTORWAIT;
660 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
661 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
662 } else {
663 callout_stop(&fd->toffhandle);
664 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
665 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
666 }
667 fdout_wr(fdc, fdc->fdout);
668}
669
670static void
671fd_turnoff(void *xfd)
672{
673 struct fd_data *fd = xfd;
674
675 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
676 fd_motor(fd, 0);
677}
678
679/*
680 * fdc_intr - wake up the worker thread.
681 */
682
683static void
684fdc_intr(void *arg)
685{
686
687 wakeup(arg);
688}
689
690static int
691fdc_intr_fast(void *arg)
692{
693
694 wakeup(arg);
695 return(FILTER_HANDLED);
696}
697
698/*
699 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
700 */
701static void
702fdc_pio(struct fdc_data *fdc)
703{
704 u_char *cptr;
705 struct bio *bp;
706 u_int count;
707
708 bp = fdc->bp;
709 cptr = fdc->fd->fd_ioptr;
710 count = fdc->fd->fd_iosize;
711
712 if (bp->bio_cmd == BIO_READ) {
713 fdbcdr_wr(fdc, 0, count);
714 bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
715 fdc->ioff[FD_YE_DATAPORT], cptr, count);
716 } else {
717 bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
718 fdc->ioff[FD_YE_DATAPORT], cptr, count);
719 fdbcdr_wr(fdc, 0, count); /* needed? */
720 }
721}
722
723static int
724fdc_biodone(struct fdc_data *fdc, int error)
725{
726 struct fd_data *fd;
727 struct bio *bp;
728
729 fd = fdc->fd;
730 bp = fdc->bp;
731
732 mtx_lock(&fdc->fdc_mtx);
733 if (--fd->fd_iocount == 0)
734 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
735 fdc->bp = NULL;
736 fdc->fd = NULL;
737 mtx_unlock(&fdc->fdc_mtx);
738 if (bp->bio_to != NULL) {
739 if ((debugflags & 2) && fd->fdc->retry > 0)
740 printf("retries: %d\n", fd->fdc->retry);
741 g_io_deliver(bp, error);
742 return (0);
743 }
744 bp->bio_error = error;
745 bp->bio_flags |= BIO_DONE;
746 wakeup(bp);
747 return (0);
748}
749
750static int retry_line;
751
752static int
753fdc_worker(struct fdc_data *fdc)
754{
755 struct fd_data *fd;
756 struct bio *bp;
757 int i, nsect;
758 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
759 int head;
760 static int need_recal;
761 struct fdc_readid *idp;
762 struct fd_formb *finfo;
763
764 /* Have we exhausted our retries ? */
765 bp = fdc->bp;
766 fd = fdc->fd;
767 if (bp != NULL &&
768 (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
769 if ((debugflags & 4))
770 printf("Too many retries (EIO)\n");
269
270SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW, 0, "fdc driver");
271
272static int fifo_threshold = 8;
273SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
274 "FIFO threshold setting");
275
276static int debugflags = 0;
277SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
278 "Debug flags");
279
280static int retries = 10;
281SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
282 "Number of retries to attempt");
283
284static int spec1 = 0xaf;
285SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
286 "Specification byte one (step-rate + head unload)");
287
288static int spec2 = 0x10;
289SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
290 "Specification byte two (head load time + no-dma)");
291
292static int settle;
293SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
294 "Head settling time in sec/hz");
295
296static void
297fdprinttype(struct fd_type *ft)
298{
299
300 printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
301 ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
302 ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
303 ft->offset_side2, ft->flags);
304}
305
306static void
307fdsettype(struct fd_data *fd, struct fd_type *ft)
308{
309 fd->ft = ft;
310 ft->size = ft->sectrac * ft->heads * ft->tracks;
311 fd->sectorsize = 128 << fd->ft->secsize;
312}
313
314/*
315 * Bus space handling (access to low-level IO).
316 */
317__inline static void
318fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
319{
320
321 bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
322}
323
324__inline static uint8_t
325fdregrd(struct fdc_data *fdc, int reg)
326{
327
328 return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
329}
330
331static void
332fdctl_wr(struct fdc_data *fdc, u_int8_t v)
333{
334
335 fdregwr(fdc, FDCTL, v);
336}
337
338static void
339fdout_wr(struct fdc_data *fdc, u_int8_t v)
340{
341
342 fdregwr(fdc, FDOUT, v);
343}
344
345static u_int8_t
346fdsts_rd(struct fdc_data *fdc)
347{
348
349 return fdregrd(fdc, FDSTS);
350}
351
352static void
353fddsr_wr(struct fdc_data *fdc, u_int8_t v)
354{
355
356 fdregwr(fdc, FDDSR, v);
357}
358
359static void
360fddata_wr(struct fdc_data *fdc, u_int8_t v)
361{
362
363 fdregwr(fdc, FDDATA, v);
364}
365
366static u_int8_t
367fddata_rd(struct fdc_data *fdc)
368{
369
370 return fdregrd(fdc, FDDATA);
371}
372
373static u_int8_t
374fdin_rd(struct fdc_data *fdc)
375{
376
377 return fdregrd(fdc, FDCTL);
378}
379
380/*
381 * Magic pseudo-DMA initialization for YE FDC. Sets count and
382 * direction.
383 */
384static void
385fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
386{
387 fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
388 fdregwr(fdc, FDBCDR + 1,
389 (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
390}
391
392static int
393fdc_err(struct fdc_data *fdc, const char *s)
394{
395 fdc->fdc_errs++;
396 if (s) {
397 if (fdc->fdc_errs < FDC_ERRMAX)
398 device_printf(fdc->fdc_dev, "%s", s);
399 else if (fdc->fdc_errs == FDC_ERRMAX)
400 device_printf(fdc->fdc_dev, "too many errors, not "
401 "logging any more\n");
402 }
403
404 return (1);
405}
406
407/*
408 * FDC IO functions, take care of the main status register, timeout
409 * in case the desired status bits are never set.
410 *
411 * These PIO loops initially start out with short delays between
412 * each iteration in the expectation that the required condition
413 * is usually met quickly, so it can be handled immediately.
414 */
415static int
416fdc_in(struct fdc_data *fdc, int *ptr)
417{
418 int i, j, step;
419
420 step = 1;
421 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
422 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
423 if (i == (NE7_DIO|NE7_RQM)) {
424 i = fddata_rd(fdc);
425 if (ptr)
426 *ptr = i;
427 return (0);
428 }
429 if (i == NE7_RQM)
430 return (fdc_err(fdc, "ready for output in input\n"));
431 step += step;
432 DELAY(step);
433 }
434 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
435}
436
437static int
438fdc_out(struct fdc_data *fdc, int x)
439{
440 int i, j, step;
441
442 step = 1;
443 for (j = 0; j < FDSTS_TIMEOUT; j += step) {
444 i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
445 if (i == NE7_RQM) {
446 fddata_wr(fdc, x);
447 return (0);
448 }
449 if (i == (NE7_DIO|NE7_RQM))
450 return (fdc_err(fdc, "ready for input in output\n"));
451 step += step;
452 DELAY(step);
453 }
454 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
455}
456
457/*
458 * fdc_cmd: Send a command to the chip.
459 * Takes a varargs with this structure:
460 * # of output bytes
461 * output bytes as int [...]
462 * # of input bytes
463 * input bytes as int* [...]
464 */
465static int
466fdc_cmd(struct fdc_data *fdc, int n_out, ...)
467{
468 u_char cmd = 0;
469 int n_in;
470 int n, i;
471 va_list ap;
472
473 va_start(ap, n_out);
474 for (n = 0; n < n_out; n++) {
475 i = va_arg(ap, int);
476 if (n == 0)
477 cmd = i;
478 if (fdc_out(fdc, i) < 0) {
479 char msg[50];
480 snprintf(msg, sizeof(msg),
481 "cmd %x failed at out byte %d of %d\n",
482 cmd, n + 1, n_out);
483 fdc->flags |= FDC_NEEDS_RESET;
484 va_end(ap);
485 return fdc_err(fdc, msg);
486 }
487 }
488 n_in = va_arg(ap, int);
489 for (n = 0; n < n_in; n++) {
490 int *ptr = va_arg(ap, int *);
491 if (fdc_in(fdc, ptr) < 0) {
492 char msg[50];
493 snprintf(msg, sizeof(msg),
494 "cmd %02x failed at in byte %d of %d\n",
495 cmd, n + 1, n_in);
496 fdc->flags |= FDC_NEEDS_RESET;
497 va_end(ap);
498 return fdc_err(fdc, msg);
499 }
500 }
501 va_end(ap);
502 return (0);
503}
504
505static void
506fdc_reset(struct fdc_data *fdc)
507{
508 int i, r[10];
509
510 if (fdc->fdct == FDC_ENHANCED) {
511 /* Try a software reset, default precomp, and 500 kb/s */
512 fddsr_wr(fdc, I8207X_DSR_SR);
513 } else {
514 /* Try a hardware reset, keep motor on */
515 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
516 DELAY(100);
517 /* enable FDC, but defer interrupts a moment */
518 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
519 }
520 DELAY(100);
521 fdout_wr(fdc, fdc->fdout);
522
523 /* XXX after a reset, silently believe the FDC will accept commands */
524 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
525 device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
526
527 if (fdc->fdct == FDC_ENHANCED) {
528 if (fdc_cmd(fdc, 4,
529 I8207X_CONFIG,
530 0,
531 0x40 | /* Enable Implied Seek */
532 0x10 | /* Polling disabled */
533 (fifo_threshold - 1), /* Fifo threshold */
534 0x00, /* Precomp track */
535 0))
536 device_printf(fdc->fdc_dev,
537 " CONFIGURE failed in reset\n");
538 if (debugflags & 1) {
539 if (fdc_cmd(fdc, 1,
540 I8207X_DUMPREG,
541 10, &r[0], &r[1], &r[2], &r[3], &r[4],
542 &r[5], &r[6], &r[7], &r[8], &r[9]))
543 device_printf(fdc->fdc_dev,
544 " DUMPREG failed in reset\n");
545 for (i = 0; i < 10; i++)
546 printf(" %02x", r[i]);
547 printf("\n");
548 }
549 }
550}
551
552static int
553fdc_sense_drive(struct fdc_data *fdc, int *st3p)
554{
555 int st3;
556
557 if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
558 return (fdc_err(fdc, "Sense Drive Status failed\n"));
559 if (st3p)
560 *st3p = st3;
561 return (0);
562}
563
564static int
565fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
566{
567 int cyl, st0, ret;
568
569 ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
570 if (ret) {
571 (void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
572 return (ret);
573 }
574
575 if (st0p)
576 *st0p = st0;
577
578 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
579 /*
580 * There doesn't seem to have been an interrupt.
581 */
582 return (FD_NOT_VALID);
583 }
584
585 if (fdc_in(fdc, &cyl) < 0)
586 return fdc_err(fdc, "can't get cyl num\n");
587
588 if (cylp)
589 *cylp = cyl;
590
591 return (0);
592}
593
594static int
595fdc_read_status(struct fdc_data *fdc)
596{
597 int i, ret, status;
598
599 for (i = ret = 0; i < 7; i++) {
600 ret = fdc_in(fdc, &status);
601 fdc->status[i] = status;
602 if (ret != 0)
603 break;
604 }
605
606 if (ret == 0)
607 fdc->flags |= FDC_STAT_VALID;
608 else
609 fdc->flags &= ~FDC_STAT_VALID;
610
611 return ret;
612}
613
614/*
615 * Select this drive
616 */
617static void
618fd_select(struct fd_data *fd)
619{
620 struct fdc_data *fdc;
621
622 /* XXX: lock controller */
623 fdc = fd->fdc;
624 fdc->fdout &= ~FDO_FDSEL;
625 fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
626 fdout_wr(fdc, fdc->fdout);
627}
628
629static void
630fd_turnon(void *arg)
631{
632 struct fd_data *fd;
633 struct bio *bp;
634 int once;
635
636 fd = arg;
637 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
638 fd->flags &= ~FD_MOTORWAIT;
639 fd->flags |= FD_MOTOR;
640 once = 0;
641 for (;;) {
642 bp = bioq_takefirst(&fd->fd_bq);
643 if (bp == NULL)
644 break;
645 bioq_disksort(&fd->fdc->head, bp);
646 once = 1;
647 }
648 if (once)
649 wakeup(&fd->fdc->head);
650}
651
652static void
653fd_motor(struct fd_data *fd, int turnon)
654{
655 struct fdc_data *fdc;
656
657 fdc = fd->fdc;
658/*
659 mtx_assert(&fdc->fdc_mtx, MA_OWNED);
660*/
661 if (turnon) {
662 fd->flags |= FD_MOTORWAIT;
663 fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
664 callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
665 } else {
666 callout_stop(&fd->toffhandle);
667 fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
668 fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
669 }
670 fdout_wr(fdc, fdc->fdout);
671}
672
673static void
674fd_turnoff(void *xfd)
675{
676 struct fd_data *fd = xfd;
677
678 mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
679 fd_motor(fd, 0);
680}
681
682/*
683 * fdc_intr - wake up the worker thread.
684 */
685
686static void
687fdc_intr(void *arg)
688{
689
690 wakeup(arg);
691}
692
693static int
694fdc_intr_fast(void *arg)
695{
696
697 wakeup(arg);
698 return(FILTER_HANDLED);
699}
700
701/*
702 * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
703 */
704static void
705fdc_pio(struct fdc_data *fdc)
706{
707 u_char *cptr;
708 struct bio *bp;
709 u_int count;
710
711 bp = fdc->bp;
712 cptr = fdc->fd->fd_ioptr;
713 count = fdc->fd->fd_iosize;
714
715 if (bp->bio_cmd == BIO_READ) {
716 fdbcdr_wr(fdc, 0, count);
717 bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
718 fdc->ioff[FD_YE_DATAPORT], cptr, count);
719 } else {
720 bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
721 fdc->ioff[FD_YE_DATAPORT], cptr, count);
722 fdbcdr_wr(fdc, 0, count); /* needed? */
723 }
724}
725
726static int
727fdc_biodone(struct fdc_data *fdc, int error)
728{
729 struct fd_data *fd;
730 struct bio *bp;
731
732 fd = fdc->fd;
733 bp = fdc->bp;
734
735 mtx_lock(&fdc->fdc_mtx);
736 if (--fd->fd_iocount == 0)
737 callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
738 fdc->bp = NULL;
739 fdc->fd = NULL;
740 mtx_unlock(&fdc->fdc_mtx);
741 if (bp->bio_to != NULL) {
742 if ((debugflags & 2) && fd->fdc->retry > 0)
743 printf("retries: %d\n", fd->fdc->retry);
744 g_io_deliver(bp, error);
745 return (0);
746 }
747 bp->bio_error = error;
748 bp->bio_flags |= BIO_DONE;
749 wakeup(bp);
750 return (0);
751}
752
753static int retry_line;
754
755static int
756fdc_worker(struct fdc_data *fdc)
757{
758 struct fd_data *fd;
759 struct bio *bp;
760 int i, nsect;
761 int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
762 int head;
763 static int need_recal;
764 struct fdc_readid *idp;
765 struct fd_formb *finfo;
766
767 /* Have we exhausted our retries ? */
768 bp = fdc->bp;
769 fd = fdc->fd;
770 if (bp != NULL &&
771 (fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
772 if ((debugflags & 4))
773 printf("Too many retries (EIO)\n");
771 mtx_lock(&fdc->fdc_mtx);
772 fd->flags |= FD_EMPTY;
773 mtx_unlock(&fdc->fdc_mtx);
774 if (fdc->flags & FDC_NEEDS_RESET) {
775 mtx_lock(&fdc->fdc_mtx);
776 fd->flags |= FD_EMPTY;
777 mtx_unlock(&fdc->fdc_mtx);
778 }
774 return (fdc_biodone(fdc, EIO));
775 }
776
777 /* Disable ISADMA if we bailed while it was active */
778 if (fd != NULL && (fd->flags & FD_ISADMA)) {
779 mtx_lock(&Giant);
780 isa_dmadone(
781 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
782 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
783 mtx_unlock(&Giant);
784 mtx_lock(&fdc->fdc_mtx);
785 fd->flags &= ~FD_ISADMA;
786 mtx_unlock(&fdc->fdc_mtx);
787 }
788
789 /* Unwedge the controller ? */
790 if (fdc->flags & FDC_NEEDS_RESET) {
791 fdc->flags &= ~FDC_NEEDS_RESET;
792 fdc_reset(fdc);
793 tsleep(fdc, PRIBIO, "fdcrst", hz);
794 /* Discard results */
795 for (i = 0; i < 4; i++)
796 fdc_sense_int(fdc, &st0, &cyl);
797 /* All drives must recal */
798 need_recal = 0xf;
799 }
800
801 /* Pick up a request, if need be wait for it */
802 if (fdc->bp == NULL) {
803 mtx_lock(&fdc->fdc_mtx);
804 do {
805 fdc->bp = bioq_takefirst(&fdc->head);
806 if (fdc->bp == NULL)
807 msleep(&fdc->head, &fdc->fdc_mtx,
808 PRIBIO, "-", hz);
809 } while (fdc->bp == NULL &&
810 (fdc->flags & FDC_KTHREAD_EXIT) == 0);
811 mtx_unlock(&fdc->fdc_mtx);
812
813 if (fdc->bp == NULL)
814 /*
815 * Nothing to do, worker thread has been
816 * requested to stop.
817 */
818 return (0);
819
820 bp = fdc->bp;
821 fd = fdc->fd = bp->bio_driver1;
822 fdc->retry = 0;
823 fd->fd_ioptr = bp->bio_data;
824 if (bp->bio_cmd & BIO_FMT) {
825 i = offsetof(struct fd_formb, fd_formb_cylno(0));
826 fd->fd_ioptr += i;
827 fd->fd_iosize = bp->bio_length - i;
828 }
829 }
830
831 /* Select drive, setup params */
832 fd_select(fd);
833 if (fdc->fdct == FDC_ENHANCED)
834 fddsr_wr(fdc, fd->ft->trans);
835 else
836 fdctl_wr(fdc, fd->ft->trans);
837
838 if (bp->bio_cmd & BIO_PROBE) {
779 return (fdc_biodone(fdc, EIO));
780 }
781
782 /* Disable ISADMA if we bailed while it was active */
783 if (fd != NULL && (fd->flags & FD_ISADMA)) {
784 mtx_lock(&Giant);
785 isa_dmadone(
786 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
787 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
788 mtx_unlock(&Giant);
789 mtx_lock(&fdc->fdc_mtx);
790 fd->flags &= ~FD_ISADMA;
791 mtx_unlock(&fdc->fdc_mtx);
792 }
793
794 /* Unwedge the controller ? */
795 if (fdc->flags & FDC_NEEDS_RESET) {
796 fdc->flags &= ~FDC_NEEDS_RESET;
797 fdc_reset(fdc);
798 tsleep(fdc, PRIBIO, "fdcrst", hz);
799 /* Discard results */
800 for (i = 0; i < 4; i++)
801 fdc_sense_int(fdc, &st0, &cyl);
802 /* All drives must recal */
803 need_recal = 0xf;
804 }
805
806 /* Pick up a request, if need be wait for it */
807 if (fdc->bp == NULL) {
808 mtx_lock(&fdc->fdc_mtx);
809 do {
810 fdc->bp = bioq_takefirst(&fdc->head);
811 if (fdc->bp == NULL)
812 msleep(&fdc->head, &fdc->fdc_mtx,
813 PRIBIO, "-", hz);
814 } while (fdc->bp == NULL &&
815 (fdc->flags & FDC_KTHREAD_EXIT) == 0);
816 mtx_unlock(&fdc->fdc_mtx);
817
818 if (fdc->bp == NULL)
819 /*
820 * Nothing to do, worker thread has been
821 * requested to stop.
822 */
823 return (0);
824
825 bp = fdc->bp;
826 fd = fdc->fd = bp->bio_driver1;
827 fdc->retry = 0;
828 fd->fd_ioptr = bp->bio_data;
829 if (bp->bio_cmd & BIO_FMT) {
830 i = offsetof(struct fd_formb, fd_formb_cylno(0));
831 fd->fd_ioptr += i;
832 fd->fd_iosize = bp->bio_length - i;
833 }
834 }
835
836 /* Select drive, setup params */
837 fd_select(fd);
838 if (fdc->fdct == FDC_ENHANCED)
839 fddsr_wr(fdc, fd->ft->trans);
840 else
841 fdctl_wr(fdc, fd->ft->trans);
842
843 if (bp->bio_cmd & BIO_PROBE) {
839
840 if (!(fdin_rd(fdc) & FDI_DCHG) && !(fd->flags & FD_EMPTY))
844 if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
845 !(fdin_rd(fdc) & FDI_DCHG) &&
846 !(fd->flags & FD_EMPTY)) ||
847 fd_probe_disk(fd, &need_recal) == 0)
841 return (fdc_biodone(fdc, 0));
848 return (fdc_biodone(fdc, 0));
842
843 /*
844 * Try to find out if we have a disk in the drive
845 *
846 * First recal, then seek to cyl#1, this clears the
847 * old condition on the disk change line so we can
848 * examine it for current status
849 */
850 if (debugflags & 0x40)
851 printf("New disk in probe\n");
852 mtx_lock(&fdc->fdc_mtx);
853 fd->flags |= FD_NEWDISK;
854 mtx_unlock(&fdc->fdc_mtx);
855 retry_line = __LINE__;
856 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
857 return (1);
858 tsleep(fdc, PRIBIO, "fdrecal", hz);
859 retry_line = __LINE__;
860 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
861 return (1); /* XXX */
862 retry_line = __LINE__;
863 if ((st0 & 0xc0) || cyl != 0)
864 return (1);
865
866 /* Seek to track 1 */
867 retry_line = __LINE__;
868 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
869 return (1);
870 tsleep(fdc, PRIBIO, "fdseek", hz);
871 retry_line = __LINE__;
872 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
873 return (1); /* XXX */
874 need_recal |= (1 << fd->fdsu);
875 if (fdin_rd(fdc) & FDI_DCHG) {
876 if (debugflags & 0x40)
877 printf("Empty in probe\n");
878 mtx_lock(&fdc->fdc_mtx);
879 fd->flags |= FD_EMPTY;
880 mtx_unlock(&fdc->fdc_mtx);
881 } else {
882 if (debugflags & 0x40)
883 printf("Got disk in probe\n");
884 mtx_lock(&fdc->fdc_mtx);
885 fd->flags &= ~FD_EMPTY;
886 mtx_unlock(&fdc->fdc_mtx);
887 retry_line = __LINE__;
888 if(fdc_sense_drive(fdc, &st3) != 0)
889 return (1);
890 mtx_lock(&fdc->fdc_mtx);
891 if(st3 & NE7_ST3_WP)
892 fd->flags |= FD_WP;
893 else
894 fd->flags &= ~FD_WP;
895 mtx_unlock(&fdc->fdc_mtx);
896 }
897 return (fdc_biodone(fdc, 0));
849 return (1);
898 }
899
900 /*
901 * If we are dead just flush the requests
902 */
903 if (fd->flags & FD_EMPTY)
904 return (fdc_biodone(fdc, ENXIO));
905
906 /* Check if we lost our media */
907 if (fdin_rd(fdc) & FDI_DCHG) {
908 if (debugflags & 0x40)
909 printf("Lost disk\n");
910 mtx_lock(&fdc->fdc_mtx);
911 fd->flags |= FD_EMPTY;
912 fd->flags |= FD_NEWDISK;
913 mtx_unlock(&fdc->fdc_mtx);
914 g_topology_lock();
915 g_orphan_provider(fd->fd_provider, EXDEV);
916 fd->fd_provider->flags |= G_PF_WITHER;
917 fd->fd_provider =
918 g_new_providerf(fd->fd_geom, fd->fd_geom->name);
919 g_error_provider(fd->fd_provider, 0);
920 g_topology_unlock();
921 return (fdc_biodone(fdc, ENXIO));
922 }
923
924 /* Check if the floppy is write-protected */
925 if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
926 retry_line = __LINE__;
927 if(fdc_sense_drive(fdc, &st3) != 0)
928 return (1);
929 if(st3 & NE7_ST3_WP)
930 return (fdc_biodone(fdc, EROFS));
931 }
932
933 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
934 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
935 i = fd->ft->sectrac * fd->ft->heads;
936 cylinder = bp->bio_pblkno / i;
937 descyl = cylinder * steptrac;
938 sec = bp->bio_pblkno % i;
939 nsect = i - sec;
940 head = sec / fd->ft->sectrac;
941 sec = sec % fd->ft->sectrac + 1;
942
943 /* If everything is going swimmingly, use multisector xfer */
944 if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
945 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
946 nsect = fd->fd_iosize / fd->sectorsize;
947 } else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
948 fd->fd_iosize = fd->sectorsize;
949 nsect = 1;
950 }
951
952 /* Do RECAL if we need to or are going to track zero anyway */
953 if ((need_recal & (1 << fd->fdsu)) ||
954 (cylinder == 0 && fd->track != 0) ||
955 fdc->retry > 2) {
956 retry_line = __LINE__;
957 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
958 return (1);
959 tsleep(fdc, PRIBIO, "fdrecal", hz);
960 retry_line = __LINE__;
961 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
962 return (1); /* XXX */
963 retry_line = __LINE__;
964 if ((st0 & 0xc0) || cyl != 0)
965 return (1);
966 need_recal &= ~(1 << fd->fdsu);
967 fd->track = 0;
968 /* let the heads settle */
969 if (settle)
970 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
971 }
972
973 /*
974 * SEEK to where we want to be
975 *
976 * Enhanced controllers do implied seeks for read&write as long as
977 * we do not need multiple steps per track.
978 */
979 if (cylinder != fd->track && (
980 fdc->fdct != FDC_ENHANCED ||
981 descyl != cylinder ||
982 (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
983 retry_line = __LINE__;
984 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
985 return (1);
986 tsleep(fdc, PRIBIO, "fdseek", hz);
987 retry_line = __LINE__;
988 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
989 return (1); /* XXX */
990 retry_line = __LINE__;
991 if ((st0 & 0xc0) || cyl != descyl) {
992 need_recal |= (1 << fd->fdsu);
993 return (1);
994 }
995 /* let the heads settle */
996 if (settle)
997 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
998 }
999 fd->track = cylinder;
1000
1001 if (debugflags & 8)
1002 printf("op %x bn %ju siz %u ptr %p retry %d\n",
1003 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
1004 fd->fd_ioptr, fdc->retry);
1005
1006 /* Setup ISADMA if we need it and have it */
1007 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
1008 && !(fdc->flags & FDC_NODMA)) {
1009 mtx_lock(&Giant);
1010 isa_dmastart(
1011 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1012 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1013 mtx_unlock(&Giant);
1014 mtx_lock(&fdc->fdc_mtx);
1015 fd->flags |= FD_ISADMA;
1016 mtx_unlock(&fdc->fdc_mtx);
1017 }
1018
1019 /* Do PIO if we have to */
1020 if (fdc->flags & FDC_NODMA) {
1021 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
1022 fdbcdr_wr(fdc, 1, fd->fd_iosize);
1023 if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
1024 fdc_pio(fdc);
1025 }
1026
1027 switch(bp->bio_cmd) {
1028 case BIO_FMT:
1029 /* formatting */
1030 finfo = (struct fd_formb *)bp->bio_data;
1031 retry_line = __LINE__;
1032 if (fdc_cmd(fdc, 6,
1033 NE7CMD_FORMAT | mfm,
1034 head << 2 | fd->fdsu,
1035 finfo->fd_formb_secshift,
1036 finfo->fd_formb_nsecs,
1037 finfo->fd_formb_gaplen,
1038 finfo->fd_formb_fillbyte, 0))
1039 return (1);
1040 break;
1041 case BIO_RDID:
1042 retry_line = __LINE__;
1043 if (fdc_cmd(fdc, 2,
1044 NE7CMD_READID | mfm,
1045 head << 2 | fd->fdsu, 0))
1046 return (1);
1047 break;
1048 case BIO_READ:
1049 retry_line = __LINE__;
1050 if (fdc_cmd(fdc, 9,
1051 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1052 head << 2 | fd->fdsu, /* head & unit */
1053 fd->track, /* track */
1054 head, /* head */
1055 sec, /* sector + 1 */
1056 fd->ft->secsize, /* sector size */
1057 fd->ft->sectrac, /* sectors/track */
1058 fd->ft->gap, /* gap size */
1059 fd->ft->datalen, /* data length */
1060 0))
1061 return (1);
1062 break;
1063 case BIO_WRITE:
1064 retry_line = __LINE__;
1065 if (fdc_cmd(fdc, 9,
1066 NE7CMD_WRITE | mfm | NE7CMD_MT,
1067 head << 2 | fd->fdsu, /* head & unit */
1068 fd->track, /* track */
1069 head, /* head */
1070 sec, /* sector + 1 */
1071 fd->ft->secsize, /* sector size */
1072 fd->ft->sectrac, /* sectors/track */
1073 fd->ft->gap, /* gap size */
1074 fd->ft->datalen, /* data length */
1075 0))
1076 return (1);
1077 break;
1078 default:
1079 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1080 }
1081
1082 /* Wait for interrupt */
1083 i = tsleep(fdc, PRIBIO, "fddata", hz);
1084
1085 /* PIO if the read looks good */
1086 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
1087 fdc_pio(fdc);
1088
1089 /* Finish DMA */
1090 if (fd->flags & FD_ISADMA) {
1091 mtx_lock(&Giant);
1092 isa_dmadone(
1093 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1094 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1095 mtx_unlock(&Giant);
1096 mtx_lock(&fdc->fdc_mtx);
1097 fd->flags &= ~FD_ISADMA;
1098 mtx_unlock(&fdc->fdc_mtx);
1099 }
1100
1101 if (i != 0) {
1102 /*
1103 * Timeout.
1104 *
1105 * Due to IBM's brain-dead design, the FDC has a faked ready
1106 * signal, hardwired to ready == true. Thus, any command
1107 * issued if there's no diskette in the drive will _never_
1108 * complete, and must be aborted by resetting the FDC.
1109 * Many thanks, Big Blue!
1110 */
1111 retry_line = __LINE__;
1112 fdc->flags |= FDC_NEEDS_RESET;
1113 return (1);
1114 }
1115
1116 retry_line = __LINE__;
1117 if (fdc_read_status(fdc))
1118 return (1);
1119
1120 if (debugflags & 0x10)
1121 printf(" -> %x %x %x %x\n",
1122 fdc->status[0], fdc->status[1],
1123 fdc->status[2], fdc->status[3]);
1124
1125 st0 = fdc->status[0] & NE7_ST0_IC;
1126 if (st0 != 0) {
1127 retry_line = __LINE__;
1128 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1129 /*
1130 * DMA overrun. Someone hogged the bus and
1131 * didn't release it in time for the next
1132 * FDC transfer.
1133 */
1134 return (1);
1135 }
1136 retry_line = __LINE__;
1137 if(st0 == NE7_ST0_IC_IV) {
1138 fdc->flags |= FDC_NEEDS_RESET;
1139 return (1);
1140 }
1141 retry_line = __LINE__;
1142 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1143 need_recal |= (1 << fd->fdsu);
1144 return (1);
1145 }
1146 if (debugflags & 0x20) {
1147 printf("status %02x %02x %02x %02x %02x %02x\n",
1148 fdc->status[0], fdc->status[1], fdc->status[2],
1149 fdc->status[3], fdc->status[4], fdc->status[5]);
1150 }
1151 retry_line = __LINE__;
1152 return (1);
1153 }
1154 /* All OK */
1155 switch(bp->bio_cmd) {
1156 case BIO_RDID:
1157 /* copy out ID field contents */
1158 idp = (struct fdc_readid *)bp->bio_data;
1159 idp->cyl = fdc->status[3];
1160 idp->head = fdc->status[4];
1161 idp->sec = fdc->status[5];
1162 idp->secshift = fdc->status[6];
1163 if (debugflags & 0x40)
1164 printf("c %d h %d s %d z %d\n",
1165 idp->cyl, idp->head, idp->sec, idp->secshift);
1166 break;
1167 case BIO_READ:
1168 case BIO_WRITE:
1169 bp->bio_pblkno += nsect;
1170 bp->bio_resid -= fd->fd_iosize;
1171 bp->bio_completed += fd->fd_iosize;
1172 fd->fd_ioptr += fd->fd_iosize;
1173 /* Since we managed to get something done, reset the retry */
1174 fdc->retry = 0;
1175 if (bp->bio_resid > 0)
1176 return (0);
1177 break;
1178 case BIO_FMT:
1179 break;
1180 }
1181 return (fdc_biodone(fdc, 0));
1182}
1183
1184static void
1185fdc_thread(void *arg)
1186{
1187 struct fdc_data *fdc;
1188
1189 fdc = arg;
1190 int i;
1191
1192 mtx_lock(&fdc->fdc_mtx);
1193 fdc->flags |= FDC_KTHREAD_ALIVE;
1194 while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1195 mtx_unlock(&fdc->fdc_mtx);
1196 i = fdc_worker(fdc);
1197 if (i && debugflags & 0x20) {
1198 if (fdc->bp != NULL) {
1199 g_print_bio(fdc->bp);
1200 printf("\n");
1201 }
1202 printf("Retry line %d\n", retry_line);
1203 }
1204 fdc->retry += i;
1205 mtx_lock(&fdc->fdc_mtx);
1206 }
1207 fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1208 mtx_unlock(&fdc->fdc_mtx);
1209
1210 kproc_exit(0);
1211}
1212
1213/*
1214 * Enqueue a request.
1215 */
1216static void
1217fd_enqueue(struct fd_data *fd, struct bio *bp)
1218{
1219 struct fdc_data *fdc;
1220 int call;
1221
1222 call = 0;
1223 fdc = fd->fdc;
1224 mtx_lock(&fdc->fdc_mtx);
1225 /* If we go from idle, cancel motor turnoff */
1226 if (fd->fd_iocount++ == 0)
1227 callout_stop(&fd->toffhandle);
1228 if (fd->flags & FD_MOTOR) {
1229 /* The motor is on, send it directly to the controller */
1230 bioq_disksort(&fdc->head, bp);
1231 wakeup(&fdc->head);
1232 } else {
1233 /* Queue it on the drive until the motor has started */
1234 bioq_insert_tail(&fd->fd_bq, bp);
1235 if (!(fd->flags & FD_MOTORWAIT))
1236 fd_motor(fd, 1);
1237 }
1238 mtx_unlock(&fdc->fdc_mtx);
1239}
1240
850 }
851
852 /*
853 * If we are dead just flush the requests
854 */
855 if (fd->flags & FD_EMPTY)
856 return (fdc_biodone(fdc, ENXIO));
857
858 /* Check if we lost our media */
859 if (fdin_rd(fdc) & FDI_DCHG) {
860 if (debugflags & 0x40)
861 printf("Lost disk\n");
862 mtx_lock(&fdc->fdc_mtx);
863 fd->flags |= FD_EMPTY;
864 fd->flags |= FD_NEWDISK;
865 mtx_unlock(&fdc->fdc_mtx);
866 g_topology_lock();
867 g_orphan_provider(fd->fd_provider, EXDEV);
868 fd->fd_provider->flags |= G_PF_WITHER;
869 fd->fd_provider =
870 g_new_providerf(fd->fd_geom, fd->fd_geom->name);
871 g_error_provider(fd->fd_provider, 0);
872 g_topology_unlock();
873 return (fdc_biodone(fdc, ENXIO));
874 }
875
876 /* Check if the floppy is write-protected */
877 if(bp->bio_cmd & (BIO_FMT | BIO_WRITE)) {
878 retry_line = __LINE__;
879 if(fdc_sense_drive(fdc, &st3) != 0)
880 return (1);
881 if(st3 & NE7_ST3_WP)
882 return (fdc_biodone(fdc, EROFS));
883 }
884
885 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
886 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
887 i = fd->ft->sectrac * fd->ft->heads;
888 cylinder = bp->bio_pblkno / i;
889 descyl = cylinder * steptrac;
890 sec = bp->bio_pblkno % i;
891 nsect = i - sec;
892 head = sec / fd->ft->sectrac;
893 sec = sec % fd->ft->sectrac + 1;
894
895 /* If everything is going swimmingly, use multisector xfer */
896 if (fdc->retry == 0 && bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
897 fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
898 nsect = fd->fd_iosize / fd->sectorsize;
899 } else if (bp->bio_cmd & (BIO_READ|BIO_WRITE)) {
900 fd->fd_iosize = fd->sectorsize;
901 nsect = 1;
902 }
903
904 /* Do RECAL if we need to or are going to track zero anyway */
905 if ((need_recal & (1 << fd->fdsu)) ||
906 (cylinder == 0 && fd->track != 0) ||
907 fdc->retry > 2) {
908 retry_line = __LINE__;
909 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
910 return (1);
911 tsleep(fdc, PRIBIO, "fdrecal", hz);
912 retry_line = __LINE__;
913 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
914 return (1); /* XXX */
915 retry_line = __LINE__;
916 if ((st0 & 0xc0) || cyl != 0)
917 return (1);
918 need_recal &= ~(1 << fd->fdsu);
919 fd->track = 0;
920 /* let the heads settle */
921 if (settle)
922 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
923 }
924
925 /*
926 * SEEK to where we want to be
927 *
928 * Enhanced controllers do implied seeks for read&write as long as
929 * we do not need multiple steps per track.
930 */
931 if (cylinder != fd->track && (
932 fdc->fdct != FDC_ENHANCED ||
933 descyl != cylinder ||
934 (bp->bio_cmd & (BIO_RDID|BIO_FMT)))) {
935 retry_line = __LINE__;
936 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
937 return (1);
938 tsleep(fdc, PRIBIO, "fdseek", hz);
939 retry_line = __LINE__;
940 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
941 return (1); /* XXX */
942 retry_line = __LINE__;
943 if ((st0 & 0xc0) || cyl != descyl) {
944 need_recal |= (1 << fd->fdsu);
945 return (1);
946 }
947 /* let the heads settle */
948 if (settle)
949 tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
950 }
951 fd->track = cylinder;
952
953 if (debugflags & 8)
954 printf("op %x bn %ju siz %u ptr %p retry %d\n",
955 bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
956 fd->fd_ioptr, fdc->retry);
957
958 /* Setup ISADMA if we need it and have it */
959 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
960 && !(fdc->flags & FDC_NODMA)) {
961 mtx_lock(&Giant);
962 isa_dmastart(
963 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
964 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
965 mtx_unlock(&Giant);
966 mtx_lock(&fdc->fdc_mtx);
967 fd->flags |= FD_ISADMA;
968 mtx_unlock(&fdc->fdc_mtx);
969 }
970
971 /* Do PIO if we have to */
972 if (fdc->flags & FDC_NODMA) {
973 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_FMT))
974 fdbcdr_wr(fdc, 1, fd->fd_iosize);
975 if (bp->bio_cmd & (BIO_WRITE|BIO_FMT))
976 fdc_pio(fdc);
977 }
978
979 switch(bp->bio_cmd) {
980 case BIO_FMT:
981 /* formatting */
982 finfo = (struct fd_formb *)bp->bio_data;
983 retry_line = __LINE__;
984 if (fdc_cmd(fdc, 6,
985 NE7CMD_FORMAT | mfm,
986 head << 2 | fd->fdsu,
987 finfo->fd_formb_secshift,
988 finfo->fd_formb_nsecs,
989 finfo->fd_formb_gaplen,
990 finfo->fd_formb_fillbyte, 0))
991 return (1);
992 break;
993 case BIO_RDID:
994 retry_line = __LINE__;
995 if (fdc_cmd(fdc, 2,
996 NE7CMD_READID | mfm,
997 head << 2 | fd->fdsu, 0))
998 return (1);
999 break;
1000 case BIO_READ:
1001 retry_line = __LINE__;
1002 if (fdc_cmd(fdc, 9,
1003 NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1004 head << 2 | fd->fdsu, /* head & unit */
1005 fd->track, /* track */
1006 head, /* head */
1007 sec, /* sector + 1 */
1008 fd->ft->secsize, /* sector size */
1009 fd->ft->sectrac, /* sectors/track */
1010 fd->ft->gap, /* gap size */
1011 fd->ft->datalen, /* data length */
1012 0))
1013 return (1);
1014 break;
1015 case BIO_WRITE:
1016 retry_line = __LINE__;
1017 if (fdc_cmd(fdc, 9,
1018 NE7CMD_WRITE | mfm | NE7CMD_MT,
1019 head << 2 | fd->fdsu, /* head & unit */
1020 fd->track, /* track */
1021 head, /* head */
1022 sec, /* sector + 1 */
1023 fd->ft->secsize, /* sector size */
1024 fd->ft->sectrac, /* sectors/track */
1025 fd->ft->gap, /* gap size */
1026 fd->ft->datalen, /* data length */
1027 0))
1028 return (1);
1029 break;
1030 default:
1031 KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1032 }
1033
1034 /* Wait for interrupt */
1035 i = tsleep(fdc, PRIBIO, "fddata", hz);
1036
1037 /* PIO if the read looks good */
1038 if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd & BIO_READ))
1039 fdc_pio(fdc);
1040
1041 /* Finish DMA */
1042 if (fd->flags & FD_ISADMA) {
1043 mtx_lock(&Giant);
1044 isa_dmadone(
1045 bp->bio_cmd & BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1046 fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1047 mtx_unlock(&Giant);
1048 mtx_lock(&fdc->fdc_mtx);
1049 fd->flags &= ~FD_ISADMA;
1050 mtx_unlock(&fdc->fdc_mtx);
1051 }
1052
1053 if (i != 0) {
1054 /*
1055 * Timeout.
1056 *
1057 * Due to IBM's brain-dead design, the FDC has a faked ready
1058 * signal, hardwired to ready == true. Thus, any command
1059 * issued if there's no diskette in the drive will _never_
1060 * complete, and must be aborted by resetting the FDC.
1061 * Many thanks, Big Blue!
1062 */
1063 retry_line = __LINE__;
1064 fdc->flags |= FDC_NEEDS_RESET;
1065 return (1);
1066 }
1067
1068 retry_line = __LINE__;
1069 if (fdc_read_status(fdc))
1070 return (1);
1071
1072 if (debugflags & 0x10)
1073 printf(" -> %x %x %x %x\n",
1074 fdc->status[0], fdc->status[1],
1075 fdc->status[2], fdc->status[3]);
1076
1077 st0 = fdc->status[0] & NE7_ST0_IC;
1078 if (st0 != 0) {
1079 retry_line = __LINE__;
1080 if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1081 /*
1082 * DMA overrun. Someone hogged the bus and
1083 * didn't release it in time for the next
1084 * FDC transfer.
1085 */
1086 return (1);
1087 }
1088 retry_line = __LINE__;
1089 if(st0 == NE7_ST0_IC_IV) {
1090 fdc->flags |= FDC_NEEDS_RESET;
1091 return (1);
1092 }
1093 retry_line = __LINE__;
1094 if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1095 need_recal |= (1 << fd->fdsu);
1096 return (1);
1097 }
1098 if (debugflags & 0x20) {
1099 printf("status %02x %02x %02x %02x %02x %02x\n",
1100 fdc->status[0], fdc->status[1], fdc->status[2],
1101 fdc->status[3], fdc->status[4], fdc->status[5]);
1102 }
1103 retry_line = __LINE__;
1104 return (1);
1105 }
1106 /* All OK */
1107 switch(bp->bio_cmd) {
1108 case BIO_RDID:
1109 /* copy out ID field contents */
1110 idp = (struct fdc_readid *)bp->bio_data;
1111 idp->cyl = fdc->status[3];
1112 idp->head = fdc->status[4];
1113 idp->sec = fdc->status[5];
1114 idp->secshift = fdc->status[6];
1115 if (debugflags & 0x40)
1116 printf("c %d h %d s %d z %d\n",
1117 idp->cyl, idp->head, idp->sec, idp->secshift);
1118 break;
1119 case BIO_READ:
1120 case BIO_WRITE:
1121 bp->bio_pblkno += nsect;
1122 bp->bio_resid -= fd->fd_iosize;
1123 bp->bio_completed += fd->fd_iosize;
1124 fd->fd_ioptr += fd->fd_iosize;
1125 /* Since we managed to get something done, reset the retry */
1126 fdc->retry = 0;
1127 if (bp->bio_resid > 0)
1128 return (0);
1129 break;
1130 case BIO_FMT:
1131 break;
1132 }
1133 return (fdc_biodone(fdc, 0));
1134}
1135
1136static void
1137fdc_thread(void *arg)
1138{
1139 struct fdc_data *fdc;
1140
1141 fdc = arg;
1142 int i;
1143
1144 mtx_lock(&fdc->fdc_mtx);
1145 fdc->flags |= FDC_KTHREAD_ALIVE;
1146 while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1147 mtx_unlock(&fdc->fdc_mtx);
1148 i = fdc_worker(fdc);
1149 if (i && debugflags & 0x20) {
1150 if (fdc->bp != NULL) {
1151 g_print_bio(fdc->bp);
1152 printf("\n");
1153 }
1154 printf("Retry line %d\n", retry_line);
1155 }
1156 fdc->retry += i;
1157 mtx_lock(&fdc->fdc_mtx);
1158 }
1159 fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1160 mtx_unlock(&fdc->fdc_mtx);
1161
1162 kproc_exit(0);
1163}
1164
1165/*
1166 * Enqueue a request.
1167 */
1168static void
1169fd_enqueue(struct fd_data *fd, struct bio *bp)
1170{
1171 struct fdc_data *fdc;
1172 int call;
1173
1174 call = 0;
1175 fdc = fd->fdc;
1176 mtx_lock(&fdc->fdc_mtx);
1177 /* If we go from idle, cancel motor turnoff */
1178 if (fd->fd_iocount++ == 0)
1179 callout_stop(&fd->toffhandle);
1180 if (fd->flags & FD_MOTOR) {
1181 /* The motor is on, send it directly to the controller */
1182 bioq_disksort(&fdc->head, bp);
1183 wakeup(&fdc->head);
1184 } else {
1185 /* Queue it on the drive until the motor has started */
1186 bioq_insert_tail(&fd->fd_bq, bp);
1187 if (!(fd->flags & FD_MOTORWAIT))
1188 fd_motor(fd, 1);
1189 }
1190 mtx_unlock(&fdc->fdc_mtx);
1191}
1192
1193/*
1194 * Try to find out if we have a disk in the drive.
1195 */
1241static int
1196static int
1197fd_probe_disk(struct fd_data *fd, int *recal)
1198{
1199 struct fdc_data *fdc;
1200 int st0, st3, cyl;
1201 int oopts, ret;
1202
1203 fdc = fd->fdc;
1204 oopts = fd->options;
1205 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1206 ret = 1;
1207
1208 /*
1209 * First recal, then seek to cyl#1, this clears the old condition on
1210 * the disk change line so we can examine it for current status.
1211 */
1212 if (debugflags & 0x40)
1213 printf("New disk in probe\n");
1214 mtx_lock(&fdc->fdc_mtx);
1215 fd->flags |= FD_NEWDISK;
1216 mtx_unlock(&fdc->fdc_mtx);
1217 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1218 goto done;
1219 tsleep(fdc, PRIBIO, "fdrecal", hz);
1220 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1221 goto done; /* XXX */
1222 if ((st0 & 0xc0) || cyl != 0)
1223 goto done;
1224
1225 /* Seek to track 1 */
1226 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1227 goto done;
1228 tsleep(fdc, PRIBIO, "fdseek", hz);
1229 if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1230 goto done; /* XXX */
1231 *recal |= (1 << fd->fdsu);
1232 if (fdin_rd(fdc) & FDI_DCHG) {
1233 if (debugflags & 0x40)
1234 printf("Empty in probe\n");
1235 mtx_lock(&fdc->fdc_mtx);
1236 fd->flags |= FD_EMPTY;
1237 mtx_unlock(&fdc->fdc_mtx);
1238 } else {
1239 if (fdc_sense_drive(fdc, &st3) != 0)
1240 goto done;
1241 if (debugflags & 0x40)
1242 printf("Got disk in probe\n");
1243 mtx_lock(&fdc->fdc_mtx);
1244 fd->flags &= ~FD_EMPTY;
1245 if (st3 & NE7_ST3_WP)
1246 fd->flags |= FD_WP;
1247 else
1248 fd->flags &= ~FD_WP;
1249 mtx_unlock(&fdc->fdc_mtx);
1250 }
1251 ret = 0;
1252
1253done:
1254 fd->options = oopts;
1255 return (ret);
1256}
1257
1258static int
1242fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1243{
1244 struct bio *bp;
1245 struct fd_formb *finfo;
1246 struct fdc_readid *idfield;
1247 int error;
1248
1249 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1250
1251 /*
1252 * Set up a bio request for fdstrategy(). bio_offset is faked
1253 * so that fdstrategy() will seek to the the requested
1254 * cylinder, and use the desired head.
1255 */
1256 bp->bio_cmd = cmd;
1257 if (cmd == BIO_FMT) {
1258 finfo = (struct fd_formb *)data;
1259 bp->bio_pblkno =
1260 (finfo->cyl * fd->ft->heads + finfo->head) *
1261 fd->ft->sectrac;
1262 bp->bio_length = sizeof *finfo;
1263 } else if (cmd == BIO_RDID) {
1264 idfield = (struct fdc_readid *)data;
1265 bp->bio_pblkno =
1266 (idfield->cyl * fd->ft->heads + idfield->head) *
1267 fd->ft->sectrac;
1268 bp->bio_length = sizeof(struct fdc_readid);
1269 } else if (cmd == BIO_PROBE) {
1270 /* nothing */
1271 } else
1272 panic("wrong cmd in fdmisccmd()");
1273 bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1274 bp->bio_data = data;
1275 bp->bio_driver1 = fd;
1276 bp->bio_flags = 0;
1277
1278 fd_enqueue(fd, bp);
1279
1280 do {
1281 tsleep(bp, PRIBIO, "fdwait", hz);
1282 } while (!(bp->bio_flags & BIO_DONE));
1283 error = bp->bio_error;
1284
1285 free(bp, M_TEMP);
1286 return (error);
1287}
1288
1289/*
1290 * Try figuring out the density of the media present in our device.
1291 */
1292static int
1293fdautoselect(struct fd_data *fd)
1294{
1295 struct fd_type *fdtp;
1296 struct fdc_readid id;
1297 int oopts, rv;
1298
1299 if (!(fd->ft->flags & FL_AUTO))
1300 return (0);
1301
1302 fdtp = fd_native_types[fd->type];
1303 fdsettype(fd, fdtp);
1304 if (!(fd->ft->flags & FL_AUTO))
1305 return (0);
1306
1307 /*
1308 * Try reading sector ID fields, first at cylinder 0, head 0,
1309 * then at cylinder 2, head N. We don't probe cylinder 1,
1310 * since for 5.25in DD media in a HD drive, there are no data
1311 * to read (2 step pulses per media cylinder required). For
1312 * two-sided media, the second probe always goes to head 1, so
1313 * we can tell them apart from single-sided media. As a
1314 * side-effect this means that single-sided media should be
1315 * mentioned in the search list after two-sided media of an
1316 * otherwise identical density. Media with a different number
1317 * of sectors per track but otherwise identical parameters
1318 * cannot be distinguished at all.
1319 *
1320 * If we successfully read an ID field on both cylinders where
1321 * the recorded values match our expectation, we are done.
1322 * Otherwise, we try the next density entry from the table.
1323 *
1324 * Stepping to cylinder 2 has the side-effect of clearing the
1325 * unit attention bit.
1326 */
1327 oopts = fd->options;
1328 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1329 for (; fdtp->heads; fdtp++) {
1330 fdsettype(fd, fdtp);
1331
1332 id.cyl = id.head = 0;
1333 rv = fdmisccmd(fd, BIO_RDID, &id);
1334 if (rv != 0)
1335 continue;
1336 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1337 continue;
1338 id.cyl = 2;
1339 id.head = fd->ft->heads - 1;
1340 rv = fdmisccmd(fd, BIO_RDID, &id);
1341 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1342 id.secshift != fdtp->secsize)
1343 continue;
1344 if (rv == 0)
1345 break;
1346 }
1347
1348 fd->options = oopts;
1349 if (fdtp->heads == 0) {
1350 if (debugflags & 0x40)
1351 device_printf(fd->dev, "autoselection failed\n");
1352 fdsettype(fd, fd_native_types[fd->type]);
1259fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1260{
1261 struct bio *bp;
1262 struct fd_formb *finfo;
1263 struct fdc_readid *idfield;
1264 int error;
1265
1266 bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1267
1268 /*
1269 * Set up a bio request for fdstrategy(). bio_offset is faked
1270 * so that fdstrategy() will seek to the the requested
1271 * cylinder, and use the desired head.
1272 */
1273 bp->bio_cmd = cmd;
1274 if (cmd == BIO_FMT) {
1275 finfo = (struct fd_formb *)data;
1276 bp->bio_pblkno =
1277 (finfo->cyl * fd->ft->heads + finfo->head) *
1278 fd->ft->sectrac;
1279 bp->bio_length = sizeof *finfo;
1280 } else if (cmd == BIO_RDID) {
1281 idfield = (struct fdc_readid *)data;
1282 bp->bio_pblkno =
1283 (idfield->cyl * fd->ft->heads + idfield->head) *
1284 fd->ft->sectrac;
1285 bp->bio_length = sizeof(struct fdc_readid);
1286 } else if (cmd == BIO_PROBE) {
1287 /* nothing */
1288 } else
1289 panic("wrong cmd in fdmisccmd()");
1290 bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1291 bp->bio_data = data;
1292 bp->bio_driver1 = fd;
1293 bp->bio_flags = 0;
1294
1295 fd_enqueue(fd, bp);
1296
1297 do {
1298 tsleep(bp, PRIBIO, "fdwait", hz);
1299 } while (!(bp->bio_flags & BIO_DONE));
1300 error = bp->bio_error;
1301
1302 free(bp, M_TEMP);
1303 return (error);
1304}
1305
1306/*
1307 * Try figuring out the density of the media present in our device.
1308 */
1309static int
1310fdautoselect(struct fd_data *fd)
1311{
1312 struct fd_type *fdtp;
1313 struct fdc_readid id;
1314 int oopts, rv;
1315
1316 if (!(fd->ft->flags & FL_AUTO))
1317 return (0);
1318
1319 fdtp = fd_native_types[fd->type];
1320 fdsettype(fd, fdtp);
1321 if (!(fd->ft->flags & FL_AUTO))
1322 return (0);
1323
1324 /*
1325 * Try reading sector ID fields, first at cylinder 0, head 0,
1326 * then at cylinder 2, head N. We don't probe cylinder 1,
1327 * since for 5.25in DD media in a HD drive, there are no data
1328 * to read (2 step pulses per media cylinder required). For
1329 * two-sided media, the second probe always goes to head 1, so
1330 * we can tell them apart from single-sided media. As a
1331 * side-effect this means that single-sided media should be
1332 * mentioned in the search list after two-sided media of an
1333 * otherwise identical density. Media with a different number
1334 * of sectors per track but otherwise identical parameters
1335 * cannot be distinguished at all.
1336 *
1337 * If we successfully read an ID field on both cylinders where
1338 * the recorded values match our expectation, we are done.
1339 * Otherwise, we try the next density entry from the table.
1340 *
1341 * Stepping to cylinder 2 has the side-effect of clearing the
1342 * unit attention bit.
1343 */
1344 oopts = fd->options;
1345 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1346 for (; fdtp->heads; fdtp++) {
1347 fdsettype(fd, fdtp);
1348
1349 id.cyl = id.head = 0;
1350 rv = fdmisccmd(fd, BIO_RDID, &id);
1351 if (rv != 0)
1352 continue;
1353 if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1354 continue;
1355 id.cyl = 2;
1356 id.head = fd->ft->heads - 1;
1357 rv = fdmisccmd(fd, BIO_RDID, &id);
1358 if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1359 id.secshift != fdtp->secsize)
1360 continue;
1361 if (rv == 0)
1362 break;
1363 }
1364
1365 fd->options = oopts;
1366 if (fdtp->heads == 0) {
1367 if (debugflags & 0x40)
1368 device_printf(fd->dev, "autoselection failed\n");
1369 fdsettype(fd, fd_native_types[fd->type]);
1353 return (0);
1370 return (-1);
1354 } else {
1355 if (debugflags & 0x40) {
1356 device_printf(fd->dev,
1357 "autoselected %d KB medium\n", fd->ft->size / 2);
1358 fdprinttype(fd->ft);
1359 }
1360 return (0);
1361 }
1362}
1363
1364/*
1365 * GEOM class implementation
1366 */
1367
1368static g_access_t fd_access;
1369static g_start_t fd_start;
1370static g_ioctl_t fd_ioctl;
1371
1372struct g_class g_fd_class = {
1373 .name = "FD",
1374 .version = G_VERSION,
1375 .start = fd_start,
1376 .access = fd_access,
1377 .ioctl = fd_ioctl,
1378};
1379
1380static int
1381fd_access(struct g_provider *pp, int r, int w, int e)
1382{
1383 struct fd_data *fd;
1384 struct fdc_data *fdc;
1385 int ar, aw, ae;
1386 int busy;
1387
1388 fd = pp->geom->softc;
1389 fdc = fd->fdc;
1390
1391 /*
1392 * If our provider is withering, we can only get negative requests
1393 * and we don't want to even see them
1394 */
1395 if (pp->flags & G_PF_WITHER)
1396 return (0);
1397
1398 ar = r + pp->acr;
1399 aw = w + pp->acw;
1400 ae = e + pp->ace;
1401
1402 if (ar == 0 && aw == 0 && ae == 0) {
1403 device_unbusy(fd->dev);
1404 return (0);
1405 }
1406
1407 busy = 0;
1408 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1409 if (fdmisccmd(fd, BIO_PROBE, NULL))
1410 return (ENXIO);
1411 if (fd->flags & FD_EMPTY)
1412 return (ENXIO);
1413 if (fd->flags & FD_NEWDISK) {
1371 } else {
1372 if (debugflags & 0x40) {
1373 device_printf(fd->dev,
1374 "autoselected %d KB medium\n", fd->ft->size / 2);
1375 fdprinttype(fd->ft);
1376 }
1377 return (0);
1378 }
1379}
1380
1381/*
1382 * GEOM class implementation
1383 */
1384
1385static g_access_t fd_access;
1386static g_start_t fd_start;
1387static g_ioctl_t fd_ioctl;
1388
1389struct g_class g_fd_class = {
1390 .name = "FD",
1391 .version = G_VERSION,
1392 .start = fd_start,
1393 .access = fd_access,
1394 .ioctl = fd_ioctl,
1395};
1396
1397static int
1398fd_access(struct g_provider *pp, int r, int w, int e)
1399{
1400 struct fd_data *fd;
1401 struct fdc_data *fdc;
1402 int ar, aw, ae;
1403 int busy;
1404
1405 fd = pp->geom->softc;
1406 fdc = fd->fdc;
1407
1408 /*
1409 * If our provider is withering, we can only get negative requests
1410 * and we don't want to even see them
1411 */
1412 if (pp->flags & G_PF_WITHER)
1413 return (0);
1414
1415 ar = r + pp->acr;
1416 aw = w + pp->acw;
1417 ae = e + pp->ace;
1418
1419 if (ar == 0 && aw == 0 && ae == 0) {
1420 device_unbusy(fd->dev);
1421 return (0);
1422 }
1423
1424 busy = 0;
1425 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1426 if (fdmisccmd(fd, BIO_PROBE, NULL))
1427 return (ENXIO);
1428 if (fd->flags & FD_EMPTY)
1429 return (ENXIO);
1430 if (fd->flags & FD_NEWDISK) {
1414 fdautoselect(fd);
1431 if (fdautoselect(fd) != 0 &&
1432 (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1433 mtx_lock(&fdc->fdc_mtx);
1434 fd->flags |= FD_EMPTY;
1435 mtx_unlock(&fdc->fdc_mtx);
1436 return (ENXIO);
1437 }
1415 mtx_lock(&fdc->fdc_mtx);
1416 fd->flags &= ~FD_NEWDISK;
1417 mtx_unlock(&fdc->fdc_mtx);
1418 }
1419 device_busy(fd->dev);
1420 busy = 1;
1421 }
1422
1423 if (w > 0 && (fd->flags & FD_WP)) {
1424 if (busy)
1425 device_unbusy(fd->dev);
1426 return (EROFS);
1427 }
1428
1429 pp->sectorsize = fd->sectorsize;
1430 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1431 pp->mediasize = pp->stripesize * fd->ft->tracks;
1432 return (0);
1433}
1434
1435static void
1436fd_start(struct bio *bp)
1437{
1438 struct fdc_data * fdc;
1439 struct fd_data * fd;
1440
1441 fd = bp->bio_to->geom->softc;
1442 fdc = fd->fdc;
1443 bp->bio_driver1 = fd;
1444 if (bp->bio_cmd & BIO_GETATTR) {
1445 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1446 return;
1447 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1448 return;
1449 g_io_deliver(bp, ENOIOCTL);
1450 return;
1451 }
1452 if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
1453 g_io_deliver(bp, EOPNOTSUPP);
1454 return;
1455 }
1456 bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1457 bp->bio_resid = bp->bio_length;
1458 fd_enqueue(fd, bp);
1459 return;
1460}
1461
1462static int
1463fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1464{
1465 struct fd_data *fd;
1466 struct fdc_status *fsp;
1467 struct fdc_readid *rid;
1468 int error;
1469
1470 fd = pp->geom->softc;
1471
1472 switch (cmd) {
1473 case FD_GTYPE: /* get drive type */
1474 *(struct fd_type *)data = *fd->ft;
1475 return (0);
1476
1477 case FD_STYPE: /* set drive type */
1478 if (!(fflag & FWRITE))
1479 return (EPERM);
1480 /*
1481 * Allow setting drive type temporarily iff
1482 * currently unset. Used for fdformat so any
1483 * user can set it, and then start formatting.
1484 */
1485 fd->fts = *(struct fd_type *)data;
1486 if (fd->fts.sectrac) {
1487 /* XXX: check for rubbish */
1488 fdsettype(fd, &fd->fts);
1489 } else {
1490 fdsettype(fd, fd_native_types[fd->type]);
1491 }
1492 if (debugflags & 0x40)
1493 fdprinttype(fd->ft);
1494 return (0);
1495
1496 case FD_GOPTS: /* get drive options */
1497 *(int *)data = fd->options;
1498 return (0);
1499
1500 case FD_SOPTS: /* set drive options */
1501 if (!(fflag & FWRITE))
1502 return (EPERM);
1503 fd->options = *(int *)data;
1504 return (0);
1505
1506 case FD_CLRERR:
1507 error = priv_check(td, PRIV_DRIVER);
1508 if (error)
1509 return (error);
1510 fd->fdc->fdc_errs = 0;
1511 return (0);
1512
1513 case FD_GSTAT:
1514 fsp = (struct fdc_status *)data;
1515 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1516 return (EINVAL);
1517 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1518 return (0);
1519
1520 case FD_GDTYPE:
1521 *(enum fd_drivetype *)data = fd->type;
1522 return (0);
1523
1524 case FD_FORM:
1525 if (!(fflag & FWRITE))
1526 return (EPERM);
1527 if (((struct fd_formb *)data)->format_version !=
1528 FD_FORMAT_VERSION)
1529 return (EINVAL); /* wrong version of formatting prog */
1530 error = fdmisccmd(fd, BIO_FMT, data);
1531 mtx_lock(&fd->fdc->fdc_mtx);
1532 fd->flags |= FD_NEWDISK;
1533 mtx_unlock(&fd->fdc->fdc_mtx);
1534 break;
1535
1536 case FD_READID:
1537 rid = (struct fdc_readid *)data;
1538 if (rid->cyl > 85 || rid->head > 1)
1539 return (EINVAL);
1540 error = fdmisccmd(fd, BIO_RDID, data);
1541 break;
1542
1543 case FIONBIO:
1544 case FIOASYNC:
1545 /* For backwards compat with old fd*(8) tools */
1546 error = 0;
1547 break;
1548
1549 default:
1550 if (debugflags & 0x80)
1551 printf("Unknown ioctl %lx\n", cmd);
1552 error = ENOIOCTL;
1553 break;
1554 }
1555 return (error);
1556};
1557
1558
1559
1560/*
1561 * Configuration/initialization stuff, per controller.
1562 */
1563
1564devclass_t fdc_devclass;
1565static devclass_t fd_devclass;
1566
1567struct fdc_ivars {
1568 int fdunit;
1569 int fdtype;
1570};
1571
1572void
1573fdc_release_resources(struct fdc_data *fdc)
1574{
1575 device_t dev;
1576 struct resource *last;
1577 int i;
1578
1579 dev = fdc->fdc_dev;
1580 if (fdc->fdc_intr)
1581 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1582 fdc->fdc_intr = NULL;
1583 if (fdc->res_irq != NULL)
1584 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1585 fdc->res_irq);
1586 fdc->res_irq = NULL;
1587 last = NULL;
1588 for (i = 0; i < FDC_MAXREG; i++) {
1589 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1590 bus_release_resource(dev, SYS_RES_IOPORT,
1591 fdc->ridio[i], fdc->resio[i]);
1592 last = fdc->resio[i];
1593 fdc->resio[i] = NULL;
1594 }
1595 }
1596 if (fdc->res_drq != NULL)
1597 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1598 fdc->res_drq);
1599 fdc->res_drq = NULL;
1600}
1601
1602int
1603fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1604{
1605 struct fdc_ivars *ivars = device_get_ivars(child);
1606
1607 switch (which) {
1608 case FDC_IVAR_FDUNIT:
1609 *result = ivars->fdunit;
1610 break;
1611 case FDC_IVAR_FDTYPE:
1612 *result = ivars->fdtype;
1613 break;
1614 default:
1615 return (ENOENT);
1616 }
1617 return (0);
1618}
1619
1620int
1621fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1622{
1623 struct fdc_ivars *ivars = device_get_ivars(child);
1624
1625 switch (which) {
1626 case FDC_IVAR_FDUNIT:
1627 ivars->fdunit = value;
1628 break;
1629 case FDC_IVAR_FDTYPE:
1630 ivars->fdtype = value;
1631 break;
1632 default:
1633 return (ENOENT);
1634 }
1635 return (0);
1636}
1637
1638int
1639fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1640{
1641 int ic_type, part_id;
1642
1643 /*
1644 * A status value of 0xff is very unlikely, but not theoretically
1645 * impossible, but it is far more likely to indicate an empty bus.
1646 */
1647 if (fdsts_rd(fdc) == 0xff)
1648 return (ENXIO);
1649
1650 /*
1651 * Assert a reset to the floppy controller and check that the status
1652 * register goes to zero.
1653 */
1654 fdout_wr(fdc, 0);
1655 fdout_wr(fdc, 0);
1656 if (fdsts_rd(fdc) != 0)
1657 return (ENXIO);
1658
1659 /*
1660 * Clear the reset and see it come ready.
1661 */
1662 fdout_wr(fdc, FDO_FRST);
1663 DELAY(100);
1664 if (fdsts_rd(fdc) != 0x80)
1665 return (ENXIO);
1666
1667 /* Then, see if it can handle a command. */
1668 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1669 return (ENXIO);
1670
1671 /*
1672 * Try to identify the chip.
1673 *
1674 * The i8272 datasheet documents that unknown commands
1675 * will return ST0 as 0x80. The i8272 is supposedly identical
1676 * to the NEC765.
1677 * The i82077SL datasheet says 0x90 for the VERSION command,
1678 * and several "superio" chips emulate this.
1679 */
1680 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1681 return (ENXIO);
1682 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1683 return (ENXIO);
1684 if (bootverbose)
1685 device_printf(dev,
1686 "ic_type %02x part_id %02x\n", ic_type, part_id);
1687 switch (ic_type & 0xff) {
1688 case 0x80:
1689 device_set_desc(dev, "NEC 765 or clone");
1690 fdc->fdct = FDC_NE765;
1691 break;
1692 case 0x81:
1693 case 0x90:
1694 device_set_desc(dev,
1695 "Enhanced floppy controller");
1696 fdc->fdct = FDC_ENHANCED;
1697 break;
1698 default:
1699 device_set_desc(dev, "Generic floppy controller");
1700 fdc->fdct = FDC_UNKNOWN;
1701 break;
1702 }
1703 return (0);
1704}
1705
1706int
1707fdc_detach(device_t dev)
1708{
1709 struct fdc_data *fdc;
1710 int error;
1711
1712 fdc = device_get_softc(dev);
1713
1714 /* have our children detached first */
1715 if ((error = bus_generic_detach(dev)))
1716 return (error);
1717
1718 /* kill worker thread */
1719 mtx_lock(&fdc->fdc_mtx);
1720 fdc->flags |= FDC_KTHREAD_EXIT;
1721 wakeup(&fdc->head);
1722 while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1723 msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1724 mtx_unlock(&fdc->fdc_mtx);
1725
1726 /* reset controller, turn motor off */
1727 fdout_wr(fdc, 0);
1728
1729 if (!(fdc->flags & FDC_NODMA))
1730 isa_dma_release(fdc->dmachan);
1731 fdc_release_resources(fdc);
1732 mtx_destroy(&fdc->fdc_mtx);
1733 return (0);
1734}
1735
1736/*
1737 * Add a child device to the fdc controller. It will then be probed etc.
1738 */
1739device_t
1740fdc_add_child(device_t dev, const char *name, int unit)
1741{
1742 struct fdc_ivars *ivar;
1743 device_t child;
1744
1745 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1746 if (ivar == NULL)
1747 return (NULL);
1748 child = device_add_child(dev, name, unit);
1749 if (child == NULL) {
1750 free(ivar, M_DEVBUF);
1751 return (NULL);
1752 }
1753 device_set_ivars(child, ivar);
1754 ivar->fdunit = unit;
1755 ivar->fdtype = FDT_NONE;
1756 if (resource_disabled(name, unit))
1757 device_disable(child);
1758 return (child);
1759}
1760
1761int
1762fdc_attach(device_t dev)
1763{
1764 struct fdc_data *fdc;
1765 int error;
1766
1767 fdc = device_get_softc(dev);
1768 fdc->fdc_dev = dev;
1769 error = fdc_initial_reset(dev, fdc);
1770 if (error) {
1771 device_printf(dev, "does not respond\n");
1772 return (error);
1773 }
1774 error = bus_setup_intr(dev, fdc->res_irq,
1775 INTR_TYPE_BIO | INTR_ENTROPY |
1776 ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1777 ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1778 ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1779 fdc, &fdc->fdc_intr);
1780 if (error) {
1781 device_printf(dev, "cannot setup interrupt\n");
1782 return (error);
1783 }
1784 if (!(fdc->flags & FDC_NODMA)) {
1785 error = isa_dma_acquire(fdc->dmachan);
1786 if (!error) {
1787 error = isa_dma_init(fdc->dmachan,
1788 MAX_BYTES_PER_CYL, M_WAITOK);
1789 if (error)
1790 isa_dma_release(fdc->dmachan);
1791 }
1792 if (error)
1793 return (error);
1794 }
1795 fdc->fdcu = device_get_unit(dev);
1796 fdc->flags |= FDC_NEEDS_RESET;
1797
1798 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1799
1800 /* reset controller, turn motor off, clear fdout mirror reg */
1801 fdout_wr(fdc, fdc->fdout = 0);
1802 bioq_init(&fdc->head);
1803
1804 kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1805 "fdc%d", device_get_unit(dev));
1806
1807 settle = hz / 8;
1808
1809 return (0);
1810}
1811
1812int
1813fdc_hints_probe(device_t dev)
1814{
1815 const char *name, *dname;
1816 int i, error, dunit;
1817
1818 /*
1819 * Probe and attach any children. We should probably detect
1820 * devices from the BIOS unless overridden.
1821 */
1822 name = device_get_nameunit(dev);
1823 i = 0;
1824 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1825 resource_int_value(dname, dunit, "drive", &dunit);
1826 fdc_add_child(dev, dname, dunit);
1827 }
1828
1829 if ((error = bus_generic_attach(dev)) != 0)
1830 return (error);
1831 return (0);
1832}
1833
1834int
1835fdc_print_child(device_t me, device_t child)
1836{
1837 int retval = 0, flags;
1838
1839 retval += bus_print_child_header(me, child);
1840 retval += printf(" on %s drive %d", device_get_nameunit(me),
1841 fdc_get_fdunit(child));
1842 if ((flags = device_get_flags(me)) != 0)
1843 retval += printf(" flags %#x", flags);
1844 retval += printf("\n");
1845
1846 return (retval);
1847}
1848
1849/*
1850 * Configuration/initialization, per drive.
1851 */
1852static int
1853fd_probe(device_t dev)
1854{
1855 int i, unit;
1856 u_int st0, st3;
1857 struct fd_data *fd;
1858 struct fdc_data *fdc;
1859 int fdsu;
1860 int flags, type;
1861
1862 fdsu = fdc_get_fdunit(dev);
1863 fd = device_get_softc(dev);
1864 fdc = device_get_softc(device_get_parent(dev));
1865 flags = device_get_flags(dev);
1866
1867 fd->dev = dev;
1868 fd->fdc = fdc;
1869 fd->fdsu = fdsu;
1870 unit = device_get_unit(dev);
1871
1872 /* Auto-probe if fdinfo is present, but always allow override. */
1873 type = flags & FD_TYPEMASK;
1874 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1875 fd->type = type;
1876 goto done;
1877 } else {
1878 /* make sure fdautoselect() will be called */
1879 fd->flags = FD_EMPTY;
1880 fd->type = type;
1881 }
1882
1883#if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
1884 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1885 /* Look up what the BIOS thinks we have. */
1886 if (unit == 0)
1887 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1888 else
1889 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1890 if (fd->type == FDT_288M_1)
1891 fd->type = FDT_288M;
1892 }
1893#endif /* __i386__ || __amd64__ */
1894 /* is there a unit? */
1895 if (fd->type == FDT_NONE)
1896 return (ENXIO);
1897
1898/*
1899 mtx_lock(&fdc->fdc_mtx);
1900*/
1901 /* select it */
1902 fd_select(fd);
1903 fd_motor(fd, 1);
1904 fdc->fd = fd;
1905 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1906 DELAY(1000000); /* 1 sec */
1907
1908 if ((flags & FD_NO_PROBE) == 0) {
1909 /* If we're at track 0 first seek inwards. */
1910 if ((fdc_sense_drive(fdc, &st3) == 0) &&
1911 (st3 & NE7_ST3_T0)) {
1912 /* Seek some steps... */
1913 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1914 /* ...wait a moment... */
1915 DELAY(300000);
1916 /* make ctrlr happy: */
1917 fdc_sense_int(fdc, NULL, NULL);
1918 }
1919 }
1920
1921 for (i = 0; i < 2; i++) {
1922 /*
1923 * we must recalibrate twice, just in case the
1924 * heads have been beyond cylinder 76, since
1925 * most FDCs still barf when attempting to
1926 * recalibrate more than 77 steps
1927 */
1928 /* go back to 0: */
1929 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1930 /* a second being enough for full stroke seek*/
1931 DELAY(i == 0 ? 1000000 : 300000);
1932
1933 /* anything responding? */
1934 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1935 (st0 & NE7_ST0_EC) == 0)
1936 break; /* already probed succesfully */
1937 }
1938 }
1939 }
1940
1941 fd_motor(fd, 0);
1942 fdc->fd = NULL;
1943/*
1944 mtx_unlock(&fdc->fdc_mtx);
1945*/
1946
1947 if ((flags & FD_NO_PROBE) == 0 &&
1948 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1949 return (ENXIO);
1950
1951done:
1952
1953 switch (fd->type) {
1954 case FDT_12M:
1955 device_set_desc(dev, "1200-KB 5.25\" drive");
1956 break;
1957 case FDT_144M:
1958 device_set_desc(dev, "1440-KB 3.5\" drive");
1959 break;
1960 case FDT_288M:
1961 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1962 break;
1963 case FDT_360K:
1964 device_set_desc(dev, "360-KB 5.25\" drive");
1965 break;
1966 case FDT_720K:
1967 device_set_desc(dev, "720-KB 3.5\" drive");
1968 break;
1969 default:
1970 return (ENXIO);
1971 }
1972 fd->track = FD_NO_TRACK;
1973 fd->fdc = fdc;
1974 fd->fdsu = fdsu;
1975 fd->options = 0;
1976 callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
1977
1978 /* initialize densities for subdevices */
1979 fdsettype(fd, fd_native_types[fd->type]);
1980 return (0);
1981}
1982
1983/*
1984 * We have to do this in a geom event because GEOM is not running
1985 * when fd_attach() is.
1986 * XXX: move fd_attach after geom like ata/scsi disks
1987 */
1988static void
1989fd_attach2(void *arg, int flag)
1990{
1991 struct fd_data *fd;
1992
1993 fd = arg;
1994
1995 fd->fd_geom = g_new_geomf(&g_fd_class,
1996 "fd%d", device_get_unit(fd->dev));
1997 fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
1998 fd->fd_geom->softc = fd;
1999 g_error_provider(fd->fd_provider, 0);
2000}
2001
2002static int
2003fd_attach(device_t dev)
2004{
2005 struct fd_data *fd;
2006
2007 fd = device_get_softc(dev);
2008 g_post_event(fd_attach2, fd, M_WAITOK, NULL);
2009 fd->flags |= FD_EMPTY;
2010 bioq_init(&fd->fd_bq);
2011
2012 return (0);
2013}
2014
2015static int
2016fd_detach(device_t dev)
2017{
2018 struct fd_data *fd;
2019
2020 fd = device_get_softc(dev);
2021 g_topology_lock();
2022 g_wither_geom(fd->fd_geom, ENXIO);
2023 g_topology_unlock();
2024 while (device_get_state(dev) == DS_BUSY)
2025 tsleep(fd, PZERO, "fdd", hz/10);
2026 callout_drain(&fd->toffhandle);
2027
2028 return (0);
2029}
2030
2031static device_method_t fd_methods[] = {
2032 /* Device interface */
2033 DEVMETHOD(device_probe, fd_probe),
2034 DEVMETHOD(device_attach, fd_attach),
2035 DEVMETHOD(device_detach, fd_detach),
2036 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2037 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2038 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2039 { 0, 0 }
2040};
2041
2042static driver_t fd_driver = {
2043 "fd",
2044 fd_methods,
2045 sizeof(struct fd_data)
2046};
2047
2048static int
2049fdc_modevent(module_t mod, int type, void *data)
2050{
2051
2052 g_modevent(NULL, type, &g_fd_class);
2053 return (0);
2054}
2055
2056DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);
1438 mtx_lock(&fdc->fdc_mtx);
1439 fd->flags &= ~FD_NEWDISK;
1440 mtx_unlock(&fdc->fdc_mtx);
1441 }
1442 device_busy(fd->dev);
1443 busy = 1;
1444 }
1445
1446 if (w > 0 && (fd->flags & FD_WP)) {
1447 if (busy)
1448 device_unbusy(fd->dev);
1449 return (EROFS);
1450 }
1451
1452 pp->sectorsize = fd->sectorsize;
1453 pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1454 pp->mediasize = pp->stripesize * fd->ft->tracks;
1455 return (0);
1456}
1457
1458static void
1459fd_start(struct bio *bp)
1460{
1461 struct fdc_data * fdc;
1462 struct fd_data * fd;
1463
1464 fd = bp->bio_to->geom->softc;
1465 fdc = fd->fdc;
1466 bp->bio_driver1 = fd;
1467 if (bp->bio_cmd & BIO_GETATTR) {
1468 if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1469 return;
1470 if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1471 return;
1472 g_io_deliver(bp, ENOIOCTL);
1473 return;
1474 }
1475 if (!(bp->bio_cmd & (BIO_READ|BIO_WRITE))) {
1476 g_io_deliver(bp, EOPNOTSUPP);
1477 return;
1478 }
1479 bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1480 bp->bio_resid = bp->bio_length;
1481 fd_enqueue(fd, bp);
1482 return;
1483}
1484
1485static int
1486fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1487{
1488 struct fd_data *fd;
1489 struct fdc_status *fsp;
1490 struct fdc_readid *rid;
1491 int error;
1492
1493 fd = pp->geom->softc;
1494
1495 switch (cmd) {
1496 case FD_GTYPE: /* get drive type */
1497 *(struct fd_type *)data = *fd->ft;
1498 return (0);
1499
1500 case FD_STYPE: /* set drive type */
1501 if (!(fflag & FWRITE))
1502 return (EPERM);
1503 /*
1504 * Allow setting drive type temporarily iff
1505 * currently unset. Used for fdformat so any
1506 * user can set it, and then start formatting.
1507 */
1508 fd->fts = *(struct fd_type *)data;
1509 if (fd->fts.sectrac) {
1510 /* XXX: check for rubbish */
1511 fdsettype(fd, &fd->fts);
1512 } else {
1513 fdsettype(fd, fd_native_types[fd->type]);
1514 }
1515 if (debugflags & 0x40)
1516 fdprinttype(fd->ft);
1517 return (0);
1518
1519 case FD_GOPTS: /* get drive options */
1520 *(int *)data = fd->options;
1521 return (0);
1522
1523 case FD_SOPTS: /* set drive options */
1524 if (!(fflag & FWRITE))
1525 return (EPERM);
1526 fd->options = *(int *)data;
1527 return (0);
1528
1529 case FD_CLRERR:
1530 error = priv_check(td, PRIV_DRIVER);
1531 if (error)
1532 return (error);
1533 fd->fdc->fdc_errs = 0;
1534 return (0);
1535
1536 case FD_GSTAT:
1537 fsp = (struct fdc_status *)data;
1538 if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1539 return (EINVAL);
1540 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1541 return (0);
1542
1543 case FD_GDTYPE:
1544 *(enum fd_drivetype *)data = fd->type;
1545 return (0);
1546
1547 case FD_FORM:
1548 if (!(fflag & FWRITE))
1549 return (EPERM);
1550 if (((struct fd_formb *)data)->format_version !=
1551 FD_FORMAT_VERSION)
1552 return (EINVAL); /* wrong version of formatting prog */
1553 error = fdmisccmd(fd, BIO_FMT, data);
1554 mtx_lock(&fd->fdc->fdc_mtx);
1555 fd->flags |= FD_NEWDISK;
1556 mtx_unlock(&fd->fdc->fdc_mtx);
1557 break;
1558
1559 case FD_READID:
1560 rid = (struct fdc_readid *)data;
1561 if (rid->cyl > 85 || rid->head > 1)
1562 return (EINVAL);
1563 error = fdmisccmd(fd, BIO_RDID, data);
1564 break;
1565
1566 case FIONBIO:
1567 case FIOASYNC:
1568 /* For backwards compat with old fd*(8) tools */
1569 error = 0;
1570 break;
1571
1572 default:
1573 if (debugflags & 0x80)
1574 printf("Unknown ioctl %lx\n", cmd);
1575 error = ENOIOCTL;
1576 break;
1577 }
1578 return (error);
1579};
1580
1581
1582
1583/*
1584 * Configuration/initialization stuff, per controller.
1585 */
1586
1587devclass_t fdc_devclass;
1588static devclass_t fd_devclass;
1589
1590struct fdc_ivars {
1591 int fdunit;
1592 int fdtype;
1593};
1594
1595void
1596fdc_release_resources(struct fdc_data *fdc)
1597{
1598 device_t dev;
1599 struct resource *last;
1600 int i;
1601
1602 dev = fdc->fdc_dev;
1603 if (fdc->fdc_intr)
1604 bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1605 fdc->fdc_intr = NULL;
1606 if (fdc->res_irq != NULL)
1607 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1608 fdc->res_irq);
1609 fdc->res_irq = NULL;
1610 last = NULL;
1611 for (i = 0; i < FDC_MAXREG; i++) {
1612 if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1613 bus_release_resource(dev, SYS_RES_IOPORT,
1614 fdc->ridio[i], fdc->resio[i]);
1615 last = fdc->resio[i];
1616 fdc->resio[i] = NULL;
1617 }
1618 }
1619 if (fdc->res_drq != NULL)
1620 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1621 fdc->res_drq);
1622 fdc->res_drq = NULL;
1623}
1624
1625int
1626fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1627{
1628 struct fdc_ivars *ivars = device_get_ivars(child);
1629
1630 switch (which) {
1631 case FDC_IVAR_FDUNIT:
1632 *result = ivars->fdunit;
1633 break;
1634 case FDC_IVAR_FDTYPE:
1635 *result = ivars->fdtype;
1636 break;
1637 default:
1638 return (ENOENT);
1639 }
1640 return (0);
1641}
1642
1643int
1644fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1645{
1646 struct fdc_ivars *ivars = device_get_ivars(child);
1647
1648 switch (which) {
1649 case FDC_IVAR_FDUNIT:
1650 ivars->fdunit = value;
1651 break;
1652 case FDC_IVAR_FDTYPE:
1653 ivars->fdtype = value;
1654 break;
1655 default:
1656 return (ENOENT);
1657 }
1658 return (0);
1659}
1660
1661int
1662fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1663{
1664 int ic_type, part_id;
1665
1666 /*
1667 * A status value of 0xff is very unlikely, but not theoretically
1668 * impossible, but it is far more likely to indicate an empty bus.
1669 */
1670 if (fdsts_rd(fdc) == 0xff)
1671 return (ENXIO);
1672
1673 /*
1674 * Assert a reset to the floppy controller and check that the status
1675 * register goes to zero.
1676 */
1677 fdout_wr(fdc, 0);
1678 fdout_wr(fdc, 0);
1679 if (fdsts_rd(fdc) != 0)
1680 return (ENXIO);
1681
1682 /*
1683 * Clear the reset and see it come ready.
1684 */
1685 fdout_wr(fdc, FDO_FRST);
1686 DELAY(100);
1687 if (fdsts_rd(fdc) != 0x80)
1688 return (ENXIO);
1689
1690 /* Then, see if it can handle a command. */
1691 if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, 0xaf, 0x1e, 0))
1692 return (ENXIO);
1693
1694 /*
1695 * Try to identify the chip.
1696 *
1697 * The i8272 datasheet documents that unknown commands
1698 * will return ST0 as 0x80. The i8272 is supposedly identical
1699 * to the NEC765.
1700 * The i82077SL datasheet says 0x90 for the VERSION command,
1701 * and several "superio" chips emulate this.
1702 */
1703 if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1704 return (ENXIO);
1705 if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1706 return (ENXIO);
1707 if (bootverbose)
1708 device_printf(dev,
1709 "ic_type %02x part_id %02x\n", ic_type, part_id);
1710 switch (ic_type & 0xff) {
1711 case 0x80:
1712 device_set_desc(dev, "NEC 765 or clone");
1713 fdc->fdct = FDC_NE765;
1714 break;
1715 case 0x81:
1716 case 0x90:
1717 device_set_desc(dev,
1718 "Enhanced floppy controller");
1719 fdc->fdct = FDC_ENHANCED;
1720 break;
1721 default:
1722 device_set_desc(dev, "Generic floppy controller");
1723 fdc->fdct = FDC_UNKNOWN;
1724 break;
1725 }
1726 return (0);
1727}
1728
1729int
1730fdc_detach(device_t dev)
1731{
1732 struct fdc_data *fdc;
1733 int error;
1734
1735 fdc = device_get_softc(dev);
1736
1737 /* have our children detached first */
1738 if ((error = bus_generic_detach(dev)))
1739 return (error);
1740
1741 /* kill worker thread */
1742 mtx_lock(&fdc->fdc_mtx);
1743 fdc->flags |= FDC_KTHREAD_EXIT;
1744 wakeup(&fdc->head);
1745 while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1746 msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1747 mtx_unlock(&fdc->fdc_mtx);
1748
1749 /* reset controller, turn motor off */
1750 fdout_wr(fdc, 0);
1751
1752 if (!(fdc->flags & FDC_NODMA))
1753 isa_dma_release(fdc->dmachan);
1754 fdc_release_resources(fdc);
1755 mtx_destroy(&fdc->fdc_mtx);
1756 return (0);
1757}
1758
1759/*
1760 * Add a child device to the fdc controller. It will then be probed etc.
1761 */
1762device_t
1763fdc_add_child(device_t dev, const char *name, int unit)
1764{
1765 struct fdc_ivars *ivar;
1766 device_t child;
1767
1768 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1769 if (ivar == NULL)
1770 return (NULL);
1771 child = device_add_child(dev, name, unit);
1772 if (child == NULL) {
1773 free(ivar, M_DEVBUF);
1774 return (NULL);
1775 }
1776 device_set_ivars(child, ivar);
1777 ivar->fdunit = unit;
1778 ivar->fdtype = FDT_NONE;
1779 if (resource_disabled(name, unit))
1780 device_disable(child);
1781 return (child);
1782}
1783
1784int
1785fdc_attach(device_t dev)
1786{
1787 struct fdc_data *fdc;
1788 int error;
1789
1790 fdc = device_get_softc(dev);
1791 fdc->fdc_dev = dev;
1792 error = fdc_initial_reset(dev, fdc);
1793 if (error) {
1794 device_printf(dev, "does not respond\n");
1795 return (error);
1796 }
1797 error = bus_setup_intr(dev, fdc->res_irq,
1798 INTR_TYPE_BIO | INTR_ENTROPY |
1799 ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1800 ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1801 ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1802 fdc, &fdc->fdc_intr);
1803 if (error) {
1804 device_printf(dev, "cannot setup interrupt\n");
1805 return (error);
1806 }
1807 if (!(fdc->flags & FDC_NODMA)) {
1808 error = isa_dma_acquire(fdc->dmachan);
1809 if (!error) {
1810 error = isa_dma_init(fdc->dmachan,
1811 MAX_BYTES_PER_CYL, M_WAITOK);
1812 if (error)
1813 isa_dma_release(fdc->dmachan);
1814 }
1815 if (error)
1816 return (error);
1817 }
1818 fdc->fdcu = device_get_unit(dev);
1819 fdc->flags |= FDC_NEEDS_RESET;
1820
1821 mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1822
1823 /* reset controller, turn motor off, clear fdout mirror reg */
1824 fdout_wr(fdc, fdc->fdout = 0);
1825 bioq_init(&fdc->head);
1826
1827 kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1828 "fdc%d", device_get_unit(dev));
1829
1830 settle = hz / 8;
1831
1832 return (0);
1833}
1834
1835int
1836fdc_hints_probe(device_t dev)
1837{
1838 const char *name, *dname;
1839 int i, error, dunit;
1840
1841 /*
1842 * Probe and attach any children. We should probably detect
1843 * devices from the BIOS unless overridden.
1844 */
1845 name = device_get_nameunit(dev);
1846 i = 0;
1847 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1848 resource_int_value(dname, dunit, "drive", &dunit);
1849 fdc_add_child(dev, dname, dunit);
1850 }
1851
1852 if ((error = bus_generic_attach(dev)) != 0)
1853 return (error);
1854 return (0);
1855}
1856
1857int
1858fdc_print_child(device_t me, device_t child)
1859{
1860 int retval = 0, flags;
1861
1862 retval += bus_print_child_header(me, child);
1863 retval += printf(" on %s drive %d", device_get_nameunit(me),
1864 fdc_get_fdunit(child));
1865 if ((flags = device_get_flags(me)) != 0)
1866 retval += printf(" flags %#x", flags);
1867 retval += printf("\n");
1868
1869 return (retval);
1870}
1871
1872/*
1873 * Configuration/initialization, per drive.
1874 */
1875static int
1876fd_probe(device_t dev)
1877{
1878 int i, unit;
1879 u_int st0, st3;
1880 struct fd_data *fd;
1881 struct fdc_data *fdc;
1882 int fdsu;
1883 int flags, type;
1884
1885 fdsu = fdc_get_fdunit(dev);
1886 fd = device_get_softc(dev);
1887 fdc = device_get_softc(device_get_parent(dev));
1888 flags = device_get_flags(dev);
1889
1890 fd->dev = dev;
1891 fd->fdc = fdc;
1892 fd->fdsu = fdsu;
1893 unit = device_get_unit(dev);
1894
1895 /* Auto-probe if fdinfo is present, but always allow override. */
1896 type = flags & FD_TYPEMASK;
1897 if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1898 fd->type = type;
1899 goto done;
1900 } else {
1901 /* make sure fdautoselect() will be called */
1902 fd->flags = FD_EMPTY;
1903 fd->type = type;
1904 }
1905
1906#if (defined(__i386__) && !defined(PC98)) || defined(__amd64__)
1907 if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1908 /* Look up what the BIOS thinks we have. */
1909 if (unit == 0)
1910 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1911 else
1912 fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1913 if (fd->type == FDT_288M_1)
1914 fd->type = FDT_288M;
1915 }
1916#endif /* __i386__ || __amd64__ */
1917 /* is there a unit? */
1918 if (fd->type == FDT_NONE)
1919 return (ENXIO);
1920
1921/*
1922 mtx_lock(&fdc->fdc_mtx);
1923*/
1924 /* select it */
1925 fd_select(fd);
1926 fd_motor(fd, 1);
1927 fdc->fd = fd;
1928 fdc_reset(fdc); /* XXX reset, then unreset, etc. */
1929 DELAY(1000000); /* 1 sec */
1930
1931 if ((flags & FD_NO_PROBE) == 0) {
1932 /* If we're at track 0 first seek inwards. */
1933 if ((fdc_sense_drive(fdc, &st3) == 0) &&
1934 (st3 & NE7_ST3_T0)) {
1935 /* Seek some steps... */
1936 if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1937 /* ...wait a moment... */
1938 DELAY(300000);
1939 /* make ctrlr happy: */
1940 fdc_sense_int(fdc, NULL, NULL);
1941 }
1942 }
1943
1944 for (i = 0; i < 2; i++) {
1945 /*
1946 * we must recalibrate twice, just in case the
1947 * heads have been beyond cylinder 76, since
1948 * most FDCs still barf when attempting to
1949 * recalibrate more than 77 steps
1950 */
1951 /* go back to 0: */
1952 if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1953 /* a second being enough for full stroke seek*/
1954 DELAY(i == 0 ? 1000000 : 300000);
1955
1956 /* anything responding? */
1957 if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1958 (st0 & NE7_ST0_EC) == 0)
1959 break; /* already probed succesfully */
1960 }
1961 }
1962 }
1963
1964 fd_motor(fd, 0);
1965 fdc->fd = NULL;
1966/*
1967 mtx_unlock(&fdc->fdc_mtx);
1968*/
1969
1970 if ((flags & FD_NO_PROBE) == 0 &&
1971 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1972 return (ENXIO);
1973
1974done:
1975
1976 switch (fd->type) {
1977 case FDT_12M:
1978 device_set_desc(dev, "1200-KB 5.25\" drive");
1979 break;
1980 case FDT_144M:
1981 device_set_desc(dev, "1440-KB 3.5\" drive");
1982 break;
1983 case FDT_288M:
1984 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
1985 break;
1986 case FDT_360K:
1987 device_set_desc(dev, "360-KB 5.25\" drive");
1988 break;
1989 case FDT_720K:
1990 device_set_desc(dev, "720-KB 3.5\" drive");
1991 break;
1992 default:
1993 return (ENXIO);
1994 }
1995 fd->track = FD_NO_TRACK;
1996 fd->fdc = fdc;
1997 fd->fdsu = fdsu;
1998 fd->options = 0;
1999 callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
2000
2001 /* initialize densities for subdevices */
2002 fdsettype(fd, fd_native_types[fd->type]);
2003 return (0);
2004}
2005
2006/*
2007 * We have to do this in a geom event because GEOM is not running
2008 * when fd_attach() is.
2009 * XXX: move fd_attach after geom like ata/scsi disks
2010 */
2011static void
2012fd_attach2(void *arg, int flag)
2013{
2014 struct fd_data *fd;
2015
2016 fd = arg;
2017
2018 fd->fd_geom = g_new_geomf(&g_fd_class,
2019 "fd%d", device_get_unit(fd->dev));
2020 fd->fd_provider = g_new_providerf(fd->fd_geom, fd->fd_geom->name);
2021 fd->fd_geom->softc = fd;
2022 g_error_provider(fd->fd_provider, 0);
2023}
2024
2025static int
2026fd_attach(device_t dev)
2027{
2028 struct fd_data *fd;
2029
2030 fd = device_get_softc(dev);
2031 g_post_event(fd_attach2, fd, M_WAITOK, NULL);
2032 fd->flags |= FD_EMPTY;
2033 bioq_init(&fd->fd_bq);
2034
2035 return (0);
2036}
2037
2038static int
2039fd_detach(device_t dev)
2040{
2041 struct fd_data *fd;
2042
2043 fd = device_get_softc(dev);
2044 g_topology_lock();
2045 g_wither_geom(fd->fd_geom, ENXIO);
2046 g_topology_unlock();
2047 while (device_get_state(dev) == DS_BUSY)
2048 tsleep(fd, PZERO, "fdd", hz/10);
2049 callout_drain(&fd->toffhandle);
2050
2051 return (0);
2052}
2053
2054static device_method_t fd_methods[] = {
2055 /* Device interface */
2056 DEVMETHOD(device_probe, fd_probe),
2057 DEVMETHOD(device_attach, fd_attach),
2058 DEVMETHOD(device_detach, fd_detach),
2059 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2060 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */
2061 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */
2062 { 0, 0 }
2063};
2064
2065static driver_t fd_driver = {
2066 "fd",
2067 fd_methods,
2068 sizeof(struct fd_data)
2069};
2070
2071static int
2072fdc_modevent(module_t mod, int type, void *data)
2073{
2074
2075 g_modevent(NULL, type, &g_fd_class);
2076 return (0);
2077}
2078
2079DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);