fd.c revision 1.47 1 /* $NetBSD: fd.c,v 1.47 2003/09/23 21:36:07 mycroft 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. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)fd.c 7.4 (Berkeley) 5/25/91
71 */
72
73 /*
74 * Floppy formatting facilities merged from FreeBSD fd.c driver:
75 * Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
76 * which carries the same copyright/redistribution notice as shown above with
77 * the addition of the following statement before the "Redistribution and
78 * use ..." clause:
79 *
80 * Copyright (c) 1993, 1994 by
81 * jc (at) irbs.UUCP (John Capo)
82 * vak (at) zebub.msk.su (Serge Vakulenko)
83 * ache (at) astral.msk.su (Andrew A. Chernov)
84 *
85 * Copyright (c) 1993, 1994, 1995 by
86 * joerg_wunsch (at) uriah.sax.de (Joerg Wunsch)
87 * dufault (at) hda.com (Peter Dufault)
88 */
89
90 #include <sys/cdefs.h>
91 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.47 2003/09/23 21:36:07 mycroft Exp $");
92
93 #include "rnd.h"
94 #include "opt_ddb.h"
95
96 /*
97 * XXX This driver should be properly MI'd some day, but this allows us
98 * XXX to eliminate a lot of code duplication for now.
99 */
100 #if !defined(alpha) && !defined(algor) && !defined(atari) && \
101 !defined(bebox) && !defined(evbmips) && !defined(i386) && \
102 !defined(prep) && !defined(sandpoint) && !defined(x86_64)
103 #error platform not supported by this driver, yet
104 #endif
105
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/callout.h>
109 #include <sys/kernel.h>
110 #include <sys/file.h>
111 #include <sys/ioctl.h>
112 #include <sys/device.h>
113 #include <sys/disklabel.h>
114 #include <sys/disk.h>
115 #include <sys/buf.h>
116 #include <sys/malloc.h>
117 #include <sys/uio.h>
118 #include <sys/syslog.h>
119 #include <sys/queue.h>
120 #include <sys/proc.h>
121 #include <sys/fdio.h>
122 #include <sys/conf.h>
123 #if NRND > 0
124 #include <sys/rnd.h>
125 #endif
126
127 #include <uvm/uvm_extern.h>
128
129 #include <dev/cons.h>
130
131 #include <machine/cpu.h>
132 #include <machine/bus.h>
133
134 #if defined(atari)
135 /*
136 * On the atari, it is configured as fdcisa
137 */
138 #define FDCCF_DRIVE FDCISACF_DRIVE
139 #define FDCCF_DRIVE_DEFAULT FDCISACF_DRIVE_DEFAULT
140
141 #define fd_cd fdisa_cd
142 #endif /* atari */
143
144 #include <machine/intr.h>
145
146 #include <dev/isa/isavar.h>
147 #include <dev/isa/isadmavar.h>
148
149 #include <dev/isa/fdreg.h>
150 #include <dev/isa/fdcvar.h>
151
152 #if defined(i386)
153
154 #include <dev/ic/mc146818reg.h> /* for NVRAM access */
155 #include <i386/isa/nvram.h>
156
157 #include "mca.h"
158 #if NMCA > 0
159 #include <machine/mca_machdep.h> /* for MCA_system */
160 #endif
161
162 #endif /* i386 */
163
164 #include <dev/isa/fdvar.h>
165
166 #define FDUNIT(dev) (minor(dev) / 8)
167 #define FDTYPE(dev) (minor(dev) % 8)
168
169 /* XXX misuse a flag to identify format operation */
170 #define B_FORMAT B_XXX
171
172 /* controller driver configuration */
173 int fdprint __P((void *, const char *));
174
175 #if NMCA > 0
176 /* MCA - specific entries */
177 const struct fd_type mca_fd_types[] = {
178 { 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 */
179 { 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 */
180 };
181 #endif /* NMCA > 0 */
182
183 /* The order of entries in the following table is important -- BEWARE! */
184
185 #if defined(atari)
186 const struct fd_type fd_types[] = {
187 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
188 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette */
189 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
190 };
191 #else
192 const struct fd_type fd_types[] = {
193 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
194 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB" }, /* 1.2 MB AT-diskettes */
195 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
196 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
197 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette */
198 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x" }, /* 720kB in 1.2MB drive */
199 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */
200 };
201 #endif /* defined(atari) */
202
203 int fdprobe __P((struct device *, struct cfdata *, void *));
204 void fdattach __P((struct device *, struct device *, void *));
205
206 extern struct cfdriver fd_cd;
207
208 #ifdef atari
209 CFATTACH_DECL(fdisa, sizeof(struct fd_softc),
210 fdprobe, fdattach, NULL, NULL);
211 #else
212 CFATTACH_DECL(fd, sizeof(struct fd_softc),
213 fdprobe, fdattach, NULL, NULL);
214 #endif
215
216 dev_type_open(fdopen);
217 dev_type_close(fdclose);
218 dev_type_read(fdread);
219 dev_type_write(fdwrite);
220 dev_type_ioctl(fdioctl);
221 dev_type_strategy(fdstrategy);
222
223 const struct bdevsw fd_bdevsw = {
224 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
225 };
226
227 const struct cdevsw fd_cdevsw = {
228 fdopen, fdclose, fdread, fdwrite, fdioctl,
229 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
230 };
231
232 void fdgetdisklabel __P((struct fd_softc *));
233 int fd_get_parms __P((struct fd_softc *));
234 void fdstart __P((struct fd_softc *));
235
236 struct dkdriver fddkdriver = { fdstrategy };
237
238 #if defined(i386)
239 const struct fd_type *fd_nvtotype __P((char *, int, int));
240 #endif /* i386 */
241 void fd_set_motor __P((struct fdc_softc *fdc, int reset));
242 void fd_motor_off __P((void *arg));
243 void fd_motor_on __P((void *arg));
244 int fdcresult __P((struct fdc_softc *fdc));
245 void fdcstart __P((struct fdc_softc *fdc));
246 void fdcstatus __P((struct device *dv, int n, char *s));
247 void fdctimeout __P((void *arg));
248 void fdcpseudointr __P((void *arg));
249 void fdcretry __P((struct fdc_softc *fdc));
250 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
251 __inline const struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
252 int fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
253
254 void fd_mountroot_hook __P((struct device *));
255
256 /*
257 * Arguments passed between fdcattach and fdprobe.
258 */
259 struct fdc_attach_args {
260 int fa_drive;
261 const struct fd_type *fa_deftype;
262 };
263
264 /*
265 * Print the location of a disk drive (called just before attaching the
266 * the drive). If `fdc' is not NULL, the drive was found but was not
267 * in the system config file; print the drive name as well.
268 * Return QUIET (config_find ignores this if the device was configured) to
269 * avoid printing `fdN not configured' messages.
270 */
271 int
272 fdprint(aux, fdc)
273 void *aux;
274 const char *fdc;
275 {
276 register struct fdc_attach_args *fa = aux;
277
278 if (!fdc)
279 aprint_normal(" drive %d", fa->fa_drive);
280 return QUIET;
281 }
282
283 void
284 fdcattach(fdc)
285 struct fdc_softc *fdc;
286 {
287 struct fdc_attach_args fa;
288 bus_space_tag_t iot;
289 bus_space_handle_t ioh;
290 #if defined(i386)
291 int type;
292 #endif
293
294 iot = fdc->sc_iot;
295 ioh = fdc->sc_ioh;
296 callout_init(&fdc->sc_timo_ch);
297 callout_init(&fdc->sc_intr_ch);
298
299 fdc->sc_state = DEVIDLE;
300 TAILQ_INIT(&fdc->sc_drives);
301
302 fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
303
304 if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) {
305 printf("%s: can't reserve drq %d\n",
306 fdc->sc_dev.dv_xname, fdc->sc_drq);
307 return;
308 }
309
310 if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
311 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
312 printf("%s: can't set up ISA DMA map\n",
313 fdc->sc_dev.dv_xname);
314 return;
315 }
316
317 /*
318 * Reset the controller to get it into a known state. Not all
319 * probes necessarily need do this to discover the controller up
320 * front, so don't assume anything.
321 */
322
323 bus_space_write_1(iot, ioh, fdout, 0);
324 delay(100);
325 bus_space_write_1(iot, ioh, fdout, FDO_FRST);
326
327 /* see if it can handle a command */
328 if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
329 printf ("%s: can't reset controller\n", fdc->sc_dev.dv_xname);
330 return;
331 }
332 out_fdc(iot, ioh, 0xdf);
333 out_fdc(iot, ioh, 2);
334
335 #if defined(i386)
336 /*
337 * The NVRAM info only tells us about the first two disks on the
338 * `primary' floppy controller.
339 */
340 if (fdc->sc_dev.dv_unit == 0)
341 type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
342 else
343 type = -1;
344 #endif /* i386 */
345
346 /* physical limit: four drives per controller. */
347 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
348 if (fdc->sc_known) {
349 if (fdc->sc_present & (1 << fa.fa_drive)) {
350 fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive];
351 config_found(&fdc->sc_dev, (void *)&fa,
352 fdprint);
353 }
354 } else {
355 #if defined(i386)
356 if (type >= 0 && fa.fa_drive < 2)
357 fa.fa_deftype = fd_nvtotype(fdc->sc_dev.dv_xname,
358 type, fa.fa_drive);
359 else
360 fa.fa_deftype = NULL; /* unknown */
361 #elif defined(atari)
362 /*
363 * Atari has a different ordening, defaults to 1.44
364 */
365 fa.fa_deftype = &fd_types[2];
366 #else
367 /*
368 * Default to 1.44MB on Alpha and BeBox. How do we tell
369 * on these platforms?
370 */
371 fa.fa_deftype = &fd_types[0];
372 #endif /* i386 */
373 (void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
374 }
375 }
376 }
377
378 int
379 fdprobe(parent, match, aux)
380 struct device *parent;
381 struct cfdata *match;
382 void *aux;
383 {
384 struct fdc_softc *fdc = (void *)parent;
385 struct cfdata *cf = match;
386 struct fdc_attach_args *fa = aux;
387 int drive = fa->fa_drive;
388 bus_space_tag_t iot = fdc->sc_iot;
389 bus_space_handle_t ioh = fdc->sc_ioh;
390 int n;
391
392 if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
393 cf->cf_loc[FDCCF_DRIVE] != drive)
394 return 0;
395 /*
396 * XXX
397 * This is to work around some odd interactions between this driver
398 * and SMC Ethernet cards.
399 */
400 if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
401 return 0;
402
403 /* Use PNP information if available */
404 if (fdc->sc_known)
405 return 1;
406
407 /* select drive and turn on motor */
408 bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
409 /* wait for motor to spin up */
410 delay(250000);
411 out_fdc(iot, ioh, NE7CMD_RECAL);
412 out_fdc(iot, ioh, drive);
413 /* wait for recalibrate, up to 2s */
414 for (n = 20000; n; n--) {
415 delay(100);
416 if ((bus_space_read_1(iot, ioh, fdsts) & FDS_DRVBUSY(drive)) == 0)
417 break;
418 }
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 u_char i;
781 u_int j = 100000;
782
783 for (; j; j--) {
784 i = bus_space_read_1(iot, ioh, fdsts) &
785 (NE7_DIO | NE7_RQM);
786 if (i == NE7_RQM) {
787 bus_space_write_1(iot, ioh, fddata, x);
788 return 0;
789 }
790 delay(10);
791 }
792 return -1;
793 }
794
795 int
796 fdopen(dev, flags, mode, p)
797 dev_t dev;
798 int flags;
799 int mode;
800 struct proc *p;
801 {
802 struct fd_softc *fd;
803 const struct fd_type *type;
804
805 fd = device_lookup(&fd_cd, FDUNIT(dev));
806 if (fd == NULL)
807 return (ENXIO);
808
809 type = fd_dev_to_type(fd, dev);
810 if (type == NULL)
811 return ENXIO;
812
813 if ((fd->sc_flags & FD_OPEN) != 0 &&
814 memcmp(fd->sc_type, type, sizeof(*type)))
815 return EBUSY;
816
817 fd->sc_type_copy = *type;
818 fd->sc_type = &fd->sc_type_copy;
819 fd->sc_cylin = -1;
820 fd->sc_flags |= FD_OPEN;
821
822 return 0;
823 }
824
825 int
826 fdclose(dev, flags, mode, p)
827 dev_t dev;
828 int flags;
829 int mode;
830 struct proc *p;
831 {
832 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
833
834 fd->sc_flags &= ~FD_OPEN;
835 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
836 return 0;
837 }
838
839 void
840 fdcstart(fdc)
841 struct fdc_softc *fdc;
842 {
843
844 #ifdef DIAGNOSTIC
845 /* only got here if controller's drive queue was inactive; should
846 be in idle state */
847 if (fdc->sc_state != DEVIDLE) {
848 printf("fdcstart: not idle\n");
849 return;
850 }
851 #endif
852 (void) fdcintr(fdc);
853 }
854
855 void
856 fdcstatus(dv, n, s)
857 struct device *dv;
858 int n;
859 char *s;
860 {
861 struct fdc_softc *fdc = (void *)dv->dv_parent;
862 char bits[64];
863
864 if (n == 0) {
865 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
866 (void) fdcresult(fdc);
867 n = 2;
868 }
869
870 printf("%s: %s", dv->dv_xname, s);
871
872 switch (n) {
873 case 0:
874 printf("\n");
875 break;
876 case 2:
877 printf(" (st0 %s cyl %d)\n",
878 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
879 bits, sizeof(bits)), fdc->sc_status[1]);
880 break;
881 case 7:
882 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
883 NE7_ST0BITS, bits, sizeof(bits)));
884 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
885 NE7_ST1BITS, bits, sizeof(bits)));
886 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
887 NE7_ST2BITS, bits, sizeof(bits)));
888 printf(" cyl %d head %d sec %d)\n",
889 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
890 break;
891 #ifdef DIAGNOSTIC
892 default:
893 printf("\nfdcstatus: weird size");
894 break;
895 #endif
896 }
897 }
898
899 void
900 fdctimeout(arg)
901 void *arg;
902 {
903 struct fdc_softc *fdc = arg;
904 struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
905 int s;
906
907 s = splbio();
908 #ifdef DEBUG
909 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
910 #endif
911 fdcstatus(&fd->sc_dev, 0, "timeout");
912
913 if (BUFQ_PEEK(&fd->sc_q) != NULL)
914 fdc->sc_state++;
915 else
916 fdc->sc_state = DEVIDLE;
917
918 (void) fdcintr(fdc);
919 splx(s);
920 }
921
922 void
923 fdcpseudointr(arg)
924 void *arg;
925 {
926 int s;
927
928 /* Just ensure it has the right spl. */
929 s = splbio();
930 (void) fdcintr(arg);
931 splx(s);
932 }
933
934 int
935 fdcintr(arg)
936 void *arg;
937 {
938 struct fdc_softc *fdc = arg;
939 #define st0 fdc->sc_status[0]
940 #define cyl fdc->sc_status[1]
941 struct fd_softc *fd;
942 struct buf *bp;
943 bus_space_tag_t iot = fdc->sc_iot;
944 bus_space_handle_t ioh = fdc->sc_ioh;
945 int read, head, sec, i, nblks;
946 struct fd_type *type;
947 struct ne7_fd_formb *finfo = NULL;
948
949 loop:
950 /* Is there a drive for the controller to do a transfer with? */
951 fd = TAILQ_FIRST(&fdc->sc_drives);
952 if (fd == NULL) {
953 fdc->sc_state = DEVIDLE;
954 return 1;
955 }
956
957 /* Is there a transfer to this drive? If not, deactivate drive. */
958 bp = BUFQ_PEEK(&fd->sc_q);
959 if (bp == NULL) {
960 fd->sc_ops = 0;
961 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
962 fd->sc_active = 0;
963 goto loop;
964 }
965
966 if (bp->b_flags & B_FORMAT)
967 finfo = (struct ne7_fd_formb *)bp->b_data;
968
969 switch (fdc->sc_state) {
970 case DEVIDLE:
971 fdc->sc_errors = 0;
972 fd->sc_skip = 0;
973 fd->sc_bcount = bp->b_bcount;
974 fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
975 callout_stop(&fd->sc_motoroff_ch);
976 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
977 fdc->sc_state = MOTORWAIT;
978 return 1;
979 }
980 if ((fd->sc_flags & FD_MOTOR) == 0) {
981 /* Turn on the motor, being careful about pairing. */
982 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
983 if (ofd && ofd->sc_flags & FD_MOTOR) {
984 callout_stop(&ofd->sc_motoroff_ch);
985 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
986 }
987 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
988 fd_set_motor(fdc, 0);
989 fdc->sc_state = MOTORWAIT;
990 /* Allow .25s for motor to stabilize. */
991 callout_reset(&fd->sc_motoron_ch, hz / 4,
992 fd_motor_on, fd);
993 return 1;
994 }
995 /* Make sure the right drive is selected. */
996 fd_set_motor(fdc, 0);
997
998 /* fall through */
999 case DOSEEK:
1000 doseek:
1001 if (fd->sc_cylin == bp->b_cylinder)
1002 goto doio;
1003
1004 out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
1005 out_fdc(iot, ioh, fd->sc_type->steprate);
1006 out_fdc(iot, ioh, 6); /* XXX head load time == 6ms */
1007
1008 out_fdc(iot, ioh, NE7CMD_SEEK); /* seek function */
1009 out_fdc(iot, ioh, fd->sc_drive); /* drive number */
1010 out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
1011
1012 fd->sc_cylin = -1;
1013 fdc->sc_state = SEEKWAIT;
1014
1015 fd->sc_dk.dk_seek++;
1016 disk_busy(&fd->sc_dk);
1017
1018 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1019 return 1;
1020
1021 case DOIO:
1022 doio:
1023 type = fd->sc_type;
1024 if (finfo)
1025 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1026 (char *)finfo;
1027 sec = fd->sc_blkno % type->seccyl;
1028 nblks = type->seccyl - sec;
1029 nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
1030 nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
1031 fd->sc_nblks = nblks;
1032 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1033 head = sec / type->sectrac;
1034 sec -= head * type->sectrac;
1035 #ifdef DIAGNOSTIC
1036 {
1037 int block;
1038 block = (fd->sc_cylin * type->heads + head)
1039 * type->sectrac + sec;
1040 if (block != fd->sc_blkno) {
1041 printf("fdcintr: block %d != blkno "
1042 "%" PRId64 "\n", block, fd->sc_blkno);
1043 #ifdef DDB
1044 Debugger();
1045 #endif
1046 }
1047 }
1048 #endif
1049 read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
1050 isa_dmastart(fdc->sc_ic, fdc->sc_drq,
1051 bp->b_data + fd->sc_skip, fd->sc_nbytes,
1052 NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
1053 bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
1054 #ifdef FD_DEBUG
1055 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1056 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1057 head, sec, nblks);
1058 #endif
1059 if (finfo) {
1060 /* formatting */
1061 if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
1062 fdc->sc_errors = 4;
1063 fdcretry(fdc);
1064 goto loop;
1065 }
1066 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1067 out_fdc(iot, ioh, finfo->fd_formb_secshift);
1068 out_fdc(iot, ioh, finfo->fd_formb_nsecs);
1069 out_fdc(iot, ioh, finfo->fd_formb_gaplen);
1070 out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
1071 } else {
1072 if (read)
1073 out_fdc(iot, ioh, NE7CMD_READ); /* READ */
1074 else
1075 out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
1076 out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1077 out_fdc(iot, ioh, fd->sc_cylin); /* track */
1078 out_fdc(iot, ioh, head);
1079 out_fdc(iot, ioh, sec + 1); /* sector +1 */
1080 out_fdc(iot, ioh, type->secsize);/* sector size */
1081 out_fdc(iot, ioh, type->sectrac);/* sectors/track */
1082 out_fdc(iot, ioh, type->gap1); /* gap1 size */
1083 out_fdc(iot, ioh, type->datalen);/* data length */
1084 }
1085 fdc->sc_state = IOCOMPLETE;
1086
1087 disk_busy(&fd->sc_dk);
1088
1089 /* allow 2 seconds for operation */
1090 callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1091 return 1; /* will return later */
1092
1093 case SEEKWAIT:
1094 callout_stop(&fdc->sc_timo_ch);
1095 fdc->sc_state = SEEKCOMPLETE;
1096 /* allow 1/50 second for heads to settle */
1097 callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
1098 return 1;
1099
1100 case SEEKCOMPLETE:
1101 /* no data on seek */
1102 disk_unbusy(&fd->sc_dk, 0, 0);
1103
1104 /* Make sure seek really happened. */
1105 out_fdc(iot, ioh, NE7CMD_SENSEI);
1106 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
1107 cyl != bp->b_cylinder * fd->sc_type->step) {
1108 #ifdef FD_DEBUG
1109 fdcstatus(&fd->sc_dev, 2, "seek failed");
1110 #endif
1111 fdcretry(fdc);
1112 goto loop;
1113 }
1114 fd->sc_cylin = bp->b_cylinder;
1115 goto doio;
1116
1117 case IOTIMEDOUT:
1118 isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1119 case SEEKTIMEDOUT:
1120 case RECALTIMEDOUT:
1121 case RESETTIMEDOUT:
1122 fdcretry(fdc);
1123 goto loop;
1124
1125 case IOCOMPLETE: /* IO DONE, post-analyze */
1126 callout_stop(&fdc->sc_timo_ch);
1127
1128 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1129 (bp->b_flags & B_READ));
1130
1131 if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
1132 isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1133 #ifdef FD_DEBUG
1134 fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
1135 "read failed" : "write failed");
1136 printf("blkno %d nblks %d\n",
1137 fd->sc_blkno, fd->sc_nblks);
1138 #endif
1139 fdcretry(fdc);
1140 goto loop;
1141 }
1142 isa_dmadone(fdc->sc_ic, fdc->sc_drq);
1143 if (fdc->sc_errors) {
1144 diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
1145 fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
1146 printf("\n");
1147 fdc->sc_errors = 0;
1148 }
1149 fd->sc_blkno += fd->sc_nblks;
1150 fd->sc_skip += fd->sc_nbytes;
1151 fd->sc_bcount -= fd->sc_nbytes;
1152 if (!finfo && fd->sc_bcount > 0) {
1153 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1154 goto doseek;
1155 }
1156 fdfinish(fd, bp);
1157 goto loop;
1158
1159 case DORESET:
1160 /* try a reset, keep motor on */
1161 fd_set_motor(fdc, 1);
1162 delay(100);
1163 fd_set_motor(fdc, 0);
1164 fdc->sc_state = RESETCOMPLETE;
1165 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1166 return 1; /* will return later */
1167
1168 case RESETCOMPLETE:
1169 callout_stop(&fdc->sc_timo_ch);
1170 /* clear the controller output buffer */
1171 for (i = 0; i < 4; i++) {
1172 out_fdc(iot, ioh, NE7CMD_SENSEI);
1173 (void) fdcresult(fdc);
1174 }
1175
1176 /* fall through */
1177 case DORECAL:
1178 out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1179 out_fdc(iot, ioh, fd->sc_drive);
1180 fdc->sc_state = RECALWAIT;
1181 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1182 return 1; /* will return later */
1183
1184 case RECALWAIT:
1185 callout_stop(&fdc->sc_timo_ch);
1186 fdc->sc_state = RECALCOMPLETE;
1187 /* allow 1/30 second for heads to settle */
1188 callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
1189 return 1; /* will return later */
1190
1191 case RECALCOMPLETE:
1192 out_fdc(iot, ioh, NE7CMD_SENSEI);
1193 if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1194 #ifdef FD_DEBUG
1195 fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
1196 #endif
1197 fdcretry(fdc);
1198 goto loop;
1199 }
1200 fd->sc_cylin = 0;
1201 goto doseek;
1202
1203 case MOTORWAIT:
1204 if (fd->sc_flags & FD_MOTOR_WAIT)
1205 return 1; /* time's not up yet */
1206 goto doseek;
1207
1208 default:
1209 fdcstatus(&fd->sc_dev, 0, "stray interrupt");
1210 return 1;
1211 }
1212 #ifdef DIAGNOSTIC
1213 panic("fdcintr: impossible");
1214 #endif
1215 #undef st0
1216 #undef cyl
1217 }
1218
1219 void
1220 fdcretry(fdc)
1221 struct fdc_softc *fdc;
1222 {
1223 char bits[64];
1224 struct fd_softc *fd;
1225 struct buf *bp;
1226
1227 fd = TAILQ_FIRST(&fdc->sc_drives);
1228 bp = BUFQ_PEEK(&fd->sc_q);
1229
1230 if (fd->sc_opts & FDOPT_NORETRY)
1231 goto fail;
1232 switch (fdc->sc_errors) {
1233 case 0:
1234 /* try again */
1235 fdc->sc_state = DOSEEK;
1236 break;
1237
1238 case 1: case 2: case 3:
1239 /* didn't work; try recalibrating */
1240 fdc->sc_state = DORECAL;
1241 break;
1242
1243 case 4:
1244 /* still no go; reset the bastard */
1245 fdc->sc_state = DORESET;
1246 break;
1247
1248 default:
1249 fail:
1250 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1251 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1252 fd->sc_skip / FDC_BSIZE,
1253 (struct disklabel *)NULL);
1254
1255 printf(" (st0 %s",
1256 bitmask_snprintf(fdc->sc_status[0],
1257 NE7_ST0BITS, bits,
1258 sizeof(bits)));
1259 printf(" st1 %s",
1260 bitmask_snprintf(fdc->sc_status[1],
1261 NE7_ST1BITS, bits,
1262 sizeof(bits)));
1263 printf(" st2 %s",
1264 bitmask_snprintf(fdc->sc_status[2],
1265 NE7_ST2BITS, bits,
1266 sizeof(bits)));
1267 printf(" cyl %d head %d sec %d)\n",
1268 fdc->sc_status[3],
1269 fdc->sc_status[4],
1270 fdc->sc_status[5]);
1271 }
1272
1273 bp->b_flags |= B_ERROR;
1274 bp->b_error = EIO;
1275 fdfinish(fd, bp);
1276 }
1277 fdc->sc_errors++;
1278 }
1279
1280 int
1281 fdioctl(dev, cmd, addr, flag, p)
1282 dev_t dev;
1283 u_long cmd;
1284 caddr_t addr;
1285 int flag;
1286 struct proc *p;
1287 {
1288 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1289 struct fdformat_parms *form_parms;
1290 struct fdformat_cmd *form_cmd;
1291 struct ne7_fd_formb *fd_formb;
1292 struct disklabel buffer;
1293 int error;
1294 unsigned int scratch;
1295 int il[FD_MAX_NSEC + 1];
1296 register int i, j;
1297 #ifdef __HAVE_OLD_DISKLABEL
1298 struct disklabel newlabel;
1299 #endif
1300
1301 switch (cmd) {
1302 case DIOCGDINFO:
1303 #ifdef __HAVE_OLD_DISKLABEL
1304 case ODIOCGDINFO:
1305 #endif
1306 memset(&buffer, 0, sizeof(buffer));
1307
1308 buffer.d_secpercyl = fd->sc_type->seccyl;
1309 buffer.d_type = DTYPE_FLOPPY;
1310 buffer.d_secsize = FDC_BSIZE;
1311
1312 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1313 return EINVAL;
1314
1315 #ifdef __HAVE_OLD_DISKLABEL
1316 if (cmd == ODIOCGDINFO) {
1317 if (buffer.d_npartitions > OLDMAXPARTITIONS)
1318 return ENOTTY;
1319 memcpy(addr, &buffer, sizeof (struct olddisklabel));
1320 } else
1321 #endif
1322 *(struct disklabel *)addr = buffer;
1323 return 0;
1324
1325 case DIOCWLABEL:
1326 if ((flag & FWRITE) == 0)
1327 return EBADF;
1328 /* XXX do something */
1329 return 0;
1330
1331 case DIOCWDINFO:
1332 #ifdef __HAVE_OLD_DISKLABEL
1333 case ODIOCWDINFO:
1334 #endif
1335 {
1336 struct disklabel *lp;
1337
1338 if ((flag & FWRITE) == 0)
1339 return EBADF;
1340 #ifdef __HAVE_OLD_DISKLABEL
1341 if (cmd == ODIOCWDINFO) {
1342 memset(&newlabel, 0, sizeof newlabel);
1343 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1344 lp = &newlabel;
1345 } else
1346 #endif
1347 lp = (struct disklabel *)addr;
1348
1349 error = setdisklabel(&buffer, lp, 0, NULL);
1350 if (error)
1351 return error;
1352
1353 error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1354 return error;
1355 }
1356
1357 case FDIOCGETFORMAT:
1358 form_parms = (struct fdformat_parms *)addr;
1359 form_parms->fdformat_version = FDFORMAT_VERSION;
1360 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1361 form_parms->ncyl = fd->sc_type->cyls;
1362 form_parms->nspt = fd->sc_type->sectrac;
1363 form_parms->ntrk = fd->sc_type->heads;
1364 form_parms->stepspercyl = fd->sc_type->step;
1365 form_parms->gaplen = fd->sc_type->gap2;
1366 form_parms->fillbyte = fd->sc_type->fillbyte;
1367 form_parms->interleave = fd->sc_type->interleave;
1368 switch (fd->sc_type->rate) {
1369 case FDC_500KBPS:
1370 form_parms->xfer_rate = 500 * 1024;
1371 break;
1372 case FDC_300KBPS:
1373 form_parms->xfer_rate = 300 * 1024;
1374 break;
1375 case FDC_250KBPS:
1376 form_parms->xfer_rate = 250 * 1024;
1377 break;
1378 default:
1379 return EINVAL;
1380 }
1381 return 0;
1382
1383 case FDIOCSETFORMAT:
1384 if((flag & FWRITE) == 0)
1385 return EBADF; /* must be opened for writing */
1386 form_parms = (struct fdformat_parms *)addr;
1387 if (form_parms->fdformat_version != FDFORMAT_VERSION)
1388 return EINVAL; /* wrong version of formatting prog */
1389
1390 scratch = form_parms->nbps >> 7;
1391 if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1392 scratch & ~(1 << (ffs(scratch)-1)))
1393 /* not a power-of-two multiple of 128 */
1394 return EINVAL;
1395
1396 switch (form_parms->xfer_rate) {
1397 case 500 * 1024:
1398 fd->sc_type->rate = FDC_500KBPS;
1399 break;
1400 case 300 * 1024:
1401 fd->sc_type->rate = FDC_300KBPS;
1402 break;
1403 case 250 * 1024:
1404 fd->sc_type->rate = FDC_250KBPS;
1405 break;
1406 default:
1407 return EINVAL;
1408 }
1409
1410 if (form_parms->nspt > FD_MAX_NSEC ||
1411 form_parms->fillbyte > 0xff ||
1412 form_parms->interleave > 0xff)
1413 return EINVAL;
1414 fd->sc_type->sectrac = form_parms->nspt;
1415 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1416 return EINVAL;
1417 fd->sc_type->heads = form_parms->ntrk;
1418 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1419 fd->sc_type->secsize = ffs(scratch)-1;
1420 fd->sc_type->gap2 = form_parms->gaplen;
1421 fd->sc_type->cyls = form_parms->ncyl;
1422 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1423 form_parms->nbps / DEV_BSIZE;
1424 fd->sc_type->step = form_parms->stepspercyl;
1425 fd->sc_type->fillbyte = form_parms->fillbyte;
1426 fd->sc_type->interleave = form_parms->interleave;
1427 return 0;
1428
1429 case FDIOCFORMAT_TRACK:
1430 if((flag & FWRITE) == 0)
1431 return EBADF; /* must be opened for writing */
1432 form_cmd = (struct fdformat_cmd *)addr;
1433 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1434 return EINVAL; /* wrong version of formatting prog */
1435
1436 if (form_cmd->head >= fd->sc_type->heads ||
1437 form_cmd->cylinder >= fd->sc_type->cyls) {
1438 return EINVAL;
1439 }
1440
1441 fd_formb = malloc(sizeof(struct ne7_fd_formb),
1442 M_TEMP, M_NOWAIT);
1443 if (fd_formb == 0)
1444 return ENOMEM;
1445
1446 fd_formb->head = form_cmd->head;
1447 fd_formb->cyl = form_cmd->cylinder;
1448 fd_formb->transfer_rate = fd->sc_type->rate;
1449 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1450 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1451 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1452 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1453
1454 memset(il, 0, sizeof il);
1455 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1456 while (il[(j%fd_formb->fd_formb_nsecs)+1])
1457 j++;
1458 il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1459 j += fd->sc_type->interleave;
1460 }
1461 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1462 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1463 fd_formb->fd_formb_headno(i) = form_cmd->head;
1464 fd_formb->fd_formb_secno(i) = il[i+1];
1465 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1466 }
1467
1468 error = fdformat(dev, fd_formb, p);
1469 free(fd_formb, M_TEMP);
1470 return error;
1471
1472 case FDIOCGETOPTS: /* get drive options */
1473 *(int *)addr = fd->sc_opts;
1474 return 0;
1475
1476 case FDIOCSETOPTS: /* set drive options */
1477 fd->sc_opts = *(int *)addr;
1478 return 0;
1479
1480 default:
1481 return ENOTTY;
1482 }
1483
1484 #ifdef DIAGNOSTIC
1485 panic("fdioctl: impossible");
1486 #endif
1487 }
1488
1489 int
1490 fdformat(dev, finfo, p)
1491 dev_t dev;
1492 struct ne7_fd_formb *finfo;
1493 struct proc *p;
1494 {
1495 int rv = 0, s;
1496 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1497 struct fd_type *type = fd->sc_type;
1498 struct buf *bp;
1499
1500 /* set up a buffer header for fdstrategy() */
1501 s = splbio();
1502 bp = (struct buf *)pool_get(&bufpool, PR_NOWAIT);
1503 splx(s);
1504 if (bp == NULL)
1505 return ENOBUFS;
1506
1507 memset((void *)bp, 0, sizeof(struct buf));
1508 BUF_INIT(bp);
1509 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1510 bp->b_proc = p;
1511 bp->b_dev = dev;
1512
1513 /*
1514 * calculate a fake blkno, so fdstrategy() would initiate a
1515 * seek to the requested cylinder
1516 */
1517 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1518 + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1519
1520 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1521 bp->b_data = (caddr_t)finfo;
1522
1523 #ifdef DEBUG
1524 printf("fdformat: blkno %" PRIx64 " count %lx\n",
1525 bp->b_blkno, bp->b_bcount);
1526 #endif
1527
1528 /* now do the format */
1529 fdstrategy(bp);
1530
1531 /* ...and wait for it to complete */
1532 rv = biowait(bp);
1533 s = splbio();
1534 pool_put(&bufpool, bp);
1535 splx(s);
1536 return rv;
1537 }
1538
1539 /*
1540 * Mountroot hook: prompt the user to enter the root file system
1541 * floppy.
1542 */
1543 void
1544 fd_mountroot_hook(dev)
1545 struct device *dev;
1546 {
1547 int c;
1548
1549 printf("Insert filesystem floppy and press return.");
1550 cnpollc(1);
1551 for (;;) {
1552 c = cngetc();
1553 if ((c == '\r') || (c == '\n')) {
1554 printf("\n");
1555 break;
1556 }
1557 }
1558 cnpollc(0);
1559 }
1560