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