fd.c revision 1.73.2.1 1 /* $NetBSD: fd.c,v 1.73.2.1 2007/05/13 17:36:26 ad 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.73.2.1 2007/05/13 17:36:26 ad 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 <prop/proplib.h>
129
130 #include <uvm/uvm_extern.h>
131
132 #include <dev/cons.h>
133
134 #include <machine/cpu.h>
135 #include <machine/bus.h>
136
137 #include "locators.h"
138
139 #if defined(atari)
140 /*
141 * On the atari, it is configured as fdcisa
142 */
143 #define FDCCF_DRIVE FDCISACF_DRIVE
144 #define FDCCF_DRIVE_DEFAULT FDCISACF_DRIVE_DEFAULT
145
146 #define fd_cd fdisa_cd
147 #endif /* atari */
148
149 #include <machine/intr.h>
150
151 #include <dev/isa/isavar.h>
152 #include <dev/isa/isadmavar.h>
153
154 #include <dev/isa/fdreg.h>
155 #include <dev/isa/fdcvar.h>
156
157 #if defined(i386)
158
159 #include <dev/ic/mc146818reg.h> /* for NVRAM access */
160 #include <i386/isa/nvram.h>
161
162 #include "mca.h"
163 #if NMCA > 0
164 #include <machine/mca_machdep.h> /* for MCA_system */
165 #endif
166
167 #endif /* i386 */
168
169 #include <dev/isa/fdvar.h>
170
171 #define FDUNIT(dev) (minor(dev) / 8)
172 #define FDTYPE(dev) (minor(dev) % 8)
173
174 /* (mis)use device use flag to identify format operation */
175 #define B_FORMAT B_DEVPRIVATE
176
177 /* controller driver configuration */
178 int fdprint(void *, const char *);
179
180 #if NMCA > 0
181 /* MCA - specific entries */
182 const struct fd_type mca_fd_types[] = {
183 { 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 */
184 { 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 */
185 };
186 #endif /* NMCA > 0 */
187
188 /* The order of entries in the following table is important -- BEWARE! */
189
190 #if defined(atari)
191 const struct fd_type fd_types[] = {
192 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
193 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette */
194 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
195 };
196 #else
197 const struct fd_type fd_types[] = {
198 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
199 { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB" }, /* 1.2 MB AT-diskettes */
200 { 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
201 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
202 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5 inch 720kB diskette */
203 { 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x" }, /* 720kB in 1.2MB drive */
204 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */
205 };
206 #endif /* defined(atari) */
207
208 void fdcfinishattach(struct device *);
209 int fdprobe(struct device *, struct cfdata *, void *);
210 void fdattach(struct device *, struct device *, void *);
211
212 extern struct cfdriver fd_cd;
213
214 #ifdef atari
215 CFATTACH_DECL(fdisa, sizeof(struct fd_softc),
216 fdprobe, fdattach, NULL, NULL);
217 #else
218 CFATTACH_DECL(fd, sizeof(struct fd_softc),
219 fdprobe, fdattach, NULL, NULL);
220 #endif
221
222 dev_type_open(fdopen);
223 dev_type_close(fdclose);
224 dev_type_read(fdread);
225 dev_type_write(fdwrite);
226 dev_type_ioctl(fdioctl);
227 dev_type_strategy(fdstrategy);
228
229 const struct bdevsw fd_bdevsw = {
230 fdopen, fdclose, fdstrategy, fdioctl, nodump, nosize, D_DISK
231 };
232
233 const struct cdevsw fd_cdevsw = {
234 fdopen, fdclose, fdread, fdwrite, fdioctl,
235 nostop, notty, nopoll, nommap, nokqfilter, D_DISK
236 };
237
238 void fdgetdisklabel(struct fd_softc *);
239 int fd_get_parms(struct fd_softc *);
240 void fdstart(struct fd_softc *);
241
242 struct dkdriver fddkdriver = { fdstrategy, NULL };
243
244 #if defined(i386)
245 const struct fd_type *fd_nvtotype(char *, int, int);
246 #endif /* i386 */
247 void fd_set_motor(struct fdc_softc *fdc, int reset);
248 void fd_motor_off(void *arg);
249 void fd_motor_on(void *arg);
250 int fdcresult(struct fdc_softc *fdc);
251 void fdcstart(struct fdc_softc *fdc);
252 void fdcstatus(struct device *dv, int n, const char *s);
253 void fdctimeout(void *arg);
254 void fdcpseudointr(void *arg);
255 void fdcretry(struct fdc_softc *fdc);
256 void fdfinish(struct fd_softc *fd, struct buf *bp, int);
257 inline const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
258 int fdformat(dev_t, struct ne7_fd_formb *, struct lwp *);
259 static void fd_set_properties(struct fd_softc *fd);
260
261 void fd_mountroot_hook(struct device *);
262
263 /*
264 * Arguments passed between fdcattach and fdprobe.
265 */
266 struct fdc_attach_args {
267 int fa_drive;
268 const struct fd_type *fa_deftype;
269 };
270
271 /*
272 * Print the location of a disk drive (called just before attaching the
273 * the drive). If `fdc' is not NULL, the drive was found but was not
274 * in the system config file; print the drive name as well.
275 * Return QUIET (config_find ignores this if the device was configured) to
276 * avoid printing `fdN not configured' messages.
277 */
278 int
279 fdprint(aux, fdc)
280 void *aux;
281 const char *fdc;
282 {
283 register struct fdc_attach_args *fa = aux;
284
285 if (!fdc)
286 aprint_normal(" drive %d", fa->fa_drive);
287 return QUIET;
288 }
289
290 void
291 fdcattach(fdc)
292 struct fdc_softc *fdc;
293 {
294 callout_init(&fdc->sc_timo_ch);
295 callout_init(&fdc->sc_intr_ch);
296
297 fdc->sc_state = DEVIDLE;
298 TAILQ_INIT(&fdc->sc_drives);
299
300 fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
301
302 if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) {
303 printf("%s: can't reserve drq %d\n",
304 fdc->sc_dev.dv_xname, fdc->sc_drq);
305 return;
306 }
307
308 if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
309 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
310 printf("%s: can't set up ISA DMA map\n",
311 fdc->sc_dev.dv_xname);
312 return;
313 }
314
315 config_interrupts(&fdc->sc_dev, fdcfinishattach);
316 }
317
318 void
319 fdcfinishattach(self)
320 struct device *self;
321 {
322 struct fdc_softc *fdc = (void *)self;
323 bus_space_tag_t iot = fdc->sc_iot;
324 bus_space_handle_t ioh = fdc->sc_ioh;
325 struct fdc_attach_args fa;
326 #if defined(i386)
327 int type;
328 #endif
329
330 /*
331 * Reset the controller to get it into a known state. Not all
332 * probes necessarily need do this to discover the controller up
333 * front, so don't assume anything.
334 */
335
336 bus_space_write_1(iot, ioh, fdout, 0);
337 delay(100);
338 bus_space_write_1(iot, ioh, fdout, FDO_FRST);
339
340 /* see if it can handle a command */
341 if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
342 printf ("%s: can't reset controller\n", fdc->sc_dev.dv_xname);
343 return;
344 }
345 out_fdc(iot, ioh, 0xdf);
346 out_fdc(iot, ioh, 2);
347
348 #if defined(i386)
349 /*
350 * The NVRAM info only tells us about the first two disks on the
351 * `primary' floppy controller.
352 */
353 /* XXX device_unit() abuse */
354 if (device_unit(&fdc->sc_dev) == 0)
355 type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
356 else
357 type = -1;
358 #endif /* i386 */
359
360 /* physical limit: four drives per controller. */
361 fdc->sc_state = PROBING;
362 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
363 if (fdc->sc_known) {
364 if (fdc->sc_present & (1 << fa.fa_drive)) {
365 fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive];
366 config_found(&fdc->sc_dev, (void *)&fa,
367 fdprint);
368 }
369 } else {
370 #if defined(i386)
371 if (type >= 0 && fa.fa_drive < 2)
372 fa.fa_deftype = fd_nvtotype(fdc->sc_dev.dv_xname,
373 type, fa.fa_drive);
374 else
375 fa.fa_deftype = NULL; /* unknown */
376 #elif defined(atari)
377 /*
378 * Atari has a different ordening, defaults to 1.44
379 */
380 fa.fa_deftype = &fd_types[2];
381 #else
382 /*
383 * Default to 1.44MB on Alpha and BeBox. How do we tell
384 * on these platforms?
385 */
386 fa.fa_deftype = &fd_types[0];
387 #endif /* i386 */
388 (void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
389 }
390 }
391 fdc->sc_state = DEVIDLE;
392 }
393
394 int
395 fdprobe(parent, match, aux)
396 struct device *parent;
397 struct cfdata *match;
398 void *aux;
399 {
400 struct fdc_softc *fdc = (void *)parent;
401 struct cfdata *cf = match;
402 struct fdc_attach_args *fa = aux;
403 int drive = fa->fa_drive;
404 bus_space_tag_t iot = fdc->sc_iot;
405 bus_space_handle_t ioh = fdc->sc_ioh;
406 int n;
407 int s;
408
409 if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
410 cf->cf_loc[FDCCF_DRIVE] != drive)
411 return 0;
412 /*
413 * XXX
414 * This is to work around some odd interactions between this driver
415 * and SMC Ethernet cards.
416 */
417 if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
418 return 0;
419
420 /* Use PNP information if available */
421 if (fdc->sc_known)
422 return 1;
423
424 s = splbio();
425 /* toss any interrupt status */
426 for (n = 0; n < 4; n++) {
427 out_fdc(iot, ioh, NE7CMD_SENSEI);
428 (void) fdcresult(fdc);
429 }
430 /* select drive and turn on motor */
431 bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
432 /* wait for motor to spin up */
433 (void) tsleep(fdc, PWAIT, "fdprobe1", hz / 4);
434 out_fdc(iot, ioh, NE7CMD_RECAL);
435 out_fdc(iot, ioh, drive);
436 /* wait for recalibrate, up to 2s */
437 if (tsleep(fdc, PWAIT, "fdprobe2", 2 * hz) != EWOULDBLOCK) {
438 #ifdef FD_DEBUG
439 /* XXX */
440 printf("fdprobe: got intr\n");
441 #endif
442 }
443 out_fdc(iot, ioh, NE7CMD_SENSEI);
444 n = fdcresult(fdc);
445 #ifdef FD_DEBUG
446 {
447 int i;
448 printf("fdprobe: status");
449 for (i = 0; i < n; i++)
450 printf(" %x", fdc->sc_status[i]);
451 printf("\n");
452 }
453 #endif
454 /* turn off motor */
455 bus_space_write_1(iot, ioh, fdout, FDO_FRST);
456 splx(s);
457
458 #if defined(bebox) /* XXX What is this about? --thorpej (at) NetBSD.org */
459 if (n != 2 || (fdc->sc_status[1] != 0))
460 return 0;
461 #else
462 if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
463 return 0;
464 #endif /* bebox */
465
466 return 1;
467 }
468
469 /*
470 * Controller is working, and drive responded. Attach it.
471 */
472 void
473 fdattach(parent, self, aux)
474 struct device *parent, *self;
475 void *aux;
476 {
477 struct fdc_softc *fdc = (void *)parent;
478 struct fd_softc *fd = (void *)self;
479 struct fdc_attach_args *fa = aux;
480 const struct fd_type *type = fa->fa_deftype;
481 int drive = fa->fa_drive;
482
483 callout_init(&fd->sc_motoron_ch);
484 callout_init(&fd->sc_motoroff_ch);
485
486 /* XXX Allow `flags' to override device type? */
487
488 if (type)
489 printf(": %s, %d cyl, %d head, %d sec\n", type->name,
490 type->cyls, type->heads, type->sectrac);
491 else
492 printf(": density unknown\n");
493
494 bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
495 fd->sc_cylin = -1;
496 fd->sc_drive = drive;
497 fd->sc_deftype = type;
498 fdc->sc_fd[drive] = fd;
499
500 /*
501 * Initialize and attach the disk structure.
502 */
503 fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
504 fd->sc_dk.dk_driver = &fddkdriver;
505 disk_attach(&fd->sc_dk);
506
507 /*
508 * Establish a mountroot hook.
509 */
510 mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
511
512 /* Needed to power off if the motor is on when we halt. */
513 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
514
515 #if NRND > 0
516 rnd_attach_source(&fd->rnd_source, fd->sc_dev.dv_xname,
517 RND_TYPE_DISK, 0);
518 #endif
519
520 fd_set_properties(fd);
521 }
522
523 #if defined(i386)
524 /*
525 * Translate nvram type into internal data structure. Return NULL for
526 * none/unknown/unusable.
527 */
528 const struct fd_type *
529 fd_nvtotype(fdc, nvraminfo, drive)
530 char *fdc;
531 int nvraminfo, drive;
532 {
533 int type;
534
535 type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
536 switch (type) {
537 case NVRAM_DISKETTE_NONE:
538 return NULL;
539 case NVRAM_DISKETTE_12M:
540 return &fd_types[1];
541 case NVRAM_DISKETTE_TYPE5:
542 case NVRAM_DISKETTE_TYPE6:
543 /* XXX We really ought to handle 2.88MB format. */
544 case NVRAM_DISKETTE_144M:
545 #if NMCA > 0
546 if (MCA_system)
547 return &mca_fd_types[0];
548 else
549 #endif /* NMCA > 0 */
550 return &fd_types[0];
551 case NVRAM_DISKETTE_360K:
552 return &fd_types[3];
553 case NVRAM_DISKETTE_720K:
554 #if NMCA > 0
555 if (MCA_system)
556 return &mca_fd_types[1];
557 else
558 #endif /* NMCA > 0 */
559 return &fd_types[4];
560 default:
561 printf("%s: drive %d: unknown device type 0x%x\n",
562 fdc, drive, type);
563 return NULL;
564 }
565 }
566 #endif /* i386 */
567
568 inline const struct fd_type *
569 fd_dev_to_type(fd, dev)
570 struct fd_softc *fd;
571 dev_t dev;
572 {
573 u_int type = FDTYPE(dev);
574
575 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
576 return NULL;
577 return type ? &fd_types[type - 1] : fd->sc_deftype;
578 }
579
580 void
581 fdstrategy(bp)
582 register struct buf *bp; /* IO operation to perform */
583 {
584 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(bp->b_dev));
585 int sz, s, error = 0;
586
587 /* Valid unit, controller, and request? */
588 if (bp->b_blkno < 0 ||
589 ((bp->b_bcount % FDC_BSIZE) != 0 &&
590 (bp->b_flags & B_FORMAT) == 0)) {
591 error = EINVAL;
592 goto done;
593 }
594
595 /* If it's a null transfer, return immediately. */
596 if (bp->b_bcount == 0)
597 goto done;
598
599 sz = howmany(bp->b_bcount, FDC_BSIZE);
600
601 if (bp->b_blkno + sz > fd->sc_type->size) {
602 sz = fd->sc_type->size - bp->b_blkno;
603 if (sz == 0) {
604 /* If exactly at end of disk, return EOF. */
605 goto done;
606 }
607 if (sz < 0) {
608 /* If past end of disk, return EINVAL. */
609 error = EINVAL;
610 goto done;
611 }
612 /* Otherwise, truncate request. */
613 bp->b_bcount = sz << DEV_BSHIFT;
614 }
615
616 bp->b_rawblkno = bp->b_blkno;
617 bp->b_cylinder =
618 bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
619
620 #ifdef FD_DEBUG
621 printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d "
622 "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount,
623 (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz);
624 #endif
625
626 /* Queue transfer on drive, activate drive and controller if idle. */
627 s = splbio();
628 BUFQ_PUT(fd->sc_q, bp);
629 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
630 if (fd->sc_active == 0)
631 fdstart(fd);
632 #ifdef DIAGNOSTIC
633 else {
634 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
635 if (fdc->sc_state == DEVIDLE) {
636 printf("fdstrategy: controller inactive\n");
637 fdcstart(fdc);
638 }
639 }
640 #endif
641 splx(s);
642 return;
643
644 done:
645 /* Toss transfer; we're done early. */
646 biodone(bp, error, 0);
647 }
648
649 void
650 fdstart(fd)
651 struct fd_softc *fd;
652 {
653 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
654 int active = !TAILQ_EMPTY(&fdc->sc_drives);
655
656 /* Link into controller queue. */
657 fd->sc_active = 1;
658 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
659
660 /* If controller not already active, start it. */
661 if (!active)
662 fdcstart(fdc);
663 }
664
665 void
666 fdfinish(fd, bp, error)
667 struct fd_softc *fd;
668 struct buf *bp;
669 int error;
670 {
671 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
672
673 /*
674 * Move this drive to the end of the queue to give others a `fair'
675 * chance. We only force a switch if N operations are completed while
676 * another drive is waiting to be serviced, since there is a long motor
677 * startup delay whenever we switch.
678 */
679 (void)BUFQ_GET(fd->sc_q);
680 if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
681 fd->sc_ops = 0;
682 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
683 if (BUFQ_PEEK(fd->sc_q) != NULL)
684 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
685 else
686 fd->sc_active = 0;
687 }
688 fd->sc_skip = 0;
689
690 #if NRND > 0
691 rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
692 #endif
693
694 biodone(bp, error, fd->sc_bcount);
695 /* turn off motor 5s from now */
696 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
697 fdc->sc_state = DEVIDLE;
698 }
699
700 int
701 fdread(dev_t dev, struct uio *uio, int flags)
702 {
703
704 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
705 }
706
707 int
708 fdwrite(dev_t dev, struct uio *uio, int flags)
709 {
710
711 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
712 }
713
714 void
715 fd_set_motor(fdc, reset)
716 struct fdc_softc *fdc;
717 int reset;
718 {
719 struct fd_softc *fd;
720 u_char status;
721 int n;
722
723 if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
724 status = fd->sc_drive;
725 else
726 status = 0;
727 if (!reset)
728 status |= FDO_FRST | FDO_FDMAEN;
729 for (n = 0; n < 4; n++)
730 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
731 status |= FDO_MOEN(n);
732 bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
733 }
734
735 void
736 fd_motor_off(arg)
737 void *arg;
738 {
739 struct fd_softc *fd = arg;
740 int s;
741
742 s = splbio();
743 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
744 fd_set_motor((struct fdc_softc *)device_parent(&fd->sc_dev), 0);
745 splx(s);
746 }
747
748 void
749 fd_motor_on(arg)
750 void *arg;
751 {
752 struct fd_softc *fd = arg;
753 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dev);
754 int s;
755
756 s = splbio();
757 fd->sc_flags &= ~FD_MOTOR_WAIT;
758 if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&(fdc->sc_state == MOTORWAIT))
759 (void) fdcintr(fdc);
760 splx(s);
761 }
762
763 int
764 fdcresult(fdc)
765 struct fdc_softc *fdc;
766 {
767 bus_space_tag_t iot = fdc->sc_iot;
768 bus_space_handle_t ioh = fdc->sc_ioh;
769 u_char i;
770 u_int j = 100000,
771 n = 0;
772
773 for (; j; j--) {
774 i = bus_space_read_1(iot, ioh, fdsts) &
775 (NE7_DIO | NE7_RQM | NE7_CB);
776 if (i == NE7_RQM)
777 return n;
778 if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
779 if (n >= sizeof(fdc->sc_status)) {
780 log(LOG_ERR, "fdcresult: overrun\n");
781 return -1;
782 }
783 fdc->sc_status[n++] =
784 bus_space_read_1(iot, ioh, fddata);
785 }
786 delay(10);
787 }
788 log(LOG_ERR, "fdcresult: timeout\n");
789 return -1;
790 }
791
792 int
793 out_fdc(iot, ioh, x)
794 bus_space_tag_t iot;
795 bus_space_handle_t ioh;
796 u_char x;
797 {
798 u_char i;
799 u_int j = 100000;
800
801 for (; j; j--) {
802 i = bus_space_read_1(iot, ioh, fdsts) &
803 (NE7_DIO | NE7_RQM);
804 if (i == NE7_RQM) {
805 bus_space_write_1(iot, ioh, fddata, x);
806 return 0;
807 }
808 delay(10);
809 }
810 return -1;
811 }
812
813 int
814 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
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 fd_set_properties(fd);
837
838 return 0;
839 }
840
841 int
842 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
843 {
844 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
845
846 fd->sc_flags &= ~FD_OPEN;
847 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
848 return 0;
849 }
850
851 void
852 fdcstart(fdc)
853 struct fdc_softc *fdc;
854 {
855
856 #ifdef DIAGNOSTIC
857 /* only got here if controller's drive queue was inactive; should
858 be in idle state */
859 if (fdc->sc_state != DEVIDLE) {
860 printf("fdcstart: not idle\n");
861 return;
862 }
863 #endif
864 (void) fdcintr(fdc);
865 }
866
867 void
868 fdcstatus(dv, n, s)
869 struct device *dv;
870 int n;
871 const char *s;
872 {
873 struct fdc_softc *fdc = (void *)device_parent(dv);
874 char bits[64];
875
876 if (n == 0) {
877 out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
878 (void) fdcresult(fdc);
879 n = 2;
880 }
881
882 printf("%s: %s", dv->dv_xname, s);
883
884 switch (n) {
885 case 0:
886 printf("\n");
887 break;
888 case 2:
889 printf(" (st0 %s cyl %d)\n",
890 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
891 bits, sizeof(bits)), fdc->sc_status[1]);
892 break;
893 case 7:
894 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
895 NE7_ST0BITS, bits, sizeof(bits)));
896 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
897 NE7_ST1BITS, bits, sizeof(bits)));
898 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
899 NE7_ST2BITS, bits, sizeof(bits)));
900 printf(" cyl %d head %d sec %d)\n",
901 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
902 break;
903 #ifdef DIAGNOSTIC
904 default:
905 printf("\nfdcstatus: weird size");
906 break;
907 #endif
908 }
909 }
910
911 void
912 fdctimeout(arg)
913 void *arg;
914 {
915 struct fdc_softc *fdc = arg;
916 struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
917 int s;
918
919 s = splbio();
920 #ifdef DEBUG
921 log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
922 #endif
923 fdcstatus(&fd->sc_dev, 0, "timeout");
924
925 if (BUFQ_PEEK(fd->sc_q) != NULL)
926 fdc->sc_state++;
927 else
928 fdc->sc_state = DEVIDLE;
929
930 (void) fdcintr(fdc);
931 splx(s);
932 }
933
934 void
935 fdcpseudointr(arg)
936 void *arg;
937 {
938 int s;
939
940 /* Just ensure it has the right spl. */
941 s = splbio();
942 (void) fdcintr(arg);
943 splx(s);
944 }
945
946 int
947 fdcintr(arg)
948 void *arg;
949 {
950 struct fdc_softc *fdc = arg;
951 #define st0 fdc->sc_status[0]
952 #define cyl fdc->sc_status[1]
953 struct fd_softc *fd;
954 struct buf *bp;
955 bus_space_tag_t iot = fdc->sc_iot;
956 bus_space_handle_t ioh = fdc->sc_ioh;
957 int read, head, sec, i, nblks;
958 struct fd_type *type;
959 struct ne7_fd_formb *finfo = NULL;
960
961 if (fdc->sc_state == PROBING) {
962 #ifdef DEBUG
963 printf("fdcintr: got probe interrupt\n");
964 #endif
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 iostat_seek(fd->sc_dk.dk_stats);
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 (char *)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 %llu nblks %d\n",
1157 (unsigned long long)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, 0);
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 fdfinish(fd, bp, EIO);
1294 }
1295 fdc->sc_errors++;
1296 }
1297
1298 int
1299 fdioctl(dev, cmd, addr, flag, l)
1300 dev_t dev;
1301 u_long cmd;
1302 void *addr;
1303 int flag;
1304 struct lwp *l;
1305 {
1306 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1307 struct fdformat_parms *form_parms;
1308 struct fdformat_cmd *form_cmd;
1309 struct ne7_fd_formb *fd_formb;
1310 struct disklabel buffer;
1311 int error;
1312 unsigned int scratch;
1313 int il[FD_MAX_NSEC + 1];
1314 register int i, j;
1315 #ifdef __HAVE_OLD_DISKLABEL
1316 struct disklabel newlabel;
1317 #endif
1318
1319 switch (cmd) {
1320 case DIOCGDINFO:
1321 #ifdef __HAVE_OLD_DISKLABEL
1322 case ODIOCGDINFO:
1323 #endif
1324 memset(&buffer, 0, sizeof(buffer));
1325
1326 buffer.d_secpercyl = fd->sc_type->seccyl;
1327 buffer.d_type = DTYPE_FLOPPY;
1328 buffer.d_secsize = FDC_BSIZE;
1329
1330 if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
1331 return EINVAL;
1332
1333 #ifdef __HAVE_OLD_DISKLABEL
1334 if (cmd == ODIOCGDINFO) {
1335 if (buffer.d_npartitions > OLDMAXPARTITIONS)
1336 return ENOTTY;
1337 memcpy(addr, &buffer, sizeof (struct olddisklabel));
1338 } else
1339 #endif
1340 *(struct disklabel *)addr = buffer;
1341 return 0;
1342
1343 case DIOCWLABEL:
1344 if ((flag & FWRITE) == 0)
1345 return EBADF;
1346 /* XXX do something */
1347 return 0;
1348
1349 case DIOCWDINFO:
1350 #ifdef __HAVE_OLD_DISKLABEL
1351 case ODIOCWDINFO:
1352 #endif
1353 {
1354 struct disklabel *lp;
1355
1356 if ((flag & FWRITE) == 0)
1357 return EBADF;
1358 #ifdef __HAVE_OLD_DISKLABEL
1359 if (cmd == ODIOCWDINFO) {
1360 memset(&newlabel, 0, sizeof newlabel);
1361 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1362 lp = &newlabel;
1363 } else
1364 #endif
1365 lp = (struct disklabel *)addr;
1366
1367 error = setdisklabel(&buffer, lp, 0, NULL);
1368 if (error)
1369 return error;
1370
1371 error = writedisklabel(dev, fdstrategy, &buffer, NULL);
1372 return error;
1373 }
1374
1375 case FDIOCGETFORMAT:
1376 form_parms = (struct fdformat_parms *)addr;
1377 form_parms->fdformat_version = FDFORMAT_VERSION;
1378 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1379 form_parms->ncyl = fd->sc_type->cyls;
1380 form_parms->nspt = fd->sc_type->sectrac;
1381 form_parms->ntrk = fd->sc_type->heads;
1382 form_parms->stepspercyl = fd->sc_type->step;
1383 form_parms->gaplen = fd->sc_type->gap2;
1384 form_parms->fillbyte = fd->sc_type->fillbyte;
1385 form_parms->interleave = fd->sc_type->interleave;
1386 switch (fd->sc_type->rate) {
1387 case FDC_500KBPS:
1388 form_parms->xfer_rate = 500 * 1024;
1389 break;
1390 case FDC_300KBPS:
1391 form_parms->xfer_rate = 300 * 1024;
1392 break;
1393 case FDC_250KBPS:
1394 form_parms->xfer_rate = 250 * 1024;
1395 break;
1396 default:
1397 return EINVAL;
1398 }
1399 return 0;
1400
1401 case FDIOCSETFORMAT:
1402 if((flag & FWRITE) == 0)
1403 return EBADF; /* must be opened for writing */
1404 form_parms = (struct fdformat_parms *)addr;
1405 if (form_parms->fdformat_version != FDFORMAT_VERSION)
1406 return EINVAL; /* wrong version of formatting prog */
1407
1408 scratch = form_parms->nbps >> 7;
1409 if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1410 scratch & ~(1 << (ffs(scratch)-1)))
1411 /* not a power-of-two multiple of 128 */
1412 return EINVAL;
1413
1414 switch (form_parms->xfer_rate) {
1415 case 500 * 1024:
1416 fd->sc_type->rate = FDC_500KBPS;
1417 break;
1418 case 300 * 1024:
1419 fd->sc_type->rate = FDC_300KBPS;
1420 break;
1421 case 250 * 1024:
1422 fd->sc_type->rate = FDC_250KBPS;
1423 break;
1424 default:
1425 return EINVAL;
1426 }
1427
1428 if (form_parms->nspt > FD_MAX_NSEC ||
1429 form_parms->fillbyte > 0xff ||
1430 form_parms->interleave > 0xff)
1431 return EINVAL;
1432 fd->sc_type->sectrac = form_parms->nspt;
1433 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1434 return EINVAL;
1435 fd->sc_type->heads = form_parms->ntrk;
1436 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1437 fd->sc_type->secsize = ffs(scratch)-1;
1438 fd->sc_type->gap2 = form_parms->gaplen;
1439 fd->sc_type->cyls = form_parms->ncyl;
1440 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1441 form_parms->nbps / DEV_BSIZE;
1442 fd->sc_type->step = form_parms->stepspercyl;
1443 fd->sc_type->fillbyte = form_parms->fillbyte;
1444 fd->sc_type->interleave = form_parms->interleave;
1445 return 0;
1446
1447 case FDIOCFORMAT_TRACK:
1448 if((flag & FWRITE) == 0)
1449 return EBADF; /* must be opened for writing */
1450 form_cmd = (struct fdformat_cmd *)addr;
1451 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1452 return EINVAL; /* wrong version of formatting prog */
1453
1454 if (form_cmd->head >= fd->sc_type->heads ||
1455 form_cmd->cylinder >= fd->sc_type->cyls) {
1456 return EINVAL;
1457 }
1458
1459 fd_formb = malloc(sizeof(struct ne7_fd_formb),
1460 M_TEMP, M_NOWAIT);
1461 if (fd_formb == 0)
1462 return ENOMEM;
1463
1464 fd_formb->head = form_cmd->head;
1465 fd_formb->cyl = form_cmd->cylinder;
1466 fd_formb->transfer_rate = fd->sc_type->rate;
1467 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1468 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1469 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1470 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1471
1472 memset(il, 0, sizeof il);
1473 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1474 while (il[(j%fd_formb->fd_formb_nsecs)+1])
1475 j++;
1476 il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1477 j += fd->sc_type->interleave;
1478 }
1479 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1480 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1481 fd_formb->fd_formb_headno(i) = form_cmd->head;
1482 fd_formb->fd_formb_secno(i) = il[i+1];
1483 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1484 }
1485
1486 error = fdformat(dev, fd_formb, l);
1487 free(fd_formb, M_TEMP);
1488 return error;
1489
1490 case FDIOCGETOPTS: /* get drive options */
1491 *(int *)addr = fd->sc_opts;
1492 return 0;
1493
1494 case FDIOCSETOPTS: /* set drive options */
1495 fd->sc_opts = *(int *)addr;
1496 return 0;
1497
1498 default:
1499 return ENOTTY;
1500 }
1501
1502 #ifdef DIAGNOSTIC
1503 panic("fdioctl: impossible");
1504 #endif
1505 }
1506
1507 int
1508 fdformat(dev, finfo, l)
1509 dev_t dev;
1510 struct ne7_fd_formb *finfo;
1511 struct lwp *l;
1512 {
1513 int rv = 0;
1514 struct fd_softc *fd = device_lookup(&fd_cd, FDUNIT(dev));
1515 struct fd_type *type = fd->sc_type;
1516 struct buf *bp;
1517
1518 /* set up a buffer header for fdstrategy() */
1519 bp = getiobuf_nowait();
1520 if (bp == NULL)
1521 return ENOBUFS;
1522
1523 bp->b_vp = NULL;
1524 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1525 bp->b_proc = l->l_proc;
1526 bp->b_dev = dev;
1527
1528 /*
1529 * calculate a fake blkno, so fdstrategy() would initiate a
1530 * seek to the requested cylinder
1531 */
1532 bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1533 + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1534
1535 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1536 bp->b_data = (void *)finfo;
1537
1538 #ifdef DEBUG
1539 printf("fdformat: blkno %" PRIx64 " count %x\n",
1540 bp->b_blkno, bp->b_bcount);
1541 #endif
1542
1543 /* now do the format */
1544 fdstrategy(bp);
1545
1546 /* ...and wait for it to complete */
1547 rv = biowait(bp);
1548 putiobuf(bp);
1549 return rv;
1550 }
1551
1552 /*
1553 * Mountroot hook: prompt the user to enter the root file system
1554 * floppy.
1555 */
1556 void
1557 fd_mountroot_hook(struct device *dev)
1558 {
1559 int c;
1560
1561 printf("Insert filesystem floppy and press return.");
1562 cnpollc(1);
1563 for (;;) {
1564 c = cngetc();
1565 if ((c == '\r') || (c == '\n')) {
1566 printf("\n");
1567 break;
1568 }
1569 }
1570 cnpollc(0);
1571 }
1572
1573 static void
1574 fd_set_properties(struct fd_softc *fd)
1575 {
1576 prop_dictionary_t disk_info, odisk_info, geom;
1577 const struct fd_type *fdt;
1578 int secsize;
1579
1580 fdt = fd->sc_type;
1581 if (fdt == NULL) {
1582 fdt = fd->sc_deftype;
1583 if (fdt == NULL)
1584 return;
1585 }
1586
1587 disk_info = prop_dictionary_create();
1588
1589 geom = prop_dictionary_create();
1590
1591 prop_dictionary_set_uint64(geom, "sectors-per-unit",
1592 fdt->size);
1593
1594 switch (fdt->secsize) {
1595 case 2:
1596 secsize = 512;
1597 break;
1598 case 3:
1599 secsize = 1024;
1600 break;
1601 default:
1602 secsize = 0;
1603 }
1604
1605 prop_dictionary_set_uint32(geom, "sector-size",
1606 secsize);
1607
1608 prop_dictionary_set_uint16(geom, "sectors-per-track",
1609 fdt->sectrac);
1610
1611 prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
1612 fdt->heads);
1613
1614 prop_dictionary_set_uint64(geom, "cylinders-per-unit",
1615 fdt->cyls);
1616
1617 prop_dictionary_set(disk_info, "geometry", geom);
1618 prop_object_release(geom);
1619
1620 prop_dictionary_set(device_properties(&fd->sc_dev),
1621 "disk-info", disk_info);
1622
1623 /*
1624 * Don't release disk_info here; we keep a reference to it.
1625 * disk_detach() will release it when we go away.
1626 */
1627
1628 odisk_info = fd->sc_dk.dk_info;
1629 fd->sc_dk.dk_info = disk_info;
1630 if (odisk_info)
1631 prop_object_release(odisk_info);
1632 }
1633