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