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