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