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