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