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