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