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