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