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