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