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