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