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