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