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