fd.c revision 1.70 1 /* $NetBSD: fd.c,v 1.70 2000/01/17 16:57:15 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum.
5 * Copyright (c) 1995 Paul Kranenburg.
6 * Copyright (c) 1990 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Don Ahn.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)fd.c 7.4 (Berkeley) 5/25/91
41 */
42
43 #include "opt_ddb.h"
44 #include "opt_md.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/file.h>
50 #include <sys/ioctl.h>
51 #include <sys/device.h>
52 #include <sys/disklabel.h>
53 #include <sys/dkstat.h>
54 #include <sys/disk.h>
55 #include <sys/fdio.h>
56 #include <sys/buf.h>
57 #include <sys/malloc.h>
58 #include <sys/proc.h>
59 #include <sys/uio.h>
60 #include <sys/stat.h>
61 #include <sys/syslog.h>
62 #include <sys/queue.h>
63 #include <sys/conf.h>
64
65 #include <dev/cons.h>
66
67 #include <vm/vm.h>
68
69 #include <uvm/uvm_extern.h>
70
71 #include <machine/cpu.h>
72 #include <machine/autoconf.h>
73 #include <machine/conf.h>
74
75 #include <sparc/sparc/auxreg.h>
76 #include <sparc/dev/fdreg.h>
77 #include <sparc/dev/fdvar.h>
78
79 #define FDUNIT(dev) (minor(dev) / 8)
80 #define FDTYPE(dev) (minor(dev) % 8)
81
82 /* XXX misuse a flag to identify format operation */
83 #define B_FORMAT B_XXX
84 #define b_cylin b_resid
85
86 #define FD_DEBUG
87 #ifdef FD_DEBUG
88 int fdc_debug = 0;
89 #endif
90
91 enum fdc_state {
92 DEVIDLE = 0,
93 MOTORWAIT,
94 DOSEEK,
95 SEEKWAIT,
96 SEEKTIMEDOUT,
97 SEEKCOMPLETE,
98 DOIO,
99 IOCOMPLETE,
100 IOTIMEDOUT,
101 DORESET,
102 RESETCOMPLETE,
103 RESETTIMEDOUT,
104 DORECAL,
105 RECALWAIT,
106 RECALTIMEDOUT,
107 RECALCOMPLETE,
108 };
109
110 /* software state, per controller */
111 struct fdc_softc {
112 struct device sc_dev; /* boilerplate */
113 bus_space_tag_t sc_bustag;
114
115 struct fd_softc *sc_fd[4]; /* pointers to children */
116 TAILQ_HEAD(drivehead, fd_softc) sc_drives;
117 enum fdc_state sc_state;
118 int sc_flags;
119 #define FDC_82077 0x01
120 #define FDC_NEEDHEADSETTLE 0x02
121 #define FDC_EIS 0x04
122 int sc_errors; /* number of retries so far */
123 int sc_overruns; /* number of DMA overruns */
124 int sc_cfg; /* current configuration */
125 struct fdcio sc_io;
126 #define sc_handle sc_io.fdcio_handle
127 #define sc_reg_msr sc_io.fdcio_reg_msr
128 #define sc_reg_fifo sc_io.fdcio_reg_fifo
129 #define sc_reg_dor sc_io.fdcio_reg_dor
130 #define sc_reg_drs sc_io.fdcio_reg_msr
131 #define sc_istate sc_io.fdcio_istate
132 #define sc_data sc_io.fdcio_data
133 #define sc_tc sc_io.fdcio_tc
134 #define sc_nstat sc_io.fdcio_nstat
135 #define sc_status sc_io.fdcio_status
136 #define sc_intrcnt sc_io.fdcio_intrcnt
137 };
138
139 #ifndef FDC_C_HANDLER
140 extern struct fdcio *fdciop;
141 #endif
142
143 /* controller driver configuration */
144 int fdcmatch_mainbus __P((struct device *, struct cfdata *, void *));
145 int fdcmatch_obio __P((struct device *, struct cfdata *, void *));
146 void fdcattach_mainbus __P((struct device *, struct device *, void *));
147 void fdcattach_obio __P((struct device *, struct device *, void *));
148
149 void fdcattach __P((struct fdc_softc *, int));
150
151 struct cfattach fdc_mainbus_ca = {
152 sizeof(struct fdc_softc), fdcmatch_mainbus, fdcattach_mainbus
153 };
154 struct cfattach fdc_obio_ca = {
155 sizeof(struct fdc_softc), fdcmatch_obio, fdcattach_obio
156 };
157
158 __inline struct fd_type *fd_dev_to_type __P((struct fd_softc *, dev_t));
159
160 /*
161 * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
162 * we tell them apart.
163 */
164 struct fd_type {
165 int sectrac; /* sectors per track */
166 int heads; /* number of heads */
167 int seccyl; /* sectors per cylinder */
168 int secsize; /* size code for sectors */
169 int datalen; /* data len when secsize = 0 */
170 int steprate; /* step rate and head unload time */
171 int gap1; /* gap len between sectors */
172 int gap2; /* formatting gap */
173 int cylinders; /* total num of cylinders */
174 int size; /* size of disk in sectors */
175 int step; /* steps per cylinder */
176 int rate; /* transfer speed code */
177 int fillbyte; /* format fill byte */
178 int interleave; /* interleave factor (formatting) */
179 char *name;
180 };
181
182 /* The order of entries in the following table is important -- BEWARE! */
183 struct fd_type fd_types[] = {
184 { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" }, /* 1.44MB diskette */
185 { 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" }, /* 3.5" 720kB diskette */
186 { 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" }, /* 360kB in 720kB drive */
187 { 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" } /* 1.2 MB japanese format */
188 };
189
190 /* software state, per disk (with up to 4 disks per ctlr) */
191 struct fd_softc {
192 struct device sc_dv; /* generic device info */
193 struct disk sc_dk; /* generic disk info */
194
195 struct fd_type *sc_deftype; /* default type descriptor */
196 struct fd_type *sc_type; /* current type descriptor */
197
198 daddr_t sc_blkno; /* starting block number */
199 int sc_bcount; /* byte count left */
200 int sc_skip; /* bytes already transferred */
201 int sc_nblks; /* number of blocks currently tranferring */
202 int sc_nbytes; /* number of bytes currently tranferring */
203
204 int sc_drive; /* physical unit number */
205 int sc_flags;
206 #define FD_OPEN 0x01 /* it's open */
207 #define FD_MOTOR 0x02 /* motor should be on */
208 #define FD_MOTOR_WAIT 0x04 /* motor coming up */
209 int sc_cylin; /* where we think the head is */
210 int sc_opts; /* user-set options */
211
212 void *sc_sdhook; /* shutdownhook cookie */
213
214 TAILQ_ENTRY(fd_softc) sc_drivechain;
215 int sc_ops; /* I/O ops since last switch */
216 struct buf sc_q; /* head of buf chain */
217 };
218
219 /* floppy driver configuration */
220 int fdmatch __P((struct device *, struct cfdata *, void *));
221 void fdattach __P((struct device *, struct device *, void *));
222
223 struct cfattach fd_ca = {
224 sizeof(struct fd_softc), fdmatch, fdattach
225 };
226
227 extern struct cfdriver fd_cd;
228
229 void fdgetdisklabel __P((dev_t));
230 int fd_get_parms __P((struct fd_softc *));
231 void fdstrategy __P((struct buf *));
232 void fdstart __P((struct fd_softc *));
233 int fdprint __P((void *, const char *));
234
235 struct dkdriver fddkdriver = { fdstrategy };
236
237 struct fd_type *fd_nvtotype __P((char *, int, int));
238 void fd_set_motor __P((struct fdc_softc *fdc));
239 void fd_motor_off __P((void *arg));
240 void fd_motor_on __P((void *arg));
241 int fdcresult __P((struct fdc_softc *fdc));
242 int out_fdc __P((struct fdc_softc *fdc, u_char x));
243 void fdcstart __P((struct fdc_softc *fdc));
244 void fdcstatus __P((struct device *dv, int n, char *s));
245 void fdc_reset __P((struct fdc_softc *fdc));
246 void fdctimeout __P((void *arg));
247 void fdcpseudointr __P((void *arg));
248 #ifdef FDC_C_HANDLER
249 int fdchwintr __P((void *));
250 #else
251 void fdchwintr __P((void));
252 #endif
253 int fdcswintr __P((void *));
254 int fdcstate __P((struct fdc_softc *));
255 void fdcretry __P((struct fdc_softc *fdc));
256 void fdfinish __P((struct fd_softc *fd, struct buf *bp));
257 int fdformat __P((dev_t, struct ne7_fd_formb *, struct proc *));
258 void fd_do_eject __P((struct fd_softc *));
259 void fd_mountroot_hook __P((struct device *));
260 static int fdconf __P((struct fdc_softc *));
261 static void establish_chip_type __P((
262 struct fdc_softc *,
263 bus_space_tag_t,
264 bus_type_t,
265 bus_addr_t,
266 bus_size_t,
267 bus_space_handle_t));
268
269
270 #if PIL_FDSOFT == 4
271 #define IE_FDSOFT IE_L4
272 #else
273 #error 4
274 #endif
275
276 #ifdef FDC_C_HANDLER
277 #if defined(SUN4M)
278 #define FD_SET_SWINTR do { \
279 if (CPU_ISSUN4M) \
280 raise(0, PIL_FDSOFT); \
281 else \
282 ienab_bis(IE_L4); \
283 } while(0)
284 #else
285 #define AUDIO_SET_SWINTR ienab_bis(IE_FDSOFT)
286 #endif /* defined(SUN4M) */
287 #endif /* FDC_C_HANDLER */
288
289 #define OBP_FDNAME (CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
290
291 int
292 fdcmatch_mainbus(parent, match, aux)
293 struct device *parent;
294 struct cfdata *match;
295 void *aux;
296 {
297 struct mainbus_attach_args *ma = aux;
298
299 /*
300 * Floppy controller is on mainbus on sun4c.
301 */
302 if (!CPU_ISSUN4C)
303 return (0);
304
305 /* sun4c PROMs call the controller "fd" */
306 if (strcmp("fd", ma->ma_name) != 0)
307 return (0);
308
309 return (bus_space_probe(ma->ma_bustag,
310 ma->ma_iospace,
311 ma->ma_paddr,
312 1, /* probe size */
313 0, /* offset */
314 0, /* flags */
315 NULL, NULL));
316 }
317
318 int
319 fdcmatch_obio(parent, match, aux)
320 struct device *parent;
321 struct cfdata *match;
322 void *aux;
323 {
324 union obio_attach_args *uoba = aux;
325 struct sbus_attach_args *sa;
326
327 /*
328 * Floppy controller is on obio on sun4m.
329 */
330 if (uoba->uoba_isobio4 != 0)
331 return (0);
332
333 sa = &uoba->uoba_sbus;
334
335 /* sun4m PROMs call the controller "SUNW,fdtwo" */
336 if (strcmp("SUNW,fdtwo", sa->sa_name) != 0)
337 return (0);
338
339 return (bus_space_probe(sa->sa_bustag, sa->sa_slot, sa->sa_offset,
340 1, /* probe size */
341 0, /* offset */
342 0, /* flags */
343 NULL, NULL));
344 }
345
346 static void
347 establish_chip_type(fdc, tag, type, addr, size, handle)
348 struct fdc_softc *fdc;
349 bus_space_tag_t tag;
350 bus_type_t type;
351 bus_addr_t addr;
352 bus_size_t size;
353 bus_space_handle_t handle;
354 {
355 u_int8_t v;
356
357 /*
358 * This hack from Chris Torek: apparently DOR really
359 * addresses MSR/DRS on a 82072.
360 * We used to rely on the VERSION command to tell the
361 * difference (which did not work).
362 */
363
364 /* First, check the size of the register bank */
365 if (size < 8)
366 /* It isn't a 82077 */
367 return;
368
369 /* Then probe the DOR register offset */
370 if (bus_space_probe(tag, type, addr,
371 1, /* probe size */
372 FDREG77_DOR, /* offset */
373 0, /* flags */
374 NULL, NULL) == 0) {
375
376 /* It isn't a 82077 */
377 return;
378 }
379
380 v = bus_space_read_1(tag, handle, FDREG77_DOR);
381 if (v == NE7_RQM) {
382 /*
383 * Value in DOR looks like it's really MSR
384 */
385 bus_space_write_1(tag, handle, FDREG77_DOR, FDC_250KBPS);
386 v = bus_space_read_1(tag, handle, FDREG77_DOR);
387 if (v == NE7_RQM) {
388 /*
389 * The value in the DOR didn't stick;
390 * it isn't a 82077
391 */
392 return;
393 }
394 }
395
396 fdc->sc_flags |= FDC_82077;
397 }
398
399 /*
400 * Arguments passed between fdcattach and fdprobe.
401 */
402 struct fdc_attach_args {
403 int fa_drive;
404 struct fd_type *fa_deftype;
405 };
406
407 /*
408 * Print the location of a disk drive (called just before attaching the
409 * the drive). If `fdc' is not NULL, the drive was found but was not
410 * in the system config file; print the drive name as well.
411 * Return QUIET (config_find ignores this if the device was configured) to
412 * avoid printing `fdN not configured' messages.
413 */
414 int
415 fdprint(aux, fdc)
416 void *aux;
417 const char *fdc;
418 {
419 register struct fdc_attach_args *fa = aux;
420
421 if (!fdc)
422 printf(" drive %d", fa->fa_drive);
423 return (QUIET);
424 }
425
426 static int
427 fdconf(fdc)
428 struct fdc_softc *fdc;
429 {
430 int vroom;
431
432 if (out_fdc(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
433 return (-1);
434
435 /*
436 * dumpreg[7] seems to be a motor-off timeout; set it to whatever
437 * the PROM thinks is appropriate.
438 */
439 if ((vroom = fdc->sc_status[7]) == 0)
440 vroom = 0x64;
441
442 /* Configure controller to use FIFO and Implied Seek */
443 out_fdc(fdc, NE7CMD_CFG);
444 out_fdc(fdc, vroom);
445 out_fdc(fdc, fdc->sc_cfg);
446 out_fdc(fdc, 0); /* PRETRK */
447 /* No result phase for the NE7CMD_CFG command */
448
449 if ((fdc->sc_flags & FDC_82077) != 0) {
450 /* Lock configuration across soft resets. */
451 out_fdc(fdc, NE7CMD_LOCK | CFG_LOCK);
452 if (fdcresult(fdc) != 1) {
453 #ifdef DEBUG
454 printf("fdconf: CFGLOCK failed");
455 #endif
456 return (-1);
457 }
458 }
459
460 return (0);
461 #if 0
462 if (out_fdc(fdc, NE7CMD_VERSION) == 0 &&
463 fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
464 if (fdc_debug)
465 printf("[version cmd]");
466 }
467 #endif
468 }
469
470 /*
471 * Controller and drives are represented by one and the same
472 * Openprom node, so we can as well check for the floppy boots here.
473 */
474
475 void
476 fdcattach_mainbus(parent, self, aux)
477 struct device *parent, *self;
478 void *aux;
479 {
480 struct fdc_softc *fdc = (void *)self;
481 struct mainbus_attach_args *ma = aux;
482
483 fdc->sc_bustag = ma->ma_bustag;
484
485 if (ma->ma_promvaddr != 0)
486 fdc->sc_handle = (bus_space_handle_t)ma->ma_promvaddr;
487 else {
488 if (bus_space_map2(
489 ma->ma_bustag,
490 ma->ma_iospace,
491 ma->ma_paddr,
492 ma->ma_size,
493 BUS_SPACE_MAP_LINEAR,
494 0,
495 &fdc->sc_handle) != 0) {
496 printf("%s: cannot map registers\n", self->dv_xname);
497 return;
498 }
499 }
500
501 establish_chip_type(fdc,
502 ma->ma_bustag,
503 ma->ma_iospace,
504 ma->ma_paddr,
505 ma->ma_size,
506 fdc->sc_handle);
507
508 fdcattach(fdc, ma->ma_pri);
509 }
510
511 void
512 fdcattach_obio(parent, self, aux)
513 struct device *parent, *self;
514 void *aux;
515 {
516 struct fdc_softc *fdc = (void *)self;
517 union obio_attach_args *uoba = aux;
518 struct sbus_attach_args *sa = &uoba->uoba_sbus;
519
520 fdc->sc_bustag = sa->sa_bustag;
521
522 if (sa->sa_npromvaddrs != 0)
523 fdc->sc_handle = (bus_space_handle_t)sa->sa_promvaddrs[0];
524 else {
525 if (sbus_bus_map(sa->sa_bustag, sa->sa_slot,
526 sa->sa_offset,
527 sa->sa_size,
528 BUS_SPACE_MAP_LINEAR,
529 0,
530 &fdc->sc_handle) != 0) {
531 printf("%s: cannot map control registers\n",
532 self->dv_xname);
533 return;
534 }
535 }
536
537 establish_chip_type(fdc,
538 sa->sa_bustag,
539 sa->sa_slot,
540 sa->sa_offset,
541 sa->sa_size,
542 fdc->sc_handle);
543
544 if (sa->sa_nintr != 0)
545 fdcattach(fdc, sa->sa_pri);
546 }
547
548 void
549 fdcattach(fdc, pri)
550 struct fdc_softc *fdc;
551 int pri;
552 {
553 struct fdc_attach_args fa;
554 char code;
555
556 fdc->sc_state = DEVIDLE;
557 fdc->sc_istate = ISTATE_IDLE;
558 fdc->sc_flags |= FDC_EIS;
559 TAILQ_INIT(&fdc->sc_drives);
560
561 if ((fdc->sc_flags & FDC_82077) != 0) {
562 fdc->sc_reg_msr = FDREG77_MSR;
563 fdc->sc_reg_fifo = FDREG77_FIFO;
564 fdc->sc_reg_dor = FDREG77_DOR;
565 code = '7';
566 } else {
567 fdc->sc_reg_msr = FDREG72_MSR;
568 fdc->sc_reg_fifo = FDREG72_FIFO;
569 fdc->sc_reg_dor = 0;
570 code = '2';
571 }
572
573 printf(" softpri %d: chip 8207%c\n", PIL_FDSOFT, code);
574
575 /*
576 * Configure controller; enable FIFO, Implied seek, no POLL mode?.
577 * Note: CFG_EFIFO is active-low, initial threshold value: 8
578 */
579 fdc->sc_cfg = CFG_EIS|/*CFG_EFIFO|*/CFG_POLL|(8 & CFG_THRHLD_MASK);
580 if (fdconf(fdc) != 0) {
581 printf("%s: no drives attached\n", fdc->sc_dev.dv_xname);
582 return;
583 }
584
585 #ifdef FDC_C_HANDLER
586 (void)bus_intr_establish(fdc->sc_bustag, pri, 0,
587 fdchwintr, fdc);
588 #else
589 fdciop = &fdc->sc_io;
590 (void)bus_intr_establish(fdc->sc_bustag, pri,
591 BUS_INTR_ESTABLISH_FASTTRAP,
592 (int (*) __P((void *)))fdchwintr, NULL);
593 #endif
594 (void)bus_intr_establish(fdc->sc_bustag, PIL_FDSOFT,
595 BUS_INTR_ESTABLISH_SOFTINTR,
596 fdcswintr, fdc);
597
598 evcnt_attach(&fdc->sc_dev, "intr", &fdc->sc_intrcnt);
599
600 /* physical limit: four drives per controller. */
601 for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
602 fa.fa_deftype = NULL; /* unknown */
603 fa.fa_deftype = &fd_types[0]; /* XXX */
604 (void)config_found(&fdc->sc_dev, (void *)&fa, fdprint);
605 }
606 }
607
608 int
609 fdmatch(parent, match, aux)
610 struct device *parent;
611 struct cfdata *match;
612 void *aux;
613 {
614 struct fdc_softc *fdc = (void *)parent;
615 bus_space_tag_t t = fdc->sc_bustag;
616 bus_space_handle_t h = fdc->sc_handle;
617 struct fdc_attach_args *fa = aux;
618 int drive = fa->fa_drive;
619 int n, ok;
620
621 if (drive > 0)
622 /* XXX - for now, punt on more than one drive */
623 return (0);
624
625 if (fdc->sc_flags & FDC_82077) {
626 /* select drive and turn on motor */
627 bus_space_write_1(t, h, fdc->sc_reg_dor,
628 drive | FDO_FRST | FDO_MOEN(drive));
629 /* wait for motor to spin up */
630 delay(250000);
631 } else {
632 auxregbisc(AUXIO4C_FDS, 0);
633 }
634 fdc->sc_nstat = 0;
635 out_fdc(fdc, NE7CMD_RECAL);
636 out_fdc(fdc, drive);
637
638 /* Wait for recalibration to complete */
639 for (n = 0; n < 10000; n++) {
640 u_int8_t v;
641
642 delay(1000);
643 v = bus_space_read_1(t, h, fdc->sc_reg_msr);
644 if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
645 /* wait a bit longer till device *really* is ready */
646 delay(100000);
647 if (out_fdc(fdc, NE7CMD_SENSEI))
648 break;
649 if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
650 /*
651 * Got `invalid command'; we interpret it
652 * to mean that the re-calibrate hasn't in
653 * fact finished yet
654 */
655 continue;
656 break;
657 }
658 }
659 n = fdc->sc_nstat;
660 #ifdef FD_DEBUG
661 if (fdc_debug) {
662 int i;
663 printf("fdprobe: %d stati:", n);
664 for (i = 0; i < n; i++)
665 printf(" 0x%x", fdc->sc_status[i]);
666 printf("\n");
667 }
668 #endif
669 ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
670
671 /* turn off motor */
672 if (fdc->sc_flags & FDC_82077) {
673 /* deselect drive and turn motor off */
674 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
675 } else {
676 auxregbisc(0, AUXIO4C_FDS);
677 }
678
679 return (ok);
680 }
681
682 /*
683 * Controller is working, and drive responded. Attach it.
684 */
685 void
686 fdattach(parent, self, aux)
687 struct device *parent, *self;
688 void *aux;
689 {
690 struct fdc_softc *fdc = (void *)parent;
691 struct fd_softc *fd = (void *)self;
692 struct fdc_attach_args *fa = aux;
693 struct fd_type *type = fa->fa_deftype;
694 int drive = fa->fa_drive;
695
696 /* XXX Allow `flags' to override device type? */
697
698 if (type)
699 printf(": %s %d cyl, %d head, %d sec\n", type->name,
700 type->cylinders, type->heads, type->sectrac);
701 else
702 printf(": density unknown\n");
703
704 fd->sc_cylin = -1;
705 fd->sc_drive = drive;
706 fd->sc_deftype = type;
707 fdc->sc_fd[drive] = fd;
708
709 out_fdc(fdc, NE7CMD_SPECIFY);
710 out_fdc(fdc, type->steprate);
711 out_fdc(fdc, 6 | NE7_SPECIFY_NODMA);
712
713 /*
714 * Initialize and attach the disk structure.
715 */
716 fd->sc_dk.dk_name = fd->sc_dv.dv_xname;
717 fd->sc_dk.dk_driver = &fddkdriver;
718 disk_attach(&fd->sc_dk);
719
720 /*
721 * Establish a mountroot_hook anyway in case we booted
722 * with RB_ASKNAME and get selected as the boot device.
723 */
724 mountroothook_establish(fd_mountroot_hook, &fd->sc_dv);
725
726 /* Make sure the drive motor gets turned off at shutdown time. */
727 fd->sc_sdhook = shutdownhook_establish(fd_motor_off, fd);
728
729 /* XXX Need to do some more fiddling with sc_dk. */
730 dk_establish(&fd->sc_dk, &fd->sc_dv);
731 }
732
733 __inline struct fd_type *
734 fd_dev_to_type(fd, dev)
735 struct fd_softc *fd;
736 dev_t dev;
737 {
738 int type = FDTYPE(dev);
739
740 if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
741 return (NULL);
742 return (type ? &fd_types[type - 1] : fd->sc_deftype);
743 }
744
745 void
746 fdstrategy(bp)
747 register struct buf *bp; /* IO operation to perform */
748 {
749 struct fd_softc *fd;
750 int unit = FDUNIT(bp->b_dev);
751 int sz;
752 int s;
753
754 /* Valid unit, controller, and request? */
755 if (unit >= fd_cd.cd_ndevs ||
756 (fd = fd_cd.cd_devs[unit]) == 0 ||
757 bp->b_blkno < 0 ||
758 (((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
759 (bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
760 (bp->b_flags & B_FORMAT) == 0)) {
761 bp->b_error = EINVAL;
762 goto bad;
763 }
764
765 /* If it's a null transfer, return immediately. */
766 if (bp->b_bcount == 0)
767 goto done;
768
769 sz = howmany(bp->b_bcount, DEV_BSIZE);
770
771 if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
772 sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
773 - bp->b_blkno;
774 if (sz == 0) {
775 /* If exactly at end of disk, return EOF. */
776 bp->b_resid = bp->b_bcount;
777 goto done;
778 }
779 if (sz < 0) {
780 /* If past end of disk, return EINVAL. */
781 bp->b_error = EINVAL;
782 goto bad;
783 }
784 /* Otherwise, truncate request. */
785 bp->b_bcount = sz << DEV_BSHIFT;
786 }
787
788 bp->b_cylin = (bp->b_blkno * DEV_BSIZE) /
789 (FD_BSIZE(fd) * fd->sc_type->seccyl);
790
791 #ifdef FD_DEBUG
792 if (fdc_debug > 1)
793 printf("fdstrategy: b_blkno %d b_bcount %ld blkno %d cylin %ld\n",
794 bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylin);
795 #endif
796
797 /* Queue transfer on drive, activate drive and controller if idle. */
798 s = splbio();
799 disksort(&fd->sc_q, bp);
800 untimeout(fd_motor_off, fd); /* a good idea */
801 if (!fd->sc_q.b_active)
802 fdstart(fd);
803 #ifdef DIAGNOSTIC
804 else {
805 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
806 if (fdc->sc_state == DEVIDLE) {
807 printf("fdstrategy: controller inactive\n");
808 fdcstart(fdc);
809 }
810 }
811 #endif
812 splx(s);
813 return;
814
815 bad:
816 bp->b_flags |= B_ERROR;
817 done:
818 /* Toss transfer; we're done early. */
819 biodone(bp);
820 }
821
822 void
823 fdstart(fd)
824 struct fd_softc *fd;
825 {
826 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
827 int active = fdc->sc_drives.tqh_first != 0;
828
829 /* Link into controller queue. */
830 fd->sc_q.b_active = 1;
831 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
832
833 /* If controller not already active, start it. */
834 if (!active)
835 fdcstart(fdc);
836 }
837
838 void
839 fdfinish(fd, bp)
840 struct fd_softc *fd;
841 struct buf *bp;
842 {
843 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
844
845 /*
846 * Move this drive to the end of the queue to give others a `fair'
847 * chance. We only force a switch if N operations are completed while
848 * another drive is waiting to be serviced, since there is a long motor
849 * startup delay whenever we switch.
850 */
851 if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
852 fd->sc_ops = 0;
853 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
854 if (bp->b_actf) {
855 TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
856 } else
857 fd->sc_q.b_active = 0;
858 }
859 bp->b_resid = fd->sc_bcount;
860 fd->sc_skip = 0;
861 fd->sc_q.b_actf = bp->b_actf;
862
863 biodone(bp);
864 /* turn off motor 5s from now */
865 timeout(fd_motor_off, fd, 5 * hz);
866 fdc->sc_state = DEVIDLE;
867 }
868
869 void
870 fdc_reset(fdc)
871 struct fdc_softc *fdc;
872 {
873 bus_space_tag_t t = fdc->sc_bustag;
874 bus_space_handle_t h = fdc->sc_handle;
875
876 if (fdc->sc_flags & FDC_82077) {
877 bus_space_write_1(t, h, fdc->sc_reg_dor,
878 FDO_FDMAEN | FDO_MOEN(0));
879 }
880
881 bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
882 delay(10);
883 bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
884
885 if (fdc->sc_flags & FDC_82077) {
886 bus_space_write_1(t, h, fdc->sc_reg_dor,
887 FDO_FRST | FDO_FDMAEN | FDO_DS);
888 }
889 #ifdef FD_DEBUG
890 if (fdc_debug)
891 printf("fdc reset\n");
892 #endif
893 }
894
895 void
896 fd_set_motor(fdc)
897 struct fdc_softc *fdc;
898 {
899 struct fd_softc *fd;
900 u_char status;
901 int n;
902
903 if (fdc->sc_flags & FDC_82077) {
904 status = FDO_FRST | FDO_FDMAEN;
905 if ((fd = fdc->sc_drives.tqh_first) != NULL)
906 status |= fd->sc_drive;
907
908 for (n = 0; n < 4; n++)
909 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
910 status |= FDO_MOEN(n);
911 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
912 fdc->sc_reg_dor, status);
913 } else {
914 int on = 0;
915
916 for (n = 0; n < 4; n++)
917 if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
918 on = 1;
919 if (on) {
920 auxregbisc(AUXIO4C_FDS, 0);
921 } else {
922 auxregbisc(0, AUXIO4C_FDS);
923 }
924 }
925 }
926
927 void
928 fd_motor_off(arg)
929 void *arg;
930 {
931 struct fd_softc *fd = arg;
932 int s;
933
934 s = splbio();
935 fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
936 fd_set_motor((struct fdc_softc *)fd->sc_dv.dv_parent);
937 splx(s);
938 }
939
940 void
941 fd_motor_on(arg)
942 void *arg;
943 {
944 struct fd_softc *fd = arg;
945 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
946 int s;
947
948 s = splbio();
949 fd->sc_flags &= ~FD_MOTOR_WAIT;
950 if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
951 (void) fdcstate(fdc);
952 splx(s);
953 }
954
955 int
956 fdcresult(fdc)
957 struct fdc_softc *fdc;
958 {
959 bus_space_tag_t t = fdc->sc_bustag;
960 bus_space_handle_t h = fdc->sc_handle;
961 int j = 100000,
962 n = 0;
963
964 for (; j; j--) {
965 u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
966 v &= (NE7_DIO | NE7_RQM | NE7_CB);
967 if (v == NE7_RQM)
968 return (fdc->sc_nstat = n);
969 if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
970 if (n >= sizeof(fdc->sc_status)) {
971 log(LOG_ERR, "fdcresult: overrun\n");
972 return (-1);
973 }
974 fdc->sc_status[n++] =
975 bus_space_read_1(t, h, fdc->sc_reg_fifo);
976 } else
977 delay(10);
978 }
979 log(LOG_ERR, "fdcresult: timeout\n");
980 return (fdc->sc_nstat = -1);
981 }
982
983 int
984 out_fdc(fdc, x)
985 struct fdc_softc *fdc;
986 u_int8_t x;
987 {
988 bus_space_tag_t t = fdc->sc_bustag;
989 bus_space_handle_t h = fdc->sc_handle;
990 int i;
991
992 for (i = 100000; i-- > 0;) {
993 u_int8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
994 if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM)
995 break;
996 delay(1);
997 }
998 if (i <= 0)
999 return (-1);
1000
1001 bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
1002 return (0);
1003 }
1004
1005 int
1006 fdopen(dev, flags, fmt, p)
1007 dev_t dev;
1008 int flags, fmt;
1009 struct proc *p;
1010 {
1011 int unit, pmask;
1012 struct fd_softc *fd;
1013 struct fd_type *type;
1014
1015 unit = FDUNIT(dev);
1016 if (unit >= fd_cd.cd_ndevs)
1017 return (ENXIO);
1018 fd = fd_cd.cd_devs[unit];
1019 if (fd == 0)
1020 return (ENXIO);
1021 type = fd_dev_to_type(fd, dev);
1022 if (type == NULL)
1023 return (ENXIO);
1024
1025 if ((fd->sc_flags & FD_OPEN) != 0 &&
1026 fd->sc_type != type)
1027 return (EBUSY);
1028
1029 fd->sc_type = type;
1030 fd->sc_cylin = -1;
1031 fd->sc_flags |= FD_OPEN;
1032
1033 /*
1034 * Only update the disklabel if we're not open anywhere else.
1035 */
1036 if (fd->sc_dk.dk_openmask == 0)
1037 fdgetdisklabel(dev);
1038
1039 pmask = (1 << DISKPART(dev));
1040
1041 switch (fmt) {
1042 case S_IFCHR:
1043 fd->sc_dk.dk_copenmask |= pmask;
1044 break;
1045
1046 case S_IFBLK:
1047 fd->sc_dk.dk_bopenmask |= pmask;
1048 break;
1049 }
1050 fd->sc_dk.dk_openmask =
1051 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1052
1053 return (0);
1054 }
1055
1056 int
1057 fdclose(dev, flags, fmt, p)
1058 dev_t dev;
1059 int flags, fmt;
1060 struct proc *p;
1061 {
1062 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1063 int pmask = (1 << DISKPART(dev));
1064
1065 fd->sc_flags &= ~FD_OPEN;
1066 fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
1067
1068 switch (fmt) {
1069 case S_IFCHR:
1070 fd->sc_dk.dk_copenmask &= ~pmask;
1071 break;
1072
1073 case S_IFBLK:
1074 fd->sc_dk.dk_bopenmask &= ~pmask;
1075 break;
1076 }
1077 fd->sc_dk.dk_openmask =
1078 fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
1079
1080 return (0);
1081 }
1082
1083 int
1084 fdread(dev, uio, flag)
1085 dev_t dev;
1086 struct uio *uio;
1087 int flag;
1088 {
1089
1090 return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
1091 }
1092
1093 int
1094 fdwrite(dev, uio, flag)
1095 dev_t dev;
1096 struct uio *uio;
1097 int flag;
1098 {
1099
1100 return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
1101 }
1102
1103 void
1104 fdcstart(fdc)
1105 struct fdc_softc *fdc;
1106 {
1107
1108 #ifdef DIAGNOSTIC
1109 /* only got here if controller's drive queue was inactive; should
1110 be in idle state */
1111 if (fdc->sc_state != DEVIDLE) {
1112 printf("fdcstart: not idle\n");
1113 return;
1114 }
1115 #endif
1116 (void) fdcstate(fdc);
1117 }
1118
1119 void
1120 fdcstatus(dv, n, s)
1121 struct device *dv;
1122 int n;
1123 char *s;
1124 {
1125 struct fdc_softc *fdc = (void *)dv->dv_parent;
1126 char bits[64];
1127 #if 0
1128 /*
1129 * A 82072 seems to return <invalid command> on
1130 * gratuitous Sense Interrupt commands.
1131 */
1132 if (n == 0 && (fdc->sc_flags & FDC_82077)) {
1133 out_fdc(fdc, NE7CMD_SENSEI);
1134 (void) fdcresult(fdc);
1135 n = 2;
1136 }
1137 #endif
1138
1139 /* Just print last status */
1140 n = fdc->sc_nstat;
1141
1142 printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
1143
1144 switch (n) {
1145 case 0:
1146 printf("\n");
1147 break;
1148 case 2:
1149 printf(" (st0 %s cyl %d)\n",
1150 bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
1151 bits, sizeof(bits)), fdc->sc_status[1]);
1152 break;
1153 case 7:
1154 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1155 NE7_ST0BITS, bits, sizeof(bits)));
1156 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1157 NE7_ST1BITS, bits, sizeof(bits)));
1158 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1159 NE7_ST2BITS, bits, sizeof(bits)));
1160 printf(" cyl %d head %d sec %d)\n",
1161 fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1162 break;
1163 #ifdef DIAGNOSTIC
1164 default:
1165 printf(" fdcstatus: weird size: %d\n", n);
1166 break;
1167 #endif
1168 }
1169 }
1170
1171 void
1172 fdctimeout(arg)
1173 void *arg;
1174 {
1175 struct fdc_softc *fdc = arg;
1176 struct fd_softc *fd = fdc->sc_drives.tqh_first;
1177 int s;
1178
1179 s = splbio();
1180 fdcstatus(&fd->sc_dv, 0, "timeout");
1181
1182 if (fd->sc_q.b_actf)
1183 fdc->sc_state++;
1184 else
1185 fdc->sc_state = DEVIDLE;
1186
1187 (void) fdcstate(fdc);
1188 splx(s);
1189 }
1190
1191 void
1192 fdcpseudointr(arg)
1193 void *arg;
1194 {
1195 struct fdc_softc *fdc = arg;
1196 int s;
1197
1198 /* Just ensure it has the right spl. */
1199 s = splbio();
1200 (void) fdcstate(fdc);
1201 splx(s);
1202 }
1203
1204
1205 #ifdef FDC_C_HANDLER
1206 /*
1207 * hardware interrupt entry point: must be converted to `fast'
1208 * (in-window) handler.
1209 */
1210 int
1211 fdchwintr(arg)
1212 void *arg;
1213 {
1214 struct fdc_softc *fdc = arg;
1215 bus_space_tag_t t = fdc->sc_bustag;
1216 bus_space_handle_t h = fdc->sc_handle;
1217
1218 switch (fdc->sc_istate) {
1219 case ISTATE_IDLE:
1220 return (0);
1221 case ISTATE_SENSEI:
1222 out_fdc(fdc, NE7CMD_SENSEI);
1223 fdcresult(fdc);
1224 fdc->sc_istate = ISTATE_IDLE;
1225 FD_SET_SWINTR;
1226 return (1);
1227 case ISTATE_SPURIOUS:
1228 fdcresult(fdc);
1229 fdc->sc_istate = ISTATE_SPURIOUS;
1230 printf("fdc: stray hard interrupt... ");
1231 FD_SET_SWINTR;
1232 return (1);
1233 case ISTATE_DMA:
1234 break;
1235 default:
1236 printf("fdc: goofed ...\n");
1237 return (1);
1238 }
1239
1240 for (;;) {
1241 u_int8_t msr;
1242
1243 msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
1244
1245 if ((msr & NE7_RQM) == 0)
1246 break;
1247
1248 if ((msr & NE7_NDM) == 0) {
1249 fdcresult(fdc);
1250 fdc->sc_istate = ISTATE_IDLE;
1251 ienab_bis(IE_FDSOFT);
1252 printf("fdc: overrun: tc = %d\n", fdc->sc_tc);
1253 break;
1254 }
1255
1256 if (msr & NE7_DIO) {
1257 *fdc->sc_data++ =
1258 bus_space_read_1(t, h, fdc->sc_reg_fifo);
1259 } else {
1260 bus_space_write_1(t, h, fdc->sc_reg_fifo,
1261 *fdc->sc_data);
1262 fdc->sc_data++;
1263 }
1264 if (--fdc->sc_tc == 0) {
1265 fdc->sc_istate = ISTATE_DONE;
1266 FTC_FLIP;
1267 fdcresult(fdc);
1268 FD_SET_SWINTR;
1269 break;
1270 }
1271 }
1272 return (1);
1273 }
1274 #endif
1275
1276 int
1277 fdcswintr(arg)
1278 void *arg;
1279 {
1280 struct fdc_softc *fdc = arg;
1281 int s;
1282
1283 if (fdc->sc_istate != ISTATE_DONE)
1284 return (0);
1285
1286 fdc->sc_istate = ISTATE_IDLE;
1287 s = splbio();
1288 fdcstate(fdc);
1289 splx(s);
1290 return (1);
1291 }
1292
1293 int
1294 fdcstate(fdc)
1295 struct fdc_softc *fdc;
1296 {
1297 #define st0 fdc->sc_status[0]
1298 #define st1 fdc->sc_status[1]
1299 #define cyl fdc->sc_status[1]
1300 #define OUT_FDC(fdc, c, s) \
1301 do { if (out_fdc(fdc, (c))) { (fdc)->sc_state = (s); goto loop; } } while(0)
1302
1303 struct fd_softc *fd;
1304 struct buf *bp;
1305 int read, head, sec, nblks;
1306 struct fd_type *type;
1307 struct ne7_fd_formb *finfo = NULL;
1308
1309
1310 if (fdc->sc_istate != ISTATE_IDLE) {
1311 /* Trouble... */
1312 printf("fdc: spurious interrupt: state %d, istate=%d\n",
1313 fdc->sc_state, fdc->sc_istate);
1314 fdc->sc_istate = ISTATE_IDLE;
1315 if (fdc->sc_state == RESETCOMPLETE ||
1316 fdc->sc_state == RESETTIMEDOUT) {
1317 panic("fdcintr: spurious interrupt can't be cleared");
1318 }
1319 goto doreset;
1320 }
1321
1322 loop:
1323 /* Is there a drive for the controller to do a transfer with? */
1324 fd = fdc->sc_drives.tqh_first;
1325 if (fd == NULL) {
1326 fdc->sc_state = DEVIDLE;
1327 return (0);
1328 }
1329
1330 /* Is there a transfer to this drive? If not, deactivate drive. */
1331 bp = fd->sc_q.b_actf;
1332 if (bp == NULL) {
1333 fd->sc_ops = 0;
1334 TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1335 fd->sc_q.b_active = 0;
1336 goto loop;
1337 }
1338
1339 if (bp->b_flags & B_FORMAT)
1340 finfo = (struct ne7_fd_formb *)bp->b_data;
1341
1342 switch (fdc->sc_state) {
1343 case DEVIDLE:
1344 fdc->sc_errors = 0;
1345 fd->sc_skip = 0;
1346 fd->sc_bcount = bp->b_bcount;
1347 fd->sc_blkno = (bp->b_blkno * DEV_BSIZE) / FD_BSIZE(fd);
1348 untimeout(fd_motor_off, fd);
1349 if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1350 fdc->sc_state = MOTORWAIT;
1351 return (1);
1352 }
1353 if ((fd->sc_flags & FD_MOTOR) == 0) {
1354 /* Turn on the motor, being careful about pairing. */
1355 struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1356 if (ofd && ofd->sc_flags & FD_MOTOR) {
1357 untimeout(fd_motor_off, ofd);
1358 ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1359 }
1360 fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1361 fd_set_motor(fdc);
1362 fdc->sc_state = MOTORWAIT;
1363 if (fdc->sc_flags & FDC_82077) { /* XXX */
1364 /* Allow .25s for motor to stabilize. */
1365 timeout(fd_motor_on, fd, hz / 4);
1366 } else {
1367 fd->sc_flags &= ~FD_MOTOR_WAIT;
1368 goto loop;
1369 }
1370 return (1);
1371 }
1372 /* Make sure the right drive is selected. */
1373 fd_set_motor(fdc);
1374
1375 /*FALLTHROUGH*/
1376 case DOSEEK:
1377 doseek:
1378 if ((fdc->sc_flags & FDC_EIS) &&
1379 (bp->b_flags & B_FORMAT) == 0) {
1380 fd->sc_cylin = bp->b_cylin;
1381 /* We use implied seek */
1382 goto doio;
1383 }
1384
1385 if (fd->sc_cylin == bp->b_cylin)
1386 goto doio;
1387
1388 /* specify command */
1389 OUT_FDC(fdc, NE7CMD_SPECIFY, SEEKTIMEDOUT);
1390 OUT_FDC(fdc, fd->sc_type->steprate, SEEKTIMEDOUT);
1391 /* XXX head load time == 6ms */
1392 OUT_FDC(fdc, 6 | NE7_SPECIFY_NODMA, SEEKTIMEDOUT);
1393
1394 fdc->sc_istate = ISTATE_SENSEI;
1395 /* seek function */
1396 OUT_FDC(fdc, NE7CMD_SEEK, SEEKTIMEDOUT);
1397 OUT_FDC(fdc, fd->sc_drive, SEEKTIMEDOUT); /* drive number */
1398 OUT_FDC(fdc, bp->b_cylin * fd->sc_type->step, SEEKTIMEDOUT);
1399
1400 fd->sc_cylin = -1;
1401 fdc->sc_state = SEEKWAIT;
1402 fdc->sc_nstat = 0;
1403
1404 fd->sc_dk.dk_seek++;
1405 disk_busy(&fd->sc_dk);
1406
1407 timeout(fdctimeout, fdc, 4 * hz);
1408 return (1);
1409
1410 case DOIO:
1411 doio:
1412 if (finfo != NULL)
1413 fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1414 (char *)finfo;
1415 type = fd->sc_type;
1416 sec = fd->sc_blkno % type->seccyl;
1417 nblks = type->seccyl - sec;
1418 nblks = min(nblks, fd->sc_bcount / FD_BSIZE(fd));
1419 nblks = min(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
1420 fd->sc_nblks = nblks;
1421 fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
1422 head = sec / type->sectrac;
1423 sec -= head * type->sectrac;
1424 #ifdef DIAGNOSTIC
1425 {int block;
1426 block = (fd->sc_cylin * type->heads + head) * type->sectrac + sec;
1427 if (block != fd->sc_blkno) {
1428 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
1429 #ifdef DDB
1430 Debugger();
1431 #endif
1432 }}
1433 #endif
1434 read = bp->b_flags & B_READ;
1435
1436 /* Setup for pseudo DMA */
1437 fdc->sc_data = bp->b_data + fd->sc_skip;
1438 fdc->sc_tc = fd->sc_nbytes;
1439
1440 bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
1441 fdc->sc_reg_drs, type->rate);
1442 #ifdef FD_DEBUG
1443 if (fdc_debug > 1)
1444 printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1445 read ? "read" : "write", fd->sc_drive,
1446 fd->sc_cylin, head, sec, nblks);
1447 #endif
1448 fdc->sc_state = IOCOMPLETE;
1449 fdc->sc_istate = ISTATE_DMA;
1450 fdc->sc_nstat = 0;
1451 if (finfo != NULL) {
1452 /* formatting */
1453 OUT_FDC(fdc, NE7CMD_FORMAT, IOTIMEDOUT);
1454 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1455 OUT_FDC(fdc, finfo->fd_formb_secshift, IOTIMEDOUT);
1456 OUT_FDC(fdc, finfo->fd_formb_nsecs, IOTIMEDOUT);
1457 OUT_FDC(fdc, finfo->fd_formb_gaplen, IOTIMEDOUT);
1458 OUT_FDC(fdc, finfo->fd_formb_fillbyte, IOTIMEDOUT);
1459 } else {
1460 if (read)
1461 OUT_FDC(fdc, NE7CMD_READ, IOTIMEDOUT);
1462 else
1463 OUT_FDC(fdc, NE7CMD_WRITE, IOTIMEDOUT);
1464 OUT_FDC(fdc, (head << 2) | fd->sc_drive, IOTIMEDOUT);
1465 OUT_FDC(fdc, fd->sc_cylin, IOTIMEDOUT); /*track*/
1466 OUT_FDC(fdc, head, IOTIMEDOUT);
1467 OUT_FDC(fdc, sec + 1, IOTIMEDOUT); /*sector+1*/
1468 OUT_FDC(fdc, type->secsize, IOTIMEDOUT);/*sector size*/
1469 OUT_FDC(fdc, type->sectrac, IOTIMEDOUT);/*secs/track*/
1470 OUT_FDC(fdc, type->gap1, IOTIMEDOUT); /*gap1 size*/
1471 OUT_FDC(fdc, type->datalen, IOTIMEDOUT);/*data length*/
1472 }
1473
1474 disk_busy(&fd->sc_dk);
1475
1476 /* allow 2 seconds for operation */
1477 timeout(fdctimeout, fdc, 2 * hz);
1478 return (1); /* will return later */
1479
1480 case SEEKWAIT:
1481 untimeout(fdctimeout, fdc);
1482 fdc->sc_state = SEEKCOMPLETE;
1483 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1484 /* allow 1/50 second for heads to settle */
1485 timeout(fdcpseudointr, fdc, hz / 50);
1486 return (1); /* will return later */
1487 }
1488 /*FALLTHROUGH*/
1489 case SEEKCOMPLETE:
1490 disk_unbusy(&fd->sc_dk, 0); /* no data on seek */
1491
1492 /* Make sure seek really happened. */
1493 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
1494 cyl != bp->b_cylin * fd->sc_type->step) {
1495 #ifdef FD_DEBUG
1496 if (fdc_debug)
1497 fdcstatus(&fd->sc_dv, 2, "seek failed");
1498 #endif
1499 fdcretry(fdc);
1500 goto loop;
1501 }
1502 fd->sc_cylin = bp->b_cylin;
1503 goto doio;
1504
1505 case IOTIMEDOUT:
1506 FTC_FLIP;
1507 (void)fdcresult(fdc);
1508 /*FALLTHROUGH*/
1509 case SEEKTIMEDOUT:
1510 case RECALTIMEDOUT:
1511 case RESETTIMEDOUT:
1512 fdcretry(fdc);
1513 goto loop;
1514
1515 case IOCOMPLETE: /* IO DONE, post-analyze */
1516 untimeout(fdctimeout, fdc);
1517
1518 disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid));
1519
1520 if (fdc->sc_nstat != 7 || st1 != 0 ||
1521 ((st0 & 0xf8) != 0 &&
1522 ((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
1523 #ifdef FD_DEBUG
1524 if (fdc_debug) {
1525 fdcstatus(&fd->sc_dv, 7,
1526 bp->b_flags & B_READ
1527 ? "read failed" : "write failed");
1528 printf("blkno %d nblks %d nstat %d tc %d\n",
1529 fd->sc_blkno, fd->sc_nblks,
1530 fdc->sc_nstat, fdc->sc_tc);
1531 }
1532 #endif
1533 if (fdc->sc_nstat == 7 &&
1534 (st1 & ST1_OVERRUN) == ST1_OVERRUN) {
1535
1536 /*
1537 * Silently retry overruns if no other
1538 * error bit is set. Adjust threshold.
1539 */
1540 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1541 if (thr < 15) {
1542 thr++;
1543 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1544 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1545 #ifdef FD_DEBUG
1546 if (fdc_debug)
1547 printf("fdc: %d -> threshold\n", thr);
1548 #endif
1549 fdconf(fdc);
1550 fdc->sc_overruns = 0;
1551 }
1552 if (++fdc->sc_overruns < 3) {
1553 fdc->sc_state = DOIO;
1554 goto loop;
1555 }
1556 }
1557 fdcretry(fdc);
1558 goto loop;
1559 }
1560 if (fdc->sc_errors) {
1561 diskerr(bp, "fd", "soft error", LOG_PRINTF,
1562 fd->sc_skip / FD_BSIZE(fd),
1563 (struct disklabel *)NULL);
1564 printf("\n");
1565 fdc->sc_errors = 0;
1566 } else {
1567 if (--fdc->sc_overruns < -20) {
1568 int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
1569 if (thr > 0) {
1570 thr--;
1571 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1572 fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
1573 #ifdef FD_DEBUG
1574 if (fdc_debug)
1575 printf("fdc: %d -> threshold\n", thr);
1576 #endif
1577 fdconf(fdc);
1578 }
1579 fdc->sc_overruns = 0;
1580 }
1581 }
1582 fd->sc_blkno += fd->sc_nblks;
1583 fd->sc_skip += fd->sc_nbytes;
1584 fd->sc_bcount -= fd->sc_nbytes;
1585 if (finfo == NULL && fd->sc_bcount > 0) {
1586 bp->b_cylin = fd->sc_blkno / fd->sc_type->seccyl;
1587 goto doseek;
1588 }
1589 fdfinish(fd, bp);
1590 goto loop;
1591
1592 case DORESET:
1593 doreset:
1594 /* try a reset, keep motor on */
1595 fd_set_motor(fdc);
1596 delay(100);
1597 fdc_reset(fdc);
1598 fdc->sc_nstat = 0;
1599 fdc->sc_istate = ISTATE_SENSEI;
1600 fdc->sc_state = RESETCOMPLETE;
1601 timeout(fdctimeout, fdc, hz / 2);
1602 return (1); /* will return later */
1603
1604 case RESETCOMPLETE:
1605 untimeout(fdctimeout, fdc);
1606 fdconf(fdc);
1607
1608 /* fall through */
1609 case DORECAL:
1610 fdc->sc_state = RECALWAIT;
1611 fdc->sc_istate = ISTATE_SENSEI;
1612 fdc->sc_nstat = 0;
1613 /* recalibrate function */
1614 OUT_FDC(fdc, NE7CMD_RECAL, RECALTIMEDOUT);
1615 OUT_FDC(fdc, fd->sc_drive, RECALTIMEDOUT);
1616 timeout(fdctimeout, fdc, 5 * hz);
1617 return (1); /* will return later */
1618
1619 case RECALWAIT:
1620 untimeout(fdctimeout, fdc);
1621 fdc->sc_state = RECALCOMPLETE;
1622 if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
1623 /* allow 1/30 second for heads to settle */
1624 timeout(fdcpseudointr, fdc, hz / 30);
1625 return (1); /* will return later */
1626 }
1627
1628 case RECALCOMPLETE:
1629 if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1630 #ifdef FD_DEBUG
1631 if (fdc_debug)
1632 fdcstatus(&fd->sc_dv, 2, "recalibrate failed");
1633 #endif
1634 fdcretry(fdc);
1635 goto loop;
1636 }
1637 fd->sc_cylin = 0;
1638 goto doseek;
1639
1640 case MOTORWAIT:
1641 if (fd->sc_flags & FD_MOTOR_WAIT)
1642 return (1); /* time's not up yet */
1643 goto doseek;
1644
1645 default:
1646 fdcstatus(&fd->sc_dv, 0, "stray interrupt");
1647 return (1);
1648 }
1649 #ifdef DIAGNOSTIC
1650 panic("fdcintr: impossible");
1651 #endif
1652 #undef st0
1653 #undef st1
1654 #undef cyl
1655 }
1656
1657 void
1658 fdcretry(fdc)
1659 struct fdc_softc *fdc;
1660 {
1661 char bits[64];
1662 struct fd_softc *fd;
1663 struct buf *bp;
1664
1665 fd = fdc->sc_drives.tqh_first;
1666 bp = fd->sc_q.b_actf;
1667
1668 fdc->sc_overruns = 0;
1669 if (fd->sc_opts & FDOPT_NORETRY)
1670 goto fail;
1671
1672 switch (fdc->sc_errors) {
1673 case 0:
1674 /* try again */
1675 fdc->sc_state =
1676 (fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
1677 break;
1678
1679 case 1: case 2: case 3:
1680 /* didn't work; try recalibrating */
1681 fdc->sc_state = DORECAL;
1682 break;
1683
1684 case 4:
1685 /* still no go; reset the bastard */
1686 fdc->sc_state = DORESET;
1687 break;
1688
1689 default:
1690 fail:
1691 if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1692 diskerr(bp, "fd", "hard error", LOG_PRINTF,
1693 fd->sc_skip / FD_BSIZE(fd),
1694 (struct disklabel *)NULL);
1695
1696 printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
1697 NE7_ST0BITS, bits, sizeof(bits)));
1698 printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
1699 NE7_ST1BITS, bits, sizeof(bits)));
1700 printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
1701 NE7_ST2BITS, bits, sizeof(bits)));
1702 printf(" cyl %d head %d sec %d)\n",
1703 fdc->sc_status[3], fdc->sc_status[4],
1704 fdc->sc_status[5]);
1705 }
1706
1707 bp->b_flags |= B_ERROR;
1708 bp->b_error = EIO;
1709 fdfinish(fd, bp);
1710 }
1711 fdc->sc_errors++;
1712 }
1713
1714 int
1715 fdsize(dev)
1716 dev_t dev;
1717 {
1718
1719 /* Swapping to floppies would not make sense. */
1720 return (-1);
1721 }
1722
1723 int
1724 fddump(dev, blkno, va, size)
1725 dev_t dev;
1726 daddr_t blkno;
1727 caddr_t va;
1728 size_t size;
1729 {
1730
1731 /* Not implemented. */
1732 return (EINVAL);
1733 }
1734
1735 int
1736 fdioctl(dev, cmd, addr, flag, p)
1737 dev_t dev;
1738 u_long cmd;
1739 caddr_t addr;
1740 int flag;
1741 struct proc *p;
1742 {
1743 struct fd_softc *fd;
1744 struct fdc_softc *fdc;
1745 struct fdformat_parms *form_parms;
1746 struct fdformat_cmd *form_cmd;
1747 struct ne7_fd_formb fd_formb;
1748 int il[FD_MAX_NSEC + 1];
1749 int unit;
1750 int i, j;
1751 int error;
1752
1753 unit = FDUNIT(dev);
1754 if (unit >= fd_cd.cd_ndevs)
1755 return (ENXIO);
1756
1757 fd = fd_cd.cd_devs[FDUNIT(dev)];
1758 fdc = (struct fdc_softc *)fd->sc_dv.dv_parent;
1759
1760 switch (cmd) {
1761 case DIOCGDINFO:
1762 *(struct disklabel *)addr = *(fd->sc_dk.dk_label);
1763 return 0;
1764
1765 case DIOCWLABEL:
1766 if ((flag & FWRITE) == 0)
1767 return EBADF;
1768 /* XXX do something */
1769 return (0);
1770
1771 case DIOCWDINFO:
1772 if ((flag & FWRITE) == 0)
1773 return (EBADF);
1774
1775 error = setdisklabel(fd->sc_dk.dk_label,
1776 (struct disklabel *)addr, 0,
1777 fd->sc_dk.dk_cpulabel);
1778 if (error)
1779 return (error);
1780
1781 error = writedisklabel(dev, fdstrategy,
1782 fd->sc_dk.dk_label,
1783 fd->sc_dk.dk_cpulabel);
1784 return (error);
1785
1786 case DIOCLOCK:
1787 /*
1788 * Nothing to do here, really.
1789 */
1790 return (0);
1791
1792 case DIOCEJECT:
1793 if (*(int *)addr == 0) {
1794 int part = DISKPART(dev);
1795 /*
1796 * Don't force eject: check that we are the only
1797 * partition open. If so, unlock it.
1798 */
1799 if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
1800 fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
1801 fd->sc_dk.dk_openmask) {
1802 return (EBUSY);
1803 }
1804 }
1805 /* FALLTHROUGH */
1806 case ODIOCEJECT:
1807 fd_do_eject(fd);
1808 return (0);
1809
1810 case FDIOCGETFORMAT:
1811 form_parms = (struct fdformat_parms *)addr;
1812 form_parms->fdformat_version = FDFORMAT_VERSION;
1813 form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1814 form_parms->ncyl = fd->sc_type->cylinders;
1815 form_parms->nspt = fd->sc_type->sectrac;
1816 form_parms->ntrk = fd->sc_type->heads;
1817 form_parms->stepspercyl = fd->sc_type->step;
1818 form_parms->gaplen = fd->sc_type->gap2;
1819 form_parms->fillbyte = fd->sc_type->fillbyte;
1820 form_parms->interleave = fd->sc_type->interleave;
1821 switch (fd->sc_type->rate) {
1822 case FDC_500KBPS:
1823 form_parms->xfer_rate = 500 * 1024;
1824 break;
1825 case FDC_300KBPS:
1826 form_parms->xfer_rate = 300 * 1024;
1827 break;
1828 case FDC_250KBPS:
1829 form_parms->xfer_rate = 250 * 1024;
1830 break;
1831 default:
1832 return (EINVAL);
1833 }
1834 return (0);
1835
1836 case FDIOCSETFORMAT:
1837 if ((flag & FWRITE) == 0)
1838 return (EBADF); /* must be opened for writing */
1839
1840 form_parms = (struct fdformat_parms *)addr;
1841 if (form_parms->fdformat_version != FDFORMAT_VERSION)
1842 return (EINVAL);/* wrong version of formatting prog */
1843
1844 i = form_parms->nbps >> 7;
1845 if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
1846 i & ~(1 << (ffs(i)-1)))
1847 /* not a power-of-two multiple of 128 */
1848 return (EINVAL);
1849
1850 switch (form_parms->xfer_rate) {
1851 case 500 * 1024:
1852 fd->sc_type->rate = FDC_500KBPS;
1853 break;
1854 case 300 * 1024:
1855 fd->sc_type->rate = FDC_300KBPS;
1856 break;
1857 case 250 * 1024:
1858 fd->sc_type->rate = FDC_250KBPS;
1859 break;
1860 default:
1861 return (EINVAL);
1862 }
1863
1864 if (form_parms->nspt > FD_MAX_NSEC ||
1865 form_parms->fillbyte > 0xff ||
1866 form_parms->interleave > 0xff)
1867 return EINVAL;
1868 fd->sc_type->sectrac = form_parms->nspt;
1869 if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1870 return EINVAL;
1871 fd->sc_type->heads = form_parms->ntrk;
1872 fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1873 fd->sc_type->secsize = ffs(i)-1;
1874 fd->sc_type->gap2 = form_parms->gaplen;
1875 fd->sc_type->cylinders = form_parms->ncyl;
1876 fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1877 form_parms->nbps / DEV_BSIZE;
1878 fd->sc_type->step = form_parms->stepspercyl;
1879 fd->sc_type->fillbyte = form_parms->fillbyte;
1880 fd->sc_type->interleave = form_parms->interleave;
1881 return (0);
1882
1883 case FDIOCFORMAT_TRACK:
1884 if((flag & FWRITE) == 0)
1885 /* must be opened for writing */
1886 return (EBADF);
1887 form_cmd = (struct fdformat_cmd *)addr;
1888 if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1889 /* wrong version of formatting prog */
1890 return (EINVAL);
1891
1892 if (form_cmd->head >= fd->sc_type->heads ||
1893 form_cmd->cylinder >= fd->sc_type->cylinders) {
1894 return (EINVAL);
1895 }
1896
1897 fd_formb.head = form_cmd->head;
1898 fd_formb.cyl = form_cmd->cylinder;
1899 fd_formb.transfer_rate = fd->sc_type->rate;
1900 fd_formb.fd_formb_secshift = fd->sc_type->secsize;
1901 fd_formb.fd_formb_nsecs = fd->sc_type->sectrac;
1902 fd_formb.fd_formb_gaplen = fd->sc_type->gap2;
1903 fd_formb.fd_formb_fillbyte = fd->sc_type->fillbyte;
1904
1905 bzero(il, sizeof il);
1906 for (j = 0, i = 1; i <= fd_formb.fd_formb_nsecs; i++) {
1907 while (il[(j%fd_formb.fd_formb_nsecs) + 1])
1908 j++;
1909 il[(j%fd_formb.fd_formb_nsecs) + 1] = i;
1910 j += fd->sc_type->interleave;
1911 }
1912 for (i = 0; i < fd_formb.fd_formb_nsecs; i++) {
1913 fd_formb.fd_formb_cylno(i) = form_cmd->cylinder;
1914 fd_formb.fd_formb_headno(i) = form_cmd->head;
1915 fd_formb.fd_formb_secno(i) = il[i+1];
1916 fd_formb.fd_formb_secsize(i) = fd->sc_type->secsize;
1917 }
1918
1919 return fdformat(dev, &fd_formb, p);
1920
1921 case FDIOCGETOPTS: /* get drive options */
1922 *(int *)addr = fd->sc_opts;
1923 return (0);
1924
1925 case FDIOCSETOPTS: /* set drive options */
1926 fd->sc_opts = *(int *)addr;
1927 return (0);
1928
1929 #ifdef DEBUG
1930 case _IO('f', 100):
1931 out_fdc(fdc, NE7CMD_DUMPREG);
1932 fdcresult(fdc);
1933 printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
1934 for (i = 0; i < fdc->sc_nstat; i++)
1935 printf(" 0x%x", fdc->sc_status[i]);
1936 printf(">\n");
1937 return (0);
1938
1939 case _IOW('f', 101, int):
1940 fdc->sc_cfg &= ~CFG_THRHLD_MASK;
1941 fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
1942 fdconf(fdc);
1943 return (0);
1944
1945 case _IO('f', 102):
1946 out_fdc(fdc, NE7CMD_SENSEI);
1947 fdcresult(fdc);
1948 printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
1949 for (i=0; i< fdc->sc_nstat; i++)
1950 printf(" 0x%x", fdc->sc_status[i]);
1951 printf(">\n");
1952 return (0);
1953 #endif
1954 default:
1955 return (ENOTTY);
1956 }
1957
1958 #ifdef DIAGNOSTIC
1959 panic("fdioctl: impossible");
1960 #endif
1961 }
1962
1963 int
1964 fdformat(dev, finfo, p)
1965 dev_t dev;
1966 struct ne7_fd_formb *finfo;
1967 struct proc *p;
1968 {
1969 int rv = 0, s;
1970 struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
1971 struct fd_type *type = fd->sc_type;
1972 struct buf *bp;
1973
1974 /* set up a buffer header for fdstrategy() */
1975 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT);
1976 if (bp == 0)
1977 return (ENOBUFS);
1978
1979 PHOLD(p);
1980 bzero((void *)bp, sizeof(struct buf));
1981 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT;
1982 bp->b_proc = p;
1983 bp->b_dev = dev;
1984
1985 /*
1986 * Calculate a fake blkno, so fdstrategy() would initiate a
1987 * seek to the requested cylinder.
1988 */
1989 bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
1990 + finfo->head * type->sectrac) * FD_BSIZE(fd))
1991 / DEV_BSIZE;
1992
1993 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1994 bp->b_data = (caddr_t)finfo;
1995
1996 #ifdef FD_DEBUG
1997 if (fdc_debug)
1998 printf("fdformat: blkno 0x%x count %ld\n",
1999 bp->b_blkno, bp->b_bcount);
2000 #endif
2001
2002 /* now do the format */
2003 fdstrategy(bp);
2004
2005 /* ...and wait for it to complete */
2006 s = splbio();
2007 while (!(bp->b_flags & B_DONE)) {
2008 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz);
2009 if (rv == EWOULDBLOCK)
2010 break;
2011 }
2012 splx(s);
2013
2014 if (rv == EWOULDBLOCK) {
2015 /* timed out */
2016 rv = EIO;
2017 biodone(bp);
2018 }
2019 if (bp->b_flags & B_ERROR) {
2020 rv = bp->b_error;
2021 }
2022 PRELE(p);
2023 free(bp, M_TEMP);
2024 return (rv);
2025 }
2026
2027 void
2028 fdgetdisklabel(dev)
2029 dev_t dev;
2030 {
2031 int unit = FDUNIT(dev), i;
2032 struct fd_softc *fd = fd_cd.cd_devs[unit];
2033 struct disklabel *lp = fd->sc_dk.dk_label;
2034 struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
2035
2036 bzero(lp, sizeof(struct disklabel));
2037 bzero(lp, sizeof(struct cpu_disklabel));
2038
2039 lp->d_type = DTYPE_FLOPPY;
2040 lp->d_secsize = FD_BSIZE(fd);
2041 lp->d_secpercyl = fd->sc_type->seccyl;
2042 lp->d_nsectors = fd->sc_type->sectrac;
2043 lp->d_ncylinders = fd->sc_type->cylinders;
2044 lp->d_ntracks = fd->sc_type->heads; /* Go figure... */
2045 lp->d_rpm = 3600; /* XXX like it matters... */
2046
2047 strncpy(lp->d_typename, "floppy", sizeof(lp->d_typename));
2048 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
2049 lp->d_interleave = 1;
2050
2051 lp->d_partitions[RAW_PART].p_offset = 0;
2052 lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
2053 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
2054 lp->d_npartitions = RAW_PART + 1;
2055
2056 lp->d_magic = DISKMAGIC;
2057 lp->d_magic2 = DISKMAGIC;
2058 lp->d_checksum = dkcksum(lp);
2059
2060 /*
2061 * Call the generic disklabel extraction routine. If there's
2062 * not a label there, fake it.
2063 */
2064 if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
2065 strncpy(lp->d_packname, "default label",
2066 sizeof(lp->d_packname));
2067 /*
2068 * Reset the partition info; it might have gotten
2069 * trashed in readdisklabel().
2070 *
2071 * XXX Why do we have to do this? readdisklabel()
2072 * should be safe...
2073 */
2074 for (i = 0; i < MAXPARTITIONS; ++i) {
2075 lp->d_partitions[i].p_offset = 0;
2076 if (i == RAW_PART) {
2077 lp->d_partitions[i].p_size =
2078 lp->d_secpercyl * lp->d_ncylinders;
2079 lp->d_partitions[i].p_fstype = FS_BSDFFS;
2080 } else {
2081 lp->d_partitions[i].p_size = 0;
2082 lp->d_partitions[i].p_fstype = FS_UNUSED;
2083 }
2084 }
2085 lp->d_npartitions = RAW_PART + 1;
2086 }
2087 }
2088
2089 void
2090 fd_do_eject(fd)
2091 struct fd_softc *fd;
2092 {
2093 struct fdc_softc *fdc = (void *)fd->sc_dv.dv_parent;
2094
2095 if (CPU_ISSUN4C) {
2096 auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
2097 delay(10);
2098 auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
2099 return;
2100 }
2101 if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077)) {
2102 bus_space_tag_t t = fdc->sc_bustag;
2103 bus_space_handle_t h = fdc->sc_handle;
2104 u_int8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
2105
2106 bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
2107 delay(10);
2108 bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
2109 return;
2110 }
2111 }
2112
2113 #ifdef MEMORY_DISK_HOOKS
2114 int fd_read_md_image __P((size_t *, caddr_t *));
2115 #endif
2116
2117 /* ARGSUSED */
2118 void
2119 fd_mountroot_hook(dev)
2120 struct device *dev;
2121 {
2122 int c;
2123
2124 fd_do_eject((struct fd_softc *)dev);
2125 printf("Insert filesystem floppy and press return.");
2126 for (;;) {
2127 c = cngetc();
2128 if ((c == '\r') || (c == '\n')) {
2129 printf("\n");
2130 break;
2131 }
2132 }
2133 }
2134
2135 #ifdef MEMORY_DISK_HOOKS
2136
2137 #define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
2138
2139 int
2140 fd_read_md_image(sizep, addrp)
2141 size_t *sizep;
2142 caddr_t *addrp;
2143 {
2144 struct buf buf, *bp = &buf;
2145 dev_t dev;
2146 off_t offset;
2147 caddr_t addr;
2148
2149 dev = makedev(54,0); /* XXX */
2150
2151 MALLOC(addr, caddr_t, FDMICROROOTSIZE, M_DEVBUF, M_WAITOK);
2152 *addrp = addr;
2153
2154 if (fdopen(dev, 0, S_IFCHR, NULL))
2155 panic("fd: mountroot: fdopen");
2156
2157 offset = 0;
2158
2159 for (;;) {
2160 bp->b_dev = dev;
2161 bp->b_error = 0;
2162 bp->b_resid = 0;
2163 bp->b_proc = NULL;
2164 bp->b_flags = B_BUSY | B_PHYS | B_RAW | B_READ;
2165 bp->b_blkno = btodb(offset);
2166 bp->b_bcount = DEV_BSIZE;
2167 bp->b_data = addr;
2168 fdstrategy(bp);
2169 while ((bp->b_flags & B_DONE) == 0) {
2170 tsleep((caddr_t)bp, PRIBIO + 1, "physio", 0);
2171 }
2172 if (bp->b_error)
2173 panic("fd: mountroot: fdread error %d", bp->b_error);
2174
2175 if (bp->b_resid != 0)
2176 break;
2177
2178 addr += DEV_BSIZE;
2179 offset += DEV_BSIZE;
2180 if (offset + DEV_BSIZE > FDMICROROOTSIZE)
2181 break;
2182 }
2183 (void)fdclose(dev, 0, S_IFCHR, NULL);
2184 *sizep = offset;
2185 fd_do_eject(fd_cd.cd_devs[FDUNIT(dev)]);
2186 return (0);
2187 }
2188 #endif
2189