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