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