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