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