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