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