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