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