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