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