Home | History | Annotate | Line # | Download | only in dev
fd.c revision 1.12
      1  1.12       jtk /*	$NetBSD: fd.c,v 1.12 1997/07/17 02:16:19 jtk Exp $	*/
      2   1.1       oki 
      3   1.1       oki /*-
      4   1.1       oki  * Copyright (c) 1993, 1994, 1995 Charles Hannum.
      5   1.1       oki  * Copyright (c) 1990 The Regents of the University of California.
      6   1.1       oki  * All rights reserved.
      7   1.1       oki  *
      8   1.1       oki  * This code is derived from software contributed to Berkeley by
      9   1.1       oki  * Don Ahn.
     10   1.1       oki  *
     11   1.1       oki  * Redistribution and use in source and binary forms, with or without
     12   1.1       oki  * modification, are permitted provided that the following conditions
     13   1.1       oki  * are met:
     14   1.1       oki  * 1. Redistributions of source code must retain the above copyright
     15   1.1       oki  *    notice, this list of conditions and the following disclaimer.
     16   1.1       oki  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       oki  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       oki  *    documentation and/or other materials provided with the distribution.
     19   1.1       oki  * 3. All advertising materials mentioning features or use of this software
     20   1.1       oki  *    must display the following acknowledgement:
     21   1.1       oki  *	This product includes software developed by the University of
     22   1.1       oki  *	California, Berkeley and its contributors.
     23   1.1       oki  * 4. Neither the name of the University nor the names of its contributors
     24   1.1       oki  *    may be used to endorse or promote products derived from this software
     25   1.1       oki  *    without specific prior written permission.
     26   1.1       oki  *
     27   1.1       oki  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28   1.1       oki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29   1.1       oki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30   1.1       oki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31   1.1       oki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32   1.1       oki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33   1.1       oki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34   1.1       oki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35   1.1       oki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36   1.1       oki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37   1.1       oki  * SUCH DAMAGE.
     38   1.1       oki  *
     39   1.1       oki  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     40   1.1       oki  */
     41   1.1       oki 
     42   1.1       oki #include <sys/param.h>
     43   1.1       oki #include <sys/systm.h>
     44   1.1       oki #include <sys/kernel.h>
     45   1.1       oki #include <sys/conf.h>
     46   1.1       oki #include <sys/file.h>
     47   1.1       oki #include <sys/stat.h>
     48   1.1       oki #include <sys/ioctl.h>
     49   1.1       oki #include <sys/malloc.h>
     50   1.1       oki #include <sys/device.h>
     51   1.1       oki #include <sys/disklabel.h>
     52   1.1       oki #include <sys/dkstat.h>
     53   1.1       oki #include <sys/disk.h>
     54   1.1       oki #include <sys/buf.h>
     55   1.1       oki #include <sys/uio.h>
     56   1.1       oki #include <sys/syslog.h>
     57   1.1       oki #include <sys/queue.h>
     58   1.1       oki 
     59   1.1       oki #include <machine/cpu.h>
     60   1.1       oki 
     61   1.1       oki #include <x68k/x68k/iodevice.h>
     62   1.1       oki #include <x68k/dev/dmavar.h>
     63   1.1       oki #include <x68k/dev/fdreg.h>
     64   1.1       oki #include <x68k/dev/opmreg.h>
     65   1.1       oki 
     66  1.12       jtk #include "locators.h"
     67  1.12       jtk 
     68   1.1       oki #define infdc   (IODEVbase->io_fdc)
     69   1.1       oki 
     70   1.1       oki #ifdef DEBUG
     71   1.8  christos #define DPRINTF(x)      if (fddebug) printf x
     72   1.1       oki int     fddebug = 0;
     73   1.1       oki #else
     74   1.1       oki #define DPRINTF(x)
     75   1.1       oki #endif
     76   1.1       oki 
     77   1.1       oki #define FDUNIT(dev)	(minor(dev) / 8)
     78   1.1       oki #define FDTYPE(dev)	(minor(dev) % 8)
     79   1.1       oki 
     80   1.1       oki #define b_cylin b_resid
     81   1.1       oki 
     82   1.1       oki enum fdc_state {
     83   1.1       oki 	DEVIDLE = 0,
     84   1.1       oki 	MOTORWAIT,
     85   1.1       oki 	DOSEEK,
     86   1.1       oki 	SEEKWAIT,
     87   1.1       oki 	SEEKTIMEDOUT,
     88   1.1       oki 	SEEKCOMPLETE,
     89   1.1       oki 	DOIO,
     90   1.1       oki 	IOCOMPLETE,
     91   1.1       oki 	IOTIMEDOUT,
     92   1.1       oki 	DORESET,
     93   1.1       oki 	RESETCOMPLETE,
     94   1.1       oki 	RESETTIMEDOUT,
     95   1.1       oki 	DORECAL,
     96   1.1       oki 	RECALWAIT,
     97   1.1       oki 	RECALTIMEDOUT,
     98   1.1       oki 	RECALCOMPLETE,
     99   1.1       oki 	DOCOPY,
    100   1.1       oki 	DOIOHALF,
    101   1.1       oki 	COPYCOMPLETE,
    102   1.1       oki };
    103   1.1       oki 
    104   1.1       oki /* software state, per controller */
    105   1.1       oki struct fdc_softc {
    106   1.1       oki 	struct device sc_dev;		/* boilerplate */
    107   1.1       oki 	u_char	sc_flags;
    108   1.1       oki 
    109   1.1       oki 	struct fd_softc *sc_fd[4];	/* pointers to children */
    110   1.1       oki 	TAILQ_HEAD(drivehead, fd_softc) sc_drives;
    111   1.1       oki 	enum fdc_state sc_state;
    112   1.1       oki 	int sc_errors;			/* number of retries so far */
    113   1.1       oki 	u_char sc_status[7];		/* copy of registers */
    114   1.1       oki } fdc_softc;
    115   1.1       oki 
    116   1.1       oki /* controller driver configuration */
    117   1.1       oki int fdcinit();
    118   1.1       oki void fdcstart();
    119   1.1       oki void fdcgo();
    120   1.1       oki int fdcintr ();
    121   1.1       oki void fdcdone();
    122   1.1       oki void fdcreset();
    123   1.1       oki 
    124   1.1       oki /* controller driver configuration */
    125   1.1       oki int fdcprobe __P((struct device *, void *, void *));
    126   1.1       oki void fdcattach __P((struct device *, struct device *, void *));
    127   1.1       oki 
    128   1.1       oki struct cfattach fdc_ca = {
    129   1.1       oki 	sizeof(struct fdc_softc), fdcprobe, fdcattach
    130   1.1       oki };
    131   1.1       oki 
    132   1.1       oki struct cfdriver fdc_cd = {
    133   1.1       oki 	NULL, "fdc", DV_DULL
    134   1.1       oki };
    135   1.1       oki 
    136   1.1       oki /*
    137   1.1       oki  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    138   1.1       oki  * we tell them apart.
    139   1.1       oki  */
    140   1.1       oki struct fd_type {
    141   1.1       oki 	int	sectrac;	/* sectors per track */
    142   1.1       oki 	int	heads;		/* number of heads */
    143   1.1       oki 	int	seccyl;		/* sectors per cylinder */
    144   1.1       oki 	int	secsize;	/* size code for sectors */
    145   1.1       oki 	int	datalen;	/* data len when secsize = 0 */
    146   1.1       oki 	int	steprate;	/* step rate and head unload time */
    147   1.1       oki 	int	gap1;		/* gap len between sectors */
    148   1.1       oki 	int	gap2;		/* formatting gap */
    149   1.1       oki 	int	tracks;		/* total num of tracks */
    150   1.1       oki 	int	size;		/* size of disk in sectors */
    151   1.1       oki 	int	step;		/* steps per cylinder */
    152   1.1       oki 	int	rate;		/* transfer speed code */
    153   1.1       oki 	char	*name;
    154   1.1       oki };
    155   1.1       oki 
    156   1.1       oki /* The order of entries in the following table is important -- BEWARE! */
    157   1.1       oki struct fd_type fd_types[] = {
    158   1.1       oki         {  8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS, "1.2MB/[1024bytes/sector]"    }, /* 1.2 MB japanese format */
    159   1.1       oki         { 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    }, /* 1.44MB diskette */
    160   1.1       oki         { 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    }, /* 1.2 MB AT-diskettes */
    161   1.1       oki         {  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" }, /* 360kB in 1.2MB drive */
    162   1.1       oki         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" }, /* 360kB PC diskettes */
    163   1.1       oki         {  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    }, /* 3.5" 720kB diskette */
    164   1.1       oki         {  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  }, /* 720kB in 1.2MB drive */
    165   1.1       oki         {  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  }, /* 360kB in 720kB drive */
    166   1.1       oki };
    167   1.1       oki 
    168   1.1       oki /* software state, per disk (with up to 4 disks per ctlr) */
    169   1.1       oki struct fd_softc {
    170   1.1       oki 	struct device sc_dev;
    171   1.1       oki 	struct disk sc_dk;
    172   1.1       oki 
    173   1.1       oki 	struct fd_type *sc_deftype;	/* default type descriptor */
    174   1.1       oki 	struct fd_type *sc_type;	/* current type descriptor */
    175   1.1       oki 
    176   1.1       oki 	daddr_t	sc_blkno;	/* starting block number */
    177   1.1       oki 	int sc_bcount;		/* byte count left */
    178   1.1       oki 	int sc_skip;		/* bytes already transferred */
    179   1.1       oki 	int sc_nblks;		/* number of blocks currently tranferring */
    180   1.1       oki 	int sc_nbytes;		/* number of bytes currently tranferring */
    181   1.1       oki 
    182   1.1       oki 	int sc_drive;		/* physical unit number */
    183   1.1       oki 	int sc_flags;
    184   1.1       oki #define	FD_BOPEN	0x01		/* it's open */
    185   1.1       oki #define	FD_COPEN	0x02		/* it's open */
    186   1.1       oki #define	FD_OPEN		(FD_BOPEN|FD_COPEN)	/* it's open */
    187   1.1       oki #define	FD_MOTOR	0x04		/* motor should be on */
    188   1.1       oki #define	FD_MOTOR_WAIT	0x08		/* motor coming up */
    189   1.1       oki #define	FD_ALIVE	0x10		/* alive */
    190   1.1       oki 	int sc_cylin;		/* where we think the head is */
    191   1.1       oki 
    192   1.1       oki 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    193   1.1       oki 	int sc_ops;		/* I/O ops since last switch */
    194   1.1       oki 	struct buf sc_q;	/* head of buf chain */
    195   1.1       oki 	u_char *sc_copybuf;	/* for secsize >=3 */
    196   1.1       oki 	u_char sc_part;		/* for secsize >=3 */
    197   1.1       oki #define	SEC_P10	0x02		/* first part */
    198   1.1       oki #define	SEC_P01	0x01		/* second part */
    199   1.1       oki #define	SEC_P11	0x03		/* both part */
    200   1.1       oki };
    201   1.1       oki 
    202   1.1       oki /* floppy driver configuration */
    203   1.1       oki int fdprobe __P((struct device *, void *, void *));
    204   1.1       oki void fdattach __P((struct device *, struct device *, void *));
    205   1.1       oki 
    206   1.1       oki struct cfattach fd_ca = {
    207   1.1       oki 	sizeof(struct fd_softc), fdprobe, fdattach
    208   1.1       oki };
    209   1.1       oki 
    210   1.1       oki struct cfdriver fd_cd = {
    211   1.1       oki 	NULL, "fd", DV_DISK
    212   1.1       oki };
    213   1.1       oki 
    214   1.1       oki /* floppy driver configuration */
    215   1.1       oki void fdstart __P((struct fd_softc *fd));
    216   1.1       oki void fdgo();
    217   1.1       oki void fdintr();
    218   1.1       oki 
    219   1.1       oki void fdstrategy __P((struct buf *));
    220   1.1       oki 
    221   1.1       oki struct dkdriver fddkdriver = { fdstrategy };
    222   1.1       oki 
    223   1.1       oki void fd_set_motor __P((struct fdc_softc *fdc, int reset));
    224   1.1       oki void fd_motor_off __P((void *arg));
    225   1.1       oki void fd_motor_on __P((void *arg));
    226   1.1       oki int fdcresult __P((struct fdc_softc *fdc));
    227   1.1       oki int out_fdc __P((u_char x));
    228   1.1       oki void fdcstart __P((struct fdc_softc *fdc));
    229   1.1       oki void fdcstatus __P((struct device *dv, int n, char *s));
    230   1.1       oki void fdctimeout __P((void *arg));
    231   1.1       oki void fdcpseudointr __P((void *arg));
    232   1.1       oki void fdcretry __P((struct fdc_softc *fdc));
    233   1.1       oki void fdfinish __P((struct fd_softc *fd, struct buf *bp));
    234   1.1       oki static int fdgetdisklabel __P((struct fd_softc *, dev_t));
    235   1.1       oki static void fd_do_eject __P((int));
    236   1.4       oki void fd_mountroot_hook __P((struct device *));
    237   1.1       oki 
    238   1.1       oki #define FDDI_EN	0x02
    239   1.1       oki #define FDCI_EN	0x04
    240   1.1       oki #define	FDD_INT	0x40
    241   1.1       oki #define	FDC_INT	0x80
    242   1.1       oki 
    243   1.1       oki #define DMA_BRD	0x01
    244   1.1       oki #define	DMA_BWR	0x02
    245   1.1       oki 
    246   1.1       oki #define DRQ 0
    247   1.1       oki 
    248   1.1       oki static u_char *fdc_dmabuf;
    249   1.1       oki 
    250   1.1       oki static inline void
    251   1.1       oki fdc_dmastart(read, addr, count)
    252   1.1       oki 	int read;
    253   1.1       oki 	caddr_t addr;
    254   1.1       oki 	int count;
    255   1.1       oki {
    256   1.1       oki 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    257   1.1       oki 
    258   1.2       oki 	DPRINTF(("fdc_dmastart: (%s, addr = %p, count = %d\n",
    259   1.1       oki 		 read ? "read" : "write", addr, count));
    260   1.1       oki 	if (dmarangecheck((vm_offset_t)addr, count)) {
    261   1.1       oki 		dma_bouncebytes[DRQ] = count;
    262   1.1       oki 		dma_dataaddr[DRQ] = addr;
    263   1.1       oki 		if (!(read)) {
    264   1.1       oki 			bcopy(addr, dma_bouncebuf[DRQ], count);
    265   1.1       oki 			dma_bounced[DRQ] = DMA_BWR;
    266   1.1       oki 		} else {
    267   1.1       oki 			dma_bounced[DRQ] = DMA_BRD;
    268   1.1       oki 		}
    269   1.1       oki 		addr = dma_bouncebuf[DRQ];
    270   1.1       oki 	} else {
    271   1.1       oki 		dma_bounced[DRQ] = 0;
    272   1.1       oki 	}
    273   1.1       oki 
    274   1.1       oki 	dmac->csr = 0xff;
    275   1.1       oki 	dmac->ocr = read ? 0xb2 : 0x32;
    276   1.1       oki 	dmac->mtc = (unsigned short)count;
    277   1.1       oki 	asm("nop");
    278   1.1       oki 	asm("nop");
    279   1.1       oki 	dmac->mar = (unsigned long)kvtop(addr);
    280   1.1       oki #if defined(M68040)
    281   1.1       oki 		/*
    282   1.1       oki 		 * Push back dirty cache lines
    283   1.1       oki 		 */
    284   1.1       oki 		if (mmutype == MMU_68040)
    285   1.1       oki 			DCFP(kvtop(addr));
    286   1.1       oki #endif
    287   1.1       oki 	dmac->ccr = 0x88;
    288   1.1       oki }
    289   1.1       oki 
    290   1.1       oki void
    291   1.1       oki fdcdmaintr()
    292   1.1       oki {
    293   1.1       oki 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    294   1.1       oki 	dmac->csr = 0xff;
    295   1.1       oki 	PCIA(); /* XXX? by oki */
    296   1.1       oki 	if (dma_bounced[DRQ] == DMA_BRD) {
    297   1.1       oki 		bcopy(dma_bouncebuf[DRQ], dma_dataaddr[DRQ], dma_bouncebytes[DRQ]);
    298   1.1       oki 	}
    299   1.1       oki 	dma_bounced[DRQ] = 0;
    300   1.1       oki }
    301   1.1       oki 
    302   1.1       oki void
    303   1.1       oki fdcdmaerrintr()
    304   1.1       oki {
    305   1.1       oki 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    306   1.8  christos 	printf("fdcdmaerrintr: csr=%x, cer=%x\n", dmac->csr, dmac->cer);
    307   1.1       oki 	dmac->csr = 0xff;
    308   1.1       oki }
    309   1.1       oki 
    310   1.1       oki int
    311   1.1       oki fdcprobe(parent, match, aux)
    312   1.1       oki 	struct device *parent;
    313   1.1       oki 	void *match, *aux;
    314   1.1       oki {
    315   1.1       oki 	if (strcmp("fdc", aux) != 0)
    316   1.1       oki 		return 0;
    317   1.1       oki 	return 1;
    318   1.1       oki }
    319   1.1       oki 
    320   1.1       oki /*
    321   1.1       oki  * Arguments passed between fdcattach and fdprobe.
    322   1.1       oki  */
    323   1.1       oki struct fdc_attach_args {
    324   1.1       oki 	int fa_drive;
    325   1.1       oki 	struct fd_type *fa_deftype;
    326   1.1       oki };
    327   1.1       oki 
    328   1.1       oki /*
    329   1.1       oki  * Print the location of a disk drive (called just before attaching the
    330   1.1       oki  * the drive).  If `fdc' is not NULL, the drive was found but was not
    331   1.1       oki  * in the system config file; print the drive name as well.
    332   1.1       oki  * Return QUIET (config_find ignores this if the device was configured) to
    333   1.1       oki  * avoid printing `fdN not configured' messages.
    334   1.1       oki  */
    335   1.1       oki int
    336   1.1       oki fdprint(aux, fdc)
    337   1.1       oki 	void *aux;
    338   1.6       cgd 	const char *fdc;
    339   1.1       oki {
    340   1.1       oki 	register struct fdc_attach_args *fa = aux;
    341   1.1       oki 
    342   1.1       oki 	if (!fdc)
    343   1.8  christos 		printf(" drive %d", fa->fa_drive);
    344   1.1       oki 	return QUIET;
    345   1.1       oki }
    346   1.1       oki 
    347   1.1       oki void
    348   1.1       oki fdcattach(parent, self, aux)
    349   1.1       oki 	struct device *parent, *self;
    350   1.1       oki 	void *aux;
    351   1.1       oki {
    352   1.1       oki 	struct fdc_softc *fdc = (void *)self;
    353   1.1       oki 	volatile struct dmac *dmac = &IODEVbase->io_dma[DRQ];
    354   1.1       oki 	struct fdc_attach_args fa;
    355   1.1       oki 
    356   1.1       oki 	fdc->sc_state = DEVIDLE;
    357   1.1       oki 	TAILQ_INIT(&fdc->sc_drives);
    358   1.1       oki 
    359   1.1       oki 	fdc->sc_flags  = 0;
    360   1.1       oki 
    361   1.1       oki 	/* reset */
    362   1.1       oki 	ioctlr.intr &= (~FDDI_EN);
    363   1.1       oki 	ioctlr.intr |= FDCI_EN;
    364   1.1       oki 	fdcresult(fdc);
    365   1.1       oki 	fdcreset();
    366   1.1       oki 
    367   1.1       oki 	/* Initialize DMAC channel */
    368   1.1       oki 	dmac->dcr = 0x80;
    369   1.1       oki 	dmac->scr = 0x04;
    370   1.1       oki 	dmac->csr = 0xff;
    371   1.1       oki 	dmac->cpr = 0x00;
    372   1.3       oki 	dmac->dar = (unsigned long) kvtop((void *)&infdc.data);
    373   1.1       oki 	dmac->mfc = 0x05;
    374   1.1       oki 	dmac->dfc = 0x05;
    375   1.1       oki 	dmac->bfc = 0x05;
    376   1.1       oki 	dmac->niv = 0x64;
    377   1.1       oki 	dmac->eiv = 0x65;
    378   1.1       oki 
    379   1.8  christos 	printf(": uPD72065 FDC\n");
    380   1.1       oki 	out_fdc(NE7CMD_SPECIFY);/* specify command */
    381   1.1       oki 	out_fdc(0xd0);
    382   1.1       oki 	out_fdc(0x10);
    383   1.1       oki 
    384   1.1       oki 	fdc_dmabuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
    385   1.1       oki 	if (fdc_dmabuf == 0)
    386   1.8  christos 		printf("fdcinit: WARNING!! malloc() failed.\n");
    387   1.1       oki 	dma_bouncebuf[DRQ] = fdc_dmabuf;
    388   1.1       oki 
    389   1.1       oki 	/* physical limit: four drives per controller. */
    390   1.1       oki 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
    391   1.1       oki 		(void)config_found(self, (void *)&fa, fdprint);
    392   1.1       oki 	}
    393   1.1       oki }
    394   1.1       oki 
    395   1.1       oki void
    396   1.1       oki fdcreset()
    397   1.1       oki {
    398   1.1       oki 	infdc.stat = FDC_RESET;
    399   1.1       oki }
    400   1.1       oki 
    401   1.1       oki static int
    402   1.1       oki fdcpoll(fdc)
    403   1.1       oki 	struct fdc_softc *fdc;
    404   1.1       oki {
    405   1.1       oki 	int i = 25000;
    406   1.1       oki 	while (--i > 0) {
    407   1.1       oki 		if ((ioctlr.intr & 0x80)) {
    408   1.1       oki 			out_fdc(NE7CMD_SENSEI);
    409   1.1       oki 			fdcresult(fdc);
    410   1.1       oki 			break;
    411   1.1       oki 		}
    412   1.1       oki 		DELAY(100);
    413   1.1       oki 	}
    414   1.1       oki 	return i;
    415   1.1       oki }
    416   1.1       oki 
    417   1.1       oki int
    418   1.1       oki fdprobe(parent, match, aux)
    419   1.1       oki 	struct device *parent;
    420   1.1       oki 	void *match, *aux;
    421   1.1       oki {
    422   1.1       oki 	struct fdc_softc *fdc = (void *)parent;
    423   1.1       oki 	struct cfdata *cf = match;
    424   1.1       oki 	struct fd_type *type;
    425   1.1       oki 	int drive = cf->cf_unit;
    426   1.1       oki 	int n;
    427   1.1       oki 	int found = 0;
    428   1.1       oki 	int i;
    429   1.1       oki 
    430  1.12       jtk 	if (cf->cf_loc[FDCCF_UNIT] != FDCCF_UNIT_DEFAULT &&
    431  1.12       jtk 	    cf->cf_loc[FDCCF_UNIT] != drive)
    432   1.1       oki 		return 0;
    433   1.1       oki 
    434   1.1       oki 	type = &fd_types[0];	/* XXX 1.2MB */
    435   1.1       oki 
    436   1.1       oki 	ioctlr.intr &= (~FDCI_EN);
    437   1.1       oki 
    438   1.1       oki 	/* select drive and turn on motor */
    439   1.1       oki 	infdc.select = 0x80 | (type->rate << 4)| drive;
    440   1.1       oki 	fdc_force_ready(FDCRDY);
    441   1.1       oki 	fdcpoll(fdc);
    442   1.1       oki 
    443   1.1       oki retry:
    444   1.1       oki 	out_fdc(NE7CMD_RECAL);
    445   1.1       oki 	out_fdc(drive);
    446   1.1       oki 
    447   1.1       oki 	i = 25000;
    448   1.1       oki 	while (--i > 0) {
    449   1.1       oki 		if ((ioctlr.intr & 0x80)) {
    450   1.1       oki 			out_fdc(NE7CMD_SENSEI);
    451   1.1       oki 			n = fdcresult(fdc);
    452   1.1       oki 			break;
    453   1.1       oki 		}
    454   1.1       oki 		DELAY(100);
    455   1.1       oki 	}
    456   1.1       oki 
    457   1.1       oki #ifdef FDDEBUG
    458   1.1       oki 	{
    459   1.1       oki 		int i;
    460   1.8  christos 		printf("fdprobe: status");
    461   1.1       oki 		for (i = 0; i < n; i++)
    462   1.8  christos 			printf(" %x", fdc->sc_status[i]);
    463   1.8  christos 		printf("\n");
    464   1.1       oki 	}
    465   1.1       oki #endif
    466   1.1       oki 
    467   1.1       oki 	if (n == 2) {
    468   1.1       oki 		if ((fdc->sc_status[0] & 0xf0) == 0x20) {
    469   1.1       oki 			found = 1;
    470   1.1       oki 		} else if ((fdc->sc_status[0] & 0xf0) == 0xc0) {
    471   1.1       oki 			goto retry;
    472   1.1       oki 		}
    473   1.1       oki 	}
    474   1.1       oki 
    475   1.1       oki 	/* turn off motor */
    476   1.1       oki 	infdc.select = (type->rate << 4)| drive;
    477   1.1       oki 	fdc_force_ready(FDCSTBY);
    478   1.1       oki 	if (!found) {
    479   1.1       oki 		ioctlr.intr |= FDCI_EN;
    480   1.1       oki 		return 0;
    481   1.1       oki 	}
    482   1.1       oki 
    483   1.1       oki 	return 1;
    484   1.1       oki }
    485   1.1       oki 
    486   1.1       oki void
    487   1.1       oki fdattach(parent, self, aux)
    488   1.1       oki 	struct device *parent;
    489   1.1       oki 	struct device *self;
    490   1.1       oki 	void *aux;
    491   1.1       oki {
    492   1.1       oki 	struct fdc_softc *fdc = (void *)parent;
    493   1.1       oki 	register struct fd_softc *fd = (void *)self;
    494   1.1       oki 	struct fdc_attach_args *fa = aux;
    495   1.1       oki 	int drive = fa->fa_drive;
    496   1.1       oki 	struct fd_type *type = &fd_types[0];	/* XXX 1.2MB */
    497   1.1       oki 
    498   1.1       oki 	fd->sc_flags = 0;
    499   1.1       oki 
    500   1.1       oki 	ioctlr.intr |= FDCI_EN;
    501   1.1       oki 
    502   1.1       oki 	if (type)
    503   1.8  christos 		printf(": %s %d cyl, %d head, %d sec\n", type->name,
    504   1.1       oki 			type->tracks, type->heads, type->sectrac);
    505   1.1       oki 	else
    506   1.8  christos 		printf(": density unknown\n");
    507   1.1       oki 
    508   1.1       oki 	fd->sc_cylin = -1;
    509   1.1       oki 	fd->sc_drive = drive;
    510   1.1       oki 	fd->sc_deftype = type;
    511   1.1       oki 	fdc->sc_fd[drive] = fd;
    512   1.1       oki 
    513   1.1       oki 	fd->sc_copybuf = (u_char *)malloc(NBPG, M_DEVBUF, M_WAITOK);
    514   1.1       oki 	if (fd->sc_copybuf == 0)
    515   1.8  christos 		printf("fdprobe: WARNING!! malloc() failed.\n");
    516   1.1       oki 	fd->sc_flags |= FD_ALIVE;
    517   1.1       oki 
    518   1.1       oki 	/*
    519   1.1       oki 	 * Initialize and attach the disk structure.
    520   1.1       oki 	 */
    521   1.1       oki 	fd->sc_dk.dk_name = fd->sc_dev.dv_xname;
    522   1.1       oki 	fd->sc_dk.dk_driver = &fddkdriver;
    523   1.1       oki 	disk_attach(&fd->sc_dk);
    524   1.4       oki 
    525   1.4       oki 	/*
    526   1.4       oki 	 * Establish a mountroot_hook anyway in case we booted
    527   1.4       oki 	 * with RB_ASKNAME and get selected as the boot device.
    528   1.4       oki 	 */
    529   1.9   thorpej 	mountroothook_establish(fd_mountroot_hook, &fd->sc_dev);
    530   1.1       oki }
    531   1.1       oki 
    532   1.1       oki inline struct fd_type *
    533   1.1       oki fd_dev_to_type(fd, dev)
    534   1.1       oki 	struct fd_softc *fd;
    535   1.1       oki 	dev_t dev;
    536   1.1       oki {
    537   1.1       oki 	int type = FDTYPE(dev);
    538   1.1       oki 
    539   1.1       oki 	if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
    540   1.1       oki 		return NULL;
    541   1.1       oki 	return &fd_types[type];
    542   1.1       oki }
    543   1.1       oki 
    544   1.1       oki void
    545   1.1       oki fdstrategy(bp)
    546   1.1       oki 	register struct buf *bp;	/* IO operation to perform */
    547   1.1       oki {
    548   1.1       oki 	struct fd_softc *fd;
    549   1.1       oki 	int unit = FDUNIT(bp->b_dev);
    550   1.1       oki 	int sz;
    551   1.1       oki  	int s;
    552   1.1       oki 
    553   1.1       oki 	if (unit >= fd_cd.cd_ndevs ||
    554   1.1       oki 	    (fd = fd_cd.cd_devs[unit]) == 0 ||
    555   1.1       oki 	    bp->b_blkno < 0 ||
    556   1.1       oki 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    557   1.1       oki #ifdef FDDEBUG
    558   1.8  christos 		printf("fdstrategy: unit=%d, blkno=%d, bcount=%d\n", unit,
    559   1.1       oki 		       bp->b_blkno, bp->b_bcount);
    560   1.1       oki #endif
    561   1.1       oki 		bp->b_error = EINVAL;
    562   1.1       oki 		goto bad;
    563   1.1       oki 	}
    564   1.1       oki 
    565   1.1       oki 	/* If it's a null transfer, return immediately. */
    566   1.1       oki 	if (bp->b_bcount == 0)
    567   1.1       oki 		goto done;
    568   1.1       oki 
    569   1.1       oki 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    570   1.1       oki 
    571   1.1       oki 	if (bp->b_blkno + sz > (fd->sc_type->size << (fd->sc_type->secsize - 2))) {
    572   1.1       oki 		sz = (fd->sc_type->size << (fd->sc_type->secsize - 2)) - bp->b_blkno;
    573   1.1       oki 		if (sz == 0) {
    574   1.1       oki 			/* If exactly at end of disk, return EOF. */
    575   1.1       oki 			bp->b_resid = bp->b_bcount;
    576   1.1       oki 			goto done;
    577   1.1       oki 		}
    578   1.1       oki 		if (sz < 0) {
    579   1.1       oki 			/* If past end of disk, return EINVAL. */
    580   1.1       oki 			bp->b_error = EINVAL;
    581   1.1       oki 			goto bad;
    582   1.1       oki 		}
    583   1.1       oki 		/* Otherwise, truncate request. */
    584   1.1       oki 		bp->b_bcount = sz << DEV_BSHIFT;
    585   1.1       oki 	}
    586   1.1       oki 
    587   1.1       oki  	bp->b_cylin = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE)
    588   1.1       oki 		/ (fd->sc_type->seccyl * (1 << (fd->sc_type->secsize - 2)));
    589   1.1       oki 
    590   1.2       oki 	DPRINTF(("fdstrategy: %s b_blkno %d b_bcount %ld cylin %ld\n",
    591   1.1       oki 		 bp->b_flags & B_READ ? "read" : "write",
    592   1.1       oki 		 bp->b_blkno, bp->b_bcount, bp->b_cylin));
    593   1.1       oki 	/* Queue transfer on drive, activate drive and controller if idle. */
    594   1.1       oki 	s = splbio();
    595   1.1       oki 	disksort(&fd->sc_q, bp);
    596   1.1       oki 	untimeout(fd_motor_off, fd); /* a good idea */
    597   1.1       oki 	if (!fd->sc_q.b_active)
    598   1.1       oki 		fdstart(fd);
    599   1.1       oki #ifdef DIAGNOSTIC
    600   1.1       oki 	else {
    601   1.1       oki 		struct fdc_softc *fdc = fdc_cd.cd_devs[0];	/* XXX */
    602   1.1       oki 		if (fdc->sc_state == DEVIDLE) {
    603   1.8  christos 			printf("fdstrategy: controller inactive\n");
    604   1.1       oki 			fdcstart(fdc);
    605   1.1       oki 		}
    606   1.1       oki 	}
    607   1.1       oki #endif
    608   1.1       oki 	splx(s);
    609   1.1       oki 	return;
    610   1.1       oki 
    611   1.1       oki bad:
    612   1.1       oki 	bp->b_flags |= B_ERROR;
    613   1.1       oki done:
    614   1.1       oki 	/* Toss transfer; we're done early. */
    615   1.1       oki 	biodone(bp);
    616   1.1       oki }
    617   1.1       oki 
    618   1.1       oki void
    619   1.1       oki fdstart(fd)
    620   1.1       oki 	struct fd_softc *fd;
    621   1.1       oki {
    622   1.1       oki 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    623   1.1       oki 	int active = fdc->sc_drives.tqh_first != 0;
    624   1.1       oki 
    625   1.1       oki 	/* Link into controller queue. */
    626   1.1       oki 	fd->sc_q.b_active = 1;
    627   1.1       oki 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    628   1.1       oki 
    629   1.1       oki 	/* If controller not already active, start it. */
    630   1.1       oki 	if (!active)
    631   1.1       oki 		fdcstart(fdc);
    632   1.1       oki }
    633   1.1       oki 
    634   1.1       oki void
    635   1.1       oki fdfinish(fd, bp)
    636   1.1       oki 	struct fd_softc *fd;
    637   1.1       oki 	struct buf *bp;
    638   1.1       oki {
    639   1.1       oki 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    640   1.1       oki 
    641   1.1       oki 	/*
    642   1.1       oki 	 * Move this drive to the end of the queue to give others a `fair'
    643   1.1       oki 	 * chance.  We only force a switch if N operations are completed while
    644   1.1       oki 	 * another drive is waiting to be serviced, since there is a long motor
    645   1.1       oki 	 * startup delay whenever we switch.
    646   1.1       oki 	 */
    647   1.1       oki 	if (fd->sc_drivechain.tqe_next && ++fd->sc_ops >= 8) {
    648   1.1       oki 		fd->sc_ops = 0;
    649   1.1       oki 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    650   1.1       oki 		if (bp->b_actf) {
    651   1.1       oki 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    652   1.1       oki 		} else
    653   1.1       oki 			fd->sc_q.b_active = 0;
    654   1.1       oki 	}
    655   1.1       oki 	bp->b_resid = fd->sc_bcount;
    656   1.1       oki 	fd->sc_skip = 0;
    657   1.1       oki 	fd->sc_q.b_actf = bp->b_actf;
    658   1.1       oki 	biodone(bp);
    659   1.1       oki 	/* turn off motor 5s from now */
    660   1.1       oki 	timeout(fd_motor_off, fd, 5 * hz);
    661   1.1       oki 	fdc->sc_state = DEVIDLE;
    662   1.1       oki }
    663   1.1       oki 
    664   1.1       oki int
    665   1.1       oki fdread(dev, uio)
    666   1.1       oki 	dev_t dev;
    667   1.1       oki 	struct uio *uio;
    668   1.1       oki {
    669   1.1       oki 
    670   1.1       oki 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
    671   1.1       oki }
    672   1.1       oki 
    673   1.1       oki int
    674   1.1       oki fdwrite(dev, uio)
    675   1.1       oki 	dev_t dev;
    676   1.1       oki 	struct uio *uio;
    677   1.1       oki {
    678   1.1       oki 
    679   1.1       oki 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
    680   1.1       oki }
    681   1.1       oki 
    682   1.1       oki void
    683   1.1       oki fd_set_motor(fdc, reset)
    684   1.1       oki 	struct fdc_softc *fdc;
    685   1.1       oki 	int reset;
    686   1.1       oki {
    687   1.1       oki 	struct fd_softc *fd;
    688   1.1       oki 	int n;
    689   1.1       oki 
    690   1.1       oki 	DPRINTF(("fd_set_motor:\n"));
    691   1.1       oki 	for (n = 0; n < 4; n++)
    692   1.1       oki 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR)) {
    693   1.1       oki 			infdc.select = 0x80 | (fd->sc_type->rate << 4)| n;
    694   1.1       oki 		}
    695   1.1       oki }
    696   1.1       oki 
    697   1.1       oki void
    698   1.1       oki fd_motor_off(arg)
    699   1.1       oki 	void *arg;
    700   1.1       oki {
    701   1.1       oki 	struct fd_softc *fd = arg;
    702   1.1       oki 	int s;
    703   1.1       oki 
    704   1.1       oki 	DPRINTF(("fd_motor_off:\n"));
    705   1.1       oki 
    706   1.1       oki 	s = splbio();
    707   1.1       oki 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    708   1.1       oki 	infdc.select = (fd->sc_type->rate << 4) | fd->sc_drive;
    709   1.1       oki #if 0
    710   1.1       oki 	fd_set_motor((struct fdc_softc *)&fdc_softc[0], 0); /* XXX */
    711   1.1       oki #endif
    712   1.1       oki 	splx(s);
    713   1.1       oki }
    714   1.1       oki 
    715   1.1       oki void
    716   1.1       oki fd_motor_on(arg)
    717   1.1       oki 	void *arg;
    718   1.1       oki {
    719   1.1       oki 	struct fd_softc *fd = arg;
    720   1.1       oki 	struct fdc_softc *fdc = (void *)fd->sc_dev.dv_parent;
    721   1.1       oki 	int s;
    722   1.1       oki 
    723   1.1       oki 	DPRINTF(("fd_motor_on:\n"));
    724   1.1       oki 
    725   1.1       oki 	s = splbio();
    726   1.1       oki 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    727   1.1       oki 	if ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
    728   1.1       oki 		(void) fdcintr();
    729   1.1       oki 	splx(s);
    730   1.1       oki }
    731   1.1       oki 
    732   1.1       oki int
    733   1.1       oki fdcresult(fdc)
    734   1.1       oki 	struct fdc_softc *fdc;
    735   1.1       oki {
    736   1.1       oki 	u_char i;
    737   1.1       oki 	int j = 100000,
    738   1.1       oki 	    n = 0;
    739   1.1       oki 
    740   1.1       oki 	for (; j; j--) {
    741   1.1       oki 
    742   1.1       oki 		i = infdc.stat & (NE7_DIO | NE7_RQM | NE7_CB);
    743   1.1       oki 
    744   1.1       oki 
    745   1.1       oki 		if (i == NE7_RQM)
    746   1.1       oki 			return n;
    747   1.1       oki 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    748   1.1       oki 			if (n >= sizeof(fdc->sc_status)) {
    749   1.1       oki 				log(LOG_ERR, "fdcresult: overrun\n");
    750   1.1       oki 				return -1;
    751   1.1       oki 			}
    752   1.1       oki 			fdc->sc_status[n++] = infdc.data;
    753   1.1       oki 		}
    754   1.1       oki 	}
    755   1.1       oki 	log(LOG_ERR, "fdcresult: timeout\n");
    756   1.1       oki 	return -1;
    757   1.1       oki }
    758   1.1       oki 
    759   1.1       oki int
    760   1.1       oki out_fdc(x)
    761   1.1       oki 	u_char x;
    762   1.1       oki {
    763   1.1       oki 	int i = 100000;
    764   1.1       oki 
    765   1.1       oki 	while ((infdc.stat & NE7_DIO) && i-- > 0);
    766   1.1       oki 	if (i <= 0)
    767   1.1       oki 		return -1;
    768   1.1       oki 	while ((infdc.stat & NE7_RQM) == 0 && i-- > 0);
    769   1.1       oki 	if (i <= 0)
    770   1.1       oki 		return -1;
    771   1.1       oki 
    772   1.1       oki 	infdc.data = x;
    773   1.1       oki 
    774   1.1       oki 	return 0;
    775   1.1       oki }
    776   1.1       oki 
    777   1.1       oki int
    778   1.1       oki Fdopen(dev, flags, fmt)
    779   1.1       oki 	dev_t dev;
    780   1.1       oki 	int flags, fmt;
    781   1.1       oki {
    782   1.1       oki  	int unit;
    783   1.1       oki 	struct fd_softc *fd;
    784   1.1       oki 	struct fd_type *type;
    785   1.1       oki 
    786   1.1       oki 	unit = FDUNIT(dev);
    787   1.1       oki 	if (unit >= fd_cd.cd_ndevs)
    788   1.1       oki 		return ENXIO;
    789   1.1       oki 	fd = fd_cd.cd_devs[unit];
    790   1.1       oki 	if (fd == 0)
    791   1.1       oki 		return ENXIO;
    792   1.1       oki 	type = fd_dev_to_type(fd, dev);
    793   1.1       oki 	if (type == NULL)
    794   1.1       oki 		return ENXIO;
    795   1.1       oki 
    796   1.1       oki 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    797   1.1       oki 	    fd->sc_type != type)
    798   1.1       oki 		return EBUSY;
    799   1.1       oki 
    800   1.1       oki 	if ((fd->sc_flags & FD_OPEN) == 0) {
    801   1.1       oki 		/* Lock eject button */
    802   1.1       oki 		infdc.drvstat = 0x40 | ( 1 << unit);
    803   1.1       oki 		infdc.drvstat = 0x40;
    804   1.1       oki 	}
    805   1.1       oki 
    806   1.1       oki 	fd->sc_type = type;
    807   1.1       oki 	fd->sc_cylin = -1;
    808   1.1       oki 
    809   1.1       oki 	switch (fmt) {
    810   1.1       oki 	case S_IFCHR:
    811   1.1       oki 		fd->sc_flags |= FD_COPEN;
    812   1.1       oki 		break;
    813   1.1       oki 	case S_IFBLK:
    814   1.1       oki 		fd->sc_flags |= FD_BOPEN;
    815   1.1       oki 		break;
    816   1.1       oki 	}
    817   1.1       oki 
    818   1.1       oki 	fdgetdisklabel(fd, dev);
    819   1.1       oki 
    820   1.1       oki 	return 0;
    821   1.1       oki }
    822   1.1       oki 
    823   1.1       oki int
    824   1.1       oki fdclose(dev, flags, fmt)
    825   1.1       oki 	dev_t dev;
    826   1.1       oki 	int flags, fmt;
    827   1.1       oki {
    828   1.1       oki  	int unit = FDUNIT(dev);
    829   1.1       oki 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
    830   1.1       oki 
    831   1.1       oki 	DPRINTF(("fdclose %d\n", unit));
    832   1.1       oki 
    833   1.1       oki 	switch (fmt) {
    834   1.1       oki 	case S_IFCHR:
    835   1.1       oki 		fd->sc_flags &= ~FD_COPEN;
    836   1.1       oki 		break;
    837   1.1       oki 	case S_IFBLK:
    838   1.1       oki 		fd->sc_flags &= ~FD_BOPEN;
    839   1.1       oki 		break;
    840   1.1       oki 	}
    841   1.1       oki 
    842   1.1       oki 	if ((fd->sc_flags & FD_OPEN) == 0) {
    843   1.1       oki 		infdc.drvstat = ( 1 << unit);
    844   1.1       oki 		infdc.drvstat = 0x00;
    845   1.1       oki 	}
    846   1.1       oki 	return 0;
    847   1.1       oki }
    848   1.1       oki 
    849   1.1       oki void
    850   1.1       oki fdcstart(fdc)
    851   1.1       oki 	struct fdc_softc *fdc;
    852   1.1       oki {
    853   1.1       oki 
    854   1.1       oki #ifdef DIAGNOSTIC
    855   1.1       oki 	/* only got here if controller's drive queue was inactive; should
    856   1.1       oki 	   be in idle state */
    857   1.1       oki 	if (fdc->sc_state != DEVIDLE) {
    858   1.8  christos 		printf("fdcstart: not idle\n");
    859   1.1       oki 		return;
    860   1.1       oki 	}
    861   1.1       oki #endif
    862   1.1       oki 	(void) fdcintr();
    863   1.1       oki }
    864   1.1       oki 
    865   1.1       oki void
    866   1.1       oki fdcstatus(dv, n, s)
    867   1.1       oki 	struct device *dv;
    868   1.1       oki 	int n;
    869   1.1       oki 	char *s;
    870   1.1       oki {
    871   1.1       oki 	struct fdc_softc *fdc = (void *)dv->dv_parent;
    872  1.10       oki 	char bits[64];
    873   1.1       oki 
    874   1.1       oki 	if (n == 0) {
    875   1.1       oki 		out_fdc(NE7CMD_SENSEI);
    876   1.1       oki 		(void) fdcresult(fdc);
    877   1.1       oki 		n = 2;
    878   1.1       oki 	}
    879   1.1       oki 
    880   1.8  christos 	printf("%s: %s: state %d", dv->dv_xname, s, fdc->sc_state);
    881   1.1       oki 
    882   1.1       oki 	switch (n) {
    883   1.1       oki 	case 0:
    884   1.8  christos 		printf("\n");
    885   1.1       oki 		break;
    886   1.1       oki 	case 2:
    887  1.10       oki 		printf(" (st0 %s cyl %d)\n",
    888  1.10       oki 		    bitmask_snprintf(fdc->sc_status[0], NE7_ST0BITS,
    889  1.10       oki 		    bits, sizeof(bits)), fdc->sc_status[1]);
    890   1.1       oki 		break;
    891   1.1       oki 	case 7:
    892  1.10       oki 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
    893  1.10       oki 		    NE7_ST0BITS, bits, sizeof(bits)));
    894  1.10       oki 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
    895  1.10       oki 		    NE7_ST1BITS, bits, sizeof(bits)));
    896  1.10       oki 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
    897  1.10       oki 		    NE7_ST2BITS, bits, sizeof(bits)));
    898  1.10       oki 		printf(" cyl %d head %d sec %d)\n",
    899   1.1       oki 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    900   1.1       oki 		break;
    901   1.1       oki #ifdef DIAGNOSTIC
    902   1.1       oki 	default:
    903   1.8  christos 		printf(" fdcstatus: weird size: %d\n", n);
    904   1.1       oki 		break;
    905   1.1       oki #endif
    906   1.1       oki 	}
    907   1.1       oki }
    908   1.1       oki 
    909   1.1       oki void
    910   1.1       oki fdctimeout(arg)
    911   1.1       oki 	void *arg;
    912   1.1       oki {
    913   1.1       oki 	struct fdc_softc *fdc = arg;
    914   1.1       oki 	struct fd_softc *fd = fdc->sc_drives.tqh_first;
    915   1.1       oki 	int s;
    916   1.1       oki 
    917   1.1       oki 	s = splbio();
    918   1.1       oki 	fdcstatus(&fd->sc_dev, 0, "timeout");
    919   1.1       oki 
    920   1.1       oki 	if (fd->sc_q.b_actf)
    921   1.1       oki 		fdc->sc_state++;
    922   1.1       oki 	else
    923   1.1       oki 		fdc->sc_state = DEVIDLE;
    924   1.1       oki 
    925   1.1       oki 	(void) fdcintr();
    926   1.1       oki 	splx(s);
    927   1.1       oki }
    928   1.1       oki 
    929   1.1       oki void
    930   1.1       oki fdcpseudointr(arg)
    931   1.1       oki 	void *arg;
    932   1.1       oki {
    933   1.1       oki 	int s;
    934   1.1       oki 
    935   1.1       oki 	/* just ensure it has the right spl */
    936   1.1       oki 	s = splbio();
    937   1.1       oki 	(void) fdcintr();
    938   1.1       oki 	splx(s);
    939   1.1       oki }
    940   1.1       oki 
    941   1.1       oki int
    942   1.1       oki fdcintr()
    943   1.1       oki {
    944   1.1       oki 	struct fdc_softc *fdc = fdc_cd.cd_devs[0];	/* XXX */
    945   1.1       oki #define	st0	fdc->sc_status[0]
    946   1.1       oki #define	cyl	fdc->sc_status[1]
    947   1.1       oki 	struct fd_softc *fd;
    948   1.1       oki 	struct buf *bp;
    949   1.1       oki 	int read, head, sec, pos, i, sectrac, nblks;
    950   1.1       oki 	int	tmp;
    951   1.1       oki 	struct fd_type *type;
    952   1.1       oki 
    953   1.1       oki loop:
    954   1.1       oki 	fd = fdc->sc_drives.tqh_first;
    955   1.1       oki 	if (fd == NULL) {
    956   1.1       oki 		DPRINTF(("fdcintr: set DEVIDLE\n"));
    957   1.1       oki 		if (fdc->sc_state == DEVIDLE) {
    958   1.1       oki 			if ((ioctlr.intr & 0x80)) {
    959   1.1       oki 				out_fdc(NE7CMD_SENSEI);
    960   1.1       oki 				if ((tmp = fdcresult(fdc)) != 2 || (st0 & 0xf8) != 0x20) {
    961   1.1       oki 					goto loop;
    962   1.1       oki 				}
    963   1.1       oki 			}
    964   1.1       oki 		}
    965   1.1       oki 		/* no drives waiting; end */
    966   1.1       oki 		fdc->sc_state = DEVIDLE;
    967   1.1       oki  		return 1;
    968   1.1       oki 	}
    969   1.1       oki 
    970   1.1       oki 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    971   1.1       oki 	bp = fd->sc_q.b_actf;
    972   1.1       oki 	if (bp == NULL) {
    973   1.1       oki 		fd->sc_ops = 0;
    974   1.1       oki 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    975   1.1       oki 		fd->sc_q.b_active = 0;
    976   1.1       oki 		goto loop;
    977   1.1       oki 	}
    978   1.1       oki 
    979   1.1       oki 	switch (fdc->sc_state) {
    980   1.1       oki 	case DEVIDLE:
    981   1.1       oki 		DPRINTF(("fdcintr: in DEVIDLE\n"));
    982   1.1       oki 		fdc->sc_errors = 0;
    983   1.1       oki 		fd->sc_skip = 0;
    984   1.1       oki 		fd->sc_bcount = bp->b_bcount;
    985   1.1       oki 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
    986   1.1       oki 		untimeout(fd_motor_off, fd);
    987   1.1       oki 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
    988   1.1       oki 			fdc->sc_state = MOTORWAIT;
    989   1.1       oki 			return 1;
    990   1.1       oki 		}
    991   1.1       oki 		if ((fd->sc_flags & FD_MOTOR) == 0) {
    992   1.5       oki 			/* Turn on the motor */
    993   1.5       oki 			/* being careful about other drives. */
    994   1.5       oki 			for (i = 0; i < 4; i++) {
    995   1.5       oki 				struct fd_softc *ofd = fdc->sc_fd[i];
    996   1.5       oki 				if (ofd && ofd->sc_flags & FD_MOTOR) {
    997   1.5       oki 					untimeout(fd_motor_off, ofd);
    998   1.5       oki 					ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    999   1.5       oki 					break;
   1000   1.5       oki 				}
   1001   1.1       oki 			}
   1002   1.1       oki 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
   1003   1.1       oki 			fd_set_motor(fdc, 0);
   1004   1.1       oki 			fdc->sc_state = MOTORWAIT;
   1005   1.1       oki 			/* allow .5s for motor to stabilize */
   1006   1.1       oki 			timeout(fd_motor_on, fd, hz / 2);
   1007   1.1       oki 			return 1;
   1008   1.1       oki 		}
   1009   1.1       oki 		/* Make sure the right drive is selected. */
   1010   1.1       oki 		fd_set_motor(fdc, 0);
   1011   1.1       oki 
   1012   1.1       oki 		/* fall through */
   1013   1.1       oki 	case DOSEEK:
   1014   1.1       oki 	doseek:
   1015   1.1       oki 		DPRINTF(("fdcintr: in DOSEEK\n"));
   1016   1.1       oki 		if (fd->sc_cylin == bp->b_cylin)
   1017   1.1       oki 			goto doio;
   1018   1.1       oki 
   1019   1.1       oki 		out_fdc(NE7CMD_SPECIFY);/* specify command */
   1020   1.1       oki 		out_fdc(0xd0);		/* XXX const */
   1021   1.1       oki 		out_fdc(0x10);
   1022   1.1       oki 
   1023   1.1       oki 		out_fdc(NE7CMD_SEEK);	/* seek function */
   1024   1.1       oki 		out_fdc(fd->sc_drive);	/* drive number */
   1025   1.1       oki 		out_fdc(bp->b_cylin * fd->sc_type->step);
   1026   1.1       oki 
   1027   1.1       oki 		fd->sc_cylin = -1;
   1028   1.1       oki 		fdc->sc_state = SEEKWAIT;
   1029   1.1       oki 
   1030   1.1       oki 		fd->sc_dk.dk_seek++;
   1031   1.1       oki 		disk_busy(&fd->sc_dk);
   1032   1.1       oki 
   1033   1.1       oki 		timeout(fdctimeout, fdc, 4 * hz);
   1034   1.1       oki 		return 1;
   1035   1.1       oki 
   1036   1.1       oki 	case DOIO:
   1037   1.1       oki 	doio:
   1038   1.1       oki 		DPRINTF(("fdcintr: DOIO: "));
   1039   1.1       oki 		type = fd->sc_type;
   1040   1.1       oki 		sectrac = type->sectrac;
   1041   1.1       oki 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
   1042   1.1       oki 		sec = pos / (1 << (type->secsize - 2));
   1043   1.1       oki 		if (type->secsize == 2) {
   1044   1.1       oki 			fd->sc_part = SEC_P11;
   1045   1.1       oki 			nblks = (sectrac - sec) << (type->secsize - 2);
   1046   1.1       oki 			nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1047   1.1       oki 			DPRINTF(("nblks(0)"));
   1048   1.1       oki 		} else if ((fd->sc_blkno % 2) == 0) {
   1049   1.1       oki 			if (fd->sc_bcount & 0x00000200) {
   1050   1.1       oki 				if (fd->sc_bcount == FDC_BSIZE) {
   1051   1.1       oki 					fd->sc_part = SEC_P10;
   1052   1.1       oki 					nblks = 1;
   1053   1.1       oki 					DPRINTF(("nblks(1)"));
   1054   1.1       oki 				} else {
   1055   1.1       oki 					fd->sc_part = SEC_P11;
   1056   1.1       oki 					nblks = (sectrac - sec) * 2;
   1057   1.1       oki 					nblks = min(nblks, fd->sc_bcount
   1058   1.1       oki 						    / FDC_BSIZE - 1);
   1059   1.1       oki 					DPRINTF(("nblks(2)"));
   1060   1.1       oki 				}
   1061   1.1       oki 			} else {
   1062   1.1       oki 				fd->sc_part = SEC_P11;
   1063   1.1       oki 				nblks = (sectrac - sec)
   1064   1.1       oki 					<< (type->secsize - 2);
   1065   1.1       oki 				nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
   1066   1.1       oki 				DPRINTF(("nblks(3)"));
   1067   1.1       oki 			}
   1068   1.1       oki 		} else {
   1069   1.1       oki 			fd->sc_part = SEC_P01;
   1070   1.1       oki 			nblks = 1;
   1071   1.1       oki 			DPRINTF(("nblks(4)"));
   1072   1.1       oki 		}
   1073   1.1       oki 		nblks = min(nblks, FDC_MAXIOSIZE / FDC_BSIZE);
   1074   1.1       oki 		DPRINTF((" %d\n", nblks));
   1075   1.1       oki 		fd->sc_nblks = nblks;
   1076   1.1       oki 		fd->sc_nbytes = nblks * FDC_BSIZE;
   1077   1.1       oki 		head = (fd->sc_blkno
   1078   1.1       oki 			% (type->seccyl * (1 << (type->secsize - 2))))
   1079   1.1       oki 			 / (type->sectrac * (1 << (type->secsize - 2)));
   1080   1.1       oki 
   1081   1.1       oki #ifdef DIAGNOSTIC
   1082   1.1       oki 		{int block;
   1083   1.1       oki 		 block = ((fd->sc_cylin * type->heads + head) * type->sectrac
   1084   1.1       oki 			  + sec) * (1 << (type->secsize - 2));
   1085   1.1       oki 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
   1086   1.1       oki 		 if (block != fd->sc_blkno) {
   1087   1.8  christos 			 printf("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec, type->secsize);
   1088   1.8  christos 			 printf("fdcintr: doio: block %d != blkno %d\n", block, fd->sc_blkno);
   1089   1.1       oki #ifdef DDB
   1090   1.1       oki 			 Debugger();
   1091   1.1       oki #endif
   1092   1.1       oki 		 }}
   1093   1.1       oki #endif
   1094   1.1       oki 		read = bp->b_flags & B_READ;
   1095   1.1       oki 		DPRINTF(("fdcintr: %s drive %d track %d head %d sec %d nblks %d, skip %d\n",
   1096   1.1       oki 			 read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
   1097   1.1       oki 			 head, sec, nblks, fd->sc_skip));
   1098   1.1       oki 		DPRINTF(("C H R N: %d %d %d %d\n", fd->sc_cylin, head, sec,
   1099   1.1       oki 			 type->secsize));
   1100   1.1       oki 
   1101   1.1       oki 		if (fd->sc_part != SEC_P11)
   1102   1.1       oki 			goto docopy;
   1103   1.1       oki 
   1104   1.1       oki 		fdc_dmastart(read, bp->b_data + fd->sc_skip, fd->sc_nbytes);
   1105   1.1       oki 		if (read)
   1106   1.1       oki 			out_fdc(NE7CMD_READ);	/* READ */
   1107   1.1       oki 		else
   1108   1.1       oki 			out_fdc(NE7CMD_WRITE);	/* WRITE */
   1109   1.1       oki 		out_fdc((head << 2) | fd->sc_drive);
   1110   1.1       oki 		out_fdc(bp->b_cylin);		/* cylinder */
   1111   1.1       oki 		out_fdc(head);
   1112   1.1       oki 		out_fdc(sec + 1);		/* sector +1 */
   1113   1.1       oki 		out_fdc(type->secsize);		/* sector size */
   1114   1.1       oki 		out_fdc(type->sectrac);		/* sectors/track */
   1115   1.1       oki 		out_fdc(type->gap1);		/* gap1 size */
   1116   1.1       oki 		out_fdc(type->datalen);		/* data length */
   1117   1.1       oki 		fdc->sc_state = IOCOMPLETE;
   1118   1.1       oki 
   1119   1.1       oki 		disk_busy(&fd->sc_dk);
   1120   1.1       oki 
   1121   1.1       oki 		/* allow 2 seconds for operation */
   1122   1.1       oki 		timeout(fdctimeout, fdc, 2 * hz);
   1123   1.1       oki 		return 1;				/* will return later */
   1124   1.1       oki 
   1125   1.1       oki 	case DOCOPY:
   1126   1.1       oki 	docopy:
   1127   1.1       oki 		DPRINTF(("fdcintr: DOCOPY:\n"));
   1128   1.1       oki 		fdc_dmastart(B_READ, fd->sc_copybuf, 1024);
   1129   1.1       oki 		out_fdc(NE7CMD_READ);	/* READ */
   1130   1.1       oki 		out_fdc((head << 2) | fd->sc_drive);
   1131   1.1       oki 		out_fdc(bp->b_cylin);		/* cylinder */
   1132   1.1       oki 		out_fdc(head);
   1133   1.1       oki 		out_fdc(sec + 1);		/* sector +1 */
   1134   1.1       oki 		out_fdc(type->secsize);		/* sector size */
   1135   1.1       oki 		out_fdc(type->sectrac);		/* sectors/track */
   1136   1.1       oki 		out_fdc(type->gap1);		/* gap1 size */
   1137   1.1       oki 		out_fdc(type->datalen);		/* data length */
   1138   1.1       oki 		fdc->sc_state = COPYCOMPLETE;
   1139   1.1       oki 		/* allow 2 seconds for operation */
   1140   1.1       oki 		timeout(fdctimeout, fdc, 2 * hz);
   1141   1.1       oki 		return 1;				/* will return later */
   1142   1.1       oki 
   1143   1.1       oki 	case DOIOHALF:
   1144   1.1       oki 	doiohalf:
   1145   1.1       oki 		DPRINTF((" DOIOHALF:\n"));
   1146   1.1       oki 
   1147   1.1       oki #ifdef DIAGNOSTIC
   1148   1.1       oki 		type = fd->sc_type;
   1149   1.1       oki 		sectrac = type->sectrac;
   1150   1.1       oki 		pos = fd->sc_blkno % (sectrac * (1 << (type->secsize - 2)));
   1151   1.1       oki 		sec = pos / (1 << (type->secsize - 2));
   1152   1.1       oki 		head = (fd->sc_blkno
   1153   1.1       oki 			% (type->seccyl * (1 << (type->secsize - 2))))
   1154   1.1       oki 			 / (type->sectrac * (1 << (type->secsize - 2)));
   1155   1.1       oki 		{int block;
   1156   1.1       oki 		 block = ((fd->sc_cylin * type->heads + head) * type->sectrac + sec)
   1157   1.1       oki 			 * (1 << (type->secsize - 2));
   1158   1.1       oki 		 block += (fd->sc_part == SEC_P01) ? 1 : 0;
   1159   1.1       oki 		 if (block != fd->sc_blkno) {
   1160   1.8  christos 			 printf("fdcintr: block %d != blkno %d\n", block, fd->sc_blkno);
   1161   1.1       oki #ifdef DDB
   1162   1.1       oki 			 Debugger();
   1163   1.1       oki #endif
   1164   1.1       oki 		 }}
   1165   1.1       oki #endif
   1166   1.1       oki 		if (read = bp->b_flags & B_READ) {
   1167   1.1       oki 			bcopy(fd->sc_copybuf
   1168   1.1       oki 			      + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
   1169   1.1       oki 			      bp->b_data + fd->sc_skip,
   1170   1.1       oki 			      FDC_BSIZE);
   1171   1.1       oki 			fdc->sc_state = IOCOMPLETE;
   1172   1.1       oki 			goto iocomplete2;
   1173   1.1       oki 		} else {
   1174   1.1       oki 			bcopy(bp->b_data + fd->sc_skip,
   1175   1.1       oki 			      fd->sc_copybuf
   1176   1.1       oki 			      + (fd->sc_part & SEC_P01 ? FDC_BSIZE : 0),
   1177   1.1       oki 			      FDC_BSIZE);
   1178   1.1       oki 			fdc_dmastart(read, fd->sc_copybuf, 1024);
   1179   1.1       oki 		}
   1180   1.1       oki 		out_fdc(NE7CMD_WRITE);	/* WRITE */
   1181   1.1       oki 		out_fdc((head << 2) | fd->sc_drive);
   1182   1.1       oki 		out_fdc(bp->b_cylin);		/* cylinder */
   1183   1.1       oki 		out_fdc(head);
   1184   1.1       oki 		out_fdc(sec + 1);		/* sector +1 */
   1185   1.1       oki 		out_fdc(fd->sc_type->secsize);		/* sector size */
   1186   1.1       oki 		out_fdc(sectrac);		/* sectors/track */
   1187   1.1       oki 		out_fdc(fd->sc_type->gap1);		/* gap1 size */
   1188   1.1       oki 		out_fdc(fd->sc_type->datalen);		/* data length */
   1189   1.1       oki 		fdc->sc_state = IOCOMPLETE;
   1190   1.1       oki 		/* allow 2 seconds for operation */
   1191   1.1       oki 		timeout(fdctimeout, fdc, 2 * hz);
   1192   1.1       oki 		return 1;				/* will return later */
   1193   1.1       oki 
   1194   1.1       oki 	case SEEKWAIT:
   1195   1.1       oki 		untimeout(fdctimeout, fdc);
   1196   1.1       oki 		fdc->sc_state = SEEKCOMPLETE;
   1197   1.1       oki 		/* allow 1/50 second for heads to settle */
   1198   1.1       oki /*		timeout(fdcpseudointr, fdc, hz / 50);*/
   1199   1.1       oki 		return 1;
   1200   1.1       oki 
   1201   1.1       oki 	case SEEKCOMPLETE:
   1202   1.1       oki 		/* Make sure seek really happened */
   1203   1.1       oki 		DPRINTF(("fdcintr: SEEKCOMPLETE: FDC status = %x\n",
   1204   1.1       oki 			 infdc.stat));
   1205   1.1       oki 		out_fdc(NE7CMD_SENSEI);
   1206   1.1       oki 		tmp = fdcresult(fdc);
   1207   1.1       oki 		if ((st0 & 0xf8) == 0xc0) {
   1208   1.1       oki 			DPRINTF(("fdcintr: first seek!\n"));
   1209   1.1       oki 			fdc->sc_state = DORECAL;
   1210   1.1       oki 			goto loop;
   1211   1.1       oki 		} else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != bp->b_cylin) {
   1212   1.1       oki #ifdef FDDEBUG
   1213   1.1       oki 			fdcstatus(&fd->sc_dev, 2, "seek failed");
   1214   1.1       oki #endif
   1215   1.1       oki 			fdcretry(fdc);
   1216   1.1       oki 			goto loop;
   1217   1.1       oki 		}
   1218   1.1       oki 		fd->sc_cylin = bp->b_cylin;
   1219   1.1       oki 		goto doio;
   1220   1.1       oki 
   1221   1.1       oki 	case IOTIMEDOUT:
   1222   1.1       oki #if 0
   1223   1.1       oki 		isa_dmaabort(fdc->sc_drq);
   1224   1.1       oki #endif
   1225   1.1       oki 	case SEEKTIMEDOUT:
   1226   1.1       oki 	case RECALTIMEDOUT:
   1227   1.1       oki 	case RESETTIMEDOUT:
   1228   1.1       oki 		fdcretry(fdc);
   1229   1.1       oki 		goto loop;
   1230   1.1       oki 
   1231   1.1       oki 	case IOCOMPLETE: /* IO DONE, post-analyze */
   1232   1.1       oki 		untimeout(fdctimeout, fdc);
   1233   1.1       oki 		DPRINTF(("fdcintr: in IOCOMPLETE\n"));
   1234   1.1       oki 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
   1235   1.8  christos 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
   1236   1.1       oki #if 0
   1237   1.1       oki 			isa_dmaabort(fdc->sc_drq);
   1238   1.1       oki #endif
   1239   1.1       oki 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1240   1.1       oki 				  "read failed" : "write failed");
   1241   1.8  christos 			printf("blkno %d nblks %d\n",
   1242   1.1       oki 			    fd->sc_blkno, fd->sc_nblks);
   1243   1.1       oki 			fdcretry(fdc);
   1244   1.1       oki 			goto loop;
   1245   1.1       oki 		}
   1246   1.1       oki #if 0
   1247   1.1       oki 		isa_dmadone(bp->b_flags & B_READ, bp->b_data + fd->sc_skip,
   1248   1.1       oki 		    nblks * FDC_BSIZE, fdc->sc_drq);
   1249   1.1       oki #endif
   1250   1.1       oki 	iocomplete2:
   1251   1.1       oki 		if (fdc->sc_errors) {
   1252   1.1       oki 			diskerr(bp, "fd", "soft error", LOG_PRINTF,
   1253   1.1       oki 			    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1254   1.8  christos 			printf("\n");
   1255   1.1       oki 			fdc->sc_errors = 0;
   1256   1.1       oki 		}
   1257   1.1       oki 		fd->sc_blkno += fd->sc_nblks;
   1258   1.1       oki 		fd->sc_skip += fd->sc_nbytes;
   1259   1.1       oki 		fd->sc_bcount -= fd->sc_nbytes;
   1260   1.1       oki 		DPRINTF(("fd->sc_bcount = %d\n", fd->sc_bcount));
   1261   1.1       oki 		if (fd->sc_bcount > 0) {
   1262   1.1       oki 			bp->b_cylin = fd->sc_blkno
   1263   1.1       oki 				/ (fd->sc_type->seccyl
   1264   1.1       oki 				   * (1 << (fd->sc_type->secsize - 2)));
   1265   1.1       oki 			goto doseek;
   1266   1.1       oki 		}
   1267   1.1       oki 		fdfinish(fd, bp);
   1268   1.1       oki 		goto loop;
   1269   1.1       oki 
   1270   1.1       oki 	case COPYCOMPLETE: /* IO DONE, post-analyze */
   1271   1.1       oki 		DPRINTF(("fdcintr: COPYCOMPLETE:"));
   1272   1.1       oki 		untimeout(fdctimeout, fdc);
   1273   1.1       oki 		if ((tmp = fdcresult(fdc)) != 7 || (st0 & 0xf8) != 0) {
   1274   1.8  christos 			printf("fdcintr: resnum=%d, st0=%x\n", tmp, st0);
   1275   1.1       oki #if 0
   1276   1.1       oki 			isa_dmaabort(fdc->sc_drq);
   1277   1.1       oki #endif
   1278   1.1       oki 			fdcstatus(&fd->sc_dev, 7, bp->b_flags & B_READ ?
   1279   1.1       oki 				  "read failed" : "write failed");
   1280   1.8  christos 			printf("blkno %d nblks %d\n",
   1281   1.1       oki 			    fd->sc_blkno, fd->sc_nblks);
   1282   1.1       oki 			fdcretry(fdc);
   1283   1.1       oki 			goto loop;
   1284   1.1       oki 		}
   1285   1.1       oki 		goto doiohalf;
   1286   1.1       oki 
   1287   1.1       oki 	case DORESET:
   1288   1.1       oki 		DPRINTF(("fdcintr: in DORESET\n"));
   1289   1.1       oki 		/* try a reset, keep motor on */
   1290   1.1       oki 		fd_set_motor(fdc, 1);
   1291   1.1       oki 		DELAY(100);
   1292   1.1       oki 		fd_set_motor(fdc, 0);
   1293   1.1       oki 		fdc->sc_state = RESETCOMPLETE;
   1294   1.1       oki 		timeout(fdctimeout, fdc, hz / 2);
   1295   1.1       oki 		return 1;			/* will return later */
   1296   1.1       oki 
   1297   1.1       oki 	case RESETCOMPLETE:
   1298   1.1       oki 		DPRINTF(("fdcintr: in RESETCOMPLETE\n"));
   1299   1.1       oki 		untimeout(fdctimeout, fdc);
   1300   1.1       oki 		/* clear the controller output buffer */
   1301   1.1       oki 		for (i = 0; i < 4; i++) {
   1302   1.1       oki 			out_fdc(NE7CMD_SENSEI);
   1303   1.1       oki 			(void) fdcresult(fdc);
   1304   1.1       oki 		}
   1305   1.1       oki 
   1306   1.1       oki 		/* fall through */
   1307   1.1       oki 	case DORECAL:
   1308   1.1       oki 		DPRINTF(("fdcintr: in DORECAL\n"));
   1309   1.1       oki 		out_fdc(NE7CMD_RECAL);	/* recalibrate function */
   1310   1.1       oki 		out_fdc(fd->sc_drive);
   1311   1.1       oki 		fdc->sc_state = RECALWAIT;
   1312   1.1       oki 		timeout(fdctimeout, fdc, 5 * hz);
   1313   1.1       oki 		return 1;			/* will return later */
   1314   1.1       oki 
   1315   1.1       oki 	case RECALWAIT:
   1316   1.1       oki 		DPRINTF(("fdcintr: in RECALWAIT\n"));
   1317   1.1       oki 		untimeout(fdctimeout, fdc);
   1318   1.1       oki 		fdc->sc_state = RECALCOMPLETE;
   1319   1.1       oki 		/* allow 1/30 second for heads to settle */
   1320   1.1       oki /*		timeout(fdcpseudointr, fdc, hz / 30);*/
   1321   1.1       oki 		return 1;			/* will return later */
   1322   1.1       oki 
   1323   1.1       oki 	case RECALCOMPLETE:
   1324   1.1       oki 		DPRINTF(("fdcintr: in RECALCOMPLETE\n"));
   1325   1.1       oki 		out_fdc(NE7CMD_SENSEI);
   1326   1.1       oki 		tmp = fdcresult(fdc);
   1327   1.1       oki 		if ((st0 & 0xf8) == 0xc0) {
   1328   1.1       oki 			DPRINTF(("fdcintr: first seek!\n"));
   1329   1.1       oki 			fdc->sc_state = DORECAL;
   1330   1.1       oki 			goto loop;
   1331   1.1       oki 		} else if (tmp != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1332   1.1       oki #ifdef FDDEBUG
   1333   1.1       oki 			fdcstatus(&fd->sc_dev, 2, "recalibrate failed");
   1334   1.1       oki #endif
   1335   1.1       oki 			fdcretry(fdc);
   1336   1.1       oki 			goto loop;
   1337   1.1       oki 		}
   1338   1.1       oki 		fd->sc_cylin = 0;
   1339   1.1       oki 		goto doseek;
   1340   1.1       oki 
   1341   1.1       oki 	case MOTORWAIT:
   1342   1.1       oki 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1343   1.1       oki 			return 1;		/* time's not up yet */
   1344   1.1       oki 		goto doseek;
   1345   1.1       oki 
   1346   1.1       oki 	default:
   1347   1.1       oki 		fdcstatus(&fd->sc_dev, 0, "stray interrupt");
   1348   1.1       oki 		return 1;
   1349   1.1       oki 	}
   1350   1.1       oki #ifdef DIAGNOSTIC
   1351   1.1       oki 	panic("fdcintr: impossible");
   1352   1.1       oki #endif
   1353   1.1       oki #undef	st0
   1354   1.1       oki #undef	cyl
   1355   1.1       oki }
   1356   1.1       oki 
   1357   1.1       oki void
   1358   1.1       oki fdcretry(fdc)
   1359   1.1       oki 	struct fdc_softc *fdc;
   1360   1.1       oki {
   1361   1.1       oki 	struct fd_softc *fd;
   1362   1.1       oki 	struct buf *bp;
   1363  1.11       oki 	char bits[64];
   1364   1.1       oki 
   1365   1.1       oki 	DPRINTF(("fdcretry:\n"));
   1366   1.1       oki 	fd = fdc->sc_drives.tqh_first;
   1367   1.1       oki 	bp = fd->sc_q.b_actf;
   1368   1.1       oki 
   1369   1.1       oki 	switch (fdc->sc_errors) {
   1370   1.1       oki 	case 0:
   1371   1.1       oki 		/* try again */
   1372   1.1       oki 		fdc->sc_state = SEEKCOMPLETE;
   1373   1.1       oki 		break;
   1374   1.1       oki 
   1375   1.1       oki 	case 1: case 2: case 3:
   1376   1.1       oki 		/* didn't work; try recalibrating */
   1377   1.1       oki 		fdc->sc_state = DORECAL;
   1378   1.1       oki 		break;
   1379   1.1       oki 
   1380   1.1       oki 	case 4:
   1381   1.1       oki 		/* still no go; reset the bastard */
   1382   1.1       oki 		fdc->sc_state = DORESET;
   1383   1.1       oki 		break;
   1384   1.1       oki 
   1385   1.1       oki 	default:
   1386   1.1       oki 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1387   1.1       oki 			fd->sc_skip, (struct disklabel *)NULL);
   1388  1.10       oki 		printf(" (st0 %s", bitmask_snprintf(fdc->sc_status[0],
   1389  1.10       oki 						    NE7_ST0BITS, bits,
   1390  1.10       oki 						    sizeof(bits)));
   1391  1.10       oki 		printf(" st1 %s", bitmask_snprintf(fdc->sc_status[1],
   1392  1.10       oki 						   NE7_ST1BITS, bits,
   1393  1.10       oki 						   sizeof(bits)));
   1394  1.10       oki 		printf(" st2 %s", bitmask_snprintf(fdc->sc_status[2],
   1395  1.10       oki 						   NE7_ST2BITS, bits,
   1396  1.10       oki 						   sizeof(bits)));
   1397  1.10       oki 		printf(" cyl %d head %d sec %d)\n",
   1398  1.10       oki 		       fdc->sc_status[3],
   1399  1.10       oki 		       fdc->sc_status[4],
   1400  1.10       oki 		       fdc->sc_status[5]);
   1401   1.1       oki 
   1402   1.1       oki 		bp->b_flags |= B_ERROR;
   1403   1.1       oki 		bp->b_error = EIO;
   1404   1.1       oki 		fdfinish(fd, bp);
   1405   1.1       oki 	}
   1406   1.1       oki 	fdc->sc_errors++;
   1407   1.1       oki }
   1408   1.1       oki 
   1409   1.1       oki int
   1410   1.1       oki fdsize(dev)
   1411   1.1       oki 	dev_t dev;
   1412   1.1       oki {
   1413   1.1       oki 
   1414   1.1       oki 	/* Swapping to floppies would not make sense. */
   1415   1.1       oki 	return -1;
   1416   1.1       oki }
   1417   1.1       oki 
   1418   1.1       oki int
   1419   1.1       oki fddump(dev, blkno, va, size)
   1420   1.1       oki 	dev_t dev;
   1421   1.1       oki 	daddr_t blkno;
   1422   1.1       oki 	caddr_t va;
   1423   1.1       oki 	size_t size;
   1424   1.1       oki {
   1425   1.1       oki 
   1426   1.1       oki 	/* Not implemented. */
   1427   1.1       oki 	return ENXIO;
   1428   1.1       oki }
   1429   1.1       oki 
   1430   1.1       oki int
   1431   1.1       oki fdioctl(dev, cmd, addr, flag)
   1432   1.1       oki 	dev_t dev;
   1433   1.1       oki 	u_long cmd;
   1434   1.1       oki 	caddr_t addr;
   1435   1.1       oki 	int flag;
   1436   1.1       oki {
   1437   1.1       oki 	struct fd_softc *fd = fd_cd.cd_devs[FDUNIT(dev)];
   1438   1.1       oki 	int unit = FDUNIT(dev);
   1439   1.1       oki 	struct disklabel buffer;
   1440   1.1       oki 	int error;
   1441   1.1       oki 
   1442   1.1       oki 	DPRINTF(("fdioctl:\n"));
   1443   1.1       oki 	switch (cmd) {
   1444   1.1       oki 	case DIOCGDINFO:
   1445   1.1       oki #if 1
   1446   1.1       oki 		*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
   1447   1.1       oki 		return(0);
   1448   1.1       oki #else
   1449   1.1       oki 		bzero(&buffer, sizeof(buffer));
   1450   1.1       oki 
   1451   1.1       oki 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1452   1.1       oki 		buffer.d_type = DTYPE_FLOPPY;
   1453   1.1       oki 		buffer.d_secsize = 128 << fd->sc_type->secsize;
   1454   1.1       oki 
   1455   1.1       oki 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1456   1.1       oki 			return EINVAL;
   1457   1.1       oki 
   1458   1.1       oki 		*(struct disklabel *)addr = buffer;
   1459   1.1       oki 		return 0;
   1460   1.1       oki #endif
   1461   1.1       oki 
   1462   1.1       oki 	case DIOCGPART:
   1463   1.1       oki 		((struct partinfo *)addr)->disklab = fd->sc_dk.dk_label;
   1464   1.1       oki 		((struct partinfo *)addr)->part =
   1465   1.1       oki 		    &fd->sc_dk.dk_label->d_partitions[DISKPART(dev)];
   1466   1.1       oki 		return(0);
   1467   1.1       oki 
   1468   1.1       oki 	case DIOCWLABEL:
   1469   1.1       oki 		if ((flag & FWRITE) == 0)
   1470   1.1       oki 			return EBADF;
   1471   1.1       oki 		/* XXX do something */
   1472   1.1       oki 		return 0;
   1473   1.1       oki 
   1474   1.1       oki 	case DIOCWDINFO:
   1475   1.1       oki 		if ((flag & FWRITE) == 0)
   1476   1.1       oki 			return EBADF;
   1477   1.1       oki 
   1478   1.1       oki 		error = setdisklabel(&buffer, (struct disklabel *)addr, 0, NULL);
   1479   1.1       oki 		if (error)
   1480   1.1       oki 			return error;
   1481   1.1       oki 
   1482   1.1       oki 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1483   1.1       oki 		return error;
   1484   1.1       oki 
   1485   1.1       oki 	case DIOCLOCK:
   1486   1.1       oki 		/*
   1487   1.1       oki 		 * Nothing to do here, really.
   1488   1.1       oki 		 */
   1489   1.1       oki 		return 0; /* XXX */
   1490   1.1       oki 
   1491   1.1       oki 	case DIOCEJECT:
   1492   1.1       oki 		fd_do_eject(unit);
   1493   1.1       oki 		return 0;
   1494   1.1       oki 
   1495   1.1       oki 	default:
   1496   1.1       oki 		return ENOTTY;
   1497   1.1       oki 	}
   1498   1.1       oki 
   1499   1.1       oki #ifdef DIAGNOSTIC
   1500   1.1       oki 	panic("fdioctl: impossible");
   1501   1.1       oki #endif
   1502   1.1       oki }
   1503   1.1       oki 
   1504   1.1       oki void
   1505   1.1       oki fd_do_eject(unit)
   1506   1.1       oki 	int unit;
   1507   1.1       oki {
   1508   1.1       oki 	infdc.drvstat = 0x20 | ( 1 << unit);
   1509   1.1       oki 	DELAY(1); /* XXX */
   1510   1.1       oki 	infdc.drvstat = 0x20;
   1511   1.1       oki }
   1512   1.1       oki 
   1513   1.1       oki /*
   1514   1.1       oki  * Build disk label. For now we only create a label from what we know
   1515   1.1       oki  * from 'sc'.
   1516   1.1       oki  */
   1517   1.1       oki static int
   1518   1.1       oki fdgetdisklabel(sc, dev)
   1519   1.1       oki 	struct fd_softc *sc;
   1520   1.1       oki 	dev_t dev;
   1521   1.1       oki {
   1522   1.1       oki 	struct disklabel *lp;
   1523   1.1       oki 	int part;
   1524   1.1       oki 
   1525   1.1       oki #ifdef FDDEBUG
   1526   1.8  christos 	printf("fdgetdisklabel()\n");
   1527   1.1       oki #endif
   1528   1.1       oki 
   1529   1.1       oki 	part = DISKPART(dev);
   1530   1.1       oki 	lp = sc->sc_dk.dk_label;
   1531   1.1       oki 	bzero(lp, sizeof(struct disklabel));
   1532   1.1       oki 
   1533   1.1       oki 	lp->d_secsize     = 128 << sc->sc_type->secsize;
   1534   1.1       oki 	lp->d_ntracks     = sc->sc_type->heads;
   1535   1.1       oki 	lp->d_nsectors    = sc->sc_type->sectrac;
   1536   1.1       oki 	lp->d_secpercyl   = lp->d_ntracks * lp->d_nsectors;
   1537   1.1       oki 	lp->d_ncylinders  = sc->sc_type->size / lp->d_secpercyl;
   1538   1.1       oki 	lp->d_secperunit  = sc->sc_type->size;
   1539   1.1       oki 
   1540   1.1       oki 	lp->d_type        = DTYPE_FLOPPY;
   1541   1.1       oki 	lp->d_rpm         = 300; 	/* XXX */
   1542   1.1       oki 	lp->d_interleave  = 1;		/* FIXME: is this OK?		*/
   1543   1.1       oki 	lp->d_bbsize      = 0;
   1544   1.1       oki 	lp->d_sbsize      = 0;
   1545   1.1       oki 	lp->d_npartitions = part + 1;
   1546   1.1       oki #define STEP_DELAY	6000	/* 6ms (6000us) delay after stepping	*/
   1547   1.1       oki 	lp->d_trkseek     = STEP_DELAY; /* XXX */
   1548   1.1       oki 	lp->d_magic       = DISKMAGIC;
   1549   1.1       oki 	lp->d_magic2      = DISKMAGIC;
   1550   1.1       oki 	lp->d_checksum    = dkcksum(lp);
   1551   1.1       oki 	lp->d_partitions[part].p_size   = lp->d_secperunit;
   1552   1.1       oki 	lp->d_partitions[part].p_fstype = FS_UNUSED;
   1553   1.1       oki 	lp->d_partitions[part].p_fsize  = 1024;
   1554   1.1       oki 	lp->d_partitions[part].p_frag   = 8;
   1555   1.1       oki 
   1556   1.1       oki 	return(0);
   1557   1.1       oki }
   1558   1.1       oki 
   1559   1.4       oki /* ARGSUSED */
   1560   1.4       oki void
   1561   1.4       oki fd_mountroot_hook(dev)
   1562   1.4       oki 	struct device *dev;
   1563   1.4       oki {
   1564   1.4       oki 	int c;
   1565   1.4       oki 
   1566   1.4       oki 	fd_do_eject(dev->dv_unit);
   1567   1.8  christos 	printf("Insert filesystem floppy and press return.");
   1568   1.4       oki 	for (;;) {
   1569   1.4       oki 		c = cngetc();
   1570   1.4       oki 		if ((c == '\r') || (c == '\n')) {
   1571   1.8  christos 			printf("\n");
   1572   1.4       oki 			return;
   1573   1.4       oki 		}
   1574   1.4       oki 	}
   1575   1.4       oki }
   1576