Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.7
      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.7 1994/06/07 05:41:51 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)	(minor(dev) / MAXPARTITIONS)
     67 #define FDPART(dev)	(minor(dev) % MAXPARTITIONS)
     68 #define FDMAKEDEV(m, u, p)	makedev((m), (u) * MAXPARTITIONS + (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 = 512;
    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 = 512;
    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 	 */
    779 	if (pp->p_offset + pp->p_size >= lp->d_secperunit)
    780 		return(EINVAL);
    781 done:
    782 	bcopy(lp, clp, sizeof(struct disklabel));
    783 	return(0);
    784 }
    785 
    786 /*
    787  * write out the incore copy of this units disklabel
    788  */
    789 int
    790 fdputdisklabel(sc, dev)
    791 	struct fd_softc *sc;
    792 	dev_t dev;
    793 {
    794 	struct disklabel *lp, *dlp;
    795 	struct buf *bp;
    796 	int error;
    797 
    798 	if ((sc->flags & FDF_HAVELABEL) == 0)
    799 		return(EBADF);
    800 #ifdef FDDEBUG
    801 	printf("fdputdisklabel\n");
    802 #endif
    803 	/*
    804 	 * get buf and read in sector 0
    805 	 */
    806 	lp = &sc->dkdev.dk_label;
    807 	bp = (void *)geteblk((int)lp->d_secsize);
    808 	bp->b_dev = FDMAKEDEV(major(dev), FDUNIT(dev), RAW_PART);
    809 	bp->b_blkno = 0;
    810 	bp->b_cylin = 0;
    811 	bp->b_bcount = FDSECSIZE;
    812 	bp->b_flags = B_BUSY | B_READ;
    813 	fdstrategy(bp);
    814 	if (error = biowait(bp))
    815 		goto done;
    816 	/*
    817 	 * copy disklabel to buf and write it out syncronous
    818 	 */
    819 	dlp = (struct disklabel *)(bp->b_data + LABELOFFSET);
    820 	bcopy(lp, dlp, sizeof(struct disklabel));
    821 	bp->b_blkno = 0;
    822 	bp->b_cylin = 0;
    823 	bp->b_flags = B_WRITE;
    824 	fdstrategy(bp);
    825 	error = biowait(bp);
    826 done:
    827 	brelse(bp);
    828 	return(error);
    829 }
    830 
    831 /*
    832  * figure out drive type or NULL if none.
    833  */
    834 struct fdtype *
    835 fdcgetfdtype(unit)
    836 	int unit;
    837 {
    838 	struct fdtype *ftp;
    839 	u_long id, idb;
    840 	int cnt, umask;
    841 
    842 	id = 0;
    843 	umask = 1 << (3 + unit);
    844 
    845 	FDDESELECT(FDCUNITMASK);
    846 
    847 	FDSETMOTOR(1);
    848 	delay(1);
    849 	FDSELECT(umask);
    850 	delay(1);
    851 	FDDESELECT(umask);
    852 
    853 	FDSETMOTOR(0);
    854 	delay(1);
    855 	FDSELECT(umask);
    856 	delay(1);
    857 	FDDESELECT(umask);
    858 
    859 	for (idb = 0x80000000; idb; idb >>= 1) {
    860 		FDSELECT(umask);
    861 		delay(1);
    862 		if (FDTESTC(FDB_READY) == 0)
    863 			id |= idb;
    864 		FDDESELECT(umask);
    865 		delay(1);
    866 	}
    867 #ifdef FDDEBUG
    868 	printf("fdcgettype unit %d id 0x%x\n", unit, id);
    869 #endif
    870 
    871 	for (cnt = 0, ftp = fdtype; cnt < nfdtype; ftp++, cnt++)
    872 		if (ftp->driveid == id)
    873 			return(ftp);
    874 	/*
    875 	 * 3.5dd's at unit 0 do not always return id.
    876 	 */
    877 	if (unit == 0)
    878 		return(fdtype);
    879 	return(NULL);
    880 }
    881 
    882 /*
    883  * turn motor off if possible otherwise mark as needed and will be done
    884  * later.
    885  */
    886 void
    887 fdmotoroff(arg)
    888 	void *arg;
    889 {
    890 	struct fd_softc *sc;
    891 	int unitmask, s;
    892 
    893 	sc = arg;
    894 	s = splbio();
    895 
    896 #ifdef FDDEBUG
    897 	printf("fdmotoroff: unit %d\n", sc->hwunit);
    898 #endif
    899 	if ((sc->flags & FDF_MOTORON) == 0)
    900 		goto done;
    901 	/*
    902 	 * if we have a timeout on a dma operation let fddmadone()
    903 	 * deal with it.
    904 	 */
    905 	if (fdc_indma == sc) {
    906 		fddmadone(sc, 1);
    907 		goto done;
    908 	}
    909 #ifdef FDDEBUG
    910 	printf(" motor was on, turning off\n");
    911 #endif
    912 
    913 	/*
    914 	 * flush cache if needed
    915 	 */
    916 	if (sc->flags & FDF_DIRTY) {
    917 		sc->flags |= FDF_JUSTFLUSH | FDF_MOTOROFF;
    918 #ifdef FDDEBUG
    919 		printf("  flushing dirty buffer first\n");
    920 #endif
    921 		/*
    922 		 * if dma'ing done for now, fddone() will call us again
    923 		 */
    924 		if (fdc_indma)
    925 			goto done;
    926 		fddmastart(sc, sc->cachetrk);
    927 		goto done;
    928 	}
    929 
    930 	/*
    931 	 * if controller is busy just schedule us to be called back
    932 	 */
    933 	if (fdc_indma) {
    934 		/*
    935 		 * someone else has the controller now
    936 		 * just set flag and let fddone() call us again.
    937 		 */
    938 		sc->flags |= FDF_MOTOROFF;
    939 		goto done;
    940 	}
    941 
    942 #ifdef FDDEBUG
    943 	printf("  hw turing unit off\n");
    944 #endif
    945 
    946 	sc->flags &= ~(FDF_MOTORON | FDF_MOTOROFF);
    947 	FDDESELECT(FDCUNITMASK);
    948 	FDSETMOTOR(0);
    949 	delay(1);
    950 	FDSELECT(sc->unitmask);
    951 	delay(4);
    952 	FDDESELECT(sc->unitmask);
    953 	delay(1);
    954 	if (sc->flags & FDF_WMOTOROFF)
    955 		wakeup(fdmotoroff);
    956 done:
    957 	splx(s);
    958 }
    959 
    960 /*
    961  * select drive seek to track exit with motor on.
    962  * fdsetpos(x, 0, 0) does calibrates the drive.
    963  */
    964 void
    965 fdsetpos(sc, trk, towrite)
    966 	struct fd_softc *sc;
    967 	int trk, towrite;
    968 {
    969 	int nstep, sdir, ondly, ncyl, nside;
    970 
    971 	FDDESELECT(FDCUNITMASK);
    972 	FDSETMOTOR(1);
    973 	delay(1);
    974 	FDSELECT(sc->unitmask);
    975 	delay(1);
    976 	if ((sc->flags & FDF_MOTORON) == 0) {
    977 		ondly = 0;
    978 		while (FDTESTC(FDB_READY) == 0) {
    979 			delay(1000);
    980 			if (++ondly >= 1000)
    981 				break;
    982 		}
    983 	}
    984 	sc->flags |= FDF_MOTORON;
    985 
    986 	ncyl = trk / FDNHEADS;
    987 	nside = trk % FDNHEADS;
    988 
    989 	if (sc->curcyl == ncyl && fdc_side == nside)
    990 		return;
    991 
    992 	if (towrite)
    993 		sc->flags |= FDF_WRITEWAIT;
    994 
    995 #ifdef FDDEBUG
    996 	printf("fdsetpos: cyl %d head %d towrite %d\n", trk / FDNHEADS,
    997 	    trk % FDNHEADS, towrite);
    998 #endif
    999 	nstep = ncyl - sc->curcyl;
   1000 	if (nstep) {
   1001 		/*
   1002 		 * figure direction
   1003 		 */
   1004 		if (nstep > 0 && ncyl != 0) {
   1005 			sdir = FDSTEPIN;
   1006 			FDSETDIR(1);
   1007 		} else {
   1008 			nstep = -nstep;
   1009 			sdir = FDSTEPOUT;
   1010 			FDSETDIR(0);
   1011 		}
   1012 		if (ncyl == 0) {
   1013 			/*
   1014 			 * either just want cylinder 0 or doing
   1015 			 * a calibrate.
   1016 			 */
   1017 			while (FDTESTC(FDB_CYLZERO) == 0) {
   1018 				FDSTEP;
   1019 				delay(sc->stepdelay);
   1020 			}
   1021 		} else {
   1022 			/*
   1023 			 * step the needed amount amount.
   1024 			 */
   1025 			while (nstep--) {
   1026 				FDSTEP;
   1027 				delay(sc->stepdelay);
   1028 			}
   1029 		}
   1030 		/*
   1031 		 * if switched directions
   1032 		 * allow drive to settle.
   1033 		 */
   1034 		if (sc->pstepdir != sdir)
   1035 			delay(FDSETTLEDELAY);
   1036 		sc->pstepdir = sdir;
   1037 		sc->curcyl = ncyl;
   1038 	}
   1039 	if (nside == fdc_side)
   1040 		return;
   1041 	/*
   1042 	 * select side
   1043 	 */
   1044 	fdc_side = nside;
   1045 	FDSETHEAD(nside);
   1046 	delay(FDPRESIDEDELAY);
   1047 }
   1048 
   1049 void
   1050 fdselunit(sc)
   1051 	struct fd_softc *sc;
   1052 {
   1053 	FDDESELECT(FDCUNITMASK);		/* deselect all */
   1054 	FDSETMOTOR(sc->flags & FDF_MOTORON);	/* set motor to unit's state */
   1055 	delay(1);
   1056 	FDSELECT(sc->unitmask);			/* select unit */
   1057 	delay(1);
   1058 }
   1059 
   1060 /*
   1061  * process next buf on device queue.
   1062  * normall sequence of events:
   1063  * fdstart() -> fddmastart();
   1064  * fdintr() -> fddmadone() -> fddone();
   1065  * if the track is in the cache then fdstart() will short-circuit
   1066  * to fddone() else if the track cache is dirty it will flush.  If
   1067  * the buf is not an entire track it will cache the requested track.
   1068  */
   1069 void
   1070 fdstart(sc)
   1071 	struct fd_softc *sc;
   1072 {
   1073 	int trk, error, write;
   1074 	struct buf *bp, *dp;
   1075 
   1076 #ifdef FDDEBUG
   1077 	printf("fdstart: unit %d\n", sc->hwunit);
   1078 #endif
   1079 
   1080 	/*
   1081 	 * if dma'ing just return. we must have been called from fdstartegy.
   1082 	 */
   1083 	if (fdc_indma)
   1084 		return;
   1085 
   1086 	/*
   1087 	 * get next buf if there.
   1088 	 */
   1089 	dp = &sc->bufq;
   1090 	if ((bp = dp->b_actf) == NULL) {
   1091 #ifdef FDDEBUG
   1092 		printf("  nothing to do\n");
   1093 #endif
   1094 		return;
   1095 	}
   1096 
   1097 	/*
   1098 	 * make sure same disk is loaded
   1099 	 */
   1100 	fdselunit(sc);
   1101 	if (FDTESTC(FDB_CHANGED)) {
   1102 		/*
   1103 		 * disk missing, invalidate all future io on
   1104 		 * this unit until re-open()'ed also invalidate
   1105 		 * all current io
   1106 		 */
   1107 #ifdef FDDEBUG
   1108 		printf("  disk was removed invalidating all io\n");
   1109 #endif
   1110 		sc->flags &= ~FDF_HAVELABEL;
   1111 		for (;;) {
   1112 			bp->b_flags |= B_ERROR;
   1113 			bp->b_error = EIO;
   1114 			if (bp->b_actf == NULL)
   1115 				break;
   1116 			biodone(bp);
   1117 			bp = bp->b_actf;
   1118 		}
   1119 		/*
   1120 		 * do fddone() on last buf to allow other units to start.
   1121 		 */
   1122 		dp->b_actf = bp;
   1123 		fddone(sc);
   1124 		return;
   1125 	}
   1126 
   1127 	/*
   1128 	 * we have a valid buf, setup our local version
   1129 	 * we use this count to allow reading over multiple tracks.
   1130 	 * into a single buffer
   1131 	 */
   1132 	dp->b_bcount = bp->b_bcount;
   1133 	dp->b_blkno = bp->b_blkno;
   1134 	dp->b_data = bp->b_data;
   1135 	dp->b_flags = bp->b_flags;
   1136 	dp->b_resid = 0;
   1137 
   1138 	if (bp->b_flags & B_READ)
   1139 		write = 0;
   1140 	else if (FDTESTC(FDB_PROTECT) == 0)
   1141 		write = 1;
   1142 	else {
   1143 		error = EPERM;
   1144 		goto bad;
   1145 	}
   1146 
   1147 	/*
   1148 	 * figure trk given blkno
   1149 	 */
   1150 	trk = bp->b_blkno / sc->nsectors;
   1151 
   1152 	/*
   1153 	 * check to see if same as currently cached track
   1154 	 * if so we need to do no dma read.
   1155 	 */
   1156 	if (trk == sc->cachetrk) {
   1157 		fddone(sc);
   1158 		return;
   1159 	}
   1160 
   1161 	/*
   1162 	 * if we will be overwriting the entire cache, don't bother to
   1163 	 * fetch it.
   1164 	 */
   1165 	if (bp->b_bcount == (sc->nsectors * FDSECSIZE) && write) {
   1166 		if (sc->flags & FDF_DIRTY)
   1167 			sc->flags |= FDF_JUSTFLUSH;
   1168 		else {
   1169 			sc->cachetrk = trk;
   1170 			fddone(sc);
   1171 			return;
   1172 		}
   1173 	}
   1174 
   1175 	/*
   1176 	 * start dma read of `trk'
   1177 	 */
   1178 	fddmastart(sc, trk);
   1179 	return;
   1180 bad:
   1181 	bp->b_flags |= B_ERROR;
   1182 	bp->b_error = error;
   1183 	fddone(sc);
   1184 }
   1185 
   1186 /*
   1187  * continue a started operation on next track.
   1188  */
   1189 void
   1190 fdcont(sc)
   1191 	struct fd_softc *sc;
   1192 {
   1193 	struct buf *dp, *bp;
   1194 	int trk, write;
   1195 
   1196 	dp = &sc->bufq;
   1197 	bp = dp->b_actf;
   1198 	dp->b_data += (dp->b_bcount - bp->b_resid);
   1199 	dp->b_blkno += (dp->b_bcount - bp->b_resid) / FDSECSIZE;
   1200 	dp->b_bcount = bp->b_resid;
   1201 
   1202 	/*
   1203 	 * figure trk given blkno
   1204 	 */
   1205 	trk = dp->b_blkno / sc->nsectors;
   1206 #ifdef DEBUG
   1207 	if (trk == sc->cachetrk || trk != sc->cachetrk + 1)
   1208 		panic("fdcont: confused");
   1209 #endif
   1210 	if (dp->b_flags & B_READ)
   1211 		write = 0;
   1212 	else
   1213 		write = 1;
   1214 	/*
   1215 	 * if we will be overwriting the entire cache, don't bother to
   1216 	 * fetch it.
   1217 	 */
   1218 	if (dp->b_bcount == (sc->nsectors * FDSECSIZE) && write) {
   1219 		if (sc->flags & FDF_DIRTY)
   1220 			sc->flags |= FDF_JUSTFLUSH;
   1221 		else {
   1222 			sc->cachetrk = trk;
   1223 			fddone(sc);
   1224 			return;
   1225 		}
   1226 	}
   1227 	/*
   1228 	 * start dma read of `trk'
   1229 	 */
   1230 	fddmastart(sc, trk);
   1231 	return;
   1232 }
   1233 
   1234 void
   1235 fddmastart(sc, trk)
   1236 	struct fd_softc *sc;
   1237 	int trk;
   1238 {
   1239 	int adkmask, ndmaw, write, dmatrk;
   1240 
   1241 #ifdef FDDEBUG
   1242 	printf("fddmastart: unit %d cyl %d head %d", sc->hwunit,
   1243 	    trk / FDNHEADS, trk % FDNHEADS);
   1244 #endif
   1245 	/*
   1246 	 * flush the cached track if dirty else read requested track.
   1247 	 */
   1248 	if (sc->flags & FDF_DIRTY) {
   1249 		fdcachetoraw(sc);
   1250 		ndmaw = sc->type->nwritew;
   1251 		dmatrk = sc->cachetrk;
   1252 		write = 1;
   1253 	} else {
   1254 		ndmaw = sc->type->nreadw;
   1255 		dmatrk = trk;
   1256 		write = 0;
   1257 	}
   1258 
   1259 #ifdef FDDEBUG
   1260 	printf(" %s", write ? " flushing cache\n" : " loading cache\n");
   1261 #endif
   1262 	sc->cachetrk = trk;
   1263 	fdc_indma = sc;
   1264 	fdsetpos(sc, dmatrk, write);
   1265 
   1266 	/*
   1267 	 * setup dma stuff
   1268 	 */
   1269 	if (write == 0) {
   1270 		custom.adkcon = ADKF_MSBSYNC;
   1271 		custom.adkcon = ADKF_SETCLR | ADKF_WORDSYNC | ADKF_FAST;
   1272 		custom.dsksync = FDMFMSYNC;
   1273 	} else {
   1274 		custom.adkcon = ADKF_PRECOMP1 | ADKF_PRECOMP0 | ADKF_WORDSYNC |
   1275 		    ADKF_MSBSYNC;
   1276 		adkmask = ADKF_SETCLR | ADKF_FAST | ADKF_MFMPREC;
   1277 		if (dmatrk >= sc->type->precomp[0])
   1278 			adkmask |= ADKF_PRECOMP0;
   1279 		if (dmatrk >= sc->type->precomp[1])
   1280 			adkmask |= ADKF_PRECOMP1;
   1281 		custom.adkcon = adkmask;
   1282 	}
   1283 	custom.dskpt = (u_char *)kvtop(fdc_dmap);
   1284 	FDDMASTART(ndmaw, write);
   1285 
   1286 #ifdef FDDEBUG
   1287 	printf("  dma started\n");
   1288 #endif
   1289 }
   1290 
   1291 /*
   1292  * recalibrate the drive
   1293  */
   1294 void
   1295 fdcalibrate(arg)
   1296 	void *arg;
   1297 {
   1298 	struct fd_softc *sc;
   1299 	static int loopcnt;
   1300 
   1301 	sc = arg;
   1302 
   1303 	if (loopcnt == 0) {
   1304 		/*
   1305 		 * seek cyl 0
   1306 		 */
   1307 		fdc_indma = sc;
   1308 		sc->stepdelay += 900;
   1309 		if (sc->cachetrk > 1)
   1310 			fdsetpos(sc, sc->cachetrk % FDNHEADS, 0);
   1311 		sc->stepdelay -= 900;
   1312 	}
   1313 	if (loopcnt++ & 1)
   1314 		fdsetpos(sc, sc->cachetrk, 0);
   1315 	else
   1316 		fdsetpos(sc, sc->cachetrk + FDNHEADS, 0);
   1317 	/*
   1318 	 * trk++, trk, trk++, trk, trk++, trk, trk++, trk and dma
   1319 	 */
   1320 	if (loopcnt < 8)
   1321 		timeout(fdcalibrate, sc, hz / 8);
   1322 	else {
   1323 		loopcnt = 0;
   1324 		fdc_indma = NULL;
   1325 		timeout(fdmotoroff, sc, 3 * hz / 2);
   1326 		fddmastart(sc, sc->cachetrk);
   1327 	}
   1328 }
   1329 
   1330 void
   1331 fddmadone(sc, timeo)
   1332 	struct fd_softc *sc;
   1333 	int timeo;
   1334 {
   1335 #ifdef FDDEBUG
   1336 	printf("fddmadone: unit %d, timeo %d\n", sc->hwunit, timeo);
   1337 #endif
   1338 	fdc_indma = NULL;
   1339 	untimeout(fdmotoroff, sc);
   1340 	FDDMASTOP;
   1341 
   1342 	/*
   1343 	 * guarantee the drive has been at current head and cyl
   1344 	 * for at least FDWRITEDELAY after a write.
   1345 	 */
   1346 	if (sc->flags & FDF_WRITEWAIT) {
   1347 		delay(FDWRITEDELAY);
   1348 		sc->flags &= ~FDF_WRITEWAIT;
   1349 	}
   1350 
   1351 	if ((sc->flags & FDF_MOTOROFF) == 0) {
   1352 		/*
   1353 		 * motor runs for 1.5 seconds after last dma
   1354 		 */
   1355 		timeout(fdmotoroff, sc, 3 * hz / 2);
   1356 	}
   1357 	if (sc->flags & FDF_DIRTY) {
   1358 		/*
   1359 		 * if buffer dirty, the last dma cleaned it
   1360 		 */
   1361 		sc->flags &= ~FDF_DIRTY;
   1362 		if (timeo)
   1363 			printf("%s: write of track cache timed out.\n",
   1364 			    sc->dkdev.dk_dev.dv_xname);
   1365 		if (sc->flags & FDF_JUSTFLUSH) {
   1366 			/*
   1367 			 * we are done dma'ing
   1368 			 */
   1369 			fddone(sc);
   1370 			return;
   1371 		}
   1372 		/*
   1373 		 * load the cache
   1374 		 */
   1375 		fddmastart(sc, sc->cachetrk);
   1376 		return;
   1377 	}
   1378 #ifdef FDDEBUG
   1379 	else if (sc->flags & FDF_MOTOROFF)
   1380 		panic("fddmadone: FDF_MOTOROFF with no FDF_DIRTY");
   1381 #endif
   1382 
   1383 	/*
   1384 	 * cache loaded decode it into cache buffer
   1385 	 */
   1386 	if (timeo == 0 && fdrawtocache(sc) == 0)
   1387 		sc->retried = 0;
   1388 	else {
   1389 #ifdef FDDEBUG
   1390 		if (timeo)
   1391 			printf("%s: fddmadone: cache load timed out.\n",
   1392 			    sc->dkdev.dk_dev.dv_xname);
   1393 #endif
   1394 		if (sc->retried >= sc->retries) {
   1395 			sc->retried = 0;
   1396 			sc->cachetrk = -1;
   1397 		} else {
   1398 			sc->retried++;
   1399 			/*
   1400 			 * this will be restarted at end of calibrate loop.
   1401 			 */
   1402 			untimeout(fdmotoroff, sc);
   1403 			fdcalibrate(sc);
   1404 			return;
   1405 		}
   1406 	}
   1407 	fddone(sc);
   1408 }
   1409 
   1410 void
   1411 fddone(sc)
   1412 	struct fd_softc *sc;
   1413 {
   1414 	struct buf *dp, *bp;
   1415 	char *data;
   1416 	int sz, blk;
   1417 
   1418 #ifdef FDDEBUG
   1419 	printf("fddone: unit %d\n", sc->hwunit);
   1420 #endif
   1421 	/*
   1422 	 * check to see if unit is just flushing the cache,
   1423 	 * if bufq is not for us.
   1424 	 */
   1425 	if (sc->flags & FDF_JUSTFLUSH) {
   1426 		sc->flags &= ~FDF_JUSTFLUSH;
   1427 		goto nobuf;
   1428 	}
   1429 
   1430 #ifdef DEBUG
   1431 	if (sc->flags & FDF_MOTOROFF)
   1432 		panic("fddone: motoroff on dma unit");
   1433 #endif
   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