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