fd.c revision 1.87.4.2 1 /* $NetBSD: fd.c,v 1.87.4.2 2001/10/10 11:56:32 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
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) 1993, 1994, 1995 Charles M. Hannum.
41 * Copyright (c) 1995 Paul Kranenburg.
42 * Copyright (c) 1990 The Regents of the University of California.
43 * All rights reserved.
44 *
45 * This code is derived from software contributed to Berkeley by
46 * Don Ahn.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
56 * 3. All advertising materials mentioning features or use of this software
57 * must display the following acknowledgement:
58 * This product includes software developed by the University of
59 * California, Berkeley and its contributors.
60 * 4. Neither the name of the University nor the names of its contributors
61 * may be used to endorse or promote products derived from this software
62 * without specific prior written permission.
63 *
64 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * SUCH DAMAGE.
75 *
76 * @(#)fd.c 7.4 (Berkeley) 5/25/91
77 */
78
79 #include "opt_ddb.h"
80 #include "opt_md.h"
81
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/callout.h>
85 #include <sys/kernel.h>
86 #include <sys/file.h>
87 #include <sys/ioctl.h>
88 #include <sys/device.h>
89 #include <sys/disklabel.h>
90 #include <sys/dkstat.h>
91 #include <sys/disk.h>
92 #include <sys/fdio.h>
93 #include <sys/buf.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/uio.h>
97 #include <sys/stat.h>
98 #include <sys/syslog.h>
99 #include <sys/queue.h>
100 #include <sys/conf.h>
101 #include <sys/vnode.h>
102
103 #include <dev/cons.h>
104
105 #include <uvm/uvm_extern.h>
106
107 #include <machine/autoconf.h>
108 #include <machine/intr.h>
109 #include <machine/conf.h>
110
111 #include <sparc/sparc/auxreg.h>
112 #include <sparc/dev/fdreg.h>
113 #include <sparc/dev/fdvar.h>
114
115 #define FDUNIT(dev) (minor(dev) / 8)
116 #define FDTYPE(dev) (minor(dev) % 8)
117
118 /* XXX misuse a flag to identify format operation */
119 #define B_FORMAT B_XXX
120
121 #define FD_DEBUG
122 #ifdef FD_DEBUG
123 int fdc_debug = 0;
124 #endif
125
126 enum fdc_state {
127 DEVIDLE = 0,
128 MOTORWAIT, /* 1 */
129 DOSEEK, /* 2 */
130 SEEKWAIT, /* 3 */
131 SEEKTIMEDOUT, /* 4 */
132 SEEKCOMPLETE, /* 5 */
133 DOIO, /* 6 */
134 IOCOMPLETE, /* 7 */
135 IOTIMEDOUT, /* 8 */
136 IOCLEANUPWAIT, /* 9 */
137 IOCLEANUPTIMEDOUT,/*10 */
138 DORESET, /* 11 */
139 RESETCOMPLETE, /* 12 */
140 RESETTIMEDOUT, /* 13 */
141 DORECAL, /* 14 */
142 RECALWAIT, /* 15 */
143 RECALTIMEDOUT, /* 16 */
144 RECALCOMPLETE, /* 17 */
145 };
146
147 /* software state, per controller */
148 struct fdc_softc {
149 struct device sc_dev; /* boilerplate */
150 bus_space_tag_t sc_bustag;
151
152 struct callout sc_timo_ch; /* timeout callout */
153 struct callout sc_intr_ch; /* pseudo-intr callout */
154
155 struct fd_softc *sc_fd[4]; /* pointers to children */
156 TAILQ_HEAD(drivehead, fd_softc) sc_drives;
157 enum fdc_state sc_state;
158 int sc_flags;
159 #define FDC_82077 0x01
160 #define FDC_NEEDHEADSETTLE 0x02
161 #define FDC_EIS 0x04
162 #define FDC_NEEDMOTORWAIT 0x08
163 int sc_errors; /* number of retries so far */
164 int sc_overruns; /* number of DMA overruns */
165 int sc_cfg; /* current configuration */
166 struct fdcio sc_io;
167 #define sc_handle sc_io.fdcio_handle
168 #define sc_reg_msr sc_io.fdcio_reg_msr
169 #define sc_reg_fifo sc_io.fdcio_reg_fifo
170 #define sc_reg_dor sc_io.fdcio_reg_dor
171 #define sc_reg_drs sc_io.fdcio_reg_msr
172 #define sc_itask sc_io.fdcio_itask
173 #define sc_istatus sc_io.fdcio_istatus
174 #define sc_data sc_io.fdcio_data
175 #define sc_tc sc_io.fdcio_tc
176 #define sc_nstat sc_io.fdcio_nstat
177 #define sc_status sc_io.fdcio_status
178 #define sc_intrcnt sc_io.fdcio_intrcnt
179 };
180
181 extern struct fdcio *fdciop; /* I/O descriptor used in fdintr.s */
182
183 /* controller driver configuration */
184 int fdcmatch_mainbus __P((struct device *, struct cfdata *, void *));
185 int fdcmatch_obio __P((struct device *, struct cfdata *, void *));
186 void fdcattach_mainbus __P((struct device *, struct device *, void *));
187 void fdcattach_obio __P((struct device *, struct device *, void *));
188
189 int fdcattach __P((struct fdc_softc *, int));
190
191 struct cfattach fdc_mainbus_ca = {
192 sizeof(struct fdc_softc), fdcmatch_mainbus, fdcattach_mainbus
193 };
194 struct cfattach fdc_obio_ca = {
195 sizeof(struct fdc_softc), fdcmatch_obio, fdcattach_obio
196 };
197
198 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
199
200 /*
201 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
202 * we tell them apart.
203 */
204 struct fd_type {
205 int sectrac; /* sectors per track */
206 int heads; /* number of heads */
207 int seccyl; /* sectors per cylinder */
208 int secsize; /* size code for sectors */
209 int datalen; /* data len when secsize = 0 */
210 int steprate; /* step rate and head unload time */
211 int gap1; /* gap len between sectors */
212 int gap2; /* formatting gap */
213 int cylinders; /* total num of cylinders */
214 int size; /* size of disk in sectors */
215 int step; /* steps per cylinder */
216 int rate; /* transfer speed code */
217 int fillbyte; /* format fill byte */
218 int interleave; /* interleave factor (formatting) */
219 char *name;
220 };
221
222 /* The order of entries in the following table is important -- BEWARE! */
223 struct fd_type fd_types[] = {
224 { 18,2,36,2,0xff,0xcf,0x1b,0x54,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
225 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5" 720kB diskette */
226 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */
227 { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
228 };
229
230 /* software state, per disk (with up to 4 disks per ctlr) */
231 struct fd_softc {
232 struct device sc_dv; /* generic device info */
233 struct disk sc_dk; /* generic disk info */
234
235 struct fd_type *sc_deftype; /* default type descriptor */
236 struct fd_type *sc_type; /* current type descriptor */
237
238 struct callout sc_motoron_ch;
239 struct callout sc_motoroff_ch;
240
241 daddr_t sc_blkno; /* starting block number */
242 int sc_bcount; /* byte count left */
243 int sc_skip; /* bytes already transferred */
244 int sc_nblks; /* number of blocks currently transferring */
245 int sc_nbytes; /* number of bytes currently transferring */
246
247 int sc_drive; /* physical unit number */
248 int sc_flags;
249 #define FD_OPEN 0x01 /* it's open */
250 #define FD_MOTOR 0x02 /* motor should be on */
251 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
252 int sc_cylin; /* where we think the head is */
253 int sc_opts; /* user-set options */
254
255 void *sc_sdhook; /* shutdownhook cookie */
256
257 TAILQ_ENTRY(fd_softc) sc_drivechain;
258 int sc_ops; /* I/O ops since last switch */
259 struct buf_queue sc_q; /* pending I/O requests */
260 int sc_active; /* number of active I/O requests */
261 };
262
263 /* floppy driver configuration */
264 int fdmatch __P((struct device *, struct cfdata *, void *));
265 void fdattach __P((struct device *, struct device *, void *));
266
267 struct cfattach fd_ca = {
268 sizeof(struct fd_softc), fdmatch, fdattach
269 };
270
271 extern struct cfdriver fd_cd;
272
273 void fdgetdisklabel __P((struct vnode *));
274 int fd_get_parms __P((struct fd_softc *));
275 void fdstrategy __P((struct buf *));
276 void fdstart __P((struct fd_softc *));
277 int fdprint __P((void *, const char *));
278
279 struct dkdriver fddkdriver = { fdstrategy };
280
281 struct fd_type *fd_nvtotype __P((char *, int, int));
282 void fd_set_motor __P((struct fdc_softc *fdc));
283 void fd_motor_off __P((void *arg));
284 void fd_motor_on __P((void *arg));
285 int fdcresult __P((struct fdc_softc *fdc));
286 int fdc_wrfifo __P((struct fdc_softc *fdc, u_char x));
287 void fdcstart __P((struct fdc_softc *fdc));
288 void fdcstatus __P((struct fdc_softc *fdc, char *s));
289 void fdc_reset __P((struct fdc_softc *fdc));
290 void fdctimeout __P((void *arg));
291 void fdcpseudointr __P((void *arg));
292 int fdc_c_hwintr __P((void *));
293 void fdchwintr __P((void));
294 int fdcswintr __P((void *));
295 int fdcstate __P((struct fdc_softc *));
296 void fdcretry __P((struct fdc_softc *fdc));
297 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
298 int fdformat __P((struct vnode *, struct ne7_fd_formb *, struct proc *));
299 void fd_do_eject __P((struct fd_softc *));
300 void fd_mountroot_hook __P((struct device *));
301 static int fdconf __P((struct fdc_softc *));
302 static void establish_chip_type __P((
303 struct fdc_softc *,
304 bus_space_tag_t,
305 bus_type_t,
306 bus_addr_t,
307 bus_size_t,
308 bus_space_handle_t));
309
310
311 #if PIL_FDSOFT == 4
312 #define IE_FDSOFT IE_L4
313 #else
314 #error 4
315 #endif
316
317 #if defined(SUN4M)
318 #define FD_SET_SWINTR do { \
319 if (CPU_ISSUN4M) \
320 raise(0, PIL_FDSOFT); \
321 else \
322 ienab_bis(IE_L4); \
323 } while(0)
324 #else
325 #define FD_SET_SWINTR ienab_bis(IE_FDSOFT)
326 #endif /* defined(SUN4M) */
327
328 #define OBP_FDNAME (CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
329
330 int
331 fdcmatch_mainbus(parent, match, aux)
332 struct device *parent;
333 struct cfdata *match;
334 void *aux;
335 {
336 struct mainbus_attach_args *ma = aux;
337
338 /*
339 * Floppy controller is on mainbus on sun4c.
340 */
341 if (!CPU_ISSUN4C)
342 return (0);
343
344 /* sun4c PROMs call the controller "fd" */
345 if (strcmp("fd", ma->ma_name) != 0)
346 return (0);
347
348 return (bus_space_probe(ma->ma_bustag,
349 ma->ma_iospace,
350 ma->ma_paddr,
351 1, /* probe size */
352 0, /* offset */
353 0, /* flags */
354 NULL, NULL));
355 }
356
357 int
358 fdcmatch_obio(parent, match, aux)
359 struct device *parent;
360 struct cfdata *match;
361 void *aux;
362 {
363 union obio_attach_args *uoba = aux;
364 struct sbus_attach_args *sa;
365
366 /*
367 * Floppy controller is on obio on sun4m.
368 */
369 if (uoba->uoba_isobio4 != 0)
370 return (0);
371
372 sa = &uoba->uoba_sbus;
373
374 /* sun4m PROMs call the controller "SUNW,fdtwo" */
375 if (strcmp("SUNW,fdtwo", sa->sa_name) != 0)
376 return (0);
377
378 return (bus_space_probe(sa->sa_bustag, sa->sa_slot, sa->sa_offset,
379 1, /* probe size */
380 0, /* offset */
381 0, /* flags */
382 NULL, NULL));
383 }
384
385 static void
386 establish_chip_type(fdc, tag, type, addr, size, handle)
387 struct fdc_softc *fdc;
388 bus_space_tag_t tag;
389 bus_type_t type;
390 bus_addr_t addr;
391 bus_size_t size;
392 bus_space_handle_t handle;
393 {
394 u_int8_t v;
395
396 /*
397 * This hack from Chris Torek: apparently DOR really
398 * addresses MSR/DRS on a 82072.
399 * We used to rely on the VERSION command to tell the
400 * difference (which did not work).
401 */
402
403 /* First, check the size of the register bank */
404 if (size < 8)
405 /* It isn't a 82077 */
406 return;
407
408 /* Then probe the DOR register offset */
409 if (bus_space_probe(tag, type, addr,
410 1, /* probe size */
411 FDREG77_DOR, /* offset */
412 0, /* flags */
413 NULL, NULL) == 0) {
414
415 /* It isn't a 82077 */
416 return;
417 }
418
419 v = bus_space_read_1(tag, handle, FDREG77_DOR);
420 if (v == NE7_RQM) {
421 /*
422 * Value in DOR looks like it's really MSR
423 */
424 bus_space_write_1(tag, handle, FDREG77_DOR, FDC_250KBPS);
425 v = bus_space_read_1(tag, handle, FDREG77_DOR);
426 if (v == NE7_RQM) {
427 /*
428 * The value in the DOR didn't stick;
429 * it isn't a 82077
430 */
431 return;
432 }
433 }
434
435 fdc->sc_flags |= FDC_82077;
436 }
437
438 /*
439 * Arguments passed between fdcattach and fdprobe.
440 */
441 struct fdc_attach_args {
442 int fa_drive;
443 struct fd_type *fa_deftype;
444 };
445
446 /*
447 * Print the location of a disk drive (called just before attaching the
448 * the drive). If `fdc' is not NULL, the drive was found but was not
449 * in the system config file; print the drive name as well.
450 * Return QUIET (config_find ignores this if the device was configured) to
451 * avoid printing `fdN not configured' messages.
452 */
453 int
454 fdprint(aux, fdc)
455 void *aux;
456 const char *fdc;
457 {
458 register struct fdc_attach_args *fa = aux;
459
460 if (!fdc)
461 printf(" drive %d", fa->fa_drive);
462 return (QUIET);
463 }
464
465 /*
466 * Configure several parameters and features on the FDC.
467 * Return 0 on success.
468 */
469 static int
470 fdconf(fdc)
471 struct fdc_softc *fdc;
472 {
473 int vroom;
474
475 if (fdc_wrfifo(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
476 return (-1);
477
478 /*
479 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
480 * the PROM thinks is appropriate.
481 */
482 if ((vroom = fdc->sc_status[7]) == 0)
483 vroom = 0x64;
484
485 /* Configure controller to use FIFO and Implied Seek */
486 if (fdc_wrfifo(fdc, NE7CMD_CFG) != 0)
487 return (-1);
488 if (fdc_wrfifo(fdc, vroom) != 0)
489 return (-1);
490 if (fdc_wrfifo(fdc, fdc->sc_cfg) != 0)
491 return (-1);
492 if (fdc_wrfifo(fdc, 0) != 0) /* PRETRK */
493 return (-1);
494 /* No result phase for the NE7CMD_CFG command */
495
496 if ((fdc->sc_flags & FDC_82077) != 0) {
497 /* Lock configuration across soft resets. */
498 if (fdc_wrfifo(fdc, NE7CMD_LOCK | CFG_LOCK) != 0 ||
499 fdcresult(fdc) != 1) {
500 #ifdef DEBUG
501 printf("fdconf: CFGLOCK failed");
502 #endif
503 return (-1);
504 }
505 }
506
507 return (0);
508 #if 0
509 if (fdc_wrfifo(fdc, NE7CMD_VERSION) == 0 &&
510 fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
511 if (fdc_debug)
512 printf("[version cmd]");
513 }
514 #endif
515 }
516
517 void
518 fdcattach_mainbus(parent, self, aux)
519 struct device *parent, *self;
520 void *aux;
521 {
522 struct fdc_softc *fdc = (void *)self;
523 struct mainbus_attach_args *ma = aux;
524
525 fdc->sc_bustag = ma->ma_bustag;
526
527 if (bus_space_map2(
528 ma->ma_bustag,
529 ma->ma_iospace,
530 ma->ma_paddr,
531 ma->ma_size,
532 BUS_SPACE_MAP_LINEAR,
533 0,
534 &fdc->sc_handle) != 0) {
535 printf("%s: cannot map registers\n", self->dv_xname);
536 return;
537 }
538
539 establish_chip_type(fdc,
540 ma->ma_bustag,
541 ma->ma_iospace,
542 ma->ma_paddr,
543 ma->ma_size,
544 fdc->sc_handle);
545
546 if (fdcattach(fdc, ma->ma_pri) != 0)
547 bus_space_unmap(ma->ma_bustag, fdc->sc_handle, ma->ma_size);
548 }
549
550 void
551 fdcattach_obio(parent, self, aux)
552 struct device *parent, *self;
553 void *aux;
554 {
555 struct fdc_softc *fdc = (void *)self;
556 union obio_attach_args *uoba = aux;
557 struct sbus_attach_args *sa = &uoba->uoba_sbus;
558
559 if (sa->sa_nintr == 0) {
560 printf(": no interrupt line configured\n");
561 return;
562 }
563
564 fdc->sc_bustag = sa->sa_bustag;
565
566 if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
567 sa->sa_offset,
568 sa->sa_size,
569 BUS_SPACE_MAP_LINEAR,
570 0,
571 &fdc->sc_handle) != 0) {
572 printf("%s: cannot map control registers\n",
573 self->dv_xname);
574 return;
575 }
576
577 establish_chip_type(fdc,
578 sa->sa_bustag,
579 sa->sa_slot,
580 sa->sa_offset,
581 sa->sa_size,
582 fdc->sc_handle);
583
584 if (strcmp(PROM_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
585 printf(": no drives attached\n");
586 return;
587 }
588
589 if (fdcattach(fdc, sa->sa_pri) != 0)
590 bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
591 }
592
593 int
594 fdcattach(fdc, pri)
595 struct fdc_softc *fdc;
596 int pri;
597 {
598 struct fdc_attach_args fa;
599 int drive_attached;
600 char code;
601
602 callout_init(&fdc->sc_timo_ch);
603 callout_init(&fdc->sc_intr_ch);
604
605 fdc->sc_state = DEVIDLE;
606 fdc->sc_itask = FDC_ITASK_NONE;
607 fdc->sc_istatus = FDC_ISTATUS_NONE;
608 fdc->sc_flags |= FDC_EIS;
609 TAILQ_INIT(&fdc->sc_drives);
610
611 if ((fdc->sc_flags & FDC_82077) != 0) {
612 fdc->sc_reg_msr = FDREG77_MSR;
613 fdc->sc_reg_fifo = FDREG77_FIFO;
614 fdc->sc_reg_dor = FDREG77_DOR;
615 code = '7';
616 fdc->sc_flags |= FDC_NEEDMOTORWAIT;
617 } else {
618 fdc->sc_reg_msr = FDREG72_MSR;
619 fdc->sc_reg_fifo = FDREG72_FIFO;
620 fdc->sc_reg_dor = 0;
621 code = '2';
622 }
623
624 printf(" softpri %d: chip 8207%c\n", PIL_FDSOFT, code);
625
626 /*
627 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
628 * Note: CFG_EFIFO is active-low, initial threshold value: 8
629 */
630 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
631 if (fdconf(fdc) != 0) {
632 printf("%s: no drives attached\n", fdc->sc_dev.dv_xname);
633 return (-1);
634 }
635
636 fdciop = &fdc->sc_io;
637 if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO,
638 BUS_INTR_ESTABLISH_FASTTRAP,
639 (int (*) __P((void *)))fdchwintr, NULL) == NULL) {
640
641 printf("%s: notice: no fast trap handler slot available\n",
642 fdc->sc_dev.dv_xname);
643 if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO, 0,
644 fdc_c_hwintr, fdc) == NULL) {
645 printf("%s: cannot register interrupt handler\n",
646 fdc->sc_dev.dv_xname);
647 return (-1);
648 }
649 }
650
651 if (bus_intr_establish(fdc->sc_bustag, PIL_FDSOFT, IPL_BIO,
652 BUS_INTR_ESTABLISH_SOFTINTR,
653 fdcswintr, fdc) == NULL) {
654 printf("%s: cannot register interrupt handler\n",
655 fdc->sc_dev.dv_xname);
656 return (-1);
657 }
658
659 evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
660 fdc->sc_dev.dv_xname, "intr");
661
662 /* physical limit: four drives per controller. */
663 drive_attached = 0;
664 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
665 fa.fa_deftype = NULL; /* unknown */
666 fa.fa_deftype = &fd_types[0]; /* XXX */
667 if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
668 drive_attached = 1;
669 }
670
671 if (drive_attached == 0) {
672 /* XXX - dis-establish interrupts here */
673 /* return (-1); */
674 }
675
676 return (0);
677 }
678
679 int
680 fdmatch(parent, match, aux)
681 struct device *parent;
682 struct cfdata *match;
683 void *aux;
684 {
685 struct fdc_softc *fdc = (void *)parent;
686 bus_space_tag_t t = fdc->sc_bustag;
687 bus_space_handle_t h = fdc->sc_handle;
688 struct fdc_attach_args *fa = aux;
689 int drive = fa->fa_drive;
690 int n, ok;
691
692 if (drive > 0)
693 /* XXX - for now, punt on more than one drive */
694 return (0);
695
696 if ((fdc->sc_flags & FDC_82077) != 0) {
697 /* select drive and turn on motor */
698 bus_space_write_1(t, h, fdc->sc_reg_dor,
699 drive | FDO_FRST | FDO_MOEN(drive));
700 /* wait for motor to spin up */
701 delay(250000);
702 } else {
703 auxregbisc(AUXIO4C_FDS, 0);
704 }
705 fdc->sc_nstat = 0;
706 fdc_wrfifo(fdc, NE7CMD_RECAL);
707 fdc_wrfifo(fdc, drive);
708
709 /* Wait for recalibration to complete */
710 for (n = 0; n < 10000; n++) {
711 u_int8_t v;
712
713 delay(1000);
714 v = bus_space_read_1(t, h, fdc->sc_reg_msr);
715 if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
716 /* wait a bit longer till device *really* is ready */
717 delay(100000);
718 if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
719 break;
720 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
721 /*
722 * Got `invalid command'; we interpret it
723 * to mean that the re-calibrate hasn't in
724 * fact finished yet
725 */
726 continue;
727 break;
728 }
729 }
730 n = fdc->sc_nstat;
731 #ifdef FD_DEBUG
732 if (fdc_debug) {
733 int i;
734 printf("fdprobe: %d stati:", n);
735 for (i = 0; i < n; i++)
736 printf(" 0x%x", fdc->sc_status[i]);
737 printf("\n");
738 }
739 #endif
740 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
741
742 /* turn off motor */
743 if ((fdc->sc_flags & FDC_82077) != 0) {
744 /* deselect drive and turn motor off */
745 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
746 } else {
747 auxregbisc(0, AUXIO4C_FDS);
748 }
749
750 return (ok);
751 }
752
753 /*
754 * Controller is working, and drive responded. Attach it.
755 */
756 void
757 fdattach(parent, self, aux)
758 struct device *parent, *self;
759 void *aux;
760 {
761 struct fdc_softc *fdc = (void *)parent;
762 struct fd_softc *fd = (void *)self;
763 struct fdc_attach_args *fa = aux;
764 struct fd_type *type = fa->fa_deftype;
765 int drive = fa->fa_drive;
766
767 callout_init(&fd->sc_motoron_ch);
768 callout_init(&fd->sc_motoroff_ch);
769
770 /* XXX Allow `flags' to override device type? */
771
772 if (type)
773 printf(": %s %d cyl, %d head, %d sec\n", type->name,
774 type->cylinders, type->heads, type->sectrac);
775 else
776 printf(": density unknown\n");
777
778 BUFQ_INIT(&fd->sc_q);
779 fd->sc_cylin = -1;
780 fd->sc_drive = drive;
781 fd->sc_deftype = type;
782 fdc->sc_fd[drive] = fd;
783
784 fdc_wrfifo(fdc, NE7CMD_SPECIFY);
785 fdc_wrfifo(fdc, type->steprate);
786 /* XXX head load time == 6ms */
787 fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
788
789 /*
790 * Initialize and attach the disk structure.
791 */
792 fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
793 fd->sc_dk.dk_driver = &fddkdriver;
794 disk_attach(&fd->sc_dk);
795
796 /*
797 * Establish a mountroot_hook anyway in case we booted
798 * with RB_ASKNAME and get selected as the boot device.
799 */
800 mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
801
802 /* Make sure the drive motor gets turned off at shutdown time. */
803 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
804 }
805
806 __inline struct fd_type *
807 fd_dev_to_type(fd, dev)
808 struct fd_softc *fd;
809 dev_t dev;
810 {
811 int type = FDTYPE(dev);
812
813 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
814 return (NULL);
815 return (type ? &fd_types[type - 1] : fd->sc_deftype);
816 }
817
818 void
819 fdstrategy(bp)
820 register struct buf *bp; /* IO operation to perform */
821 {
822 struct fd_softc *fd;
823 int sz;
824 int s;
825
826 fd = vdev_privdata(bp->b_devvp);
827
828 /* Valid unit, controller, and request? */
829 if (fd == NULL ||
830 bp->b_blkno < 0 ||
831 (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
832 (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
833 (bp->b_flags & B_FORMAT) == 0)) {
834 bp->b_error = EINVAL;
835 goto bad;
836 }
837
838 /* If it's a null transfer, return immediately. */
839 if (bp->b_bcount == 0)
840 goto done;
841
842 sz = howmany(bp->b_bcount, DEV_BSIZE);
843
844 if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
845 sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
846 - bp->b_blkno;
847 if (sz == 0) {
848 /* If exactly at end of disk, return EOF. */
849 bp->b_resid = bp->b_bcount;
850 goto done;
851 }
852 if (sz < 0) {
853 /* If past end of disk, return EINVAL. */
854 bp->b_error = EINVAL;
855 goto bad;
856 }
857 /* Otherwise, truncate request. */
858 bp->b_bcount = sz << DEV_BSHIFT;
859 }
860
861 bp->b_rawblkno = bp->b_blkno;
862 bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
863 (FD_BSIZE(fd) * fd->sc_type->seccyl);
864
865 #ifdef FD_DEBUG
866 if (fdc_debug > 1)
867 printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
868 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder);
869 #endif
870
871 /* Queue transfer on drive, activate drive and controller if idle. */
872 s = splbio();
873 disksort_cylinder(&fd->sc_q, bp);
874 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
875 if (fd->sc_active == 0)
876 fdstart(fd);
877 #ifdef DIAGNOSTIC
878 else {
879 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
880 if (fdc->sc_state == DEVIDLE) {
881 printf("fdstrategy: controller inactive\n");
882 fdcstart(fdc);
883 }
884 }
885 #endif
886 splx(s);
887 return;
888
889 bad:
890 bp->b_flags |= B_ERROR;
891 done:
892 /* Toss transfer; we're done early. */
893 biodone(bp);
894 }
895
896 void
897 fdstart(fd)
898 struct fd_softc *fd;
899 {
900 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
901 int active = fdc->sc_drives.tqh_first != 0;
902
903 /* Link into controller queue. */
904 fd->sc_active = 1;
905 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
906
907 /* If controller not already active, start it. */
908 if (!active)
909 fdcstart(fdc);
910 }
911
912 void
913 fdfinish(fd, bp)
914 struct fd_softc *fd;
915 struct buf *bp;
916 {
917 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
918
919 /*
920 * Move this drive to the end of the queue to give others a `fair'
921 * chance. We only force a switch if N operations are completed while
922 * another drive is waiting to be serviced, since there is a long motor
923 * startup delay whenever we switch.
924 */
925 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
926 fd->sc_ops = 0;
927 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
928 if (BUFQ_NEXT(bp) != NULL) {
929 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
930 } else
931 fd->sc_active = 0;
932 }
933 bp->b_resid = fd->sc_bcount;
934 fd->sc_skip = 0;
935 BUFQ_REMOVE(&fd->sc_q, bp);
936
937 biodone(bp);
938 /* turn off motor 5s from now */
939 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
940 fdc->sc_state = DEVIDLE;
941 }
942
943 void
944 fdc_reset(fdc)
945 struct fdc_softc *fdc;
946 {
947 bus_space_tag_t t = fdc->sc_bustag;
948 bus_space_handle_t h = fdc->sc_handle;
949
950 if ((fdc->sc_flags & FDC_82077) != 0) {
951 bus_space_write_1(t, h, fdc->sc_reg_dor,
952 FDO_FDMAEN | FDO_MOEN(0));
953 }
954
955 bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
956 delay(10);
957 bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
958
959 if ((fdc->sc_flags & FDC_82077) != 0) {
960 bus_space_write_1(t, h, fdc->sc_reg_dor,
961 FDO_FRST | FDO_FDMAEN | FDO_DS);
962 }
963 #ifdef FD_DEBUG
964 if (fdc_debug)
965 printf("fdc reset\n");
966 #endif
967 }
968
969 void
970 fd_set_motor(fdc)
971 struct fdc_softc *fdc;
972 {
973 struct fd_softc *fd;
974 u_char status;
975 int n;
976
977 if ((fdc->sc_flags & FDC_82077) != 0) {
978 status = FDO_FRST | FDO_FDMAEN;
979 if ((fd = fdc->sc_drives.tqh_first) != NULL)
980 status |= fd->sc_drive;
981
982 for (n = 0; n < 4; n++)
983 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
984 status |= FDO_MOEN(n);
985 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
986 fdc->sc_reg_dor, status);
987 } else {
988
989 for (n = 0; n < 4; n++) {
990 if ((fd = fdc->sc_fd[n]) != NULL &&
991 (fd->sc_flags & FD_MOTOR) != 0) {
992 auxregbisc(AUXIO4C_FDS, 0);
993 return;
994 }
995 }
996 auxregbisc(0, AUXIO4C_FDS);
997 }
998 }
999
1000 void
1001 fd_motor_off(arg)
1002 void *arg;
1003 {
1004 struct fd_softc *fd = arg;
1005 int s;
1006
1007 s = splbio();
1008 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1009 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
1010 splx(s);
1011 }
1012
1013 void
1014 fd_motor_on(arg)
1015 void *arg;
1016 {
1017 struct fd_softc *fd = arg;
1018 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
1019 int s;
1020
1021 s = splbio();
1022 fd->sc_flags &= ~FD_MOTOR_WAIT;
1023 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
1024 (void) fdcstate(fdc);
1025 splx(s);
1026 }
1027
1028 /*
1029 * Get status bytes off the FDC after a command has finished
1030 * Returns the number of status bytes read; -1 on error.
1031 * The return value is also stored in `sc_nstat'.
1032 */
1033 int
1034 fdcresult(fdc)
1035 struct fdc_softc *fdc;
1036 {
1037 bus_space_tag_t t = fdc->sc_bustag;
1038 bus_space_handle_t h = fdc->sc_handle;
1039 int j, n = 0;
1040
1041 for (j = 10000; j; j--) {
1042 u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1043 v &= (NE7_DIO | NE7_RQM | NE7_CB);
1044 if (v == NE7_RQM)
1045 return (fdc->sc_nstat = n);
1046 if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
1047 if (n >= sizeof(fdc->sc_status)) {
1048 log(LOG_ERR, "fdcresult: overrun\n");
1049 return (-1);
1050 }
1051 fdc->sc_status[n++] =
1052 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1053 } else
1054 delay(1);
1055 }
1056
1057 log(LOG_ERR, "fdcresult: timeout\n");
1058 return (fdc->sc_nstat = -1);
1059 }
1060
1061 /*
1062 * Write a command byte to the FDC.
1063 * Returns 0 on success; -1 on failure (i.e. timeout)
1064 */
1065 int
1066 fdc_wrfifo(fdc, x)
1067 struct fdc_softc *fdc;
1068 u_int8_t x;
1069 {
1070 bus_space_tag_t t = fdc->sc_bustag;
1071 bus_space_handle_t h = fdc->sc_handle;
1072 int i;
1073
1074 for (i = 100000; i-- > 0;) {
1075 u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1076 if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
1077 /* The chip is ready */
1078 bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
1079 return (0);
1080 }
1081 delay(1);
1082 }
1083 return (-1);
1084 }
1085
1086 int
1087 fdopen(devvp, flags, fmt, p)
1088 struct vnode *devvp;
1089 int flags, fmt;
1090 struct proc *p;
1091 {
1092 dev_t dev;
1093 int unit, pmask;
1094 struct fd_softc *fd;
1095 struct fd_type *type;
1096
1097 dev = vdev_rdev(devvp);
1098
1099 unit = FDUNIT(dev);
1100 if (unit >= fd_cd.cd_ndevs)
1101 return (ENXIO);
1102 fd = fd_cd.cd_devs[unit];
1103 if (fd == NULL)
1104 return (ENXIO);
1105 type = fd_dev_to_type(fd, dev);
1106 if (type == NULL)
1107 return (ENXIO);
1108
1109 if ((fd->sc_flags & FD_OPEN) != 0 &&
1110 fd->sc_type != type)
1111 return (EBUSY);
1112
1113 vdev_setprivdata(devvp, fd);
1114
1115 fd->sc_type = type;
1116 fd->sc_cylin = -1;
1117 fd->sc_flags |= FD_OPEN;
1118
1119 /*
1120 * Only update the disklabel if we're not open anywhere else.
1121 */
1122 if (fd->sc_dk.dk_openmask == 0)
1123 fdgetdisklabel(devvp);
1124
1125 pmask = (1 << DISKPART(dev));
1126
1127 switch (fmt) {
1128 case S_IFCHR:
1129 fd->sc_dk.dk_copenmask |= pmask;
1130 break;
1131
1132 case S_IFBLK:
1133 fd->sc_dk.dk_bopenmask |= pmask;
1134 break;
1135 }
1136 fd->sc_dk.dk_openmask =
1137 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1138
1139 return (0);
1140 }
1141
1142 int
1143 fdclose(devvp, flags, fmt, p)
1144 struct vnode *devvp;
1145 int flags, fmt;
1146 struct proc *p;
1147 {
1148 struct fd_softc *fd = vdev_privdata(devvp);
1149 int pmask = (1 << DISKPART(vdev_rdev(devvp)));
1150
1151 fd->sc_flags &= ~FD_OPEN;
1152 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
1153
1154 switch (fmt) {
1155 case S_IFCHR:
1156 fd->sc_dk.dk_copenmask &= ~pmask;
1157 break;
1158
1159 case S_IFBLK:
1160 fd->sc_dk.dk_bopenmask &= ~pmask;
1161 break;
1162 }
1163 fd->sc_dk.dk_openmask =
1164 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1165
1166 return (0);
1167 }
1168
1169 int
1170 fdread(devvp, uio, flag)
1171 struct vnode *devvp;
1172 struct uio *uio;
1173 int flag;
1174 {
1175
1176 return (physio(fdstrategy, NULL, devvp, B_READ, minphys, uio));
1177 }
1178
1179 int
1180 fdwrite(devvp, uio, flag)
1181 struct vnode *devvp;
1182 struct uio *uio;
1183 int flag;
1184 {
1185
1186 return (physio(fdstrategy, NULL, devvp, B_WRITE, minphys, uio));
1187 }
1188
1189 void
1190 fdcstart(fdc)
1191 struct fdc_softc *fdc;
1192 {
1193
1194 #ifdef DIAGNOSTIC
1195 /* only got here if controller's drive queue was inactive; should
1196 be in idle state */
1197 if (fdc->sc_state != DEVIDLE) {
1198 printf("fdcstart: not idle\n");
1199 return;
1200 }
1201 #endif
1202 (void) fdcstate(fdc);
1203 }
1204
1205 void
1206 fdcstatus(fdc, s)
1207 struct fdc_softc *fdc;
1208 char *s;
1209 {
1210 struct fd_softc *fd = fdc->sc_drives.tqh_first;
1211 int n;
1212 char bits[64];
1213
1214 /* Just print last status */
1215 n = fdc->sc_nstat;
1216
1217 #if 0
1218 /*
1219 * A 82072 seems to return <invalid command> on
1220 * gratuitous Sense Interrupt commands.
1221 */
1222 if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
1223 fdc_wrfifo(fdc, NE7CMD_SENSEI);
1224 (void) fdcresult(fdc);
1225 n = 2;
1226 }
1227 #endif
1228
1229 printf("%s: %s: state %d",
1230 fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
1231
1232 switch (n) {
1233 case 0:
1234 printf("\n");
1235 break;
1236 case 2:
1237 printf(" (st0 %s cyl %d)\n",
1238 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
1239 bits, sizeof(bits)), fdc->sc_status[1]);
1240 break;
1241 case 7:
1242 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1243 NE7_ST0BITS, bits, sizeof(bits)));
1244 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1245 NE7_ST1BITS, bits, sizeof(bits)));
1246 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1247 NE7_ST2BITS, bits, sizeof(bits)));
1248 printf(" cyl %d head %d sec %d)\n",
1249 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1250 break;
1251 #ifdef DIAGNOSTIC
1252 default:
1253 printf(" fdcstatus: weird size: %d\n", n);
1254 break;
1255 #endif
1256 }
1257 }
1258
1259 void
1260 fdctimeout(arg)
1261 void *arg;
1262 {
1263 struct fdc_softc *fdc = arg;
1264 struct fd_softc *fd;
1265 int s;
1266
1267 s = splbio();
1268 fd = fdc->sc_drives.tqh_first;
1269 if (fd == NULL) {
1270 printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
1271 fdc->sc_dev.dv_xname,
1272 fdc->sc_state, fdc->sc_istatus);
1273 fdc->sc_state = DEVIDLE;
1274 goto out;
1275 }
1276
1277 if (BUFQ_FIRST(&fd->sc_q) != NULL)
1278 fdc->sc_state++;
1279 else
1280 fdc->sc_state = DEVIDLE;
1281
1282 (void) fdcstate(fdc);
1283 out:
1284 splx(s);
1285
1286 }
1287
1288 void
1289 fdcpseudointr(arg)
1290 void *arg;
1291 {
1292 struct fdc_softc *fdc = arg;
1293 int s;
1294
1295 /* Just ensure it has the right spl. */
1296 s = splbio();
1297 (void) fdcstate(fdc);
1298 splx(s);
1299 }
1300
1301
1302 /*
1303 * hardware interrupt entry point: used only if no `fast trap' * (in-window)
1304 * handler is available. Unfortunately, we have no reliable way to
1305 * determine that the interrupt really came from the floppy controller;
1306 * just hope that the other devices that share this interrupt level
1307 * can do better..
1308 */
1309 int
1310 fdc_c_hwintr(arg)
1311 void *arg;
1312 {
1313 struct fdc_softc *fdc = arg;
1314 bus_space_tag_t t = fdc->sc_bustag;
1315 bus_space_handle_t h = fdc->sc_handle;
1316
1317 switch (fdc->sc_itask) {
1318 case FDC_ITASK_NONE:
1319 return (0);
1320 case FDC_ITASK_SENSEI:
1321 if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
1322 fdc->sc_istatus = FDC_ISTATUS_ERROR;
1323 else
1324 fdc->sc_istatus = FDC_ISTATUS_DONE;
1325 FD_SET_SWINTR;
1326 return (1);
1327 case FDC_ITASK_DMA:
1328 /* Proceed with pseudo-dma below */
1329 break;
1330 default:
1331 printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
1332 fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
1333 FD_SET_SWINTR;
1334 return (1);
1335 }
1336
1337 /*
1338 * Pseudo DMA in progress
1339 */
1340 for (;;) {
1341 u_int8_t msr;
1342
1343 msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
1344
1345 if ((msr & NE7_RQM) == 0)
1346 /* That's all this round */
1347 break;
1348
1349 if ((msr & NE7_NDM) == 0) {
1350 fdcresult(fdc);
1351 fdc->sc_istatus = FDC_ISTATUS_DONE;
1352 FD_SET_SWINTR;
1353 #ifdef FD_DEBUG
1354 if (fdc_debug > 1)
1355 printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
1356 #endif
1357 break;
1358 }
1359
1360 /* Another byte can be transferred */
1361 if ((msr & NE7_DIO) != 0)
1362 *fdc->sc_data =
1363 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1364 else
1365 bus_space_write_1(t, h, fdc->sc_reg_fifo,
1366 *fdc->sc_data);
1367
1368 fdc->sc_data++;
1369 if (--fdc->sc_tc == 0) {
1370 fdc->sc_istatus = FDC_ISTATUS_DONE;
1371 FTC_FLIP;
1372 fdcresult(fdc);
1373 FD_SET_SWINTR;
1374 break;
1375 }
1376 }
1377 return (1);
1378 }
1379
1380 int
1381 fdcswintr(arg)
1382 void *arg;
1383 {
1384 struct fdc_softc *fdc = arg;
1385 int s;
1386
1387 if (fdc->sc_istatus == FDC_ISTATUS_NONE)
1388 /* This (software) interrupt is not for us */
1389 return (0);
1390
1391 switch (fdc->sc_istatus) {
1392 case FDC_ISTATUS_ERROR:
1393 printf("fdc: ierror status: state %d\n", fdc->sc_state);
1394 break;
1395 case FDC_ISTATUS_SPURIOUS:
1396 printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
1397 break;
1398 }
1399
1400 s = splbio();
1401 fdcstate(fdc);
1402 splx(s);
1403 return (1);
1404 }
1405
1406 int
1407 fdcstate(fdc)
1408 struct fdc_softc *fdc;
1409 {
1410 #define st0 fdc->sc_status[0]
1411 #define st1 fdc->sc_status[1]
1412 #define cyl fdc->sc_status[1]
1413 #define FDC_WRFIFO(fdc, c) do { \
1414 if (fdc_wrfifo(fdc, (c))) { \
1415 goto xxx; \
1416 } \
1417 } while(0)
1418
1419 struct fd_softc *fd;
1420 struct buf *bp;
1421 int read, head, sec, nblks;
1422 struct fd_type *type;
1423 struct ne7_fd_formb *finfo = NULL;
1424
1425 if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
1426 /* Prevent loop if the reset sequence produces errors */
1427 if (fdc->sc_state != RESETCOMPLETE &&
1428 fdc->sc_state != RECALWAIT &&
1429 fdc->sc_state != RECALCOMPLETE)
1430 fdc->sc_state = DORESET;
1431 }
1432
1433 /* Clear I task/status field */
1434 fdc->sc_istatus = FDC_ISTATUS_NONE;
1435 fdc->sc_itask = FDC_ITASK_NONE;
1436
1437 loop:
1438 /* Is there a drive for the controller to do a transfer with? */
1439 fd = fdc->sc_drives.tqh_first;
1440 if (fd == NULL) {
1441 fdc->sc_state = DEVIDLE;
1442 return (0);
1443 }
1444
1445 /* Is there a transfer to this drive? If not, deactivate drive. */
1446 bp = BUFQ_FIRST(&fd->sc_q);
1447 if (bp == NULL) {
1448 fd->sc_ops = 0;
1449 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1450 fd->sc_active = 0;
1451 goto loop;
1452 }
1453
1454 if (bp->b_flags & B_FORMAT)
1455 finfo = (struct ne7_fd_formb *)bp->b_data;
1456
1457 switch (fdc->sc_state) {
1458 case DEVIDLE:
1459 fdc->sc_errors = 0;
1460 fd->sc_skip = 0;
1461 fd->sc_bcount = bp->b_bcount;
1462 fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
1463 callout_stop(&fd->sc_motoroff_ch);
1464 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1465 fdc->sc_state = MOTORWAIT;
1466 return (1);
1467 }
1468 if ((fd->sc_flags & FD_MOTOR) == 0) {
1469 /* Turn on the motor, being careful about pairing. */
1470 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1471 if (ofd && ofd->sc_flags & FD_MOTOR) {
1472 callout_stop(&ofd->sc_motoroff_ch);
1473 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1474 }
1475 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1476 fd_set_motor(fdc);
1477 fdc->sc_state = MOTORWAIT;
1478 if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
1479 /* Allow .25s for motor to stabilize. */
1480 callout_reset(&fd->sc_motoron_ch, hz / 4,
1481 fd_motor_on, fd);
1482 } else {
1483 fd->sc_flags &= ~FD_MOTOR_WAIT;
1484 goto loop;
1485 }
1486 return (1);
1487 }
1488 /* Make sure the right drive is selected. */
1489 fd_set_motor(fdc);
1490
1491 /*FALLTHROUGH*/
1492 case DOSEEK:
1493 doseek:
1494 if ((fdc->sc_flags & FDC_EIS) &&
1495 (bp->b_flags & B_FORMAT) == 0) {
1496 fd->sc_cylin = bp->b_cylinder;
1497 /* We use implied seek */
1498 goto doio;
1499 }
1500
1501 if (fd->sc_cylin == bp->b_cylinder)
1502 goto doio;
1503
1504 fd->sc_cylin = -1;
1505 fdc->sc_state = SEEKWAIT;
1506 fdc->sc_nstat = 0;
1507
1508 fd->sc_dk.dk_seek++;
1509
1510 disk_busy(&fd->sc_dk);
1511 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1512
1513 /* specify command */
1514 FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
1515 FDC_WRFIFO(fdc, fd->sc_type->steprate);
1516 /* XXX head load time == 6ms */
1517 FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
1518
1519 fdc->sc_itask = FDC_ITASK_SENSEI;
1520 /* seek function */
1521 FDC_WRFIFO(fdc, NE7CMD_SEEK);
1522 FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1523 FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
1524 return (1);
1525
1526 case DOIO:
1527 doio:
1528 if (finfo != NULL)
1529 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1530 (char *)finfo;
1531 type = fd->sc_type;
1532 sec = fd->sc_blkno % type->seccyl;
1533 nblks = type->seccyl - sec;
1534 nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
1535 nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
1536 fd->sc_nblks = nblks;
1537 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
1538 head = sec / type->sectrac;
1539 sec -= head * type->sectrac;
1540 #ifdef DIAGNOSTIC
1541 {int block;
1542 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1543 if (block != fd->sc_blkno) {
1544 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1545 #ifdef DDB
1546 Debugger();
1547 #endif
1548 }}
1549 #endif
1550 read = bp->b_flags & B_READ;
1551
1552 /* Setup for pseudo DMA */
1553 fdc->sc_data = bp->b_data + fd->sc_skip;
1554 fdc->sc_tc = fd->sc_nbytes;
1555
1556 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1557 fdc->sc_reg_drs, type->rate);
1558 #ifdef FD_DEBUG
1559 if (fdc_debug > 1)
1560 printf("fdcstate: doio: %s drive %d "
1561 "track %d head %d sec %d nblks %d\n",
1562 finfo ? "format" :
1563 (read ? "read" : "write"),
1564 fd->sc_drive, fd->sc_cylin, head, sec, nblks);
1565 #endif
1566 fdc->sc_state = IOCOMPLETE;
1567 fdc->sc_itask = FDC_ITASK_DMA;
1568 fdc->sc_nstat = 0;
1569
1570 disk_busy(&fd->sc_dk);
1571
1572 /* allow 3 seconds for operation */
1573 callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
1574
1575 if (finfo != NULL) {
1576 /* formatting */
1577 FDC_WRFIFO(fdc, NE7CMD_FORMAT);
1578 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1579 FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
1580 FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
1581 FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
1582 FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
1583 } else {
1584 if (read)
1585 FDC_WRFIFO(fdc, NE7CMD_READ);
1586 else
1587 FDC_WRFIFO(fdc, NE7CMD_WRITE);
1588 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1589 FDC_WRFIFO(fdc, fd->sc_cylin); /*track*/
1590 FDC_WRFIFO(fdc, head);
1591 FDC_WRFIFO(fdc, sec + 1); /*sector+1*/
1592 FDC_WRFIFO(fdc, type->secsize);/*sector size*/
1593 FDC_WRFIFO(fdc, type->sectrac);/*secs/track*/
1594 FDC_WRFIFO(fdc, type->gap1); /*gap1 size*/
1595 FDC_WRFIFO(fdc, type->datalen);/*data length*/
1596 }
1597
1598 return (1); /* will return later */
1599
1600 case SEEKWAIT:
1601 callout_stop(&fdc->sc_timo_ch);
1602 fdc->sc_state = SEEKCOMPLETE;
1603 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1604 /* allow 1/50 second for heads to settle */
1605 callout_reset(&fdc->sc_intr_ch, hz / 50,
1606 fdcpseudointr, fdc);
1607 return (1); /* will return later */
1608 }
1609 /*FALLTHROUGH*/
1610 case SEEKCOMPLETE:
1611 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */
1612
1613 /* Make sure seek really happened. */
1614 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1615 cyl != bp->b_cylinder * fd->sc_type->step) {
1616 #ifdef FD_DEBUG
1617 if (fdc_debug)
1618 fdcstatus(fdc, "seek failed");
1619 #endif
1620 fdcretry(fdc);
1621 goto loop;
1622 }
1623 fd->sc_cylin = bp->b_cylinder;
1624 goto doio;
1625
1626 case IOTIMEDOUT:
1627 /*
1628 * Try to abort the I/O operation without resetting
1629 * the chip first. Poke TC and arrange to pick up
1630 * the timed out I/O command's status.
1631 */
1632 fdc->sc_itask = FDC_ITASK_RESULT;
1633 fdc->sc_state = IOCLEANUPWAIT;
1634 fdc->sc_nstat = 0;
1635 /* 1/10 second should be enough */
1636 callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
1637 FTC_FLIP;
1638 return (1);
1639
1640 case IOCLEANUPTIMEDOUT:
1641 case SEEKTIMEDOUT:
1642 case RECALTIMEDOUT:
1643 case RESETTIMEDOUT:
1644 fdcstatus(fdc, "timeout");
1645
1646 /* All other timeouts always roll through to a chip reset */
1647 fdcretry(fdc);
1648
1649 /* Force reset, no matter what fdcretry() says */
1650 fdc->sc_state = DORESET;
1651 goto loop;
1652
1653 case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
1654 callout_stop(&fdc->sc_timo_ch);
1655 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1656 fdcretry(fdc);
1657 goto loop;
1658
1659 case IOCOMPLETE: /* IO DONE, post-analyze */
1660 callout_stop(&fdc->sc_timo_ch);
1661
1662 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1663
1664 if (fdc->sc_nstat != 7 || st1 != 0 ||
1665 ((st0 & 0xf8) != 0 &&
1666 ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
1667 #ifdef FD_DEBUG
1668 if (fdc_debug) {
1669 fdcstatus(fdc,
1670 bp->b_flags & B_READ
1671 ? "read failed" : "write failed");
1672 printf("blkno %d nblks %d nstat %d tc %d\n",
1673 fd->sc_blkno, fd->sc_nblks,
1674 fdc->sc_nstat, fdc->sc_tc);
1675 }
1676 #endif
1677 if (fdc->sc_nstat == 7 &&
1678 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1679
1680 /*
1681 * Silently retry overruns if no other
1682 * error bit is set. Adjust threshold.
1683 */
1684 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1685 if (thr < 15) {
1686 thr++;
1687 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1688 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1689 #ifdef FD_DEBUG
1690 if (fdc_debug)
1691 printf("fdc: %d -> threshold\n", thr);
1692 #endif
1693 fdconf(fdc);
1694 fdc->sc_overruns = 0;
1695 }
1696 if (++fdc->sc_overruns < 3) {
1697 fdc->sc_state = DOIO;
1698 goto loop;
1699 }
1700 }
1701 fdcretry(fdc);
1702 goto loop;
1703 }
1704 if (fdc->sc_errors) {
1705 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1706 fd->sc_skip / FD_BSIZE(fd),
1707 (struct disklabel *)NULL);
1708 printf("\n");
1709 fdc->sc_errors = 0;
1710 } else {
1711 if (--fdc->sc_overruns < -20) {
1712 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1713 if (thr > 0) {
1714 thr--;
1715 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1716 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1717 #ifdef FD_DEBUG
1718 if (fdc_debug)
1719 printf("fdc: %d -> threshold\n", thr);
1720 #endif
1721 fdconf(fdc);
1722 }
1723 fdc->sc_overruns = 0;
1724 }
1725 }
1726 fd->sc_blkno += fd->sc_nblks;
1727 fd->sc_skip += fd->sc_nbytes;
1728 fd->sc_bcount -= fd->sc_nbytes;
1729 if (finfo == NULL && fd->sc_bcount > 0) {
1730 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1731 goto doseek;
1732 }
1733 fdfinish(fd, bp);
1734 goto loop;
1735
1736 case DORESET:
1737 /* try a reset, keep motor on */
1738 fd_set_motor(fdc);
1739 delay(100);
1740 fdc->sc_nstat = 0;
1741 fdc->sc_itask = FDC_ITASK_SENSEI;
1742 fdc->sc_state = RESETCOMPLETE;
1743 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1744 fdc_reset(fdc);
1745 return (1); /* will return later */
1746
1747 case RESETCOMPLETE:
1748 callout_stop(&fdc->sc_timo_ch);
1749 fdconf(fdc);
1750
1751 /* FALLTHROUGH */
1752 case DORECAL:
1753 fdc->sc_state = RECALWAIT;
1754 fdc->sc_itask = FDC_ITASK_SENSEI;
1755 fdc->sc_nstat = 0;
1756 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1757 /* recalibrate function */
1758 FDC_WRFIFO(fdc, NE7CMD_RECAL);
1759 FDC_WRFIFO(fdc, fd->sc_drive);
1760 return (1); /* will return later */
1761
1762 case RECALWAIT:
1763 callout_stop(&fdc->sc_timo_ch);
1764 fdc->sc_state = RECALCOMPLETE;
1765 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1766 /* allow 1/30 second for heads to settle */
1767 callout_reset(&fdc->sc_intr_ch, hz / 30,
1768 fdcpseudointr, fdc);
1769 return (1); /* will return later */
1770 }
1771
1772 case RECALCOMPLETE:
1773 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1774 #ifdef FD_DEBUG
1775 if (fdc_debug)
1776 fdcstatus(fdc, "recalibrate failed");
1777 #endif
1778 fdcretry(fdc);
1779 goto loop;
1780 }
1781 fd->sc_cylin = 0;
1782 goto doseek;
1783
1784 case MOTORWAIT:
1785 if (fd->sc_flags & FD_MOTOR_WAIT)
1786 return (1); /* time's not up yet */
1787 goto doseek;
1788
1789 default:
1790 fdcstatus(fdc, "stray interrupt");
1791 return (1);
1792 }
1793 #ifdef DIAGNOSTIC
1794 panic("fdcintr: impossible");
1795 #endif
1796
1797 xxx:
1798 /*
1799 * We get here if the chip locks up in FDC_WRFIFO()
1800 * Cancel any operation and schedule a reset
1801 */
1802 callout_stop(&fdc->sc_timo_ch);
1803 fdcretry(fdc);
1804 (fdc)->sc_state = DORESET;
1805 goto loop;
1806
1807 #undef st0
1808 #undef st1
1809 #undef cyl
1810 }
1811
1812 void
1813 fdcretry(fdc)
1814 struct fdc_softc *fdc;
1815 {
1816 struct fd_softc *fd;
1817 struct buf *bp;
1818 int error = EIO;
1819
1820 fd = fdc->sc_drives.tqh_first;
1821 bp = BUFQ_FIRST(&fd->sc_q);
1822
1823 fdc->sc_overruns = 0;
1824 if (fd->sc_opts & FDOPT_NORETRY)
1825 goto fail;
1826
1827 switch (fdc->sc_errors) {
1828 case 0:
1829 if (fdc->sc_nstat == 7 &&
1830 (fdc->sc_status[0] & 0xd8) == 0x40 &&
1831 (fdc->sc_status[1] & 0x2) == 0x2) {
1832 printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
1833 error = EROFS;
1834 goto failsilent;
1835 }
1836 /* try again */
1837 fdc->sc_state =
1838 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1839 break;
1840
1841 case 1: case 2: case 3:
1842 /* didn't work; try recalibrating */
1843 fdc->sc_state = DORECAL;
1844 break;
1845
1846 case 4:
1847 if (fdc->sc_nstat == 7 &&
1848 fdc->sc_status[0] == 0 &&
1849 fdc->sc_status[1] == 0 &&
1850 fdc->sc_status[2] == 0) {
1851 /*
1852 * We've retried a few times and we've got
1853 * valid status and all three status bytes
1854 * are zero. Assume this condition is the
1855 * result of no disk loaded into the drive.
1856 */
1857 printf("%s: no medium?\n", fd->sc_dv.dv_xname);
1858 error = ENODEV;
1859 goto failsilent;
1860 }
1861
1862 /* still no go; reset the bastard */
1863 fdc->sc_state = DORESET;
1864 break;
1865
1866 default:
1867 fail:
1868 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1869 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1870 fd->sc_skip / FD_BSIZE(fd),
1871 (struct disklabel *)NULL);
1872 printf("\n");
1873 fdcstatus(fdc, "controller status");
1874 }
1875
1876 failsilent:
1877 bp->b_flags |= B_ERROR;
1878 bp->b_error = error;
1879 fdfinish(fd, bp);
1880 }
1881 fdc->sc_errors++;
1882 }
1883
1884 int
1885 fdsize(dev)
1886 dev_t dev;
1887 {
1888
1889 /* Swapping to floppies would not make sense. */
1890 return (-1);
1891 }
1892
1893 int
1894 fddump(dev, blkno, va, size)
1895 dev_t dev;
1896 daddr_t blkno;
1897 caddr_t va;
1898 size_t size;
1899 {
1900
1901 /* Not implemented. */
1902 return (EINVAL);
1903 }
1904
1905 int
1906 fdioctl(devvp, cmd, addr, flag, p)
1907 struct vnode *devvp;
1908 u_long cmd;
1909 caddr_t addr;
1910 int flag;
1911 struct proc *p;
1912 {
1913 struct fd_softc *fd;
1914 struct fdc_softc *fdc;
1915 struct fdformat_parms *form_parms;
1916 struct fdformat_cmd *form_cmd;
1917 struct ne7_fd_formb *fd_formb;
1918 int il[FD_MAX_NSEC + 1];
1919 int i, j;
1920 int error;
1921
1922 fd = vdev_privdata(devvp);
1923 fdc = (struct fdc_softc *)fd->sc_dv.dv_parent;
1924
1925 switch (cmd) {
1926 case DIOCGDINFO:
1927 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1928 return 0;
1929
1930 case DIOCWLABEL:
1931 if ((flag & FWRITE) == 0)
1932 return EBADF;
1933 /* XXX do something */
1934 return (0);
1935
1936 case DIOCWDINFO:
1937 if ((flag & FWRITE) == 0)
1938 return (EBADF);
1939
1940 error = setdisklabel(fd->sc_dk.dk_label,
1941 (struct disklabel *)addr, 0,
1942 fd->sc_dk.dk_cpulabel);
1943 if (error)
1944 return (error);
1945
1946 error = writedisklabel(devvp, fdstrategy,
1947 fd->sc_dk.dk_label,
1948 fd->sc_dk.dk_cpulabel);
1949 return (error);
1950
1951 case DIOCLOCK:
1952 /*
1953 * Nothing to do here, really.
1954 */
1955 return (0);
1956
1957 case DIOCEJECT:
1958 if (*(int *)addr == 0) {
1959 int part = DISKPART(vdev_rdev(devvp));
1960 /*
1961 * Don't force eject: check that we are the only
1962 * partition open. If so, unlock it.
1963 */
1964 if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
1965 fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
1966 fd->sc_dk.dk_openmask) {
1967 return (EBUSY);
1968 }
1969 }
1970 /* FALLTHROUGH */
1971 case ODIOCEJECT:
1972 fd_do_eject(fd);
1973 return (0);
1974
1975 case FDIOCGETFORMAT:
1976 form_parms = (struct fdformat_parms *)addr;
1977 form_parms->fdformat_version = FDFORMAT_VERSION;
1978 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1979 form_parms->ncyl = fd->sc_type->cylinders;
1980 form_parms->nspt = fd->sc_type->sectrac;
1981 form_parms->ntrk = fd->sc_type->heads;
1982 form_parms->stepspercyl = fd->sc_type->step;
1983 form_parms->gaplen = fd->sc_type->gap2;
1984 form_parms->fillbyte = fd->sc_type->fillbyte;
1985 form_parms->interleave = fd->sc_type->interleave;
1986 switch (fd->sc_type->rate) {
1987 case FDC_500KBPS:
1988 form_parms->xfer_rate = 500 * 1024;
1989 break;
1990 case FDC_300KBPS:
1991 form_parms->xfer_rate = 300 * 1024;
1992 break;
1993 case FDC_250KBPS:
1994 form_parms->xfer_rate = 250 * 1024;
1995 break;
1996 default:
1997 return (EINVAL);
1998 }
1999 return (0);
2000
2001 case FDIOCSETFORMAT:
2002 if ((flag & FWRITE) == 0)
2003 return (EBADF); /* must be opened for writing */
2004
2005 form_parms = (struct fdformat_parms *)addr;
2006 if (form_parms->fdformat_version != FDFORMAT_VERSION)
2007 return (EINVAL);/* wrong version of formatting prog */
2008
2009 i = form_parms->nbps >> 7;
2010 if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
2011 i & ~(1 << (ffs(i)-1)))
2012 /* not a power-of-two multiple of 128 */
2013 return (EINVAL);
2014
2015 switch (form_parms->xfer_rate) {
2016 case 500 * 1024:
2017 fd->sc_type->rate = FDC_500KBPS;
2018 break;
2019 case 300 * 1024:
2020 fd->sc_type->rate = FDC_300KBPS;
2021 break;
2022 case 250 * 1024:
2023 fd->sc_type->rate = FDC_250KBPS;
2024 break;
2025 default:
2026 return (EINVAL);
2027 }
2028
2029 if (form_parms->nspt > FD_MAX_NSEC ||
2030 form_parms->fillbyte > 0xff ||
2031 form_parms->interleave > 0xff)
2032 return EINVAL;
2033 fd->sc_type->sectrac = form_parms->nspt;
2034 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
2035 return EINVAL;
2036 fd->sc_type->heads = form_parms->ntrk;
2037 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
2038 fd->sc_type->secsize = ffs(i)-1;
2039 fd->sc_type->gap2 = form_parms->gaplen;
2040 fd->sc_type->cylinders = form_parms->ncyl;
2041 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
2042 form_parms->nbps / DEV_BSIZE;
2043 fd->sc_type->step = form_parms->stepspercyl;
2044 fd->sc_type->fillbyte = form_parms->fillbyte;
2045 fd->sc_type->interleave = form_parms->interleave;
2046 return (0);
2047
2048 case FDIOCFORMAT_TRACK:
2049 if((flag & FWRITE) == 0)
2050 /* must be opened for writing */
2051 return (EBADF);
2052 form_cmd = (struct fdformat_cmd *)addr;
2053 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
2054 /* wrong version of formatting prog */
2055 return (EINVAL);
2056
2057 if (form_cmd->head >= fd->sc_type->heads ||
2058 form_cmd->cylinder >= fd->sc_type->cylinders) {
2059 return (EINVAL);
2060 }
2061
2062 fd_formb = malloc(sizeof(struct ne7_fd_formb),
2063 M_TEMP, M_NOWAIT);
2064 if (fd_formb == 0)
2065 return (ENOMEM);
2066
2067 fd_formb->head = form_cmd->head;
2068 fd_formb->cyl = form_cmd->cylinder;
2069 fd_formb->transfer_rate = fd->sc_type->rate;
2070 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
2071 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
2072 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
2073 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
2074
2075 bzero(il, sizeof il);
2076 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
2077 while (il[(j%fd_formb->fd_formb_nsecs) + 1])
2078 j++;
2079 il[(j%fd_formb->fd_formb_nsecs) + 1] = i;
2080 j += fd->sc_type->interleave;
2081 }
2082 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
2083 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
2084 fd_formb->fd_formb_headno(i) = form_cmd->head;
2085 fd_formb->fd_formb_secno(i) = il[i+1];
2086 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
2087 }
2088
2089 error = fdformat(devvp, fd_formb, p);
2090 free(fd_formb, M_TEMP);
2091 return error;
2092
2093 case FDIOCGETOPTS: /* get drive options */
2094 *(int *)addr = fd->sc_opts;
2095 return (0);
2096
2097 case FDIOCSETOPTS: /* set drive options */
2098 fd->sc_opts = *(int *)addr;
2099 return (0);
2100
2101 #ifdef FD_DEBUG
2102 case _IO('f', 100):
2103 fdc_wrfifo(fdc, NE7CMD_DUMPREG);
2104 fdcresult(fdc);
2105 printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
2106 for (i = 0; i < fdc->sc_nstat; i++)
2107 printf(" 0x%x", fdc->sc_status[i]);
2108 printf(">\n");
2109 return (0);
2110
2111 case _IOW('f', 101, int):
2112 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
2113 fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
2114 fdconf(fdc);
2115 return (0);
2116
2117 case _IO('f', 102):
2118 fdc_wrfifo(fdc, NE7CMD_SENSEI);
2119 fdcresult(fdc);
2120 printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
2121 for (i=0; i< fdc->sc_nstat; i++)
2122 printf(" 0x%x", fdc->sc_status[i]);
2123 printf(">\n");
2124 return (0);
2125 #endif
2126 default:
2127 return (ENOTTY);
2128 }
2129
2130 #ifdef DIAGNOSTIC
2131 panic("fdioctl: impossible");
2132 #endif
2133 }
2134
2135 int
2136 fdformat(devvp, finfo, p)
2137 struct vnode *devvp;
2138 struct ne7_fd_formb *finfo;
2139 struct proc *p;
2140 {
2141 int rv = 0, s;
2142 struct fd_softc *fd = vdev_privdata(devvp);
2143 struct fd_type *type = fd->sc_type;
2144 struct buf *bp;
2145
2146 /* set up a buffer header for fdstrategy() */
2147 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
2148 if (bp == 0)
2149 return (ENOBUFS);
2150
2151 memset((void *)bp, 0, sizeof(struct buf));
2152 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
2153 bp->b_proc = p;
2154 bp->b_devvp = devvp;
2155
2156 /*
2157 * Calculate a fake blkno, so fdstrategy() would initiate a
2158 * seek to the requested cylinder.
2159 */
2160 bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
2161 + finfo->head * type->sectrac) * FD_BSIZE(fd))
2162 / DEV_BSIZE;
2163
2164 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2165 bp->b_data = (caddr_t)finfo;
2166
2167 #ifdef FD_DEBUG
2168 if (fdc_debug) {
2169 int i;
2170
2171 printf("fdformat: blkno 0x%x count %ld\n",
2172 bp->b_blkno, bp->b_bcount);
2173
2174 printf("\tcyl:\t%d\n", finfo->cyl);
2175 printf("\thead:\t%d\n", finfo->head);
2176 printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
2177 printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
2178 printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
2179 printf("\ttrack data:");
2180 for (i = 0; i < finfo->fd_formb_nsecs; i++) {
2181 printf(" [c%d h%d s%d]",
2182 finfo->fd_formb_cylno(i),
2183 finfo->fd_formb_headno(i),
2184 finfo->fd_formb_secno(i) );
2185 if (finfo->fd_formb_secsize(i) != 2)
2186 printf("<sz:%d>", finfo->fd_formb_secsize(i));
2187 }
2188 printf("\n");
2189 }
2190 #endif
2191
2192 /* now do the format */
2193 fdstrategy(bp);
2194
2195 /* ...and wait for it to complete */
2196 s = splbio();
2197 while (!(bp->b_flags & B_DONE)) {
2198 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2199 if (rv == EWOULDBLOCK)
2200 break;
2201 }
2202 splx(s);
2203
2204 if (rv == EWOULDBLOCK) {
2205 /* timed out */
2206 rv = EIO;
2207 biodone(bp);
2208 }
2209 if (bp->b_flags & B_ERROR) {
2210 rv = bp->b_error;
2211 }
2212 free(bp, M_TEMP);
2213 return (rv);
2214 }
2215
2216 void
2217 fdgetdisklabel(devvp)
2218 struct vnode *devvp;
2219 {
2220 struct fd_softc *fd = vdev_privdata(devvp);
2221 struct disklabel *lp = fd->sc_dk.dk_label;
2222 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
2223 int i;
2224
2225 bzero(lp, sizeof(struct disklabel));
2226 bzero(lp, sizeof(struct cpu_disklabel));
2227
2228 lp->d_type = DTYPE_FLOPPY;
2229 lp->d_secsize = FD_BSIZE(fd);
2230 lp->d_secpercyl = fd->sc_type->seccyl;
2231 lp->d_nsectors = fd->sc_type->sectrac;
2232 lp->d_ncylinders = fd->sc_type->cylinders;
2233 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
2234 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
2235 lp->d_rpm = 3600; /* XXX like it matters... */
2236
2237 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
2238 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
2239 lp->d_interleave = 1;
2240
2241 lp->d_partitions[RAW_PART].p_offset = 0;
2242 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
2243 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2244 lp->d_npartitions = RAW_PART + 1;
2245
2246 lp->d_magic = DISKMAGIC;
2247 lp->d_magic2 = DISKMAGIC;
2248 lp->d_checksum = dkcksum(lp);
2249
2250 /*
2251 * Call the generic disklabel extraction routine. If there's
2252 * not a label there, fake it.
2253 */
2254 if (readdisklabel(devvp, fdstrategy, lp, clp) != NULL) {
2255 strncpy(lp->d_packname, "default label",
2256 sizeof(lp->d_packname));
2257 /*
2258 * Reset the partition info; it might have gotten
2259 * trashed in readdisklabel().
2260 *
2261 * XXX Why do we have to do this? readdisklabel()
2262 * should be safe...
2263 */
2264 for (i = 0; i < MAXPARTITIONS; ++i) {
2265 lp->d_partitions[i].p_offset = 0;
2266 if (i == RAW_PART) {
2267 lp->d_partitions[i].p_size =
2268 lp->d_secpercyl * lp->d_ncylinders;
2269 lp->d_partitions[i].p_fstype = FS_BSDFFS;
2270 } else {
2271 lp->d_partitions[i].p_size = 0;
2272 lp->d_partitions[i].p_fstype = FS_UNUSED;
2273 }
2274 }
2275 lp->d_npartitions = RAW_PART + 1;
2276 }
2277 }
2278
2279 void
2280 fd_do_eject(fd)
2281 struct fd_softc *fd;
2282 {
2283 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
2284
2285 if (CPU_ISSUN4C) {
2286 auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
2287 delay(10);
2288 auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
2289 return;
2290 }
2291 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
2292 bus_space_tag_t t = fdc->sc_bustag;
2293 bus_space_handle_t h = fdc->sc_handle;
2294 u_int8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
2295
2296 bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
2297 delay(10);
2298 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
2299 return;
2300 }
2301 }
2302
2303 #ifdef MEMORY_DISK_HOOKS
2304 int fd_read_md_image __P((size_t *, caddr_t *));
2305 #endif
2306
2307 /* ARGSUSED */
2308 void
2309 fd_mountroot_hook(dev)
2310 struct device *dev;
2311 {
2312 int c;
2313
2314 fd_do_eject((struct fd_softc *)dev);
2315 printf("Insert filesystem floppy and press return.");
2316 for (;;) {
2317 c = cngetc();
2318 if ((c == '\r') || (c == '\n')) {
2319 printf("\n");
2320 break;
2321 }
2322 }
2323 }
2324
2325 #ifdef MEMORY_DISK_HOOKS
2326
2327 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
2328
2329 int
2330 fd_read_md_image(sizep, addrp)
2331 size_t *sizep;
2332 caddr_t *addrp;
2333 {
2334 struct buf buf, *bp = &buf;
2335 off_t offset;
2336 caddr_t addr;
2337 struct vnode *devvp;
2338
2339 if (cdevvp(makedev(54,0), &devvp) != 0) /* XXX */
2340 panic("can't create md vnode");
2341
2342 MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
2343 *addrp = addr;
2344
2345 if (fdopen(devvp, 0, S_IFCHR, NULL))
2346 panic("fd: mountroot: fdopen");
2347
2348 offset = 0;
2349
2350 for (;;) {
2351 bp->b_devvp = devvp;
2352 bp->b_error = 0;
2353 bp->b_resid = 0;
2354 bp->b_proc = NULL;
2355 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
2356 bp->b_blkno = btodb(offset);
2357 bp->b_bcount = DEV_BSIZE;
2358 bp->b_data = addr;
2359 fdstrategy(bp);
2360 while ((bp->b_flags & B_DONE) == 0) {
2361 tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
2362 }
2363 if (bp->b_error)
2364 panic("fd: mountroot: fdread error %d", bp->b_error);
2365
2366 if (bp->b_resid != 0)
2367 break;
2368
2369 addr += DEV_BSIZE;
2370 offset += DEV_BSIZE;
2371 if (offset + DEV_BSIZE > FDMICROROOTSIZE)
2372 break;
2373 }
2374 (void)fdclose(devvp, 0, S_IFCHR, NULL);
2375 vrele(devvp);
2376 *sizep = offset;
2377 fd_do_eject(fd_cd.cd_devs[0]);
2378 return (0);
2379 }
2380 #endif
2381