fd.c revision 1.110.2.2 1 /* $NetBSD: fd.c,v 1.110.2.2 2016/07/25 23:40:33 pgoyette 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.110.2.2 2016/07/25 23:40:33 pgoyette 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/rndsource.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 = {
257 .d_strategy = fdstrategy,
258 .d_minphys = minphys
259 };
260
261 #if defined(i386) || defined(x86_64)
262 const struct fd_type *fd_nvtotype(const char *, int, int);
263 #endif /* i386 || x86_64 */
264 void fd_set_motor(struct fdc_softc *fdc, int reset);
265 void fd_motor_off(void *arg);
266 void fd_motor_on(void *arg);
267 int fdcresult(struct fdc_softc *fdc);
268 void fdcstart(struct fdc_softc *fdc);
269 void fdcstatus(device_t, int, const char *);
270 void fdctimeout(void *arg);
271 void fdcretry(struct fdc_softc *fdc);
272 void fdfinish(struct fd_softc *fd, struct buf *bp);
273 static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
274 int fdformat(dev_t, struct ne7_fd_formb *, struct lwp *);
275 static void fd_set_geometry(struct fd_softc *fd);
276
277 void fd_mountroot_hook(device_t);
278
279 /*
280 * Arguments passed between fdcattach and fdprobe.
281 */
282 struct fdc_attach_args {
283 int fa_drive;
284 const struct fd_type *fa_deftype;
285 };
286
287 /*
288 * Print the location of a disk drive (called just before attaching the
289 * the drive). If `fdc' is not NULL, the drive was found but was not
290 * in the system config file; print the drive name as well.
291 * Return QUIET (config_find ignores this if the device was configured) to
292 * avoid printing `fdN not configured' messages.
293 */
294 int
295 fdprint(void *aux, const char *fdc)
296 {
297 struct fdc_attach_args *fa = aux;
298
299 if (!fdc)
300 aprint_normal(" drive %d", fa->fa_drive);
301 return QUIET;
302 }
303
304 static bool
305 fdcresume(device_t self, const pmf_qual_t *qual)
306 {
307 struct fdc_softc *fdc = device_private(self);
308
309 mutex_enter(&fdc->sc_mtx);
310 (void)fdcintr1(fdc);
311 mutex_exit(&fdc->sc_mtx);
312 return true;
313 }
314
315 static bool
316 fdcsuspend(device_t self, const pmf_qual_t *qual)
317 {
318 struct fdc_softc *fdc = device_private(self);
319 int drive;
320 struct fd_softc *fd;
321
322 mutex_enter(&fdc->sc_mtx);
323 while (fdc->sc_state != DEVIDLE)
324 cv_wait(&fdc->sc_cv, &fdc->sc_mtx);
325 for (drive = 0; drive < 4; drive++) {
326 if ((fd = fdc->sc_fd[drive]) == NULL)
327 continue;
328 fd->sc_flags &= ~(FD_MOTOR|FD_MOTOR_WAIT);
329 }
330 fd_set_motor(fdc, 0);
331 mutex_exit(&fdc->sc_mtx);
332 return true;
333 }
334
335 void
336 fdc_childdet(device_t self, device_t child)
337 {
338 struct fdc_softc *fdc = device_private(self);
339 struct fd_softc *fd = device_private(child);
340 int drive = fd->sc_drive;
341
342 KASSERT(fdc->sc_fd[drive] == fd); /* but the kid is not my son */
343 fdc->sc_fd[drive] = NULL;
344 }
345
346 int
347 fdcdetach(device_t self, int flags)
348 {
349 int rc;
350 struct fdc_softc *fdc = device_private(self);
351
352 if ((rc = config_detach_children(self, flags)) != 0)
353 return rc;
354
355 pmf_device_deregister(self);
356
357 isa_dmamap_destroy(fdc->sc_ic, fdc->sc_drq);
358 isa_drq_free(fdc->sc_ic, fdc->sc_drq);
359
360 callout_destroy(&fdc->sc_intr_ch);
361 callout_destroy(&fdc->sc_timo_ch);
362
363 cv_destroy(&fdc->sc_cv);
364 mutex_destroy(&fdc->sc_mtx);
365
366 return 0;
367 }
368
369 void
370 fdcattach(struct fdc_softc *fdc)
371 {
372 mutex_init(&fdc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
373 cv_init(&fdc->sc_cv, "fdcwake");
374 callout_init(&fdc->sc_timo_ch, 0);
375 callout_init(&fdc->sc_intr_ch, 0);
376
377 fdc->sc_state = DEVIDLE;
378 TAILQ_INIT(&fdc->sc_drives);
379
380 fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
381
382 if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) {
383 aprint_normal_dev(fdc->sc_dev, "can't reserve drq %d\n",
384 fdc->sc_drq);
385 return;
386 }
387
388 if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
389 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
390 aprint_normal_dev(fdc->sc_dev, "can't set up ISA DMA map\n");
391 return;
392 }
393
394 config_interrupts(fdc->sc_dev, fdcfinishattach);
395
396 if (!pmf_device_register(fdc->sc_dev, fdcsuspend, fdcresume)) {
397 aprint_error_dev(fdc->sc_dev,
398 "cannot set power mgmt handler\n");
399 }
400 }
401
402 void
403 fdcfinishattach(device_t self)
404 {
405 struct fdc_softc *fdc = device_private(self);
406 bus_space_tag_t iot = fdc->sc_iot;
407 bus_space_handle_t ioh = fdc->sc_ioh;
408 struct fdc_attach_args fa;
409
410 /*
411 * Reset the controller to get it into a known state. Not all
412 * probes necessarily need do this to discover the controller up
413 * front, so don't assume anything.
414 */
415
416 bus_space_write_1(iot, ioh, fdout, 0);
417 delay(100);
418 bus_space_write_1(iot, ioh, fdout, FDO_FRST);
419
420 /* see if it can handle a command */
421 if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
422 aprint_normal_dev(fdc->sc_dev, "can't reset controller\n");
423 return;
424 }
425 out_fdc(iot, ioh, 0xdf);
426 out_fdc(iot, ioh, 2);
427
428 #if defined(i386) || defined(x86_64)
429 /*
430 * The NVRAM info only tells us about the first two disks on the
431 * `primary' floppy controller.
432 */
433 /* XXX device_unit() abuse */
434 if (device_unit(fdc->sc_dev) == 0) {
435 int type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
436 fdc->sc_known = 1;
437 fdc->sc_knownfds[0] = fd_nvtotype(device_xname(fdc->sc_dev),
438 type, 0);
439 if (fdc->sc_knownfds[0] != NULL)
440 fdc->sc_present |= 1;
441 fdc->sc_knownfds[1] = fd_nvtotype(device_xname(fdc->sc_dev),
442 type, 1);
443 if (fdc->sc_knownfds[1] != NULL)
444 fdc->sc_present |= 2;
445 }
446 #endif /* i386 || x86_64 */
447
448 /* physical limit: four drives per controller. */
449 fdc->sc_state = PROBING;
450 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
451 if (fdc->sc_known) {
452 if (fdc->sc_present & (1 << fa.fa_drive)) {
453 fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive];
454 config_found(fdc->sc_dev, (void *)&fa,
455 fdprint);
456 }
457 } else {
458 #if defined(atari)
459 /*
460 * Atari has a different ordening, defaults to 1.44
461 */
462 fa.fa_deftype = &fd_types[2];
463 #else
464 /*
465 * Default to 1.44MB on Alpha and BeBox. How do we tell
466 * on these platforms?
467 */
468 fa.fa_deftype = &fd_types[0];
469 #endif
470 (void)config_found_ia(fdc->sc_dev, "fdc", (void *)&fa, fdprint);
471 }
472 }
473 fdc->sc_state = DEVIDLE;
474 }
475
476 int
477 fdprobe(device_t parent, cfdata_t match, void *aux)
478 {
479 struct fdc_softc *fdc = device_private(parent);
480 cfdata_t cf = match;
481 struct fdc_attach_args *fa = aux;
482 int drive = fa->fa_drive;
483 bus_space_tag_t iot = fdc->sc_iot;
484 bus_space_handle_t ioh = fdc->sc_ioh;
485 int n;
486
487 if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
488 cf->cf_loc[FDCCF_DRIVE] != drive)
489 return 0;
490 /*
491 * XXX
492 * This is to work around some odd interactions between this driver
493 * and SMC Ethernet cards.
494 */
495 if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
496 return 0;
497
498 /* Use PNP information if available */
499 if (fdc->sc_known)
500 return 1;
501
502 mutex_enter(&fdc->sc_mtx);
503 /* toss any interrupt status */
504 for (n = 0; n < 4; n++) {
505 out_fdc(iot, ioh, NE7CMD_SENSEI);
506 (void) fdcresult(fdc);
507 }
508 /* select drive and turn on motor */
509 bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
510 /* wait for motor to spin up */
511 /* XXX check sc_probe */
512 (void) cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, hz / 4);
513 out_fdc(iot, ioh, NE7CMD_RECAL);
514 out_fdc(iot, ioh, drive);
515 /* wait for recalibrate, up to 2s */
516 /* XXX check sc_probe */
517 if (cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, 2 * hz) != EWOULDBLOCK){
518 #ifdef FD_DEBUG
519 /* XXX */
520 printf("fdprobe: got intr\n");
521 #endif
522 }
523 out_fdc(iot, ioh, NE7CMD_SENSEI);
524 n = fdcresult(fdc);
525 #ifdef FD_DEBUG
526 {
527 int i;
528 printf("fdprobe: status");
529 for (i = 0; i < n; i++)
530 printf(" %x", fdc->sc_status[i]);
531 printf("\n");
532 }
533 #endif
534 /* turn off motor */
535 bus_space_write_1(iot, ioh, fdout, FDO_FRST);
536 mutex_exit(&fdc->sc_mtx);
537
538 #if defined(bebox) /* XXX What is this about? --thorpej (at) NetBSD.org */
539 if (n != 2 || (fdc->sc_status[1] != 0))
540 return 0;
541 #else
542 if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
543 return 0;
544 #endif /* bebox */
545
546 return 1;
547 }
548
549 /*
550 * Controller is working, and drive responded. Attach it.
551 */
552 void
553 fdattach(device_t parent, device_t self, void *aux)
554 {
555 struct fdc_softc *fdc = device_private(parent);
556 struct fd_softc *fd = device_private(self);
557 struct fdc_attach_args *fa = aux;
558 const struct fd_type *type = fa->fa_deftype;
559 int drive = fa->fa_drive;
560
561 fd->sc_dev = self;
562
563 callout_init(&fd->sc_motoron_ch, 0);
564 callout_init(&fd->sc_motoroff_ch, 0);
565
566 /* XXX Allow `flags' to override device type? */
567
568 if (type)
569 aprint_normal(": %s, %d cyl, %d head, %d sec\n", type->name,
570 type->cyls, type->heads, type->sectrac);
571 else
572 aprint_normal(": density unknown\n");
573
574 bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
575 fd->sc_cylin = -1;
576 fd->sc_drive = drive;
577 fd->sc_deftype = type;
578 fdc->sc_fd[drive] = fd;
579
580 /*
581 * Initialize and attach the disk structure.
582 */
583 disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
584 disk_attach(&fd->sc_dk);
585
586 /*
587 * Establish a mountroot hook.
588 */
589 fd->sc_roothook =
590 mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
591
592 rnd_attach_source(&fd->rnd_source, device_xname(fd->sc_dev),
593 RND_TYPE_DISK, RND_FLAG_DEFAULT);
594
595 fd_set_geometry(fd);
596
597 if (!pmf_device_register(self, NULL, NULL))
598 aprint_error_dev(self, "cannot set power mgmt handler\n");
599 }
600
601 static int
602 fddetach(device_t self, int flags)
603 {
604 struct fd_softc *fd = device_private(self);
605 int bmaj, cmaj, i, mn;
606
607 fd_motor_off(fd);
608
609 /* locate the major number */
610 bmaj = bdevsw_lookup_major(&fd_bdevsw);
611 cmaj = cdevsw_lookup_major(&fd_cdevsw);
612
613 /* Nuke the vnodes for any open instances. */
614 for (i = 0; i < MAXPARTITIONS; i++) {
615 mn = DISKMINOR(device_unit(self), i);
616 vdevgone(bmaj, mn, mn, VBLK);
617 vdevgone(cmaj, mn, mn, VCHR);
618 }
619
620 pmf_device_deregister(self);
621
622 #if 0 /* XXX need to undo at detach? */
623 fd_set_geometry(fd);
624 #endif
625
626 rnd_detach_source(&fd->rnd_source);
627
628 disk_detach(&fd->sc_dk);
629 disk_destroy(&fd->sc_dk);
630
631 /* Kill off any queued buffers. */
632 bufq_drain(fd->sc_q);
633
634 bufq_free(fd->sc_q);
635
636 callout_destroy(&fd->sc_motoroff_ch);
637 callout_destroy(&fd->sc_motoron_ch);
638
639 return 0;
640 }
641
642 #if defined(i386) || defined(x86_64)
643 /*
644 * Translate nvram type into internal data structure. Return NULL for
645 * none/unknown/unusable.
646 */
647 const struct fd_type *
648 fd_nvtotype(const char *fdc, int nvraminfo, int drive)
649 {
650 int type;
651
652 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
653 switch (type) {
654 case NVRAM_DISKETTE_NONE:
655 return NULL;
656 case NVRAM_DISKETTE_12M:
657 return &fd_types[1];
658 case NVRAM_DISKETTE_TYPE5:
659 case NVRAM_DISKETTE_TYPE6:
660 /* XXX We really ought to handle 2.88MB format. */
661 case NVRAM_DISKETTE_144M:
662 #if NMCA > 0
663 if (MCA_system)
664 return &mca_fd_types[0];
665 else
666 #endif /* NMCA > 0 */
667 return &fd_types[0];
668 case NVRAM_DISKETTE_360K:
669 return &fd_types[3];
670 case NVRAM_DISKETTE_720K:
671 #if NMCA > 0
672 if (MCA_system)
673 return &mca_fd_types[1];
674 else
675 #endif /* NMCA > 0 */
676 return &fd_types[4];
677 default:
678 printf("%s: drive %d: unknown device type 0x%x\n",
679 fdc, drive, type);
680 return NULL;
681 }
682 }
683 #endif /* i386 || x86_64 */
684
685 static const struct fd_type *
686 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
687 {
688 u_int type = FDTYPE(dev);
689
690 if (type > __arraycount(fd_types))
691 return NULL;
692 return type ? &fd_types[type - 1] : fd->sc_deftype;
693 }
694
695 void
696 fdstrategy(struct buf *bp)
697 {
698 device_t self = device_lookup_acquire(&fd_cd, FDUNIT(bp->b_dev));
699 struct fd_softc *fd = device_private(self);
700 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
701 int sz;
702
703 /* Valid unit, controller, and request? */
704 if (bp->b_blkno < 0 ||
705 ((bp->b_bcount % FDC_BSIZE) != 0 &&
706 (bp->b_flags & B_FORMAT) == 0)) {
707 bp->b_error = EINVAL;
708 goto done;
709 }
710
711 /* If it's a null transfer, return immediately. */
712 if (bp->b_bcount == 0)
713 goto done;
714
715 sz = howmany(bp->b_bcount, FDC_BSIZE);
716
717 if (bp->b_blkno + sz > fd->sc_type->size) {
718 sz = fd->sc_type->size - bp->b_blkno;
719 if (sz == 0) {
720 /* If exactly at end of disk, return EOF. */
721 goto done;
722 }
723 if (sz < 0) {
724 /* If past end of disk, return EINVAL. */
725 bp->b_error = EINVAL;
726 goto done;
727 }
728 /* Otherwise, truncate request. */
729 bp->b_bcount = sz << DEV_BSHIFT;
730 }
731
732 bp->b_rawblkno = bp->b_blkno;
733 bp->b_cylinder =
734 bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
735
736 #ifdef FD_DEBUG
737 printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d "
738 "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount,
739 (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz);
740 #endif
741
742 /* Queue transfer on drive, activate drive and controller if idle. */
743 mutex_enter(&fdc->sc_mtx);
744 bufq_put(fd->sc_q, bp);
745 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
746 if (fd->sc_active == 0)
747 fdstart(fd);
748 #ifdef DIAGNOSTIC
749 else {
750 if (fdc->sc_state == DEVIDLE) {
751 printf("fdstrategy: controller inactive\n");
752 fdcstart(fdc);
753 }
754 }
755 #endif
756 mutex_exit(&fdc->sc_mtx);
757 device_release(self);
758 return;
759
760 done:
761 /* Toss transfer; we're done early. */
762 bp->b_resid = bp->b_bcount;
763 biodone(bp);
764 device_release(self);
765 }
766
767 void
768 fdstart(struct fd_softc *fd)
769 {
770 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
771 int active = !TAILQ_EMPTY(&fdc->sc_drives);
772
773 KASSERT(mutex_owned(&fdc->sc_mtx));
774 /* Link into controller queue. */
775 fd->sc_active = 1;
776 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
777
778 /* If controller not already active, start it. */
779 if (!active)
780 fdcstart(fdc);
781 }
782
783 void
784 fdfinish(struct fd_softc *fd, struct buf *bp)
785 {
786 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
787
788 /*
789 * Move this drive to the end of the queue to give others a `fair'
790 * chance. We only force a switch if N operations are completed while
791 * another drive is waiting to be serviced, since there is a long motor
792 * startup delay whenever we switch.
793 */
794 (void)bufq_get(fd->sc_q);
795 if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
796 fd->sc_ops = 0;
797 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
798 if (bufq_peek(fd->sc_q) != NULL)
799 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
800 else
801 fd->sc_active = 0;
802 }
803 bp->b_resid = fd->sc_bcount;
804 fd->sc_skip = 0;
805
806 rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
807
808 biodone(bp);
809 /* turn off motor 5s from now */
810 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
811 fdc->sc_state = DEVIDLE;
812 }
813
814 int
815 fdread(dev_t dev, struct uio *uio, int flags)
816 {
817
818 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
819 }
820
821 int
822 fdwrite(dev_t dev, struct uio *uio, int flags)
823 {
824
825 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
826 }
827
828 void
829 fd_set_motor(struct fdc_softc *fdc, int reset)
830 {
831 struct fd_softc *fd;
832 u_char status;
833 int n;
834
835 if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
836 status = fd->sc_drive;
837 else
838 status = 0;
839 if (!reset)
840 status |= FDO_FRST | FDO_FDMAEN;
841 for (n = 0; n < 4; n++)
842 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
843 status |= FDO_MOEN(n);
844 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
845 }
846
847 void
848 fd_motor_off(void *arg)
849 {
850 struct fd_softc *fd = arg;
851 struct fdc_softc *fdc;
852
853 fdc = device_private(device_parent(fd->sc_dev));
854
855 mutex_enter(&fdc->sc_mtx);
856 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
857 fd_set_motor(fdc, 0);
858 mutex_exit(&fdc->sc_mtx);
859 }
860
861 void
862 fd_motor_on(void *arg)
863 {
864 struct fd_softc *fd = arg;
865 struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
866
867 mutex_enter(&fdc->sc_mtx);
868 fd->sc_flags &= ~FD_MOTOR_WAIT;
869 if (TAILQ_FIRST(&fdc->sc_drives) == fd && fdc->sc_state == MOTORWAIT)
870 (void)fdcintr1(fdc);
871 mutex_exit(&fdc->sc_mtx);
872 }
873
874 int
875 fdcresult(struct fdc_softc *fdc)
876 {
877 bus_space_tag_t iot = fdc->sc_iot;
878 bus_space_handle_t ioh = fdc->sc_ioh;
879 u_char i;
880 u_int j = 100000,
881 n = 0;
882
883 for (; j; j--) {
884 i = bus_space_read_1(iot, ioh, fdsts) &
885 (NE7_DIO | NE7_RQM | NE7_CB);
886 if (i == NE7_RQM)
887 return n;
888 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
889 if (n >= sizeof(fdc->sc_status)) {
890 log(LOG_ERR, "fdcresult: overrun\n");
891 return -1;
892 }
893 fdc->sc_status[n++] =
894 bus_space_read_1(iot, ioh, fddata);
895 }
896 delay(10);
897 }
898 log(LOG_ERR, "fdcresult: timeout\n");
899 return -1;
900 }
901
902 int
903 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
904 {
905 u_char i;
906 u_int j = 100000;
907
908 for (; j; j--) {
909 i = bus_space_read_1(iot, ioh, fdsts) &
910 (NE7_DIO | NE7_RQM);
911 if (i == NE7_RQM) {
912 bus_space_write_1(iot, ioh, fddata, x);
913 return 0;
914 }
915 delay(10);
916 }
917 return -1;
918 }
919
920 int
921 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
922 {
923 device_t self;
924 struct fd_softc *fd;
925 const struct fd_type *type;
926
927 self = device_lookup_acquire(&fd_cd, FDUNIT(dev));
928 if (self == NULL)
929 return ENXIO;
930 fd = device_private(self);
931
932 type = fd_dev_to_type(fd, dev);
933 if (type == NULL) {
934 device_release(self);
935 return ENXIO;
936 }
937
938 if ((fd->sc_flags & FD_OPEN) != 0 &&
939 memcmp(fd->sc_type, type, sizeof(*type))) {
940 device_release(self);
941 return EBUSY;
942 }
943
944 fd->sc_type_copy = *type;
945 fd->sc_type = &fd->sc_type_copy;
946 fd->sc_cylin = -1;
947 fd->sc_flags |= FD_OPEN;
948
949 fd_set_geometry(fd);
950
951 device_release(self);
952 return 0;
953 }
954
955 int
956 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
957 {
958 device_t self = device_lookup_acquire(&fd_cd, FDUNIT(dev));
959 struct fd_softc *fd = device_private(self);
960
961 fd->sc_flags &= ~FD_OPEN;
962 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
963 device_release(self);
964 return 0;
965 }
966
967 void
968 fdcstart(struct fdc_softc *fdc)
969 {
970
971 KASSERT(mutex_owned(&fdc->sc_mtx));
972
973 if (!device_is_active(fdc->sc_dev))
974 return;
975
976 #ifdef DIAGNOSTIC
977 /* only got here if controller's drive queue was inactive; should
978 be in idle state */
979 if (fdc->sc_state != DEVIDLE) {
980 printf("fdcstart: not idle\n");
981 return;
982 }
983 #endif
984 (void)fdcintr1(fdc);
985 }
986
987 static void
988 fdcpstatus(int n, struct fdc_softc *fdc)
989 {
990 char bits[64];
991
992 switch (n) {
993 case 0:
994 printf("\n");
995 break;
996 case 2:
997 snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
998 printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]);
999 break;
1000 case 7:
1001 snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
1002 printf(" (st0 %s", bits);
1003 snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]);
1004 printf(" st1 %s", bits);
1005 snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]);
1006 printf(" st2 %s", bits);
1007 printf(" cyl %d head %d sec %d)\n",
1008 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1009 break;
1010 #ifdef DIAGNOSTIC
1011 default:
1012 printf("\nfdcstatus: weird size");
1013 break;
1014 #endif
1015 }
1016 }
1017
1018 void
1019 fdcstatus(device_t dv, int n, const char *s)
1020 {
1021 struct fdc_softc *fdc = device_private(device_parent(dv));
1022
1023 if (n == 0) {
1024 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
1025 (void) fdcresult(fdc);
1026 n = 2;
1027 }
1028 fdcpstatus(n, fdc);
1029
1030 aprint_normal_dev(dv, "%s", s);
1031
1032 }
1033
1034 void
1035 fdctimeout(void *arg)
1036 {
1037 struct fdc_softc *fdc = arg;
1038 struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
1039
1040 mutex_enter(&fdc->sc_mtx);
1041 #ifdef DEBUG
1042 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
1043 #endif
1044 fdcstatus(fd->sc_dev, 0, "timeout");
1045
1046 if (bufq_peek(fd->sc_q) != NULL)
1047 fdc->sc_state++;
1048 else
1049 fdc->sc_state = DEVIDLE;
1050
1051 (void)fdcintr1(fdc);
1052 mutex_exit(&fdc->sc_mtx);
1053 }
1054
1055 static int
1056 fdcintr1(struct fdc_softc *fdc)
1057 {
1058 #define st0 fdc->sc_status[0]
1059 #define cyl fdc->sc_status[1]
1060 struct fd_softc *fd;
1061 struct buf *bp;
1062 bus_space_tag_t iot = fdc->sc_iot;
1063 bus_space_handle_t ioh = fdc->sc_ioh;
1064 int read, head, sec, i, nblks;
1065 struct fd_type *type;
1066 struct ne7_fd_formb *finfo = NULL;
1067
1068 KASSERT(mutex_owned(&fdc->sc_mtx));
1069 if (fdc->sc_state == PROBING) {
1070 #ifdef DEBUG
1071 printf("fdcintr: got probe interrupt\n");
1072 #endif
1073 fdc->sc_probe++;
1074 goto out;
1075 }
1076
1077 loop:
1078 /* Is there a drive for the controller to do a transfer with? */
1079 fd = TAILQ_FIRST(&fdc->sc_drives);
1080 if (fd == NULL) {
1081 fdc->sc_state = DEVIDLE;
1082 goto out;
1083 }
1084
1085 /* Is there a transfer to this drive? If not, deactivate drive. */
1086 bp = bufq_peek(fd->sc_q);
1087 if (bp == NULL) {
1088 fd->sc_ops = 0;
1089 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1090 fd->sc_active = 0;
1091 goto loop;
1092 }
1093
1094 if (bp->b_flags & B_FORMAT)
1095 finfo = (struct ne7_fd_formb *)bp->b_data;
1096
1097 switch (fdc->sc_state) {
1098 case DEVIDLE:
1099 fdc->sc_errors = 0;
1100 fd->sc_skip = 0;
1101 fd->sc_bcount = bp->b_bcount;
1102 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1103 callout_stop(&fd->sc_motoroff_ch);
1104 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1105 fdc->sc_state = MOTORWAIT;
1106 return 1;
1107 }
1108 if ((fd->sc_flags & FD_MOTOR) == 0) {
1109 /* Turn on the motor, being careful about pairing. */
1110 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1111 if (ofd && ofd->sc_flags & FD_MOTOR) {
1112 callout_stop(&ofd->sc_motoroff_ch);
1113 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1114 }
1115 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1116 fd_set_motor(fdc, 0);
1117 fdc->sc_state = MOTORWAIT;
1118 /* Allow .25s for motor to stabilize. */
1119 callout_reset(&fd->sc_motoron_ch, hz / 4,
1120 fd_motor_on, fd);
1121 return 1;
1122 }
1123 /* Make sure the right drive is selected. */
1124 fd_set_motor(fdc, 0);
1125
1126 /* fall through */
1127 case DOSEEK:
1128 doseek:
1129 if (fd->sc_cylin == bp->b_cylinder)
1130 goto doio;
1131
1132 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
1133 out_fdc(iot, ioh, fd->sc_type->steprate);
1134 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
1135
1136 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
1137 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
1138 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
1139
1140 fd->sc_cylin = -1;
1141 fdc->sc_state = SEEKWAIT;
1142
1143 iostat_seek(fd->sc_dk.dk_stats);
1144 disk_busy(&fd->sc_dk);
1145
1146 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1147 return 1;
1148
1149 case DOIO:
1150 doio:
1151 type = fd->sc_type;
1152 if (finfo)
1153 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1154 (char *)finfo;
1155 sec = fd->sc_blkno % type->seccyl;
1156 nblks = type->seccyl - sec;
1157 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1158 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
1159 fd->sc_nblks = nblks;
1160 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1161 head = sec / type->sectrac;
1162 sec -= head * type->sectrac;
1163 #ifdef DIAGNOSTIC
1164 {
1165 int block;
1166 block = (fd->sc_cylin * type->heads + head)
1167 * type->sectrac + sec;
1168 if (block != fd->sc_blkno) {
1169 printf("fdcintr: block %d != blkno "
1170 "%" PRId64 "\n", block, fd->sc_blkno);
1171 #ifdef DDB
1172 Debugger();
1173 #endif
1174 }
1175 }
1176 #endif
1177 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
1178 isa_dmastart(fdc->sc_ic, fdc->sc_drq,
1179 (char *)bp->b_data + fd->sc_skip, fd->sc_nbytes,
1180 NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
1181 bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
1182 #ifdef FD_DEBUG
1183 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1184 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1185 head, sec, nblks);
1186 #endif
1187 if (finfo) {
1188 /* formatting */
1189 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
1190 fdc->sc_errors = 4;
1191 fdcretry(fdc);
1192 goto loop;
1193 }
1194 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1195 out_fdc(iot, ioh, finfo->fd_formb_secshift);
1196 out_fdc(iot, ioh, finfo->fd_formb_nsecs);
1197 out_fdc(iot, ioh, finfo->fd_formb_gaplen);
1198 out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
1199 } else {
1200 if (read)
1201 out_fdc(iot, ioh, NE7CMD_READ); /* READ */
1202 else
1203 out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
1204 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1205 out_fdc(iot, ioh, fd->sc_cylin); /* track */
1206 out_fdc(iot, ioh, head);
1207 out_fdc(iot, ioh, sec + 1); /* sector +1 */
1208 out_fdc(iot, ioh, type->secsize);/* sector size */
1209 out_fdc(iot, ioh, type->sectrac);/* sectors/track */
1210 out_fdc(iot, ioh, type->gap1); /* gap1 size */
1211 out_fdc(iot, ioh, type->datalen);/* data length */
1212 }
1213 fdc->sc_state = IOCOMPLETE;
1214
1215 disk_busy(&fd->sc_dk);
1216
1217 /* allow 2 seconds for operation */
1218 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1219 return 1; /* will return later */
1220
1221 case SEEKWAIT:
1222 callout_stop(&fdc->sc_timo_ch);
1223 fdc->sc_state = SEEKCOMPLETE;
1224 /* allow 1/50 second for heads to settle */
1225 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcintrcb, fdc);
1226 return 1;
1227
1228 case SEEKCOMPLETE:
1229 /* no data on seek */
1230 disk_unbusy(&fd->sc_dk, 0, 0);
1231
1232 /* Make sure seek really happened. */
1233 out_fdc(iot, ioh, NE7CMD_SENSEI);
1234 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
1235 cyl != bp->b_cylinder * fd->sc_type->step) {
1236 #ifdef FD_DEBUG
1237 fdcstatus(fd->sc_dev, 2, "seek failed");
1238 #endif
1239 fdcretry(fdc);
1240 goto loop;
1241 }
1242 fd->sc_cylin = bp->b_cylinder;
1243 goto doio;
1244
1245 case IOTIMEDOUT:
1246 isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1247 case SEEKTIMEDOUT:
1248 case RECALTIMEDOUT:
1249 case RESETTIMEDOUT:
1250 fdcretry(fdc);
1251 goto loop;
1252
1253 case IOCOMPLETE: /* IO DONE, post-analyze */
1254 callout_stop(&fdc->sc_timo_ch);
1255
1256 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1257 (bp->b_flags & B_READ));
1258
1259 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
1260 isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1261 #ifdef FD_DEBUG
1262 fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ?
1263 "read failed" : "write failed");
1264 printf("blkno %llu nblks %d\n",
1265 (unsigned long long)fd->sc_blkno, fd->sc_nblks);
1266 #endif
1267 fdcretry(fdc);
1268 goto loop;
1269 }
1270 isa_dmadone(fdc->sc_ic, fdc->sc_drq);
1271 if (fdc->sc_errors) {
1272 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
1273 fd->sc_skip / FDC_BSIZE, NULL);
1274 printf("\n");
1275 fdc->sc_errors = 0;
1276 }
1277 fd->sc_blkno += fd->sc_nblks;
1278 fd->sc_skip += fd->sc_nbytes;
1279 fd->sc_bcount -= fd->sc_nbytes;
1280 if (!finfo && fd->sc_bcount > 0) {
1281 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1282 goto doseek;
1283 }
1284 fdfinish(fd, bp);
1285 goto loop;
1286
1287 case DORESET:
1288 /* try a reset, keep motor on */
1289 fd_set_motor(fdc, 1);
1290 delay(100);
1291 fd_set_motor(fdc, 0);
1292 fdc->sc_state = RESETCOMPLETE;
1293 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1294 return 1; /* will return later */
1295
1296 case RESETCOMPLETE:
1297 callout_stop(&fdc->sc_timo_ch);
1298 /* clear the controller output buffer */
1299 for (i = 0; i < 4; i++) {
1300 out_fdc(iot, ioh, NE7CMD_SENSEI);
1301 (void) fdcresult(fdc);
1302 }
1303
1304 /* fall through */
1305 case DORECAL:
1306 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1307 out_fdc(iot, ioh, fd->sc_drive);
1308 fdc->sc_state = RECALWAIT;
1309 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1310 return 1; /* will return later */
1311
1312 case RECALWAIT:
1313 callout_stop(&fdc->sc_timo_ch);
1314 fdc->sc_state = RECALCOMPLETE;
1315 /* allow 1/30 second for heads to settle */
1316 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcintrcb, fdc);
1317 return 1; /* will return later */
1318
1319 case RECALCOMPLETE:
1320 out_fdc(iot, ioh, NE7CMD_SENSEI);
1321 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1322 #ifdef FD_DEBUG
1323 fdcstatus(fd->sc_dev, 2, "recalibrate failed");
1324 #endif
1325 fdcretry(fdc);
1326 goto loop;
1327 }
1328 fd->sc_cylin = 0;
1329 goto doseek;
1330
1331 case MOTORWAIT:
1332 if (fd->sc_flags & FD_MOTOR_WAIT)
1333 return 1; /* time's not up yet */
1334 goto doseek;
1335
1336 default:
1337 fdcstatus(fd->sc_dev, 0, "stray interrupt");
1338 return 1;
1339 }
1340 #undef st0
1341 #undef cyl
1342
1343 out:
1344 cv_signal(&fdc->sc_cv);
1345 return 1;
1346 }
1347
1348 static void
1349 fdcintrcb(void *arg)
1350 {
1351 (void)fdcintr(arg);
1352 }
1353
1354 int
1355 fdcintr(void *arg)
1356 {
1357 int rc;
1358 struct fdc_softc *fdc = arg;
1359
1360 mutex_enter(&fdc->sc_mtx);
1361 rc = fdcintr1(fdc);
1362 mutex_exit(&fdc->sc_mtx);
1363 return rc;
1364 }
1365
1366 void
1367 fdcretry(struct fdc_softc *fdc)
1368 {
1369 struct fd_softc *fd;
1370 struct buf *bp;
1371
1372 fd = TAILQ_FIRST(&fdc->sc_drives);
1373 bp = bufq_peek(fd->sc_q);
1374
1375 if (fd->sc_opts & FDOPT_NORETRY)
1376 goto fail;
1377 switch (fdc->sc_errors) {
1378 case 0:
1379 /* try again */
1380 fdc->sc_state = DOSEEK;
1381 break;
1382
1383 case 1: case 2: case 3:
1384 /* didn't work; try recalibrating */
1385 fdc->sc_state = DORECAL;
1386 break;
1387
1388 case 4:
1389 /* still no go; reset the bastard */
1390 fdc->sc_state = DORESET;
1391 break;
1392
1393 default:
1394 fail:
1395 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1396 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1397 fd->sc_skip / FDC_BSIZE, NULL);
1398 fdcpstatus(7, fdc);
1399 }
1400
1401 bp->b_error = EIO;
1402 fdfinish(fd, bp);
1403 }
1404 fdc->sc_errors++;
1405 }
1406
1407 int
1408 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1409 {
1410 device_t self = device_lookup_acquire(&fd_cd, FDUNIT(dev));
1411 struct fd_softc *fd = device_private(self);
1412 struct fdformat_parms *form_parms;
1413 struct fdformat_cmd *form_cmd;
1414 struct ne7_fd_formb *fd_formb;
1415 struct disklabel *lp = fd->sc_dk.dk_label;
1416 int error;
1417 unsigned int scratch;
1418 int il[FD_MAX_NSEC + 1];
1419 int i, j;
1420 #ifdef __HAVE_OLD_DISKLABEL
1421 struct disklabel newlabel;
1422 #endif
1423
1424 switch (cmd) {
1425 case DIOCGPARTINFO:
1426 case DIOCGDINFO:
1427 #ifdef __HAVE_OLD_DISKLABEL
1428 case ODIOCGDINFO:
1429 #endif
1430 memset(lp, 0, sizeof(*lp));
1431
1432 lp->d_type = DKTYPE_FLOPPY;
1433 lp->d_secsize = FDC_BSIZE;
1434 lp->d_nsectors = fd->sc_type->sectrac;
1435 lp->d_ntracks = fd->sc_type->heads;
1436 lp->d_ncylinders = fd->sc_type->cyls;
1437 lp->d_secpercyl = fd->sc_type->seccyl;
1438 lp->d_secperunit = fd->sc_type->size;
1439
1440 if (readdisklabel(dev, fdstrategy, lp, NULL) != NULL) {
1441 device_release(self);
1442 return EINVAL;
1443 }
1444 break;
1445 }
1446
1447 error = disk_ioctl(&fd->sc_dk, dev, cmd, addr, flag, l);
1448 if (error != EPASSTHROUGH) {
1449 device_release(self);
1450 return error;
1451 }
1452
1453 switch (cmd) {
1454 case DIOCWLABEL:
1455 if ((flag & FWRITE) == 0)
1456 return EBADF;
1457 /* XXX do something */
1458 device_release(self);
1459 return 0;
1460
1461 case DIOCWDINFO:
1462 #ifdef __HAVE_OLD_DISKLABEL
1463 case ODIOCWDINFO:
1464 #endif
1465 {
1466 if ((flag & FWRITE) == 0) {
1467 device_release(self);
1468 return EBADF;
1469 }
1470 #ifdef __HAVE_OLD_DISKLABEL
1471 if (cmd == ODIOCWDINFO) {
1472 memset(&newlabel, 0, sizeof newlabel);
1473 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1474 addr = &newlabel;
1475 }
1476 #endif
1477 error = setdisklabel(lp, addr, 0, NULL);
1478 if (error == 0)
1479 error = writedisklabel(dev, fdstrategy, lp, NULL);
1480
1481 device_release(self);
1482 return error;
1483 }
1484
1485 case FDIOCGETFORMAT:
1486 form_parms = (struct fdformat_parms *)addr;
1487 form_parms->fdformat_version = FDFORMAT_VERSION;
1488 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1489 form_parms->ncyl = fd->sc_type->cyls;
1490 form_parms->nspt = fd->sc_type->sectrac;
1491 form_parms->ntrk = fd->sc_type->heads;
1492 form_parms->stepspercyl = fd->sc_type->step;
1493 form_parms->gaplen = fd->sc_type->gap2;
1494 form_parms->fillbyte = fd->sc_type->fillbyte;
1495 form_parms->interleave = fd->sc_type->interleave;
1496 switch (fd->sc_type->rate) {
1497 case FDC_500KBPS:
1498 form_parms->xfer_rate = 500 * 1024;
1499 break;
1500 case FDC_300KBPS:
1501 form_parms->xfer_rate = 300 * 1024;
1502 break;
1503 case FDC_250KBPS:
1504 form_parms->xfer_rate = 250 * 1024;
1505 break;
1506 default:
1507 return EINVAL;
1508 }
1509 device_release(self);
1510 return 0;
1511
1512 case FDIOCSETFORMAT:
1513 if((flag & FWRITE) == 0)
1514 return EBADF; /* must be opened for writing */
1515 form_parms = (struct fdformat_parms *)addr;
1516 if (form_parms->fdformat_version != FDFORMAT_VERSION) {
1517 device_release(self);
1518 return EINVAL; /* wrong version of formatting prog */
1519 }
1520 scratch = form_parms->nbps >> 7;
1521 if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1522 scratch & ~(1 << (ffs(scratch)-1))) {
1523 device_release(self);
1524 return EINVAL; /* not a power-of-two multiple of 128 */
1525 }
1526 switch (form_parms->xfer_rate) {
1527 case 500 * 1024:
1528 fd->sc_type->rate = FDC_500KBPS;
1529 break;
1530 case 300 * 1024:
1531 fd->sc_type->rate = FDC_300KBPS;
1532 break;
1533 case 250 * 1024:
1534 fd->sc_type->rate = FDC_250KBPS;
1535 break;
1536 default:
1537 device_release(self);
1538 return EINVAL;
1539 }
1540
1541 if (form_parms->nspt > FD_MAX_NSEC ||
1542 form_parms->fillbyte > 0xff ||
1543 form_parms->interleave > 0xff) {
1544 device_release(self);
1545 return EINVAL;
1546 }
1547 fd->sc_type->sectrac = form_parms->nspt;
1548 if (form_parms->ntrk != 2 && form_parms->ntrk != 1) {
1549 device_release(self);
1550 return EINVAL;
1551 }
1552 fd->sc_type->heads = form_parms->ntrk;
1553 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1554 fd->sc_type->secsize = ffs(scratch)-1;
1555 fd->sc_type->gap2 = form_parms->gaplen;
1556 fd->sc_type->cyls = form_parms->ncyl;
1557 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1558 form_parms->nbps / DEV_BSIZE;
1559 fd->sc_type->step = form_parms->stepspercyl;
1560 fd->sc_type->fillbyte = form_parms->fillbyte;
1561 fd->sc_type->interleave = form_parms->interleave;
1562
1563 device_release(self);
1564 return 0;
1565
1566 case FDIOCFORMAT_TRACK:
1567 if((flag & FWRITE) == 0) {
1568 device_release(self);
1569 return EBADF; /* must be opened for writing */
1570 }
1571 form_cmd = (struct fdformat_cmd *)addr;
1572 if (form_cmd->formatcmd_version != FDFORMAT_VERSION) {
1573 device_release(self);
1574 return EINVAL; /* wrong version of formatting prog */
1575 }
1576
1577 if (form_cmd->head >= fd->sc_type->heads ||
1578 form_cmd->cylinder >= fd->sc_type->cyls) {
1579 device_release(self);
1580 return EINVAL;
1581 }
1582
1583 fd_formb = malloc(sizeof(struct ne7_fd_formb),
1584 M_TEMP, M_NOWAIT);
1585 if (fd_formb == 0) {
1586 device_release(self);
1587 return ENOMEM;
1588 }
1589
1590 fd_formb->head = form_cmd->head;
1591 fd_formb->cyl = form_cmd->cylinder;
1592 fd_formb->transfer_rate = fd->sc_type->rate;
1593 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1594 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1595 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1596 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1597
1598 memset(il, 0, sizeof il);
1599 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1600 while (il[(j%fd_formb->fd_formb_nsecs)+1])
1601 j++;
1602 il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1603 j += fd->sc_type->interleave;
1604 }
1605 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1606 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1607 fd_formb->fd_formb_headno(i) = form_cmd->head;
1608 fd_formb->fd_formb_secno(i) = il[i+1];
1609 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1610 }
1611
1612 error = fdformat(dev, fd_formb, l);
1613 free(fd_formb, M_TEMP);
1614 device_release(self);
1615 return error;
1616
1617 case FDIOCGETOPTS: /* get drive options */
1618 *(int *)addr = fd->sc_opts;
1619 device_release(self);
1620 return 0;
1621
1622 case FDIOCSETOPTS: /* set drive options */
1623 fd->sc_opts = *(int *)addr;
1624 device_release(self);
1625 return 0;
1626
1627 default:
1628 device_release(self);
1629 return ENOTTY;
1630 }
1631
1632 #ifdef DIAGNOSTIC
1633 panic("fdioctl: impossible");
1634 #endif
1635 }
1636
1637 int
1638 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct lwp *l)
1639 {
1640 device_t self = device_lookup_acquire(&fd_cd, FDUNIT(dev));
1641 int rv = 0;
1642 struct fd_softc *fd = device_private(self);
1643 struct fd_type *type = fd->sc_type;
1644 struct buf *bp;
1645
1646 /* set up a buffer header for fdstrategy() */
1647 bp = getiobuf(NULL, false);
1648 if (bp == NULL) {
1649 if (self != NULL)
1650 device_release(self);
1651 return ENOBUFS;
1652 }
1653
1654 bp->b_cflags = BC_BUSY;
1655 bp->b_flags = B_PHYS | B_FORMAT;
1656 bp->b_proc = l->l_proc;
1657 bp->b_dev = dev;
1658
1659 /*
1660 * calculate a fake blkno, so fdstrategy() would initiate a
1661 * seek to the requested cylinder
1662 */
1663 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1664 + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1665
1666 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1667 bp->b_data = (void *)finfo;
1668
1669 #ifdef FD_DEBUG
1670 printf("fdformat: blkno %" PRIx64 " count %x\n",
1671 bp->b_blkno, bp->b_bcount);
1672 #endif
1673
1674 /* now do the format */
1675 fdstrategy(bp);
1676
1677 /* ...and wait for it to complete */
1678 rv = biowait(bp);
1679 putiobuf(bp);
1680 device_release(self);
1681 return rv;
1682 }
1683
1684 /*
1685 * Mountroot hook: prompt the user to enter the root file system
1686 * floppy.
1687 */
1688 void
1689 fd_mountroot_hook(device_t dev)
1690 {
1691 int c;
1692
1693 printf("Insert filesystem floppy and press return.");
1694 cnpollc(1);
1695 for (;;) {
1696 c = cngetc();
1697 if ((c == '\r') || (c == '\n')) {
1698 printf("\n");
1699 break;
1700 }
1701 }
1702 cnpollc(0);
1703 }
1704
1705 static void
1706 fd_set_geometry(struct fd_softc *fd)
1707 {
1708 const struct fd_type *fdt;
1709
1710 fdt = fd->sc_type;
1711 if (fdt == NULL) {
1712 fdt = fd->sc_deftype;
1713 if (fdt == NULL)
1714 return;
1715 }
1716
1717 struct disk_geom *dg = &fd->sc_dk.dk_geom;
1718
1719 memset(dg, 0, sizeof(*dg));
1720 dg->dg_secperunit = fdt->size;
1721 dg->dg_nsectors = fdt->sectrac;
1722 switch (fdt->secsize) {
1723 case 2:
1724 dg->dg_secsize = 512;
1725 break;
1726 case 3:
1727 dg->dg_secsize = 1024;
1728 break;
1729 default:
1730 break;
1731 }
1732 dg->dg_ntracks = fdt->heads;
1733 dg->dg_ncylinders = fdt->cyls;
1734 disk_set_info(fd->sc_dev, &fd->sc_dk, NULL);
1735 }
1736