fd.c revision 1.18.4.1 1 /* $NetBSD: fd.c,v 1.18.4.1 2001/09/07 04:45:27 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 "rnd.h"
95 #include "opt_ddb.h"
96
97 /*
98 * XXX This driver should be properly MI'd some day, but this allows us
99 * XXX to eliminate a lot of code duplication for now.
100 */
101 #if !defined(alpha) && !defined(algor) && !defined(atari) && \
102 !defined(bebox) && !defined(i386) && !defined(prep) && \
103 !defined(sandpoint)
104 #error platform not supported by this driver, yet
105 #endif
106
107 #include <sys/param.h>
108 #include <sys/systm.h>
109 #include <sys/callout.h>
110 #include <sys/kernel.h>
111 #include <sys/file.h>
112 #include <sys/ioctl.h>
113 #include <sys/device.h>
114 #include <sys/disklabel.h>
115 #include <sys/dkstat.h>
116 #include <sys/disk.h>
117 #include <sys/buf.h>
118 #include <sys/malloc.h>
119 #include <sys/uio.h>
120 #include <sys/syslog.h>
121 #include <sys/queue.h>
122 #include <sys/proc.h>
123 #include <sys/fdio.h>
124 #include <sys/vnode.h>
125 #include <sys/conf.h>
126 #if NRND > 0
127 #include <sys/rnd.h>
128 #endif
129
130 #include <miscfs/specfs/specdev.h>
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((struct vnode *, 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 = bp->b_devvp->v_devcookie;
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(devvp, uio, flags)
704 struct vnode *devvp;
705 struct uio *uio;
706 int flags;
707 {
708
709 return (physio(fdstrategy, NULL, devvp, B_READ, minphys, uio));
710 }
711
712 int
713 fdwrite(devvp, uio, flags)
714 struct vnode *devvp;
715 struct uio *uio;
716 int flags;
717 {
718
719 return (physio(fdstrategy, NULL, devvp, 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(devvp, flags, mode, p)
820 struct vnode *devvp;
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(devvp->v_rdev));
829 if (fd == NULL)
830 return (ENXIO);
831
832 type = fd_dev_to_type(fd, devvp->v_rdev);
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 devvp->v_devcookie = fd;
841
842 fd->sc_type_copy = *type;
843 fd->sc_type = &fd->sc_type_copy;
844 fd->sc_cylin = -1;
845 fd->sc_flags |= FD_OPEN;
846
847 return 0;
848 }
849
850 int
851 fdclose(devvp, flags, mode, p)
852 struct vnode *devvp;
853 int flags;
854 int mode;
855 struct proc *p;
856 {
857 struct fd_softc *fd = devvp->v_devcookie;
858
859 fd->sc_flags &= ~FD_OPEN;
860 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
861 return 0;
862 }
863
864 void
865 fdcstart(fdc)
866 struct fdc_softc *fdc;
867 {
868
869 #ifdef DIAGNOSTIC
870 /* only got here if controller's drive queue was inactive; should
871 be in idle state */
872 if (fdc->sc_state != DEVIDLE) {
873 printf("fdcstart: not idle\n");
874 return;
875 }
876 #endif
877 (void) fdcintr(fdc);
878 }
879
880 void
881 fdcstatus(dv, n, s)
882 struct device *dv;
883 int n;
884 char *s;
885 {
886 struct fdc_softc *fdc = (void *)dv->dv_parent;
887 char bits[64];
888
889 if (n == 0) {
890 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
891 (void) fdcresult(fdc);
892 n = 2;
893 }
894
895 printf("%s: %s", dv->dv_xname, s);
896
897 switch (n) {
898 case 0:
899 printf("\n");
900 break;
901 case 2:
902 printf(" (st0 %s cyl %d)\n",
903 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
904 bits, sizeof(bits)), fdc->sc_status[1]);
905 break;
906 case 7:
907 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
908 NE7_ST0BITS, bits, sizeof(bits)));
909 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
910 NE7_ST1BITS, bits, sizeof(bits)));
911 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
912 NE7_ST2BITS, bits, sizeof(bits)));
913 printf(" cyl %d head %d sec %d)\n",
914 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
915 break;
916 #ifdef DIAGNOSTIC
917 default:
918 printf("\nfdcstatus: weird size");
919 break;
920 #endif
921 }
922 }
923
924 void
925 fdctimeout(arg)
926 void *arg;
927 {
928 struct fdc_softc *fdc = arg;
929 struct fd_softc *fd = fdc->sc_drives.tqh_first;
930 int s;
931
932 s = splbio();
933 #ifdef DEBUG
934 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
935 #endif
936 fdcstatus(&fd->sc_dev, 0, "timeout");
937
938 if (BUFQ_FIRST(&fd->sc_q) != NULL)
939 fdc->sc_state++;
940 else
941 fdc->sc_state = DEVIDLE;
942
943 (void) fdcintr(fdc);
944 splx(s);
945 }
946
947 void
948 fdcpseudointr(arg)
949 void *arg;
950 {
951 int s;
952
953 /* Just ensure it has the right spl. */
954 s = splbio();
955 (void) fdcintr(arg);
956 splx(s);
957 }
958
959 int
960 fdcintr(arg)
961 void *arg;
962 {
963 struct fdc_softc *fdc = arg;
964 #define st0 fdc->sc_status[0]
965 #define cyl fdc->sc_status[1]
966 struct fd_softc *fd;
967 struct buf *bp;
968 bus_space_tag_t iot = fdc->sc_iot;
969 bus_space_handle_t ioh = fdc->sc_ioh;
970 int read, head, sec, i, nblks;
971 struct fd_type *type;
972 struct ne7_fd_formb *finfo = NULL;
973
974 loop:
975 /* Is there a drive for the controller to do a transfer with? */
976 fd = fdc->sc_drives.tqh_first;
977 if (fd == NULL) {
978 fdc->sc_state = DEVIDLE;
979 return 1;
980 }
981
982 /* Is there a transfer to this drive? If not, deactivate drive. */
983 bp = BUFQ_FIRST(&fd->sc_q);
984 if (bp == NULL) {
985 fd->sc_ops = 0;
986 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
987 fd->sc_active = 0;
988 goto loop;
989 }
990
991 if (bp->b_flags & B_FORMAT)
992 finfo = (struct ne7_fd_formb *)bp->b_data;
993
994 switch (fdc->sc_state) {
995 case DEVIDLE:
996 fdc->sc_errors = 0;
997 fd->sc_skip = 0;
998 fd->sc_bcount = bp->b_bcount;
999 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1000 callout_stop(&fd->sc_motoroff_ch);
1001 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1002 fdc->sc_state = MOTORWAIT;
1003 return 1;
1004 }
1005 if ((fd->sc_flags & FD_MOTOR) == 0) {
1006 /* Turn on the motor, being careful about pairing. */
1007 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1008 if (ofd && ofd->sc_flags & FD_MOTOR) {
1009 callout_stop(&ofd->sc_motoroff_ch);
1010 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1011 }
1012 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1013 fd_set_motor(fdc, 0);
1014 fdc->sc_state = MOTORWAIT;
1015 /* Allow .25s for motor to stabilize. */
1016 callout_reset(&fd->sc_motoron_ch, hz / 4,
1017 fd_motor_on, fd);
1018 return 1;
1019 }
1020 /* Make sure the right drive is selected. */
1021 fd_set_motor(fdc, 0);
1022
1023 /* fall through */
1024 case DOSEEK:
1025 doseek:
1026 if (fd->sc_cylin == bp->b_cylinder)
1027 goto doio;
1028
1029 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
1030 out_fdc(iot, ioh, fd->sc_type->steprate);
1031 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
1032
1033 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
1034 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
1035 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
1036
1037 fd->sc_cylin = -1;
1038 fdc->sc_state = SEEKWAIT;
1039
1040 fd->sc_dk.dk_seek++;
1041 disk_busy(&fd->sc_dk);
1042
1043 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1044 return 1;
1045
1046 case DOIO:
1047 doio:
1048 type = fd->sc_type;
1049 if (finfo)
1050 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1051 (char *)finfo;
1052 sec = fd->sc_blkno % type->seccyl;
1053 nblks = type->seccyl - sec;
1054 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1055 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
1056 fd->sc_nblks = nblks;
1057 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1058 head = sec / type->sectrac;
1059 sec -= head * type->sectrac;
1060 #ifdef DIAGNOSTIC
1061 {
1062 int block;
1063 block = (fd->sc_cylin * type->heads + head)
1064 * type->sectrac + sec;
1065 if (block != fd->sc_blkno) {
1066 printf("fdcintr: block %d != blkno %d\n",
1067 block, fd->sc_blkno);
1068 #ifdef DDB
1069 Debugger();
1070 #endif
1071 }
1072 }
1073 #endif
1074 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
1075 isa_dmastart(fdc->sc_ic, fdc->sc_drq,
1076 bp->b_data + fd->sc_skip, fd->sc_nbytes,
1077 NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
1078 bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
1079 #ifdef FD_DEBUG
1080 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1081 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1082 head, sec, nblks);
1083 #endif
1084 if (finfo) {
1085 /* formatting */
1086 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
1087 fdc->sc_errors = 4;
1088 fdcretry(fdc);
1089 goto loop;
1090 }
1091 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1092 out_fdc(iot, ioh, finfo->fd_formb_secshift);
1093 out_fdc(iot, ioh, finfo->fd_formb_nsecs);
1094 out_fdc(iot, ioh, finfo->fd_formb_gaplen);
1095 out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
1096 } else {
1097 if (read)
1098 out_fdc(iot, ioh, NE7CMD_READ); /* READ */
1099 else
1100 out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
1101 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1102 out_fdc(iot, ioh, fd->sc_cylin); /* track */
1103 out_fdc(iot, ioh, head);
1104 out_fdc(iot, ioh, sec + 1); /* sector +1 */
1105 out_fdc(iot, ioh, type->secsize);/* sector size */
1106 out_fdc(iot, ioh, type->sectrac);/* sectors/track */
1107 out_fdc(iot, ioh, type->gap1); /* gap1 size */
1108 out_fdc(iot, ioh, type->datalen);/* data length */
1109 }
1110 fdc->sc_state = IOCOMPLETE;
1111
1112 disk_busy(&fd->sc_dk);
1113
1114 /* allow 2 seconds for operation */
1115 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1116 return 1; /* will return later */
1117
1118 case SEEKWAIT:
1119 callout_stop(&fdc->sc_timo_ch);
1120 fdc->sc_state = SEEKCOMPLETE;
1121 /* allow 1/50 second for heads to settle */
1122 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
1123 return 1;
1124
1125 case SEEKCOMPLETE:
1126 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */
1127
1128 /* Make sure seek really happened. */
1129 out_fdc(iot, ioh, NE7CMD_SENSEI);
1130 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
1131 cyl != bp->b_cylinder * fd->sc_type->step) {
1132 #ifdef FD_DEBUG
1133 fdcstatus(&fd->sc_dev, 2, "seek failed");
1134 #endif
1135 fdcretry(fdc);
1136 goto loop;
1137 }
1138 fd->sc_cylin = bp->b_cylinder;
1139 goto doio;
1140
1141 case IOTIMEDOUT:
1142 isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1143 case SEEKTIMEDOUT:
1144 case RECALTIMEDOUT:
1145 case RESETTIMEDOUT:
1146 fdcretry(fdc);
1147 goto loop;
1148
1149 case IOCOMPLETE: /* IO DONE, post-analyze */
1150 callout_stop(&fdc->sc_timo_ch);
1151
1152 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1153
1154 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
1155 isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1156 #ifdef FD_DEBUG
1157 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1158 "read failed" : "write failed");
1159 printf("blkno %d nblks %d\n",
1160 fd->sc_blkno, fd->sc_nblks);
1161 #endif
1162 fdcretry(fdc);
1163 goto loop;
1164 }
1165 isa_dmadone(fdc->sc_ic, fdc->sc_drq);
1166 if (fdc->sc_errors) {
1167 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
1168 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1169 printf("\n");
1170 fdc->sc_errors = 0;
1171 }
1172 fd->sc_blkno += fd->sc_nblks;
1173 fd->sc_skip += fd->sc_nbytes;
1174 fd->sc_bcount -= fd->sc_nbytes;
1175 if (!finfo && fd->sc_bcount > 0) {
1176 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1177 goto doseek;
1178 }
1179 fdfinish(fd, bp);
1180 goto loop;
1181
1182 case DORESET:
1183 /* try a reset, keep motor on */
1184 fd_set_motor(fdc, 1);
1185 delay(100);
1186 fd_set_motor(fdc, 0);
1187 fdc->sc_state = RESETCOMPLETE;
1188 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1189 return 1; /* will return later */
1190
1191 case RESETCOMPLETE:
1192 callout_stop(&fdc->sc_timo_ch);
1193 /* clear the controller output buffer */
1194 for (i = 0; i < 4; i++) {
1195 out_fdc(iot, ioh, NE7CMD_SENSEI);
1196 (void) fdcresult(fdc);
1197 }
1198
1199 /* fall through */
1200 case DORECAL:
1201 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1202 out_fdc(iot, ioh, fd->sc_drive);
1203 fdc->sc_state = RECALWAIT;
1204 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1205 return 1; /* will return later */
1206
1207 case RECALWAIT:
1208 callout_stop(&fdc->sc_timo_ch);
1209 fdc->sc_state = RECALCOMPLETE;
1210 /* allow 1/30 second for heads to settle */
1211 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
1212 return 1; /* will return later */
1213
1214 case RECALCOMPLETE:
1215 out_fdc(iot, ioh, NE7CMD_SENSEI);
1216 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1217 #ifdef FD_DEBUG
1218 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1219 #endif
1220 fdcretry(fdc);
1221 goto loop;
1222 }
1223 fd->sc_cylin = 0;
1224 goto doseek;
1225
1226 case MOTORWAIT:
1227 if (fd->sc_flags & FD_MOTOR_WAIT)
1228 return 1; /* time's not up yet */
1229 goto doseek;
1230
1231 default:
1232 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1233 return 1;
1234 }
1235 #ifdef DIAGNOSTIC
1236 panic("fdcintr: impossible");
1237 #endif
1238 #undef st0
1239 #undef cyl
1240 }
1241
1242 void
1243 fdcretry(fdc)
1244 struct fdc_softc *fdc;
1245 {
1246 char bits[64];
1247 struct fd_softc *fd;
1248 struct buf *bp;
1249
1250 fd = fdc->sc_drives.tqh_first;
1251 bp = BUFQ_FIRST(&fd->sc_q);
1252
1253 if (fd->sc_opts & FDOPT_NORETRY)
1254 goto fail;
1255 switch (fdc->sc_errors) {
1256 case 0:
1257 /* try again */
1258 fdc->sc_state = DOSEEK;
1259 break;
1260
1261 case 1: case 2: case 3:
1262 /* didn't work; try recalibrating */
1263 fdc->sc_state = DORECAL;
1264 break;
1265
1266 case 4:
1267 /* still no go; reset the bastard */
1268 fdc->sc_state = DORESET;
1269 break;
1270
1271 default:
1272 fail:
1273 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1274 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1275 fd->sc_skip / FDC_BSIZE,
1276 (struct disklabel *)NULL);
1277
1278 printf(" (st0 %s",
1279 bitmask_snprintf(fdc->sc_status[0],
1280 NE7_ST0BITS, bits,
1281 sizeof(bits)));
1282 printf(" st1 %s",
1283 bitmask_snprintf(fdc->sc_status[1],
1284 NE7_ST1BITS, bits,
1285 sizeof(bits)));
1286 printf(" st2 %s",
1287 bitmask_snprintf(fdc->sc_status[2],
1288 NE7_ST2BITS, bits,
1289 sizeof(bits)));
1290 printf(" cyl %d head %d sec %d)\n",
1291 fdc->sc_status[3],
1292 fdc->sc_status[4],
1293 fdc->sc_status[5]);
1294 }
1295
1296 bp->b_flags |= B_ERROR;
1297 bp->b_error = EIO;
1298 fdfinish(fd, bp);
1299 }
1300 fdc->sc_errors++;
1301 }
1302
1303 int
1304 fdsize(dev)
1305 dev_t dev;
1306 {
1307
1308 /* Swapping to floppies would not make sense. */
1309 return -1;
1310 }
1311
1312 int
1313 fddump(dev, blkno, va, size)
1314 dev_t dev;
1315 daddr_t blkno;
1316 caddr_t va;
1317 size_t size;
1318 {
1319
1320 /* Not implemented. */
1321 return ENXIO;
1322 }
1323
1324 int
1325 fdioctl(devvp, cmd, addr, flag, p)
1326 struct vnode *devvp;
1327 u_long cmd;
1328 caddr_t addr;
1329 int flag;
1330 struct proc *p;
1331 {
1332 struct fd_softc *fd = devvp->v_devcookie;
1333 struct fdformat_parms *form_parms;
1334 struct fdformat_cmd *form_cmd;
1335 struct ne7_fd_formb *fd_formb;
1336 struct disklabel buffer;
1337 int error;
1338 unsigned int scratch;
1339 int il[FD_MAX_NSEC + 1];
1340 register int i, j;
1341 #ifdef __HAVE_OLD_DISKLABEL
1342 struct disklabel newlabel;
1343 #endif
1344
1345 switch (cmd) {
1346 case DIOCGDINFO:
1347 #ifdef __HAVE_OLD_DISKLABEL
1348 case ODIOCGDINFO:
1349 #endif
1350 memset(&buffer, 0, sizeof(buffer));
1351
1352 buffer.d_secpercyl = fd->sc_type->seccyl;
1353 buffer.d_type = DTYPE_FLOPPY;
1354 buffer.d_secsize = FDC_BSIZE;
1355
1356 if (readdisklabel(devvp, fdstrategy, &buffer, NULL) != NULL)
1357 return EINVAL;
1358
1359 #ifdef __HAVE_OLD_DISKLABEL
1360 if (cmd == ODIOCGDINFO) {
1361 if (buffer.d_npartitions > OLDMAXPARTITIONS)
1362 return ENOTTY;
1363 memcpy(addr, &buffer, sizeof (struct olddisklabel));
1364 } else
1365 #endif
1366 *(struct disklabel *)addr = buffer;
1367 return 0;
1368
1369 case DIOCWLABEL:
1370 if ((flag & FWRITE) == 0)
1371 return EBADF;
1372 /* XXX do something */
1373 return 0;
1374
1375 case DIOCWDINFO:
1376 #ifdef __HAVE_OLD_DISKLABEL
1377 case ODIOCWDINFO:
1378 #endif
1379 {
1380 struct disklabel *lp;
1381
1382 if ((flag & FWRITE) == 0)
1383 return EBADF;
1384 #ifdef __HAVE_OLD_DISKLABEL
1385 if (cmd == ODIOCWDINFO) {
1386 memset(&newlabel, 0, sizeof newlabel);
1387 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1388 lp = &newlabel;
1389 } else
1390 #endif
1391 lp = (struct disklabel *)addr;
1392
1393 error = setdisklabel(&buffer, lp, 0, NULL);
1394 if (error)
1395 return error;
1396
1397 error = writedisklabel(devvp, fdstrategy, &buffer, NULL);
1398 return error;
1399 }
1400
1401 case FDIOCGETFORMAT:
1402 form_parms = (struct fdformat_parms *)addr;
1403 form_parms->fdformat_version = FDFORMAT_VERSION;
1404 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1405 form_parms->ncyl = fd->sc_type->cyls;
1406 form_parms->nspt = fd->sc_type->sectrac;
1407 form_parms->ntrk = fd->sc_type->heads;
1408 form_parms->stepspercyl = fd->sc_type->step;
1409 form_parms->gaplen = fd->sc_type->gap2;
1410 form_parms->fillbyte = fd->sc_type->fillbyte;
1411 form_parms->interleave = fd->sc_type->interleave;
1412 switch (fd->sc_type->rate) {
1413 case FDC_500KBPS:
1414 form_parms->xfer_rate = 500 * 1024;
1415 break;
1416 case FDC_300KBPS:
1417 form_parms->xfer_rate = 300 * 1024;
1418 break;
1419 case FDC_250KBPS:
1420 form_parms->xfer_rate = 250 * 1024;
1421 break;
1422 default:
1423 return EINVAL;
1424 }
1425 return 0;
1426
1427 case FDIOCSETFORMAT:
1428 if((flag & FWRITE) == 0)
1429 return EBADF; /* must be opened for writing */
1430 form_parms = (struct fdformat_parms *)addr;
1431 if (form_parms->fdformat_version != FDFORMAT_VERSION)
1432 return EINVAL; /* wrong version of formatting prog */
1433
1434 scratch = form_parms->nbps >> 7;
1435 if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1436 scratch & ~(1 << (ffs(scratch)-1)))
1437 /* not a power-of-two multiple of 128 */
1438 return EINVAL;
1439
1440 switch (form_parms->xfer_rate) {
1441 case 500 * 1024:
1442 fd->sc_type->rate = FDC_500KBPS;
1443 break;
1444 case 300 * 1024:
1445 fd->sc_type->rate = FDC_300KBPS;
1446 break;
1447 case 250 * 1024:
1448 fd->sc_type->rate = FDC_250KBPS;
1449 break;
1450 default:
1451 return EINVAL;
1452 }
1453
1454 if (form_parms->nspt > FD_MAX_NSEC ||
1455 form_parms->fillbyte > 0xff ||
1456 form_parms->interleave > 0xff)
1457 return EINVAL;
1458 fd->sc_type->sectrac = form_parms->nspt;
1459 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1460 return EINVAL;
1461 fd->sc_type->heads = form_parms->ntrk;
1462 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1463 fd->sc_type->secsize = ffs(scratch)-1;
1464 fd->sc_type->gap2 = form_parms->gaplen;
1465 fd->sc_type->cyls = form_parms->ncyl;
1466 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1467 form_parms->nbps / DEV_BSIZE;
1468 fd->sc_type->step = form_parms->stepspercyl;
1469 fd->sc_type->fillbyte = form_parms->fillbyte;
1470 fd->sc_type->interleave = form_parms->interleave;
1471 return 0;
1472
1473 case FDIOCFORMAT_TRACK:
1474 if((flag & FWRITE) == 0)
1475 return EBADF; /* must be opened for writing */
1476 form_cmd = (struct fdformat_cmd *)addr;
1477 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1478 return EINVAL; /* wrong version of formatting prog */
1479
1480 if (form_cmd->head >= fd->sc_type->heads ||
1481 form_cmd->cylinder >= fd->sc_type->cyls) {
1482 return EINVAL;
1483 }
1484
1485 fd_formb = malloc(sizeof(struct ne7_fd_formb),
1486 M_TEMP, M_NOWAIT);
1487 if (fd_formb == 0)
1488 return ENOMEM;
1489
1490 fd_formb->head = form_cmd->head;
1491 fd_formb->cyl = form_cmd->cylinder;
1492 fd_formb->transfer_rate = fd->sc_type->rate;
1493 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1494 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1495 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1496 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1497
1498 memset(il, 0, sizeof il);
1499 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1500 while (il[(j%fd_formb->fd_formb_nsecs)+1])
1501 j++;
1502 il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1503 j += fd->sc_type->interleave;
1504 }
1505 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1506 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1507 fd_formb->fd_formb_headno(i) = form_cmd->head;
1508 fd_formb->fd_formb_secno(i) = il[i+1];
1509 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1510 }
1511
1512 error = fdformat(devvp, fd_formb, p);
1513 free(fd_formb, M_TEMP);
1514 return error;
1515
1516 case FDIOCGETOPTS: /* get drive options */
1517 *(int *)addr = fd->sc_opts;
1518 return 0;
1519
1520 case FDIOCSETOPTS: /* set drive options */
1521 fd->sc_opts = *(int *)addr;
1522 return 0;
1523
1524 default:
1525 return ENOTTY;
1526 }
1527
1528 #ifdef DIAGNOSTIC
1529 panic("fdioctl: impossible");
1530 #endif
1531 }
1532
1533 int
1534 fdformat(devvp, finfo, p)
1535 struct vnode *devvp;
1536 struct ne7_fd_formb *finfo;
1537 struct proc *p;
1538 {
1539 int rv = 0, s;
1540 struct fd_softc *fd = devvp->v_devcookie;
1541 struct fd_type *type = fd->sc_type;
1542 struct buf *bp;
1543
1544 /* set up a buffer header for fdstrategy() */
1545 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1546 if(bp == 0)
1547 return ENOBUFS;
1548 memset((void *)bp, 0, sizeof(struct buf));
1549 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1550 bp->b_proc = p;
1551 bp->b_devvp = devvp;
1552
1553 /*
1554 * calculate a fake blkno, so fdstrategy() would initiate a
1555 * seek to the requested cylinder
1556 */
1557 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1558 + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1559
1560 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1561 bp->b_data = (caddr_t)finfo;
1562
1563 #ifdef DEBUG
1564 printf("fdformat: blkno %x count %lx\n", bp->b_blkno, bp->b_bcount);
1565 #endif
1566
1567 /* now do the format */
1568 fdstrategy(bp);
1569
1570 /* ...and wait for it to complete */
1571 s = splbio();
1572 while (!(bp->b_flags & B_DONE)) {
1573 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
1574 if (rv == EWOULDBLOCK)
1575 break;
1576 }
1577 splx(s);
1578
1579 if (rv == EWOULDBLOCK) {
1580 /* timed out */
1581 rv = EIO;
1582 biodone(bp);
1583 }
1584 if(bp->b_flags & B_ERROR) {
1585 rv = bp->b_error;
1586 }
1587 free(bp, M_TEMP);
1588 return rv;
1589 }
1590
1591 /*
1592 * Mountroot hook: prompt the user to enter the root file system
1593 * floppy.
1594 */
1595 void
1596 fd_mountroot_hook(dev)
1597 struct device *dev;
1598 {
1599 int c;
1600
1601 printf("Insert filesystem floppy and press return.");
1602 cnpollc(1);
1603 for (;;) {
1604 c = cngetc();
1605 if ((c == '\r') || (c == '\n')) {
1606 printf("\n");
1607 break;
1608 }
1609 }
1610 cnpollc(0);
1611 }
1612