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