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