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