fd.c revision 1.141.12.1 1 /* $NetBSD: fd.c,v 1.141.12.1 2010/04/21 00:33:47 matt 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.141.12.1 2010/04/21 00:33:47 matt 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 = device_private(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 = device_private(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,
643 #ifdef notyet /* XXX bsd_fdintr.s needs to be fixed for MI softint(9) */
644 fdchwintr
645 #else
646 NULL
647 #endif
648 ) == NULL) {
649 printf("\n%s: cannot register interrupt handler\n",
650 fdc->sc_dev.dv_xname);
651 return (-1);
652 }
653
654 fdc->sc_sicookie = softint_establish(SOFTINT_BIO, fdcswintr, fdc);
655 if (fdc->sc_sicookie == NULL) {
656 printf("\n%s: cannot register soft interrupt handler\n",
657 fdc->sc_dev.dv_xname);
658 return (-1);
659 }
660 printf(" softpri %d: chip 8207%c\n", IPL_SOFTFDC, code);
661
662 evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
663 fdc->sc_dev.dv_xname, "intr");
664
665 /* physical limit: four drives per controller. */
666 drive_attached = 0;
667 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
668 fa.fa_deftype = NULL; /* unknown */
669 fa.fa_deftype = &fd_types[0]; /* XXX */
670 if (config_found(&fdc->sc_dev, (void *)&fa, fdprint) != NULL)
671 drive_attached = 1;
672 }
673
674 if (drive_attached == 0) {
675 /* XXX - dis-establish interrupts here */
676 /* return (-1); */
677 }
678
679 return (0);
680 }
681
682 int
683 fdmatch(struct device *parent, struct cfdata *match, void *aux)
684 {
685 struct fdc_softc *fdc = device_private(parent);
686 bus_space_tag_t t = fdc->sc_bustag;
687 bus_space_handle_t h = fdc->sc_handle;
688 struct fdc_attach_args *fa = aux;
689 int drive = fa->fa_drive;
690 int n, ok;
691
692 if (drive > 0)
693 /* XXX - for now, punt on more than one drive */
694 return (0);
695
696 if ((fdc->sc_flags & FDC_82077) != 0) {
697 /* select drive and turn on motor */
698 bus_space_write_1(t, h, fdc->sc_reg_dor,
699 drive | FDO_FRST | FDO_MOEN(drive));
700 /* wait for motor to spin up */
701 delay(250000);
702 } else {
703 auxregbisc(AUXIO4C_FDS, 0);
704 }
705 fdc->sc_nstat = 0;
706 fdc_wrfifo(fdc, NE7CMD_RECAL);
707 fdc_wrfifo(fdc, drive);
708
709 /* Wait for recalibration to complete */
710 for (n = 0; n < 10000; n++) {
711 uint8_t v;
712
713 delay(1000);
714 v = bus_space_read_1(t, h, fdc->sc_reg_msr);
715 if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
716 /* wait a bit longer till device *really* is ready */
717 delay(100000);
718 if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
719 break;
720 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
721 /*
722 * Got `invalid command'; we interpret it
723 * to mean that the re-calibrate hasn't in
724 * fact finished yet
725 */
726 continue;
727 break;
728 }
729 }
730 n = fdc->sc_nstat;
731 #ifdef FD_DEBUG
732 if (fdc_debug) {
733 int i;
734 printf("fdprobe: %d stati:", n);
735 for (i = 0; i < n; i++)
736 printf(" 0x%x", fdc->sc_status[i]);
737 printf("\n");
738 }
739 #endif
740 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
741
742 /* turn off motor */
743 if ((fdc->sc_flags & FDC_82077) != 0) {
744 /* deselect drive and turn motor off */
745 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
746 } else {
747 auxregbisc(0, AUXIO4C_FDS);
748 }
749
750 return (ok);
751 }
752
753 /*
754 * Controller is working, and drive responded. Attach it.
755 */
756 void
757 fdattach(struct device *parent, struct device *self, void *aux)
758 {
759 struct fdc_softc *fdc = device_private(parent);
760 struct fd_softc *fd = device_private(self);
761 struct fdc_attach_args *fa = aux;
762 struct fd_type *type = fa->fa_deftype;
763 int drive = fa->fa_drive;
764
765 callout_init(&fd->sc_motoron_ch, 0);
766 callout_init(&fd->sc_motoroff_ch, 0);
767
768 /* XXX Allow `flags' to override device type? */
769
770 if (type)
771 printf(": %s %d cyl, %d head, %d sec\n", type->name,
772 type->cylinders, type->heads, type->sectrac);
773 else
774 printf(": density unknown\n");
775
776 bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
777 fd->sc_cylin = -1;
778 fd->sc_drive = drive;
779 fd->sc_deftype = type;
780 fdc->sc_fd[drive] = fd;
781
782 fdc_wrfifo(fdc, NE7CMD_SPECIFY);
783 fdc_wrfifo(fdc, type->steprate);
784 /* XXX head load time == 6ms */
785 fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
786
787 /*
788 * Initialize and attach the disk structure.
789 */
790 disk_init(&fd->sc_dk, fd->sc_dv.dv_xname, &fddkdriver);
791 disk_attach(&fd->sc_dk);
792
793 /*
794 * Establish a mountroot_hook anyway in case we booted
795 * with RB_ASKNAME and get selected as the boot device.
796 */
797 mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
798
799 /* Make sure the drive motor gets turned off at shutdown time. */
800 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
801 }
802
803 inline struct fd_type *
804 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
805 {
806 int type = FDTYPE(dev);
807
808 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
809 return (NULL);
810 return (type ? &fd_types[type - 1] : fd->sc_deftype);
811 }
812
813 void
814 fdstrategy(struct buf *bp)
815 {
816 struct fd_softc *fd;
817 int unit = FDUNIT(bp->b_dev);
818 int sz;
819 int s;
820
821 /* Valid unit, controller, and request? */
822 if ((fd = device_lookup_private(&fd_cd, unit)) == 0 ||
823 bp->b_blkno < 0 ||
824 (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
825 (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
826 (bp->b_flags & B_FORMAT) == 0)) {
827 bp->b_error = EINVAL;
828 goto done;
829 }
830
831 /* If it's a null transfer, return immediately. */
832 if (bp->b_bcount == 0)
833 goto done;
834
835 sz = howmany(bp->b_bcount, DEV_BSIZE);
836
837 if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
838 sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
839 - bp->b_blkno;
840 if (sz == 0) {
841 /* If exactly at end of disk, return EOF. */
842 bp->b_resid = bp->b_bcount;
843 goto done;
844 }
845 if (sz < 0) {
846 /* If past end of disk, return EINVAL. */
847 bp->b_error = EINVAL;
848 goto done;
849 }
850 /* Otherwise, truncate request. */
851 bp->b_bcount = sz << DEV_BSHIFT;
852 }
853
854 bp->b_rawblkno = bp->b_blkno;
855 bp->b_cylinder = (bp->b_blkno * DEV_BSIZE) /
856 (FD_BSIZE(fd) * fd->sc_type->seccyl);
857
858 #ifdef FD_DEBUG
859 if (fdc_debug > 1)
860 printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld cylin %d\n",
861 (long long)bp->b_blkno, bp->b_bcount,
862 (long long)fd->sc_blkno, bp->b_cylinder);
863 #endif
864
865 /* Queue transfer on drive, activate drive and controller if idle. */
866 s = splbio();
867 BUFQ_PUT(fd->sc_q, bp);
868 callout_stop(&fd->sc_motoroff_ch); /* a good idea */
869 if (fd->sc_active == 0)
870 fdstart(fd);
871 #ifdef DIAGNOSTIC
872 else {
873 struct fdc_softc *fdc = (void *)device_parent(&fd->sc_dv);
874 if (fdc->sc_state == DEVIDLE) {
875 printf("fdstrategy: controller inactive\n");
876 fdcstart(fdc);
877 }
878 }
879 #endif
880 splx(s);
881 return;
882
883 done:
884 /* Toss transfer; we're done early. */
885 biodone(bp);
886 }
887
888 void
889 fdstart(struct fd_softc *fd)
890 {
891 struct fdc_softc *fdc = device_private(device_parent(&fd->sc_dv));
892 int active = fdc->sc_drives.tqh_first != 0;
893
894 /* Link into controller queue. */
895 fd->sc_active = 1;
896 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
897
898 /* If controller not already active, start it. */
899 if (!active)
900 fdcstart(fdc);
901 }
902
903 void
904 fdfinish(struct fd_softc *fd, struct buf *bp)
905 {
906 struct fdc_softc *fdc = device_private(device_parent(&fd->sc_dv));
907
908 /*
909 * Move this drive to the end of the queue to give others a `fair'
910 * chance. We only force a switch if N operations are completed while
911 * another drive is waiting to be serviced, since there is a long motor
912 * startup delay whenever we switch.
913 */
914 (void)BUFQ_GET(fd->sc_q);
915 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
916 fd->sc_ops = 0;
917 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
918 if (BUFQ_PEEK(fd->sc_q) != NULL) {
919 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
920 } else
921 fd->sc_active = 0;
922 }
923 bp->b_resid = fd->sc_bcount;
924 fd->sc_skip = 0;
925
926 biodone(bp);
927 /* turn off motor 5s from now */
928 callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
929 fdc->sc_state = DEVIDLE;
930 }
931
932 void
933 fdc_reset(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(struct fdc_softc *fdc)
959 {
960 struct fd_softc *fd;
961 u_char status;
962 int n;
963
964 if ((fdc->sc_flags & FDC_82077) != 0) {
965 status = FDO_FRST | FDO_FDMAEN;
966 if ((fd = fdc->sc_drives.tqh_first) != NULL)
967 status |= fd->sc_drive;
968
969 for (n = 0; n < 4; n++)
970 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
971 status |= FDO_MOEN(n);
972 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
973 fdc->sc_reg_dor, status);
974 } else {
975
976 for (n = 0; n < 4; n++) {
977 if ((fd = fdc->sc_fd[n]) != NULL &&
978 (fd->sc_flags & FD_MOTOR) != 0) {
979 auxregbisc(AUXIO4C_FDS, 0);
980 return;
981 }
982 }
983 auxregbisc(0, AUXIO4C_FDS);
984 }
985 }
986
987 void
988 fd_motor_off(void *arg)
989 {
990 struct fd_softc *fd = arg;
991 int s;
992
993 s = splbio();
994 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
995 fd_set_motor((struct fdc_softc *)device_parent(&fd->sc_dv));
996 splx(s);
997 }
998
999 void
1000 fd_motor_on(void *arg)
1001 {
1002 struct fd_softc *fd = arg;
1003 struct fdc_softc *fdc = device_private(device_parent(&fd->sc_dv));
1004 int s;
1005
1006 s = splbio();
1007 fd->sc_flags &= ~FD_MOTOR_WAIT;
1008 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
1009 (void) fdcstate(fdc);
1010 splx(s);
1011 }
1012
1013 /*
1014 * Get status bytes off the FDC after a command has finished
1015 * Returns the number of status bytes read; -1 on error.
1016 * The return value is also stored in `sc_nstat'.
1017 */
1018 int
1019 fdcresult(struct fdc_softc *fdc)
1020 {
1021 bus_space_tag_t t = fdc->sc_bustag;
1022 bus_space_handle_t h = fdc->sc_handle;
1023 int j, n = 0;
1024
1025 for (j = 10000; j; j--) {
1026 uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1027 v &= (NE7_DIO | NE7_RQM | NE7_CB);
1028 if (v == NE7_RQM)
1029 return (fdc->sc_nstat = n);
1030 if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
1031 if (n >= sizeof(fdc->sc_status)) {
1032 log(LOG_ERR, "fdcresult: overrun\n");
1033 return (-1);
1034 }
1035 fdc->sc_status[n++] =
1036 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1037 } else
1038 delay(1);
1039 }
1040
1041 log(LOG_ERR, "fdcresult: timeout\n");
1042 return (fdc->sc_nstat = -1);
1043 }
1044
1045 /*
1046 * Write a command byte to the FDC.
1047 * Returns 0 on success; -1 on failure (i.e. timeout)
1048 */
1049 int
1050 fdc_wrfifo(struct fdc_softc *fdc, uint8_t x)
1051 {
1052 bus_space_tag_t t = fdc->sc_bustag;
1053 bus_space_handle_t h = fdc->sc_handle;
1054 int i;
1055
1056 for (i = 100000; i-- > 0;) {
1057 uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
1058 if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
1059 /* The chip is ready */
1060 bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
1061 return (0);
1062 }
1063 delay(1);
1064 }
1065 return (-1);
1066 }
1067
1068 int
1069 fdc_diskchange(struct fdc_softc *fdc)
1070 {
1071
1072 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
1073 bus_space_tag_t t = fdc->sc_bustag;
1074 bus_space_handle_t h = fdc->sc_handle;
1075 uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_dir);
1076 return ((v & FDI_DCHG) != 0);
1077 } else if (CPU_ISSUN4C) {
1078 return ((*AUXIO4C_REG & AUXIO4C_FDC) != 0);
1079 }
1080 return (0);
1081 }
1082
1083 int
1084 fdopen(dev_t dev, int flags, int fmt, struct lwp *l)
1085 {
1086 int unit, pmask;
1087 struct fd_softc *fd;
1088 struct fd_type *type;
1089
1090 unit = FDUNIT(dev);
1091 fd = device_lookup_private(&fd_cd, unit);
1092 if (fd == NULL)
1093 return (ENXIO);
1094 type = fd_dev_to_type(fd, dev);
1095 if (type == NULL)
1096 return (ENXIO);
1097
1098 if ((fd->sc_flags & FD_OPEN) != 0 &&
1099 fd->sc_type != type)
1100 return (EBUSY);
1101
1102 fd->sc_type = type;
1103 fd->sc_cylin = -1;
1104 fd->sc_flags |= FD_OPEN;
1105
1106 /*
1107 * Only update the disklabel if we're not open anywhere else.
1108 */
1109 if (fd->sc_dk.dk_openmask == 0)
1110 fdgetdisklabel(dev);
1111
1112 pmask = (1 << DISKPART(dev));
1113
1114 switch (fmt) {
1115 case S_IFCHR:
1116 fd->sc_dk.dk_copenmask |= pmask;
1117 break;
1118
1119 case S_IFBLK:
1120 fd->sc_dk.dk_bopenmask |= pmask;
1121 break;
1122 }
1123 fd->sc_dk.dk_openmask =
1124 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1125
1126 return (0);
1127 }
1128
1129 int
1130 fdclose(dev_t dev, int flags, int fmt, struct lwp *l)
1131 {
1132 struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
1133 int pmask = (1 << DISKPART(dev));
1134
1135 fd->sc_flags &= ~FD_OPEN;
1136 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
1137
1138 switch (fmt) {
1139 case S_IFCHR:
1140 fd->sc_dk.dk_copenmask &= ~pmask;
1141 break;
1142
1143 case S_IFBLK:
1144 fd->sc_dk.dk_bopenmask &= ~pmask;
1145 break;
1146 }
1147 fd->sc_dk.dk_openmask =
1148 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1149
1150 return (0);
1151 }
1152
1153 int
1154 fdread(dev_t dev, struct uio *uio, int flag)
1155 {
1156
1157 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
1158 }
1159
1160 int
1161 fdwrite(dev_t dev, struct uio *uio, int flag)
1162 {
1163
1164 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
1165 }
1166
1167 void
1168 fdcstart(struct fdc_softc *fdc)
1169 {
1170
1171 #ifdef DIAGNOSTIC
1172 /* only got here if controller's drive queue was inactive; should
1173 be in idle state */
1174 if (fdc->sc_state != DEVIDLE) {
1175 printf("fdcstart: not idle\n");
1176 return;
1177 }
1178 #endif
1179 (void) fdcstate(fdc);
1180 }
1181
1182 void
1183 fdcstatus(struct fdc_softc *fdc, const char *s)
1184 {
1185 struct fd_softc *fd = fdc->sc_drives.tqh_first;
1186 int n;
1187 char bits[64];
1188
1189 /* Just print last status */
1190 n = fdc->sc_nstat;
1191
1192 #if 0
1193 /*
1194 * A 82072 seems to return <invalid command> on
1195 * gratuitous Sense Interrupt commands.
1196 */
1197 if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
1198 fdc_wrfifo(fdc, NE7CMD_SENSEI);
1199 (void) fdcresult(fdc);
1200 n = 2;
1201 }
1202 #endif
1203
1204 printf("%s: %s: state %d",
1205 fd ? fd->sc_dv.dv_xname : "fdc", s, fdc->sc_state);
1206
1207 switch (n) {
1208 case 0:
1209 printf("\n");
1210 break;
1211 case 2:
1212 printf(" (st0 %s cyl %d)\n",
1213 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
1214 bits, sizeof(bits)), fdc->sc_status[1]);
1215 break;
1216 case 7:
1217 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1218 NE7_ST0BITS, bits, sizeof(bits)));
1219 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1220 NE7_ST1BITS, bits, sizeof(bits)));
1221 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1222 NE7_ST2BITS, bits, sizeof(bits)));
1223 printf(" cyl %d head %d sec %d)\n",
1224 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1225 break;
1226 #ifdef DIAGNOSTIC
1227 default:
1228 printf(" fdcstatus: weird size: %d\n", n);
1229 break;
1230 #endif
1231 }
1232 }
1233
1234 void
1235 fdctimeout(void *arg)
1236 {
1237 struct fdc_softc *fdc = arg;
1238 struct fd_softc *fd;
1239 int s;
1240
1241 s = splbio();
1242 fd = fdc->sc_drives.tqh_first;
1243 if (fd == NULL) {
1244 printf("%s: timeout but no I/O pending: state %d, istatus=%d\n",
1245 fdc->sc_dev.dv_xname,
1246 fdc->sc_state, fdc->sc_istatus);
1247 fdc->sc_state = DEVIDLE;
1248 goto out;
1249 }
1250
1251 if (BUFQ_PEEK(fd->sc_q) != NULL)
1252 fdc->sc_state++;
1253 else
1254 fdc->sc_state = DEVIDLE;
1255
1256 (void) fdcstate(fdc);
1257 out:
1258 splx(s);
1259
1260 }
1261
1262 void
1263 fdcpseudointr(void *arg)
1264 {
1265 struct fdc_softc *fdc = arg;
1266 int s;
1267
1268 /* Just ensure it has the right spl. */
1269 s = splbio();
1270 (void) fdcstate(fdc);
1271 splx(s);
1272 }
1273
1274
1275 /*
1276 * hardware interrupt entry point: used only if no `fast trap' * (in-window)
1277 * handler is available. Unfortunately, we have no reliable way to
1278 * determine that the interrupt really came from the floppy controller;
1279 * just hope that the other devices that share this interrupt level
1280 * can do better..
1281 */
1282 int
1283 fdc_c_hwintr(void *arg)
1284 {
1285 struct fdc_softc *fdc = arg;
1286 bus_space_tag_t t = fdc->sc_bustag;
1287 bus_space_handle_t h = fdc->sc_handle;
1288
1289 switch (fdc->sc_itask) {
1290 case FDC_ITASK_NONE:
1291 return (0);
1292 case FDC_ITASK_SENSEI:
1293 if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
1294 fdc->sc_istatus = FDC_ISTATUS_ERROR;
1295 else
1296 fdc->sc_istatus = FDC_ISTATUS_DONE;
1297 softint_schedule(fdc->sc_sicookie);
1298 return (1);
1299 case FDC_ITASK_RESULT:
1300 if (fdcresult(fdc) == -1)
1301 fdc->sc_istatus = FDC_ISTATUS_ERROR;
1302 else
1303 fdc->sc_istatus = FDC_ISTATUS_DONE;
1304 softint_schedule(fdc->sc_sicookie);
1305 return (1);
1306 case FDC_ITASK_DMA:
1307 /* Proceed with pseudo-DMA below */
1308 break;
1309 default:
1310 printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
1311 fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
1312 softint_schedule(fdc->sc_sicookie);
1313 return (1);
1314 }
1315
1316 /*
1317 * Pseudo DMA in progress
1318 */
1319 for (;;) {
1320 uint8_t msr;
1321
1322 msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
1323
1324 if ((msr & NE7_RQM) == 0)
1325 /* That's all this round */
1326 break;
1327
1328 if ((msr & NE7_NDM) == 0) {
1329 /* Execution phase finished, get result. */
1330 fdcresult(fdc);
1331 fdc->sc_istatus = FDC_ISTATUS_DONE;
1332 softint_schedule(fdc->sc_sicookie);
1333 break;
1334 }
1335
1336 if (fdc->sc_tc == 0)
1337 /* For some reason the controller wants to transfer
1338 more data then what we want to transfer. */
1339 panic("fdc: overrun");
1340
1341 /* Another byte can be transferred */
1342 if ((msr & NE7_DIO) != 0)
1343 *fdc->sc_data =
1344 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1345 else
1346 bus_space_write_1(t, h, fdc->sc_reg_fifo,
1347 *fdc->sc_data);
1348
1349 fdc->sc_data++;
1350 if (--fdc->sc_tc == 0) {
1351 FTC_FLIP;
1352 break;
1353 }
1354 }
1355 return (1);
1356 }
1357
1358 void
1359 fdcswintr(void *arg)
1360 {
1361 struct fdc_softc *fdc = arg;
1362
1363 if (fdc->sc_istatus == FDC_ISTATUS_NONE)
1364 /* This (software) interrupt is not for us */
1365 return;
1366
1367 switch (fdc->sc_istatus) {
1368 case FDC_ISTATUS_ERROR:
1369 printf("fdc: ierror status: state %d\n", fdc->sc_state);
1370 break;
1371 case FDC_ISTATUS_SPURIOUS:
1372 printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
1373 break;
1374 }
1375
1376 fdcstate(fdc);
1377 return;
1378 }
1379
1380 int
1381 fdcstate(struct fdc_softc *fdc)
1382 {
1383
1384 #define st0 fdc->sc_status[0]
1385 #define st1 fdc->sc_status[1]
1386 #define cyl fdc->sc_status[1]
1387 #define FDC_WRFIFO(fdc, c) do { \
1388 if (fdc_wrfifo(fdc, (c))) { \
1389 goto xxx; \
1390 } \
1391 } while(0)
1392
1393 struct fd_softc *fd;
1394 struct buf *bp;
1395 int read, head, sec, nblks;
1396 struct fd_type *type;
1397 struct ne7_fd_formb *finfo = NULL;
1398
1399 if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
1400 /* Prevent loop if the reset sequence produces errors */
1401 if (fdc->sc_state != RESETCOMPLETE &&
1402 fdc->sc_state != RECALWAIT &&
1403 fdc->sc_state != RECALCOMPLETE)
1404 fdc->sc_state = DORESET;
1405 }
1406
1407 /* Clear I task/status field */
1408 fdc->sc_istatus = FDC_ISTATUS_NONE;
1409 fdc->sc_itask = FDC_ITASK_NONE;
1410
1411 loop:
1412 /* Is there a drive for the controller to do a transfer with? */
1413 fd = fdc->sc_drives.tqh_first;
1414 if (fd == NULL) {
1415 fdc->sc_state = DEVIDLE;
1416 return (0);
1417 }
1418
1419 /* Is there a transfer to this drive? If not, deactivate drive. */
1420 bp = BUFQ_PEEK(fd->sc_q);
1421 if (bp == NULL) {
1422 fd->sc_ops = 0;
1423 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1424 fd->sc_active = 0;
1425 goto loop;
1426 }
1427
1428 if (bp->b_flags & B_FORMAT)
1429 finfo = (struct ne7_fd_formb *)bp->b_data;
1430
1431 switch (fdc->sc_state) {
1432 case DEVIDLE:
1433 fdc->sc_errors = 0;
1434 fd->sc_skip = 0;
1435 fd->sc_bcount = bp->b_bcount;
1436 fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
1437 callout_stop(&fd->sc_motoroff_ch);
1438 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1439 fdc->sc_state = MOTORWAIT;
1440 return (1);
1441 }
1442 if ((fd->sc_flags & FD_MOTOR) == 0) {
1443 /* Turn on the motor, being careful about pairing. */
1444 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1445 if (ofd && ofd->sc_flags & FD_MOTOR) {
1446 callout_stop(&ofd->sc_motoroff_ch);
1447 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1448 }
1449 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1450 fd_set_motor(fdc);
1451 fdc->sc_state = MOTORWAIT;
1452 if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) { /*XXX*/
1453 /* Allow .25s for motor to stabilize. */
1454 callout_reset(&fd->sc_motoron_ch, hz / 4,
1455 fd_motor_on, fd);
1456 } else {
1457 fd->sc_flags &= ~FD_MOTOR_WAIT;
1458 goto loop;
1459 }
1460 return (1);
1461 }
1462 /* Make sure the right drive is selected. */
1463 fd_set_motor(fdc);
1464
1465 if (fdc_diskchange(fdc))
1466 goto dodskchg;
1467
1468 /*FALLTHROUGH*/
1469 case DOSEEK:
1470 doseek:
1471 if ((fdc->sc_flags & FDC_EIS) &&
1472 (bp->b_flags & B_FORMAT) == 0) {
1473 fd->sc_cylin = bp->b_cylinder;
1474 /* We use implied seek */
1475 goto doio;
1476 }
1477
1478 if (fd->sc_cylin == bp->b_cylinder)
1479 goto doio;
1480
1481 fd->sc_cylin = -1;
1482 fdc->sc_state = SEEKWAIT;
1483 fdc->sc_nstat = 0;
1484
1485 iostat_seek(fd->sc_dk.dk_stats);
1486
1487 disk_busy(&fd->sc_dk);
1488 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1489
1490 /* specify command */
1491 FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
1492 FDC_WRFIFO(fdc, fd->sc_type->steprate);
1493 /* XXX head load time == 6ms */
1494 FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
1495
1496 fdc->sc_itask = FDC_ITASK_SENSEI;
1497 /* seek function */
1498 FDC_WRFIFO(fdc, NE7CMD_SEEK);
1499 FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1500 FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
1501 return (1);
1502
1503 case DODSKCHG:
1504 dodskchg:
1505 /*
1506 * Disk change: force a seek operation by going to cyl 1
1507 * followed by a recalibrate.
1508 */
1509 disk_busy(&fd->sc_dk);
1510 callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1511 fd->sc_cylin = -1;
1512 fdc->sc_nstat = 0;
1513 fdc->sc_state = DSKCHGWAIT;
1514
1515 fdc->sc_itask = FDC_ITASK_SENSEI;
1516 /* seek function */
1517 FDC_WRFIFO(fdc, NE7CMD_SEEK);
1518 FDC_WRFIFO(fdc, fd->sc_drive); /* drive number */
1519 FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
1520 return (1);
1521
1522 case DSKCHGWAIT:
1523 callout_stop(&fdc->sc_timo_ch);
1524 disk_unbusy(&fd->sc_dk, 0, 0);
1525 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1526 cyl != 1 * fd->sc_type->step) {
1527 fdcstatus(fdc, "dskchg seek failed");
1528 fdc->sc_state = DORESET;
1529 } else
1530 fdc->sc_state = DORECAL;
1531
1532 if (fdc_diskchange(fdc)) {
1533 printf("%s: cannot clear disk change status\n",
1534 fdc->sc_dev.dv_xname);
1535 fdc->sc_state = DORESET;
1536 }
1537 goto loop;
1538
1539 case DOIO:
1540 doio:
1541 if (finfo != NULL)
1542 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1543 (char *)finfo;
1544 type = fd->sc_type;
1545 sec = fd->sc_blkno % type->seccyl;
1546 nblks = type->seccyl - sec;
1547 nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
1548 nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
1549 fd->sc_nblks = nblks;
1550 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
1551 head = sec / type->sectrac;
1552 sec -= head * type->sectrac;
1553 #ifdef DIAGNOSTIC
1554 {int block;
1555 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1556 if (block != fd->sc_blkno) {
1557 printf("fdcintr: block %d != blkno %d\n", block, (int)fd->sc_blkno);
1558 #ifdef DDB
1559 Debugger();
1560 #endif
1561 }}
1562 #endif
1563 read = bp->b_flags & B_READ;
1564
1565 /* Setup for pseudo DMA */
1566 fdc->sc_data = (char *)bp->b_data + fd->sc_skip;
1567 fdc->sc_tc = fd->sc_nbytes;
1568
1569 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1570 fdc->sc_reg_drs, type->rate);
1571 #ifdef FD_DEBUG
1572 if (fdc_debug > 1)
1573 printf("fdcstate: doio: %s drive %d "
1574 "track %d head %d sec %d nblks %d\n",
1575 finfo ? "format" :
1576 (read ? "read" : "write"),
1577 fd->sc_drive, fd->sc_cylin, head, sec, nblks);
1578 #endif
1579 fdc->sc_state = IOCOMPLETE;
1580 fdc->sc_itask = FDC_ITASK_DMA;
1581 fdc->sc_nstat = 0;
1582
1583 disk_busy(&fd->sc_dk);
1584
1585 /* allow 3 seconds for operation */
1586 callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
1587
1588 if (finfo != NULL) {
1589 /* formatting */
1590 FDC_WRFIFO(fdc, NE7CMD_FORMAT);
1591 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1592 FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
1593 FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
1594 FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
1595 FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
1596 } else {
1597 if (read)
1598 FDC_WRFIFO(fdc, NE7CMD_READ);
1599 else
1600 FDC_WRFIFO(fdc, NE7CMD_WRITE);
1601 FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
1602 FDC_WRFIFO(fdc, fd->sc_cylin); /*track*/
1603 FDC_WRFIFO(fdc, head);
1604 FDC_WRFIFO(fdc, sec + 1); /*sector+1*/
1605 FDC_WRFIFO(fdc, type->secsize);/*sector size*/
1606 FDC_WRFIFO(fdc, type->sectrac);/*secs/track*/
1607 FDC_WRFIFO(fdc, type->gap1); /*gap1 size*/
1608 FDC_WRFIFO(fdc, type->datalen);/*data length*/
1609 }
1610
1611 return (1); /* will return later */
1612
1613 case SEEKWAIT:
1614 callout_stop(&fdc->sc_timo_ch);
1615 fdc->sc_state = SEEKCOMPLETE;
1616 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1617 /* allow 1/50 second for heads to settle */
1618 callout_reset(&fdc->sc_intr_ch, hz / 50,
1619 fdcpseudointr, fdc);
1620 return (1); /* will return later */
1621 }
1622 /*FALLTHROUGH*/
1623 case SEEKCOMPLETE:
1624 /* no data on seek */
1625 disk_unbusy(&fd->sc_dk, 0, 0);
1626
1627 /* Make sure seek really happened. */
1628 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1629 cyl != bp->b_cylinder * fd->sc_type->step) {
1630 #ifdef FD_DEBUG
1631 if (fdc_debug)
1632 fdcstatus(fdc, "seek failed");
1633 #endif
1634 fdcretry(fdc);
1635 goto loop;
1636 }
1637 fd->sc_cylin = bp->b_cylinder;
1638 goto doio;
1639
1640 case IOTIMEDOUT:
1641 /*
1642 * Try to abort the I/O operation without resetting
1643 * the chip first. Poke TC and arrange to pick up
1644 * the timed out I/O command's status.
1645 */
1646 fdc->sc_itask = FDC_ITASK_RESULT;
1647 fdc->sc_state = IOCLEANUPWAIT;
1648 fdc->sc_nstat = 0;
1649 /* 1/10 second should be enough */
1650 callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
1651 FTC_FLIP;
1652 return (1);
1653
1654 case IOCLEANUPTIMEDOUT:
1655 case SEEKTIMEDOUT:
1656 case RECALTIMEDOUT:
1657 case RESETTIMEDOUT:
1658 case DSKCHGTIMEDOUT:
1659 fdcstatus(fdc, "timeout");
1660
1661 /* All other timeouts always roll through to a chip reset */
1662 fdcretry(fdc);
1663
1664 /* Force reset, no matter what fdcretry() says */
1665 fdc->sc_state = DORESET;
1666 goto loop;
1667
1668 case IOCLEANUPWAIT: /* IO FAILED, cleanup succeeded */
1669 callout_stop(&fdc->sc_timo_ch);
1670 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1671 (bp->b_flags & B_READ));
1672 fdcretry(fdc);
1673 goto loop;
1674
1675 case IOCOMPLETE: /* IO DONE, post-analyze */
1676 callout_stop(&fdc->sc_timo_ch);
1677
1678 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1679 (bp->b_flags & B_READ));
1680
1681 if (fdc->sc_nstat != 7 || st1 != 0 ||
1682 ((st0 & 0xf8) != 0 &&
1683 ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
1684 #ifdef FD_DEBUG
1685 if (fdc_debug) {
1686 fdcstatus(fdc,
1687 bp->b_flags & B_READ
1688 ? "read failed" : "write failed");
1689 printf("blkno %lld nblks %d nstat %d tc %d\n",
1690 (long long)fd->sc_blkno, fd->sc_nblks,
1691 fdc->sc_nstat, fdc->sc_tc);
1692 }
1693 #endif
1694 if (fdc->sc_nstat == 7 &&
1695 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1696
1697 /*
1698 * Silently retry overruns if no other
1699 * error bit is set. Adjust threshold.
1700 */
1701 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1702 if (thr < 15) {
1703 thr++;
1704 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1705 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1706 #ifdef FD_DEBUG
1707 if (fdc_debug)
1708 printf("fdc: %d -> threshold\n", thr);
1709 #endif
1710 fdconf(fdc);
1711 fdc->sc_overruns = 0;
1712 }
1713 if (++fdc->sc_overruns < 3) {
1714 fdc->sc_state = DOIO;
1715 goto loop;
1716 }
1717 }
1718 fdcretry(fdc);
1719 goto loop;
1720 }
1721 if (fdc->sc_errors) {
1722 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1723 fd->sc_skip / FD_BSIZE(fd),
1724 (struct disklabel *)NULL);
1725 printf("\n");
1726 fdc->sc_errors = 0;
1727 } else {
1728 if (--fdc->sc_overruns < -20) {
1729 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1730 if (thr > 0) {
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 }
1740 fdc->sc_overruns = 0;
1741 }
1742 }
1743 fd->sc_blkno += fd->sc_nblks;
1744 fd->sc_skip += fd->sc_nbytes;
1745 fd->sc_bcount -= fd->sc_nbytes;
1746 if (finfo == NULL && fd->sc_bcount > 0) {
1747 bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1748 goto doseek;
1749 }
1750 fdfinish(fd, bp);
1751 goto loop;
1752
1753 case DORESET:
1754 /* try a reset, keep motor on */
1755 fd_set_motor(fdc);
1756 delay(100);
1757 fdc->sc_nstat = 0;
1758 fdc->sc_itask = FDC_ITASK_SENSEI;
1759 fdc->sc_state = RESETCOMPLETE;
1760 callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1761 fdc_reset(fdc);
1762 return (1); /* will return later */
1763
1764 case RESETCOMPLETE:
1765 callout_stop(&fdc->sc_timo_ch);
1766 fdconf(fdc);
1767
1768 /* FALLTHROUGH */
1769 case DORECAL:
1770 fdc->sc_state = RECALWAIT;
1771 fdc->sc_itask = FDC_ITASK_SENSEI;
1772 fdc->sc_nstat = 0;
1773 callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1774 /* recalibrate function */
1775 FDC_WRFIFO(fdc, NE7CMD_RECAL);
1776 FDC_WRFIFO(fdc, fd->sc_drive);
1777 return (1); /* will return later */
1778
1779 case RECALWAIT:
1780 callout_stop(&fdc->sc_timo_ch);
1781 fdc->sc_state = RECALCOMPLETE;
1782 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1783 /* allow 1/30 second for heads to settle */
1784 callout_reset(&fdc->sc_intr_ch, hz / 30,
1785 fdcpseudointr, fdc);
1786 return (1); /* will return later */
1787 }
1788
1789 case RECALCOMPLETE:
1790 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1791 #ifdef FD_DEBUG
1792 if (fdc_debug)
1793 fdcstatus(fdc, "recalibrate failed");
1794 #endif
1795 fdcretry(fdc);
1796 goto loop;
1797 }
1798 fd->sc_cylin = 0;
1799 goto doseek;
1800
1801 case MOTORWAIT:
1802 if (fd->sc_flags & FD_MOTOR_WAIT)
1803 return (1); /* time's not up yet */
1804 goto doseek;
1805
1806 default:
1807 fdcstatus(fdc, "stray interrupt");
1808 return (1);
1809 }
1810 #ifdef DIAGNOSTIC
1811 panic("fdcintr: impossible");
1812 #endif
1813
1814 xxx:
1815 /*
1816 * We get here if the chip locks up in FDC_WRFIFO()
1817 * Cancel any operation and schedule a reset
1818 */
1819 callout_stop(&fdc->sc_timo_ch);
1820 fdcretry(fdc);
1821 (fdc)->sc_state = DORESET;
1822 goto loop;
1823
1824 #undef st0
1825 #undef st1
1826 #undef cyl
1827 }
1828
1829 void
1830 fdcretry(struct fdc_softc *fdc)
1831 {
1832 struct fd_softc *fd;
1833 struct buf *bp;
1834 int error = EIO;
1835
1836 fd = fdc->sc_drives.tqh_first;
1837 bp = BUFQ_PEEK(fd->sc_q);
1838
1839 fdc->sc_overruns = 0;
1840 if (fd->sc_opts & FDOPT_NORETRY)
1841 goto fail;
1842
1843 switch (fdc->sc_errors) {
1844 case 0:
1845 if (fdc->sc_nstat == 7 &&
1846 (fdc->sc_status[0] & 0xd8) == 0x40 &&
1847 (fdc->sc_status[1] & 0x2) == 0x2) {
1848 printf("%s: read-only medium\n", fd->sc_dv.dv_xname);
1849 error = EROFS;
1850 goto failsilent;
1851 }
1852 /* try again */
1853 fdc->sc_state =
1854 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1855 break;
1856
1857 case 1: case 2: case 3:
1858 /* didn't work; try recalibrating */
1859 fdc->sc_state = DORECAL;
1860 break;
1861
1862 case 4:
1863 if (fdc->sc_nstat == 7 &&
1864 fdc->sc_status[0] == 0 &&
1865 fdc->sc_status[1] == 0 &&
1866 fdc->sc_status[2] == 0) {
1867 /*
1868 * We've retried a few times and we've got
1869 * valid status and all three status bytes
1870 * are zero. Assume this condition is the
1871 * result of no disk loaded into the drive.
1872 */
1873 printf("%s: no medium?\n", fd->sc_dv.dv_xname);
1874 error = ENODEV;
1875 goto failsilent;
1876 }
1877
1878 /* still no go; reset the bastard */
1879 fdc->sc_state = DORESET;
1880 break;
1881
1882 default:
1883 fail:
1884 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1885 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1886 fd->sc_skip / FD_BSIZE(fd),
1887 (struct disklabel *)NULL);
1888 printf("\n");
1889 fdcstatus(fdc, "controller status");
1890 }
1891
1892 failsilent:
1893 bp->b_error = error;
1894 fdfinish(fd, bp);
1895 }
1896 fdc->sc_errors++;
1897 }
1898
1899 int
1900 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1901 {
1902 struct fd_softc *fd;
1903 struct fdc_softc *fdc;
1904 struct fdformat_parms *form_parms;
1905 struct fdformat_cmd *form_cmd;
1906 struct ne7_fd_formb *fd_formb;
1907 int il[FD_MAX_NSEC + 1];
1908 int unit;
1909 int i, j;
1910 int error;
1911
1912 unit = FDUNIT(dev);
1913 if (unit >= fd_cd.cd_ndevs)
1914 return (ENXIO);
1915
1916 fd = device_lookup_private(&fd_cd, FDUNIT(dev));
1917 fdc = device_private(device_parent(&fd->sc_dv));
1918
1919 switch (cmd) {
1920 case DIOCGDINFO:
1921 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1922 return 0;
1923
1924 case DIOCWLABEL:
1925 if ((flag & FWRITE) == 0)
1926 return EBADF;
1927 /* XXX do something */
1928 return (0);
1929
1930 case DIOCWDINFO:
1931 if ((flag & FWRITE) == 0)
1932 return (EBADF);
1933
1934 error = setdisklabel(fd->sc_dk.dk_label,
1935 (struct disklabel *)addr, 0,
1936 fd->sc_dk.dk_cpulabel);
1937 if (error)
1938 return (error);
1939
1940 error = writedisklabel(dev, fdstrategy,
1941 fd->sc_dk.dk_label,
1942 fd->sc_dk.dk_cpulabel);
1943 return (error);
1944
1945 case DIOCLOCK:
1946 /*
1947 * Nothing to do here, really.
1948 */
1949 return (0);
1950
1951 case DIOCEJECT:
1952 if (*(int *)addr == 0) {
1953 int part = DISKPART(dev);
1954 /*
1955 * Don't force eject: check that we are the only
1956 * partition open. If so, unlock it.
1957 */
1958 if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
1959 fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
1960 fd->sc_dk.dk_openmask) {
1961 return (EBUSY);
1962 }
1963 }
1964 /* FALLTHROUGH */
1965 case ODIOCEJECT:
1966 fd_do_eject(fd);
1967 return (0);
1968
1969 case FDIOCGETFORMAT:
1970 form_parms = (struct fdformat_parms *)addr;
1971 form_parms->fdformat_version = FDFORMAT_VERSION;
1972 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1973 form_parms->ncyl = fd->sc_type->cylinders;
1974 form_parms->nspt = fd->sc_type->sectrac;
1975 form_parms->ntrk = fd->sc_type->heads;
1976 form_parms->stepspercyl = fd->sc_type->step;
1977 form_parms->gaplen = fd->sc_type->gap2;
1978 form_parms->fillbyte = fd->sc_type->fillbyte;
1979 form_parms->interleave = fd->sc_type->interleave;
1980 switch (fd->sc_type->rate) {
1981 case FDC_500KBPS:
1982 form_parms->xfer_rate = 500 * 1024;
1983 break;
1984 case FDC_300KBPS:
1985 form_parms->xfer_rate = 300 * 1024;
1986 break;
1987 case FDC_250KBPS:
1988 form_parms->xfer_rate = 250 * 1024;
1989 break;
1990 default:
1991 return (EINVAL);
1992 }
1993 return (0);
1994
1995 case FDIOCSETFORMAT:
1996 if ((flag & FWRITE) == 0)
1997 return (EBADF); /* must be opened for writing */
1998
1999 form_parms = (struct fdformat_parms *)addr;
2000 if (form_parms->fdformat_version != FDFORMAT_VERSION)
2001 return (EINVAL);/* wrong version of formatting prog */
2002
2003 i = form_parms->nbps >> 7;
2004 if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
2005 i & ~(1 << (ffs(i)-1)))
2006 /* not a power-of-two multiple of 128 */
2007 return (EINVAL);
2008
2009 switch (form_parms->xfer_rate) {
2010 case 500 * 1024:
2011 fd->sc_type->rate = FDC_500KBPS;
2012 break;
2013 case 300 * 1024:
2014 fd->sc_type->rate = FDC_300KBPS;
2015 break;
2016 case 250 * 1024:
2017 fd->sc_type->rate = FDC_250KBPS;
2018 break;
2019 default:
2020 return (EINVAL);
2021 }
2022
2023 if (form_parms->nspt > FD_MAX_NSEC ||
2024 form_parms->fillbyte > 0xff ||
2025 form_parms->interleave > 0xff)
2026 return EINVAL;
2027 fd->sc_type->sectrac = form_parms->nspt;
2028 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
2029 return EINVAL;
2030 fd->sc_type->heads = form_parms->ntrk;
2031 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
2032 fd->sc_type->secsize = ffs(i)-1;
2033 fd->sc_type->gap2 = form_parms->gaplen;
2034 fd->sc_type->cylinders = form_parms->ncyl;
2035 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
2036 form_parms->nbps / DEV_BSIZE;
2037 fd->sc_type->step = form_parms->stepspercyl;
2038 fd->sc_type->fillbyte = form_parms->fillbyte;
2039 fd->sc_type->interleave = form_parms->interleave;
2040 return (0);
2041
2042 case FDIOCFORMAT_TRACK:
2043 if((flag & FWRITE) == 0)
2044 /* must be opened for writing */
2045 return (EBADF);
2046 form_cmd = (struct fdformat_cmd *)addr;
2047 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
2048 /* wrong version of formatting prog */
2049 return (EINVAL);
2050
2051 if (form_cmd->head >= fd->sc_type->heads ||
2052 form_cmd->cylinder >= fd->sc_type->cylinders) {
2053 return (EINVAL);
2054 }
2055
2056 fd_formb = malloc(sizeof(struct ne7_fd_formb),
2057 M_TEMP, M_NOWAIT);
2058 if (fd_formb == 0)
2059 return (ENOMEM);
2060
2061 fd_formb->head = form_cmd->head;
2062 fd_formb->cyl = form_cmd->cylinder;
2063 fd_formb->transfer_rate = fd->sc_type->rate;
2064 fd_formb->fd_formb_secshift = fd->sc_type->secsize;
2065 fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
2066 fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
2067 fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
2068
2069 bzero(il, sizeof il);
2070 for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
2071 while (il[(j%fd_formb->fd_formb_nsecs) + 1])
2072 j++;
2073 il[(j%fd_formb->fd_formb_nsecs) + 1] = i;
2074 j += fd->sc_type->interleave;
2075 }
2076 for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
2077 fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
2078 fd_formb->fd_formb_headno(i) = form_cmd->head;
2079 fd_formb->fd_formb_secno(i) = il[i+1];
2080 fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
2081 }
2082
2083 error = fdformat(dev, fd_formb, l->l_proc);
2084 free(fd_formb, M_TEMP);
2085 return error;
2086
2087 case FDIOCGETOPTS: /* get drive options */
2088 *(int *)addr = fd->sc_opts;
2089 return (0);
2090
2091 case FDIOCSETOPTS: /* set drive options */
2092 fd->sc_opts = *(int *)addr;
2093 return (0);
2094
2095 #ifdef FD_DEBUG
2096 case _IO('f', 100):
2097 fdc_wrfifo(fdc, NE7CMD_DUMPREG);
2098 fdcresult(fdc);
2099 printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
2100 for (i = 0; i < fdc->sc_nstat; i++)
2101 printf(" 0x%x", fdc->sc_status[i]);
2102 printf(">\n");
2103 return (0);
2104
2105 case _IOW('f', 101, int):
2106 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
2107 fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
2108 fdconf(fdc);
2109 return (0);
2110
2111 case _IO('f', 102):
2112 fdc_wrfifo(fdc, NE7CMD_SENSEI);
2113 fdcresult(fdc);
2114 printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
2115 for (i=0; i< fdc->sc_nstat; i++)
2116 printf(" 0x%x", fdc->sc_status[i]);
2117 printf(">\n");
2118 return (0);
2119 #endif
2120 default:
2121 return (ENOTTY);
2122 }
2123
2124 #ifdef DIAGNOSTIC
2125 panic("fdioctl: impossible");
2126 #endif
2127 }
2128
2129 int
2130 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct proc *p)
2131 {
2132 int rv = 0;
2133 struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
2134 struct fd_type *type = fd->sc_type;
2135 struct buf *bp;
2136
2137 /* set up a buffer header for fdstrategy() */
2138 bp = getiobuf(NULL, false);
2139 if (bp == NULL)
2140 return (ENOBUFS);
2141
2142 bp->b_vp = NULL;
2143 bp->b_cflags = BC_BUSY;
2144 bp->b_flags = B_PHYS | B_FORMAT;
2145 bp->b_proc = p;
2146 bp->b_dev = dev;
2147
2148 /*
2149 * Calculate a fake blkno, so fdstrategy() would initiate a
2150 * seek to the requested cylinder.
2151 */
2152 bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
2153 + finfo->head * type->sectrac) * FD_BSIZE(fd))
2154 / DEV_BSIZE;
2155
2156 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
2157 bp->b_data = (void *)finfo;
2158
2159 #ifdef FD_DEBUG
2160 if (fdc_debug) {
2161 int i;
2162
2163 printf("fdformat: blkno 0x%llx count %d\n",
2164 (unsigned long long)bp->b_blkno, bp->b_bcount);
2165
2166 printf("\tcyl:\t%d\n", finfo->cyl);
2167 printf("\thead:\t%d\n", finfo->head);
2168 printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
2169 printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
2170 printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
2171 printf("\ttrack data:");
2172 for (i = 0; i < finfo->fd_formb_nsecs; i++) {
2173 printf(" [c%d h%d s%d]",
2174 finfo->fd_formb_cylno(i),
2175 finfo->fd_formb_headno(i),
2176 finfo->fd_formb_secno(i) );
2177 if (finfo->fd_formb_secsize(i) != 2)
2178 printf("<sz:%d>", finfo->fd_formb_secsize(i));
2179 }
2180 printf("\n");
2181 }
2182 #endif
2183
2184 /* now do the format */
2185 fdstrategy(bp);
2186
2187 /* ...and wait for it to complete */
2188 rv = biowait(bp);
2189 putiobuf(bp);
2190 return (rv);
2191 }
2192
2193 void
2194 fdgetdisklabel(dev_t dev)
2195 {
2196 int unit = FDUNIT(dev), i;
2197 struct fd_softc *fd = device_lookup_private(&fd_cd, unit);
2198 struct disklabel *lp = fd->sc_dk.dk_label;
2199 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
2200
2201 bzero(lp, sizeof(struct disklabel));
2202 bzero(lp, sizeof(struct cpu_disklabel));
2203
2204 lp->d_type = DTYPE_FLOPPY;
2205 lp->d_secsize = FD_BSIZE(fd);
2206 lp->d_secpercyl = fd->sc_type->seccyl;
2207 lp->d_nsectors = fd->sc_type->sectrac;
2208 lp->d_ncylinders = fd->sc_type->cylinders;
2209 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
2210 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
2211 lp->d_rpm = 3600; /* XXX like it matters... */
2212
2213 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
2214 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
2215 lp->d_interleave = 1;
2216
2217 lp->d_partitions[RAW_PART].p_offset = 0;
2218 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
2219 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2220 lp->d_npartitions = RAW_PART + 1;
2221
2222 lp->d_magic = DISKMAGIC;
2223 lp->d_magic2 = DISKMAGIC;
2224 lp->d_checksum = dkcksum(lp);
2225
2226 /*
2227 * Call the generic disklabel extraction routine. If there's
2228 * not a label there, fake it.
2229 */
2230 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
2231 strncpy(lp->d_packname, "default label",
2232 sizeof(lp->d_packname));
2233 /*
2234 * Reset the partition info; it might have gotten
2235 * trashed in readdisklabel().
2236 *
2237 * XXX Why do we have to do this? readdisklabel()
2238 * should be safe...
2239 */
2240 for (i = 0; i < MAXPARTITIONS; ++i) {
2241 lp->d_partitions[i].p_offset = 0;
2242 if (i == RAW_PART) {
2243 lp->d_partitions[i].p_size =
2244 lp->d_secpercyl * lp->d_ncylinders;
2245 lp->d_partitions[i].p_fstype = FS_BSDFFS;
2246 } else {
2247 lp->d_partitions[i].p_size = 0;
2248 lp->d_partitions[i].p_fstype = FS_UNUSED;
2249 }
2250 }
2251 lp->d_npartitions = RAW_PART + 1;
2252 }
2253 }
2254
2255 void
2256 fd_do_eject(struct fd_softc *fd)
2257 {
2258 struct fdc_softc *fdc = device_private(device_parent(&fd->sc_dv));
2259
2260 if (CPU_ISSUN4C) {
2261 auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
2262 delay(10);
2263 auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
2264 return;
2265 }
2266 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
2267 bus_space_tag_t t = fdc->sc_bustag;
2268 bus_space_handle_t h = fdc->sc_handle;
2269 uint8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
2270
2271 bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
2272 delay(10);
2273 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
2274 return;
2275 }
2276 }
2277
2278 /* ARGSUSED */
2279 void
2280 fd_mountroot_hook(struct device *dev)
2281 {
2282 int c;
2283
2284 fd_do_eject(device_private(dev));
2285 printf("Insert filesystem floppy and press return.");
2286 for (;;) {
2287 c = cngetc();
2288 if ((c == '\r') || (c == '\n')) {
2289 printf("\n");
2290 break;
2291 }
2292 }
2293 }
2294
2295 #ifdef MEMORY_DISK_HOOKS
2296
2297 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
2298
2299 int
2300 fd_read_md_image(size_t *sizep, void * *addrp)
2301 {
2302 struct buf buf, *bp = &buf;
2303 dev_t dev;
2304 off_t offset;
2305 void *addr;
2306
2307 dev = makedev(54,0); /* XXX */
2308
2309 MALLOC(addr, void *, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
2310 *addrp = addr;
2311
2312 if (fdopen(dev, 0, S_IFCHR, NULL))
2313 panic("fd: mountroot: fdopen");
2314
2315 offset = 0;
2316
2317 for (;;) {
2318 bp->b_dev = dev;
2319 bp->b_error = 0;
2320 bp->b_resid = 0;
2321 bp->b_proc = NULL;
2322 bp->b_cflags |= BC_BUSY;
2323 bp->b_flags = B_PHYS | B_RAW | B_READ;
2324 bp->b_blkno = btodb(offset);
2325 bp->b_bcount = DEV_BSIZE;
2326 bp->b_data = addr;
2327 fdstrategy(bp);
2328 biowait(bp);
2329 if (bp->b_error)
2330 panic("fd: mountroot: fdread error %d", bp->b_error);
2331
2332 if (bp->b_resid != 0)
2333 break;
2334
2335 addr = (char *)addr + DEV_BSIZE;
2336 offset += DEV_BSIZE;
2337 if (offset + DEV_BSIZE > FDMICROROOTSIZE)
2338 break;
2339 }
2340 (void)fdclose(dev, 0, S_IFCHR, NULL);
2341 *sizep = offset;
2342 fd_do_eject(device_lookup_private(&fd_cd, FDUNIT(dev)));
2343 return (0);
2344 }
2345 #endif /* MEMORY_DISK_HOOKS */
2346