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