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