fd.c revision 1.31 1 /* $NetBSD: fd.c,v 1.31 1996/04/30 06:09:51 mhitch Exp $ */
2
3 /*
4 * Copyright (c) 1994 Christian E. Hopps
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christian E. Hopps.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/buf.h>
37 #include <sys/device.h>
38 #include <sys/ioctl.h>
39 #include <sys/fcntl.h>
40 #include <sys/disklabel.h>
41 #include <sys/disk.h>
42 #include <sys/dkbad.h>
43 #include <sys/proc.h>
44 #include <machine/cpu.h>
45 #include <amiga/amiga/device.h>
46 #include <amiga/amiga/custom.h>
47 #include <amiga/amiga/cia.h>
48 #include <amiga/amiga/cc.h>
49
50 #include <sys/conf.h>
51 #include <machine/conf.h>
52
53 enum fdc_bits { FDB_CHANGED = 2, FDB_PROTECT, FDB_CYLZERO, FDB_READY };
54 /*
55 * partitions in fd represent different format floppies
56 * partition a is 0 etc..
57 */
58 enum fd_parttypes {
59 FDAMIGAPART = 0,
60 #ifdef not_yet
61 FDMSDOSPART,
62 #endif
63 FDMAXPARTS
64 };
65
66 #define FDBBSIZE (8192)
67 #define FDSBSIZE (8192)
68
69 #define b_cylin b_resid
70 #define FDUNIT(dev) DISKUNIT(dev)
71 #define FDPART(dev) DISKPART(dev)
72 #define FDMAKEDEV(m, u, p) MAKEDISKDEV((m), (u), (p))
73
74 #define FDNHEADS (2) /* amiga drives always have 2 heads */
75 #define FDSECSIZE (512) /* amiga drives always have 512 byte sectors */
76 #define FDSECLWORDS (128)
77
78 #define FDSETTLEDELAY (18000) /* usec delay after seeking after switch dir */
79 #define FDSTEPDELAY (3500) /* usec delay after steping */
80 #define FDPRESIDEDELAY (1000) /* usec delay before writing can occur */
81 #define FDWRITEDELAY (1300) /* usec delay after write */
82
83 #define FDSTEPOUT (1) /* decrease track step */
84 #define FDSTEPIN (0) /* increase track step */
85
86 #define FDCUNITMASK (0x78) /* mask for all units (bits 6-3) */
87
88 #define FDRETRIES (2) /* default number of retries */
89 #define FDMAXUNITS (4) /* maximum number of supported units */
90
91 #define DISKLEN_READ (0) /* fake mask for reading */
92 #define DISKLEN_WRITE (1 << 14) /* bit for writing */
93 #define DISKLEN_DMAEN (1 << 15) /* dma go */
94 #define DMABUFSZ ((DISKLEN_WRITE - 1) * 2) /* largest dma possible */
95
96 #define FDMFMSYNC (0x4489)
97
98 /*
99 * floppy device type
100 */
101 struct fdtype {
102 u_int driveid; /* drive identification (from drive) */
103 u_int ncylinders; /* number of cylinders on drive */
104 u_int amiga_nsectors; /* number of sectors per amiga track */
105 u_int msdos_nsectors; /* number of sectors per msdos track */
106 u_int nreadw; /* number of words (short) read per track */
107 u_int nwritew; /* number of words (short) written per track */
108 u_int gap; /* track gap size in long words */
109 u_int precomp[2]; /* 1st and 2nd precomp values */
110 char *desc; /* description of drive type (useq) */
111 };
112
113 /*
114 * floppy disk device data
115 */
116 struct fd_softc {
117 struct device sc_dv; /* generic device info; must come first */
118 struct disk dkdev; /* generic disk info */
119 struct buf bufq; /* queue of buf's */
120 struct fdtype *type;
121 void *cachep; /* cached track data (write through) */
122 int cachetrk; /* cahced track -1 for none */
123 int hwunit; /* unit for amiga controlling hw */
124 int unitmask; /* mask for cia select deslect */
125 int pstepdir; /* previous step direction */
126 int curcyl; /* current curcyl head positioned on */
127 int flags; /* misc flags */
128 int wlabel;
129 int stepdelay; /* useq to delay after seek user setable */
130 int nsectors; /* number of sectors per track */
131 int openpart; /* which partition [ab] == [12] is open */
132 short retries; /* number of times to retry failed io */
133 short retried; /* number of times current io retried */
134 };
135
136 /* fd_softc->flags */
137 #define FDF_MOTORON (0x01) /* motor is running */
138 #define FDF_MOTOROFF (0x02) /* motor is waiting to be turned off */
139 #define FDF_WMOTOROFF (0x04) /* unit wants a wakeup after off */
140 #define FDF_DIRTY (0x08) /* track cache needs write */
141 #define FDF_WRITEWAIT (0x10) /* need to head select delay on next setpos */
142 #define FDF_HAVELABEL (0x20) /* label is valid */
143 #define FDF_JUSTFLUSH (0x40) /* don't bother caching track. */
144 #define FDF_NOTRACK0 (0x80) /* was not able to recalibrate drive */
145
146 int fdc_wantwakeup;
147 int fdc_side;
148 void *fdc_dmap;
149 struct fd_softc *fdc_indma;
150 int fdc_dmalen;
151 int fdc_dmawrite;
152
153 struct fdcargs {
154 struct fdtype *type;
155 int unit;
156 };
157
158 int fdcmatch __P((struct device *, void *, void *));
159 void fdcattach __P((struct device *, struct device *, void *));
160 int fdcprint __P((void *, char *));
161 int fdmatch __P((struct device *, void *, void *));
162 void fdattach __P((struct device *, struct device *, void *));
163
164 void fdintr __P((int));
165 void fdidxintr __P((void));
166 void fdstrategy __P((struct buf *));
167 int fdloaddisk __P((struct fd_softc *));
168 int fdgetdisklabel __P((struct fd_softc *, dev_t));
169 int fdsetdisklabel __P((struct fd_softc *, struct disklabel *));
170 int fdputdisklabel __P((struct fd_softc *, dev_t));
171 struct fdtype * fdcgetfdtype __P((int));
172 void fdmotoroff __P((void *));
173 void fdsetpos __P((struct fd_softc *, int, int));
174 void fdselunit __P((struct fd_softc *));
175 void fdstart __P((struct fd_softc *));
176 void fdcont __P((struct fd_softc *));
177 void fddmastart __P((struct fd_softc *, int));
178 void fdcalibrate __P((void *));
179 void fddmadone __P((struct fd_softc *, int));
180 void fddone __P((struct fd_softc *));
181 void fdfindwork __P((int));
182 void fdminphys __P((struct buf *));
183 void fdcachetoraw __P((struct fd_softc *));
184 u_long *fdfindsync __P((u_long *, u_long *));
185 int fdrawtocache __P((struct fd_softc *));
186 u_long *mfmblkencode __P((u_long *, u_long *, u_long *, int));
187 u_long *mfmblkdecode __P((u_long *, u_long *, u_long *, int));
188
189 struct dkdriver fddkdriver = { fdstrategy };
190
191 /*
192 * read size is (nsectors + 1) * mfm secsize + gap bytes + 2 shorts
193 * write size is nsectors * mfm secsize + gap bytes + 3 shorts
194 * the extra shorts are to deal with a dma hw bug in the controller
195 * they are probably too much (I belive the bug is 1 short on write and
196 * 3 bits on read) but there is no need to be cheap here.
197 */
198 #define MAXTRKSZ (22 * FDSECSIZE)
199 struct fdtype fdtype[] = {
200 { 0x00000000, 80, 11, 9, 7358, 6815, 414, { 80, 161 }, "3.5dd" },
201 { 0x55555555, 40, 11, 9, 7358, 6815, 414, { 80, 161 }, "5.25dd" },
202 { 0xAAAAAAAA, 80, 22, 18, 14716, 13630, 828, { 80, 161 }, "3.5hd" }
203 };
204 int nfdtype = sizeof(fdtype) / sizeof(*fdtype);
205
206 struct cfattach fd_ca = {
207 sizeof(struct fd_softc), fdmatch, fdattach
208 };
209
210 struct cfdriver fd_cd = {
211 NULL, "fd", DV_DISK, NULL, 0
212 };
213
214 struct cfattach fdc_ca = {
215 sizeof(struct device), fdcmatch, fdcattach
216 };
217
218 struct cfdriver fdc_cd = {
219 NULL, "fdc", DV_DULL, NULL, 0
220 };
221
222 /*
223 * all hw access through macros, this helps to hide the active low
224 * properties
225 */
226
227 #define FDUNITMASK(unit) (1 << (3 + (unit)))
228
229 /*
230 * select units using mask
231 */
232 #define FDSELECT(um) do { ciab.prb &= ~(um); } while (0)
233
234 /*
235 * deselect units using mask
236 */
237 #define FDDESELECT(um) do { ciab.prb |= (um); delay(1); } while (0)
238
239 /*
240 * test hw condition bits
241 */
242 #define FDTESTC(bit) ((ciaa.pra & (1 << (bit))) == 0)
243
244 /*
245 * set motor for select units, true motor on else off
246 */
247 #define FDSETMOTOR(on) do { \
248 if (on) ciab.prb &= ~CIAB_PRB_MTR; else ciab.prb |= CIAB_PRB_MTR; \
249 } while (0)
250
251 /*
252 * set head for select units
253 */
254 #define FDSETHEAD(head) do { \
255 if (head) ciab.prb &= ~CIAB_PRB_SIDE; else ciab.prb |= CIAB_PRB_SIDE; \
256 delay(1); } while (0)
257
258 /*
259 * select direction, true towards spindle else outwards
260 */
261 #define FDSETDIR(in) do { \
262 if (in) ciab.prb &= ~CIAB_PRB_DIR; else ciab.prb |= CIAB_PRB_DIR; \
263 delay(1); } while (0)
264
265 /*
266 * step the selected units
267 */
268 #define FDSTEP do { \
269 ciab.prb &= ~CIAB_PRB_STEP; ciab.prb |= CIAB_PRB_STEP; \
270 } while (0)
271
272 #define FDDMASTART(len, towrite) do { \
273 int dmasz = (len) | ((towrite) ? DISKLEN_WRITE : 0) | DISKLEN_DMAEN; \
274 custom.dsklen = dmasz; custom.dsklen = dmasz; } while (0)
275
276 #define FDDMASTOP do { custom.dsklen = 0; } while (0)
277
278
279 int
280 fdcmatch(pdp, match, auxp)
281 struct device *pdp;
282 void *match, *auxp;
283 {
284 struct cfdata *cfp = match;
285
286 if (matchname("fdc", auxp) == 0 || cfp->cf_unit != 0)
287 return(0);
288 if ((fdc_dmap = alloc_chipmem(DMABUFSZ)) == NULL) {
289 printf("fdc: unable to allocate dma buffer\n");
290 return(0);
291 }
292 return(1);
293 }
294
295 void
296 fdcattach(pdp, dp, auxp)
297 struct device *pdp, *dp;
298 void *auxp;
299 {
300 struct fdcargs args;
301
302 printf(": dmabuf pa 0x%x\n", kvtop(fdc_dmap));
303 args.unit = 0;
304 args.type = fdcgetfdtype(args.unit);
305
306 fdc_side = -1;
307 config_found(dp, &args, fdcprint);
308 for (args.unit++; args.unit < FDMAXUNITS; args.unit++) {
309 if ((args.type = fdcgetfdtype(args.unit)) == NULL)
310 continue;
311 config_found(dp, &args, fdcprint);
312 }
313 }
314
315 int
316 fdcprint(auxp, pnp)
317 void *auxp;
318 char *pnp;
319 {
320 struct fdcargs *fcp;
321
322 fcp = auxp;
323 if (pnp)
324 printf("fd%d at %s unit %d:", fcp->unit, pnp,
325 fcp->type->driveid);
326 return(UNCONF);
327 }
328
329 /*ARGSUSED*/
330 int
331 fdmatch(pdp, match, auxp)
332 struct device *pdp;
333 void *match, *auxp;
334 {
335 struct cfdata *cfp = match;
336
337 #define cf_unit cf_loc[0]
338 struct fdcargs *fdap;
339
340 fdap = auxp;
341 if (cfp->cf_unit == fdap->unit || cfp->cf_unit == -1)
342 return(1);
343 return(0);
344 #undef cf_unit
345 }
346
347 void
348 fdattach(pdp, dp, auxp)
349 struct device *pdp, *dp;
350 void *auxp;
351 {
352 struct fdcargs *ap;
353 struct fd_softc *sc;
354
355 ap = auxp;
356 sc = (struct fd_softc *)dp;
357
358 sc->curcyl = sc->cachetrk = -1;
359 sc->openpart = -1;
360 sc->type = ap->type;
361 sc->hwunit = ap->unit;
362 sc->unitmask = 1 << (3 + ap->unit);
363 sc->retries = FDRETRIES;
364 sc->stepdelay = FDSTEPDELAY;
365 printf(" unit %d: %s %d cyl, %d head, %d sec [%d sec], 512 bytes/sec\n",
366 sc->hwunit, sc->type->desc, sc->type->ncylinders, FDNHEADS,
367 sc->type->amiga_nsectors, sc->type->msdos_nsectors);
368
369 /*
370 * Initialize and attach the disk structure.
371 */
372 sc->dkdev.dk_name = sc->sc_dv.dv_xname;
373 sc->dkdev.dk_driver = &fddkdriver;
374 disk_attach(&sc->dkdev);
375
376 /*
377 * calibrate the drive
378 */
379 fdsetpos(sc, 0, 0);
380 fdsetpos(sc, sc->type->ncylinders, 0);
381 fdsetpos(sc, 0, 0);
382 fdmotoroff(sc);
383
384 /*
385 * enable disk related interrupts
386 */
387 custom.dmacon = DMAF_SETCLR | DMAF_MASTER | DMAF_DISK;
388 custom.intena = INTF_SETCLR | INTF_DSKBLK;
389 ciab.icr = CIA_ICR_FLG;
390 }
391
392 /*ARGSUSED*/
393 int
394 fdopen(dev, flags, devtype, p)
395 dev_t dev;
396 int flags, devtype;
397 struct proc *p;
398 {
399 struct fd_softc *sc;
400 int wasopen, fwork, error, s;
401
402 error = 0;
403
404 if (FDPART(dev) >= FDMAXPARTS)
405 return(ENXIO);
406
407 if ((sc = getsoftc(fd_cd, FDUNIT(dev))) == NULL)
408 return(ENXIO);
409 if (sc->flags & FDF_NOTRACK0)
410 return(ENXIO);
411 if (sc->cachep == NULL)
412 sc->cachep = malloc(MAXTRKSZ, M_DEVBUF, M_WAITOK);
413
414 s = splbio();
415 /*
416 * if we are sleeping in fdclose(); waiting for a chance to
417 * shut the motor off, do a sleep here also.
418 */
419 while (sc->flags & FDF_WMOTOROFF)
420 tsleep(fdmotoroff, PRIBIO, "fdopen", 0);
421
422 fwork = 0;
423 /*
424 * if not open let user open request type, otherwise
425 * ensure they are trying to open same type.
426 */
427 if (sc->openpart == FDPART(dev))
428 wasopen = 1;
429 else if (sc->openpart == -1) {
430 sc->openpart = FDPART(dev);
431 wasopen = 0;
432 } else {
433 wasopen = 1;
434 error = EPERM;
435 goto done;
436 }
437
438 /*
439 * wait for current io to complete if any
440 */
441 if (fdc_indma) {
442 fwork = 1;
443 fdc_wantwakeup++;
444 tsleep(fdopen, PRIBIO, "fdopen", 0);
445 }
446 if ((error = fdloaddisk(sc)) != 0)
447 goto done;
448 if ((error = fdgetdisklabel(sc, dev)) != 0)
449 goto done;
450 #ifdef FDDEBUG
451 printf(" open successful\n");
452 #endif
453 done:
454 /*
455 * if we requested that fddone()->fdfindwork() wake us, allow it to
456 * complete its job now
457 */
458 if (fwork)
459 fdfindwork(FDUNIT(dev));
460 splx(s);
461
462 /*
463 * if we were not open and we marked us so reverse that.
464 */
465 if (error && wasopen == 0)
466 sc->openpart = 0;
467 return(error);
468 }
469
470 /*ARGSUSED*/
471 int
472 fdclose(dev, flags, devtype, p)
473 dev_t dev;
474 int flags, devtype;
475 struct proc *p;
476 {
477 struct fd_softc *sc;
478 int s;
479
480 #ifdef FDDEBUG
481 printf("fdclose()\n");
482 #endif
483 sc = getsoftc(fd_cd, FDUNIT(dev));
484 s = splbio();
485 if (sc->flags & FDF_MOTORON) {
486 sc->flags |= FDF_WMOTOROFF;
487 tsleep(fdmotoroff, PRIBIO, "fdclose", 0);
488 sc->flags &= ~FDF_WMOTOROFF;
489 wakeup(fdmotoroff);
490 }
491 sc->openpart = -1;
492 splx(s);
493 return(0);
494 }
495
496 int
497 fdioctl(dev, cmd, addr, flag, p)
498 dev_t dev;
499 u_long cmd;
500 caddr_t addr;
501 int flag;
502 struct proc *p;
503 {
504 struct fd_softc *sc;
505 int error, wlab;
506
507 sc = getsoftc(fd_cd, FDUNIT(dev));
508
509 if ((sc->flags & FDF_HAVELABEL) == 0)
510 return(EBADF);
511
512 switch (cmd) {
513 case DIOCSBAD:
514 return(EINVAL);
515 case DIOCSRETRIES:
516 if (*(int *)addr < 0)
517 return(EINVAL);
518 sc->retries = *(int *)addr;
519 return(0);
520 case DIOCSSTEP:
521 if (*(int *)addr < FDSTEPDELAY)
522 return(EINVAL);
523 sc->dkdev.dk_label->d_trkseek = sc->stepdelay = *(int *)addr;
524 return(0);
525 case DIOCGDINFO:
526 *(struct disklabel *)addr = *(sc->dkdev.dk_label);
527 return(0);
528 case DIOCGPART:
529 ((struct partinfo *)addr)->disklab = sc->dkdev.dk_label;
530 ((struct partinfo *)addr)->part =
531 &sc->dkdev.dk_label->d_partitions[FDPART(dev)];
532 return(0);
533 case DIOCSDINFO:
534 if ((flag & FWRITE) == 0)
535 return(EBADF);
536 return(fdsetdisklabel(sc, (struct disklabel *)addr));
537 case DIOCWDINFO:
538 if ((flag & FWRITE) == 0)
539 return(EBADF);
540 if ((error = fdsetdisklabel(sc, (struct disklabel *)addr)) != 0)
541 return(error);
542 wlab = sc->wlabel;
543 sc->wlabel = 1;
544 error = fdputdisklabel(sc, dev);
545 sc->wlabel = wlab;
546 return(error);
547 case DIOCWLABEL:
548 if ((flag & FWRITE) == 0)
549 return(EBADF);
550 sc->wlabel = *(int *)addr;
551 return(0);
552 default:
553 return(ENOTTY);
554 }
555 }
556
557 /*
558 * no dumps to floppy disks thank you.
559 */
560 int
561 fdsize(dev)
562 dev_t dev;
563 {
564 return(-1);
565 }
566
567 int
568 fdread(dev, uio, flags)
569 dev_t dev;
570 struct uio *uio;
571 int flags;
572 {
573 return (physio(fdstrategy, NULL, dev, B_READ, fdminphys, uio));
574 }
575
576 int
577 fdwrite(dev, uio, flags)
578 dev_t dev;
579 struct uio *uio;
580 int flags;
581 {
582 return (physio(fdstrategy, NULL, dev, B_WRITE, fdminphys, uio));
583 }
584
585
586 void
587 fdintr(flag)
588 int flag;
589 {
590 int s;
591
592 s = splbio();
593 if (fdc_indma)
594 fddmadone(fdc_indma, 0);
595 splx(s);
596 }
597
598 void
599 fdidxintr()
600 {
601 if (fdc_indma && fdc_dmalen) {
602 /*
603 * turn off intr and start actual dma
604 */
605 ciab.icr = CIA_ICR_FLG;
606 FDDMASTART(fdc_dmalen, fdc_dmawrite);
607 fdc_dmalen = 0;
608 }
609 }
610
611 void
612 fdstrategy(bp)
613 struct buf *bp;
614 {
615 struct disklabel *lp;
616 struct fd_softc *sc;
617 struct buf *dp;
618 int unit, part, s;
619
620 unit = FDUNIT(bp->b_dev);
621 part = FDPART(bp->b_dev);
622 sc = getsoftc(fd_cd, unit);
623
624 #ifdef FDDEBUG
625 printf("fdstrategy: 0x%x\n", bp);
626 #endif
627 /*
628 * check for valid partition and bounds
629 */
630 lp = sc->dkdev.dk_label;
631 if ((sc->flags & FDF_HAVELABEL) == 0) {
632 bp->b_error = EIO;
633 goto bad;
634 }
635 if (bounds_check_with_label(bp, lp, sc->wlabel) <= 0)
636 goto done;
637
638 /*
639 * trans count of zero or bounds check indicates io is done
640 * we are done.
641 */
642 if (bp->b_bcount == 0)
643 goto done;
644
645 /*
646 * queue the buf and kick the low level code
647 */
648 s = splbio();
649 dp = &sc->bufq;
650 disksort(dp, bp);
651 fdstart(sc);
652 splx(s);
653 return;
654 bad:
655 bp->b_flags |= B_ERROR;
656 done:
657 bp->b_resid = bp->b_bcount;
658 biodone(bp);
659 }
660
661 /*
662 * make sure disk is loaded and label is up-to-date.
663 */
664 int
665 fdloaddisk(sc)
666 struct fd_softc *sc;
667 {
668 /*
669 * if diskchange is low step drive to 0 then up one then to zero.
670 */
671 fdselunit(sc); /* make sure the unit is selected */
672 if (FDTESTC(FDB_CHANGED)) {
673 fdsetpos(sc, 0, 0);
674 sc->cachetrk = -1; /* invalidate the cache */
675 sc->flags &= ~FDF_HAVELABEL;
676 fdsetpos(sc, FDNHEADS, 0);
677 fdsetpos(sc, 0, 0);
678 if (FDTESTC(FDB_CHANGED)) {
679 fdmotoroff(sc);
680 return(ENXIO);
681 }
682 }
683 fdmotoroff(sc);
684 sc->type = fdcgetfdtype(sc->hwunit);
685 if (sc->type == NULL)
686 return(ENXIO);
687 #ifdef not_yet
688 if (sc->openpart == FDMSDOSPART)
689 sc->nsectors = sc->type->msdos_nsectors;
690 else
691 #endif
692 sc->nsectors = sc->type->amiga_nsectors;
693 return(0);
694 }
695
696 /*
697 * read disk label, if present otherwise create one
698 * return a new label if raw part and none found, otherwise err.
699 */
700 int
701 fdgetdisklabel(sc, dev)
702 struct fd_softc *sc;
703 dev_t dev;
704 {
705 struct disklabel *lp, *dlp;
706 struct cpu_disklabel *clp;
707 struct buf *bp;
708 int error, part;
709
710 if (sc->flags & FDF_HAVELABEL)
711 return(0);
712 #ifdef FDDEBUG
713 printf("fdgetdisklabel()\n");
714 #endif
715 part = FDPART(dev);
716 lp = sc->dkdev.dk_label;
717 clp = sc->dkdev.dk_cpulabel;
718 bzero(lp, sizeof(struct disklabel));
719 bzero(clp, sizeof(struct cpu_disklabel));
720
721 lp->d_secsize = FDSECSIZE;
722 lp->d_ntracks = FDNHEADS;
723 lp->d_ncylinders = sc->type->ncylinders;
724 lp->d_nsectors = sc->nsectors;
725 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
726 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
727 lp->d_npartitions = part + 1;
728 lp->d_partitions[part].p_size = lp->d_secperunit;
729 lp->d_partitions[part].p_fstype = FS_UNUSED;
730 lp->d_partitions[part].p_fsize = 1024;
731 lp->d_partitions[part].p_frag = 8;
732 lp->d_partitions[part].p_cpg = 2; /* for adosfs: reserved blks */
733
734 sc->flags |= FDF_HAVELABEL;
735
736 bp = (void *)geteblk((int)lp->d_secsize);
737 bp->b_dev = dev;
738 bp->b_blkno = 0;
739 bp->b_cylin = 0;
740 bp->b_bcount = FDSECSIZE;
741 bp->b_flags = B_BUSY | B_READ;
742 fdstrategy(bp);
743 if ((error = biowait(bp)) != 0)
744 goto nolabel;
745 dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
746 if (dlp->d_magic != DISKMAGIC || dlp->d_magic2 != DISKMAGIC ||
747 dkcksum(dlp)) {
748 error = EINVAL;
749 goto nolabel;
750 }
751 bcopy(dlp, lp, sizeof(struct disklabel));
752 if (lp->d_trkseek > FDSTEPDELAY)
753 sc->stepdelay = lp->d_trkseek;
754 brelse(bp);
755 return(0);
756 nolabel:
757 bzero(lp, sizeof(struct disklabel));
758 lp->d_secsize = FDSECSIZE;
759 lp->d_ntracks = FDNHEADS;
760 lp->d_ncylinders = sc->type->ncylinders;
761 lp->d_nsectors = sc->nsectors;
762 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
763 lp->d_type = DTYPE_FLOPPY;
764 lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
765 lp->d_rpm = 300; /* good guess I suppose. */
766 lp->d_interleave = 1; /* should change when adding msdos */
767 sc->stepdelay = lp->d_trkseek = FDSTEPDELAY;
768 lp->d_bbsize = 0;
769 lp->d_sbsize = 0;
770 lp->d_partitions[part].p_size = lp->d_secperunit;
771 lp->d_partitions[part].p_fstype = FS_UNUSED;
772 lp->d_partitions[part].p_fsize = 1024;
773 lp->d_partitions[part].p_frag = 8;
774 lp->d_partitions[part].p_cpg = 2; /* adosfs: reserved blocks */
775 lp->d_npartitions = part + 1;
776 lp->d_magic = lp->d_magic2 = DISKMAGIC;
777 lp->d_checksum = dkcksum(lp);
778 brelse(bp);
779 return(0);
780 }
781
782 /*
783 * set the incore copy of this units disklabel
784 */
785 int
786 fdsetdisklabel(sc, lp)
787 struct fd_softc *sc;
788 struct disklabel *lp;
789 {
790 struct disklabel *clp;
791 struct partition *pp;
792
793 /*
794 * must have at least opened raw unit to fetch the
795 * raw_part stuff.
796 */
797 if ((sc->flags & FDF_HAVELABEL) == 0)
798 return(EINVAL);
799 clp = sc->dkdev.dk_label;
800 /*
801 * make sure things check out and we only have one valid
802 * partition
803 */
804 #ifdef FDDEBUG
805 printf("fdsetdisklabel\n");
806 #endif
807 if (lp->d_secsize != FDSECSIZE ||
808 lp->d_nsectors != clp->d_nsectors ||
809 lp->d_ntracks != FDNHEADS ||
810 lp->d_ncylinders != clp->d_ncylinders ||
811 lp->d_secpercyl != clp->d_secpercyl ||
812 lp->d_secperunit != clp->d_secperunit ||
813 lp->d_magic != DISKMAGIC ||
814 lp->d_magic2 != DISKMAGIC ||
815 lp->d_npartitions == 0 ||
816 lp->d_npartitions > FDMAXPARTS ||
817 (lp->d_partitions[0].p_offset && lp->d_partitions[1].p_offset) ||
818 dkcksum(lp))
819 return(EINVAL);
820 /*
821 * if any partitions are present make sure they
822 * represent the currently open type
823 */
824 if ((pp = &lp->d_partitions[0])->p_size) {
825 if ((pp = &lp->d_partitions[1])->p_size == 0)
826 goto done;
827 else if (sc->openpart != 1)
828 return(EINVAL);
829 } else if (sc->openpart != 0)
830 return(EINVAL);
831 /*
832 * make sure selected partition is within bounds
833 * XXX on the second check, its to handle a bug in
834 * XXX the cluster routines as they require mutliples
835 * XXX of CLBYTES currently
836 */
837 if ((pp->p_offset + pp->p_size >= lp->d_secperunit) ||
838 (pp->p_frag * pp->p_fsize % CLBYTES))
839 return(EINVAL);
840 done:
841 bcopy(lp, clp, sizeof(struct disklabel));
842 return(0);
843 }
844
845 /*
846 * write out the incore copy of this units disklabel
847 */
848 int
849 fdputdisklabel(sc, dev)
850 struct fd_softc *sc;
851 dev_t dev;
852 {
853 struct disklabel *lp, *dlp;
854 struct buf *bp;
855 int error;
856
857 if ((sc->flags & FDF_HAVELABEL) == 0)
858 return(EBADF);
859 #ifdef FDDEBUG
860 printf("fdputdisklabel\n");
861 #endif
862 /*
863 * get buf and read in sector 0
864 */
865 lp = sc->dkdev.dk_label;
866 bp = (void *)geteblk((int)lp->d_secsize);
867 bp->b_dev = FDMAKEDEV(major(dev), FDUNIT(dev), RAW_PART);
868 bp->b_blkno = 0;
869 bp->b_cylin = 0;
870 bp->b_bcount = FDSECSIZE;
871 bp->b_flags = B_BUSY | B_READ;
872 fdstrategy(bp);
873 if ((error = biowait(bp)) != 0)
874 goto done;
875 /*
876 * copy disklabel to buf and write it out syncronous
877 */
878 dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
879 bcopy(lp, dlp, sizeof(struct disklabel));
880 bp->b_blkno = 0;
881 bp->b_cylin = 0;
882 bp->b_flags = B_WRITE;
883 fdstrategy(bp);
884 error = biowait(bp);
885 done:
886 brelse(bp);
887 return(error);
888 }
889
890 /*
891 * figure out drive type or NULL if none.
892 */
893 struct fdtype *
894 fdcgetfdtype(unit)
895 int unit;
896 {
897 struct fdtype *ftp;
898 u_long id, idb;
899 int cnt, umask;
900
901 id = 0;
902 umask = 1 << (3 + unit);
903
904 FDDESELECT(FDCUNITMASK);
905
906 FDSETMOTOR(1);
907 delay(1);
908 FDSELECT(umask);
909 delay(1);
910 FDDESELECT(umask);
911
912 FDSETMOTOR(0);
913 delay(1);
914 FDSELECT(umask);
915 delay(1);
916 FDDESELECT(umask);
917
918 for (idb = 0x80000000; idb; idb >>= 1) {
919 FDSELECT(umask);
920 delay(1);
921 if (FDTESTC(FDB_READY) == 0)
922 id |= idb;
923 FDDESELECT(umask);
924 delay(1);
925 }
926 #ifdef FDDEBUG
927 printf("fdcgettype unit %d id 0x%lx\n", unit, id);
928 #endif
929
930 for (cnt = 0, ftp = fdtype; cnt < nfdtype; ftp++, cnt++)
931 if (ftp->driveid == id)
932 return(ftp);
933 /*
934 * 3.5dd's at unit 0 do not always return id.
935 */
936 if (unit == 0)
937 return(fdtype);
938 return(NULL);
939 }
940
941 /*
942 * turn motor off if possible otherwise mark as needed and will be done
943 * later.
944 */
945 void
946 fdmotoroff(arg)
947 void *arg;
948 {
949 struct fd_softc *sc;
950 int s;
951
952 sc = arg;
953 s = splbio();
954
955 #ifdef FDDEBUG
956 printf("fdmotoroff: unit %d\n", sc->hwunit);
957 #endif
958 if ((sc->flags & FDF_MOTORON) == 0)
959 goto done;
960 /*
961 * if we have a timeout on a dma operation let fddmadone()
962 * deal with it.
963 */
964 if (fdc_indma == sc) {
965 fddmadone(sc, 1);
966 goto done;
967 }
968 #ifdef FDDEBUG
969 printf(" motor was on, turning off\n");
970 #endif
971
972 /*
973 * flush cache if needed
974 */
975 if (sc->flags & FDF_DIRTY) {
976 sc->flags |= FDF_JUSTFLUSH | FDF_MOTOROFF;
977 #ifdef FDDEBUG
978 printf(" flushing dirty buffer first\n");
979 #endif
980 /*
981 * if dma'ing done for now, fddone() will call us again
982 */
983 if (fdc_indma)
984 goto done;
985 fddmastart(sc, sc->cachetrk);
986 goto done;
987 }
988
989 /*
990 * if controller is busy just schedule us to be called back
991 */
992 if (fdc_indma) {
993 /*
994 * someone else has the controller now
995 * just set flag and let fddone() call us again.
996 */
997 sc->flags |= FDF_MOTOROFF;
998 goto done;
999 }
1000
1001 #ifdef FDDEBUG
1002 printf(" hw turning unit off\n");
1003 #endif
1004
1005 sc->flags &= ~(FDF_MOTORON | FDF_MOTOROFF);
1006 FDDESELECT(FDCUNITMASK);
1007 FDSETMOTOR(0);
1008 delay(1);
1009 FDSELECT(sc->unitmask);
1010 delay(4);
1011 FDDESELECT(sc->unitmask);
1012 delay(1);
1013 if (sc->flags & FDF_WMOTOROFF)
1014 wakeup(fdmotoroff);
1015 done:
1016 splx(s);
1017 }
1018
1019 /*
1020 * select drive seek to track exit with motor on.
1021 * fdsetpos(x, 0, 0) does calibrates the drive.
1022 */
1023 void
1024 fdsetpos(sc, trk, towrite)
1025 struct fd_softc *sc;
1026 int trk, towrite;
1027 {
1028 int nstep, sdir, ondly, ncyl, nside;
1029
1030 FDDESELECT(FDCUNITMASK);
1031 FDSETMOTOR(1);
1032 delay(1);
1033 FDSELECT(sc->unitmask);
1034 delay(1);
1035 if ((sc->flags & FDF_MOTORON) == 0) {
1036 ondly = 0;
1037 while (FDTESTC(FDB_READY) == 0) {
1038 delay(1000);
1039 if (++ondly >= 1000)
1040 break;
1041 }
1042 }
1043 sc->flags |= FDF_MOTORON;
1044
1045 ncyl = trk / FDNHEADS;
1046 nside = trk % FDNHEADS;
1047
1048 if (sc->curcyl == ncyl && fdc_side == nside)
1049 return;
1050
1051 if (towrite)
1052 sc->flags |= FDF_WRITEWAIT;
1053
1054 #ifdef FDDEBUG
1055 printf("fdsetpos: cyl %d head %d towrite %d\n", trk / FDNHEADS,
1056 trk % FDNHEADS, towrite);
1057 #endif
1058 nstep = ncyl - sc->curcyl;
1059 if (nstep) {
1060 /*
1061 * figure direction
1062 */
1063 if (nstep > 0 && ncyl != 0) {
1064 sdir = FDSTEPIN;
1065 FDSETDIR(1);
1066 } else {
1067 nstep = -nstep;
1068 sdir = FDSTEPOUT;
1069 FDSETDIR(0);
1070 }
1071 if (ncyl == 0) {
1072 /*
1073 * either just want cylinder 0 or doing
1074 * a calibrate.
1075 */
1076 nstep = 256;
1077 while (FDTESTC(FDB_CYLZERO) == 0 && nstep--) {
1078 FDSTEP;
1079 delay(sc->stepdelay);
1080 }
1081 if (nstep < 0)
1082 sc->flags |= FDF_NOTRACK0;
1083 } else {
1084 /*
1085 * step the needed amount amount.
1086 */
1087 while (nstep--) {
1088 FDSTEP;
1089 delay(sc->stepdelay);
1090 }
1091 }
1092 /*
1093 * if switched directions
1094 * allow drive to settle.
1095 */
1096 if (sc->pstepdir != sdir)
1097 delay(FDSETTLEDELAY);
1098 sc->pstepdir = sdir;
1099 sc->curcyl = ncyl;
1100 }
1101 if (nside == fdc_side)
1102 return;
1103 /*
1104 * select side
1105 */
1106 fdc_side = nside;
1107 FDSETHEAD(nside);
1108 delay(FDPRESIDEDELAY);
1109 }
1110
1111 void
1112 fdselunit(sc)
1113 struct fd_softc *sc;
1114 {
1115 FDDESELECT(FDCUNITMASK); /* deselect all */
1116 FDSETMOTOR(sc->flags & FDF_MOTORON); /* set motor to unit's state */
1117 delay(1);
1118 FDSELECT(sc->unitmask); /* select unit */
1119 delay(1);
1120 }
1121
1122 /*
1123 * process next buf on device queue.
1124 * normall sequence of events:
1125 * fdstart() -> fddmastart();
1126 * fdintr() -> fddmadone() -> fddone();
1127 * if the track is in the cache then fdstart() will short-circuit
1128 * to fddone() else if the track cache is dirty it will flush. If
1129 * the buf is not an entire track it will cache the requested track.
1130 */
1131 void
1132 fdstart(sc)
1133 struct fd_softc *sc;
1134 {
1135 int trk, error, write;
1136 struct buf *bp, *dp;
1137
1138 #ifdef FDDEBUG
1139 printf("fdstart: unit %d\n", sc->hwunit);
1140 #endif
1141
1142 /*
1143 * if dma'ing just return. we must have been called from fdstartegy.
1144 */
1145 if (fdc_indma)
1146 return;
1147
1148 /*
1149 * get next buf if there.
1150 */
1151 dp = &sc->bufq;
1152 if ((bp = dp->b_actf) == NULL) {
1153 #ifdef FDDEBUG
1154 printf(" nothing to do\n");
1155 #endif
1156 return;
1157 }
1158
1159 /*
1160 * Mark us as busy now, in case fddone() gets called in one
1161 * of the cases below.
1162 */
1163 disk_busy(&sc->dkdev);
1164
1165 /*
1166 * make sure same disk is loaded
1167 */
1168 fdselunit(sc);
1169 if (FDTESTC(FDB_CHANGED)) {
1170 /*
1171 * disk missing, invalidate all future io on
1172 * this unit until re-open()'ed also invalidate
1173 * all current io
1174 */
1175 #ifdef FDDEBUG
1176 printf(" disk was removed invalidating all io\n");
1177 #endif
1178 sc->flags &= ~FDF_HAVELABEL;
1179 for (;;) {
1180 bp->b_flags |= B_ERROR;
1181 bp->b_error = EIO;
1182 if (bp->b_actf == NULL)
1183 break;
1184 biodone(bp);
1185 bp = bp->b_actf;
1186 }
1187 /*
1188 * do fddone() on last buf to allow other units to start.
1189 */
1190 dp->b_actf = bp;
1191 fddone(sc);
1192 return;
1193 }
1194
1195 /*
1196 * we have a valid buf, setup our local version
1197 * we use this count to allow reading over multiple tracks.
1198 * into a single buffer
1199 */
1200 dp->b_bcount = bp->b_bcount;
1201 dp->b_blkno = bp->b_blkno;
1202 dp->b_data = bp->b_data;
1203 dp->b_flags = bp->b_flags;
1204 dp->b_resid = 0;
1205
1206 if (bp->b_flags & B_READ)
1207 write = 0;
1208 else if (FDTESTC(FDB_PROTECT) == 0)
1209 write = 1;
1210 else {
1211 error = EPERM;
1212 goto bad;
1213 }
1214
1215 /*
1216 * figure trk given blkno
1217 */
1218 trk = bp->b_blkno / sc->nsectors;
1219
1220 /*
1221 * check to see if same as currently cached track
1222 * if so we need to do no dma read.
1223 */
1224 if (trk == sc->cachetrk) {
1225 fddone(sc);
1226 return;
1227 }
1228
1229 /*
1230 * if we will be overwriting the entire cache, don't bother to
1231 * fetch it.
1232 */
1233 if (bp->b_bcount == (sc->nsectors * FDSECSIZE) && write &&
1234 bp->b_blkno % sc->nsectors == 0) {
1235 if (sc->flags & FDF_DIRTY)
1236 sc->flags |= FDF_JUSTFLUSH;
1237 else {
1238 sc->cachetrk = trk;
1239 fddone(sc);
1240 return;
1241 }
1242 }
1243
1244 /*
1245 * start dma read of `trk'
1246 */
1247 fddmastart(sc, trk);
1248 return;
1249 bad:
1250 bp->b_flags |= B_ERROR;
1251 bp->b_error = error;
1252 fddone(sc);
1253 }
1254
1255 /*
1256 * continue a started operation on next track. always begin at
1257 * sector 0 on the next track.
1258 */
1259 void
1260 fdcont(sc)
1261 struct fd_softc *sc;
1262 {
1263 struct buf *dp, *bp;
1264 int trk, write;
1265
1266 dp = &sc->bufq;
1267 bp = dp->b_actf;
1268 dp->b_data += (dp->b_bcount - bp->b_resid);
1269 dp->b_blkno += (dp->b_bcount - bp->b_resid) / FDSECSIZE;
1270 dp->b_bcount = bp->b_resid;
1271
1272 /*
1273 * figure trk given blkno
1274 */
1275 trk = dp->b_blkno / sc->nsectors;
1276 #ifdef DEBUG
1277 if (trk != sc->cachetrk + 1 || dp->b_blkno % sc->nsectors != 0)
1278 panic("fdcont: confused");
1279 #endif
1280 if (dp->b_flags & B_READ)
1281 write = 0;
1282 else
1283 write = 1;
1284 /*
1285 * if we will be overwriting the entire cache, don't bother to
1286 * fetch it.
1287 */
1288 if (dp->b_bcount == (sc->nsectors * FDSECSIZE) && write) {
1289 if (sc->flags & FDF_DIRTY)
1290 sc->flags |= FDF_JUSTFLUSH;
1291 else {
1292 sc->cachetrk = trk;
1293 fddone(sc);
1294 return;
1295 }
1296 }
1297 /*
1298 * start dma read of `trk'
1299 */
1300 fddmastart(sc, trk);
1301 return;
1302 }
1303
1304 void
1305 fddmastart(sc, trk)
1306 struct fd_softc *sc;
1307 int trk;
1308 {
1309 int adkmask, ndmaw, write, dmatrk;
1310
1311 #ifdef FDDEBUG
1312 printf("fddmastart: unit %d cyl %d head %d", sc->hwunit,
1313 trk / FDNHEADS, trk % FDNHEADS);
1314 #endif
1315 /*
1316 * flush the cached track if dirty else read requested track.
1317 */
1318 if (sc->flags & FDF_DIRTY) {
1319 fdcachetoraw(sc);
1320 ndmaw = sc->type->nwritew;
1321 dmatrk = sc->cachetrk;
1322 write = 1;
1323 } else {
1324 ndmaw = sc->type->nreadw;
1325 dmatrk = trk;
1326 write = 0;
1327 }
1328
1329 #ifdef FDDEBUG
1330 printf(" %s", write ? " flushing cache\n" : " loading cache\n");
1331 #endif
1332 sc->cachetrk = trk;
1333 fdc_indma = sc;
1334 fdsetpos(sc, dmatrk, write);
1335
1336 /*
1337 * setup dma stuff
1338 */
1339 if (write == 0) {
1340 custom.adkcon = ADKF_MSBSYNC;
1341 custom.adkcon = ADKF_SETCLR | ADKF_WORDSYNC | ADKF_FAST;
1342 custom.dsksync = FDMFMSYNC;
1343 } else {
1344 custom.adkcon = ADKF_PRECOMP1 | ADKF_PRECOMP0 | ADKF_WORDSYNC |
1345 ADKF_MSBSYNC;
1346 adkmask = ADKF_SETCLR | ADKF_FAST | ADKF_MFMPREC;
1347 if (dmatrk >= sc->type->precomp[0])
1348 adkmask |= ADKF_PRECOMP0;
1349 if (dmatrk >= sc->type->precomp[1])
1350 adkmask |= ADKF_PRECOMP1;
1351 custom.adkcon = adkmask;
1352 }
1353 custom.dskpt = (u_char *)kvtop(fdc_dmap);
1354 FDDMASTART(ndmaw, write);
1355
1356 #ifdef FDDEBUG
1357 printf(" dma started\n");
1358 #endif
1359 }
1360
1361 /*
1362 * recalibrate the drive
1363 */
1364 void
1365 fdcalibrate(arg)
1366 void *arg;
1367 {
1368 struct fd_softc *sc;
1369 static int loopcnt;
1370
1371 sc = arg;
1372
1373 if (loopcnt == 0) {
1374 /*
1375 * seek cyl 0
1376 */
1377 fdc_indma = sc;
1378 sc->stepdelay += 900;
1379 if (sc->cachetrk > 1)
1380 fdsetpos(sc, sc->cachetrk % FDNHEADS, 0);
1381 sc->stepdelay -= 900;
1382 }
1383 if (loopcnt++ & 1)
1384 fdsetpos(sc, sc->cachetrk, 0);
1385 else
1386 fdsetpos(sc, sc->cachetrk + FDNHEADS, 0);
1387 /*
1388 * trk++, trk, trk++, trk, trk++, trk, trk++, trk and dma
1389 */
1390 if (loopcnt < 8)
1391 timeout(fdcalibrate, sc, hz / 8);
1392 else {
1393 loopcnt = 0;
1394 fdc_indma = NULL;
1395 timeout(fdmotoroff, sc, 3 * hz / 2);
1396 fddmastart(sc, sc->cachetrk);
1397 }
1398 }
1399
1400 void
1401 fddmadone(sc, timeo)
1402 struct fd_softc *sc;
1403 int timeo;
1404 {
1405 #ifdef FDDEBUG
1406 printf("fddmadone: unit %d, timeo %d\n", sc->hwunit, timeo);
1407 #endif
1408 fdc_indma = NULL;
1409 untimeout(fdmotoroff, sc);
1410 FDDMASTOP;
1411
1412 /*
1413 * guarantee the drive has been at current head and cyl
1414 * for at least FDWRITEDELAY after a write.
1415 */
1416 if (sc->flags & FDF_WRITEWAIT) {
1417 delay(FDWRITEDELAY);
1418 sc->flags &= ~FDF_WRITEWAIT;
1419 }
1420
1421 if ((sc->flags & FDF_MOTOROFF) == 0) {
1422 /*
1423 * motor runs for 1.5 seconds after last dma
1424 */
1425 timeout(fdmotoroff, sc, 3 * hz / 2);
1426 }
1427 if (sc->flags & FDF_DIRTY) {
1428 /*
1429 * if buffer dirty, the last dma cleaned it
1430 */
1431 sc->flags &= ~FDF_DIRTY;
1432 if (timeo)
1433 printf("%s: write of track cache timed out.\n",
1434 sc->sc_dv.dv_xname);
1435 if (sc->flags & FDF_JUSTFLUSH) {
1436 sc->flags &= ~FDF_JUSTFLUSH;
1437 /*
1438 * we are done dma'ing
1439 */
1440 fddone(sc);
1441 return;
1442 }
1443 /*
1444 * load the cache
1445 */
1446 fddmastart(sc, sc->cachetrk);
1447 return;
1448 }
1449 #ifdef FDDEBUG
1450 else if (sc->flags & FDF_MOTOROFF)
1451 panic("fddmadone: FDF_MOTOROFF with no FDF_DIRTY");
1452 #endif
1453
1454 /*
1455 * cache loaded decode it into cache buffer
1456 */
1457 if (timeo == 0 && fdrawtocache(sc) == 0)
1458 sc->retried = 0;
1459 else {
1460 #ifdef FDDEBUG
1461 if (timeo)
1462 printf("%s: fddmadone: cache load timed out.\n",
1463 sc->sc_dv.dv_xname);
1464 #endif
1465 if (sc->retried >= sc->retries) {
1466 sc->retried = 0;
1467 sc->cachetrk = -1;
1468 } else {
1469 sc->retried++;
1470 /*
1471 * this will be restarted at end of calibrate loop.
1472 */
1473 untimeout(fdmotoroff, sc);
1474 fdcalibrate(sc);
1475 return;
1476 }
1477 }
1478 fddone(sc);
1479 }
1480
1481 void
1482 fddone(sc)
1483 struct fd_softc *sc;
1484 {
1485 struct buf *dp, *bp;
1486 char *data;
1487 int sz;
1488
1489 #ifdef FDDEBUG
1490 printf("fddone: unit %d\n", sc->hwunit);
1491 #endif
1492 /*
1493 * check to see if unit is just flushing the cache,
1494 * that is we have no io queued.
1495 */
1496 if (sc->flags & FDF_MOTOROFF)
1497 goto nobuf;
1498
1499 dp = &sc->bufq;
1500 if ((bp = dp->b_actf) == NULL)
1501 panic ("fddone");
1502 /*
1503 * check for an error that may have occured
1504 * while getting the track.
1505 */
1506 if (sc->cachetrk == -1) {
1507 sc->retried = 0;
1508 bp->b_flags |= B_ERROR;
1509 bp->b_error = EIO;
1510 } else if ((bp->b_flags & B_ERROR) == 0) {
1511 data = sc->cachep;
1512 /*
1513 * get offset of data in track cache and limit
1514 * the copy size to not exceed the cache's end.
1515 */
1516 data += (dp->b_blkno % sc->nsectors) * FDSECSIZE;
1517 sz = sc->nsectors - dp->b_blkno % sc->nsectors;
1518 sz *= FDSECSIZE;
1519 sz = min(dp->b_bcount, sz);
1520 if (bp->b_flags & B_READ)
1521 bcopy(data, dp->b_data, sz);
1522 else {
1523 bcopy(dp->b_data, data, sz);
1524 sc->flags |= FDF_DIRTY;
1525 }
1526 bp->b_resid = dp->b_bcount - sz;
1527 if (bp->b_resid == 0) {
1528 bp->b_error = 0;
1529 } else {
1530 /*
1531 * not done yet need to read next track
1532 */
1533 fdcont(sc);
1534 return;
1535 }
1536 }
1537 /*
1538 * remove from queue.
1539 */
1540 dp->b_actf = bp->b_actf;
1541
1542 disk_unbusy(&sc->dkdev, (bp->b_bcount - bp->b_resid));
1543
1544 biodone(bp);
1545 nobuf:
1546 fdfindwork(sc->sc_dv.dv_unit);
1547 }
1548
1549 void
1550 fdfindwork(unit)
1551 int unit;
1552 {
1553 struct fd_softc *ssc, *sc;
1554 int i, last;
1555
1556 /*
1557 * first see if we have any fdopen()'s waiting
1558 */
1559 if (fdc_wantwakeup) {
1560 wakeup(fdopen);
1561 fdc_wantwakeup--;
1562 return;
1563 }
1564
1565 /*
1566 * start next available unit, linear search from the next unit
1567 * wrapping and finally this unit.
1568 */
1569 last = 0;
1570 ssc = NULL;
1571 for (i = unit + 1; last == 0; i++) {
1572 if (i == unit)
1573 last = 1;
1574 if (i >= fd_cd.cd_ndevs) {
1575 i = -1;
1576 continue;
1577 }
1578 if ((sc = fd_cd.cd_devs[i]) == NULL)
1579 continue;
1580
1581 /*
1582 * if unit has requested to be turned off
1583 * and it has no buf's queued do it now
1584 */
1585 if (sc->flags & FDF_MOTOROFF) {
1586 if (sc->bufq.b_actf == NULL)
1587 fdmotoroff(sc);
1588 else {
1589 /*
1590 * we gained a buf request while
1591 * we waited, forget the motoroff
1592 */
1593 sc->flags &= ~FDF_MOTOROFF;
1594 }
1595 /*
1596 * if we now have dma unit must have needed
1597 * flushing, quit
1598 */
1599 if (fdc_indma)
1600 return;
1601 }
1602 /*
1603 * if we have no start unit and the current unit has
1604 * io waiting choose this unit to start.
1605 */
1606 if (ssc == NULL && sc->bufq.b_actf)
1607 ssc = sc;
1608 }
1609 if (ssc)
1610 fdstart(ssc);
1611 }
1612
1613 /*
1614 * min byte count to whats left of the track in question
1615 */
1616 void
1617 fdminphys(bp)
1618 struct buf *bp;
1619 {
1620 struct fd_softc *sc;
1621 int trk, sec, toff, tsz;
1622
1623 if ((sc = getsoftc(fd_cd, FDUNIT(bp->b_dev))) == NULL)
1624 panic("fdminphys: couldn't get softc");
1625
1626 trk = bp->b_blkno / sc->nsectors;
1627 sec = bp->b_blkno % sc->nsectors;
1628
1629 toff = sec * FDSECSIZE;
1630 tsz = sc->nsectors * FDSECSIZE;
1631 #ifdef FDDEBUG
1632 printf("fdminphys: before %d", bp->b_bcount);
1633 #endif
1634 bp->b_bcount = min(bp->b_bcount, tsz - toff);
1635 #ifdef FDDEBUG
1636 printf(" after %d\n", bp->b_bcount);
1637 #endif
1638 minphys(bp);
1639 }
1640
1641 /*
1642 * encode the track cache into raw MFM ready for dma
1643 * when we go to multiple disk formats, this will call type dependent
1644 * functions
1645 */
1646 void
1647 fdcachetoraw(sc)
1648 struct fd_softc *sc;
1649 {
1650 static u_long mfmnull[4];
1651 u_long *rp, *crp, *dp, hcksum, dcksum, info, zero;
1652 int sec, i;
1653
1654 rp = fdc_dmap;
1655
1656 /*
1657 * not yet one sector (- 1 long) gap.
1658 * for now use previous drivers values
1659 */
1660 for (i = 0; i < sc->type->gap; i++)
1661 *rp++ = 0xaaaaaaaa;
1662 /*
1663 * process sectors
1664 */
1665 dp = sc->cachep;
1666 zero = 0;
1667 info = 0xff000000 | (sc->cachetrk << 16) | sc->nsectors;
1668 for (sec = 0; sec < sc->nsectors; sec++, info += (1 << 8) - 1) {
1669 hcksum = dcksum = 0;
1670 /*
1671 * sector format
1672 * offset description
1673 *-----------------------------------
1674 * 0 null
1675 * 1 sync
1676 * oddbits evenbits
1677 *----------------------
1678 * 2 3 [0xff]b [trk]b [sec]b [togap]b
1679 * 4-7 8-11 null
1680 * 12 13 header cksum [2-11]
1681 * 14 15 data cksum [16-271]
1682 * 16-143 144-271 data
1683 */
1684 *rp = 0xaaaaaaaa;
1685 if (*(rp - 1) & 0x1)
1686 *rp &= 0x7fffffff; /* clock bit correction */
1687 rp++;
1688 *rp++ = (FDMFMSYNC << 16) | FDMFMSYNC;
1689 rp = mfmblkencode(&info, rp, &hcksum, 1);
1690 rp = mfmblkencode(mfmnull, rp, &hcksum, 4);
1691 rp = mfmblkencode(&hcksum, rp, NULL, 1);
1692
1693 crp = rp;
1694 rp = mfmblkencode(dp, rp + 2, &dcksum, FDSECLWORDS);
1695 dp += FDSECLWORDS;
1696 crp = mfmblkencode(&dcksum, crp, NULL, 1);
1697 if (*(crp - 1) & 0x1)
1698 *crp &= 0x7fffffff; /* clock bit correction */
1699 else if ((*crp & 0x40000000) == 0)
1700 *crp |= 0x80000000;
1701 }
1702 *rp = 0xaaa80000;
1703 if (*(rp - 1) & 0x1)
1704 *rp &= 0x7fffffff;
1705 }
1706
1707 u_long *
1708 fdfindsync(rp, ep)
1709 u_long *rp, *ep;
1710 {
1711 u_short *sp;
1712
1713 sp = (u_short *)rp;
1714 while ((u_long *)sp < ep && *sp != FDMFMSYNC)
1715 sp++;
1716 while ((u_long *)sp < ep && *sp == FDMFMSYNC)
1717 sp++;
1718 if ((u_long *)sp < ep)
1719 return((u_long *)sp);
1720 return(NULL);
1721 }
1722
1723 /*
1724 * decode raw MFM from dma into units track cache.
1725 * when we go to multiple disk formats, this will call type dependent
1726 * functions
1727 */
1728 int
1729 fdrawtocache(sc)
1730 struct fd_softc *sc;
1731 {
1732 u_long mfmnull[4];
1733 u_long *dp, *rp, *erp, *crp, *srp, hcksum, dcksum, info, cktmp;
1734 int cnt, doagain;
1735
1736 doagain = 1;
1737 srp = rp = fdc_dmap;
1738 erp = (u_long *)((u_short *)rp + sc->type->nreadw);
1739 cnt = 0;
1740 again:
1741 if (doagain == 0 || (rp = srp = fdfindsync(srp, erp)) == NULL) {
1742 #ifdef DIAGNOSTIC
1743 printf("%s: corrupted track (%d) data.\n",
1744 sc->sc_dv.dv_xname, sc->cachetrk);
1745 #endif
1746 return(-1);
1747 }
1748
1749 /*
1750 * process sectors
1751 */
1752 for (; cnt < sc->nsectors; cnt++) {
1753 hcksum = dcksum = 0;
1754 rp = mfmblkdecode(rp, &info, &hcksum, 1);
1755 rp = mfmblkdecode(rp, mfmnull, &hcksum, 4);
1756 rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1757 if (cktmp != hcksum) {
1758 #ifdef FDDEBUG
1759 printf(" info 0x%x hchksum 0x%x trkhcksum 0x%x\n",
1760 info, hcksum, cktmp);
1761 #endif
1762 goto again;
1763 }
1764 if (((info >> 16) & 0xff) != sc->cachetrk) {
1765 #ifdef DEBUG
1766 printf("%s: incorrect track found: 0x%lx %d\n",
1767 sc->sc_dv.dv_xname, info, sc->cachetrk);
1768 #endif
1769 goto again;
1770 }
1771 #ifdef FDDEBUG
1772 printf(" info 0x%x\n", info);
1773 #endif
1774
1775 rp = mfmblkdecode(rp, &cktmp, NULL, 1);
1776 dp = sc->cachep;
1777 dp += FDSECLWORDS * ((info >> 8) & 0xff);
1778 crp = mfmblkdecode(rp, dp, &dcksum, FDSECLWORDS);
1779 if (cktmp != dcksum) {
1780 #ifdef FDDEBUG
1781 printf(" info 0x%x dchksum 0x%x trkdcksum 0x%x\n",
1782 info, dcksum, cktmp);
1783 #endif
1784 goto again;
1785 }
1786
1787 /*
1788 * if we are at gap then we can no longer be sure
1789 * of correct sync marks
1790 */
1791 if ((info && 0xff) == 1)
1792 doagain = 1;
1793 else
1794 doagain = 0;
1795 srp = rp = fdfindsync(crp, erp);
1796 }
1797 return(0);
1798 }
1799
1800 /*
1801 * encode len longwords of `dp' data in amiga mfm block format (`rp')
1802 * this format specified that the odd bits are at current pos and even
1803 * bits at len + current pos
1804 */
1805 u_long *
1806 mfmblkencode(dp, rp, cp, len)
1807 u_long *dp, *rp, *cp;
1808 int len;
1809 {
1810 u_long *sdp, *edp, d, dtmp, correct;
1811
1812 sdp = dp;
1813 edp = dp + len;
1814
1815 if (*(rp - 1) & 0x1)
1816 correct = 1;
1817 else
1818 correct = 0;
1819 /*
1820 * do odd bits
1821 */
1822 while (dp < edp) {
1823 d = (*dp >> 1) & 0x55555555; /* remove clock bits */
1824 dtmp = d ^ 0x55555555;
1825 d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
1826 /*
1827 * correct upper clock bit if needed
1828 */
1829 if (correct)
1830 d &= 0x7fffffff;
1831 if (d & 0x1)
1832 correct = 1;
1833 else
1834 correct = 0;
1835 /*
1836 * do checksums and store in raw buffer
1837 */
1838 if (cp)
1839 *cp ^= d;
1840 *rp++ = d;
1841 dp++;
1842 }
1843 /*
1844 * do even bits
1845 */
1846 dp = sdp;
1847 while (dp < edp) {
1848 d = *dp & 0x55555555; /* remove clock bits */
1849 dtmp = d ^ 0x55555555;
1850 d |= ((dtmp >> 1) | 0x80000000) & (dtmp << 1);
1851 /*
1852 * correct upper clock bit if needed
1853 */
1854 if (correct)
1855 d &= 0x7fffffff;
1856 if (d & 0x1)
1857 correct = 1;
1858 else
1859 correct = 0;
1860 /*
1861 * do checksums and store in raw buffer
1862 */
1863 if (cp)
1864 *cp ^= d;
1865 *rp++ = d;
1866 dp++;
1867 }
1868 if (cp)
1869 *cp &= 0x55555555;
1870 return(rp);
1871 }
1872
1873 /*
1874 * decode len longwords of `dp' data in amiga mfm block format (`rp')
1875 * this format specified that the odd bits are at current pos and even
1876 * bits at len + current pos
1877 */
1878 u_long *
1879 mfmblkdecode(rp, dp, cp, len)
1880 u_long *rp, *dp, *cp;
1881 int len;
1882 {
1883 u_long o, e;
1884 int cnt;
1885
1886 cnt = len;
1887 while (cnt--) {
1888 o = *rp;
1889 e = *(rp + len);
1890 if (cp) {
1891 *cp ^= o;
1892 *cp ^= e;
1893 }
1894 o &= 0x55555555;
1895 e &= 0x55555555;
1896 *dp++ = (o << 1) | e;
1897 rp++;
1898 }
1899 if (cp)
1900 *cp &= 0x55555555;
1901 return(rp + len);
1902 }
1903
1904 int
1905 fddump(dev, blkno, va, size)
1906 dev_t dev;
1907 daddr_t blkno;
1908 caddr_t va;
1909 size_t size;
1910 {
1911 return (EINVAL);
1912 }
1913