fd.c revision 1.12 1 1.12 fvdl /* $NetBSD: fd.c,v 1.12 2001/01/08 02:03:47 fvdl Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Charles M. Hannum.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /*-
40 1.1 thorpej * Copyright (c) 1990 The Regents of the University of California.
41 1.1 thorpej * All rights reserved.
42 1.1 thorpej *
43 1.1 thorpej * This code is derived from software contributed to Berkeley by
44 1.1 thorpej * Don Ahn.
45 1.1 thorpej *
46 1.1 thorpej * Redistribution and use in source and binary forms, with or without
47 1.1 thorpej * modification, are permitted provided that the following conditions
48 1.1 thorpej * are met:
49 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
50 1.1 thorpej * notice, this list of conditions and the following disclaimer.
51 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
53 1.1 thorpej * documentation and/or other materials provided with the distribution.
54 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
55 1.1 thorpej * must display the following acknowledgement:
56 1.1 thorpej * This product includes software developed by the University of
57 1.1 thorpej * California, Berkeley and its contributors.
58 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
59 1.1 thorpej * may be used to endorse or promote products derived from this software
60 1.1 thorpej * without specific prior written permission.
61 1.1 thorpej *
62 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.1 thorpej * SUCH DAMAGE.
73 1.1 thorpej *
74 1.1 thorpej * @(#)fd.c 7.4 (Berkeley) 5/25/91
75 1.1 thorpej */
76 1.1 thorpej
77 1.1 thorpej /*
78 1.1 thorpej * Floppy formatting facilities merged from FreeBSD fd.c driver:
79 1.1 thorpej * Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
80 1.1 thorpej * which carries the same copyright/redistribution notice as shown above with
81 1.1 thorpej * the addition of the following statement before the "Redistribution and
82 1.1 thorpej * use ..." clause:
83 1.1 thorpej *
84 1.1 thorpej * Copyright (c) 1993, 1994 by
85 1.1 thorpej * jc (at) irbs.UUCP (John Capo)
86 1.1 thorpej * vak (at) zebub.msk.su (Serge Vakulenko)
87 1.1 thorpej * ache (at) astral.msk.su (Andrew A. Chernov)
88 1.1 thorpej *
89 1.1 thorpej * Copyright (c) 1993, 1994, 1995 by
90 1.1 thorpej * joerg_wunsch (at) uriah.sax.de (Joerg Wunsch)
91 1.1 thorpej * dufault (at) hda.com (Peter Dufault)
92 1.1 thorpej */
93 1.1 thorpej
94 1.1 thorpej #include "rnd.h"
95 1.1 thorpej #include "opt_ddb.h"
96 1.1 thorpej
97 1.1 thorpej /*
98 1.1 thorpej * XXX This driver should be properly MI'd some day, but this allows us
99 1.1 thorpej * XXX to eliminate a lot of code duplication for now.
100 1.1 thorpej */
101 1.9 matt #if !defined(alpha) && !defined(bebox) && !defined(i386) && !defined(prep)
102 1.1 thorpej #error platform not supported by this driver, yet
103 1.1 thorpej #endif
104 1.1 thorpej
105 1.1 thorpej #include <sys/param.h>
106 1.1 thorpej #include <sys/systm.h>
107 1.1 thorpej #include <sys/callout.h>
108 1.1 thorpej #include <sys/kernel.h>
109 1.1 thorpej #include <sys/file.h>
110 1.1 thorpej #include <sys/ioctl.h>
111 1.1 thorpej #include <sys/device.h>
112 1.1 thorpej #include <sys/disklabel.h>
113 1.1 thorpej #include <sys/dkstat.h>
114 1.1 thorpej #include <sys/disk.h>
115 1.1 thorpej #include <sys/buf.h>
116 1.1 thorpej #include <sys/malloc.h>
117 1.1 thorpej #include <sys/uio.h>
118 1.1 thorpej #include <sys/syslog.h>
119 1.1 thorpej #include <sys/queue.h>
120 1.1 thorpej #include <sys/proc.h>
121 1.1 thorpej #include <sys/fdio.h>
122 1.1 thorpej #if NRND > 0
123 1.1 thorpej #include <sys/rnd.h>
124 1.1 thorpej #endif
125 1.1 thorpej
126 1.1 thorpej #include <uvm/uvm_extern.h>
127 1.1 thorpej
128 1.1 thorpej #include <dev/cons.h>
129 1.1 thorpej
130 1.1 thorpej #include <machine/cpu.h>
131 1.1 thorpej #include <machine/bus.h>
132 1.1 thorpej #include <machine/conf.h>
133 1.1 thorpej #include <machine/intr.h>
134 1.1 thorpej
135 1.1 thorpej #include <dev/isa/isavar.h>
136 1.1 thorpej #include <dev/isa/isadmavar.h>
137 1.3 thorpej
138 1.1 thorpej #include <dev/isa/fdreg.h>
139 1.3 thorpej #include <dev/isa/fdcvar.h>
140 1.1 thorpej
141 1.9 matt #if defined(i386)
142 1.5 jdolecek
143 1.1 thorpej #include <dev/ic/mc146818reg.h> /* for NVRAM access */
144 1.1 thorpej #include <i386/isa/nvram.h>
145 1.5 jdolecek
146 1.5 jdolecek #include "mca.h"
147 1.5 jdolecek #if NMCA > 0
148 1.5 jdolecek #include <machine/mca_machdep.h> /* for MCA_system */
149 1.5 jdolecek #endif
150 1.5 jdolecek
151 1.9 matt #endif /* i386 */
152 1.1 thorpej
153 1.1 thorpej #define FDUNIT(dev) (minor(dev) / 8)
154 1.1 thorpej #define FDTYPE(dev) (minor(dev) % 8)
155 1.1 thorpej
156 1.1 thorpej /* XXX misuse a flag to identify format operation */
157 1.1 thorpej #define B_FORMAT B_XXX
158 1.1 thorpej
159 1.1 thorpej /* controller driver configuration */
160 1.1 thorpej int fdprint __P((void *, const char *));
161 1.1 thorpej
162 1.1 thorpej /*
163 1.1 thorpej * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
164 1.1 thorpej * we tell them apart.
165 1.1 thorpej */
166 1.1 thorpej struct fd_type {
167 1.1 thorpej int sectrac; /* sectors per track */
168 1.1 thorpej int heads; /* number of heads */
169 1.1 thorpej int seccyl; /* sectors per cylinder */
170 1.1 thorpej int secsize; /* size code for sectors */
171 1.1 thorpej int datalen; /* data len when secsize = 0 */
172 1.1 thorpej int steprate; /* step rate and head unload time */
173 1.1 thorpej int gap1; /* gap len between sectors */
174 1.1 thorpej int gap2; /* formatting gap */
175 1.1 thorpej int cyls; /* total num of cylinders */
176 1.1 thorpej int size; /* size of disk in sectors */
177 1.1 thorpej int step; /* steps per cylinder */
178 1.1 thorpej int rate; /* transfer speed code */
179 1.1 thorpej u_char fillbyte; /* format fill byte */
180 1.1 thorpej u_char interleave; /* interleave factor (formatting) */
181 1.1 thorpej char *name;
182 1.1 thorpej };
183 1.1 thorpej
184 1.5 jdolecek #if NMCA > 0
185 1.5 jdolecek /* MCA - specific entries */
186 1.5 jdolecek struct fd_type mca_fd_types[] = {
187 1.5 jdolecek { 18,2,36,2,0xff,0x0f,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette - XXX try 16ms step rate */
188 1.5 jdolecek { 9,2,18,2,0xff,0x4f,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette - XXX try 24ms step rate */
189 1.5 jdolecek };
190 1.5 jdolecek #endif /* NMCA > 0 */
191 1.5 jdolecek
192 1.1 thorpej /* The order of entries in the following table is important -- BEWARE! */
193 1.1 thorpej struct fd_type fd_types[] = {
194 1.1 thorpej { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
195 1.1 thorpej { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB" }, /* 1.2 MB AT-diskettes */
196 1.1 thorpej { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
197 1.1 thorpej { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
198 1.1 thorpej { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette */
199 1.1 thorpej { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x" }, /* 720kB in 1.2MB drive */
200 1.1 thorpej { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */
201 1.1 thorpej };
202 1.1 thorpej
203 1.1 thorpej /* software state, per disk (with up to 4 disks per ctlr) */
204 1.1 thorpej struct fd_softc {
205 1.1 thorpej struct device sc_dev;
206 1.1 thorpej struct disk sc_dk;
207 1.1 thorpej
208 1.1 thorpej struct fd_type *sc_deftype; /* default type descriptor */
209 1.1 thorpej struct fd_type *sc_type; /* current type descriptor */
210 1.1 thorpej struct fd_type sc_type_copy; /* copy for fiddling when formatting */
211 1.1 thorpej
212 1.2 thorpej struct callout sc_motoron_ch;
213 1.2 thorpej struct callout sc_motoroff_ch;
214 1.1 thorpej
215 1.1 thorpej daddr_t sc_blkno; /* starting block number */
216 1.1 thorpej int sc_bcount; /* byte count left */
217 1.1 thorpej int sc_opts; /* user-set options */
218 1.1 thorpej int sc_skip; /* bytes already transferred */
219 1.1 thorpej int sc_nblks; /* number of blocks currently tranferring */
220 1.1 thorpej int sc_nbytes; /* number of bytes currently tranferring */
221 1.1 thorpej
222 1.1 thorpej int sc_drive; /* physical unit number */
223 1.1 thorpej int sc_flags;
224 1.1 thorpej #define FD_OPEN 0x01 /* it's open */
225 1.1 thorpej #define FD_MOTOR 0x02 /* motor should be on */
226 1.1 thorpej #define FD_MOTOR_WAIT 0x04 /* motor coming up */
227 1.1 thorpej int sc_cylin; /* where we think the head is */
228 1.1 thorpej
229 1.1 thorpej void *sc_sdhook; /* saved shutdown hook for drive. */
230 1.1 thorpej
231 1.1 thorpej TAILQ_ENTRY(fd_softc) sc_drivechain;
232 1.1 thorpej int sc_ops; /* I/O ops since last switch */
233 1.1 thorpej struct buf_queue sc_q; /* pending I/O requests */
234 1.1 thorpej int sc_active; /* number of active I/O operations */
235 1.1 thorpej
236 1.1 thorpej #if NRND > 0
237 1.1 thorpej rndsource_element_t rnd_source;
238 1.1 thorpej #endif
239 1.1 thorpej };
240 1.1 thorpej
241 1.1 thorpej int fdprobe __P((struct device *, struct cfdata *, void *));
242 1.1 thorpej void fdattach __P((struct device *, struct device *, void *));
243 1.1 thorpej
244 1.3 thorpej extern struct cfdriver fd_cd;
245 1.3 thorpej
246 1.1 thorpej struct cfattach fd_ca = {
247 1.3 thorpej sizeof(struct fd_softc), fdprobe, fdattach,
248 1.1 thorpej };
249 1.1 thorpej
250 1.1 thorpej void fdgetdisklabel __P((struct fd_softc *));
251 1.1 thorpej int fd_get_parms __P((struct fd_softc *));
252 1.1 thorpej void fdstrategy __P((struct buf *));
253 1.1 thorpej void fdstart __P((struct fd_softc *));
254 1.1 thorpej
255 1.1 thorpej struct dkdriver fddkdriver = { fdstrategy };
256 1.1 thorpej
257 1.9 matt #if defined(i386)
258 1.1 thorpej struct fd_type *fd_nvtotype __P((char *, int, int));
259 1.9 matt #endif /* i386 */
260 1.1 thorpej void fd_set_motor __P((struct fdc_softc *fdc, int reset));
261 1.1 thorpej void fd_motor_off __P((void *arg));
262 1.1 thorpej void fd_motor_on __P((void *arg));
263 1.1 thorpej int fdcresult __P((struct fdc_softc *fdc));
264 1.1 thorpej void fdcstart __P((struct fdc_softc *fdc));
265 1.1 thorpej void fdcstatus __P((struct device *dv, int n, char *s));
266 1.1 thorpej void fdctimeout __P((void *arg));
267 1.1 thorpej void fdcpseudointr __P((void *arg));
268 1.1 thorpej void fdcretry __P((struct fdc_softc *fdc));
269 1.1 thorpej void fdfinish __P((struct fd_softc *fd, struct buf *bp));
270 1.1 thorpej __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
271 1.1 thorpej int fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
272 1.1 thorpej
273 1.1 thorpej void fd_mountroot_hook __P((struct device *));
274 1.1 thorpej
275 1.1 thorpej /*
276 1.1 thorpej * Arguments passed between fdcattach and fdprobe.
277 1.1 thorpej */
278 1.1 thorpej struct fdc_attach_args {
279 1.1 thorpej int fa_drive;
280 1.1 thorpej struct fd_type *fa_deftype;
281 1.1 thorpej };
282 1.1 thorpej
283 1.1 thorpej /*
284 1.1 thorpej * Print the location of a disk drive (called just before attaching the
285 1.1 thorpej * the drive). If `fdc' is not NULL, the drive was found but was not
286 1.1 thorpej * in the system config file; print the drive name as well.
287 1.1 thorpej * Return QUIET (config_find ignores this if the device was configured) to
288 1.1 thorpej * avoid printing `fdN not configured' messages.
289 1.1 thorpej */
290 1.1 thorpej int
291 1.1 thorpej fdprint(aux, fdc)
292 1.1 thorpej void *aux;
293 1.1 thorpej const char *fdc;
294 1.1 thorpej {
295 1.1 thorpej register struct fdc_attach_args *fa = aux;
296 1.1 thorpej
297 1.1 thorpej if (!fdc)
298 1.1 thorpej printf(" drive %d", fa->fa_drive);
299 1.1 thorpej return QUIET;
300 1.1 thorpej }
301 1.1 thorpej
302 1.1 thorpej void
303 1.3 thorpej fdcattach(fdc)
304 1.3 thorpej struct fdc_softc *fdc;
305 1.1 thorpej {
306 1.1 thorpej struct fdc_attach_args fa;
307 1.10 jmc bus_space_tag_t iot;
308 1.10 jmc bus_space_handle_t ioh;
309 1.9 matt #if defined(i386)
310 1.1 thorpej int type;
311 1.1 thorpej #endif
312 1.1 thorpej
313 1.10 jmc iot = fdc->sc_iot;
314 1.10 jmc ioh = fdc->sc_ioh;
315 1.1 thorpej callout_init(&fdc->sc_timo_ch);
316 1.1 thorpej callout_init(&fdc->sc_intr_ch);
317 1.1 thorpej
318 1.1 thorpej fdc->sc_state = DEVIDLE;
319 1.1 thorpej TAILQ_INIT(&fdc->sc_drives);
320 1.1 thorpej
321 1.1 thorpej fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
322 1.1 thorpej
323 1.1 thorpej if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
324 1.1 thorpej BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
325 1.1 thorpej printf("%s: can't set up ISA DMA map\n",
326 1.1 thorpej fdc->sc_dev.dv_xname);
327 1.1 thorpej return;
328 1.1 thorpej }
329 1.10 jmc
330 1.10 jmc /*
331 1.10 jmc * Reset the controller to get it into a known state. Not all
332 1.10 jmc * probes necessarily need do this to discover the controller up
333 1.10 jmc * front, so don't assume anything.
334 1.10 jmc */
335 1.10 jmc
336 1.10 jmc bus_space_write_1(iot, ioh, fdout, 0);
337 1.10 jmc delay(100);
338 1.10 jmc bus_space_write_1(iot, ioh, fdout, FDO_FRST);
339 1.10 jmc
340 1.10 jmc /* see if it can handle a command */
341 1.10 jmc if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
342 1.10 jmc printf ("%s: can't reset controller\n",
343 1.10 jmc fdc->sc_dev.dv_xname);
344 1.10 jmc return;
345 1.10 jmc }
346 1.10 jmc out_fdc(iot, ioh, 0xdf);
347 1.10 jmc out_fdc(iot, ioh, 2);
348 1.10 jmc
349 1.1 thorpej
350 1.9 matt #if defined(i386)
351 1.1 thorpej /*
352 1.1 thorpej * The NVRAM info only tells us about the first two disks on the
353 1.1 thorpej * `primary' floppy controller.
354 1.1 thorpej */
355 1.1 thorpej if (fdc->sc_dev.dv_unit == 0)
356 1.1 thorpej type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
357 1.1 thorpej else
358 1.1 thorpej type = -1;
359 1.9 matt #endif /* i386 */
360 1.1 thorpej
361 1.1 thorpej /* physical limit: four drives per controller. */
362 1.1 thorpej for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
363 1.9 matt #if defined(i386)
364 1.1 thorpej if (type >= 0 && fa.fa_drive < 2)
365 1.1 thorpej fa.fa_deftype = fd_nvtotype(fdc->sc_dev.dv_xname,
366 1.1 thorpej type, fa.fa_drive);
367 1.1 thorpej else
368 1.1 thorpej fa.fa_deftype = NULL; /* unknown */
369 1.1 thorpej #else
370 1.1 thorpej /*
371 1.1 thorpej * Default to 1.44MB on Alpha and BeBox. How do we tell
372 1.1 thorpej * on these platforms?
373 1.1 thorpej */
374 1.1 thorpej fa.fa_deftype = &fd_types[0];
375 1.9 matt #endif /* i386 */
376 1.3 thorpej (void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
377 1.1 thorpej }
378 1.1 thorpej }
379 1.1 thorpej
380 1.1 thorpej int
381 1.1 thorpej fdprobe(parent, match, aux)
382 1.1 thorpej struct device *parent;
383 1.1 thorpej struct cfdata *match;
384 1.1 thorpej void *aux;
385 1.1 thorpej {
386 1.1 thorpej struct fdc_softc *fdc = (void *)parent;
387 1.1 thorpej struct cfdata *cf = match;
388 1.1 thorpej struct fdc_attach_args *fa = aux;
389 1.1 thorpej int drive = fa->fa_drive;
390 1.1 thorpej bus_space_tag_t iot = fdc->sc_iot;
391 1.1 thorpej bus_space_handle_t ioh = fdc->sc_ioh;
392 1.1 thorpej int n;
393 1.1 thorpej
394 1.1 thorpej if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
395 1.1 thorpej cf->cf_loc[FDCCF_DRIVE] != drive)
396 1.1 thorpej return 0;
397 1.1 thorpej /*
398 1.1 thorpej * XXX
399 1.1 thorpej * This is to work around some odd interactions between this driver
400 1.1 thorpej * and SMC Ethernet cards.
401 1.1 thorpej */
402 1.1 thorpej if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
403 1.1 thorpej return 0;
404 1.1 thorpej
405 1.1 thorpej /* select drive and turn on motor */
406 1.1 thorpej bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
407 1.1 thorpej /* wait for motor to spin up */
408 1.1 thorpej delay(250000);
409 1.1 thorpej out_fdc(iot, ioh, NE7CMD_RECAL);
410 1.1 thorpej out_fdc(iot, ioh, drive);
411 1.1 thorpej /* wait for recalibrate */
412 1.1 thorpej delay(2000000);
413 1.1 thorpej out_fdc(iot, ioh, NE7CMD_SENSEI);
414 1.1 thorpej n = fdcresult(fdc);
415 1.1 thorpej #ifdef FD_DEBUG
416 1.1 thorpej {
417 1.1 thorpej int i;
418 1.1 thorpej printf("fdprobe: status");
419 1.1 thorpej for (i = 0; i < n; i++)
420 1.1 thorpej printf(" %x", fdc->sc_status[i]);
421 1.1 thorpej printf("\n");
422 1.1 thorpej }
423 1.1 thorpej #endif
424 1.1 thorpej /* turn off motor */
425 1.1 thorpej bus_space_write_1(iot, ioh, fdout, FDO_FRST);
426 1.1 thorpej
427 1.1 thorpej #if defined(bebox) /* XXX What is this about? --thorpej (at) netbsd.org */
428 1.1 thorpej if (n != 2 || (fdc->sc_status[1] != 0))
429 1.1 thorpej return 0;
430 1.1 thorpej #else
431 1.1 thorpej if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
432 1.1 thorpej return 0;
433 1.1 thorpej #endif /* bebox */
434 1.1 thorpej
435 1.1 thorpej return 1;
436 1.1 thorpej }
437 1.1 thorpej
438 1.1 thorpej /*
439 1.1 thorpej * Controller is working, and drive responded. Attach it.
440 1.1 thorpej */
441 1.1 thorpej void
442 1.1 thorpej fdattach(parent, self, aux)
443 1.1 thorpej struct device *parent, *self;
444 1.1 thorpej void *aux;
445 1.1 thorpej {
446 1.1 thorpej struct fdc_softc *fdc = (void *)parent;
447 1.1 thorpej struct fd_softc *fd = (void *)self;
448 1.1 thorpej struct fdc_attach_args *fa = aux;
449 1.1 thorpej struct fd_type *type = fa->fa_deftype;
450 1.1 thorpej int drive = fa->fa_drive;
451 1.1 thorpej
452 1.2 thorpej callout_init(&fd->sc_motoron_ch);
453 1.2 thorpej callout_init(&fd->sc_motoroff_ch);
454 1.1 thorpej
455 1.1 thorpej /* XXX Allow `flags' to override device type? */
456 1.1 thorpej
457 1.1 thorpej if (type)
458 1.1 thorpej printf(": %s, %d cyl, %d head, %d sec\n", type->name,
459 1.1 thorpej type->cyls, type->heads, type->sectrac);
460 1.1 thorpej else
461 1.1 thorpej printf(": density unknown\n");
462 1.1 thorpej
463 1.1 thorpej BUFQ_INIT(&fd->sc_q);
464 1.1 thorpej fd->sc_cylin = -1;
465 1.1 thorpej fd->sc_drive = drive;
466 1.1 thorpej fd->sc_deftype = type;
467 1.1 thorpej fdc->sc_fd[drive] = fd;
468 1.1 thorpej
469 1.1 thorpej /*
470 1.1 thorpej * Initialize and attach the disk structure.
471 1.1 thorpej */
472 1.1 thorpej fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
473 1.1 thorpej fd->sc_dk.dk_driver = &fddkdriver;
474 1.1 thorpej disk_attach(&fd->sc_dk);
475 1.1 thorpej
476 1.1 thorpej /*
477 1.1 thorpej * Establish a mountroot hook.
478 1.1 thorpej */
479 1.1 thorpej mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
480 1.1 thorpej
481 1.1 thorpej /* Needed to power off if the motor is on when we halt. */
482 1.1 thorpej fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
483 1.1 thorpej
484 1.1 thorpej #if NRND > 0
485 1.1 thorpej rnd_attach_source(&fd->rnd_source, fd->sc_dev.dv_xname,
486 1.1 thorpej RND_TYPE_DISK, 0);
487 1.1 thorpej #endif
488 1.1 thorpej }
489 1.1 thorpej
490 1.9 matt #if defined(i386)
491 1.1 thorpej /*
492 1.1 thorpej * Translate nvram type into internal data structure. Return NULL for
493 1.1 thorpej * none/unknown/unusable.
494 1.1 thorpej */
495 1.1 thorpej struct fd_type *
496 1.1 thorpej fd_nvtotype(fdc, nvraminfo, drive)
497 1.1 thorpej char *fdc;
498 1.1 thorpej int nvraminfo, drive;
499 1.1 thorpej {
500 1.1 thorpej int type;
501 1.1 thorpej
502 1.1 thorpej type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
503 1.1 thorpej switch (type) {
504 1.1 thorpej case NVRAM_DISKETTE_NONE:
505 1.1 thorpej return NULL;
506 1.1 thorpej case NVRAM_DISKETTE_12M:
507 1.1 thorpej return &fd_types[1];
508 1.1 thorpej case NVRAM_DISKETTE_TYPE5:
509 1.1 thorpej case NVRAM_DISKETTE_TYPE6:
510 1.1 thorpej /* XXX We really ought to handle 2.88MB format. */
511 1.1 thorpej case NVRAM_DISKETTE_144M:
512 1.5 jdolecek #if NMCA > 0
513 1.5 jdolecek if (MCA_system)
514 1.5 jdolecek return &mca_fd_types[0];
515 1.5 jdolecek else
516 1.5 jdolecek #endif /* NMCA > 0 */
517 1.5 jdolecek return &fd_types[0];
518 1.1 thorpej case NVRAM_DISKETTE_360K:
519 1.1 thorpej return &fd_types[3];
520 1.1 thorpej case NVRAM_DISKETTE_720K:
521 1.5 jdolecek #if NMCA > 0
522 1.5 jdolecek if (MCA_system)
523 1.5 jdolecek return &mca_fd_types[1];
524 1.5 jdolecek else
525 1.5 jdolecek #endif /* NMCA > 0 */
526 1.5 jdolecek return &fd_types[4];
527 1.1 thorpej default:
528 1.1 thorpej printf("%s: drive %d: unknown device type 0x%x\n",
529 1.1 thorpej fdc, drive, type);
530 1.1 thorpej return NULL;
531 1.1 thorpej }
532 1.1 thorpej }
533 1.9 matt #endif /* i386 */
534 1.1 thorpej
535 1.1 thorpej __inline struct fd_type *
536 1.1 thorpej fd_dev_to_type(fd, dev)
537 1.1 thorpej struct fd_softc *fd;
538 1.1 thorpej dev_t dev;
539 1.1 thorpej {
540 1.1 thorpej int type = FDTYPE(dev);
541 1.1 thorpej
542 1.1 thorpej if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
543 1.1 thorpej return NULL;
544 1.1 thorpej return type ? &fd_types[type - 1] : fd->sc_deftype;
545 1.1 thorpej }
546 1.1 thorpej
547 1.1 thorpej void
548 1.1 thorpej fdstrategy(bp)
549 1.1 thorpej register struct buf *bp; /* IO operation to perform */
550 1.1 thorpej {
551 1.7 thorpej struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(bp->b_dev));
552 1.1 thorpej int sz;
553 1.1 thorpej int s;
554 1.1 thorpej
555 1.1 thorpej /* Valid unit, controller, and request? */
556 1.1 thorpej if (bp->b_blkno < 0 ||
557 1.1 thorpej ((bp->b_bcount % FDC_BSIZE) != 0 &&
558 1.1 thorpej (bp->b_flags & B_FORMAT) == 0)) {
559 1.1 thorpej bp->b_error = EINVAL;
560 1.1 thorpej goto bad;
561 1.1 thorpej }
562 1.1 thorpej
563 1.1 thorpej /* If it's a null transfer, return immediately. */
564 1.1 thorpej if (bp->b_bcount == 0)
565 1.1 thorpej goto done;
566 1.1 thorpej
567 1.1 thorpej sz = howmany(bp->b_bcount, FDC_BSIZE);
568 1.1 thorpej
569 1.1 thorpej if (bp->b_blkno + sz > fd->sc_type->size) {
570 1.1 thorpej sz = fd->sc_type->size - bp->b_blkno;
571 1.1 thorpej if (sz == 0) {
572 1.1 thorpej /* If exactly at end of disk, return EOF. */
573 1.1 thorpej goto done;
574 1.1 thorpej }
575 1.1 thorpej if (sz < 0) {
576 1.1 thorpej /* If past end of disk, return EINVAL. */
577 1.1 thorpej bp->b_error = EINVAL;
578 1.1 thorpej goto bad;
579 1.1 thorpej }
580 1.1 thorpej /* Otherwise, truncate request. */
581 1.1 thorpej bp->b_bcount = sz << DEV_BSHIFT;
582 1.1 thorpej }
583 1.1 thorpej
584 1.1 thorpej bp->b_rawblkno = bp->b_blkno;
585 1.1 thorpej bp->b_cylinder = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
586 1.1 thorpej
587 1.1 thorpej #ifdef FD_DEBUG
588 1.1 thorpej printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld sz %d\n",
589 1.1 thorpej bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
590 1.1 thorpej #endif
591 1.1 thorpej
592 1.1 thorpej /* Queue transfer on drive, activate drive and controller if idle. */
593 1.1 thorpej s = splbio();
594 1.1 thorpej disksort_cylinder(&fd->sc_q, bp);
595 1.2 thorpej callout_stop(&fd->sc_motoroff_ch); /* a good idea */
596 1.1 thorpej if (fd->sc_active == 0)
597 1.1 thorpej fdstart(fd);
598 1.1 thorpej #ifdef DIAGNOSTIC
599 1.1 thorpej else {
600 1.1 thorpej struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
601 1.1 thorpej if (fdc->sc_state == DEVIDLE) {
602 1.1 thorpej printf("fdstrategy: controller inactive\n");
603 1.1 thorpej fdcstart(fdc);
604 1.1 thorpej }
605 1.1 thorpej }
606 1.1 thorpej #endif
607 1.1 thorpej splx(s);
608 1.1 thorpej return;
609 1.1 thorpej
610 1.1 thorpej bad:
611 1.1 thorpej bp->b_flags |= B_ERROR;
612 1.1 thorpej done:
613 1.1 thorpej /* Toss transfer; we're done early. */
614 1.1 thorpej bp->b_resid = bp->b_bcount;
615 1.1 thorpej biodone(bp);
616 1.1 thorpej }
617 1.1 thorpej
618 1.1 thorpej void
619 1.1 thorpej fdstart(fd)
620 1.1 thorpej struct fd_softc *fd;
621 1.1 thorpej {
622 1.1 thorpej struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
623 1.1 thorpej int active = fdc->sc_drives.tqh_first != 0;
624 1.1 thorpej
625 1.1 thorpej /* Link into controller queue. */
626 1.1 thorpej fd->sc_active = 1;
627 1.1 thorpej TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
628 1.1 thorpej
629 1.1 thorpej /* If controller not already active, start it. */
630 1.1 thorpej if (!active)
631 1.1 thorpej fdcstart(fdc);
632 1.1 thorpej }
633 1.1 thorpej
634 1.1 thorpej void
635 1.1 thorpej fdfinish(fd, bp)
636 1.1 thorpej struct fd_softc *fd;
637 1.1 thorpej struct buf *bp;
638 1.1 thorpej {
639 1.1 thorpej struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
640 1.1 thorpej
641 1.1 thorpej /*
642 1.1 thorpej * Move this drive to the end of the queue to give others a `fair'
643 1.1 thorpej * chance. We only force a switch if N operations are completed while
644 1.1 thorpej * another drive is waiting to be serviced, since there is a long motor
645 1.1 thorpej * startup delay whenever we switch.
646 1.1 thorpej */
647 1.1 thorpej if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
648 1.1 thorpej fd->sc_ops = 0;
649 1.1 thorpej TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
650 1.1 thorpej if (BUFQ_NEXT(bp) != NULL)
651 1.1 thorpej TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
652 1.1 thorpej else
653 1.1 thorpej fd->sc_active = 0;
654 1.1 thorpej }
655 1.1 thorpej bp->b_resid = fd->sc_bcount;
656 1.1 thorpej fd->sc_skip = 0;
657 1.1 thorpej BUFQ_REMOVE(&fd->sc_q, bp);
658 1.1 thorpej
659 1.1 thorpej #if NRND > 0
660 1.1 thorpej rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
661 1.1 thorpej #endif
662 1.1 thorpej
663 1.1 thorpej biodone(bp);
664 1.1 thorpej /* turn off motor 5s from now */
665 1.2 thorpej callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
666 1.1 thorpej fdc->sc_state = DEVIDLE;
667 1.1 thorpej }
668 1.1 thorpej
669 1.1 thorpej int
670 1.1 thorpej fdread(dev, uio, flags)
671 1.1 thorpej dev_t dev;
672 1.1 thorpej struct uio *uio;
673 1.1 thorpej int flags;
674 1.1 thorpej {
675 1.1 thorpej
676 1.1 thorpej return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
677 1.1 thorpej }
678 1.1 thorpej
679 1.1 thorpej int
680 1.1 thorpej fdwrite(dev, uio, flags)
681 1.1 thorpej dev_t dev;
682 1.1 thorpej struct uio *uio;
683 1.1 thorpej int flags;
684 1.1 thorpej {
685 1.1 thorpej
686 1.1 thorpej return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
687 1.1 thorpej }
688 1.1 thorpej
689 1.1 thorpej void
690 1.1 thorpej fd_set_motor(fdc, reset)
691 1.1 thorpej struct fdc_softc *fdc;
692 1.1 thorpej int reset;
693 1.1 thorpej {
694 1.1 thorpej struct fd_softc *fd;
695 1.1 thorpej u_char status;
696 1.1 thorpej int n;
697 1.1 thorpej
698 1.1 thorpej if ((fd = fdc->sc_drives.tqh_first) != NULL)
699 1.1 thorpej status = fd->sc_drive;
700 1.1 thorpej else
701 1.1 thorpej status = 0;
702 1.1 thorpej if (!reset)
703 1.1 thorpej status |= FDO_FRST | FDO_FDMAEN;
704 1.1 thorpej for (n = 0; n < 4; n++)
705 1.1 thorpej if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
706 1.1 thorpej status |= FDO_MOEN(n);
707 1.1 thorpej bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
708 1.1 thorpej }
709 1.1 thorpej
710 1.1 thorpej void
711 1.1 thorpej fd_motor_off(arg)
712 1.1 thorpej void *arg;
713 1.1 thorpej {
714 1.1 thorpej struct fd_softc *fd = arg;
715 1.1 thorpej int s;
716 1.1 thorpej
717 1.1 thorpej s = splbio();
718 1.1 thorpej fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
719 1.1 thorpej fd_set_motor((struct fdc_softc *)fd->sc_dev.dv_parent, 0);
720 1.1 thorpej splx(s);
721 1.1 thorpej }
722 1.1 thorpej
723 1.1 thorpej void
724 1.1 thorpej fd_motor_on(arg)
725 1.1 thorpej void *arg;
726 1.1 thorpej {
727 1.1 thorpej struct fd_softc *fd = arg;
728 1.1 thorpej struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
729 1.1 thorpej int s;
730 1.1 thorpej
731 1.1 thorpej s = splbio();
732 1.1 thorpej fd->sc_flags &= ~FD_MOTOR_WAIT;
733 1.1 thorpej if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
734 1.1 thorpej (void) fdcintr(fdc);
735 1.1 thorpej splx(s);
736 1.1 thorpej }
737 1.1 thorpej
738 1.1 thorpej int
739 1.1 thorpej fdcresult(fdc)
740 1.1 thorpej struct fdc_softc *fdc;
741 1.1 thorpej {
742 1.1 thorpej bus_space_tag_t iot = fdc->sc_iot;
743 1.1 thorpej bus_space_handle_t ioh = fdc->sc_ioh;
744 1.1 thorpej u_char i;
745 1.1 thorpej int j = 100000,
746 1.1 thorpej n = 0;
747 1.1 thorpej
748 1.1 thorpej for (; j; j--) {
749 1.1 thorpej i = bus_space_read_1(iot, ioh, fdsts) &
750 1.1 thorpej (NE7_DIO | NE7_RQM | NE7_CB);
751 1.1 thorpej if (i == NE7_RQM)
752 1.1 thorpej return n;
753 1.1 thorpej if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
754 1.1 thorpej if (n >= sizeof(fdc->sc_status)) {
755 1.1 thorpej log(LOG_ERR, "fdcresult: overrun\n");
756 1.1 thorpej return -1;
757 1.1 thorpej }
758 1.1 thorpej fdc->sc_status[n++] =
759 1.1 thorpej bus_space_read_1(iot, ioh, fddata);
760 1.1 thorpej }
761 1.1 thorpej delay(10);
762 1.1 thorpej }
763 1.1 thorpej log(LOG_ERR, "fdcresult: timeout\n");
764 1.1 thorpej return -1;
765 1.1 thorpej }
766 1.1 thorpej
767 1.1 thorpej int
768 1.1 thorpej out_fdc(iot, ioh, x)
769 1.1 thorpej bus_space_tag_t iot;
770 1.1 thorpej bus_space_handle_t ioh;
771 1.1 thorpej u_char x;
772 1.1 thorpej {
773 1.1 thorpej int i = 100000;
774 1.1 thorpej
775 1.1 thorpej while ((bus_space_read_1(iot, ioh, fdsts) & NE7_DIO) && i-- > 0);
776 1.1 thorpej if (i <= 0)
777 1.1 thorpej return -1;
778 1.1 thorpej while ((bus_space_read_1(iot, ioh, fdsts) & NE7_RQM) == 0 && i-- > 0);
779 1.1 thorpej if (i <= 0)
780 1.1 thorpej return -1;
781 1.1 thorpej bus_space_write_1(iot, ioh, fddata, x);
782 1.1 thorpej return 0;
783 1.1 thorpej }
784 1.1 thorpej
785 1.1 thorpej int
786 1.1 thorpej fdopen(dev, flags, mode, p)
787 1.1 thorpej dev_t dev;
788 1.1 thorpej int flags;
789 1.1 thorpej int mode;
790 1.1 thorpej struct proc *p;
791 1.1 thorpej {
792 1.1 thorpej struct fd_softc *fd;
793 1.1 thorpej struct fd_type *type;
794 1.1 thorpej
795 1.7 thorpej fd = device_lookup(&fd_cd, FDUNIT(dev));
796 1.7 thorpej if (fd == NULL)
797 1.7 thorpej return (ENXIO);
798 1.7 thorpej
799 1.1 thorpej type = fd_dev_to_type(fd, dev);
800 1.1 thorpej if (type == NULL)
801 1.1 thorpej return ENXIO;
802 1.1 thorpej
803 1.1 thorpej if ((fd->sc_flags & FD_OPEN) != 0 &&
804 1.1 thorpej memcmp(fd->sc_type, type, sizeof(*type)))
805 1.1 thorpej return EBUSY;
806 1.1 thorpej
807 1.1 thorpej fd->sc_type_copy = *type;
808 1.1 thorpej fd->sc_type = &fd->sc_type_copy;
809 1.1 thorpej fd->sc_cylin = -1;
810 1.1 thorpej fd->sc_flags |= FD_OPEN;
811 1.1 thorpej
812 1.1 thorpej return 0;
813 1.1 thorpej }
814 1.1 thorpej
815 1.1 thorpej int
816 1.1 thorpej fdclose(dev, flags, mode, p)
817 1.1 thorpej dev_t dev;
818 1.1 thorpej int flags;
819 1.1 thorpej int mode;
820 1.1 thorpej struct proc *p;
821 1.1 thorpej {
822 1.7 thorpej struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
823 1.1 thorpej
824 1.1 thorpej fd->sc_flags &= ~FD_OPEN;
825 1.1 thorpej fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
826 1.1 thorpej return 0;
827 1.1 thorpej }
828 1.1 thorpej
829 1.1 thorpej void
830 1.1 thorpej fdcstart(fdc)
831 1.1 thorpej struct fdc_softc *fdc;
832 1.1 thorpej {
833 1.1 thorpej
834 1.1 thorpej #ifdef DIAGNOSTIC
835 1.1 thorpej /* only got here if controller's drive queue was inactive; should
836 1.1 thorpej be in idle state */
837 1.1 thorpej if (fdc->sc_state != DEVIDLE) {
838 1.1 thorpej printf("fdcstart: not idle\n");
839 1.1 thorpej return;
840 1.1 thorpej }
841 1.1 thorpej #endif
842 1.1 thorpej (void) fdcintr(fdc);
843 1.1 thorpej }
844 1.1 thorpej
845 1.1 thorpej void
846 1.1 thorpej fdcstatus(dv, n, s)
847 1.1 thorpej struct device *dv;
848 1.1 thorpej int n;
849 1.1 thorpej char *s;
850 1.1 thorpej {
851 1.1 thorpej struct fdc_softc *fdc = (void *)dv->dv_parent;
852 1.1 thorpej char bits[64];
853 1.1 thorpej
854 1.1 thorpej if (n == 0) {
855 1.1 thorpej out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
856 1.1 thorpej (void) fdcresult(fdc);
857 1.1 thorpej n = 2;
858 1.1 thorpej }
859 1.1 thorpej
860 1.1 thorpej printf("%s: %s", dv->dv_xname, s);
861 1.1 thorpej
862 1.1 thorpej switch (n) {
863 1.1 thorpej case 0:
864 1.1 thorpej printf("\n");
865 1.1 thorpej break;
866 1.1 thorpej case 2:
867 1.1 thorpej printf(" (st0 %s cyl %d)\n",
868 1.1 thorpej bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
869 1.1 thorpej bits, sizeof(bits)), fdc->sc_status[1]);
870 1.1 thorpej break;
871 1.1 thorpej case 7:
872 1.1 thorpej printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
873 1.1 thorpej NE7_ST0BITS, bits, sizeof(bits)));
874 1.1 thorpej printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
875 1.1 thorpej NE7_ST1BITS, bits, sizeof(bits)));
876 1.1 thorpej printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
877 1.1 thorpej NE7_ST2BITS, bits, sizeof(bits)));
878 1.1 thorpej printf(" cyl %d head %d sec %d)\n",
879 1.1 thorpej fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
880 1.1 thorpej break;
881 1.1 thorpej #ifdef DIAGNOSTIC
882 1.1 thorpej default:
883 1.1 thorpej printf("\nfdcstatus: weird size");
884 1.1 thorpej break;
885 1.1 thorpej #endif
886 1.1 thorpej }
887 1.1 thorpej }
888 1.1 thorpej
889 1.1 thorpej void
890 1.1 thorpej fdctimeout(arg)
891 1.1 thorpej void *arg;
892 1.1 thorpej {
893 1.1 thorpej struct fdc_softc *fdc = arg;
894 1.1 thorpej struct fd_softc *fd = fdc->sc_drives.tqh_first;
895 1.1 thorpej int s;
896 1.1 thorpej
897 1.1 thorpej s = splbio();
898 1.1 thorpej #ifdef DEBUG
899 1.1 thorpej log(LOG_ERR,"fdctimeout: state %d\n", fdc->sc_state);
900 1.1 thorpej #endif
901 1.1 thorpej fdcstatus(&fd->sc_dev, 0, "timeout");
902 1.1 thorpej
903 1.1 thorpej if (BUFQ_FIRST(&fd->sc_q) != NULL)
904 1.1 thorpej fdc->sc_state++;
905 1.1 thorpej else
906 1.1 thorpej fdc->sc_state = DEVIDLE;
907 1.1 thorpej
908 1.1 thorpej (void) fdcintr(fdc);
909 1.1 thorpej splx(s);
910 1.1 thorpej }
911 1.1 thorpej
912 1.1 thorpej void
913 1.1 thorpej fdcpseudointr(arg)
914 1.1 thorpej void *arg;
915 1.1 thorpej {
916 1.1 thorpej int s;
917 1.1 thorpej
918 1.1 thorpej /* Just ensure it has the right spl. */
919 1.1 thorpej s = splbio();
920 1.1 thorpej (void) fdcintr(arg);
921 1.1 thorpej splx(s);
922 1.1 thorpej }
923 1.1 thorpej
924 1.1 thorpej int
925 1.1 thorpej fdcintr(arg)
926 1.1 thorpej void *arg;
927 1.1 thorpej {
928 1.1 thorpej struct fdc_softc *fdc = arg;
929 1.1 thorpej #define st0 fdc->sc_status[0]
930 1.1 thorpej #define cyl fdc->sc_status[1]
931 1.1 thorpej struct fd_softc *fd;
932 1.1 thorpej struct buf *bp;
933 1.1 thorpej bus_space_tag_t iot = fdc->sc_iot;
934 1.1 thorpej bus_space_handle_t ioh = fdc->sc_ioh;
935 1.1 thorpej int read, head, sec, i, nblks;
936 1.1 thorpej struct fd_type *type;
937 1.1 thorpej struct ne7_fd_formb *finfo = NULL;
938 1.1 thorpej
939 1.1 thorpej loop:
940 1.1 thorpej /* Is there a drive for the controller to do a transfer with? */
941 1.1 thorpej fd = fdc->sc_drives.tqh_first;
942 1.1 thorpej if (fd == NULL) {
943 1.1 thorpej fdc->sc_state = DEVIDLE;
944 1.1 thorpej return 1;
945 1.1 thorpej }
946 1.1 thorpej
947 1.1 thorpej /* Is there a transfer to this drive? If not, deactivate drive. */
948 1.1 thorpej bp = BUFQ_FIRST(&fd->sc_q);
949 1.1 thorpej if (bp == NULL) {
950 1.1 thorpej fd->sc_ops = 0;
951 1.1 thorpej TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
952 1.1 thorpej fd->sc_active = 0;
953 1.1 thorpej goto loop;
954 1.1 thorpej }
955 1.1 thorpej
956 1.1 thorpej if (bp->b_flags & B_FORMAT)
957 1.1 thorpej finfo = (struct ne7_fd_formb *)bp->b_data;
958 1.1 thorpej
959 1.1 thorpej switch (fdc->sc_state) {
960 1.1 thorpej case DEVIDLE:
961 1.1 thorpej fdc->sc_errors = 0;
962 1.1 thorpej fd->sc_skip = 0;
963 1.1 thorpej fd->sc_bcount = bp->b_bcount;
964 1.1 thorpej fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
965 1.2 thorpej callout_stop(&fd->sc_motoroff_ch);
966 1.1 thorpej if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
967 1.1 thorpej fdc->sc_state = MOTORWAIT;
968 1.1 thorpej return 1;
969 1.1 thorpej }
970 1.1 thorpej if ((fd->sc_flags & FD_MOTOR) == 0) {
971 1.1 thorpej /* Turn on the motor, being careful about pairing. */
972 1.1 thorpej struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
973 1.1 thorpej if (ofd && ofd->sc_flags & FD_MOTOR) {
974 1.2 thorpej callout_stop(&ofd->sc_motoroff_ch);
975 1.1 thorpej ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
976 1.1 thorpej }
977 1.1 thorpej fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
978 1.1 thorpej fd_set_motor(fdc, 0);
979 1.1 thorpej fdc->sc_state = MOTORWAIT;
980 1.1 thorpej /* Allow .25s for motor to stabilize. */
981 1.2 thorpej callout_reset(&fd->sc_motoron_ch, hz / 4,
982 1.1 thorpej fd_motor_on, fd);
983 1.1 thorpej return 1;
984 1.1 thorpej }
985 1.1 thorpej /* Make sure the right drive is selected. */
986 1.1 thorpej fd_set_motor(fdc, 0);
987 1.1 thorpej
988 1.1 thorpej /* fall through */
989 1.1 thorpej case DOSEEK:
990 1.1 thorpej doseek:
991 1.1 thorpej if (fd->sc_cylin == bp->b_cylinder)
992 1.1 thorpej goto doio;
993 1.1 thorpej
994 1.1 thorpej out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
995 1.1 thorpej out_fdc(iot, ioh, fd->sc_type->steprate);
996 1.1 thorpej out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
997 1.1 thorpej
998 1.1 thorpej out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
999 1.1 thorpej out_fdc(iot, ioh, fd->sc_drive); /* drive number */
1000 1.1 thorpej out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
1001 1.1 thorpej
1002 1.1 thorpej fd->sc_cylin = -1;
1003 1.1 thorpej fdc->sc_state = SEEKWAIT;
1004 1.1 thorpej
1005 1.1 thorpej fd->sc_dk.dk_seek++;
1006 1.1 thorpej disk_busy(&fd->sc_dk);
1007 1.1 thorpej
1008 1.1 thorpej callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1009 1.1 thorpej return 1;
1010 1.1 thorpej
1011 1.1 thorpej case DOIO:
1012 1.1 thorpej doio:
1013 1.1 thorpej type = fd->sc_type;
1014 1.1 thorpej if (finfo)
1015 1.1 thorpej fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1016 1.1 thorpej (char *)finfo;
1017 1.1 thorpej sec = fd->sc_blkno % type->seccyl;
1018 1.1 thorpej nblks = type->seccyl - sec;
1019 1.1 thorpej nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1020 1.1 thorpej nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
1021 1.1 thorpej fd->sc_nblks = nblks;
1022 1.1 thorpej fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1023 1.1 thorpej head = sec / type->sectrac;
1024 1.1 thorpej sec -= head * type->sectrac;
1025 1.1 thorpej #ifdef DIAGNOSTIC
1026 1.1 thorpej {int block;
1027 1.1 thorpej block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1028 1.1 thorpej if (block != fd->sc_blkno) {
1029 1.1 thorpej printf("fdcintr: block %d != blkno %d\n",
1030 1.1 thorpej block, fd->sc_blkno);
1031 1.1 thorpej #ifdef DDB
1032 1.1 thorpej Debugger();
1033 1.1 thorpej #endif
1034 1.1 thorpej }}
1035 1.1 thorpej #endif
1036 1.1 thorpej read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
1037 1.1 thorpej isa_dmastart(fdc->sc_ic, fdc->sc_drq,
1038 1.1 thorpej bp->b_data + fd->sc_skip, fd->sc_nbytes,
1039 1.1 thorpej NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
1040 1.1 thorpej bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
1041 1.1 thorpej #ifdef FD_DEBUG
1042 1.1 thorpej printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1043 1.1 thorpej read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1044 1.1 thorpej head, sec, nblks);
1045 1.1 thorpej #endif
1046 1.1 thorpej if (finfo) {
1047 1.1 thorpej /* formatting */
1048 1.1 thorpej if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
1049 1.1 thorpej fdc->sc_errors = 4;
1050 1.1 thorpej fdcretry(fdc);
1051 1.1 thorpej goto loop;
1052 1.1 thorpej }
1053 1.1 thorpej out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1054 1.1 thorpej out_fdc(iot, ioh, finfo->fd_formb_secshift);
1055 1.1 thorpej out_fdc(iot, ioh, finfo->fd_formb_nsecs);
1056 1.1 thorpej out_fdc(iot, ioh, finfo->fd_formb_gaplen);
1057 1.1 thorpej out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
1058 1.1 thorpej } else {
1059 1.1 thorpej if (read)
1060 1.1 thorpej out_fdc(iot, ioh, NE7CMD_READ); /* READ */
1061 1.1 thorpej else
1062 1.1 thorpej out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
1063 1.1 thorpej out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1064 1.1 thorpej out_fdc(iot, ioh, fd->sc_cylin); /* track */
1065 1.1 thorpej out_fdc(iot, ioh, head);
1066 1.1 thorpej out_fdc(iot, ioh, sec + 1); /* sector +1 */
1067 1.1 thorpej out_fdc(iot, ioh, type->secsize);/* sector size */
1068 1.1 thorpej out_fdc(iot, ioh, type->sectrac);/* sectors/track */
1069 1.1 thorpej out_fdc(iot, ioh, type->gap1); /* gap1 size */
1070 1.1 thorpej out_fdc(iot, ioh, type->datalen);/* data length */
1071 1.1 thorpej }
1072 1.1 thorpej fdc->sc_state = IOCOMPLETE;
1073 1.1 thorpej
1074 1.1 thorpej disk_busy(&fd->sc_dk);
1075 1.1 thorpej
1076 1.1 thorpej /* allow 2 seconds for operation */
1077 1.1 thorpej callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1078 1.1 thorpej return 1; /* will return later */
1079 1.1 thorpej
1080 1.1 thorpej case SEEKWAIT:
1081 1.1 thorpej callout_stop(&fdc->sc_timo_ch);
1082 1.1 thorpej fdc->sc_state = SEEKCOMPLETE;
1083 1.1 thorpej /* allow 1/50 second for heads to settle */
1084 1.1 thorpej callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
1085 1.1 thorpej return 1;
1086 1.1 thorpej
1087 1.1 thorpej case SEEKCOMPLETE:
1088 1.1 thorpej disk_unbusy(&fd->sc_dk, 0); /* no data on seek */
1089 1.1 thorpej
1090 1.1 thorpej /* Make sure seek really happened. */
1091 1.1 thorpej out_fdc(iot, ioh, NE7CMD_SENSEI);
1092 1.1 thorpej if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
1093 1.1 thorpej cyl != bp->b_cylinder * fd->sc_type->step) {
1094 1.1 thorpej #ifdef FD_DEBUG
1095 1.1 thorpej fdcstatus(&fd->sc_dev, 2, "seek failed");
1096 1.1 thorpej #endif
1097 1.1 thorpej fdcretry(fdc);
1098 1.1 thorpej goto loop;
1099 1.1 thorpej }
1100 1.1 thorpej fd->sc_cylin = bp->b_cylinder;
1101 1.1 thorpej goto doio;
1102 1.1 thorpej
1103 1.1 thorpej case IOTIMEDOUT:
1104 1.1 thorpej isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1105 1.1 thorpej case SEEKTIMEDOUT:
1106 1.1 thorpej case RECALTIMEDOUT:
1107 1.1 thorpej case RESETTIMEDOUT:
1108 1.1 thorpej fdcretry(fdc);
1109 1.1 thorpej goto loop;
1110 1.1 thorpej
1111 1.1 thorpej case IOCOMPLETE: /* IO DONE, post-analyze */
1112 1.1 thorpej callout_stop(&fdc->sc_timo_ch);
1113 1.1 thorpej
1114 1.1 thorpej disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1115 1.1 thorpej
1116 1.1 thorpej if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
1117 1.1 thorpej isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1118 1.1 thorpej #ifdef FD_DEBUG
1119 1.1 thorpej fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1120 1.1 thorpej "read failed" : "write failed");
1121 1.1 thorpej printf("blkno %d nblks %d\n",
1122 1.1 thorpej fd->sc_blkno, fd->sc_nblks);
1123 1.1 thorpej #endif
1124 1.1 thorpej fdcretry(fdc);
1125 1.1 thorpej goto loop;
1126 1.1 thorpej }
1127 1.1 thorpej isa_dmadone(fdc->sc_ic, fdc->sc_drq);
1128 1.1 thorpej if (fdc->sc_errors) {
1129 1.1 thorpej diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
1130 1.1 thorpej fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1131 1.1 thorpej printf("\n");
1132 1.1 thorpej fdc->sc_errors = 0;
1133 1.1 thorpej }
1134 1.1 thorpej fd->sc_blkno += fd->sc_nblks;
1135 1.1 thorpej fd->sc_skip += fd->sc_nbytes;
1136 1.1 thorpej fd->sc_bcount -= fd->sc_nbytes;
1137 1.1 thorpej if (!finfo && fd->sc_bcount > 0) {
1138 1.1 thorpej bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1139 1.1 thorpej goto doseek;
1140 1.1 thorpej }
1141 1.1 thorpej fdfinish(fd, bp);
1142 1.1 thorpej goto loop;
1143 1.1 thorpej
1144 1.1 thorpej case DORESET:
1145 1.1 thorpej /* try a reset, keep motor on */
1146 1.1 thorpej fd_set_motor(fdc, 1);
1147 1.1 thorpej delay(100);
1148 1.1 thorpej fd_set_motor(fdc, 0);
1149 1.1 thorpej fdc->sc_state = RESETCOMPLETE;
1150 1.1 thorpej callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1151 1.1 thorpej return 1; /* will return later */
1152 1.1 thorpej
1153 1.1 thorpej case RESETCOMPLETE:
1154 1.1 thorpej callout_stop(&fdc->sc_timo_ch);
1155 1.1 thorpej /* clear the controller output buffer */
1156 1.1 thorpej for (i = 0; i < 4; i++) {
1157 1.1 thorpej out_fdc(iot, ioh, NE7CMD_SENSEI);
1158 1.1 thorpej (void) fdcresult(fdc);
1159 1.1 thorpej }
1160 1.1 thorpej
1161 1.1 thorpej /* fall through */
1162 1.1 thorpej case DORECAL:
1163 1.1 thorpej out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1164 1.1 thorpej out_fdc(iot, ioh, fd->sc_drive);
1165 1.1 thorpej fdc->sc_state = RECALWAIT;
1166 1.1 thorpej callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1167 1.1 thorpej return 1; /* will return later */
1168 1.1 thorpej
1169 1.1 thorpej case RECALWAIT:
1170 1.1 thorpej callout_stop(&fdc->sc_timo_ch);
1171 1.1 thorpej fdc->sc_state = RECALCOMPLETE;
1172 1.1 thorpej /* allow 1/30 second for heads to settle */
1173 1.1 thorpej callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
1174 1.1 thorpej return 1; /* will return later */
1175 1.1 thorpej
1176 1.1 thorpej case RECALCOMPLETE:
1177 1.1 thorpej out_fdc(iot, ioh, NE7CMD_SENSEI);
1178 1.1 thorpej if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1179 1.1 thorpej #ifdef FD_DEBUG
1180 1.1 thorpej fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1181 1.1 thorpej #endif
1182 1.1 thorpej fdcretry(fdc);
1183 1.1 thorpej goto loop;
1184 1.1 thorpej }
1185 1.1 thorpej fd->sc_cylin = 0;
1186 1.1 thorpej goto doseek;
1187 1.1 thorpej
1188 1.1 thorpej case MOTORWAIT:
1189 1.1 thorpej if (fd->sc_flags & FD_MOTOR_WAIT)
1190 1.1 thorpej return 1; /* time's not up yet */
1191 1.1 thorpej goto doseek;
1192 1.1 thorpej
1193 1.1 thorpej default:
1194 1.1 thorpej fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1195 1.1 thorpej return 1;
1196 1.1 thorpej }
1197 1.1 thorpej #ifdef DIAGNOSTIC
1198 1.1 thorpej panic("fdcintr: impossible");
1199 1.1 thorpej #endif
1200 1.1 thorpej #undef st0
1201 1.1 thorpej #undef cyl
1202 1.1 thorpej }
1203 1.1 thorpej
1204 1.1 thorpej void
1205 1.1 thorpej fdcretry(fdc)
1206 1.1 thorpej struct fdc_softc *fdc;
1207 1.1 thorpej {
1208 1.1 thorpej char bits[64];
1209 1.1 thorpej struct fd_softc *fd;
1210 1.1 thorpej struct buf *bp;
1211 1.1 thorpej
1212 1.1 thorpej fd = fdc->sc_drives.tqh_first;
1213 1.1 thorpej bp = BUFQ_FIRST(&fd->sc_q);
1214 1.1 thorpej
1215 1.1 thorpej if (fd->sc_opts & FDOPT_NORETRY)
1216 1.1 thorpej goto fail;
1217 1.1 thorpej switch (fdc->sc_errors) {
1218 1.1 thorpej case 0:
1219 1.1 thorpej /* try again */
1220 1.1 thorpej fdc->sc_state = DOSEEK;
1221 1.1 thorpej break;
1222 1.1 thorpej
1223 1.1 thorpej case 1: case 2: case 3:
1224 1.1 thorpej /* didn't work; try recalibrating */
1225 1.1 thorpej fdc->sc_state = DORECAL;
1226 1.1 thorpej break;
1227 1.1 thorpej
1228 1.1 thorpej case 4:
1229 1.1 thorpej /* still no go; reset the bastard */
1230 1.1 thorpej fdc->sc_state = DORESET;
1231 1.1 thorpej break;
1232 1.1 thorpej
1233 1.1 thorpej default:
1234 1.1 thorpej fail:
1235 1.1 thorpej if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1236 1.1 thorpej diskerr(bp, "fd", "hard error", LOG_PRINTF,
1237 1.1 thorpej fd->sc_skip / FDC_BSIZE,
1238 1.1 thorpej (struct disklabel *)NULL);
1239 1.1 thorpej
1240 1.1 thorpej printf(" (st0 %s",
1241 1.1 thorpej bitmask_snprintf(fdc->sc_status[0],
1242 1.1 thorpej NE7_ST0BITS, bits,
1243 1.1 thorpej sizeof(bits)));
1244 1.1 thorpej printf(" st1 %s",
1245 1.1 thorpej bitmask_snprintf(fdc->sc_status[1],
1246 1.1 thorpej NE7_ST1BITS, bits,
1247 1.1 thorpej sizeof(bits)));
1248 1.1 thorpej printf(" st2 %s",
1249 1.1 thorpej bitmask_snprintf(fdc->sc_status[2],
1250 1.1 thorpej NE7_ST2BITS, bits,
1251 1.1 thorpej sizeof(bits)));
1252 1.1 thorpej printf(" cyl %d head %d sec %d)\n",
1253 1.1 thorpej fdc->sc_status[3],
1254 1.1 thorpej fdc->sc_status[4],
1255 1.1 thorpej fdc->sc_status[5]);
1256 1.1 thorpej }
1257 1.1 thorpej
1258 1.1 thorpej bp->b_flags |= B_ERROR;
1259 1.1 thorpej bp->b_error = EIO;
1260 1.1 thorpej fdfinish(fd, bp);
1261 1.1 thorpej }
1262 1.1 thorpej fdc->sc_errors++;
1263 1.1 thorpej }
1264 1.1 thorpej
1265 1.1 thorpej int
1266 1.1 thorpej fdsize(dev)
1267 1.1 thorpej dev_t dev;
1268 1.1 thorpej {
1269 1.1 thorpej
1270 1.1 thorpej /* Swapping to floppies would not make sense. */
1271 1.1 thorpej return -1;
1272 1.1 thorpej }
1273 1.1 thorpej
1274 1.1 thorpej int
1275 1.1 thorpej fddump(dev, blkno, va, size)
1276 1.1 thorpej dev_t dev;
1277 1.1 thorpej daddr_t blkno;
1278 1.1 thorpej caddr_t va;
1279 1.1 thorpej size_t size;
1280 1.1 thorpej {
1281 1.1 thorpej
1282 1.1 thorpej /* Not implemented. */
1283 1.1 thorpej return ENXIO;
1284 1.1 thorpej }
1285 1.1 thorpej
1286 1.1 thorpej int
1287 1.1 thorpej fdioctl(dev, cmd, addr, flag, p)
1288 1.1 thorpej dev_t dev;
1289 1.1 thorpej u_long cmd;
1290 1.1 thorpej caddr_t addr;
1291 1.1 thorpej int flag;
1292 1.1 thorpej struct proc *p;
1293 1.1 thorpej {
1294 1.7 thorpej struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1295 1.1 thorpej struct fdformat_parms *form_parms;
1296 1.1 thorpej struct fdformat_cmd *form_cmd;
1297 1.8 nathanw struct ne7_fd_formb *fd_formb;
1298 1.1 thorpej struct disklabel buffer;
1299 1.1 thorpej int error;
1300 1.1 thorpej unsigned int scratch;
1301 1.1 thorpej int il[FD_MAX_NSEC + 1];
1302 1.1 thorpej register int i, j;
1303 1.11 fvdl #ifdef __HAVE_OLD_DISKLABEL
1304 1.11 fvdl struct disklabel newlabel;
1305 1.11 fvdl #endif
1306 1.1 thorpej
1307 1.1 thorpej switch (cmd) {
1308 1.1 thorpej case DIOCGDINFO:
1309 1.11 fvdl #ifdef __HAVE_OLD_DISKLABEL
1310 1.11 fvdl case ODIOCGDINFO:
1311 1.11 fvdl #endif
1312 1.1 thorpej memset(&buffer, 0, sizeof(buffer));
1313 1.1 thorpej
1314 1.1 thorpej buffer.d_secpercyl = fd->sc_type->seccyl;
1315 1.1 thorpej buffer.d_type = DTYPE_FLOPPY;
1316 1.1 thorpej buffer.d_secsize = FDC_BSIZE;
1317 1.1 thorpej
1318 1.1 thorpej if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1319 1.1 thorpej return EINVAL;
1320 1.1 thorpej
1321 1.11 fvdl #ifdef __HAVE_OLD_DISKLABEL
1322 1.11 fvdl if (cmd == ODIOCGDINFO) {
1323 1.11 fvdl if (buffer.d_npartitions > OLDMAXPARTITIONS)
1324 1.12 fvdl return ENOTTY;
1325 1.11 fvdl memcpy(addr, &buffer, sizeof (struct olddisklabel));
1326 1.11 fvdl } else
1327 1.11 fvdl #endif
1328 1.1 thorpej *(struct disklabel *)addr = buffer;
1329 1.1 thorpej return 0;
1330 1.1 thorpej
1331 1.1 thorpej case DIOCWLABEL:
1332 1.1 thorpej if ((flag & FWRITE) == 0)
1333 1.1 thorpej return EBADF;
1334 1.1 thorpej /* XXX do something */
1335 1.1 thorpej return 0;
1336 1.1 thorpej
1337 1.1 thorpej case DIOCWDINFO:
1338 1.11 fvdl #ifdef __HAVE_OLD_DISKLABEL
1339 1.11 fvdl case ODIOCWDINFO:
1340 1.11 fvdl #endif
1341 1.11 fvdl {
1342 1.11 fvdl struct disklabel *lp;
1343 1.11 fvdl
1344 1.1 thorpej if ((flag & FWRITE) == 0)
1345 1.1 thorpej return EBADF;
1346 1.11 fvdl #ifdef __HAVE_OLD_DISKLABEL
1347 1.11 fvdl if (cmd == ODIOCWDINFO) {
1348 1.11 fvdl memset(&newlabel, 0, sizeof newlabel);
1349 1.11 fvdl memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1350 1.11 fvdl lp = &newlabel;
1351 1.11 fvdl } else
1352 1.11 fvdl #endif
1353 1.11 fvdl lp = (struct disklabel *)addr;
1354 1.1 thorpej
1355 1.11 fvdl error = setdisklabel(&buffer, lp, 0, NULL);
1356 1.1 thorpej if (error)
1357 1.1 thorpej return error;
1358 1.1 thorpej
1359 1.1 thorpej error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1360 1.1 thorpej return error;
1361 1.11 fvdl }
1362 1.1 thorpej
1363 1.1 thorpej case FDIOCGETFORMAT:
1364 1.1 thorpej form_parms = (struct fdformat_parms *)addr;
1365 1.1 thorpej form_parms->fdformat_version = FDFORMAT_VERSION;
1366 1.1 thorpej form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1367 1.1 thorpej form_parms->ncyl = fd->sc_type->cyls;
1368 1.1 thorpej form_parms->nspt = fd->sc_type->sectrac;
1369 1.1 thorpej form_parms->ntrk = fd->sc_type->heads;
1370 1.1 thorpej form_parms->stepspercyl = fd->sc_type->step;
1371 1.1 thorpej form_parms->gaplen = fd->sc_type->gap2;
1372 1.1 thorpej form_parms->fillbyte = fd->sc_type->fillbyte;
1373 1.1 thorpej form_parms->interleave = fd->sc_type->interleave;
1374 1.1 thorpej switch (fd->sc_type->rate) {
1375 1.1 thorpej case FDC_500KBPS:
1376 1.1 thorpej form_parms->xfer_rate = 500 * 1024;
1377 1.1 thorpej break;
1378 1.1 thorpej case FDC_300KBPS:
1379 1.1 thorpej form_parms->xfer_rate = 300 * 1024;
1380 1.1 thorpej break;
1381 1.1 thorpej case FDC_250KBPS:
1382 1.1 thorpej form_parms->xfer_rate = 250 * 1024;
1383 1.1 thorpej break;
1384 1.1 thorpej default:
1385 1.1 thorpej return EINVAL;
1386 1.1 thorpej }
1387 1.1 thorpej return 0;
1388 1.1 thorpej
1389 1.1 thorpej case FDIOCSETFORMAT:
1390 1.1 thorpej if((flag & FWRITE) == 0)
1391 1.1 thorpej return EBADF; /* must be opened for writing */
1392 1.1 thorpej form_parms = (struct fdformat_parms *)addr;
1393 1.1 thorpej if (form_parms->fdformat_version != FDFORMAT_VERSION)
1394 1.1 thorpej return EINVAL; /* wrong version of formatting prog */
1395 1.1 thorpej
1396 1.1 thorpej scratch = form_parms->nbps >> 7;
1397 1.1 thorpej if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1398 1.1 thorpej scratch & ~(1 << (ffs(scratch)-1)))
1399 1.1 thorpej /* not a power-of-two multiple of 128 */
1400 1.1 thorpej return EINVAL;
1401 1.1 thorpej
1402 1.1 thorpej switch (form_parms->xfer_rate) {
1403 1.1 thorpej case 500 * 1024:
1404 1.1 thorpej fd->sc_type->rate = FDC_500KBPS;
1405 1.1 thorpej break;
1406 1.1 thorpej case 300 * 1024:
1407 1.1 thorpej fd->sc_type->rate = FDC_300KBPS;
1408 1.1 thorpej break;
1409 1.1 thorpej case 250 * 1024:
1410 1.1 thorpej fd->sc_type->rate = FDC_250KBPS;
1411 1.1 thorpej break;
1412 1.1 thorpej default:
1413 1.1 thorpej return EINVAL;
1414 1.1 thorpej }
1415 1.1 thorpej
1416 1.1 thorpej if (form_parms->nspt > FD_MAX_NSEC ||
1417 1.1 thorpej form_parms->fillbyte > 0xff ||
1418 1.1 thorpej form_parms->interleave > 0xff)
1419 1.1 thorpej return EINVAL;
1420 1.1 thorpej fd->sc_type->sectrac = form_parms->nspt;
1421 1.1 thorpej if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1422 1.1 thorpej return EINVAL;
1423 1.1 thorpej fd->sc_type->heads = form_parms->ntrk;
1424 1.1 thorpej fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1425 1.1 thorpej fd->sc_type->secsize = ffs(scratch)-1;
1426 1.1 thorpej fd->sc_type->gap2 = form_parms->gaplen;
1427 1.1 thorpej fd->sc_type->cyls = form_parms->ncyl;
1428 1.1 thorpej fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1429 1.1 thorpej form_parms->nbps / DEV_BSIZE;
1430 1.1 thorpej fd->sc_type->step = form_parms->stepspercyl;
1431 1.1 thorpej fd->sc_type->fillbyte = form_parms->fillbyte;
1432 1.1 thorpej fd->sc_type->interleave = form_parms->interleave;
1433 1.1 thorpej return 0;
1434 1.1 thorpej
1435 1.1 thorpej case FDIOCFORMAT_TRACK:
1436 1.1 thorpej if((flag & FWRITE) == 0)
1437 1.1 thorpej return EBADF; /* must be opened for writing */
1438 1.1 thorpej form_cmd = (struct fdformat_cmd *)addr;
1439 1.1 thorpej if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1440 1.1 thorpej return EINVAL; /* wrong version of formatting prog */
1441 1.1 thorpej
1442 1.1 thorpej if (form_cmd->head >= fd->sc_type->heads ||
1443 1.1 thorpej form_cmd->cylinder >= fd->sc_type->cyls) {
1444 1.1 thorpej return EINVAL;
1445 1.1 thorpej }
1446 1.1 thorpej
1447 1.8 nathanw fd_formb = malloc(sizeof(struct ne7_fd_formb),
1448 1.8 nathanw M_TEMP, M_NOWAIT);
1449 1.8 nathanw if (fd_formb == 0)
1450 1.8 nathanw return ENOMEM;
1451 1.8 nathanw
1452 1.8 nathanw fd_formb->head = form_cmd->head;
1453 1.8 nathanw fd_formb->cyl = form_cmd->cylinder;
1454 1.8 nathanw fd_formb->transfer_rate = fd->sc_type->rate;
1455 1.8 nathanw fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1456 1.8 nathanw fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1457 1.8 nathanw fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1458 1.8 nathanw fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1459 1.1 thorpej
1460 1.1 thorpej memset(il, 0, sizeof il);
1461 1.8 nathanw for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1462 1.8 nathanw while (il[(j%fd_formb->fd_formb_nsecs)+1])
1463 1.1 thorpej j++;
1464 1.8 nathanw il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1465 1.1 thorpej j += fd->sc_type->interleave;
1466 1.1 thorpej }
1467 1.8 nathanw for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1468 1.8 nathanw fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1469 1.8 nathanw fd_formb->fd_formb_headno(i) = form_cmd->head;
1470 1.8 nathanw fd_formb->fd_formb_secno(i) = il[i+1];
1471 1.8 nathanw fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1472 1.1 thorpej }
1473 1.1 thorpej
1474 1.8 nathanw error = fdformat(dev, fd_formb, p);
1475 1.8 nathanw free(fd_formb, M_TEMP);
1476 1.8 nathanw return error;
1477 1.1 thorpej
1478 1.1 thorpej case FDIOCGETOPTS: /* get drive options */
1479 1.1 thorpej *(int *)addr = fd->sc_opts;
1480 1.1 thorpej return 0;
1481 1.1 thorpej
1482 1.1 thorpej case FDIOCSETOPTS: /* set drive options */
1483 1.1 thorpej fd->sc_opts = *(int *)addr;
1484 1.1 thorpej return 0;
1485 1.1 thorpej
1486 1.1 thorpej default:
1487 1.1 thorpej return ENOTTY;
1488 1.1 thorpej }
1489 1.1 thorpej
1490 1.1 thorpej #ifdef DIAGNOSTIC
1491 1.1 thorpej panic("fdioctl: impossible");
1492 1.1 thorpej #endif
1493 1.1 thorpej }
1494 1.1 thorpej
1495 1.1 thorpej int
1496 1.1 thorpej fdformat(dev, finfo, p)
1497 1.1 thorpej dev_t dev;
1498 1.1 thorpej struct ne7_fd_formb *finfo;
1499 1.1 thorpej struct proc *p;
1500 1.1 thorpej {
1501 1.1 thorpej int rv = 0, s;
1502 1.7 thorpej struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1503 1.1 thorpej struct fd_type *type = fd->sc_type;
1504 1.1 thorpej struct buf *bp;
1505 1.1 thorpej
1506 1.1 thorpej /* set up a buffer header for fdstrategy() */
1507 1.1 thorpej bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1508 1.1 thorpej if(bp == 0)
1509 1.1 thorpej return ENOBUFS;
1510 1.1 thorpej memset((void *)bp, 0, sizeof(struct buf));
1511 1.1 thorpej bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1512 1.1 thorpej bp->b_proc = p;
1513 1.1 thorpej bp->b_dev = dev;
1514 1.1 thorpej
1515 1.1 thorpej /*
1516 1.1 thorpej * calculate a fake blkno, so fdstrategy() would initiate a
1517 1.1 thorpej * seek to the requested cylinder
1518 1.1 thorpej */
1519 1.1 thorpej bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1520 1.1 thorpej + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1521 1.1 thorpej
1522 1.1 thorpej bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1523 1.1 thorpej bp->b_data = (caddr_t)finfo;
1524 1.1 thorpej
1525 1.1 thorpej #ifdef DEBUG
1526 1.1 thorpej printf("fdformat: blkno %x count %lx\n", bp->b_blkno, bp->b_bcount);
1527 1.1 thorpej #endif
1528 1.1 thorpej
1529 1.1 thorpej /* now do the format */
1530 1.1 thorpej fdstrategy(bp);
1531 1.1 thorpej
1532 1.1 thorpej /* ...and wait for it to complete */
1533 1.1 thorpej s = splbio();
1534 1.1 thorpej while(!(bp->b_flags & B_DONE)) {
1535 1.1 thorpej rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1536 1.1 thorpej if (rv == EWOULDBLOCK)
1537 1.1 thorpej break;
1538 1.1 thorpej }
1539 1.1 thorpej splx(s);
1540 1.1 thorpej
1541 1.1 thorpej if (rv == EWOULDBLOCK) {
1542 1.1 thorpej /* timed out */
1543 1.1 thorpej rv = EIO;
1544 1.1 thorpej biodone(bp);
1545 1.1 thorpej }
1546 1.1 thorpej if(bp->b_flags & B_ERROR) {
1547 1.1 thorpej rv = bp->b_error;
1548 1.1 thorpej }
1549 1.1 thorpej free(bp, M_TEMP);
1550 1.1 thorpej return rv;
1551 1.1 thorpej }
1552 1.1 thorpej
1553 1.1 thorpej /*
1554 1.1 thorpej * Mountroot hook: prompt the user to enter the root file system
1555 1.1 thorpej * floppy.
1556 1.1 thorpej */
1557 1.1 thorpej void
1558 1.1 thorpej fd_mountroot_hook(dev)
1559 1.1 thorpej struct device *dev;
1560 1.1 thorpej {
1561 1.1 thorpej int c;
1562 1.1 thorpej
1563 1.1 thorpej printf("Insert filesystem floppy and press return.");
1564 1.1 thorpej cnpollc(1);
1565 1.1 thorpej for (;;) {
1566 1.1 thorpej c = cngetc();
1567 1.1 thorpej if ((c == '\r') || (c == '\n')) {
1568 1.1 thorpej printf("\n");
1569 1.1 thorpej break;
1570 1.1 thorpej }
1571 1.1 thorpej }
1572 1.1 thorpej cnpollc(0);
1573 1.1 thorpej }
1574