Home | History | Annotate | Line # | Download | only in jazz
fd.c revision 1.43
      1  1.43  dholland /*	$NetBSD: fd.c,v 1.43 2014/03/16 05:20:22 dholland Exp $	*/
      2   1.1        ur /*	$OpenBSD: fd.c,v 1.6 1998/10/03 21:18:57 millert Exp $	*/
      3   1.1        ur /*	NetBSD: fd.c,v 1.78 1995/07/04 07:23:09 mycroft Exp 	*/
      4   1.1        ur 
      5   1.1        ur /*-
      6   1.1        ur  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      7   1.1        ur  * All rights reserved.
      8   1.1        ur  *
      9   1.1        ur  * This code is derived from software contributed to The NetBSD Foundation
     10   1.1        ur  * by Charles M. Hannum.
     11   1.1        ur  *
     12   1.1        ur  * Redistribution and use in source and binary forms, with or without
     13   1.1        ur  * modification, are permitted provided that the following conditions
     14   1.1        ur  * are met:
     15   1.1        ur  * 1. Redistributions of source code must retain the above copyright
     16   1.1        ur  *    notice, this list of conditions and the following disclaimer.
     17   1.1        ur  * 2. Redistributions in binary form must reproduce the above copyright
     18   1.1        ur  *    notice, this list of conditions and the following disclaimer in the
     19   1.1        ur  *    documentation and/or other materials provided with the distribution.
     20   1.1        ur  *
     21   1.1        ur  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22   1.1        ur  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23   1.1        ur  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24   1.1        ur  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25   1.1        ur  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26   1.1        ur  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27   1.1        ur  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28   1.1        ur  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29   1.1        ur  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30   1.1        ur  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31   1.1        ur  * POSSIBILITY OF SUCH DAMAGE.
     32   1.1        ur  */
     33   1.1        ur 
     34   1.1        ur /*-
     35   1.1        ur  * Copyright (c) 1990 The Regents of the University of California.
     36   1.1        ur  * All rights reserved.
     37   1.1        ur  *
     38   1.1        ur  * This code is derived from software contributed to Berkeley by
     39   1.1        ur  * Don Ahn.
     40   1.1        ur  *
     41   1.1        ur  * Redistribution and use in source and binary forms, with or without
     42   1.1        ur  * modification, are permitted provided that the following conditions
     43   1.1        ur  * are met:
     44   1.1        ur  * 1. Redistributions of source code must retain the above copyright
     45   1.1        ur  *    notice, this list of conditions and the following disclaimer.
     46   1.1        ur  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.1        ur  *    notice, this list of conditions and the following disclaimer in the
     48   1.1        ur  *    documentation and/or other materials provided with the distribution.
     49  1.17       agc  * 3. Neither the name of the University nor the names of its contributors
     50   1.1        ur  *    may be used to endorse or promote products derived from this software
     51   1.1        ur  *    without specific prior written permission.
     52   1.1        ur  *
     53   1.1        ur  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54   1.1        ur  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55   1.1        ur  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56   1.1        ur  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57   1.1        ur  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58   1.1        ur  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59   1.1        ur  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60   1.1        ur  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61   1.1        ur  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62   1.1        ur  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63   1.1        ur  * SUCH DAMAGE.
     64   1.1        ur  *
     65   1.1        ur  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
     66   1.1        ur  */
     67  1.16     lukem 
     68  1.16     lukem #include <sys/cdefs.h>
     69  1.43  dholland __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.43 2014/03/16 05:20:22 dholland Exp $");
     70   1.1        ur 
     71   1.1        ur #include <sys/param.h>
     72   1.1        ur #include <sys/systm.h>
     73   1.1        ur #include <sys/callout.h>
     74   1.1        ur #include <sys/kernel.h>
     75   1.1        ur #include <sys/conf.h>
     76   1.1        ur #include <sys/file.h>
     77   1.1        ur #include <sys/ioctl.h>
     78   1.1        ur #include <sys/device.h>
     79   1.1        ur #include <sys/disklabel.h>
     80   1.1        ur #include <sys/disk.h>
     81   1.1        ur #include <sys/buf.h>
     82  1.18      yamt #include <sys/bufq.h>
     83   1.1        ur #include <sys/uio.h>
     84   1.1        ur #include <sys/syslog.h>
     85   1.1        ur #include <sys/queue.h>
     86   1.1        ur 
     87   1.1        ur #include <uvm/uvm_extern.h>
     88   1.1        ur 
     89   1.2      soda #include <dev/cons.h>
     90   1.2      soda 
     91  1.41    dyoung #include <sys/bus.h>
     92   1.1        ur #include <machine/cpu.h>
     93   1.1        ur 
     94   1.1        ur #include <arc/jazz/fdreg.h>
     95   1.2      soda #include <arc/jazz/fdcvar.h>
     96   1.1        ur 
     97  1.27   tsutsui #include "ioconf.h"
     98   1.1        ur #include "locators.h"
     99   1.1        ur 
    100   1.2      soda #define FDUNIT(dev)	DISKUNIT(dev)
    101   1.2      soda #define FDTYPE(dev)	DISKPART(dev)
    102   1.1        ur 
    103   1.1        ur /* controller driver configuration */
    104  1.37   tsutsui static int fdprint(void *, const char *);
    105   1.1        ur 
    106   1.1        ur /*
    107   1.1        ur  * Floppies come in various flavors, e.g., 1.2MB vs 1.44MB; here is how
    108   1.1        ur  * we tell them apart.
    109   1.1        ur  */
    110   1.1        ur struct fd_type {
    111   1.1        ur 	int	sectrac;	/* sectors per track */
    112   1.1        ur 	int	heads;		/* number of heads */
    113   1.1        ur 	int	seccyl;		/* sectors per cylinder */
    114   1.1        ur 	int	secsize;	/* size code for sectors */
    115   1.1        ur 	int	datalen;	/* data len when secsize = 0 */
    116   1.1        ur 	int	steprate;	/* step rate and head unload time */
    117   1.1        ur 	int	gap1;		/* gap len between sectors */
    118   1.1        ur 	int	gap2;		/* formatting gap */
    119   1.2      soda 	int	cyls;		/* total num of cylinders */
    120   1.1        ur 	int	size;		/* size of disk in sectors */
    121   1.1        ur 	int	step;		/* steps per cylinder */
    122   1.1        ur 	int	rate;		/* transfer speed code */
    123   1.2      soda 	const char *name;
    124   1.1        ur };
    125   1.1        ur 
    126   1.1        ur /* The order of entries in the following table is important -- BEWARE! */
    127  1.37   tsutsui const static struct fd_type fd_types[] = {
    128  1.11   tsutsui 	/* 1.44MB diskette */
    129  1.11   tsutsui 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB"    },
    130  1.11   tsutsui 	/* 1.2 MB AT-diskettes */
    131  1.11   tsutsui 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB"    },
    132  1.11   tsutsui 	/* 360kB in 1.2MB drive */
    133  1.11   tsutsui 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" },
    134  1.11   tsutsui 	/* 360kB PC diskettes */
    135  1.11   tsutsui 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" },
    136  1.11   tsutsui 	/* 3.5" 720kB diskette */
    137  1.11   tsutsui 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB"    },
    138  1.11   tsutsui 	/* 720kB in 1.2MB drive */
    139  1.11   tsutsui 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x"  },
    140  1.11   tsutsui 	/* 360kB in 720kB drive */
    141  1.11   tsutsui 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x"  },
    142   1.1        ur };
    143   1.1        ur 
    144   1.1        ur /* software state, per disk (with up to 4 disks per ctlr) */
    145   1.1        ur struct fd_softc {
    146  1.37   tsutsui 	device_t sc_dev;
    147   1.1        ur 	struct disk sc_dk;
    148   1.1        ur 
    149   1.2      soda 	const struct fd_type *sc_deftype; /* default type descriptor */
    150   1.1        ur 	struct fd_type *sc_type;	/* current type descriptor */
    151   1.2      soda 	struct fd_type sc_type_copy;	/* copy for fiddling when formatting */
    152   1.1        ur 
    153   1.1        ur 	struct callout sc_motoron_ch;
    154   1.1        ur 	struct callout sc_motoroff_ch;
    155   1.1        ur 
    156   1.1        ur 	daddr_t	sc_blkno;	/* starting block number */
    157   1.1        ur 	int sc_bcount;		/* byte count left */
    158   1.2      soda 	int sc_opts;		/* user-set options */
    159   1.1        ur 	int sc_skip;		/* bytes already transferred */
    160   1.3       wiz 	int sc_nblks;		/* number of blocks currently transferring */
    161   1.3       wiz 	int sc_nbytes;		/* number of bytes currently transferring */
    162   1.1        ur 
    163   1.1        ur 	int sc_drive;		/* physical unit number */
    164   1.1        ur 	int sc_flags;
    165   1.1        ur #define	FD_OPEN		0x01		/* it's open */
    166   1.1        ur #define	FD_MOTOR	0x02		/* motor should be on */
    167   1.1        ur #define	FD_MOTOR_WAIT	0x04		/* motor coming up */
    168   1.1        ur 	int sc_cylin;		/* where we think the head is */
    169   1.1        ur 
    170   1.1        ur 	TAILQ_ENTRY(fd_softc) sc_drivechain;
    171   1.1        ur 	int sc_ops;		/* I/O ops since last switch */
    172  1.22      yamt 	struct bufq_state *sc_q;/* pending I/O requests */
    173   1.1        ur 	int sc_active;		/* number of active I/O operations */
    174   1.1        ur };
    175   1.1        ur 
    176   1.1        ur /* floppy driver configuration */
    177  1.37   tsutsui static int fdprobe(device_t, cfdata_t, void *);
    178  1.37   tsutsui static void fdattach(device_t, device_t, void *);
    179   1.1        ur 
    180  1.37   tsutsui CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc), fdprobe, fdattach, NULL, NULL);
    181   1.1        ur 
    182   1.5   gehenna dev_type_open(fdopen);
    183   1.5   gehenna dev_type_close(fdclose);
    184   1.5   gehenna dev_type_read(fdread);
    185   1.5   gehenna dev_type_write(fdwrite);
    186   1.5   gehenna dev_type_ioctl(fdioctl);
    187   1.5   gehenna dev_type_strategy(fdstrategy);
    188   1.5   gehenna 
    189   1.5   gehenna const struct bdevsw fd_bdevsw = {
    190  1.43  dholland 	.d_open = fdopen,
    191  1.43  dholland 	.d_close = fdclose,
    192  1.43  dholland 	.d_strategy = fdstrategy,
    193  1.43  dholland 	.d_ioctl = fdioctl,
    194  1.43  dholland 	.d_dump = nodump,
    195  1.43  dholland 	.d_psize = nosize,
    196  1.43  dholland 	.d_flag = D_DISK
    197   1.5   gehenna };
    198   1.5   gehenna 
    199   1.5   gehenna const struct cdevsw fd_cdevsw = {
    200  1.43  dholland 	.d_open = fdopen,
    201  1.43  dholland 	.d_close = fdclose,
    202  1.43  dholland 	.d_read = fdread,
    203  1.43  dholland 	.d_write = fdwrite,
    204  1.43  dholland 	.d_ioctl = fdioctl,
    205  1.43  dholland 	.d_stop = nostop,
    206  1.43  dholland 	.d_tty = notty,
    207  1.43  dholland 	.d_poll = nopoll,
    208  1.43  dholland 	.d_mmap = nommap,
    209  1.43  dholland 	.d_kqfilter = nokqfilter,
    210  1.43  dholland 	.d_flag = D_DISK
    211   1.5   gehenna };
    212   1.5   gehenna 
    213  1.37   tsutsui static void fdstart(struct fd_softc *);
    214   1.1        ur 
    215   1.1        ur struct dkdriver fddkdriver = { fdstrategy };
    216   1.1        ur 
    217  1.40   tsutsui static bool fd_shutdown(device_t, int);
    218   1.2      soda #if 0
    219  1.37   tsutsui static const struct fd_type *fd_nvtotype(char *, int, int);
    220   1.2      soda #endif
    221  1.37   tsutsui static void fd_set_motor(struct fdc_softc *, int);
    222  1.37   tsutsui static void fd_motor_off(void *);
    223  1.37   tsutsui static void fd_motor_on(void *);
    224  1.37   tsutsui static int fdcresult(struct fdc_softc *);
    225  1.37   tsutsui static void fdcstart(struct fdc_softc *);
    226  1.37   tsutsui static void fdcstatus(device_t, int, const char *);
    227  1.37   tsutsui static void fdctimeout(void *);
    228  1.37   tsutsui static void fdcpseudointr(void *);
    229  1.37   tsutsui static void fdcretry(struct fdc_softc *);
    230  1.37   tsutsui static void fdfinish(struct fd_softc *, struct buf *);
    231  1.37   tsutsui static inline const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
    232  1.37   tsutsui static void fd_mountroot_hook(device_t);
    233   1.1        ur 
    234   1.1        ur /*
    235   1.1        ur  * Arguments passed between fdcattach and fdprobe.
    236   1.1        ur  */
    237   1.1        ur struct fdc_attach_args {
    238   1.1        ur 	int fa_drive;
    239   1.2      soda 	const struct fd_type *fa_deftype;
    240   1.1        ur };
    241   1.1        ur 
    242   1.1        ur /*
    243   1.1        ur  * Print the location of a disk drive (called just before attaching the
    244   1.1        ur  * the drive).  If `fdc' is not NULL, the drive was found but was not
    245   1.1        ur  * in the system config file; print the drive name as well.
    246   1.1        ur  * Return QUIET (config_find ignores this if the device was configured) to
    247   1.1        ur  * avoid printing `fdN not configured' messages.
    248   1.1        ur  */
    249  1.37   tsutsui static int
    250  1.19   tsutsui fdprint(void *aux, const char *fdc)
    251   1.1        ur {
    252  1.14   tsutsui 	struct fdc_attach_args *fa = aux;
    253   1.1        ur 
    254  1.37   tsutsui 	if (fdc == NULL)
    255  1.10   thorpej 		aprint_normal(" drive %d", fa->fa_drive);
    256   1.1        ur 	return QUIET;
    257   1.1        ur }
    258   1.1        ur 
    259   1.1        ur void
    260  1.19   tsutsui fdcattach(struct fdc_softc *fdc)
    261   1.1        ur {
    262   1.1        ur 	struct fdc_attach_args fa;
    263   1.1        ur 	int type;
    264   1.1        ur 
    265  1.30        ad 	callout_init(&fdc->sc_timo_ch, 0);
    266  1.30        ad 	callout_init(&fdc->sc_intr_ch, 0);
    267   1.2      soda 
    268   1.1        ur 	fdc->sc_state = DEVIDLE;
    269   1.1        ur 	TAILQ_INIT(&fdc->sc_drives);
    270   1.1        ur 
    271   1.1        ur 	/*
    272   1.1        ur 	 * No way yet to determine default disk types.
    273   1.1        ur 	 * we assume 1.44 3.5" type for the moment.
    274   1.1        ur 	 */
    275   1.1        ur 	type = 0;
    276   1.1        ur 
    277   1.1        ur 	/* physical limit: two drives per controller. */
    278   1.1        ur 	for (fa.fa_drive = 0; fa.fa_drive < 2; fa.fa_drive++) {
    279   1.2      soda 		fa.fa_deftype = &fd_types[type];
    280  1.37   tsutsui 		(void)config_found(fdc->sc_dev, (void *)&fa, fdprint);
    281   1.1        ur 	}
    282   1.1        ur }
    283   1.1        ur 
    284  1.37   tsutsui static int
    285  1.37   tsutsui fdprobe(device_t parent, cfdata_t cf , void *aux)
    286   1.1        ur {
    287  1.37   tsutsui 	struct fdc_softc *fdc = device_private(parent);
    288   1.1        ur 	struct fdc_attach_args *fa = aux;
    289   1.1        ur 	int drive = fa->fa_drive;
    290   1.2      soda 	bus_space_tag_t iot = fdc->sc_iot;
    291   1.2      soda 	bus_space_handle_t ioh = fdc->sc_ioh;
    292   1.1        ur 	int n;
    293   1.1        ur 
    294   1.2      soda 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
    295   1.2      soda 	    cf->cf_loc[FDCCF_DRIVE] != drive)
    296   1.1        ur 		return 0;
    297   1.1        ur 
    298   1.1        ur 	/* select drive and turn on motor */
    299   1.2      soda 	bus_space_write_1(iot, ioh, FDOUT, drive | FDO_FRST | FDO_MOEN(drive));
    300   1.1        ur 	/* wait for motor to spin up */
    301   1.2      soda 	delay(250000);
    302   1.2      soda 	out_fdc(iot, ioh, NE7CMD_RECAL);
    303   1.2      soda 	out_fdc(iot, ioh, drive);
    304   1.1        ur 	/* wait for recalibrate */
    305   1.1        ur 	delay(2000000);
    306   1.2      soda 	out_fdc(iot, ioh, NE7CMD_SENSEI);
    307   1.1        ur 	n = fdcresult(fdc);
    308   1.1        ur #ifdef FD_DEBUG
    309   1.1        ur 	{
    310   1.1        ur 		int i;
    311  1.37   tsutsui 		aprint_debug("%s: status", __func__);
    312   1.1        ur 		for (i = 0; i < n; i++)
    313  1.37   tsutsui 			aprint_debug(" %x", fdc->sc_status[i]);
    314  1.37   tsutsui 		aprint_debug("\n");
    315   1.1        ur 	}
    316   1.1        ur #endif
    317   1.1        ur 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
    318   1.1        ur 		return 0;
    319   1.1        ur 	/* turn off motor */
    320   1.2      soda 	bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
    321   1.1        ur 
    322   1.1        ur 	return 1;
    323   1.1        ur }
    324   1.1        ur 
    325   1.1        ur /*
    326   1.1        ur  * Controller is working, and drive responded.  Attach it.
    327   1.1        ur  */
    328   1.1        ur void
    329  1.37   tsutsui fdattach(device_t parent, device_t self, void *aux)
    330   1.1        ur {
    331  1.37   tsutsui 	struct fdc_softc *fdc = device_private(parent);
    332  1.37   tsutsui 	struct fd_softc *fd = device_private(self);
    333   1.1        ur 	struct fdc_attach_args *fa = aux;
    334   1.2      soda 	const struct fd_type *type = fa->fa_deftype;
    335   1.1        ur 	int drive = fa->fa_drive;
    336   1.1        ur 
    337  1.37   tsutsui 	fd->sc_dev = self;
    338  1.37   tsutsui 
    339  1.30        ad 	callout_init(&fd->sc_motoron_ch, 0);
    340  1.30        ad 	callout_init(&fd->sc_motoroff_ch, 0);
    341   1.1        ur 
    342   1.1        ur 	/* XXX Allow `flags' to override device type? */
    343   1.1        ur 
    344   1.1        ur 	if (type)
    345   1.2      soda 		printf(": %s, %d cyl, %d head, %d sec\n", type->name,
    346   1.2      soda 		    type->cyls, type->heads, type->sectrac);
    347   1.1        ur 	else
    348   1.1        ur 		printf(": density unknown\n");
    349   1.1        ur 
    350  1.22      yamt 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    351   1.1        ur 	fd->sc_cylin = -1;
    352   1.1        ur 	fd->sc_drive = drive;
    353   1.1        ur 	fd->sc_deftype = type;
    354   1.1        ur 	fdc->sc_fd[drive] = fd;
    355   1.2      soda 
    356   1.2      soda 	/*
    357   1.2      soda 	 * Initialize and attach the disk structure.
    358   1.2      soda 	 */
    359  1.37   tsutsui 	disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
    360   1.2      soda 	disk_attach(&fd->sc_dk);
    361   1.2      soda 
    362   1.2      soda 	/* Establish a mountroot hook. */
    363  1.37   tsutsui 	mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
    364   1.1        ur 
    365   1.1        ur 	/* Needed to power off if the motor is on when we halt. */
    366  1.40   tsutsui 	if (!pmf_device_register1(self, NULL, NULL, fd_shutdown))
    367  1.40   tsutsui 		aprint_error_dev(self, "couldn't establish power handler\n");
    368  1.40   tsutsui }
    369  1.40   tsutsui 
    370  1.40   tsutsui bool
    371  1.40   tsutsui fd_shutdown(device_t self, int howto)
    372  1.40   tsutsui {
    373  1.40   tsutsui 	struct fd_softc *fd;
    374  1.40   tsutsui 
    375  1.40   tsutsui 	fd = device_private(self);
    376  1.40   tsutsui 	fd_motor_off(fd);
    377  1.40   tsutsui 
    378  1.40   tsutsui 	return true;
    379   1.1        ur }
    380   1.1        ur 
    381   1.2      soda #if 0
    382   1.1        ur /*
    383   1.1        ur  * Translate nvram type into internal data structure.  Return NULL for
    384   1.1        ur  * none/unknown/unusable.
    385   1.1        ur  */
    386  1.37   tsutsui static const struct fd_type *
    387  1.19   tsutsui fd_nvtotype(char *fdc, int nvraminfo, int drive)
    388   1.1        ur {
    389   1.1        ur 	int type;
    390   1.1        ur 
    391   1.1        ur 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
    392   1.1        ur #if 0
    393   1.1        ur 	switch (type) {
    394   1.1        ur 	case NVRAM_DISKETTE_NONE:
    395   1.1        ur 		return NULL;
    396   1.1        ur 	case NVRAM_DISKETTE_12M:
    397   1.1        ur 		return &fd_types[1];
    398   1.1        ur 	case NVRAM_DISKETTE_TYPE5:
    399   1.1        ur 	case NVRAM_DISKETTE_TYPE6:
    400   1.1        ur 		/* XXX We really ought to handle 2.88MB format. */
    401   1.1        ur 	case NVRAM_DISKETTE_144M:
    402   1.1        ur 		return &fd_types[0];
    403   1.1        ur 	case NVRAM_DISKETTE_360K:
    404   1.1        ur 		return &fd_types[3];
    405   1.1        ur 	case NVRAM_DISKETTE_720K:
    406   1.1        ur 		return &fd_types[4];
    407   1.1        ur 	default:
    408   1.1        ur 		printf("%s: drive %d: unknown device type 0x%x\n",
    409   1.1        ur 		    fdc, drive, type);
    410   1.1        ur 		return NULL;
    411   1.1        ur 	}
    412   1.1        ur #else
    413   1.1        ur 	return &fd_types[0]; /* Use only 1.44 for now */
    414   1.1        ur #endif
    415   1.1        ur }
    416   1.2      soda #endif
    417   1.2      soda 
    418  1.37   tsutsui static inline const struct fd_type *
    419  1.19   tsutsui fd_dev_to_type(struct fd_softc *fd, dev_t dev)
    420   1.2      soda {
    421   1.2      soda 	int type = FDTYPE(dev);
    422   1.2      soda 
    423  1.37   tsutsui 	if (type > __arraycount(fd_types))
    424   1.2      soda 		return NULL;
    425   1.2      soda 	return type ? &fd_types[type - 1] : fd->sc_deftype;
    426   1.2      soda }
    427   1.1        ur 
    428   1.1        ur void
    429  1.19   tsutsui fdstrategy(struct buf *bp)
    430   1.1        ur {
    431  1.36   tsutsui 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
    432   1.1        ur 	int sz;
    433  1.11   tsutsui 	int s;
    434   1.1        ur 
    435   1.1        ur 	/* Valid unit, controller, and request? */
    436   1.2      soda 	if (bp->b_blkno < 0 ||
    437   1.1        ur 	    (bp->b_bcount % FDC_BSIZE) != 0) {
    438   1.1        ur 		bp->b_error = EINVAL;
    439  1.32        he 		goto done;
    440   1.1        ur 	}
    441   1.1        ur 
    442   1.1        ur 	/* If it's a null transfer, return immediately. */
    443   1.1        ur 	if (bp->b_bcount == 0)
    444   1.1        ur 		goto done;
    445   1.1        ur 
    446   1.1        ur 	sz = howmany(bp->b_bcount, FDC_BSIZE);
    447   1.1        ur 
    448   1.1        ur 	if (bp->b_blkno + sz > fd->sc_type->size) {
    449   1.1        ur 		sz = fd->sc_type->size - bp->b_blkno;
    450   1.1        ur 		if (sz == 0) {
    451   1.1        ur 			/* If exactly at end of disk, return EOF. */
    452   1.1        ur 			goto done;
    453   1.1        ur 		}
    454   1.1        ur 		if (sz < 0) {
    455   1.1        ur 			/* If past end of disk, return EINVAL. */
    456   1.1        ur 			bp->b_error = EINVAL;
    457  1.31        ad 			goto done;
    458   1.1        ur 		}
    459   1.1        ur 		/* Otherwise, truncate request. */
    460   1.1        ur 		bp->b_bcount = sz << DEV_BSHIFT;
    461   1.1        ur 	}
    462   1.1        ur 
    463   1.1        ur 	bp->b_rawblkno = bp->b_blkno;
    464  1.11   tsutsui 	bp->b_cylinder =
    465   1.2      soda 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
    466   1.1        ur 
    467   1.1        ur #ifdef FD_DEBUG
    468  1.37   tsutsui 	printf("%s: b_blkno %" PRId64 " b_bcount %ld blkno %" PRId64
    469  1.37   tsutsui 	    " cylin %ld sz %d\n", __func__,
    470   1.1        ur 	    bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
    471   1.1        ur #endif
    472   1.1        ur 
    473   1.1        ur 	/* Queue transfer on drive, activate drive and controller if idle. */
    474   1.1        ur 	s = splbio();
    475  1.39      yamt 	bufq_put(fd->sc_q, bp);
    476   1.1        ur 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
    477   1.1        ur 	if (fd->sc_active == 0)
    478   1.1        ur 		fdstart(fd);
    479   1.1        ur #ifdef DIAGNOSTIC
    480   1.1        ur 	else {
    481  1.37   tsutsui 		struct fdc_softc *fdc =
    482  1.37   tsutsui 		    device_private(device_parent(fd->sc_dev));
    483   1.1        ur 		if (fdc->sc_state == DEVIDLE) {
    484  1.37   tsutsui 			printf("%s: controller inactive\n", __func__);
    485   1.1        ur 			fdcstart(fdc);
    486   1.1        ur 		}
    487   1.1        ur 	}
    488   1.1        ur #endif
    489   1.1        ur 	splx(s);
    490   1.1        ur 	return;
    491   1.1        ur 
    492  1.37   tsutsui  done:
    493   1.1        ur 	/* Toss transfer; we're done early. */
    494   1.2      soda 	bp->b_resid = bp->b_bcount;
    495   1.1        ur 	biodone(bp);
    496   1.1        ur }
    497   1.1        ur 
    498   1.1        ur void
    499  1.19   tsutsui fdstart(struct fd_softc *fd)
    500   1.1        ur {
    501  1.37   tsutsui 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    502  1.20   tsutsui 	int active = TAILQ_FIRST(&fdc->sc_drives) != 0;
    503   1.1        ur 
    504   1.1        ur 	/* Link into controller queue. */
    505   1.1        ur 	fd->sc_active = 1;
    506   1.1        ur 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    507   1.1        ur 
    508   1.1        ur 	/* If controller not already active, start it. */
    509   1.1        ur 	if (!active)
    510   1.1        ur 		fdcstart(fdc);
    511   1.1        ur }
    512   1.1        ur 
    513   1.1        ur void
    514  1.19   tsutsui fdfinish(struct fd_softc *fd, struct buf *bp)
    515   1.1        ur {
    516  1.37   tsutsui 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    517   1.1        ur 
    518   1.1        ur 	/*
    519   1.1        ur 	 * Move this drive to the end of the queue to give others a `fair'
    520   1.1        ur 	 * chance.  We only force a switch if N operations are completed while
    521   1.1        ur 	 * another drive is waiting to be serviced, since there is a long motor
    522   1.1        ur 	 * startup delay whenever we switch.
    523   1.1        ur 	 */
    524  1.39      yamt 	(void)bufq_get(fd->sc_q);
    525  1.37   tsutsui 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
    526   1.1        ur 		fd->sc_ops = 0;
    527   1.1        ur 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    528  1.39      yamt 		if (bufq_peek(fd->sc_q) != NULL)
    529   1.1        ur 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
    530   1.2      soda 		else
    531   1.1        ur 			fd->sc_active = 0;
    532   1.1        ur 	}
    533   1.1        ur 	bp->b_resid = fd->sc_bcount;
    534   1.1        ur 	fd->sc_skip = 0;
    535   1.1        ur 	biodone(bp);
    536   1.1        ur 	/* turn off motor 5s from now */
    537   1.2      soda 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
    538   1.1        ur 	fdc->sc_state = DEVIDLE;
    539   1.1        ur }
    540   1.1        ur 
    541   1.1        ur int
    542  1.19   tsutsui fdread(dev_t dev, struct uio *uio, int flags)
    543   1.1        ur {
    544   1.1        ur 
    545  1.19   tsutsui 	return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
    546   1.1        ur }
    547   1.1        ur 
    548   1.1        ur int
    549  1.19   tsutsui fdwrite(dev_t dev, struct uio *uio, int flags)
    550   1.1        ur {
    551   1.1        ur 
    552  1.19   tsutsui 	return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
    553   1.1        ur }
    554   1.1        ur 
    555   1.1        ur void
    556  1.19   tsutsui fd_set_motor(struct fdc_softc *fdc, int reset)
    557   1.1        ur {
    558   1.1        ur 	struct fd_softc *fd;
    559   1.1        ur 	u_char status;
    560   1.1        ur 	int n;
    561   1.1        ur 
    562  1.20   tsutsui 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
    563   1.1        ur 		status = fd->sc_drive;
    564   1.1        ur 	else
    565   1.1        ur 		status = 0;
    566   1.1        ur 	if (!reset)
    567   1.1        ur 		status |= FDO_FRST | FDO_FDMAEN;
    568   1.1        ur 	for (n = 0; n < 4; n++)
    569   1.1        ur 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
    570   1.1        ur 			status |= FDO_MOEN(n);
    571   1.2      soda 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
    572   1.1        ur }
    573   1.1        ur 
    574   1.1        ur void
    575  1.19   tsutsui fd_motor_off(void *arg)
    576   1.1        ur {
    577   1.1        ur 	struct fd_softc *fd = arg;
    578  1.37   tsutsui 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    579   1.1        ur 	int s;
    580   1.1        ur 
    581   1.1        ur 	s = splbio();
    582   1.1        ur 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    583  1.37   tsutsui 	fd_set_motor(fdc, 0);
    584   1.1        ur 	splx(s);
    585   1.1        ur }
    586   1.1        ur 
    587   1.1        ur void
    588  1.19   tsutsui fd_motor_on(void *arg)
    589   1.1        ur {
    590   1.1        ur 	struct fd_softc *fd = arg;
    591  1.37   tsutsui 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
    592   1.1        ur 	int s;
    593   1.1        ur 
    594   1.1        ur 	s = splbio();
    595   1.1        ur 	fd->sc_flags &= ~FD_MOTOR_WAIT;
    596  1.20   tsutsui 	if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&
    597  1.20   tsutsui 	    (fdc->sc_state == MOTORWAIT))
    598  1.37   tsutsui 		(void)fdcintr(fdc);
    599   1.1        ur 	splx(s);
    600   1.1        ur }
    601   1.1        ur 
    602   1.1        ur int
    603  1.19   tsutsui fdcresult(struct fdc_softc *fdc)
    604   1.1        ur {
    605   1.2      soda 	bus_space_tag_t iot = fdc->sc_iot;
    606   1.2      soda 	bus_space_handle_t ioh = fdc->sc_ioh;
    607   1.1        ur 	u_char i;
    608  1.37   tsutsui 	int j, n = 0;
    609   1.1        ur 
    610  1.37   tsutsui 	for (j = 100000; j; j--) {
    611   1.2      soda 		i = bus_space_read_1(iot, ioh, FDSTS) &
    612   1.2      soda 		    (NE7_DIO | NE7_RQM | NE7_CB);
    613   1.2      soda 		if (i == NE7_RQM)
    614   1.1        ur 			return n;
    615   1.1        ur 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
    616   1.1        ur 			if (n >= sizeof(fdc->sc_status)) {
    617  1.37   tsutsui 				log(LOG_ERR, "%s: overrun\n", __func__);
    618   1.1        ur 				return -1;
    619   1.1        ur 			}
    620   1.2      soda 			fdc->sc_status[n++] =
    621   1.2      soda 			    bus_space_read_1(iot, ioh, FDDATA);
    622   1.1        ur 		}
    623   1.2      soda 		delay(10);
    624   1.1        ur 	}
    625  1.37   tsutsui 	log(LOG_ERR, "%s: timeout\n", __func__);
    626   1.1        ur 	return -1;
    627   1.1        ur }
    628   1.1        ur 
    629   1.1        ur int
    630  1.37   tsutsui out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t x)
    631   1.1        ur {
    632   1.1        ur 	int i = 100000;
    633   1.1        ur 
    634   1.2      soda 	while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
    635   1.1        ur 	if (i <= 0)
    636   1.1        ur 		return -1;
    637   1.2      soda 	while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
    638   1.1        ur 	if (i <= 0)
    639   1.1        ur 		return -1;
    640   1.2      soda 	bus_space_write_1(iot, ioh, FDDATA, x);
    641   1.1        ur 	return 0;
    642   1.1        ur }
    643   1.1        ur 
    644   1.1        ur int
    645  1.23  christos fdopen(dev_t dev, int flags, int mode, struct lwp *l)
    646   1.1        ur {
    647   1.1        ur 	struct fd_softc *fd;
    648   1.2      soda 	const struct fd_type *type;
    649   1.1        ur 
    650  1.36   tsutsui 	fd = device_lookup_private(&fd_cd, FDUNIT(dev));
    651   1.2      soda 	if (fd == NULL)
    652   1.1        ur 		return ENXIO;
    653   1.1        ur 
    654   1.2      soda 	type = fd_dev_to_type(fd, dev);
    655   1.1        ur 	if (type == NULL)
    656   1.1        ur 		return ENXIO;
    657   1.1        ur 
    658   1.1        ur 	if ((fd->sc_flags & FD_OPEN) != 0 &&
    659   1.2      soda 	    memcmp(fd->sc_type, type, sizeof(*type)))
    660   1.1        ur 		return EBUSY;
    661   1.1        ur 
    662   1.2      soda 	fd->sc_type_copy = *type;
    663   1.2      soda 	fd->sc_type = &fd->sc_type_copy;
    664   1.1        ur 	fd->sc_cylin = -1;
    665   1.1        ur 	fd->sc_flags |= FD_OPEN;
    666   1.1        ur 
    667   1.1        ur 	return 0;
    668   1.1        ur }
    669   1.1        ur 
    670   1.1        ur int
    671  1.23  christos fdclose(dev_t dev, int flags, int mode, struct lwp *l)
    672   1.1        ur {
    673  1.36   tsutsui 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
    674   1.1        ur 
    675   1.1        ur 	fd->sc_flags &= ~FD_OPEN;
    676   1.1        ur 	return 0;
    677   1.1        ur }
    678   1.1        ur 
    679   1.1        ur void
    680  1.19   tsutsui fdcstart(struct fdc_softc *fdc)
    681   1.1        ur {
    682   1.1        ur 
    683   1.1        ur #ifdef DIAGNOSTIC
    684   1.1        ur 	/* only got here if controller's drive queue was inactive; should
    685   1.1        ur 	   be in idle state */
    686   1.1        ur 	if (fdc->sc_state != DEVIDLE) {
    687  1.37   tsutsui 		printf("%s: not idle\n", __func__);
    688   1.1        ur 		return;
    689   1.1        ur 	}
    690   1.1        ur #endif
    691  1.37   tsutsui 	(void)fdcintr(fdc);
    692   1.1        ur }
    693   1.1        ur 
    694  1.38  christos static void
    695  1.38  christos fdcpstatus(int n, struct fdc_softc *fdc)
    696   1.1        ur {
    697   1.1        ur 	char bits[64];
    698   1.1        ur 
    699   1.1        ur 	switch (n) {
    700   1.1        ur 	case 0:
    701   1.1        ur 		printf("\n");
    702   1.1        ur 		break;
    703   1.1        ur 	case 2:
    704  1.38  christos 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
    705  1.38  christos 		printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]);
    706   1.1        ur 		break;
    707   1.1        ur 	case 7:
    708  1.38  christos 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
    709  1.38  christos 		printf(" (st0 %s", bits);
    710  1.38  christos 		snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]);
    711  1.38  christos 		printf(" st1 %s", bits);
    712  1.38  christos 		snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]);
    713  1.38  christos 		printf(" st2 %s", bits);
    714   1.1        ur 		printf(" cyl %d head %d sec %d)\n",
    715   1.1        ur 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
    716   1.1        ur 		break;
    717   1.1        ur #ifdef DIAGNOSTIC
    718   1.1        ur 	default:
    719  1.38  christos 		printf("\nfdcstatus: weird size");
    720   1.1        ur 		break;
    721   1.1        ur #endif
    722   1.1        ur 	}
    723   1.1        ur }
    724   1.1        ur 
    725   1.1        ur void
    726  1.38  christos fdcstatus(device_t dev, int n, const char *s)
    727  1.38  christos {
    728  1.38  christos 	struct fdc_softc *fdc = device_private(device_parent(dev));
    729  1.38  christos 
    730  1.38  christos 	if (n == 0) {
    731  1.38  christos 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
    732  1.38  christos 		(void)fdcresult(fdc);
    733  1.38  christos 		n = 2;
    734  1.38  christos 	}
    735  1.38  christos 
    736  1.38  christos 	printf("%s: %s", device_xname(dev), s);
    737  1.38  christos 	fdcpstatus(n, fdc);
    738  1.38  christos }
    739  1.38  christos 
    740  1.38  christos void
    741  1.19   tsutsui fdctimeout(void *arg)
    742   1.1        ur {
    743   1.1        ur 	struct fdc_softc *fdc = arg;
    744  1.20   tsutsui 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
    745   1.1        ur 	int s;
    746   1.1        ur 
    747   1.1        ur 	s = splbio();
    748   1.2      soda #ifdef DEBUG
    749  1.37   tsutsui 	log(LOG_ERR, "%s: state %d\n", __func__, fdc->sc_state);
    750   1.2      soda #endif
    751  1.37   tsutsui 	fdcstatus(fd->sc_dev, 0, "timeout");
    752   1.1        ur 
    753  1.39      yamt 	if (bufq_peek(fd->sc_q) != NULL)
    754   1.1        ur 		fdc->sc_state++;
    755   1.1        ur 	else
    756   1.1        ur 		fdc->sc_state = DEVIDLE;
    757   1.1        ur 
    758  1.37   tsutsui 	(void)fdcintr(fdc);
    759   1.1        ur 	splx(s);
    760   1.1        ur }
    761   1.1        ur 
    762   1.1        ur void
    763  1.19   tsutsui fdcpseudointr(void *arg)
    764   1.1        ur {
    765   1.1        ur 	int s;
    766   1.1        ur 
    767   1.1        ur 	/* Just ensure it has the right spl. */
    768   1.1        ur 	s = splbio();
    769  1.37   tsutsui 	(void)fdcintr(arg);
    770   1.1        ur 	splx(s);
    771   1.1        ur }
    772   1.1        ur 
    773   1.1        ur int
    774  1.19   tsutsui fdcintr(void *arg)
    775   1.1        ur {
    776   1.1        ur 	struct fdc_softc *fdc = arg;
    777   1.1        ur #define	st0	fdc->sc_status[0]
    778   1.1        ur #define	cyl	fdc->sc_status[1]
    779   1.1        ur 	struct fd_softc *fd;
    780   1.1        ur 	struct buf *bp;
    781   1.2      soda 	bus_space_tag_t iot = fdc->sc_iot;
    782   1.2      soda 	bus_space_handle_t ioh = fdc->sc_ioh;
    783   1.1        ur 	int read, head, sec, i, nblks;
    784   1.1        ur 	struct fd_type *type;
    785   1.1        ur 
    786  1.37   tsutsui  loop:
    787   1.1        ur 	/* Is there a drive for the controller to do a transfer with? */
    788  1.20   tsutsui 	fd = TAILQ_FIRST(&fdc->sc_drives);
    789   1.1        ur 	if (fd == NULL) {
    790   1.1        ur 		fdc->sc_state = DEVIDLE;
    791  1.11   tsutsui 		return 1;
    792   1.1        ur 	}
    793   1.1        ur 
    794   1.1        ur 	/* Is there a transfer to this drive?  If not, deactivate drive. */
    795  1.39      yamt 	bp = bufq_peek(fd->sc_q);
    796   1.1        ur 	if (bp == NULL) {
    797   1.1        ur 		fd->sc_ops = 0;
    798   1.1        ur 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
    799   1.1        ur 		fd->sc_active = 0;
    800   1.1        ur 		goto loop;
    801   1.1        ur 	}
    802   1.1        ur 
    803   1.1        ur 	switch (fdc->sc_state) {
    804   1.1        ur 	case DEVIDLE:
    805   1.1        ur 		fdc->sc_errors = 0;
    806   1.1        ur 		fd->sc_skip = 0;
    807   1.1        ur 		fd->sc_bcount = bp->b_bcount;
    808   1.1        ur 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
    809   1.1        ur 		callout_stop(&fd->sc_motoroff_ch);
    810   1.1        ur 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
    811   1.1        ur 			fdc->sc_state = MOTORWAIT;
    812   1.1        ur 			return 1;
    813   1.1        ur 		}
    814   1.1        ur 		if ((fd->sc_flags & FD_MOTOR) == 0) {
    815   1.1        ur 			/* Turn on the motor, being careful about pairing. */
    816   1.1        ur 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
    817   1.1        ur 			if (ofd && ofd->sc_flags & FD_MOTOR) {
    818   1.1        ur 				callout_stop(&ofd->sc_motoroff_ch);
    819   1.1        ur 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
    820   1.1        ur 			}
    821   1.1        ur 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
    822   1.1        ur 			fd_set_motor(fdc, 0);
    823   1.1        ur 			fdc->sc_state = MOTORWAIT;
    824   1.2      soda 			/* Allow .25s for motor to stabilize. */
    825   1.2      soda 			callout_reset(&fd->sc_motoron_ch, hz / 4,
    826   1.1        ur 			    fd_motor_on, fd);
    827   1.1        ur 			return 1;
    828   1.1        ur 		}
    829   1.1        ur 		/* Make sure the right drive is selected. */
    830   1.1        ur 		fd_set_motor(fdc, 0);
    831   1.1        ur 
    832   1.1        ur 		/* fall through */
    833   1.1        ur 	case DOSEEK:
    834  1.37   tsutsui  doseek:
    835   1.1        ur 		if (fd->sc_cylin == bp->b_cylinder)
    836   1.1        ur 			goto doio;
    837   1.1        ur 
    838   1.2      soda 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
    839   1.2      soda 		out_fdc(iot, ioh, fd->sc_type->steprate);
    840   1.2      soda 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
    841   1.2      soda 
    842   1.2      soda 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
    843   1.2      soda 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
    844   1.2      soda 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
    845   1.1        ur 
    846   1.1        ur 		fd->sc_cylin = -1;
    847   1.1        ur 		fdc->sc_state = SEEKWAIT;
    848   1.2      soda 
    849  1.26     blymn 		iostat_seek(fd->sc_dk.dk_stats);
    850   1.2      soda 		disk_busy(&fd->sc_dk);
    851   1.2      soda 
    852   1.1        ur 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
    853   1.1        ur 		return 1;
    854   1.1        ur 
    855   1.1        ur 	case DOIO:
    856  1.37   tsutsui  doio:
    857   1.1        ur 		type = fd->sc_type;
    858   1.1        ur 		sec = fd->sc_blkno % type->seccyl;
    859   1.1        ur 		nblks = type->seccyl - sec;
    860   1.1        ur 		nblks = min(nblks, fd->sc_bcount / FDC_BSIZE);
    861   1.2      soda 		nblks = min(nblks, fdc->sc_maxiosize / FDC_BSIZE);
    862   1.1        ur 		fd->sc_nblks = nblks;
    863   1.1        ur 		fd->sc_nbytes = nblks * FDC_BSIZE;
    864   1.1        ur 		head = sec / type->sectrac;
    865   1.1        ur 		sec -= head * type->sectrac;
    866   1.1        ur #ifdef DIAGNOSTIC
    867   1.2      soda 		{
    868   1.2      soda 			int block;
    869   1.2      soda 			block = (fd->sc_cylin * type->heads + head) *
    870   1.2      soda 			    type->sectrac + sec;
    871   1.2      soda 			if (block != fd->sc_blkno) {
    872  1.37   tsutsui 				printf("%s: block %d != blkno %" PRId64
    873  1.37   tsutsui 				    "\n", __func__, block, fd->sc_blkno);
    874   1.1        ur #ifdef DDB
    875   1.2      soda 				 Debugger();
    876   1.1        ur #endif
    877   1.2      soda 			}
    878   1.2      soda 		}
    879   1.1        ur #endif
    880   1.2      soda 		read = (bp->b_flags & B_READ) != 0;
    881  1.37   tsutsui 		FDCDMA_START(fdc, (uint8_t *)bp->b_data + fd->sc_skip,
    882   1.2      soda 		    fd->sc_nbytes, read);
    883   1.2      soda 		bus_space_write_1(iot, ioh, FDCTL, type->rate);
    884   1.1        ur #ifdef FD_DEBUG
    885  1.37   tsutsui 		printf("%s: %s drive %d track %d head %d sec %d nblks %d\n",
    886  1.37   tsutsui 		    __func__, read ? "read" : "write", fd->sc_drive,
    887  1.37   tsutsui 		    fd->sc_cylin, head, sec, nblks);
    888   1.1        ur #endif
    889   1.1        ur 		if (read)
    890   1.2      soda 			out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
    891   1.1        ur 		else
    892   1.2      soda 			out_fdc(iot, ioh, NE7CMD_WRITE);/* WRITE */
    893   1.2      soda 		out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
    894   1.2      soda 		out_fdc(iot, ioh, fd->sc_cylin);	/* track */
    895   1.2      soda 		out_fdc(iot, ioh, head);
    896   1.2      soda 		out_fdc(iot, ioh, sec + 1);		/* sector + 1 */
    897   1.2      soda 		out_fdc(iot, ioh, type->secsize);	/* sector size */
    898   1.2      soda 		out_fdc(iot, ioh, type->sectrac);	/* sectors/track */
    899   1.2      soda 		out_fdc(iot, ioh, type->gap1);		/* gap1 size */
    900   1.2      soda 		out_fdc(iot, ioh, type->datalen);	/* data length */
    901   1.1        ur 		fdc->sc_state = IOCOMPLETE;
    902   1.2      soda 
    903   1.2      soda 		disk_busy(&fd->sc_dk);
    904   1.2      soda 
    905   1.1        ur 		/* allow 2 seconds for operation */
    906   1.1        ur 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
    907   1.1        ur 		return 1;				/* will return later */
    908   1.1        ur 
    909   1.1        ur 	case SEEKWAIT:
    910   1.1        ur 		callout_stop(&fdc->sc_timo_ch);
    911   1.1        ur 		fdc->sc_state = SEEKCOMPLETE;
    912   1.1        ur 		/* allow 1/50 second for heads to settle */
    913   1.2      soda 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
    914   1.1        ur 		return 1;
    915   1.2      soda 
    916   1.1        ur 	case SEEKCOMPLETE:
    917   1.9       mrg 		disk_unbusy(&fd->sc_dk, 0, 0);
    918   1.2      soda 
    919   1.1        ur 		/* Make sure seek really happened. */
    920   1.2      soda 		out_fdc(iot, ioh, NE7CMD_SENSEI);
    921   1.1        ur 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
    922   1.1        ur 		    cyl != bp->b_cylinder * fd->sc_type->step) {
    923   1.1        ur #ifdef FD_DEBUG
    924  1.37   tsutsui 			fdcstatus(fd->sc_dev, 2, "seek failed");
    925   1.1        ur #endif
    926   1.1        ur 			fdcretry(fdc);
    927   1.1        ur 			goto loop;
    928   1.1        ur 		}
    929   1.1        ur 		fd->sc_cylin = bp->b_cylinder;
    930   1.1        ur 		goto doio;
    931   1.1        ur 
    932   1.1        ur 	case IOTIMEDOUT:
    933   1.2      soda 		FDCDMA_ABORT(fdc);
    934   1.1        ur 
    935   1.1        ur 	case SEEKTIMEDOUT:
    936   1.1        ur 	case RECALTIMEDOUT:
    937   1.1        ur 	case RESETTIMEDOUT:
    938   1.1        ur 		fdcretry(fdc);
    939   1.1        ur 		goto loop;
    940   1.1        ur 
    941   1.1        ur 	case IOCOMPLETE: /* IO DONE, post-analyze */
    942   1.1        ur 		callout_stop(&fdc->sc_timo_ch);
    943   1.2      soda 
    944   1.9       mrg 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
    945   1.9       mrg 		    (bp->b_flags & B_READ));
    946   1.2      soda 
    947   1.2      soda 		i = fdcresult(fdc);
    948   1.2      soda 		if (i != 7 || (st0 & 0xf8) != 0) {
    949   1.2      soda 			FDCDMA_ABORT(fdc);
    950   1.1        ur #ifdef FD_DEBUG
    951  1.37   tsutsui 			fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ?
    952   1.1        ur 			    "read failed" : "write failed");
    953  1.13   tsutsui 			printf("blkno %" PRId64 " nblks %d\n",
    954   1.1        ur 			    fd->sc_blkno, fd->sc_nblks);
    955   1.1        ur #endif
    956   1.1        ur 			fdcretry(fdc);
    957   1.1        ur 			goto loop;
    958   1.1        ur 		}
    959   1.2      soda 		FDCDMA_DONE(fdc);
    960   1.1        ur 		if (fdc->sc_errors) {
    961   1.2      soda 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
    962  1.37   tsutsui 			    fd->sc_skip / FDC_BSIZE, NULL);
    963   1.1        ur 			printf("\n");
    964   1.1        ur 			fdc->sc_errors = 0;
    965   1.1        ur 		}
    966   1.1        ur 		fd->sc_blkno += fd->sc_nblks;
    967   1.1        ur 		fd->sc_skip += fd->sc_nbytes;
    968   1.1        ur 		fd->sc_bcount -= fd->sc_nbytes;
    969   1.1        ur 		if (fd->sc_bcount > 0) {
    970   1.1        ur 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
    971   1.1        ur 			goto doseek;
    972   1.1        ur 		}
    973   1.1        ur 		fdfinish(fd, bp);
    974   1.1        ur 		goto loop;
    975   1.1        ur 
    976   1.1        ur 	case DORESET:
    977   1.1        ur 		/* try a reset, keep motor on */
    978   1.1        ur 		fd_set_motor(fdc, 1);
    979   1.1        ur 		delay(100);
    980   1.1        ur 		fd_set_motor(fdc, 0);
    981   1.1        ur 		fdc->sc_state = RESETCOMPLETE;
    982   1.1        ur 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
    983   1.1        ur 		return 1;			/* will return later */
    984   1.1        ur 
    985   1.1        ur 	case RESETCOMPLETE:
    986   1.1        ur 		callout_stop(&fdc->sc_timo_ch);
    987   1.1        ur 		/* clear the controller output buffer */
    988   1.1        ur 		for (i = 0; i < 4; i++) {
    989   1.2      soda 			out_fdc(iot, ioh, NE7CMD_SENSEI);
    990  1.37   tsutsui 			(void)fdcresult(fdc);
    991   1.1        ur 		}
    992   1.1        ur 
    993   1.1        ur 		/* fall through */
    994   1.1        ur 	case DORECAL:
    995   1.2      soda 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
    996   1.2      soda 		out_fdc(iot, ioh, fd->sc_drive);
    997   1.1        ur 		fdc->sc_state = RECALWAIT;
    998   1.1        ur 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
    999   1.1        ur 		return 1;			/* will return later */
   1000   1.1        ur 
   1001   1.1        ur 	case RECALWAIT:
   1002   1.1        ur 		callout_stop(&fdc->sc_timo_ch);
   1003   1.1        ur 		fdc->sc_state = RECALCOMPLETE;
   1004   1.1        ur 		/* allow 1/30 second for heads to settle */
   1005   1.2      soda 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
   1006   1.1        ur 		return 1;			/* will return later */
   1007   1.1        ur 
   1008   1.1        ur 	case RECALCOMPLETE:
   1009   1.2      soda 		out_fdc(iot, ioh, NE7CMD_SENSEI);
   1010   1.1        ur 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
   1011   1.1        ur #ifdef FD_DEBUG
   1012  1.37   tsutsui 			fdcstatus(fd->sc_dev, 2, "recalibrate failed");
   1013   1.1        ur #endif
   1014   1.1        ur 			fdcretry(fdc);
   1015   1.1        ur 			goto loop;
   1016   1.1        ur 		}
   1017   1.1        ur 		fd->sc_cylin = 0;
   1018   1.1        ur 		goto doseek;
   1019   1.1        ur 
   1020   1.1        ur 	case MOTORWAIT:
   1021   1.1        ur 		if (fd->sc_flags & FD_MOTOR_WAIT)
   1022   1.1        ur 			return 1;		/* time's not up yet */
   1023   1.1        ur 		goto doseek;
   1024   1.1        ur 
   1025   1.1        ur 	default:
   1026  1.37   tsutsui 		fdcstatus(fd->sc_dev, 0, "stray interrupt");
   1027   1.1        ur 		return 1;
   1028   1.1        ur 	}
   1029   1.1        ur #ifdef DIAGNOSTIC
   1030  1.37   tsutsui 	panic("%s: impossible", __func__);
   1031   1.1        ur #endif
   1032   1.1        ur #undef	st0
   1033   1.1        ur #undef	cyl
   1034   1.1        ur }
   1035   1.1        ur 
   1036   1.1        ur void
   1037  1.19   tsutsui fdcretry(struct fdc_softc *fdc)
   1038   1.1        ur {
   1039   1.1        ur 	struct fd_softc *fd;
   1040   1.1        ur 	struct buf *bp;
   1041   1.1        ur 
   1042  1.20   tsutsui 	fd = TAILQ_FIRST(&fdc->sc_drives);
   1043  1.39      yamt 	bp = bufq_peek(fd->sc_q);
   1044   1.1        ur 
   1045   1.1        ur 	switch (fdc->sc_errors) {
   1046   1.1        ur 	case 0:
   1047   1.1        ur 		/* try again */
   1048   1.2      soda 		fdc->sc_state = DOSEEK;
   1049   1.1        ur 		break;
   1050   1.1        ur 
   1051  1.37   tsutsui 	case 1:
   1052  1.37   tsutsui 	case 2:
   1053  1.37   tsutsui 	case 3:
   1054   1.1        ur 		/* didn't work; try recalibrating */
   1055   1.1        ur 		fdc->sc_state = DORECAL;
   1056   1.1        ur 		break;
   1057   1.1        ur 
   1058   1.1        ur 	case 4:
   1059   1.1        ur 		/* still no go; reset the bastard */
   1060   1.1        ur 		fdc->sc_state = DORESET;
   1061   1.1        ur 		break;
   1062   1.1        ur 
   1063   1.1        ur 	default:
   1064   1.1        ur 		diskerr(bp, "fd", "hard error", LOG_PRINTF,
   1065   1.1        ur 		    fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
   1066   1.1        ur 
   1067  1.38  christos 		fdcpstatus(7, fdc);
   1068   1.1        ur 		bp->b_error = EIO;
   1069   1.1        ur 		fdfinish(fd, bp);
   1070   1.1        ur 	}
   1071   1.1        ur 	fdc->sc_errors++;
   1072   1.1        ur }
   1073   1.1        ur 
   1074   1.1        ur int
   1075  1.28  christos fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
   1076   1.1        ur {
   1077  1.36   tsutsui 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
   1078   1.1        ur 	struct disklabel buffer;
   1079   1.1        ur 	int error;
   1080   1.1        ur 
   1081   1.1        ur 	switch (cmd) {
   1082   1.1        ur 	case DIOCGDINFO:
   1083   1.2      soda 		memset(&buffer, 0, sizeof(buffer));
   1084   1.2      soda 
   1085   1.1        ur 		buffer.d_secpercyl = fd->sc_type->seccyl;
   1086   1.1        ur 		buffer.d_type = DTYPE_FLOPPY;
   1087   1.1        ur 		buffer.d_secsize = FDC_BSIZE;
   1088   1.1        ur 
   1089   1.1        ur 		if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
   1090   1.1        ur 			return EINVAL;
   1091   1.1        ur 
   1092   1.1        ur 		*(struct disklabel *)addr = buffer;
   1093   1.1        ur 		return 0;
   1094   1.1        ur 
   1095   1.1        ur 	case DIOCWLABEL:
   1096   1.1        ur 		if ((flag & FWRITE) == 0)
   1097   1.1        ur 			return EBADF;
   1098   1.1        ur 		/* XXX do something */
   1099   1.1        ur 		return 0;
   1100   1.1        ur 
   1101   1.1        ur 	case DIOCWDINFO:
   1102   1.1        ur 		if ((flag & FWRITE) == 0)
   1103   1.1        ur 			return EBADF;
   1104   1.1        ur 
   1105   1.2      soda 		error = setdisklabel(&buffer, (struct disklabel *)addr,
   1106   1.2      soda 		    0, NULL);
   1107   1.1        ur 		if (error)
   1108   1.1        ur 			return error;
   1109   1.1        ur 
   1110   1.1        ur 		error = writedisklabel(dev, fdstrategy, &buffer, NULL);
   1111   1.1        ur 		return error;
   1112   1.1        ur 
   1113   1.1        ur 	default:
   1114   1.1        ur 		return ENOTTY;
   1115   1.1        ur 	}
   1116   1.1        ur 
   1117   1.1        ur #ifdef DIAGNOSTIC
   1118  1.37   tsutsui 	panic("%s: impossible", __func__);
   1119   1.1        ur #endif
   1120   1.2      soda }
   1121   1.2      soda 
   1122   1.2      soda /*
   1123   1.2      soda  * Mountroot hook: prompt the user to enter the root file system floppy.
   1124   1.2      soda  */
   1125   1.2      soda void
   1126  1.37   tsutsui fd_mountroot_hook(device_t dev)
   1127   1.2      soda {
   1128   1.2      soda 	int c;
   1129   1.2      soda 
   1130   1.2      soda 	printf("Insert filesystem floppy and press return.");
   1131   1.2      soda 	cnpollc(1);
   1132   1.2      soda 	for (;;) {
   1133   1.2      soda 		c = cngetc();
   1134   1.2      soda 		if ((c == '\r') || (c == '\n')) {
   1135   1.2      soda 			printf("\n");
   1136   1.2      soda 			break;
   1137   1.2      soda 		}
   1138   1.2      soda 	}
   1139   1.2      soda 	cnpollc(0);
   1140   1.1        ur }
   1141