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