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