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